Skip to content

fix(server): history trim stops on activity and publishes what it removed - #264

Merged
jayleaton merged 5 commits into
mainfrom
fix/thread-trim-followup-20261117
Nov 17, 2026
Merged

jayleaton merged 5 commits into
mainfrom
fix/thread-trim-followup-20261117

Conversation

@jayleaton

@jayleaton jayleaton commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #259, covering Randy's post-merge review (#259 (comment)) and his reviews of this PR. A history trim checked eligibility once per thread, so a thread opened mid-pass still lost rows. Its deletes bypassed the event log: the snapshot sequence stayed the same for the thread and its forks, live clients and the timeline and search caches kept stale rows, and the preview counted ancestors the delete keeps.

Fix

  • New domain event thread.history-trimmed names the source thread and the turn items and nodes removed from it. The projection deletes those rows when it applies the event, in the same transaction that appends it. The thread's sequence advances, so the timeline index and ThreadFind refresh. Web and mobile apply the event in client-runtime, and a reconnect replays it. Older clients skip the unknown event type. Trim events don't count as thread activity, so threads keep their place in lists sorted by activity.
  • Forks: every live fork that shows the source's rows, at any depth and including forks below a deleted fork, gets a compact copy on its own stream in the same transaction. The copy holds the source thread id and an empty row list, under 128 bytes. The fork's revision advances durably. The server answers the copy with a fresh snapshot, both in the live tail and when a reconnect's replay contains it, so no client needs fork-specific logic. Deleted forks get no copy. Side chats inherit nothing and get none.
  • EventSink.writeBuilt builds a write's events inside the commit transaction. Each page of up to 200 rows rechecks the current setting and the thread's idle eligibility there. A thread that is opened, messaged, run, pinned, snoozed or PR-linked stops its trim; setting trimming to 0 stops the whole pass.
  • Rows are deleted children first. Each page also rechecks references inside its transaction: a row stays if anything outside the page still hangs off it, whether a child item, a child node, an item on a node, or another table's reference to a node. A pass that stops early never orphans a row.
  • Preview and delete share one selection, including kept ancestors and node references, so the counts match.

Tests (ThreadHistoryTrim.test.ts, ws.test.ts, orchestrationV2Projection.test.ts)

  • Activity during a pass: one table row per kind of activity, run by a hook after the pass has chosen its rows and before it deletes them. All rows fail on main.
  • A pass stopped between pages, with a reasoning child of a removable command placed on a later page: no orphan is left. At 6940c4a the child was left pointing at a deleted command.
  • A row that gains a child after the pass chose it is kept. This fails with the in-transaction recheck removed.
  • Reconnect, live replay, paging and search across a pass, for the thread itself: the sequence advances, replaying from the mid-pass snapshot matches a fresh one, and windows and search report the new sequence.
  • Forks, with a chain of source → deleted fork → live fork plus a direct fork: each live fork's sequence advances, its replay holds compact copies of at most 128 bytes each, and windows and search report the new sequence without the trimmed rows. The deleted fork gets no copy. At 5363779 the live descendant's sequence stayed at 64, and a direct fork's copy was 791 bytes.
  • ws.test.ts: a fork's copy in a replay, and another in the live tail, each arrive as a fresh snapshot.
  • The preview item count matches the delete (main reported one more).
  • Inline outputs (send_to_user, HTML renders, MCP apps, delegate_task) survive a trim. They stay in the snapshot timeline (which transcript export uses), the wire snapshot web and mobile receive, and the rows thread transfer reads. These pass on main too; they guard against regressions.
  • The hooked cases share one migrated database. Each case resets the trim setting and clears what an earlier case left. The file runs in about 25 s, down from about 103 s.

Transfer budget fix (b4868b6): 2b5b815 checked each live thread-stream item for a fork invalidation with Stream.mapEffect, which handles one item at a time, so every batch went out as one WebSocket message per item: 26 for the measured turn, against a budget of 8. The check now runs per batch (Stream.mapArrayEffect). A batch without a fork invalidation passes through unchanged as one message; only a batch that contains one is mapped to a fresh snapshot. Locally, transferBudgetV2.integration.test.ts fails without this commit (26 messages against 8, the same as CI) and passes with it. The budget is unchanged.

