Repository navigation
perf(queue): memoize Bay projection and lean claimed-run reads - #1385
Merged
Merged
Conversation
steipete
force-pushed
the
perf/queue-bay-and-claimed-runs
branch
from
September 3, 2026 23:37
102fa6b to
d08c148
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_overloadedlogs./lifecycle-bayaccounted for 17,051 ms and/claimed-runsfor 11,985 ms;/records/exportcontributed 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-baycache 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-runspreviously read metadata and every queue item, thenObject.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(0disables 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 instrumentsql.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.tsAfter 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-rebaseThe 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.jsoncontains source and bundle SHA-256 hashes, both runtime response traces, per-route timings, SQL counts, and tail aggregates;/tmp/bay-cpu-before-rebase.txtand/tmp/bay-cpu-after-rebase.txtcontain 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.
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, andgit diff --check. The fullpnpm run checkwas 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.