perf(agent-worker): settling a turn reads only that turn's transcript rows

Draft
SA
1d ago
+48-38

What

Each agent-session turn downloaded the session's whole transcript (every page) up to four times: twice in runTurn, once in settleTurn, and once to read a failed turn's last error. A Dev's main session never ends, so every one of those reads grew with the Dev's whole history.

  • SessionStore.transcript(afterSeq?) starts its paging at afterSeq.
  • turnRows(store, turnId) gets the turn's start row with the existing transcriptStep (?stepKey=) query and reads only the rows after it. It replaces turnStartIndex, turnStartOf, entriesOfTurn and tailEntries.
  • runTurn's second read (after it appends the run marker) fetches only the new rows.

The one full read that restores pdev's session file stays.

Why it keeps behaviour

Transcript seq is INTEGER PRIMARY KEY AUTOINCREMENT in the session's Durable Object, so "rows after the start row in the full list" is "rows with seq above the start row's seq". A turn with no start row still gives no entries and no start time.

Tests

  • New settleTurn test "reads only the rows of its own turn": two turns, then settle the second; the store sees one read, from the second turn's start. It fails on main (the read is from 0).
  • The in-memory store now honours afterSeq, so the existing runTurn tests cover the incremental read.
  • bun test in apps/agent-worker 20/20, bun run typecheck and biome check clean.

Summary by cubic

Makes turn settling read only the current turn's transcript rows instead of the session's whole history, so reads no longer grow with a Dev's lifetime.

Each turn previously downloaded the entire transcript up to four times: twice in runTurn, once in settleTurn, and once to read a failed turn's last error. settleTurn and the error read now find the turn's start row via the existing transcriptStep query and fetch only the rows after it; runTurn's post-marker read fetches only new rows.

  • SessionStore.transcript(afterSeq?) now starts paging at afterSeq.
  • The new turnRows(store, turnId) helper replaces turnStartIndex, turnStartOf, entriesOfTurn, and tailEntries.
  • Behavior is unchanged because transcript seq is an autoincrement key: rows after the start row are rows with a higher seq, and a turn with no start row still yields no entries and no start time.
  • The one full read that restores pdev's session file stays.