Skip to content

Prune duplicated command output and started file-change diffs - #7806

Merged
ymichael merged 1 commit into
mainfrom
bb/optimize-provider-pi-storage-usage-thr_grsdgpm2wc
Nov 19, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/optimize-provider-pi-storage-usage-thr_grsdgpm2wc

Conversation

@ymichael

@ymichael ymichael commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Two kinds of event rows were stored in full and never read again. Nothing pruned them, so they stayed for the life of the thread:

What changed

All of this is in packages/db/src/data/resolved-item-pruning.ts and thread-pruning.ts. The resolved-items policy now runs three steps per thread: deltas, background tasks, and started file-change diffs.

  • Command output deltas: once the completion with aggregatedOutput is found, the first delta is also deleted if an item/started row for the same item comes before it, with the same turn, parent tool call and item kind. One indexed lookup (prepareStarted) does the check. Other delta kinds keep their first delta. A message's or reasoning item's text start time comes from its first delta, so deleting it would change what the timeline shows.
  • Started file-change diffs: each started fileChange row that has a later item/completed for the same item, with the same parent, gets its $.item.changes[].diff removed in SQL. Path, kind and move path stay. The pass rests at a bound like the delta pass does, and reaches back below it only for items that gained a completion since the last pass.
  • Revisiting existing threads: the resolved-items version goes from 1 to 2, which queues every thread. The resting cursor version is now set per kind; deltas goes from 2 to 3, so threads already proven under the old rule rescan their deltas once.
  • Docs: docs/events-table-pruning.md documents the new rules.

There are no wire, CLI or schema changes. Event rows are rewritten in place, which the completed-output migration in sweeps.ts already does.

How you verified

  • New tests in packages/db/test/data/thread-pruning.test.ts: all three fail on main and pass here.
    • First delta deleted only when a started row comes before it. Kept when there's no started row, when the completion has no output, or when the started row has a different parent.
    • Started diffs stripped only once a completion exists, including a completion that arrives after the thread's pass has rested. Open items and items under a different parent keep their diffs, and the completed row is untouched.
    • A thread proven under the old rules is revisited after the version bump.
  • Suites: pnpm exec turbo run test --filter=@bb/db passes (722 tests). The @bb/server pruning and timeline tests pass (test/system/thread-pruning-sweep.test.ts, test/services/threads, test/timeline-history: 379 passed, 2 skipped).
  • Checks: typecheck and lint pass, and check:query-costs passes. The BB_QUERY_PLAN_REPORT run over the db tests reports no findings for the new queries. EXPLAIN QUERY PLAN shows an index search for the new UPDATE, DELETE, resume and listing queries.

Real data. I ran the resolved-items cycle on a VACUUM INTO copy of a 10.9 GB production bb.db (mostly Codex and Claude Code), with main's code and this branch on identical copies. Main's code had nothing queued and removed nothing. This branch removed or rewrote 441k rows:

Rows Before After
Codex command output deltas 331.1 MB 3.7 MB
pi command output deltas 5.5 MB 0
acp-omp command output deltas 1.2 MB 0
Codex started fileChange 146.3 MB 12.7 MB
Claude Code started fileChange 37.0 MB 4.3 MB
pi started fileChange 1.3 MB 0.4 MB

That's about 0.5 GB freed. On the reporter's pi-only install, #7523 and #7524 estimate about 0.57 GB together. When I forced main's code through the same full delta rescan, Codex deltas only dropped to 320.4 MB, so the new first-delta rule accounts for about 316 MB of the delta savings.

The one-time pass took 94 s of total work across 23k advances: p50 0.9 ms, p99 35.5 ms. The file-change step's p99 was 18 ms. Both runs had rare multi-second stalls on small threads: main's forced rescan peaked at 2.6 s, this branch at 4.4 s. Those are disk/WAL stalls on this machine, not pruning work: the slowest thread here has 350 delta rows totalling 65 KB. The p99 for main's forced rescan was 44.7 ms.

The stored pi system prompts from #7522 are not part of this PR. Deleting them in core needs a check on the pi provider id, which the provider-literal ratchet rejects. #7525 stops new writes.

Fixes #7523
Fixes #7524

AGENT GENERATED

🤖 Generated with Claude Code

Resolved-item pruning now also deletes a command's first output delta when
its item/started row precedes it and the completion carries
aggregatedOutput, and removes diffs from started fileChange rows once the
item has a completion. The policy version bump queues every thread, and the
deltas cursor version bump makes threads proven under the old rule rescan.

Fixes #7523
Fixes #7524

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/optimize-provider-pi-storage-usage-thr_grsdgpm2wc branch from b9129ae to 8f2d12d Compare November 19, 2026 00:39
@ymichael ymichael changed the title Prune duplicated command output, started file-change diffs and pi system prompts Prune duplicated command output and started file-change diffs Nov 19, 2026
@ymichael
ymichael merged commit df9619d into main Nov 19, 2026
55 checks passed
@ymichael
ymichael deleted the bb/optimize-provider-pi-storage-usage-thr_grsdgpm2wc branch November 19, 2026 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant