Skip to content

perf(queue): memoize Bay projection and lean claimed-run reads - #1385

Merged
steipete merged 1 commit into
mainfrom
perf/queue-bay-and-claimed-runs
Sep 3, 2026
Merged

steipete merged 1 commit into
mainfrom
perf/queue-bay-and-claimed-runs

Conversation

@steipete

@steipete steipete commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Reduces read-side Durable Object CPU associated with #1372. The supplied production tail from 2026-09-03 23:16–23:18 UTC covered 106.6 seconds, approximately 61 seconds of object CPU, and 223 outer-Worker platform_overloaded logs. /lifecycle-bay accounted for 17,051 ms and /claimed-runs for 11,985 ms; /records/export contributed 9,049 ms across 640 calls and is separate work.

Streaming decoding of the raw tail found two Bay fetches (8,595 and 8,456 ms) and five claimed-run fetches (11,958, 7, 5, 6, and 9 ms). Those totals are dominated by outliers, rather than a steady 400/300 ms per request. The raw tail was deleted and is not included in this change.

Why

The Worker makes one object call per /api/durable-lifecycle-bay cache miss and already caches its sanitized response for up to 20 seconds. The object still streams, validates, counts, and samples every lifecycle projection on each miss; its existing 512-entry parsed-projection cache does not avoid that work. Bay's browser poll runs every 20 seconds, while distinct Worker cache locations can each miss independently.

/claimed-runs previously read metadata and every queue item, then Object.values() forced every lazy JSON decision and publication payload to parse. The route needs only lease identities. It had two SQL calls, not a per-lease SQL lookup; the expensive full-state transfer and parsing are removed.

What Changed

Added a repository-scoped 10-second object response memo, configurable with EXACT_REVIEW_LIFECYCLE_BAY_CACHE_MS (0 disables it). It shares concurrent reads, preserves the original snapshot timestamp and headers, and invalidates on lifecycle/state changes, mutating requests, and alarms. Only completed serialized snapshots with unchanged storage and mutation generations enter the memo. Concurrent waiters revalidate after an in-flight write, preserving the stats-memo fix already on main.

Claimed-run inventory now uses one SQL query that extracts only identity fields from leased rows and optionally filters the requested run set. Existing order, two matches per run, the 128-result bound, null attempts, generation defaults, and reconciliation fences remain intact. No claimed-run memo or incremental lifecycle materialization was needed to reach the measured targets.

OpenClaw Bay is affected only in read cost and bounded observation freshness; its public contract is unchanged. No change is needed in Bay UI or projection consumers. /api/exact-review-queue, /api/lifecycle, and Bay field sets and meanings are unchanged. No changelog, binding, migration, admission, publication, or reconciliation-fence change is included.

pr-behavior-proof

Claim and exercised surface: lower recurring CPU for the real ExactReviewQueue.fetch() Bay and claimed-run paths while preserving their projections and claim identities. Baseline: 0633e8cc1487dc9ce31d9ac24b3ab49b17c945c6. Candidate: d08c14817ca571d0ea2533697ed268c7de3892a3.

Scenario/environment: 500 lifecycle projections, 400 pending items, and 100 leased items. The opt-in Node benchmark uses real in-memory SQLite, retained synthetic publication data, 20 requests per route, and process.cpuUsage(), including response parsing. Environment: macOS arm64, Node 26.8.1. Counts instrument sql.exec; no SQL calls are mocked.

EXACT_REVIEW_CPU_BENCH=1 node --test --test-name-pattern='Bay and claimed-run CPU benchmark' test/exact-review-queue-cpu.test.ts
Route Before median CPU After median CPU Before → after SQL/call
Bay, uncached 6.256 ms 6.414 ms 3 → 3
Bay, repeated/cached 5.293 ms 0.072 ms 3 → 1
Claimed runs, all 100 leases 2.482 ms 1.173 ms 2 → 1
Claimed runs, 32 requested IDs 2.528 ms 0.927 ms 2 → 1

After maxima were 15.586 ms uncached Bay, 0.131 ms cached Bay, 2.756 ms all claims, and 1.550 ms filtered claims. All requested CPU targets were met locally.

Controlled runtime proof: both baseline and candidate were bundled into isolated local workerd instances with real SQLite Durable Objects (Miniflare 4.20260701.0), seeded with 500 synthetic projections and 100 leases, and exercised over the Worker/DO binding. Temporary proof commands:

node /tmp/bay-cpu-proof/run.mjs /tmp/bay-cpu-proof/rebased-base /tmp/bay-cpu-proof before-rebase
node /tmp/bay-cpu-proof/run.mjs "$PWD" /tmp/bay-cpu-proof after-rebase

The normalized before/after Bay and claimed-run responses matched exactly; all-claims returned 100 identities, filtered claims returned 32, and a lifecycle terminal write immediately changed the cached Bay lane count. Median HTTP wall times were 7.16 → 1.72 ms for repeated Bay and 10.81 → 5.21 ms for all claims. Workerd SQL counts independently confirmed 3 → 1 for repeated Bay and 2 → 1 for claims. These are HTTP wall timings, not Worker CPU measurements.

Artifacts/trace: local /tmp/bay-cpu-proof/evidence.json contains source and bundle SHA-256 hashes, both runtime response traces, per-route timings, SQL counts, and tail aggregates; /tmp/bay-cpu-before-rebase.txt and /tmp/bay-cpu-after-rebase.txt contain benchmark output. The temporary runtime harness and bundles are retained beside that evidence for inspection. No production data was sent to the proof runtime.

Limits: synthetic local proof does not establish production CPU, cache hit rate under write load, or the cause/removal of the long cold-request outliers. Cache invalidation is conservative; sustained writes still require fresh reads. No live workflow run, deployment, or merge was performed.

Validation: memo freshness/expiry/disablement, normalized repository scopes, queue and lifecycle invalidation, in-flight write/failure races, malformed durable projections, and lean/full equivalence across duplicates, legacy claims, expired/finalizing leases, selection, and truncation. The requested seven-suite command passes, with the signed reconciliation policy suite also passing (578 passed, two opt-in benchmarks skipped). Independent pre-commit review of the rebased candidate found no actionable P0–P2 findings.

node --test test/exact-review-queue-cpu.test.ts test/exact-review-lifecycle-bay.test.ts test/dashboard-worker-publication-lifecycle.test.ts test/dashboard-worker-observability.test.ts test/dashboard-worker-queue-runtime.test.ts test/exact-review-publication-batches.test.ts test/dashboard-worker-bay-records-routes.test.ts

Passed gates: pnpm run format, pnpm run build:all, pnpm run lint, pnpm run check:docs, pnpm run check:dashboard-queue-boundary, pnpm run check:dashboard-strict, and git diff --check. The full pnpm run check was rerun after rebasing; its result is pending. An unrelated synthetic-fixture admission test failed during the first broad run and passed an isolated retry; queue-focused suites passed throughout.

@steipete
steipete force-pushed the perf/queue-bay-and-claimed-runs branch from 102fa6b to d08c148 Compare September 3, 2026 23:37
@clawsweeper

clawsweeper Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: perf(queue): memoize Bay projection and lean claimed-run reads This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant