Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
27 changes: 27 additions & 0 deletions e2e/judge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((r) => { deliver410 = r; });
let deliverStale!: () => void;
const staleGate = new Promise<void>((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);
});
17 changes: 13 additions & 4 deletions ui/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ export class ApiError extends Error {
}

let sessionPromise: Promise<void> | 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<T = any>(method: string, path: string, body?: unknown, retried = false): Promise<T> {
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);
Expand Down
Loading