Skip to content

fix(knowledge): tools fail with "authority no longer active" after the first turn of a warm Claude Code session - #193687

Merged
VACInc merged 2 commits into
openclaw:mainfrom
VACInc:fix/knowledge-storage-caller-context
Nov 5, 2026
Merged

VACInc merged 2 commits into
openclaw:mainfrom
VACInc:fix/knowledge-storage-caller-context

Conversation

@VACInc

@VACInc VACInc commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

High Level TLDR

Fixes: Knowledge tools (knowledge_grep, knowledge_status, knowledge_diagnostics, knowledge_remember, ...) fail with tool invocation authority is no longer active on every turn after the first when a Claude Code (claude-cli) session keeps its process warm. The first turn works; every later turn fails until the Gateway restarts, then the cycle repeats.

The Knowledge storage worker asks the main thread to re-check the caller's authority mid-operation. That check ran inside the worker's message listener, and Node runs that listener in the async context of whichever call started the worker. So every later call was checked against the first call's retired turn. The fix runs each call's check in that call's own async context.

What Problem This Solves

Fixes: Knowledge tool calls fail with tool invocation authority is no longer active on the second and later turns of a warm claude-cli session.

User Impact

Agents on the Claude Code runtime can use Knowledge tools on every turn, not only the first turn after a Gateway (re)start. Authority checks stay fail-closed for retired turns.

Root Cause

  • StorageClient (extensions/knowledge/src/storage-client.ts) starts one storage worker lazily on the first storage call and keeps it for the store's lifetime. During an operation the worker posts { type: "assert" }; the main thread answers by calling call.access.assertCurrent() inside worker.on("message", ...).
  • For tool callers, access.assertCurrent() re-resolves the principal, which calls the tool context's assertInvocationCurrent(). That host guard reads the ambient per-call source (assertPluginToolInvocationSourceCurrent() -> AsyncLocalStorage.getStore(), set by runWithPluginToolInvocationSource at call entry) and checks its receipt (captureAgentToolSourceExecutionGuard -> "tool invocation authority is no longer active").
  • A Worker's message events are emitted in the async context captured when the Worker was constructed, not the context of the call that posted the request. Probe on Node 26.13.0 (Worker created inside als.run("turn-1"), later calls under "turn-2"/"turn-3"):
    [{"caller":"turn-1","storeInListener":"turn-1"},{"caller":"turn-2","storeInListener":"turn-1"},{"caller":"turn-3","storeInListener":"turn-1"}]
    
  • On the claude-cli path each turn gets a fresh loopback grant row (transferred onto the bearer the warm child already holds), so turn 1's receipt goes false when turn 1 ends. Every later worker-side check therefore read turn 1's retired receipt and failed. A Gateway restart creates a new worker, so the next first call works again; that matches the observed "works once after restart, then breaks".
  • The same listener context also means a worker first started outside a tool call (no ambient source) silently skipped later tool calls' caller check, and a worker started by one caller checked other callers against that first caller's authority.

Why This Change Was Made

StorageClient.call now captures AsyncLocalStorage.snapshot() when it dispatches, and receive() runs that call's access.assertCurrent() through the snapshot. The answer still reaches the worker through the same shared-memory handshake, and a failed check still revokes the operation. No host contract, SDK surface, or storage protocol changes. (Same pattern as src/gateway/session-row-projection.ts and extensions/copilot/src/tool-bridge.ts.)

Real behavior proof

Isolated Gateway built from this branch (separate state dir and config, loopback port, auth.mode: none), agent model anthropic/claude-haiku-5-5 on agentRuntime: claude-cli, plugins.slots.memory: knowledge. Turns were sent with gateway call chat.send to one direct session, waited with agent.wait, and read back with chat.history. "Before" swaps only the compiled receive() line back to the old direct call; everything else is identical.

Run Turn Knowledge tool result
before 1 knowledge_grep ok (total 0)
before 2 (same warm Claude Code process) tool invocation authority is no longer active (is_error)
after 1 knowledge_grep ok (total 0)
after 2 (same warm process) knowledge_grep ok (total 0)
after 3 (same warm process) knowledge_status ok (phase: structured-writes)

