mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +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:
@@ -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] {
|
||||
|
||||
Reference in New Issue
Block a user