Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

test(web): lock v2 turn lifecycle behavior

Bingxi Zhao (Frank)6fb5b657865b
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-desktop.png
new file mode 100644
index 000000000..fe94da0e2
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-mobile.png
new file mode 100644
index 000000000..a801a21ad
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-desktop.png
new file mode 100644
index 000000000..253a36bce
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-mobile.png
new file mode 100644
index 000000000..9bb51c8ec
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-desktop.png
new file mode 100644
index 000000000..7717f22ae
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-mobile.png
new file mode 100644
index 000000000..39429470c
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-error-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-desktop.png
new file mode 100644
index 000000000..909e35576
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-mobile.png
new file mode 100644
index 000000000..2919109e6
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-desktop.png
new file mode 100644
index 000000000..0803ce4c9
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-mobile.png
new file mode 100644
index 000000000..d8f83121d
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-desktop.png
new file mode 100644
index 000000000..812729fd9
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-mobile.png
new file mode 100644
index 000000000..5cb045cb1
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/chat-streaming-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-desktop.png
new file mode 100644
index 000000000..293eabf77
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-mobile.png
new file mode 100644
index 000000000..0b5245faf
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-desktop.png
new file mode 100644
index 000000000..74b9b3800
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-mobile.png
new file mode 100644
index 000000000..28ede0337
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/mastery-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-desktop.png
new file mode 100644
index 000000000..b395499e3
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-mobile.png
new file mode 100644
index 000000000..922d5409d
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-desktop.png
new file mode 100644
index 000000000..a652ef8e1
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-mobile.png
new file mode 100644
index 000000000..cba3ce898
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/reading-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-desktop.png
new file mode 100644
index 000000000..6193b0d3c
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-mobile.png
new file mode 100644
index 000000000..1f557df64
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-dark-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-desktop.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-desktop.png
new file mode 100644
index 000000000..e2ef6591e
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-desktop.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-mobile.png b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-mobile.png
new file mode 100644
index 000000000..fbdb3c06f
Binary files /dev/null and b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline-screenshots/settings-snow-mobile.png differ
diff --git a/docs/plans/2026-09-01-frontend-stabilization-v2-baseline.md b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline.md
new file mode 100644
index 000000000..f945f256e
--- /dev/null
+++ b/docs/plans/2026-09-01-frontend-stabilization-v2-baseline.md
@@ -0,0 +1,157 @@
+# DeepTutor v2 Frontend Baseline
+
+**Captured:** 2026-09-01
+
+**Git base:** `02b68dda` (`dev`)
+
+**Purpose:** Preserve the observable frontend contract and measurable starting point before the v2 protocol, state, and feature-boundary migration.
+
+## 1. Turn lifecycle contract
+
+The fixture-backed characterization test records these client commands:
+
+- `start_turn` with content, session, and capability;
+- `subscribe_turn` with `after_seq`;
+- `resume_from` with the last sequence;
+- `regenerate` with overrides;
+- `cancel_turn`;
+- `submit_user_reply` with structured answers.
+
+The successful event fixture starts with `session`, advances monotonically by `seq`, pauses at `wait_for_input`, resumes content, and ends with `done` carrying authoritative terminal metadata. The failed fixture ends with `error_code=worker_lost` and `retryable=true`.
+
+The initial test run failed as expected:
+
+```text
+tests/turn-lifecycle-characterization.test.ts(10,7): error TS2322:
+Type '"wait_for_input"' is not assignable to type 'StreamEventType'.
+```
+
+The temporary handwritten frontend union was brought into parity with the already-existing Python `StreamEventType.WAIT_FOR_INPUT`. Generated contracts replace this union in the contract phase.
+
+## 2. Source baseline
+
+### Largest TypeScript modules
+
+| Lines | Module |
+| ---: | --- |
+| 2,808 | `app/(workspace)/home/[[...sessionId]]/page.tsx` |
+| 2,750 | `components/chat/home/TracePanels.tsx` |
+| 2,542 | `app/(workspace)/co-writer/[docId]/page.tsx` |
+| 2,377 | `context/UnifiedChatContext.tsx` |
+| 1,979 | `components/settings/SettingsContext.tsx` |
+| 1,949 | `components/knowledge/EngineDetail.tsx` |
+| 1,664 | `components/chat/home/ChatMessages.tsx` |
+| 1,635 | `components/settings/ServiceConfigEditor.tsx` |
+| 1,522 | `components/memory/MemorySection.tsx` |
+| 1,473 | `components/knowledge/CreateKbModal.tsx` |
+| 1,444 | `lib/knowledge-api.ts` |
+| 1,404 | `app/(utility)/settings/document-parsing/page.tsx` |
+| 1,386 | `components/quiz/QuizViewer.tsx` |
+| 1,250 | `app/(workspace)/book/components/BookCreator.tsx` |
+| 1,239 | `components/chat/home/ChatComposer.tsx` |
+| 1,208 | `components/chat/home/SessionViewerPanel.tsx` |
+| 1,206 | `app/(workspace)/book/page.tsx` |
+| 1,023 | `components/space/SkillsSection.tsx` |
+| 1,023 | `components/cli-apps/CliAppsSection.tsx` |
+| 1,003 | `components/chat/home/StandaloneComposer.tsx` |
+| 991 | `components/chat/home/AskUserOptions.tsx` |
+| 989 | `components/memory/MemoryGraph.tsx` |
+| 942 | `components/memory/MemoryRunPanel.tsx` |
+| 931 | `components/partners/PartnerChat.tsx` |
+| 905 | `components/reading/ReaderPane.tsx` |
+
+There are 169,601 lines across canonical TS/TSX source and tests, excluding dependencies and generated build directories.
+
+### Route and state ownership indicators
+
+| Measure | Baseline |
+| --- | ---: |
+| App route pages | 81 |
+| Client route pages | 63 |
+| Modules declaring React Context | 9 |
+| `useUnifiedChat()` consumer modules | 11 |
+| Node test files | 136 |
+| Node test cases | 846 |
+
+Source sizes for the requested routes:
+
+| Route module | Lines | Bytes |
+| --- | ---: | ---: |
+| Chat `/home/[[...sessionId]]` | 2,808 | 109,463 |
+| Continuous Settings `/settings` | 37 | 1,512 |
+| Knowledge `/knowledge` | 19 | 477 |
+| Co-Writer `/co-writer/[docId]` | 2,542 | 90,677 |
+
+The small Settings and Knowledge route shells do not imply a small surface: Settings imports route modules into a continuous page and Knowledge delegates into large component/API modules.
+
+## 3. Build and client-chunk baseline
+
+The production build completed successfully with Next.js 16.2.3. Client chunk sizes below are the unique chunks referenced by each route page module in the generated client-reference manifest; they include shared chunks referenced from that entry and are comparison metrics, not transfer-size claims.
+
+| Surface | Referenced client JS | Chunks |
+| --- | ---: | ---: |
+| Chat | 692 KB | 20 |
+| Settings | 505 KB | 19 |
+| Knowledge | 293 KB | 7 |
+| Co-Writer | 209 KB | 7 |
+| Mastery study | 686 KB | 19 |
+| Workspace shell / `UnifiedChatContext` set | 318 KB | 14 |
+
+The existing `npm run perf:check` does not understand the Next.js 16 client-reference manifest. It reads removed `entryJSFiles` data and fails with:
+
+```text
+TypeError: Cannot read properties of undefined (reading '[project]/app/layout')
+```
+
+This is a baseline tooling defect. No passing route-budget claim is made.
+
+## 4. Check duration baseline
+
+Measured on the same checkout with Node 25.6.0 after `npm ci --legacy-peer-deps`:
+
+| Check | Result | Wall time |
+| --- | --- | ---: |
+| TypeScript, non-incremental | Pass | 9.95 s |
+| Source-only ESLint, quiet | Pass | 16.27 s |
+| Node tests | 846 passed | 4.57 s process / 2.02 s test duration |
+| Production build | Pass | 20.4 s compile + 7.8 s TypeScript |
+| Route budgets | Tool crash | n/a |
+
+`npm ci` reported engine warnings because some dependencies support even-numbered Node releases while the capture environment used Node 25. It also reported 11 dependency audit findings. Neither warning was silently treated as a source-test failure; the supported CI Node version remains the release authority.
+
+## 5. Visual baseline
+
+Twenty-four screenshots are stored in `2026-09-01-frontend-stabilization-v2-baseline-screenshots/`:
+
+- Chat empty, streaming, and retryable-error states;
+- Settings;
+- Immersive Reading;
+- Mastery Path;
+- Snow and Dark themes;
+- 1440×1000 desktop and 390×844 mobile viewports.
+
+Chat streaming/error screenshots use a deterministic browser-injected WebSocket fixture with the recorded event envelope. Empty, Settings, Reading, and Mastery screenshots use the built frontend without a backend. The resulting Settings load warning and Reading HTTP 500 state are intentionally preserved as part of the error baseline.
+
+Observed interaction problems to preserve as regression targets:
+
+- A retryable `worker_lost` error can coexist with a `Done` stage heading, giving contradictory lifecycle language.
+- Streaming status is embedded in message trace presentation rather than exposed as one stable cross-surface state region.
+- Settings renders a very long continuous document with weak use of available desktop width.
+- Mobile Chat keeps the composer reachable, but lifecycle feedback competes with message content and has no dedicated stable slot.
+- Reading has a persistent inline retry affordance, while equivalent lifecycle semantics are not shared with Chat or Mastery.
+
+The deterministic multi-worker backend fixture does not exist yet, so this phase does not claim live reconnect, cross-worker replay, or cancellation-acknowledgement screenshots.
+
+## 6. Reproduction commands
+
+```bash
+cd web
+npm ci --legacy-peer-deps
+npx tsc --noEmit --incremental false
+npx eslint app components context features hooks lib tests --quiet
+npm run test:node
+npm run build
+npm run perf:check
+```
+
+The lifecycle browser audit is registered under the `critical-turns` Playwright project. It remains explicitly skipped until the deterministic backend fixture is delivered in the multi-worker acceptance phase.
diff --git a/web/lib/unified-ws.ts b/web/lib/unified-ws.ts
index 41ac903a8..13bfb67f7 100644
--- a/web/lib/unified-ws.ts
+++ b/web/lib/unified-ws.ts
@@ -32,6 +32,7 @@ export type StreamEventType =
| "error"
| "session"
| "session_meta"
+ | "wait_for_input"
| "done";
export interface StreamEvent {
diff --git a/web/playwright.config.ts b/web/playwright.config.ts
index ef63eeaad..004653e8b 100644
--- a/web/playwright.config.ts
+++ b/web/playwright.config.ts
@@ -21,7 +21,15 @@ export default defineConfig({
{
name: "ui-audit",
testMatch: "**/*.audit.ts",
- testIgnore: "**/epub-reader.audit.ts",
+ testIgnore: [
+ "**/epub-reader.audit.ts",
+ "**/e2e/turn-lifecycle.audit.ts",
+ ],
+ use: { ...devices["Desktop Chrome"] },
+ },
+ {
+ name: "critical-turns",
+ testMatch: "**/e2e/turn-lifecycle.audit.ts",
use: { ...devices["Desktop Chrome"] },
},
{
diff --git a/web/tests/e2e/turn-lifecycle.audit.ts b/web/tests/e2e/turn-lifecycle.audit.ts
new file mode 100644
index 000000000..240303364
--- /dev/null
+++ b/web/tests/e2e/turn-lifecycle.audit.ts
@@ -0,0 +1,54 @@
+import { expect, test } from "@playwright/test";
+
+const integrationFixtureAvailable =
+ process.env.DEEPTUTOR_TURN_E2E_FIXTURE === "1";
+
+test.describe("v2 turn lifecycle", () => {
+ test.skip(
+ !integrationFixtureAvailable,
+ "Requires the deterministic backend turn fixture added with the multi-worker acceptance phase.",
+ );
+
+ test("keeps server-authoritative lifecycle states visible", async ({ page }) => {
+ await page.goto("/");
+
+ const composer = page.getByRole("textbox").last();
+ await expect(composer).toBeVisible();
+ await composer.fill("Explain replay-safe turns");
+ await composer.press("Enter");
+
+ const status = page.getByTestId("turn-status");
+ await expect(status).toContainText(/queued|connecting/i);
+ await expect(status).toContainText(/streaming|responding/i);
+ await expect(status).toContainText(/waiting for input/i);
+
+ await page.getByRole("textbox", { name: /answer/i }).fill("Continue");
+ await page.getByRole("button", { name: /answer|submit/i }).click();
+ await expect(status).toContainText(/streaming|responding/i);
+ await expect(status).toContainText(/completed/i);
+ });
+
+ test("recovers without inventing a terminal failure", async ({ page }) => {
+ await page.goto("/");
+ await page.getByRole("textbox").last().fill("Start a long turn");
+ await page.getByRole("textbox").last().press("Enter");
+
+ const status = page.getByTestId("turn-status");
+ await expect(status).toContainText(/streaming|responding/i);
+ await page.getByTestId("fixture-drop-socket").click();
+ await expect(status).toContainText(/recovering|reconnecting/i);
+ await expect(status).not.toContainText(/failed/i);
+ await expect(status).toContainText(/streaming|completed/i);
+ });
+
+ test("waits for cancellation acknowledgement", async ({ page }) => {
+ await page.goto("/");
+ await page.getByRole("textbox").last().fill("Start a cancellable turn");
+ await page.getByRole("textbox").last().press("Enter");
+ await page.getByRole("button", { name: /stop|cancel/i }).click();
+
+ const status = page.getByTestId("turn-status");
+ await expect(status).toContainText(/cancelling/i);
+ await expect(status).toContainText(/cancelled/i);
+ });
+});
diff --git a/web/tests/fixtures/turn-lifecycle.json b/web/tests/fixtures/turn-lifecycle.json
new file mode 100644
index 000000000..261d4373b
--- /dev/null
+++ b/web/tests/fixtures/turn-lifecycle.json
@@ -0,0 +1,154 @@
+{
+ "commands": [
+ {
+ "name": "start",
+ "payload": {
+ "type": "start_turn",
+ "content": "Explain replay-safe turns",
+ "session_id": null,
+ "capability": "chat"
+ }
+ },
+ {
+ "name": "subscribe",
+ "payload": {
+ "type": "subscribe_turn",
+ "turn_id": "turn-1",
+ "after_seq": 2
+ }
+ },
+ {
+ "name": "resume",
+ "payload": {
+ "type": "resume_from",
+ "turn_id": "turn-1",
+ "seq": 2
+ }
+ },
+ {
+ "name": "regenerate",
+ "payload": {
+ "type": "regenerate",
+ "session_id": "session-1",
+ "overrides": {
+ "language": "en"
+ }
+ }
+ },
+ {
+ "name": "cancel",
+ "payload": {
+ "type": "cancel_turn",
+ "turn_id": "turn-1"
+ }
+ },
+ {
+ "name": "submit_user_reply",
+ "payload": {
+ "type": "submit_user_reply",
+ "turn_id": "turn-1",
+ "answers": [
+ {
+ "questionId": "scope",
+ "text": "Use the multi-worker path"
+ }
+ ]
+ }
+ }
+ ],
+ "successfulEvents": [
+ {
+ "type": "session",
+ "source": "chat",
+ "stage": "",
+ "content": "",
+ "metadata": {
+ "status": "running"
+ },
+ "session_id": "session-1",
+ "turn_id": "turn-1",
+ "seq": 1,
+ "timestamp": 1788249600
+ },
+ {
+ "type": "content",
+ "source": "chat",
+ "stage": "responding",
+ "content": "A turn can be resumed",
+ "metadata": {},
+ "session_id": "session-1",
+ "turn_id": "turn-1",
+ "seq": 2,
+ "timestamp": 1788249601
+ },
+ {
+ "type": "wait_for_input",
+ "source": "chat",
+ "stage": "responding",
+ "content": "Which scope should I use?",
+ "metadata": {
+ "status": "waiting_input"
+ },
+ "session_id": "session-1",
+ "turn_id": "turn-1",
+ "seq": 3,
+ "timestamp": 1788249602
+ },
+ {
+ "type": "content",
+ "source": "chat",
+ "stage": "responding",
+ "content": " through another worker.",
+ "metadata": {},
+ "session_id": "session-1",
+ "turn_id": "turn-1",
+ "seq": 4,
+ "timestamp": 1788249603
+ },
+ {
+ "type": "done",
+ "source": "chat",
+ "stage": "responding",
+ "content": "",
+ "metadata": {
+ "turn_terminal": true,
+ "status": "completed"
+ },
+ "session_id": "session-1",
+ "turn_id": "turn-1",
+ "seq": 5,
+ "timestamp": 1788249604
+ }
+ ],
+ "failedEvents": [
+ {
+ "type": "session",
+ "source": "chat",
+ "stage": "",
+ "content": "",
+ "metadata": {
+ "status": "running"
+ },
+ "session_id": "session-2",
+ "turn_id": "turn-2",
+ "seq": 1,
+ "timestamp": 1788249610
+ },
+ {
+ "type": "error",
+ "source": "chat",
+ "stage": "responding",
+ "content": "The owning worker was lost.",
+ "metadata": {
+ "turn_terminal": true,
+ "status": "failed",
+ "error_code": "worker_lost",
+ "retryable": true
+ },
+ "session_id": "session-2",
+ "turn_id": "turn-2",
+ "seq": 2,
+ "timestamp": 1788249611
+ }
+ ]
+}
diff --git a/web/tests/turn-lifecycle-characterization.test.ts b/web/tests/turn-lifecycle-characterization.test.ts
new file mode 100644
index 000000000..e5064151f
--- /dev/null
+++ b/web/tests/turn-lifecycle-characterization.test.ts
@@ -0,0 +1,86 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import fixtures from "./fixtures/turn-lifecycle.json";
+import type {
+ ChatMessage,
+ StreamEvent,
+ StreamEventType,
+} from "../lib/unified-ws";
+
+const waitForInputKind: StreamEventType = "wait_for_input";
+
+function asMessage(value: unknown): ChatMessage {
+ return value as ChatMessage;
+}
+
+function asEvent(value: unknown): StreamEvent {
+ return value as StreamEvent;
+}
+
+test("turn commands retain their public serialized shapes", () => {
+ const commands = new Map(
+ fixtures.commands.map(({ name, payload }) => [name, asMessage(payload)]),
+ );
+
+ assert.deepEqual(commands.get("start"), {
+ type: "start_turn",
+ content: "Explain replay-safe turns",
+ session_id: null,
+ capability: "chat",
+ });
+ assert.deepEqual(commands.get("subscribe"), {
+ type: "subscribe_turn",
+ turn_id: "turn-1",
+ after_seq: 2,
+ });
+ assert.deepEqual(commands.get("resume"), {
+ type: "resume_from",
+ turn_id: "turn-1",
+ seq: 2,
+ });
+ assert.equal(commands.get("regenerate")?.type, "regenerate");
+ assert.deepEqual(commands.get("cancel"), {
+ type: "cancel_turn",
+ turn_id: "turn-1",
+ });
+ assert.deepEqual(commands.get("submit_user_reply"), {
+ type: "submit_user_reply",
+ turn_id: "turn-1",
+ answers: [{ questionId: "scope", text: "Use the multi-worker path" }],
+ });
+});
+
+test("the first event identifies the session and replay order is monotonic", () => {
+ const events = fixtures.successfulEvents.map(asEvent);
+
+ assert.equal(events[0].type, "session");
+ assert.equal(events[0].session_id, "session-1");
+ assert.equal(events[0].turn_id, "turn-1");
+ assert.deepEqual(
+ events.map((event) => event.seq),
+ [1, 2, 3, 4, 5],
+ );
+ assert.equal(events[2].type, waitForInputKind);
+});
+
+test("done carries authoritative terminal metadata", () => {
+ const done = asEvent(fixtures.successfulEvents.at(-1));
+
+ assert.equal(done.type, "done");
+ assert.deepEqual(done.metadata, {
+ turn_terminal: true,
+ status: "completed",
+ });
+});
+
+test("error carries a stable code and retryability metadata", () => {
+ const error = asEvent(fixtures.failedEvents.at(-1));
+
+ assert.equal(error.type, "error");
+ assert.deepEqual(error.metadata, {
+ turn_terminal: true,
+ status: "failed",
+ error_code: "worker_lost",
+ retryable: true,
+ });
+});