Skip to content

perf(acp): maintain ledger byte budgets at insert time - #104739

Merged
steipete merged 2 commits into
mainfrom
fix/acp-ledger-insert-budgets
Jul 11, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/acp-ledger-insert-budgets

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #100622: estimateSqliteLedgerBytes ran a full SUM(length(...)) scan over every acp_replay_events row 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_bytes computed at insert, session rows maintain a running footprint aggregate (row overhead + their events), and the budget check becomes a SUM over at most maxSessions (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 via ensureAdditiveStateColumns with 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).
  • Both tsgo lanes clean; oxlint clean.
  • Codex autoreview (gpt-5.5, xhigh): two aggregate-drift findings raised and fixed in the second commit; final branch review clean ("patch is correct", no actionable findings).

steipete added 2 commits July 11, 2026 15:12
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.
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Jul 11, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@steipete
steipete merged commit 4eef09d into main Jul 11, 2026
119 of 121 checks passed
@steipete
steipete deleted the fix/acp-ledger-insert-budgets branch July 11, 2026 22:30
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 12, 2026
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: estimateSqliteLedgerBytes runs full table scans on every event ledger write, causing performance degradation

1 participant