Repository navigation
fix(knowledge): tools fail with "authority no longer active" after the first turn of a warm Claude Code session - #193687
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
@clawsweeper review |
|
🦞👀 Re-review progress:
|
|
Codex review: needs maintainer review before merge. What this changesThis 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.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete 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 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherKnowledge 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]
Technical reviewBest 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
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +11, Tests +46. Total +57 across 2 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. LabelsLabel changes:
Label justifications:
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 5, 2026, 2:08 PM ET / 19:08 UTC. |
…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]>
…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]>
…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]>
Summary
High Level TLDR
Fixes: Knowledge tools (
knowledge_grep,knowledge_status,knowledge_diagnostics,knowledge_remember, ...) fail withtool invocation authority is no longer activeon 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
messagelistener, 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 activeon the second and later turns of a warmclaude-clisession.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 callingcall.access.assertCurrent()insideworker.on("message", ...).access.assertCurrent()re-resolves the principal, which calls the tool context'sassertInvocationCurrent(). That host guard reads the ambient per-call source (assertPluginToolInvocationSourceCurrent()->AsyncLocalStorage.getStore(), set byrunWithPluginToolInvocationSourceat call entry) and checks its receipt (captureAgentToolSourceExecutionGuard-> "tool invocation authority is no longer active").messageevents 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 insideals.run("turn-1"), later calls under"turn-2"/"turn-3"):claude-clipath 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".Why This Change Was Made
StorageClient.callnow capturesAsyncLocalStorage.snapshot()when it dispatches, andreceive()runs that call'saccess.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 assrc/gateway/session-row-projection.tsandextensions/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 modelanthropic/claude-haiku-5-5onagentRuntime: claude-cli,plugins.slots.memory: knowledge. Turns were sent withgateway call chat.sendto one direct session, waited withagent.wait, and read back withchat.history. "Before" swaps only the compiledreceive()line back to the old direct call; everything else is identical.knowledge_grepok (total 0)tool invocation authority is no longer active(is_error)knowledge_grepok (total 0)knowledge_grepok (total 0)knowledge_statusok (phase: structured-writes)The after Gateway log shows a single
cli live session startfor all three turns (one warm process) and nono longer activelines.The same failure was first seen on a long-running Gateway:
knowledge_grep/knowledge_status/knowledge_diagnosticsworked on the first turn after a restart and returnedtool invocation authority is no longer activeon every later turn of the sameclaude-clisession, 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 workerpostMessageround 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
claude-clipath was reproduced end to end.autoreviewpass was run locally; ClawSweeper review requested on the PR.Evidence / Verification
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 anAsyncLocalStorage; 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.oxfmtcheck on both touched files: clean.scripts/check-assertion-safety-ratchet.mtsandscripts/check-test-lines-ratchet.mts: OK (test-lines allocationconfig/test-lines-allocations/knowledge-worker-caller-context.txt, 28 lines, for the regression test).