Repository navigation
perf(sessions): defer transcript index rebuilds to the reconcile worker - #129182
Conversation
Transcript replace, exact rewrite, text repair, and message-cut no longer rebuild the full FTS/active-event projection inside their synchronous write transactions. They mark the projection dirty and schedule the existing chunked worker-owned reconcile after commit via the post-commit publication seam; search already hides needs_rebuild sessions and mirror-facts falls back to raw rows while the projection lags. Doctor import and canonical repair keep synchronous reconciliation. A/B probe: compaction replace of a 60k-event transcript blocked the event loop 3197ms before, 764ms after, with the projection converging off-thread.
Replace now defers the projection rebuild, so the ordered-turn setup must wait for the reconcile worker before committed cursors are provable; the invalidation fallback for unproven cursors keeps its own coverage.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: found issues before merge. Reviewed August 25, 2026, 6:23 AM ET / 10:23 UTC. ClawSweeper reviewWhat this changesThis PR defers large transcript projection rebuilds to the reconciliation worker while keeping small-session rebuilds synchronous. Merge readinessKeep open: the large-transcript path still excludes the interactive fork action, leaving synchronous projection indexing inside its gateway write transaction. Priority: P2 Review scores
Verification
Live VerificationCommand: Result: PASS (completed) Assertions:
How this fits togetherSession mutations store canonical transcript events and maintain derived search/history projections. Gateway handlers use those projections for session actions and readers either serve current data or return a retryable rebuilding state. flowchart LR
A[Gateway session action] --> B[Transcript mutation]
B --> C{Transcript size gate}
C -->|small| D[Rebuild projection in transaction]
C -->|large| E[Mark projection dirty]
E --> F[Post-commit reconcile worker]
D --> G[History and search readers]
F --> G
Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +106, Tests +91. Total +197 across 14 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Apply the existing dirty-marker and post-commit reconciliation flow to oversized forks as well, then prove the temporary rebuilding state and eventual projection convergence. Do we have a high-confidence way to reproduce the issue? Yes: a fork action whose active-path prefix exceeds the configured byte or row threshold reaches the explicit fork exclusion and still forward-indexes every copied event in its write transaction. Is this the best way to solve the issue? No: the current size gate is a plausible fix for rewinds and switches but not the best complete fix because it omits the sibling fork action that uses the same transcript-copy owner. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 23382e77b5ee. LabelsLabel changes:
Label justifications:
EvidenceWhat 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
History |
Small transcripts (<=4k events and <=4MiB) keep the original synchronous in-transaction rebuild (~250ms bound, no reader dirty window); only large transcripts defer to the reconcile worker where the FTS retokenization cost would stall the event loop. Probe: 60k-event replace stalls 639ms (deferred) vs 3197ms before; 2k-event replace is 95ms with search current immediately.
… reads Oversized chat.history fixtures take the deferred rebuild path, so the shared transcript seeding helper awaits projection convergence, and the archive-worker suite settles the reconcile worker before removing its temp dir (Windows cannot unlink the still-open agent DB).
The reconcile promise previously settled via fire-and-forget terminate, letting waiters delete the database file while the worker thread still held its SQLite handle open (Windows unlink fails with EBUSY). Settle now finishes through worker.terminate()'s exit promise on every path.
|
Merged via squash.
|
…er (openclaw#129182) * perf(sessions): defer transcript index rebuilds to the reconcile worker Transcript replace, exact rewrite, text repair, and message-cut no longer rebuild the full FTS/active-event projection inside their synchronous write transactions. They mark the projection dirty and schedule the existing chunked worker-owned reconcile after commit via the post-commit publication seam; search already hides needs_rebuild sessions and mirror-facts falls back to raw rows while the projection lags. Doctor import and canonical repair keep synchronous reconciliation. A/B probe: compaction replace of a 60k-event transcript blocked the event loop 3197ms before, 764ms after, with the projection converging off-thread. * test(sessions): await projection convergence before proving turn cursors Replace now defers the projection rebuild, so the ordered-turn setup must wait for the reconcile worker before committed cursors are provable; the invalidation fallback for unproven cursors keeps its own coverage. * perf(sessions): size-gate synchronous transcript index rebuilds Small transcripts (<=4k events and <=4MiB) keep the original synchronous in-transaction rebuild (~250ms bound, no reader dirty window); only large transcripts defer to the reconcile worker where the FTS retokenization cost would stall the event loop. Probe: 60k-event replace stalls 639ms (deferred) vs 3197ms before; 2k-event replace is 95ms with search current immediately. * test(sessions): settle deferred reconcile before teardown and history reads Oversized chat.history fixtures take the deferred rebuild path, so the shared transcript seeding helper awaits projection convergence, and the archive-worker suite settles the reconcile worker before removing its temp dir (Windows cannot unlink the still-open agent DB). * fix(test): satisfy agent database options in archive-worker teardown * fix(sessions): resolve reconcile only after the worker thread exits The reconcile promise previously settled via fire-and-forget terminate, letting waiters delete the database file while the worker thread still held its SQLite handle open (Windows unlink fails with EBUSY). Settle now finishes through worker.terminate()'s exit promise on every path.
… the session (#130930) * fix(agents): wait for deferred transcript projection before reopening the session After #129182, compaction replace on transcripts past the sync-rebuild gate (4,000 rows / 4 MiB) defers the projection rebuild to the reconcile worker. The overflow-recovery retry reopens the session immediately and the bounded open fails closed with "Session transcript projection is rebuilding", so the turn dies right after a successful compaction. The attempt session open now settles its own session's projection first. * fix(agents): scope projection waits to owned retries * fix(agents): exclude no-op transcript retries * fix(agents): consume projection retry ownership * test(agents): prove owned projection retry ordering * fix(agents): make projection retry wait abortable --------- Co-authored-by: VACInc <[email protected]>
… the session (openclaw#130930) * fix(agents): wait for deferred transcript projection before reopening the session After openclaw#129182, compaction replace on transcripts past the sync-rebuild gate (4,000 rows / 4 MiB) defers the projection rebuild to the reconcile worker. The overflow-recovery retry reopens the session immediately and the bounded open fails closed with "Session transcript projection is rebuilding", so the turn dies right after a successful compaction. The attempt session open now settles its own session's projection first. * fix(agents): scope projection waits to owned retries * fix(agents): exclude no-op transcript retries * fix(agents): consume projection retry ownership * test(agents): prove owned projection retry ordering * fix(agents): make projection retry wait abortable --------- Co-authored-by: VACInc <[email protected]>
What Problem This Solves
Follow-up to #129135. The remaining whole-gateway stall source on busy hosts: transcript replacement/rewrite paths rebuilt the full FTS + active-event projection inside their synchronous SQLite write transactions on the gateway main thread (
reconcileSessionTranscriptIndexInTransaction, src/config/sessions/session-transcript-index.ts). FTS5 deletion retokenizes every removed row, so compacting or repairing a large session blocked the event loop for O(transcript bytes) — the best match for the ~11s synchronized-stall windows observed live on the team host.The chunked, claim-guarded off-thread rebuild machinery already existed (
session-transcript-reconcile.worker.ts+session-transcript-projection-rebuild.ts, header: "request paths may only schedule it and return a bounded retryable response") — the write paths just never adopted it. Consumers are already lag-tolerant: history readers throw the retryableSessionTranscriptProjectionUnavailableError(#129114 contract) which gateway handlers map to retryable responses, FTS search schedules the worker for dirty sessions and reportsindexing=true, and transcript mirror-facts fall back to raw rows.Why This Change Was Made
Transcript replace (compaction), exact rewrite, text repair, and message-cut are now size-gated: transcripts within
SYNC_REBUILD_MAX_ROWS = 4_000/SYNC_REBUILD_MAX_BYTES = 4 MiBkeep the original synchronous in-transaction rebuild (measured60ms per 1k events/1MiB, so ≤250ms — no reader dirty window, semantics identical to today for the vast majority of sessions). Larger transcripts mark the projection dirty in-transaction (markSessionTranscriptIndexDirtyInTransaction, which also invalidates in-flight worker claims) and schedule the worker-owned reconcile after commit through the existingdeferOpenClawAgentPostCommitPublicationseam — discarded on rollback, with durableneeds_rebuildmaking a missed kick recoverable by startup/search reconciliation. Old FTS rows are preserved on the deferred path (the dirty watermark hides them from search) instead of paying the synchronous O(bytes) FTS mass-delete. Doctor import and canonical repair intentionally stay synchronous (maintenance contexts that must finish with a complete projection; commented in place).Accepted tradeoff on the deferred (large-transcript) path only: reads during the ~1–2s convergence window get the designed retryable/
indexing=trueresponses, and the first appended turn publishes the cursor-invalidation fallback. Small sessions see no behavior change at all.User Impact
Compaction and transcript repair of large sessions no longer freeze every session on the gateway. Small-session behavior is unchanged. No schema, worker-protocol, or config changes.
Evidence
A/B stall probe through the real accessor path (
replaceTranscriptEventswith compaction-shaped replacement + event-loop lag sampler):indexing=true, and after off-thread convergence (~1.6s) FTS search returns the correct post-compaction rows.indexing=false— no dirty window.Tests: full
src/config/sessionssuite green (86 files, 947 tests), plus the gateway suites that exercise replace-then-read seeding:session-transcript-readers(.markers),server.chat-history-cursor,server.chat.gateway-server-chat,chat.directive-tags(312 tests) andui/src/pages/chat/chat-pane-terminal.test.ts. New regressions: deferred path forced via an over-4MiB event proves search dirty-window exclusion + convergence; an oversized-rewind regression covers message-cut's deferred path; the deferred-FTS regression was confirmed failing on pre-fix code.pnpm check:changedgreen (import cycles, database-first, storage guards). Codex autoreview clean.Production LOC +109 net vs main (tests +64): the growth is the size gate + post-commit scheduling seam, justified by the bounded-latency capability (stall budget enforced at the projection owner) while preserving small-session synchronous semantics.