Skip to content

refactor(gateway): consolidate chat abort authorization - #119642

Merged
steipete merged 1 commit into
mainfrom
codex/loc-0805-gateway-dmvazq
Aug 5, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/loc-0805-gateway-dmvazq

Conversation

@steipete

@steipete steipete commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Gateway chat cancellation maintained separate authorization and lifecycle paths for active, queued, and preregistered runs, making connection/device ownership and agent-scoped session checks harder to keep consistent.

Why This Change Was Made

Route active and queued cancellation through one canonical owner-level authorization decision, reuse the lifecycle-owned queue map and pending ownership metadata, and remove duplicate exact-run validators, obsolete wrappers, and synthetic pending-run controllers. The shared target explicitly models ChatAbortControllerEntry | QueuedChatTurnEntry, preserving the existing queue-entry production contract.

User Impact

No intended behavior change. Administrator privileges, same-device reconnect and connection ownership, ownerless same-session access with fail-closed cross-session rejection, selected global-agent isolation, canonical/session aliases, hidden/internal/side-run exclusion, partial persistence, and queued-before-active cancellation order remain unchanged. Production change: 69 additions, 180 deletions; net -111 lines across four files.

Evidence

  • Trusted Blacksmith Testbox tbx_01kz92dt9nvd6bh3xpj8d4bagx: 19 gateway test files and 341 tests passed, including authorization (32), abort persistence (28), selected-agent scope (29), queued turns (14), chat abort (46), full gateway chat integration (96), and sibling ownership/dispatch coverage.
  • Full, unfiltered pnpm check:changed passed: production and full-tree dead-export scans, public Plugin SDK API manifest, core and core-test TypeScript checks, formatting, lint with zero warnings/errors, import-cycle checks, and security guards.
  • Fresh independent structured review: .agents/skills/autoreview/scripts/autoreview --mode local --engine codex --model gpt-5.6-sol --thinking xhigh passed with no actionable findings and 0.97 confidence.
  • Remote proof: https://github.com/openclaw/openclaw/actions/runs/31011294284. The sole Testbox lease was stopped and independently verified completed.

Exact-head redacted Gateway client trace

Observed by executing the committed chat.abort RPC handler, authorization owner, cancellation runtime, active-run abort owner, and queued-turn abort owner at 8d1afa5395b821e3013f558100e08d6c7c11ff07. The fixture uses synthetic client/device IDs and isolated in-memory session registries; it does not start a listener or access the network, credentials, or operator state.

[
  {
    "scenario": "same-connection-active",
    "request": {
      "sessionKey": "main",
      "connection": "conn-owner",
      "device": "device-other",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": true,
      "aborted": true,
      "runIds": [
        "same-connection-active"
      ]
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": false,
      "signalAborted": true
    }
  },
  {
    "scenario": "same-device-reconnected-active",
    "request": {
      "sessionKey": "main",
      "connection": "conn-new",
      "device": "device-owner",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": true,
      "aborted": true,
      "runIds": [
        "same-device-reconnected-active"
      ]
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": false,
      "signalAborted": true
    }
  },
  {
    "scenario": "foreign-active",
    "request": {
      "sessionKey": "main",
      "connection": "conn-foreign",
      "device": "device-foreign",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": false,
      "code": "INVALID_REQUEST",
      "error": "unauthorized"
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": true,
      "signalAborted": false
    }
  },
  {
    "scenario": "ownerless-cross-session-active",
    "request": {
      "sessionKey": "other",
      "connection": "conn-foreign",
      "device": "device-foreign",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": false,
      "code": "INVALID_REQUEST",
      "error": "runId does not match sessionKey"
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": true,
      "signalAborted": false
    }
  },
  {
    "scenario": "same-device-reconnected-queued",
    "request": {
      "sessionKey": "main",
      "connection": "conn-new",
      "device": "device-owner",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": true,
      "aborted": true,
      "runIds": [
        "same-device-reconnected-queued"
      ]
    },
    "stateAfter": {
      "registry": "queued",
      "runRegistered": false,
      "signalAborted": true
    }
  },
  {
    "scenario": "foreign-queued",
    "request": {
      "sessionKey": "main",
      "connection": "conn-foreign",
      "device": "device-foreign",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": false,
      "code": "INVALID_REQUEST",
      "error": "unauthorized"
    },
    "stateAfter": {
      "registry": "queued",
      "runRegistered": true,
      "signalAborted": false
    }
  },
  {
    "scenario": "ownerless-cross-session-queued",
    "request": {
      "sessionKey": "other",
      "connection": "conn-foreign",
      "device": "device-foreign",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": false,
      "code": "INVALID_REQUEST",
      "error": "runId does not match sessionKey"
    },
    "stateAfter": {
      "registry": "queued",
      "runRegistered": true,
      "signalAborted": false
    }
  },
  {
    "scenario": "admin-cross-session-active",
    "request": {
      "sessionKey": "other",
      "connection": "conn-admin",
      "device": "device-admin",
      "scopes": [
        "operator.admin"
      ]
    },
    "response": {
      "ok": true,
      "aborted": true,
      "runIds": [
        "admin-cross-session-active"
      ]
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": false,
      "signalAborted": true
    }
  },
  {
    "scenario": "global-agent-mismatch",
    "request": {
      "sessionKey": "global",
      "connection": "conn-owner",
      "device": "device-owner",
      "scopes": [
        "operator.write"
      ]
    },
    "response": {
      "ok": false,
      "code": "INVALID_REQUEST",
      "error": "runId does not match agentId"
    },
    "stateAfter": {
      "registry": "active",
      "runRegistered": true,
      "signalAborted": false
    }
  }
]

