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:
Nick Felt authored and copyberry committed 2026-09-17 02:18:05 +00:00
1 parent 1e9564fb85
commit 8452164c76
5 files changed
+210 -15

No files matched your search

+15 -10
View File
@@ -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"] } });