Repository navigation
refactor(gateway): move worker transcript replay off the main thread - #163594
Conversation
Route ledger begin, terminal completion, and exact discard through the existing shared-state broker and placement mutation owner. Preserve live grants, native receipt settlement, ordered replay, and fresh-claim rollback cleanup. Await pending and terminal persistence around the existing transcript application. Also repair the inherited async session-manager fixture in worker claim recovery. Validation covers 100 focused tests, zero caller-thread ledger SQL, Gateway runtime build, type/lint guards, zero cycles, and P2 review. The inherited env-name budget remains unchanged.
|
🦞👀 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. |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Codex review: needs maintainer review before merge. Reviewed October 2, 2026, 2:30 PM ET / 18:30 UTC (Revision 5). ClawSweeper reviewWhat this changesMoves worker transcript replay bookkeeping into the existing SQLite worker broker and updates callers and recovery fixtures to await persistence. Merge readiness✅ Ready for maintainer review The central refactor remains necessary, and no actionable introduced defect was found. The resolved head addresses the previous conflict blocker while preserving live authority checks and durable receipt ordering. Priority: P2 Review scores
Verification
How this fits togetherWorkers submit ordered transcript batches to the Gateway. Its replay ledger prevents duplicate application and records durable outcomes before acknowledgments return to workers. flowchart TD
A[Worker transcript batch] --> B[Gateway session queue]
B --> C[Live authority checks]
C --> D[Shared state worker ledger]
D --> E[Agent transcript writer]
E --> F[Durable terminal receipt]
F --> G[Worker acknowledgment]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +167, Tests +301, Docs +13, Other 0. Total +481 across 19 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one worker-backed replay ledger with captured store identity, live commit admission, and durable receipts governing transcript application and acknowledgment. Do we have a high-confidence way to reproduce the issue? Not applicable to a new product bug: current main and the latest release visibly execute replay-ledger SQL synchronously, establishing the refactor's starting condition. Is this the best way to solve the issue? Yes. Reusing the existing shared-state broker and placement mutation owner preserves a single persistence authority and avoids a competing writer or new storage contract. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 0bf122da27a6. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
Preserve both launcher fixture fixes: await session-manager opening and asynchronous transcript append. The integrated candidate passed all 100 focused tests. Production conflict resolution is mechanical; reuse the approved P2 review.
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Capture the Gateway transcript storage environment before the in-process worker changes process.env. Keep the production RPC deadline so cold worker admission does not inject extra timeout faults into exact replay-count tests. Production code, test deadlines, and assertions are unchanged.
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Keep main’s Gateway storage-environment binding in the shared worker fixture. Retain the ledger worker import and the production RPC deadline. Verified the resolved tree with 123 focused tests on Blacksmith Testbox.
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
…penclaw#163594) * refactor(gateway): move transcript replay ledger off thread Route ledger begin, terminal completion, and exact discard through the existing shared-state broker and placement mutation owner. Preserve live grants, native receipt settlement, ordered replay, and fresh-claim rollback cleanup. Await pending and terminal persistence around the existing transcript application. Also repair the inherited async session-manager fixture in worker claim recovery. Validation covers 100 focused tests, zero caller-thread ledger SQL, Gateway runtime build, type/lint guards, zero cycles, and P2 review. The inherited env-name budget remains unchanged. * test(worker): retain harness storage identity across worker startup Capture the Gateway transcript storage environment before the in-process worker changes process.env. Keep the production RPC deadline so cold worker admission does not inject extra timeout faults into exact replay-count tests. Production code, test deadlines, and assertions are unchanged.
What Problem This Solves
Worker transcript replay-ledger begin, terminal completion, and exact discard still ran SQLite on the Gateway main thread. This moves the eight ledger SQL sites into the existing shared-state worker broker.
User Impact
Internal execution change: ledger SQL leaves the Gateway thread while transcript replay, ordering, and live revocation retain their existing behavior. No schema, stored format, retention, durability, released SDK, or update contract changes. No operator action is required.
Why This Change Was Made
Typed
placementTranscript.begin,.complete, and.discardoperations use the existing placement mutation owner and registry. The committer awaits pending persistence before transcript effects and terminal persistence before replying. Captured inputs preserve the original environment, epoch, sequence, request hash, and store identity. Lost replies use acknowledged native receipts; unknown outcomes keep pending recovery. Only a fresh claim may be discarded after a known rollback, and database close joins accepted work.The pre-existing short authority reads remain live at transaction and commit admission. The overbroad landing-only assertion against all host SQL was removed; the PR's regression still proves that ledger SQL runs off the caller thread. No authority check was weakened.
Released SDK audit against
v2026.9.7: the changed ledger factory and methods are internal-only. Current and releasedsrc/plugin-sdkexports,src/plugins/runtime, and documented SDK subpaths do not expose them. No compatibility boundary or public type test is needed.Evidence
d3d7dcb05290d087ccdd2d37eb87f1323e757bab. Later authored changes are test fixtures and mechanical main integration; no production redesign was introduced.pnpm buildpassed at5ce2d2d6573dfc79b52f777c0f1251a901b6421cin 18m39.09s, including runtime bundles, runtime/public SDK declarations, export checks, and Control UI production bundling. This completed the full-build request from that head's ClawSweeper review. It is prior-head evidence.d20ba7392c1de16a0eb9dab79336ade5ebf996a3, the scopedqaRuntimebuild passed in 14m30.76s. It covers the changed worker admission and registration inputs.git diff --checkpasses on the resolved merge.d20ba7392c1de16a0eb9dab79336ade5ebf996a3: 477 → 477, no added or removed names. Main carries the owner-approved 477 budget; this PR does not change it. The standalone env-count gate passes 477/477 on the final merge commit. The earlier 478/476 failure is historical.node scripts/run-vitest.mjs <eight focused files> --maxWorkers=1. Test wrapper wall: 207.89s; Vitest shard durations: 136.07s + 55.27s = 191.34s. Providerblacksmith-testbox, leasetbx_01m3yx0mjxfbpq45sc1ez2drb4, run 37045755991. The receiver verified staged treebae33c2bc2135f253aab2725f26aed34312efd08plus key ledger/runtime/fixture/native-registration blob hashes. Provider result: exit 0, lease stopped and independently confirmed completed. The local wrapper was subsequently killed with signal 9 during local capsule cleanup (exit 137); all its children are gone. This does not change the completed remote test result, and no test rerun was performed. Exact-head PR CI remains a separate landing gate.The suites use real agent/shared-state workers for their integration contracts. No test timeout or assertion was weakened. The existing lazy-loading suite resets module generations to exercise admission before runtime loading.
CI fixture repair and main integration
Final merge-resolution commit:
efdd370171149b9fc4faed3f1b480d8972c8b320; its tree exactly matches the 123-test remote proof. This is a merge commit with main, not a rebase.CI run
37025678680tested head5ce2d2d6573dfc79b52f777c0f1251a901b6421cand failed in the chat-abort and fault-injection fixtures. The in-process worker changesOPENCLAW_STATE_DIRafter the Gateway creates its agent owner; an unbound transcript target then selects the worker namespace and correctly fails ownership admission. The same error reproduced on exact CI main parentb1c09f1088e0fba7b91c661d3b20b945c96ab141. The full ordered fault suite on that baseline produced 10 failures and 5 passes, so that evidence was not used to waive all 15 CI failures.Main independently fixed the storage binding in #163637 (
d20ba7392c1de16a0eb9dab79336ade5ebf996a3). This merge takes main'sgatewayStorageEnvironmentimplementation and removes the duplicate fixture fix from5ab3c24ce009a2fbe7aa1ea0c8f3931c1475f72b. The remaining fixture delta is the ledger import and removal of an artificial two-second RPC deadline, retaining the existing production 30-second deadline. Diagnostics observed unintended timeouts at 2000.8, 2002.0, and 2001.9 ms in addition to the deliberately injected socket close. Exact replay counts, immutable payloads, single provider invocation, and durable transcript uniqueness remain asserted.Before this final main merge, all 15 fault-injection cases and 7 chat-abort cases passed together. One first-use chat startup exceeded its unchanged 10-second budget under concurrent typechecking before any ledger request; the same case passed unchanged after typechecking settled, and all 8 chat cases had passed in the earlier binding-fix run. This is evidence of startup timing sensitivity, not a claimed repair of it. No inherited-failure admin exception has been used.
Authority-read follow-up debt
The eight observed SQL events below remain follow-up debt. The recorder logs both
prepareand execution, so the two ownership observations at each grant are one query, not two independent reads: eight observations represent six executions.PRAGMA data_versionexecutionsrc/infra/sqlite-schema-facts.ts:296src/state/openclaw-state-ownership.ts:126state_key = ? LIMIT 1src/state/openclaw-state-ownership.ts:126session_id = ?src/gateway/worker-environments/placement-row-codec.ts:122viaplacement-turn-claims.ts:463PRAGMA data_versionexecutionsrc/infra/sqlite-schema-facts.ts:296src/state/openclaw-state-ownership.ts:126state_key = ? LIMIT 1src/state/openclaw-state-ownership.ts:126session_id = ?src/gateway/worker-environments/placement-row-codec.ts:122viaplacement-turn-claims.ts:463These calls use the existing warm native handle (
placement-store.ts:105,openclaw-state-db.ts:274).config_machine_state.state_keyandworker_session_placements.session_idare primary keys; both SELECTs are indexed point lookups, and the pragma is constant-size. The ownership reader callsinspectOpenClawStateOwnershipFromDatabasedirectly with admitted schema facts, not the path-based/read-only inspection helpers. None of the eight observations enters maintenance-scoped current/read-only state admission, creates a snapshot subprocess, scans a table, or waits for writer admission. Existing lifecycle/maintenance and external-ownership revocation checks remain intentional live authority gates. WAL allows these reads while the worker holds its write transaction.Separately retained source-authority debt:
worker-turn-transcript-target.ts:28checks the bound session through its existing session owner;placement-turn-claim-events.ts:211rechecks the live run/claim/operator capability. These are not additional ledger SQL sites.