Repository navigation
perf(state): check state ownership once per read boundary - #159876
Conversation
|
🦞👀 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. |
|
Codex review: needs maintainer review before merge. Reviewed September 27, 2026, 8:41 PM ET / September 28, 2026, 00:41 UTC (Revision 3). ClawSweeper reviewWhat this changesThe branch reduces repeated synchronous ownership checks during Gateway state reads and adds regression coverage for path aliases, maintenance admission, and worker dispatch. Merge readiness✅ Ready for maintainer review Current main still repeats the ownership checks this PR targets. The proposed read path retains admission before native retention, at worker dispatch, and when accepting the result; this review found no concrete introduced defect. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway reads shared SQLite state for requests such as Control UI session loading. It checks access, retains the read source, sends work to a SQLite worker, and validates authority before using the result. flowchart LR
A[Control UI session request] --> B[Gateway state read]
B --> C[Ownership admission]
C --> D[Read source retention]
D --> E[Worker queue]
E --> F[Dispatch authority check]
F --> G[SQLite read worker]
G --> H[Result authority check]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +29, Tests +180. Total +209 across 9 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep admission at synchronous read retention and preserve fresh checks before worker I/O and result acceptance, with the alias and maintenance regressions guarding those boundaries. Do we have a high-confidence way to reproduce the issue? Yes. Current main visibly repeats ownership checks across read preparation and transport, and the PR body gives a seeded warm-reload measurement; this read-only review did not run that scenario. Is this the best way to solve the issue? Yes. The patch removes checks repeated within synchronous spans while retaining fresh checks across the worker queue and after the reply. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 86c4bf83e699. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (2 earlier review cycles)
|
94386bf to
80cda31
Compare
Admit native read retention once before finding and retaining its handle. Keep that admitted step private to the synchronous retainer entry points. Let the retainer own the initial database access check and the read transport own queued dispatch admission, removing repeated checks in read preparation and the transport's synchronous wrapper. Build the Unix lock layout from the freshly canonical database ancestor, without realpathing that ancestor again. Still resolve the final lock directory because tmp and its child may be links. Windows layout is unchanged. Revalidate after snapshot preparation and queue waits, before worker dispatch, and after worker replies and cleanup. Preserve worker message receive and grant checks because domain callbacks can revoke authority, generic actor checks for direct callers, native SQLite admission, fresh alias resolution, and path-based verifyStillHeld. No cross-await cache, dependency, configuration, schema, or update change. Proof: 931 tests passed across 60 files, with two platform skips; core and core test typechecks, CI oxlint on touched files, formatting, and diff checks passed. The four changed test files also passed with one worker (100 tests, 57.84s wall; new cases 0.264s combined). Count regressions fail on the original code. Alias retargeting, linked lock directories, missing suffixes, and message-hop refusal retain their outcomes. Independent Codex review is clean through P2. Matched warm scratch-harness runs on base 42e53a4 reduced main-thread realpathSync.native calls from 2570 to 1300 (49.4%) and openSync calls from 726 to 452 (37.7%). Runtime JavaScript and Control UI builds passed. This is syscall-count evidence on macOS/Node, not a CPU-time or cross-platform claim.
80cda31 to
f5866a5
Compare
|
Merged via squash.
|
…aw#159876) Admit native read retention once before finding and retaining its handle. Keep that admitted step private to the synchronous retainer entry points. Let the retainer own the initial database access check and the read transport own queued dispatch admission, removing repeated checks in read preparation and the transport's synchronous wrapper. Build the Unix lock layout from the freshly canonical database ancestor, without realpathing that ancestor again. Still resolve the final lock directory because tmp and its child may be links. Windows layout is unchanged. Revalidate after snapshot preparation and queue waits, before worker dispatch, and after worker replies and cleanup. Preserve worker message receive and grant checks because domain callbacks can revoke authority, generic actor checks for direct callers, native SQLite admission, fresh alias resolution, and path-based verifyStillHeld. No cross-await cache, dependency, configuration, schema, or update change. Proof: 931 tests passed across 60 files, with two platform skips; core and core test typechecks, CI oxlint on touched files, formatting, and diff checks passed. The four changed test files also passed with one worker (100 tests, 57.84s wall; new cases 0.264s combined). Count regressions fail on the original code. Alias retargeting, linked lock directories, missing suffixes, and message-hop refusal retain their outcomes. Independent Codex review is clean through P2. Matched warm scratch-harness runs on base 05eb3a8 reduced main-thread realpathSync.native calls from 2570 to 1300 (49.4%) and openSync calls from 726 to 452 (37.7%). Runtime JavaScript and Control UI builds passed. This is syscall-count evidence on macOS/Node, not a CPU-time or cross-platform claim.
What Problem This Solves
Fixes: every shared-state read on the Gateway main thread repeated the full state-ownership check several times, so loading a Control UI session spent a large share of main-thread time in synchronous
realpathand sidecar-lockopencalls.User Impact
Session loading and other state reads block the Gateway event loop less. On a warm Gateway, one Control UI reload of a long session now makes 1,300 synchronous
realpathcalls instead of 2,570 (−49%) and 452 sidecar-lock opens instead of 726 (−38%). Ownership semantics, error messages, and maintenance/lease outcomes are unchanged.Why This Change Was Made
A warm Gateway CPU profile across one reload and three session switches attributed about 25% of main-thread busy time to synchronous
realpathandopenSyncunderassertStateDatabaseAccessAllowed(). Counting calls by caller showed about 780 checks per reload for about 128 state reads, so each read passed the full check (three realpaths plus one sidecar open) about six times. Several of those checks ran back to back in the same synchronous span, with no awaited work or side effect between them.Caching the owner path was ruled out: access checks must keep selecting the owner of the current canonical target, so retargeting a state-directory alias during maintenance still refuses ordinary access and the old lease. This change keeps per-check freshness and removes only checks that repeat a boundary already checked in the same span.
openclaw-state-db-borrow.ts: the retainer admits once (maintenance admission + owner check) before finding the cached handle and passes that admission, synchronously, into retaining the same handle. The publicretainentry still admits for itself.openclaw-state-db-readonly.tsandopenclaw-state-read-worker.ts: read preparation no longer repeats the owner check that the retainer (or, for snapshot reads, an explicit check) just made, and the transport's synchronous wrapper no longer checks before building the request. The pool's dispatch callback still checks current authority right before the worker receives the request, including after a queue wait, and again after the reply. Checks after snapshot preparation and before retaining snapshot temp directories remain.resolveGatewayStateOwnerPathbuilds the Unix lock directory from the state directory derived from the freshly canonical database path, which is already canonical because it is an ancestor, instead of realpathing it again (resolveGatewayLockDirForCanonicalStateDir). The final lock-directory resolution stays, becausetmpand its child may be links. The Windows layout is unchanged.Kept on purpose: fresh alias resolution per check, path-based
verifyStillHeld(), worker message receive/grant checks (intervening callbacks can revoke authority), generic actor preparation checks for direct callers, and native SQLite admission.Evidence
pnpm tsgo:core,pnpm tsgo:core:test, CI oxlint on the touched files, and independent Codex P2 review.realpathSync.native/openSyncby caller. Same seeded state (1,500-turn session + 30 × 60-turn sessions), one warm-up reload, then one measured reload:realpath2,570 → 1,300,openSync726 → 452. This is macOS count evidence, not a CPU-time or cross-platform claim.Per-file cost: the four changed test files together take 57.8 s wall with
--maxWorkers=1on a heavily loaded host (100 tests); the new cases add 0.26 s combined. CI seconds will be added from the first run.Update behavior: none. No config, state, schema, lock layout, or service lifecycle change.