Skip to content

fix(tool-search): skip catalog rebuild when tool set is unchanged - #87172

Merged
steipete merged 1 commit into
openclaw:mainfrom
SebTardif:fix/tool-search-catalog-reuse
May 27, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
SebTardif:fix/tool-search-catalog-reuse

Conversation

@SebTardif

@SebTardif SebTardif commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the repeated Tool Search catalog work reported in #86887. Embedded agent runs now mark Tool Search catalog reuse only when the active catalog is actually reused or restored for the current run binding.

The rewrite keeps run-scoped active catalogs intact while adding a bounded session-level reusable snapshot for unchanged tool sets after run cleanup. Reuse is guarded by a full catalog-affecting fingerprint: id/source/name/label/description, parameters, and executable tool identity. When reuse happens, the current catalogRef and all current lookup aliases are bound before returning, so later Tool Search resolution still works.

Changes:

  • Return catalogReused from the shared Tool Search compaction path.
  • Reuse same-run catalogs only after binding the current ref/aliases.
  • Restore unchanged catalogs after clearToolSearchCatalog from a bounded session snapshot.
  • Invalidate reuse when parameters or executable tool identity changes.
  • Suppress repeated embedded-agent catalog logs only on real reuse.

Closes #86887

Verification

  • pnpm test src/agents/tool-search.test.ts -- --reporter=verbose
    • 25 tests passed, including same-run reuse, cross-run restore after cleanup, executable-change invalidation, and parameter-change invalidation.
  • pnpm check:changed
    • Blacksmith Testbox tbx_01ksney4f00wgk9n39yv7jsh4m, exit 0.
  • git merge-tree --write-tree origin/main HEAD
    • clean merge tree f82ca88ddcfc695a8d631d569a92590900999f1d.
  • $autoreview
    • final result: autoreview clean: no accepted/actionable findings reported.

Real behavior proof

  • Behavior addressed: repeated embedded-agent Tool Search catalog registration/logging for unchanged effective tool sets.

  • Real environment tested: local OpenClaw source checkout on macOS, running the actual Tool Search source module with Node and the repo runtime loader.

  • Exact steps or command run after this patch:

    node --import tsx --input-type=module -e 'import { applyToolSearchCatalog, clearToolSearchCatalog, createToolSearchTools, TOOL_SEARCH_CODE_MODE_TOOL_NAME } from "./src/agents/tool-search.ts"; /* constructs one OpenClaw Tool Search catalog, clears the run, reapplies the same tools, then calls a target through tool_search_code */'
  • Evidence after fix: terminal output from that OpenClaw runtime path:

    openclaw tool-search proof: first run compacted=true reused=false catalogToolCount=3 visible=tool_search_code
    openclaw tool-search proof: second run compacted=true reused=true catalogToolCount=3 visible=tool_search_code
    openclaw tool-search proof: runtime call ok=true calls=1 telemetry={"catalogSize":3,"sources":{"openclaw":3,"mcp":0,"client":0},"searchCount":1,"describeCount":0,"callCount":1}
    
  • Observed result after fix: the first run registers the compact catalog, run cleanup does not destroy the reusable snapshot, the second run restores it with catalogReused=true, and tool_search_code still dispatches to the restored target catalog.

  • What was not tested: no live Matrix gateway delivery; this proof exercises the OpenClaw Tool Search runtime path that owns catalog registration/reuse and target dispatch.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S proof: supplied External PR includes structured after-fix real behavior proof. labels May 27, 2026
@clawsweeper

clawsweeper Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed May 27, 2026, 3:55 PM ET / 19:55 UTC.

Summary
The PR adds Tool Search catalog reuse state, returns catalogReused from compaction, suppresses embedded catalog logs on reuse, and adds reuse/invalidation tests.

PR surface: Source +204, Tests +144. Total +348 across 3 files.

Reproducibility: yes. from source and reporter logs, though I did not run a live embedded session. Current main unconditionally registers the compact catalog on the enabled path, and the linked report includes repeated embedded-run catalog log lines.

