Harden Code Mode tool-call completeness tracking (#44472)

## Why

Reused call IDs, ambiguous exec/wait associations, and missing history can make tool-call completeness claims unreliable. Truncating one cell's recorded calls should also preserve completeness for unrelated cells.

## What changed

- Track observed call and runtime cell IDs with bounded memory, including call IDs from initial history, and withhold completeness when evidence is missing or ambiguous.
- Validate exec/wait input-output associations and keep invalidated completeness from returning on retries. Withhold wait completeness after resume or fork, where runtime cell IDs can be reused.
- Keep truncation markers and omission counts local to each output, clearing completeness across the affected cell while preserving unrelated cells within the metadata budget.
- Refresh recording enablement from runtime configuration without changing execution features, and preserve the live recorder during legacy configuration reloads.

## Testing

Add regression coverage for reused IDs, compaction, resumed waits, mismatched wrappers, runtime recording changes, and metadata budgeting that preserves unrelated complete calls.

GitOrigin-RevId: 0bc85964bee079879620ec0664eea530a13b61fb
This commit is contained in:
ningyi-oai
2026-09-10 08:57:35 +00:00
committed by copyberry
parent b348fc2667
commit 03f014564d
12 changed files with 1792 additions and 658 deletions
+156 -43
View File
@@ -7,10 +7,12 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use codex_code_mode::CellId;
use codex_features::Feature;
use codex_features::Features;
use codex_history::InitialHistory;
use codex_protocol::models::ExecutedToolCall;
use codex_protocol::models::ExecutedToolCallArguments;
use codex_protocol::models::ResponseItem;
@@ -28,6 +30,10 @@ use crate::tools::context::ToolPayload;
use crate::tools::router::ToolCall;
use crate::utils::json::serialized_json_bytes;
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;
@@ -36,10 +42,10 @@ type ExecutedToolCallCache =
HashMap<(std::mem::Discriminant<ResponseItem>, String), Vec<ExecutedToolCall>>;
/// Best-effort recording shared by a session and its Code Mode broker. Disabled
/// sessions allocate no state; missing calls and truncated evidence remain incomplete.
/// sessions allocate no recorder state; missing evidence remains incomplete.
#[derive(Clone, Default)]
pub(crate) struct ExecutedToolCalls {
state: Option<Arc<Mutex<ExecutedToolCallRecorderState>>>,
state: Arc<Mutex<Option<ExecutedToolCallRecorderState>>>,
}
#[derive(Default)]
@@ -49,6 +55,9 @@ struct ExecutedToolCallRecorderState {
output_cells: HashMap<String, CellId>,
retained_calls: HashMap<(std::mem::Discriminant<ResponseItem>, String), RetainedToolCalls>,
pending_nested_calls: usize,
seen_ids: SeenIds,
can_prove_wait_completion: bool,
pending_wrapper_origins: HashSet<String>,
}
/// Keep each output's calls and completion marker together through replay and pruning.
@@ -82,6 +91,41 @@ struct RecordedCell {
}
impl ExecutedToolCallRecorderState {
fn invalidate_origin(&mut self, origin: &str) {
self.pending_wrapper_origins.remove(origin);
for cell in self.cells.values_mut() {
if cell.originating_call_id.as_deref() == Some(origin) {
cell.completion = CellCompletion::Incomplete;
}
}
for ((_, output_id), retained) in &mut self.retained_calls {
if output_id == origin || retained.cell_id.as_deref() == Some(origin) {
retained.complete = false;
}
}
}
// A successful callback consumes the freshness observed at wrapper submission.
fn observe_cell_origin(&mut self, origin: &str) -> bool {
let fresh =
self.pending_wrapper_origins.remove(origin) || self.seen_ids.observe_call_id(origin);
if !fresh {
self.invalidate_origin(origin);
}
fresh
}
fn invalidate_cell(&mut self, cell_id: &CellId) {
if let Some(cell) = self.cells.get_mut(cell_id) {
cell.completion = CellCompletion::Incomplete;
}
for retained in self.retained_calls.values_mut() {
if retained.runtime_cell_id.as_ref() == Some(cell_id) {
retained.complete = false;
}
}
}
fn register_cell(&mut self, cell_id: &CellId, output_call_id: &str) {
if self.cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS && !self.cells.contains_key(cell_id)
{
@@ -117,10 +161,38 @@ impl ExecutedToolCallRecorderState {
}
impl ExecutedToolCalls {
pub(crate) fn new(features: &Features) -> Self {
pub(crate) fn new(features: &Features, history: &InitialHistory) -> Self {
Self {
state: Self::is_enabled(features)
.then(|| Arc::new(Mutex::new(ExecutedToolCallRecorderState::default()))),
state: Arc::new(Mutex::new(Self::is_enabled(features).then(|| {
ExecutedToolCallRecorderState {
seen_ids: SeenIds::from_history(history),
// Runtime cell IDs can restart after resume/fork; inherited wait
// handles cannot be distinguished from newly allocated handles.
can_prove_wait_completion: matches!(
history,
InitialHistory::New | InitialHistory::Cleared
),
..Default::default()
}
}))),
}
}
fn lock_state(&self) -> MutexGuard<'_, Option<ExecutedToolCallRecorderState>> {
self.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Called under the session config lock; this never changes execution features.
pub(crate) fn refresh(&self, features: &Features) {
let enabled = Self::is_enabled(features);
let mut state = self.lock_state();
if enabled != state.is_some() {
*state = enabled.then(|| ExecutedToolCallRecorderState {
seen_ids: SeenIds::unobserved_history(),
..Default::default()
});
}
}
@@ -135,9 +207,7 @@ impl ExecutedToolCalls {
source: &ToolCallSource,
step_context: &StepContext,
) {
if Self::is_enabled(&step_context.turn.config.features) && self.state.is_some() {
self.record_call(call, source, step_context.tool_router.tool_mode());
}
self.record_call(call, source, step_context.tool_router.tool_mode());
}
pub(crate) fn record_accepted_result(
@@ -146,17 +216,17 @@ impl ExecutedToolCalls {
call_id: &str,
result: &dyn ToolOutput,
) {
if self.state.is_some()
&& let Some(metadata) = result.tool_result_metadata()
{
// Release the lock before calling the output's trait method.
let recording = self.lock_state().is_some();
if recording && let Some(metadata) = result.tool_result_metadata() {
self.record_tool_result_metadata(source, call_id, metadata);
}
}
fn record_call(&self, call: &ToolCall, source: &ToolCallSource, tool_mode: ToolMode) {
let Some(state) = &self.state else {
if self.lock_state().is_none() {
return;
};
}
if matches!(source, ToolCallSource::Direct)
&& matches!(tool_mode, ToolMode::CodeMode | ToolMode::CodeModeOnly)
&& call.tool_name.is_default_namespace()
@@ -171,6 +241,17 @@ impl ExecutedToolCalls {
)
)
{
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
let fresh = state.seen_ids.observe_call_id(&call.call_id);
if !fresh {
state.invalidate_origin(&call.call_id);
}
if fresh && state.pending_wrapper_origins.len() < MAX_PENDING_EXECUTED_TOOL_CALLS {
state.pending_wrapper_origins.insert(call.call_id.clone());
}
return;
}
@@ -197,9 +278,13 @@ impl ExecutedToolCalls {
};
match source {
ToolCallSource::Direct | ToolCallSource::DirectPlaintextMessage => {
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
if !state.seen_ids.observe_call_id(&call.call_id) {
state.invalidate_origin(&call.call_id);
}
if state.direct_calls.len() < MAX_PENDING_EXECUTED_TOOL_CALLS {
state
.direct_calls
@@ -236,12 +321,13 @@ impl ExecutedToolCalls {
call: ExecutedToolCall,
original_bytes: usize,
) {
let Some(state) = &self.state else {
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if !state.cells.contains_key(&cell_id) {
state.invalidate_cell(&cell_id);
}
if state.pending_nested_calls > MAX_PENDING_EXECUTED_TOOL_CALLS
|| (state.cells.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS
&& !state.cells.contains_key(&cell_id))
@@ -253,6 +339,7 @@ impl ExecutedToolCalls {
}
let at_pending_call_limit = state.pending_nested_calls == MAX_PENDING_EXECUTED_TOOL_CALLS;
let cell = state.cells.entry(cell_id).or_default();
let duplicate_call_id = cell.pending_calls.contains_key(&call_id);
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),
@@ -270,10 +357,11 @@ impl ExecutedToolCalls {
cell.completion = if matches!(
cell.completion,
CellCompletion::Started | CellCompletion::Recording
) && !matches!(
call.arguments(),
ExecutedToolCallArguments::Truncated { .. }
) {
) && !duplicate_call_id
&& !matches!(
call.arguments(),
ExecutedToolCallArguments::Truncated { .. }
) {
CellCompletion::Recording
} else {
CellCompletion::Incomplete
@@ -288,14 +376,12 @@ impl ExecutedToolCalls {
call_id: &str,
metadata: &JsonValue,
) -> bool {
let Some(state) = &self.state else {
return false;
};
let metadata = codex_protocol::models::ToolResultMetadata::new(metadata);
let has_metadata = metadata.is_some();
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return false;
};
let call = match source {
ToolCallSource::Direct | ToolCallSource::DirectPlaintextMessage => {
state.direct_calls.get_mut(call_id)
@@ -328,35 +414,43 @@ impl ExecutedToolCalls {
}
pub(crate) fn register_cell(&self, cell_id: &CellId, output_call_id: &str) {
let Some(state) = &self.state else {
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if !state.observe_cell_origin(output_call_id) {
state.invalidate_cell(cell_id);
}
state.register_cell(cell_id, output_call_id);
}
pub(crate) fn start_cell(&self, cell_id: &CellId, output_call_id: &str) {
let Some(state) = &self.state else {
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let unique_cell = state.seen_ids.observe_runtime_cell_id(cell_id);
let unique_origin = state.observe_cell_origin(output_call_id);
if !unique_cell {
state.invalidate_cell(cell_id);
}
let history_ids_indexed = state.seen_ids.history_ids_indexed();
state.register_cell(cell_id, output_call_id);
if let Some(cell) = state.cells.get_mut(cell_id) {
cell.completion = CellCompletion::Started;
// Failed indexing must not make a known historical ID look fresh.
cell.completion = if unique_cell && unique_origin && history_ids_indexed {
CellCompletion::Started
} else {
CellCompletion::Incomplete
};
}
}
pub(crate) fn finish_cell_recording(&self, cell_id: &CellId) {
let Some(state) = &self.state else {
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return;
};
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(cell) = state.cells.get_mut(cell_id) {
if cell.completion == CellCompletion::Recording {
cell.completion = CellCompletion::Complete;
@@ -366,3 +460,22 @@ impl ExecutedToolCalls {
}
}
}
fn input_call_id(item: &ResponseItem) -> Option<&str> {
match item {
ResponseItem::FunctionCall { call_id, .. }
| ResponseItem::CustomToolCall { call_id, .. } => Some(call_id),
ResponseItem::ToolSearchCall { call_id, .. }
| ResponseItem::LocalShellCall { call_id, .. } => call_id.as_deref(),
_ => None,
}
}
fn output_call_id(item: &ResponseItem) -> Option<&str> {
match item {
ResponseItem::FunctionCallOutput { call_id, .. }
| ResponseItem::ToolSearchOutput { call_id, .. } => call_id.as_deref(),
ResponseItem::CustomToolCallOutput { call_id, .. } => Some(call_id),
_ => None,
}
}
@@ -4,7 +4,7 @@
use super::*;
impl ExecutedToolCalls {
/// Attaches host-recorded calls within the existing request metadata budget.
/// Attaches trusted completeness; request budgeting only revokes damaged inventories.
pub(crate) fn attach_to_prompt(
&self,
items: &mut [ResponseItem],
@@ -20,12 +20,12 @@ impl ExecutedToolCalls {
items: &mut [ResponseItem],
retry_cache: &mut ExecutedToolCallCache,
) -> bool {
let Some(state) = &self.state else {
let mut state = self.lock_state();
let Some(state) = state.as_mut() else {
return false;
};
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
// Failed wrappers have no callback; only their bounded bitmap observation survives.
state.pending_wrapper_origins.clear();
if state.direct_calls.is_empty()
&& state.output_cells.is_empty()
&& state.retained_calls.is_empty()
@@ -34,6 +34,20 @@ impl ExecutedToolCalls {
return false;
}
let mut output_counts = HashMap::new();
let mut input_indices = HashMap::new();
for (index, item) in items.iter().enumerate() {
if let Some(call_id) = output_call_id(item) {
*output_counts.entry(call_id.to_string()).or_insert(0_usize) += 1;
}
if let Some(call_id) = input_call_id(item) {
input_indices
.entry(call_id.to_string())
.and_modify(|entry: &mut Option<usize>| *entry = None)
.or_insert(Some(index));
}
}
// Updated records supersede older retry snapshots.
retry_cache.retain(|key, _| {
!state
@@ -41,60 +55,69 @@ impl ExecutedToolCalls {
.get(key)
.is_some_and(|retained| retained.result_metadata_updated)
});
let mut pending_retry_outputs = retry_cache.keys().cloned().collect::<HashSet<_>>();
let mut pending_retained_outputs =
state.retained_calls.keys().cloned().collect::<HashSet<_>>();
let mut pending_outputs = retry_cache
.keys()
.chain(state.retained_calls.keys())
.cloned()
.collect::<HashSet<_>>();
let mut attached = false;
let mut retained_bytes = 0_usize;
for item in items.iter_mut().rev() {
for index in (0..items.len()).rev() {
if state.direct_calls.is_empty()
&& state.output_cells.is_empty()
&& pending_retry_outputs.is_empty()
&& pending_retained_outputs.is_empty()
&& pending_outputs.is_empty()
{
break;
}
let call_id = match &*item {
ResponseItem::FunctionCallOutput {
call_id: Some(call_id),
..
}
| ResponseItem::CustomToolCallOutput { call_id, .. }
| ResponseItem::ToolSearchOutput {
call_id: Some(call_id),
..
} => call_id,
_ => continue,
let item = &items[index];
let Some(call_id) = output_call_id(item) else {
continue;
};
let key = (std::mem::discriminant(&*item), call_id.clone());
let key = (std::mem::discriminant(item), call_id.to_string());
let retained = state.retained_calls.get(&key);
let mut complete = retained.is_some_and(|retained| retained.complete);
let mut cell_id = retained.and_then(|retained| retained.cell_id.clone());
let mut runtime_cell_id =
retained.and_then(|retained| retained.runtime_cell_id.clone());
let calls = if let Some(cached) = retry_cache.get(&key) {
if !pending_retry_outputs.remove(&key) {
let calls = if let Some(cached) =
retry_cache.get(&key).or_else(|| retained.map(|r| &r.calls))
{
if !pending_outputs.remove(&key) {
continue;
}
pending_retained_outputs.remove(&key);
cached.clone()
} else if let Some(retained) = retained {
if !pending_retained_outputs.remove(&key) {
continue;
}
retained.calls.clone()
} else {
let mut runtime_cell_id = None;
let mut calls = state
.direct_calls
.remove(call_id)
.into_iter()
.collect::<Vec<_>>();
let direct_call_collision = !calls.is_empty();
let mut call_index_by_id = HashMap::new();
if let Some(output_cell_id) = state.output_cells.remove(call_id)
&& let Some(cell) = state.cells.get_mut(&output_cell_id)
{
cell_id = cell.originating_call_id.clone();
runtime_cell_id = Some(output_cell_id.clone());
// Validate the first delta too: a later wait cannot repair a
// missing or ambiguous exec/output association.
let matches_input =
input_indices
.get(call_id)
.copied()
.flatten()
.is_some_and(|input_index| {
input_index < index
&& cell_id.as_deref().is_some_and(|origin| {
code_mode_input_matches_output(
&items[input_index],
item,
origin,
&output_cell_id,
)
})
});
if output_counts.get(call_id) != Some(&1) || !matches_input {
cell.completion = CellCompletion::Incomplete;
}
let pending_calls = cell.pending_calls.len();
for (call_id, call) in cell.pending_calls.drain(..) {
if matches!(call.arguments(), ExecutedToolCallArguments::Raw(_)) {
@@ -103,7 +126,10 @@ impl ExecutedToolCalls {
calls.push(call);
}
cell.pending_full_argument_bytes = 0;
complete = cell.completion == CellCompletion::Complete;
complete = cell.completion == CellCompletion::Complete
&& !direct_call_collision
&& (state.can_prove_wait_completion
|| matches!(item, ResponseItem::CustomToolCallOutput { .. }));
if matches!(
cell.completion,
CellCompletion::Complete | CellCompletion::Incomplete
@@ -133,49 +159,117 @@ impl ExecutedToolCalls {
);
calls
};
let item = &mut items[index];
item.append_executed_tool_calls(calls);
if let Some(cell_id) = cell_id {
item.set_tool_call_cell_id(&cell_id);
}
if complete {
item.mark_tool_calls_complete();
} else {
item.clear_tool_calls_complete();
}
retained_bytes = retained_bytes.saturating_add(executed_tool_call_metadata_bytes(item));
attached = true;
}
if !pending_retained_outputs.is_empty() {
if !pending_outputs.is_empty() {
state
.retained_calls
.retain(|key, _| !pending_retained_outputs.contains(key));
.retain(|key, _| !pending_outputs.contains(key));
}
if retained_bytes > MAX_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT {
let mut invalid_cells = HashSet::new();
for index in 0..items.len() {
let item = &items[index];
let Some(call_id) = output_call_id(item) else {
continue;
};
let key = (std::mem::discriminant(item), call_id.to_string());
let Some(retained) = state.retained_calls.get_mut(&key) else {
continue;
};
let matches_inventory = item.executed_tool_call_metadata().is_some_and(|metadata| {
metadata.cell_id == retained.cell_id
&& (metadata.has_same_tool_calls(&retained.calls)
|| (retained.calls.is_empty() && metadata.executed_tool_calls.is_none()))
});
// A verified output may outlive its input after compaction. Any input
// still present must continue to identify the same exec or wait.
let matches_input = input_indices.get(call_id).is_none_or(|input_index| {
input_index.is_some_and(|input_index| {
input_index < index
&& match (&retained.cell_id, &retained.runtime_cell_id) {
(Some(origin), Some(runtime_cell)) => code_mode_input_matches_output(
&items[input_index],
item,
origin,
runtime_cell,
),
_ => true,
}
})
});
// Lost or ambiguous evidence cannot become complete again on a retry.
let intact =
output_counts.get(call_id) == Some(&1) && matches_inventory && matches_input;
retained.complete &= intact;
if !intact && let Some(cell) = retained.runtime_cell_id.clone() {
invalid_cells.insert(cell);
}
}
for cell_id in &invalid_cells {
if let Some(cell) = state.cells.get_mut(cell_id) {
cell.completion = CellCompletion::Incomplete;
}
}
if !invalid_cells.is_empty() {
for retained in state.retained_calls.values_mut() {
if retained
.runtime_cell_id
.as_ref()
.is_some_and(|cell_id| invalid_cells.contains(cell_id))
{
retained.complete = false;
}
}
}
for item in items.iter_mut() {
let Some(call_id) = output_call_id(item) else {
continue;
};
let key = (std::mem::discriminant(&*item), call_id.to_string());
let Some(retained) = state.retained_calls.get(&key) else {
continue;
};
if retained.complete {
item.mark_tool_calls_complete();
} else {
item.clear_tool_calls_complete();
}
}
let metadata_bytes = items.iter().fold(0_usize, |bytes, item| {
bytes.saturating_add(executed_tool_call_metadata_bytes(item))
});
if metadata_bytes > MAX_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT {
bound_executed_tool_calls_for_prompt_prioritizing_recent(items);
let retained_before_bounding = std::mem::take(&mut state.retained_calls);
let mut bounded_outputs = HashSet::new();
for item in items {
let call_id = match &*item {
ResponseItem::FunctionCallOutput {
call_id: Some(call_id),
..
}
| ResponseItem::CustomToolCallOutput { call_id, .. }
| ResponseItem::ToolSearchOutput {
call_id: Some(call_id),
..
} => call_id,
_ => continue,
for item in items.iter_mut() {
let Some(call_id) = output_call_id(item) else {
continue;
};
let key = (std::mem::discriminant(&*item), call_id.to_string());
// History already carries unmanaged records; retaining them would append them again.
let Some(previous) = retained_before_bounding.get(&key) else {
continue;
};
let key = (std::mem::discriminant(&*item), call_id.clone());
let metadata = item.executed_tool_call_metadata();
let unique_output = bounded_outputs.insert(key.clone());
if !unique_output && let Some(retained) = state.retained_calls.get_mut(&key) {
retained.call_index_by_id.clear();
}
let previous = retained_before_bounding.get(&key);
if let Some(retained) = previous
&& let Some(runtime_cell_id) = &retained.runtime_cell_id
if let Some(runtime_cell_id) = &previous.runtime_cell_id
&& metadata
.is_none_or(|metadata| !metadata.has_same_tool_calls(&retained.calls))
.is_none_or(|metadata| !metadata.has_same_tool_calls(&previous.calls))
&& let Some(cell) = state.cells.get_mut(runtime_cell_id)
{
cell.completion = CellCompletion::Incomplete;
@@ -187,25 +281,22 @@ impl ExecutedToolCalls {
.is_some_and(|calls| !calls.is_empty())
|| metadata.tool_calls_complete.is_some())
{
let runtime_cell_id =
previous.and_then(|retained| retained.runtime_cell_id.clone());
// Metadata-only shedding preserves slots; changed calls or duplicate outputs do not.
let call_index_by_id = previous
.filter(|retained| {
unique_output && metadata.has_same_tool_calls(&retained.calls)
})
.map(|retained| retained.call_index_by_id.clone())
.unwrap_or_default();
let call_index_by_id =
if unique_output && metadata.has_same_tool_calls(&previous.calls) {
previous.call_index_by_id.clone()
} else {
HashMap::new()
};
let retained = state.retained_calls.entry(key).or_default();
retained.runtime_cell_id = runtime_cell_id;
retained.runtime_cell_id = previous.runtime_cell_id.clone();
retained.calls = metadata.executed_tool_calls.clone().unwrap_or_default();
if let Some(cell_id) = metadata.cell_id.as_ref() {
retained.cell_id = Some(cell_id.clone());
}
retained.complete |= metadata.tool_calls_complete == Some(true);
retained.call_index_by_id = call_index_by_id;
retained.result_metadata_updated =
previous.is_some_and(|retained| retained.result_metadata_updated);
retained.result_metadata_updated = previous.result_metadata_updated;
}
}
}
@@ -214,6 +305,50 @@ impl ExecutedToolCalls {
}
}
fn code_mode_input_matches_output(
input: &ResponseItem,
output: &ResponseItem,
origin: &str,
runtime_cell: &CellId,
) -> bool {
match (input, output) {
(
ResponseItem::CustomToolCall {
name,
namespace,
call_id,
..
},
ResponseItem::CustomToolCallOutput { .. },
) => {
call_id == origin
&& crate::tools::code_mode::is_exec_tool_name(&codex_tools::ToolName::new(
namespace.clone(),
name,
))
}
(
ResponseItem::FunctionCall {
name,
namespace,
call_id,
arguments,
..
},
ResponseItem::FunctionCallOutput { .. },
) => {
call_id != origin
&& name == crate::tools::code_mode::WAIT_TOOL_NAME
&& codex_tools::ToolName::new(namespace.clone(), name).is_default_namespace()
&& serde_json::from_str::<JsonValue>(arguments).is_ok_and(|arguments| {
arguments.get("cell_id").and_then(JsonValue::as_str)
== Some(runtime_cell.as_str())
})
}
_ => false,
}
}
#[cfg(test)]
#[path = "request_metadata_tests.rs"]
mod tests;
@@ -1,15 +1,19 @@
use codex_history::RolloutItem;
use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ToolResultMetadata;
use codex_protocol::protocol::SessionMeta;
use codex_protocol::protocol::SessionMetaLine;
use codex_protocol::protocol::ThreadHistoryMode;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
fn new_recorder() -> ExecutedToolCalls {
fn new_recorder(history: InitialHistory) -> ExecutedToolCalls {
let mut features = Features::default();
features.enable(Feature::ExecutedToolCallMetadata);
ExecutedToolCalls::new(&features)
ExecutedToolCalls::new(&features, &history)
}
fn output(call_id: &str) -> ResponseItem {
@@ -19,11 +23,54 @@ fn output(call_id: &str) -> ResponseItem {
})
}
#[tokio::test]
async fn recorder_preserves_session_and_turn_gates_and_lazy_result_metadata() {
struct MetadataLookup(std::cell::Cell<usize>, JsonValue);
fn exec_output(call_id: &str) -> ResponseItem {
ResponseItem::from(ResponseInputItem::CustomToolCallOutput {
call_id: call_id.to_string(),
name: None,
output: FunctionCallOutputPayload::from_text(String::new()),
})
}
impl ToolOutput for MetadataLookup {
fn exec_input(call_id: &str) -> ResponseItem {
serde_json::from_value(json!({
"type": "custom_tool_call", "call_id": call_id, "name": "exec", "input": "",
}))
.expect("exec input must deserialize")
}
fn wait_input(call_id: &str, cell: &CellId) -> ResponseItem {
serde_json::from_value(json!({
"type": "function_call", "call_id": call_id, "name": "wait",
"arguments": json!({"cell_id": cell.as_str()}).to_string(),
}))
.expect("wait input must deserialize")
}
fn record_nested_call(
recorder: &ExecutedToolCalls,
cell: &CellId,
call_id: &str,
) -> ExecutedToolCall {
let call = ExecutedToolCall::new("nested_tool".to_string(), json!({}));
recorder.record_nested_tool_call(
cell.clone(),
call_id.to_string(),
call.clone(),
/*original_bytes*/ 2,
);
call
}
fn tool_calls_complete(item: &ResponseItem) -> Option<bool> {
item.executed_tool_call_metadata()
.and_then(|metadata| metadata.tool_calls_complete)
}
#[tokio::test]
async fn recorder_refreshes_without_changing_execution_features_or_claiming_missing_history() {
struct MetadataLookup<'a>(std::cell::Cell<usize>, JsonValue, &'a ExecutedToolCalls);
impl ToolOutput for MetadataLookup<'_> {
fn log_output(&self) -> String {
panic!("recording must not read the diagnostic output")
}
@@ -37,82 +84,107 @@ async fn recorder_preserves_session_and_turn_gates_and_lazy_result_metadata() {
}
fn tool_result_metadata(&self) -> Option<&JsonValue> {
assert!(
self.2.state.try_lock().is_ok(),
"result callbacks must run unlocked"
);
self.0.set(self.0.get() + 1);
Some(&self.1)
}
}
let (_session, turn) = crate::session::tests::make_session_and_context().await;
let mut turn = Arc::new(turn);
assert!(ExecutedToolCalls::default().state.is_none());
for session_enabled in [false, true] {
let mut features = Features::default();
features.set_enabled(Feature::ExecutedToolCallMetadata, session_enabled);
for turn_enabled in [false, true] {
let calls = ExecutedToolCalls::new(&features);
assert_eq!(calls.state.is_some(), session_enabled);
Arc::make_mut(
&mut Arc::get_mut(&mut turn)
.expect("the previous step must have released its turn")
.config,
)
let (session, turn) = crate::session::tests::make_session_and_context().await;
let step = StepContext::for_test(Arc::new(turn));
// The broker holds a clone before rollout enablement reaches the session.
let calls = session.services.executed_tool_calls.clone();
assert!(calls.lock_state().is_none());
let mut next_config = (*session.get_config().await).clone();
let mut retry_cache = HashMap::new();
for (index, enabled) in [false, true, false, true].into_iter().enumerate() {
next_config
.features
.set_enabled(Feature::ExecutedToolCallMetadata, turn_enabled)
.set_enabled(Feature::ExecutedToolCallMetadata, enabled)
.expect("test feature must be configurable");
let step = StepContext::for_test(Arc::clone(&turn));
assert_eq!(
ExecutedToolCalls::is_enabled(&step.turn.config.features),
turn_enabled
);
session.refresh_runtime_config(next_config.clone()).await;
assert_eq!(calls.lock_state().is_some(), enabled);
// A legacy file reload has no new rollout snapshot and must keep this setting.
session.reload_user_config_layer().await;
assert_eq!(calls.lock_state().is_some(), enabled);
// An already-running turn and future turns keep their execution feature policy.
assert!(!ExecutedToolCalls::is_enabled(&step.turn.config.features));
assert!(!ExecutedToolCalls::is_enabled(
&session.get_config().await.features
));
let call = ToolCall {
tool_name: codex_tools::ToolName::plain("test_tool"),
call_id: "call".to_string(),
payload: ToolPayload::Function {
arguments: r#"{"value":7}"#.to_string(),
},
encrypted_function_args: None,
};
calls.record_tool_call(&call, &ToolCallSource::Direct, &step);
let result = MetadataLookup(std::cell::Cell::new(0), json!({}));
calls.record_accepted_result(&ToolCallSource::Direct, &call.call_id, &result);
// Accepted metadata follows session availability even when this turn stops recording calls.
assert_eq!(result.0.get(), usize::from(session_enabled));
let call = ToolCall {
tool_name: codex_tools::ToolName::plain("test_tool"),
call_id: format!("call-{index}"),
payload: ToolPayload::Function {
arguments: r#"{"value":7}"#.to_string(),
},
encrypted_function_args: None,
};
calls.record_tool_call(&call, &ToolCallSource::Direct, &step);
let result = MetadataLookup(std::cell::Cell::new(0), json!({}), &calls);
calls.record_accepted_result(&ToolCallSource::Direct, &call.call_id, &result);
assert_eq!(result.0.get(), usize::from(enabled));
let input = serde_json::from_value(json!({
"type": "function_call",
"call_id": "call",
"name": "test_tool",
"arguments": "{\"value\":7}",
}))
.expect("tool input must deserialize");
let mut prompt = vec![input, output("call")];
if !session_enabled {
prompt[1].mark_tool_calls_complete();
}
let original = prompt.clone();
calls.attach_to_prompt(&mut prompt, &mut HashMap::new());
if !session_enabled {
assert_eq!(prompt, original);
}
let mut expected_call =
ExecutedToolCall::new("test_tool".to_string(), json!({"value": 7}));
expected_call.set_tool_result_metadata(ToolResultMetadata::new(&json!({})));
let expected_calls = vec![expected_call];
assert_eq!(
prompt[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.executed_tool_calls.as_ref()),
(session_enabled && turn_enabled).then_some(&expected_calls),
"session_enabled={session_enabled}, turn_enabled={turn_enabled}",
);
let input = serde_json::from_value(json!({
"type": "function_call",
"call_id": call.call_id,
"name": "test_tool",
"arguments": "{\"value\":7}",
}))
.expect("tool input must deserialize");
let mut prompt = vec![input, output(&call.call_id)];
if !enabled {
prompt[1].mark_tool_calls_complete();
}
let original = prompt.clone();
calls.attach_to_prompt(&mut prompt, &mut retry_cache);
if !enabled {
assert_eq!(prompt, original);
} else {
assert_eq!(tool_calls_complete(&prompt[1]), None);
}
let mut expected_call = ExecutedToolCall::new("test_tool".to_string(), json!({"value": 7}));
expected_call.set_tool_result_metadata(ToolResultMetadata::new(&json!({})));
let expected_calls = vec![expected_call];
assert_eq!(
prompt[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.executed_tool_calls.as_ref()),
enabled.then_some(&expected_calls),
);
if enabled {
// Reapplying an enabled config must preserve already-recorded calls.
session.refresh_runtime_config(next_config.clone()).await;
let mut replay = original;
calls.attach_to_prompt(&mut replay, &mut HashMap::new());
assert_eq!(replay, prompt);
}
let cell = CellId::new(format!("cell-{index}"));
let origin = format!("exec-{index}");
calls.start_cell(&cell, &origin);
let nested = record_nested_call(&calls, &cell, "nested");
calls.finish_cell_recording(&cell);
let mut prompt = vec![exec_input(&origin), exec_output(&origin)];
calls.attach_to_prompt(&mut prompt, &mut retry_cache);
assert_eq!(tool_calls_complete(&prompt[1]), None);
assert_eq!(
prompt[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.executed_tool_calls.as_ref()),
enabled.then_some(&vec![nested]),
);
}
}
#[test]
fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS + 2 {
recorder.record_call(
@@ -127,6 +199,18 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
&ToolCallSource::Direct,
ToolMode::Direct,
);
recorder.record_call(
&ToolCall {
tool_name: codex_tools::ToolName::plain(crate::tools::code_mode::PUBLIC_TOOL_NAME),
call_id: format!("failed-wrapper-{index}"),
payload: ToolPayload::Custom {
input: "".to_string(),
},
encrypted_function_args: None,
},
&ToolCallSource::Direct,
ToolMode::CodeMode,
);
}
let metadata = json!({ "arbitrary-key": ["R1", null] });
assert!(recorder.record_tool_result_metadata(&ToolCallSource::Direct, "direct-0", &metadata));
@@ -150,12 +234,8 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
}
{
let state = recorder
.state
.as_ref()
.unwrap()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let state = recorder.lock_state();
let state = state.as_ref().unwrap();
assert_eq!(
state.direct_calls.len(),
MAX_PENDING_EXECUTED_TOOL_CALLS + 1
@@ -186,29 +266,26 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
state.pending_nested_calls,
MAX_PENDING_EXECUTED_TOOL_CALLS + 1
);
assert_eq!(
state.pending_wrapper_origins.len(),
MAX_PENDING_EXECUTED_TOOL_CALLS
);
assert_eq!(state.cells.len(), MAX_PENDING_EXECUTED_TOOL_CALLS);
assert_eq!(state.output_cells.len(), MAX_PENDING_EXECUTED_TOOL_CALLS);
}
let mut items = [ResponseItem::FunctionCallOutput {
id: None,
call_id: Some("bounded-output".to_string()),
name: None,
namespace: None,
output: FunctionCallOutputPayload::from_text(String::new()),
internal_chat_message_metadata_passthrough: None,
}];
let mut items = [exec_input("bounded-output"), exec_output("bounded-output")];
let mut retry_cache = HashMap::new();
recorder.finish_cell_recording(&cell_id);
recorder.attach_pending_to_prompt(&mut items, &mut retry_cache);
assert_eq!(
items[0]
items[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.tool_calls_complete),
None,
);
let calls = items[0]
let calls = items[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.executed_tool_calls.as_ref())
.expect("bounded nested calls must attach to their own output");
@@ -229,25 +306,15 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
let expected_calls = calls.clone();
{
let state = recorder
.state
.as_ref()
.unwrap()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let state = recorder.lock_state();
let state = state.as_ref().unwrap();
assert!(state.pending_wrapper_origins.is_empty());
assert_eq!(state.pending_nested_calls, 0);
assert!(!state.cells.contains_key(&cell_id));
assert_eq!(retry_cache.len(), 1);
}
let mut replayed_items = [ResponseItem::FunctionCallOutput {
id: None,
call_id: Some("bounded-output".to_string()),
name: None,
namespace: None,
output: FunctionCallOutputPayload::from_text(String::new()),
internal_chat_message_metadata_passthrough: None,
}];
let mut replayed_items = [exec_output("bounded-output")];
let mut replay_retry_cache = HashMap::new();
assert!(recorder.attach_pending_to_prompt(&mut replayed_items, &mut replay_retry_cache));
assert_eq!(
@@ -259,18 +326,14 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
let mut compacted_retry_cache = HashMap::new();
assert!(!recorder.attach_pending_to_prompt(&mut [], &mut compacted_retry_cache));
let state = recorder
.state
.as_ref()
.unwrap()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let state = recorder.lock_state();
let state = state.as_ref().unwrap();
assert!(state.retained_calls.is_empty());
}
#[test]
fn executed_tool_call_recorder_bounds_retained_history_and_reports_omissions() {
let recorder = new_recorder();
fn executed_tool_call_recorder_bounds_retained_history_and_keeps_latest_calls() {
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
let mut history = Vec::new();
let arguments = serde_json::to_string(&json!({ "payload": "x".repeat(1024) }))
.expect("tool arguments must serialize");
@@ -317,12 +380,8 @@ fn executed_tool_call_recorder_bounds_retained_history_and_reports_omissions() {
);
}
let state = recorder
.state
.as_ref()
.unwrap()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let state = recorder.lock_state();
let state = state.as_ref().unwrap();
let retained_bytes = state
.retained_calls
.values()
@@ -338,20 +397,17 @@ fn executed_tool_call_recorder_bounds_retained_history_and_reports_omissions() {
.flatten()
.map(|call| serde_json::to_value(call).expect("retained call must serialize"))
.collect::<Vec<_>>();
let omitted_calls = metadata
.iter()
.filter_map(|call| {
call["arguments"]["_codex_executed_tool_call_truncated"]["omitted_calls"].as_u64()
})
.sum::<u64>();
assert!(omitted_calls > 0);
assert_eq!(metadata.len() as u64 + omitted_calls, 512);
assert!(metadata.len() < 512);
// Dropped outputs do not add their omission counts to unrelated surviving calls.
assert!(metadata.iter().all(|call| {
call["arguments"]["_codex_executed_tool_call_truncated"]["omitted_calls"].is_null()
}));
}
#[test]
fn tool_call_completeness_requires_finished_lossless_recording() {
for scenario in ["empty", "unobserved", "late_call"] {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
let cell_id = CellId::new(scenario.to_string());
if scenario == "unobserved" {
recorder.register_cell(&cell_id, "output");
@@ -371,10 +427,10 @@ fn tool_call_completeness_requires_finished_lossless_recording() {
}
recorder.finish_cell_recording(&cell_id);
let mut items = [output("output")];
let mut items = [exec_input("output"), exec_output("output")];
recorder.attach_pending_to_prompt(&mut items, &mut HashMap::new());
assert_eq!(
items[0]
items[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.tool_calls_complete),
None,
@@ -387,7 +443,7 @@ fn tool_call_completeness_requires_finished_lossless_recording() {
fn tool_call_completeness_survives_waits_without_changing_deltas() {
let metadata = json!({ "provider": { "ids": ["CLATE"] } });
for truncated in [false, true] {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::New);
let cell_id = CellId::new("multi-wait".to_string());
recorder.start_cell(&cell_id, "exec");
let mut history = Vec::new();
@@ -397,9 +453,36 @@ fn tool_call_completeness_survives_waits_without_changing_deltas() {
.into_iter()
.enumerate()
{
recorder.register_cell(&cell_id, call_id);
let mut expected_output = output(call_id);
if index > 0 {
recorder.record_call(
&ToolCall {
tool_name: codex_tools::ToolName::plain(
crate::tools::code_mode::WAIT_TOOL_NAME,
),
call_id: call_id.to_string(),
payload: ToolPayload::Function {
arguments: json!({"cell_id": cell_id.as_str()}).to_string(),
},
encrypted_function_args: None,
},
&ToolCallSource::Direct,
ToolMode::CodeModeOnly,
);
recorder.register_cell(&cell_id, call_id);
}
let input = if index == 0 {
exec_input(call_id)
} else {
wait_input(call_id, &cell_id)
};
let mut expected_output = if index == 0 {
exec_output(call_id)
} else {
output(call_id)
};
history.push(input.clone());
history.push(expected_output.clone());
expected.push(input);
if index < 2 {
// Identical calls are distinct submissions; truncation stays sticky across waits.
let original_bytes = if truncated && index == 1 { 9_000 } else { 2 };
@@ -434,10 +517,10 @@ fn tool_call_completeness_survives_waits_without_changing_deltas() {
let mut delayed_call =
ExecutedToolCall::new("nested_tool".to_string(), json!({}));
delayed_call.set_tool_result_metadata(ToolResultMetadata::new(&metadata));
let mut delayed_output = output("exec");
let mut delayed_output = exec_output("exec");
delayed_output.append_executed_tool_calls(vec![delayed_call]);
delayed_output.set_tool_call_cell_id("exec");
expected[0] = delayed_output;
expected[1] = delayed_output;
assert!(recorder.record_tool_result_metadata(&source, "nested-1", &json!({})));
call.set_tool_result_metadata(ToolResultMetadata::new(&json!({})));
}
@@ -458,50 +541,261 @@ fn tool_call_completeness_survives_waits_without_changing_deltas() {
assert_eq!(prompt, expected);
}
}
let state = recorder.state.as_ref().unwrap().lock().unwrap();
let independent = CellId::new("independent-runtime".to_string());
recorder.start_cell(&independent, "independent-exec");
record_nested_call(&recorder, &independent, "independent-call");
recorder.finish_cell_recording(&independent);
history.extend([
exec_input("independent-exec"),
exec_output("independent-exec"),
]);
// Damaged deltas revoke their terminal sibling, not an independent cell.
history[2] = wait_input("wait-1", &independent);
history[0] = wait_input("exec", &independent);
recorder.attach_pending_to_prompt(&mut history, &mut retry_cache);
assert_eq!(tool_calls_complete(&history[7]), None);
assert_eq!(tool_calls_complete(&history[9]), Some(true));
let state = recorder.lock_state();
let state = state.as_ref().unwrap();
assert!(state.cells.is_empty());
assert_eq!(state.pending_nested_calls, 0);
}
}
#[test]
fn cell_correlation_uses_originating_exec_across_runtime_restarts() {
let runtime_cell_id = CellId::new("1".to_string());
fn unrelated_raw_overflow_retains_a_complete_exec_within_the_request_budget() {
let request_budget = 32 * 1024;
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("clean-overflow-cell".to_string());
recorder.start_cell(&cell, "exec-clean");
let clean_call = record_nested_call(&recorder, &cell, "nested-clean");
recorder.finish_cell_recording(&cell);
for originating_call_id in ["first-exec", "resumed-exec"] {
let recorder = new_recorder();
recorder.start_cell(&runtime_cell_id, originating_call_id);
recorder.record_nested_tool_call(
runtime_cell_id.clone(),
"nested-call".to_string(),
ExecutedToolCall::new("nested_tool".to_string(), json!({})),
/*original_bytes*/ 2,
);
let mut initial = [output(originating_call_id)];
recorder.attach_pending_to_prompt(&mut initial, &mut HashMap::new());
assert_eq!(
initial[0]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.cell_id.as_deref()),
Some(originating_call_id),
);
recorder.register_cell(&runtime_cell_id, "wait");
recorder.finish_cell_recording(&runtime_cell_id);
let mut final_output = [output("wait")];
recorder.attach_pending_to_prompt(&mut final_output, &mut HashMap::new());
let metadata = final_output[0]
let mut unrelated = output("direct-oversized");
let arguments = json!("x".repeat(7 * 1024));
assert!(serialized_json_bytes(&arguments).unwrap() <= MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
unrelated.append_executed_tool_calls(
(0..6)
.map(|index| ExecutedToolCall::new(format!("direct-{index}"), arguments.clone()))
.collect(),
);
let original = [
unrelated,
exec_input("exec-clean"),
exec_output("exec-clean"),
];
let mut prompt = original.clone();
assert!(
prompt
.iter()
.map(executed_tool_call_metadata_bytes)
.sum::<usize>()
> request_budget
);
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
let metadata = prompt[2]
.executed_tool_call_metadata()
.expect("the later clean output must retain its calls");
assert_eq!(metadata.executed_tool_calls, Some(vec![clean_call]));
assert_eq!(metadata.tool_calls_complete, Some(true));
assert!(
prompt[0]
.executed_tool_call_metadata()
.expect("completed cell must have metadata");
assert_eq!(metadata.cell_id.as_deref(), Some(originating_call_id));
assert_eq!(metadata.tool_calls_complete, Some(true));
.and_then(|metadata| metadata.executed_tool_calls.as_ref())
.is_some_and(|calls| calls.iter().any(|call| matches!(
call.arguments(),
ExecutedToolCallArguments::Truncated { .. }
)))
);
assert!(
prompt
.iter()
.map(executed_tool_call_metadata_bytes)
.sum::<usize>()
<= request_budget
);
let mut retry = original;
recorder.attach_to_prompt(&mut retry, &mut HashMap::new());
assert_eq!(retry, prompt);
}
#[test]
fn paginated_history_allows_fresh_origins_but_rejects_known_ids() {
for origin in ["fresh-exec", "old-exec"] {
let history = InitialHistory::Forked(vec![
RolloutItem::SessionMeta(SessionMetaLine {
meta: SessionMeta {
history_mode: ThreadHistoryMode::Paginated,
..SessionMeta::default()
},
git: None,
}),
RolloutItem::ResponseItem(exec_input("old-exec").into()),
RolloutItem::ResponseItem(exec_output("old-exec").into()),
]);
let recorder = new_recorder(history);
let cell = CellId::new("1".to_string());
recorder.start_cell(&cell, origin);
let call = record_nested_call(&recorder, &cell, "nested");
recorder.finish_cell_recording(&cell);
// The old call is only in inherited history, so duplicate prompt IDs cannot reject it.
let mut prompt = [exec_input(origin), exec_output(origin)];
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
assert_eq!(
tool_calls_complete(&prompt[1]),
(origin == "fresh-exec").then_some(true),
);
assert_eq!(
prompt[1]
.executed_tool_call_metadata()
.unwrap()
.executed_tool_calls,
Some(vec![call]),
);
// Compaction may drop the input after the output has already been authenticated.
let mut retry = [exec_output(origin)];
recorder.attach_to_prompt(&mut retry, &mut HashMap::new());
assert_eq!(retry[0], prompt[1]);
}
}
#[test]
fn forked_wait_keeps_calls_without_completion() {
let recorder = new_recorder(InitialHistory::Forked(vec![RolloutItem::ResponseItem(
exec_input("old-exec").into(),
)]));
let cell = CellId::new("1".to_string());
recorder.start_cell(&cell, "new-exec");
let call = record_nested_call(&recorder, &cell, "nested");
recorder.register_cell(&cell, "wait");
recorder.finish_cell_recording(&cell);
let mut prompt = [wait_input("wait", &cell), output("wait")];
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
let mut expected = output("wait");
expected.append_executed_tool_calls(vec![call]);
expected.set_tool_call_cell_id("new-exec");
assert_eq!(prompt[1], expected);
let mut compacted = [output("wait")];
recorder.attach_to_prompt(&mut compacted, &mut HashMap::new());
assert_eq!(compacted[0], expected);
}
#[test]
fn wrapper_mismatch_cannot_become_complete_on_a_later_wait() {
for scenario in [
"missing_exec",
"wrong_output",
"wrong_wait_cell",
"changed_exec",
] {
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("runtime".to_string());
recorder.start_cell(&cell, "exec");
let call = record_nested_call(&recorder, &cell, "nested");
let mut prompt = match scenario {
"missing_exec" => vec![exec_output("exec")],
"changed_exec" => vec![exec_input("exec"), exec_output("exec")],
"wrong_wait_cell" => {
recorder.register_cell(&cell, "first-wait");
vec![
wait_input("first-wait", &CellId::new("other-runtime".to_string())),
output("first-wait"),
]
}
// An exec custom call cannot authenticate a function output with the same ID.
_ => vec![exec_input("exec"), output("exec")],
};
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
assert_eq!(
prompt
.last()
.unwrap()
.executed_tool_call_metadata()
.unwrap()
.executed_tool_calls,
Some(vec![call]),
);
if scenario == "changed_exec" {
// Invalidate already-retained evidence while its cell is still active.
prompt[0] = wait_input("exec", &cell);
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
}
recorder.register_cell(&cell, "last-wait");
recorder.finish_cell_recording(&cell);
let mut last = [wait_input("last-wait", &cell), output("last-wait")];
recorder.attach_to_prompt(&mut last, &mut HashMap::new());
assert_eq!(tool_calls_complete(&last[1]), None);
}
}
#[test]
fn completeness_rejects_reused_origin() {
let recorder = new_recorder(InitialHistory::New);
let first = CellId::new("first-runtime".to_string());
recorder.start_cell(&first, "reused-exec");
record_nested_call(&recorder, &first, "nested-first");
recorder.finish_cell_recording(&first);
let mut original = [exec_input("reused-exec"), exec_output("reused-exec")];
recorder.attach_pending_to_prompt(&mut original, &mut HashMap::new());
assert_eq!(tool_calls_complete(&original[1]), Some(true));
recorder.attach_pending_to_prompt(&mut [], &mut HashMap::new());
let second = CellId::new("second-runtime".to_string());
recorder.start_cell(&second, "reused-exec");
record_nested_call(&recorder, &second, "nested-second");
recorder.finish_cell_recording(&second);
let mut prompt = [exec_input("reused-exec"), exec_output("reused-exec")];
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
assert_eq!(tool_calls_complete(&prompt[1]), None);
}
#[test]
fn completeness_rejects_direct_id_collision_and_late_calls() {
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("collision-runtime".to_string());
recorder.start_cell(&cell, "exec-collision");
recorder.record_call(
&ToolCall {
tool_name: codex_tools::ToolName::plain("direct_tool"),
call_id: "exec-collision".to_string(),
payload: ToolPayload::Function {
arguments: "{}".to_string(),
},
encrypted_function_args: None,
},
&ToolCallSource::Direct,
ToolMode::Direct,
);
record_nested_call(&recorder, &cell, "nested");
recorder.finish_cell_recording(&cell);
let mut collision = [exec_input("exec-collision"), exec_output("exec-collision")];
recorder.attach_to_prompt(&mut collision, &mut HashMap::new());
assert_eq!(tool_calls_complete(&collision[1]), None);
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("late-runtime".to_string());
recorder.start_cell(&cell, "exec-late");
record_nested_call(&recorder, &cell, "nested-first");
recorder.finish_cell_recording(&cell);
let mut first = [exec_input("exec-late"), exec_output("exec-late")];
recorder.attach_to_prompt(&mut first, &mut HashMap::new());
assert_eq!(tool_calls_complete(&first[1]), Some(true));
record_nested_call(&recorder, &cell, "nested-late");
let mut late = [exec_input("exec-late"), exec_output("exec-late")];
recorder.attach_to_prompt(&mut late, &mut HashMap::new());
assert_eq!(tool_calls_complete(&late[1]), None);
}
#[test]
fn request_truncation_prevents_completion_after_compaction() {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::New);
let cell_id = CellId::new("compacted-cell".to_string());
recorder.start_cell(&cell_id, "exec");
@@ -517,10 +811,10 @@ fn request_truncation_prevents_completion_after_compaction() {
);
}
let mut initial = [output("exec")];
let mut initial = [exec_input("exec"), exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut initial, &mut HashMap::new()));
assert!(
initial[0]
initial[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.executed_tool_calls.as_ref())
.is_some_and(|calls| calls.iter().any(|call| matches!(
@@ -543,10 +837,10 @@ fn request_truncation_prevents_completion_after_compaction() {
recorder.attach_pending_to_prompt(&mut [], &mut HashMap::new());
recorder.register_cell(&cell_id, "wait");
recorder.finish_cell_recording(&cell_id);
let mut final_output = [output("wait")];
let mut final_output = [wait_input("wait", &cell_id), output("wait")];
recorder.attach_pending_to_prompt(&mut final_output, &mut HashMap::new());
assert_eq!(
final_output[0]
final_output[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.tool_calls_complete),
None,
@@ -555,7 +849,7 @@ fn request_truncation_prevents_completion_after_compaction() {
#[test]
fn result_metadata_updates_the_exact_retained_call_and_marks_oversized_retries() {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
let cell_id = CellId::new("metadata-cell".to_string());
recorder.start_cell(&cell_id, "exec");
let call = ExecutedToolCall::new("nested_tool".to_string(), json!({}));
@@ -569,7 +863,7 @@ fn result_metadata_updates_the_exact_retained_call_and_marks_oversized_retries()
}
recorder.finish_cell_recording(&cell_id);
let mut retry_cache = HashMap::new();
let mut initial = [output("exec")];
let mut initial = [exec_input("exec"), exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache));
let metadata = json!({ "all-keys": { "ids": ["R1"] }, "other": true });
@@ -583,11 +877,11 @@ fn result_metadata_updates_the_exact_retained_call_and_marks_oversized_retries()
let mut expected_call = call.clone();
expected_call.set_tool_result_metadata(ToolResultMetadata::new(&metadata));
let mut expected = output("exec");
let mut expected = exec_output("exec");
expected.append_executed_tool_calls(vec![expected_call, call]);
expected.set_tool_call_cell_id("exec");
expected.mark_tool_calls_complete();
let mut retry = [output("exec")];
let mut retry = [exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));
assert_eq!(retry, [expected]);
@@ -596,17 +890,48 @@ fn result_metadata_updates_the_exact_retained_call_and_marks_oversized_retries()
"first",
&json!({ "oversized": "x".repeat(32 * 1024) }),
));
let mut expected = serde_json::to_value(&initial).unwrap();
let mut expected = serde_json::to_value(&initial[1..]).unwrap();
expected[0]["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]["tool_result_metadata"] =
json!("omitted_due_to_size_limit");
let mut retry = [output("exec")];
let mut retry = [exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));
assert_eq!(serde_json::to_value(&retry).unwrap(), expected);
}
#[test]
fn untracked_history_budget_loss_keeps_later_wait_incomplete() {
let recorder = new_recorder(InitialHistory::New);
let cell = CellId::new("running-cell".to_string());
recorder.start_cell(&cell, "exec");
recorder.record_nested_tool_call(
cell.clone(),
"nested".to_string(),
ExecutedToolCall::new("nested_tool".to_string(), json!("x".repeat(8190))),
/*original_bytes*/ 8192,
);
let mut prompt = vec![exec_input("exec"), exec_output("exec")];
for index in 0..64 {
let mut item = output(&format!("untracked-{index}"));
item.append_executed_tool_calls(vec![ExecutedToolCall::new(
"other_tool".to_string(),
json!("x".repeat(1024)),
)]);
prompt.push(item);
}
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
assert!(prompt[1].executed_tool_call_metadata().is_none());
recorder.attach_to_prompt(&mut [], &mut HashMap::new());
recorder.register_cell(&cell, "wait");
recorder.finish_cell_recording(&cell);
let mut wait = [wait_input("wait", &cell), output("wait")];
recorder.attach_to_prompt(&mut wait, &mut HashMap::new());
assert_eq!(tool_calls_complete(&wait[1]), None);
}
#[test]
fn result_metadata_shedding_preserves_completion_after_compaction() {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::New);
let cell_id = CellId::new("compacted-cell".to_string());
recorder.start_cell(&cell_id, "exec");
@@ -627,7 +952,10 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
runtime_tool_call_id: "runtime-call".to_string(),
};
let mut retry_cache = HashMap::new();
recorder.attach_pending_to_prompt(&mut [output("exec")], &mut retry_cache);
recorder.attach_pending_to_prompt(
&mut [exec_input("exec"), exec_output("exec")],
&mut retry_cache,
);
// This snapshot fits alone, but pushes the four calls over the aggregate budget.
assert!(recorder.record_tool_result_metadata(
&source,
@@ -640,7 +968,7 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
"nested-1",
&json!({ "status": "unavailable" })
));
let mut initial = [output("exec")];
let mut initial = [exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache));
let calls = initial[0]
.executed_tool_call_metadata()
@@ -651,7 +979,7 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
call.set_tool_result_metadata(ToolResultMetadata::new(&json!("omitted_due_to_size_limit")));
}
assert_eq!(calls, &expected_calls);
let mut retry = [output("exec")];
let mut retry = [exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));
assert_eq!(retry, initial);
@@ -660,10 +988,10 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
assert!(recorder.record_tool_result_metadata(&source, "nested-2", &metadata));
let mut expected_calls = calls.clone();
expected_calls[2].set_tool_result_metadata(ToolResultMetadata::new(&metadata));
let mut expected = output("exec");
let mut expected = exec_output("exec");
expected.append_executed_tool_calls(expected_calls);
expected.set_tool_call_cell_id("exec");
let mut retry = [output("exec")];
let mut retry = [exec_output("exec")];
assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));
assert_eq!(retry, [expected]);
@@ -672,10 +1000,10 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
assert!(!recorder.record_tool_result_metadata(&source, "nested-2", &metadata));
recorder.register_cell(&cell_id, "wait");
recorder.finish_cell_recording(&cell_id);
let mut final_output = [output("wait")];
let mut final_output = [wait_input("wait", &cell_id), output("wait")];
recorder.attach_pending_to_prompt(&mut final_output, &mut HashMap::new());
assert_eq!(
final_output[0]
final_output[1]
.executed_tool_call_metadata()
.and_then(|metadata| metadata.tool_calls_complete),
Some(true),
@@ -684,7 +1012,7 @@ fn result_metadata_shedding_preserves_completion_after_compaction() {
#[test]
fn finished_cells_without_more_waits_do_not_block_new_calls() {
let recorder = new_recorder();
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
let call = ExecutedToolCall::new("nested_tool".to_string(), json!({}));
for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS {
let cell = CellId::new(format!("cell-{index}"));
@@ -695,7 +1023,10 @@ fn finished_cells_without_more_waits_do_not_block_new_calls() {
call.clone(),
/*original_bytes*/ 2,
);
recorder.attach_pending_to_prompt(&mut [output(cell.as_str())], &mut HashMap::new());
recorder.attach_pending_to_prompt(
&mut [exec_input(cell.as_str()), exec_output(cell.as_str())],
&mut HashMap::new(),
);
recorder.finish_cell_recording(&cell);
}
let fresh = CellId::new("fresh".to_string());
@@ -707,11 +1038,11 @@ fn finished_cells_without_more_waits_do_not_block_new_calls() {
/*original_bytes*/ 2,
);
recorder.finish_cell_recording(&fresh);
let mut expected = output("fresh-output");
let mut expected = exec_output("fresh-output");
expected.append_executed_tool_calls(vec![call]);
expected.set_tool_call_cell_id("fresh-output");
expected.mark_tool_calls_complete();
let mut items = [output("fresh-output")];
let mut items = [exec_input("fresh-output"), exec_output("fresh-output")];
assert!(recorder.attach_pending_to_prompt(&mut items, &mut HashMap::new()));
assert_eq!(items, [expected]);
assert_eq!(items, [exec_input("fresh-output"), expected]);
}
@@ -0,0 +1,133 @@
//! Tracks seen call and runtime cell IDs with bounded memory. Bits are never cleared:
//! collisions can withhold a proof, but cannot make an observed ID appear fresh again.
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;
use codex_code_mode::CellId;
use codex_history::InitialHistory;
use codex_history::RolloutItem;
use codex_protocol::models::ResponseItem;
const ID_FILTER_WORDS: usize = 8 * 1024;
const MAX_HISTORY_ENTRIES: usize = 100_000;
const MAX_HISTORY_ID_BYTES: usize = 4 * 1024 * 1024;
// IDs are untrusted strings. A longer ID cannot prove freshness without unbounded hashing.
const MAX_ID_BYTES: usize = 1024;
pub(super) struct SeenIds {
bits: Box<[u64]>,
history_ids_indexed: bool,
}
impl Default for SeenIds {
fn default() -> Self {
Self {
bits: vec![0; ID_FILTER_WORDS].into_boxed_slice(),
history_ids_indexed: true,
}
}
}
impl SeenIds {
pub(super) fn unobserved_history() -> Self {
// Recording started after session initialization, so earlier IDs are unknown.
Self {
history_ids_indexed: false,
..Self::default()
}
}
pub(super) fn from_history(history: &InitialHistory) -> Self {
// Index the supplied history, including paginated context. Missing older pages
// do not invalidate a new cell whose calls and outputs are observed locally.
let mut tracker = Self::default();
let mut entries_left = MAX_HISTORY_ENTRIES;
let mut id_bytes_left = MAX_HISTORY_ID_BYTES;
'history: for item in history.get_rollout_items() {
if entries_left == 0 {
tracker.history_ids_indexed = false;
break;
}
entries_left -= 1;
match item {
RolloutItem::ResponseItem(item)
if !tracker.observe_history_item(item, &mut id_bytes_left) =>
{
tracker.history_ids_indexed = false;
break;
}
RolloutItem::Compacted(item) => {
for item in item.replacement_history.as_deref().unwrap_or_default() {
if entries_left == 0 {
tracker.history_ids_indexed = false;
break 'history;
}
entries_left -= 1;
if !tracker.observe_history_item(item, &mut id_bytes_left) {
tracker.history_ids_indexed = false;
break 'history;
}
}
}
_ => {}
}
}
tracker
}
/// Fresh relative to IDs observed in the supplied history and this recorder.
pub(super) fn observe_call_id(&mut self, id: &str) -> bool {
self.observe(/*namespace*/ 0, id)
}
pub(super) fn observe_runtime_cell_id(&mut self, id: &CellId) -> bool {
// Runtime handles are tracked only within this recorder's lifetime.
self.observe(/*namespace*/ 1, id.as_str())
}
pub(super) fn history_ids_indexed(&self) -> bool {
self.history_ids_indexed
}
fn observe_history_item(&mut self, item: &ResponseItem, bytes_left: &mut usize) -> bool {
let call_id = super::input_call_id(item).or_else(|| super::output_call_id(item));
// Metadata cell_id identifies the originating exec call, not a runtime handle.
let exec_origin = item
.executed_tool_call_metadata()
.and_then(|metadata| metadata.cell_id.as_deref());
for id in [call_id, exec_origin].into_iter().flatten() {
if id.len() > MAX_ID_BYTES || id.len() > *bytes_left {
return false;
}
*bytes_left -= id.len();
self.observe_call_id(id);
}
true
}
fn observe(&mut self, namespace: u8, id: &str) -> bool {
if id.len() > MAX_ID_BYTES {
return false;
}
let mut hasher = DefaultHasher::new();
(namespace, id).hash(&mut hasher);
let hash = hasher.finish();
let first = hash as usize;
let step = (hash >> 32) as usize | 1;
let mut fresh = false;
for probe in 0_usize..4 {
let bit = first.wrapping_add(probe.wrapping_mul(step)) % (ID_FILTER_WORDS * 64);
let word = &mut self.bits[bit / 64];
let mask = 1_u64 << (bit % 64);
fresh |= *word & mask == 0;
*word |= mask;
}
fresh
}
}
#[cfg(test)]
#[path = "seen_ids_tests.rs"]
mod tests;
@@ -0,0 +1,14 @@
use super::*;
#[test]
fn id_namespaces_are_separate_and_saturated_filters_reject_new_ids() {
let mut tracker = SeenIds::default();
assert!(tracker.observe_call_id("shared-id"));
assert!(tracker.observe_runtime_cell_id(&CellId::new("shared-id".to_string())));
assert!(!tracker.observe_call_id("shared-id"));
assert!(!tracker.observe_runtime_cell_id(&CellId::new("shared-id".to_string())));
tracker.bits.fill(u64::MAX);
assert!(!tracker.observe_call_id("another-origin"));
assert!(!tracker.observe_runtime_cell_id(&CellId::new("another-runtime".to_string())));
}
+4 -2
View File
@@ -709,8 +709,10 @@ mod tests {
.features
.enable(Feature::ExecutedToolCallMetadata)
.expect("test feature must be configurable");
let recorder =
crate::tools::executed_tool_calls::ExecutedToolCalls::new(&turn.config.features);
let recorder = crate::tools::executed_tool_calls::ExecutedToolCalls::new(
&turn.config.features,
&codex_history::InitialHistory::New,
);
session.services.executed_tool_calls = recorder.clone();
let turn = Arc::new(turn);
let handler = McpHandler::new(tool_info("filesystem", "filesystem", "read_file"))