Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.19] - 2026-12-04

### Added
Expand Down
15 changes: 15 additions & 0 deletions server/store/readings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions server/store/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
DONE_AFTER_MS,
GAP_AFTER_MS,
isEmptySession,
type ProtocolEvent,
type SessionSummary,
SLOW_TOOL_MS,
SNIPPET_CHARS,
STALE_AFTER_MS,
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions web/src/mock/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down