Repository navigation
🚥 feat: Inspect Selected Tool Results Before Use - #719
Conversation
Commit-hook output only (sort-imports, prettier, eslint --fix) for three existing files the next commit edits, kept separate so that commit shows only the change.
Adds default-off RunConfig.toolResultProtection. For the tools a host policy names, the text a tool returns is replaced by the host inspector's text before hooks, the error handler, completion events, the output reference registry, checkpoints or the model read it. A failed inspection ends the run with a coded ToolResultProtectionError. Covers in-process tools, host-executed tools, error text and subagents. Selected tools are kept out of eager start, per-call early completion and programmatic tool calling; client delegation and remote SDK runs reject the policy. Unselected tools are untouched. Host process code is trusted: text authored by hooks or handlers and history the host supplies are not inspected. Supersedes #597.
|
@codex review the latest head, final review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3f64587dbc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const gate = this.protectedResultGate(call.name); | ||
| if (gate != null) { | ||
| e = new Error( | ||
| await inspectToolText( |
There was a problem hiding this comment.
Inspect selected tool errors before the fail-fast rethrow
When an exported ToolNode is configured with both toolResultProtection and handleToolErrors: false, a selected tool that throws bypasses this inspection entirely: the earlier if (!this.handleToolErrors) rethrows its original error first. Consequently, uninspected error text escapes through the node's rejection and enclosing runnable callbacks, despite the policy's guarantee that selected tool errors are inspected before SDK consumption. Sanitize selected tool failures before that early rethrow while preserving the requested fail-fast behavior.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in 1191d80.
A ToolNode built with handleToolErrors: false rethrew a protected tool's failure before the inspection ran. The catch block now inspects first, for both the fail-fast rethrow and the error-message path, so the rethrown error is a plain Error carrying the inspected message (no original type or stack). Interrupts and run safety aborts (StreamLimitExceededError, PreparedSubagentError, ProviderTextProtectionError) still pass through untouched, and an unselected tool's error is rethrown as the same object.
Tests in ToolNode.toolResultProtection.test.ts: "inspects a selected tool error before a fail-fast rethrow" (fails with the rethrow reverted to the original error) and "rethrows an unselected tool error unchanged in fail-fast mode".
| if (graph.toolResultProtection?.toolNames.includes(name) === true) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Apply the protection exclusion to eager subagents too
When eager subagent execution is enabled and toolNames includes subagent, this exclusion does not prevent that selected tool from starting speculatively. The separate startPreparedSubagents() path calls Graph.prestartSubagent() and then ToolNode.prestartSubagent(), neither of which checks the protection policy. Besides violating the documented guarantee that selected tools never start eagerly, this means a subsequently revised or discarded call triggers the fatal PreparedSubagentError in PreparedSubagents.finish() instead of remaining on the committed execution path. Apply the protected-tool exclusion to the prepared-subagent path as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Accepted, fixed in 1191d80, with one correction to the premise.
No raw text was published by this path: a prestarted subagent's output is only read through preparedSubagents.take() in runTool, which is where the inspection sits, so the result was inspected either way. The PreparedSubagentError on a revised call is existing prestart behaviour and not specific to protection.
The documented rule should still hold without an exception, so ToolNode.prestartSubagent now returns early for a protected tool, next to the existing excludeToolNames check. The call then runs on the committed path like every other protected tool.
Test in ToolNode.preparedSubagents.test.ts: "leaves a subagent tool with protected results on the normal path" (fails without the check: the child starts before the batch).
… Prestart A ToolNode built with handleToolErrors false rethrew a protected tool's failure before it was inspected. The failure is now inspected first, for both the rethrow and the error-message path; interrupts and run safety aborts still pass through untouched. A protected subagent tool could still be started ahead of its batch by prestartSubagent. It now stays on the committed path like every other protected tool.
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Adds default-off
RunConfig.toolResultProtection. A host names the tools whose output must be inspected; for those tools the SDK replaces the text the tool returned with the text the host'sinspectreturned before anything else reads it. A failed inspection ends the run with a codedToolResultProtectionError.Supersedes #597, which implemented the same public option. That branch treated code in the host process as hostile and grew to 12k lines without converging. With its policy on, a follow-up turn failed once history held a protected result, unselected tools were rejected for returning non-plain artifacts, and every model call revalidated the whole history. This PR keeps the option's shape and starts from a written trust boundary instead.
Contract
Guaranteed. For a selected tool, post-tool hooks, the error handler, run-step completion events, the output reference registry, checkpoints and every later model request receive the inspected text. This holds for in-process tools, tools the host runs through
ON_TOOL_EXECUTE, error text from either, and the same tools inside subagents.Trust boundary. The tool's text is untrusted data. Code running in the host process is trusted: tools, hooks, handlers, callbacks and checkpoint savers are not defended against, and text they author is not inspected. History the host supplies is not inspected; the host only ever received inspected text for selected tools.
Limits, all explicit.
Commandends the run asunsupported.Run.create.docs/tool-result-protection.mdcarries the same contract for hosts.Mechanism
Inspection reuses
ProviderTextAttemptfor the timeout, abort handling and the byte budget, which is shared per policy object across every graph of a run. Inspector exceptions surface asunavailable; their message is never forwarded.With no policy, every added branch is skipped; no existing statement changes behaviour.
The first commit is commit-hook formatting of three existing files and changes no logic. Review the second commit for the change.
Verification
src/protection/toolResult.test.tsToolNode.toolResultProtection.test.tsstream.eagerEventExecutionandToolNode.lazyCodeFilesnpx jestllm.spec.tslive-provider suites fail without API keys and are excluded from the CI unit shardsnpx tsc --noEmit,npm run build:typesnpx eslint "src/**/*.ts"check:circular-deps,test:light-entriesEach call site was disabled in turn and a test failed for it: in-process output, in-process error, host results, early completion, programmatic exclusion, eager exclusion, client delegation, subagent inheritance, and code-session retention on failure.
Not run: live provider integrations, a packed-package consumer install, and any LibreChat integration. No host adopts the option yet.
Related to AI-2213.