Skip to content

getSubagentRunsSnapshotForRead does an uncached full SQLite re-read on every call; many consumers drive it to ~22 Hz and pin a CPU core #91517

Closed

Description

@saphoroth

In OpenClaw 2026.6.5-beta.2 (and observed on 2026.6.1, repro on clean install with no hotfixes), the gateway main thread is pinned at ~95–100% CPU on most operations, including trivial CLI commands (e.g. openclaw cron list would time out at 30s) and the gateway's own /health endpoint (10s timeout).

perf record profiles show the hot native frame is node::sqlite::StatementSync::All in a tight JS loop. A V8 sampling profile (CDP capture, 25.36s, 22,940 samples) shows the JS caller is loadSubagentRegistryFromSqliteOnly at dist/subagent-registry-state-DxLh3f1M.js:872, called ~22×/sec from getSubagentRunsSnapshotForRead (line 929) which does loadSubagentRegistryFromSqlite() on every invocation — no cache, no interval, no rate limit. The query it issues is SELECT * FROM subagent_runs ORDER BY created_at ASC, run_id ASC (full table scan, no WHERE/LIMIT). Many read-side consumers (delivery, completion, route, registry-read) hit getSubagentRunsSnapshotForRead ~22 times per second, pinning a core.

Top 10 self-time frames = 96.99% of CPU, all in this read path:

  • all (V8 native sqlite binding) — 30.60% / 7,761ms / 6,960 hits
  • parseJson (subagent-registry-state:705) — 20.73% / 5,257ms / 4,898 hits
  • executeCompiledSqliteQuerySync (openclaw-state-db:135) — 18.40% / 4,666ms / 4,283 hits
  • rowToSubagentRunRecord (subagent-registry-state:743) — 11.69% / 2,964ms / 2,757 hits
  • mergeDeliveryState (subagent-registry-state:444) — 4.53%
  • loadSubagentRegistryFromSqliteOnly (the hot leaf) — 2.34% / 553 hits
  • (garbage collector) — 2.29%
  • normalizeSubagentRunState — 2.65%
  • createDeliveryFromTypedColumns — 2.13%
  • normalizeDeliveryContext (delivery-context.shared:25) — 1.62%

Pruning the table from 984 → 498 rows (49% reduction) only reduced the read-path CPU from 97% → 77% — the call rate is the real cost driver, not table size.

Suggested fix: memoize the snapshot returned by getSubagentRunsSnapshotForRead. The snapshot only changes on subagent run start/end (which go through saveSubagentRegistryToSqlite, in the same module). Either:

  • a 500ms–1s TTL cache (simple, ≤1s staleness for non-write consumers), OR
  • event-driven invalidation: invalidate the cache inside saveSubagentRegistryToSqlite after each write (keeps read-after-own-write exact).

We applied a 500ms TTL + write-invalidation in our local build and saw CPU drop from 102% to 4% (a 25× reduction); trivial CLI commands went from 30s timeout to 1.5s, and /health went from 10s timeout to 0.7ms. The patch is reversible + dist-ephemeral; we'll carry it until upstream lands.

Sibling of CLOSED #75707 (its fix didn't cover this read path).

V8 profile: /tmp/openclaw-cpuprofile-kiz-1165-2026-06-08T16-46-35-513Z.cpuprofile (22,940 samples, 134 nodes).

