Track cumulative MCP attribution across requests and thread history (#47081)

## What changed

- Record each MCP source and its first turn, including connector and plugin identifiers when available. Cover direct and Code Mode calls, including returned errors and results that are not printed.
- Persist cumulative attribution checkpoints and restore them across compaction, resume, and forks, including forks limited to recent turns.
- Attach fresh attribution to Responses API `client_metadata` under `mcp_attribution`, outside model-visible content. Restrict transmission to allowed OpenAI HTTPS destinations for both HTTP and WebSocket requests.
- Report `attribution_error` for legacy history without checkpoints, malformed or conflicting attribution, and serialized attribution exceeding 16 KiB.

## Testing

Add coverage for source deduplication, checkpoint persistence and restoration, recent-turn forks, direct and Code Mode calls, destination filtering, and the serialized size limit.

GitOrigin-RevId: fd285e152ca9e24bfa000debb20013709d67a86e
This commit is contained in:
peilin-openai
2026-09-21 18:19:07 +00:00
committed by copyberry
parent 7cec3e110c
commit ac98537678
28 changed files with 1119 additions and 73 deletions
+35
View File
@@ -5939,6 +5939,41 @@ contentLength=0"
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn code_mode_records_mcp_completion_even_when_result_is_not_printed() -> Result<()> {
skip_if_no_network!(Ok(()));
let server = responses::start_mock_server().await;
let (test, second_mock) = run_code_mode_turn_with_rmcp(
&server,
"call the rmcp echo tool without printing its result",
r#"await tools.mcp__rmcp__echo({ message: "discarded" }); text("done");"#,
)
.await?;
let request = second_mock.single_request();
assert_eq!(
custom_tool_output_body_and_success(&request, "call-1").0,
"done"
);
let first_turn_id = request.body_json()["client_metadata"]["turn_id"].clone();
assert!(first_turn_id.is_string());
assert_eq!(
serde_json::to_value(codex_core::test_support::mcp_attribution_snapshot(
&test.codex
))?,
serde_json::json!({
"status": "complete",
"sources": [{
"server_name": "rmcp",
"tool_name": "echo",
"first_turn_id": first_turn_id,
}],
})
);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[test_case("node_repl"; "node_repl")]
#[test_case("cua_repl"; "cua_repl")]
@@ -1493,6 +1493,28 @@ async fn apps_default_writes_prompts_for_writes_but_not_reads() -> Result<()> {
recorded_apps_tool_call_by_call_id(&server, read_call_id).await;
recorded_apps_tool_call_by_call_id(&server, write_call_id).await;
let first_turn_id = responses.requests()[2].body_json()["client_metadata"]["turn_id"].clone();
assert!(first_turn_id.is_string());
assert_eq!(
serde_json::to_value(codex_core::test_support::mcp_attribution_snapshot(
&test.codex
))?,
json!({
"status": "complete",
"sources": [{
"connector_id": "calendar",
"server_name": "codex_apps",
"tool_name": "calendar_list_events",
"first_turn_id": first_turn_id,
}, {
"connector_id": "calendar",
"server_name": "codex_apps",
"tool_name": "calendar_create_event",
"first_turn_id": first_turn_id,
}],
})
);
test.codex.ensure_rollout_materialized().await;
test.codex.flush_rollout().await?;
let rollout_path = test.codex.rollout_path().expect("rollout path");
+18 -1
View File
@@ -670,7 +670,8 @@ async fn text_only_mcp_content_uses_content_items() -> anyhow::Result<()> {
})
.await;
let output = final_mock.single_request().function_call_output(call_id);
let request = final_mock.single_request();
let output = request.function_call_output(call_id);
let header = output["output"][0]["text"]
.as_str()
.expect("first content item should contain the wall-time header");
@@ -689,6 +690,22 @@ async fn text_only_mcp_content_uses_content_items() -> anyhow::Result<()> {
])
);
let first_turn_id = request.body_json()["client_metadata"]["turn_id"].clone();
assert!(first_turn_id.is_string());
assert_eq!(
serde_json::to_value(codex_core::test_support::mcp_attribution_snapshot(
&fixture.codex
))?,
json!({
"status": "complete",
"sources": [{
"server_name": "rmcp",
"tool_name": "image_scenario",
"first_turn_id": first_turn_id,
}],
})
);
server.verify().await;
Ok(())
}