Repository navigation
perf(state): keep one-shot CLI state readers between reads - #206504
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: passed. What this changesThis PR reuses one-shot CLI reader workers while closing their native database resources before successful reads return. Example: A Node
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this maintainer-authored PR open: it removes repeated reader startup through the existing lifecycle owner, and the reviewed hunks introduce no definite correctness defect. Reviewed head: Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherThe shared-state read transport dispatches admitted CLI queries to worker pools, returns decoded results after cleanup, and closes the pools through the state-database lifecycle. flowchart TD
A[One-shot CLI command] --> B[State read transport]
B --> C[Existing reader pool]
C --> D[Shared-state query]
D --> E[Native resource cleanup]
E --> B
F[CLI command cleanup] --> C
Technical reviewBest possible solution: Retain this focused reuse of the existing reader and cleanup owners, supported by completed CLI performance and native-release evidence. Do we have a high-confidence way to reproduce the issue? Current-main source retires the reader after each successful one-shot query, establishing repeated startup work; the reported measurements were not independently rerun during this read-only review. Is this the best way to solve the issue? The change targets unnecessary isolate retirement at its existing owner while preserving joined native cleanup, error retirement, runtime-specific rotation, and command shutdown. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 0a8e37b7d5cc. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +8, Tests +12. Total +20 across 3 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:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
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 27, 2026, 3:38 AM ET / 08:38 UTC. |
…206504) ## Problem A one-shot CLI (`openclaw agent --local`) boots a fresh shared-state reader worker for every state read: since openclaw#204868, each successful one-shot read retires its worker so that no SQLite handle outlives the read (Windows `EPERM` on a release switch). The handle release is required; the worker exit is not. Each boot re-imports the reader's module graph (~90-880 modules, ~140 ms CPU per boot measured with per-thread `--cpu-prof`). ## Change `src/state/openclaw-state-read-worker.ts`: in the `cli-oneshot` role, a successful read no longer retires its reader. It closes the task, then joins `closeReadResources(pool)` before the result returns: - Node (`explicitSqliteCloseReleasesNativeResources`): native handles close in place and the isolate stays for the next read. - Runtimes without explicit native close: `closeReadResources` rotates the pool, which joins worker exit, as before. The kept reader exits when the command closes shared state at CLI cleanup (`withCliCommandCleanup` resets the state-database lifecycle, which closes the read pools). Other roles are unchanged. The openclaw#206466 gate budget drops from 19 to 17 worker threads before the first request. ## Guarantee kept No SQLite handle outlives a successful one-shot read: the result still settles only after the native release completes. `src/state/openclaw-state-read-source.test.ts`: - `releases CLI reader handles before a successful read returns and keeps the reader`: replaces the per-read exit assertion. The read result stays pending until the handle release settles, the task closes without retiring, one pool is created for four reads, and `closeOpenClawStateDatabaseAsync()` (the CLI cleanup path) closes that pool. - `uses the cli-oneshot process lifetime for early awaited reads`: tasks close without retirement in `cli-oneshot`. ## Evidence - Worker threads started before the first provider request, `src/cli/agent-local-cold-start.process.test.ts` (Node, macOS arm64): 18 -> 16. Linux CI was 19 before; the budget is 17. - Per-thread CPU profile of one fake-provider turn (3 runs, median): 4 state-read worker boots per turn at 137 ms boot CPU each; this change removes 2 of them in the measured turn (one stays, admission reads keep their worker). - Tests: the 26 test files under `src/state/openclaw-state-read*` plus every importer of `openclaw-state-read-worker.js`, `src/cli/agent-local-cold-start.process.test.ts`, `src/cli/agent-local-rpc-cleanup.process.test.ts`, `src/cli/one-shot-exit.cleanup.process.test.ts` pass locally. - Wall-time ABAB is owed: host load stayed above 20 during this work; it will be appended to the W4 status at load < 10. Co-authored-by: Ayaan Zaidi <[email protected]>
Source commit: 90154cd Source PR: #206504 Release adaptation: Preserve stable's queued startup admission and cancellation checks. Release native handles before returning a successful one-shot read; keep the isolate until CLI cleanup. The main-only agent cold-start process fixture is absent, so verify release ordering and final pool teardown in the stable state reader test. Co-authored-by: RomneyDa <[email protected]>
Problem
A one-shot CLI (
openclaw agent --local) boots a fresh shared-state reader worker for every state read: since #204868, each successful one-shot read retires its worker so that no SQLite handle outlives the read (WindowsEPERMon a release switch). The handle release is required; the worker exit is not. Each boot re-imports the reader's module graph (~90-880 modules, ~140 ms CPU per boot measured with per-thread--cpu-prof).Change
src/state/openclaw-state-read-worker.ts: in thecli-oneshotrole, a successful read no longer retires its reader. It closes the task, then joinscloseReadResources(pool)before the result returns:explicitSqliteCloseReleasesNativeResources): native handles close in place and the isolate stays for the next read.closeReadResourcesrotates the pool, which joins worker exit, as before.The kept reader exits when the command closes shared state at CLI cleanup (
withCliCommandCleanupresets the state-database lifecycle, which closes the read pools). Other roles are unchanged.The #206466 gate budget drops from 19 to 17 worker threads before the first request.
Guarantee kept
No SQLite handle outlives a successful one-shot read: the result still settles only after the native release completes.
src/state/openclaw-state-read-source.test.ts:releases CLI reader handles before a successful read returns and keeps the reader: replaces the per-read exit assertion. The read result stays pending until the handle release settles, the task closes without retiring, one pool is created for four reads, andcloseOpenClawStateDatabaseAsync()(the CLI cleanup path) closes that pool.uses the cli-oneshot process lifetime for early awaited reads: tasks close without retirement incli-oneshot.Evidence
src/cli/agent-local-cold-start.process.test.ts(Node, macOS arm64): 18 -> 16. Linux CI was 19 before; the budget is 17.src/state/openclaw-state-read*plus every importer ofopenclaw-state-read-worker.js,src/cli/agent-local-cold-start.process.test.ts,src/cli/agent-local-rpc-cleanup.process.test.ts,src/cli/one-shot-exit.cleanup.process.test.tspass locally.