From 7cdbdb24d73faecb4fa2c9fb47d5bb09048c3079 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Tue, 1 Dec 2026 11:08:27 +0700 Subject: [PATCH 1/2] store: exchanges a backfill sends count as turns and keep the session backfilled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symptom: a session that `sessionpipe backfill` (core 0.19) sent reads on its card with no turns, loses its backfilled mark, and its last time moves to the newest exchange's (or to the moment it arrived, when the exchanges carry no time of their own). nothingHappened() (shared/api.ts) then reads it as a session nothing happened in. Cause: core now sends a backfilled session's exchanges as turn.transcript events (harness.event "backfill") after a session.backfill that carries no `turns` (PROTOCOL.md §4, conformance/delivery/35-backfill-transcript.json). The receiver counted turns only on turn.started and on a backfill's `turns`, and took any other event on a session that was not yet live as its first live one. Fix: a turn.transcript marked "backfill" on a session that so far had only a backfill is history: it keeps the session backfilled with the backfill's span, and the session has as many turns as the highest exchange ordinal read plus one, so a backfill's own `turns`, an exchange sent twice and a later backfill that goes on from the last one count each turn once. Such a later backfill sends no session.backfill again, so an exchange's own time (`data.at`) widens the span; one with no readable time leaves it. A live session, and a session whose first event this receiver reads is such a transcript, read as before. The mock world counts a backfill's exchanges as its turns too. Signed-off-by: Connor Burgess --- server/store/readings.ts | 15 +++++++++++ server/store/store.test.ts | 55 ++++++++++++++++++++++++++++++++++++++ web/src/mock/world.ts | 3 +++ 3 files changed, 73 insertions(+) diff --git a/server/store/readings.ts b/server/store/readings.ts index d77a370a..10a2360b 100644 --- a/server/store/readings.ts +++ b/server/store/readings.ts @@ -677,6 +677,21 @@ export function applyEvent(rec: SessionRecord, ev: ProtocolEvent, arrivedAt: str } const turns = num(d.turns); if (turns !== undefined && turns > s.counts.turns) s.counts.turns = turns; + } else if (ev.type === "turn.transcript" && ev.harness.event === "backfill" && !first && !rec.live) { + // `sessionpipe backfill` sends a session's exchanges as turn.transcript after a + // session.backfill that then carries no `turns` (PROTOCOL.md §4): history, stamped + // with each turn's own time, inside the span the backfill gave. `turn` is the + // exchange's ordinal from 0, so the count is the highest one read plus one: a + // backfill's own `turns`, an exchange sent twice and a later backfill that goes on + // from where the last one stopped count each turn once. Such a later backfill sends + // no session.backfill again, so the exchange's own time widens the span; an exchange + // with none says nothing about when it was. + const turn = num(d.turn); + if (turn !== undefined && turn >= 0) s.counts.turns = Math.max(s.counts.turns, Math.floor(turn) + 1); + const stamped = str(d.at); + const when = stamped && Number.isFinite(Date.parse(stamped)) ? clampTime(stamped, arrivedAt) : undefined; + if (when && when < s.startedAt) s.startedAt = when; + if (when && when > s.lastAt) s.lastAt = when; } else if (ev.type === "limits.observed" && !first) { // The account's numbers, not the session's activity (server/ui/limits.ts keeps them): // they never move lastAt, so they never wake a session or keep it working. One thing diff --git a/server/store/store.test.ts b/server/store/store.test.ts index 7d2af78c..be02abce 100644 --- a/server/store/store.test.ts +++ b/server/store/store.test.ts @@ -6,6 +6,8 @@ import { DONE_AFTER_MS, GAP_AFTER_MS, isEmptySession, + type ProtocolEvent, + type SessionSummary, SLOW_TOOL_MS, SNIPPET_CHARS, STALE_AFTER_MS, @@ -352,6 +354,59 @@ describe("readings", () => { expect(store.session(sessionKey("claude-code", "old-one"))!.backfilled).toBe(false); }); + it("counts a backfill's exchanges as its turns and keeps it backfilled, after a restart too", async () => { + // `sessionpipe backfill` (core 0.19): a session.backfill with no `turns`, then each + // exchange as a turn.transcript stamped with its own time (conformance 35). + const b = new Session("old-one"); + const exchange = (of: Session, turn: number, time?: string): ProtocolEvent => { + const e = of.ev( + "turn.transcript", + { turn, user: `ask ${turn}`, assistant: `answer ${turn}`, at: time }, + { time }, + ); + return { ...e, harness: { ...e.harness, event: "backfill" } }; + }; + const history = (turn: number, time: string) => exchange(b, turn, time); + const key = sessionKey("claude-code", "old-one"); + const span = { started_at: "2026-10-01T09:00:00.000Z", last_at: "2026-10-01T09:20:00.000Z" }; + store.ingest( + [b.ev("session.backfill", span), history(0, "2026-10-01T09:00:00.000Z"), history(1, "2026-10-01T09:15:00.000Z")], + "outbox", + ); + const read = (): SessionSummary => store.session(key)!; + const want = { state: "done", backfilled: true, startedAt: span.started_at, lastAt: span.last_at }; + expect(read()).toMatchObject(want); + expect(read().counts.turns).toBe(2); + expect(read().lastAssistant).toBe("answer 1"); + // The same exchange again, one with no time (or none that reads) of its own, and a later backfill that + // goes on from where this one stopped with no session.backfill of its own. + store.ingest([history(1, "2026-10-01T09:15:00.000Z"), exchange(b, 2), history(2, "not a time")], "outbox"); + expect(read()).toMatchObject(want); + expect(read().counts.turns).toBe(3); + store.ingest([history(3, "2026-10-01T10:05:00.000Z")], "outbox"); + want.lastAt = "2026-10-01T10:05:00.000Z"; + expect(read()).toMatchObject(want); + expect(read().counts.turns).toBe(4); + await store.close(); + store = new Store(tmp.dir, { tickMs: 1_000, statsMs: 1_000 }); + expect(read()).toMatchObject(want); + expect(read().counts.turns).toBe(4); + // A backfill that says how many turns it read keeps that number. + const c = new Session("counted"); + store.ingest([c.ev("session.backfill", { ...span, turns: 5 }), exchange(c, 0, span.started_at)], "outbox"); + expect(store.session(sessionKey("claude-code", "counted"))!.counts.turns).toBe(5); + // The live session reads as before: a transcript is not a turn there, and one this + // receiver reads first is the first live event. + store.ingest([b.ev("session.started", { source: "resume" }), history(4, new Date().toISOString())], "outbox"); + expect(read()).toMatchObject({ backfilled: false }); + expect(read().counts.turns).toBe(4); + store.ingest([exchange(new Session("joined"), 4)], "http"); + expect(store.session(sessionKey("claude-code", "joined"))!).toMatchObject({ + backfilled: false, + counts: { turns: 0 }, + }); + }); + it("goes stale and then done on the timers, emitting session messages", async () => { await store.close(); vi.useFakeTimers(); diff --git a/web/src/mock/world.ts b/web/src/mock/world.ts index d9b04283..c1cb579b 100644 --- a/web/src/mock/world.ts +++ b/web/src/mock/world.ts @@ -553,6 +553,9 @@ export class World { lastUser = cut(String(d.user ?? "")); lastAssistant = cut(String(d.assistant ?? "")); if (progress && progress.turn === d.turn) progress = undefined; + // A backfill's exchanges are its turns (readings.ts). + if (!live && counts.events > 1 && e.harness.event === "backfill" && typeof d.turn === "number" && d.turn >= 0) + counts.turns = Math.max(counts.turns, Math.floor(d.turn) + 1); break; case "turn.progress": progress = { From 478687e64a8d16b12848257b478f0d5658517bb2 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 07:19:25 +0700 Subject: [PATCH 2/2] docs: record the fix under Unreleased Signed-off-by: Connor Burgess --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5299e266..210216b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- Backfilled transcript exchanges count as turns and preserve the session's backfilled state after a restart. + ## [0.8.18] - 2026-12-04 ### Fixed