Preserve deferred tool declarations in prediction forks (#65146)

## Why

A prediction fork that disables MCP inherits the parent's tool declarations,
but recomputing the deferred tool list can append a conflicting declaration
to the request.

## What changed

Skip the deferred tool list update when the fork preserves the parent's tool
declarations. The fork still reports MCP tool unavailability separately.

## Testing

Extend `prediction_fork_keeps_inherited_tools` to cover both inline and
deferred MCP tools and assert that the fork carries exactly the parent's
`<tools>` messages.

GitOrigin-RevId: 04b4a542e0d587a20706935e8b028b7f1fdb9209
This commit is contained in:
Charlie Marsh
2026-12-02 02:40:50 +00:00
committed by copyberry
parent 3fef28219c
commit 028590df7d
3 changed files with 46 additions and 16 deletions
+2
View File
@@ -220,6 +220,7 @@ impl Session {
let extension_metrics = super::extension_metrics::from_session_telemetry(
step_context.session_telemetry.clone(),
);
// Prediction forks keep the parent's tool declarations and announce disabled MCP separately.
let preserve_parent_tool_declarations =
!new_window && self.preserve_parent_tool_declarations();
if incremental_tools && !preserve_parent_tool_declarations {
@@ -464,6 +465,7 @@ impl Session {
.config
.features
.enabled(Feature::DeferredToolWorldState)
&& !preserve_parent_tool_declarations
{
world_state.add_section(ToolsState::new(
step_context.tool_router.deferred_tool_namespaces(),
@@ -1,6 +1,7 @@
//! Request-history coverage for incremental tools on Responses and Responses Lite.
use anyhow::Result;
use codex_config::types::McpServerConfig;
use codex_core::ForkSnapshot;
use codex_core::McpServersDisabled;
use codex_core::compact::AutoCompactionDisabled;
@@ -14,6 +15,7 @@ use codex_otel::MetricsConfig;
use codex_otel::TOOL_INCREMENTAL_UPDATES_METRIC;
use codex_otel::TOOL_REGISTRATIONS_METRIC;
use codex_otel::TOOL_SPEC_DESCRIPTION_BYTES_METRIC;
use codex_protocol::config_types::ToolExposureSurface;
use codex_protocol::openai_models::CodeModeToolMessages;
use codex_protocol::openai_models::IncrementalToolMessages;
use codex_protocol::openai_models::ToolMessage;
@@ -49,16 +51,18 @@ async fn prediction_fork_keeps_inherited_tools() -> Result<()> {
);
skip_if_no_network!(Ok(()));
let server = responses::start_mock_server().await;
let mcp = serde_json::from_value(json!({
let deferred: McpServerConfig = serde_json::from_value(json!({
"command": remote_aware_stdio_server_bin()?,
"environment_id": remote_aware_environment_id(),
"enabled_tools": ["echo"],
"required": true,
}))?;
let mut inline = deferred.clone();
inline.omit_tools_from = Some(vec![ToolExposureSurface::Deferred]);
let mut test = test_codex()
.with_model_info_override("gpt-5.4", |model| {
model.use_responses_lite = true;
model.supports_search_tool = false;
model.supports_search_tool = true;
model.tool_mode = Some(ToolMode::CodeModeOnly);
})
.with_config(move |config| {
@@ -70,15 +74,15 @@ async fn prediction_fork_keeps_inherited_tools() -> Result<()> {
.expect("enable incremental tools");
config
.mcp_servers
.set(std::collections::HashMap::from([(
"echo_service".into(),
mcp,
)]))
.expect("set MCP server");
.set(std::collections::HashMap::from([
("echo_service".into(), inline),
("deferred_echo".into(), deferred),
]))
.expect("set MCP servers");
})
.build_with_auto_env(&server)
.await?;
wait_for_mcp_server(&test.codex, "echo_service").await?;
wait_for_mcp_server(&test.codex, "deferred_echo").await?;
let mock = responses::mount_sse_sequence(
&server,
["parent", "prediction", "continued-parent"]
@@ -139,6 +143,20 @@ async fn prediction_fork_keeps_inherited_tools() -> Result<()> {
&child["input"].as_array().expect("child input")[..prefix.len()],
prefix
);
let tool_lists = |request: &responses::ResponsesRequest| {
request
.message_input_texts("developer")
.into_iter()
.filter(|text| text.starts_with("<tools>"))
.collect::<Vec<_>>()
};
let parent_lists = tool_lists(&requests[0]);
assert!(
parent_lists
.iter()
.any(|text| text.contains("mcp__deferred_echo"))
);
assert_eq!(tool_lists(&requests[1]), parent_lists);
insta::assert_snapshot!(
"prediction_fork_inherited_tools",
context_snapshot::format_request_history_snapshot(
@@ -16,15 +16,20 @@ Settings:
text: {"verbosity":"low"}
tool_choice: "auto"
-- request 1 (turn) --
00:additional_tools/developer (1; hash=db5407962f84b284):
- namespace/functions; hash=42507901e1c84d86
00:additional_tools/developer (1; hash=5fbdd8da3f5d8dcd):
- namespace/functions; hash=123dcb801a67fcb7
- custom/exec
- function/wait
- function/request_user_input
01:message/developer:
<MODEL_INSTRUCTIONS>
02:message/developer:
<PERMISSIONS_INSTRUCTIONS>
02:message/developer[2]:
[01] <PERMISSIONS_INSTRUCTIONS>
[02] <tools>
Deferred tool namespaces:
- mcp__deferred_echo: Use these tools to exercise the rmcp test server.
- multi_agent_v1: Tools for spawning and managing sub-agents.
</tools>
03:message/user:
<ENVIRONMENT_CONTEXT>
04:message/user:
@@ -40,15 +45,20 @@ Settings:
## Window 2 (after request 2: input diverged at item 05)
Settings: same as window 1
-- request 3 (turn) --
00:additional_tools/developer (1; hash=db5407962f84b284):
- namespace/functions; hash=42507901e1c84d86
00:additional_tools/developer (1; hash=5fbdd8da3f5d8dcd):
- namespace/functions; hash=123dcb801a67fcb7
- custom/exec
- function/wait
- function/request_user_input
01:message/developer:
<MODEL_INSTRUCTIONS>
02:message/developer:
<PERMISSIONS_INSTRUCTIONS>
02:message/developer[2]:
[01] <PERMISSIONS_INSTRUCTIONS>
[02] <tools>
Deferred tool namespaces:
- mcp__deferred_echo: Use these tools to exercise the rmcp test server.
- multi_agent_v1: Tools for spawning and managing sub-agents.
</tools>
03:message/user:
<ENVIRONMENT_CONTEXT>
04:message/user: