Repository navigation
fix(sessions): agents with small transcript rebuild backlogs wait behind larger agents at startup - #165207
Conversation
The transcript reconcile pool runs one whole-agent task per pass on a single worker in FIFO order, so an agent with one pending projection rebuild waited for every earlier agent's full backlog before it could serve history. Admission is now owned by the reconcile pool: planning tasks are ordered by pending session backlog (operation order breaks ties), and a running planner yields at a completed session boundary only to a strictly smaller waiting backlog, reserving its own place before releasing the worker. Large stores never interleave, total work is unchanged, MAX_WORKERS stays 1, and lease release tasks bypass admission. Startup still awaits each agent's complete rebuild.
|
🦞👀 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: needs changes before merge. Reviewed October 4, 2026, 9:58 PM ET / October 5, 2026, 01:58 UTC (Revision 3). ClawSweeper reviewWhat this changesPrioritize smaller transcript-index rebuild backlogs and let larger rebuilds yield between sessions while preserving startup completion and lease cleanup. Merge readiness⛔ Needs changes before merge - 2 items remain This remains useful: current main and the latest release lack backlog-aware admission. The runtime measurements support the fix, but the prior P3 test finding remains unresolved. Priority: P1 Review scores
Verification
How this fits togetherTranscript reconciliation rebuilds searchable history from each agent’s stored conversations. Gateway startup waits for these rebuilds before making the affected agents available. flowchart TD
A[Agent conversation stores] --> B[Count pending rebuilds]
B --> C[Backlog admission queue]
C --> D[Single rebuild worker]
D --> E[Publish completed session index]
E --> F{Smaller backlog waiting?}
F -->|Yes| C
F -->|No| D
E --> G[Completed agent startup]
Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +212, Tests +286, Docs +7. Total +505 across 8 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one pool owner for backlog scheduling, with complete rebuild settlement and deterministic admission-boundary regression coverage. Do we have a high-confidence way to reproduce the issue? Yes: current main’s single-worker whole-agent queue establishes the blocking mechanism, and the supplied before/after run exercises competing real rebuild backlogs. This review did not execute a reproduction. Is this the best way to solve the issue? Yes: scheduling belongs in the existing pool, and yielding at completed-session boundaries preserves bounded publication and cleanup. The regression test still needs deterministic admission gates. Full review comments:
Overall correctness: patch is correct AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against ff1c79a1f101. LabelsLabel changes: No label changes. Label justifications:
EvidenceAcceptance criteria:
What I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (2 earlier review cycles) |
…site The database-worker ratchet counts SQL call sites in non-worker modules. Reuse the pending-session listing inside the publication worker preflight instead of adding a COUNT helper to session-transcript-index.ts.
|
Merged via squash.
|
…ind larger agents at startup (openclaw#165207) * fix(sessions): admit smaller transcript rebuild backlogs first The transcript reconcile pool runs one whole-agent task per pass on a single worker in FIFO order, so an agent with one pending projection rebuild waited for every earlier agent's full backlog before it could serve history. Admission is now owned by the reconcile pool: planning tasks are ordered by pending session backlog (operation order breaks ties), and a running planner yields at a completed session boundary only to a strictly smaller waiting backlog, reserving its own place before releasing the worker. Large stores never interleave, total work is unchanged, MAX_WORKERS stays 1, and lease release tasks bypass admission. Startup still awaits each agent's complete rebuild. * fix(sessions): count reconcile backlog without a new main-thread SQL site The database-worker ratchet counts SQL call sites in non-worker modules. Reuse the pending-session listing inside the publication worker preflight instead of adding a COUNT helper to session-transcript-index.ts.
Related: #165107
What Problem This Solves
Fixes: after a Gateway restart, an agent with only a few pending transcript-projection rebuilds stays unavailable until every earlier agent's whole rebuild backlog has finished, because all agents share one reconcile worker that runs each agent's backlog to completion in arrival order.
User Impact
User impact: agents with small rebuild backlogs become available after at most one in-flight session rebuild instead of waiting behind larger agents. Large agents still rebuild one at a time, never interleaved, so the first large agent is not delayed by the second. Total rebuild work and worker count are unchanged. No config, schema, or stored-data changes.
Why This Change Was Made
#165107 described the two large stores' rebuilds as interleaving inside the single-worker pool. Measured on
main, they do not. Each reconcile pass is one whole-agent worker task, and the pool runs those tasks strictly FIFO. With backlogs large-a=4, large-b=4, small=1 submitted in that order, plan order isa:s0..s3, b:s0..s3, small:s0. So the cost #165107 observed is enqueue order. The real defect is head-of-line blocking: a one-session agent waits for every earlier large backlog.The reconcile pool now owns admission for planning tasks:
MAX_WORKERSstays 1. Raising it would not help here: on a CPU-bound host, two concurrent large rebuilds just delay both, and each extra worker costs up to 512 MB of heap on large fleets. Lease-release tasks bypass admission, so cleanup can never wait behind planners. No deadlines were added.The lease-settlement tail moved unchanged from
session-transcript-reconcile.tsinto the pool module (which already owns lease release) to stay under the file-size lint limit.Known tradeoff: shortest-backlog-first lets a continuous stream of smaller backlogs postpone a large store. Those small backlogs are real pending rebuilds, and the large store resumes as soon as they drain. No starvation deadline was added, by design.
Evidence
Regression test
src/config/sessions/session-transcript-reconcile.backlog-order.test.ts(real reconcile worker, observed ports). It holds large-a's firstplan-finish, queues large-b(3) and small(1), then asserts plan ordera:s0, small:s0, a:s1, a:s2, a:s3, b:s0, b:s1, b:s2and completion order small, large-a, large-b. It also checks that every store ends fully indexed and the pool ends idle. A second case checks that a revoked waiter leaves the queue without leaking the permit. On the unmodified code the first case fails as expected:pnpm test src/config/sessions/session-transcript-reconcile.backlog-order.test.ts --maxWorkers=1: 2 passed, 14.0 s in tests (160 s wall, including Vitest startup on a loaded Mac).Neighbours: 19 files / 152 tests passed: reconcile pool,
session-transcript-reconcile.*, transcript index, and startup session migration suites. Core and test typechecks, scoped oxlint, oxfmt, andgit diff --checkare clean. Codex autoreview (P2 threshold): scoped-clean.Before/after measurement (Crabbox direct AWS
c7a.4xlarge, leasecbx_b73c0ff6081c, runrun_76e6a3496a913c645e83b0898a02d664). Both arms ran on the same box from a fresh checkout of this PR. The "before" arm swapped in the five base production files froma58fe52a98b5. The benchmark itself is proof-only and not committed. It used isolated temp state and the real reconcile worker, pinned to 2 cores withtaskset -c 0,1. Each round has two stores with 48 sessions × 120 messages and one store with 2 sessions, all markedneeds_rebuild. Both large reconciles start at t=0; the small one starts at t=250 ms. Values are completion times in ms from t=0 over 3 rounds. Testbox was queued with no runner for 15+ minutes, so I fell back to AWS.On base, the small agent always waited for large-a's entire backlog. It slipped ahead of large-b only because large-b's preflight finished later. With this PR it runs after at most one in-flight large-a session; most of its remaining 1.5 s is its own preflight competing for the two pinned cores. The large stores still complete one after the other, never interleaved. The +128 ms total is the cost of one yield and resumed pass (re-open, lease, re-list) plus the count preflight. Each yield costs a similar fixed amount. On base, the small agent's wait grows with the large backlog; with this PR it is bounded by a single session rebuild. The synthetic stores here are small (about 2.4 s each), so production-sized stores like the 1.6 GB ones in #165107 should show a proportionally larger gain for small agents and the same fixed per-yield overhead.