Bind direct tool-call metadata to invocation outputs (#45185)

## Why

Direct tool-call records need to stay associated with the invocation that produced each output, including when call IDs are reused. Completeness must describe the recorded call inventory, independently of tool success.

## What changed

- Attach direct-call records to outputs before they enter history, and set `tool_calls_complete` when the invocation's arguments are fully recorded.
- Bound pending recordings and retained metadata, release reservations on completion or cancellation, and invalidate pending records when capture is disabled.
- Apply request budgets to direct metadata and strip it from inference and compaction inputs when capture is disabled.
- Remove executed-call metadata from app-server raw response notifications and exclude its size from Guardian history retention budgets.
- Track call IDs that bypass dispatch so their reuse cannot incorrectly establish Code Mode completeness.

## Testing

Add regression coverage for direct-call attribution, malformed calls, metadata budgets, cancellation, configuration changes, compaction, notification filtering, and Guardian context isolation.

GitOrigin-RevId: 2ebd39c7f141d04788736491495109841656b4c0
This commit is contained in:
ningyi-oai authored and copyberry committed 2026-09-13 06:53:20 +00:00
1 parent e61f381900
commit 1715e55076
24 files changed
+1760 -297

No files matched your search

+7 -1
View File
@@ -14,6 +14,7 @@ use std::sync::Arc;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::models::executed_tool_call_metadata_bytes;
use crate::SectionHistory;
@@ -52,8 +53,10 @@ impl TranscriptHistory {
/// Non-user overflow removes at least half the existing entries; byte limits may need more.
/// Oversized user images fall back to bounded text; other oversized items are skipped.
pub fn record(&mut self, item: &ResponseItem) {
let metadata_bytes = executed_tool_call_metadata_bytes(item);
let mut size = BoundedSize {
bytes: std::mem::size_of::<ResponseItem>(),
max_bytes: MAX_BYTES_PER_KIND.saturating_add(metadata_bytes),
};
let measured = serde_json::to_writer(&mut size, item).and_then(|()| {
// ResponseItem serialization can omit reasoning content that cloning retains.
@@ -111,6 +114,8 @@ impl TranscriptHistory {
}
return;
}
// Analytics must not change which evidence fits Guardian's retention budget.
size.bytes = size.bytes.saturating_sub(metadata_bytes);
let is_user = item.is_user_message();
let (mut count, mut bytes) = self
.items
@@ -178,11 +183,12 @@ impl SectionHistory for TranscriptHistory {
// Count without allocating a serialized copy, and stop before cloning an oversized item.
struct BoundedSize {
bytes: usize,
max_bytes: usize,
}
impl Write for BoundedSize {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
if bytes.len() > MAX_BYTES_PER_KIND.saturating_sub(self.bytes) {
if bytes.len() > self.max_bytes.saturating_sub(self.bytes) {
return Err(io::Error::other(
"review history item exceeds storage budget",
));
@@ -1,3 +1,4 @@
use codex_protocol::models::ExecutedToolCall;
use codex_protocol::models::ReasoningItemContent;
use pretty_assertions::assert_eq;
use serde_json::json;
@@ -91,6 +92,40 @@ fn byte_limits_are_independent_and_oversized_items_do_not_clear_history() {
assert_eq!(history.items().collect::<Vec<_>>(), vec![&keep, &large]);
}
#[test]
fn tool_metadata_is_retained_without_changing_retention_size() {
let before = tool("keep this");
let mut plain = tool(&"x".repeat(MAX_BYTES_PER_KIND - 4096));
plain.set_turn_id_if_missing("turn-1");
let mut recorded = plain.clone();
recorded.append_executed_tool_calls(vec![ExecutedToolCall::new(
"command".to_string(),
json!({"command": "x".repeat(7000)}),
)]);
recorded.mark_tool_calls_complete();
let mut baseline = TranscriptHistory::default();
let mut with_metadata = TranscriptHistory::default();
baseline.reset([&before, &plain]);
with_metadata.reset([&before, &recorded]);
assert_eq!(
with_metadata.items().collect::<Vec<_>>(),
vec![&before, &recorded]
);
assert_eq!(
with_metadata
.items
.iter()
.map(|(_, size)| *size)
.collect::<Vec<_>>(),
baseline
.items
.iter()
.map(|(_, size)| *size)
.collect::<Vec<_>>()
);
assert_eq!(with_metadata.generation(), baseline.generation());
}
#[test]
fn oversized_user_images_preserve_text_and_metadata_in_order() {
let before = tool("earlier tool result");