mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Remove cumulative MCP attribution from requests and rollouts (#62589)
## What changed Stop collecting cumulative MCP tool attribution, sending it in Responses API request metadata, and writing it to rollout checkpoints. Use `model_search_state` for search provider history in new rollouts. When reading older rollouts, decode `mcp_attribution` only to determine whether Search MCP may have been used. Treat malformed or error attribution as possible Search MCP use so native search fallback stays disabled. ## Testing Add a history test that distinguishes legacy Search MCP use, an explicit record of no use, and missing attribution. GitOrigin-RevId: a145b5b6b7d2f423d989af10623ef21120f49d4d
This commit is contained in:
BIN
Binary file not shown.
@@ -527,7 +527,7 @@ fn tool_plugin_context_collects_app_and_mcp_sources() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_mcp_attribution_does_not_join_an_unrelated_local_summary() {
|
||||
fn selected_plugin_attribution_does_not_join_an_unrelated_local_summary() {
|
||||
let key = McpServerKey::in_environment(DEFAULT_MCP_SERVER_ENVIRONMENT_ID, "github");
|
||||
let mut config = test_mcp_config(PathBuf::new());
|
||||
let mut catalog = ResolvedMcpCatalog::builder();
|
||||
|
||||
@@ -29,12 +29,10 @@ use crate::context::MultiAgentRoleInstructions;
|
||||
use crate::context::SubagentNotification;
|
||||
use crate::context::world_state::IncrementalToolsUpdate;
|
||||
use crate::init_state_db;
|
||||
use crate::responses_metadata::CodexResponsesRequestKind;
|
||||
use crate::session::SessionSettingsUpdate;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::step_settings::StepSettingsUpdate;
|
||||
use crate::session::tests::update_selected_settings_for_test;
|
||||
use crate::thread_manager::ForkSnapshot;
|
||||
use crate::thread_manager::StartThreadOptions;
|
||||
use crate::tools::context::ToolCallSource;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
@@ -66,7 +64,6 @@ use codex_protocol::capabilities::SelectedCapabilityRoot;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::ReasoningSummary;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::error::AgentErrorContext;
|
||||
use codex_protocol::error::CodexErrKind;
|
||||
@@ -74,10 +71,6 @@ use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::items::UserMessageItem;
|
||||
use codex_protocol::mcp::ClientMcpExtensions;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionErrorReason;
|
||||
use codex_protocol::mcp::McpAttributionSource;
|
||||
use codex_protocol::mcp::McpAttributionStatus;
|
||||
use codex_protocol::mcp::OPENAI_FORM_EXTENSION_ID;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
@@ -491,56 +484,6 @@ async fn persist_thread_for_tree_resume(thread: &Arc<CodexThread>, message: &str
|
||||
.expect("test thread rollout should flush");
|
||||
}
|
||||
|
||||
/// Builds a request entirely in process; it does not install a transport or send anything.
|
||||
/// The destination filtering behavior is covered separately in `client_tests`.
|
||||
async fn mcp_attribution_in_constructed_request(thread: &CodexThread) -> McpAttribution {
|
||||
let turn_context = thread.session.new_default_turn().await;
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let prompt = crate::session::turn::build_prompt(
|
||||
thread
|
||||
.session
|
||||
.clone_history()
|
||||
.await
|
||||
.for_prompt(
|
||||
&step_context.settings.model_info.input_modalities,
|
||||
thread.session.services.image_store.as_ref(),
|
||||
)
|
||||
.await,
|
||||
&step_context,
|
||||
thread.session.get_prompt_base_instructions().await,
|
||||
thread
|
||||
.session
|
||||
.current_window_uses_incremental_tools(&step_context)
|
||||
.await,
|
||||
);
|
||||
let metadata = thread
|
||||
.session
|
||||
.responses_metadata(&step_context, CodexResponsesRequestKind::Turn)
|
||||
.await;
|
||||
let request = thread
|
||||
.session
|
||||
.services
|
||||
.model_client
|
||||
.build_responses_request(
|
||||
&prompt,
|
||||
&step_context.settings.model_info,
|
||||
/*effort*/ None,
|
||||
ReasoningSummary::None,
|
||||
/*service_tier*/ None,
|
||||
&metadata,
|
||||
/*include_internal*/ true,
|
||||
)
|
||||
.expect("construct request");
|
||||
serde_json::from_str(
|
||||
request
|
||||
.client_metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| metadata.get("mcp_attribution"))
|
||||
.expect("request MCP attribution"),
|
||||
)
|
||||
.expect("valid request MCP attribution")
|
||||
}
|
||||
|
||||
async fn wait_for_live_thread_spawn_children(
|
||||
control: &LocalAgentControl,
|
||||
parent_thread_id: ThreadId,
|
||||
@@ -4122,131 +4065,6 @@ async fn spawn_agent_full_fork_legacy_compaction_rebuilds_child_instructions_onc
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case::test_case(false; "complete")]
|
||||
#[test_case::test_case(true; "error_reason")]
|
||||
#[tokio::test]
|
||||
async fn resume_and_cold_fork_restore_mcp_attribution_in_constructed_requests(
|
||||
record_invalid_source: bool,
|
||||
) {
|
||||
let harness = AgentControlHarness::new().await;
|
||||
let source = harness
|
||||
.manager
|
||||
.start_thread(StartThreadOptions {
|
||||
history_mode: Some(ThreadHistoryMode::Legacy),
|
||||
..StartThreadOptions::new(harness.config.clone())
|
||||
})
|
||||
.await
|
||||
.expect("start legacy source");
|
||||
let source_thread_id = source.thread_id;
|
||||
let source_thread = source.thread;
|
||||
let turn_context = source_thread.session.new_default_turn().await;
|
||||
source_thread
|
||||
.session
|
||||
.record_conversation_items(
|
||||
turn_context.as_ref(),
|
||||
turn_context.model_info(),
|
||||
&[user_message("source turn")],
|
||||
)
|
||||
.await;
|
||||
let source = McpAttributionSource {
|
||||
connector_id: None,
|
||||
plugin_id: Some("messages@openai-bundled".to_string()),
|
||||
server_name: "messages".to_string(),
|
||||
tool_name: "search".to_string(),
|
||||
first_turn_id: turn_context.sub_id.clone(),
|
||||
};
|
||||
source_thread
|
||||
.session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.record_mcp_source(source.clone());
|
||||
if record_invalid_source {
|
||||
source_thread
|
||||
.session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.record_mcp_source(McpAttributionSource {
|
||||
tool_name: String::new(),
|
||||
..source.clone()
|
||||
});
|
||||
}
|
||||
source_thread
|
||||
.session
|
||||
.record_conversation_items(
|
||||
turn_context.as_ref(),
|
||||
turn_context.model_info(),
|
||||
&[assistant_message("source result", /*phase*/ None)],
|
||||
)
|
||||
.await;
|
||||
source_thread.ensure_rollout_materialized().await;
|
||||
source_thread
|
||||
.flush_rollout()
|
||||
.await
|
||||
.expect("flush source rollout");
|
||||
let rollout_path = source_thread.rollout_path().expect("source rollout path");
|
||||
source_thread
|
||||
.shutdown_and_wait()
|
||||
.await
|
||||
.expect("shutdown source thread");
|
||||
assert!(
|
||||
harness
|
||||
.manager
|
||||
.remove_thread(&source_thread_id)
|
||||
.await
|
||||
.is_some()
|
||||
);
|
||||
drop(source_thread);
|
||||
|
||||
let forked = harness
|
||||
.manager
|
||||
.fork_legacy_thread(
|
||||
ForkSnapshot::Interrupted,
|
||||
StartThreadOptions::new(harness.config.clone()),
|
||||
rollout_path.clone(),
|
||||
)
|
||||
.await
|
||||
.expect("fork unloaded source thread");
|
||||
let resumed = harness
|
||||
.manager
|
||||
.resume_legacy_thread_from_rollout(
|
||||
harness.config.clone(),
|
||||
rollout_path,
|
||||
AuthManager::from_auth_for_testing(CodexAuth::from_api_key("dummy")),
|
||||
/*parent_trace*/ None,
|
||||
ClientMcpExtensions::default(),
|
||||
)
|
||||
.await
|
||||
.expect("resume source thread");
|
||||
let expected = McpAttribution {
|
||||
status: if record_invalid_source {
|
||||
McpAttributionStatus::AttributionError
|
||||
} else {
|
||||
McpAttributionStatus::Complete
|
||||
},
|
||||
error_reason: record_invalid_source.then_some(McpAttributionErrorReason::SourceInvalid),
|
||||
sources: vec![source],
|
||||
};
|
||||
assert_eq!(
|
||||
mcp_attribution_in_constructed_request(&forked.thread).await,
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
mcp_attribution_in_constructed_request(&resumed.thread).await,
|
||||
expected
|
||||
);
|
||||
|
||||
forked
|
||||
.thread
|
||||
.shutdown_and_wait()
|
||||
.await
|
||||
.expect("shutdown forked thread");
|
||||
resumed
|
||||
.thread
|
||||
.shutdown_and_wait()
|
||||
.await
|
||||
.expect("shutdown resumed thread");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn spawn_agent_fork_flushes_parent_rollout_before_loading_history() {
|
||||
let harness = AgentControlHarness::new().await;
|
||||
|
||||
@@ -17,8 +17,6 @@ use crate::AttestationContext;
|
||||
use crate::AttestationProvider;
|
||||
use crate::GenerateAttestationFuture;
|
||||
use crate::responses_metadata::CodexResponsesMetadata;
|
||||
use crate::responses_metadata::MAX_MCP_ATTRIBUTION_BYTES;
|
||||
use crate::responses_metadata::MCP_ATTRIBUTION_CLIENT_METADATA_KEY;
|
||||
use crate::test_support::TestCodexResponsesRequestKind;
|
||||
use crate::test_support::responses_metadata as test_responses_metadata;
|
||||
use base64::Engine;
|
||||
@@ -51,9 +49,6 @@ use codex_protocol::ThreadId;
|
||||
use codex_protocol::auth::AuthMode;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionSource;
|
||||
use codex_protocol::mcp::McpAttributionStatus;
|
||||
use codex_protocol::models::BaseInstructions;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ExecutedToolCall;
|
||||
@@ -645,29 +640,17 @@ async fn responses_request_includes_internal_metadata_for_provider_grant_or_firs
|
||||
"private": { "resource": "raw-result-metadata" },
|
||||
})));
|
||||
let without_raw_metadata = output_with_tool_result_metadata(ToolResultMetadata::default());
|
||||
let attribution = McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![McpAttributionSource {
|
||||
connector_id: Some("connector_example".to_string()),
|
||||
plugin_id: Some("example@openai-bundled".to_string()),
|
||||
server_name: "codex_apps".to_string(),
|
||||
tool_name: "search".to_string(),
|
||||
first_turn_id: "turn_123".to_string(),
|
||||
}],
|
||||
};
|
||||
let prompt = Prompt {
|
||||
input: vec![output.clone(), omitted_output.clone()],
|
||||
..Default::default()
|
||||
};
|
||||
let mut responses_metadata = test_responses_metadata_for_client(
|
||||
let responses_metadata = test_responses_metadata_for_client(
|
||||
&client,
|
||||
/*turn_id*/ None,
|
||||
format!("{}:0", client.state.thread_id),
|
||||
/*parent_thread_id*/ None,
|
||||
TestCodexResponsesRequestKind::Turn,
|
||||
);
|
||||
responses_metadata.mcp_attribution = Some(attribution.clone());
|
||||
for (provider_grant, base_url, allowed) in [
|
||||
(false, "https://api.openai.com/v1", true),
|
||||
(false, "https://chatgpt.com/backend-api/codex", true),
|
||||
@@ -718,15 +701,6 @@ async fn responses_request_includes_internal_metadata_for_provider_grant_or_firs
|
||||
serde_json::to_value(&request)?["input"],
|
||||
expected_wire_input
|
||||
);
|
||||
let expected_json = allowed
|
||||
.then(|| serde_json::to_string(&attribution).map(serde_json::Value::String))
|
||||
.transpose()?;
|
||||
assert_eq!(
|
||||
serde_json::to_value(&request)?
|
||||
.get("client_metadata")
|
||||
.and_then(|metadata| metadata.get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY)),
|
||||
expected_json.as_ref(),
|
||||
);
|
||||
let ws_client_metadata = client.build_ws_client_metadata(
|
||||
&responses_metadata,
|
||||
include_internal,
|
||||
@@ -738,70 +712,9 @@ async fn responses_request_includes_internal_metadata_for_provider_grant_or_firs
|
||||
};
|
||||
let ws_request = serde_json::to_value(ws_request)?;
|
||||
assert_eq!(ws_request["input"], expected_wire_input);
|
||||
assert_eq!(
|
||||
ws_request
|
||||
.get("client_metadata")
|
||||
.and_then(|metadata| metadata.get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY)),
|
||||
expected_json.as_ref(),
|
||||
);
|
||||
assert!(
|
||||
serde_json::to_value(&request)?
|
||||
.get("mcp_attribution")
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(prompt.input, vec![output.clone(), omitted_output.clone()]);
|
||||
}
|
||||
}
|
||||
responses_metadata.mcp_attribution = Some(McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(
|
||||
codex_protocol::mcp::McpAttributionErrorReason::HistoryMissingCheckpoint,
|
||||
),
|
||||
sources: attribution.sources.clone(),
|
||||
});
|
||||
let request = client.build_responses_request(
|
||||
&prompt,
|
||||
&test_model_info(),
|
||||
/*effort*/ None,
|
||||
codex_protocol::config_types::ReasoningSummary::None,
|
||||
/*service_tier*/ None,
|
||||
&responses_metadata,
|
||||
/*include_internal*/ true,
|
||||
)?;
|
||||
assert_eq!(
|
||||
request
|
||||
.client_metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| metadata.get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY))
|
||||
.map(|value| serde_json::from_str::<serde_json::Value>(value))
|
||||
.transpose()?,
|
||||
Some(json!({
|
||||
"status": "attribution_error",
|
||||
"sources": attribution.sources,
|
||||
"error_reason": "history_missing_checkpoint",
|
||||
})),
|
||||
);
|
||||
|
||||
let mut oversized_attribution = attribution;
|
||||
oversized_attribution.sources[0].tool_name = "a".repeat(MAX_MCP_ATTRIBUTION_BYTES);
|
||||
responses_metadata.mcp_attribution = Some(oversized_attribution);
|
||||
let request = client.build_responses_request(
|
||||
&prompt,
|
||||
&test_model_info(),
|
||||
/*effort*/ None,
|
||||
codex_protocol::config_types::ReasoningSummary::None,
|
||||
/*service_tier*/ None,
|
||||
&responses_metadata,
|
||||
/*include_internal*/ true,
|
||||
)?;
|
||||
assert_eq!(
|
||||
request.client_metadata.as_ref().and_then(|metadata| {
|
||||
metadata
|
||||
.get(MCP_ATTRIBUTION_CLIENT_METADATA_KEY)
|
||||
.map(String::as_str)
|
||||
}),
|
||||
Some(r#"{"status":"attribution_error","error_reason":"payload_too_large"}"#),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -819,21 +819,6 @@ async fn handle_approved_mcp_tool_call(
|
||||
if let Some(elicitation_type) = elicitation_type {
|
||||
track_mcp_tool_call_elicitation(sess, turn_context, call_id, elicitation_type);
|
||||
}
|
||||
// Direct and Code Mode calls share this boundary. Once an approved call enters
|
||||
// it, conservatively attribute returned errors too: they may contain peer data.
|
||||
let source_connector_id = prepared_call
|
||||
.is_host_owned_apps()
|
||||
.then(|| prepared_call.tool_info().connector_id.clone())
|
||||
.flatten();
|
||||
sess.services.executed_tool_calls.record_mcp_source(
|
||||
codex_protocol::mcp::McpAttributionSource {
|
||||
connector_id: source_connector_id,
|
||||
plugin_id: prepared_call.plugin_id().map(str::to_string),
|
||||
server_name: prepared_call.server_name().to_string(),
|
||||
tool_name: prepared_call.tool_info().tool.name.to_string(),
|
||||
first_turn_id: turn_context.sub_id.clone(),
|
||||
},
|
||||
);
|
||||
notify_mcp_tool_call_completed(
|
||||
sess,
|
||||
turn_context,
|
||||
|
||||
@@ -163,15 +163,11 @@ impl SearchOwner {
|
||||
Some(ModelSearchState::Unused) | Some(ModelSearchState::Standalone) | None => {}
|
||||
}
|
||||
legacy_native |= metadata.native_search_fallback_used;
|
||||
if let Some(attribution) = metadata.mcp_attribution.as_ref() {
|
||||
legacy_search_mcp |= attribution.status
|
||||
== codex_protocol::mcp::McpAttributionStatus::AttributionError
|
||||
|| attribution.sources.iter().any(|source| {
|
||||
source.connector_id.as_deref() == Some(codex_mcp::SEARCH_CONNECTOR)
|
||||
});
|
||||
if let Some(search_mcp_used) = metadata.legacy_search_mcp_used {
|
||||
legacy_search_mcp |= search_mcp_used;
|
||||
}
|
||||
has_checkpoint |=
|
||||
metadata.model_search_state.is_some() || metadata.mcp_attribution.is_some();
|
||||
metadata.model_search_state.is_some() || metadata.legacy_search_mcp_used.is_some();
|
||||
};
|
||||
for item in history.get_rollout_items() {
|
||||
match item {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use codex_protocol::RequestCountry;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use codex_analytics::CompactionImplementation;
|
||||
use codex_analytics::CompactionPhase;
|
||||
@@ -11,12 +10,10 @@ use codex_analytics::CompactionTrigger;
|
||||
use codex_git_utils::SanitizedGitUrl;
|
||||
use codex_protocol::ContextWindowId;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::protocol::ThreadSource;
|
||||
use codex_utils_string::to_ascii_json_string;
|
||||
use codex_utils_string::to_json_string_bounded;
|
||||
use http::HeaderMap as ApiHeaderMap;
|
||||
use http::HeaderValue;
|
||||
use serde::Serialize;
|
||||
@@ -46,8 +43,6 @@ pub(crate) const TURN_STARTED_AT_UNIX_MS_KEY: &str = "turn_started_at_unix_ms";
|
||||
pub(crate) const HISTORY_INGEST_REQUESTED_KEY: &str = "history_ingest_requested";
|
||||
pub(crate) const ANALYTICS_ENABLED_KEY: &str = "analytics_enabled";
|
||||
pub(crate) const HISTORY_INITIALIZATION_KEY: &str = "history_initialization";
|
||||
pub(crate) const MCP_ATTRIBUTION_CLIENT_METADATA_KEY: &str = "mcp_attribution";
|
||||
pub(crate) const MAX_MCP_ATTRIBUTION_BYTES: usize = 16 * 1024;
|
||||
// ResponsesAPI rejects a whole client_metadata value above this character limit.
|
||||
// Turn metadata is ASCII JSON, so its byte and character lengths are identical.
|
||||
const MAX_TURN_METADATA_BYTES: usize = 8096;
|
||||
@@ -72,7 +67,6 @@ pub(crate) const WORKSPACES_KEY: &str = "workspaces";
|
||||
const RESERVED_METADATA_KEYS: &[&str] = &[
|
||||
"guardian_credits_requested",
|
||||
"guardian_recorded_items",
|
||||
MCP_ATTRIBUTION_CLIENT_METADATA_KEY,
|
||||
INSTALLATION_ID_KEY,
|
||||
X_CODEX_INSTALLATION_ID_HEADER,
|
||||
SESSION_ID_KEY,
|
||||
@@ -278,8 +272,6 @@ pub struct CodexResponsesMetadata {
|
||||
/// Selected session analytics client's collection state, independent of event eligibility or delivery.
|
||||
/// Absent when the request has no initialized session analytics context.
|
||||
pub(crate) analytics_enabled: Option<bool>,
|
||||
/// Cumulative MCP attribution for this logical request; body-only and model-invisible.
|
||||
pub(crate) mcp_attribution: Option<McpAttribution>,
|
||||
pub(crate) extra: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
@@ -350,7 +342,6 @@ impl CodexResponsesMetadata {
|
||||
turn_started_at_unix_ms: None,
|
||||
history_ingest_requested: None,
|
||||
analytics_enabled: None,
|
||||
mcp_attribution: None,
|
||||
extra: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -403,19 +394,6 @@ impl CodexResponsesMetadata {
|
||||
{
|
||||
client_metadata.insert(X_CODEX_TURN_METADATA_HEADER.to_string(), turn_metadata_json);
|
||||
}
|
||||
if include_internal && let Some(attribution) = &self.mcp_attribution {
|
||||
let serialized = to_json_string_bounded(attribution, MAX_MCP_ATTRIBUTION_BYTES)
|
||||
.unwrap_or_else(|error| {
|
||||
if error.io_error_kind() == Some(ErrorKind::WriteZero) {
|
||||
r#"{"status":"attribution_error","error_reason":"payload_too_large"}"#
|
||||
.to_string()
|
||||
} else {
|
||||
r#"{"status":"attribution_error","error_reason":"serialization_failed"}"#
|
||||
.to_string()
|
||||
}
|
||||
});
|
||||
client_metadata.insert(MCP_ATTRIBUTION_CLIENT_METADATA_KEY.to_string(), serialized);
|
||||
}
|
||||
client_metadata
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +37,6 @@ async fn harness_authored_configuration_updates_preserve_metadata_and_resume() {
|
||||
metadata.history_sequence = Some(1);
|
||||
metadata.sampled_through_sequence = Some(0);
|
||||
metadata.model_search_state = Some(codex_history::ModelSearchState::Unused);
|
||||
metadata.mcp_attribution = Some(
|
||||
session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.mcp_attribution_snapshot(),
|
||||
);
|
||||
|
||||
let recorded = session.clone_history().await.into_annotated_items();
|
||||
assert_eq!(recorded, vec![expected.clone()]);
|
||||
|
||||
@@ -181,7 +181,6 @@ use codex_protocol::request_permissions::RequestPermissionsResponse;
|
||||
use codex_protocol::request_user_input::RequestUserInputArgs;
|
||||
use codex_protocol::request_user_input::RequestUserInputResponse;
|
||||
use codex_rmcp_client::ElicitationResponse;
|
||||
use codex_rollout::should_persist_response_item;
|
||||
use codex_rollout::state_db;
|
||||
use codex_rollout_trace::CompactionTraceContext;
|
||||
use codex_rollout_trace::ThreadStartedTraceMetadata;
|
||||
@@ -4153,40 +4152,13 @@ impl Session {
|
||||
.get_or_insert_with(|| with_serialization_allowance(policy).token_budget());
|
||||
}
|
||||
}
|
||||
// Last-N-turn forks retain a suffix starting at a user turn boundary. Repeat the
|
||||
// cumulative checkpoint there so the suffix remains self-contained even when the fork
|
||||
// happens mid-turn; dirty checkpoints preserve new sources between boundaries.
|
||||
let force_mcp_checkpoint = items
|
||||
let has_user_turn_boundary = items
|
||||
.iter()
|
||||
.any(|envelope| crate::context_manager::is_user_turn_boundary(&envelope.item));
|
||||
let cached_retry = matches!(&realtime, Some(RealtimeRecord::Handoff(input)) if input.cached_envelope.is_some());
|
||||
let mcp_revision = (!cached_retry)
|
||||
.then(|| {
|
||||
self.services
|
||||
.executed_tool_calls
|
||||
.mcp_attribution_checkpoint(force_mcp_checkpoint)
|
||||
})
|
||||
.flatten()
|
||||
.and_then(|(attribution, revision)| {
|
||||
let first_persisted = items
|
||||
.iter()
|
||||
.position(|envelope| should_persist_response_item(&envelope.item))?;
|
||||
for (index, envelope) in items.iter_mut().enumerate() {
|
||||
// Rollout batches may be partially written. Checkpoint the first persisted
|
||||
// item, and repeat the checkpoint at turn boundaries retained by forks.
|
||||
if index == first_persisted
|
||||
|| crate::context_manager::is_user_turn_boundary(&envelope.item)
|
||||
{
|
||||
envelope.metadata.get_or_insert_default().mcp_attribution =
|
||||
Some(attribution.clone());
|
||||
}
|
||||
}
|
||||
Some(revision)
|
||||
});
|
||||
// Thread settings precede the first accepted input. Keep the persistence lock
|
||||
// through its seal, and clear the pending flag only after the seal succeeds.
|
||||
let realtime_settings_guard = if realtime.is_some()
|
||||
&& force_mcp_checkpoint
|
||||
&& has_user_turn_boundary
|
||||
&& self.reference_context_item().await.is_none()
|
||||
{
|
||||
Some(thread_settings::acquire_persistence_lock(self).await)
|
||||
@@ -4302,14 +4274,11 @@ impl Session {
|
||||
metadata: image,
|
||||
});
|
||||
}
|
||||
let persisted = if let Some(RealtimeRecord::Handoff(prepared)) = &realtime {
|
||||
if let Some(RealtimeRecord::Handoff(prepared)) = &realtime {
|
||||
self.finish_realtime_handoff(prepared, &items[0]).await;
|
||||
true
|
||||
} else if matches!(&realtime, Some(RealtimeRecord::Chunk)) {
|
||||
true
|
||||
} else {
|
||||
} else if !matches!(&realtime, Some(RealtimeRecord::Chunk)) {
|
||||
let settings_guard =
|
||||
if force_mcp_checkpoint && self.reference_context_item().await.is_none() {
|
||||
if has_user_turn_boundary && self.reference_context_item().await.is_none() {
|
||||
Some(thread_settings::acquire_persistence_lock(self).await)
|
||||
} else {
|
||||
None
|
||||
@@ -4327,14 +4296,10 @@ impl Session {
|
||||
.map(RolloutItem::EventMsg)
|
||||
.chain(items.into_iter().map(RolloutItem::ResponseItem))
|
||||
.collect::<Vec<_>>();
|
||||
self.persist_rollout_items(&rollout_items).await
|
||||
};
|
||||
drop(realtime_settings_guard);
|
||||
if persisted && let Some(revision) = mcp_revision {
|
||||
self.services
|
||||
.executed_tool_calls
|
||||
.mark_mcp_attribution_persisted(revision);
|
||||
self.persist_rollout_items(&rollout_items).await;
|
||||
drop(settings_guard);
|
||||
}
|
||||
drop(realtime_settings_guard);
|
||||
if turn_context.config.memories.disable_on_external_context
|
||||
&& let Some(item) = response_items
|
||||
.iter()
|
||||
@@ -5001,16 +4966,6 @@ impl Session {
|
||||
&self.services.thread_extension_data,
|
||||
&mut items,
|
||||
);
|
||||
let mcp_revision = self
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.mcp_attribution_checkpoint(/*force*/ true)
|
||||
.and_then(|(attribution, revision)| {
|
||||
items.last_mut().map(|envelope| {
|
||||
envelope.metadata.get_or_insert_default().mcp_attribution = Some(attribution);
|
||||
revision
|
||||
})
|
||||
});
|
||||
if let Some(checkpoint) = items.iter_mut().rev().find(|envelope| {
|
||||
matches!(
|
||||
envelope.item,
|
||||
@@ -5152,13 +5107,7 @@ impl Session {
|
||||
// The frozen turn context must not override current settings in persisted metadata.
|
||||
RolloutItem::EventMsg(thread_settings::applied_event(self).await),
|
||||
];
|
||||
if self.persist_rollout_items(&rollout_items).await
|
||||
&& let Some(revision) = mcp_revision
|
||||
{
|
||||
self.services
|
||||
.executed_tool_calls
|
||||
.mark_mcp_attribution_persisted(revision);
|
||||
}
|
||||
self.persist_rollout_items(&rollout_items).await;
|
||||
{
|
||||
let mut state = self.state.lock().await;
|
||||
state.queue_pending_session_start_source(codex_hooks::SessionStartSource::Compact);
|
||||
|
||||
@@ -778,7 +778,6 @@ impl Session {
|
||||
history_initialization: Some(self.history_initialization),
|
||||
window_number: Some(window_number),
|
||||
context_window_id: Some(context_window_id),
|
||||
mcp_attribution: Some(self.services.executed_tool_calls.mcp_attribution_snapshot()),
|
||||
analytics_enabled: Some(self.services.analytics_events_client.is_enabled()),
|
||||
history_ingest_requested: turn_context
|
||||
.config
|
||||
|
||||
@@ -51,10 +51,8 @@ use crate::shell_snapshot::ShellSnapshot;
|
||||
use crate::test_support::models_manager_with_provider;
|
||||
use crate::tools::format_exec_output_str;
|
||||
use crate::tools::registry::ToolRegistry;
|
||||
use codex_analytics::CompactionImplementation;
|
||||
use codex_analytics::CompactionPhase;
|
||||
use codex_analytics::CompactionReason;
|
||||
use codex_analytics::CompactionTrigger;
|
||||
use codex_config::ConfigLayerStack;
|
||||
use codex_config::DEFAULT_MCP_SERVER_ENVIRONMENT_ID;
|
||||
use codex_config::LoaderOverrides;
|
||||
@@ -132,7 +130,6 @@ use tracing::Span;
|
||||
|
||||
use crate::connectors::AppInfo;
|
||||
use crate::responses_metadata::CodexResponsesRequestKind;
|
||||
use crate::responses_metadata::CompactionTurnMetadata;
|
||||
use crate::rollout::recorder::RolloutRecorder;
|
||||
use crate::session::Submission;
|
||||
use crate::state::ActiveTurn;
|
||||
@@ -177,9 +174,6 @@ use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::items::HookPromptFragment;
|
||||
use codex_protocol::items::build_hook_prompt_message;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionSource;
|
||||
use codex_protocol::mcp::McpAttributionStatus;
|
||||
use codex_protocol::models::BaseInstructions;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
@@ -2813,12 +2807,6 @@ async fn annotated_history_uses_explicit_model_without_a_step(
|
||||
session
|
||||
.record_annotated_conversation_items(&turn_context, &model_info, expected.clone())
|
||||
.await;
|
||||
expected[0].metadata.get_or_insert_default().mcp_attribution = Some(
|
||||
session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.mcp_attribution_snapshot(),
|
||||
);
|
||||
for (index, envelope) in expected.iter_mut().enumerate() {
|
||||
let metadata = envelope.metadata.get_or_insert_default();
|
||||
metadata.history_sequence = Some(index as u64 + 1);
|
||||
@@ -6029,105 +6017,6 @@ async fn settings_checkpoint_waits_for_accepted_settings_persistence() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mcp_attribution_checkpoints_cover_batch_prefixes_compaction_and_restore() {
|
||||
let (mut session, turn_context) = make_session_and_context().await;
|
||||
session.services.executed_tool_calls =
|
||||
crate::state::ExecutedToolCalls::new(&turn_context.config.features, &InitialHistory::New);
|
||||
let rollout_path = attach_thread_persistence(&mut session).await;
|
||||
let source = McpAttributionSource {
|
||||
connector_id: None,
|
||||
plugin_id: None,
|
||||
server_name: "example".to_string(),
|
||||
tool_name: "search".to_string(),
|
||||
first_turn_id: "turn_1".to_string(),
|
||||
};
|
||||
session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.record_mcp_source(source.clone());
|
||||
let expected = McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source],
|
||||
};
|
||||
session
|
||||
.record_annotated_conversation_items(
|
||||
&turn_context,
|
||||
turn_context.model_info(),
|
||||
vec![
|
||||
ResponseItemEnvelope::new(ResponseItem::FunctionCallOutput {
|
||||
id: None,
|
||||
call_id: Some("call_1".to_string()),
|
||||
name: None,
|
||||
namespace: None,
|
||||
output: FunctionCallOutputPayload::from_text("result".to_string()),
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}),
|
||||
ResponseItemEnvelope::new(user_message("next turn")),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
let checkpoints = session
|
||||
.clone_history()
|
||||
.await
|
||||
.annotated_items()
|
||||
.iter()
|
||||
.map(|envelope| {
|
||||
envelope
|
||||
.metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| metadata.mcp_attribution.clone())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
checkpoints,
|
||||
vec![Some(expected.clone()), Some(expected.clone())]
|
||||
);
|
||||
|
||||
let (window_number, window_ids) = session.advance_auto_compact_window().await;
|
||||
session
|
||||
.replace_compacted_history(
|
||||
vec![ResponseItemEnvelope::new(user_message("compacted history"))],
|
||||
turn_context.to_turn_context_item(),
|
||||
WorldStateSnapshot::default(),
|
||||
/*rejected_audio_file_ids*/ Default::default(),
|
||||
CompactedHistoryMetadata {
|
||||
input_goal_ids: Default::default(),
|
||||
message: "summary".to_string(),
|
||||
window_number,
|
||||
window_ids,
|
||||
compaction_response_id: None,
|
||||
compaction_model_hash: None,
|
||||
reviewer_compaction_hash: None,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
session
|
||||
.flush_rollout()
|
||||
.await
|
||||
.expect("flush compacted history");
|
||||
|
||||
let (items, _, _) = RolloutRecorder::load_rollout_items(&rollout_path)
|
||||
.await
|
||||
.expect("read compacted history");
|
||||
let checkpoint = items.iter().rev().find_map(|item| match item {
|
||||
RolloutItem::Compacted(compacted) => compacted
|
||||
.replacement_history
|
||||
.as_ref()?
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|envelope| envelope.metadata.as_ref()?.mcp_attribution.as_ref()),
|
||||
_ => None,
|
||||
});
|
||||
assert_eq!(checkpoint, Some(&expected));
|
||||
let restored = crate::state::ExecutedToolCalls::new(
|
||||
&turn_context.config.features,
|
||||
&InitialHistory::Forked(items),
|
||||
);
|
||||
assert_eq!(restored.mcp_attribution_snapshot(), expected);
|
||||
}
|
||||
|
||||
#[test_case(None; "idle")]
|
||||
#[test_case(Some(ActiveTurn::default()); "reserved turn")]
|
||||
#[test_case(Some(ActiveTurn {
|
||||
@@ -6758,51 +6647,6 @@ pub(crate) async fn build_world_state_from_turn_context(
|
||||
.expect("world state should build")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn response_metadata_builders_capture_fresh_mcp_attribution() {
|
||||
let (mut session, turn_context) = make_session_and_context().await;
|
||||
session.services.executed_tool_calls =
|
||||
crate::state::ExecutedToolCalls::new(&turn_context.config.features, &InitialHistory::New);
|
||||
let turn_context = Arc::new(turn_context);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let before = session
|
||||
.responses_metadata(&step_context, CodexResponsesRequestKind::Turn)
|
||||
.await;
|
||||
let source = McpAttributionSource {
|
||||
connector_id: None,
|
||||
plugin_id: None,
|
||||
server_name: "example".to_string(),
|
||||
tool_name: "search".to_string(),
|
||||
first_turn_id: turn_context.sub_id.clone(),
|
||||
};
|
||||
session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.record_mcp_source(source.clone());
|
||||
let after = session
|
||||
.responses_metadata(&step_context, CodexResponsesRequestKind::Turn)
|
||||
.await;
|
||||
let compaction = session
|
||||
.compaction_responses_metadata(
|
||||
&step_context,
|
||||
CompactionTurnMetadata::new(
|
||||
CompactionTrigger::Auto,
|
||||
CompactionReason::ContextLimit,
|
||||
CompactionImplementation::Responses,
|
||||
CompactionPhase::MidTurn,
|
||||
),
|
||||
)
|
||||
.await;
|
||||
let expected = Some(McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source],
|
||||
});
|
||||
assert_eq!(before.mcp_attribution, Some(McpAttribution::default()));
|
||||
assert_eq!(after.mcp_attribution, expected);
|
||||
assert_eq!(compaction.mcp_attribution, expected);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_metadata_uses_selected_harness_analytics_client() {
|
||||
for enabled in [true, false] {
|
||||
|
||||
@@ -109,18 +109,6 @@ pub async fn history_with_tool_call_metadata(
|
||||
items
|
||||
}
|
||||
|
||||
/// Returns the recorder state used by the next session metadata snapshot.
|
||||
/// This does not change destination filtering or issue a request.
|
||||
pub fn mcp_attribution_snapshot(
|
||||
thread: &crate::CodexThread,
|
||||
) -> codex_protocol::mcp::McpAttribution {
|
||||
thread
|
||||
.session
|
||||
.services
|
||||
.executed_tool_calls
|
||||
.mcp_attribution_snapshot()
|
||||
}
|
||||
|
||||
/// Retains the active turn's timing after completion without exporting telemetry.
|
||||
/// Returns `None` when there is no running task. The observer reports exclusive
|
||||
/// durations for the caller's interval in inference, automatic review, local
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#[cfg(test)]
|
||||
#[path = "executed_tool_calls_direct_tests.rs"]
|
||||
mod direct_tests;
|
||||
mod mcp_attribution;
|
||||
mod request_metadata;
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -26,8 +25,6 @@ use codex_model_provider::provider_uses_first_party_auth_path;
|
||||
use codex_protocol::ResponseItemId;
|
||||
use codex_protocol::auth::AuthMode;
|
||||
use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionSource;
|
||||
use codex_protocol::models::ExecutedToolCall;
|
||||
use codex_protocol::models::ExecutedToolCallArguments;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
@@ -72,7 +69,6 @@ pub(crate) struct ExecutedToolCalls {
|
||||
captures_result_bodies: bool,
|
||||
retained_direct_metadata_bytes: Arc<AtomicUsize>,
|
||||
pending_direct_calls: Arc<AtomicUsize>,
|
||||
mcp_attribution: mcp_attribution::McpAttributionRecorder,
|
||||
}
|
||||
|
||||
/// Private bytes belong to one regular task. History keeps only the host-issued IDs.
|
||||
@@ -453,7 +449,6 @@ impl ExecutedToolCalls {
|
||||
}))),
|
||||
retained_direct_metadata_bytes: Arc::new(AtomicUsize::new(0)),
|
||||
pending_direct_calls: Arc::new(AtomicUsize::new(0)),
|
||||
mcp_attribution: mcp_attribution::McpAttributionRecorder::new(history),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,23 +556,6 @@ impl ExecutedToolCalls {
|
||||
.filter(|capture| call.permits_result_body(capture.auth_mode))?
|
||||
.start_capture(nested_call_id)
|
||||
}
|
||||
|
||||
pub(crate) fn record_mcp_source(&self, source: McpAttributionSource) {
|
||||
self.mcp_attribution.record(source);
|
||||
}
|
||||
|
||||
pub(crate) fn mcp_attribution_snapshot(&self) -> McpAttribution {
|
||||
self.mcp_attribution.snapshot()
|
||||
}
|
||||
|
||||
pub(crate) fn mcp_attribution_checkpoint(&self, force: bool) -> Option<(McpAttribution, u64)> {
|
||||
self.mcp_attribution.checkpoint(force)
|
||||
}
|
||||
|
||||
pub(crate) fn mark_mcp_attribution_persisted(&self, revision: u64) {
|
||||
self.mcp_attribution.mark_persisted(revision);
|
||||
}
|
||||
|
||||
/// Consult the inventory attached to this exact exec output during actor sampling.
|
||||
/// Pending cells can still produce other outputs and have no per-output inventory yet.
|
||||
pub(crate) fn exec_output_matches(
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
//! Cumulative MCP result attribution, independent of the best-effort call recorder.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::MutexGuard;
|
||||
|
||||
use codex_history::InitialHistory;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionErrorReason;
|
||||
use codex_protocol::mcp::McpAttributionSource;
|
||||
use codex_protocol::mcp::McpAttributionStatus;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct McpAttributionRecorder(Arc<Mutex<State>>);
|
||||
|
||||
struct State {
|
||||
attribution: McpAttribution,
|
||||
revision: u64,
|
||||
persisted_revision: u64,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
struct SourceIdentity<'a> {
|
||||
connector_id: Option<&'a str>,
|
||||
plugin_id: Option<&'a str>,
|
||||
server_name: &'a str,
|
||||
tool_name: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a McpAttributionSource> for SourceIdentity<'a> {
|
||||
fn from(source: &'a McpAttributionSource) -> Self {
|
||||
Self {
|
||||
connector_id: source.connector_id.as_deref(),
|
||||
plugin_id: source.plugin_id.as_deref(),
|
||||
server_name: &source.server_name,
|
||||
tool_name: &source.tool_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for McpAttributionRecorder {
|
||||
fn default() -> Self {
|
||||
Self::new(&InitialHistory::New)
|
||||
}
|
||||
}
|
||||
|
||||
impl McpAttributionRecorder {
|
||||
pub(super) fn new(history: &InitialHistory) -> Self {
|
||||
let mut state = State {
|
||||
attribution: McpAttribution::default(),
|
||||
// Persist an initial checkpoint even when no MCP result has been recorded.
|
||||
revision: 1,
|
||||
persisted_revision: 0,
|
||||
};
|
||||
let mut found_checkpoint = matches!(history, InitialHistory::New | InitialHistory::Cleared);
|
||||
for item in history.get_rollout_items() {
|
||||
match item {
|
||||
RolloutItem::ResponseItem(envelope) => {
|
||||
if let Some(metadata) = envelope.metadata.as_ref()
|
||||
&& let Some(checkpoint) = metadata.mcp_attribution.as_ref()
|
||||
{
|
||||
found_checkpoint = true;
|
||||
state.merge_checkpoint(checkpoint);
|
||||
}
|
||||
}
|
||||
RolloutItem::Compacted(compacted) => {
|
||||
for metadata in compacted
|
||||
.replacement_history
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|envelope| envelope.metadata.as_ref())
|
||||
{
|
||||
if let Some(checkpoint) = metadata.mcp_attribution.as_ref() {
|
||||
found_checkpoint = true;
|
||||
state.merge_checkpoint(checkpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !found_checkpoint {
|
||||
// Pre-attribution history cannot establish that earlier context was MCP-free.
|
||||
state.mark_error(McpAttributionErrorReason::HistoryMissingCheckpoint);
|
||||
}
|
||||
Self(Arc::new(Mutex::new(state)))
|
||||
}
|
||||
|
||||
fn lock(&self) -> MutexGuard<'_, State> {
|
||||
self.0.lock().unwrap_or_else(|poisoned| {
|
||||
let mut state = poisoned.into_inner();
|
||||
state.mark_error(McpAttributionErrorReason::RecorderPoisoned);
|
||||
state
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn record(&self, source: McpAttributionSource) {
|
||||
self.lock().record(source, /*restoring*/ false);
|
||||
}
|
||||
|
||||
pub(super) fn snapshot(&self) -> McpAttribution {
|
||||
self.lock().attribution.clone()
|
||||
}
|
||||
|
||||
pub(super) fn checkpoint(&self, force: bool) -> Option<(McpAttribution, u64)> {
|
||||
let state = self.lock();
|
||||
(force || state.revision != state.persisted_revision)
|
||||
.then(|| (state.attribution.clone(), state.revision))
|
||||
}
|
||||
|
||||
pub(super) fn mark_persisted(&self, revision: u64) {
|
||||
let mut state = self.lock();
|
||||
state.persisted_revision = state.persisted_revision.max(revision.min(state.revision));
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn mark_error(&mut self, reason: McpAttributionErrorReason) {
|
||||
if self.attribution.status != McpAttributionStatus::AttributionError {
|
||||
self.attribution.status = McpAttributionStatus::AttributionError;
|
||||
self.attribution.error_reason = Some(reason);
|
||||
self.revision += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_checkpoint(&mut self, checkpoint: &McpAttribution) {
|
||||
if checkpoint.status == McpAttributionStatus::AttributionError {
|
||||
self.mark_error(
|
||||
checkpoint
|
||||
.error_reason
|
||||
.unwrap_or(McpAttributionErrorReason::RestoredErrorUnknown),
|
||||
);
|
||||
} else if (checkpoint.status == McpAttributionStatus::None
|
||||
&& !checkpoint.sources.is_empty())
|
||||
|| (checkpoint.status == McpAttributionStatus::Complete
|
||||
&& checkpoint.sources.is_empty())
|
||||
{
|
||||
self.mark_error(McpAttributionErrorReason::CheckpointInvalid);
|
||||
}
|
||||
for source in &checkpoint.sources {
|
||||
self.record(source.clone(), /*restoring*/ true);
|
||||
}
|
||||
}
|
||||
|
||||
fn record(&mut self, source: McpAttributionSource, restoring: bool) {
|
||||
let identity = SourceIdentity::from(&source);
|
||||
if let Some(existing) = self
|
||||
.attribution
|
||||
.sources
|
||||
.iter()
|
||||
.find(|existing| SourceIdentity::from(*existing) == identity)
|
||||
{
|
||||
if restoring && existing.first_turn_id != source.first_turn_id {
|
||||
self.mark_error(McpAttributionErrorReason::CheckpointSourceConflict);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if source.server_name.is_empty()
|
||||
|| source.tool_name.is_empty()
|
||||
|| source.first_turn_id.is_empty()
|
||||
{
|
||||
self.mark_error(if restoring {
|
||||
McpAttributionErrorReason::CheckpointInvalid
|
||||
} else {
|
||||
McpAttributionErrorReason::SourceInvalid
|
||||
});
|
||||
return;
|
||||
}
|
||||
if self.attribution.status == McpAttributionStatus::None {
|
||||
self.attribution.status = McpAttributionStatus::Complete;
|
||||
}
|
||||
self.attribution.sources.push(source);
|
||||
self.revision += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "mcp_attribution_tests.rs"]
|
||||
mod tests;
|
||||
@@ -1,242 +0,0 @@
|
||||
//! Tests for cumulative MCP attribution restoration and checkpoint acknowledgements.
|
||||
|
||||
use super::*;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::CompactedItem;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn source(tool_name: &str, first_turn_id: &str) -> McpAttributionSource {
|
||||
McpAttributionSource {
|
||||
connector_id: None,
|
||||
plugin_id: None,
|
||||
server_name: "example".to_string(),
|
||||
tool_name: tool_name.to_string(),
|
||||
first_turn_id: first_turn_id.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn envelope(attribution: Option<McpAttribution>) -> ResponseItemEnvelope {
|
||||
ResponseItemEnvelope {
|
||||
item: ResponseItem::Message {
|
||||
status: None,
|
||||
encrypted_content: None,
|
||||
id: None,
|
||||
role: "user".to_string(),
|
||||
content: Vec::new(),
|
||||
phase: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
},
|
||||
metadata: attribution.map(|mcp_attribution| CodexHarnessMetadata {
|
||||
mcp_attribution: Some(mcp_attribution),
|
||||
..Default::default()
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn records_the_first_turn_for_each_unique_source() {
|
||||
let recorder = McpAttributionRecorder::default();
|
||||
recorder.record(source("search", "turn_1"));
|
||||
recorder.record(source("search", "turn_2"));
|
||||
recorder.record(source("fetch", "turn_2"));
|
||||
|
||||
assert_eq!(
|
||||
recorder.snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source("search", "turn_1"), source("fetch", "turn_2")],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restores_cumulative_item_and_compaction_checkpoints() {
|
||||
let initial = McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source("search", "turn_1")],
|
||||
};
|
||||
let cumulative = McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source("search", "turn_1"), source("fetch", "turn_2")],
|
||||
};
|
||||
let history = InitialHistory::Forked(vec![
|
||||
RolloutItem::ResponseItem(envelope(Some(initial))),
|
||||
RolloutItem::Compacted(Box::new(CompactedItem {
|
||||
message: "summary".to_string(),
|
||||
replacement_history: Some(vec![envelope(Some(cumulative.clone()))]),
|
||||
guardian_history: None,
|
||||
retained_context: None,
|
||||
mcp_resource_origins: None,
|
||||
window_number: None,
|
||||
first_window_id: None,
|
||||
previous_window_id: None,
|
||||
window_id: None,
|
||||
compaction_response_id: None,
|
||||
latest_token_usage_record: None,
|
||||
resume_metadata: None,
|
||||
})),
|
||||
]);
|
||||
|
||||
assert_eq!(McpAttributionRecorder::new(&history).snapshot(), cumulative);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_or_conflicting_history_is_not_complete() {
|
||||
let legacy = InitialHistory::Forked(vec![RolloutItem::ResponseItem(envelope(
|
||||
/*attribution*/ None,
|
||||
))]);
|
||||
assert_eq!(
|
||||
McpAttributionRecorder::new(&legacy).snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(McpAttributionErrorReason::HistoryMissingCheckpoint),
|
||||
sources: Vec::new(),
|
||||
}
|
||||
);
|
||||
|
||||
let history = InitialHistory::Forked(
|
||||
["turn_1", "turn_2"]
|
||||
.map(|turn_id| {
|
||||
RolloutItem::ResponseItem(envelope(Some(McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source("search", turn_id)],
|
||||
})))
|
||||
})
|
||||
.to_vec(),
|
||||
);
|
||||
assert_eq!(
|
||||
McpAttributionRecorder::new(&history).snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(McpAttributionErrorReason::CheckpointSourceConflict),
|
||||
sources: vec![source("search", "turn_1")],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_error_reason_survives_checkpoints_and_later_sources() {
|
||||
let recorder = McpAttributionRecorder::default();
|
||||
recorder.record(source("", "turn_1"));
|
||||
recorder.record(source("search", "turn_2"));
|
||||
let (snapshot, _) = recorder
|
||||
.checkpoint(/*force*/ true)
|
||||
.expect("error checkpoint");
|
||||
let checkpoint = envelope(Some(snapshot));
|
||||
let mut compacted: CompactedItem =
|
||||
serde_json::from_value(serde_json::json!({"message": "summary"}))
|
||||
.expect("compaction checkpoint");
|
||||
compacted.replacement_history = Some(vec![checkpoint.clone()]);
|
||||
for item in [
|
||||
RolloutItem::ResponseItem(checkpoint),
|
||||
RolloutItem::Compacted(Box::new(compacted)),
|
||||
] {
|
||||
let restored = McpAttributionRecorder::new(&InitialHistory::Forked(vec![item]));
|
||||
restored.record(source("", "turn_3"));
|
||||
|
||||
assert_eq!(
|
||||
restored.snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(McpAttributionErrorReason::SourceInvalid),
|
||||
sources: vec![source("search", "turn_2")],
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poisoned_recorder_reports_a_bounded_reason() {
|
||||
let recorder = McpAttributionRecorder::default();
|
||||
let poisoned = recorder.clone();
|
||||
assert!(
|
||||
std::thread::spawn(move || {
|
||||
let _state = poisoned.0.lock().expect("unpoisoned recorder");
|
||||
panic!("poison recorder");
|
||||
})
|
||||
.join()
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
recorder.snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(McpAttributionErrorReason::RecorderPoisoned),
|
||||
sources: Vec::new(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restored_diagnostics_do_not_change_attribution_state() {
|
||||
let checkpoint = envelope(Some(McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: Some(McpAttributionErrorReason::SourceInvalid),
|
||||
sources: vec![source("search", "turn_1")],
|
||||
}));
|
||||
assert_eq!(
|
||||
McpAttributionRecorder::new(&InitialHistory::Forked(vec![RolloutItem::ResponseItem(
|
||||
checkpoint
|
||||
),]))
|
||||
.snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: vec![source("search", "turn_1")],
|
||||
}
|
||||
);
|
||||
|
||||
for (attribution, expected_reason) in [
|
||||
(
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: None,
|
||||
sources: Vec::new(),
|
||||
},
|
||||
McpAttributionErrorReason::RestoredErrorUnknown,
|
||||
),
|
||||
(
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::Complete,
|
||||
error_reason: None,
|
||||
sources: Vec::new(),
|
||||
},
|
||||
McpAttributionErrorReason::CheckpointInvalid,
|
||||
),
|
||||
] {
|
||||
assert_eq!(
|
||||
McpAttributionRecorder::new(&InitialHistory::Forked(vec![RolloutItem::ResponseItem(
|
||||
envelope(Some(attribution))
|
||||
),]))
|
||||
.snapshot(),
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(expected_reason),
|
||||
sources: Vec::new(),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acknowledging_an_older_checkpoint_does_not_clear_newer_changes() {
|
||||
let recorder = McpAttributionRecorder::default();
|
||||
let (_, initial_revision) = recorder
|
||||
.checkpoint(/*force*/ false)
|
||||
.expect("initial checkpoint");
|
||||
recorder.record(source("search", "turn_1"));
|
||||
recorder.mark_persisted(initial_revision);
|
||||
|
||||
let (_, latest_revision) = recorder
|
||||
.checkpoint(/*force*/ false)
|
||||
.expect("dirty checkpoint");
|
||||
recorder.mark_persisted(latest_revision);
|
||||
assert_eq!(recorder.checkpoint(/*force*/ false), None);
|
||||
assert!(recorder.checkpoint(/*force*/ true).is_some());
|
||||
}
|
||||
@@ -9,7 +9,6 @@ use crate::responses_metadata::CompactionTurnMetadata;
|
||||
use crate::responses_metadata::FORKED_FROM_ORDINAL_EXCLUSIVE_KEY;
|
||||
use crate::responses_metadata::INSTALLATION_ID_KEY;
|
||||
use crate::responses_metadata::LEGACY_CODE_MODE_TOOL_NAMES_KEY;
|
||||
use crate::responses_metadata::MCP_ATTRIBUTION_CLIENT_METADATA_KEY;
|
||||
use crate::responses_metadata::NODE_REPL_AUTO_REVIEW_REQUIRED_KEY;
|
||||
use crate::responses_metadata::NODE_REPL_DISABLED_KEY;
|
||||
use crate::responses_metadata::PARENT_TURN_ID_KEY;
|
||||
@@ -1364,7 +1363,6 @@ fn responses_api_metadata_rejects_reserved_keys() {
|
||||
TURN_TRIGGER_KEY,
|
||||
WINDOW_ID_KEY,
|
||||
CONTEXT_WINDOW_ID_KEY,
|
||||
MCP_ATTRIBUTION_CLIENT_METADATA_KEY,
|
||||
] {
|
||||
assert_eq!(
|
||||
validate_extra_metadata(
|
||||
|
||||
@@ -7430,7 +7430,7 @@ async fn code_mode_records_mcp_completion_even_when_result_is_not_printed() -> R
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = responses::start_mock_server().await;
|
||||
let (test, second_mock) = run_code_mode_turn_with_rmcp(
|
||||
let (_, 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");"#,
|
||||
@@ -7442,21 +7442,6 @@ async fn code_mode_records_mcp_completion_even_when_result_is_not_printed() -> R
|
||||
"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(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1751,28 +1751,6 @@ async fn apps_default_approval_prompts_for_writes_but_not_reads(
|
||||
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");
|
||||
|
||||
@@ -789,32 +789,6 @@ async fn text_only_mcp_content_uses_content_items() -> anyhow::Result<()> {
|
||||
])
|
||||
);
|
||||
|
||||
let request_body = request.body_json();
|
||||
let first_turn_id = request_body["client_metadata"]["turn_id"].clone();
|
||||
assert!(first_turn_id.is_string());
|
||||
let expected_attribution = json!({
|
||||
"status": "complete",
|
||||
"sources": [{
|
||||
"server_name": "rmcp",
|
||||
"tool_name": "image_scenario",
|
||||
"first_turn_id": first_turn_id,
|
||||
}],
|
||||
});
|
||||
assert_eq!(
|
||||
serde_json::to_value(codex_core::test_support::mcp_attribution_snapshot(
|
||||
&fixture.codex
|
||||
))?,
|
||||
expected_attribution,
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<serde_json::Value>(
|
||||
request_body["client_metadata"]["mcp_attribution"]
|
||||
.as_str()
|
||||
.context("MCP attribution should be included for the OpenAI provider")?,
|
||||
)?,
|
||||
expected_attribution,
|
||||
);
|
||||
|
||||
server.verify().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
use super::*;
|
||||
use anyhow::Context;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::config_types::WebSearchMode;
|
||||
use codex_protocol::openai_models::WebSearchProvider;
|
||||
@@ -205,25 +204,6 @@ fn model_search_builder(apps: &AppsTestServer, tool_mode: ToolMode) -> TestCodex
|
||||
})
|
||||
}
|
||||
|
||||
fn rewrite_rollout_response_metadata(
|
||||
path: &std::path::Path,
|
||||
mut rewrite: impl FnMut(&mut CodexHarnessMetadata),
|
||||
) -> Result<()> {
|
||||
let mut rollout = Vec::new();
|
||||
for original in std::fs::read_to_string(path)?.lines() {
|
||||
let mut line = codex_rollout::parse_rollout_line(original)?;
|
||||
if let RolloutItem::ResponseItem(item) = &mut line.item
|
||||
&& let Some(metadata) = item.metadata.as_mut()
|
||||
{
|
||||
rewrite(metadata);
|
||||
}
|
||||
rollout.push(serde_json::to_string(&line)?);
|
||||
}
|
||||
let rollout = rollout.join("\n");
|
||||
std::fs::write(path, format!("{rollout}\n"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn model_selected_search_preserves_discovery_and_call_history() -> Result<()> {
|
||||
use codex_protocol::protocol::ThreadSettingsOverrides;
|
||||
@@ -332,19 +312,6 @@ async fn model_selected_search_preserves_discovery_and_call_history() -> Result<
|
||||
.build_with_auto_env(&server)
|
||||
.await?;
|
||||
test.submit_turn("Find the FlashAttention-2 paper.").await?;
|
||||
let rollout_path = test.codex.rollout_path().context("rollout path")?;
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
let mut replaced_legacy_checkpoint = false;
|
||||
rewrite_rollout_response_metadata(&rollout_path, |metadata| {
|
||||
if let Some(attribution) = metadata.mcp_attribution.as_mut() {
|
||||
*attribution = Default::default();
|
||||
replaced_legacy_checkpoint = true;
|
||||
}
|
||||
})?;
|
||||
assert!(
|
||||
replaced_legacy_checkpoint,
|
||||
"first turn should persist legacy MCP attribution"
|
||||
);
|
||||
test = model_search_builder(&apps, ToolMode::Direct)
|
||||
.restart_with_auto_env(&server, &test)
|
||||
.await?;
|
||||
@@ -421,10 +388,18 @@ async fn metadata_free_history_withholds_native_fallback() -> Result<()> {
|
||||
let rollout_path = test.codex.rollout_path().context("rollout path")?;
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
let mut removed_checkpoint = false;
|
||||
rewrite_rollout_response_metadata(&rollout_path, |metadata| {
|
||||
removed_checkpoint |= metadata.model_search_state.take().is_some();
|
||||
metadata.mcp_attribution = None;
|
||||
})?;
|
||||
let mut rollout = Vec::new();
|
||||
for original in std::fs::read_to_string(&rollout_path)?.lines() {
|
||||
let mut line = codex_rollout::parse_rollout_line(original)?;
|
||||
if let RolloutItem::ResponseItem(item) = &mut line.item
|
||||
&& let Some(metadata) = item.metadata.as_mut()
|
||||
{
|
||||
removed_checkpoint |= metadata.model_search_state.take().is_some();
|
||||
}
|
||||
rollout.push(serde_json::to_string(&line)?);
|
||||
}
|
||||
let rollout = rollout.join("\n");
|
||||
std::fs::write(&rollout_path, format!("{rollout}\n"))?;
|
||||
assert!(
|
||||
removed_checkpoint,
|
||||
"first turn should persist model search state"
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! Decodes the removed `mcp_attribution` field in old rollouts.
|
||||
//!
|
||||
//! Only whether Search MCP may have been used is kept. New rollouts use
|
||||
//! `model_search_state`.
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Deserializer;
|
||||
|
||||
const SEARCH_CONNECTOR_ID: &str = "connector_openai_search_service";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LegacyMcpAttribution {
|
||||
status: LegacyStatus,
|
||||
#[serde(default)]
|
||||
sources: Vec<LegacySource>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
enum LegacyStatus {
|
||||
None,
|
||||
Complete,
|
||||
#[serde(other)]
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LegacySource {
|
||||
#[serde(default)]
|
||||
connector_id: Option<String>,
|
||||
}
|
||||
|
||||
pub(super) fn deserialize_search_mcp_use<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
Ok(Some(
|
||||
serde_json::from_value::<LegacyMcpAttribution>(value).map_or(true, |attribution| {
|
||||
matches!(attribution.status, LegacyStatus::Error)
|
||||
|| attribution
|
||||
.sources
|
||||
.iter()
|
||||
.any(|source| source.connector_id.as_deref() == Some(SEARCH_CONNECTOR_ID))
|
||||
}),
|
||||
))
|
||||
}
|
||||
@@ -18,6 +18,8 @@ pub use compaction_checkpoint::CompactionCheckpoint;
|
||||
mod initialization;
|
||||
pub use initialization::HistoryInitialization;
|
||||
|
||||
mod legacy_mcp_attribution;
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::borrow::Cow;
|
||||
use std::ops::Deref;
|
||||
@@ -28,8 +30,6 @@ use std::sync::Arc;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::capabilities::SelectedCapabilityRoot;
|
||||
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
||||
use codex_protocol::mcp::McpAttribution;
|
||||
use codex_protocol::mcp::McpAttributionStatus;
|
||||
use codex_protocol::mcp::McpResourceOriginCheckpoint;
|
||||
use codex_protocol::models::BaseInstructions;
|
||||
use codex_protocol::models::ContentItem;
|
||||
@@ -193,13 +193,16 @@ pub struct CodexHarnessMetadata {
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub inherited_user_message: bool,
|
||||
|
||||
/// Cumulative MCP tools/call attribution checkpoint, never model-visible.
|
||||
/// Whether an old `mcp_attribution` value indicates Search MCP may have been used.
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "deserialize_mcp_attribution_checkpoint"
|
||||
rename = "mcp_attribution",
|
||||
skip_serializing,
|
||||
deserialize_with = "legacy_mcp_attribution::deserialize_search_mcp_use"
|
||||
)]
|
||||
pub mcp_attribution: Option<McpAttribution>,
|
||||
#[schemars(skip)]
|
||||
#[doc(hidden)]
|
||||
pub legacy_search_mcp_used: Option<bool>,
|
||||
|
||||
/// Search provider already used by this thread, or confirmation that neither has run yet.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -249,22 +252,6 @@ impl std::fmt::Debug for ImageReplaySource {
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_mcp_attribution_checkpoint<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<McpAttribution>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
Ok(Some(serde_json::from_value(value).unwrap_or_else(|_| {
|
||||
McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: None,
|
||||
sources: Vec::new(),
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
impl CodexHarnessMetadata {
|
||||
/// Shortened messages no longer prove complete instruction or review delivery.
|
||||
pub fn mark_retained_sources_incomplete(&mut self) {
|
||||
|
||||
@@ -269,70 +269,31 @@ fn response_item_envelope_accessors_preserve_item() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mcp_checkpoint_handles_missing_or_unknown_identity() -> Result<()> {
|
||||
let metadata: CodexHarnessMetadata = serde_json::from_value(json!({}))?;
|
||||
assert_eq!(metadata.mcp_attribution, None);
|
||||
|
||||
let restored: CodexHarnessMetadata = serde_json::from_value(json!({
|
||||
fn old_rollout_metadata_restores_search_mcp_use() -> Result<()> {
|
||||
let metadata: CodexHarnessMetadata = serde_json::from_value(json!({
|
||||
"mcp_attribution": {
|
||||
"status": "complete",
|
||||
"sources": [{
|
||||
"connector_id": "connector_openai_search_service",
|
||||
"server_name": "example",
|
||||
"tool_name": "search",
|
||||
"first_turn_id": "turn_1",
|
||||
"future_identity": "unknown to this reader"
|
||||
"first_turn_id": "turn_1"
|
||||
}]
|
||||
}
|
||||
}))?;
|
||||
assert_eq!(
|
||||
restored.mcp_attribution,
|
||||
Some(McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: None,
|
||||
sources: Vec::new(),
|
||||
})
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mcp_error_diagnostics_do_not_invalidate_checkpoints() -> Result<()> {
|
||||
let metadata = CodexHarnessMetadata {
|
||||
mcp_attribution: Some(McpAttribution {
|
||||
status: McpAttributionStatus::AttributionError,
|
||||
error_reason: Some(
|
||||
codex_protocol::mcp::McpAttributionErrorReason::HistoryMissingCheckpoint,
|
||||
),
|
||||
sources: Vec::new(),
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
for (reason, expected_reason) in [
|
||||
(None, None),
|
||||
(
|
||||
Some(json!("future_reason")),
|
||||
Some(codex_protocol::mcp::McpAttributionErrorReason::Unknown),
|
||||
),
|
||||
(Some(json!({"invalid": true})), None),
|
||||
] {
|
||||
let mut serialized = serde_json::to_value(&metadata)?;
|
||||
let attribution = serialized["mcp_attribution"]
|
||||
.as_object_mut()
|
||||
.expect("attribution object");
|
||||
if let Some(reason) = reason {
|
||||
attribution.insert("error_reason".to_string(), reason);
|
||||
} else {
|
||||
attribution.remove("error_reason");
|
||||
assert_eq!(metadata.legacy_search_mcp_used, Some(true));
|
||||
|
||||
let clean: CodexHarnessMetadata = serde_json::from_value(json!({
|
||||
"mcp_attribution": {
|
||||
"status": "none",
|
||||
"sources": []
|
||||
}
|
||||
let restored: CodexHarnessMetadata = serde_json::from_value(serialized)?;
|
||||
let mut expected = metadata.clone();
|
||||
expected
|
||||
.mcp_attribution
|
||||
.as_mut()
|
||||
.expect("attribution checkpoint")
|
||||
.error_reason = expected_reason;
|
||||
assert_eq!(restored, expected);
|
||||
}
|
||||
}))?;
|
||||
assert_eq!(clean.legacy_search_mcp_used, Some(false));
|
||||
|
||||
let missing: CodexHarnessMetadata = serde_json::from_value(json!({}))?;
|
||||
assert_eq!(missing.legacy_search_mcp_used, None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
//! `schemars`) so they can be embedded in Codex's own protocol structures.
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::Deserializer;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use ts_rs::TS;
|
||||
@@ -64,80 +63,6 @@ pub fn is_node_repl_backed_tool(name: &str, namespace: Option<&str>) -> bool {
|
||||
.is_some_and(|(server, _)| is_node_repl_backed_server(server))
|
||||
}
|
||||
|
||||
/// Producer-reported completeness of the thread's MCP tools/call attribution.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum McpAttributionStatus {
|
||||
#[default]
|
||||
None,
|
||||
Complete,
|
||||
AttributionError,
|
||||
}
|
||||
|
||||
/// Bounded diagnostics for an attribution error, never a policy decision.
|
||||
///
|
||||
/// Recorder reasons identify the first observed error and survive checkpoints.
|
||||
/// Payload reasons describe a request-serialization fallback and are not checkpointed.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[non_exhaustive]
|
||||
pub enum McpAttributionErrorReason {
|
||||
HistoryMissingCheckpoint,
|
||||
CheckpointInvalid,
|
||||
CheckpointSourceConflict,
|
||||
SourceInvalid,
|
||||
RecorderPoisoned,
|
||||
/// An earlier error checkpoint did not retain its cause.
|
||||
RestoredErrorUnknown,
|
||||
PayloadTooLarge,
|
||||
SerializationFailed,
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// One source and the first runtime turn in which Codex recorded it.
|
||||
///
|
||||
/// Server and tool names are retained when no stable identifier is available;
|
||||
/// they are not guaranteed to be globally unique or independently authenticated.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct McpAttributionSource {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub connector_id: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub plugin_id: Option<String>,
|
||||
pub server_name: String,
|
||||
pub tool_name: String,
|
||||
pub first_turn_id: String,
|
||||
}
|
||||
|
||||
/// Cumulative, model-invisible attribution reported on Responses API requests.
|
||||
/// This is provenance data, not a training-eligibility decision or attestation.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct McpAttribution {
|
||||
pub status: McpAttributionStatus,
|
||||
/// First observed attribution error, absent for non-error states.
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "deserialize_mcp_attribution_error_reason"
|
||||
)]
|
||||
pub error_reason: Option<McpAttributionErrorReason>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub sources: Vec<McpAttributionSource>,
|
||||
}
|
||||
|
||||
fn deserialize_mcp_attribution_error_reason<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<McpAttributionErrorReason>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
Ok(serde_json::from_value(value).ok())
|
||||
}
|
||||
|
||||
/// Bounded app-resource provenance retained across a compaction checkpoint.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
pub struct McpResourceOriginCheckpoint {
|
||||
|
||||
Reference in New Issue
Block a user