From c8b388c39d7845bfac0196e19492e1f81aaaecbb Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 26 Nov 2026 16:29:55 +0000 Subject: [PATCH 1/2] Keep bench practice state until a restart succeeds AI-assisted reliability fix for the shared practice UI and its regression suite. --- .../src/components/BenchPractice.test.tsx | 65 +++++++++++++++++++ frontend/src/components/BenchPractice.tsx | 36 ++++++---- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/frontend/src/components/BenchPractice.test.tsx b/frontend/src/components/BenchPractice.test.tsx index 6b6dd59..22f5eae 100644 --- a/frontend/src/components/BenchPractice.test.tsx +++ b/frontend/src/components/BenchPractice.test.tsx @@ -159,4 +159,69 @@ describe("bench practice", () => { ); expect(screen.queryByRole("heading", { name: "Debrief" })).toBeNull(); }); + + it("keeps the active run and draft when a restart fails", async () => { + const user = userEvent.setup(); + const random = vi.spyOn(Math, "random").mockReturnValue(0.25); + vi.spyOn(api, "benchProtocols").mockResolvedValue([protocol]); + vi.spyOn(api, "benchProtocol").mockResolvedValue(protocol); + const replay = vi + .spyOn(api, "benchReplay") + .mockResolvedValueOnce(run({ seed: 500_000_000 })) + .mockResolvedValueOnce(run({ seed: 500_000_000, clock_minutes: 1 })) + .mockRejectedValueOnce(new ApiError("Bench unavailable", 503)) + .mockResolvedValueOnce(run({ seed: 500_000_000, clock_minutes: 2 })) + .mockResolvedValueOnce(run({ seed: 500_000_000 })); + render(); + + await user.click(await screen.findByRole("button", { name: "Start run" })); + await user.click(await screen.findByRole("button", { name: "Do it" })); + await screen.findByText("(clock 1.0 min)"); + await user.selectOptions( + screen.getByRole("combobox", { name: "Action" }), + "set_volume", + ); + await user.type( + screen.getByRole("spinbutton", { name: "Volume (µL)" }), + "450", + ); + + random.mockReturnValue(0.75); + await user.click(screen.getByRole("button", { name: "Start a new run" })); + expect(await screen.findByRole("alert")).toHaveTextContent( + "Bench unavailable", + ); + expect(screen.getByText("(clock 1.0 min)")).toBeInTheDocument(); + expect(screen.getByRole("spinbutton", { name: "Volume (µL)" })).toHaveValue( + 450, + ); + expect(replay).toHaveBeenNthCalledWith( + 3, + protocol.id, + 1_500_000_000, + "guided", + [], + ); + + await user.click(screen.getByRole("button", { name: "Do it" })); + await screen.findByText("(clock 2.0 min)"); + expect(replay).toHaveBeenNthCalledWith( + 4, + protocol.id, + 500_000_000, + "guided", + [ + { name: "fit_tip", params: {} }, + { name: "set_volume", params: { volume_ul: 450 } }, + ], + ); + + random.mockReturnValue(0.25); + await user.click(screen.getByRole("button", { name: "Start a new run" })); + await screen.findByText("(clock 0.0 min)"); + expect(screen.getByRole("combobox", { name: "Action" })).toHaveValue( + "fit_tip", + ); + expect(screen.queryByRole("spinbutton")).toBeNull(); + }); }); diff --git a/frontend/src/components/BenchPractice.tsx b/frontend/src/components/BenchPractice.tsx index c5a85a3..b6b5997 100644 --- a/frontend/src/components/BenchPractice.tsx +++ b/frontend/src/components/BenchPractice.tsx @@ -73,11 +73,15 @@ export function BenchPractice({ protocolId }: { protocolId?: string }) { function ProtocolRunner({ id }: { id: string }) { const [protocol, setProtocol] = useState(null); const [mode, setMode] = useState("guided"); - const [seed, setSeed] = useState(null); - const [actions, setActions] = useState([]); - const [run, setRun] = useState(null); + const [session, setSession] = useState<{ + actions: BenchAction[]; + run: BenchRun; + generation: number; + } | null>(null); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); + const run = session?.run ?? null; + const actions = session?.actions ?? []; useEffect(() => { api @@ -86,14 +90,21 @@ function ProtocolRunner({ id }: { id: string }) { .catch((reason) => setError(errorMessage(reason))); }, [id]); - async function submit(next: BenchAction[], runSeed = seed, runMode = mode) { - if (runSeed === null) return; + async function submit( + next: BenchAction[], + restart?: { seed: number; mode: Mode }, + ) { + const request = restart ?? run; + if (!request) return; setBusy(true); setError(""); try { - const result = await api.benchReplay(id, runSeed, runMode, next); - setActions(next); - setRun(result); + const result = await api.benchReplay(id, request.seed, request.mode, next); + setSession((previous) => ({ + run: result, + actions: next, + generation: (previous?.generation ?? 0) + (restart ? 1 : 0), + })); } catch (reason) { setError(errorMessage(reason)); } finally { @@ -102,9 +113,7 @@ function ProtocolRunner({ id }: { id: string }) { } function start() { - const fresh = newSeed(); - setSeed(fresh); - void submit([], fresh, mode); + void submit([], { seed: newSeed(), mode }); } if (!protocol) { @@ -130,7 +139,7 @@ function ProtocolRunner({ id }: { id: string }) { Mode{" "}