Review metrics: 1 noteworthy metric.

  • Process-local catalog state: 3 stores added, 1 counter added. The reusable snapshot map, catalog fingerprint WeakMap, tool identity WeakMap, and counter decide cross-run Tool Search reuse and retain runtime objects.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🦪 silver shellfish
Patch quality: 🧂 unranked krab
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Fix the fingerprint so it does not depend on per-run wrapper identity while still rebinding fresh run/session hook context.
  • Add focused coverage where toolHookContext is present and consecutive embedded-style runs reuse only when safe.
  • Add redacted embedded-agent logs or terminal output showing unchanged repeated turns no longer rebuild/register the catalog.

Proof guidance:
Needs stronger real behavior proof before merge: The PR supplies terminal output from a direct Tool Search module script, but it does not show repeated embedded-agent runs with per-run hook wrapping, which is the reported behavior. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • The current fingerprint can keep returning catalogReused: false on the reported embedded-run path because consecutive runs create fresh wrapped tool objects.
  • If the cache is repaired by ignoring wrapper identity, restoring old entries without rebinding fresh hook/session/run context could execute tools with stale run state.
  • The supplied terminal proof shows a direct module path, not repeated embedded-agent turns with per-run hook wrapping.

Maintainer options:

  1. Fix wrapper-stable reuse before merge (recommended)
    Change the cache semantics so equivalent embedded-run tool sets can reuse safely while restoring fresh hook/session/run context, then add focused proof for that path.
  2. Land only as groundwork
    Maintainers could accept the internal cache scaffolding knowingly, but the linked runtime bug should remain open until embedded-run reuse is proven.
  3. Pause for a narrower replacement
    If the cache ownership is too risky, replace this branch with a smaller patch that proves one safe embedded-run reuse path first.

Next step before merge
Needs human review because the cache semantics must avoid both no-op reuse and stale run/session hook reuse before this PR can safely merge.

Security
Cleared: The diff does not change dependencies, CI, credentials, permissions, package scripts, or other supply-chain surfaces; the concrete concerns are functional cache/session correctness.

Review findings

  • [P1] Do not fingerprint wrapped per-run tools for reuse — src/agents/tool-search.ts:525
Review details

Best possible solution:

Make reuse stable across equivalent embedded-run tool sets while rebinding fresh hook/session/run context, then prove it with toolHookContext coverage and redacted repeated embedded-agent logs.

Do we have a high-confidence way to reproduce the issue?

Yes, from source and reporter logs, though I did not run a live embedded session. Current main unconditionally registers the compact catalog on the enabled path, and the linked report includes repeated embedded-run catalog log lines.

Is this the best way to solve the issue?

No. The PR fingerprints wrapped catalog tool object identity, while embedded attempts use per-run wrapped tools, so the advertised cross-run reuse path does not cover the reported behavior.

Full review comments:

  • [P1] Do not fingerprint wrapped per-run tools for reuse — src/agents/tool-search.ts:525
    The fingerprint includes catalogToolIdentity(entry.tool), but embedded attempts pass run/session hook context and toCatalogEntry wraps catalog tools with that context. Consecutive embedded turns therefore get fresh wrapped tool identities, so the reusable snapshot comparison will miss on the reported path and the repeated catalog rebuild/log remains.
    Confidence: 0.9

Overall correctness: patch is incorrect
Overall confidence: 0.9

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 74f9d6b96d48.

Label changes

Label justifications:

  • P2: This is a normal-priority agent runtime performance fix with limited blast radius, but the PR still has a correctness blocker.
  • merge-risk: 🚨 session-state: The PR adds cross-run Tool Search snapshot state that can affect which catalog is bound to a session, session key, agent, or run.
  • merge-risk: 🚨 availability: Incorrect catalog reuse or stale retained entries could make Tool Search resolve unavailable or wrong tools in long-lived embedded-agent processes.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🦪 silver shellfish and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR supplies terminal output from a direct Tool Search module script, but it does not show repeated embedded-agent runs with per-run hook wrapping, which is the reported behavior. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +204, Tests +144. Total +348 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 207 3 +204
