mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
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:
1 parent
e61f381900
commit
1715e55076
24 files changed
+1760
-297
No files matched your search
@@ -1152,7 +1152,9 @@ pub(crate) async fn apply_bespoke_event_handling(
|
|||||||
.send_server_notification(ServerNotification::HookCompleted(notification))
|
.send_server_notification(ServerNotification::HookCompleted(notification))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
EventMsg::RawResponseItem(raw_response_item_event) => {
|
EventMsg::RawResponseItem(mut raw_response_item_event) => {
|
||||||
|
// Keep warehouse metadata out of app-server notifications.
|
||||||
|
raw_response_item_event.item.clear_executed_tool_calls();
|
||||||
let mut notification = ServerNotification::RawResponseItemCompleted(
|
let mut notification = ServerNotification::RawResponseItemCompleted(
|
||||||
RawResponseItemCompletedNotification {
|
RawResponseItemCompletedNotification {
|
||||||
thread_id: conversation_id.to_string(),
|
thread_id: conversation_id.to_string(),
|
||||||
@@ -3164,6 +3166,7 @@ mod tests {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,9 @@ use codex_protocol::openai_models::ReasoningEffort;
|
|||||||
use codex_protocol::protocol::MULTI_AGENT_MODE_OPEN_TAG;
|
use codex_protocol::protocol::MULTI_AGENT_MODE_OPEN_TAG;
|
||||||
use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS;
|
use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS;
|
||||||
use codex_utils_absolute_path::test_support::PathExt;
|
use codex_utils_absolute_path::test_support::PathExt;
|
||||||
|
use core_test_support::apps_test_server::AppsTestServer;
|
||||||
|
use core_test_support::apps_test_server::SEARCH_CALENDAR_LIST_TOOL;
|
||||||
|
use core_test_support::apps_test_server::SEARCH_CALENDAR_NAMESPACE;
|
||||||
use core_test_support::responses;
|
use core_test_support::responses;
|
||||||
use core_test_support::skip_if_no_network;
|
use core_test_support::skip_if_no_network;
|
||||||
use core_test_support::skip_if_remote;
|
use core_test_support::skip_if_remote;
|
||||||
@@ -106,7 +109,12 @@ use tempfile::TempDir;
|
|||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
|
use wiremock::Mock;
|
||||||
|
use wiremock::Request;
|
||||||
use wiremock::ResponseTemplate;
|
use wiremock::ResponseTemplate;
|
||||||
|
use wiremock::matchers::body_partial_json;
|
||||||
|
use wiremock::matchers::method;
|
||||||
|
use wiremock::matchers::path;
|
||||||
|
|
||||||
use super::analytics::mount_analytics_capture;
|
use super::analytics::mount_analytics_capture;
|
||||||
use super::analytics::wait_for_analytics_event;
|
use super::analytics::wait_for_analytics_event;
|
||||||
@@ -330,6 +338,187 @@ async fn turn_start_omits_notification_media_without_changing_model_input() -> R
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn tool_call_metadata_stays_out_of_raw_response_item_notifications() -> Result<()> {
|
||||||
|
skip_if_no_network!(Ok(()));
|
||||||
|
|
||||||
|
let arguments = json!({"query": "redaction"});
|
||||||
|
let result_metadata = json!({"provider/private": {"sentinel": "raw-result-metadata"}});
|
||||||
|
let responses = vec![
|
||||||
|
responses::sse(vec![
|
||||||
|
responses::ev_response_created("resp-1"),
|
||||||
|
responses::ev_function_call_with_namespace(
|
||||||
|
"mcp-call",
|
||||||
|
SEARCH_CALENDAR_NAMESPACE,
|
||||||
|
SEARCH_CALENDAR_LIST_TOOL,
|
||||||
|
&arguments.to_string(),
|
||||||
|
),
|
||||||
|
responses::ev_completed("resp-1"),
|
||||||
|
]),
|
||||||
|
create_final_assistant_message_sse_response("Done")?,
|
||||||
|
];
|
||||||
|
let server = responses::start_mock_server().await;
|
||||||
|
let model = responses::mount_sse_sequence(&server, responses).await;
|
||||||
|
let apps = AppsTestServer::mount(&server).await?;
|
||||||
|
let tool_result = json!({
|
||||||
|
"content": [{"type": "text", "text": "Calendar result"}],
|
||||||
|
"_meta": result_metadata,
|
||||||
|
"isError": false,
|
||||||
|
});
|
||||||
|
Mock::given(method("POST"))
|
||||||
|
.and(path("/api/codex/ps/mcp"))
|
||||||
|
.and(body_partial_json(json!({
|
||||||
|
"method": "tools/call",
|
||||||
|
"params": {"name": "calendar_list_events", "arguments": arguments},
|
||||||
|
})))
|
||||||
|
.respond_with(move |request: &Request| {
|
||||||
|
let request: Value = serde_json::from_slice(&request.body).unwrap();
|
||||||
|
ResponseTemplate::new(200).set_body_json(json!({
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": request["id"],
|
||||||
|
"result": tool_result,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
.with_priority(/*p*/ 1)
|
||||||
|
.expect(1)
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
let codex_home = TempDir::new()?;
|
||||||
|
MockResponsesConfig::new(&server.uri())
|
||||||
|
.with_provider_name("OpenAI")
|
||||||
|
.with_provider_config("supports_websockets = false")
|
||||||
|
.with_root_config(&format!(
|
||||||
|
"chatgpt_base_url = \"{}\"\ncli_auth_credentials_store = \"file\"",
|
||||||
|
apps.chatgpt_base_url
|
||||||
|
))
|
||||||
|
.enable_feature(Feature::Apps)
|
||||||
|
.enable_feature(Feature::ExecutedToolCallMetadata)
|
||||||
|
.write(codex_home.path())?;
|
||||||
|
write_chatgpt_auth(
|
||||||
|
codex_home.path(),
|
||||||
|
ChatGptAuthFixture::new("chatgpt-test-token")
|
||||||
|
.account_id("account-123")
|
||||||
|
.chatgpt_account_id("account-123")
|
||||||
|
.chatgpt_user_id("user-123")
|
||||||
|
.email("employee@openai.com"),
|
||||||
|
AuthCredentialsStoreMode::File,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut mcp = TestAppServer::builder()
|
||||||
|
.with_codex_home(codex_home.path())
|
||||||
|
.build_initialized()
|
||||||
|
.await?;
|
||||||
|
let ThreadStartResponse { thread, .. } = mcp
|
||||||
|
.start_thread(ThreadStartParams {
|
||||||
|
model: Some("mock-model".to_string()),
|
||||||
|
experimental_raw_events: true,
|
||||||
|
history_mode: Some(ThreadHistoryMode::Legacy),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
let rollout_path = thread.path.context("thread rollout path")?;
|
||||||
|
let _: TurnStartResponse = mcp
|
||||||
|
.request(|request_id| ClientRequest::TurnStart {
|
||||||
|
request_id,
|
||||||
|
params: TurnStartParams {
|
||||||
|
thread_id: thread.id,
|
||||||
|
input: vec![V2UserInput::Text {
|
||||||
|
text: "list calendar events".to_string(),
|
||||||
|
text_elements: Vec::new(),
|
||||||
|
}],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let raw_params = timeout(DEFAULT_READ_TIMEOUT, async {
|
||||||
|
loop {
|
||||||
|
let notification = mcp
|
||||||
|
.read_stream_until_matching_notification("raw output or turn completion", |n| {
|
||||||
|
matches!(
|
||||||
|
n.method.as_str(),
|
||||||
|
"rawResponseItem/completed" | "turn/completed"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
if notification.method == "turn/completed" {
|
||||||
|
anyhow::bail!("missing raw tool output notification");
|
||||||
|
}
|
||||||
|
let params = notification.params.context("raw notification params")?;
|
||||||
|
let item = ¶ms["item"];
|
||||||
|
if item["type"] == "function_call_output" && item["call_id"] == "mcp-call" {
|
||||||
|
return Ok::<Value, anyhow::Error>(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
timeout(
|
||||||
|
DEFAULT_READ_TIMEOUT,
|
||||||
|
mcp.read_stream_until_notification_message("turn/completed"),
|
||||||
|
)
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
let requests = model.requests();
|
||||||
|
assert_eq!(requests.len(), 2);
|
||||||
|
let follow_up = requests[1].body_json();
|
||||||
|
let output = follow_up["input"]
|
||||||
|
.as_array()
|
||||||
|
.context("follow-up model input")?
|
||||||
|
.iter()
|
||||||
|
.find(|item| item["type"] == "function_call_output" && item["call_id"] == "mcp-call")
|
||||||
|
.context("follow-up tool output")?;
|
||||||
|
assert!(serde_json::to_string(&output["output"])?.contains("Calendar result"));
|
||||||
|
assert_eq!(
|
||||||
|
output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"],
|
||||||
|
json!([{"name": "mcp__codex_apps__calendar_list_events", "arguments": arguments}]),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
output["internal_chat_message_metadata_passthrough"]["tool_calls_complete"],
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
let raw_output = &raw_params["item"];
|
||||||
|
assert_eq!(raw_output["output"], output["output"]);
|
||||||
|
assert!(!serde_json::to_string(&raw_params)?.contains("raw-result-metadata"));
|
||||||
|
let public_metadata = raw_output["internal_chat_message_metadata_passthrough"]
|
||||||
|
.as_object()
|
||||||
|
.context("public passthrough metadata")?;
|
||||||
|
assert_eq!(
|
||||||
|
public_metadata.get("turn_id").context("public turn id")?,
|
||||||
|
&output["internal_chat_message_metadata_passthrough"]["turn_id"],
|
||||||
|
);
|
||||||
|
for field in ["executed_tool_calls", "tool_calls_complete", "cell_id"] {
|
||||||
|
assert!(
|
||||||
|
public_metadata.get(field).is_none(),
|
||||||
|
"notification leaked {field}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let status = timeout(DEFAULT_READ_TIMEOUT, mcp.shutdown_gracefully()).await??;
|
||||||
|
assert!(status.success());
|
||||||
|
// Read raw JSON: ResponseItem deserialization intentionally discards host-owned metadata.
|
||||||
|
let rollout = std::fs::read_to_string(rollout_path)?;
|
||||||
|
let entries = rollout
|
||||||
|
.lines()
|
||||||
|
.map(serde_json::from_str::<Value>)
|
||||||
|
.collect::<serde_json::Result<Vec<_>>>()?;
|
||||||
|
let captured = entries
|
||||||
|
.iter()
|
||||||
|
.filter(|entry| entry["type"] == "response_item")
|
||||||
|
.map(|entry| &entry["payload"])
|
||||||
|
.find(|item| item["type"] == "function_call_output" && item["call_id"] == "mcp-call")
|
||||||
|
.context("persisted MCP output")?;
|
||||||
|
assert_eq!(captured["output"], raw_output["output"]);
|
||||||
|
// The custom inference endpoint omits raw metadata, so verify capture in the rollout.
|
||||||
|
let expected_metadata: Option<&Value> = None;
|
||||||
|
assert_eq!(
|
||||||
|
captured["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]
|
||||||
|
.get("tool_result_metadata"),
|
||||||
|
expected_metadata,
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn turn_start_with_empty_input_runs_model_request() -> Result<()> {
|
async fn turn_start_with_empty_input_runs_model_request() -> Result<()> {
|
||||||
let responses = vec![create_final_assistant_message_sse_response("Done")?];
|
let responses = vec![create_final_assistant_message_sse_response("Done")?];
|
||||||
|
|||||||
@@ -266,9 +266,12 @@ async fn run_compact_task_inner_impl(
|
|||||||
|
|
||||||
let compaction_response_id = loop {
|
let compaction_response_id = loop {
|
||||||
// Clone is required because of the loop
|
// Clone is required because of the loop
|
||||||
let turn_input = history
|
let mut turn_input = history
|
||||||
.clone()
|
.clone()
|
||||||
.for_prompt(&turn_context.model_info().input_modalities);
|
.for_prompt(&turn_context.model_info().input_modalities);
|
||||||
|
sess.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.strip_disabled_direct_metadata(&mut turn_input);
|
||||||
let turn_input_len = turn_input.len();
|
let turn_input_len = turn_input.len();
|
||||||
let prompt = Prompt {
|
let prompt = Prompt {
|
||||||
input: turn_input,
|
input: turn_input,
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ pub(super) async fn run_remote_compact_v2_attempt(
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|envelope| (envelope.item, envelope.metadata))
|
.map(|envelope| (envelope.item, envelope.metadata))
|
||||||
.unzip();
|
.unzip();
|
||||||
|
sess.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.strip_disabled_direct_metadata(&mut input);
|
||||||
let tool_router = &step_context.tool_router;
|
let tool_router = &step_context.tool_router;
|
||||||
input.push(ResponseItem::CompactionTrigger {});
|
input.push(ResponseItem::CompactionTrigger {});
|
||||||
let prompt = Prompt {
|
let prompt = Prompt {
|
||||||
|
|||||||
@@ -1,11 +1,157 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::session::tests::make_session_and_context_with_auth_and_config_and_rx;
|
||||||
|
use codex_features::Feature;
|
||||||
use codex_history::CodexHarnessMetadata;
|
use codex_history::CodexHarnessMetadata;
|
||||||
use codex_history::ResponseItemEnvelope;
|
use codex_history::ResponseItemEnvelope;
|
||||||
|
use codex_login::CodexAuth;
|
||||||
|
use codex_model_provider_info::ModelProviderInfo;
|
||||||
use codex_protocol::ResponseItemId;
|
use codex_protocol::ResponseItemId;
|
||||||
use codex_protocol::models::ContentItemKind;
|
use codex_protocol::models::ContentItemKind;
|
||||||
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
|
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
|
||||||
|
use codex_protocol::models::ExecutedToolCall;
|
||||||
|
use codex_protocol::models::FunctionCallOutputPayload;
|
||||||
use codex_protocol::models::InternalChatMessageMetadataPassthrough;
|
use codex_protocol::models::InternalChatMessageMetadataPassthrough;
|
||||||
|
use core_test_support::responses;
|
||||||
|
use core_test_support::skip_if_no_network;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
use serde_json::json;
|
||||||
|
use test_case::test_case;
|
||||||
|
|
||||||
|
#[test_case(true; "metadata enabled")]
|
||||||
|
#[test_case(false; "metadata disabled after capture")]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn local_compaction_respects_tool_metadata_state(
|
||||||
|
metadata_enabled: bool,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
skip_if_no_network!(Ok(()));
|
||||||
|
let server = responses::start_mock_server().await;
|
||||||
|
let provider = ModelProviderInfo::create_openai_provider(Some(format!("{}/v1", server.uri())));
|
||||||
|
let (session, turn, _events) = make_session_and_context_with_auth_and_config_and_rx(
|
||||||
|
CodexAuth::create_dummy_chatgpt_auth_for_testing(),
|
||||||
|
Vec::new(),
|
||||||
|
move |config| {
|
||||||
|
config.model = Some("gpt-5.2".to_string());
|
||||||
|
config.model_provider = provider;
|
||||||
|
config.model_provider.supports_websockets = false;
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("enable tool-call metadata");
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let mut items = vec![user_message("Update the plan")];
|
||||||
|
for index in 0..5 {
|
||||||
|
let call_id = format!("direct-{index}");
|
||||||
|
let arguments = json!({"plan": [{"step": "x".repeat(7 * 1024), "status": "completed"}]});
|
||||||
|
assert!(serde_json::to_vec(&arguments)?.len() < 8 * 1024);
|
||||||
|
items.push(ResponseItem::FunctionCall {
|
||||||
|
id: None,
|
||||||
|
name: "update_plan".to_string(),
|
||||||
|
namespace: None,
|
||||||
|
arguments: arguments.to_string(),
|
||||||
|
encrypted_function_args: None,
|
||||||
|
call_id: call_id.clone(),
|
||||||
|
internal_chat_message_metadata_passthrough: None,
|
||||||
|
});
|
||||||
|
let mut output = ResponseItem::FunctionCallOutput {
|
||||||
|
id: None,
|
||||||
|
call_id: Some(call_id),
|
||||||
|
name: None,
|
||||||
|
namespace: None,
|
||||||
|
output: FunctionCallOutputPayload::from_text("Plan updated".to_string()),
|
||||||
|
internal_chat_message_metadata_passthrough: Some(
|
||||||
|
InternalChatMessageMetadataPassthrough {
|
||||||
|
turn_id: Some("metadata-canary".to_string()),
|
||||||
|
create_time: Some(123.into()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
};
|
||||||
|
output.append_executed_tool_calls(vec![ExecutedToolCall::new(
|
||||||
|
"update_plan".to_string(),
|
||||||
|
arguments,
|
||||||
|
)]);
|
||||||
|
output.mark_tool_calls_complete();
|
||||||
|
items.push(output);
|
||||||
|
}
|
||||||
|
session
|
||||||
|
.record_conversation_items(&turn, turn.model_info(), &items)
|
||||||
|
.await;
|
||||||
|
let live_history = session.clone_history().await;
|
||||||
|
let outputs = live_history
|
||||||
|
.raw_items()
|
||||||
|
.filter_map(|item| match item {
|
||||||
|
ResponseItem::FunctionCallOutput { .. } => Some(serde_json::to_value(item)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<serde_json::Result<Vec<_>>>()?;
|
||||||
|
assert_eq!(outputs.len(), 5);
|
||||||
|
let metadata_bytes: usize = outputs
|
||||||
|
.iter()
|
||||||
|
.map(|item| {
|
||||||
|
serde_json::to_vec(&item["internal_chat_message_metadata_passthrough"])
|
||||||
|
.unwrap()
|
||||||
|
.len()
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
// Compaction does not rebudget source records as a normal inference request.
|
||||||
|
// Passthrough bytes are also excluded from model token estimates.
|
||||||
|
assert!(metadata_bytes > 32 * 1024);
|
||||||
|
|
||||||
|
if !metadata_enabled {
|
||||||
|
let mut config = (*session.get_config().await).clone();
|
||||||
|
config.features.disable(Feature::ExecutedToolCallMetadata)?;
|
||||||
|
session.refresh_runtime_config(config).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mock = responses::mount_sse_once(
|
||||||
|
&server,
|
||||||
|
responses::sse(vec![
|
||||||
|
responses::ev_assistant_message("summary", "The prior calls finished."),
|
||||||
|
responses::ev_completed("compact-response"),
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
// OpenAI identity keeps the client from removing passthrough for compatibility.
|
||||||
|
run_compact_task(
|
||||||
|
Arc::clone(&session),
|
||||||
|
turn,
|
||||||
|
vec![UserInput::Text {
|
||||||
|
text: "Summarize the conversation.".to_string(),
|
||||||
|
text_elements: Vec::new(),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let request = mock.single_request();
|
||||||
|
assert!(request.inputs_of_type("compaction_trigger").is_empty());
|
||||||
|
for mut output in outputs {
|
||||||
|
let call_id = output["call_id"].as_str().expect("source call id");
|
||||||
|
let compact_output = request.function_call_output(call_id);
|
||||||
|
assert_eq!(compact_output["output"], json!("Plan updated"));
|
||||||
|
if !metadata_enabled {
|
||||||
|
let metadata = output["internal_chat_message_metadata_passthrough"]
|
||||||
|
.as_object_mut()
|
||||||
|
.expect("source metadata");
|
||||||
|
metadata.remove("executed_tool_calls");
|
||||||
|
metadata.remove("tool_calls_complete");
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
compact_output["internal_chat_message_metadata_passthrough"],
|
||||||
|
output["internal_chat_message_metadata_passthrough"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let compacted_history = session.clone_history().await;
|
||||||
|
let expected_summary = format!("{SUMMARY_PREFIX}\nThe prior calls finished.");
|
||||||
|
assert!(compacted_history.raw_items().any(|item| {
|
||||||
|
matches!(item, ResponseItem::Message { role, content, .. }
|
||||||
|
if role == "user"
|
||||||
|
&& content_items_to_text(content).as_deref() == Some(expected_summary.as_str()))
|
||||||
|
}));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn annotated(items: Vec<ResponseItem>) -> Vec<ResponseItemEnvelope> {
|
fn annotated(items: Vec<ResponseItem>) -> Vec<ResponseItemEnvelope> {
|
||||||
items.into_iter().map(ResponseItemEnvelope::new).collect()
|
items.into_iter().map(ResponseItemEnvelope::new).collect()
|
||||||
|
|||||||
@@ -2658,6 +2658,47 @@ fn non_base64_image_urls_use_image_estimates() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn passthrough_metadata_does_not_change_context_estimates() {
|
||||||
|
for output in [
|
||||||
|
serde_json::json!({"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hello"}]}),
|
||||||
|
serde_json::json!({"type": "function_call_output", "call_id": "call", "output": "result"}),
|
||||||
|
serde_json::json!({"type": "custom_tool_call_output", "call_id": "call", "output": "result"}),
|
||||||
|
serde_json::json!({"type": "tool_search_output", "call_id": "call", "execution": "client", "status": "completed", "tools": []}),
|
||||||
|
] {
|
||||||
|
let original: ResponseItem = serde_json::from_value(output.clone()).expect("response item");
|
||||||
|
let before = estimate_response_item_model_visible_bytes(&original);
|
||||||
|
for turn_metadata in [
|
||||||
|
None,
|
||||||
|
Some(InternalChatMessageMetadataPassthrough::default()),
|
||||||
|
Some(InternalChatMessageMetadataPassthrough {
|
||||||
|
turn_id: Some("turn-\"你好\"".to_string()),
|
||||||
|
create_time: Some(123.into()),
|
||||||
|
..unknown_content_metadata()
|
||||||
|
}),
|
||||||
|
] {
|
||||||
|
let mut output = output.clone();
|
||||||
|
output["internal_chat_message_metadata_passthrough"] =
|
||||||
|
serde_json::to_value(turn_metadata).expect("turn metadata");
|
||||||
|
let mut item: ResponseItem = serde_json::from_value(output).expect("response item");
|
||||||
|
assert_eq!(estimate_response_item_model_visible_bytes(&item), before);
|
||||||
|
let mut call = codex_protocol::models::ExecutedToolCall::new(
|
||||||
|
"test_tool".to_string(),
|
||||||
|
serde_json::json!({"input": "x".repeat(8 * 1024)}),
|
||||||
|
);
|
||||||
|
call.set_tool_result_metadata(codex_protocol::models::ToolResultMetadata::new(
|
||||||
|
&serde_json::json!({"provider": "x".repeat(16 * 1024)}),
|
||||||
|
));
|
||||||
|
item.append_executed_tool_calls(vec![call]);
|
||||||
|
item.set_tool_call_cell_id("cell");
|
||||||
|
item.mark_tool_calls_complete();
|
||||||
|
assert_eq!(estimate_response_item_model_visible_bytes(&item), before);
|
||||||
|
item.clear_internal_chat_message_metadata_passthrough();
|
||||||
|
assert_eq!(item, original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn encrypted_function_output_uses_plaintext_byte_estimate() {
|
fn encrypted_function_output_uses_plaintext_byte_estimate() {
|
||||||
let encrypted_content = "A".repeat(1_868);
|
let encrypted_content = "A".repeat(1_868);
|
||||||
|
|||||||
@@ -345,6 +345,10 @@ pub(crate) async fn handle_output_item_done(
|
|||||||
}
|
}
|
||||||
// No tool call: convert messages/reasoning into turn items and mark them as complete.
|
// No tool call: convert messages/reasoning into turn items and mark them as complete.
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
|
ctx.sess
|
||||||
|
.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.observe_non_dispatched_call(&item);
|
||||||
let finalized_turn_item = finalize_non_tool_response_item(
|
let finalized_turn_item = finalize_non_tool_response_item(
|
||||||
ctx.sess.as_ref(),
|
ctx.sess.as_ref(),
|
||||||
TurnItemContributorPolicy::Run(ctx.turn_store.as_ref()),
|
TurnItemContributorPolicy::Run(ctx.turn_store.as_ref()),
|
||||||
@@ -381,6 +385,10 @@ pub(crate) async fn handle_output_item_done(
|
|||||||
}
|
}
|
||||||
// The tool request should be answered directly (or was denied); push that response into the transcript.
|
// The tool request should be answered directly (or was denied); push that response into the transcript.
|
||||||
Err(FunctionCallError::RespondToModel(message)) => {
|
Err(FunctionCallError::RespondToModel(message)) => {
|
||||||
|
ctx.sess
|
||||||
|
.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.observe_non_dispatched_call(&item);
|
||||||
let response = ResponseInputItem::FunctionCallOutput {
|
let response = ResponseInputItem::FunctionCallOutput {
|
||||||
call_id: String::new(),
|
call_id: String::new(),
|
||||||
output: FunctionCallOutputPayload {
|
output: FunctionCallOutputPayload {
|
||||||
|
|||||||
@@ -6,14 +6,19 @@ use super::handle_non_tool_response_item;
|
|||||||
use super::handle_output_item_done;
|
use super::handle_output_item_done;
|
||||||
use super::last_assistant_message_from_item;
|
use super::last_assistant_message_from_item;
|
||||||
use super::response_item_may_include_external_context;
|
use super::response_item_may_include_external_context;
|
||||||
|
use crate::session::session::Session;
|
||||||
use crate::session::step_context::StepContext;
|
use crate::session::step_context::StepContext;
|
||||||
use crate::session::tests::make_session_and_context;
|
use crate::session::tests::make_session_and_context;
|
||||||
|
use crate::session::tests::make_session_and_context_with_auth_and_config_and_rx;
|
||||||
use crate::session::tests::tool_registry_for_test_step;
|
use crate::session::tests::tool_registry_for_test_step;
|
||||||
|
use crate::session::turn_context::TurnContext;
|
||||||
use crate::tools::ToolRouter;
|
use crate::tools::ToolRouter;
|
||||||
use crate::tools::parallel::ToolCallRuntime;
|
use crate::tools::parallel::ToolCallRuntime;
|
||||||
use crate::turn_diff_tracker::TurnDiffTracker;
|
use crate::turn_diff_tracker::TurnDiffTracker;
|
||||||
use codex_extension_api::ExtensionData;
|
use codex_extension_api::ExtensionData;
|
||||||
use codex_extension_api::TurnItemContributor;
|
use codex_extension_api::TurnItemContributor;
|
||||||
|
use codex_features::Feature;
|
||||||
|
use codex_login::CodexAuth;
|
||||||
use codex_protocol::ResponseItemId;
|
use codex_protocol::ResponseItemId;
|
||||||
use codex_protocol::items::AgentMessageContent;
|
use codex_protocol::items::AgentMessageContent;
|
||||||
use codex_protocol::items::TurnItem;
|
use codex_protocol::items::TurnItem;
|
||||||
@@ -26,6 +31,7 @@ use codex_protocol::models::LocalShellStatus;
|
|||||||
use codex_protocol::models::MessagePhase;
|
use codex_protocol::models::MessagePhase;
|
||||||
use codex_protocol::models::ResponseItem;
|
use codex_protocol::models::ResponseItem;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
use serde_json::json;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
@@ -279,14 +285,7 @@ async fn handle_non_tool_response_item_runs_turn_item_contributors_only_when_req
|
|||||||
assert_eq!(text, "hello world");
|
assert_eq!(text, "hello world");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
fn output_context(session: Arc<Session>, turn_context: Arc<TurnContext>) -> HandleOutputCtx {
|
||||||
async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
|
||||||
let (mut session, turn_context) = make_session_and_context().await;
|
|
||||||
let mut builder = codex_extension_api::ExtensionRegistryBuilder::new();
|
|
||||||
builder.turn_item_contributor(Arc::new(RewriteAgentMessageContributor));
|
|
||||||
session.services.extensions = Arc::new(builder.build());
|
|
||||||
let session = Arc::new(session);
|
|
||||||
let turn_context = Arc::new(turn_context);
|
|
||||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||||
let (registry, hosted_specs) = tool_registry_for_test_step(step_context.as_ref());
|
let (registry, hosted_specs) = tool_registry_for_test_step(step_context.as_ref());
|
||||||
let router = Arc::new(ToolRouter::from_registry(
|
let router = Arc::new(ToolRouter::from_registry(
|
||||||
@@ -300,14 +299,23 @@ async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
|||||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||||
let tool_runtime =
|
let tool_runtime =
|
||||||
ToolCallRuntime::new(Arc::clone(&session), Arc::clone(&step_context), tracker);
|
ToolCallRuntime::new(Arc::clone(&session), Arc::clone(&step_context), tracker);
|
||||||
let item = assistant_output_text("original assistant text");
|
HandleOutputCtx {
|
||||||
let mut ctx = HandleOutputCtx {
|
|
||||||
sess: session,
|
sess: session,
|
||||||
step_context,
|
step_context,
|
||||||
turn_store: Arc::new(ExtensionData::new(turn_context.sub_id.clone())),
|
turn_store: Arc::new(ExtensionData::new(turn_context.sub_id.clone())),
|
||||||
tool_runtime,
|
tool_runtime,
|
||||||
cancellation_token: CancellationToken::new(),
|
cancellation_token: CancellationToken::new(),
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
||||||
|
let (mut session, turn_context) = make_session_and_context().await;
|
||||||
|
let mut builder = codex_extension_api::ExtensionRegistryBuilder::new();
|
||||||
|
builder.turn_item_contributor(Arc::new(RewriteAgentMessageContributor));
|
||||||
|
session.services.extensions = Arc::new(builder.build());
|
||||||
|
let mut ctx = output_context(Arc::new(session), Arc::new(turn_context));
|
||||||
|
let item = assistant_output_text("original assistant text");
|
||||||
|
|
||||||
let output = handle_output_item_done(&mut ctx, item, /*previously_active_item*/ None)
|
let output = handle_output_item_done(&mut ctx, item, /*previously_active_item*/ None)
|
||||||
.await
|
.await
|
||||||
@@ -319,6 +327,126 @@ async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn direct_results_keep_their_own_records_when_call_ids_repeat() {
|
||||||
|
let (session, turn_context, _events) = make_session_and_context_with_auth_and_config_and_rx(
|
||||||
|
CodexAuth::from_api_key("Test API Key"),
|
||||||
|
Vec::new(),
|
||||||
|
|config| {
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("enable metadata");
|
||||||
|
config.update_plan_enabled = true;
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let mut ctx = output_context(Arc::clone(&session), turn_context);
|
||||||
|
let mut pending = Vec::new();
|
||||||
|
for step in ["read", "write"] {
|
||||||
|
let arguments = json!({"plan": [{"step": step, "status": "in_progress"}]});
|
||||||
|
let call = serde_json::from_value(json!({
|
||||||
|
"type": "function_call", "call_id": "reused", "name": "update_plan",
|
||||||
|
"arguments": arguments.to_string(),
|
||||||
|
}))
|
||||||
|
.expect("direct input");
|
||||||
|
let result = handle_output_item_done(&mut ctx, call, /*previously_active_item*/ None)
|
||||||
|
.await
|
||||||
|
.expect("direct call should be handled");
|
||||||
|
assert!(result.needs_follow_up);
|
||||||
|
pending.push((
|
||||||
|
arguments,
|
||||||
|
result.tool_future.expect("dispatched direct call"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reapplying an enabled config keeps the same recording lifetime.
|
||||||
|
session
|
||||||
|
.refresh_runtime_config((*session.get_config().await).clone())
|
||||||
|
.await;
|
||||||
|
// Await in reverse order: each result must already own its record before history attachment.
|
||||||
|
let mut outputs = Vec::new();
|
||||||
|
for (arguments, future) in pending.into_iter().rev() {
|
||||||
|
let envelope = future.await.expect("plan result");
|
||||||
|
let item = &envelope.item;
|
||||||
|
let ResponseItem::FunctionCallOutput {
|
||||||
|
call_id: output_call_id,
|
||||||
|
output,
|
||||||
|
..
|
||||||
|
} = item
|
||||||
|
else {
|
||||||
|
panic!("expected plan output");
|
||||||
|
};
|
||||||
|
assert_eq!(output_call_id.as_deref(), Some("reused"));
|
||||||
|
assert_eq!(output.text_content(), Some("Plan updated"));
|
||||||
|
let metadata = item
|
||||||
|
.executed_tool_call_metadata()
|
||||||
|
.expect("recorded direct call");
|
||||||
|
assert_eq!(metadata.tool_calls_complete, Some(true));
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&metadata.executed_tool_calls).expect("call metadata"),
|
||||||
|
json!([{
|
||||||
|
"name": "update_plan", "arguments": arguments,
|
||||||
|
}])
|
||||||
|
);
|
||||||
|
outputs.push(envelope.item);
|
||||||
|
}
|
||||||
|
let original = outputs.clone();
|
||||||
|
session
|
||||||
|
.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.attach_to_prompt(&mut outputs, &mut Default::default());
|
||||||
|
assert_eq!(outputs, original);
|
||||||
|
|
||||||
|
let mut pending = Vec::new();
|
||||||
|
for call_id in ["disabled", "re-enabled"] {
|
||||||
|
let call = serde_json::from_value(json!({
|
||||||
|
"type": "function_call", "call_id": call_id, "name": "update_plan",
|
||||||
|
"arguments": "{\"plan\": []}",
|
||||||
|
}))
|
||||||
|
.expect("direct input");
|
||||||
|
pending.push(
|
||||||
|
handle_output_item_done(&mut ctx, call, /*previously_active_item*/ None)
|
||||||
|
.await
|
||||||
|
.expect("direct call should be handled")
|
||||||
|
.tool_future
|
||||||
|
.expect("dispatched direct call"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let mut config = (*session.get_config().await).clone();
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.disable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("disable metadata");
|
||||||
|
session.refresh_runtime_config(config.clone()).await;
|
||||||
|
let result = pending
|
||||||
|
.remove(0)
|
||||||
|
.await
|
||||||
|
.expect("plan result after capture disabled");
|
||||||
|
assert!(result.item.executed_tool_call_metadata().is_none());
|
||||||
|
session
|
||||||
|
.services
|
||||||
|
.executed_tool_calls
|
||||||
|
.attach_to_prompt(&mut outputs, &mut Default::default());
|
||||||
|
let mut expected = original;
|
||||||
|
for item in &mut expected {
|
||||||
|
item.clear_executed_tool_calls();
|
||||||
|
}
|
||||||
|
assert_eq!(outputs, expected);
|
||||||
|
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("re-enable metadata");
|
||||||
|
session.refresh_runtime_config(config).await;
|
||||||
|
let result = pending
|
||||||
|
.pop()
|
||||||
|
.expect("call prepared before disable")
|
||||||
|
.await
|
||||||
|
.expect("plan result after capture re-enabled");
|
||||||
|
assert!(result.item.executed_tool_call_metadata().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn finalized_turn_item_defers_mailbox_for_contributed_visible_text() {
|
async fn finalized_turn_item_defers_mailbox_for_contributed_visible_text() {
|
||||||
let (mut session, turn_context) = make_session_and_context().await;
|
let (mut session, turn_context) = make_session_and_context().await;
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
//! Records attempted calls at existing execution boundaries. Request metadata policy
|
//! Records attempted calls at existing execution boundaries. Request metadata policy
|
||||||
//! lives in the private request metadata module; this recorder must never dispatch or await tools.
|
//! lives in the private request metadata module; this recorder must never dispatch or await tools.
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "executed_tool_calls_direct_tests.rs"]
|
||||||
|
mod direct_tests;
|
||||||
mod request_metadata;
|
mod request_metadata;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -8,6 +11,9 @@ use std::collections::HashSet;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::sync::MutexGuard;
|
use std::sync::MutexGuard;
|
||||||
|
use std::sync::Weak;
|
||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
|
||||||
use codex_code_mode::CellId;
|
use codex_code_mode::CellId;
|
||||||
use codex_features::Feature;
|
use codex_features::Feature;
|
||||||
@@ -37,6 +43,10 @@ use seen_ids::SeenIds;
|
|||||||
const MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES: usize = 8 * 1024;
|
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_EXECUTED_TOOL_CALL_FULL_ARGUMENT_BYTES_PER_OUTPUT: usize = 32 * 1024;
|
||||||
const MAX_PENDING_EXECUTED_TOOL_CALLS: usize = 256;
|
const MAX_PENDING_EXECUTED_TOOL_CALLS: usize = 256;
|
||||||
|
// Limits new Direct metadata retained in history and the append-only rollout
|
||||||
|
// during one recorder lifetime. A new process cannot recover the exact prior
|
||||||
|
// budget because raw result metadata is intentionally not deserialized.
|
||||||
|
const MAX_RETAINED_DIRECT_METADATA_BYTES: usize = 1024 * 1024;
|
||||||
|
|
||||||
type ExecutedToolCallCache =
|
type ExecutedToolCallCache =
|
||||||
HashMap<(std::mem::Discriminant<ResponseItem>, String), Vec<ExecutedToolCall>>;
|
HashMap<(std::mem::Discriminant<ResponseItem>, String), Vec<ExecutedToolCall>>;
|
||||||
@@ -46,11 +56,32 @@ type ExecutedToolCallCache =
|
|||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub(crate) struct ExecutedToolCalls {
|
pub(crate) struct ExecutedToolCalls {
|
||||||
state: Arc<Mutex<Option<ExecutedToolCallRecorderState>>>,
|
state: Arc<Mutex<Option<ExecutedToolCallRecorderState>>>,
|
||||||
|
retained_direct_metadata_bytes: Arc<AtomicUsize>,
|
||||||
|
pending_direct_calls: Arc<AtomicUsize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// The tool future owns this reservation, so completion or cancellation releases it.
|
||||||
|
pub(crate) struct DirectCallPermit {
|
||||||
|
recording: Weak<()>,
|
||||||
|
pending_direct_calls: Arc<AtomicUsize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DirectCallPermit {
|
||||||
|
pub(crate) fn strong_count(&self) -> usize {
|
||||||
|
self.recording.strong_count()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for DirectCallPermit {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.pending_direct_calls.fetch_sub(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct ExecutedToolCallRecorderState {
|
struct ExecutedToolCallRecorderState {
|
||||||
direct_calls: HashMap<String, ExecutedToolCall>,
|
// Disabling drops this lifetime, permanently invalidating prepared Direct records.
|
||||||
|
recording: Arc<()>,
|
||||||
cells: HashMap<CellId, RecordedCell>,
|
cells: HashMap<CellId, RecordedCell>,
|
||||||
output_cells: HashMap<String, CellId>,
|
output_cells: HashMap<String, CellId>,
|
||||||
retained_calls: HashMap<(std::mem::Discriminant<ResponseItem>, String), RetainedToolCalls>,
|
retained_calls: HashMap<(std::mem::Discriminant<ResponseItem>, String), RetainedToolCalls>,
|
||||||
@@ -175,6 +206,8 @@ impl ExecutedToolCalls {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}))),
|
}))),
|
||||||
|
retained_direct_metadata_bytes: Arc::new(AtomicUsize::new(0)),
|
||||||
|
pending_direct_calls: Arc::new(AtomicUsize::new(0)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,12 +243,105 @@ impl ExecutedToolCalls {
|
|||||||
self.record_call(call, source, step_context.tool_router.tool_mode());
|
self.record_call(call, source, step_context.tool_router.tool_mode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Keep a Direct record with its invocation so it can be attached to that invocation's output.
|
||||||
|
pub(crate) fn prepare_direct_call(
|
||||||
|
&self,
|
||||||
|
call: &ToolCall,
|
||||||
|
source: &ToolCallSource,
|
||||||
|
step_context: &StepContext,
|
||||||
|
) -> Option<(ExecutedToolCall, DirectCallPermit)> {
|
||||||
|
if is_code_mode_wrapper(call, source, step_context.tool_router.tool_mode()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let permit = self.reserve_direct_call()?;
|
||||||
|
let (mut recorded, _) = recorded_call(call);
|
||||||
|
let argument_bytes = serialized_json_bytes(recorded.arguments()).unwrap_or(usize::MAX);
|
||||||
|
if matches!(recorded.arguments(), ExecutedToolCallArguments::Raw(_))
|
||||||
|
&& argument_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES
|
||||||
|
{
|
||||||
|
recorded = ExecutedToolCall::truncated(
|
||||||
|
recorded.name,
|
||||||
|
argument_bytes,
|
||||||
|
MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Some((recorded, permit))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reserve_direct_call(&self) -> Option<DirectCallPermit> {
|
||||||
|
let recording = Arc::downgrade(&self.lock_state().as_ref()?.recording);
|
||||||
|
self.pending_direct_calls
|
||||||
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |pending| {
|
||||||
|
(pending < MAX_PENDING_EXECUTED_TOOL_CALLS).then_some(pending + 1)
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
Some(DirectCallPermit {
|
||||||
|
recording,
|
||||||
|
pending_direct_calls: Arc::clone(&self.pending_direct_calls),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate the recording lifetime and attach together so a config refresh
|
||||||
|
/// cannot invalidate the call between those two operations.
|
||||||
|
pub(crate) fn attach_direct_call_to_output(
|
||||||
|
&self,
|
||||||
|
item: &mut ResponseItem,
|
||||||
|
prepared: Option<(ExecutedToolCall, DirectCallPermit)>,
|
||||||
|
) {
|
||||||
|
let Some((call, permit)) = prepared else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let state = self.lock_state();
|
||||||
|
let Some(state) = state.as_ref() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if !permit.recording.ptr_eq(&Arc::downgrade(&state.recording)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let complete = matches!(call.arguments(), ExecutedToolCallArguments::Raw(_));
|
||||||
|
item.append_executed_tool_calls(vec![call]);
|
||||||
|
if complete {
|
||||||
|
item.mark_tool_calls_complete();
|
||||||
|
}
|
||||||
|
let retained = self.retained_direct_metadata_bytes.load(Ordering::Relaxed);
|
||||||
|
let available = MAX_RETAINED_DIRECT_METADATA_BYTES.saturating_sub(retained);
|
||||||
|
let mut bytes = executed_tool_call_metadata_bytes(item);
|
||||||
|
if bytes > available {
|
||||||
|
item.clear_tool_result_metadata();
|
||||||
|
bytes = executed_tool_call_metadata_bytes(item);
|
||||||
|
}
|
||||||
|
if bytes > available {
|
||||||
|
item.clear_executed_tool_calls();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// All Direct attachments share the recorder lock, including cloned handles.
|
||||||
|
self.retained_direct_metadata_bytes
|
||||||
|
.store(retained + bytes, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remember IDs from response items that bypass local tool dispatch.
|
||||||
|
pub(crate) fn observe_non_dispatched_call(&self, item: &ResponseItem) {
|
||||||
|
let Some(call_id) = input_call_id(item) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut state = self.lock_state();
|
||||||
|
let Some(state) = state.as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if !state.seen_ids.observe_call_id(call_id) {
|
||||||
|
state.invalidate_origin(call_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn record_accepted_result(
|
pub(crate) fn record_accepted_result(
|
||||||
&self,
|
&self,
|
||||||
source: &ToolCallSource,
|
source: &ToolCallSource,
|
||||||
call_id: &str,
|
call_id: &str,
|
||||||
result: &dyn ToolOutput,
|
result: &dyn ToolOutput,
|
||||||
) {
|
) {
|
||||||
|
if !matches!(source, ToolCallSource::CodeMode { .. }) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Release the lock before calling the output's trait method.
|
// Release the lock before calling the output's trait method.
|
||||||
let recording = self.lock_state().is_some();
|
let recording = self.lock_state().is_some();
|
||||||
if recording && let Some(metadata) = result.tool_result_metadata() {
|
if recording && let Some(metadata) = result.tool_result_metadata() {
|
||||||
@@ -227,83 +353,25 @@ impl ExecutedToolCalls {
|
|||||||
if self.lock_state().is_none() {
|
if self.lock_state().is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if matches!(source, ToolCallSource::Direct)
|
|
||||||
&& matches!(tool_mode, ToolMode::CodeMode | ToolMode::CodeModeOnly)
|
|
||||||
&& call.tool_name.is_default_namespace()
|
|
||||||
&& matches!(
|
|
||||||
(call.tool_name.name.as_str(), &call.payload),
|
|
||||||
(
|
|
||||||
crate::tools::code_mode::PUBLIC_TOOL_NAME,
|
|
||||||
ToolPayload::Custom { .. }
|
|
||||||
) | (
|
|
||||||
crate::tools::code_mode::WAIT_TOOL_NAME,
|
|
||||||
ToolPayload::Function { .. }
|
|
||||||
)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
let original_bytes = match &call.payload {
|
|
||||||
ToolPayload::Function { arguments } => arguments.len(),
|
|
||||||
ToolPayload::Custom { input } => serialized_json_bytes(input).unwrap_or(usize::MAX),
|
|
||||||
ToolPayload::ToolSearch { arguments } => {
|
|
||||||
serialized_json_bytes(arguments).unwrap_or(usize::MAX)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let name = codex_tools::code_mode_name_for_tool_name(&call.tool_name);
|
|
||||||
let recorded_call = if original_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES {
|
|
||||||
ExecutedToolCall::truncated(name, original_bytes, MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES)
|
|
||||||
} else {
|
|
||||||
let arguments = match &call.payload {
|
|
||||||
ToolPayload::Function { arguments } => serde_json::from_str(arguments)
|
|
||||||
.unwrap_or_else(|_| JsonValue::String(arguments.clone())),
|
|
||||||
ToolPayload::Custom { input } => JsonValue::String(input.clone()),
|
|
||||||
ToolPayload::ToolSearch { arguments } => {
|
|
||||||
serde_json::to_value(arguments).unwrap_or_default()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
ExecutedToolCall::new(name, arguments)
|
|
||||||
};
|
|
||||||
match source {
|
match source {
|
||||||
ToolCallSource::Direct | ToolCallSource::DirectPlaintextMessage => {
|
ToolCallSource::Direct | ToolCallSource::DirectPlaintextMessage => {
|
||||||
let mut state = self.lock_state();
|
let mut state = self.lock_state();
|
||||||
let Some(state) = state.as_mut() else {
|
let Some(state) = state.as_mut() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !state.seen_ids.observe_call_id(&call.call_id) {
|
let fresh = state.seen_ids.observe_call_id(&call.call_id);
|
||||||
|
if !fresh {
|
||||||
state.invalidate_origin(&call.call_id);
|
state.invalidate_origin(&call.call_id);
|
||||||
}
|
}
|
||||||
if state.direct_calls.len() < MAX_PENDING_EXECUTED_TOOL_CALLS {
|
if fresh
|
||||||
state
|
&& is_code_mode_wrapper(call, source, tool_mode)
|
||||||
.direct_calls
|
&& state.pending_wrapper_origins.len() < MAX_PENDING_EXECUTED_TOOL_CALLS
|
||||||
.entry(call.call_id.clone())
|
|
||||||
.or_insert(recorded_call);
|
|
||||||
} else if state.direct_calls.len() == MAX_PENDING_EXECUTED_TOOL_CALLS
|
|
||||||
&& !state.direct_calls.contains_key(&call.call_id)
|
|
||||||
{
|
{
|
||||||
state.direct_calls.insert(
|
state.pending_wrapper_origins.insert(call.call_id.clone());
|
||||||
call.call_id.clone(),
|
|
||||||
ExecutedToolCall::truncated(
|
|
||||||
recorded_call.name,
|
|
||||||
original_bytes,
|
|
||||||
/*max_bytes*/ 0,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ToolCallSource::CodeMode { cell_id, .. } => {
|
ToolCallSource::CodeMode { cell_id, .. } => {
|
||||||
|
let (recorded_call, original_bytes) = recorded_call(call);
|
||||||
self.record_nested_tool_call(
|
self.record_nested_tool_call(
|
||||||
CellId::new(cell_id.clone()),
|
CellId::new(cell_id.clone()),
|
||||||
call.call_id.clone(),
|
call.call_id.clone(),
|
||||||
@@ -376,28 +444,23 @@ impl ExecutedToolCalls {
|
|||||||
call_id: &str,
|
call_id: &str,
|
||||||
metadata: &JsonValue,
|
metadata: &JsonValue,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
let ToolCallSource::CodeMode { cell_id, .. } = source else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
let metadata = codex_protocol::models::ToolResultMetadata::new(metadata);
|
let metadata = codex_protocol::models::ToolResultMetadata::new(metadata);
|
||||||
let has_metadata = metadata.is_some();
|
let has_metadata = metadata.is_some();
|
||||||
let mut state = self.lock_state();
|
let mut state = self.lock_state();
|
||||||
let Some(state) = state.as_mut() else {
|
let Some(state) = state.as_mut() else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let call = match source {
|
let call = state
|
||||||
ToolCallSource::Direct | ToolCallSource::DirectPlaintextMessage => {
|
.cells
|
||||||
state.direct_calls.get_mut(call_id)
|
.get_mut(&CellId::new(cell_id.clone()))
|
||||||
}
|
.and_then(|cell| cell.pending_calls.get_mut(call_id));
|
||||||
ToolCallSource::CodeMode { cell_id, .. } => state
|
|
||||||
.cells
|
|
||||||
.get_mut(&CellId::new(cell_id.clone()))
|
|
||||||
.and_then(|cell| cell.pending_calls.get_mut(call_id)),
|
|
||||||
};
|
|
||||||
if let Some(call) = call {
|
if let Some(call) = call {
|
||||||
call.set_tool_result_metadata(metadata);
|
call.set_tool_result_metadata(metadata);
|
||||||
return has_metadata;
|
return has_metadata;
|
||||||
}
|
}
|
||||||
let ToolCallSource::CodeMode { cell_id, .. } = source else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
let Some((retained, index)) = state.retained_calls.values_mut().find_map(|retained| {
|
let Some((retained, index)) = state.retained_calls.values_mut().find_map(|retained| {
|
||||||
if retained.runtime_cell_id.as_ref()?.as_str() != cell_id.as_str() {
|
if retained.runtime_cell_id.as_ref()?.as_str() != cell_id.as_str() {
|
||||||
return None;
|
return None;
|
||||||
@@ -461,6 +524,47 @@ impl ExecutedToolCalls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_code_mode_wrapper(call: &ToolCall, source: &ToolCallSource, tool_mode: ToolMode) -> bool {
|
||||||
|
matches!(source, ToolCallSource::Direct)
|
||||||
|
&& matches!(tool_mode, ToolMode::CodeMode | ToolMode::CodeModeOnly)
|
||||||
|
&& call.tool_name.is_default_namespace()
|
||||||
|
&& matches!(
|
||||||
|
(call.tool_name.name.as_str(), &call.payload),
|
||||||
|
(
|
||||||
|
crate::tools::code_mode::PUBLIC_TOOL_NAME,
|
||||||
|
ToolPayload::Custom { .. }
|
||||||
|
) | (
|
||||||
|
crate::tools::code_mode::WAIT_TOOL_NAME,
|
||||||
|
ToolPayload::Function { .. }
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recorded_call(call: &ToolCall) -> (ExecutedToolCall, usize) {
|
||||||
|
let original_bytes = match &call.payload {
|
||||||
|
ToolPayload::Function { arguments } => arguments.len(),
|
||||||
|
ToolPayload::Custom { input } => serialized_json_bytes(input).unwrap_or(usize::MAX),
|
||||||
|
ToolPayload::ToolSearch { arguments } => {
|
||||||
|
serialized_json_bytes(arguments).unwrap_or(usize::MAX)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let name = codex_tools::code_mode_name_for_tool_name(&call.tool_name);
|
||||||
|
let recorded_call = if original_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES {
|
||||||
|
ExecutedToolCall::truncated(name, original_bytes, MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES)
|
||||||
|
} else {
|
||||||
|
let arguments = match &call.payload {
|
||||||
|
ToolPayload::Function { arguments } => serde_json::from_str(arguments)
|
||||||
|
.unwrap_or_else(|_| JsonValue::String(arguments.clone())),
|
||||||
|
ToolPayload::Custom { input } => JsonValue::String(input.clone()),
|
||||||
|
ToolPayload::ToolSearch { arguments } => {
|
||||||
|
serde_json::to_value(arguments).unwrap_or_default()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ExecutedToolCall::new(name, arguments)
|
||||||
|
};
|
||||||
|
(recorded_call, original_bytes)
|
||||||
|
}
|
||||||
|
|
||||||
fn input_call_id(item: &ResponseItem) -> Option<&str> {
|
fn input_call_id(item: &ResponseItem) -> Option<&str> {
|
||||||
match item {
|
match item {
|
||||||
ResponseItem::FunctionCall { call_id, .. }
|
ResponseItem::FunctionCall { call_id, .. }
|
||||||
|
|||||||
@@ -4,30 +4,43 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
impl ExecutedToolCalls {
|
impl ExecutedToolCalls {
|
||||||
|
/// Compaction preserves captured records without applying the sampling request budget.
|
||||||
|
pub(crate) fn strip_disabled_direct_metadata(&self, items: &mut [ResponseItem]) {
|
||||||
|
if self.lock_state().is_none() {
|
||||||
|
clear_direct_call_metadata(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Attaches trusted completeness; request budgeting only revokes damaged inventories.
|
/// Attaches trusted completeness; request budgeting only revokes damaged inventories.
|
||||||
pub(crate) fn attach_to_prompt(
|
pub(crate) fn attach_to_prompt(
|
||||||
&self,
|
&self,
|
||||||
items: &mut [ResponseItem],
|
items: &mut [ResponseItem],
|
||||||
retry_cache: &mut ExecutedToolCallCache,
|
retry_cache: &mut ExecutedToolCallCache,
|
||||||
) {
|
) {
|
||||||
if self.attach_pending_to_prompt(items, retry_cache) {
|
let attached = {
|
||||||
|
let mut state = self.lock_state();
|
||||||
|
state
|
||||||
|
.as_mut()
|
||||||
|
.map(|state| Self::attach_pending_to_prompt_with_state(state, items, retry_cache))
|
||||||
|
};
|
||||||
|
let Some(attached) = attached else {
|
||||||
|
// Direct records now live in history; disabling capture also stops replaying them.
|
||||||
|
clear_direct_call_metadata(items);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if attached || items.iter().any(has_direct_call_metadata) {
|
||||||
bound_executed_tool_calls_for_prompt(items);
|
bound_executed_tool_calls_for_prompt(items);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attach_pending_to_prompt(
|
fn attach_pending_to_prompt_with_state(
|
||||||
&self,
|
state: &mut ExecutedToolCallRecorderState,
|
||||||
items: &mut [ResponseItem],
|
items: &mut [ResponseItem],
|
||||||
retry_cache: &mut ExecutedToolCallCache,
|
retry_cache: &mut ExecutedToolCallCache,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let mut state = self.lock_state();
|
|
||||||
let Some(state) = state.as_mut() else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
// Failed wrappers have no callback; only their bounded bitmap observation survives.
|
// Failed wrappers have no callback; only their bounded bitmap observation survives.
|
||||||
state.pending_wrapper_origins.clear();
|
state.pending_wrapper_origins.clear();
|
||||||
if state.direct_calls.is_empty()
|
if state.output_cells.is_empty()
|
||||||
&& state.output_cells.is_empty()
|
|
||||||
&& state.retained_calls.is_empty()
|
&& state.retained_calls.is_empty()
|
||||||
&& retry_cache.is_empty()
|
&& retry_cache.is_empty()
|
||||||
{
|
{
|
||||||
@@ -62,13 +75,13 @@ impl ExecutedToolCalls {
|
|||||||
.collect::<HashSet<_>>();
|
.collect::<HashSet<_>>();
|
||||||
let mut attached = false;
|
let mut attached = false;
|
||||||
for index in (0..items.len()).rev() {
|
for index in (0..items.len()).rev() {
|
||||||
if state.direct_calls.is_empty()
|
if state.output_cells.is_empty() && pending_outputs.is_empty() {
|
||||||
&& state.output_cells.is_empty()
|
|
||||||
&& pending_outputs.is_empty()
|
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let item = &items[index];
|
let item = &items[index];
|
||||||
|
if has_direct_call_metadata(item) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let Some(call_id) = output_call_id(item) else {
|
let Some(call_id) = output_call_id(item) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -85,12 +98,7 @@ impl ExecutedToolCalls {
|
|||||||
cached.clone()
|
cached.clone()
|
||||||
} else {
|
} else {
|
||||||
let mut runtime_cell_id = None;
|
let mut runtime_cell_id = None;
|
||||||
let mut calls = state
|
let mut calls = Vec::new();
|
||||||
.direct_calls
|
|
||||||
.remove(call_id)
|
|
||||||
.into_iter()
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
let direct_call_collision = !calls.is_empty();
|
|
||||||
let mut call_index_by_id = HashMap::new();
|
let mut call_index_by_id = HashMap::new();
|
||||||
if let Some(output_cell_id) = state.output_cells.remove(call_id)
|
if let Some(output_cell_id) = state.output_cells.remove(call_id)
|
||||||
&& let Some(cell) = state.cells.get_mut(&output_cell_id)
|
&& let Some(cell) = state.cells.get_mut(&output_cell_id)
|
||||||
@@ -127,7 +135,6 @@ impl ExecutedToolCalls {
|
|||||||
}
|
}
|
||||||
cell.pending_full_argument_bytes = 0;
|
cell.pending_full_argument_bytes = 0;
|
||||||
complete = cell.completion == CellCompletion::Complete
|
complete = cell.completion == CellCompletion::Complete
|
||||||
&& !direct_call_collision
|
|
||||||
&& (state.can_prove_wait_completion
|
&& (state.can_prove_wait_completion
|
||||||
|| matches!(item, ResponseItem::CustomToolCallOutput { .. }));
|
|| matches!(item, ResponseItem::CustomToolCallOutput { .. }));
|
||||||
if matches!(
|
if matches!(
|
||||||
@@ -179,6 +186,9 @@ impl ExecutedToolCalls {
|
|||||||
let mut invalid_cells = HashSet::new();
|
let mut invalid_cells = HashSet::new();
|
||||||
for index in 0..items.len() {
|
for index in 0..items.len() {
|
||||||
let item = &items[index];
|
let item = &items[index];
|
||||||
|
if has_direct_call_metadata(item) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let Some(call_id) = output_call_id(item) else {
|
let Some(call_id) = output_call_id(item) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -232,6 +242,9 @@ impl ExecutedToolCalls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for item in items.iter_mut() {
|
for item in items.iter_mut() {
|
||||||
|
if has_direct_call_metadata(item) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let Some(call_id) = output_call_id(item) else {
|
let Some(call_id) = output_call_id(item) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -254,6 +267,9 @@ impl ExecutedToolCalls {
|
|||||||
let retained_before_bounding = std::mem::take(&mut state.retained_calls);
|
let retained_before_bounding = std::mem::take(&mut state.retained_calls);
|
||||||
let mut bounded_outputs = HashSet::new();
|
let mut bounded_outputs = HashSet::new();
|
||||||
for item in items.iter_mut() {
|
for item in items.iter_mut() {
|
||||||
|
if has_direct_call_metadata(item) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let Some(call_id) = output_call_id(item) else {
|
let Some(call_id) = output_call_id(item) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -305,6 +321,22 @@ impl ExecutedToolCalls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Direct records already belong to their output; never rebuild them from the Code Mode cache.
|
||||||
|
fn clear_direct_call_metadata(items: &mut [ResponseItem]) {
|
||||||
|
for item in items
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|item| has_direct_call_metadata(item))
|
||||||
|
{
|
||||||
|
item.clear_executed_tool_calls();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_direct_call_metadata(item: &ResponseItem) -> bool {
|
||||||
|
item.executed_tool_call_metadata().is_some_and(|metadata| {
|
||||||
|
metadata.cell_id.is_none() && metadata.executed_tool_calls.is_some()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn code_mode_input_matches_output(
|
fn code_mode_input_matches_output(
|
||||||
input: &ResponseItem,
|
input: &ResponseItem,
|
||||||
output: &ResponseItem,
|
output: &ResponseItem,
|
||||||
|
|||||||
@@ -10,6 +10,20 @@ use serde_json::json;
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
impl ExecutedToolCalls {
|
||||||
|
fn attach_pending_to_prompt(
|
||||||
|
&self,
|
||||||
|
items: &mut [ResponseItem],
|
||||||
|
retry_cache: &mut ExecutedToolCallCache,
|
||||||
|
) -> bool {
|
||||||
|
let mut state = self.lock_state();
|
||||||
|
let Some(state) = state.as_mut() else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
Self::attach_pending_to_prompt_with_state(state, items, retry_cache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn new_recorder(history: InitialHistory) -> ExecutedToolCalls {
|
fn new_recorder(history: InitialHistory) -> ExecutedToolCalls {
|
||||||
let mut features = Features::default();
|
let mut features = Features::default();
|
||||||
features.enable(Feature::ExecutedToolCallMetadata);
|
features.enable(Feature::ExecutedToolCallMetadata);
|
||||||
@@ -66,6 +80,82 @@ fn tool_calls_complete(item: &ResponseItem) -> Option<bool> {
|
|||||||
.and_then(|metadata| metadata.tool_calls_complete)
|
.and_then(|metadata| metadata.tool_calls_complete)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn direct_retained_metadata_budget_sheds_results_then_calls_across_refresh() {
|
||||||
|
let recorder = new_recorder(InitialHistory::New);
|
||||||
|
let mut call = ExecutedToolCall::new("test_tool".to_string(), json!({"argument": "kept"}));
|
||||||
|
call.set_tool_result_metadata(ToolResultMetadata::new(&json!({
|
||||||
|
"provider": "x".repeat(1024),
|
||||||
|
})));
|
||||||
|
let mut first = output("first");
|
||||||
|
recorder.attach_direct_call_to_output(
|
||||||
|
&mut first,
|
||||||
|
Some((call.clone(), recorder.reserve_direct_call().unwrap())),
|
||||||
|
);
|
||||||
|
let full_bytes = executed_tool_call_metadata_bytes(&first);
|
||||||
|
assert!(full_bytes > 0);
|
||||||
|
assert_eq!(
|
||||||
|
recorder
|
||||||
|
.retained_direct_metadata_bytes
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
full_bytes,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut without_result = first.clone();
|
||||||
|
without_result.clear_tool_result_metadata();
|
||||||
|
let call_bytes = executed_tool_call_metadata_bytes(&without_result);
|
||||||
|
assert!(full_bytes > call_bytes);
|
||||||
|
recorder.retained_direct_metadata_bytes.store(
|
||||||
|
MAX_RETAINED_DIRECT_METADATA_BYTES - call_bytes,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
);
|
||||||
|
let mut second = output("second");
|
||||||
|
let result_before =
|
||||||
|
serde_json::to_value(&second).expect("serializable output")["output"].clone();
|
||||||
|
recorder.clone().attach_direct_call_to_output(
|
||||||
|
&mut second,
|
||||||
|
Some((call.clone(), recorder.reserve_direct_call().unwrap())),
|
||||||
|
);
|
||||||
|
let second_metadata = second
|
||||||
|
.executed_tool_call_metadata()
|
||||||
|
.expect("call remains when only its result metadata exceeds the budget");
|
||||||
|
assert_eq!(second_metadata.tool_calls_complete, Some(true));
|
||||||
|
assert_eq!(
|
||||||
|
second_metadata.executed_tool_calls.as_ref().map(Vec::len),
|
||||||
|
Some(1)
|
||||||
|
);
|
||||||
|
let serialized = serde_json::to_value(&second).expect("serializable output");
|
||||||
|
assert!(
|
||||||
|
serialized["internal_chat_message_metadata_passthrough"]["executed_tool_calls"][0]
|
||||||
|
.get("tool_result_metadata")
|
||||||
|
.is_none()
|
||||||
|
);
|
||||||
|
assert_eq!(serialized["output"], result_before);
|
||||||
|
assert_eq!(
|
||||||
|
recorder
|
||||||
|
.retained_direct_metadata_bytes
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
MAX_RETAINED_DIRECT_METADATA_BYTES,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut features = Features::default();
|
||||||
|
recorder.refresh(&features);
|
||||||
|
features.enable(Feature::ExecutedToolCallMetadata);
|
||||||
|
recorder.refresh(&features);
|
||||||
|
let mut third = output("third");
|
||||||
|
recorder.attach_direct_call_to_output(
|
||||||
|
&mut third,
|
||||||
|
Some((call, recorder.reserve_direct_call().unwrap())),
|
||||||
|
);
|
||||||
|
assert!(third.executed_tool_call_metadata().is_none());
|
||||||
|
assert_eq!(
|
||||||
|
recorder
|
||||||
|
.retained_direct_metadata_bytes
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
MAX_RETAINED_DIRECT_METADATA_BYTES,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn recorder_refreshes_without_changing_execution_features_or_claiming_missing_history() {
|
async fn recorder_refreshes_without_changing_execution_features_or_claiming_missing_history() {
|
||||||
struct MetadataLookup<'a>(std::cell::Cell<usize>, JsonValue, &'a ExecutedToolCalls);
|
struct MetadataLookup<'a>(std::cell::Cell<usize>, JsonValue, &'a ExecutedToolCalls);
|
||||||
@@ -100,6 +190,7 @@ async fn recorder_refreshes_without_changing_execution_features_or_claiming_miss
|
|||||||
assert!(calls.lock_state().is_none());
|
assert!(calls.lock_state().is_none());
|
||||||
let mut next_config = (*session.get_config().await).clone();
|
let mut next_config = (*session.get_config().await).clone();
|
||||||
let mut retry_cache = HashMap::new();
|
let mut retry_cache = HashMap::new();
|
||||||
|
let mut prior_generation = None;
|
||||||
for (index, enabled) in [false, true, false, true].into_iter().enumerate() {
|
for (index, enabled) in [false, true, false, true].into_iter().enumerate() {
|
||||||
next_config
|
next_config
|
||||||
.features
|
.features
|
||||||
@@ -124,51 +215,46 @@ async fn recorder_refreshes_without_changing_execution_features_or_claiming_miss
|
|||||||
},
|
},
|
||||||
encrypted_function_args: None,
|
encrypted_function_args: None,
|
||||||
};
|
};
|
||||||
calls.record_tool_call(&call, &ToolCallSource::Direct, &step);
|
let prepared = calls.prepare_direct_call(&call, &ToolCallSource::Direct, &step);
|
||||||
let result = MetadataLookup(std::cell::Cell::new(0), json!({}), &calls);
|
let expected = ExecutedToolCall::new("test_tool".to_string(), json!({"value": 7}));
|
||||||
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.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!(
|
assert_eq!(
|
||||||
prompt[1]
|
prepared.as_ref().map(|(call, _)| call),
|
||||||
|
enabled.then_some(&expected)
|
||||||
|
);
|
||||||
|
if enabled {
|
||||||
|
if let Some(stale) = prior_generation.take() {
|
||||||
|
let mut stale_output = output("stale");
|
||||||
|
calls.attach_direct_call_to_output(&mut stale_output, Some(stale));
|
||||||
|
assert!(stale_output.executed_tool_call_metadata().is_none());
|
||||||
|
}
|
||||||
|
prior_generation = calls.prepare_direct_call(&call, &ToolCallSource::Direct, &step);
|
||||||
|
}
|
||||||
|
let mut direct_output = output(&call.call_id);
|
||||||
|
calls.attach_direct_call_to_output(&mut direct_output, prepared);
|
||||||
|
assert_eq!(
|
||||||
|
direct_output
|
||||||
.executed_tool_call_metadata()
|
.executed_tool_call_metadata()
|
||||||
.and_then(|metadata| metadata.executed_tool_calls.as_ref()),
|
.and_then(|metadata| metadata.executed_tool_calls.as_ref()),
|
||||||
enabled.then_some(&expected_calls),
|
enabled.then_some(&vec![expected]),
|
||||||
);
|
);
|
||||||
|
assert_eq!(tool_calls_complete(&direct_output), enabled.then_some(true));
|
||||||
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 cell = CellId::new(format!("cell-{index}"));
|
||||||
let origin = format!("exec-{index}");
|
let origin = format!("exec-{index}");
|
||||||
calls.start_cell(&cell, &origin);
|
calls.start_cell(&cell, &origin);
|
||||||
let nested = record_nested_call(&calls, &cell, "nested");
|
let mut nested = record_nested_call(&calls, &cell, "nested");
|
||||||
|
let source = ToolCallSource::CodeMode {
|
||||||
|
cell_id: cell.as_str().to_string(),
|
||||||
|
runtime_tool_call_id: "runtime-nested".to_string(),
|
||||||
|
};
|
||||||
|
let result = MetadataLookup(std::cell::Cell::new(0), json!({}), &calls);
|
||||||
|
calls.record_accepted_result(&source, "nested", &result);
|
||||||
|
assert_eq!(result.0.get(), usize::from(enabled));
|
||||||
|
nested.set_tool_result_metadata(ToolResultMetadata::new(&json!({})));
|
||||||
|
if enabled {
|
||||||
|
// Reapplying an enabled config must preserve pending calls and their metadata.
|
||||||
|
session.refresh_runtime_config(next_config.clone()).await;
|
||||||
|
}
|
||||||
calls.finish_cell_recording(&cell);
|
calls.finish_cell_recording(&cell);
|
||||||
let mut prompt = vec![exec_input(&origin), exec_output(&origin)];
|
let mut prompt = vec![exec_input(&origin), exec_output(&origin)];
|
||||||
calls.attach_to_prompt(&mut prompt, &mut retry_cache);
|
calls.attach_to_prompt(&mut prompt, &mut retry_cache);
|
||||||
@@ -187,18 +273,6 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
|
|||||||
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
|
let recorder = new_recorder(InitialHistory::Forked(Vec::new()));
|
||||||
|
|
||||||
for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS + 2 {
|
for index in 0..MAX_PENDING_EXECUTED_TOOL_CALLS + 2 {
|
||||||
recorder.record_call(
|
|
||||||
&ToolCall {
|
|
||||||
tool_name: codex_tools::ToolName::plain("direct_tool"),
|
|
||||||
call_id: format!("direct-{index}"),
|
|
||||||
payload: ToolPayload::Function {
|
|
||||||
arguments: "{}".to_string(),
|
|
||||||
},
|
|
||||||
encrypted_function_args: None,
|
|
||||||
},
|
|
||||||
&ToolCallSource::Direct,
|
|
||||||
ToolMode::Direct,
|
|
||||||
);
|
|
||||||
recorder.record_call(
|
recorder.record_call(
|
||||||
&ToolCall {
|
&ToolCall {
|
||||||
tool_name: codex_tools::ToolName::plain(crate::tools::code_mode::PUBLIC_TOOL_NAME),
|
tool_name: codex_tools::ToolName::plain(crate::tools::code_mode::PUBLIC_TOOL_NAME),
|
||||||
@@ -212,8 +286,6 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
|
|||||||
ToolMode::CodeMode,
|
ToolMode::CodeMode,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let metadata = json!({ "arbitrary-key": ["R1", null] });
|
|
||||||
assert!(recorder.record_tool_result_metadata(&ToolCallSource::Direct, "direct-0", &metadata));
|
|
||||||
|
|
||||||
let cell_id = CellId::new("bounded-cell".to_string());
|
let cell_id = CellId::new("bounded-cell".to_string());
|
||||||
recorder.start_cell(&cell_id, "bounded-output");
|
recorder.start_cell(&cell_id, "bounded-output");
|
||||||
@@ -236,32 +308,6 @@ fn executed_tool_call_recorder_bounds_pending_calls_and_preserves_overflow() {
|
|||||||
{
|
{
|
||||||
let state = recorder.lock_state();
|
let state = recorder.lock_state();
|
||||||
let state = state.as_ref().unwrap();
|
let state = state.as_ref().unwrap();
|
||||||
assert_eq!(
|
|
||||||
state.direct_calls.len(),
|
|
||||||
MAX_PENDING_EXECUTED_TOOL_CALLS + 1
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
serde_json::to_value(state.direct_calls.get("direct-0").unwrap()).unwrap()["tool_result_metadata"],
|
|
||||||
metadata,
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
serde_json::to_value(
|
|
||||||
state
|
|
||||||
.direct_calls
|
|
||||||
.get(&format!("direct-{MAX_PENDING_EXECUTED_TOOL_CALLS}"))
|
|
||||||
.expect("first excess direct call must be marked"),
|
|
||||||
)
|
|
||||||
.expect("direct overflow marker must serialize"),
|
|
||||||
json!({
|
|
||||||
"name": "direct_tool",
|
|
||||||
"arguments": {
|
|
||||||
"_codex_executed_tool_call_truncated": {
|
|
||||||
"original_bytes": 2,
|
|
||||||
"max_bytes": 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
state.pending_nested_calls,
|
state.pending_nested_calls,
|
||||||
MAX_PENDING_EXECUTED_TOOL_CALLS + 1
|
MAX_PENDING_EXECUTED_TOOL_CALLS + 1
|
||||||
@@ -341,26 +387,19 @@ fn executed_tool_call_recorder_bounds_retained_history_and_keeps_latest_calls()
|
|||||||
|
|
||||||
for index in 0..512 {
|
for index in 0..512 {
|
||||||
let call_id = format!("retained-{index}");
|
let call_id = format!("retained-{index}");
|
||||||
recorder.record_call(
|
let cell_id = CellId::new(format!("retained-cell-{index}"));
|
||||||
&ToolCall {
|
recorder.start_cell(&cell_id, &call_id);
|
||||||
tool_name: codex_tools::ToolName::plain(format!("retained_tool_{index}")),
|
recorder.record_nested_tool_call(
|
||||||
call_id: call_id.clone(),
|
cell_id.clone(),
|
||||||
payload: ToolPayload::Function {
|
format!("nested-{index}"),
|
||||||
arguments: arguments.clone(),
|
ExecutedToolCall::new(
|
||||||
},
|
format!("retained_tool_{index}"),
|
||||||
encrypted_function_args: None,
|
json!({ "payload": "x".repeat(1024) }),
|
||||||
},
|
),
|
||||||
&ToolCallSource::Direct,
|
arguments.len(),
|
||||||
ToolMode::Direct,
|
|
||||||
);
|
);
|
||||||
history.push(ResponseItem::FunctionCallOutput {
|
recorder.finish_cell_recording(&cell_id);
|
||||||
id: None,
|
history.extend([exec_input(&call_id), exec_output(&call_id)]);
|
||||||
call_id: Some(call_id),
|
|
||||||
name: None,
|
|
||||||
namespace: None,
|
|
||||||
output: FunctionCallOutputPayload::from_text(String::new()),
|
|
||||||
internal_chat_message_metadata_passthrough: None,
|
|
||||||
});
|
|
||||||
prompt = history.clone();
|
prompt = history.clone();
|
||||||
assert!(recorder.attach_pending_to_prompt(&mut prompt, &mut HashMap::new()));
|
assert!(recorder.attach_pending_to_prompt(&mut prompt, &mut HashMap::new()));
|
||||||
codex_protocol::models::bound_executed_tool_calls_for_prompt(&mut prompt);
|
codex_protocol::models::bound_executed_tool_calls_for_prompt(&mut prompt);
|
||||||
@@ -756,6 +795,26 @@ fn completeness_rejects_reused_origin() {
|
|||||||
assert_eq!(tool_calls_complete(&prompt[1]), None);
|
assert_eq!(tool_calls_complete(&prompt[1]), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn malformed_search_id_cannot_prove_a_later_exec_complete() {
|
||||||
|
let recorder = new_recorder(InitialHistory::New);
|
||||||
|
let malformed_search = serde_json::from_value(json!({
|
||||||
|
"type": "tool_search_call", "call_id": "reused", "execution": "client",
|
||||||
|
"arguments": {"query": 42},
|
||||||
|
}))
|
||||||
|
.expect("tool search response item");
|
||||||
|
recorder.observe_non_dispatched_call(&malformed_search);
|
||||||
|
|
||||||
|
let cell = CellId::new("later-runtime".to_string());
|
||||||
|
recorder.start_cell(&cell, "reused");
|
||||||
|
record_nested_call(&recorder, &cell, "nested");
|
||||||
|
recorder.finish_cell_recording(&cell);
|
||||||
|
let mut prompt = [exec_input("reused"), exec_output("reused")];
|
||||||
|
recorder.attach_to_prompt(&mut prompt, &mut HashMap::new());
|
||||||
|
assert_eq!(tool_calls_complete(&prompt[1]), None);
|
||||||
|
assert!(prompt[1].executed_tool_call_metadata().is_some());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn completeness_rejects_direct_id_collision_and_late_calls() {
|
fn completeness_rejects_direct_id_collision_and_late_calls() {
|
||||||
let recorder = new_recorder(InitialHistory::New);
|
let recorder = new_recorder(InitialHistory::New);
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
//! Tests Direct metadata admission and permit lifetimes.
|
||||||
|
//! Metadata limits must leave ordinary tool outputs unchanged.
|
||||||
|
|
||||||
|
use codex_protocol::models::FunctionCallOutputPayload;
|
||||||
|
use codex_protocol::models::ResponseInputItem;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn output(call_id: &str) -> ResponseItem {
|
||||||
|
ResponseItem::from(ResponseInputItem::FunctionCallOutput {
|
||||||
|
call_id: call_id.to_string(),
|
||||||
|
output: FunctionCallOutputPayload::from_text("tool result".to_string()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn direct_pending_limit_releases_on_completion_or_dropped_future() {
|
||||||
|
let (_, turn) = crate::session::tests::make_session_and_context().await;
|
||||||
|
let step = StepContext::for_test(Arc::new(turn));
|
||||||
|
let mut features = Features::default();
|
||||||
|
features.enable(Feature::ExecutedToolCallMetadata);
|
||||||
|
let recorder = ExecutedToolCalls::new(&features, &InitialHistory::New);
|
||||||
|
let call = ToolCall {
|
||||||
|
tool_name: codex_tools::ToolName::plain("test_tool"),
|
||||||
|
call_id: "direct".to_string(),
|
||||||
|
payload: ToolPayload::Function {
|
||||||
|
arguments: json!({ "argument": "kept" }).to_string(),
|
||||||
|
},
|
||||||
|
encrypted_function_args: None,
|
||||||
|
};
|
||||||
|
let prepare = |recorder: &ExecutedToolCalls| {
|
||||||
|
recorder.prepare_direct_call(&call, &ToolCallSource::Direct, &step)
|
||||||
|
};
|
||||||
|
let mut pending = (0..MAX_PENDING_EXECUTED_TOOL_CALLS)
|
||||||
|
.map(|_| prepare(&recorder).expect("metadata slot"))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let cloned = recorder.clone();
|
||||||
|
let mut overflow = output("overflow");
|
||||||
|
let before = serde_json::to_value(&overflow).expect("serializable output");
|
||||||
|
cloned.attach_direct_call_to_output(&mut overflow, prepare(&cloned));
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&overflow).expect("serializable output"),
|
||||||
|
before,
|
||||||
|
);
|
||||||
|
|
||||||
|
let canceled = pending.pop().expect("pending call");
|
||||||
|
let unpolled = async move {
|
||||||
|
std::future::pending::<()>().await;
|
||||||
|
drop(canceled);
|
||||||
|
};
|
||||||
|
assert!(prepare(&cloned).is_none());
|
||||||
|
drop(unpolled);
|
||||||
|
let replacement = prepare(&cloned).expect("dropped future released its slot");
|
||||||
|
assert!(prepare(&recorder).is_none());
|
||||||
|
|
||||||
|
let mut completed = output("completed");
|
||||||
|
recorder.attach_direct_call_to_output(&mut completed, pending.pop());
|
||||||
|
assert_eq!(
|
||||||
|
completed
|
||||||
|
.executed_tool_call_metadata()
|
||||||
|
.and_then(|metadata| metadata.tool_calls_complete),
|
||||||
|
Some(true),
|
||||||
|
);
|
||||||
|
let after_completion = prepare(&recorder).expect("completion released its slot");
|
||||||
|
|
||||||
|
recorder.refresh(&Features::default());
|
||||||
|
recorder.refresh(&features);
|
||||||
|
assert!(prepare(&recorder).is_none());
|
||||||
|
let mut stale = output("stale");
|
||||||
|
recorder.attach_direct_call_to_output(&mut stale, Some(replacement));
|
||||||
|
assert!(stale.executed_tool_call_metadata().is_none());
|
||||||
|
let current = prepare(&recorder).expect("stale call released its slot");
|
||||||
|
let mut current_output = output("current");
|
||||||
|
recorder.attach_direct_call_to_output(&mut current_output, Some(current));
|
||||||
|
assert_eq!(
|
||||||
|
current_output
|
||||||
|
.executed_tool_call_metadata()
|
||||||
|
.and_then(|metadata| metadata.tool_calls_complete),
|
||||||
|
Some(true),
|
||||||
|
);
|
||||||
|
drop(pending);
|
||||||
|
drop(after_completion);
|
||||||
|
assert_eq!(recorder.pending_direct_calls.load(Ordering::Relaxed), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn direct_budget_counts_the_encoded_argument_before_it_enters_history() {
|
||||||
|
let (_, turn) = crate::session::tests::make_session_and_context().await;
|
||||||
|
let step = StepContext::for_test(Arc::new(turn));
|
||||||
|
let mut features = Features::default();
|
||||||
|
features.enable(Feature::ExecutedToolCallMetadata);
|
||||||
|
let recorder = ExecutedToolCalls::new(&features, &InitialHistory::New);
|
||||||
|
let invalid_json = "\"".repeat(5_000);
|
||||||
|
let wire_bytes = serialized_json_bytes(&JsonValue::String(invalid_json.clone())).unwrap();
|
||||||
|
assert!(invalid_json.len() < MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
|
||||||
|
assert!(wire_bytes > MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
|
||||||
|
let call = ToolCall {
|
||||||
|
tool_name: codex_tools::ToolName::plain("test_tool"),
|
||||||
|
call_id: "direct".to_string(),
|
||||||
|
payload: ToolPayload::Function {
|
||||||
|
arguments: invalid_json,
|
||||||
|
},
|
||||||
|
encrypted_function_args: None,
|
||||||
|
};
|
||||||
|
let mut item = output("direct");
|
||||||
|
let ordinary_output = serde_json::to_value(&item).unwrap()["output"].clone();
|
||||||
|
recorder.attach_direct_call_to_output(
|
||||||
|
&mut item,
|
||||||
|
recorder.prepare_direct_call(&call, &ToolCallSource::Direct, &step),
|
||||||
|
);
|
||||||
|
let wire = serde_json::to_value(&item).unwrap();
|
||||||
|
assert_eq!(wire["output"], ordinary_output);
|
||||||
|
let metadata = &wire["internal_chat_message_metadata_passthrough"];
|
||||||
|
let arguments = &metadata["executed_tool_calls"][0]["arguments"];
|
||||||
|
assert!(serialized_json_bytes(arguments).unwrap() < MAX_EXECUTED_TOOL_CALL_ARGUMENT_BYTES);
|
||||||
|
assert_eq!(
|
||||||
|
arguments["_codex_executed_tool_call_truncated"]["original_bytes"],
|
||||||
|
wire_bytes,
|
||||||
|
);
|
||||||
|
assert!(metadata.get("tool_calls_complete").is_none());
|
||||||
|
}
|
||||||
@@ -748,7 +748,14 @@ mod tests {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reuse the direct invocation to catch recording placed after the Code Mode-only return.
|
// Nested MCP result metadata still reaches the owning Code Mode cell.
|
||||||
|
let cell_id = codex_code_mode::CellId::new("mcp-cell".to_string());
|
||||||
|
recorder.start_cell(&cell_id, "exec-mcp-post");
|
||||||
|
let mut invocation = invocation;
|
||||||
|
invocation.source = ToolCallSource::CodeMode {
|
||||||
|
cell_id: cell_id.as_str().to_string(),
|
||||||
|
runtime_tool_call_id: "mcp-runtime-call".to_string(),
|
||||||
|
};
|
||||||
let mut output = output;
|
let mut output = output;
|
||||||
output.result.meta = Some(json!({ "provider/custom": { "items": [1, null] } }));
|
output.result.meta = Some(json!({ "provider/custom": { "items": [1, null] } }));
|
||||||
output.result_metadata_capture_allowed = true;
|
output.result_metadata_capture_allowed = true;
|
||||||
@@ -763,9 +770,10 @@ mod tests {
|
|||||||
&invocation.step_context,
|
&invocation.step_context,
|
||||||
);
|
);
|
||||||
handler.on_tool_result_accepted(&invocation, &output);
|
handler.on_tool_result_accepted(&invocation, &output);
|
||||||
let mut items = [codex_protocol::models::ResponseItem::from(
|
let mut items = [serde_json::from_value(json!({
|
||||||
output.to_response_item(&invocation.call_id, &invocation.payload),
|
"type": "custom_tool_call_output", "call_id": "exec-mcp-post", "output": "notes",
|
||||||
)];
|
}))
|
||||||
|
.expect("Code Mode output")];
|
||||||
recorder.attach_to_prompt(&mut items, &mut Default::default());
|
recorder.attach_to_prompt(&mut items, &mut Default::default());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(items[0].executed_tool_call_metadata()).unwrap()["executed_tool_calls"],
|
serde_json::to_value(items[0].executed_tool_call_metadata()).unwrap()["executed_tool_calls"],
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ use crate::tools::router::ToolCallSource;
|
|||||||
use codex_history::ResponseItemEnvelope;
|
use codex_history::ResponseItemEnvelope;
|
||||||
use codex_protocol::error::CodexErr;
|
use codex_protocol::error::CodexErr;
|
||||||
use codex_protocol::models::ResponseInputItem;
|
use codex_protocol::models::ResponseInputItem;
|
||||||
|
use codex_protocol::models::ToolResultMetadata;
|
||||||
|
|
||||||
struct ToolCallTimingGuard {
|
struct ToolCallTimingGuard {
|
||||||
started_at: Instant,
|
started_at: Instant,
|
||||||
@@ -79,17 +80,31 @@ impl ToolCallRuntime {
|
|||||||
) -> impl std::future::Future<Output = Result<ResponseItemEnvelope, CodexErr>> {
|
) -> impl std::future::Future<Output = Result<ResponseItemEnvelope, CodexErr>> {
|
||||||
let error_call = call.clone();
|
let error_call = call.clone();
|
||||||
let source = call.direct_source();
|
let source = call.direct_source();
|
||||||
|
let recorder = self.session.services.executed_tool_calls.clone();
|
||||||
|
let recorded_call = recorder.prepare_direct_call(&call, &source, &self.step_context);
|
||||||
let step_context = Arc::clone(&self.step_context);
|
let step_context = Arc::clone(&self.step_context);
|
||||||
let future =
|
let future =
|
||||||
self.handle_tool_call_with_source(step_context, call, source, cancellation_token);
|
self.handle_tool_call_with_source(step_context, call, source, cancellation_token);
|
||||||
async move {
|
async move {
|
||||||
match future.await {
|
let result = future.await;
|
||||||
Ok(response) => Ok(response.into_response()),
|
let mut recorded_call =
|
||||||
Err(FunctionCallError::Fatal(message)) => Err(CodexErr::Fatal(message)),
|
recorded_call.filter(|(_, recording)| recording.strong_count() > 0);
|
||||||
Err(other) => Ok(ResponseItemEnvelope::new(
|
let mut response = match result {
|
||||||
Self::failure_response(error_call, other).into(),
|
Ok(result) => {
|
||||||
)),
|
if let Some((call, _)) = recorded_call.as_mut()
|
||||||
}
|
&& let Some(metadata) = result.result.tool_result_metadata()
|
||||||
|
{
|
||||||
|
call.set_tool_result_metadata(ToolResultMetadata::new(metadata));
|
||||||
|
}
|
||||||
|
result.into_response()
|
||||||
|
}
|
||||||
|
Err(FunctionCallError::Fatal(message)) => return Err(CodexErr::Fatal(message)),
|
||||||
|
Err(other) => {
|
||||||
|
ResponseItemEnvelope::new(Self::failure_response(error_call, other).into())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
recorder.attach_direct_call_to_output(&mut response.item, recorded_call);
|
||||||
|
Ok(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1620,21 +1620,26 @@ fn assert_result_metadata_call(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
#[test_case(true, true, true, false, "employee@openai.com"; "copies_full_metadata_without_rules")]
|
#[test_case(true, true, true, false, "employee@openai.com", ToolMode::CodeModeOnly; "copies_full_metadata_without_rules")]
|
||||||
#[test_case(true, true, true, true, "employee@openai.com"; "accepted_error_keeps_metadata")]
|
#[test_case(true, true, true, true, "employee@openai.com", ToolMode::CodeModeOnly; "accepted_error_keeps_metadata")]
|
||||||
#[test_case(true, true, false, false, "employee@openai.com"; "missing_metadata_stays_absent")]
|
#[test_case(true, true, false, false, "employee@openai.com", ToolMode::CodeModeOnly; "missing_metadata_stays_absent")]
|
||||||
#[test_case(false, true, true, false, "employee@openai.com"; "feature_off_does_not_record")]
|
#[test_case(false, true, true, false, "employee@openai.com", ToolMode::CodeModeOnly; "feature_off_does_not_record")]
|
||||||
#[test_case(true, false, true, false, "employee@openai.com"; "extension_owned_apps_do_not_record")]
|
#[test_case(true, false, true, false, "employee@openai.com", ToolMode::CodeModeOnly; "extension_owned_apps_do_not_record")]
|
||||||
#[test_case(true, true, true, false, "employee@example.com"; "external_user_keeps_calls_without_metadata")]
|
#[test_case(true, true, true, false, "employee@example.com", ToolMode::CodeModeOnly; "external_user_keeps_calls_without_metadata")]
|
||||||
#[test_case(true, true, true, false, "employee@openai.com.attacker.invalid"; "lookalike_domain_does_not_record")]
|
#[test_case(true, true, true, false, "employee@openai.com.attacker.invalid", ToolMode::CodeModeOnly; "lookalike_domain_does_not_record")]
|
||||||
async fn code_mode_result_metadata_follows_call_binding(
|
#[test_case(true, true, true, false, "employee@openai.com", ToolMode::Direct; "direct_keeps_metadata")]
|
||||||
|
#[test_case(true, true, true, true, "employee@openai.com", ToolMode::Direct; "direct_accepted_error_keeps_metadata")]
|
||||||
|
#[test_case(false, true, true, false, "employee@openai.com", ToolMode::Direct; "direct_feature_off_does_not_record")]
|
||||||
|
async fn result_metadata_follows_call_binding(
|
||||||
metadata_enabled: bool,
|
metadata_enabled: bool,
|
||||||
host_owned: bool,
|
host_owned: bool,
|
||||||
has_metadata: bool,
|
has_metadata: bool,
|
||||||
is_error: bool,
|
is_error: bool,
|
||||||
account_email: &str,
|
account_email: &str,
|
||||||
|
tool_mode: ToolMode,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
skip_if_no_network!(Ok(()));
|
skip_if_no_network!(Ok(()));
|
||||||
|
let direct = matches!(tool_mode, ToolMode::Direct);
|
||||||
let server = responses::start_mock_server().await;
|
let server = responses::start_mock_server().await;
|
||||||
let result_metadata = has_metadata.then(|| {
|
let result_metadata = has_metadata.then(|| {
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
@@ -1654,6 +1659,10 @@ async fn code_mode_result_metadata_follows_call_binding(
|
|||||||
let mut builder =
|
let mut builder =
|
||||||
result_metadata_apps_builder(apps_server.chatgpt_base_url.clone(), account_email)
|
result_metadata_apps_builder(apps_server.chatgpt_base_url.clone(), account_email)
|
||||||
.with_config(move |config| {
|
.with_config(move |config| {
|
||||||
|
if direct {
|
||||||
|
config.features.disable(Feature::CodeMode).unwrap();
|
||||||
|
config.features.disable(Feature::CodeModeOnly).unwrap();
|
||||||
|
}
|
||||||
if !metadata_enabled {
|
if !metadata_enabled {
|
||||||
config
|
config
|
||||||
.features
|
.features
|
||||||
@@ -1677,43 +1686,87 @@ async fn code_mode_result_metadata_follows_call_binding(
|
|||||||
builder = builder.with_extensions(Arc::new(extensions.build()));
|
builder = builder.with_extensions(Arc::new(extensions.build()));
|
||||||
}
|
}
|
||||||
let arguments = serde_json::json!({ "search": "launch plan" });
|
let arguments = serde_json::json!({ "search": "launch plan" });
|
||||||
let code = format!(
|
let (test, follow_up) = if direct {
|
||||||
"const tool = ALL_TOOLS.find(({{ name }}) => name.endsWith(\"{RESULT_METADATA_TOOL}\")); \
|
let test = builder.build(&server).await?;
|
||||||
const result = await tools[tool.name]({arguments}); \
|
responses::mount_sse_once(
|
||||||
text(JSON.stringify({{ isError: Boolean(result.isError), hasMeta: Object.hasOwn(result, \"_meta\") }}));"
|
&server,
|
||||||
);
|
sse(vec![
|
||||||
let (test, follow_up) =
|
responses::ev_function_call_with_namespace(
|
||||||
run_code_mode_turn_with_builder(&server, "Search a connected app", &code, builder).await?;
|
"call-1",
|
||||||
|
"mcp__codex_apps__messagesearch",
|
||||||
|
RESULT_METADATA_TOOL,
|
||||||
|
&arguments.to_string(),
|
||||||
|
),
|
||||||
|
ev_completed("resp-1"),
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let follow_up = responses::mount_sse_once(&server, sse(vec![ev_completed("resp-2")])).await;
|
||||||
|
test.submit_turn("Search a connected app").await?;
|
||||||
|
(test, follow_up)
|
||||||
|
} else {
|
||||||
|
let code = format!(
|
||||||
|
"const tool = ALL_TOOLS.find(({{ name }}) => name.endsWith(\"{RESULT_METADATA_TOOL}\")); \
|
||||||
|
const result = await tools[tool.name]({arguments}); \
|
||||||
|
text(JSON.stringify({{ isError: Boolean(result.isError), hasMeta: Object.hasOwn(result, \"_meta\") }}));"
|
||||||
|
);
|
||||||
|
run_code_mode_turn_with_builder(&server, "Search a connected app", &code, builder).await?
|
||||||
|
};
|
||||||
let request = follow_up.single_request();
|
let request = follow_up.single_request();
|
||||||
assert_eq!(recorded_apps_tool_calls(&server).await.len(), 1);
|
assert_eq!(recorded_apps_tool_calls(&server).await.len(), 1);
|
||||||
assert!(
|
let output = if direct {
|
||||||
!request
|
let output = request.function_call_output("call-1");
|
||||||
.body_json()
|
assert!(
|
||||||
.to_string()
|
output["output"]
|
||||||
.contains(RESULT_METADATA_PRIVATE_RESULT)
|
.to_string()
|
||||||
);
|
.contains(RESULT_METADATA_PRIVATE_RESULT)
|
||||||
let (body, success) = custom_tool_output_body_and_success(&request, "call-1");
|
);
|
||||||
assert_ne!(success, Some(false), "Code Mode failed: {body}");
|
assert!(!output["output"].to_string().contains("provider_state"));
|
||||||
assert_eq!(
|
output
|
||||||
serde_json::from_str::<Value>(&body)?,
|
} else {
|
||||||
serde_json::json!({ "isError": is_error, "hasMeta": false }),
|
assert!(
|
||||||
);
|
!request
|
||||||
|
.body_json()
|
||||||
|
.to_string()
|
||||||
|
.contains(RESULT_METADATA_PRIVATE_RESULT)
|
||||||
|
);
|
||||||
|
let (body, success) = custom_tool_output_body_and_success(&request, "call-1");
|
||||||
|
assert_ne!(success, Some(false), "Code Mode failed: {body}");
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::from_str::<Value>(&body)?,
|
||||||
|
serde_json::json!({ "isError": is_error, "hasMeta": false }),
|
||||||
|
);
|
||||||
|
request.custom_tool_call_output("call-1")
|
||||||
|
};
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result_metadata_fixture_calls(&request.input()).count(),
|
result_metadata_fixture_calls(&request.input()).count(),
|
||||||
usize::from(metadata_enabled),
|
usize::from(metadata_enabled),
|
||||||
);
|
);
|
||||||
let output = request.custom_tool_call_output("call-1");
|
let captured = codex_core::test_support::history_with_tool_call_metadata(&test.codex).await;
|
||||||
|
if direct {
|
||||||
|
let result = captured
|
||||||
|
.iter()
|
||||||
|
.find_map(|item| match item {
|
||||||
|
codex_protocol::models::ResponseItem::FunctionCallOutput {
|
||||||
|
call_id,
|
||||||
|
output,
|
||||||
|
..
|
||||||
|
} if call_id.as_deref() == Some("call-1") => Some(output),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.expect("captured direct output");
|
||||||
|
assert_eq!(result.success, Some(!is_error));
|
||||||
|
}
|
||||||
if metadata_enabled {
|
if metadata_enabled {
|
||||||
// The custom inference endpoint gets no raw metadata; inspect capture independently.
|
// The custom inference endpoint gets no raw metadata; inspect capture independently.
|
||||||
assert_result_metadata_call(&output, &arguments, /*expected_metadata*/ None);
|
assert_result_metadata_call(&output, &arguments, /*expected_metadata*/ None);
|
||||||
let captured = codex_core::test_support::history_with_tool_call_metadata(&test.codex).await;
|
|
||||||
let captured = serde_json::to_value(captured)?;
|
let captured = serde_json::to_value(captured)?;
|
||||||
let captured_output = captured
|
let captured_output = captured
|
||||||
.as_array()
|
.as_array()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.find(|item| item["type"] == "custom_tool_call_output" && item["call_id"] == "call-1")
|
.find(|item| item["type"] == output["type"] && item["call_id"] == "call-1")
|
||||||
.expect("captured exec output");
|
.expect("captured tool output");
|
||||||
// The public build never captures result metadata, even with employee test credentials.
|
// The public build never captures result metadata, even with employee test credentials.
|
||||||
let expected_metadata = None;
|
let expected_metadata = None;
|
||||||
assert_result_metadata_call(captured_output, &arguments, expected_metadata);
|
assert_result_metadata_call(captured_output, &arguments, expected_metadata);
|
||||||
|
|||||||
@@ -0,0 +1,357 @@
|
|||||||
|
//! Direct-call metadata coverage, including malformed calls and request-budget pruning.
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use codex_features::Feature;
|
||||||
|
use codex_model_provider::RemoteCompactionSupport;
|
||||||
|
use codex_protocol::models::PermissionProfile;
|
||||||
|
use codex_protocol::protocol::AskForApproval;
|
||||||
|
use codex_protocol::protocol::EventMsg;
|
||||||
|
use codex_protocol::protocol::Op;
|
||||||
|
use core_test_support::apps_test_server::configure_search_capable_model;
|
||||||
|
use core_test_support::responses::ev_assistant_message;
|
||||||
|
use core_test_support::responses::ev_completed;
|
||||||
|
use core_test_support::responses::ev_function_call;
|
||||||
|
use core_test_support::responses::ev_response_created;
|
||||||
|
use core_test_support::responses::ev_tool_search_call;
|
||||||
|
use core_test_support::responses::mount_sse_sequence;
|
||||||
|
use core_test_support::responses::sse;
|
||||||
|
use core_test_support::responses::start_mock_server;
|
||||||
|
use core_test_support::skip_if_no_network;
|
||||||
|
use core_test_support::test_codex::test_codex;
|
||||||
|
use core_test_support::wait_for_event;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use serde_json::Value;
|
||||||
|
use serde_json::json;
|
||||||
|
use test_case::test_case;
|
||||||
|
|
||||||
|
pub(super) fn tool_call_metadata(mut item: Value) -> Value {
|
||||||
|
let mut metadata = item["internal_chat_message_metadata_passthrough"].take();
|
||||||
|
let fields = metadata.as_object_mut().expect("output metadata");
|
||||||
|
fields.remove("turn_id");
|
||||||
|
fields.remove("create_time");
|
||||||
|
metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case(RemoteCompactionSupport::Unsupported, true; "local")]
|
||||||
|
#[test_case(RemoteCompactionSupport::V2, true; "remote_v2")]
|
||||||
|
#[test_case(RemoteCompactionSupport::V2, false; "remote_v2_disabled_after_capture")]
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn direct_call_metadata_during_compaction_respects_provider_support(
|
||||||
|
remote_compaction: RemoteCompactionSupport,
|
||||||
|
metadata_enabled: bool,
|
||||||
|
) -> Result<()> {
|
||||||
|
skip_if_no_network!(Ok(()));
|
||||||
|
let server = start_mock_server().await;
|
||||||
|
let mut builder = test_codex().with_config(move |config| {
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("enable tool-call metadata");
|
||||||
|
config.update_plan_enabled = true;
|
||||||
|
if remote_compaction == RemoteCompactionSupport::Unsupported {
|
||||||
|
config.model_provider.name = "OpenAI-compatible test provider".to_string();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let test = builder.build_with_auto_env(&server).await?;
|
||||||
|
let seed_arguments = json!({"plan": [{"step": "read", "status": "in_progress"}]});
|
||||||
|
let arguments = json!({"plan": [{"step": "read", "status": "completed"}]});
|
||||||
|
let summary = "The prior call finished.";
|
||||||
|
let compact_output = match remote_compaction {
|
||||||
|
RemoteCompactionSupport::Unsupported => ev_assistant_message("summary", summary),
|
||||||
|
RemoteCompactionSupport::V2 => json!({
|
||||||
|
"type": "response.output_item.done",
|
||||||
|
"item": {"type": "compaction", "encrypted_content": summary},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
let mock = mount_sse_sequence(
|
||||||
|
&server,
|
||||||
|
vec![
|
||||||
|
sse(vec![
|
||||||
|
ev_function_call("shared", "update_plan", &seed_arguments.to_string()),
|
||||||
|
ev_completed("seed-call-response"),
|
||||||
|
]),
|
||||||
|
sse(vec![
|
||||||
|
ev_assistant_message("seed", "done"),
|
||||||
|
ev_completed("seed-response"),
|
||||||
|
]),
|
||||||
|
sse(vec![compact_output, ev_completed("compact-response")]),
|
||||||
|
sse(vec![
|
||||||
|
ev_function_call("shared", "update_plan", &arguments.to_string()),
|
||||||
|
ev_completed("reused-response"),
|
||||||
|
]),
|
||||||
|
sse(vec![ev_completed("done")]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
test.submit_turn("Update the plan before compaction")
|
||||||
|
.await?;
|
||||||
|
// Read the live history: deserializing a rollout intentionally drops host-owned metadata.
|
||||||
|
let history = test.codex.conversation_history_snapshot().await;
|
||||||
|
let history = serde_json::to_value(history.items().collect::<Vec<_>>())?;
|
||||||
|
let history = history.as_array().expect("source history");
|
||||||
|
let seed_output = history
|
||||||
|
.iter()
|
||||||
|
.find(|item| item["type"] == "function_call_output" && item["call_id"] == "shared")
|
||||||
|
.expect("direct output before compaction");
|
||||||
|
assert_eq!(seed_output["output"], "Plan updated");
|
||||||
|
assert_eq!(
|
||||||
|
tool_call_metadata(seed_output.clone()),
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{"name": "update_plan", "arguments": seed_arguments}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
let user_content = &history
|
||||||
|
.iter()
|
||||||
|
.find(|item| {
|
||||||
|
item["role"] == "user"
|
||||||
|
&& item["content"][0]["text"] == "Update the plan before compaction"
|
||||||
|
})
|
||||||
|
.expect("source user message")["content"];
|
||||||
|
if !metadata_enabled {
|
||||||
|
let mut config = test.config.clone();
|
||||||
|
config.features.disable(Feature::ExecutedToolCallMetadata)?;
|
||||||
|
test.codex.refresh_runtime_config(config).await;
|
||||||
|
}
|
||||||
|
test.codex.submit(Op::Compact).await?;
|
||||||
|
wait_for_event(&test.codex, |event| {
|
||||||
|
matches!(event, EventMsg::TurnComplete(_))
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
let compacted = test.codex.conversation_history_snapshot().await;
|
||||||
|
let compacted = serde_json::to_value(compacted.items().collect::<Vec<_>>())?;
|
||||||
|
let compacted = compacted.as_array().expect("compacted history");
|
||||||
|
// Both paths retain the user message and summary, not the old call or output.
|
||||||
|
assert!(compacted.iter().all(|item| item["call_id"] != "shared"));
|
||||||
|
assert!(
|
||||||
|
compacted
|
||||||
|
.iter()
|
||||||
|
.any(|item| item["role"] == "user" && &item["content"] == user_content)
|
||||||
|
);
|
||||||
|
match remote_compaction {
|
||||||
|
RemoteCompactionSupport::Unsupported => assert!(compacted.iter().any(|item| {
|
||||||
|
item["role"] == "user"
|
||||||
|
&& item["content"][0]["text"]
|
||||||
|
== format!("{}\n{summary}", codex_core::compact::SUMMARY_PREFIX)
|
||||||
|
})),
|
||||||
|
RemoteCompactionSupport::V2 => {
|
||||||
|
assert!(compacted.iter().any(|item| {
|
||||||
|
item["type"] == "compaction" && item["encrypted_content"] == summary
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test.submit_turn("Update the plan").await?;
|
||||||
|
|
||||||
|
let requests = mock.requests();
|
||||||
|
assert_eq!(requests.len(), 5);
|
||||||
|
let compact_request = &requests[2];
|
||||||
|
assert_eq!(
|
||||||
|
compact_request.inputs_of_type("compaction_trigger").len(),
|
||||||
|
usize::from(remote_compaction == RemoteCompactionSupport::V2),
|
||||||
|
);
|
||||||
|
let compact_output = compact_request.function_call_output("shared");
|
||||||
|
assert_eq!(compact_output["output"], seed_output["output"]);
|
||||||
|
// The local case uses a third-party provider; compact_tests.rs separately covers
|
||||||
|
// local compaction with an OpenAI provider that accepts passthrough metadata.
|
||||||
|
match remote_compaction {
|
||||||
|
RemoteCompactionSupport::Unsupported => assert!(
|
||||||
|
compact_output
|
||||||
|
.get("internal_chat_message_metadata_passthrough")
|
||||||
|
.is_none()
|
||||||
|
),
|
||||||
|
RemoteCompactionSupport::V2 => {
|
||||||
|
let mut expected = seed_output["internal_chat_message_metadata_passthrough"].clone();
|
||||||
|
if !metadata_enabled {
|
||||||
|
let metadata = expected.as_object_mut().expect("source metadata");
|
||||||
|
metadata.remove("executed_tool_calls");
|
||||||
|
metadata.remove("tool_calls_complete");
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
compact_output["internal_chat_message_metadata_passthrough"],
|
||||||
|
expected
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
requests[3]
|
||||||
|
.input()
|
||||||
|
.iter()
|
||||||
|
.all(|item| item["call_id"] != "shared")
|
||||||
|
);
|
||||||
|
let output = requests[4].function_call_output("shared");
|
||||||
|
assert_eq!(output["output"], "Plan updated");
|
||||||
|
let captured = test.codex.conversation_history_snapshot().await;
|
||||||
|
let captured = serde_json::to_value(captured.items().collect::<Vec<_>>())?;
|
||||||
|
let captured_output = captured
|
||||||
|
.as_array()
|
||||||
|
.expect("captured history")
|
||||||
|
.iter()
|
||||||
|
.find(|item| item["type"] == "function_call_output" && item["call_id"] == "shared")
|
||||||
|
.expect("captured direct output");
|
||||||
|
assert_eq!(
|
||||||
|
tool_call_metadata(captured_output.clone()),
|
||||||
|
if metadata_enabled {
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{"name": "update_plan", "arguments": arguments}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
json!({})
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case(false, 0; "metadata disabled")]
|
||||||
|
#[test_case(true, 6; "request budget exceeded")]
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn direct_function_and_tool_search_mark_complete_attempts(
|
||||||
|
metadata_enabled: bool,
|
||||||
|
budget_calls: usize,
|
||||||
|
) -> Result<()> {
|
||||||
|
skip_if_no_network!(Ok(()));
|
||||||
|
|
||||||
|
let request_budget = 32 * 1024;
|
||||||
|
let server = start_mock_server().await;
|
||||||
|
let mut builder = test_codex().with_config(move |config| {
|
||||||
|
configure_search_capable_model(config);
|
||||||
|
config.update_plan_enabled = true;
|
||||||
|
if metadata_enabled {
|
||||||
|
let _ = config.features.enable(Feature::ExecutedToolCallMetadata);
|
||||||
|
} else {
|
||||||
|
let _ = config.features.disable(Feature::ExecutedToolCallMetadata);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let test = builder.build_with_auto_env(&server).await?;
|
||||||
|
let valid_arguments = json!({"plan": [{"step": "read", "status": "in_progress"}]});
|
||||||
|
let malformed_arguments = "{malformed";
|
||||||
|
let search_arguments = json!({"query": "nonexistent-completeness-proof-tool", "limit": null});
|
||||||
|
// The bulk calls fit the per-call limit; their combined metadata forces pruning.
|
||||||
|
let budget_arguments =
|
||||||
|
json!({"plan": [{"step": "x".repeat(7 * 1024), "status": "in_progress"}]});
|
||||||
|
let budget_arguments_json = budget_arguments.to_string();
|
||||||
|
assert!(budget_arguments_json.len() < 8 * 1024);
|
||||||
|
if budget_calls > 0 {
|
||||||
|
assert!(budget_calls * budget_arguments_json.len() > request_budget);
|
||||||
|
}
|
||||||
|
let mut events = vec![ev_response_created("resp-1")];
|
||||||
|
if budget_calls > 0 {
|
||||||
|
events.push(ev_function_call(
|
||||||
|
"plan-oversized",
|
||||||
|
"update_plan",
|
||||||
|
&json!({"plan": [{"step": "x".repeat(9 * 1024), "status": "in_progress"}]}).to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
for index in 0..budget_calls {
|
||||||
|
events.push(ev_function_call(
|
||||||
|
&format!("plan-budget-{index}"),
|
||||||
|
"update_plan",
|
||||||
|
&budget_arguments_json,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
events.extend([
|
||||||
|
ev_function_call("plan-valid", "update_plan", &valid_arguments.to_string()),
|
||||||
|
ev_function_call("plan-malformed", "update_plan", malformed_arguments),
|
||||||
|
ev_tool_search_call("search", &search_arguments),
|
||||||
|
ev_completed("resp-1"),
|
||||||
|
]);
|
||||||
|
let mock = mount_sse_sequence(
|
||||||
|
&server,
|
||||||
|
vec![
|
||||||
|
sse(events),
|
||||||
|
sse(vec![
|
||||||
|
ev_assistant_message("msg-1", "done"),
|
||||||
|
ev_completed("resp-2"),
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
test.submit_turn_with_approval_and_permission_profile(
|
||||||
|
"exercise direct attempts",
|
||||||
|
AskForApproval::Never,
|
||||||
|
PermissionProfile::Disabled,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let requests = mock.requests();
|
||||||
|
assert_eq!(requests.len(), 2);
|
||||||
|
let request = &requests[1];
|
||||||
|
let valid_output = request.function_call_output("plan-valid");
|
||||||
|
let malformed_output = request.function_call_output("plan-malformed");
|
||||||
|
let search_output = request.tool_search_output("search");
|
||||||
|
assert_eq!(valid_output["output"], json!("Plan updated"));
|
||||||
|
assert!(
|
||||||
|
malformed_output["output"]
|
||||||
|
.as_str()
|
||||||
|
.expect("parse error output")
|
||||||
|
.starts_with("failed to parse function arguments:"),
|
||||||
|
);
|
||||||
|
assert_eq!(search_output["execution"], json!("client"));
|
||||||
|
|
||||||
|
let mut metadata_bytes = 0;
|
||||||
|
for (output, name, arguments) in [
|
||||||
|
(valid_output, "update_plan", valid_arguments),
|
||||||
|
(malformed_output, "update_plan", json!(malformed_arguments)),
|
||||||
|
(
|
||||||
|
search_output,
|
||||||
|
"tool_search",
|
||||||
|
json!({"query": "nonexistent-completeness-proof-tool"}),
|
||||||
|
),
|
||||||
|
] {
|
||||||
|
let expected = if metadata_enabled {
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{"name": name, "arguments": arguments}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
json!({})
|
||||||
|
};
|
||||||
|
let metadata = tool_call_metadata(output);
|
||||||
|
metadata_bytes += serde_json::to_vec(&metadata)?.len();
|
||||||
|
assert_eq!(metadata, expected);
|
||||||
|
}
|
||||||
|
if budget_calls > 0 {
|
||||||
|
let output = request.function_call_output("plan-oversized");
|
||||||
|
assert_eq!(output["output"], json!("Plan updated"));
|
||||||
|
let metadata = tool_call_metadata(output);
|
||||||
|
metadata_bytes += serde_json::to_vec(&metadata)?.len();
|
||||||
|
assert!(
|
||||||
|
metadata["executed_tool_calls"][0]["arguments"]
|
||||||
|
.get("_codex_executed_tool_call_truncated")
|
||||||
|
.is_some()
|
||||||
|
);
|
||||||
|
assert!(metadata.get("tool_calls_complete").is_none());
|
||||||
|
}
|
||||||
|
let mut truncated = 0;
|
||||||
|
for index in 0..budget_calls {
|
||||||
|
let output = request.function_call_output(&format!("plan-budget-{index}"));
|
||||||
|
assert_eq!(output["output"], json!("Plan updated"));
|
||||||
|
let metadata = tool_call_metadata(output);
|
||||||
|
metadata_bytes += serde_json::to_vec(&metadata)?.len();
|
||||||
|
let calls = metadata["executed_tool_calls"]
|
||||||
|
.as_array()
|
||||||
|
.expect("recorded calls");
|
||||||
|
assert_eq!(calls.len(), 1);
|
||||||
|
if calls[0]["arguments"]
|
||||||
|
.get("_codex_executed_tool_call_truncated")
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
truncated += 1;
|
||||||
|
assert!(metadata.get("tool_calls_complete").is_none());
|
||||||
|
} else {
|
||||||
|
assert_eq!(
|
||||||
|
metadata,
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{"name": "update_plan", "arguments": budget_arguments}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(metadata_bytes <= request_budget);
|
||||||
|
assert!(
|
||||||
|
budget_calls == 0 || truncated > 0,
|
||||||
|
"request must exercise pruning"
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -300,7 +300,7 @@ async fn guardian_review_compacts_with_summary_despite_parent_token_budget(
|
|||||||
skip_if_no_network!(Ok(()));
|
skip_if_no_network!(Ok(()));
|
||||||
skip_if_wine_exec!(
|
skip_if_wine_exec!(
|
||||||
Ok(()),
|
Ok(()),
|
||||||
"Guardian approval actions require host-native paths"
|
"approved commands can collide on process IDs in the shared Wine exec server"
|
||||||
);
|
);
|
||||||
|
|
||||||
let server = start_mock_server().await;
|
let server = start_mock_server().await;
|
||||||
@@ -440,6 +440,127 @@ async fn guardian_review_compacts_with_summary_despite_parent_token_budget(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
#[test_case(false; "legacy_transcript")]
|
||||||
|
#[test_case(true; "thread_owned_transcript")]
|
||||||
|
async fn guardian_requests_record_only_their_own_tool_calls(thread_owned: bool) -> Result<()> {
|
||||||
|
skip_if_no_network!(Ok(()));
|
||||||
|
skip_if_wine_exec!(
|
||||||
|
Ok(()),
|
||||||
|
"basic PowerShell execution through Wine exec is not passing yet"
|
||||||
|
);
|
||||||
|
|
||||||
|
let server = start_mock_server().await;
|
||||||
|
let mut builder = test_codex().with_config(move |config| {
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.set_enabled(Feature::GuardianThreadContext, thread_owned)
|
||||||
|
.expect("configure Guardian context mode");
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("enable tool-call metadata");
|
||||||
|
config.update_plan_enabled = true;
|
||||||
|
config.permissions.approval_policy = Constrained::allow_any(AskForApproval::OnRequest);
|
||||||
|
config.approvals_reviewer = ApprovalsReviewer::AutoReview;
|
||||||
|
});
|
||||||
|
let test = builder.build_with_auto_env(&server).await?;
|
||||||
|
let plan_args = json!({"plan": [{"step": "inspect", "status": "completed"}]});
|
||||||
|
let guardian_tool_args = json!({
|
||||||
|
"cmd": "printf guardian-metadata-own-call",
|
||||||
|
"login": false,
|
||||||
|
"yield_time_ms": 1000,
|
||||||
|
});
|
||||||
|
let responses = mount_sse_sequence(
|
||||||
|
&server,
|
||||||
|
vec![
|
||||||
|
sse(vec![
|
||||||
|
ev_function_call("plan", "update_plan", &plan_args.to_string()),
|
||||||
|
ev_completed("parent-plan"),
|
||||||
|
]),
|
||||||
|
sse(vec![
|
||||||
|
ev_function_call(
|
||||||
|
"reviewed-command",
|
||||||
|
"exec_command",
|
||||||
|
r#"{"cmd":"true","sandbox_permissions":"require_escalated","justification":"Exercise Guardian metadata filtering."}"#,
|
||||||
|
),
|
||||||
|
ev_completed("parent-review"),
|
||||||
|
]),
|
||||||
|
sse(vec![
|
||||||
|
ev_function_call(
|
||||||
|
"guardian-command",
|
||||||
|
"exec_command",
|
||||||
|
&guardian_tool_args.to_string(),
|
||||||
|
),
|
||||||
|
ev_completed("guardian-command-response"),
|
||||||
|
]),
|
||||||
|
sse(vec![
|
||||||
|
ev_assistant_message(
|
||||||
|
"guardian-denial",
|
||||||
|
r#"{"risk_level":"high","user_authorization":"low","outcome":"deny","rationale":"The test denies escalation."}"#,
|
||||||
|
),
|
||||||
|
ev_completed("guardian-review"),
|
||||||
|
]),
|
||||||
|
sse(vec![ev_completed("parent-done")]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
test.submit_text_turn("Update the plan, then request a reviewed command")
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let expected_calls = json!([{"name": "update_plan", "arguments": plan_args}]);
|
||||||
|
let requests = responses.requests();
|
||||||
|
assert_eq!(requests.len(), 5);
|
||||||
|
let parent_output = requests[1].function_call_output("plan");
|
||||||
|
assert_eq!(parent_output["output"], "Plan updated");
|
||||||
|
assert_eq!(
|
||||||
|
parent_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"],
|
||||||
|
expected_calls,
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
parent_output["internal_chat_message_metadata_passthrough"]["tool_calls_complete"],
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
let guardian_output = requests[3].function_call_output("guardian-command");
|
||||||
|
let guardian_text = guardian_output["output"].as_str().expect("command output");
|
||||||
|
assert!(
|
||||||
|
guardian_text.contains("Process exited with code 0"),
|
||||||
|
"Guardian command did not complete successfully: {guardian_text}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
guardian_text.ends_with("guardian-metadata-own-call"),
|
||||||
|
"Guardian command returned unexpected output: {guardian_text}"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
super::direct_tool_metadata::tool_call_metadata(guardian_output),
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{"name": "exec_command", "arguments": guardian_tool_args}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
for guardian_request in &requests[2..4] {
|
||||||
|
assert_eq!(
|
||||||
|
guardian_request.body_json()["client_metadata"]["x-openai-subagent"],
|
||||||
|
"guardian",
|
||||||
|
);
|
||||||
|
assert!(guardian_request.body_contains_text("Plan updated"));
|
||||||
|
for item in guardian_request.input() {
|
||||||
|
if item["type"] == "function_call_output" && item["call_id"] == "guardian-command" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let metadata = &item["internal_chat_message_metadata_passthrough"];
|
||||||
|
for field in ["executed_tool_calls", "tool_calls_complete", "cell_id"] {
|
||||||
|
assert!(
|
||||||
|
metadata.get(field).is_none(),
|
||||||
|
"Guardian reattributed parent {field} to an unrelated input item"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
#[test_case(CodexAuth::from_api_key("test-api-key"), true, "gpt-5.6-luna"; "api_key_uses_luna_with_responses_lite")]
|
#[test_case(CodexAuth::from_api_key("test-api-key"), true, "gpt-5.6-luna"; "api_key_uses_luna_with_responses_lite")]
|
||||||
#[test_case(CodexAuth::create_dummy_chatgpt_auth_for_testing(), true, "codex-auto-review"; "chatgpt_uses_codex_auto_review")]
|
#[test_case(CodexAuth::create_dummy_chatgpt_auth_for_testing(), true, "codex-auto-review"; "chatgpt_uses_codex_auto_review")]
|
||||||
@@ -2214,10 +2335,6 @@ printf '%s\n' "${@: -1}" >> "${payload_path}""#,
|
|||||||
async fn yielded_code_mode_denials_interrupt_the_servicing_turn() -> Result<()> {
|
async fn yielded_code_mode_denials_interrupt_the_servicing_turn() -> Result<()> {
|
||||||
skip_if_no_network!(Ok(()));
|
skip_if_no_network!(Ok(()));
|
||||||
skip_if_sandbox!(Ok(()));
|
skip_if_sandbox!(Ok(()));
|
||||||
skip_if_wine_exec!(
|
|
||||||
Ok(()),
|
|
||||||
"Guardian approval actions require host-native paths"
|
|
||||||
);
|
|
||||||
|
|
||||||
let server = start_mock_server().await;
|
let server = start_mock_server().await;
|
||||||
let mut builder = test_codex()
|
let mut builder = test_codex()
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ mod cyber_access_program;
|
|||||||
mod cyber_exec_policy;
|
mod cyber_exec_policy;
|
||||||
mod daybreak_access;
|
mod daybreak_access;
|
||||||
mod deprecation_notice;
|
mod deprecation_notice;
|
||||||
|
mod direct_tool_metadata;
|
||||||
mod exec;
|
mod exec;
|
||||||
mod exec_policy;
|
mod exec_policy;
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ use wiremock::MockServer;
|
|||||||
use wiremock::matchers::method;
|
use wiremock::matchers::method;
|
||||||
use wiremock::matchers::path;
|
use wiremock::matchers::path;
|
||||||
|
|
||||||
|
use super::direct_tool_metadata::tool_call_metadata;
|
||||||
|
|
||||||
const SPAWN_CALL_ID: &str = "spawn-call-1";
|
const SPAWN_CALL_ID: &str = "spawn-call-1";
|
||||||
const MULTI_AGENT_V1_NAMESPACE: &str = "multi_agent_v1";
|
const MULTI_AGENT_V1_NAMESPACE: &str = "multi_agent_v1";
|
||||||
const MULTI_AGENT_V2_NAMESPACE: &str = "collaboration";
|
const MULTI_AGENT_V2_NAMESPACE: &str = "collaboration";
|
||||||
@@ -2235,16 +2237,24 @@ async fn multi_agent_v2_spawn_sends_agent_message_to_child(
|
|||||||
} else {
|
} else {
|
||||||
"koffing"
|
"koffing"
|
||||||
};
|
};
|
||||||
let mut builder = test_codex().with_model(parent_model).with_config(|config| {
|
let mut builder = test_codex()
|
||||||
config
|
.with_model(parent_model)
|
||||||
.features
|
.with_config(move |config| {
|
||||||
.enable(Feature::Collab)
|
config
|
||||||
.expect("test config should allow feature update");
|
.features
|
||||||
config
|
.enable(Feature::Collab)
|
||||||
.features
|
.expect("test config should allow feature update");
|
||||||
.enable(Feature::MultiAgentV2)
|
config
|
||||||
.expect("test config should allow feature update");
|
.features
|
||||||
});
|
.enable(Feature::MultiAgentV2)
|
||||||
|
.expect("test config should allow feature update");
|
||||||
|
if plaintext {
|
||||||
|
config
|
||||||
|
.features
|
||||||
|
.enable(Feature::ExecutedToolCallMetadata)
|
||||||
|
.expect("enable tool-call metadata");
|
||||||
|
}
|
||||||
|
});
|
||||||
let test = builder.build(&server).await?;
|
let test = builder.build(&server).await?;
|
||||||
let root_thread_id = test.session_configured.thread_id;
|
let root_thread_id = test.session_configured.thread_id;
|
||||||
|
|
||||||
@@ -2307,15 +2317,33 @@ async fn multi_agent_v2_spawn_sends_agent_message_to_child(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if plaintext {
|
if plaintext {
|
||||||
|
let parent_request = parent_request_log
|
||||||
|
.requests()
|
||||||
|
.into_iter()
|
||||||
|
.find(|request| {
|
||||||
|
request
|
||||||
|
.inputs_of_type("function_call_output")
|
||||||
|
.iter()
|
||||||
|
.any(|item| item["call_id"] == SPAWN_CALL_ID)
|
||||||
|
})
|
||||||
|
.expect("parent request with spawn result");
|
||||||
assert!(
|
assert!(
|
||||||
parent_request_log.requests().into_iter().any(|request| {
|
parent_request.input().iter().any(|item| {
|
||||||
request.input().iter().any(|item| {
|
item["call_id"].as_str() == Some(SPAWN_CALL_ID)
|
||||||
item["call_id"].as_str() == Some(SPAWN_CALL_ID)
|
&& item["encrypted_function_args"] == json!([])
|
||||||
&& item["encrypted_function_args"] == json!([])
|
|
||||||
})
|
|
||||||
}),
|
}),
|
||||||
"plaintext function-call metadata should survive replay"
|
"plaintext function-call metadata should survive replay"
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
tool_call_metadata(parent_request.function_call_output(SPAWN_CALL_ID)),
|
||||||
|
json!({
|
||||||
|
"executed_tool_calls": [{
|
||||||
|
"name": "collaboration__spawn_agent",
|
||||||
|
"arguments": serde_json::from_str::<Value>(&spawn_args)?,
|
||||||
|
}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let child_thread_id = test
|
let child_thread_id = test
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ use serde_json::json;
|
|||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
use wiremock::ResponseTemplate;
|
use wiremock::ResponseTemplate;
|
||||||
|
|
||||||
|
use super::direct_tool_metadata::tool_call_metadata;
|
||||||
|
|
||||||
fn tool_names(body: &Value) -> Vec<String> {
|
fn tool_names(body: &Value) -> Vec<String> {
|
||||||
body.get("tools")
|
body.get("tools")
|
||||||
.and_then(Value::as_array)
|
.and_then(Value::as_array)
|
||||||
@@ -448,6 +450,7 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re
|
|||||||
"name": format!("{namespace}__{tool_name}"),
|
"name": format!("{namespace}__{tool_name}"),
|
||||||
"arguments": input,
|
"arguments": input,
|
||||||
}],
|
}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -484,12 +487,14 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re
|
|||||||
.await?;
|
.await?;
|
||||||
let escaped_request = escaped_mock.single_request();
|
let escaped_request = escaped_mock.single_request();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
escaped_request.custom_tool_call_output(call_id)["internal_chat_message_metadata_passthrough"]
|
tool_call_metadata(escaped_request.custom_tool_call_output(call_id)),
|
||||||
["executed_tool_calls"],
|
json!({
|
||||||
json!([{
|
"executed_tool_calls": [{
|
||||||
"name": format!("{namespace}__{tool_name}"),
|
"name": format!("{namespace}__{tool_name}"),
|
||||||
"arguments": input,
|
"arguments": input,
|
||||||
}]),
|
}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
let expected_escaped_calls = json!([{
|
let expected_escaped_calls = json!([{
|
||||||
"name": format!("{namespace}__{tool_name}"),
|
"name": format!("{namespace}__{tool_name}"),
|
||||||
@@ -501,9 +506,8 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re
|
|||||||
},
|
},
|
||||||
}]);
|
}]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
escaped_request.custom_tool_call_output(escaped_call_id)["internal_chat_message_metadata_passthrough"]
|
tool_call_metadata(escaped_request.custom_tool_call_output(escaped_call_id)),
|
||||||
["executed_tool_calls"],
|
json!({"executed_tool_calls": expected_escaped_calls}),
|
||||||
expected_escaped_calls,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let direct_exec_call_id = "custom-direct-exec";
|
let direct_exec_call_id = "custom-direct-exec";
|
||||||
@@ -538,17 +542,12 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re
|
|||||||
|
|
||||||
let direct_exec_request = direct_exec_mock.single_request();
|
let direct_exec_request = direct_exec_mock.single_request();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
direct_exec_request.custom_tool_call_output(call_id)["internal_chat_message_metadata_passthrough"]
|
tool_call_metadata(direct_exec_request.custom_tool_call_output(call_id)),
|
||||||
["executed_tool_calls"],
|
tool_call_metadata(escaped_request.custom_tool_call_output(call_id)),
|
||||||
json!([{
|
|
||||||
"name": format!("{namespace}__{tool_name}"),
|
|
||||||
"arguments": input,
|
|
||||||
}]),
|
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
direct_exec_request.custom_tool_call_output(escaped_call_id)["internal_chat_message_metadata_passthrough"]
|
tool_call_metadata(direct_exec_request.custom_tool_call_output(escaped_call_id)),
|
||||||
["executed_tool_calls"],
|
tool_call_metadata(escaped_request.custom_tool_call_output(escaped_call_id)),
|
||||||
expected_escaped_calls,
|
|
||||||
);
|
);
|
||||||
let direct_exec_output = direct_exec_request.custom_tool_call_output(direct_exec_call_id);
|
let direct_exec_output = direct_exec_request.custom_tool_call_output(direct_exec_call_id);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -556,11 +555,14 @@ async fn namespaced_custom_tool_call_preserves_namespace_through_dispatch_and_re
|
|||||||
json!("unsupported custom tool call: exec"),
|
json!("unsupported custom tool call: exec"),
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
direct_exec_output["internal_chat_message_metadata_passthrough"]["executed_tool_calls"],
|
tool_call_metadata(direct_exec_output),
|
||||||
json!([{
|
json!({
|
||||||
"name": codex_code_mode::PUBLIC_TOOL_NAME,
|
"executed_tool_calls": [{
|
||||||
"arguments": input,
|
"name": codex_code_mode::PUBLIC_TOOL_NAME,
|
||||||
}]),
|
"arguments": input,
|
||||||
|
}],
|
||||||
|
"tool_calls_complete": true,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use codex_protocol::models::ContentItem;
|
use codex_protocol::models::ContentItem;
|
||||||
use codex_protocol::models::ResponseItem;
|
use codex_protocol::models::ResponseItem;
|
||||||
|
use codex_protocol::models::executed_tool_call_metadata_bytes;
|
||||||
|
|
||||||
use crate::SectionHistory;
|
use crate::SectionHistory;
|
||||||
|
|
||||||
@@ -52,8 +53,10 @@ impl TranscriptHistory {
|
|||||||
/// Non-user overflow removes at least half the existing entries; byte limits may need more.
|
/// 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.
|
/// Oversized user images fall back to bounded text; other oversized items are skipped.
|
||||||
pub fn record(&mut self, item: &ResponseItem) {
|
pub fn record(&mut self, item: &ResponseItem) {
|
||||||
|
let metadata_bytes = executed_tool_call_metadata_bytes(item);
|
||||||
let mut size = BoundedSize {
|
let mut size = BoundedSize {
|
||||||
bytes: std::mem::size_of::<ResponseItem>(),
|
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(|()| {
|
let measured = serde_json::to_writer(&mut size, item).and_then(|()| {
|
||||||
// ResponseItem serialization can omit reasoning content that cloning retains.
|
// ResponseItem serialization can omit reasoning content that cloning retains.
|
||||||
@@ -111,6 +114,8 @@ impl TranscriptHistory {
|
|||||||
}
|
}
|
||||||
return;
|
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 is_user = item.is_user_message();
|
||||||
let (mut count, mut bytes) = self
|
let (mut count, mut bytes) = self
|
||||||
.items
|
.items
|
||||||
@@ -178,11 +183,12 @@ impl SectionHistory for TranscriptHistory {
|
|||||||
// Count without allocating a serialized copy, and stop before cloning an oversized item.
|
// Count without allocating a serialized copy, and stop before cloning an oversized item.
|
||||||
struct BoundedSize {
|
struct BoundedSize {
|
||||||
bytes: usize,
|
bytes: usize,
|
||||||
|
max_bytes: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for BoundedSize {
|
impl Write for BoundedSize {
|
||||||
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
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(
|
return Err(io::Error::other(
|
||||||
"review history item exceeds storage budget",
|
"review history item exceeds storage budget",
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use codex_protocol::models::ExecutedToolCall;
|
||||||
use codex_protocol::models::ReasoningItemContent;
|
use codex_protocol::models::ReasoningItemContent;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use serde_json::json;
|
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]);
|
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]
|
#[test]
|
||||||
fn oversized_user_images_preserve_text_and_metadata_in_order() {
|
fn oversized_user_images_preserve_text_and_metadata_in_order() {
|
||||||
let before = tool("earlier tool result");
|
let before = tool("earlier tool result");
|
||||||
|
|||||||
@@ -961,7 +961,8 @@ pub struct InternalChatMessageMetadataPassthrough {
|
|||||||
#[schemars(skip)]
|
#[schemars(skip)]
|
||||||
#[ts(skip)]
|
#[ts(skip)]
|
||||||
pub content_item_kinds: Option<Vec<ContentItemKind>>,
|
pub content_item_kinds: Option<Vec<ContentItemKind>>,
|
||||||
// Ignore input values so requests cannot fake tool call records.
|
// Ignore input values so requests and rollout reloads cannot fake tool call records.
|
||||||
|
// A resumed thread can record new calls, but cannot prove old calls from this payload.
|
||||||
/// Host-owned Code Mode cell shared by its `exec` and subsequent `wait` outputs.
|
/// Host-owned Code Mode cell shared by its `exec` and subsequent `wait` outputs.
|
||||||
#[serde(default, skip_deserializing, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_deserializing, skip_serializing_if = "Option::is_none")]
|
||||||
#[schemars(skip)]
|
#[schemars(skip)]
|
||||||
@@ -972,8 +973,9 @@ pub struct InternalChatMessageMetadataPassthrough {
|
|||||||
#[schemars(skip)]
|
#[schemars(skip)]
|
||||||
#[ts(skip)]
|
#[ts(skip)]
|
||||||
pub executed_tool_calls: Option<Vec<ExecutedToolCall>>,
|
pub executed_tool_calls: Option<Vec<ExecutedToolCall>>,
|
||||||
/// Whether the host finished recording this cell's calls without losing calls or arguments.
|
/// Whether the host recorded the complete call inventory without losing calls or arguments.
|
||||||
/// This describes the call inventory across the cell's outputs, not tool success.
|
/// For a direct tool output this covers its single invocation; with `cell_id`, it covers
|
||||||
|
/// the Code Mode cell across its outputs. Neither case describes tool success.
|
||||||
#[serde(default, skip_deserializing, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_deserializing, skip_serializing_if = "Option::is_none")]
|
||||||
#[schemars(skip)]
|
#[schemars(skip)]
|
||||||
#[ts(skip)]
|
#[ts(skip)]
|
||||||
|
|||||||
@@ -463,7 +463,7 @@ impl ResponseItem {
|
|||||||
.extend(calls);
|
.extend(calls);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Marks a host-owned cell's recorded tool calls as complete.
|
/// Marks a host-owned direct invocation or Code Mode cell's call inventory as complete.
|
||||||
pub fn mark_tool_calls_complete(&mut self) {
|
pub fn mark_tool_calls_complete(&mut self) {
|
||||||
if let Some(metadata) = self.ensure_tool_call_metadata() {
|
if let Some(metadata) = self.ensure_tool_call_metadata() {
|
||||||
metadata.tool_calls_complete = Some(true);
|
metadata.tool_calls_complete = Some(true);
|
||||||
|
|||||||
Reference in new issue
Block a user