Exclude per-item usage attribution by default (#63219)

## What changed

Add `usage.attribution` to the `x-codex-exclude` header for Responses HTTP
requests and WebSocket handshakes by default. Threads started with
`experimentalRawEvents` keep receiving per-item attribution in raw responses.
Subagents inherit the policy, and the app server restores it when a thread
resumes with an existing raw-event subscription.

## Testing

Cover the default exclusion, raw completion events with upstream usage,
subagent inheritance, and resuming a closed subagent after both its runtime
and its parent's runtime have unloaded.

GitOrigin-RevId: 0dad92f97606300907f1294f09dd7845d1e6476f
This commit is contained in:
Charlie Marsh
2026-11-23 01:06:41 +00:00
committed by copyberry
parent 9e3479e00a
commit ae6c0c8ac1
13 changed files with 237 additions and 19 deletions
+35
View File
@@ -13,6 +13,11 @@ use codex_app_server_protocol::TurnError;
use codex_app_server_protocol::TurnItemsView;
use codex_core::CodexThread;
use codex_core::ThreadConfigSnapshot;
use codex_core::UsageAttributionPolicy;
use codex_core::config::Config;
use codex_extension_api::ExtensionFuture;
use codex_extension_api::ThreadLifecycleContributor;
use codex_extension_api::ThreadStartInput;
use codex_file_watcher::WatchRegistration;
use codex_protocol::ThreadId;
#[cfg(test)]
@@ -499,6 +504,25 @@ impl ListenerStateCandidate {
}
}
impl ThreadLifecycleContributor<Config> for ThreadStateManager {
/// Restore attribution for raw-event subscriptions that outlive a Core runtime.
fn on_thread_start<'a>(
&'a self,
input: ThreadStartInput<'a, Config>,
) -> ExtensionFuture<'a, ()> {
Box::pin(async move {
let thread_id = ThreadId::from_string(input.thread_store.level_id()).ok();
let parent_id = input.session_source.parent_thread_id();
for id in [thread_id, parent_id].into_iter().flatten() {
if self.raw_events_enabled(id).await {
input.thread_store.insert(UsageAttributionPolicy::Retain);
break;
}
}
})
}
}
impl ThreadStateManager {
/// Coordinates goal edits with cold/path-based resume for just this thread.
pub(crate) async fn lock_goal_resume(
@@ -589,6 +613,17 @@ impl ThreadStateManager {
state.threads.entry(thread_id).or_default().state.clone()
}
pub(crate) async fn raw_events_enabled(&self, thread_id: ThreadId) -> bool {
let thread_state = {
let state = self.state.lock().await;
let Some(entry) = state.threads.get(&thread_id) else {
return false;
};
Arc::clone(&entry.state)
};
thread_state.lock().await.experimental_raw_events
}
pub(crate) async fn created_listener_state(
&self,
thread_id: ThreadId,