mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Include nested Apps tool results in Code Mode sampling requests (#62423)
## Why Result body capture covered direct Apps calls, leaving eligible calls made inside Code Mode without their result bodies in follow-up sampling metadata. ## What changed Capture eligible nested MCP results and bind them to the originating `exec` or `wait` output for the current turn. Send `tool_result` and `tool_result_call_id` together when both fit the request budget. Keep the bodies out of conversation history and reclaim capture capacity when the output binding is lost. Require analytics to be enabled and the provider to allow request metadata before starting turn-level capture. Check the call's credentials at dispatch and again when recording the result. ## Testing Add coverage for yielded and finished outputs, late results, retries with replacement output items, capture budget recovery, and the request size boundary for sending the body and call ID together. GitOrigin-RevId: 0983261ac39021099d14efa5c9a9acac47ac9269
This commit is contained in:
@@ -26,6 +26,10 @@ use std::task::Poll;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "common_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
pub const WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY: &str = "ws_request_header_traceparent";
|
||||
pub const WS_REQUEST_HEADER_TRACESTATE_CLIENT_METADATA_KEY: &str = "ws_request_header_tracestate";
|
||||
|
||||
@@ -81,12 +85,22 @@ pub(crate) fn with_tool_results(
|
||||
return None;
|
||||
}
|
||||
const FIELD: &str = r#","tool_result":"#;
|
||||
let Some(next_remaining) = remaining.checked_sub(FIELD.len() + body.compact_bytes()) else {
|
||||
const CALL_ID_FIELD: &str = r#","tool_result_call_id":"#;
|
||||
let call_id_field_bytes = body
|
||||
.nested_call_id_json()
|
||||
.map_or(0, |_| CALL_ID_FIELD.len());
|
||||
let Some(next_remaining) =
|
||||
remaining.checked_sub(FIELD.len() + body.compact_bytes() + call_id_field_bytes)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
enriched.push_str(&json[cursor..close]);
|
||||
enriched.push_str(FIELD);
|
||||
enriched.push_str(body.as_json());
|
||||
if let Some(id) = body.nested_call_id_json() {
|
||||
enriched.push_str(CALL_ID_FIELD);
|
||||
enriched.push_str(id);
|
||||
}
|
||||
cursor = close;
|
||||
remaining = next_remaining;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//! Checks that private nested call IDs and their results share the wire budget.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::MAX_RESPONSE_MESSAGE_BYTES;
|
||||
use super::with_tool_results;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn nested_call_id_and_result_fit_the_wire_budget_together() {
|
||||
let call_id = "private-\"id\\\nλ";
|
||||
let result = json!({"content": [{"type": "text", "text": "private result"}]});
|
||||
let mcp_result = serde_json::from_value(result.clone()).unwrap();
|
||||
let mut body = ToolResultBody::from_mcp_result(&mcp_result).unwrap();
|
||||
body.set_nested_call_id(call_id);
|
||||
assert!(!format!("{body:?}").contains("private"));
|
||||
let bodies = [(0, 0, Arc::new(body))];
|
||||
let mut request = json!({"input": [{"internal_chat_message_metadata_passthrough": {
|
||||
"executed_tool_calls": [{"name": "search", "arguments": {}}]
|
||||
}}]})
|
||||
.to_string();
|
||||
let enriched = with_tool_results(&request, &bodies).unwrap();
|
||||
let wire: Value = serde_json::from_str(&enriched).unwrap();
|
||||
assert_eq!(
|
||||
wire["input"][0]["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0],
|
||||
json!({"name": "search", "arguments": {}, "tool_result": result, "tool_result_call_id": call_id})
|
||||
);
|
||||
|
||||
request.push_str(&" ".repeat(MAX_RESPONSE_MESSAGE_BYTES - enriched.len()));
|
||||
assert_eq!(
|
||||
with_tool_results(&request, &bodies).unwrap().len(),
|
||||
MAX_RESPONSE_MESSAGE_BYTES
|
||||
);
|
||||
request.push(' ');
|
||||
assert!(with_tool_results(&request, &bodies).is_none());
|
||||
}
|
||||
@@ -468,7 +468,10 @@ impl Session {
|
||||
).await?,
|
||||
None => task_input,
|
||||
};
|
||||
if task_kind == TaskKind::Regular {
|
||||
if task_kind == TaskKind::Regular
|
||||
&& session.services.executed_tool_calls.captures_result_bodies()
|
||||
&& session.services.analytics_events_client.is_enabled()
|
||||
{
|
||||
_tool_result_capture = session.services.executed_tool_calls.begin_turn(&ctx);
|
||||
}
|
||||
task_for_run
|
||||
|
||||
@@ -90,6 +90,44 @@ struct RetainedResultBodies {
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
/// Permission at dispatch for this call. Holding it must not extend the originating turn.
|
||||
pub(super) struct ResultBodyCapture<'a> {
|
||||
turn: Weak<ResultBodyTurn>,
|
||||
nested_call_id: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl ResultBodyCapture<'_> {
|
||||
pub(super) fn record(
|
||||
self,
|
||||
result: &CallToolResult,
|
||||
permits_source: impl FnOnce(AuthMode) -> bool,
|
||||
) -> Option<Arc<PendingResultBody>> {
|
||||
let turn = self.turn.upgrade()?;
|
||||
if !permits_source(turn.auth_mode) || !turn.is_current() {
|
||||
return None;
|
||||
}
|
||||
let mut body = ToolResultBody::from_mcp_result(result)?;
|
||||
if let Some(call_id) = self.nested_call_id {
|
||||
body.set_nested_call_id(call_id);
|
||||
}
|
||||
let mut retained = turn.lock_bodies();
|
||||
if !turn.is_current()
|
||||
|| retained.by_id.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS
|
||||
|| body.compact_bytes() > MAX_TURN_RESULT_BODY_BYTES - retained.bytes
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let id = Uuid::new_v4();
|
||||
retained.bytes += body.compact_bytes();
|
||||
retained.by_id.insert(id, Arc::new(body));
|
||||
Some(Arc::new(PendingResultBody {
|
||||
turn: self.turn,
|
||||
id,
|
||||
accepted: AtomicBool::new(false),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Refund the provisional slot when a callback blocks or the call is cancelled.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PendingResultBody {
|
||||
@@ -99,14 +137,6 @@ pub(crate) struct PendingResultBody {
|
||||
}
|
||||
|
||||
impl PendingResultBody {
|
||||
fn record(turn: &Arc<ResultBodyTurn>, result: &CallToolResult) -> Option<Arc<Self>> {
|
||||
Some(Arc::new(Self {
|
||||
id: turn.record(result)?,
|
||||
turn: Arc::downgrade(turn),
|
||||
accepted: AtomicBool::new(false),
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) fn accept(&self) -> Option<Uuid> {
|
||||
self.turn.upgrade().filter(|turn| turn.is_current())?;
|
||||
self.accepted.store(true, Ordering::Relaxed);
|
||||
@@ -149,21 +179,17 @@ impl ResultBodyTurn {
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
pub(super) fn record(&self, result: &CallToolResult) -> Option<Uuid> {
|
||||
fn start_capture<'a>(
|
||||
self: &Arc<Self>,
|
||||
nested_call_id: Option<&'a str>,
|
||||
) -> Option<ResultBodyCapture<'a>> {
|
||||
if !self.is_current() {
|
||||
return None;
|
||||
}
|
||||
let body = ToolResultBody::from_mcp_result(result)?;
|
||||
let mut retained = self.lock_bodies();
|
||||
if retained.by_id.len() >= MAX_PENDING_EXECUTED_TOOL_CALLS
|
||||
|| body.compact_bytes() > MAX_TURN_RESULT_BODY_BYTES - retained.bytes
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let id = Uuid::new_v4();
|
||||
retained.bytes += body.compact_bytes();
|
||||
retained.by_id.insert(id, Arc::new(body));
|
||||
Some(id)
|
||||
Some(ResultBodyCapture {
|
||||
turn: Arc::downgrade(self),
|
||||
nested_call_id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Select the existing call records on this authorized sampling request.
|
||||
@@ -246,6 +272,7 @@ struct ExecutedToolCallRecorderState {
|
||||
/// Keep each output's calls and completion marker together through replay and pruning.
|
||||
#[derive(Default)]
|
||||
struct RetainedToolCalls {
|
||||
output_item_id: Option<ResponseItemId>,
|
||||
calls: Vec<ExecutedToolCall>,
|
||||
complete: bool,
|
||||
cell_id: Option<String>,
|
||||
@@ -259,9 +286,19 @@ struct RetainedToolCalls {
|
||||
}
|
||||
|
||||
impl RetainedToolCalls {
|
||||
fn clear_late_truncated_metadata(&mut self) {
|
||||
fn invalidate_output(&mut self, turn: &Weak<ResultBodyTurn>) {
|
||||
if self.output_item_id.take().is_some() {
|
||||
for call in &mut self.calls {
|
||||
ResultBodyTurn::discard_call(turn, call);
|
||||
}
|
||||
}
|
||||
self.clear_late_truncated_metadata(turn);
|
||||
}
|
||||
|
||||
fn clear_late_truncated_metadata(&mut self, turn: &Weak<ResultBodyTurn>) {
|
||||
for index in self.late_truncated_indices.drain() {
|
||||
if let Some(call) = self.calls.get_mut(index) {
|
||||
ResultBodyTurn::discard(turn, call.tool_result_id());
|
||||
call.set_tool_result_metadata(ToolResultMetadata::default());
|
||||
self.result_metadata_updated = true;
|
||||
}
|
||||
@@ -281,6 +318,7 @@ enum CellCompletion {
|
||||
|
||||
#[derive(Default)]
|
||||
struct RecordedCell {
|
||||
output_item_id: Option<ResponseItemId>,
|
||||
pending_calls: IndexMap<String, ExecutedToolCall>,
|
||||
completion: CellCompletion,
|
||||
originating_call_id: Option<String>,
|
||||
@@ -289,19 +327,30 @@ struct RecordedCell {
|
||||
dispatch_closed: bool,
|
||||
}
|
||||
|
||||
impl RecordedCell {
|
||||
fn invalidate_binding(&mut self, turn: &Weak<ResultBodyTurn>) {
|
||||
// Unbound rows lose their IDs at attachment. Only a live turn has bytes to release here.
|
||||
if std::mem::take(&mut self.truncated_metadata_binding_valid) && turn.strong_count() != 0 {
|
||||
for call in self.pending_calls.values_mut() {
|
||||
ResultBodyTurn::discard_call(turn, call);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
cell.invalidate_binding(&self.body_turn);
|
||||
}
|
||||
}
|
||||
for ((_, output_id), retained) in &mut self.retained_calls {
|
||||
if output_id == origin || retained.cell_id.as_deref() == Some(origin) {
|
||||
retained.complete = false;
|
||||
retained.clear_late_truncated_metadata();
|
||||
retained.invalidate_output(&self.body_turn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -319,12 +368,12 @@ impl ExecutedToolCallRecorderState {
|
||||
fn invalidate_cell(&mut self, cell_id: &CellId) {
|
||||
if let Some(cell) = self.cells.get_mut(cell_id) {
|
||||
cell.completion = CellCompletion::Incomplete;
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
cell.invalidate_binding(&self.body_turn);
|
||||
}
|
||||
for retained in self.retained_calls.values_mut() {
|
||||
if retained.runtime_cell_id.as_ref() == Some(cell_id) {
|
||||
retained.complete = false;
|
||||
retained.clear_late_truncated_metadata();
|
||||
retained.invalidate_output(&self.body_turn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -425,7 +474,8 @@ impl ExecutedToolCalls {
|
||||
let info = turn.provider.info();
|
||||
// The built-in provider declares optional organization/project selectors. The
|
||||
// transport checks the resolved headers again and declines either active selector.
|
||||
if info.is_amazon_bedrock()
|
||||
if !info.include_internal_metadata
|
||||
|| info.is_amazon_bedrock()
|
||||
|| info.is_gcp()
|
||||
|| !provider_uses_first_party_auth_path(info)
|
||||
|| info.gateway_oauth.is_some()
|
||||
@@ -482,19 +532,24 @@ impl ExecutedToolCalls {
|
||||
.filter(|capture| capture.turn_id == turn_id)
|
||||
}
|
||||
|
||||
pub(super) fn capture_mcp_result(
|
||||
fn current_result_turn(&self, turn: &TurnContext) -> Option<Arc<ResultBodyTurn>> {
|
||||
self.result_body_turn(&turn.sub_id).filter(|capture| {
|
||||
capture.is_current()
|
||||
&& capture
|
||||
.origin
|
||||
.ptr_eq(&Arc::downgrade(&turn.turn_metadata_state))
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn start_mcp_result_capture<'a>(
|
||||
&self,
|
||||
turn: &TurnContext,
|
||||
call: &codex_mcp::PreparedMcpCall,
|
||||
result: &CallToolResult,
|
||||
) -> Option<Arc<PendingResultBody>> {
|
||||
let capture = self.result_body_turn(&turn.sub_id).filter(|capture| {
|
||||
capture
|
||||
.origin
|
||||
.ptr_eq(&Arc::downgrade(&turn.turn_metadata_state))
|
||||
&& call.permits_result_body(capture.auth_mode)
|
||||
})?;
|
||||
PendingResultBody::record(&capture, result)
|
||||
nested_call_id: Option<&'a str>,
|
||||
) -> Option<ResultBodyCapture<'a>> {
|
||||
self.current_result_turn(turn)
|
||||
.filter(|capture| call.permits_result_body(capture.auth_mode))?
|
||||
.start_capture(nested_call_id)
|
||||
}
|
||||
|
||||
pub(crate) fn record_mcp_source(&self, source: McpAttributionSource) {
|
||||
@@ -652,6 +707,17 @@ impl ExecutedToolCalls {
|
||||
item_id_generator: &codex_protocol::ItemIdGenerator,
|
||||
) {
|
||||
let Some((mut call, permit)) = prepared else {
|
||||
if self.captures_result_bodies
|
||||
&& let Some(state) = self.lock_state().as_mut()
|
||||
&& state.body_turn.strong_count() != 0
|
||||
&& let Some(id) = output_call_id(item).and_then(|id| state.output_cells.get(id))
|
||||
&& let Some(cell) = state.cells.get_mut(id)
|
||||
&& cell.truncated_metadata_binding_valid
|
||||
{
|
||||
// Bind nested results to the actual exec/wait output before history projection.
|
||||
crate::session::assign_missing_item_id(item, item_id_generator);
|
||||
cell.output_item_id = item.id().cloned();
|
||||
}
|
||||
return;
|
||||
};
|
||||
let mut state = self.lock_state();
|
||||
@@ -736,10 +802,18 @@ impl ExecutedToolCalls {
|
||||
if !matches!(source, ToolCallSource::CodeMode { .. }) {
|
||||
return;
|
||||
}
|
||||
// Release the lock before calling the output's trait method.
|
||||
let recording = self.lock_state().is_some();
|
||||
if recording {
|
||||
self.record_result_metadata(source, call_id, result.tool_result_metadata());
|
||||
// Release the recorder lock before reading the output. Only refund this turn on failure.
|
||||
let Some(turn) = self
|
||||
.lock_state()
|
||||
.as_ref()
|
||||
.map(|state| state.body_turn.clone())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let metadata = result.tool_result_metadata();
|
||||
let id = metadata.result_id();
|
||||
if !self.record_result_metadata(source, call_id, metadata) {
|
||||
ResultBodyTurn::discard(&turn, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -791,7 +865,7 @@ impl ExecutedToolCalls {
|
||||
if !unique_nested_id {
|
||||
for retained in state.retained_calls.values_mut() {
|
||||
if retained.truncated_call_index_by_id.contains_key(&call_id) {
|
||||
retained.clear_late_truncated_metadata();
|
||||
retained.clear_late_truncated_metadata(&state.body_turn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -815,7 +889,7 @@ impl ExecutedToolCalls {
|
||||
let cell = state.cells.entry(cell_id).or_default();
|
||||
let duplicate_call_id = cell.pending_calls.contains_key(&call_id);
|
||||
if !unique_nested_id || duplicate_call_id {
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
cell.invalidate_binding(&state.body_turn);
|
||||
}
|
||||
let call = if at_pending_call_limit {
|
||||
ExecutedToolCall::truncated(call.name, original_bytes, /*max_bytes*/ 0)
|
||||
@@ -857,12 +931,20 @@ impl ExecutedToolCalls {
|
||||
let Some(state) = state.as_mut() else {
|
||||
return false;
|
||||
};
|
||||
let call = state
|
||||
.cells
|
||||
.get_mut(&CellId::new(cell_id.clone()))
|
||||
.and_then(|cell| cell.pending_calls.get_mut(call_id));
|
||||
if let Some(call) = call {
|
||||
let turn = &state.body_turn;
|
||||
let update = |call: &mut ExecutedToolCall, can_bind_body: bool| {
|
||||
if call.tool_result_id() != metadata.result_id() {
|
||||
ResultBodyTurn::discard(turn, call.tool_result_id());
|
||||
}
|
||||
call.set_tool_result_metadata(metadata);
|
||||
if !can_bind_body {
|
||||
ResultBodyTurn::discard_call(turn, call);
|
||||
}
|
||||
};
|
||||
if let Some(cell) = state.cells.get_mut(&CellId::new(cell_id.clone()))
|
||||
&& let Some(call) = cell.pending_calls.get_mut(call_id)
|
||||
{
|
||||
update(call, cell.truncated_metadata_binding_valid);
|
||||
return true;
|
||||
}
|
||||
let retained_call = state.retained_calls.values_mut().find_map(|retained| {
|
||||
@@ -875,7 +957,10 @@ impl ExecutedToolCalls {
|
||||
if let Some((retained, index)) = retained_call {
|
||||
// Older retry copies must not overwrite this output's accepted result metadata.
|
||||
retained.result_metadata_updated = true;
|
||||
retained.calls[index].set_tool_result_metadata(metadata);
|
||||
update(
|
||||
&mut retained.calls[index],
|
||||
retained.output_item_id.is_some(),
|
||||
);
|
||||
return true;
|
||||
}
|
||||
let mut candidates = state.retained_calls.values_mut().filter_map(|retained| {
|
||||
@@ -893,7 +978,10 @@ impl ExecutedToolCalls {
|
||||
}
|
||||
retained.late_truncated_indices.insert(index);
|
||||
retained.result_metadata_updated = true;
|
||||
retained.calls[index].set_tool_result_metadata(metadata);
|
||||
update(
|
||||
&mut retained.calls[index],
|
||||
retained.output_item_id.is_some(),
|
||||
);
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
@@ -279,7 +279,7 @@ impl ExecutedToolCalls {
|
||||
}
|
||||
for cell_id in &untrusted_cells {
|
||||
if let Some(cell) = state.cells.get_mut(cell_id) {
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
cell.invalidate_binding(&state.body_turn);
|
||||
}
|
||||
}
|
||||
for retained in state.retained_calls.values_mut() {
|
||||
@@ -288,7 +288,7 @@ impl ExecutedToolCalls {
|
||||
.as_ref()
|
||||
.is_some_and(|id| untrusted_cells.contains(id))
|
||||
{
|
||||
retained.clear_late_truncated_metadata();
|
||||
retained.invalidate_output(&state.body_turn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -330,6 +330,7 @@ impl ExecutedToolCalls {
|
||||
cached.clone()
|
||||
} else {
|
||||
let mut runtime_cell_id = None;
|
||||
let mut output_item_id = None;
|
||||
let mut calls = Vec::new();
|
||||
let mut call_index_by_id = HashMap::new();
|
||||
let mut truncated_call_index_by_id = HashMap::new();
|
||||
@@ -361,8 +362,13 @@ impl ExecutedToolCalls {
|
||||
cell.completion = CellCompletion::Incomplete;
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
}
|
||||
if cell.truncated_metadata_binding_valid
|
||||
&& cell.output_item_id.as_ref() == item.id()
|
||||
{
|
||||
output_item_id = cell.output_item_id.take();
|
||||
}
|
||||
let pending_calls = cell.pending_calls.len();
|
||||
for (call_id, call) in cell.pending_calls.drain(..) {
|
||||
for (call_id, mut call) in cell.pending_calls.drain(..) {
|
||||
if matches!(call.arguments(), ExecutedToolCallArguments::Raw(_)) {
|
||||
call_index_by_id.insert(call_id, calls.len());
|
||||
} else if cell.truncated_metadata_binding_valid
|
||||
@@ -370,6 +376,9 @@ impl ExecutedToolCalls {
|
||||
{
|
||||
truncated_call_index_by_id.insert(call_id, calls.len());
|
||||
}
|
||||
if output_item_id.is_none() {
|
||||
ResultBodyTurn::discard_call(&state.body_turn, &mut call);
|
||||
}
|
||||
calls.push(call);
|
||||
}
|
||||
complete = cell.completion == CellCompletion::Complete
|
||||
@@ -407,6 +416,7 @@ impl ExecutedToolCalls {
|
||||
state.retained_calls.insert(
|
||||
key,
|
||||
RetainedToolCalls {
|
||||
output_item_id,
|
||||
calls: calls.clone(),
|
||||
complete,
|
||||
cell_id: cell_id.clone(),
|
||||
@@ -434,9 +444,11 @@ impl ExecutedToolCalls {
|
||||
// Compaction may retry with a shortened history without installing that history.
|
||||
// Keep absent observations until a normal sampling request confirms the live window.
|
||||
if request_kind == RequestKind::Sampling && !pending_outputs.is_empty() {
|
||||
state
|
||||
.retained_calls
|
||||
.retain(|key, _| !pending_outputs.contains(key));
|
||||
for key in pending_outputs {
|
||||
if let Some(mut retained) = state.retained_calls.remove(&key) {
|
||||
retained.invalidate_output(&state.body_turn);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut invalid_cells = HashSet::new();
|
||||
for index in 0..items.len() {
|
||||
@@ -476,6 +488,9 @@ impl ExecutedToolCalls {
|
||||
let intact =
|
||||
output_counts.get(call_id) == Some(&1) && matches_inventory && matches_input;
|
||||
retained.complete &= intact;
|
||||
if !intact {
|
||||
retained.invalidate_output(&state.body_turn);
|
||||
}
|
||||
if !intact && let Some(cell) = retained.runtime_cell_id.clone() {
|
||||
invalid_cells.insert(cell);
|
||||
}
|
||||
@@ -483,7 +498,7 @@ impl ExecutedToolCalls {
|
||||
for cell_id in &invalid_cells {
|
||||
if let Some(cell) = state.cells.get_mut(cell_id) {
|
||||
cell.completion = CellCompletion::Incomplete;
|
||||
cell.truncated_metadata_binding_valid = false;
|
||||
cell.invalidate_binding(&state.body_turn);
|
||||
}
|
||||
}
|
||||
if !invalid_cells.is_empty() {
|
||||
@@ -494,8 +509,8 @@ impl ExecutedToolCalls {
|
||||
.is_some_and(|cell_id| invalid_cells.contains(cell_id))
|
||||
{
|
||||
retained.complete = false;
|
||||
retained.invalidate_output(&state.body_turn);
|
||||
retained.call_index_by_id.clear();
|
||||
retained.clear_late_truncated_metadata();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -507,9 +522,15 @@ impl ExecutedToolCalls {
|
||||
continue;
|
||||
};
|
||||
let key = (std::mem::discriminant(&*item), call_id.to_string());
|
||||
let Some(retained) = state.retained_calls.get(&key) else {
|
||||
continue;
|
||||
};
|
||||
let retained = state.retained_calls.get(&key);
|
||||
if state.body_turn.strong_count() != 0
|
||||
&& retained
|
||||
.and_then(|row| row.output_item_id.as_ref())
|
||||
.is_none_or(|id| Some(id) != item.id())
|
||||
{
|
||||
item.clear_tool_result_ids();
|
||||
}
|
||||
let Some(retained) = retained else { continue };
|
||||
if retained.complete {
|
||||
item.mark_tool_calls_complete();
|
||||
} else {
|
||||
|
||||
@@ -9,6 +9,7 @@ use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::tools::executed_tool_calls::direct_tests::connector_result_turn;
|
||||
|
||||
impl ExecutedToolCalls {
|
||||
fn record_tool_result_metadata(
|
||||
@@ -136,6 +137,138 @@ fn selected_exec_result_uses_only_the_retained_wrapper_inventory() {
|
||||
assert!(!recorder.exec_output_matches(&exec, &tools));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capture_only_request_metadata_omits_unbound_calls_and_empty_waits() {
|
||||
let mut features = Features::default();
|
||||
features.enable(Feature::ConnectorToolResultPassthrough);
|
||||
let recorder = ExecutedToolCalls::new(&features, &InitialHistory::New);
|
||||
// No body turn or grant exists. Connector-looking names and provider metadata are insufficient.
|
||||
let mut items = [
|
||||
output("shell"),
|
||||
exec_output("exec"),
|
||||
output("wait"),
|
||||
ResponseItem::from(ResponseInputItem::ToolSearchOutput {
|
||||
call_id: "search".into(),
|
||||
status: "completed".into(),
|
||||
execution: "client".into(),
|
||||
tools: vec![json!({"type": "function", "name": "existing_tool"})],
|
||||
}),
|
||||
];
|
||||
items[0].append_executed_tool_calls(vec![ExecutedToolCall::new(
|
||||
"exec_command".into(),
|
||||
json!({"cmd": "pwd"}),
|
||||
)]);
|
||||
let mut unbound = ExecutedToolCall::new("mcp__codex_apps__search".into(), json!({}));
|
||||
unbound.set_tool_result_metadata(ToolResultMetadata::new(&json!({
|
||||
"openai/resource_access": {"connector": "example"}
|
||||
})));
|
||||
items[1].append_executed_tool_calls(vec![unbound]);
|
||||
items[3].append_executed_tool_calls(vec![ExecutedToolCall::new(
|
||||
"tool_search".into(),
|
||||
json!({"query": "tools"}),
|
||||
)]);
|
||||
for item in &mut items {
|
||||
item.set_turn_id_if_missing("original-turn");
|
||||
item.set_create_time_if_missing(123.into());
|
||||
item.set_tool_call_cell_id("exec");
|
||||
item.mark_tool_calls_complete();
|
||||
}
|
||||
let original = items.clone();
|
||||
let mut scoped = items.clone();
|
||||
for item in &mut scoped {
|
||||
item.clear_executed_tool_calls();
|
||||
}
|
||||
for enabled in [false, true, false] {
|
||||
features.set_enabled(Feature::ExecutedToolCallMetadata, enabled);
|
||||
recorder.refresh(&features);
|
||||
let saved = recorder.scope_request_metadata(&mut items);
|
||||
assert_eq!(
|
||||
items,
|
||||
if enabled {
|
||||
original.clone()
|
||||
} else {
|
||||
scoped.clone()
|
||||
}
|
||||
);
|
||||
assert_eq!(saved.len(), if enabled { 0 } else { 4 });
|
||||
ExecutedToolCalls::restore_request_metadata(&mut items, saved);
|
||||
assert_eq!(items, original);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn capture_only_request_metadata_preserves_late_bindings_and_restores_full_inventory() {
|
||||
let turn = connector_result_turn().await;
|
||||
let mut features = turn.config.features.clone();
|
||||
features.disable(Feature::ExecutedToolCallMetadata).unwrap();
|
||||
let recorder = ExecutedToolCalls::new(&features, &InitialHistory::New);
|
||||
let capture = recorder.begin_turn(&turn).unwrap();
|
||||
let cell = CellId::new("cell".to_string());
|
||||
recorder.start_cell(&cell, "exec");
|
||||
for id in ["shell", "connector", "late"] {
|
||||
recorder.record_nested_tool_call(
|
||||
cell.clone(),
|
||||
id.into(),
|
||||
ExecutedToolCall::new(id.into(), json!({})),
|
||||
/*original_bytes*/ 2,
|
||||
);
|
||||
}
|
||||
let record = |id: &str| {
|
||||
let source = ToolCallSource::CodeMode {
|
||||
cell_id: cell.as_str().to_string(),
|
||||
runtime_tool_call_id: id.into(),
|
||||
};
|
||||
let output =
|
||||
captured_mcp_output(&capture, json!({"content": [{"type": "text", "text": id}]}));
|
||||
recorder.record_accepted_result(&source, id, &output);
|
||||
};
|
||||
record("connector");
|
||||
recorder.finish_cell_recording(&cell);
|
||||
let mut output = exec_output("exec");
|
||||
recorder.attach_direct_call_to_output(
|
||||
&mut output,
|
||||
/*prepared*/ None,
|
||||
&codex_protocol_test_support::uuid_v7_item_id_generator_for_test(),
|
||||
);
|
||||
let mut items = [exec_input("exec"), output];
|
||||
let history = items.clone();
|
||||
let mut retry_cache = HashMap::new();
|
||||
recorder.attach_to_prompt(&mut items, &mut retry_cache);
|
||||
let selected = |items: &[ResponseItem]| {
|
||||
capture
|
||||
.for_input(items, &turn.auth_manager.as_ref().unwrap().auth_owner())
|
||||
.into_iter()
|
||||
.map(|(item, row, _)| (item, row))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(selected(&items), [(1, 1)]);
|
||||
assert_eq!(tool_calls_complete(&items[1]), Some(true));
|
||||
let original = items.clone();
|
||||
let saved = recorder.scope_request_metadata(&mut items);
|
||||
assert_eq!(selected(&items), [(1, 0)]);
|
||||
assert_eq!(tool_calls_complete(&items[1]), None);
|
||||
assert_eq!(
|
||||
items[1]
|
||||
.executed_tool_call_metadata()
|
||||
.unwrap()
|
||||
.cell_id
|
||||
.as_deref(),
|
||||
Some("exec"),
|
||||
);
|
||||
assert!(recorder.scope_request_metadata(&mut items).is_empty());
|
||||
ExecutedToolCalls::restore_request_metadata(&mut items, saved);
|
||||
assert_eq!(items, original);
|
||||
|
||||
// The omitted row still has its original live slot when its result arrives after publication.
|
||||
record("late");
|
||||
items = history;
|
||||
recorder.attach_to_prompt(&mut items, &mut retry_cache);
|
||||
assert_eq!(selected(&items), [(1, 1), (1, 2)]);
|
||||
let _ = recorder.scope_request_metadata(&mut items);
|
||||
assert_eq!(selected(&items), [(1, 0), (1, 1)]);
|
||||
assert_eq!(tool_calls_complete(&items[1]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compaction_attaches_pending_and_retained_code_mode_metadata() {
|
||||
for previously_sampled in [false, true] {
|
||||
@@ -1248,9 +1381,103 @@ fn large_wrapped_arguments_preserve_later_wait_completeness() {
|
||||
assert_eq!(tool_calls_complete(&wait[1]), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries() {
|
||||
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
|
||||
fn captured_mcp_output(
|
||||
capture: &Arc<ResultBodyTurn>,
|
||||
result: JsonValue,
|
||||
) -> crate::tools::context::McpToolOutput {
|
||||
let result = serde_json::from_value(result).unwrap();
|
||||
crate::tools::context::McpToolOutput {
|
||||
result_capture: capture
|
||||
.start_capture(/*nested_call_id*/ None)
|
||||
.and_then(|call| call.record(&result, |_| true)),
|
||||
result,
|
||||
tool_input: JsonValue::Null,
|
||||
result_metadata_capture_allowed: true,
|
||||
wall_time: std::time::Duration::ZERO,
|
||||
original_image_detail_supported: false,
|
||||
truncation_policy: codex_utils_output_truncation::TruncationPolicy::Bytes(1024),
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case::test_case(false; "pruned from sampling")]
|
||||
#[test_case::test_case(true; "invalidated output")]
|
||||
#[tokio::test]
|
||||
async fn nested_result_budget_reopens_after_binding_loss(invalidate_origin: bool) {
|
||||
let turn = connector_result_turn().await;
|
||||
let recorder = ExecutedToolCalls::new(&turn.config.features, &InitialHistory::New);
|
||||
let capture = recorder.begin_turn(&turn).unwrap();
|
||||
let record = |origin: &str, id: &str, text_bytes: usize| {
|
||||
let source = ToolCallSource::CodeMode {
|
||||
cell_id: origin.to_string(),
|
||||
runtime_tool_call_id: id.to_string(),
|
||||
};
|
||||
let output = captured_mcp_output(
|
||||
&capture,
|
||||
json!({
|
||||
"content": [{"type": "text", "text": "x".repeat(text_bytes)}], "_meta": {"id": id}
|
||||
}),
|
||||
);
|
||||
recorder.record_accepted_result(&source, id, &output);
|
||||
};
|
||||
let bind = |id: &str, text_bytes: usize| {
|
||||
let cell = CellId::new(id.to_string());
|
||||
recorder.start_cell(&cell, id);
|
||||
record_nested_call(&recorder, &cell, id);
|
||||
record(id, id, text_bytes);
|
||||
let mut output = exec_output(id);
|
||||
recorder.attach_direct_call_to_output(
|
||||
&mut output,
|
||||
/*prepared*/ None,
|
||||
&codex_protocol_test_support::uuid_v7_item_id_generator_for_test(),
|
||||
);
|
||||
[exec_input(id), output]
|
||||
};
|
||||
let selected = |items: &[ResponseItem]| {
|
||||
capture
|
||||
.for_input(items, &turn.auth_manager.as_ref().unwrap().auth_owner())
|
||||
.into_iter()
|
||||
.map(|(item, row, _)| (item, row))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let mut history = bind("old", 600 * 1024).to_vec();
|
||||
record_nested_call(&recorder, &CellId::new("old".to_string()), "late");
|
||||
history.extend(bind("survivor", 1));
|
||||
let mut retry_cache = HashMap::new();
|
||||
let mut original = history.clone();
|
||||
recorder.attach_to_prompt(&mut original, &mut retry_cache);
|
||||
recorder.attach_to_compaction_prompt(&mut history[2..].to_vec());
|
||||
assert_eq!(selected(&original), [(1, 0), (3, 0)]);
|
||||
|
||||
if invalidate_origin {
|
||||
recorder.observe_non_dispatched_call(&history[0]);
|
||||
record("old", "late", 600 * 1024);
|
||||
} else {
|
||||
recorder.attach_to_prompt(&mut history[2..].to_vec(), &mut retry_cache);
|
||||
}
|
||||
// An already projected request still holds the old UUID.
|
||||
assert_eq!(selected(&original), [(3, 0)]);
|
||||
history.extend(bind("replacement", 600 * 1024));
|
||||
recorder.attach_to_prompt(&mut history, &mut retry_cache);
|
||||
assert_eq!(selected(&history), [(3, 0), (5, 0)]);
|
||||
if invalidate_origin {
|
||||
let output = serde_json::to_value(&history[1]).unwrap();
|
||||
assert_eq!(
|
||||
output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][1]["tool_result_metadata"],
|
||||
json!({"id": "late"}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case::test_case(false; "yielded")]
|
||||
#[test_case::test_case(true; "finished")]
|
||||
#[tokio::test]
|
||||
async fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries(
|
||||
finished: bool,
|
||||
) {
|
||||
let turn = connector_result_turn().await;
|
||||
let recorder =
|
||||
ExecutedToolCalls::new(&turn.config.features, &InitialHistory::Forked(Vec::new()));
|
||||
let capture = recorder.begin_turn(&turn).unwrap();
|
||||
let cell_id = CellId::new("metadata-cell".to_string());
|
||||
recorder.start_cell(&cell_id, "exec");
|
||||
let call = ExecutedToolCall::new("nested_tool".to_string(), json!({}));
|
||||
@@ -1262,10 +1489,17 @@ fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries()
|
||||
/*original_bytes*/ 2,
|
||||
);
|
||||
}
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
if finished {
|
||||
recorder.finish_cell_recording(&cell_id);
|
||||
}
|
||||
let mut retry_cache = HashMap::new();
|
||||
let mut initial = [exec_input("exec"), exec_output("exec")];
|
||||
assert!(recorder.attach_pending_to_prompt(&mut initial, &mut retry_cache));
|
||||
let mut history = [exec_input("exec"), exec_output("exec")];
|
||||
recorder.attach_direct_call_to_output(
|
||||
&mut history[1],
|
||||
/*prepared*/ None,
|
||||
&codex_protocol_test_support::uuid_v7_item_id_generator_for_test(),
|
||||
);
|
||||
assert!(recorder.attach_pending_to_prompt(&mut history.clone(), &mut retry_cache));
|
||||
|
||||
let metadata = json!({ "all-keys": { "ids": ["R1"] }, "other": true });
|
||||
let source = |cell_id: &str| ToolCallSource::CodeMode {
|
||||
@@ -1276,12 +1510,32 @@ fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries()
|
||||
assert!(!recorder.record_tool_result_metadata(&source(cell_id.as_str()), "unknown", &metadata));
|
||||
assert!(recorder.record_tool_result_metadata(&source(cell_id.as_str()), "first", &metadata));
|
||||
|
||||
// The unbound result and the second call each fit, but together exceed the turn budget.
|
||||
let json = json!({"content": [{"type": "text", "text": "private".repeat(80 * 1024)}]});
|
||||
for id in ["unknown", "second"] {
|
||||
let output = captured_mcp_output(&capture, json.clone());
|
||||
recorder.record_accepted_result(&source(cell_id.as_str()), id, &output);
|
||||
}
|
||||
assert!(recorder.attach_pending_to_prompt(&mut history, &mut retry_cache));
|
||||
let bodies = capture.for_input(&history, &turn.auth_manager.as_ref().unwrap().auth_owner());
|
||||
let [(1, 1, body)] = bodies.as_slice() else {
|
||||
panic!("expected the second call's result")
|
||||
};
|
||||
assert_eq!(
|
||||
serde_json::from_str::<JsonValue>(body.as_json()).unwrap(),
|
||||
json
|
||||
);
|
||||
assert!(!serde_json::to_string(&history).unwrap().contains("private"));
|
||||
|
||||
// A fresh output with the same model call ID recovers only provider metadata.
|
||||
let mut expected_call = call.clone();
|
||||
expected_call.set_tool_result_metadata(ToolResultMetadata::new(&metadata));
|
||||
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();
|
||||
if finished {
|
||||
expected.mark_tool_calls_complete();
|
||||
}
|
||||
let mut retry = [exec_output("exec")];
|
||||
assert!(recorder.attach_pending_to_prompt(&mut retry, &mut retry_cache));
|
||||
assert_eq!(retry, [expected]);
|
||||
@@ -1292,7 +1546,7 @@ fn result_metadata_updates_the_exact_retained_call_and_preserves_large_retries()
|
||||
"first",
|
||||
&large_metadata,
|
||||
));
|
||||
let mut expected = serde_json::to_value(&initial[1..]).unwrap();
|
||||
let mut expected = serde_json::to_value(&retry).unwrap();
|
||||
expected[0]["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]["tool_result_metadata"] =
|
||||
large_metadata;
|
||||
let mut retry = [exec_output("exec")];
|
||||
|
||||
@@ -125,6 +125,57 @@ pub(super) async fn connector_result_turn() -> TurnContext {
|
||||
turn
|
||||
}
|
||||
|
||||
#[test_case::test_case(512; "shed on attach")]
|
||||
#[test_case::test_case(MAX_RETAINED_DIRECT_METADATA_BYTES; "pruned from sampling")]
|
||||
#[tokio::test]
|
||||
async fn direct_result_body_budget_reopens_when_the_output_is_lost(available: usize) {
|
||||
let turn = connector_result_turn().await;
|
||||
let recorder = ExecutedToolCalls::new(&turn.config.features, &InitialHistory::New);
|
||||
let capture = recorder.begin_turn(&turn).unwrap();
|
||||
recorder.retained_direct_metadata_bytes.store(
|
||||
MAX_RETAINED_DIRECT_METADATA_BYTES - available,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
let bodies = |items: &[ResponseItem]| {
|
||||
capture.for_input(items, &turn.auth_manager.as_ref().unwrap().auth_owner())
|
||||
};
|
||||
// Each body fits by itself. Only the first call's larger arguments can force row shedding.
|
||||
let result = serde_json::from_value(json!({
|
||||
"content": [{"type": "text", "text": "x".repeat(600 * 1024)}]
|
||||
}))
|
||||
.unwrap();
|
||||
let attach = |id: &str, args| {
|
||||
let mut call = ExecutedToolCall::new("read".into(), args);
|
||||
let body_id = capture
|
||||
.start_capture(/*nested_call_id*/ None)
|
||||
.and_then(|call| call.record(&result, |_| true))
|
||||
.and_then(|pending| pending.accept())
|
||||
.expect("room for the Direct body");
|
||||
call.set_tool_result_metadata(ToolResultMetadata::default().with_result_id(Some(body_id)));
|
||||
let mut item = output(id);
|
||||
recorder.attach_direct_call_to_output(
|
||||
&mut item,
|
||||
Some((call, recorder.reserve_direct_call().unwrap())),
|
||||
&codex_protocol_test_support::uuid_v7_item_id_generator_for_test(),
|
||||
);
|
||||
item
|
||||
};
|
||||
let mut first = [attach("first", json!({"argument": "x".repeat(1024)}))];
|
||||
if available == MAX_RETAINED_DIRECT_METADATA_BYTES {
|
||||
recorder.attach_to_compaction_prompt(&mut []);
|
||||
recorder.attach_to_prompt(&mut first, &mut HashMap::new());
|
||||
assert!(matches!(bodies(&first).as_slice(), [(0, 0, _)]));
|
||||
recorder.attach_to_prompt(&mut [], &mut HashMap::new());
|
||||
} else {
|
||||
assert!(first[0].executed_tool_call_metadata().is_none());
|
||||
}
|
||||
|
||||
let [first] = first;
|
||||
let mut retry = [first, attach("second", json!({}))];
|
||||
recorder.attach_to_prompt(&mut retry, &mut HashMap::new());
|
||||
assert!(matches!(bodies(&retry).as_slice(), [(1, 0, _)]));
|
||||
}
|
||||
|
||||
#[test_case::test_case(1, 600 * 1024; "byte limit")]
|
||||
#[test_case::test_case(MAX_PENDING_EXECUTED_TOOL_CALLS, 1; "count limit")]
|
||||
#[tokio::test]
|
||||
@@ -191,7 +242,12 @@ async fn direct_result_bodies_require_the_originating_output_turn_and_owner(
|
||||
let attach = |id: &str| {
|
||||
let mut call = ExecutedToolCall::new("read".into(), json!({"id": id}));
|
||||
call.set_tool_result_metadata(
|
||||
ToolResultMetadata::new(&metadata).with_result_id(capture.record(&result)),
|
||||
ToolResultMetadata::new(&metadata).with_result_id(
|
||||
capture
|
||||
.start_capture(/*nested_call_id*/ None)
|
||||
.and_then(|call| call.record(&result, |_| true))
|
||||
.and_then(|pending| pending.accept()),
|
||||
),
|
||||
);
|
||||
let mut item = output(id);
|
||||
recorder.attach_direct_call_to_output(
|
||||
|
||||
@@ -300,7 +300,16 @@ impl McpHandler {
|
||||
.map(TruncationPolicy::Tokens)
|
||||
.unwrap_or(step_context.settings.model_info.truncation_policy.into());
|
||||
let started = Instant::now();
|
||||
let recorder = &session.services.executed_tool_calls;
|
||||
let capture = prepared
|
||||
.filter(|_| result_metadata_capture_allowed)
|
||||
.and_then(|call| {
|
||||
let child_id =
|
||||
matches!(source, ToolCallSource::CodeMode { .. }).then_some(call_id.as_str());
|
||||
session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.start_mcp_result_capture(&step_context.turn, call, child_id)
|
||||
});
|
||||
let mut result_capture = None;
|
||||
// Bound the compiler's async-layout query depth for approval and dispatch.
|
||||
let result = Box::pin(handle_mcp_tool_call(
|
||||
@@ -316,11 +325,10 @@ impl McpHandler {
|
||||
&source,
|
||||
payload,
|
||||
|call, raw| {
|
||||
if result_metadata_capture_allowed
|
||||
&& !matches!(source, ToolCallSource::CodeMode { .. })
|
||||
{
|
||||
result_capture = recorder.capture_mcp_result(&step_context.turn, call, raw);
|
||||
}
|
||||
// Recovery may have selected an independent credential after dispatch.
|
||||
result_capture = capture.and_then(|capture| {
|
||||
capture.record(raw, |session_auth| call.permits_result_body(session_auth))
|
||||
});
|
||||
},
|
||||
))
|
||||
.await;
|
||||
|
||||
@@ -2345,6 +2345,27 @@ async fn direct_result_metadata_retained_budget_preserves_resource_access() -> R
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apps_rpc_call_id(rpc: &Value) -> &str {
|
||||
rpc["params"]["_meta"]["_codex_apps"]["call_id"]
|
||||
.as_str()
|
||||
.expect("Apps request carries the executed call ID")
|
||||
}
|
||||
|
||||
fn assert_nested_apps_call_ids(rpcs: &[Value]) {
|
||||
let mut ids = std::collections::HashSet::new();
|
||||
for rpc in rpcs {
|
||||
let call_id = apps_rpc_call_id(rpc);
|
||||
let suffix = call_id
|
||||
.strip_prefix("exec-")
|
||||
.expect("nested call ID prefix");
|
||||
let uuid = uuid::Uuid::parse_str(suffix).expect("nested call UUID");
|
||||
assert_eq!(uuid.get_version(), Some(uuid::Version::Random));
|
||||
assert_eq!(uuid.get_variant(), uuid::Variant::RFC4122);
|
||||
assert_eq!(suffix, uuid.to_string());
|
||||
assert!(ids.insert(call_id), "nested call IDs must be distinct");
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case(ToolMode::Direct; "direct")]
|
||||
#[test_case(ToolMode::CodeModeOnly; "code_mode")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
@@ -2481,9 +2502,9 @@ async fn result_metadata_preserves_results_within_request_budget(
|
||||
!direct,
|
||||
);
|
||||
let request = follow_up.single_request();
|
||||
let mut actual_arguments = recorded_apps_tool_calls(&server)
|
||||
.await
|
||||
.into_iter()
|
||||
let executed = recorded_apps_tool_calls(&server).await;
|
||||
let mut actual_arguments = executed
|
||||
.iter()
|
||||
.map(|call| {
|
||||
assert_eq!(call["params"]["name"], RESULT_METADATA_TOOL);
|
||||
call["params"]["arguments"].clone()
|
||||
@@ -2492,7 +2513,12 @@ async fn result_metadata_preserves_results_within_request_budget(
|
||||
actual_arguments.sort_by_key(Value::to_string);
|
||||
assert_eq!(actual_arguments, arguments);
|
||||
if direct {
|
||||
for call_id in call_ids {
|
||||
for (call_id, args) in call_ids.iter().zip(&arguments) {
|
||||
let rpc = executed
|
||||
.iter()
|
||||
.find(|rpc| &rpc["params"]["arguments"] == args)
|
||||
.expect("each direct call reached the Apps server");
|
||||
assert_eq!(apps_rpc_call_id(rpc), *call_id);
|
||||
let output = request.function_call_output(call_id);
|
||||
assert_eq!(
|
||||
&output["output"].as_array().expect("direct output content")[1..],
|
||||
@@ -2504,6 +2530,7 @@ async fn result_metadata_preserves_results_within_request_budget(
|
||||
assert!(!output["output"].to_string().contains("payload"));
|
||||
}
|
||||
} else {
|
||||
assert_nested_apps_call_ids(&executed);
|
||||
let (body, success) = custom_tool_output_body_and_success(&request, "call-1");
|
||||
assert_ne!(success, Some(false), "Code Mode failed: {body}");
|
||||
assert_eq!(
|
||||
|
||||
@@ -575,7 +575,10 @@ impl ToolResultMetadata {
|
||||
/// Projected successful Apps result, kept separately from the ordinary tool output and history.
|
||||
#[derive(derive_more::Debug)]
|
||||
#[debug("ToolResultBody([redacted])")]
|
||||
pub struct ToolResultBody(Box<str>);
|
||||
pub struct ToolResultBody {
|
||||
json: Box<str>,
|
||||
nested_call_id_json: Option<Box<str>>,
|
||||
}
|
||||
|
||||
impl ToolResultBody {
|
||||
/// Projects only plaintext and structured content that fits the receiver's limits.
|
||||
@@ -619,15 +622,28 @@ impl ToolResultBody {
|
||||
structured,
|
||||
};
|
||||
let json = to_json_string_bounded(&body, MAX_TOOL_RESULT_BYTES).ok()?;
|
||||
Some(Self(json.into_boxed_str()))
|
||||
Some(Self {
|
||||
json: json.into_boxed_str(),
|
||||
nested_call_id_json: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Keep the execution ID with the private body so both fit or are omitted together.
|
||||
pub fn set_nested_call_id(&mut self, call_id: &str) {
|
||||
let id = Value::String(call_id.to_owned()).to_string();
|
||||
self.nested_call_id_json = Some(id.into_boxed_str());
|
||||
}
|
||||
|
||||
pub fn nested_call_id_json(&self) -> Option<&str> {
|
||||
self.nested_call_id_json.as_deref()
|
||||
}
|
||||
|
||||
pub fn compact_bytes(&self) -> usize {
|
||||
self.0.len()
|
||||
self.json.len() + self.nested_call_id_json().map_or(0, str::len)
|
||||
}
|
||||
|
||||
pub fn as_json(&self) -> &str {
|
||||
&self.0
|
||||
&self.json
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user