The after Gateway log shows a single cli live session start for all three turns (one warm process) and no no longer active lines.

The same failure was first seen on a long-running Gateway: knowledge_grep / knowledge_status / knowledge_diagnostics worked on the first turn after a restart and returned tool invocation authority is no longer active on every later turn of the same claude-cli session, while non-Knowledge tools (exec) kept working in those same turns.

Performance

Per storage call the fix adds one AsyncLocalStorage.snapshot() and one run through it. Micro-benchmark (Node 26.13.0, 1,000,000 calls x 7 samples, 3 warmups, median): direct check 5.1-5.3 ns/call, snapshot + run 594-611 ns/call, so about +0.6 us per storage call. For scale, a bare worker postMessage round trip on the same host measured about 5.4 us mean (20,000 trips), before any SQLite work. Not measured: end-to-end tool latency distribution.

What was not tested

  • Embedded (non-CLI) runtime and other CLI backends (Codex app-server) were not exercised live; the fix is in the plugin's worker bridge and does not depend on the runtime, but only the claude-cli path was reproduced end to end.
  • Long-running multi-agent stores (several agents sharing one store runtime concurrently) were not exercised live.
  • No autoreview pass was run locally; ClawSweeper review requested on the PR.

Evidence / Verification

  • New regression test extensions/knowledge/src/storage-worker.test.ts ("worker authority checks use each call's context, not the call that started the worker"; both worker tests now share one setup helper): host-style per-call authority in an AsyncLocalStorage; turn 1 warms the worker, turn 1 is retired, turn 2 save + lookup must succeed, and a late turn-1 call must still fail closed. Red on the base (promise rejected "Error: tool invocation authority is no longer active" instead of resolving), green with the fix.
  • pnpm exec vitest run extensions/knowledge: 62 files, 470 tests passed.
  • node scripts/run-tsgo.mjs -p tsconfig.extensions.json: clean.
  • oxfmt check on both touched files: clean.
  • scripts/check-assertion-safety-ratchet.mts and scripts/check-test-lines-ratchet.mts: OK (test-lines allocation config/test-lines-allocations/knowledge-worker-caller-context.txt, 28 lines, for the regression test).

@clawsweeper

clawsweeper Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@VACInc

VACInc commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

@clawsweeper review

@clawsweeper

clawsweeper Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 5, 2026
@clawsweeper

clawsweeper Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR runs Knowledge storage-worker authority checks in each requesting call’s async context.

Example: A Claude Code session calls knowledge_grep on its second turn using the same warm process.

  • Before: The worker checks the first turn’s retired authority and returns “tool invocation authority is no longer active.”
  • After: The worker checks the second turn’s current authority and completes the lookup.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, useful repair supported by a reported Gateway reproduction and meaningful worker-boundary regression coverage.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied isolated-Gateway results exercise the affected warm Claude Code path before and after the fix; complementary production service-and-worker tests cover second-context success, retired-caller refusal, and mid-operation revocation with no published write.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Knowledge tools stop working after the first turn of a warm Claude Code session.
Reason: The maintainer-authored change restores an existing tool contract with a narrow repair and no new configuration or public API.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it repairs a source-confirmed warm-session failure with a focused change, and no actionable patch defect was found.

Priority: P1
Reviewed head: bbcad89a06e58891d492874648f1b207007c5d2c

Before merge

None.

Findings

None.

Agent review details

How this fits together

Knowledge resolves a caller’s store and partition on the main thread, dispatches storage operations to a shared worker, and answers the worker’s live-authority checks before returning results or publishing writes.

flowchart LR
  A[Tool invocation] --> B[Knowledge principal and store]
  B --> C[Storage client with caller context]
  C --> D[Storage worker]
  D --> E[Live authority handshake]
  E --> C
  D --> F[Vault and database]
Loading

Technical review

Best possible solution:

Retain the per-call context snapshot in the existing storage client while continuing to evaluate live authority on every worker assertion.

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

