From 6f99fd3493ca9643fb3079ab2033eeb5dd562389 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 3 Nov 2026 00:09:59 +0000 Subject: [PATCH 1/2] test(ui-react): end the phone's scroll-up swipes at rest, so no fling swallows Scroll to latest The 390 phone case scrolled C's transcript up with three fast finger swipes that each lifted mid-motion. Chromium turned them into a fling that kept moving the transcript after scrollUp returned. A tap that lands on a running fling only stops it, and Chromium suppresses that tap, so the press on "Scroll to latest" could do nothing and C never read as seen. Each swipe now carries its own event times and holds the finger still for 200ms before lifting, so the gesture leaves no fling at any dispatch speed. Every assertion and deadline is unchanged. Refs #3377 Co-Authored-By: Claude Opus 5.5 --- .../tests/parallel-sessions-runtime.test.ts | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/ui-react/tests/parallel-sessions-runtime.test.ts b/packages/ui-react/tests/parallel-sessions-runtime.test.ts index 895f7bba1c..531eed0dc5 100644 --- a/packages/ui-react/tests/parallel-sessions-runtime.test.ts +++ b/packages/ui-react/tests/parallel-sessions-runtime.test.ts @@ -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. @@ -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(); } } From b06d617bfc7f913127343cb4d0438a5aca96fd3a Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 3 Nov 2026 01:13:34 +0000 Subject: [PATCH 2/2] fix(ui-react): an answer still being written is not seen, so reopening a running session clears nothing The seen observer matched the last message's turnId against the latest turn without asking whether that message had ended. While a session streams in view, its live frames stamp their turn onto the answer; the history replay that follows a reopen strips it, since history links a turn only once it has ended. When the observer's first committed render after selecting a running session beat that replay, the stale frame turn matched and the observer recorded proof for a turn still running: the tracker read cleared before the turn ended (#3406). The observer now refuses a last message that is still being written. That is what it already saw once the replay arrived, so only the race window closes; a turn that ends while the reader is at its end is still seen. Closes #3406 Co-Authored-By: Claude Opus 5.5 --- .changeset/tracker-seen-running-answer.md | 5 ++++ .../design-kit/sessions-and-drafts.md | 6 ++++ .../ui-react/src/hooks/use-tracker-seen.ts | 11 +++++-- packages/ui-react/tests/tracker-store.test.ts | 29 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 .changeset/tracker-seen-running-answer.md diff --git a/.changeset/tracker-seen-running-answer.md b/.changeset/tracker-seen-running-answer.md new file mode 100644 index 0000000000..aa47212dd6 --- /dev/null +++ b/.changeset/tracker-seen-running-answer.md @@ -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. diff --git a/docs/decisions/design-kit/sessions-and-drafts.md b/docs/decisions/design-kit/sessions-and-drafts.md index 24b295a01f..1ff8c85c02 100644 --- a/docs/decisions/design-kit/sessions-and-drafts.md +++ b/docs/decisions/design-kit/sessions-and-drafts.md @@ -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`, diff --git a/packages/ui-react/src/hooks/use-tracker-seen.ts b/packages/ui-react/src/hooks/use-tracker-seen.ts index 4f64696888..28b104a254 100644 --- a/packages/ui-react/src/hooks/use-tracker-seen.ts +++ b/packages/ui-react/src/hooks/use-tracker-seen.ts @@ -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(); @@ -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 }); diff --git a/packages/ui-react/tests/tracker-store.test.ts b/packages/ui-react/tests/tracker-store.test.ts index e82d0e2abb..3ae853f9b5 100644 --- a/packages/ui-react/tests/tracker-store.test.ts +++ b/packages/ui-react/tests/tracker-store.test.ts @@ -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 });