mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Mark finished empty Code Mode tool inventories as complete (#46081)
## Why Code Mode cells that finish without invoking tools omit completeness metadata, leaving a verified empty inventory indistinguishable from an unverified one. ## What changed - Retain completed, losslessly recorded cells even when they contain no tool calls. - Always include `executed_tool_calls` when setting `tool_calls_complete`, using `[]` for empty inventories or terminal waits with no new calls. - Preserve completeness across request retries while revalidating history and rejecting late calls or ambiguous output mappings. ## Testing Add regression coverage for empty tool discovery, errors, termination, feature gating, retries, and fresh-session requirements for waits. Update protocol assertions to require an explicit empty call list with completeness metadata. GitOrigin-RevId: 24affe47f46068b6559d04a0103958ae83a00b92
This commit is contained in:
1 parent
1e9564fb85
commit
8452164c76
5 files changed
+210
-15
No files matched your search
@@ -107,7 +107,6 @@ struct RetainedToolCalls {
|
||||
enum CellCompletion {
|
||||
#[default]
|
||||
Unobserved,
|
||||
Started,
|
||||
Recording,
|
||||
Incomplete,
|
||||
Complete,
|
||||
@@ -422,10 +421,8 @@ impl ExecutedToolCalls {
|
||||
} else {
|
||||
ExecutedToolCall::truncated(call.name, original_bytes, max_bytes)
|
||||
};
|
||||
cell.completion = if matches!(
|
||||
cell.completion,
|
||||
CellCompletion::Started | CellCompletion::Recording
|
||||
) && !duplicate_call_id
|
||||
cell.completion = if cell.completion == CellCompletion::Recording
|
||||
&& !duplicate_call_id
|
||||
&& !matches!(
|
||||
call.arguments(),
|
||||
ExecutedToolCallArguments::Truncated { .. }
|
||||
@@ -502,7 +499,7 @@ impl ExecutedToolCalls {
|
||||
if let Some(cell) = state.cells.get_mut(cell_id) {
|
||||
// Failed indexing must not make a known historical ID look fresh.
|
||||
cell.completion = if unique_cell && unique_origin && history_ids_indexed {
|
||||
CellCompletion::Started
|
||||
CellCompletion::Recording
|
||||
} else {
|
||||
CellCompletion::Incomplete
|
||||
};
|
||||
@@ -515,10 +512,18 @@ impl ExecutedToolCalls {
|
||||
return;
|
||||
};
|
||||
if let Some(cell) = state.cells.get_mut(cell_id) {
|
||||
if cell.completion == CellCompletion::Recording {
|
||||
cell.completion = CellCompletion::Complete;
|
||||
} else if cell.completion != CellCompletion::Complete && cell.pending_calls.is_empty() {
|
||||
state.cells.remove(cell_id);
|
||||
match cell.completion {
|
||||
CellCompletion::Recording => {
|
||||
// The closed dispatch gate makes this lossless inventory final, even if empty.
|
||||
cell.completion = CellCompletion::Complete;
|
||||
}
|
||||
CellCompletion::Unobserved | CellCompletion::Incomplete => {
|
||||
// Drop unverified cells only after emitting any partial records.
|
||||
if cell.pending_calls.is_empty() {
|
||||
state.cells.remove(cell_id);
|
||||
}
|
||||
}
|
||||
CellCompletion::Complete => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -631,7 +631,7 @@ fn executed_tool_call_recorder_bounds_retained_history_and_keeps_latest_calls()
|
||||
|
||||
#[test]
|
||||
fn tool_call_completeness_requires_finished_lossless_recording() {
|
||||
for scenario in ["empty", "unobserved", "late_call"] {
|
||||
for scenario in ["unfinished", "unobserved", "late_call"] {
|
||||
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
|
||||
let cell_id = CellId::new(scenario.to_string());
|
||||
if scenario == "unobserved" {
|
||||
@@ -642,7 +642,7 @@ fn tool_call_completeness_requires_finished_lossless_recording() {
|
||||
if scenario == "late_call" {
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
}
|
||||
if scenario != "empty" {
|
||||
if scenario != "unfinished" {
|
||||
recorder.record_nested_tool_call(
|
||||
cell_id.clone(),
|
||||
"nested-call".to_string(),
|
||||
@@ -650,7 +650,9 @@ fn tool_call_completeness_requires_finished_lossless_recording() {
|
||||
/*original_bytes*/ 2,
|
||||
);
|
||||
}
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
if scenario != "unfinished" {
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
}
|
||||
|
||||
let mut items = [exec_input("output"), exec_output("output")];
|
||||
recorder.attach_pending_to_prompt(&mut items, &mut HashMap::new());
|
||||
@@ -664,6 +666,82 @@ fn tool_call_completeness_requires_finished_lossless_recording() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_empty_tool_inventory_survives_request_retries() {
|
||||
let recorder = new_recorder(InitialHistory::New);
|
||||
let cell_id = CellId::new("empty-cell".to_string());
|
||||
recorder.start_cell(&cell_id, "output");
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
let mut retry_cache = HashMap::new();
|
||||
for _ in 0..2 {
|
||||
let mut items = [exec_input("output"), exec_output("output")];
|
||||
assert!(recorder.attach_pending_to_prompt(&mut items, &mut retry_cache));
|
||||
assert!(!has_direct_call_metadata(&items[1]));
|
||||
assert_eq!(
|
||||
serde_json::to_value(items[1].executed_tool_call_metadata()).unwrap(),
|
||||
json!({
|
||||
"executed_tool_calls": [],
|
||||
"tool_calls_complete": true,
|
||||
"cell_id": "output",
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_inventory_revalidates_history_on_retry() {
|
||||
for scenario in ["wrong_input", "duplicate_output", "late_call"] {
|
||||
let recorder = new_recorder(InitialHistory::New);
|
||||
let cell = CellId::new("empty-cell".to_string());
|
||||
recorder.start_cell(&cell, "exec");
|
||||
recorder.finish_cell_recording(&cell);
|
||||
let mut retry_cache = HashMap::new();
|
||||
let mut prompt = vec![exec_input("exec"), exec_output("exec")];
|
||||
recorder.attach_to_prompt(&mut prompt, &mut retry_cache);
|
||||
assert_eq!(tool_calls_complete(&prompt[1]), Some(true));
|
||||
match scenario {
|
||||
"wrong_input" => prompt[0] = wait_input("exec", &cell),
|
||||
"duplicate_output" => prompt.push(prompt[1].clone()),
|
||||
"late_call" => {
|
||||
record_nested_call(&recorder, &cell, "late");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
recorder.attach_to_prompt(&mut prompt, &mut retry_cache);
|
||||
assert_eq!(tool_calls_complete(&prompt[1]), None, "{scenario}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_inventory_wait_requires_a_fresh_session() {
|
||||
for fresh in [true, false] {
|
||||
let history = if fresh {
|
||||
InitialHistory::New
|
||||
} else {
|
||||
InitialHistory::Forked(Vec::new())
|
||||
};
|
||||
let recorder = new_recorder(history);
|
||||
let cell = CellId::new("empty-cell".to_string());
|
||||
recorder.start_cell(&cell, "exec");
|
||||
let mut prompt = vec![exec_input("exec"), exec_output("exec")];
|
||||
let mut retry_cache = HashMap::new();
|
||||
recorder.attach_to_prompt(&mut prompt, &mut retry_cache);
|
||||
assert!(prompt[1].executed_tool_call_metadata().is_none());
|
||||
recorder.register_cell(&cell, "wait");
|
||||
recorder.finish_cell_recording(&cell);
|
||||
prompt.extend([wait_input("wait", &cell), output("wait")]);
|
||||
recorder.attach_to_prompt(&mut prompt, &mut retry_cache);
|
||||
assert!(!has_direct_call_metadata(&prompt[3]));
|
||||
assert_eq!(tool_calls_complete(&prompt[3]), fresh.then_some(true));
|
||||
if fresh {
|
||||
assert_eq!(
|
||||
serde_json::to_value(prompt[3].executed_tool_call_metadata()).unwrap(),
|
||||
json!({"cell_id": "exec", "executed_tool_calls": [], "tool_calls_complete": true}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_call_completeness_survives_waits_without_changing_deltas() {
|
||||
let metadata = json!({ "provider": { "ids": ["CLATE"] } });
|
||||
|
||||
@@ -1217,6 +1217,111 @@ async fn mcp_code_mode_exclusion_does_not_change_direct_mode_tool_exposure() ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[test_case(true; "enabled")]
|
||||
#[test_case(false; "disabled")]
|
||||
async fn code_mode_finished_discovery_has_empty_tool_inventory(
|
||||
metadata_enabled: bool,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = responses::start_mock_server().await;
|
||||
let (_test, follow_up) = run_code_mode_turn_with_config(
|
||||
&server,
|
||||
"Discover tools without calling any",
|
||||
r#"text(ALL_TOOLS.filter(({ name }) => name === "test_sync_tool").map(({ name }) => name));"#,
|
||||
move |config| {
|
||||
if !metadata_enabled {
|
||||
config.features.disable(Feature::ExecutedToolCallMetadata).unwrap();
|
||||
}
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
let request = follow_up.single_request();
|
||||
let output = request.custom_tool_call_output("call-1");
|
||||
let (body, success) = custom_tool_output_body_and_success(&request, "call-1");
|
||||
assert_ne!(success, Some(false), "Code Mode failed: {body}");
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&body)?,
|
||||
serde_json::json!(["test_sync_tool"])
|
||||
);
|
||||
let metadata = &output["internal_chat_message_metadata_passthrough"];
|
||||
let calls = serde_json::json!([]);
|
||||
let complete = Value::Bool(true);
|
||||
let cell_id = serde_json::json!("call-1");
|
||||
assert_eq!(
|
||||
(
|
||||
metadata.get("executed_tool_calls"),
|
||||
metadata.get("tool_calls_complete"),
|
||||
metadata.get("cell_id"),
|
||||
),
|
||||
(
|
||||
metadata_enabled.then_some(&calls),
|
||||
metadata_enabled.then_some(&complete),
|
||||
metadata_enabled.then_some(&cell_id),
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn code_mode_empty_error_has_complete_tool_inventory() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = responses::start_mock_server().await;
|
||||
let (_test, follow_up) = run_code_mode_turn_with_config(
|
||||
&server,
|
||||
"Fail without calling a tool",
|
||||
r#"throw new Error("empty-cell-error");"#,
|
||||
|_| {},
|
||||
)
|
||||
.await?;
|
||||
let request = follow_up.single_request();
|
||||
let (body, _) = custom_tool_output_body_and_success(&request, "call-1");
|
||||
assert!(body.contains("Error: empty-cell-error"));
|
||||
let output = request.custom_tool_call_output("call-1");
|
||||
let metadata = &output["internal_chat_message_metadata_passthrough"];
|
||||
assert_eq!(metadata["cell_id"], "call-1");
|
||||
assert_eq!(metadata["executed_tool_calls"], serde_json::json!([]));
|
||||
assert_eq!(metadata["tool_calls_complete"], true);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn code_mode_terminated_empty_cell_has_complete_tool_inventory() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = responses::start_mock_server().await;
|
||||
let (test, follow_up) = run_code_mode_turn_with_config(
|
||||
&server,
|
||||
"Yield without calling a tool",
|
||||
r#"yield_control(); await new Promise(() => {});"#,
|
||||
|_| {},
|
||||
)
|
||||
.await?;
|
||||
let initial = follow_up.single_request();
|
||||
let output = initial.custom_tool_call_output("call-1");
|
||||
assert!(
|
||||
output["internal_chat_message_metadata_passthrough"]
|
||||
.get("tool_calls_complete")
|
||||
.is_none()
|
||||
);
|
||||
let items = custom_tool_output_items(&initial, "call-1");
|
||||
let cell_id = extract_running_cell_id(text_item(&items, /*index*/ 0));
|
||||
let wait = responses::mount_function_call_agent_response(
|
||||
&server,
|
||||
"call-2",
|
||||
&serde_json::json!({"cell_id": cell_id, "terminate": true}).to_string(),
|
||||
"wait",
|
||||
)
|
||||
.await;
|
||||
test.submit_turn("Terminate the empty cell").await?;
|
||||
let request = wait.completion.single_request();
|
||||
let output = request.function_call_output("call-2");
|
||||
let metadata = &output["internal_chat_message_metadata_passthrough"];
|
||||
assert_eq!(metadata["cell_id"], "call-1");
|
||||
assert_eq!(metadata["executed_tool_calls"], serde_json::json!([]));
|
||||
assert_eq!(metadata["tool_calls_complete"], true);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[test_case(false, false, false; "disabled")]
|
||||
#[test_case(true, false, false; "completed")]
|
||||
@@ -1549,7 +1654,10 @@ async fn code_mode_wait_id_stays_known_after_compaction(
|
||||
metadata.get("tool_calls_complete").and_then(Value::as_bool),
|
||||
expected_complete,
|
||||
);
|
||||
assert!(metadata.get("executed_tool_calls").is_none());
|
||||
assert_eq!(
|
||||
metadata.get("executed_tool_calls").cloned(),
|
||||
expected_complete.map(|_| serde_json::json!([])),
|
||||
);
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user