The persistent listener and ambient host guard establish the failure path in source, supported by the author’s before-and-after Gateway reproduction; this review executed no target code.

Is this the best way to solve the issue?

The patch fixes the context mismatch at the existing worker bridge, preserves live receipt checks, and adds one distinct regression without introducing another authority owner.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 9f3ed313782f.

Provenance checked

  • Knowledge storage-worker authority handshake keeps the original intent (65ad180: The bundling change retained storage execution in a worker while keeping live caller authorization on the main thread; the earlier standalone origin was not established.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Focused introduced change: The pinned introduction captures one async-context snapshot per outstanding call and uses it only when answering that call’s worker authority checks; the existing handshake and rejection path remain intact. (extensions/knowledge/src/storage-client.ts:78, bbcad89a06e5)
  • Current-main failure path: Current main directly invokes access.assertCurrent inside the persistent worker’s message listener, without restoring the requesting call’s context. (extensions/knowledge/src/storage-client.ts:137, 9f3ed313782f)
  • Authority owner: The host invocation guard reads its caller assertion from AsyncLocalStorage, and that assertion invokes the retained receipt’s live authority check rather than a cached authorization result. (src/agents/agent-tool-source-execution-guard.ts:102, bbcad89a06e5)
  • Behavior and refusal coverage: The added service-and-worker regression saves and retrieves on a second context after retiring the first, then rejects a late first-context call; the existing worker regression revokes authority mid-operation and verifies no note or lookup result was published. (extensions/knowledge/src/storage-worker.test.ts:69, bbcad89a06e5)
  • Supplied Gateway reproduction: The PR reports an isolated Gateway using one warm Claude Code process: the old receive path fails on turn two, while the patched path completes Knowledge calls across three turns; it also reports the regression red before the fix and the Knowledge suite green afterward. (bbcad89a06e5)
  • Feature provenance and ownership: feat(knowledge): bundle the Knowledge memory plugin #180139 bundled the existing Knowledge implementation and retained worker-side live authority checks; subsequent history shows worker lifecycle and runtime-entry work by Josh Lehman and Peter Steinberger. Earlier standalone introduction remains unverified because the follow-history traversal reached unavailable objects. (extensions/knowledge/src/storage-client.ts:137, 65ad18005286)

Likely related people:

  • Josh Lehman: Raw commit 65ad180 adds extensions/knowledge/src/storage-client.ts:115 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 65ad18005286; files: extensions/knowledge/src/storage-client.ts)

PR surface

Source +11, Tests +46. Total +57 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 13 2 +11
Tests 1 46 0 +46
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 59 2 +57

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P1: Knowledge tools repeatedly fail after the first turn of an affected warm Claude Code session, breaking an active agent workflow.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P1: Knowledge tools repeatedly fail after the first turn of an affected warm Claude Code session, breaking an active agent workflow.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 5, 2026, 2:08 PM ET / 19:08 UTC.

@VACInc
VACInc merged commit 0ab5fde into openclaw:main Nov 5, 2026
111 of 112 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 6, 2026
…e first turn of a warm Claude Code session (openclaw#193687)

* fix(knowledge): check storage worker authority in each call's own context

* test(knowledge): share worker test setup; budget caller-context regression

---------

Co-authored-by: VACInc <[email protected]>
wangmiao0668000666 pushed a commit to wangmiao0668000666/openclaw that referenced this pull request Nov 6, 2026
…e first turn of a warm Claude Code session (openclaw#193687)

* fix(knowledge): check storage worker authority in each call's own context

* test(knowledge): share worker test setup; budget caller-context regression

---------

Co-authored-by: VACInc <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 17, 2026
…193687)

Capture each operation's async context and enter it for the existing live
authority check. Keep revoked callers rejected and worker cleanup unchanged.
Include the real service/worker regression and existing revocation control.

(cherry picked from commit 0ab5fde)

Co-authored-by: VACInc <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High-priority user-facing bug, regression, or broken workflow. plugin: knowledge proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant