Skip to content

Load each long transcript's older history in the background - #37

Merged
JasonPierce merged 3 commits into
mainfrom
history-backfill
Nov 14, 2026
Merged

JasonPierce merged 3 commits into
mainfrom
history-backfill

Conversation

@JasonPierce

Copy link
Copy Markdown
Contributor

On a long-running session antfarm only read the last 16 MB of the transcript, so the swimlanes started partway through, call counts and files touched undercounted, and results whose call sat before the cut were dropped. Even inside the tail, each agent kept only its last 500 activity entries, which is where the "older calls not kept" marker came from. On this machine the Greatsword transcript is 487 MB, and main shows about 1,100 of its 9,100 calls.

Attach still reads the tail first, so live state shows up as fast as before. Then a background thread reads each cut transcript backward from its boundary to the first line, main transcripts and sub-agents alike, and turns each chunk into compact call records: id, tool, summary, start, end, status, and the byte offsets of the call and result lines. The engine merges those in front of what the tail read, and never runs old lines through the live reducer, so status, names, and context fill are untouched. The same compact store also takes calls as they age out of the 500-entry activity list, so nothing is cut any more. Inputs and results aren't kept for older calls. The call screen reads them back from the transcript by offset, and it now shows the result for every call, which it never did before.

Acceptance criteria

  • First snapshot no later than today. Tail first, and the job is queued only after the tail's lines are sent. watch's settle step used to wait for snapshots to go quiet, which backfill progress would have stretched to its 5 s cap; it now waits for events to go quiet, and backfill emits none. Measured on this machine with release builds, three runs each, time to the first watch --json snapshot: main 1.3 to 2.5 s, branch 0.6 to 2.5 s, branch with --no-backfill 1.2 to 2.4 s. All of it is settle time, and the spread is noise from the live sessions.
  • Progress while loading, marker gone after. Session.history is loading {read, total} while backfill runs, then complete, and truncated_before clears. The TUI and app swimlanes show "loading older history 34%" where "older calls not kept" used to be. Tests: progress_shows_while_backfill_reads in crates/antfarm-core/tests/backfill.rs and a_tail_orphan_is_paired_with_its_backfilled_call in reduce.rs.
  • Counts, per-agent counts, files touched, and swimlanes cover the whole transcript. backfill_matches_reading_every_transcript_whole attaches to the scrubbed real/small-session fixture with a 32 KB tail and 8 KB chunks, then compares every agent's call count, every call's id, tool, start, end, status, and summary in order, and the files touched list with line counts against a run that reads every file whole. On real data, Greatsword's root shows 8,587 calls, which matches a grep of distinct tool_use ids in the transcript.
  • Sub-agents backfilled too. Same test; it asserts that sub-agent history was backfilled.
  • Opening a backfilled call shows its input and result. The integration test reads a backfilled call's input and result back by offset, and call_detail_reads_an_older_calls_input_and_result_from_the_transcript in view.rs checks that both are masked. By hand: in the TUI I opened an AskUserQuestion call from mid-October on Greatsword, weeks before the tail, and saw its full input and answer.
  • Moves and truncation during backfill. a_transcript_that_moves_during_backfill_is_followed renames the project folder mid-backfill; counts still match the whole read. Jobs open their file at attach, which this test caught: a queued sub-agent job used to lose its file after a move. a_transcript_replaced_during_backfill_has_no_duplicate_calls replaces the main transcript mid-backfill and checks that ids are unique and that what the cancelled job merged was taken back out of the store and the call count. Unit test a_batch_from_a_stale_job_is_dropped.
  • watch --json marks history and emits an event. Each session in a snapshot carries history, and the stream carries a history_complete event (backfill_matches_reading_every_transcript_whole checks the event). On this machine backfill of the 487 MB file finishes inside watch's settle window, because most of its bytes are api-request-blob lines skipped by type, so the first snapshot already says complete and the event comes before watch subscribes. That follows watch's existing rule that history up to now is in the first snapshot.
  • A flag turns it off. --no-backfill, global. ls and doctor never backfill. Test: the_flag_off_leaves_history_partial.
  • Memory measured and recorded. Peak RSS from /usr/bin/time -l antfarm watch --json --session on the 487 MB session: 71 MB. The whole machine (7 sessions, 10,188 calls): 73 MB, against 51 to 57 MB on main or with backfill off. Recorded in DESIGN.md section 5.
  • DESIGN.md section 5 and decision 4 updated, plus the model in section 4, the call screen in section 6, and the transcript sizes in 3.2. README covers the flag and results on the call screen.

Checked by hand

  • TUI on the live machine: Greatsword's swimlanes fit Oct 11 to Nov 14, back to the first line, and an October call opens with input and result.
  • App screens in the browser mock (check-ui) at 1440, 1000, and 960 wide, dark and light: a result in the Output tab, a 400-line result scrolls inside the tab with no horizontal overflow and the "cut at 20,000 characters" note at the end, an error result uses the theme's error color, and the swimlane loading note fits on one line at 960 and ends 16 px from the edge. That check also turned up a missing space before the session screen's active-time label ("3h36m· partial history"); the old "· last 500 calls" label had the same bug, and it's fixed.
  • live-repro on Claude Code 2.1.325 with Haiku: Bash calls, a background Agent, and an AskUserQuestion wait. Main and the branch showed the same status sequence (idle, thinking, working, thinking, idle, thinking, waiting on a question, thinking, idle), the same event counts, and 7 calls, which matches the transcript (5 main, 2 sub-agent).

Not checked

  • The real Tauri app (pnpm tauri dev); only the mock.
  • Backfill on a live throwaway session: its transcript is far below 16 MB, so the live repro covers "nothing changed for live state". Backfill itself is covered by the fixture tests and the real-machine runs above.
  • Linux.
  • Usage, tokens, and cost aren't backfilled; cost still comes from the last cost-state line. Backfilled background Bash calls don't carry their background run, so their output tail isn't offered.

Related: #36 (forked skills not linked to their Skill call) was found along the way and isn't part of this.

Closes #13

Comment thread crates/antfarm-view/src/view.rs Fixed
Comment thread crates/antfarm-view/src/view.rs Fixed
@JasonPierce
JasonPierce merged commit 3a41f42 into main Nov 14, 2026
7 checks passed
@JasonPierce
JasonPierce deleted the history-backfill branch November 14, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Load a session's full history, not only the last 16 MB

2 participants