From cc8463b09965d3aab00adb445d68959926a89b9c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 21:49:44 +0000 Subject: [PATCH 1/4] feat(labs): show a failed turn inline with its reason and Retry (FIX-2550) Both labs map the engine's error item to a failed entry drawn by the registry's request-recovery, mark a reply its turn never finished incomplete, and offer Retry (the client's request(id).retry(), through a new adapter verb) on the newest turn only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NczDiDAAfKeTwqH2bFcZLD --- labs/claude-code-style/README.md | 1 + labs/claude-code-style/src/adapter.ts | 45 +++++- .../src/components/Entry.tsx | 17 +- .../src/components/Thread.tsx | 8 +- .../flow-state/request-recovery.tsx | 146 ++++++++++++++++++ .../test/failed-turn-engine.test.ts | 59 +++++++ .../test/failed-turn.test.tsx | 112 ++++++++++++++ labs/claude-code-style/test/fake-lab.ts | 32 +++- labs/teammates/README.md | 1 + labs/teammates/src/adapter.ts | 48 +++++- .../teammates/src/components/Conversation.tsx | 2 + labs/teammates/src/components/Entry.tsx | 17 +- .../flow-state/request-recovery.tsx | 146 ++++++++++++++++++ .../teammates/test/failed-turn-engine.test.ts | 57 +++++++ labs/teammates/test/failed-turn.test.tsx | 104 +++++++++++++ labs/teammates/test/fake-lab.ts | 29 ++++ 16 files changed, 806 insertions(+), 18 deletions(-) create mode 100644 labs/claude-code-style/src/components/flow-state/request-recovery.tsx create mode 100644 labs/claude-code-style/test/failed-turn-engine.test.ts create mode 100644 labs/claude-code-style/test/failed-turn.test.tsx create mode 100644 labs/teammates/src/components/flow-state/request-recovery.tsx create mode 100644 labs/teammates/test/failed-turn-engine.test.ts create mode 100644 labs/teammates/test/failed-turn.test.tsx diff --git a/labs/claude-code-style/README.md b/labs/claude-code-style/README.md index 3e0832c028..3618cf5fbc 100644 --- a/labs/claude-code-style/README.md +++ b/labs/claude-code-style/README.md @@ -148,6 +148,7 @@ a package call: | A result card inside a running turn | `answerAgain: true` on the main worker's flow: the settled task's notice, taken into the turn | | The main session reading the project's files | `projectSourceCapability({ workspace })` on the `agent` flow, over the coding runs' host; `desk.main` and `desk.researcher` name its `read` preset in their `WORKER.md` | | Interrupting | aborting the session's running turn; its tasks keep running | +| A failed turn and *Retry* | the engine's `error` item for the turn, drawn by the UI registry's `request-recovery` (installed in `src/components/flow-state/`); Retry is the client's `request(id).retry()`, on the newest turn only | | The tasks pane and a task's work | the `listTasks_tasks` read (stores nothing per poll), and the items of the task's run session | | Threads mode's sessions | the same, with `desk.lead`, a `coordinator`, as the main worker | | A thread card, and *Sent to a task* | the lead's `createTask` call in the session's items | diff --git a/labs/claude-code-style/src/adapter.ts b/labs/claude-code-style/src/adapter.ts index 9857aaf298..0780c3199f 100644 --- a/labs/claude-code-style/src/adapter.ts +++ b/labs/claude-code-style/src/adapter.ts @@ -14,6 +14,7 @@ * | `openSession` | a new session on the mode's main worker, then `setWorkProject` | * | `send` | the worker's door, `respond`, in that session | * | `interrupt` | aborting the session's running request | + * | `retry` | the client's `request(id).retry()` on the session's flow: a failed turn again | * | `watchThread` | the session's items, followed through the client's `sessionFeed` | * | | and that feed's connection state, for the connection bar; `loadEarlier` | * | | is the feed's, for a thread longer than its first read; its running turn | @@ -97,7 +98,8 @@ export type ThreadEntry = ( /** Threads mode: the lead's turn filed threads for this line. */ sentToThread?: boolean; } - | { kind: "assistant"; id: string; text: string } + /** `incomplete` when its turn failed before the reply was finished. */ + | { kind: "assistant"; id: string; text: string; incomplete?: boolean } /** A run of consecutive tool calls in one turn, in words (`toolRun`): one line that opens to a line per call. */ | ({ kind: "tool"; id: string } & ToolRun) | { @@ -111,6 +113,11 @@ export type ThreadEntry = ( duringTurn: boolean; } | { kind: "interrupted"; id: string } + /** + * A turn that failed, from the engine's `error` item: its reason, and `latest` when it is the + * thread's newest turn, the one Retry is offered on. + */ + | { kind: "failed"; id: string; reason: string; code?: string; details?: Record; latest: boolean } /** Threads mode: a thread the lead filed, where its `createTask` call is. The card reads the task row. */ | { kind: "thread"; id: string; taskId: string } ) & { requestId?: string }; @@ -190,6 +197,8 @@ export interface LabAdapter { openSession(mode: ModeId, projectId: string): Promise; send(sessionId: string, text: string): Promise; interrupt(sessionId: string): Promise; + /** Run a failed turn of the session again, as a new request the engine links to `requestId`. */ + retry(sessionId: string, requestId: string): Promise; watchThread(sessionId: string, onChange: (view: ThreadView) => void): Unsubscribe; watchTasks(sessionId: string, onChange: (rows: TaskRow[]) => void): Unsubscribe; watchTaskWork(sessionId: string, taskId: string, onChange: (work: TaskWork) => void): Unsubscribe; @@ -228,8 +237,8 @@ const UPDATE_TASK_ACTION = taskActionName("updateTask", TASKS_BOARD); /** The flow a coding worker runs on: its run sessions answer which harness it is (`harness`). */ const CODING_FLOW = "harness-thread"; -/** The item types a thread draws. */ -const THREAD_ITEMS = ["message", "tool_output"]; +/** The item types a thread draws: a failed turn's `error` item too. */ +const THREAD_ITEMS = ["message", "tool_output", "error"]; /** One stored item, as far as the thread reads it. */ export interface Item { @@ -251,6 +260,10 @@ export interface Item { status?: string; /** A failed `tool_output` item's error: a `SUSPENSION` one is a call waiting on you (`isSuspensionWait`). */ error?: { code?: string }; + /** An `error` item's: why its turn failed. */ + message?: string; + code?: string; + details?: Record; } @@ -381,12 +394,16 @@ function filedTaskOf(item: Item): string | undefined { * and so is a delegate's answer that landed in the conversation as written (a task filed with `resultTo: * "conversation"`): either way the task's reply is one line, not the session's own words. * In threads mode (`threads`), a task the lead filed is a thread card where its `createTask` call is, and - * your line is marked when its turn filed one. + * your line is marked when its turn filed one. A failed turn's `error` item is a failed entry, `latest` on the + * newest turn, and a reply that turn never finished is marked incomplete. */ export function entriesOf(items: readonly Item[], threads = false): ThreadEntry[] { // A turn a user's line started holds that line; a notice's own turn holds none. const userTurns = new Set(items.filter((item) => item.type === "message" && item.role === "user").map((item) => item.requestId)); const filingTurns = new Set(items.filter((item) => filedTaskOf(item) !== undefined).map((item) => item.requestId)); + // A turn that failed; its reply, if it never finished, is marked incomplete. Retry is offered on the newest turn only. + const failedTurns = new Set(items.filter((item) => item.type === "error").map((item) => item.requestId)); + const newestTurn = items[items.length - 1]?.requestId; const entries: ThreadEntry[] = []; const runs = new Map(); for (const item of items) { @@ -407,6 +424,18 @@ export function entriesOf(items: readonly Item[], threads = false): ThreadEntry[ else entries.push(entry); continue; } + if (item.type === "error") { + entries.push({ + kind: "failed", + id: item.id, + reason: item.message ?? "", + ...(item.code === undefined ? {} : { code: item.code }), + ...(item.details === undefined ? {} : { details: item.details }), + latest: requestId !== undefined && requestId === newestTurn, + requestId, + }); + continue; + } const text = textOf(item); if (item.role === "user") { entries.push({ kind: "user", id: item.id, text, requestId, ...(threads && filingTurns.has(requestId) ? { sentToThread: true } : {}) }); @@ -434,7 +463,10 @@ export function entriesOf(items: readonly Item[], threads = false): ThreadEntry[ entries.push({ kind: "result", id: item.id, taskId: answer.taskId, title: `Answer from ${worker}`, status: "completed", summary: resultSummary(text), duringTurn: userTurns.has(requestId), requestId }); continue; } - if (text.length > 0) entries.push({ kind: "assistant", id: item.id, text, requestId }); + if (text.length > 0) { + const incomplete = failedTurns.has(requestId) && item.status !== "completed"; + entries.push({ kind: "assistant", id: item.id, text, ...(incomplete ? { incomplete: true } : {}), requestId }); + } } return entries; } @@ -943,6 +975,9 @@ export function createWiredLab(fsd: Client): LabAdapter { await fsd.flow(turn.flowId ?? turn.flowKind).request(turn.id).abort().catch(() => undefined); } }, + retry: async (sessionId, requestId) => { + await fsd.flow(await flowOfSession(sessionId)).request(requestId).retry(); + }, watchThread: (sessionId, onChange) => { // Items come from the session feed. The running turn is still read: the feed's runs are // child sessions, not this session's own turns. diff --git a/labs/claude-code-style/src/components/Entry.tsx b/labs/claude-code-style/src/components/Entry.tsx index 0fe2cc0322..c2d55baa0d 100644 --- a/labs/claude-code-style/src/components/Entry.tsx +++ b/labs/claude-code-style/src/components/Entry.tsx @@ -6,6 +6,7 @@ import { TASK_STATUS } from "../lib/task-status"; import type { ToolRun } from "../lib/tool-steps"; import { cn } from "../lib/utils"; import { MessageResponse } from "./flow-state/message"; +import { RequestRecovery } from "./flow-state/request-recovery"; import { composeToolCallsLabel, toolRunOpen, type ToolStepState } from "./flow-state/tool-grouping"; /** @@ -28,11 +29,14 @@ function AgentMarkdown({ text, className }: { text: string; className?: string } * session's text is Markdown; a run of tool calls is one line in plain words * that opens to a line per call (`ToolRunLine`); a background result is one collapsed line at the point the main * session took it, which opens to the task's reply; a thread in threads mode is - * a card (`ThreadCard`, which the main thread draws). `mark` is the feed's + * a card (`ThreadCard`, which the main thread draws). A failed turn is the + * registry's `request-recovery` line, with its reason; `onRetry` draws its + * Retry, so pass it only for the newest turn while nothing runs. A reply its + * turn never finished is marked incomplete. `mark` is the feed's * mark for your line (`useJumpToLatest`'s `mine`), so a line you send rises to * near the top of the feed. */ -export function Entry({ entry, mark }: { entry: ThreadEntry; mark?: SentLineMark }) { +export function Entry({ entry, mark, onRetry }: { entry: ThreadEntry; mark?: SentLineMark; onRetry?: () => Promise }) { switch (entry.kind) { case "user": return ( @@ -46,8 +50,9 @@ export function Entry({ entry, mark }: { entry: ThreadEntry; mark?: SentLineMark ); case "assistant": return ( -
+
+ {entry.incomplete ? Incomplete : null}
); case "tool": @@ -67,6 +72,12 @@ export function Entry({ entry, mark }: { entry: ThreadEntry; mark?: SentLineMark Interrupted. Background tasks keep running.
); + case "failed": + return ( +
+ +
+ ); } } diff --git a/labs/claude-code-style/src/components/Thread.tsx b/labs/claude-code-style/src/components/Thread.tsx index 9d682ae9a6..9a2869cb20 100644 --- a/labs/claude-code-style/src/components/Thread.tsx +++ b/labs/claude-code-style/src/components/Thread.tsx @@ -49,6 +49,7 @@ export function Thread({ const count = view?.entries.length ?? 0; const feed = useJumpToLatest({ resetKey: session.id }); // On the document, so the keys work wherever focus is; a sheet's Esc arrives already spent. + const retryOf = (requestId: string) => () => adapter.retry(session.id, requestId); const keys = useInterruptKeys({ enabled: running && session.projectId !== null, onInterrupt: () => void adapter.interrupt(session.id), @@ -92,7 +93,12 @@ export function Thread({ ) : ( // The request an entry came in on, so a check can tell which turn it landed in.
- +
), )} diff --git a/labs/claude-code-style/src/components/flow-state/request-recovery.tsx b/labs/claude-code-style/src/components/flow-state/request-recovery.tsx new file mode 100644 index 0000000000..6a17300205 --- /dev/null +++ b/labs/claude-code-style/src/components/flow-state/request-recovery.tsx @@ -0,0 +1,146 @@ +"use client"; + +/** + * The line a turn that didn't finish leaves in a thread: a failed turn's + * reason with a Retry, or a turn a restart cut off with Continue and Discard. + * + * Plain props, not a session: an app passes the reason it holds (an + * `ErrorItem`'s `message`, `code` and `details` spread straight in) and, for + * the request the server will retry, an `onRetry`. The server retries only a + * session's latest `failed` or `interrupted` request, so pass `onRetry` for + * that one and leave it off older lines; without it no button is drawn. Wire + * it to `client.flow(f).request(id).retry()` or `useSession().resumeLatestRequest()`. + * + * Pass `onContinue` or `onDiscard` for a turn a restart cut off (an + * `interrupted` request, as `client.requests.interrupted()` lists it) and the + * line reads as interrupted instead: Continue carries the same request on + * (`request(id).continue()`), Discard ends it (`request(id).abort()`). Retry is + * not drawn on that line: it would start a second request beside the lost one. + */ + +import { useState, type ReactNode } from "react"; +import { AlertCircle, CirclePause, Play, RotateCcw, X } from "lucide-react"; +import { cn } from "@/lib/utils"; +import { Button } from "@/components/ui/button"; +import { ErrorBody, type ErrorBodyProps } from "./error"; + +/** What the interrupted line says when the app passes no reason of its own. */ +export const INTERRUPTED_REASON = "This turn was cut off before it finished."; + +type Action = () => Promise; + +type CommonProps = { + /** Who was working: a worker's name, "Assistant". Shown before the reason. */ + who?: string; + className?: string; +}; + +/** A failed turn: its reason, and a Retry when the server will retry it. */ +export type FailedRequestProps = CommonProps & { + /** Why the turn failed: the error's message. */ + reason: string; + code?: ErrorBodyProps["code"]; + details?: ErrorBodyProps["details"]; + /** Start the request again. Omit it for a request the server won't retry. */ + onRetry?: Action; + onContinue?: undefined; + onDiscard?: undefined; +}; + +/** A turn a restart cut off: Continue, Discard, or both. Retry is not drawn on it. */ +export type InterruptedRequestProps = CommonProps & { + /** Replaces {@link INTERRUPTED_REASON}. */ + reason?: string; + code?: undefined; + details?: undefined; + onRetry?: Action; +} & ( + | { + /** Carry an interrupted request on under its own id. */ + onContinue: Action; + /** End an interrupted request for good. */ + onDiscard?: Action; + } + | { onContinue?: undefined; onDiscard: Action } + ); + +export type RequestRecoveryProps = FailedRequestProps | InterruptedRequestProps; + +export function RequestRecovery({ + reason, + code, + details, + who, + onRetry, + onContinue, + onDiscard, + className +}: RequestRecoveryProps) { + const [refused, setRefused] = useState(null); + /** The action running now, by its idle word; every button waits while one runs. */ + const [busy, setBusy] = useState(null); + const interrupted = onContinue !== undefined || onDiscard !== undefined; + + /** One action at a time: each button is disabled while any runs, and a refusal says which. */ + const run = (label: string, action: Action) => async () => { + setBusy(label); + setRefused(null); + try { + await action(); + } catch (error) { + setRefused(`${label} failed: ${error instanceof Error ? error.message : String(error)}`); + } finally { + setBusy(null); + } + }; + + const buttons: ReactNode[] = []; + const button = (key: string, idle: string, working: string, icon: ReactNode, action: Action) => { + buttons.push( + + ); + }; + if (interrupted) { + if (onContinue) button("continue", "Continue", "Continuing…", , onContinue); + if (onDiscard) button("discard", "Discard", "Discarding…", , onDiscard); + } else if (onRetry) { + button("retry", "Retry", "Retrying…", , onRetry); + } + return ( +
+ {interrupted ? ( + + ) : ( + + )} +
+ {who && ( + + {interrupted ? `${who}'s turn was cut off` : `${who} couldn't finish this turn`} + + )} + {interrupted ? ( + {reason ?? INTERRUPTED_REASON} + ) : ( + + )} + {refused && {refused}} +
+ {buttons.length > 0 &&
{buttons}
} +
+ ); +} diff --git a/labs/claude-code-style/test/failed-turn-engine.test.ts b/labs/claude-code-style/test/failed-turn-engine.test.ts new file mode 100644 index 0000000000..6d1639710b --- /dev/null +++ b/labs/claude-code-style/test/failed-turn-engine.test.ts @@ -0,0 +1,59 @@ +/** + * A failed turn on a real engine, end to end through the adapter: the engine + * writes its `error` item, the thread's feed carries it to a failed entry with + * the engine's own reason, and the adapter's `retry` runs the request again as + * a new one. Only the turn is scripted: a handler that throws once, where a + * model call without a key would. + */ +import { createClient, type ClientFetch } from "@flow-state-dev/client"; +import { defineFlow, handler } from "@flow-state-dev/core"; +import { createFlowState, createInProcessFetch, inMemoryStores } from "@flow-state-dev/engine"; +import { describe, expect, it } from "vitest"; +import { z } from "zod"; +import { createWiredLab, type ThreadEntry, type ThreadView } from "../src/adapter"; + +describe("on a real engine", () => { + it("a turn that throws shows its reason, and Retry runs it again as a new request", async () => { + let attempts = 0; + const answer = handler({ + name: "answer", + inputSchema: z.any(), + outputSchema: z.any(), + execute: async () => { + attempts += 1; + if (attempts === 1) throw new Error("AI_GATEWAY_API_KEY is not set"); + return { ok: true }; + }, + }); + const flow = defineFlow({ kind: "lab-turn", actions: { respond: { block: answer, inputSchema: z.any() } } })(); + const app = createFlowState({ flows: { "lab-turn": flow }, stores: { default: { primary: inMemoryStores() } } }); + try { + const fetcher = createInProcessFetch(app) as unknown as ClientFetch; + const client = createClient({ userId: "u1", fetcher }); + const { id: sessionId } = await client.flow("lab-turn").createSession(); + const sent = await client.flow("lab-turn").send("respond", { message: "hi" }, { sessionId }); + expect((await sent.wait()).status).toBe("failed"); + + const lab = createWiredLab(client); + const view = await new Promise((resolve) => { + const stop = lab.watchThread(sessionId, (next) => { + if (next.entries.some((e) => e.kind === "failed")) { + stop(); + resolve(next); + } + }); + }); + const line = view.entries.find((e): e is Extract => e.kind === "failed")!; + expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, latest: true }); + + await lab.retry(sessionId, line.requestId!); + const requests = await client.sessions.requests(sessionId, { includeResultOutput: true, limit: 10 }); + const again = requests.find((r) => r.id !== sent.id); + expect(again).toBeDefined(); + expect((await client.flow("lab-turn").request(again!.id).wait()).status).toBe("completed"); + expect(attempts).toBe(2); + } finally { + await app.dispose(); + } + }); +}); diff --git a/labs/claude-code-style/test/failed-turn.test.tsx b/labs/claude-code-style/test/failed-turn.test.tsx new file mode 100644 index 0000000000..337881b12b --- /dev/null +++ b/labs/claude-code-style/test/failed-turn.test.tsx @@ -0,0 +1,112 @@ +// @vitest-environment happy-dom +/** + * A turn that fails reads as a failure. The engine writes an `error` item for + * every failed request; before, the thread never read that type, so a crash + * looked like a short answer. Now it is one line under the thread with the + * reason and, on the newest turn only, a Retry that runs the turn again: the + * engine retries any failed request, so the screen is what keeps Retry off an + * older line, where it would re-run a turn the person has moved past. + */ +import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; +import { afterEach, describe, expect, it } from "vitest"; +import { entriesOf, type ThreadEntry } from "../src/adapter"; +import { Entry } from "../src/components/Entry"; +import { Thread } from "../src/components/Thread"; +import { createFakeLab } from "./fake-lab"; + +afterEach(cleanup); + +/** An `error` item as `runAction.ts` writes one for a failed request. */ +const errorItem = (id: string, requestId: string, message: string, code = "PROVIDER_ERROR") => ({ id, type: "error", status: "failed", requestId, message, code }); +const message = (id: string, requestId: string, role: string, text: string, status = "completed") => ({ + id, + type: "message", + requestId, + role, + status, + content: [{ type: "output_text", text }], +}); + +describe("the adapter reads a failed turn", () => { + it("as one failed entry with the engine's reason, and marks the reply it cut off incomplete", () => { + const entries = entriesOf([ + message("u1", "r1", "user", "Fix the build"), + message("a1", "r1", "assistant", "Looking at the", "in_progress"), + errorItem("e1", "r1", "AI_GATEWAY_API_KEY is not set"), + ]); + expect(entries.map((e) => e.kind)).toEqual(["user", "assistant", "failed"]); + expect(entries[1]).toMatchObject({ kind: "assistant", incomplete: true }); + expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", latest: true }); + }); + + it("offers Retry on the newest turn only", () => { + const entries = entriesOf([ + message("u1", "r1", "user", "first"), + errorItem("e1", "r1", "first failed"), + message("u2", "r2", "user", "second"), + errorItem("e2", "r2", "second failed"), + ]); + const failed = entries.filter((e): e is Extract => e.kind === "failed"); + expect(failed.map((e) => [e.reason, e.latest])).toEqual([ + ["first failed", false], + ["second failed", true], + ]); + }); + + it("leaves a finished reply of a turn that worked alone", () => { + const entries = entriesOf([message("u1", "r1", "user", "hi"), message("a1", "r1", "assistant", "Hello.")]); + expect(entries[1]).not.toHaveProperty("incomplete"); + }); +}); + +describe("the line a failed turn draws", () => { + const failed = (latest: boolean): ThreadEntry => ({ kind: "failed", id: "e1", reason: "The model refused the key.", latest, requestId: "r1" }); + + it("names who failed and why, and Retry calls back", async () => { + let retried = 0; + render( void (retried += 1)} />); + const line = screen.getByTestId("request-recovery"); + expect(line.textContent).toMatch(/The main session couldn't finish this turn/); + expect(line.textContent).toMatch(/The model refused the key\./); + await act(async () => fireEvent.click(screen.getByRole("button", { name: /Retry/ }))); + expect(retried).toBe(1); + }); + + it("draws no Retry without a callback", () => { + render(); + expect(screen.getByTestId("request-recovery").textContent).toMatch(/The model refused the key\./); + expect(screen.queryByRole("button", { name: /Retry/ })).toBeNull(); + }); +}); + +describe("the main thread", () => { + it("shows a failed turn's line and retries the newest one through the adapter", async () => { + const fake = createFakeLab({ speed: 0.001 }); + const row = await fake.openSession("session", "project-vendor-notes"); + await act(async () => { + await fake.send(row.id, "Fix the build"); + }); + act(() => fake.failTurn(row.id, "AI_GATEWAY_API_KEY is not set")); + render( undefined} />); + expect(screen.getByTestId("request-recovery").textContent).toMatch(/AI_GATEWAY_API_KEY is not set/); + await act(async () => fireEvent.click(screen.getByRole("button", { name: /Retry/ }))); + expect(fake.retries).toEqual([{ sessionId: row.id, requestId: expect.any(String) }]); + // The turn runs again, so the line keeps its reason but offers no second Retry. + expect(screen.getByTestId("request-recovery").textContent).toMatch(/AI_GATEWAY_API_KEY is not set/); + expect(screen.queryByRole("button", { name: /Retry/ })).toBeNull(); + }); + + it("offers Retry on the newest of two failed turns, not the older one", async () => { + const fake = createFakeLab({ speed: 0.001 }); + const row = await fake.openSession("session", "project-vendor-notes"); + for (const line of ["first", "second"]) { + await act(async () => { + await fake.send(row.id, line); + }); + act(() => fake.failTurn(row.id, `${line} failed`)); + } + render( undefined} />); + const lines = screen.getAllByTestId("request-recovery"); + expect(lines.map((line) => line.textContent?.includes("Retry"))).toEqual([false, true]); + }); +}); diff --git a/labs/claude-code-style/test/fake-lab.ts b/labs/claude-code-style/test/fake-lab.ts index 489962c4c9..0031ba66a9 100644 --- a/labs/claude-code-style/test/fake-lab.ts +++ b/labs/claude-code-style/test/fake-lab.ts @@ -16,7 +16,8 @@ * the judge's verdict. * * In either mode, an ask that says "stop" or "cancel" while a task runs makes - * the main session cancel it. A line sent while a turn runs waits, and runs + * the main session cancel it. `failTurn` fails the running turn, as an + * engine `error` item would; Retry runs that turn's line again. A line sent while a turn runs waits, and runs * when the turn ends. */ import type { @@ -49,6 +50,10 @@ export interface FakeLab extends LabAdapter { setConnection(state: ConnectionState): void; /** The open task titled `title`, in any session, fails with `error`, as its run would. */ fail(title: string, error: string): void; + /** The running turn of session `sessionId` fails with `reason`: a reply under way is left incomplete. */ + failTurn(sessionId: string, reason: string): void; + /** Each `retry` the screen made, in order. */ + readonly retries: { sessionId: string; requestId: string }[]; } interface FixtureTask extends TaskRow { @@ -417,7 +422,23 @@ export function createFakeLab(options: FakeOptions = {}): FakeLab { return task; }; + const retries: { sessionId: string; requestId: string }[] = []; + return { + retries, + failTurn(sessionId, reason) { + const s = session(sessionId); + if (!s.running) return; + for (const t of s.timers) clearTimeout(t); + s.timers = []; + // The turn's line names it, so Retry can run it again; an older failure is no longer the newest turn. + const line = [...s.entries].reverse().find((e) => e.kind === "user"); + const requestId = `turn-${line?.id ?? nextId("r")}`; + const last = s.entries[s.entries.length - 1]; + s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e === last && e.kind === "assistant" ? { ...e, incomplete: true } : e)); + push(s, { kind: "failed", id: nextId("f"), reason, latest: true, requestId }); + endTurn(s); + }, setConnection(state) { connection = state; notify(); @@ -475,6 +496,15 @@ export function createFakeLab(options: FakeOptions = {}): FakeLab { endTurn(s); }, + async retry(sessionId, requestId) { + retries.push({ sessionId, requestId }); + const s = session(sessionId); + const line = s.entries.find((e) => e.kind === "user" && `turn-${e.id}` === requestId); + if (line?.kind !== "user") throw new Error(`No failed turn "${requestId}" to retry.`); + s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e)); + turn(s, line.text, line.id); + }, + watchThread(sessionId, onChange) { return watch(() => { const s = sessions.get(sessionId); diff --git a/labs/teammates/README.md b/labs/teammates/README.md index a6f3f9f985..661a5c505f 100644 --- a/labs/teammates/README.md +++ b/labs/teammates/README.md @@ -199,6 +199,7 @@ names the package call it makes. | On screen | Package piece | |---|---| | The worker list, sending, the conversation, busy status | the workforce client's `roster()` and `ensureWorkerSession`, the flow's `respond`, session items, `listSessionRequests` | +| A failed turn and *Retry* | the engine's `error` item for the turn, drawn by the UI registry's `request-recovery`, installed in `src/components/flow-state/`; Retry is the client's `request(id).retry()`, on the newest turn only | | Workers messaging each other | `sendMessage`, the received-message item (`workforce:received`), and the message log | | Hiring, changing and firing your own teammates | the workforce client's `hire`, `edit` and `fire`, on the roster flow | | Task lines | the conversation board's rows | diff --git a/labs/teammates/src/adapter.ts b/labs/teammates/src/adapter.ts index 93e686f9aa..734a123c69 100644 --- a/labs/teammates/src/adapter.ts +++ b/labs/teammates/src/adapter.ts @@ -15,6 +15,7 @@ * | | via `client.subscribe` | * | `watchConnection` | the same feed's connection state: `live`, `lapsed` once a retry has failed… | * | `answerApproval` | the request's `answer`: approve or reject a `user_approval` stop | + * | `retry` | the request's `retry()` on the conversation's flow: a failed turn again | * | `watchTalk` | the user's message log (`workforceMessages`), as `sendMessage` writes it | * | `watchActivity` | `listSessionRequests` on the worker's conversation, on each request notice | * | `watchWindow` | the last answering request's `historyStart`, and the session's `historySummary` | @@ -229,8 +230,13 @@ export interface SessionStep { /** One entry of a worker's conversation, in the order it happened. */ export type ConversationEntry = | { kind: "user"; id: string; text: string; queued?: boolean } - /** `streaming` while the answer is still being written. */ - | { kind: "assistant"; id: string; text: string; streaming?: boolean } + /** `streaming` while the answer is still being written; `incomplete` when its turn failed before it was finished. */ + | { kind: "assistant"; id: string; text: string; streaming?: boolean; incomplete?: boolean } + /** + * A turn that failed, from the engine's `error` item: its reason, its request, and `latest` when + * it is the conversation's newest turn, the one Retry is offered on. + */ + | { kind: "failed"; id: string; requestId: string; reason: string; code?: string; details?: Record; latest: boolean } /** The worker messaged another worker (`sendMessage`). */ | { kind: "sent"; id: string; messageId: string; to: string; text: string } /** A message another worker sent, as the `receive` turn that read it. */ @@ -328,6 +334,8 @@ export interface LabAdapter { watchConnection(sessionId: string, onChange: (state: SessionConnectionState) => void): Unsubscribe; /** Say yes or no to what an approval entry asks; the worker's turn carries on. */ answerApproval(sessionId: string, approval: { requestId: string; suspensionId: string }, approve: boolean): Promise; + /** Run a failed turn of the conversation again, as a new request the engine links to `requestId`. */ + retry(sessionId: string, requestId: string): Promise; /** Every message to or from `worker`, oldest first. */ watchTalk(worker: string, onChange: (rows: TalkRow[]) => void): Unsubscribe; watchActivity(worker: string, onChange: (activity: Activity) => void): Unsubscribe; @@ -705,15 +713,30 @@ function harnessAndModel(spend: { harness?: string; model?: string }, setting: s /** * A conversation's items as the entries the screen draws. `ended` names the * requests the server lists as over: a step their turn left running (a turn - * that crashed mid-call) reads failed, not running for good. + * that crashed mid-call) reads failed, not running for good. A failed turn's + * `error` item is a failed entry, `latest` on the newest turn that drew one. */ export function toEntries(items: readonly Item[], workers: ReadonlySet, ended: readonly string[] = []): ConversationEntry[] { const rows = boardRows(items); const entries: ConversationEntry[] = []; /** The turn the last run of steps belongs to: a run never spans two. */ let runTurn: unknown; - for (const item of items) { - if (item.type === "message" && item.role === "user") { + // A turn that failed; its reply, if it never finished, is marked incomplete. + const failedTurns = new Set(items.filter((item) => item.type === "error").map((item) => item.requestId)); + /** The newest turn that drew an entry: a board pass that drew none is not a turn you saw. Retry is offered on it only. */ + let newestTurn: unknown; + const drawEntry = (item: Item): void => { + if (item.type === "error") { + entries.push({ + kind: "failed", + id: item.id, + requestId: item.requestId, + reason: typeof item.message === "string" ? item.message : "", + ...(typeof item.code === "string" ? { code: item.code } : {}), + ...(item.details === undefined ? {} : { details: item.details }), + latest: false, + }); + } else if (item.type === "message" && item.role === "user") { entries.push({ kind: "user", id: item.id, text: textOf(item) }); } else if (item.type === "message" && item.role === "assistant" && workers.has(item.agentName)) { // A task it filed ended: the board's line under the worker that ran it, which names its task @@ -738,7 +761,9 @@ export function toEntries(items: readonly Item[], workers: ReadonlySet, }); } else if (item.type === "message" && item.role === "assistant") { const text = textOf(item); - if (text.trim() !== "") entries.push({ kind: "assistant", id: item.id, text, ...(item.status === "in_progress" ? { streaming: true } : {}) }); + const incomplete = failedTurns.has(item.requestId) && item.status !== "completed"; + if (text.trim() !== "") + entries.push({ kind: "assistant", id: item.id, text, ...(incomplete ? { incomplete: true } : item.status === "in_progress" ? { streaming: true } : {}) }); } else if (item.type === "tool_output" && item.toolCall?.name === SEND_MESSAGE_TOOL && sentTo(item) !== undefined) { const args = parseArgs(item); entries.push({ kind: "sent", id: item.id, messageId: item.output.sent, to: String(args.to), text: String(args.text ?? "") }); @@ -765,8 +790,13 @@ export function toEntries(items: readonly Item[], workers: ReadonlySet, const { id, from, text, inReplyTo, noAnswer } = item.data ?? {}; entries.push({ kind: "received", id: item.id, messageId: id, from, text, ...(inReplyTo === undefined ? {} : { inReplyTo }), ...(noAnswer === true ? { noAnswer: true } : {}) }); } + }; + for (const item of items) { + const drawn = entries.length; + drawEntry(item); + if (entries.length !== drawn) newestTurn = item.requestId; } - return entries; + return entries.map((entry) => (entry.kind === "failed" && entry.requestId === newestTurn ? { ...entry, latest: true } : entry)); } /** @@ -958,6 +988,10 @@ export function createLabAdapter(client: Client = connect()): LabAdapter { if (flow === undefined) throw new Error(`teammates: ${sessionId} is not an open conversation`); await client.flow(flow).request(approval.requestId).answer({ suspensionId: approval.suspensionId, action: approve ? "approve" : "reject" }); }, + async retry(sessionId, requestId) { + const flow = flowOfSession.get(sessionId) ?? (await client.sessions.get(sessionId)).flowKind; + await client.flow(flow).request(requestId).retry(); + }, watchConnection(sessionId, onChange) { return feeds(sessionId).watchState(onChange); }, diff --git a/labs/teammates/src/components/Conversation.tsx b/labs/teammates/src/components/Conversation.tsx index c06ba897f1..856ca0bf29 100644 --- a/labs/teammates/src/components/Conversation.tsx +++ b/labs/teammates/src/components/Conversation.tsx @@ -189,6 +189,8 @@ export function Conversation({ onOpenMessage={openMessage} onOpenTask={openTask} {...(entry.kind === "approval" ? { onAnswer: (approve: boolean) => adapter.answerApproval(sessionId, entry, approve) } : {})} + // Retry only the newest turn, and not while one runs: the engine runs a failed request again as a new one. + {...(entry.kind === "failed" && entry.latest && !running ? { onRetry: () => adapter.retry(sessionId, entry.requestId) } : {})} /> )} diff --git a/labs/teammates/src/components/Entry.tsx b/labs/teammates/src/components/Entry.tsx index 88ebc41043..091d69bb94 100644 --- a/labs/teammates/src/components/Entry.tsx +++ b/labs/teammates/src/components/Entry.tsx @@ -5,6 +5,7 @@ import { Avatar } from "./Avatar"; import type { AnswerState } from "./Popups"; import { useSubmit } from "@flow-state-dev/react"; import { AgentMarkdown } from "./flow-state/message-response"; +import { RequestRecovery } from "./flow-state/request-recovery"; import { ToolRun } from "./ToolRun"; /** @@ -18,6 +19,10 @@ import { ToolRun } from "./ToolRun"; * yes (`suspendForUser` with an approval) is a card with Approve and Deny until * you answer. The tool calls a turn makes show inline as it makes them, a run * of them as one line that is open while it runs and folds once it is done. + * A failed turn is the registry's `request-recovery` line under the worker's + * name, with its reason; `onRetry` draws its Retry, so pass it only for the + * newest turn while nothing runs. A reply its turn never finished is marked + * incomplete. * Memoized: a streamed chunk redraws only the entry that changed. */ export const Entry = memo(function Entry({ @@ -30,6 +35,7 @@ export const Entry = memo(function Entry({ onOpenMessage, onOpenTask, onAnswer, + onRetry, }: { entry: ConversationEntry; workerName: string; @@ -45,6 +51,8 @@ export const Entry = memo(function Entry({ /** Open the task a result came from. */ onOpenTask?: (taskId: string) => void; onAnswer?: (approve: boolean) => Promise; + /** Run a failed turn again. */ + onRetry?: () => Promise; }) { switch (entry.kind) { case "user": @@ -58,8 +66,15 @@ export const Entry = memo(function Entry({ ); case "assistant": return ( -
+
+ {entry.incomplete ? Incomplete : null} +
+ ); + case "failed": + return ( +
+
); case "sent": diff --git a/labs/teammates/src/components/flow-state/request-recovery.tsx b/labs/teammates/src/components/flow-state/request-recovery.tsx new file mode 100644 index 0000000000..6a17300205 --- /dev/null +++ b/labs/teammates/src/components/flow-state/request-recovery.tsx @@ -0,0 +1,146 @@ +"use client"; + +/** + * The line a turn that didn't finish leaves in a thread: a failed turn's + * reason with a Retry, or a turn a restart cut off with Continue and Discard. + * + * Plain props, not a session: an app passes the reason it holds (an + * `ErrorItem`'s `message`, `code` and `details` spread straight in) and, for + * the request the server will retry, an `onRetry`. The server retries only a + * session's latest `failed` or `interrupted` request, so pass `onRetry` for + * that one and leave it off older lines; without it no button is drawn. Wire + * it to `client.flow(f).request(id).retry()` or `useSession().resumeLatestRequest()`. + * + * Pass `onContinue` or `onDiscard` for a turn a restart cut off (an + * `interrupted` request, as `client.requests.interrupted()` lists it) and the + * line reads as interrupted instead: Continue carries the same request on + * (`request(id).continue()`), Discard ends it (`request(id).abort()`). Retry is + * not drawn on that line: it would start a second request beside the lost one. + */ + +import { useState, type ReactNode } from "react"; +import { AlertCircle, CirclePause, Play, RotateCcw, X } from "lucide-react"; +import { cn } from "@/lib/utils"; +import { Button } from "@/components/ui/button"; +import { ErrorBody, type ErrorBodyProps } from "./error"; + +/** What the interrupted line says when the app passes no reason of its own. */ +export const INTERRUPTED_REASON = "This turn was cut off before it finished."; + +type Action = () => Promise; + +type CommonProps = { + /** Who was working: a worker's name, "Assistant". Shown before the reason. */ + who?: string; + className?: string; +}; + +/** A failed turn: its reason, and a Retry when the server will retry it. */ +export type FailedRequestProps = CommonProps & { + /** Why the turn failed: the error's message. */ + reason: string; + code?: ErrorBodyProps["code"]; + details?: ErrorBodyProps["details"]; + /** Start the request again. Omit it for a request the server won't retry. */ + onRetry?: Action; + onContinue?: undefined; + onDiscard?: undefined; +}; + +/** A turn a restart cut off: Continue, Discard, or both. Retry is not drawn on it. */ +export type InterruptedRequestProps = CommonProps & { + /** Replaces {@link INTERRUPTED_REASON}. */ + reason?: string; + code?: undefined; + details?: undefined; + onRetry?: Action; +} & ( + | { + /** Carry an interrupted request on under its own id. */ + onContinue: Action; + /** End an interrupted request for good. */ + onDiscard?: Action; + } + | { onContinue?: undefined; onDiscard: Action } + ); + +export type RequestRecoveryProps = FailedRequestProps | InterruptedRequestProps; + +export function RequestRecovery({ + reason, + code, + details, + who, + onRetry, + onContinue, + onDiscard, + className +}: RequestRecoveryProps) { + const [refused, setRefused] = useState(null); + /** The action running now, by its idle word; every button waits while one runs. */ + const [busy, setBusy] = useState(null); + const interrupted = onContinue !== undefined || onDiscard !== undefined; + + /** One action at a time: each button is disabled while any runs, and a refusal says which. */ + const run = (label: string, action: Action) => async () => { + setBusy(label); + setRefused(null); + try { + await action(); + } catch (error) { + setRefused(`${label} failed: ${error instanceof Error ? error.message : String(error)}`); + } finally { + setBusy(null); + } + }; + + const buttons: ReactNode[] = []; + const button = (key: string, idle: string, working: string, icon: ReactNode, action: Action) => { + buttons.push( + + ); + }; + if (interrupted) { + if (onContinue) button("continue", "Continue", "Continuing…", , onContinue); + if (onDiscard) button("discard", "Discard", "Discarding…", , onDiscard); + } else if (onRetry) { + button("retry", "Retry", "Retrying…", , onRetry); + } + return ( +
+ {interrupted ? ( + + ) : ( + + )} +
+ {who && ( + + {interrupted ? `${who}'s turn was cut off` : `${who} couldn't finish this turn`} + + )} + {interrupted ? ( + {reason ?? INTERRUPTED_REASON} + ) : ( + + )} + {refused && {refused}} +
+ {buttons.length > 0 &&
{buttons}
} +
+ ); +} diff --git a/labs/teammates/test/failed-turn-engine.test.ts b/labs/teammates/test/failed-turn-engine.test.ts new file mode 100644 index 0000000000..15fb1a5dd0 --- /dev/null +++ b/labs/teammates/test/failed-turn-engine.test.ts @@ -0,0 +1,57 @@ +/** + * A failed turn on a real engine, end to end through the adapter: the engine + * writes its `error` item, the conversation's feed carries it to a failed + * entry with the engine's own reason, and the adapter's `retry` runs the + * request again as a new one. Only the turn is scripted: a handler that throws + * once, where a model call without a key would. + */ +import { createClient, type ClientFetch } from "@flow-state-dev/client"; +import { defineFlow, handler } from "@flow-state-dev/core"; +import { createFlowState, createInProcessFetch, inMemoryStores } from "@flow-state-dev/engine"; +import { describe, expect, it } from "vitest"; +import { z } from "zod"; +import { createLabAdapter, type ConversationEntry, type ConversationView } from "../src/adapter"; + +describe("on a real engine", () => { + it("a turn that throws shows its reason, and Retry runs it again as a new request", async () => { + let attempts = 0; + const answer = handler({ + name: "answer", + inputSchema: z.any(), + outputSchema: z.any(), + execute: async () => { + attempts += 1; + if (attempts === 1) throw new Error("AI_GATEWAY_API_KEY is not set"); + return { ok: true }; + }, + }); + const flow = defineFlow({ kind: "lab-turn", actions: { respond: { block: answer, inputSchema: z.any() } } })(); + const app = createFlowState({ flows: { "lab-turn": flow }, stores: { default: { primary: inMemoryStores() } } }); + try { + const client = createClient({ userId: "u1", fetcher: createInProcessFetch(app) as unknown as ClientFetch }); + const { id: sessionId } = await client.flow("lab-turn").createSession(); + const sent = await client.flow("lab-turn").send("respond", { message: "hi" }, { sessionId }); + expect((await sent.wait()).status).toBe("failed"); + + const lab = createLabAdapter(client); + const view = await new Promise((resolve) => { + const stop = lab.watchConversation(sessionId, (next) => { + if (next.entries.some((e) => e.kind === "failed")) { + stop(); + resolve(next); + } + }); + }); + const line = view.entries.find((e): e is Extract => e.kind === "failed")!; + expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, latest: true }); + + await lab.retry(sessionId, line.requestId); + const again = (await client.sessions.requests(sessionId, { limit: 10 })).find((r) => r.id !== sent.id); + expect(again).toBeDefined(); + expect((await client.flow("lab-turn").request(again!.id).wait()).status).toBe("completed"); + expect(attempts).toBe(2); + } finally { + await app.dispose(); + } + }); +}); diff --git a/labs/teammates/test/failed-turn.test.tsx b/labs/teammates/test/failed-turn.test.tsx new file mode 100644 index 0000000000..3d6e27e6fa --- /dev/null +++ b/labs/teammates/test/failed-turn.test.tsx @@ -0,0 +1,104 @@ +// @vitest-environment happy-dom +/** + * A turn that fails reads as a failure. The engine writes an `error` item for + * every failed request; before, the conversation dropped that item, so a crash + * looked like a short answer. Now it is one line under the worker's name with + * the reason and, on the newest turn only, a Retry that runs the turn again: + * the engine retries any failed request, so the screen is what keeps Retry off + * an older line, where it would re-run a turn the person has moved past. + */ +import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { afterEach, describe, expect, it } from "vitest"; +import { App } from "../src/App"; +import { toEntries, type ConversationEntry } from "../src/adapter"; +import { Entry } from "../src/components/Entry"; +import { lookOf } from "../src/lib/looks"; +import { createFixtureLab } from "./fake-lab"; + +afterEach(() => { + cleanup(); + window.localStorage.clear(); + window.history.replaceState(null, "", "/"); +}); + +const LEAD = "teammates.chief-of-staff"; +/** An `error` item as `runAction.ts` writes one for a failed request. */ +const errorItem = (id: string, requestId: string, message: string, code = "PROVIDER_ERROR") => ({ id, type: "error", status: "failed", requestId, message, code }); +const message = (id: string, requestId: string, role: string, text: string, status = "completed") => ({ + id, + type: "message", + requestId, + role, + agentName: role === "assistant" ? LEAD : undefined, + status, + content: [{ type: "output_text", text }], +}); +/** A board pass's row change: an item that draws no entry. */ +const boardPass = (id: string, requestId: string) => ({ id, type: "component", component: "task-change", status: "completed", requestId, data: { task: { id: "t1", status: "pending", assignee: LEAD, title: "t1" } } }); +const failedOf = (entries: ConversationEntry[]) => entries.filter((e): e is Extract => e.kind === "failed"); + +describe("the adapter reads a failed turn", () => { + it("as one failed entry with the engine's reason, and marks the reply it cut off incomplete", () => { + const entries = toEntries( + [message("u1", "r1", "user", "Draft the launch post"), message("a1", "r1", "assistant", "Here is a first", "in_progress"), errorItem("e1", "r1", "AI_GATEWAY_API_KEY is not set")], + new Set(), + ); + expect(entries.map((e) => e.kind)).toEqual(["user", "assistant", "failed"]); + expect(entries[1]).toEqual({ kind: "assistant", id: "a1", text: "Here is a first", incomplete: true }); + expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", latest: true }); + }); + + it("offers Retry on the newest turn only", () => { + const entries = toEntries( + [message("u1", "r1", "user", "first"), errorItem("e1", "r1", "first failed"), message("u2", "r2", "user", "second"), errorItem("e2", "r2", "second failed")], + new Set(), + ); + expect(failedOf(entries).map((e) => [e.reason, e.latest])).toEqual([ + ["first failed", false], + ["second failed", true], + ]); + }); + + it("keeps Retry on a failed turn a later board pass wrote no entry for", () => { + const entries = toEntries([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed"), boardPass("c1", "dispatch-1")], new Set()); + expect(failedOf(entries).map((e) => e.latest)).toEqual([true]); + }); +}); + +describe("the line a failed turn draws", () => { + const failed = (latest: boolean): ConversationEntry => ({ kind: "failed", id: "e1", requestId: "r1", reason: "The model refused the key.", latest }); + const draw = (entry: ConversationEntry, onRetry?: () => Promise) => + render( id} lookOf={(id) => lookOf({ id, name: id })} {...(onRetry ? { onRetry } : {})} />); + + it("names the worker and the reason, and Retry calls back", async () => { + let retried = 0; + draw(failed(true), async () => void (retried += 1)); + const line = screen.getByTestId("request-recovery"); + expect(line.textContent).toMatch(/Chief of Staff couldn't finish this turn/); + expect(line.textContent).toMatch(/The model refused the key\./); + await act(async () => fireEvent.click(screen.getByRole("button", { name: /Retry/ }))); + expect(retried).toBe(1); + }); + + it("draws no Retry without a callback", () => { + draw(failed(false)); + expect(screen.queryByRole("button", { name: /Retry/ })).toBeNull(); + }); +}); + +describe("the conversation", () => { + it("shows a failed turn's line and retries only the newest through the adapter", async () => { + const lab = createFixtureLab({ speed: 0.05 }); + const sessionId = await lab.openConversation(LEAD); + lab.failTurn(sessionId, "first failed"); + lab.failTurn(sessionId, "AI_GATEWAY_API_KEY is not set"); + render(); + await waitFor(() => expect(screen.getAllByTestId("request-recovery")).toHaveLength(2)); + const lines = screen.getAllByTestId("request-recovery"); + expect(lines.map((line) => line.textContent?.includes("Retry"))).toEqual([false, true]); + expect(lines[1]!.textContent).toMatch(/AI_GATEWAY_API_KEY is not set/); + await act(async () => fireEvent.click(screen.getByRole("button", { name: /Retry/ }))); + expect(lab.retries).toEqual([{ sessionId, requestId: expect.any(String) }]); + await waitFor(() => expect(screen.queryByRole("button", { name: /Retry/ })).toBeNull()); + }); +}); diff --git a/labs/teammates/test/fake-lab.ts b/labs/teammates/test/fake-lab.ts index 04e22b2281..f8361ac61a 100644 --- a/labs/teammates/test/fake-lab.ts +++ b/labs/teammates/test/fake-lab.ts @@ -18,6 +18,8 @@ * answers one request at a time: a line you send, or a message from another * worker, waits while the worker is answering, and runs when it is done. * Chief of Staff's conversation starts past the 50-turn history window. + * `failTurn` leaves a failed turn in a conversation, as an engine `error` item + * would; Retry runs that turn's line again. */ import type { SessionConnectionState } from "@flow-state-dev/client"; import { @@ -59,6 +61,10 @@ export interface FixtureOptions { export interface FixtureLab extends LabAdapter { /** Every conversation's feed reports `state` from now on. */ setConnection(state: SessionConnectionState): void; + /** The newest turn of conversation `sessionId` failed with `reason`: a reply under way is left incomplete. */ + failTurn(sessionId: string, reason: string): void; + /** Each `retry` the screen made, in order. */ + readonly retries: { sessionId: string; requestId: string }[]; } /** @@ -463,11 +469,34 @@ export function createFixtureLab(options: FixtureOptions = {}): FixtureLab { now() - 2 * 60 * MINUTE, ); + const retries: { sessionId: string; requestId: string }[] = []; + return { setConnection(state) { connection = state; notify(); }, + retries, + failTurn(sessionId, reason) { + const w = bySession(sessionId); + if (!w) throw new Error(`no conversation ${sessionId}`); + // The turn's line names it, so Retry can run it again; an older failure is no longer the newest turn. + const line = [...w.entries].reverse().find((e) => e.kind === "user"); + const last = w.entries[w.entries.length - 1]; + w.entries = w.entries.map((e) => + e.kind === "failed" ? { ...e, latest: false } : e === last && e.kind === "assistant" ? { kind: "assistant", id: e.id, text: e.text, incomplete: true } : e, + ); + push(w, { kind: "failed", id: nextId("f"), requestId: `turn-${line?.id ?? nextId("r")}`, reason, latest: true }); + }, + async retry(sessionId, requestId) { + retries.push({ sessionId, requestId }); + const w = bySession(sessionId); + const line = w?.entries.find((e) => e.kind === "user" && `turn-${e.id}` === requestId); + if (!w || line?.kind !== "user") throw new Error(`No failed turn "${requestId}" to retry.`); + w.entries = w.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e)); + w.queue.push({ kind: "user", entryId: line.id, text: line.text }); + startNext(w); + }, async listWorkers() { if (unreachable > 0) { From 870950220bb163cf18feaeb566618a57e3b0d9bc Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 21:50:04 +0000 Subject: [PATCH 2/4] chore(fallow): ignore the labs' byte copies of request-recovery (FIX-2550) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NczDiDAAfKeTwqH2bFcZLD --- .fallowrc.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.fallowrc.json b/.fallowrc.json index 571b64cec2..16fe6c05e2 100644 --- a/.fallowrc.json +++ b/.fallowrc.json @@ -106,6 +106,8 @@ "labs/claude-code-style/src/components/flow-state/tool-grouping.ts", // and of the registry's error, whose `ErrorDisplay` export its sheet doesn't draw. "labs/claude-code-style/src/components/flow-state/error.tsx", + // and of the registry's request-recovery, whose interrupted-turn default the failed-turn line doesn't read. + "labs/claude-code-style/src/components/flow-state/request-recovery.tsx", // The stock shadcn copies the Teammates lab and Shift Manager palettes are built on. "labs/teammates/src/components/ui/**", "packages/shift-manager/src/components/ui/command.tsx", @@ -119,6 +121,8 @@ "labs/teammates/src/components/flow-state/review-hold.tsx", // and of the registry's error, whose `ErrorDisplay` export its task popup doesn't draw. "labs/teammates/src/components/flow-state/error.tsx", + // and of the registry's request-recovery, whose interrupted-turn default the failed-turn line doesn't read. + "labs/teammates/src/components/flow-state/request-recovery.tsx", // The Teammates lab's byte copy of the registry's connection screen; its // backoff export is for apps that time their own retries. "labs/teammates/src/components/flow-state/connection-screen.tsx", From 14ac1f440130f42cbc8216841ff0c326c4480ca2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 22:03:45 +0000 Subject: [PATCH 3/4] FIX-2550: offer Retry only on turns the engine will retry The engine refuses a retry of a request a task or the flow itself started (source internal/task), so a Retry on such a failed newest turn could only fail. The failed entry's `latest` becomes `retryable`: the newest turn and one of the session's recent requests from a source the engine re-enters (http, mcp, scheduled; a record with no source is http). Both labs pin their list to the engine's PUBLIC_REENTRY_SOURCES in a test. Claude Code-style reads the running turn and the retryable turns from one request listing, so a request notice is still one read. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NczDiDAAfKeTwqH2bFcZLD --- labs/claude-code-style/src/adapter.ts | 44 ++++++++++++------- .../src/components/Thread.tsx | 2 +- .../test/failed-turn-engine.test.ts | 2 +- .../test/failed-turn.test.tsx | 29 ++++++++---- labs/claude-code-style/test/fake-lab.ts | 6 +-- labs/teammates/src/adapter.ts | 37 ++++++++++++---- .../teammates/src/components/Conversation.tsx | 2 +- .../teammates/test/failed-turn-engine.test.ts | 2 +- labs/teammates/test/failed-turn.test.tsx | 35 +++++++++++---- labs/teammates/test/fake-lab.ts | 6 +-- 10 files changed, 114 insertions(+), 51 deletions(-) diff --git a/labs/claude-code-style/src/adapter.ts b/labs/claude-code-style/src/adapter.ts index 0780c3199f..9ef225c7ed 100644 --- a/labs/claude-code-style/src/adapter.ts +++ b/labs/claude-code-style/src/adapter.ts @@ -114,10 +114,10 @@ export type ThreadEntry = ( } | { kind: "interrupted"; id: string } /** - * A turn that failed, from the engine's `error` item: its reason, and `latest` when it is the - * thread's newest turn, the one Retry is offered on. + * A turn that failed, from the engine's `error` item: its reason, and `retryable` when Retry is + * offered on it: it is the thread's newest turn, and one the engine will run again for you. */ - | { kind: "failed"; id: string; reason: string; code?: string; details?: Record; latest: boolean } + | { kind: "failed"; id: string; reason: string; code?: string; details?: Record; retryable: boolean } /** Threads mode: a thread the lead filed, where its `createTask` call is. The card reads the task row. */ | { kind: "thread"; id: string; taskId: string } ) & { requestId?: string }; @@ -237,6 +237,13 @@ const UPDATE_TASK_ACTION = taskActionName("updateTask", TASKS_BOARD); /** The flow a coding worker runs on: its run sessions answer which harness it is (`harness`). */ const CODING_FLOW = "harness-thread"; +/** + * The request sources the engine retries for a caller (`PUBLIC_REENTRY_SOURCES`): a turn that came + * in another way, such as a worker's message (`internal`) or a task's run (`task`), it refuses. + * A copy, since the page can't import the engine; a test pins it to the engine's. + */ +export const RETRYABLE_SOURCES: ReadonlySet = new Set(["http", "mcp", "scheduled"]); + /** The item types a thread draws: a failed turn's `error` item too. */ const THREAD_ITEMS = ["message", "tool_output", "error"]; @@ -394,10 +401,11 @@ function filedTaskOf(item: Item): string | undefined { * and so is a delegate's answer that landed in the conversation as written (a task filed with `resultTo: * "conversation"`): either way the task's reply is one line, not the session's own words. * In threads mode (`threads`), a task the lead filed is a thread card where its `createTask` call is, and - * your line is marked when its turn filed one. A failed turn's `error` item is a failed entry, `latest` on the - * newest turn, and a reply that turn never finished is marked incomplete. + * your line is marked when its turn filed one. A failed turn's `error` item is a failed entry, `retryable` + * when it is the newest turn and one of `retryableTurns` (the requests the engine will retry), and a reply + * that turn never finished is marked incomplete. */ -export function entriesOf(items: readonly Item[], threads = false): ThreadEntry[] { +export function entriesOf(items: readonly Item[], threads = false, retryableTurns: ReadonlySet = new Set()): ThreadEntry[] { // A turn a user's line started holds that line; a notice's own turn holds none. const userTurns = new Set(items.filter((item) => item.type === "message" && item.role === "user").map((item) => item.requestId)); const filingTurns = new Set(items.filter((item) => filedTaskOf(item) !== undefined).map((item) => item.requestId)); @@ -431,7 +439,7 @@ export function entriesOf(items: readonly Item[], threads = false): ThreadEntry[ reason: item.message ?? "", ...(item.code === undefined ? {} : { code: item.code }), ...(item.details === undefined ? {} : { details: item.details }), - latest: requestId !== undefined && requestId === newestTurn, + retryable: requestId !== undefined && requestId === newestTurn && retryableTurns.has(requestId), requestId, }); continue; @@ -589,10 +597,12 @@ export function createWiredLab(fsd: Client): LabAdapter { ); /** The turns running in a session, oldest first. */ - const runningTurns = async (sessionId: string) => - (await fsd.sessions.requests(sessionId, { status: "in_progress", limit: 50 })) - .filter((request) => TURN_ACTIONS.has(request.actionName)) + /** Of a session's requests, its turns still running, oldest first. */ + const turnsRunningIn = (requests: Awaited>) => + requests + .filter((request) => request.status === "in_progress" && TURN_ACTIONS.has(request.actionName)) .sort((a, b) => (a.startedAtMs ?? 0) - (b.startedAtMs ?? 0)); + const runningTurns = async (sessionId: string) => turnsRunningIn(await fsd.sessions.requests(sessionId, { status: "in_progress", limit: 50 })); /** Every item of a session of the types a thread draws (or `types`), every page. */ const itemsOf = async (sessionId: string, types: readonly string[] = THREAD_ITEMS): Promise => { @@ -984,13 +994,13 @@ export function createWiredLab(fsd: Client): LabAdapter { let items: Item[] | undefined; let loadEarlier: (() => Promise) | undefined; let connection: ConnectionState | undefined; - let turnOf: { turn: Awaited>[number] | undefined; threads: boolean } | undefined; + let turnOf: { turn: Awaited>[number] | undefined; threads: boolean; retryable: string[] } | undefined; let last: string | undefined; const tell = () => { if (items === undefined || turnOf === undefined) return; - const { turn, threads } = turnOf; + const { turn, threads, retryable } = turnOf; const view: ThreadView = { - entries: entriesOf(items, threads), + entries: entriesOf(items, threads, new Set(retryable)), running: turn !== undefined, ...(connection === undefined ? {} : { connection }), ...(turn?.startedAtMs === undefined ? {} : { turnStartedAt: turn.startedAtMs }), @@ -1025,8 +1035,12 @@ export function createWiredLab(fsd: Client): LabAdapter { [topic.session(sessionId)], (notice) => notice.kind === "request", async () => { - const [turns, threads] = await Promise.all([runningTurns(sessionId), isThreads(sessionId)]); - return { turn: turns[0], threads }; + // One read of the latest requests, newest updated first, answers both: the running turn + // is among them, and so is the turn a failed line could retry. + const [recent, threads] = await Promise.all([fsd.sessions.requests(sessionId, { limit: 50 }), isThreads(sessionId)]); + // The turns the engine would retry: a record with no source predates it, and came in over http. + const retryable = recent.filter((request) => RETRYABLE_SOURCES.has(request.source ?? "http")).map((request) => request.id); + return { turn: turnsRunningIn(recent)[0], threads, retryable }; }, (next) => { turnOf = next; diff --git a/labs/claude-code-style/src/components/Thread.tsx b/labs/claude-code-style/src/components/Thread.tsx index 9a2869cb20..4f5dfb08d2 100644 --- a/labs/claude-code-style/src/components/Thread.tsx +++ b/labs/claude-code-style/src/components/Thread.tsx @@ -97,7 +97,7 @@ export function Thread({ entry={entry} mark={entry.kind === "user" ? feed.mine(entry.id) : undefined} // Retry only the newest turn, and not while one runs: the engine retries a failed request as a new one. - onRetry={entry.kind === "failed" && entry.latest && !running && entry.requestId !== undefined ? retryOf(entry.requestId) : undefined} + onRetry={entry.kind === "failed" && entry.retryable && !running && entry.requestId !== undefined ? retryOf(entry.requestId) : undefined} />
), diff --git a/labs/claude-code-style/test/failed-turn-engine.test.ts b/labs/claude-code-style/test/failed-turn-engine.test.ts index 6d1639710b..7b4e0963ac 100644 --- a/labs/claude-code-style/test/failed-turn-engine.test.ts +++ b/labs/claude-code-style/test/failed-turn-engine.test.ts @@ -44,7 +44,7 @@ describe("on a real engine", () => { }); }); const line = view.entries.find((e): e is Extract => e.kind === "failed")!; - expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, latest: true }); + expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, retryable: true }); await lab.retry(sessionId, line.requestId!); const requests = await client.sessions.requests(sessionId, { includeResultOutput: true, limit: 10 }); diff --git a/labs/claude-code-style/test/failed-turn.test.tsx b/labs/claude-code-style/test/failed-turn.test.tsx index 337881b12b..0aace6be14 100644 --- a/labs/claude-code-style/test/failed-turn.test.tsx +++ b/labs/claude-code-style/test/failed-turn.test.tsx @@ -3,13 +3,16 @@ * A turn that fails reads as a failure. The engine writes an `error` item for * every failed request; before, the thread never read that type, so a crash * looked like a short answer. Now it is one line under the thread with the - * reason and, on the newest turn only, a Retry that runs the turn again: the + * reason and, on the newest turn only, a Retry that runs the turn again. The * engine retries any failed request, so the screen is what keeps Retry off an - * older line, where it would re-run a turn the person has moved past. + * older line, where it would re-run a turn the person has moved past. It also + * keeps Retry off a turn the engine refuses to retry (one a task or the flow + * itself started), where the button could only fail. */ import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; import { afterEach, describe, expect, it } from "vitest"; -import { entriesOf, type ThreadEntry } from "../src/adapter"; +import { PUBLIC_REENTRY_SOURCES } from "@flow-state-dev/engine"; +import { entriesOf, RETRYABLE_SOURCES, type ThreadEntry } from "../src/adapter"; import { Entry } from "../src/components/Entry"; import { Thread } from "../src/components/Thread"; import { createFakeLab } from "./fake-lab"; @@ -33,10 +36,10 @@ describe("the adapter reads a failed turn", () => { message("u1", "r1", "user", "Fix the build"), message("a1", "r1", "assistant", "Looking at the", "in_progress"), errorItem("e1", "r1", "AI_GATEWAY_API_KEY is not set"), - ]); + ], false, new Set(["r1"])); expect(entries.map((e) => e.kind)).toEqual(["user", "assistant", "failed"]); expect(entries[1]).toMatchObject({ kind: "assistant", incomplete: true }); - expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", latest: true }); + expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", retryable: true }); }); it("offers Retry on the newest turn only", () => { @@ -45,14 +48,24 @@ describe("the adapter reads a failed turn", () => { errorItem("e1", "r1", "first failed"), message("u2", "r2", "user", "second"), errorItem("e2", "r2", "second failed"), - ]); + ], false, new Set(["r1", "r2"])); const failed = entries.filter((e): e is Extract => e.kind === "failed"); - expect(failed.map((e) => [e.reason, e.latest])).toEqual([ + expect(failed.map((e) => [e.reason, e.retryable])).toEqual([ ["first failed", false], ["second failed", true], ]); }); + it("offers no Retry on a newest turn the engine would refuse to retry", () => { + // r1 is not among the turns the engine retries: a task or the flow itself started it. + const entries = entriesOf([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed")], false, new Set()); + expect(entries.find((e) => e.kind === "failed")).toMatchObject({ retryable: false }); + }); + + it("counts a turn retryable from the same sources the engine retries", () => { + expect([...RETRYABLE_SOURCES].sort()).toEqual([...PUBLIC_REENTRY_SOURCES].sort()); + }); + it("leaves a finished reply of a turn that worked alone", () => { const entries = entriesOf([message("u1", "r1", "user", "hi"), message("a1", "r1", "assistant", "Hello.")]); expect(entries[1]).not.toHaveProperty("incomplete"); @@ -60,7 +73,7 @@ describe("the adapter reads a failed turn", () => { }); describe("the line a failed turn draws", () => { - const failed = (latest: boolean): ThreadEntry => ({ kind: "failed", id: "e1", reason: "The model refused the key.", latest, requestId: "r1" }); + const failed = (retryable: boolean): ThreadEntry => ({ kind: "failed", id: "e1", reason: "The model refused the key.", retryable, requestId: "r1" }); it("names who failed and why, and Retry calls back", async () => { let retried = 0; diff --git a/labs/claude-code-style/test/fake-lab.ts b/labs/claude-code-style/test/fake-lab.ts index 0031ba66a9..b39e1665b6 100644 --- a/labs/claude-code-style/test/fake-lab.ts +++ b/labs/claude-code-style/test/fake-lab.ts @@ -435,8 +435,8 @@ export function createFakeLab(options: FakeOptions = {}): FakeLab { const line = [...s.entries].reverse().find((e) => e.kind === "user"); const requestId = `turn-${line?.id ?? nextId("r")}`; const last = s.entries[s.entries.length - 1]; - s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e === last && e.kind === "assistant" ? { ...e, incomplete: true } : e)); - push(s, { kind: "failed", id: nextId("f"), reason, latest: true, requestId }); + s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, retryable: false } : e === last && e.kind === "assistant" ? { ...e, incomplete: true } : e)); + push(s, { kind: "failed", id: nextId("f"), reason, retryable: true, requestId }); endTurn(s); }, setConnection(state) { @@ -501,7 +501,7 @@ export function createFakeLab(options: FakeOptions = {}): FakeLab { const s = session(sessionId); const line = s.entries.find((e) => e.kind === "user" && `turn-${e.id}` === requestId); if (line?.kind !== "user") throw new Error(`No failed turn "${requestId}" to retry.`); - s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e)); + s.entries = s.entries.map((e) => (e.kind === "failed" ? { ...e, retryable: false } : e)); turn(s, line.text, line.id); }, diff --git a/labs/teammates/src/adapter.ts b/labs/teammates/src/adapter.ts index 734a123c69..a54c2e1ded 100644 --- a/labs/teammates/src/adapter.ts +++ b/labs/teammates/src/adapter.ts @@ -233,10 +233,11 @@ export type ConversationEntry = /** `streaming` while the answer is still being written; `incomplete` when its turn failed before it was finished. */ | { kind: "assistant"; id: string; text: string; streaming?: boolean; incomplete?: boolean } /** - * A turn that failed, from the engine's `error` item: its reason, its request, and `latest` when - * it is the conversation's newest turn, the one Retry is offered on. + * A turn that failed, from the engine's `error` item: its reason, its request, and `retryable` + * when Retry is offered on it: it is the conversation's newest turn, and one the engine will run + * again for you (a worker's message it was answering is not). */ - | { kind: "failed"; id: string; requestId: string; reason: string; code?: string; details?: Record; latest: boolean } + | { kind: "failed"; id: string; requestId: string; reason: string; code?: string; details?: Record; retryable: boolean } /** The worker messaged another worker (`sendMessage`). */ | { kind: "sent"; id: string; messageId: string; to: string; text: string } /** A message another worker sent, as the `receive` turn that read it. */ @@ -365,6 +366,13 @@ export interface LabAdapter { watchExchange(worker: string, messageId: string, onChange: (steps: SessionStep[]) => void): Unsubscribe; } +/** + * The request sources the engine retries for a caller (`PUBLIC_REENTRY_SOURCES`): a turn that came + * in another way, such as a worker's message (`internal`) or a task's run (`task`), it refuses. + * A copy, since the page can't import the engine; a test pins it to the engine's. + */ +export const RETRYABLE_SOURCES: ReadonlySet = new Set(["http", "mcp", "scheduled"]); + /** How often the reads with no stream are read again. */ const POLL_MS = 1_500; /** The keyed item a conversation's board emits for each row it changes. */ @@ -714,9 +722,15 @@ function harnessAndModel(spend: { harness?: string; model?: string }, setting: s * A conversation's items as the entries the screen draws. `ended` names the * requests the server lists as over: a step their turn left running (a turn * that crashed mid-call) reads failed, not running for good. A failed turn's - * `error` item is a failed entry, `latest` on the newest turn that drew one. + * `error` item is a failed entry, `retryable` when it is the newest turn that drew one and one of + * `retryableTurns`, the requests the engine will retry. */ -export function toEntries(items: readonly Item[], workers: ReadonlySet, ended: readonly string[] = []): ConversationEntry[] { +export function toEntries( + items: readonly Item[], + workers: ReadonlySet, + ended: readonly string[] = [], + retryableTurns: ReadonlySet = new Set(), +): ConversationEntry[] { const rows = boardRows(items); const entries: ConversationEntry[] = []; /** The turn the last run of steps belongs to: a run never spans two. */ @@ -734,7 +748,7 @@ export function toEntries(items: readonly Item[], workers: ReadonlySet, reason: typeof item.message === "string" ? item.message : "", ...(typeof item.code === "string" ? { code: item.code } : {}), ...(item.details === undefined ? {} : { details: item.details }), - latest: false, + retryable: false, }); } else if (item.type === "message" && item.role === "user") { entries.push({ kind: "user", id: item.id, text: textOf(item) }); @@ -796,7 +810,9 @@ export function toEntries(items: readonly Item[], workers: ReadonlySet, drawEntry(item); if (entries.length !== drawn) newestTurn = item.requestId; } - return entries.map((entry) => (entry.kind === "failed" && entry.requestId === newestTurn ? { ...entry, latest: true } : entry)); + return entries.map((entry) => + entry.kind === "failed" && entry.requestId === newestTurn && retryableTurns.has(entry.requestId) ? { ...entry, retryable: true } : entry, + ); } /** @@ -1000,6 +1016,7 @@ export function createLabAdapter(client: Client = connect()): LabAdapter { let items: Item[] = []; let inProgress: string[] = []; let ended: string[] = []; + let retryable = new Set(); let shown: ConversationView | undefined; // Unchanged entries keep their objects, so a streamed chunk redraws only the growing one. const reuse = stableList((next) => { @@ -1017,7 +1034,7 @@ export function createLabAdapter(client: Client = connect()): LabAdapter { }; const stopFeed = watchFeed(sessionId, (next) => next, (next) => { items = next; - reuse(toEntries(items, workerIds, ended)); + reuse(toEntries(items, workerIds, ended, retryable)); emit(); }); const stopPoll = onNotice( @@ -1029,8 +1046,10 @@ export function createLabAdapter(client: Client = connect()): LabAdapter { const answering = requests.filter((r) => !QUIET_ACTIONS.has(r.actionName)); inProgress = answering.filter((r) => r.status === "in_progress").map((r) => r.id); ended = requests.filter((r) => r.status !== "in_progress").map((r) => r.id); + // The turns the engine would retry: a record with no source predates it, and came in over http. + retryable = new Set(requests.filter((r) => RETRYABLE_SOURCES.has(r.source ?? "http")).map((r) => r.id)); // A turn that ended with a call still marked running reads that call failed. - reuse(toEntries(items, workerIds, ended)); + reuse(toEntries(items, workerIds, ended, retryable)); emit(); }, ); diff --git a/labs/teammates/src/components/Conversation.tsx b/labs/teammates/src/components/Conversation.tsx index 856ca0bf29..4db42f8b21 100644 --- a/labs/teammates/src/components/Conversation.tsx +++ b/labs/teammates/src/components/Conversation.tsx @@ -190,7 +190,7 @@ export function Conversation({ onOpenTask={openTask} {...(entry.kind === "approval" ? { onAnswer: (approve: boolean) => adapter.answerApproval(sessionId, entry, approve) } : {})} // Retry only the newest turn, and not while one runs: the engine runs a failed request again as a new one. - {...(entry.kind === "failed" && entry.latest && !running ? { onRetry: () => adapter.retry(sessionId, entry.requestId) } : {})} + {...(entry.kind === "failed" && entry.retryable && !running ? { onRetry: () => adapter.retry(sessionId, entry.requestId) } : {})} /> )} diff --git a/labs/teammates/test/failed-turn-engine.test.ts b/labs/teammates/test/failed-turn-engine.test.ts index 15fb1a5dd0..a2113e487d 100644 --- a/labs/teammates/test/failed-turn-engine.test.ts +++ b/labs/teammates/test/failed-turn-engine.test.ts @@ -43,7 +43,7 @@ describe("on a real engine", () => { }); }); const line = view.entries.find((e): e is Extract => e.kind === "failed")!; - expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, latest: true }); + expect(line).toMatchObject({ reason: "AI_GATEWAY_API_KEY is not set", requestId: sent.id, retryable: true }); await lab.retry(sessionId, line.requestId); const again = (await client.sessions.requests(sessionId, { limit: 10 })).find((r) => r.id !== sent.id); diff --git a/labs/teammates/test/failed-turn.test.tsx b/labs/teammates/test/failed-turn.test.tsx index 3d6e27e6fa..9252964ba5 100644 --- a/labs/teammates/test/failed-turn.test.tsx +++ b/labs/teammates/test/failed-turn.test.tsx @@ -3,14 +3,17 @@ * A turn that fails reads as a failure. The engine writes an `error` item for * every failed request; before, the conversation dropped that item, so a crash * looked like a short answer. Now it is one line under the worker's name with - * the reason and, on the newest turn only, a Retry that runs the turn again: - * the engine retries any failed request, so the screen is what keeps Retry off - * an older line, where it would re-run a turn the person has moved past. + * the reason and, on the newest turn only, a Retry that runs the turn again. + * The engine retries any failed request, so the screen is what keeps Retry off + * an older line, where it would re-run a turn the person has moved past. It + * also keeps Retry off a turn the engine refuses to retry (one a task or the + * flow itself started), where the button could only fail. */ import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { afterEach, describe, expect, it } from "vitest"; import { App } from "../src/App"; -import { toEntries, type ConversationEntry } from "../src/adapter"; +import { PUBLIC_REENTRY_SOURCES } from "@flow-state-dev/engine"; +import { RETRYABLE_SOURCES, toEntries, type ConversationEntry } from "../src/adapter"; import { Entry } from "../src/components/Entry"; import { lookOf } from "../src/lib/looks"; import { createFixtureLab } from "./fake-lab"; @@ -42,31 +45,45 @@ describe("the adapter reads a failed turn", () => { const entries = toEntries( [message("u1", "r1", "user", "Draft the launch post"), message("a1", "r1", "assistant", "Here is a first", "in_progress"), errorItem("e1", "r1", "AI_GATEWAY_API_KEY is not set")], new Set(), + [], + new Set(["r1"]), ); expect(entries.map((e) => e.kind)).toEqual(["user", "assistant", "failed"]); expect(entries[1]).toEqual({ kind: "assistant", id: "a1", text: "Here is a first", incomplete: true }); - expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", latest: true }); + expect(entries[2]).toMatchObject({ kind: "failed", reason: "AI_GATEWAY_API_KEY is not set", code: "PROVIDER_ERROR", requestId: "r1", retryable: true }); }); it("offers Retry on the newest turn only", () => { const entries = toEntries( [message("u1", "r1", "user", "first"), errorItem("e1", "r1", "first failed"), message("u2", "r2", "user", "second"), errorItem("e2", "r2", "second failed")], new Set(), + [], + new Set(["r1", "r2"]), ); - expect(failedOf(entries).map((e) => [e.reason, e.latest])).toEqual([ + expect(failedOf(entries).map((e) => [e.reason, e.retryable])).toEqual([ ["first failed", false], ["second failed", true], ]); }); it("keeps Retry on a failed turn a later board pass wrote no entry for", () => { - const entries = toEntries([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed"), boardPass("c1", "dispatch-1")], new Set()); - expect(failedOf(entries).map((e) => e.latest)).toEqual([true]); + const entries = toEntries([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed"), boardPass("c1", "dispatch-1")], new Set(), [], new Set(["r1"])); + expect(failedOf(entries).map((e) => e.retryable)).toEqual([true]); + }); + + it("offers no Retry on a newest turn the engine would refuse to retry", () => { + // r1 is not among the turns the engine retries: a task or the flow itself started it. + const entries = toEntries([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed")], new Set(), [], new Set()); + expect(failedOf(entries).map((e) => e.retryable)).toEqual([false]); + }); + + it("counts a turn retryable from the same sources the engine retries", () => { + expect([...RETRYABLE_SOURCES].sort()).toEqual([...PUBLIC_REENTRY_SOURCES].sort()); }); }); describe("the line a failed turn draws", () => { - const failed = (latest: boolean): ConversationEntry => ({ kind: "failed", id: "e1", requestId: "r1", reason: "The model refused the key.", latest }); + const failed = (retryable: boolean): ConversationEntry => ({ kind: "failed", id: "e1", requestId: "r1", reason: "The model refused the key.", retryable }); const draw = (entry: ConversationEntry, onRetry?: () => Promise) => render( id} lookOf={(id) => lookOf({ id, name: id })} {...(onRetry ? { onRetry } : {})} />); diff --git a/labs/teammates/test/fake-lab.ts b/labs/teammates/test/fake-lab.ts index f8361ac61a..963b87ca02 100644 --- a/labs/teammates/test/fake-lab.ts +++ b/labs/teammates/test/fake-lab.ts @@ -484,16 +484,16 @@ export function createFixtureLab(options: FixtureOptions = {}): FixtureLab { const line = [...w.entries].reverse().find((e) => e.kind === "user"); const last = w.entries[w.entries.length - 1]; w.entries = w.entries.map((e) => - e.kind === "failed" ? { ...e, latest: false } : e === last && e.kind === "assistant" ? { kind: "assistant", id: e.id, text: e.text, incomplete: true } : e, + e.kind === "failed" ? { ...e, retryable: false } : e === last && e.kind === "assistant" ? { kind: "assistant", id: e.id, text: e.text, incomplete: true } : e, ); - push(w, { kind: "failed", id: nextId("f"), requestId: `turn-${line?.id ?? nextId("r")}`, reason, latest: true }); + push(w, { kind: "failed", id: nextId("f"), requestId: `turn-${line?.id ?? nextId("r")}`, reason, retryable: true }); }, async retry(sessionId, requestId) { retries.push({ sessionId, requestId }); const w = bySession(sessionId); const line = w?.entries.find((e) => e.kind === "user" && `turn-${e.id}` === requestId); if (!w || line?.kind !== "user") throw new Error(`No failed turn "${requestId}" to retry.`); - w.entries = w.entries.map((e) => (e.kind === "failed" ? { ...e, latest: false } : e)); + w.entries = w.entries.map((e) => (e.kind === "failed" ? { ...e, retryable: false } : e)); w.queue.push({ kind: "user", entryId: line.id, text: line.text }); startNext(w); }, From 5f6e8e6939ebeeaed3b683aa9ad1822f0b5a83d7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 22:05:29 +0000 Subject: [PATCH 4/4] FIX-2550: Claude Code-style reads the newest turn from drawn entries The newest turn was the last raw item's request, so an item that draws no entry (a retried notice, an empty message) of another turn hid Retry on the failed line. It is now the request of the last entry drawn, as in Teammates. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NczDiDAAfKeTwqH2bFcZLD --- labs/claude-code-style/src/adapter.ts | 10 +++++++--- labs/claude-code-style/test/failed-turn.test.tsx | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/labs/claude-code-style/src/adapter.ts b/labs/claude-code-style/src/adapter.ts index 9ef225c7ed..605b5ac4ff 100644 --- a/labs/claude-code-style/src/adapter.ts +++ b/labs/claude-code-style/src/adapter.ts @@ -411,7 +411,6 @@ export function entriesOf(items: readonly Item[], threads = false, retryableTurn const filingTurns = new Set(items.filter((item) => filedTaskOf(item) !== undefined).map((item) => item.requestId)); // A turn that failed; its reply, if it never finished, is marked incomplete. Retry is offered on the newest turn only. const failedTurns = new Set(items.filter((item) => item.type === "error").map((item) => item.requestId)); - const newestTurn = items[items.length - 1]?.requestId; const entries: ThreadEntry[] = []; const runs = new Map(); for (const item of items) { @@ -439,7 +438,7 @@ export function entriesOf(items: readonly Item[], threads = false, retryableTurn reason: item.message ?? "", ...(item.code === undefined ? {} : { code: item.code }), ...(item.details === undefined ? {} : { details: item.details }), - retryable: requestId !== undefined && requestId === newestTurn && retryableTurns.has(requestId), + retryable: false, requestId, }); continue; @@ -476,7 +475,12 @@ export function entriesOf(items: readonly Item[], threads = false, retryableTurn entries.push({ kind: "assistant", id: item.id, text, ...(incomplete ? { incomplete: true } : {}), requestId }); } } - return entries; + // The newest turn is the one that drew the last entry: an item that draws none (a retried notice, + // an empty message) does not move it. + const newestTurn = entries[entries.length - 1]?.requestId; + return entries.map((entry) => + entry.kind === "failed" && entry.requestId !== undefined && entry.requestId === newestTurn && retryableTurns.has(entry.requestId) ? { ...entry, retryable: true } : entry, + ); } /** The adapter the app runs on: the served lab, as the user the page was served for. */ diff --git a/labs/claude-code-style/test/failed-turn.test.tsx b/labs/claude-code-style/test/failed-turn.test.tsx index 0aace6be14..a262ee9575 100644 --- a/labs/claude-code-style/test/failed-turn.test.tsx +++ b/labs/claude-code-style/test/failed-turn.test.tsx @@ -56,6 +56,12 @@ describe("the adapter reads a failed turn", () => { ]); }); + it("keeps Retry on a failed turn a later item that drew nothing came after", () => { + // An empty assistant message of another turn draws no entry, so it is not the newest turn. + const entries = entriesOf([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed"), message("a2", "r2", "assistant", "")], false, new Set(["r1"])); + expect(entries.find((e) => e.kind === "failed")).toMatchObject({ retryable: true }); + }); + it("offers no Retry on a newest turn the engine would refuse to retry", () => { // r1 is not among the turns the engine retries: a task or the flow itself started it. const entries = entriesOf([message("u1", "r1", "user", "first"), errorItem("e1", "r1", "failed")], false, new Set());