Activity

  1. clawsweeper commented on Jun 8, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 8, 2026, 6:31 PM ET / 22:31 UTC.

    Summary
    Keep open: current main and the latest release still read the full persisted subagent run table from the production snapshot path, so the reported CPU hotspot remains actionable and source-supported.

    Reproducibility: yes. from source inspection: current main calls loadSubagentRegistryFromSqlite() on each production getSubagentRunsSnapshotForRead call, and that loader synchronously reads all subagent_runs. I did not run a live perf reproduction in this read-only review.

    Next step
    This is a narrow, source-backed performance repair with clear files and validation; no product or config decision is required.

    Review details

    Best possible solution:

    Add a bounded process-local cache for persisted subagent registry snapshots with local write invalidation, short cross-process staleness bounds, and focused tests for read reuse and freshness.

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

    Yes from source inspection: current main calls loadSubagentRegistryFromSqlite() on each production getSubagentRunsSnapshotForRead call, and that loader synchronously reads all subagent_runs. I did not run a live perf reproduction in this read-only review.

    Is this the best way to solve the issue?

    Yes: a small process-local snapshot cache with local write invalidation and a short staleness bound is the narrowest maintainable fix for the repeated-read driver. A pure event-invalidation cache would be too narrow because other processes can write the SQLite rows.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The fix should preserve cross-process visibility; write-only invalidation is not enough unless a short TTL or equivalent staleness bound refreshes snapshots written by other processes.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against c967172f694e.

    Label changes

    Label changes:

    • add P1: The report describes a current gateway availability problem where high CPU causes health checks and ordinary CLI operations to time out.
    • add impact:crash-loop: The issue affects process availability by pinning gateway CPU and causing health checks and CLI commands to hang or time out.
    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.
    • add clawsweeper:queueable-fix: Current issue advisory state selects this label.
    • add clawsweeper:fix-shape-clear: Current issue advisory state selects this label.

    Label justifications:

    • P1: The report describes a current gateway availability problem where high CPU causes health checks and ordinary CLI operations to time out.
    • impact:crash-loop: The issue affects process availability by pinning gateway CPU and causing health checks and CLI commands to hang or time out.
    Evidence reviewed

    Acceptance criteria:

    • node scripts/run-vitest.mjs src/agents/subagent-registry.persistence.test.ts
    • node scripts/run-vitest.mjs src/agents/subagent-registry.announce-loop-guard.test.ts
    • node scripts/run-vitest.mjs src/agents/subagent-registry.store.sqlite.test.ts

    What I checked:

    Likely related people:

    • steipete: Recent subagent registry refactors and query helper extraction appear in the central file history. (role: recent area contributor; confidence: medium; commits: ca3e5ffd892a, 3b2db583cdb1; files: src/agents/subagent-registry.ts)
    • Takhoffman: Multiple March commits changed subagent registry cleanup, stale row handling, and activity checks in the same feature area. (role: adjacent subagent cleanup contributor; confidence: medium; commits: c90ae1ee7f63, b8a0258618c1; files: src/agents/subagent-registry.ts)
    • vincentkoc: The latest release tag and database-first history include the current subagent SQLite persistence surface. (role: release and SQLite state adjacent contributor; confidence: low; commits: 2e08f0f4221f; files: src/agents/subagent-registry.store.sqlite.ts, src/state/openclaw-state-schema.sql)
    How this review workflow works
    • 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.
  2. added
    P1High-priority user-facing bug, regression, or broken workflow.
    impact:crash-loopCrash, hang, restart loop, or process-level availability failure.
    issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.
    clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.
    clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.
    clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.
    on Jun 8, 2026
  3. self-assigned this
    on Jun 9, 2026
  4. vincentkoc commented on Jun 9, 2026

    @vincentkoc
    Member

    Fixed on main in c4a0ca0.

    Behavior changed: getSubagentRunsSnapshotForRead now reuses the persisted subagent registry snapshot for a short bounded window, refreshes the local cache after successful writes, and still overlays in-memory runs on every read. Cross-process updates are bounded by the TTL instead of forcing every hot read through a full synchronous SQLite table scan.

    Proof run:

    • node scripts/run-vitest.mjs src/agents/subagent-registry-state.test.ts src/agents/subagent-registry.persistence.test.ts src/agents/subagent-registry.announce-loop-guard.test.ts src/agents/subagent-registry.store.sqlite.test.ts src/agents/subagent-registry.test.ts — 1 shard, 5 files, 88 tests passed.
    • .agents/skills/autoreview/scripts/autoreview --mode local --base origin/main — clean; no accepted/actionable findings.
    • Testbox through Crabbox: provider blacksmith-testbox, id tbx_01ktmz2dftmr76xn9cajamvjvh, Actions run 27177469692, command corepack pnpm check:changed, exit 0.

    Closing as fixed.

  5. locked as resolved and limited conversation to collaborators on Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:crash-loopCrash, hang, restart loop, or process-level availability failure.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions