Skip to content

perf(state): keep one-shot CLI state readers between reads - #206504

Merged
obviyus merged 1 commit into
mainfrom
perf/one-shot-worker-reuse
Nov 27, 2026
Merged

obviyus merged 1 commit into
mainfrom
perf/one-shot-worker-reuse

Conversation

@obviyus

@obviyus obviyus commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

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 (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 #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.

@clawsweeper

clawsweeper Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: S labels Nov 27, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 27, 2026
@clawsweeper

clawsweeper Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

This PR reuses one-shot CLI reader workers while closing their native database resources before successful reads return.

Example: A Node openclaw agent --local turn performs several shared-state reads.

  • Before: Successful reads retire their workers; the reported macOS turn starts 18 workers before its first provider request.
  • After: Reader isolates can serve later reads after releasing database handles; the reported count falls to 16.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The implementation is focused and has no identified blocking defect, but the supplied runtime and performance evidence is incomplete.
Proof confidence 🦪 silver shellfish (2/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Repeated worker initialization adds CPU work before a one-shot local agent reaches its first provider request.
Reason: The maintainer-authored direction removes measured repeated startup work through existing infrastructure without adding settings or changing stored data.

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: b48a3eebd03304124d5640178c39c03a2f6b9351

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide redacted standalone CLI evidence showing worker reuse, native-handle release before read settlement, and clean command exit, plus the outstanding baseline/candidate performance comparison; executed red-on-base regression evidence is also absent.
Agent review details

How this fits together

The 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
Loading

Technical review

Best 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

  • One-shot reader retirement changes intended behavior with a stated reason (e77d359: Join reader cleanup before returning so release switches cannot encounter retained SQLite handles.)
  • Long-lived reader reuse keeps the original intent (fed0fe7: Reuse early Gateway readers while keeping one-shot native-resource retirement and failure cleanup.)
  • Cold-start worker ceiling keeps the original intent (b885f61: Detect additional startup fan-out with stable operation counts and lower the ceiling when a PR removes work.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Ayaan Zaidi: Raw commit e77d359 adds src/state/openclaw-state-read-worker.ts:178 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: e77d359bbf17; files: src/state/openclaw-state-read-worker.ts)
  • Peter Steinberger: Raw commit fed0fe7 adds src/state/openclaw-state-read-worker.ts:170 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: fed0fe7385cc; files: src/state/openclaw-state-read-worker.ts)

PR surface

Source +8, Tests +12. Total +20 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 9 1 +8
Tests 2 28 16 +12
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 37 17 +20

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Attach the CLI lifecycle trace and complete the comparable baseline/candidate performance measurements.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 27, 2026, 3:38 AM ET / 08:38 UTC.

@obviyus
obviyus merged commit 90154cd into main Nov 27, 2026
144 checks passed
@obviyus
obviyus deleted the perf/one-shot-worker-reuse branch November 27, 2026 09:00
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 28, 2026
…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]>
tideclaw-bot added a commit that referenced this pull request Nov 28, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S 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