Prewarm shell snapshots for eligible turns (#42137)

## Why

Shell Snapshot V2 previously captured the login-shell environment lazily when a
command started, adding that work to the command path.

## What changed

- Start one asynchronous snapshot capture after turn hooks accept the turn, so
  eligible local `exec_command` calls can reuse it without blocking the model.
- Use the turn's resolved tools, permissions, sandbox, environment, and shell
  configuration, and skip prewarming for unsupported or network-managed cases.
- Cancel outstanding prewarming during shutdown. Keep failed speculative
  captures retryable by real commands without consuming their retry budget.

## Testing

Added coverage for non-blocking capture and reuse, failure recovery, sandbox and
permission isolation, shutdown cancellation, hook-stopped turns, eligibility
gates, and concurrent capture retries.

GitOrigin-RevId: ff24a4f0328c06653e09d03fd0a3db1eccd65030
This commit is contained in:
jif authored and copyberry committed 2026-09-01 21:16:12 +00:00
1 parent c5b5fa8089
commit 612e6491d5
13 files changed
+783 -50

No files matched your search

+1 -1
View File
@@ -114,7 +114,7 @@ impl ToolCallRuntime {
}
let router = &self.step_context.tool_router;
let supports_parallel = router.tool_supports_parallel(&call);
let tool_runtime = router.tool_runtime(&call);
let tool_runtime = router.tool_runtime(&call.tool_name);
let router = Arc::clone(router);
let session = Arc::clone(&self.session);
let step_context = Arc::clone(&self.step_context);
+2 -2
View File
@@ -238,8 +238,8 @@ impl ToolRouter {
.unwrap_or(false)
}
pub(crate) fn tool_runtime(&self, call: &ToolCall) -> Option<Arc<dyn CoreToolRuntime>> {
self.registry.tool(&call.tool_name)
pub(crate) fn tool_runtime(&self, tool_name: &ToolName) -> Option<Arc<dyn CoreToolRuntime>> {
self.registry.tool(tool_name)
}
#[instrument(level = "trace", skip_all, err)]
+4 -11
View File
@@ -190,14 +190,7 @@ async fn parallel_support_does_not_match_namespaced_local_tool_names() -> anyhow
assert_eq!(
router
.tool_runtime(&ToolCall {
tool_name: ToolName::plain(parallel_tool_name),
call_id: "call-local-tool".to_string(),
payload: ToolPayload::Function {
arguments: "{}".to_string(),
},
encrypted_function_args: None,
})
.tool_runtime(&ToolName::plain(parallel_tool_name))
.map(|runtime| runtime.tool_name()),
Some(ToolName::plain(parallel_tool_name))
);
@@ -369,7 +362,7 @@ async fn mcp_parallel_support_uses_handler_data() -> anyhow::Result<()> {
assert!(router.tool_supports_parallel(&call));
assert_eq!(
router
.tool_runtime(&call)
.tool_runtime(&call.tool_name)
.map(|runtime| runtime.tool_name()),
Some(call.tool_name.clone())
);
@@ -385,7 +378,7 @@ async fn mcp_parallel_support_uses_handler_data() -> anyhow::Result<()> {
assert!(!router.tool_supports_parallel(&different_server_call));
assert_eq!(
router
.tool_runtime(&different_server_call)
.tool_runtime(&different_server_call.tool_name)
.map(|runtime| runtime.tool_name()),
Some(different_server_call.tool_name.clone())
);
@@ -399,7 +392,7 @@ async fn mcp_parallel_support_uses_handler_data() -> anyhow::Result<()> {
encrypted_function_args: None,
};
assert!(!router.tool_supports_parallel(&hidden_call));
assert!(router.tool_runtime(&hidden_call).is_some());
assert!(router.tool_runtime(&hidden_call.tool_name).is_some());
let nested_only_call = ToolCall {
tool_name: ToolName::namespaced("mcp__nested_echo__", "query_with_delay"),