Foreign and ownerless cross-session requests leave their original active/queued run registered and unaborted; the same connection, a reconnected paired device, and the authorized administrator cancel successfully. The global-session agent mismatch remains rejected. This adds observed evidence to the PR body only: the reviewed commit SHA is unchanged, so the existing exact-head review remains valid and no re-review is requested.

Genuine production Gateway listener + GatewayClient WebSocket verdict (exact head)

The existing setupGatewaySessionsTestHarness started the actual production startGatewayServer listener on isolated loopback port 63000. Three independent production GatewayClient instances completed real hello-ok WebSocket handshakes (protocol 4); they then sent six actual chat.abort RPCs over their WebSocket connections to the committed production listener, request dispatch, authorization owner, runtime owner, and queued-turn owner. The fixture used only synthetic isolated temporary session state; the real listener, network transport, Gateway clients, RPC dispatch, authorization, and cancellation handlers were not mocked. A passive in-memory runtime observer inspected the real Gateway-owned maps, and queued entries were registered through production registerQueuedChatTurn.

{
  "head": "8d1afa5395b821e3013f558100e08d6c7c11ff07",
  "productionClient": "src/gateway/client.ts:GatewayClient",
  "productionListener": "src/gateway/server-start.ts:startGatewayServer",
  "productionOwners": [
    "src/gateway/server-methods/chat-abort-handler.ts",
    "src/gateway/server-methods/chat-abort-authorization.ts",
    "src/gateway/server-methods/chat-abort-runtime.ts",
    "src/gateway/chat-queued-turns.ts"
  ],
  "transport": {
    "scheme": "ws",
    "host": "127.0.0.1",
    "listenerPort": 63000,
    "connectedProductionGatewayClients": 3,
    "hello": [
      "hello-ok",
      "hello-ok",
      "hello-ok"
    ],
    "protocol": 4,
    "websocketRequests": 6
  },
  "state": "synthetic isolated temporary Gateway session state; no operator data",
  "cases": [
    {
      "kind": "queued",
      "requester": "foreign",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "main",
        "runId": "synthetic-owned-queued-turn"
      },
      "productionClientResponse": {
        "ok": false,
        "errorClass": "GatewayClientRequestError",
        "code": "INVALID_REQUEST",
        "message": "unauthorized"
      },
      "serverStateAfterResponse": {
        "remainsRegistered": true,
        "signalAborted": false
      }
    },
    {
      "kind": "queued",
      "requester": "owner",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "main",
        "runId": "synthetic-owned-queued-turn"
      },
      "productionClientResponse": {
        "ok": true,
        "payload": {
          "ok": true,
          "aborted": true,
          "runIds": [
            "synthetic-owned-queued-turn"
          ]
        }
      },
      "serverStateAfterResponse": {
        "remainsRegistered": false,
        "signalAborted": true
      }
    },
    {
      "kind": "queued",
      "requester": "foreign",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "other",
        "runId": "synthetic-ownerless-queued-turn"
      },
      "productionClientResponse": {
        "ok": false,
        "errorClass": "GatewayClientRequestError",
        "code": "INVALID_REQUEST",
        "message": "runId does not match sessionKey"
      },
      "serverStateAfterResponse": {
        "remainsRegistered": true,
        "signalAborted": false
      }
    },
    {
      "kind": "queued",
      "requester": "admin",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "other",
        "runId": "synthetic-ownerless-queued-turn"
      },
      "productionClientResponse": {
        "ok": true,
        "payload": {
          "ok": true,
          "aborted": true,
          "runIds": [
            "synthetic-ownerless-queued-turn"
          ]
        }
      },
      "serverStateAfterResponse": {
        "remainsRegistered": false,
        "signalAborted": true
      }
    },
    {
      "kind": "active",
      "requester": "foreign",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "main",
        "runId": "synthetic-owned-active-turn"
      },
      "productionClientResponse": {
        "ok": false,
        "errorClass": "GatewayClientRequestError",
        "code": "INVALID_REQUEST",
        "message": "unauthorized"
      },
      "serverStateAfterResponse": {
        "remainsRegistered": true,
        "signalAborted": false
      }
    },
    {
      "kind": "active",
      "requester": "owner",
      "websocketRequest": {
        "method": "chat.abort",
        "sessionKey": "main",
        "runId": "synthetic-owned-active-turn"
      },
      "productionClientResponse": {
        "ok": true,
        "payload": {
          "ok": true,
          "aborted": true,
          "runIds": [
            "synthetic-owned-active-turn"
          ]
        }
      },
      "serverStateAfterResponse": {
        "remainsRegistered": true,
        "signalAborted": true,
        "registrationLifecycle": "terminal-persistence-pending"
      }
    }
  ]
}

