Repository navigation
refactor(gateway): check agent runtime authority once at request start - #201697
Conversation
- Check run authority one time in authorizeGatewayRequestPreDispatch, shared by WebSocket and in-process requests; the refusal names the next step. - Delete the mid-request re-checks: createAgentRuntimeAuthorityGuard, assertActiveAgentRuntimeAuthority, hasActiveAgentRuntimeAuthority, the dispatch and response-time re-checks, and the 4001 close on runtime authority. - Keep caller, participant, session-mutation and client-authority assertions. - Move the startup-agent helpers to request-authorization-startup.ts (line cap). - Delete tests that pinned refusal after the run's authority closed mid-request; add a regression that a started request still gets its response.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
…admitted request - withSessionTurnAuthority re-authorizes session access mid-request (for example sessions.files.get before publishing a host read). Pass admitted: true so the run-authority check stays at request start only.
|
Codex review: needs changes before merge. What this changesThis PR checks agent runtime authority at Gateway request admission and removes repeated checks during handlers and response delivery. Example: An agent's Gateway request starts while authorized, then its run ends before the response arrives.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 4 items remain Keep open: this maintainer-authored PR follows the approved admission-only direction, but it loses a structured caller-authority refusal and leaves several consumer tests inconsistent with the change. Priority: P2 Before merge
Findings
Tests
Agent review detailsHow this fits togetherGateway dispatch receives authenticated WebSocket and in-process requests, authorizes their principals, invokes method handlers, and returns results or structured refusals. flowchart TD
A[WebSocket request] --> C[Shared request admission]
B[In-process request] --> C
C --> D[Runtime authority check]
D --> E[Method handler]
E --> F[Response delivery]
Technical reviewBest possible solution: Keep shared runtime admission and the removed rechecks, restore typed caller-refusal handling, and complete the affected consumer-test cutover. Do we have a high-confidence way to reproduce the issue? Current-main source shows repeated runtime checks during handling and response delivery; the PR's dispatcher regression exercises the changed contract, but this review ran no reproduction. Is this the best way to solve the issue? The shared admission owner is the smallest coherent solution, and moving existing startup helpers preserves their ownership without adding state or a parallel authorization path. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 0c3f2a4ea2b3. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
PR surfaceSource -231, Tests -1056, Other -1. Total -1288 across 51 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 19, 2026, 2:47 AM ET / 07:47 UTC. |
- Remove the slack test's reset of the deleted afterGatewayMirror hook. - Teams gateway reads no longer fence on a run or turn closing mid-read; keep the plugin-retirement stages there and all stages on the tool route. - Prune the session-identity allowlist after the deleted guards.
- The Gateway already retires a run's desktop execution on run closure (gatewayComputerService.revokeRunAuthority). Delete the tool's second cleanup path, bindGatewayComputerCleanup, which sent computer.invoke with the ended run's identity and is now refused at request start. - The gateway host treats the run-ended close as done; live-run and paired-node closes are unchanged.
- Restore the caller-only message-action authorize() (session mutation, action and delivery assertions) at the same points as before, without the run check; its refusal says message action authority, not agent runtime authority. - Keep the structured INVALID_REQUEST when client authority closes during agent routing. - Restore turn-capability cases in the channel read-authority suites; drop only cases that release the run's own authority mid-request. - Narrow cron, requester-cron, sessions.create and role tests that pinned mid-request run revocation; the role test now proves the next request uses the new role. - Move the poll handler unchanged to send-poll.ts (line cap); prune baselines.
…he run check - Move the turn-token check out of the agent runtime validator into isMessageActionTurnCurrent; the validator calls it after its run checks. - Message actions assert the caller's turn first, so a revoked turn still blocks a pending ordinary channel read or download from returning or caching a result. - Remove the Slack and Teams fixture guards so those suites exercise production; send fixtures mint real turn capabilities.
…untime-guard # Conflicts: # src/agents/tools/sessions-list-privacy.test.ts
- Keep the message-action turn assertion in server-methods/agent-runtime-authority.ts with its base refusal text; message actions compose it first. No run check. - isMessageActionTurnCurrent takes a structural identity, breaking the import cycle with agent-runtime-identity-token. - Drop the cron scratch row that called the handler past request start; the start check is covered in request-authorization tests. Relax a CLI wording assertion.
openclaw#201697) Problem: an agent run's Gateway requests re-checked the run's authority many times while one request ran: before the queue, at handler start, inside cron, secrets, sessions.create, message, themes, ui.command and MCP event handlers, and on every response. When the run's authority closed partway through, a started request was refused with "agent runtime authority is no longer active". On WebSocket the client connection was also closed (4001), and a committed effect could be reported as a failure. Fix: run authority is checked once, in the shared pre-dispatch owner `authorizeGatewayRequestPreDispatch`, which WebSocket and in-process requests both pass through. A request from a run that already ended is refused with a message that names the next step. Session authorization inside an already admitted request (`withSessionTurnAuthority`) skips that check. The run guard helpers, the dispatch and response-time re-checks, and the 4001 close are deleted. The message-action turn-capability check and the caller-only message-action fences stay as on base, without the run check; they are separate check-once chains. Gateway computer cleanup after a run ends now has one owner: the Gateway retires the run's desktop execution on run closure, and the tool no longer sends a second close with the ended run's identity. Caller, participant, session-mutation and client-authority assertions are unchanged. User impact: a Gateway request that an agent run has started finishes and returns its real result even if the run's authority closes meanwhile. A new request from a closed run is refused at start. Proof: focused tests for every touched handler and consumer, including the channel read-authority suites; new regressions (a started request keeps its response; an ended run is refused at start but not inside an admitted request); tsgo and test types; the lifetime-check, line-cap, session-identity and mock-export ratchets; a local Gateway run where a subagent's Code Mode crash is followed by a successful Gateway-backed tool call; ocx build --dts plus a packed install smoke on the merged head. Co-authored-by: Ayaan Zaidi <[email protected]>
#201697) (cherry picked from commit 26a5f73) Release adaptation: Keep existing stable admission and read-result fences; the authenticated dispatcher only stops replacing outcomes of started write handlers already governed by the method layer. Companion audit: No direct later PR/SHA follow-up. Do not import the broad runtime/request authorization redesign or its changed blanket read-response contract. Co-authored-by: RomneyDa <[email protected]>
What problem this solves
An agent run's Gateway requests re-checked the run's authority many times while one request ran. The checks ran before the queue, at handler start, inside handlers (cron, secrets,
sessions.create, message send, themes,ui.command, MCP events) and again on every response. When the run's authority closed partway through, a request that had already started was refused withagent runtime authority is no longer active. On the WebSocket path the Gateway also closed the whole client connection (4001). An effect that had already committed could then be reported as a failure.The repo's rule (AGENTS.md, "Choose good compromises") is to check authority one time, when work starts. Revocation applies to the next request, and a started request may finish.
Change
authorizeGatewayRequestPreDispatch. Both WebSocket and in-process requests pass through it. A request whose run authority is already closed when it starts is refused (INVALID_REQUEST) with a message that names the next step: "This agent run has ended, so it cannot start new Gateway requests. Start a new turn to retry." No production code matched on the old text.authorizeGatewayRequestPreDispatchis also called mid-request bywithSessionTurnAuthority, for example whensessions.files.getre-authorizes before publishing a host read. That call passesadmitted: true, so the run check runs only at request start. The other callers each start a request or a tool operation, and they keep the check.createAgentRuntimeAuthorityGuard,assertActiveAgentRuntimeAuthority,hasActiveAgentRuntimeAuthority, the dispatch-level re-checks, the response-time re-checks and the 4001 connection close on runtime authority.resolveSessionRuntimeAuthoritykeeps its caller and participant assertions. Those are separate chains with their own PRs. It no longer checks runtime authority.gatewayComputerService.revokeRunAuthority,server-core-runtime.ts). The tool also sentcomputer.invoke__close_executionwith the ended run's identity through a captured binding (bindGatewayComputerCleanup). That binding is deleted. The gateway host treats therun-endedclose as done, because the Gateway owns it and an ended run cannot start new requests. Closes during a live run, and paired-node closes, are unchanged.isMessageActionTurnCurrent(one owner). The run validator still calls it.server-methods/agent-runtime-authority.tskeeps one assertion that message actions compose first, with the base refusal text. A revoked turn still stops a pending channel read, download or send from returning or caching a result. That re-check is its own check-once chain.authorize()(session mutation, action and delivery assertions) stays at the same points as on base. Its run check is gone.pollmoved unchanged tosend-poll.ts, and the startup-agent helpers moved unchanged torequest-authorization-startup.ts(line caps).User impact
A Gateway request that an agent run has started now finishes and returns its real result, even if the run's authority closes meanwhile. A new request from a closed run is still refused when it starts.
Evidence
Proof tier: source (
ocx test,ocx src).send.test.ts70,matrix-channel-read-authority13,server.agent-runtime-authority-proof13.pnpm tsgo: pass.pnpm check:lifetime-check-ratchet: pass, with no per-file growth. Format check: clean.ocx src, scripted model, Code Mode on): a parent spawns a subagent and ends its turn. The child's Code Mode guest crashes while a shellexecis still running, and the child then calls a Gateway-backed tool (sessions_search). The call succeeds, and no refusal is logged.retains reset across parent closure) was checked before deletion: plugin host cleanup still runs on/resetwith the matching session key. The test failed only at its no-delivery-after-revoke assertion. One earlier run of that case ended before its pause point. That did not reproduce in a logged rerun.Unchanged on purpose (separate check-once PRs): the message-action turn-capability re-check, the exec and plugin approval checks (
exec-approval.ts,nodes.invoke-authority.ts), the caller and participant assertions (resolveSessionRuntimeAuthoritykeeps them), and client authority (hasCurrentClientAuthority).