Tests 1 144 0 +144
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 351 3 +348

What I checked:

  • Repository policy read: Root AGENTS.md and scoped src/agents/AGENTS.md were read fully; their agent hot-path, cache lifecycle, and real behavior proof guidance affected the review. (AGENTS.md:1, 74f9d6b96d48)
  • Current main rebuild path: Current main builds a fresh catalog and calls registerToolSearchCatalog(... append: false) whenever Tool Search compaction runs. (src/agents/tool-search.ts:1052, 74f9d6b96d48)
  • Embedded runner uses per-run hook context: The embedded attempt runner creates catalogToolHookContext with run/session fields and passes it into Tool Search catalog application. (src/agents/embedded-agent-runner/run/attempt.ts:2023, 74f9d6b96d48)
  • PR fingerprint includes wrapped tool object identity: The PR head fingerprint includes catalogToolIdentity(entry.tool), so the identity of the wrapped catalog tool participates in reuse decisions. (src/agents/tool-search.ts:525, 6bfc12510115)
  • Wrapper objects are run-context-bearing: wrapToolWithBeforeToolCallHook creates a new wrapped tool object whose execute closure uses the supplied hook context, including run/session fields. (src/agents/agent-tools.before-tool-call.ts:943, 74f9d6b96d48)
  • History provenance: Blame ties the current Tool Search compaction and embedded catalog logging paths to the recent agent runtime internalization work and a follow-up control-tool suppression line. (src/agents/tool-search.ts:1004, 1a34c4833e62)

Likely related people:

  • steipete: Recent history shows agent-runtime internalization work in the affected files, and the current PR head commit is authored by this person. (role: recent area contributor and PR-head author; confidence: high; commits: bb46b79d3c14, 6bfc12510115; files: src/agents/tool-search.ts, src/agents/embedded-agent-runner/run/attempt.ts, src/agents/agent-tools.before-tool-call.ts)
  • Vincent Koc: git blame points the main Tool Search compaction function, embedded catalog logging, and before-tool-call wrapper implementation to commit 1a34c48. (role: introduced current compaction/logging path; confidence: medium; commits: 1a34c4833e62; files: src/agents/tool-search.ts, src/agents/embedded-agent-runner/run/attempt.ts, src/agents/agent-tools.before-tool-call.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. labels May 27, 2026
@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@openclaw-barnacle openclaw-barnacle Bot added triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. and removed proof: supplied External PR includes structured after-fix real behavior proof. labels May 27, 2026
@SebTardif
SebTardif force-pushed the fix/tool-search-catalog-reuse branch from c7ba718 to 5733039 Compare May 27, 2026 14:17
@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. label May 27, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. labels May 27, 2026
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label May 27, 2026
@steipete steipete self-assigned this May 27, 2026
@steipete
steipete force-pushed the fix/tool-search-catalog-reuse branch from abda6f6 to 861bad8 Compare May 27, 2026 19:31
Reuse active Tool Search catalogs only when the full catalog-affecting fingerprint matches, and restore cached entries into the current run binding after cleanup. Suppress repeated catalog logs only for real reuse.

Also fixes an existing lint issue in the session store clone helper so the changed core gate can pass on latest main.

Closes openclaw#86887
Co-authored-by: Sebastien Tardif <[email protected]>
@steipete
steipete force-pushed the fix/tool-search-catalog-reuse branch from 861bad8 to 6bfc125 Compare May 27, 2026 19:42
@openclaw-barnacle openclaw-barnacle Bot added triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. proof: supplied External PR includes structured after-fix real behavior proof. and removed proof: supplied External PR includes structured after-fix real behavior proof. triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. labels May 27, 2026
@steipete
steipete merged commit 60e8e60 into openclaw:main May 27, 2026
188 of 197 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Tool Search catalog is rebuilt on every embedded agent turn

2 participants