Observed outcomes: a foreign non-admin client cannot cancel another connection's queued or active run; both remain registered and unaborted. The owning connection successfully cancels both. An ownerless queued run rejects a foreign cross-session request and remains queued; an admin can explicitly authorize its cross-session cancellation. The successful active cancellation intentionally retains its aborted registration while terminal persistence is pending, preserving the existing lifecycle-ordering contract.

Focused real-listener result: 1 passed, 7 unrelated existing cases skipped by the explicit name filter. All production clients and the isolated listener were shut down; the temporary dependency link was removed and the source worktree remained clean. Reviewed head remains 8d1afa5395b821e3013f558100e08d6c7c11ff07; this is additional exact-head evidence only, so the existing exact-head review remains valid and no re-review is requested.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: M maintainer Maintainer-authored PR labels Aug 5, 2026
@steipete
steipete marked this pull request as ready for review August 5, 2026 14:05
@steipete
steipete requested a review from a team as a code owner August 5, 2026 14:05
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. labels Aug 5, 2026
@clawsweeper

clawsweeper Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 5, 2026, 12:11 PM ET / 16:11 UTC.

ClawSweeper review

What this changes

This PR routes active and queued Gateway chat cancellations through one shared authorization decision and removes duplicate queue-map wrappers and validators.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

No blocking findings. Current main still has the split authorization paths, while this PR retains the existing access rules through one canonical check and supplies exact-head production WebSocket evidence. Likely related people: steipete (high) and Masato Hoshino (medium).

Priority: P3
Reviewed head: 8d1afa5395b821e3013f558100e08d6c7c11ff07

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused net-negative refactor with no actionable correctness finding, green supplied validation, and strong transport-boundary proof.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.
Evidence reviewed 6 items Canonical authorization: The exact PR head applies one authorization helper to both queued and active exact-run cancellation after preserving cross-session owner matching and global-agent scope checks.
Owner-match contract: The shared helper permits admins, matching device or connection owners, and ownerless same-session runs; cross-session callers request the stricter owner-match mode.
Sibling queue path: Session-wide queued-turn selection now uses the same owner predicate and retains queued-before-active cancellation ordering.
Findings None None.
Security None None.

How this fits together

Gateway clients send chat-abort requests to the server, which checks session, agent, connection, and device ownership before cancelling active or queued work. The result controls whether the requested chat run is stopped or rejected without changing its state.

flowchart LR
A[Gateway client] --> B[Chat abort request]
B --> C[Shared authorization]
C --> D{Authorized target?}
D -->|Yes| E[Active or queued run]
D -->|No| F[Rejected request]
E --> G[Cancellation result]
Loading

Before merge

  • Complete next step (P2) - No mechanical repair is indicated; retain this PR for normal exact-head maintainer merge gating.
Agent review details

Security

None.

PR surface

Source -111. Total -111 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 4 69 180 -111
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 69 180 -111

Review metrics

Metric Value Why it matters
Production vs test LOC production +69/-180 (net -111); tests +0/-0 The refactor reduces duplicate production authorization code without adding a parallel behavior path.

Technical review

Best possible solution:

Keep one authorization owner for active and queued targets, then merge through the normal exact-head checks once GitHub’s clean merge result is available.

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

Not applicable as a reported defect: this is a behavior-preserving refactor, and the PR supplies an exact-head production GatewayClient/WebSocket trace for the preserved authorization outcomes.

Is this the best way to solve the issue?

Yes. The shared owner-level decision removes duplicate active and queued policies while retaining the stricter cross-session check at the authorization boundary.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 21bc2c43f2a5.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P3: This is a focused, behavior-preserving Gateway cleanup with strong existing and transport-boundary validation.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides exact-head, redacted production-listener and GatewayClient WebSocket output with observed authorization and post-cancellation state for active and queued runs.

Evidence

What I checked:

Likely related people:

  • steipete: Historical Gateway abort and queued-turn refactors establish sustained ownership of the authorization and lifecycle surface. (role: feature owner and recent area contributor; confidence: high; commits: 0d2db46956dc, a857c4430049, 8d1afa5395b8; files: src/gateway/server-methods/chat-abort-authorization.ts, src/gateway/server-methods/chat-abort-handler.ts, src/gateway/chat-queued-turns.ts)
  • Masato Hoshino: Recent work on session steering and active-run interruption overlaps the abort handler, authorization, and runtime boundary. (role: recent adjacent Gateway lifecycle contributor; confidence: medium; commits: 53326b9e9a81; files: src/gateway/server-methods/chat-abort-handler.ts, src/gateway/server-methods/chat-abort-runtime.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-08-05T14:11:28.845Z sha 8d1afa5 :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-05T14:26:27.807Z sha 8d1afa5 :: needs real behavior proof before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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 rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 5, 2026
@steipete
steipete merged commit c2215d0 into main Aug 5, 2026
214 checks passed
@steipete
steipete deleted the codex/loc-0805-gateway-dmvazq branch August 5, 2026 16:15
@steipete

steipete commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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