From 8ff28d89049189a74bcd6b65123f0d36909acdb9 Mon Sep 17 00:00:00 2001 From: abuzaro21 Date: Fri, 30 Oct 2026 11:29:47 +0200 Subject: [PATCH] UI: reset the session at most once per generation, so a late 401/410 cannot replace a newer session Codex live qualification L-01: an obsolete request's 410 arriving after a scenario switch started another session, and the new scenario's lookups then read a session that did not own its proposal. Each reset now bumps a generation; a 401/410 from a superseded generation retries under the current session instead of resetting again (concurrent recoveries coalesce). Adds the exact browser sequence as a regression test. Engine, API and policies unchanged. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- e2e/judge.spec.ts | 27 +++++++++++++++++++++++++++ ui/src/api.ts | 17 +++++++++++++---- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2c2c11f..1633d16 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Checks: ```bash npm test # 95 tests: evaluator, policies, store, fault injection, concurrency, HTTP, flows npm run typecheck # server, tests and UI -npm run test:e2e # 10 Playwright tests: judge journey against the real server (run `npx playwright install chromium` once) +npm run test:e2e # 11 Playwright tests: judge journey against the real server (run `npx playwright install chromium` once) # E2E_BASE_URL=https://… npm run test:e2e runs the same journey as a smoke test against a deployed URL npm run spike:sqlite ``` diff --git a/e2e/judge.spec.ts b/e2e/judge.spec.ts index cf494f5..cb78d16 100644 --- a/e2e/judge.spec.ts +++ b/e2e/judge.spec.ts @@ -135,3 +135,30 @@ test('first load: a late session response cannot split the page across two sessi await expectTruthful(page, 'DEFER'); await expect(page.getByRole('alert')).toHaveCount(0); }); + +test('a late 410 from a superseded session cannot replace the newer scenario session', async ({ page }) => { + // Codex L-01: an obsolete request's 401/410 arriving after a scenario switch must not start another session. + await page.goto('/'); + await expectTruthful(page, 'DEFER'); + await page.getByRole('tab', { name: /Audit trail/ }).click(); + let deliver410!: () => void; + const delivered = new Promise((r) => { deliver410 = r; }); + let deliverStale!: () => void; + const staleGate = new Promise((r) => { deliverStale = r; }); + await page.route('**/api/decisions/*/replay', async (route) => { + await staleGate; + await route.fulfill({ status: 410, contentType: 'application/json', body: JSON.stringify({ error: 'this demo run was restarted' }) }); + deliver410(); + }, { times: 1 }); + await page.route('**/api/scenarios/vendor-clean', async (route) => { + const response = await route.fetch(); + deliverStale(); + await delivered; + await new Promise((r) => setTimeout(r, 500)); // let any (wrong) recovery session land before the proposal reaches the UI + await route.fulfill({ response }); + }); + await page.getByRole('button', { name: 'Replay current decision' }).click(); + await page.getByRole('button', { name: 'Routine payment' }).click(); + await expectTruthful(page, 'EXECUTE'); + await expect(page.getByRole('alert')).toHaveCount(0); +}); diff --git a/ui/src/api.ts b/ui/src/api.ts index dba5092..81878c6 100644 --- a/ui/src/api.ts +++ b/ui/src/api.ts @@ -6,19 +6,28 @@ export class ApiError extends Error { } let sessionPromise: Promise | null = null; +let generation = 0; // Sessions are created one after another: two concurrent POSTs let the older response's Set-Cookie land last, // so later requests would carry a session that does not own the proposal the caller just created. -export const newSession = () => (sessionPromise = (sessionPromise ?? Promise.resolve()).catch(() => {}) - .then(() => fetch('/api/session', { method: 'POST' })).then(() => undefined)); +export const newSession = () => { + generation++; + return (sessionPromise = (sessionPromise ?? Promise.resolve()).catch(() => {}) + .then(() => fetch('/api/session', { method: 'POST' })).then(() => undefined)); +}; export async function api(method: string, path: string, body?: unknown, retried = false): Promise { if (!sessionPromise) newSession(); + const gen = generation; await sessionPromise; const res = await fetch(path, { method, headers: body ? { 'content-type': 'application/json' } : {}, body: body ? JSON.stringify(body) : undefined }); const json = await res.json().catch(() => ({})); if ((res.status === 401 || res.status === 410) && !retried) { - if (res.status === 410) window.dispatchEvent(new CustomEvent('decide:run-restarted', { detail: json })); - newSession(); + // Reset at most once per session generation: if a newer session already replaced the one this request ran in + // (a scenario switch or a concurrent recovery), retry under it instead of replacing it again. + if (gen === generation) { + if (res.status === 410) window.dispatchEvent(new CustomEvent('decide:run-restarted', { detail: json })); + newSession(); + } return api(method, path, body, true); } if (!res.ok) throw new ApiError(res.status, json);