Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 12 additions & 38 deletions codex-rs/core/src/tools/executed_tool_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use codex_protocol::models::ExecutedToolCallArguments;
use codex_protocol::models::ResponseItem;
use codex_protocol::models::ToolResultMetadata;
use codex_protocol::models::executed_tool_call_metadata_bytes;
use codex_protocol::models::normalize_executed_tool_call_arguments;
use codex_protocol::models::normalize_executed_tool_call_completeness;
use codex_protocol::openai_models::ToolMode;
use indexmap::IndexMap;
use serde_json::Value as JsonValue;
Expand All @@ -46,8 +46,6 @@ mod seen_ids;

use seen_ids::SeenIds;

const MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES: usize = 8 * 1024;
const MAX_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT: usize = 32 * 1024;
const MAX_PENDING_EXECUTED_TOOL_CALLS: usize = 256;
// Limits new Direct metadata retained in history and the append-only rollout
// during one recorder lifetime. A new process cannot recover the exact prior
Expand Down Expand Up @@ -150,7 +148,6 @@ enum CellCompletion {
#[derive(Default)]
struct RecordedCell {
pending_calls: IndexMap<String, ExecutedToolCall>,
pending_full_argument_bytes: usize,
completion: CellCompletion,
originating_call_id: Option<String>,
truncated_metadata_binding_valid: bool,
Expand Down Expand Up @@ -365,17 +362,7 @@ impl ExecutedToolCalls {
return None;
}
let permit = self.reserve_direct_call()?;
let (mut recorded, _) = recorded_call(call);
let argument_bytes = serialized_json_bytes(recorded.arguments()).unwrap_or(usize::MAX);
if matches!(recorded.arguments(), ExecutedToolCallArguments::Raw(_))
&& argument_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES
{
recorded = ExecutedToolCall::truncated(
recorded.name,
argument_bytes,
MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES,
);
}
let (recorded, _) = recorded_call(call);
Some((recorded, permit))
}

Expand Down Expand Up @@ -561,19 +548,10 @@ impl ExecutedToolCalls {
if !unique_nested_id || duplicate_call_id {
cell.truncated_metadata_binding_valid = false;
}
let max_bytes = MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES.min(
MAX_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT
.saturating_sub(cell.pending_full_argument_bytes),
);
let call = if at_pending_call_limit {
ExecutedToolCall::truncated(call.name, original_bytes, /*max_bytes*/ 0)
} else if original_bytes <= max_bytes {
cell.pending_full_argument_bytes = cell
.pending_full_argument_bytes
.saturating_add(original_bytes);
call
} else {
ExecutedToolCall::truncated(call.name, original_bytes, max_bytes)
call
};
let truncated = matches!(
call.arguments(),
Expand Down Expand Up @@ -748,20 +726,16 @@ fn recorded_call(call: &ToolCall) -> (ExecutedToolCall, usize) {
}
};
let name = codex_tools::code_mode_name_for_tool_name(&call.tool_name);
let recorded_call = if original_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES {
ExecutedToolCall::truncated(name, original_bytes, MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES)
} else {
let arguments = match &call.payload {
ToolPayload::Function { arguments } => serde_json::from_str(arguments)
.unwrap_or_else(|_| JsonValue::String(arguments.clone())),
ToolPayload::Custom { input } => JsonValue::String(input.clone()),
ToolPayload::ToolSearch { arguments } => {
serde_json::to_value(arguments).unwrap_or_default()
}
};
ExecutedToolCall::new(name, arguments)
let arguments = match &call.payload {
ToolPayload::Function { arguments } => {
serde_json::from_str(arguments).unwrap_or_else(|_| JsonValue::String(arguments.clone()))
}
ToolPayload::Custom { input } => JsonValue::String(input.clone()),
ToolPayload::ToolSearch { arguments } => {
serde_json::to_value(arguments).unwrap_or_default()
}
};
(recorded_call, original_bytes)
(ExecutedToolCall::new(name, arguments), original_bytes)
}

fn input_call_id(item: &ResponseItem) -> Option<&str> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ExecutedToolCalls {

// Argument truncation preserves inventory completeness; omitted calls or
// names still invalidate completion across the cell's outputs.
normalize_executed_tool_call_arguments(items);
normalize_executed_tool_call_completeness(items);
}

fn attach_pending_to_prompt_with_state(
Expand Down Expand Up @@ -258,7 +258,6 @@ impl ExecutedToolCalls {
}
calls.push(call);
}
cell.pending_full_argument_bytes = 0;
complete = cell.completion == CellCompletion::Complete
&& (state.can_prove_wait_completion
|| matches!(item, ResponseItem::CustomToolCallOutput { .. }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,11 @@ fn tool_call_completeness_survives_waits_without_changing_deltas() {
if index < 2 {
// Identical calls are distinct submissions; truncation stays sticky across waits.
let original_bytes = if truncated && index == 1 { 9_000 } else { 2 };
let call = if original_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES {
let call = if truncated && index == 1 {
ExecutedToolCall::truncated(
"nested_tool".to_string(),
original_bytes,
MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES,
/*max_bytes*/ 8_192,
)
} else {
ExecutedToolCall::new("nested_tool".to_string(), json!({}))
Expand Down Expand Up @@ -1156,40 +1156,33 @@ fn completeness_rejects_direct_id_collision_and_late_calls() {
}

#[test]
fn newly_normalized_arguments_preserve_later_wait_completeness() {
fn large_wrapped_arguments_preserve_later_wait_completeness() {
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("active-cell".to_string());
recorder.start_cell(&cell, "exec");
let arguments = json!({
"_codex_executed_tool_call_truncated": true,
"padding": "x".repeat(8_120),
})
.to_string();
"padding": "x".repeat(40 * 1024),
});
let (call, original_bytes) = recorded_call(&ToolCall {
tool_name: codex_tools::ToolName::plain("nested_tool"),
call_id: "nested".to_string(),
payload: ToolPayload::Function { arguments },
payload: ToolPayload::Function {
arguments: arguments.to_string(),
},
encrypted_function_args: None,
});
assert!(original_bytes <= MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
assert!(
serialized_json_bytes(call.arguments()).unwrap() > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES
);
recorder.record_nested_tool_call(cell.clone(), "nested".to_string(), call, original_bytes);
let mut initial = [exec_input("exec"), exec_output("exec")];
recorder.attach_to_prompt(&mut initial, &mut HashMap::new());
assert!(
assert_eq!(
initial[1]
.executed_tool_call_metadata()
.unwrap()
.executed_tool_calls
.as_ref()
.unwrap()
.iter()
.any(|call| matches!(
call.arguments(),
ExecutedToolCallArguments::Truncated { .. }
))
.unwrap(),
&vec![ExecutedToolCall::new("nested_tool".to_string(), arguments)],
);
recorder.register_cell(&cell, "wait");
recorder.finish_cell_recording(&cell);
Expand Down Expand Up @@ -1612,13 +1605,12 @@ fn late_truncated_metadata_preserves_large_result_on_retry() {
}

#[test]
fn late_truncated_metadata_is_not_reused_after_budget_changes_calls() {
fn late_truncated_metadata_with_wrapped_arguments_rejects_ambiguous_outputs() {
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("late-truncated-cell".to_string());
recorder.start_cell(&cell, "exec");
record_truncated_call(&recorder, &cell, "nested");
// The reserved key requires a wrapper in recorded arguments. The wrapper can
// put an otherwise accepted function argument over the later prompt limit.
// The reserved key requires a wrapper in recorded arguments.
let arguments = json!({
"_codex_executed_tool_call_truncated": true,
"padding": "x".repeat(8_120),
Expand All @@ -1630,11 +1622,6 @@ fn late_truncated_metadata_is_not_reused_after_budget_changes_calls() {
payload: ToolPayload::Function { arguments },
encrypted_function_args: None,
});
assert!(original_bytes <= MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
assert!(
serialized_json_bytes(wrapped_call.arguments()).unwrap()
> MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES
);
recorder.record_nested_tool_call(
cell.clone(),
"other".to_string(),
Expand Down
14 changes: 5 additions & 9 deletions codex-rs/core/src/tools/executed_tool_calls_direct_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,20 @@ async fn direct_pending_limit_releases_on_completion_or_dropped_future() {
}

#[tokio::test]
async fn direct_budget_counts_the_encoded_argument_before_it_enters_history() {
async fn direct_recording_preserves_encoded_arguments_in_history() {
let (_, turn) = crate::session::tests::make_session_and_context().await;
let step = StepContext::for_test(Arc::new(turn));
let mut features = Features::default();
features.enable(Feature::ExecutedToolCallMetadata);
let recorder = ExecutedToolCalls::new(&features, &InitialHistory::New);
let invalid_json = "\"".repeat(5_000);
let wire_bytes = serialized_json_bytes(&JsonValue::String(invalid_json.clone())).unwrap();
assert!(invalid_json.len() < MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
assert!(wire_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
assert!(wire_bytes > 8 * 1024);
let call = ToolCall {
tool_name: codex_tools::ToolName::plain("test_tool"),
call_id: "direct".to_string(),
payload: ToolPayload::Function {
arguments: invalid_json,
arguments: invalid_json.clone(),
},
encrypted_function_args: None,
};
Expand All @@ -273,10 +272,7 @@ async fn direct_budget_counts_the_encoded_argument_before_it_enters_history() {
assert_eq!(wire["output"], ordinary_output);
let metadata = &wire["internal_chat_message_metadata_passthrough"];
let arguments = &metadata["executed_tool_calls"][0]["arguments"];
assert!(serialized_json_bytes(arguments).unwrap() < MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
assert_eq!(
arguments["_codex_executed_tool_call_truncated"]["original_bytes"],
wire_bytes,
);
assert_eq!(arguments, &JsonValue::String(invalid_json));
assert_eq!(serialized_json_bytes(arguments).unwrap(), wire_bytes);
assert_eq!(metadata["tool_calls_complete"], true);
}
51 changes: 27 additions & 24 deletions codex-rs/core/tests/suite/code_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ async fn code_mode_tool_call_completeness_is_private_and_opt_in(
let code = if oversized {
r#"
const args = { barrier: { id: "", participants: 1 } };
args.barrier.id = "x".repeat(8192 - JSON.stringify(args).length);
args.barrier.id = "x".repeat(9000 - JSON.stringify(args).length);
for (let index = 0; index < 17; index++) await tools.test_sync_tool(args);
text("done");
yield_control();
Expand Down Expand Up @@ -1577,12 +1577,17 @@ await new Promise(() => {});
let calls = serde_json::json!([{ "name": "test_sync_tool", "arguments": {} }]);
let complete = Value::Bool(true);
if oversized {
assert!(
metadata["executed_tool_calls"]
.as_array()
.is_some_and(|calls| calls.iter().any(|call| call["arguments"]
.get("_codex_executed_tool_call_truncated")
.is_some()))
let mut arguments = serde_json::json!({ "barrier": { "id": "", "participants": 1 } });
arguments["barrier"]["id"] =
serde_json::json!("x".repeat(9000 - arguments.to_string().len()));
assert_eq!(
metadata["executed_tool_calls"],
serde_json::json!(vec![
serde_json::json!({
"name": "test_sync_tool", "arguments": arguments,
});
17
]),
);
} else {
assert_eq!(
Expand Down Expand Up @@ -2976,7 +2981,7 @@ async fn code_mode_result_metadata_keeps_prepared_call_binding_across_runtime_re
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()> {
async fn code_mode_late_result_metadata_with_large_arguments_survives_waits() -> Result<()> {
skip_if_no_network!(Ok(()));
let server = responses::start_mock_server().await;
let metadata = serde_json::json!({
Expand Down Expand Up @@ -3028,7 +3033,7 @@ async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()>
.context("first call did not reach the result gate")??;

// The call may be dispatched after the first yield. Wait while its result is held so
// the request has already recorded the truncated call before accepting its metadata.
// the request has already recorded the call before accepting its metadata.
let held = responses::mount_function_call_agent_response(
&server,
"call-2",
Expand All @@ -3044,16 +3049,16 @@ async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()>
let original_output = held_calls[0];
let original_id = original_output["call_id"].as_str().unwrap().to_string();
let original_type = original_output["type"].as_str().unwrap().to_string();
let truncated =
let recorded =
&original_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0];
assert!(
truncated["name"]
recorded["name"]
.as_str()
.unwrap()
.ends_with(RESULT_METADATA_TOOL)
);
assert!(truncated["arguments"]["_codex_executed_tool_call_truncated"].is_object());
assert!(truncated.get("tool_result_metadata").is_none());
assert_eq!(recorded["arguments"], arguments);
assert!(recorded.get("tool_result_metadata").is_none());

release_tx.send(()).unwrap();
let resumed = responses::mount_function_call_agent_response(
Expand Down Expand Up @@ -3096,7 +3101,7 @@ async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()>
let captured_calls =
&captured_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"];
assert_eq!(captured_calls.as_array().unwrap().len(), 1);
assert_eq!(captured_calls[0]["arguments"], truncated["arguments"]);
assert_eq!(captured_calls[0]["arguments"], recorded["arguments"]);
assert_eq!(captured_calls[0]["tool_result_metadata"], metadata);
assert_ne!(
captured_output["internal_chat_message_metadata_passthrough"]["tool_calls_complete"],
Expand Down Expand Up @@ -3124,7 +3129,7 @@ async fn code_mode_late_truncated_result_metadata_survives_waits() -> Result<()>
let original = terminal_request.call_output(&original_id, &original_type);
assert_eq!(
original["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]["arguments"],
truncated["arguments"]
recorded["arguments"]
);
assert!(
original["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]
Expand Down Expand Up @@ -3548,7 +3553,7 @@ text("pressure ready");"#,
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn code_mode_complete_call_survives_unrelated_truncation() -> Result<()> {
async fn code_mode_complete_calls_preserve_large_arguments() -> Result<()> {
skip_if_no_network!(Ok(()));

let server = responses::start_mock_server().await;
Expand Down Expand Up @@ -3589,12 +3594,11 @@ async fn code_mode_complete_call_survives_unrelated_truncation() -> Result<()> {
let request = follow_up.single_request();
let overflow =
&request.custom_tool_call_output("call-1")["internal_chat_message_metadata_passthrough"];
assert!(
overflow["executed_tool_calls"]
.as_array()
.is_some_and(|calls| calls.iter().any(|call| call["arguments"]
.get("_codex_executed_tool_call_truncated")
.is_some()))
assert_eq!(
overflow["executed_tool_calls"],
serde_json::json!([{ "name": "test_sync_tool", "arguments": {
"barrier": { "id": "x".repeat(8192), "participants": 1 }
} }]),
);
assert_eq!(overflow["tool_calls_complete"], true);

Expand Down Expand Up @@ -9278,8 +9282,7 @@ async fn code_mode_argument_truncation_preserves_later_wait_completeness() -> Re
probe.codex.shutdown_and_wait().await?;
probe_server.shutdown().await;

// One 7 KiB invocation stays under the recorder's per-output argument
// budget. It pushes the yielded delta over the message budget only.
// One 7 KiB invocation pushes the yielded delta over the message budget.
let padding = "x".repeat(LIMIT - base_bytes - 4 * 1024);
let code = r#"
await tools.test_sync_tool({ barrier: { id: "x".repeat(7000), participants: 1 } });
Expand Down
Loading
Loading