Preserve user input when MCP startup is interrupted (#34839)

## Why

Interrupting a turn while MCP tools were still starting could abort before the
submitted user input was recorded in conversation history.

## What changed

- Build and retain the MCP tool list and tool router as part of each step
  snapshot, using the turn cancellation token during tool discovery.
- Record turn hooks and submitted input when cancellation occurs during startup
  prewarming, pre-sampling compaction, or first-step capture.
- Reuse the captured router for sampling, compaction, and prompt debugging so
  advertised and executable tools share the same request-scoped view.

## Testing

Add coverage that interrupts each affected MCP startup phase and verifies that
the user prompt remains in history before the turn-aborted marker.

GitOrigin-RevId: 6e96c6814357a55545d69b580e28f35933cfadf7
This commit is contained in:
Ahmed Ibrahim authored and copyberry committed 2026-07-22 22:50:13 +00:00
1 parent bd5b55e403
commit d7e8f4c3dc
21 files changed
+359 -174

No files matched your search

+13 -2
View File
@@ -1,6 +1,9 @@
use crate::environment_selection::TurnEnvironmentSnapshot;
use crate::function_tool::FunctionCallError;
use crate::session::McpRuntimeSnapshot;
use crate::session::session::Session;
use crate::session::step_context::StepContext;
use crate::session::turn_context::TurnContext;
use crate::tools::context::SharedTurnDiffTracker;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
@@ -58,11 +61,19 @@ pub(crate) struct ToolSuggestCandidates {
impl ToolRouter {
pub(crate) fn from_context(
step_context: &StepContext,
turn_context: &TurnContext,
environments: &TurnEnvironmentSnapshot,
mcp: &McpRuntimeSnapshot,
params: ToolRouterParams<'_>,
tool_search_handler_cache: &ToolSearchHandlerCache,
) -> Self {
build_tool_router(step_context, params, tool_search_handler_cache)
build_tool_router(
turn_context,
environments,
mcp,
params,
tool_search_handler_cache,
)
}
pub(crate) fn from_parts(registry: ToolRegistry, model_visible_specs: Vec<ToolSpec>) -> Self {