The 30-day default and the opt-in behaviour are unchanged.

vp run check:changed passes: format, lint, knip, typecheck of 37 packages, and the 8 test files beside the changes (162 tests).

Done by Claude Opus 5.5 in Claude Code.

🤖 Generated with Claude Code

jayleaton and others added 2 commits November 17, 2026 18:22
…oved

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:L labels Nov 17, 2026
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: de3a810 · PR result: b4868b6 · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@jayleaton

Copy link
Copy Markdown
Owner Author

Randy reviewed head 6940c4a. The fixes from the #259 review are verified:

  • Each page now checks eligibility and the setting inside a BEGIN IMMEDIATE transaction.
  • Trims on the thread itself publish a revision, refresh the caches and replay correctly.
  • The preview and the delete now use the same selection.
  • The inline-output retention tests cover the timeline, the wire snapshot and transfer.

117 targeted tests passed, and so did the 7 package typechecks and changed-file lint.

Still needed:

  1. Blocking: ThreadHistoryTrim.ts:423. An interrupted pass can delete an ancestor of a row that's kept. To reproduce: 250 reasoning rows, plus a reasoning child of a removable command. Opening the thread before page two stops the trim, but the child is left pointing at a command that's already deleted. Fix: delete children before parents and recheck references inside each page transaction. Extend the interrupted-page test with a dependency that spans pages.
  2. Blocking: ThreadHistoryTrim.ts:398. Forks still change without a durable revision. An active fork's snapshot went from 11 visible rows to 10 while its sequence stayed at 17, and a replay after 17 returned no events. The gap survives a reconnect. Fix: either give affected forks a durable invalidation that forces a fresh snapshot, or send trim events that name their source through the forks' streams. Cover live updates, reconnect and paging.
  3. Nit: ThreadHistoryTrim.test.ts:622. The fixtures are slow. Each activity case takes 8.9–11.1 s, and the file takes 103.5 s. Reuse one migrated database fixture and reset the settings between cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added size:XL and removed size:L labels Nov 17, 2026
@jayleaton

Copy link
Copy Markdown
Owner Author

Randy's round 2 at 5363779. These fixes are verified:

  • cross-page interruption and late children
  • direct, nested and archived forks advance their revision and replay correctly
  • the atomic commit and publish
  • unchanged activity times
  • side chats excluded

34 focused tests pass, and check:changed passes: lint, 37 typechecks and 121 tests.

Still needed:

  1. Blocking: ThreadHistoryTrim.ts:260. In a chain of source → deleted fork → live fork, recursion stops at the deleted fork in the middle. The live descendant loses rows it inherited, but its sequence stays at 331 and a replay returns no events. Fix: walk through deleted forks in the middle of the chain, filter deleted forks out of the final list of forks that get the event, and add this chain to the regression test.
  2. Nit: ThreadHistoryTrim.ts:508. The fan-out copies the full removal payload to every fork. With 1,000 forks, a 319 KB trim wrote 3,003 events totalling 12 MB, and each page write took 1.4–1.5 s. Use compact fork invalidations that point at the source trim, and keep the revision updates atomic and durable.

jayleaton and others added 2 commits November 18, 2026 06:09
…dations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nvalidations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton
jayleaton merged commit 3ff2884 into main Nov 17, 2026
32 of 33 checks passed
@jayleaton

Copy link
Copy Markdown
Owner Author

Merged at b4868b6. Randy's round 3 confirmed the two remaining fixes: the descendant of a deleted fork now advances (sequence 331 → 375), and the 1,000-fork payload dropped from 12 MB to 218 KB. The earlier fixes still hold. Randy's only blocker was the per-item Stream.mapEffect that split batches; b4868b6 replaces it with the per-batch mapArrayEffect and Effect.forEach Randy prescribed, and the transfer-budget test and all 23 checks pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant