From 4423dc2fabc8028f9a3c6d013da0aa9ef03b617f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 02:28:49 +0000 Subject: [PATCH 1/2] fix(kitchen-sink): put the Flow State page back off live mode (FIX-2506) PR #4404 (FIX-2410) said the Flow State page stays off `live` and keeps `BackgroundWorkRefresh`, because live mode broke three Kitchen Sink E2E specs. Its final merge of main (b13a2d7f8) resolved the page and the background-work panel to the branch side instead: `live: true` on the assistant's `useSession`, and the stream-end re-read deleted. That merge landed on main as c247ebc3f, and from then on the same three specs fail: - talk-from-page:403 sees the assistant's session followed by a `/sessions/:id/stream` it must not open. - workforce-shell:82 never reaches `networkidle`: the live session stream holds a connection open for as long as the page is mounted. - background-work:17 loses the row under suite load: with the stream-end re-read gone, the list only updates when the live stream names a run. Restore both files to their pre-#4404 contents, which is what #4404 described. Moving the page to live mode stays the follow-up #4404 named. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011UNkLEJU1iqxFYoPadTRfn --- apps/kitchen-sink/app/flow-state/page.tsx | 13 ++- .../components/background-work-panel.tsx | 86 +++++++++++++++++-- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/apps/kitchen-sink/app/flow-state/page.tsx b/apps/kitchen-sink/app/flow-state/page.tsx index d9cf2f9be2..a568049a3f 100644 --- a/apps/kitchen-sink/app/flow-state/page.tsx +++ b/apps/kitchen-sink/app/flow-state/page.tsx @@ -55,7 +55,7 @@ import { PickedSessionPanel } from "@/components/picked-session-panel"; import { ArtifactDialog } from "@/components/artifact-dialog"; import { ResizeHandle } from "@/components/resize-handle"; import { SuggestionRow } from "@/components/suggestion-row"; -import { BackgroundWorkPanel } from "@/components/background-work-panel"; +import { BackgroundWorkPanel, BackgroundWorkRefresh } from "@/components/background-work-panel"; import { PullRequestsPanel } from "@/components/pull-requests-panel"; import { VoiceToggle } from "@/components/voice-toggle"; import { ThemeToggle } from "@/components/theme-toggle"; @@ -252,9 +252,7 @@ function KitchenSinkApp({ namedSessionId, pickedLive }: { namedSessionId: string writeSessionAddress(openSessionId, how.current); how.current = "replace"; }, [resolving, openSessionId]); - // `live`: follow the whole session, so background work a turn files shows - // as its run starts and finishes, without another message. - const session = useSession(flow.activeSessionId, { items: true, autoResume: true, live: true }); + const session = useSession(flow.activeSessionId, { items: true, autoResume: true }); const [message, setMessage] = useDraft(SESSION_DRAFT, flow.activeSessionId); const messageRef = useRef(message); @@ -685,6 +683,13 @@ function KitchenSinkApp({ namedSessionId, pickedLive }: { namedSessionId: string activeSkills={modeStatus?.activeSkills} /> + {/* Mounted HERE, above both responsive trees, and exactly once. The + mobile and desktop `ChatPanel`s are both alive at the same time — + one is hidden with CSS, not unmounted — so a side effect placed + inside either of them runs twice per turn. The panel itself is fine + to render twice; its stream-end re-read is not. */} + +
{(mobilePanel === "chat" || picked !== null) && stream} diff --git a/apps/kitchen-sink/components/background-work-panel.tsx b/apps/kitchen-sink/components/background-work-panel.tsx index 74bcecafab..31d51eec78 100644 --- a/apps/kitchen-sink/components/background-work-panel.tsx +++ b/apps/kitchen-sink/components/background-work-panel.tsx @@ -12,11 +12,12 @@ * Three details the hook's contract forces, and they are the reason this is a * component rather than a `.map()` at the call site: * - * - **The list is current because the page follows the session.** Its - * `useSession` passes `live: true`, so the hook re-reads the list whenever a - * run starts or finishes, and a job a turn just filed shows without another - * message. Without `live` it is re-read only on mount, at the start of each - * action, and on `session.refresh()`. + * - **The list does not update on its own.** It is re-read on mount, at the + * start of each action, and on `session.refresh()`. On its own that hides the + * *first* job until the user does something else, so a sibling component — + * {@link BackgroundWorkRefresh}, mounted once at the page level — re-reads + * when a turn stops streaming. It is not in this component on purpose; see + * its own doc for why a duplicated responsive tree makes that matter. * - **`childSessionsStale` means "this is the last list we could get".** The rows * stay on screen and get marked, rather than disappearing. (The hook's field * names follow the provenance route it reads.) @@ -24,7 +25,7 @@ * *not finished*. Neither is rendered as "running". */ -import { useCallback, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { ItemsRenderer, useSession } from "@flow-state-dev/react"; import type { ChildSessionSummary } from "@flow-state-dev/client"; import { Button } from "@/components/ui/button"; @@ -75,6 +76,79 @@ interface BackgroundWorkPanelProps { } /** The strip above the prompt input: one row per body of background work. */ +/** + * The stream-end re-read, as a component so it is mounted EXACTLY ONCE. + * + * Deliberately separate from {@link BackgroundWorkPanel}. The page keeps a + * mobile and a desktop `ChatPanel` tree alive at the same time and hides one + * with responsive CSS, so anything rendered inside a `ChatPanel` is mounted + * twice and every effect in it runs twice. With the effect living in the panel + * that meant two reads per finished turn, not one — a cost that is invisible + * from the panel's own source and contradicts what the comment below claims. + * Rendering the panel twice is harmless (both read the same hook state); firing + * its side effect twice is not. Mount this once, above those trees. + * + * Without it the job a turn just filed stays invisible until the user sends + * another message, because the hook's own re-read happens at the START of an + * action. Guarded on the actual true → false transition, so a re-render never + * triggers a second read. + * + * **Demo debt — do not copy this into an application.** The dispatched-run axis has + * a pinned read budget: ONE `listChildSessions` read per turn, taken by + * `useSession` at action start. That budget is a contract + * (`docs/architecture/server-and-client.md`), and it is why the DevTool's own + * panel reads one page plus a sentinel instead of walking the index. + * + * What this actually costs, stated plainly because it was understated twice: + * `session.refresh()` is a **full session snapshot plus** the dispatched-run read, + * and with `items: true` the snapshot paginates the entire item history. It is + * not one extra list read. The gate below at least confines that to + * conversations which use background work at all. + * + * The intended fix is a framework opt-in rather than an app-level effect, + * tracked as **FIX-1109**: `useSession` grows an explicit + * `childSessions: { refreshOnTerminal: true }`, refreshing from the terminal + * branch it already has (`onRequestStatus`), so the pinned budget stays the + * default and the second read becomes a named choice. When FIX-1109 lands, + * delete this component and pass that option instead. + */ +export function BackgroundWorkRefresh({ session }: { session: BackgroundWorkPanelProps["session"] }) { + const { refresh, isStreaming, items, childSessions } = session; + + // Only conversations that actually use background work pay for this. The + // gate matters because `session.refresh()` is NOT the cheap read this was + // originally documented as: it is `Promise.all([refreshSnapshot(), + // refreshChildSessions()])`, and with `items: true` the snapshot paginates the + // session's entire item history. Ungated, every finished turn in every + // conversation — the overwhelming majority of which never file a job — paid + // a full history refetch to update a panel with nothing in it. + // + // `useSession` exposes no dispatched-run-only refresh (`refreshChildSessions` is + // internal), which is exactly the gap FIX-1109 closes. Until it lands this + // gate is the whole of the mitigation available to an app. + // + // Detected from `task-board-meta`, and NOT from `task-change` — this board + // declares those invisible to the client on purpose (see + // `backgroundWorkLedger`). + const usesBackgroundWork = + childSessions.length > 0 || + items.some( + (item) => + item.type === "component" && + (item as { component?: string }).component === "task-board-meta" && + (item as { data?: { collectionId?: string } }).data?.collectionId === + "background-work", + ); + + const wasStreaming = useRef(isStreaming); + useEffect(() => { + const finished = wasStreaming.current && !isStreaming; + wasStreaming.current = isStreaming; + if (finished && usesBackgroundWork) void refresh(); + }, [isStreaming, refresh, usesBackgroundWork]); + return null; +} + export function BackgroundWorkPanel({ session, flowKind }: BackgroundWorkPanelProps) { const [openId, setOpenId] = useState(null); const [isRefreshing, setIsRefreshing] = useState(false); From 756c72401ef7accb18457b3c50f6385ba442f243 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 02:45:44 +0000 Subject: [PATCH 2/2] fix(kitchen-sink): re-read background work without the item history (FIX-2506) `BackgroundWorkRefresh` called `refresh()`, which re-reads the whole item history on every finished turn in a conversation that uses background work. A completed request already gets that full read from `useSession`'s terminal branch, so the stream-end re-read only needs the dispatched-run list: `refresh({ items: false })` reads state and child sessions with no item pages (Codex review on #4596). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011UNkLEJU1iqxFYoPadTRfn --- .../components/background-work-panel.tsx | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/apps/kitchen-sink/components/background-work-panel.tsx b/apps/kitchen-sink/components/background-work-panel.tsx index 31d51eec78..0b42fb7d38 100644 --- a/apps/kitchen-sink/components/background-work-panel.tsx +++ b/apps/kitchen-sink/components/background-work-panel.tsx @@ -99,11 +99,11 @@ interface BackgroundWorkPanelProps { * (`docs/architecture/server-and-client.md`), and it is why the DevTool's own * panel reads one page plus a sentinel instead of walking the index. * - * What this actually costs, stated plainly because it was understated twice: - * `session.refresh()` is a **full session snapshot plus** the dispatched-run read, - * and with `items: true` the snapshot paginates the entire item history. It is - * not one extra list read. The gate below at least confines that to - * conversations which use background work at all. + * What this costs: `session.refresh({ items: false })` is one state read plus + * the dispatched-run read. It leaves out the item history, which a completed + * turn already re-reads in full on its own (`useSession`'s terminal branch). + * The gate below confines even that to conversations which use background + * work at all. * * The intended fix is a framework opt-in rather than an app-level effect, * tracked as **FIX-1109**: `useSession` grows an explicit @@ -115,17 +115,10 @@ interface BackgroundWorkPanelProps { export function BackgroundWorkRefresh({ session }: { session: BackgroundWorkPanelProps["session"] }) { const { refresh, isStreaming, items, childSessions } = session; - // Only conversations that actually use background work pay for this. The - // gate matters because `session.refresh()` is NOT the cheap read this was - // originally documented as: it is `Promise.all([refreshSnapshot(), - // refreshChildSessions()])`, and with `items: true` the snapshot paginates the - // session's entire item history. Ungated, every finished turn in every - // conversation — the overwhelming majority of which never file a job — paid - // a full history refetch to update a panel with nothing in it. - // - // `useSession` exposes no dispatched-run-only refresh (`refreshChildSessions` is - // internal), which is exactly the gap FIX-1109 closes. Until it lands this - // gate is the whole of the mitigation available to an app. + // Only conversations that actually use background work pay for this. + // `useSession` exposes no dispatched-run-only refresh (`refreshChildSessions` + // is internal), which is exactly the gap FIX-1109 closes; until it lands, + // `items: false` plus this gate is the narrowest read available to an app. // // Detected from `task-board-meta`, and NOT from `task-change` — this board // declares those invisible to the client on purpose (see @@ -144,7 +137,7 @@ export function BackgroundWorkRefresh({ session }: { session: BackgroundWorkPane useEffect(() => { const finished = wasStreaming.current && !isStreaming; wasStreaming.current = isStreaming; - if (finished && usesBackgroundWork) void refresh(); + if (finished && usesBackgroundWork) void refresh({ items: false }); }, [isStreaming, refresh, usesBackgroundWork]); return null; }