Repository navigation
perf(acp): maintain ledger byte budgets at insert time - #104739
Conversation
estimateSqliteLedgerBytes ran a full SUM(length(...)) scan over every replay event on every append, and once the 16MB budget was hit the trim loop rescanned after each single-row delete (#100622). Event rows now carry estimated_bytes computed at insert, session rows keep a running footprint aggregate, budget checks sum over at most maxSessions rows, and eviction deletes bounded batches with RETURNING so aggregates stay exact. Additive columns backfill once via ensureAdditiveStateColumns. Fixes #100622
…nd conflicts Session-key/cwd length changes now adjust the aggregate via SET expressions that read the pre-update row, and the conflict-upsert recomputes from surviving event rows instead of clobbering to row overhead. Invariant test now rebinds a provisional key to a longer canonical key mid-stream.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c82997f910
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| SET estimated_bytes = length(session_id) + length(session_key) + length(cwd) + 32 | ||
| + COALESCE((SELECT SUM(e.estimated_bytes) FROM acp_replay_events e | ||
| WHERE e.session_id = acp_replay_sessions.session_id), 0) | ||
| WHERE estimated_bytes = 0; |
There was a problem hiding this comment.
Seed imported replay estimates before append
On the legacy file-import path, migrateFileAcpEventLedgerToSqlite still inserts acp_replay_sessions/acp_replay_events without estimated_bytes after ensureAdditiveStateColumns has already run, and ACP startup immediately reuses that same SQLite handle for the live ledger. If an imported session receives a new update before a restart, its session aggregate becomes non-zero while imported events remain zero; this WHERE estimated_bytes = 0 then prevents a later backfill from rolling those child events into the session total, so byte-budget trimming permanently undercounts that history or deletes zero-byte imported events without reducing the budget. Seed estimates in the import transaction or recompute session totals whenever any child event is backfilled.
Useful? React with 👍 / 👎.
|
Merged via squash.
|
* perf(acp): maintain ledger byte budgets at insert time estimateSqliteLedgerBytes ran a full SUM(length(...)) scan over every replay event on every append, and once the 16MB budget was hit the trim loop rescanned after each single-row delete (openclaw#100622). Event rows now carry estimated_bytes computed at insert, session rows keep a running footprint aggregate, budget checks sum over at most maxSessions rows, and eviction deletes bounded batches with RETURNING so aggregates stay exact. Additive columns backfill once via ensureAdditiveStateColumns. Fixes openclaw#100622 * fix(acp): keep ledger footprint aggregates exact across key changes and conflicts Session-key/cwd length changes now adjust the aggregate via SET expressions that read the pre-update row, and the conflict-upsert recomputes from surviving event rows instead of clobbering to row overhead. Invariant test now rebinds a provisional key to a longer canonical key mid-stream.
What Problem This Solves
Fixes #100622:
estimateSqliteLedgerBytesran a fullSUM(length(...))scan over everyacp_replay_eventsrow on every appended session update, and once the 16MB budget was exceeded the trim loop re-ran that full scan after each single-row delete — O(events) per message and quadratic during eviction. On busy gateways this is a steady per-message tax on the shared state DB.Why This Change Was Made
Applies the insert-time budget pattern (as used by Codex's log DB): event rows carry
estimated_bytescomputed at insert, session rows maintain a running footprint aggregate (row overhead + their events), and the budget check becomes aSUMover at mostmaxSessions(200) rows. Eviction deletes bounded batches (LIMIT 64 ... RETURNING estimated_bytes) and decrements aggregates exactly. Session-key/cwd length changes adjust the aggregate through SET expressions that read the pre-update row, and the conflict-upsert recomputes from surviving event rows, so aggregates cannot drift. Additive columns backfill once viaensureAdditiveStateColumnswith a cheap zero-sentinel probe.User Impact
No behavior change to replay semantics, caps, or eviction order (least-recently-updated sessions still evict first). Appends on the ACP path stop scanning the event ledger; trims stop being quadratic. Existing databases self-migrate on next open.
Evidence
node scripts/run-vitest.mjs src/acp/event-ledger.test.ts— all pass, including a new invariant test that forces repeated byte-budget eviction across three sessions with a provisional→canonical key rebind and asserts the maintained aggregate equals ground truth recomputed from stored rows (and stays ≤ the budget).node scripts/run-vitest.mjs src/state/openclaw-state-db.test.ts— pass (additive column + backfill path).