diff --git a/.fallowrc.json b/.fallowrc.json index 1ca391bae2..40b70c0449 100644 --- a/.fallowrc.json +++ b/.fallowrc.json @@ -108,6 +108,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", @@ -121,6 +123,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", diff --git a/labs/claude-code-style/README.md b/labs/claude-code-style/README.md index 873da26ee0..614410cccb 100644 --- a/labs/claude-code-style/README.md +++ b/labs/claude-code-style/README.md @@ -153,6 +153,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 bd6b61203c..23be952aa1 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 | @@ -98,7 +99,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) | { @@ -112,6 +114,11 @@ export type ThreadEntry = ( duringTurn: boolean; } | { kind: "interrupted"; id: string } + /** + * 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; 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 }; @@ -201,6 +208,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; @@ -242,8 +251,15 @@ 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 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"]; /** One stored item, as far as the thread reads it. */ export interface Item { @@ -265,6 +281,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; } @@ -395,12 +415,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, `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)); + // 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 entries: ThreadEntry[] = []; const runs = new Map(); for (const item of items) { @@ -421,6 +445,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 }), + retryable: false, + 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 } : {}) }); @@ -448,9 +484,17 @@ 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; + // 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. */ @@ -571,10 +615,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 => { @@ -959,19 +1005,22 @@ 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. 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 }), @@ -1006,8 +1055,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/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 002b73b3ea..b1263fb7c8 100644 --- a/labs/claude-code-style/src/components/Thread.tsx +++ b/labs/claude-code-style/src/components/Thread.tsx @@ -50,9 +50,10 @@ export function Thread({ const now = useNow(running && !stale); 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); // A starter line picked from the empty thread: the composer takes it as its text, and nothing is sent. const [seed, setSeed] = useState<{ text: string; at: number }>(); + // On the document, so the keys work wherever focus is; a sheet's Esc arrives already spent. const keys = useInterruptKeys({ enabled: running && session.projectId !== null, onInterrupt: () => void adapter.interrupt(session.id), @@ -111,7 +112,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..7b4e0963ac --- /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, retryable: 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..a262ee9575 --- /dev/null +++ b/labs/claude-code-style/test/failed-turn.test.tsx @@ -0,0 +1,131 @@ +// @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. 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 { 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"; + +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"), + ], 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", retryable: 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"), + ], false, new Set(["r1", "r2"])); + const failed = entries.filter((e): e is Extract => e.kind === "failed"); + expect(failed.map((e) => [e.reason, e.retryable])).toEqual([ + ["first failed", false], + ["second failed", true], + ]); + }); + + 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()); + 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"); + }); +}); + +describe("the line a failed turn draws", () => { + 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; + 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 83714de2c3..1615720243 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, 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) { connection = state; notify(); @@ -489,6 +510,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, retryable: 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 2ea41612dd..040a9825cc 100644 --- a/labs/teammates/README.md +++ b/labs/teammates/README.md @@ -206,6 +206,7 @@ names the package call it makes. | On screen | Package piece | |---|---| | The worker list, sending, the conversation, busy status | the workforce client's `watchRoster()` 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 30fdc4740b..57296b1a20 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` | @@ -231,8 +232,14 @@ 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 `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; 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. */ @@ -356,6 +363,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; @@ -385,6 +394,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. */ @@ -725,15 +741,36 @@ function harnessAndModel(spend: TaskSpendView | null, setting: string | undefine /** * 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, `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. */ 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 }), + retryable: 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 @@ -758,7 +795,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 ?? "") }); @@ -785,8 +824,15 @@ 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 && retryableTurns.has(entry.requestId) ? { ...entry, retryable: true } : entry, + ); } /** @@ -1017,6 +1063,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); }, @@ -1025,6 +1075,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) => { @@ -1042,7 +1093,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( @@ -1054,8 +1105,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 f3a269e1b8..37c63fed1d 100644 --- a/labs/teammates/src/components/Conversation.tsx +++ b/labs/teammates/src/components/Conversation.tsx @@ -220,6 +220,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.retryable && !running ? { onRetry: () => adapter.retry(sessionId, entry.requestId) } : {})} /> )} diff --git a/labs/teammates/src/components/Entry.tsx b/labs/teammates/src/components/Entry.tsx index 288d28f5cb..d7838c68f6 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 { MarkdownText } 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..a2113e487d --- /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, retryable: 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..9252964ba5 --- /dev/null +++ b/labs/teammates/test/failed-turn.test.tsx @@ -0,0 +1,121 @@ +// @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. 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 { 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"; + +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(), + [], + 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", 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.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(), [], 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 = (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 } : {})} />); + + 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 e730272203..d77b84ba62 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, 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, 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, retryable: false } : e)); + w.queue.push({ kind: "user", entryId: line.id, text: line.text }); + startNext(w); + }, async readContext() { return { folder: "/work/launch-site", folderProblem: null, model: "openai/gpt-6-luna", harness: "cursor", delegates: { [LEAD]: "all" as const } };