Repository navigation
fix(agents): stop tool discovery from breaking prompt caches - #114572
Conversation
14b4b53 to
9434063
Compare
|
Codex review: needs maintainer review before merge. Reviewed July 27, 2026, 9:49 AM ET / 13:49 UTC. ClawSweeper reviewWhat this changesThe branch makes Tool Search directory mode use a stable directly callable tool surface, sorts provider-visible tools deterministically for prompt-cache reuse, and improves cache-break diagnostics across agent and provider paths. Merge readinessThis PR has a coherent cache-stability implementation, passing CI evidence, and sufficient real gateway proof. It must remain open for maintainer review because its intentional directory-mode behavior change can alter which plugin schemas existing opted-in users receive directly, and the item carries the protected Priority: P1 Review scores
Verification
How this fits togetherTool Search compacts a large catalog of agent tools into a directory plus a small direct surface before agent requests reach OpenAI, Anthropic, or Gemini. This PR makes that provider-visible prefix deterministic so cache identity and diagnostics remain stable across plugin discovery order and unrelated user prompts. flowchart LR
A[Plugin and core tools] --> B[Tool Search catalog]
B --> C[Stable direct tool surface]
B --> D[Capability directory]
C --> E[Provider request builder]
D --> E
E --> F[Prompt cache identity]
F --> G[Agent response and tool execution]
Decision needed
Why: The code and proof support the cache-stability invariant, but choosing the upgrade behavior for already opted-in directory-mode users is a product compatibility decision rather than a mechanical correctness question. Before merge
Agent review detailsSecurityNone. PR surfaceSource -225, Tests +312, Docs +2. Total +89 across 28 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep Tool Search opt-in, retain the stable capability directory and explicitly trusted direct tools, and land only after a maintainer confirms that replacing prompt-selected direct plugin schemas is the intended upgrade behavior. Do we have a high-confidence way to reproduce the issue? Yes, at source-and-proof level: the PR supplies a real gateway QA scenario and focused cross-provider regressions for reordered tools and directory mode, although this review could not independently rerun them in the read-only environment. Is this the best way to solve the issue? Unclear pending maintainer direction: canonical ordering and a stable directory are a coherent fix for prompt caches, but maintainers must confirm that the direct-schema compatibility tradeoff is the intended solution for existing directory-mode users. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 96a7a21d3130. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
9434063 to
e3e65f1
Compare
e3e65f1 to
cb52d7e
Compare
5146988 to
b9e49f3
Compare
b9e49f3 to
b2f60ca
Compare
b2f60ca to
d954a46
Compare
|
Merged via squash.
|
Additional instructions
Maintainer edits are enabled for this maintainer-owned branch.
What Problem This Solves
Fixes an issue where users running multi-turn agents with Tool Search, changing plugin discovery order, Anthropic, or Gemini would unexpectedly lose provider prompt-cache reuse even when their trusted tools and stable instructions were unchanged. Cache-break diagnostics could also incorrectly blame dynamic per-turn instructions or miss actual tool-schema changes.
Why This Change Was Made
Give directory mode one deterministic, automatically included trusted capability directory; expose its stable search controls, core coding primitives, direct-only tools, and explicitly policy-required trusted message delivery. Delete turn-specific schema guesses and their duplicated hydration heuristics in both the embedded runner and agent harness. Canonicalize provider-visible tool declarations through one shared ASCII-stable ordering for OpenAI, Anthropic, direct Gemini, and managed Gemini; keep Anthropic's cache breakpoint and Gemini's managed-cache identity stable. Order plugin prompt guidance by plugin and command ownership. Fingerprint only the cacheable system prefix and bounded, provider-visible tool descriptor/schema digests, and do no fingerprinting when diagnostics are disabled. Preserve MCP/client trust boundaries and existing direct client-tool execution.
No config keys, environment variables, runtime defaults, migration, or protocol versions are added or changed. Tool Search remains opt-in; when enabled, its capability directory is included automatically and stays stable across turns.
User Impact
Agents can discover available trusted capabilities without sacrificing prompt KV-cache hits. Identical tool sets produce identical OpenAI, Anthropic, and Gemini request prefixes regardless of plugin/tool discovery order; repeated Gemini turns reuse existing managed cached content. Operators receive accurate cache-break diagnostics without storing prompt, tool-description, or schema contents and without adding hot-path work to ordinary runs.
Evidence
cache_controlbreakpoint.toolsdiagnostics.codex-rs/core/src/client.rs,codex-rs/core/src/tools/handlers/tool_search_spec.rs, andcodex-rs/protocol/src/dynamic_tools.rs.Maintainer decision and upgrade behavior
The repository maintainer explicitly requested this exact product decision: “If tool search is enabled we should automatically give it the directory,” followed by “review the whole system for kv friendliness - full agent loop” and “fix all of that, refactor generously and PR test land.” Existing opted-in directory-mode configurations therefore intentionally move to one automatically provided, stable capability directory rather than prompt-selected directly hydrated schemas. Preserving the previous per-prompt hydration would change the provider-visible tool prefix on every request and directly violate the approved KV-cache requirement. Core file and shell coding primitives, required message delivery, direct-only tools, configured client tools, and MCP/trust boundaries remain directly available where their contracts require it. Tool Search remains opt-in; no configuration defaults, keys, migration, or protocol versions change.
Real gateway behavior proof
The documented, user-facing command was executed on a real Linux Blacksmith Testbox against the actual gateway, QA Lab, and mock OpenAI provider:
The scenario verifies that 36 installed plugin-owned tools remain behind the stable compact bridge, the target capability is automatically visible in the bounded directory, the target plugin actually executes, and the provider payload is smaller than directly exposing all tool schemas.
Complete build and validation
pnpm buildpasses, including the Control UI and all 143 public plugin SDK subpaths.main; this PR rebases onto and reuses that upstream solution unchanged instead of adding a duplicate security action, workflow change, or CI guard.Current-main compatibility
Rebased onto current
mainand retained its newly landed core-coding visibility contract: genuine core file and shell primitives remain directly callable in Tool Search directory mode, but plugin tools that reuse core names remain deferred and prompt-selected hydration is still removed. Both the directory and agent-harness regressions cover identical visible coding tools across unrelated user prompts. The public documentation describes the stable coding, client, direct-only, and policy-required tool surface explicitly.MCP trust-boundary hardening
A fresh whole-branch review identified and verified the policy-delivery spoofing edge case. Directory mode now promotes explicitly required names only when their canonical catalog provenance is trusted OpenClaw, and core coding primitives only when they are core-owned. Uniquely named MCP tools cannot impersonate
message, file, or shell tools; ambiguous names remain deferred. Separate regressions prove both explicit MCP metadata andbundle-mcplookalikes are excluded, while legitimate plugin-owned message delivery and stable core coding tools remain directly visible.Final lean product-only SHA
d954a465b52: all 841 Linux provider, runtime, hostile-schema, MCP-boundary, and upstream workflow-guard regressions pass; the documented real gateway scenario was rerun and passes on this exact head, and fresh whole-branch autoreview reports zero findings.Prototype-safe cache diagnostics
Schema fingerprint normalization uses null-prototype records, preserving hostile but valid own JSON-schema keys such as
__proto__without prototype pollution. Regression tests prove distinct__proto__schemas produce distinct fingerprints and absent fields cannot collide; depth, width, node, getter, and string bounds remain intact.