mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Honor thread analytics opt-outs when using shared clients (#44646)
## Why An enabled shared analytics client could override a thread's explicit opt-out. Delegated threads also emitted initialization events through the parent's client, bypassing the child's analytics setting. ## What changed - Use a disabled analytics client when `config.analytics_enabled` is `Some(false)`, without disabling analytics for sibling threads or overriding a disabled host client. - Emit delegated thread initialization events through the child's analytics client. - Expose the effective analytics state through `CodexThread::analytics_enabled()`. ## Testing Add regression tests for explicit and unset thread settings with enabled, disabled, and absent shared clients, plus delegated child opt-outs with an enabled parent. Update compaction and rollout-budget rollback tests to wait for thread idle after turn completion and fail immediately on rollback errors. GitOrigin-RevId: 547852909af1a6822b95c0bcd2336b0d6bc7aad1
This commit is contained in:
1 parent
e25bedc166
commit
60825b4988
8 files changed
+295
-19
No files matched your search
@@ -53,6 +53,133 @@ use wiremock::MockServer;
|
||||
|
||||
const TEST_INSTALLATION_ID: &str = "11111111-1111-4111-8111-111111111111";
|
||||
|
||||
/// A thread opt-out wins over a shared client without disabling its siblings.
|
||||
#[tokio::test]
|
||||
async fn thread_analytics_opt_out_overrides_shared_client() {
|
||||
let server = MockServer::start().await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("POST"))
|
||||
.and(wiremock::matchers::path("/codex/analytics-events/events"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.mount(&server)
|
||||
.await;
|
||||
let temp_dir = tempdir().expect("tempdir");
|
||||
let mut config = test_config().await;
|
||||
config.chatgpt_base_url = server.uri();
|
||||
config.model_provider.base_url = Some(server.uri());
|
||||
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
|
||||
let shared_client = AnalyticsEventsClient::new(
|
||||
AuthManager::from_auth_for_testing(auth.clone()),
|
||||
server.uri(),
|
||||
/*analytics_enabled*/ Some(true),
|
||||
);
|
||||
let mut expected_thread_ids = Vec::new();
|
||||
let mut opted_out_thread_ids = Vec::new();
|
||||
|
||||
for (name, client_override, expected_enabled) in [
|
||||
(
|
||||
"enabled_override",
|
||||
Some(shared_client.clone()),
|
||||
[false, true, true],
|
||||
),
|
||||
(
|
||||
"disabled_override",
|
||||
Some(AnalyticsEventsClient::disabled()),
|
||||
[false, false, false],
|
||||
),
|
||||
("no_override", None, [false, true, true]),
|
||||
] {
|
||||
config.codex_home = temp_dir.path().join(name).abs();
|
||||
config.cwd = config.codex_home.abs();
|
||||
std::fs::create_dir_all(&config.codex_home).expect("create codex home");
|
||||
let mut manager = ThreadManager::with_models_provider_and_home_for_tests(
|
||||
auth.clone(),
|
||||
config.model_provider.clone(),
|
||||
config.codex_home.to_path_buf(),
|
||||
Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()),
|
||||
);
|
||||
Arc::get_mut(&mut manager.state)
|
||||
.expect("unshared thread manager state")
|
||||
.analytics_events_client = client_override;
|
||||
|
||||
for (setting, enabled) in [Some(false), Some(true), None]
|
||||
.into_iter()
|
||||
.zip(expected_enabled)
|
||||
{
|
||||
config.analytics_enabled = setting;
|
||||
let started = manager
|
||||
.start_thread(StartThreadOptions::new(config.clone()))
|
||||
.await
|
||||
.expect("start analytics test thread");
|
||||
let services = &started.thread.session.services;
|
||||
assert_eq!(started.thread.analytics_enabled(), enabled);
|
||||
assert_eq!(
|
||||
services
|
||||
.session_extension_data
|
||||
.get::<AnalyticsEventsClient>()
|
||||
.expect("analytics client in session store")
|
||||
.is_enabled(),
|
||||
enabled,
|
||||
);
|
||||
let thread_id = started.thread_id.to_string();
|
||||
if enabled {
|
||||
expected_thread_ids.push(thread_id.clone());
|
||||
} else {
|
||||
opted_out_thread_ids.push(thread_id.clone());
|
||||
}
|
||||
services.analytics_events_client.track_app_used(
|
||||
codex_analytics::TrackEventsContext {
|
||||
model_slug: "test-model".to_string(),
|
||||
turn_id: format!("test-turn-{thread_id}"),
|
||||
thread_id,
|
||||
product_client_id: "codex_work_cca".to_string(),
|
||||
},
|
||||
codex_analytics::AppInvocation {
|
||||
connector_id: Some("test-connector".to_string()),
|
||||
app_name: None,
|
||||
invocation_type: None,
|
||||
},
|
||||
);
|
||||
services.analytics_events_client.flush().await;
|
||||
}
|
||||
let shutdown = manager
|
||||
.shutdown_all_threads_bounded(Duration::from_secs(10))
|
||||
.await;
|
||||
assert_eq!(shutdown.completed.len(), 3);
|
||||
}
|
||||
|
||||
let events: Vec<serde_json::Value> = server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("analytics requests")
|
||||
.into_iter()
|
||||
.filter(|request| request.url.path() == "/codex/analytics-events/events")
|
||||
.flat_map(|request| {
|
||||
request.body_json::<serde_json::Value>().expect("JSON body")["events"]
|
||||
.as_array()
|
||||
.expect("events array")
|
||||
.clone()
|
||||
})
|
||||
.collect();
|
||||
assert!(events.iter().all(|event| {
|
||||
!opted_out_thread_ids
|
||||
.iter()
|
||||
.any(|thread_id| event["event_params"]["thread_id"] == thread_id.as_str())
|
||||
}));
|
||||
let mut actual_thread_ids: Vec<String> = events
|
||||
.iter()
|
||||
.filter(|event| event["event_type"] == "codex_app_used")
|
||||
.map(|event| {
|
||||
event["event_params"]["thread_id"]
|
||||
.as_str()
|
||||
.expect("app usage thread ID")
|
||||
.to_string()
|
||||
})
|
||||
.collect();
|
||||
actual_thread_ids.sort();
|
||||
expected_thread_ids.sort();
|
||||
assert_eq!(actual_thread_ids, expected_thread_ids);
|
||||
}
|
||||
|
||||
/// Controls without a custom allocation policy still produce distinct thread identifiers.
|
||||
#[test]
|
||||
fn thread_id_generator_defaults_to_standard_ids() {
|
||||
|
||||
Reference in new issue
Block a user