Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tracker-seen-running-answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-react": patch
---

Reopening a session whose turn is still running no longer clears its tracker. The seen observer could record proof from the turn id the live frames left on the answer before the history replay arrived. It now ignores an answer that is still being written, so the tracker stays until the turn has ended and its end is on screen.
6 changes: 6 additions & 0 deletions docs/decisions/design-kit/sessions-and-drafts.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ kinds keep #910's rules and state footer, with no second indicator.
> announcements until it is.
> - `Mark as seen` with no latest turn identity to store deletes the record,
> since there is nothing to acknowledge against.
> - *(2026-11-03, #3406.)* An answer still being written is not the linked
> turn, even when it carries the turn its live frames named. History links
> a turn only once it has ended, so a replay strips that turn from a
> running answer. Before #3406, reopening a running session could beat its
> replay and record proof from the stale frame turn, and the tracker
> cleared before the turn ended.

> **2026-10-06 — R3's read-only card and the shell's time, implemented by
> #1072.** The rules are `packages/ui-react/src/lib/restored-approvals.ts`,
Expand Down
11 changes: 8 additions & 3 deletions packages/ui-react/src/hooks/use-tracker-seen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const AT_BOTTOM_PX = 20;
* the document is visible, the transcript ends on the message the host
* linked to the latest turn, and that end is in the viewport by the
* transcript's own bottom test, with no scroll disc drawn. Selecting the
* session, being scrolled up, a hidden tab, a background buffer and a
* replay without the linked turn do not count.
* session, being scrolled up, a hidden tab, a background buffer, a replay
* without the linked turn and an answer still being written do not count.
*/
export function observeTrackerSeen(root: BrainUiRoot, transcript: HTMLElement | null, scrollDisc: boolean): boolean {
const chat = root.stores.chat.getState();
Expand All @@ -34,8 +34,13 @@ export function observeTrackerSeen(root: BrainUiRoot, transcript: HTMLElement |
if (typeof document === "undefined" || document.visibilityState !== "visible") return false;
// And any open modal dialog (a zoom viewer, a one-time credential, a sheet).
if (typeof document.querySelector === "function" && document.querySelector('[aria-modal="true"]')) return false;
// An answer still being written proves nothing: its turn is the one the
// live frames named before the reader left, which is correlation only,
// and history links a turn only once it has ended (#964). Its replay
// strips that turn; until the replay arrives the stale one must not
// count, or reopening a running session would read as seeing it (#3406).
const last = chat.buffers[sessionId]?.messages.at(-1);
if (!last || last.turnId !== key.turnId) return false;
if (!last || last.isStreaming || last.turnId !== key.turnId) return false;
if (transcript.clientHeight === 0) return false;
if (transcript.scrollHeight - transcript.scrollTop - transcript.clientHeight >= AT_BOTTOM_PX) return false;
return trackers.observeSeen({ sessionId, ...key });
Expand Down
27 changes: 21 additions & 6 deletions packages/ui-react/tests/parallel-sessions-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,13 @@ async function openFromSessions(run: Run, page: Page, sessionId: string) {
* The reader scrolls the transcript up: Chromium's own scroll gesture, by
* touch under a coarse pointer and by mouse wheel otherwise, through the
* input pipeline rather than a scrollTop write.
*
* Each swipe's finger comes to rest before it lifts, so the gesture leaves
* no fling behind. The events carry their own times, so the rest does not
* depend on how fast they are dispatched. A fling still moving the
* transcript would decide the next press instead: a tap that lands on a
* fling only stops it, and Chromium swallows that tap as a phone does, so
* "Scroll to latest" would never be pressed (#3377).
*/
async function scrollUp(run: Run, page: Page) {
// The transcript's scroller, as it is drawn: the gesture starts at its middle.
Expand All @@ -539,16 +546,24 @@ async function scrollUp(run: Run, page: Page) {
await cdp.send("Input.synthesizeScrollGesture", { x: Math.round(box.x), y: Math.round(box.y), yDistance: 4000, speed: 6000, gestureSourceType: "mouse" });
return;
}
// A finger dragging the transcript down, a few times.
for (let swipe = 0; swipe < 3; swipe++) {
// A finger dragging the transcript down, a few times: a move every
// 16ms, then held still for 200ms before it lifts. The times end no
// later than now, so the next real tap is never older than this swipe.
const swipes = 3, steps = 12, moveMs = 16, restMs = 200;
let at = Date.now() - swipes * ((steps + 1) * moveMs + restMs);
const touch = (type: "touchStart" | "touchMove" | "touchEnd", touchPoints: Array<{ x: number; y: number }>, afterMs: number) => {
at += afterMs;
return cdp.send("Input.dispatchTouchEvent", { type, touchPoints, timestamp: at / 1000 });
};
for (let swipe = 0; swipe < swipes; swipe++) {
const x = Math.round(box.x);
let y = Math.round(box.y - 120);
await cdp.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [{ x, y }] });
for (let step = 0; step < 12; step++) {
await touch("touchStart", [{ x, y }], moveMs);
for (let step = 0; step < steps; step++) {
y += 20;
await cdp.send("Input.dispatchTouchEvent", { type: "touchMove", touchPoints: [{ x, y }] });
await touch("touchMove", [{ x, y }], moveMs);
}
await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] });
await touch("touchEnd", [], restMs);
}
} finally { await cdp.detach(); }
}
Expand Down
29 changes: 29 additions & 0 deletions packages/ui-react/tests/tracker-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,35 @@ describe("the seen observer's conditions", () => {
});
});

describe("reopening a running session (#3406)", () => {
test("the live frames' turn on an answer still being written is not seen until the turn ends", () => {
const doc = Object.assign(new EventTarget(), { visibilityState: "visible", querySelector: () => null });
(globalThis as { document?: unknown }).document = doc;
try {
const r = root();
hello(r, false);
// A streams in view, so its answer carries the frames' turn; then the
// reader leaves while it runs and comes back before any replay.
runningIn(r, A);
r.stores.chat.getState().setActiveSession(B);
r.stores.chat.getState().setActiveSession(A);
const last = r.stores.chat.getState().buffers[A]!.messages.at(-1)!;
expect({ isStreaming: last.isStreaming, turnId: last.turnId }).toEqual({ isStreaming: true, turnId: "turn-1" });
expect(views(r).map((v) => [v.sessionId, v.state, v.cleared])).toEqual([[A, "running", false]]);
const bottom = { clientHeight: 400, scrollHeight: 1000, scrollTop: 600 } as HTMLElement;
expect(observeTrackerSeen(r, bottom, false), "a running answer at the bottom").toBe(false);
expect(views(r)[0]!.cleared).toBe(false);
// The turn ends while the reader is at its end: that is seen.
frame(r, { type: "result", sessionId: A, turnId: "turn-1", outcome: "success", durationMs: 1, numTurns: 1, isError: false });
r.connection.flushChatDeltas();
expect(observeTrackerSeen(r, bottom, false), "the ended turn at the bottom").toBe(true);
expect(views(r)[0]!.cleared).toBe(true);
} finally {
delete (globalThis as { document?: unknown }).document;
}
});
});

describe("persistence", () => {
function tracked(storage: Storage, storagePrefix: string, sessionId = A) {
const r = root({ storage, storagePrefix });
Expand Down
Loading