Preserve originating budgets for code mode notifications (#44867)

## Why

A yielded code mode cell can emit a notification after the session switches models. Its output should retain the originating model's truncation budget instead of using the receiving model's budget.

## What changed

Carry the cell's output token limit, including the serialization allowance, through notification dispatch and attach it as `history_truncation_token_limit` metadata. Allow `inject_if_running` to accept response item envelopes so queued notifications preserve this metadata in history and rollouts.

## Testing

Add regression coverage for delayed notifications across a model switch, verifying per-cell truncation budgets, full notification text in raw events, and persisted rollout metadata. Add a task-completion test that checks notification budgets are preserved alongside queued user input.

GitOrigin-RevId: 28380a039a6d41ea324401fcf1f611d81a279477
This commit is contained in:
Abhinav
2026-09-11 17:03:21 +00:00
committed by copyberry
parent 2fc4bda3ca
commit 122d55cba8
6 changed files with 301 additions and 12 deletions
+34 -8
View File
@@ -8,10 +8,13 @@ use codex_code_mode::CodeModeNestedToolCall;
use codex_code_mode::CodeModeSessionDelegate;
use codex_code_mode::NotificationFuture;
use codex_code_mode::ToolInvocationFuture;
use codex_history::CodexHarnessMetadata;
use codex_history::ResponseItemEnvelope;
use codex_protocol::ResponseItemId;
use codex_protocol::ThreadId;
use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::ResponseItem;
use codex_utils_output_truncation::with_serialization_allowance;
use serde_json::Value as JsonValue;
use tokio::sync::oneshot;
use tokio::sync::watch;
@@ -138,6 +141,7 @@ impl CodeModeDispatchBroker {
call_id,
cell_id,
text,
output_token_limit,
cancellation_token,
response_tx,
} => {
@@ -148,7 +152,8 @@ impl CodeModeDispatchBroker {
)
.await
{
host.notify(call_id, cell_id, text).await
host.notify(call_id, cell_id, text, output_token_limit)
.await
} else {
remove_dispatch_gate(&dispatch_gates, &cell_id);
Err("code mode notification cancelled".to_string())
@@ -370,6 +375,14 @@ impl CodeModeSessionDelegate for CodeModeCellDelegate {
call_id,
cell_id,
text,
output_token_limit: with_serialization_allowance(
self.step_context
.settings
.model_info
.truncation_policy
.into(),
)
.token_budget(),
cancellation_token: cancellation_token.clone(),
response_tx,
})
@@ -403,6 +416,7 @@ enum DispatchMessage {
call_id: String,
cell_id: CellId,
text: String,
output_token_limit: usize,
cancellation_token: CancellationToken,
response_tx: oneshot::Sender<Result<(), String>>,
},
@@ -445,17 +459,29 @@ impl CoreTurnHost {
async move { invocation?.await.map_err(|error| error.to_string()) }
}
async fn notify(&self, call_id: String, cell_id: CellId, text: String) -> Result<(), String> {
async fn notify(
&self,
call_id: String,
cell_id: CellId,
text: String,
output_token_limit: usize,
) -> Result<(), String> {
if text.trim().is_empty() {
return Ok(());
}
self.session
.inject_if_running(vec![ResponseItem::CustomToolCallOutput {
id: None,
call_id,
name: Some(PUBLIC_TOOL_NAME.to_string()),
output: FunctionCallOutputPayload::from_text(text),
internal_chat_message_metadata_passthrough: None,
.inject_if_running(vec![ResponseItemEnvelope {
item: ResponseItem::CustomToolCallOutput {
id: None,
call_id,
name: Some(PUBLIC_TOOL_NAME.to_string()),
output: FunctionCallOutputPayload::from_text(text),
internal_chat_message_metadata_passthrough: None,
},
metadata: Some(CodexHarnessMetadata {
history_truncation_token_limit: Some(output_token_limit),
..Default::default()
}),
}])
.await
.map_err(|_| {