From 8ef81ebcf24e210bed6e2c47777df01d36ac0162 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 14:17:03 -0800 Subject: [PATCH 01/41] test(cockpit): run React chat/input and chat/theming e2e on aimock Batch 1 of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md). - chat/input and chat/theming React e2e replay the shared Angular fixtures through a real `langgraph dev`; holds are proxy controls, not magic prompts. - Proxy: /__captures (forwarded SSE, saved messages/next from the real /state, title before/after each run), openResponses in /__lifetime, and /__inject/reasoning (documented injected data: ChatOpenAI over Chat Completions never surfaces reasoning; the theming palette checks need it). - Shared reactAimockGlobalSetup({ example, port }) plus a spec keeping it in step with configuration.mjs; nx.json `reactAimockBackend` named input so proxy/server changes select every migrated React cap. - Fixtures: title-model fixture, the shared 3-char fence fixture, and a catch-all reply (last) for client-only prompts. - Delete chat-input/chat-theming fixture servers, the theming fixture spec and its CI step; the network-free graph proof stays in each example's Python tests, which e2e still depends on. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 5 - .../input/angular/e2e/fixtures/c-input.json | 14 + .../chat/input/react/e2e/global-setup-impl.ts | 6 + cockpit/chat/input/react/e2e/input.spec.ts | 36 +- .../chat/input/react/e2e/playwright.config.ts | 3 + cockpit/chat/input/react/project.json | 2 + .../messages/react/e2e/global-setup-impl.ts | 19 +- cockpit/chat/messages/react/project.json | 1 + .../angular/e2e/fixtures/c-theming.json | 8 + .../theming/react/e2e/global-setup-impl.ts | 6 + .../theming/react/e2e/playwright.config.ts | 3 + .../chat/theming/react/e2e/theming.spec.ts | 84 +-- cockpit/chat/theming/react/project.json | 10 +- cockpit/shared/react-aimock-setup.ts | 27 + .../2026-11-18-react-cockpit-aimock-design.md | 17 +- nx.json | 8 +- scripts/ci-workflow.spec.mjs | 13 +- scripts/cockpit-frontend-matrix.spec.mjs | 27 + scripts/cockpit-matrix.spec.mjs | 4 +- .../ag-ui-client-tools-preparation.spec.mjs | 2 +- scripts/react-cockpit/chat-input-fixture.mjs | 278 -------- scripts/react-cockpit/chat-input-wire.py | 65 -- .../chat-theming-configuration.spec.mjs | 8 +- .../react-cockpit/chat-theming-fixture.mjs | 569 --------------- .../chat-theming-fixture.spec.mjs | 654 ------------------ scripts/react-cockpit/chat-theming-wire.py | 82 --- scripts/react-cockpit/configuration.mjs | 22 +- scripts/react-cockpit/langgraph-proxy.mjs | 144 +++- scripts/react-cockpit/serve.mjs | 13 +- 29 files changed, 349 insertions(+), 1781 deletions(-) create mode 100644 cockpit/chat/input/react/e2e/global-setup-impl.ts create mode 100644 cockpit/chat/theming/react/e2e/global-setup-impl.ts create mode 100644 cockpit/shared/react-aimock-setup.ts delete mode 100644 scripts/react-cockpit/chat-input-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-input-wire.py delete mode 100644 scripts/react-cockpit/chat-theming-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-theming-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-theming-wire.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd7e7929e..25508a5c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1149,11 +1149,6 @@ jobs: npx nx run cockpit-runtimes-aws-strands-react:prepare-fixture --skip-nx-cache node --test --test-concurrency=1 scripts/react-cockpit/native-strands-fixture.spec.mjs fixtures/react-parity/native-strands-react/server.spec.mjs python -m unittest discover -s fixtures/react-parity/native-strands-react -p test_model.py - - name: Verify native Theming fixture ownership - if: matrix.cap.angular == 'cockpit-chat-theming-react' - run: | - npx nx run cockpit-chat-theming-python:test --skip-nx-cache - node --test --test-concurrency=1 scripts/react-cockpit/chat-theming-fixture.spec.mjs - name: Verify native Debug fixture and installed session ownership if: matrix.cap.angular == 'cockpit-chat-debug-react' run: | diff --git a/cockpit/chat/input/angular/e2e/fixtures/c-input.json b/cockpit/chat/input/angular/e2e/fixtures/c-input.json index 2f2a7c77c..53d4e9bc3 100644 --- a/cockpit/chat/input/angular/e2e/fixtures/c-input.json +++ b/cockpit/chat/input/angular/e2e/fixtures/c-input.json @@ -1,10 +1,24 @@ { "fixtures": [ + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Aviation snippet title" } + }, { "match": { "userMessage": "Hello" }, "response": { "content": "Hi! I'm the chat-input demo. Try Enter to send, Shift+Enter for newline, and watch the sidebar reflect the streaming status." } + }, + { + "match": { "userMessage": "Stream a TypeScript code fence" }, + "response": { "content": "Here is the snippet:\n\n```typescript\nconst answer = 42;\n```\n\nThe constant is available for later use." }, + "chunkSize": 3, + "latency": 25 + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Saved answer for the chat-input demo." } } ] } diff --git a/cockpit/chat/input/react/e2e/global-setup-impl.ts b/cockpit/chat/input/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..6627775a1 --- /dev/null +++ b/cockpit/chat/input/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/input', port: 4621 }); diff --git a/cockpit/chat/input/react/e2e/input.spec.ts b/cockpit/chat/input/react/e2e/input.spec.ts index f5b2c13dc..5abd6e88c 100644 --- a/cockpit/chat/input/react/e2e/input.spec.ts +++ b/cockpit/chat/input/react/e2e/input.spec.ts @@ -1,4 +1,9 @@ import { test, expect, type Page } from '@playwright/test'; +// Backend: aimock replays the Angular example's e2e fixtures (shared) into a +// real `langgraph dev`; the React static server proxies /api to it, recording +// requests and injecting holds and failures (scripts/react-cockpit/langgraph-proxy.mjs). +const FENCE = 'Stream a TypeScript code fence'; +const HELLO = 'Hello'; type Wire = { path: string; method: string; @@ -24,18 +29,18 @@ async function send(page: Page, text: string) { const conversation = (page: Page) => page.getByRole('region', { name: 'Conversation', exact: true }); -test('installed runtime renders compiled graph code fences, isolates titles and confirms each continued turn', async ({ +test('real graph renders code fences, isolates titles and confirms each continued turn', async ({ page, request, }) => { await page.goto('/chat/input/react/'); await expect(page.getByRole('status')).toHaveText('Ready.'); expect(await (await request.get('/__requests')).json()).toEqual([]); - await send(page, 'Explain the aviation example'); + await send(page, FENCE); await expect(conversation(page).locator('pre code')).toHaveText( 'const answer = 42;' ); - await expect(conversation(page)).not.toContainText('Authored metadata title'); + await expect(conversation(page)).not.toContainText('Aviation snippet title'); await send(page, 'Continue with the same conversation'); await expect(conversation(page).locator('article')).toHaveCount(4); const wire: Wire[] = await (await request.get('/__requests')).json(); @@ -49,14 +54,6 @@ test('installed runtime renders compiled graph code fences, isolates titles and ).toBe(true); expect(wire.filter((r) => r.path.endsWith('/history'))).toHaveLength(2); expect(wire.filter((r) => r.method === 'GET')).toHaveLength(2); - const proofs = await (await request.get('/__graph-proof')).json(); - expect(proofs).toHaveLength(2); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - }); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(conversation(page).locator('article')).toHaveCount(0); @@ -88,8 +85,9 @@ test('held response Stop fences the run and explicit reset remains local', async page, request, }) => { + await request.post('/__hold/stream'); await page.goto('/chat/input/react/'); - await submit(page, 'Hold'); + await submit(page, HELLO); await expect(page.getByRole('status')).toHaveText('Receiving response…'); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.getByRole('status')).toHaveText( @@ -102,12 +100,13 @@ test('held response Stop fences the run and explicit reset remains local', async await expect(page.getByRole('status')).toHaveText('Ready.'); expect(await (await request.get('/__requests')).json()).toEqual(before); }); -test('renders actual graph output inside an unfinished code fence before release', async ({ +test('renders streamed graph output inside an unfinished code fence before release', async ({ page, request, }) => { + await request.post('/__hold/stream', { data: { afterText: 'const answer' } }); await page.goto('/chat/input/react/'); - await submit(page, 'Hold fence'); + await submit(page, FENCE); await expect(page.getByRole('status')).toHaveText('Receiving response…'); await expect(conversation(page).locator('pre code')).toContainText( 'const answer' @@ -131,8 +130,9 @@ test('pagehide closes an active installed transport and a new page never replays page, request, }) => { + await request.post('/__hold/stream'); await page.goto('/chat/input/react/'); - await submit(page, 'Hold'); + await submit(page, HELLO); await expect(page.getByRole('status')).toHaveText('Receiving response…'); expect(await (await request.get('/__lifetime')).json()).toMatchObject({ activeStream: true, @@ -323,8 +323,9 @@ for (const phase of ['stream', 'history'] as const) { request, }) => { if (phase === 'history') await request.post('/__history/hold'); + else await request.post('/__hold/stream'); await page.goto('/chat/input/react/'); - await submit(page, phase === 'stream' ? 'Hold' : 'Confirm this turn'); + await submit(page, HELLO); await expect(page.getByRole('status')).toHaveText( phase === 'stream' ? 'Receiving response…' : 'Confirming saved response…' ); @@ -370,7 +371,8 @@ test('local disabling blocks Send but preserves Stop during an active response', const disabled = page.getByRole('checkbox', { name: 'Disable message input', }); - await input.fill('Hold'); + await request.post('/__hold/stream'); + await input.fill(HELLO); await disabled.check(); await expect(input).toBeDisabled(); await expect( diff --git a/cockpit/chat/input/react/e2e/playwright.config.ts b/cockpit/chat/input/react/e2e/playwright.config.ts index 42380cadd..a20d6c7fb 100644 --- a/cockpit/chat/input/react/e2e/playwright.config.ts +++ b/cockpit/chat/input/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4621', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/input/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-input', cwd: '../../../../..', diff --git a/cockpit/chat/input/react/project.json b/cockpit/chat/input/react/project.json index 09c449287..a5a5e5027 100644 --- a/cockpit/chat/input/react/project.json +++ b/cockpit/chat/input/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/input/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-input-angular", "cockpit-chat-input-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,6 +33,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": [ "build", { diff --git a/cockpit/chat/messages/react/e2e/global-setup-impl.ts b/cockpit/chat/messages/react/e2e/global-setup-impl.ts index 6e1d73a66..22867e9fb 100644 --- a/cockpit/chat/messages/react/e2e/global-setup-impl.ts +++ b/cockpit/chat/messages/react/e2e/global-setup-impl.ts @@ -1,15 +1,6 @@ -import { resolve } from 'node:path'; -import { createLanggraphGlobalSetup } from '@threadplane-internal/e2e-harness'; -// eslint-disable-next-line @nx/enforce-module-boundaries -- Closed React cockpit identity shared with the static server. -import { reactCockpitConfiguration } from '../../../../../scripts/react-cockpit/configuration.mjs'; +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; -const { project, backend } = reactCockpitConfiguration('chat-messages'); - -// aimock replays the Angular example's fixtures; `langgraph dev` runs the real -// graph against it. The static server (webServer) proxies /api here. -export default createLanggraphGlobalSetup({ - stateKey: project, - langgraphCwd: backend.python, - langgraphPort: backend.langgraphPort, - fixturesDir: resolve(__dirname, '../../../../..', backend.fixtures), -}); +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/messages', port: 4620 }); diff --git a/cockpit/chat/messages/react/project.json b/cockpit/chat/messages/react/project.json index 3137081b0..b0552e021 100644 --- a/cockpit/chat/messages/react/project.json +++ b/cockpit/chat/messages/react/project.json @@ -43,6 +43,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": [ "build", { diff --git a/cockpit/chat/theming/angular/e2e/fixtures/c-theming.json b/cockpit/chat/theming/angular/e2e/fixtures/c-theming.json index 3da770b38..1fad62de5 100644 --- a/cockpit/chat/theming/angular/e2e/fixtures/c-theming.json +++ b/cockpit/chat/theming/angular/e2e/fixtures/c-theming.json @@ -1,10 +1,18 @@ { "fixtures": [ + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Aviation snippet title" } + }, { "match": { "userMessage": "Hello" }, "response": { "content": "Hi! I'm the chat-theming demo. Try the theme picker buttons in the sidebar to switch between dark, light, ocean, and forest schemes." } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Lift is the upward force on a wing.\n\n```typescript\nconst lift = 'up';\n```" } } ] } diff --git a/cockpit/chat/theming/react/e2e/global-setup-impl.ts b/cockpit/chat/theming/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..e4a820634 --- /dev/null +++ b/cockpit/chat/theming/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/theming', port: 4644 }); diff --git a/cockpit/chat/theming/react/e2e/playwright.config.ts b/cockpit/chat/theming/react/e2e/playwright.config.ts index 1d755f756..30d53712c 100644 --- a/cockpit/chat/theming/react/e2e/playwright.config.ts +++ b/cockpit/chat/theming/react/e2e/playwright.config.ts @@ -8,6 +8,9 @@ export default defineConfig({ outputDir: '../../../../../output/playwright/chat-theming-react', use: { baseURL: 'http://127.0.0.1:4644', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/theming/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent', cwd: '../../../../..', diff --git a/cockpit/chat/theming/react/e2e/theming.spec.ts b/cockpit/chat/theming/react/e2e/theming.spec.ts index 3d262494e..138c60216 100644 --- a/cockpit/chat/theming/react/e2e/theming.spec.ts +++ b/cockpit/chat/theming/react/e2e/theming.spec.ts @@ -55,31 +55,21 @@ async function submit(page: Page, text: string) { } test.beforeEach(async ({ request }) => { await control(request, '/__reset'); + // Real ChatOpenAI never surfaces reasoning; inject it so palettes reach the + // Reasoning region (scripts/react-cockpit/langgraph-proxy.mjs). + expect( + (await request.post('/__inject/reasoning', { + data: { text: 'Considering the aviation question.' }, + })).ok() + ).toBe(true); }); test.afterEach(async ({ page, request }, info) => { + // Every browser connection through the proxy closes and nothing stays held. + // (No per-run graph child processes exist any more: the graph is `langgraph dev`.) await page.goto('about:blank'); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) - .toEqual({ children: 0, streams: 0, histories: 0, handlers: 0 }); - await expect - .poll( - async () => (await (await request.get('/__lifetime')).json()).responseOpen - ) - .toBe(0); - const lifetime = await (await request.get('/__lifetime')).json(); - for (const child of lifetime.children) { - expect(child.reaped).toBe(true); - expect(() => process.kill(child.pid, 0)).toThrow(/ESRCH/); - expect(() => process.kill(-child.pgid, 0)).toThrow(/ESRCH/); - } - await writeFile( - info.outputPath('observed-child-absence.json'), - JSON.stringify( - { children: lifetime.children, observedPidAndPgidAbsence: 'ESRCH' }, - null, - 2 - ) - ); + .poll(async () => (await request.get('/__lifetime')).json()) + .toEqual({ activeStream: false, activeHistory: false, openResponses: 0 }); for (const path of ['/__requests', '/__captures', '/__lifetime']) { const body = JSON.stringify( await (await request.get(path)).json(), @@ -222,28 +212,38 @@ for (const width of [1280, 390]) { ); expect(runs.every((r) => r.body.assistant_id === 'c-theming')).toBe(true); expect(wire.filter((r) => r.path.endsWith('/history'))).toHaveLength(2); - const captures = await (await request.get('/__captures')).json(); + const captures: { + sse: string; + savedMessages: { type: string; content: string }[]; + titleBefore: string | null; + titleAfter: string | null; + }[] = await (await request.get('/__captures')).json(); expect(captures).toHaveLength(2); + // The title model's reply (its own aimock fixture) never streams as answer + // content. (It does appear in later runs' metadata: thread metadata.) + const streamedContent = (sse: string) => + sse + .split(/\r?\n\r?\n/) + .filter((event) => /^event: messages/m.test(event)) + .map((event) => JSON.parse(event.split(/\r?\n/).find((l) => l.startsWith('data:'))!.slice(5))[0].content) + .join(''); for (const c of captures) { - expect(c.proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - }); - expect(c.sse).not.toContain('Authored metadata title'); + expect(streamedContent(c.sse)).toContain('upward force'); + expect(streamedContent(c.sse)).not.toContain('Aviation snippet title'); } expect( - captures[0].savedMessages.find( - (m: { type: string }) => m.type === 'human' - ).content + captures[0].savedMessages.find((m) => m.type === 'human')?.content ).toBe('Explain lift ✈ café'); expect( captures[1].savedMessages - .filter((m: { type: string }) => m.type === 'human') - .map((m: { content: string }) => m.content) + .filter((m) => m.type === 'human') + .map((m) => m.content) ).toEqual(['Explain lift ✈ café', 'Continue lift — aérodrome']); - expect(captures[0].titleUpdates).toHaveLength(1); - expect(captures[1].titleUpdates).toHaveLength(0); + // The title is written once, on the first turn, and kept on the second. + expect(captures[0].titleBefore).toBeNull(); + expect(captures[0].titleAfter).toBe('Aviation snippet title'); + expect(captures[1].titleBefore).toBe('Aviation snippet title'); + expect(captures[1].titleAfter).toBe('Aviation snippet title'); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(page.locator('article')).toHaveCount(0); @@ -258,8 +258,9 @@ for (const width of [1280, 390]) { request, }) => { await page.setViewportSize({ width, height: 900 }); + await control(request, '/__hold/stream'); await page.goto('/chat/theming/react/'); - await submit(page, 'Hold'); + await submit(page, 'Hello'); await expect(page.getByRole('status')).toHaveText('Receiving response…'); await page .getByLabel('Message', { exact: true }) @@ -297,9 +298,9 @@ for (const phase of ['stream', 'history']) page, request, }) => { - if (phase === 'history') await control(request, '/__history/hold'); + await control(request, phase === 'history' ? '/__history/hold' : '/__hold/stream'); await page.goto('/chat/theming/react/'); - await submit(page, phase === 'stream' ? 'Hold' : 'Explain lift'); + await submit(page, 'Hello'); await expect(page.getByRole('status')).toHaveText( phase === 'stream' ? 'Receiving response…' : 'Confirming saved response…' ); @@ -321,8 +322,9 @@ test('pagehide closes the original stream and remount never replays it', async ( page, request, }) => { + await control(request, '/__hold/stream'); await page.goto('/chat/theming/react/'); - await submit(page, 'Hold'); + await submit(page, 'Hello'); await expect(page.getByRole('status')).toHaveText('Receiving response…'); await page.goto('about:blank'); await expect @@ -330,9 +332,9 @@ test('pagehide closes the original stream and remount never replays it', async ( async () => ( await (await request.get('/__lifetime')).json() - ).active.streams + ).activeStream ) - .toBe(0); + .toBe(false); const before = await requests(request); await page.goto('/chat/theming/react/'); await expect(page.getByRole('status')).toHaveText('Ready.'); diff --git a/cockpit/chat/theming/react/project.json b/cockpit/chat/theming/react/project.json index deaefb3d1..bcf5f399f 100644 --- a/cockpit/chat/theming/react/project.json +++ b/cockpit/chat/theming/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/theming/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-theming-angular", "cockpit-chat-theming-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,7 +33,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "inputs": ["default", "themingFixture"], + "inputs": ["default", "reactAimockBackend"], "dependsOn": [ "build", { @@ -45,12 +46,5 @@ "skipInstall": true } } - }, - "namedInputs": { - "themingFixture": [ - "{workspaceRoot}/scripts/react-cockpit/chat-theming-fixture.mjs", - "{workspaceRoot}/scripts/react-cockpit/chat-theming-wire.py", - "{workspaceRoot}/scripts/react-cockpit/chat-theming-fixture.spec.mjs" - ] } } diff --git a/cockpit/shared/react-aimock-setup.ts b/cockpit/shared/react-aimock-setup.ts new file mode 100644 index 000000000..62cccb238 --- /dev/null +++ b/cockpit/shared/react-aimock-setup.ts @@ -0,0 +1,27 @@ +import { resolve } from 'node:path'; +import { createLanggraphGlobalSetup } from '@threadplane-internal/e2e-harness'; + +/** + * Playwright global setup for an aimock-backed React cockpit example: aimock + * replays the Angular example's fixtures into a real `langgraph dev`, and the + * static server (the config's `webServer`) proxies /api to it through + * scripts/react-cockpit/langgraph-proxy.mjs. + * + * Mirrors `aimockLanggraph()` in scripts/react-cockpit/configuration.mjs + * (scripts/cockpit-frontend-matrix.spec.mjs keeps the two in step). It does + * not import it, so unrelated `scripts/` changes don't select every React cap. + */ +export function reactAimockGlobalSetup(options: { + /** e.g. 'chat/theming' */ + example: string; + /** The React app port from configuration.mjs. */ + port: number; +}): () => Promise { + const name = `cockpit-${options.example.replace('/', '-')}-react`; + return createLanggraphGlobalSetup({ + stateKey: name, + langgraphCwd: `cockpit/${options.example}/python`, + langgraphPort: options.port + 1000, + fixturesDir: resolve(__dirname, '..', options.example, 'angular/e2e/fixtures'), + }); +} diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 81dcf64b4..3e1dcbeba 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -63,10 +63,25 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | `POST /__release` | emit the rest | flushes the buffer and resumes piping | | `GET /__lifetime` | held stream or history | same (`activeStream`, `activeHistory`) | | `GET /__graph-proof` | in-process proof | removed; see "Guarantees" | + | `GET /__captures` (batch 1) | authored SSE, saved messages, title updates | per finished run: forwarded SSE text, saved `messages`/`next` from the real `/state`, and thread title before and after the run, all captured before the browser sees the stream end | + | `GET /__lifetime` child reaping (batch 1) | owned wire child processes | no per-run graph processes exist any more, so `openResponses` (proxied browser connections still open) replaces the child-reaping check | + | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data**. `ChatOpenAI` over Chat Completions never surfaces reasoning, so a real graph can't produce it. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`), so the theming palette checks still reach the Reasoning region | Holds are now explicit controls, not magic prompt text, so every test prompt can be a shared fixture prompt. + Prompts that only exercise the client (raw whitespace, multiline drafts, + IME guards) are answered by a **catch-all fixture**, + `{ "match": { "userMessage": "" } }`. It goes last in the file, because + aimock matches `userMessage` by substring and the first match wins. + Prompts whose reply matters keep their own fixture. + + Each migrated example has the same one-line `e2e/global-setup-impl.ts` + (`reactAimockGlobalSetup('')` from + `cockpit/shared/react-aimock-setup.ts`), and its `configuration.mjs` + entry gets `backend: aimockLanggraph('', )`, where the + langgraph port is the app port + 1000. + ## Guarantees: kept, moved, or replaced | Old guarantee | Where it lives now | @@ -115,7 +130,7 @@ example needs that the pilot proxy doesn't yet provide. | Batch | Examples | Controls beyond the generic proxy | Notes | | --- | --- | --- | --- | | 0 (done) | chat/messages | none | pilot | -| 1 | chat/input, chat/theming | `/__captures` (theming) | generic set otherwise | +| 1 (done) | chat/input, chat/theming | `/__captures`, `/__inject/reasoning` (theming) | | | 2 | chat/tool-calls, chat/subagents, chat/interrupts, chat/generative-ui | hold on a predicate (tool call, child or subgraph event, resume), `/__fail-resume`, `/__worker-failure`, `/__metadata-failure`, `/__configure` | add `afterEvent` predicates to `/__hold/stream`, plus route-matched failure injection | | 3 | chat/threads, chat/timeline, chat/debug | title modes and holds, state and checkpoint holds, mapped checkpoints | history and state rewriting, holds per endpoint | | 4 | langgraph/{streaming, persistence, memory, interrupts, subgraphs, client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-draft`, `/__hold-extraction`, `/__hold-plan`, `/__publish-*`, `/__bad-history`, `/__unsafe-source` | `durable-execution` `/__publish-*` injects authored events that a real graph can't emit, so it keeps a small authored layer on top of the proxy | diff --git a/nx.json b/nx.json index 840ab30f2..cae70dff9 100644 --- a/nx.json +++ b/nx.json @@ -15,7 +15,13 @@ "!{projectRoot}/.eslintrc.json", "!{projectRoot}/eslint.config.mjs" ], - "sharedGlobals": [] + "sharedGlobals": [], + "reactAimockBackend": [ + "{workspaceRoot}/scripts/react-cockpit/langgraph-proxy.mjs", + "{workspaceRoot}/scripts/react-cockpit/serve.mjs", + "{workspaceRoot}/scripts/react-cockpit/configuration.mjs", + "{workspaceRoot}/cockpit/shared/react-aimock-setup.ts" + ] }, "plugins": [ { diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index 7e964e7c5..edf8bc29a 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - '47b703c5eebce05e9b71f4e06acf7865a59a03587296b28752e88ab463389323', + '7b4549c58f77e6432e7788891dc793c976c5072b54f669d323503baaf517bde2', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); @@ -2615,14 +2615,15 @@ it('representative smoke includes the canonical tagged Theming graph', async () assert.match(ci, /--projects=[^\n]*cockpit-chat-theming-python/); }); -it('runs bounded Theming fixture tests in its actual React fleet lane', async () => { +it('runs Theming configuration and network-free graph tests for its aimock React lane', async () => { const workflow = await readFile('.github/workflows/ci.yml', 'utf8'); const scope = readNamedStep(readJobBlock(workflow, 'ci-guards'), 'Test CI scope classifier'); assert.ok(scope.includes('scripts/react-cockpit/chat-theming-configuration.spec.mjs')); - const step = readNamedStep(readJobBlock(workflow, 'cockpit-e2e'), 'Verify native Theming fixture ownership'); - assert.match(step, /if: matrix\.cap\.angular == 'cockpit-chat-theming-react'/); - assert.match(step, /npx nx run cockpit-chat-theming-python:test --skip-nx-cache/); - assert.match(step, /node --test --test-concurrency=1 scripts\/react-cockpit\/chat-theming-fixture.spec.mjs/); + // The authored fixture server is gone: e2e runs the real graph on aimock, and + // the network-free compiled-graph proof is the Python test e2e depends on. + assert.doesNotMatch(workflow, /chat-theming-fixture\.spec\.mjs/); + const project = JSON.parse(await readFile('cockpit/chat/theming/react/project.json', 'utf8')); + assert.deepEqual(project.targets.e2e.dependsOn.at(-1), { target: 'test', projects: ['cockpit-chat-theming-python'] }); assert.match(readJobBlock(workflow, 'cockpit-e2e'), /output\/playwright\/chat-theming-react\//); }); diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 531b8bae4..e427e0316 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -179,6 +179,10 @@ test('Chat Input owns a closed native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('chat-input'), { topic: 'input', adapter: 'langgraph', appPath: 'cockpit/chat/input/react', base: '/chat/input/react/', port: 4621, project: 'cockpit-chat-input-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/input/python', + fixtures: 'cockpit/chat/input/angular/e2e/fixtures', langgraphPort: 5621, + }, }); for (const key of ['input', 'chat/input', '../chat-input']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); @@ -625,3 +629,26 @@ test('AG-UI Client Tools selects the two native frontends and one canonical back assert.equal(frontends.filter(cap => cap.frontend === 'angular').length, 41); assert.equal(frontends.find(cap => cap.project === 'cockpit-ag-ui-client-tools-react')?.runtimePath, 'ag-ui/client-tools/react'); }); +test('aimock-backed React e2e setups match their closed configuration backends', async () => { + const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); + const { readdirSync, readFileSync, existsSync } = await import('node:fs'); + const walk = (dir) => readdirSync(dir, { withFileTypes: true }).flatMap((entry) => + entry.isDirectory() ? walk(`${dir}/${entry.name}`) : entry.name === 'global-setup-impl.ts' && dir.endsWith('/react/e2e') ? [`${dir}/${entry.name}`] : []); + const setups = walk('cockpit'); + assert.ok(setups.length > 0); + for (const file of setups) { + const call = /reactAimockGlobalSetup\(\{ example: '([^']+)', port: (\d+) \}\)/.exec(readFileSync(file, 'utf8')); + assert.ok(call, `${file} uses reactAimockGlobalSetup`); + const [, example, port] = call; + const serve = /serve\.mjs ([a-z0-9-]+)/.exec(readFileSync(file.replace('global-setup-impl.ts', 'playwright.config.ts'), 'utf8'))[1]; + const configuration = reactCockpitConfiguration(serve); + assert.equal(configuration.port, Number(port), file); + assert.deepEqual(configuration.backend, { + kind: 'aimock-langgraph', + python: `cockpit/${example}/python`, + fixtures: `cockpit/${example}/angular/e2e/fixtures`, + langgraphPort: Number(port) + 1000, + }, file); + assert.ok(existsSync(configuration.backend.fixtures), configuration.backend.fixtures); + } +}); diff --git a/scripts/cockpit-matrix.spec.mjs b/scripts/cockpit-matrix.spec.mjs index 4b0ac2096..e5710eb76 100644 --- a/scripts/cockpit-matrix.spec.mjs +++ b/scripts/cockpit-matrix.spec.mjs @@ -262,9 +262,7 @@ describe('Theming fixture files through actual Nx attribution', () => { const root = fileURLToPath(new URL('../', import.meta.url)); const project = 'cockpit-chat-theming-react'; for (const file of [ - 'scripts/react-cockpit/chat-theming-fixture.mjs', - 'scripts/react-cockpit/chat-theming-wire.py', - 'scripts/react-cockpit/chat-theming-fixture.spec.mjs', + 'scripts/react-cockpit/langgraph-proxy.mjs', 'scripts/cockpit-matrix.spec.mjs', ]) test(`attributes ${file} without forcing the fleet`, () => { const affected = new Set(JSON.parse(execFileSync(process.execPath, [ diff --git a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs b/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs index f43a9c2ff..0cc32197c 100644 --- a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs +++ b/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs @@ -36,7 +36,7 @@ test('proof routing scopes native and retained LangGraph fixtures and shutdown t const serve=read('scripts/react-cockpit/serve.mjs'); assert.match(serve,/configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' \? createAgUiClientToolsFixture/); assert.match(serve,/configuration.adapter !== 'ag-ui' && configuration.topic === 'client-tools' \? createClientToolsFixture/); - assert.match(serve,/await \(chatDebugFixture \?\? chatThemingFixture \?\? nativeStrandsFixture \?\? nativeMafFixture \?\? nativeMastraFixture \?\? agUiClientToolsFixture \?\? [^\n]+\)\.close\(\);/); + assert.match(serve,/await \(chatDebugFixture \?\? nativeStrandsFixture \?\? nativeMafFixture \?\? nativeMastraFixture \?\? agUiClientToolsFixture \?\? [^\n]+\)\.close\(\);/); assert.match(read('cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts'),/prepare-ag-ui-client-tools-fixture.mjs --serve/); assert.match(read('scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs'),/'ag-ui-client-tools', '--no-parent'/); }); diff --git a/scripts/react-cockpit/chat-input-fixture.mjs b/scripts/react-cockpit/chat-input-fixture.mjs deleted file mode 100644 index b288d7f31..000000000 --- a/scripts/react-cockpit/chat-input-fixture.mjs +++ /dev/null @@ -1,278 +0,0 @@ -/** Local installed-SDK proof; graph output is derived, never public assets. */ -import { spawn } from 'node:child_process'; -import { fileURLToPath } from 'node:url'; - -async function graphWire(input) { - const root = fileURLToPath(new URL('../../', import.meta.url)); - return new Promise((resolve, reject) => { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/input/python', - 'python', - 'scripts/react-cockpit/chat-input-wire.py', - ], - { cwd: root, stdio: ['pipe', 'pipe', 'pipe'] } - ); - let output = ''; - child.stdout.on('data', (chunk) => { - output += chunk; - }); - child.stderr.resume(); - child.once('error', reject); - child.once('close', (code) => { - if (code !== 0) return reject(new Error('Authored graph wire failed.')); - try { - resolve(JSON.parse(output)); - } catch { - reject(new Error('Authored graph wire was invalid.')); - } - }); - child.stdin.end(JSON.stringify(input)); - }); -} -export function createChatInputFixture() { - let requests = [], - sequence = 0, - failCreation = false, - historyMode = 'safe', - heldRun, - heldHistory; - const threads = new Map(), - runs = new Map(), - proofs = []; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - heldRun?.response.end(); - heldHistory?.response.end(); - heldRun = heldHistory = undefined; - requests = []; - sequence = 0; - failCreation = false; - historyMode = 'safe'; - threads.clear(); - runs.clear(); - proofs.length = 0; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__lifetime') { - json(response, { - activeStream: Boolean(heldRun), - activeHistory: Boolean(heldHistory), - }); - return true; - } - if (pathname === '/__fail-create') { - failCreation = true; - json(response, {}); - return true; - } - if (pathname === '/__release') { - const held = heldRun; - heldRun = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname === '/__release-history') { - const held = heldHistory; - heldHistory = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__history/')) { - const mode = pathname.split('/').at(-1); - if ( - !['safe', 'hold', 'changed', 'missing', 'pending', 'failed'].includes( - mode - ) - ) { - json(response, {}, 400); - return true; - } - historyMode = mode; - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads') && request.method === 'POST') { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - if (typeof body.thread_id !== 'string' || threads.has(body.thread_id)) { - json(response, {}, 400); - return true; - } - threads.set(body.thread_id, { messages: [], title: null, next: [] }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - state = threads.get(threadId); - if (!state) { - json(response, {}, 404); - return true; - } - if (pathname.endsWith('/history') && request.method === 'POST') { - const finish = () => { - if (response.destroyed) return; - if (historyMode === 'failed') { - json(response, { error: 'PRIVATE history' }, 500); - return; - } - let messages = state.messages; - if (historyMode === 'changed') - messages = messages.map((m, i) => - i === messages.length - 1 - ? { ...m, content: 'Changed saved answer' } - : m - ); - if (historyMode === 'missing') messages = messages.slice(0, -1); - json(response, [ - { - values: { messages }, - next: historyMode === 'pending' ? ['generate'] : state.next, - checkpoint: { - thread_id: threadId, - checkpoint_ns: '', - checkpoint_id: 'authored-checkpoint-' + sequence, - }, - parent_checkpoint: null, - created_at: '2026-10-04T00:00:00Z', - metadata: {}, - tasks: [], - }, - ]); - }; - if (historyMode === 'hold') { - const held = { response, finish }; - heldHistory = held; - response.on('close', () => { - if (heldHistory === held) heldHistory = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream') && request.method === 'POST') { - if ( - body.assistant_id !== 'c-input' || - body.command || - body.input?.messages?.length !== 1 || - body.input.messages[0].type !== 'human' - ) { - json(response, {}, 400); - return true; - } - const human = body.input.messages[0], - runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - const run = { thread_id: threadId, status: 'running' }; - runs.set(runId, run); - let wire; - try { - wire = await graphWire({ - threadId, - answerId: 'authored-answer-' + sequence, - messages: [...state.messages, human], - title: state.title, - }); - } catch { - json(response, { error: 'Graph fixture failed' }, 500); - return true; - } - proofs.push(wire.proof); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const emitNext = () => response.write(event(...wire.events[sent++])); - const finish = () => { - if (response.destroyed) return; - while (sent < wire.events.length) emitNext(); - state.messages = wire.messages; - state.next = wire.next; - state.title = wire.title; - run.status = 'success'; - response.end(); - }; - if (human.content === 'Hold' || human.content === 'Hold fence') { - let content = ''; - while (sent < wire.events.length) { - const [kind, value] = wire.events[sent]; - emitNext(); - if (kind === 'messages') { - content += value[0].content; - if (human.content === 'Hold' || content.includes('const answer =')) - break; - } - } - const held = { response, finish, run }; - heldRun = held; - response.on('close', () => { - if (heldRun === held) { - heldRun = undefined; - run.status = 'interrupted'; - } - }); - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if (cancelId && request.method === 'POST') { - if (heldRun?.run === runs.get(cancelId)) { - heldRun.run.status = 'interrupted'; - heldRun.response.end(); - heldRun = undefined; - } - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1], - run = runs.get(runId); - if (runId && request.method === 'GET' && run?.thread_id === threadId) { - json(response, { run_id: runId, ...run }); - return true; - } - json(response, { error: 'Unsupported authored endpoint' }, 404); - return true; - }; -} diff --git a/scripts/react-cockpit/chat-input-wire.py b/scripts/react-cockpit/chat-input-wire.py deleted file mode 100644 index 4bca5ca9e..000000000 --- a/scripts/react-cockpit/chat-input-wire.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Emit real compiled Chat Input graph events using network-free providers.""" -import asyncio -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/input/python" -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, AuthoredModel -from langchain_core.messages import HumanMessage, AIMessage -from langgraph.checkpoint.memory import InMemorySaver - - -async def main(): - request = json.load(sys.stdin) - client = AuthoredClient(title=request.get("title")) - source = PROJECT / "src/graph.py" - def factory(**kwargs): - return AuthoredModel(title=not kwargs.get("streaming", False), - answer_id=request["answerId"], tags=kwargs.get("tags", [])) - with patch.object(socket.socket, "connect", side_effect=AssertionError( - "Compiled fixture must never connect to a remote service" - )) as connect, patch("langchain_openai.ChatOpenAI", factory), patch( - "langgraph_sdk.get_client", return_value=client, - ): - spec = importlib.util.spec_from_file_location("authored_input_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_input_graph().builder.compile(checkpointer=InMemorySaver()) - config = {"configurable": {"thread_id": request["threadId"]}} - messages = [ - (HumanMessage if m["type"] == "human" else AIMessage)(content=m["content"], id=m["id"]) - for m in request["messages"] - ] - events = [] - async for kind, value in graph.astream({"messages": messages}, config, stream_mode=["messages", "values"]): - if kind == "messages": - message, metadata = value - if metadata.get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into the answer stream") - events.append([kind, [message.model_dump(mode="json"), metadata]]) - else: - events.append([kind, {"messages": [m.model_dump(mode="json") for m in value["messages"]]}]) - state = graph.get_state(config) - connect.assert_not_called() - print(json.dumps({ - "events": events, - "messages": [m.model_dump(mode="json") for m in state.values["messages"]], - "next": list(state.next), - "title": client.threads.updates[0][1]["title"] if client.threads.updates else request.get("title"), - "proof": {"actualCompiledGraph": True, "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0}, - })) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/chat-theming-configuration.spec.mjs b/scripts/react-cockpit/chat-theming-configuration.spec.mjs index c4892ef5c..08bbdf9cf 100644 --- a/scripts/react-cockpit/chat-theming-configuration.spec.mjs +++ b/scripts/react-cockpit/chat-theming-configuration.spec.mjs @@ -13,6 +13,10 @@ test('theming is an exact closed native LangGraph topic', () => { base: '/chat/theming/react/', port: 4644, project: 'cockpit-chat-theming-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/theming/python', + fixtures: 'cockpit/chat/theming/angular/e2e/fixtures', langgraphPort: 5644, + }, }); assert.ok(Object.isFrozen(reactCockpitConfiguration('chat-theming'))); for (const topic of [ @@ -40,9 +44,9 @@ test('theming production inputs exclude local fixture and controls', () => { for (const path of visit(root)) assert.doesNotMatch( readFileSync(path, 'utf8'), - /chat-theming-(?:wire|fixture)|\/__(?:release|history|requests|graph-proof|lifetime)/ + /langgraph-proxy|\/__(?:release|history|requests|captures|graph-proof|lifetime)/ ); const build = readFileSync('scripts/react-cockpit/build.mjs', 'utf8'); assert.match(build, /cpSync\(join\(root, appPath, 'src'\)/); - assert.doesNotMatch(build, /chat-theming-fixture|chat-theming-wire/); + assert.doesNotMatch(build, /langgraph-proxy/); }); diff --git a/scripts/react-cockpit/chat-theming-fixture.mjs b/scripts/react-cockpit/chat-theming-fixture.mjs deleted file mode 100644 index c4c6af52e..000000000 --- a/scripts/react-cockpit/chat-theming-fixture.mjs +++ /dev/null @@ -1,569 +0,0 @@ -/** Local compiled graph/installed SDK seam. Never imported by production assets. */ -import { spawn } from 'node:child_process'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -const ROOT = fileURLToPath(new URL('../../', import.meta.url)); -const MAX_BODY = 65536, - MAX_OUTPUT = 2 * 1024 * 1024; -const json = (res, value, status = 200) => { - if (res.destroyed || res.writableEnded) return; - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); -}; -const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; -const delay = (ms) => new Promise((r) => setTimeout(r, ms)); - -export function createChatThemingFixture({ - wireTimeoutMs = 10000, - wireOutputBytes = MAX_OUTPUT, -} = {}) { - if ( - !Number.isInteger(wireTimeoutMs) || - wireTimeoutMs < 1 || - wireTimeoutMs > 10000 || - !Number.isInteger(wireOutputBytes) || - wireOutputBytes < 1 || - wireOutputBytes > MAX_OUTPUT - ) - throw Error( - 'Local wire bounds must be positive and cannot exceed the fixed maxima' - ); - const threads = new Map(), - runs = new Map(), - children = new Set(); - const childReceipts = [], - streams = new Set(), - histories = new Set(), - handlers = new Set(), - responses = new Map(); - let requests = [], - captures = [], - historyMode = 'safe', - closed = false, - closing; - const receipt = () => ({ - closed, - active: { - children: children.size, - streams: streams.size, - histories: histories.size, - handlers: handlers.size, - }, - responseOpen: responses.size, - children: childReceipts.map((r) => ({ ...r })), - }); - function terminate(owner, signal) { - if (owner.child.exitCode !== null || owner.child.signalCode !== null) - return; - try { - process.kill(-owner.pid, signal); - owner.record.signals.push({ signal, group: owner.pid, result: 'sent' }); - } catch (error) { - owner.record.signalErrors.push({ signal, code: error.code }); - owner.record.signals.push({ - signal, - group: owner.pid, - result: error.code, - }); - if (error.code !== 'ESRCH') { - // Direct Python is the sole owned child: keep draining even if the OS - // refuses a group signal. This is not a cancellation acknowledgment. - try { - const sent = owner.child.kill(signal); - owner.record.signals.push({ - signal, - pid: owner.pid, - directChild: true, - result: sent ? 'sent' : 'not-sent', - }); - } catch (fallbackError) { - owner.record.signalErrors.push({ - signal, - directChild: true, - code: fallbackError.code, - }); - owner.record.signals.push({ - signal, - pid: owner.pid, - directChild: true, - result: fallbackError.code, - }); - } - } - } - } - async function graphWire(input) { - if (closed) throw Error('Fixture closed'); - const child = spawn( - fileURLToPath( - new URL( - '../../cockpit/chat/theming/python/.venv/bin/python', - import.meta.url - ) - ), - ['-E', '-B', 'scripts/react-cockpit/chat-theming-wire.py'], - { - cwd: ROOT, - detached: true, - stdio: ['pipe', 'pipe', 'pipe'], - env: { - PATH: '/usr/bin:/bin', - LANG: 'en_US.UTF-8', - LANGSMITH_TRACING: 'false', - LANGCHAIN_TRACING_V2: 'false', - PYTHONDONTWRITEBYTECODE: '1', - }, - } - ); - const record = { - pid: child.pid ?? null, - pgid: child.pid ?? null, - reaped: false, - exitCode: null, - signal: null, - stdoutBytes: 0, - stderrBytes: 0, - signalErrors: [], - signals: [], - }; - childReceipts.push(record); - let finish, fail; - const ended = new Promise((resolve) => { - finish = resolve; - }); - const result = new Promise((resolve, rejectPromise) => { - fail = rejectPromise; - let output = '', - rejected = false; - // Handlers attach synchronously before acquisition can settle. - child.once('error', (error) => { - rejected = true; - rejectPromise(error); - }); - child.stdout.on('data', (bytes) => { - record.stdoutBytes += bytes.length; - if (record.stdoutBytes > wireOutputBytes) { - rejected = true; - rejectPromise(Error('Wire output bound exceeded')); - terminate(owner, 'SIGTERM'); - } else output += bytes; - }); - child.stderr.on('data', (bytes) => { - record.stderrBytes += bytes.length; - if (record.stderrBytes > 65536) { - rejected = true; - rejectPromise(Error('Wire diagnostic bound exceeded')); - terminate(owner, 'SIGTERM'); - } - }); - child.stdin.on('error', () => { - /* close/error is settled by the child owner */ - }); - child.once('close', (code, signal) => { - clearTimeout(owner.timer); - clearTimeout(owner.killTimer); - record.reaped = true; - record.exitCode = code; - record.signal = signal; - children.delete(owner); - finish(); - if (rejected) return; - if (closed || code !== 0) - return rejectPromise(Error('Compiled graph child failed or closed')); - try { - resolve(JSON.parse(output)); - } catch { - rejectPromise(Error('Invalid compiled graph output')); - } - }); - }); - const owner = { - child, - pid: child.pid, - record, - ended, - timer: null, - killTimer: null, - fail, - }; - children.add(owner); - owner.timer = setTimeout(() => { - fail(Error('Compiled graph deadline exceeded')); - terminate(owner, 'SIGTERM'); - owner.killTimer = setTimeout(() => terminate(owner, 'SIGKILL'), 250); - }, wireTimeoutMs); - child.stdin.end(JSON.stringify(input)); - try { - return await result; - } finally { - if (!record.reaped) { - terminate(owner, 'SIGTERM'); - owner.killTimer ??= setTimeout(() => terminate(owner, 'SIGKILL'), 250); - } - await Promise.race([ - ended, - delay(1500).then(() => { - if (!record.reaped) throw Error('Owned wire child did not reap'); - }), - ]); - } - } - function hold(set, response, finish, onAbort = () => {}) { - const held = { - response, - finish: () => { - clearTimeout(held.timer); - set.delete(held); - finish(); - }, - timer: null, - }; - set.add(held); - response.once('close', () => { - clearTimeout(held.timer); - if (set.delete(held)) onAbort(); - }); - held.timer = setTimeout(() => { - set.delete(held); - onAbort(); - response.destroy(); - }, 30000); - return held; - } - async function bodyOf(request) { - const chunks = []; - let bytes = 0; - const timer = setTimeout(() => request.destroy(), 2000); - try { - for await (const chunk of request) { - bytes += chunk.length; - if (bytes > MAX_BODY) { - const e = Error('Input bound'); - e.status = 413; - throw e; - } - chunks.push(chunk); - } - if (bytes === 0) return {}; - // HTTP chunks can split a UTF-8 code point. Enforce the raw byte bound - // first, then decode the complete input once without replacement text. - const body = new TextDecoder('utf-8', { fatal: true }).decode( - Buffer.concat(chunks, bytes) - ); - return JSON.parse(body); - } finally { - clearTimeout(timer); - } - } - async function dispatch(request, response, pathname) { - if (closed) { - json(response, { error: 'Fixture closed' }, 503); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__captures') { - json(response, captures); - return true; - } - if (pathname === '/__graph-proof') { - json( - response, - captures.map((c) => c.proof) - ); - return true; - } - if (pathname === '/__lifetime') { - json(response, receipt()); - return true; - } - if (pathname.startsWith('/__')) { - if (request.method !== 'POST') { - json(response, {}, 405); - return true; - } - if (pathname === '/__reset') { - if (children.size || streams.size || histories.size) { - json(response, { error: 'Owned work remains' }, 409); - return true; - } - requests = []; - captures = []; - threads.clear(); - runs.clear(); - historyMode = 'safe'; - json(response, {}); - return true; - } - if (pathname === '/__release') { - for (const h of [...streams]) h.finish(); - json(response, {}); - return true; - } - if (pathname === '/__release-history') { - for (const h of [...histories]) h.finish(); - json(response, {}); - return true; - } - if (pathname === '/__history/hold' || pathname === '/__history/safe') { - historyMode = pathname.endsWith('hold') ? 'hold' : 'safe'; - json(response, {}); - return true; - } - json(response, {}, 404); - return true; - } - if (!/^\/(?:api|developer-api)\//.test(pathname)) return false; - let body; - try { - body = await bodyOf(request); - } catch (error) { - json(response, { error: 'Invalid local input' }, error.status ?? 400); - return true; - } - if (requests.length >= 256) { - json(response, {}, 429); - return true; - } - requests.push({ path: pathname, method: request.method, body }); - if (/\/threads$/.test(pathname) && request.method === 'POST') { - if ( - !/^[0-9a-f-]{36}$/.test(body.thread_id) || - body.if_exists !== 'raise' || - threads.has(body.thread_id) - ) { - json(response, {}, 400); - return true; - } - threads.set(body.thread_id, { - messages: [], - title: null, - next: [], - checkpoint: null, - }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - state = threads.get(threadId); - if (!state) { - json(response, {}, 404); - return true; - } - if (pathname.endsWith('/history') && request.method === 'POST') { - const finish = () => { - const saved = [ - { - values: { messages: state.messages }, - next: state.next, - checkpoint: state.checkpoint, - parent_checkpoint: state.parentCheckpoint, - created_at: state.createdAt, - metadata: {}, - tasks: [], - }, - ]; - const capture = captures.findLast((c) => c.threadId === threadId); - capture?.historyResponses.push(saved); - json(response, saved); - }; - if (historyMode === 'hold') hold(histories, response, finish); - else finish(); - return true; - } - if (pathname.endsWith('/runs/stream') && request.method === 'POST') { - const human = body.input?.messages?.[0]; - if ( - body.assistant_id !== 'c-theming' || - body.command || - body.input?.messages?.length !== 1 || - human?.type !== 'human' || - typeof human.id !== 'string' || - typeof human.content !== 'string' || - state.messages.some((m) => m.id === human.id) || - streams.size || - children.size || - captures.length >= 16 || - childReceipts.length >= 256 - ) { - json(response, {}, 400); - return true; - } - const runId = randomUUID(), - run = { thread_id: threadId, status: 'running' }; - runs.set(runId, run); - let wire; - try { - wire = await graphWire({ - threadId, - answerId: 'authored-answer-' + runId, - messages: [...state.messages, human], - title: state.title, - }); - } catch { - json(response, { error: 'Compiled graph seam failed' }, 500); - return true; - } - if (closed || response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - const capture = { - threadId, - runId, - proof: wire.proof, - titleUpdates: wire.titleUpdates, - events: wire.events, - savedMessages: wire.messages, - checkpoint: wire.checkpoint, - parentCheckpoint: wire.parentCheckpoint, - createdAt: wire.createdAt, - historyResponses: [], - sse: '', - sseSha256: '', - }; - captures.push(capture); - const write = (kind, value) => { - const bytes = event(kind, value); - capture.sse += bytes; - capture.sseSha256 = createHash('sha256') - .update(capture.sse) - .digest('hex'); - response.write(bytes); - }; - write('metadata', { run_id: runId }); - let sent = 0; - const finish = () => { - if (response.destroyed || closed) return; - while (sent < wire.events.length) write(...wire.events[sent++]); - state.messages = wire.messages; - state.next = wire.next; - state.title = wire.title; - state.checkpoint = wire.checkpoint; - state.parentCheckpoint = wire.parentCheckpoint; - state.createdAt = wire.createdAt; - run.status = 'success'; - response.end(); - }; - if (human.content === 'Hold') { - let content = ''; - while (sent < wire.events.length) { - const [kind, value] = wire.events[sent++]; - write(kind, value); - if (kind === 'messages') { - content += value[0].content; - if (content.includes('answer')) break; - } - } - hold(streams, response, finish, () => { - run.status = 'interrupted'; - }).runId = runId; - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if ( - cancelId && - request.method === 'POST' && - runs.get(cancelId)?.thread_id === threadId - ) { - if (runs.get(cancelId).status === 'running') - runs.get(cancelId).status = 'interrupted'; - for (const h of [...streams]) { - if (h.runId !== cancelId) continue; - clearTimeout(h.timer); - streams.delete(h); - h.response.end(); - } - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1], - run = runs.get(runId); - if (request.method === 'GET' && run?.thread_id === threadId) { - json(response, { run_id: runId, ...run }); - return true; - } - json(response, {}, 404); - return true; - } - async function handle(request, response, pathname) { - // Controls are not owned business handlers; lifetime observes only real work. - if (pathname.startsWith('/__')) - return dispatch(request, response, pathname); - if (!/^\/(?:api|developer-api)\//.test(pathname)) return false; - if (handlers.size >= 16) { - json(response, {}, 429); - return true; - } - let resolveClose; - const responseClosed = new Promise((r) => { - resolveClose = r; - }); - responses.set(response, responseClosed); - response.once('close', () => { - responses.delete(response); - resolveClose(); - }); - let resolve; - const settled = new Promise((r) => { - resolve = r; - }); - handlers.add(settled); - try { - return await dispatch(request, response, pathname); - } catch { - json(response, { error: 'Local fixture failed' }, 500); - return true; - } finally { - handlers.delete(settled); - resolve(); - } - } - handle.receipt = receipt; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const h of [...streams, ...histories]) { - clearTimeout(h.timer); - h.response.destroy(); - } - streams.clear(); - histories.clear(); - for (const response of responses.keys()) response.destroy(); - for (const owner of children) { - owner.fail(Error('Fixture closed')); - terminate(owner, 'SIGTERM'); - owner.killTimer ??= setTimeout(() => terminate(owner, 'SIGKILL'), 250); - } - await Promise.race([ - Promise.all( - [...children] - .map((c) => c.ended) - .concat([...handlers], [...responses.values()]) - ), - delay(2000).then(() => { - if (children.size || handlers.size || responses.size) - throw Error('Fixture close deadline exceeded'); - }), - ]); - if ( - children.size || - handlers.size || - streams.size || - histories.size || - responses.size - ) - throw Error('Fixture retained owned work'); - })()); - return handle; -} diff --git a/scripts/react-cockpit/chat-theming-fixture.spec.mjs b/scripts/react-cockpit/chat-theming-fixture.spec.mjs deleted file mode 100644 index 6c776309d..000000000 --- a/scripts/react-cockpit/chat-theming-fixture.spec.mjs +++ /dev/null @@ -1,654 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer, request as httpRequest } from 'node:http'; -import { once } from 'node:events'; -import { randomUUID, createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { spawn } from 'node:child_process'; -import test from 'node:test'; -import { Client } from '@langchain/langgraph-sdk'; -const { createChatThemingFixture } = await import( - './chat-theming-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createChatThemingFixture, - 'function', - 'Local fixture must exist' - ); - const handle = createChatThemingFixture(options); - const server = createServer(async (req, res) => { - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) - res.writeHead(404).end(); - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - const base = `http://127.0.0.1:${server.address().port}`; - t.after(async () => { - try { - await handle.close(); - } finally { - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - } - assert.deepEqual(handle.receipt().active, { - children: 0, - streams: 0, - histories: 0, - handlers: 0, - }); - assert.equal(handle.receipt().responseOpen, 0); - for (const child of handle.receipt().children) { - assert.equal(child.reaped, true); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - assert.throws(() => process.kill(-child.pgid, 0), { code: 'ESRCH' }); - } - await assert.rejects(fetch(base, { signal: AbortSignal.timeout(500) })); - console.log( - 'THEMING_FIXTURE_CLOSURE', - JSON.stringify({ - receipt: handle.receipt(), - observedPidAndPgidAbsence: 'ESRCH', - listenerClosed: true, - }) - ); - }); - const client = new Client({ - apiUrl: base + '/api', - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const control = async (path) => { - const response = await fetch(base + path, { - method: 'POST', - signal: AbortSignal.timeout(3000), - }); - assert.equal(response.status, 200); - }; - const get = async (path) => - await ( - await fetch(base + path, { signal: AbortSignal.timeout(3000) }) - ).json(); - const thread = randomUUID(); - await client.threads.create({ threadId: thread, ifExists: 'raise' }); - const stream = (content) => - client.runs.stream(thread, 'c-theming', { - input: { messages: [{ type: 'human', id: randomUUID(), content }] }, - streamMode: ['messages-tuple', 'values'], - }); - return { handle, base, client, control, get, thread, stream }; -} - -test('SDK observes actual compiled graph, chosen thread, distinct runs and confirmed saved history', async (t) => { - const f = await fixture(t); - const runs = []; - for (const text of ['Explain lift', 'Continue aviation']) { - const events = []; - for await (const event of f.stream(text)) events.push(event); - runs.push(events[0].data.run_id); - assert(events.some((e) => e.event === 'messages' && e.data[0].content)); - const history = await f.client.threads.getHistory(f.thread, { limit: 1 }); - assert.equal(history[0].checkpoint.thread_id, f.thread); - assert.equal(history[0].checkpoint.checkpoint_ns, ''); - assert.deepEqual(history[0].next, []); - assert.equal(history[0].values.messages.at(-2).content, text); - } - assert.notEqual(runs[0], runs[1]); - const captures = await f.get('/__captures'); - assert.equal(captures.length, 2); - for (const capture of captures) { - assert.equal(capture.proof.actualCompiledGraph, true); - assert.equal(capture.proof.networkConnectAttempts, 0); - assert.equal(capture.proof.titleMessageCallbacks, 0); - assert.equal( - capture.proof.sourceSha256, - createHash('sha256') - .update(readFileSync('cockpit/chat/theming/python/src/graph.py')) - .digest('hex') - ); - assert.equal( - capture.proof.lockSha256, - createHash('sha256') - .update(readFileSync('cockpit/chat/theming/python/uv.lock')) - .digest('hex') - ); - assert.equal(capture.threadId, f.thread); - assert.equal( - capture.sseSha256, - createHash('sha256').update(capture.sse).digest('hex') - ); - assert.doesNotMatch(capture.sse, /generate_title|Authored metadata title/); - } - assert.equal(captures[0].titleUpdates.length, 1); - assert.equal(captures[1].titleUpdates.length, 0); - const requests = await f.get('/__requests'); - assert.equal(requests.filter((r) => r.path.endsWith('/threads')).length, 1); - assert.equal( - requests.filter((r) => r.path.endsWith('/runs/stream')).length, - 2 - ); -}); - -test('held graph stream and saved history release independently and close drains owned work', async (t) => { - const f = await fixture(t); - const pending = (async () => { - for await (const _ of f.stream('Hold')) { - /* consume actual chunks */ - } - })(); - for (let i = 0; i < 200 && !(await f.get('/__lifetime')).active.streams; i++) - await new Promise((r) => setTimeout(r, 20)); - assert.equal((await f.get('/__lifetime')).active.streams, 1); - await f.control('/__release'); - await pending; - await f.control('/__history/hold'); - const history = f.client.threads.getHistory(f.thread, { limit: 1 }); - for ( - let i = 0; - i < 100 && !(await f.get('/__lifetime')).active.histories; - i++ - ) - await new Promise((r) => setTimeout(r, 10)); - assert.equal((await f.get('/__lifetime')).active.histories, 1); - await f.control('/__release-history'); - assert.equal((await history)[0].values.messages.length, 2); - await f.handle.close(); - assert.equal(f.handle.receipt().closed, true); - await f.handle.close(); -}); - -test('fixture rejects unsupported assistant and malformed or oversized inputs without a child', async (t) => { - const f = await fixture(t); - for (const body of [ - '{', - JSON.stringify({ assistant_id: 'other', input: { messages: [] } }), - 'x'.repeat(65537), - ]) { - const r = await fetch(`${f.base}/api/threads/${f.thread}/runs/stream`, { - method: 'POST', - body, - signal: AbortSignal.timeout(3000), - }); - assert([400, 413].includes(r.status)); - } - assert.equal(f.handle.receipt().children.length, 0); -}); - -for (const phase of ['stream', 'history']) - test(`close observes the original held ${phase} response close`, async (t) => { - const f = await fixture(t); - if (phase === 'history') await f.control('/__history/hold'); - const pending = - phase === 'stream' - ? (async () => { - try { - for await (const _ of f.stream('Hold')) { - /* consume */ - } - } catch { - /* expected owned close */ - } - })() - : (async () => { - try { - for await (const _ of f.stream('Explain lift')) { - /* consume */ - } - await f.client.threads.getHistory(f.thread); - } catch { - /* expected owned close */ - } - })(); - const field = phase === 'stream' ? 'streams' : 'histories'; - for (let i = 0; i < 200 && !(await f.get('/__lifetime')).active[field]; i++) - await new Promise((r) => setTimeout(r, 20)); - assert.equal((await f.get('/__lifetime')).active[field], 1); - await f.handle.close(); - await pending; - assert.equal(f.handle.receipt().responseOpen, 0); - }); - -for (const [name, options] of [ - ['deadline', { wireTimeoutMs: 1 }], - ['output', { wireOutputBytes: 64 }], -]) - test(`real compiled child ${name} bound rejects and reaps its registered group`, async (t) => { - const f = await fixture(t, options); - await assert.rejects(async () => { - for await (const _ of f.stream('Explain lift')) { - /* consume */ - } - }); - const receipt = f.handle.receipt(); - assert.equal(receipt.children.length, 1); - assert.equal(receipt.children[0].reaped, true); - assert.equal(receipt.active.children, 0); - }); - -test( - 'owned proof server shutdown drains an observed child and closes its actual listener', - { timeout: 15000 }, - async (t) => { - const within = async (promise, ms, label) => { - let timer; - try { - return await Promise.race([ - promise, - new Promise((_, reject) => { - timer = setTimeout(() => reject(Error(label)), ms); - }), - ]); - } finally { - clearTimeout(timer); - } - }; - await assert.rejects( - fetch('http://127.0.0.1:4644', { signal: AbortSignal.timeout(300) }) - ); - const child = spawn( - process.execPath, - ['scripts/react-cockpit/serve.mjs', 'chat-theming', '--no-parent'], - { stdio: ['ignore', 'pipe', 'pipe'] } - ); - const abort = new AbortController(); - const workDeadline = setTimeout(() => abort.abort(), 8000); - const ended = once(child, 'close'); - void ended.catch(() => { - /* awaited by bounded main and final cleanup barriers */ - }); - let pending = Promise.resolve(), - output = '', - diagnostic = '', - outputBytes = 0, - diagnosticBytes = 0, - overflow = false; - const signals = []; - const signalOwned = (signal) => { - if (child.exitCode !== null || child.signalCode !== null) return; - try { - signals.push({ - signal, - pid: child.pid, - result: child.kill(signal) ? 'sent' : 'not-sent', - }); - } catch (error) { - signals.push({ signal, pid: child.pid, result: error.code }); - } - }; - // Register final cleanup before any awaited readiness or protocol acquisition. - t.after(async () => { - clearTimeout(workDeadline); - abort.abort(); - signalOwned('SIGKILL'); - try { - await within( - Promise.all([ended, pending]), - 2000, - 'Owned server final cleanup/reap deadline exceeded' - ); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - } finally { - child.stdout.destroy(); - child.stderr.destroy(); - child.unref(); - } - }); - child.stdout.on('data', (bytes) => { - output += bytes.subarray(0, Math.max(0, 65536 - outputBytes)).toString(); - outputBytes += bytes.length; - if (outputBytes > 65536) { - overflow = true; - abort.abort(); - signalOwned('SIGKILL'); - } - }); - child.stderr.on('data', (bytes) => { - diagnostic += bytes - .subarray(0, Math.max(0, 16384 - diagnosticBytes)) - .toString(); - diagnosticBytes += bytes.length; - if (diagnosticBytes > 16384) { - overflow = true; - abort.abort(); - signalOwned('SIGKILL'); - } - }); - const base = 'http://127.0.0.1:4644'; - let ready = false; - const readinessDeadline = Date.now() + 3000; - while (Date.now() < readinessDeadline) { - try { - if ( - ( - await fetch(base + '/__lifetime', { - signal: AbortSignal.timeout(300), - }) - ).ok - ) { - ready = true; - break; - } - } catch { - /* owned start */ - } - if (child.exitCode !== null || child.signalCode !== null || overflow) - break; - await new Promise((r) => setTimeout(r, 20)); - } - assert(ready && !overflow, diagnostic); - const client = new Client({ - apiUrl: base + '/api', - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const id = randomUUID(); - await within( - client.threads.create({ - threadId: id, - ifExists: 'raise', - signal: abort.signal, - }), - 1500, - 'Owned SDK thread creation deadline exceeded' - ); - pending = (async () => { - try { - for await (const _ of client.runs.stream(id, 'c-theming', { - input: { - messages: [ - { type: 'human', id: randomUUID(), content: 'Explain lift' }, - ], - }, - signal: abort.signal, - })) { - /* observed work */ - } - } catch { - /* owned shutdown or request abort */ - } - })(); - let observed; - const observationDeadline = Date.now() + 2000; - while (Date.now() < observationDeadline) { - observed = await ( - await fetch(base + '/__lifetime', { signal: AbortSignal.timeout(300) }) - ).json(); - if (observed.active.children === 1) break; - await new Promise((r) => setTimeout(r, 10)); - } - assert.equal(observed?.active.children, 1); - signalOwned('SIGTERM'); - const result = await within( - ended, - 4000, - 'Owned server failed bounded shutdown' - ); - abort.abort(); - await within( - pending, - 1500, - 'Owned SDK stream did not settle after server close' - ); - assert.equal(overflow, false, 'Owned server output bounds'); - assert.equal(result[0], 0, diagnostic); - const final = JSON.parse(output.trim().split('\n').at(-1)); - assert.equal(final.listenersClosed, true); - assert.deepEqual(final.chatThemingFixtureClosed.active, { - children: 0, - streams: 0, - histories: 0, - handlers: 0, - }); - assert.equal(final.chatThemingFixtureClosed.responseOpen, 0); - for (const record of final.chatThemingFixtureClosed.children) { - assert.equal(record.reaped, true); - assert.throws(() => process.kill(record.pid, 0), { code: 'ESRCH' }); - assert.throws(() => process.kill(-record.pgid, 0), { code: 'ESRCH' }); - } - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - await assert.rejects(fetch(base, { signal: AbortSignal.timeout(500) })); - console.log( - 'THEMING_SERVER_CLOSURE', - JSON.stringify({ - observed, - final, - serverPid: child.pid, - serverExit: result, - serverPidAbsence: 'ESRCH', - pythonChildPidAndPgidAbsence: 'ESRCH', - serverGroupAbsence: 'not tested: spawn is not detached', - listenerClosed: true, - signals, - outputBytes, - diagnosticBytes, - pendingSettled: true, - cleanupBoundMs: 2000, - }) - ); - } -); - -test('cancel for a completed run cannot close the current held run or rewrite terminal success', async (t) => { - const f = await fixture(t); - let completed; - for await (const event of f.stream('Explain lift')) - if (event.event === 'metadata') completed = event.data.run_id; - const pending = (async () => { - for await (const _ of f.stream('Hold')) { - /* consume */ - } - })(); - for (let i = 0; i < 200 && !(await f.get('/__lifetime')).active.streams; i++) - await new Promise((r) => setTimeout(r, 20)); - await f.client.runs.cancel(f.thread, completed); - assert.equal((await f.get('/__lifetime')).active.streams, 1); - assert.equal( - (await f.client.runs.get(f.thread, completed)).status, - 'success' - ); - await f.control('/__release'); - await pending; -}); - -/** A real HTTP request split at specified original byte offsets; no interception. */ -async function fragmentedPost(url, bytes, split) { - return new Promise((resolve, reject) => { - let tail, - deadline, - settled = false; - const finish = (error, value) => { - if (settled) return; - settled = true; - clearTimeout(tail); - clearTimeout(deadline); - if (error) reject(error); - else resolve(value); - }; - const request = httpRequest( - url, - { - method: 'POST', - headers: { - 'content-type': 'application/json', - 'content-length': bytes.length, - }, - signal: AbortSignal.timeout(12000), - }, - (response) => { - const chunks = []; - let count = 0; - response.on('data', (chunk) => { - count += chunk.length; - if (count > 2 * 1024 * 1024) { - request.destroy(Error('Fragmented test response bound')); - return; - } - chunks.push(chunk); - }); - response.once('error', (error) => finish(error)); - response.once('aborted', () => - finish(Error('Fragmented test response aborted')) - ); - response.once('end', () => { - try { - const body = new TextDecoder('utf-8', { fatal: true }).decode( - Buffer.concat(chunks, count) - ); - finish(null, { status: response.statusCode, body }); - } catch (error) { - finish(error); - } - }); - } - ); - request.once('error', (error) => finish(error)); - deadline = setTimeout(() => { - const error = Error('Fragmented request deadline'); - request.destroy(error); - finish(error); - }, 12000); - request.write(bytes.subarray(0, split)); - tail = setTimeout(() => request.end(bytes.subarray(split)), 40); - }); -} - -for (const text of ['Explain lift ASCII', 'Explain lift ✈ café']) - test( - `fragmented original UTF-8 input is preserved through actual graph/history: ${text}`, - { timeout: 15000 }, - async (t) => { - const f = await fixture(t); - const id = randomUUID(); - const bytes = Buffer.from( - JSON.stringify({ - assistant_id: 'c-theming', - input: { messages: [{ type: 'human', id, content: text }] }, - stream_mode: ['messages-tuple', 'values'], - }) - ); - const split = text.includes('✈') - ? bytes.indexOf(Buffer.from('✈')) + 1 - : bytes.indexOf(Buffer.from('ASCII')) + 2; - const response = await fragmentedPost( - `${f.base}/api/threads/${f.thread}/runs/stream`, - bytes, - split - ); - assert.equal(response.status, 200); - const recorded = (await f.get('/__requests')).find((r) => - r.path.endsWith('/runs/stream') - ); - const captures = await f.get('/__captures'); - const saved = await f.client.threads.getHistory(f.thread, { limit: 1 }); - console.log( - 'THEMING_FRAGMENTED_OBSERVED', - JSON.stringify({ - original: text, - recorded: recorded.body.input.messages[0].content, - saved: saved[0].values.messages.find((m) => m.id === id).content, - proof: captures[0].proof, - requestBodyBase64: bytes.toString('base64'), - chunksBase64: [ - bytes.subarray(0, split).toString('base64'), - bytes.subarray(split).toString('base64'), - ], - }) - ); - assert.equal(recorded.body.input.messages[0].content, text); - assert.equal( - captures[0].savedMessages.find((m) => m.id === id).content, - text - ); - assert.equal( - saved[0].values.messages.find((m) => m.id === id).content, - text - ); - assert.equal(captures[0].proof.actualCompiledGraph, true); - assert.equal(captures[0].proof.networkConnectAttempts, 0); - assert.equal(captures[0].sse, response.body); - console.log( - 'THEMING_FRAGMENTED_UTF8', - JSON.stringify({ - text, - requestBodyBase64: bytes.toString('base64'), - requestSha256: createHash('sha256').update(bytes).digest('hex'), - split, - chunksBase64: [ - bytes.subarray(0, split).toString('base64'), - bytes.subarray(split).toString('base64'), - ], - status: response.status, - originalSseBase64: Buffer.from(response.body).toString('base64'), - originalSseSha256: createHash('sha256') - .update(response.body) - .digest('hex'), - recorded: recorded.body, - saved: saved[0], - proof: captures[0].proof, - }) - ); - } - ); - -test( - 'invalid UTF-8 and fragmented byte overflow are rejected before creating any graph child', - { timeout: 15000 }, - async (t) => { - const f = await fixture(t); - const prefix = Buffer.from( - JSON.stringify({ - assistant_id: 'c-theming', - input: { - messages: [ - { type: 'human', id: randomUUID(), content: 'PLACEHOLDER' }, - ], - }, - }) - ); - const offset = prefix.indexOf(Buffer.from('PLACEHOLDER')); - const malformedResults = []; - for (const invalidBytes of [ - [0xc3, 0x28], - [0xe2, 0x9c], - [0xc0, 0xaf], - ]) { - const malformed = Buffer.concat([ - prefix.subarray(0, offset), - Buffer.from(invalidBytes), - prefix.subarray(offset + 'PLACEHOLDER'.length), - ]); - const invalid = await fragmentedPost( - `${f.base}/api/threads/${f.thread}/runs/stream`, - malformed, - offset + 1 - ); - assert.equal(invalid.status, 400); - assert.equal(f.handle.receipt().children.length, 0); - malformedResults.push({ - malformedBase64: malformed.toString('base64'), - invalidBytes, - status: invalid.status, - }); - } - const oversized = Buffer.from(JSON.stringify({ text: '✈'.repeat(22000) })); - assert(oversized.length > 65536); - const overflow = await fragmentedPost( - `${f.base}/api/threads/${f.thread}/runs/stream`, - oversized, - 32001 - ); - assert.equal(overflow.status, 413); - assert.equal(f.handle.receipt().children.length, 0); - console.log( - 'THEMING_UTF8_REJECTIONS', - JSON.stringify({ - malformedResults, - oversizedBytes: oversized.length, - overflowStatus: overflow.status, - children: 0, - }) - ); - } -); diff --git a/scripts/react-cockpit/chat-theming-wire.py b/scripts/react-cockpit/chat-theming-wire.py deleted file mode 100644 index cfbf3f656..000000000 --- a/scripts/react-cockpit/chat-theming-wire.py +++ /dev/null @@ -1,82 +0,0 @@ -"""Actual canonical compiled graph; deterministic local provider seam, never public.""" -import asyncio -import hashlib -import importlib.util -import json -from pathlib import Path -import socket -import sys -from unittest.mock import patch - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/theming/python" -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, AuthoredModel -from langchain_core.messages import HumanMessage, AIMessage, AIMessageChunk -from langchain_core.outputs import ChatGenerationChunk -from langgraph.checkpoint.memory import InMemorySaver - - -class ReasoningModel(AuthoredModel): - """Explicit local display-data seam; callbacks still originate in BaseChatModel.""" - def _stream(self, messages, stop=None, run_manager=None, **kwargs): - if not self.title: - yield ChatGenerationChunk(message=AIMessageChunk( - content="", id=self.answer_id, - additional_kwargs={"reasoning_content": "Considering the aviation question."}, - )) - yield from super()._stream(messages, stop, run_manager, **kwargs) - - -async def main(): - raw = sys.stdin.buffer.read(65537) - if len(raw) > 65536: - raise ValueError("Wire input exceeds local bound") - request = json.loads(raw) - client = AuthoredClient(title=request.get("title")) - source = PROJECT / "src/graph.py" - def factory(**kwargs): - return ReasoningModel(title=not kwargs.get("streaming", False), - answer_id=request["answerId"], tags=kwargs.get("tags", [])) - with patch.object(socket.socket, "connect", side_effect=AssertionError( - "Compiled fixture cannot connect to any service" - )) as connect, patch("langchain_openai.ChatOpenAI", factory), patch( - "langgraph_sdk.get_client", return_value=client, - ): - spec = importlib.util.spec_from_file_location("authored_theming_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_theming_graph().builder.compile(checkpointer=InMemorySaver()) - config = {"configurable": {"thread_id": request["threadId"]}} - messages = [ - (HumanMessage if m["type"] == "human" else AIMessage)( - content=m["content"], id=m["id"], additional_kwargs=m.get("additional_kwargs", {})) - for m in request["messages"] - ] - events = [] - async for kind, value in graph.astream({"messages": messages}, config, stream_mode=["messages", "values"]): - if kind == "messages": - message, metadata = value - if metadata.get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked") - events.append([kind, [message.model_dump(mode="json"), metadata]]) - else: - events.append([kind, {"messages": [m.model_dump(mode="json") for m in value["messages"]]}]) - state = graph.get_state(config) - connect.assert_not_called() - print(json.dumps({ - "events": events, "messages": [m.model_dump(mode="json") for m in state.values["messages"]], - "next": list(state.next), "title": client.threads.title, - "checkpoint": state.config["configurable"], - "parentCheckpoint": state.parent_config["configurable"] if state.parent_config else None, - "createdAt": state.created_at, - "titleUpdates": client.threads.updates, - "proof": {"actualCompiledGraph": True, "localProviderSeam": True, - "reasoningProviderExtension": True, - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0}, - })) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 318be97f8..5ff3ea7a2 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -1,4 +1,16 @@ /** Closed authored frontend selection; callers cannot supply filesystem paths. */ + +/** + * aimock-backed example: the real graph runs under `langgraph dev` against the + * Angular example's aimock fixtures (see docs/superpowers/specs/ + * 2026-11-18-react-cockpit-aimock-design.md). Convention: langgraph = port + 1000. + */ +const aimockLanggraph = (example, port) => Object.freeze({ + kind: 'aimock-langgraph', + python: `cockpit/${example}/python`, + fixtures: `cockpit/${example}/angular/e2e/fixtures`, + langgraphPort: port + 1000, +}); export function reactCockpitConfiguration(topic = 'streaming') { if (topic === 'ag-ui-a2ui') return Object.freeze({ @@ -23,6 +35,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { topic: 'theming', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/theming/react', base: '/chat/theming/react/', port: 4644, project: 'cockpit-chat-theming-react', + backend: aimockLanggraph('chat/theming', 4644), }); if (topic === 'runtimes-aws-strands') return Object.freeze({ @@ -100,6 +113,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { return Object.freeze({ topic: 'input', adapter: 'langgraph', appPath: 'cockpit/chat/input/react', base: '/chat/input/react/', port: 4621, project: 'cockpit-chat-input-react', + backend: aimockLanggraph('chat/input', 4621), }); if (topic === 'chat-messages') return Object.freeze({ @@ -109,13 +123,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/chat/messages/react/', port: 4620, project: 'cockpit-chat-messages-react', - // Same aimock fixtures and `langgraph dev` path as the Angular example. - backend: Object.freeze({ - kind: 'aimock-langgraph', - python: 'cockpit/chat/messages/python', - fixtures: 'cockpit/chat/messages/angular/e2e/fixtures', - langgraphPort: 5620, - }), + backend: aimockLanggraph('chat/messages', 4620), }); if (topic === 'render-computed-functions') return Object.freeze({ diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 220175c81..a9787c28a 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -8,13 +8,22 @@ * cannot produce on demand: * * GET /__requests every /api and /developer-api request, in order - * GET /__lifetime { activeStream, activeHistory } + * GET /__lifetime { activeStream, activeHistory, openResponses } + * GET /__captures per finished run: forwarded SSE text, saved + * messages/next from the real server, title + * before and after the run * POST /__reset forget requests, drop held responses, clear modes * POST /__fail-create next thread creation answers 500 without forwarding * POST /__history/ safe | hold | changed | missing | pending | failed * POST /__release-history finish a held history read * POST /__hold/stream hold the next run stream; body { afterText? } * POST /__release flush and resume a held run stream + * POST /__inject/reasoning body { text }: until reset, every AI message the + * browser receives (stream, history, state) carries + * additional_kwargs.reasoning_content = text. + * Injected data: ChatOpenAI over Chat Completions + * never surfaces reasoning, so a real graph can't + * produce it for the reasoning-styling checks. * * Nothing here is reachable from a production build: only * scripts/react-cockpit/serve.mjs imports it. @@ -73,12 +82,54 @@ export function createLanggraphProxy({ upstream }) { let holdNextStream = null; let heldStream; let heldHistory; + let captures = []; + let openResponses = 0; + let reasoning = null; + + const withReasoning = (message) => + reasoning && message?.type === 'ai' + ? { ...message, additional_kwargs: { ...message.additional_kwargs, reasoning_content: reasoning } } + : message; + const decorateValues = (values) => + reasoning && Array.isArray(values?.messages) + ? { ...values, messages: values.messages.map(withReasoning) } + : values; + // `updates` and `checkpoints` events carry saved messages nested under node + // names or checkpoint values; decorate every `messages` array they hold. + const decorateDeep = (value) => { + if (Array.isArray(value)) return value.map(decorateDeep); + if (!value || typeof value !== 'object') return value; + const out = {}; + for (const [key, entry] of Object.entries(value)) + out[key] = key === 'messages' && Array.isArray(entry) ? entry.map(withReasoning) : decorateDeep(entry); + return out; + }; + /** Rewrite one SSE event for an armed injection; first AI chunk per message only. */ + const decoratedChunks = new Set(); + function decorateEvent(raw) { + if (!reasoning) return raw; + const name = /^event: *(.*)$/m.exec(raw)?.[1]?.trim() ?? ''; + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + if (!dataLine) return raw; + let value; + try { value = JSON.parse(dataLine.slice(5)); } catch { return raw; } + if (name.startsWith('messages') && Array.isArray(value) && String(value[0]?.type ?? '').startsWith('AI')) { + if (decoratedChunks.has(value[0].id)) return raw; + decoratedChunks.add(value[0].id); + value = [{ ...value[0], additional_kwargs: { ...value[0].additional_kwargs, reasoning_content: reasoning } }, ...value.slice(1)]; + } else if (/^(values|updates|checkpoints)\b/.test(name)) value = decorateDeep(value); + else return raw; + return raw.replace(dataLine, 'data: ' + JSON.stringify(value)); + } function reset() { heldStream?.response.destroy(); heldHistory?.response.destroy(); heldStream = heldHistory = undefined; requests = []; + captures = []; + reasoning = null; + decoratedChunks.clear(); failCreate = false; historyMode = 'safe'; holdNextStream = null; @@ -107,7 +158,10 @@ export function createLanggraphProxy({ upstream }) { return Buffer.concat(chunks); } - function transformHistory(states) { + function transformHistory(input) { + const states = Array.isArray(input) + ? input.map((state) => ({ ...state, values: decorateValues(state.values) })) + : input; if (!Array.isArray(states) || states.length === 0) return states; const [latest, ...rest] = states; let messages = latest.values?.messages ?? []; @@ -150,35 +204,72 @@ export function createLanggraphProxy({ upstream }) { }); } + async function upstreamJson(path) { + const answer = await send('GET', path, { accept: 'application/json' }, Buffer.alloc(0)); + const raw = await readAll(answer); + return answer.statusCode === 200 ? JSON.parse(raw.toString('utf8')) : null; + } + + async function threadTitle(threadId) { + return (await upstreamJson(`/threads/${threadId}`))?.metadata?.title ?? null; + } + async function proxyStream(request, response, path, body) { const hold = holdNextStream; holdNextStream = null; + const threadId = /\/threads\/([^/]+)\/runs\/stream/.exec(path)?.[1]; + const titleBefore = threadId ? await threadTitle(threadId) : null; const upstreamResponse = await send(request.method, path, request.headers, body); response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); response.flushHeaders(); - const abort = () => upstreamResponse.destroy(); - response.on('close', abort); - if (!hold) { - upstreamResponse.pipe(response); - return; - } - // Forward whole SSE events until the hold condition is met, then buffer. + response.on('close', () => upstreamResponse.destroy()); + // Forward whole SSE events; when a hold is armed, buffer after its condition. let pending = ''; let text = ''; + let sse = ''; let holding = false; - let armed = true; + let armed = Boolean(hold); + let ended = false; const buffered = []; + const finish = async () => { + if (upstreamResponse.statusCode === 200 && threadId) { + // Saved state and title as the real server persisted them, captured + // before the browser sees the end of the stream so tests never race it. + const state = await upstreamJson(`/threads/${threadId}/state`).catch(() => null); + captures.push({ + path: request.url && new URL(request.url, 'http://localhost').pathname, + sse, + savedMessages: state?.values?.messages ?? [], + next: state?.next ?? [], + titleBefore, + titleAfter: await threadTitle(threadId).catch(() => null), + }); + } + if (!response.destroyed) response.end(); + }; const held = { response, release() { if (heldStream === held) heldStream = undefined; holding = false; - for (const chunk of buffered.splice(0)) response.write(chunk); - if (upstreamResponse.readableEnded) response.end(); + let rest = buffered.splice(0).join(''); + let match; + while ((match = /\r?\n\r?\n/.exec(rest))) { + const end = match.index + match[0].length; + response.write(decorateEvent(rest.slice(0, end))); + rest = rest.slice(end); + } + pending = rest + pending; + if (ended) { + if (pending) response.write(decorateEvent(pending)); + pending = ''; + void finish(); + } }, }; upstreamResponse.setEncoding('utf8'); upstreamResponse.on('data', (chunk) => { + sse += chunk; if (holding) return void buffered.push(chunk); pending += chunk; let match; @@ -186,7 +277,7 @@ export function createLanggraphProxy({ upstream }) { const end = match.index + match[0].length; const raw = pending.slice(0, end); pending = pending.slice(end); - response.write(raw); + response.write(decorateEvent(raw)); const delta = aiText(raw); if (delta === null) continue; text += delta; @@ -202,9 +293,10 @@ export function createLanggraphProxy({ upstream }) { } }); upstreamResponse.on('end', () => { + ended = true; if (holding) return; - if (pending) response.write(pending); - response.end(); + if (pending) response.write(decorateEvent(pending)); + void finish(); }); response.on('close', () => { if (heldStream === held) heldStream = undefined; @@ -213,7 +305,12 @@ export function createLanggraphProxy({ upstream }) { const controls = { '/__requests': ['GET', () => requests], - '/__lifetime': ['GET', () => ({ activeStream: Boolean(heldStream), activeHistory: Boolean(heldHistory) })], + '/__captures': ['GET', () => captures], + '/__lifetime': ['GET', () => ({ + activeStream: Boolean(heldStream), + activeHistory: Boolean(heldHistory), + openResponses, + })], '/__reset': ['POST', () => { reset(); return {}; }], '/__fail-create': ['POST', () => { failCreate = true; return {}; }], '/__release': ['POST', () => { heldStream?.release(); return {}; }], @@ -223,6 +320,7 @@ export function createLanggraphProxy({ upstream }) { held?.finish(); return {}; }], + '/__inject/reasoning': ['POST', (body) => { reasoning = String(body.text ?? ''); return {}; }], '/__hold/stream': ['POST', (body) => { holdNextStream = { afterText: body.afterText ?? null }; return {}; }], }; @@ -253,6 +351,8 @@ export function createLanggraphProxy({ upstream }) { : null; if (!prefix) return false; + openResponses += 1; + response.once('close', () => { openResponses -= 1; }); const body = await readAll(request); const search = new URL(request.url, 'http://localhost').search; const path = pathname.slice(prefix.length) + search; @@ -274,7 +374,17 @@ export function createLanggraphProxy({ upstream }) { await proxyHistory(request, response, path, body); else if (request.method === 'POST' && /\/runs\/stream$/.test(pathname)) await proxyStream(request, response, path, body); - else { + else if (reasoning && request.method === 'GET' && /\/threads\/[^/]+\/state$/.test(pathname)) { + const upstreamResponse = await send(request.method, path, request.headers, body); + const raw = await readAll(upstreamResponse); + if (upstreamResponse.statusCode !== 200) { + response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + response.end(raw); + } else { + const state = JSON.parse(raw.toString('utf8')); + json(response, { ...state, values: decorateValues(state.values) }); + } + } else { const upstreamResponse = await send(request.method, path, request.headers, body); response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); upstreamResponse.pipe(response); diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index d9f46882a..240dbf4cf 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -19,9 +19,7 @@ import { createSubgraphsFixture } from './subgraphs-fixture.mjs'; import { createTimeTravelFixture } from './time-travel-fixture.mjs'; import { createDeploymentRuntimeFixture } from './deployment-runtime-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; -import { createChatThemingFixture } from './chat-theming-fixture.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; -import { createChatInputFixture } from './chat-input-fixture.mjs'; import { createChatInterruptsFixture } from './chat-interrupts-fixture.mjs'; import { createChatToolCallsFixture } from './chat-tool-calls-fixture.mjs'; import { createChatSubagentsFixture } from './chat-subagents-fixture.mjs'; @@ -66,7 +64,6 @@ const releaseA2uiStreams = (cancel = false) => { } }; const chatDebugFixture = configuration.library === 'chat' && configuration.topic === 'debug' ? createChatDebugFixture() : null; -const chatThemingFixture = configuration.library === 'chat' && configuration.topic === 'theming' ? createChatThemingFixture() : null; const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; @@ -86,7 +83,6 @@ const chatInterruptsFixture = configuration.library === 'chat' && configuration. const langgraphProxy = configuration.backend?.kind === 'aimock-langgraph' ? createLanggraphProxy({ upstream: `http://127.0.0.1:${configuration.backend.langgraphPort}` }) : null; -const chatInputFixture = configuration.topic === 'input' ? createChatInputFixture() : null; const agUiSubagentsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'subagents' ? createAgUiSubagentsFixture() @@ -223,7 +219,6 @@ const server = createServer(async (request, response) => { if (await chatDebugFixture.handle(request, response, pathname)) return; if (pathname.startsWith('/developer-api/')) return json(response, {}, 404); } - if (chatThemingFixture && (await chatThemingFixture(request, response, pathname))) return; if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; @@ -250,7 +245,6 @@ const server = createServer(async (request, response) => { if (chatToolCallsFixture && (await chatToolCallsFixture(request, response, pathname))) return; if (chatInterruptsFixture && (await chatInterruptsFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; - if (chatInputFixture && (await chatInputFixture(request, response, pathname))) return; if (configuration.adapter === 'none') { if (!['GET', 'HEAD'].includes(request.method)) { response.writeHead(405, { allow: 'GET, HEAD' }); @@ -464,7 +458,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || chatThemingFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture || chatInterruptsFixture || chatToolCallsFixture || chatSubagentsFixture || chatThreadsFixture || chatTimelineFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture || chatInterruptsFixture || chatToolCallsFixture || chatSubagentsFixture || chatThreadsFixture || chatTimelineFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -477,18 +471,17 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || chatThemingFixture || n try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? chatThemingFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture ?? chatInterruptsFixture ?? chatToolCallsFixture ?? chatSubagentsFixture ?? chatThreadsFixture ?? chatTimelineFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture ?? chatInterruptsFixture ?? chatToolCallsFixture ?? chatSubagentsFixture ?? chatThreadsFixture ?? chatTimelineFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); }))); - if (chatThemingFixture) console.log(JSON.stringify({ serverPid: process.pid, chatThemingFixtureClosed: chatThemingFixture.receipt(), listenersClosed: true })); if (chatDebugFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatDebugFixtureClosed: chatDebugFixture.receipt(), listenersClosed: true })); if (chatA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatA2uiFixtureClosed: a2uiReceipt(), listenersClosed: true })); if (agUiA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), agUiA2uiFixtureClosed: agUiA2uiFixture.receipt(), bufferedCanonicalWire: true, listenersClosed: true })); if (a2uiFailure) throw a2uiFailure; })(); - if (agUiA2uiFixture || isChatA2ui || chatThemingFixture || chatDebugFixture) void closing.then(() => process.exit(code), () => process.exit(1)); + if (agUiA2uiFixture || isChatA2ui || chatDebugFixture) void closing.then(() => process.exit(code), () => process.exit(1)); else void closing.finally(() => process.exit(code)); }; process.once('SIGINT', () => shutdown()); From c9240d0bad6ee63f09a1f33f6c8c07ae6e5ab16a Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:15:53 -0800 Subject: [PATCH 02/41] test(website): drive the Theming preview on aimock; opt-in reasoning injection - `serve.mjs --backend` starts that example's aimock + `langgraph dev` (scripts/react-cockpit/aimock-backend.mjs) lazily on the first app or API request; Website suites start only the preview servers they own. `/__ready` waits out the lazy start. - Website Theming preview runs on it; its assertions use the proxy's requests, captures (saved messages, title) and lifetime. - /__inject/reasoning is opt-in per test: only the cockpit theming test that styles the Reasoning region calls it. Injected because langchain_openai's ChatOpenAI does not extract `reasoning_content` from Chat Completions. - Design doc: website previews, the injected-reasoning rationale and the Responses API follow-up. Co-Authored-By: Claude Opus 5.5 --- .../website/e2e/react-theming-preview.spec.ts | 87 ++++++------------- apps/website/playwright.config.ts | 2 +- .../chat/theming/react/e2e/theming.spec.ts | 12 ++- .../2026-11-18-react-cockpit-aimock-design.md | 21 ++++- nx.json | 1 + scripts/react-cockpit/aimock-backend.mjs | 76 ++++++++++++++++ scripts/react-cockpit/langgraph-proxy.mjs | 7 +- scripts/react-cockpit/serve.mjs | 21 ++++- 8 files changed, 160 insertions(+), 67 deletions(-) create mode 100644 scripts/react-cockpit/aimock-backend.mjs diff --git a/apps/website/e2e/react-theming-preview.spec.ts b/apps/website/e2e/react-theming-preview.spec.ts index d34317e08..3d51ddeec 100644 --- a/apps/website/e2e/react-theming-preview.spec.ts +++ b/apps/website/e2e/react-theming-preview.spec.ts @@ -2,9 +2,11 @@ import { selectWorkspaceMode } from './fixtures/workspace-controls'; import { selectFramework } from './fixtures/framework-controls'; import { test, expect, type Page } from '@playwright/test'; import { writeFile } from 'node:fs/promises'; -import { createHash } from 'node:crypto'; import { fromPageOrReactPreview } from './fixtures/react-preview-requests'; +// The preview's server (`serve.mjs chat-theming --backend`) runs the real +// graph under `langgraph dev` on aimock fixtures shared with the Angular +// example, and records requests through scripts/react-cockpit/langgraph-proxy.mjs. const origin = 'http://127.0.0.1:4644'; const route = '/docs/angular/chat/guides/theming'; const reactRoute = '/docs/react/chat/guides/theming'; @@ -25,7 +27,7 @@ test.describe('native Theming Website', () => { test.describe.configure({ mode: 'serial' }); test.skip( !!process.env['BASE_URL'], - 'Compiled graph controls and deterministic providers are local only.' + 'The aimock-backed graph and its proxy controls are local only.' ); test.beforeEach(async ({ page, request }) => { const observed = { @@ -46,6 +48,10 @@ test.describe('native Theming Website', () => { }); }); expect((await request.post(origin + '/__reset')).ok()).toBe(true); + // The backend starts lazily; wait for it before measuring UI transitions. + expect( + (await request.post(origin + '/__ready', { timeout: 180000 })).ok() + ).toBe(true); }); test.afterEach(async ({ page, request }, info) => { await writeFile(info.outputPath('page.html'), await page.content()); @@ -53,37 +59,11 @@ test.describe('native Theming Website', () => { info.outputPath('browser-observations.json'), JSON.stringify(observations.get(page), null, 2) ); + // Every proxied browser connection closes and nothing stays held. await page.goto('about:blank'); await expect - .poll( - async () => - ( - await (await request.get(origin + '/__lifetime')).json() - ).active - ) - .toEqual({ children: 0, streams: 0, histories: 0, handlers: 0 }); - await expect - .poll( - async () => - ( - await (await request.get(origin + '/__lifetime')).json() - ).responseOpen - ) - .toBe(0); - const lifetime = await (await request.get(origin + '/__lifetime')).json(); - for (const child of lifetime.children) { - expect(child.reaped).toBe(true); - expect(() => process.kill(child.pid, 0)).toThrow(/ESRCH/); - expect(() => process.kill(-child.pgid, 0)).toThrow(/ESRCH/); - } - await writeFile( - info.outputPath('observed-child-absence.json'), - JSON.stringify( - { children: lifetime.children, observedPidAndPgidAbsence: 'ESRCH' }, - null, - 2 - ) - ); + .poll(async () => (await request.get(origin + '/__lifetime')).json()) + .toMatchObject({ activeStream: false, activeHistory: false, openResponses: 0 }); for (const path of ['/__requests', '/__captures', '/__lifetime']) await writeFile( info.outputPath(path.slice(3) + '.json'), @@ -216,13 +196,17 @@ test.describe('native Theming Website', () => { 'GET', 'POST', ]); - const captures = await (await request.get(origin + '/__captures')).json(); + const captures: { + sse: string; + savedMessages: { type: string; content: string }[]; + next: string[]; + titleBefore: string | null; + titleAfter: string | null; + }[] = await (await request.get(origin + '/__captures')).json(); expect(captures).toHaveLength(1); - const { threadId, runId } = captures[0]; - expect(wire[0].body).toMatchObject({ - thread_id: threadId, - if_exists: 'raise', - }); + const threadId = wire[0].body.thread_id; + const runId = wire[2].path.split('/').at(-1); + expect(wire[0].body).toMatchObject({ if_exists: 'raise' }); expect(wire.map((r: { path: string }) => r.path)).toEqual([ '/api/threads', `/api/threads/${threadId}/runs/stream`, @@ -233,32 +217,15 @@ test.describe('native Theming Website', () => { expect(wire[1].body.input.messages[0].content).toBe( 'Explain lift ✈ café' ); - expect(captures[0].sseSha256).toBe( - createHash('sha256').update(captures[0].sse).digest('hex') - ); - expect(captures[0].historyResponses).toHaveLength(1); - expect(captures[0].historyResponses[0]).toHaveLength(1); - expect(captures[0].historyResponses[0][0]).toMatchObject({ - values: { messages: captures[0].savedMessages }, - next: [], - checkpoint: { thread_id: threadId, checkpoint_ns: '' }, - }); - expect(captures[0].proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - }); - expect(captures[0].titleUpdates).toHaveLength(1); - expect(captures[0].sse).not.toContain('Authored metadata title'); + // The real server saved one finished turn with its title. + expect(captures[0].next).toEqual([]); + expect(captures[0].titleBefore).toBeNull(); + expect(captures[0].titleAfter).toBe('Aviation snippet title'); expect( - captures[0].savedMessages.find( - (m: { type: string }) => m.type === 'human' - ).content + captures[0].savedMessages.find((m) => m.type === 'human')?.content ).toBe('Explain lift ✈ café'); expect( - captures[0].savedMessages.filter( - (m: { type: string }) => m.type === 'ai' - ) + captures[0].savedMessages.filter((m) => m.type === 'ai') ).toHaveLength(1); const before = [...(observations.get(page)?.requests ?? [])]; await frame diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 5f7658215..f1d405f1e 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -434,7 +434,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent', + command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4644', reuseExistingServer: false, diff --git a/cockpit/chat/theming/react/e2e/theming.spec.ts b/cockpit/chat/theming/react/e2e/theming.spec.ts index 138c60216..7a6858c4b 100644 --- a/cockpit/chat/theming/react/e2e/theming.spec.ts +++ b/cockpit/chat/theming/react/e2e/theming.spec.ts @@ -55,14 +55,19 @@ async function submit(page: Page, text: string) { } test.beforeEach(async ({ request }) => { await control(request, '/__reset'); - // Real ChatOpenAI never surfaces reasoning; inject it so palettes reach the - // Reasoning region (scripts/react-cockpit/langgraph-proxy.mjs). +}); +/** + * Injected data, opted into only by the test that styles the Reasoning + * region: langchain_openai's ChatOpenAI does not extract `reasoning_content` + * from Chat Completions responses, so the real graph never surfaces reasoning + * (scripts/react-cockpit/langgraph-proxy.mjs, /__inject/reasoning). + */ +const injectReasoning = async (request: APIRequestContext) => expect( (await request.post('/__inject/reasoning', { data: { text: 'Considering the aviation question.' }, })).ok() ).toBe(true); -}); test.afterEach(async ({ page, request }, info) => { // Every browser connection through the proxy closes and nothing stays held. // (No per-run graph child processes exist any more: the graph is `langgraph dev`.) @@ -88,6 +93,7 @@ for (const width of [1280, 390]) { page, request, }, info) => { + await injectReasoning(request); await page.setViewportSize({ width, height: 900 }); await page.goto('/chat/theming/react/'); await expect(page.getByRole('status')).toHaveText('Ready.'); diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 3e1dcbeba..6244f8f72 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -65,7 +65,7 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | `GET /__graph-proof` | in-process proof | removed; see "Guarantees" | | `GET /__captures` (batch 1) | authored SSE, saved messages, title updates | per finished run: forwarded SSE text, saved `messages`/`next` from the real `/state`, and thread title before and after the run, all captured before the browser sees the stream end | | `GET /__lifetime` child reaping (batch 1) | owned wire child processes | no per-run graph processes exist any more, so `openResponses` (proxied browser connections still open) replaces the child-reaping check | - | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data**. `ChatOpenAI` over Chat Completions never surfaces reasoning, so a real graph can't produce it. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`), so the theming palette checks still reach the Reasoning region | + | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test prompt can be a shared fixture prompt. @@ -82,6 +82,25 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l entry gets `backend: aimockLanggraph('', )`, where the langgraph port is the app port + 1000. +## Website previews + +Website e2e (`apps/website/playwright.config.ts`) reuses the React examples as +live previews and drives several of them through the same controls. Each +batch that deletes an authored fixture server moves the website previews and +specs that used it onto aimock in the same PR, so every PR is green by itself. +There is no second, authored backend. + +- `serve.mjs --backend` starts that example's aimock + `langgraph + dev` itself (`scripts/react-cockpit/aimock-backend.mjs`), lazily on the + first request for the app or its API. Website suites already start only the + preview servers they own, so a suite pays only for the previews it drives. + The backend stays in the server's process group and stops with it. +- Preview specs call `POST /__ready` before driving a preview; it waits out the + lazy start. +- One `langgraph dev` per example, not a shared `langgraph.json`: each example + is its own Python project with its own locked dependencies and graph module, + so a shared server would need one merged environment. + ## Guarantees: kept, moved, or replaced | Old guarantee | Where it lives now | diff --git a/nx.json b/nx.json index cae70dff9..0a19da480 100644 --- a/nx.json +++ b/nx.json @@ -18,6 +18,7 @@ "sharedGlobals": [], "reactAimockBackend": [ "{workspaceRoot}/scripts/react-cockpit/langgraph-proxy.mjs", + "{workspaceRoot}/scripts/react-cockpit/aimock-backend.mjs", "{workspaceRoot}/scripts/react-cockpit/serve.mjs", "{workspaceRoot}/scripts/react-cockpit/configuration.mjs", "{workspaceRoot}/cockpit/shared/react-aimock-setup.ts" diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs new file mode 100644 index 000000000..b94dedf16 --- /dev/null +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -0,0 +1,76 @@ +/** + * Local e2e only: aimock + a real `langgraph dev` for one aimock-backed React + * cockpit example, owned by `serve.mjs --backend`. + * + * Cockpit e2e starts the same backend from its Playwright global setup + * (libs/e2e-harness). Website e2e reuses the React examples as previews and + * has no per-example setup, so the static server starts the backend itself, + * lazily on the first request for the app or its API: a Website suite only + * pays for the previews it actually drives. The child stays in this process's + * group, so Playwright's webServer teardown stops it with the server. + */ +import { spawn } from 'node:child_process'; +import { readFileSync, readdirSync } from 'node:fs'; +import { join, resolve } from 'node:path'; +import { setTimeout as delay } from 'node:timers/promises'; +import { LLMock } from 'aimock'; + +export function createAimockBackend({ root, backend }) { + let starting; + let mock; + let child; + let stderr = ''; + + async function start() { + const dir = resolve(root, backend.fixtures); + const fixtures = readdirSync(dir) + .filter((file) => file.endsWith('.json')) + .sort() + .flatMap((file) => JSON.parse(readFileSync(join(dir, file), 'utf8')).fixtures); + // Same replay defaults as libs/e2e-harness/src/aimock-runner.ts. + mock = new LLMock({ port: 0, chunkSize: 4096 }); + mock.addFixturesFromJSON(fixtures); + await mock.start(); + child = spawn( + 'uv', + ['run', 'langgraph', 'dev', '--port', String(backend.langgraphPort), '--no-browser'], + { + cwd: resolve(root, backend.python), + env: { + ...process.env, + OPENAI_BASE_URL: `${mock.url}/v1`, + OPENAI_API_KEY: 'test-not-used', + LANGSMITH_TRACING: 'false', + }, + stdio: ['ignore', 'ignore', 'pipe'], + }, + ); + child.stderr.on('data', (chunk) => { + stderr = (stderr + chunk).slice(-8192); + }); + const deadline = Date.now() + 180_000; + while (Date.now() < deadline) { + if (child.exitCode !== null) break; + try { + if ((await fetch(`http://127.0.0.1:${backend.langgraphPort}/ok`)).ok) return; + } catch { + // not listening yet + } + await delay(500); + } + process.stderr.write(stderr); + throw new Error(`langgraph dev for ${backend.python} did not become ready`); + } + + return { + /** Start once; every caller awaits the same readiness. */ + ensure() { + starting ??= start(); + return starting; + }, + async close() { + child?.kill('SIGTERM'); + await mock?.stop(); + }, + }; +} diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index a9787c28a..66b29571c 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -12,6 +12,8 @@ * GET /__captures per finished run: forwarded SSE text, saved * messages/next from the real server, title * before and after the run + * POST /__ready 200 once the backend is up (with `serve.mjs + * --backend`, after its lazy start) * POST /__reset forget requests, drop held responses, clear modes * POST /__fail-create next thread creation answers 500 without forwarding * POST /__history/ safe | hold | changed | missing | pending | failed @@ -74,7 +76,7 @@ function aiText(rawEvent) { } } -export function createLanggraphProxy({ upstream }) { +export function createLanggraphProxy({ upstream, ready = async () => {} }) { const target = new URL(upstream); let requests = []; let failCreate = false; @@ -313,6 +315,8 @@ export function createLanggraphProxy({ upstream }) { })], '/__reset': ['POST', () => { reset(); return {}; }], '/__fail-create': ['POST', () => { failCreate = true; return {}; }], + // Waits for the backend (with `serve.mjs --backend`, its lazy start). + '/__ready': ['POST', async () => { await ready(); return {}; }], '/__release': ['POST', () => { heldStream?.release(); return {}; }], '/__release-history': ['POST', () => { const held = heldHistory; @@ -353,6 +357,7 @@ export function createLanggraphProxy({ upstream }) { openResponses += 1; response.once('close', () => { openResponses -= 1; }); + await ready(); const body = await readAll(request); const search = new URL(request.url, 'http://localhost').search; const path = pathname.slice(prefix.length) + search; diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 240dbf4cf..dbaf8b6ff 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -19,6 +19,7 @@ import { createSubgraphsFixture } from './subgraphs-fixture.mjs'; import { createTimeTravelFixture } from './time-travel-fixture.mjs'; import { createDeploymentRuntimeFixture } from './deployment-runtime-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; +import { createAimockBackend } from './aimock-backend.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; import { createChatInterruptsFixture } from './chat-interrupts-fixture.mjs'; import { createChatToolCallsFixture } from './chat-tool-calls-fixture.mjs'; @@ -80,8 +81,19 @@ const chatToolCallsFixture = configuration.library === 'chat' && configuration.t const chatInterruptsFixture = configuration.library === 'chat' && configuration.topic === 'interrupts' ? createChatInterruptsFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by // the Playwright global setup); this server proxies /api to it. +// `--backend` (Website previews) starts that backend here, lazily on the first +// request for the app or its API; cockpit e2e starts it from global setup. +const aimockBackend = configuration.backend?.kind === 'aimock-langgraph' && process.argv.includes('--backend') + ? createAimockBackend({ + root: resolve(dirname(fileURLToPath(import.meta.url)), '../..'), + backend: configuration.backend, + }) + : null; const langgraphProxy = configuration.backend?.kind === 'aimock-langgraph' - ? createLanggraphProxy({ upstream: `http://127.0.0.1:${configuration.backend.langgraphPort}` }) + ? createLanggraphProxy({ + upstream: `http://127.0.0.1:${configuration.backend.langgraphPort}`, + ready: aimockBackend ? () => aimockBackend.ensure() : undefined, + }) : null; const agUiSubagentsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'subagents' @@ -153,6 +165,7 @@ const release = () => { }; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; + if (aimockBackend && pathname !== '/') void aimockBackend.ensure().catch(() => undefined); if (agUiA2uiFixture && (await agUiA2uiFixture(request, response, pathname))) return; if (chatA2uiFixture) { if (pathname.startsWith('/__')) { @@ -490,6 +503,12 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || parentServer?.once('error', () => shutdown(1)); } +if (aimockBackend && !shutdown) { + const stop = () => { void aimockBackend.close().finally(() => process.exit(0)); }; + process.once('SIGINT', stop); + process.once('SIGTERM', stop); +} + // Own signals, errors, workers and both listeners before tooling discovery or proof. // Only dependency tooling is reused; each reset still owns a fresh graph process. try { From bf82604bd1f9d36ea7cc4eaa03b36fe1f08e6936 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:50:51 -0800 Subject: [PATCH 03/41] test(website): expect the theming preview's aimock backend flag in the config spec Co-Authored-By: Claude Opus 5.5 --- apps/website/src/playwright-config.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index c8669afeb..6e0ea6aa2 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -85,7 +85,7 @@ it('scopes Chat Theming Website to two owned servers with zero retries and focus const servers = Array.isArray(config.webServer) ? config.webServer : []; expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4308', 'http://127.0.0.1:4644']); expect(servers.every(server => server.reuseExistingServer === false)).toBe(true); - expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent', gracefulShutdown: { signal: 'SIGTERM', timeout: 5000 } }); + expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend', gracefulShutdown: { signal: 'SIGTERM', timeout: 5000 } }); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); expect(project.implicitDependencies).toContain('cockpit-chat-theming-react'); expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'build').projects).toContain('cockpit-chat-theming-react'); @@ -517,7 +517,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-element-rendering --no-parent',url:'http://127.0.0.1:4618'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-computed-functions --no-parent',url:'http://127.0.0.1:4619'}), expect.objectContaining({command:'npx nx run cockpit-chat-messages-python:smoke && node scripts/react-cockpit/serve.mjs chat-messages --no-parent',url:'http://127.0.0.1:4620'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent',url:'http://127.0.0.1:4644'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend',url:'http://127.0.0.1:4644'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-debug --no-parent',url:'http://127.0.0.1:4645'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent',url:'http://127.0.0.1:4646'}), expect.objectContaining({command:'npx nx run cockpit-chat-input-python:smoke && node scripts/react-cockpit/serve.mjs chat-input --no-parent',url:'http://127.0.0.1:4621'}), From d56a41479e64bd35a193bc480c55a6e7e4856eb6 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 14:40:02 -0800 Subject: [PATCH 04/41] test(cockpit): run React chat tool-calls/subagents/interrupts e2e on aimock Batch 2 of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md). - Shared Angular fixtures gain React prompts whose tool rounds key on aimock toolCallId; subagent child calls key on role prompt + task text; a mid-stream child model failure is aimock truncateAfterChunks; a title (metadata) failure is a per-prompt error fixture. - Proxy: /__hold/stream { beforeToolResult | afterChildMessage }, /__fail-stream, /__child/ (injected, as before), tool and approval /__history modes, /__ready. - Delete the three fixture servers, their wire workers and fixture specs; e2e depends on each example's network-free Python graph test directly. - chat/generative-ui moves to batch 2b: its faults swap graph internals that aimock cannot produce. Co-Authored-By: Claude Opus 5.5 --- .../angular/e2e/fixtures/c-interrupts.json | 187 +++++++ .../interrupts/react/e2e/global-setup-impl.ts | 6 + .../interrupts/react/e2e/interrupts.spec.ts | 28 +- .../interrupts/react/e2e/playwright.config.ts | 3 + cockpit/chat/interrupts/react/project.json | 17 +- .../angular/e2e/fixtures/c-subagents.json | 474 ++++++++++++++++- .../subagents/react/e2e/global-setup-impl.ts | 6 + .../subagents/react/e2e/playwright.config.ts | 3 + .../subagents/react/e2e/subagents.spec.ts | 48 +- cockpit/chat/subagents/react/project.json | 17 +- .../angular/e2e/fixtures/c-tool-calls.json | 209 ++++++++ .../tool-calls/react/e2e/global-setup-impl.ts | 6 + .../tool-calls/react/e2e/playwright.config.ts | 3 + .../tool-calls/react/e2e/tool-calls.spec.ts | 40 +- cockpit/chat/tool-calls/react/project.json | 17 +- .../2026-11-18-react-cockpit-aimock-design.md | 9 +- scripts/cockpit-frontend-matrix.spec.mjs | 12 + .../react-cockpit/chat-interrupts-fixture.mjs | 404 --------------- .../chat-interrupts-fixture.spec.mjs | 270 ---------- scripts/react-cockpit/chat-interrupts-wire.py | 94 ---- .../react-cockpit/chat-subagents-fixture.mjs | 482 ------------------ .../chat-subagents-fixture.spec.mjs | 375 -------------- scripts/react-cockpit/chat-subagents-wire.py | 96 ---- .../react-cockpit/chat-tool-calls-fixture.mjs | 422 --------------- .../chat-tool-calls-fixture.spec.mjs | 318 ------------ scripts/react-cockpit/chat-tool-calls-wire.py | 91 ---- scripts/react-cockpit/configuration.mjs | 3 + scripts/react-cockpit/langgraph-proxy.mjs | 145 +++++- scripts/react-cockpit/serve.mjs | 13 +- 29 files changed, 1094 insertions(+), 2704 deletions(-) create mode 100644 cockpit/chat/interrupts/react/e2e/global-setup-impl.ts create mode 100644 cockpit/chat/subagents/react/e2e/global-setup-impl.ts create mode 100644 cockpit/chat/tool-calls/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/chat-interrupts-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-interrupts-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-interrupts-wire.py delete mode 100644 scripts/react-cockpit/chat-subagents-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-subagents-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-subagents-wire.py delete mode 100644 scripts/react-cockpit/chat-tool-calls-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-tool-calls-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-tool-calls-wire.py diff --git a/cockpit/chat/interrupts/angular/e2e/fixtures/c-interrupts.json b/cockpit/chat/interrupts/angular/e2e/fixtures/c-interrupts.json index fd80b43cd..c10bfce28 100644 --- a/cockpit/chat/interrupts/angular/e2e/fixtures/c-interrupts.json +++ b/cockpit/chat/interrupts/angular/e2e/fixtures/c-interrupts.json @@ -1,5 +1,26 @@ { "fixtures": [ + { + "match": { + "systemMessage": "In 3-5 words, summarize", + "userMessage": "(title fails)" + }, + "response": { + "error": { + "message": "PRIVATE title model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "In 3-5 words, summarize" + }, + "response": { + "content": "Aviation snippet title" + } + }, { "match": { "userMessage": "Book me on UA123.", @@ -71,6 +92,172 @@ "systemHash": "18e3aede", "toolsHash": "3644d4ff" } + }, + { + "match": { + "toolCallId": "ic-padded-0" + }, + "response": { + "content": "Your booking request is complete." + } + }, + { + "match": { + "toolCallId": "ic-aa404-0" + }, + "response": { + "content": "Your booking request is complete." + } + }, + { + "match": { + "toolCallId": "ic-ua123-0" + }, + "response": { + "content": "Your booking request is complete." + } + }, + { + "match": { + "toolCallId": "ic-read-0" + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "ic-read-1" + } + ] + } + }, + { + "match": { + "toolCallId": "ic-read-1" + }, + "response": { + "content": "Your booking request is complete." + } + }, + { + "match": { + "toolCallId": "ic-unknown-0" + }, + "response": { + "content": "I could not find that flight, so nothing was booked." + } + }, + { + "match": { + "toolCallId": "ic-multi-1" + }, + "response": { + "content": "Your booking request is complete." + } + }, + { + "match": { + "userMessage": "Book padded flight.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \" ua123 \"}", + "id": "ic-padded-0" + } + ] + } + }, + { + "match": { + "userMessage": "Book AA404.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"AA404\"}", + "id": "ic-aa404-0" + } + ] + } + }, + { + "match": { + "userMessage": "Book UA123.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "ic-ua123-0" + } + ] + } + }, + { + "match": { + "userMessage": "Read then book UA123.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "ic-read-0" + } + ] + } + }, + { + "match": { + "userMessage": "Book unknown flight.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"ZZ999\"}", + "id": "ic-unknown-0" + } + ] + } + }, + { + "match": { + "userMessage": "Book multiple flights.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "ic-multi-0" + }, + { + "name": "book_flight", + "arguments": "{\"flight_number\": \"AA404\"}", + "id": "ic-multi-1" + } + ] + } + }, + { + "match": { + "userMessage": "Explain aviation" + }, + "response": { + "content": "An aviation answer.\n\n```typescript\nconst approved = true;\n```" + }, + "chunkSize": 3 } ] } \ No newline at end of file diff --git a/cockpit/chat/interrupts/react/e2e/global-setup-impl.ts b/cockpit/chat/interrupts/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..8fb0e2ffa --- /dev/null +++ b/cockpit/chat/interrupts/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/interrupts', port: 4622 }); diff --git a/cockpit/chat/interrupts/react/e2e/interrupts.spec.ts b/cockpit/chat/interrupts/react/e2e/interrupts.spec.ts index 88b2f759a..d2748fe46 100644 --- a/cockpit/chat/interrupts/react/e2e/interrupts.spec.ts +++ b/cockpit/chat/interrupts/react/e2e/interrupts.spec.ts @@ -1,4 +1,9 @@ import { test, expect, type Page } from '@playwright/test'; +// Backend: aimock replays cockpit/chat/interrupts/angular/e2e/fixtures (shared +// with the Angular example; React prompts key tool rounds on toolCallId) into a +// real `langgraph dev`, which raises the real book_flight interrupt. The React +// static server proxies /api to it, recording requests and injecting holds and +// failures (scripts/react-cockpit/langgraph-proxy.mjs). type Wire = { path: string; method: string; @@ -103,16 +108,8 @@ test('installed SDK confirms then cancels real saved pauses on one conversation' expect(wire.filter((item) => item.path.endsWith('/history'))).toHaveLength(4); expect(wire.filter((item) => item.method === 'GET')).toHaveLength(4); await expect(conversation(page)).not.toContainText( - 'Authored aviation conversation' + 'Aviation snippet title' ); - const proofs = await (await request.get('/__graph-proof')).json(); - expect(proofs).toHaveLength(4); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - }); }); test('direct answers and causally completed read tools precede a saved approval', async ({ page, @@ -203,7 +200,7 @@ test('held resume admits one decision and Stop fences late completion', async ({ }) => { await page.goto(base); await pause(page); - await request.post('/__hold-resume'); + await request.post('/__hold/stream'); await page.getByRole('button', { name: 'Book flight', exact: true }).click(); await expect( page.getByRole('button', { name: 'Book flight', exact: true }) @@ -224,7 +221,7 @@ test('failed resume is never retried and New only resets local state', async ({ }) => { await page.goto(base); await pause(page); - await request.post('/__fail-resume'); + await request.post('/__fail-stream'); await page.getByRole('button', { name: "Don't book", exact: true }).click(); await blocked(page); const wire: Wire[] = await (await request.get('/__requests')).json(); @@ -259,12 +256,13 @@ test('metadata failure cannot corrupt the real graph wire or expose a title resp page, request, }) => { - await request.post('/__metadata-failure'); + // The title model call for this prompt fails (its own aimock error fixture); + // generate_title swallows it like any metadata failure. await page.goto(base); - await pause(page); + await pause(page, 'Book padded flight. (title fails)'); await page.getByRole('button', { name: "Don't book", exact: true }).click(); await saved(page); - await expect(conversation(page)).not.toContainText('Authored metadata'); + await expect(conversation(page)).not.toContainText('PRIVATE'); }); test('authored developer connection routes only to its configured target', async ({ page, @@ -343,7 +341,7 @@ test('Stop during held initial streaming cannot expose a late approval', async ( page, request, }) => { - await request.post('/__hold-stream'); + await request.post('/__hold/stream'); await page.goto(base); await submit(page, 'Book UA123.'); await expect diff --git a/cockpit/chat/interrupts/react/e2e/playwright.config.ts b/cockpit/chat/interrupts/react/e2e/playwright.config.ts index 19b92564d..e3e89579a 100644 --- a/cockpit/chat/interrupts/react/e2e/playwright.config.ts +++ b/cockpit/chat/interrupts/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4622', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/interrupts/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-interrupts', cwd: '../../../../..', diff --git a/cockpit/chat/interrupts/react/project.json b/cockpit/chat/interrupts/react/project.json index c1ae47b7f..20d8ba9de 100644 --- a/cockpit/chat/interrupts/react/project.json +++ b/cockpit/chat/interrupts/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/interrupts/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-interrupts-angular", "cockpit-chat-interrupts-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-interrupts-python"] }], "options": { "config": "cockpit/chat/interrupts/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-interrupts-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-interrupts-fixture.spec.mjs" - } } } } diff --git a/cockpit/chat/subagents/angular/e2e/fixtures/c-subagents.json b/cockpit/chat/subagents/angular/e2e/fixtures/c-subagents.json index bf1abbdad..e4474a279 100644 --- a/cockpit/chat/subagents/angular/e2e/fixtures/c-subagents.json +++ b/cockpit/chat/subagents/angular/e2e/fixtures/c-subagents.json @@ -1,5 +1,349 @@ { "fixtures": [ + { + "match": { + "systemMessage": "In 3-5 words, summarize", + "userMessage": "(title fails)" + }, + "response": { + "error": { + "message": "PRIVATE title model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "In 3-5 words, summarize" + }, + "response": { + "content": "Aviation snippet title" + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for delegate sequential" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for delegate sequential." + } + }, + { + "match": { + "systemMessage": "You are a Booking Agent", + "userMessage": "Booking LAX to JFK for delegate sequential" + }, + "response": { + "content": "Booking suggestion: Booking LAX to JFK for delegate sequential." + } + }, + { + "match": { + "systemMessage": "You are an Itinerary Agent", + "userMessage": "Itinerary LAX to JFK for delegate sequential" + }, + "response": { + "content": "Itinerary suggestion: Itinerary LAX to JFK for delegate sequential." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for delegate parallel" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for delegate parallel." + } + }, + { + "match": { + "systemMessage": "You are a Booking Agent", + "userMessage": "Booking LAX to JFK for delegate parallel" + }, + "response": { + "content": "Booking suggestion: Booking LAX to JFK for delegate parallel." + } + }, + { + "match": { + "systemMessage": "You are an Itinerary Agent", + "userMessage": "Itinerary LAX to JFK for delegate parallel" + }, + "response": { + "content": "Itinerary suggestion: Itinerary LAX to JFK for delegate parallel." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for repeated task (1)" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for repeated task (1)." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for repeated task (2)" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for repeated task (2)." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for empty-child" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for empty-child." + } + }, + { + "match": { + "systemMessage": "You are a Booking Agent", + "userMessage": "Booking LAX to JFK for empty-child" + }, + "response": { + "content": " " + } + }, + { + "match": { + "systemMessage": "You are an Itinerary Agent", + "userMessage": "Itinerary LAX to JFK for empty-child" + }, + "response": { + "content": "Itinerary suggestion: Itinerary LAX to JFK for empty-child." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for fail-child" + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for fail-child." + } + }, + { + "match": { + "systemMessage": "You are a Booking Agent", + "userMessage": "Booking LAX to JFK for fail-child" + }, + "response": { + "content": "Authored partial child output that never completes." + }, + "chunkSize": 8, + "truncateAfterChunks": 1 + }, + { + "match": { + "systemMessage": "You are an Itinerary Agent", + "userMessage": "Itinerary LAX to JFK for fail-child" + }, + "response": { + "content": "Itinerary suggestion: Itinerary LAX to JFK for fail-child." + } + }, + { + "match": { + "systemMessage": "You are a Research Agent", + "userMessage": "Research LAX to JFK for Literal long " + }, + "response": { + "content": "Research suggestion: Research LAX to JFK for Literal long ." + } + }, + { + "match": { + "systemMessage": "You are a Booking Agent", + "userMessage": "Booking LAX to JFK for Literal long " + }, + "response": { + "content": "Booking suggestion: Booking LAX to JFK for Literal long ." + } + }, + { + "match": { + "systemMessage": "You are an Itinerary Agent", + "userMessage": "Itinerary LAX to JFK for Literal long " + }, + "response": { + "content": "Itinerary suggestion: Itinerary LAX to JFK for Literal long ." + } + }, + { + "match": { + "toolCallId": "task-seq-0" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"booking\", \"task_description\": \"Booking LAX to JFK for delegate sequential\"}", + "id": "task-seq-1" + } + ] + } + }, + { + "match": { + "toolCallId": "task-seq-1" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"itinerary\", \"task_description\": \"Itinerary LAX to JFK for delegate sequential\"}", + "id": "task-seq-2" + } + ] + } + }, + { + "match": { + "toolCallId": "task-seq-2" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-par-2" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-rep-1" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-empty-0" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"booking\", \"task_description\": \"Booking LAX to JFK for empty-child\"}", + "id": "task-empty-1" + } + ] + } + }, + { + "match": { + "toolCallId": "task-empty-1" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"itinerary\", \"task_description\": \"Itinerary LAX to JFK for empty-child\"}", + "id": "task-empty-2" + } + ] + } + }, + { + "match": { + "toolCallId": "task-empty-2" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-fail-0" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"booking\", \"task_description\": \"Booking LAX to JFK for fail-child\"}", + "id": "task-fail-1" + } + ] + } + }, + { + "match": { + "toolCallId": "task-fail-1" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"itinerary\", \"task_description\": \"Itinerary LAX to JFK for fail-child\"}", + "id": "task-fail-2" + } + ] + } + }, + { + "match": { + "toolCallId": "task-fail-2" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-literal-0" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"booking\", \"task_description\": \"Booking LAX to JFK for Literal long \"}", + "id": "task-literal-1" + } + ] + } + }, + { + "match": { + "toolCallId": "task-literal-1" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"itinerary\", \"task_description\": \"Itinerary LAX to JFK for Literal long \"}", + "id": "task-literal-2" + } + ] + } + }, + { + "match": { + "toolCallId": "task-literal-2" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, + { + "match": { + "toolCallId": "task-malformed-0" + }, + "response": { + "content": "Here is your LAX to JFK trip plan." + } + }, { "match": { "userMessage": "Plan a trip from LAX to JFK", @@ -119,6 +463,134 @@ "systemHash": "03217c65", "toolsHash": "1ce7a06f" } + }, + { + "match": { + "userMessage": "delegate sequential", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for delegate sequential\"}", + "id": "task-seq-0" + } + ] + } + }, + { + "match": { + "userMessage": "delegate parallel", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for delegate parallel\"}", + "id": "task-par-0" + }, + { + "name": "task", + "arguments": "{\"subagent_type\": \"booking\", \"task_description\": \"Booking LAX to JFK for delegate parallel\"}", + "id": "task-par-1" + }, + { + "name": "task", + "arguments": "{\"subagent_type\": \"itinerary\", \"task_description\": \"Itinerary LAX to JFK for delegate parallel\"}", + "id": "task-par-2" + } + ] + } + }, + { + "match": { + "userMessage": "repeated task", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for repeated task (1)\"}", + "id": "task-rep-0" + }, + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for repeated task (2)\"}", + "id": "task-rep-1" + } + ] + } + }, + { + "match": { + "userMessage": "empty-child", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for empty-child\"}", + "id": "task-empty-0" + } + ] + } + }, + { + "match": { + "userMessage": "fail-child", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for fail-child\"}", + "id": "task-fail-0" + } + ] + } + }, + { + "match": { + "userMessage": "Literal long ", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": \"Research LAX to JFK for Literal long \"}", + "id": "task-literal-0" + } + ] + } + }, + { + "match": { + "userMessage": "malformed task", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"research\", \"task_description\": 123}", + "id": "task-malformed-0" + } + ] + } + }, + { + "match": { + "userMessage": "direct answer" + }, + "response": { + "content": "A direct answer with no specialists." + } } ] -} \ No newline at end of file +} diff --git a/cockpit/chat/subagents/react/e2e/global-setup-impl.ts b/cockpit/chat/subagents/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..e5cc2ffb4 --- /dev/null +++ b/cockpit/chat/subagents/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/subagents', port: 4624 }); diff --git a/cockpit/chat/subagents/react/e2e/playwright.config.ts b/cockpit/chat/subagents/react/e2e/playwright.config.ts index 547916ef9..2e441f380 100644 --- a/cockpit/chat/subagents/react/e2e/playwright.config.ts +++ b/cockpit/chat/subagents/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4624', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/subagents/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-subagents', cwd: '../../../../..', diff --git a/cockpit/chat/subagents/react/e2e/subagents.spec.ts b/cockpit/chat/subagents/react/e2e/subagents.spec.ts index b5098800e..67487028b 100644 --- a/cockpit/chat/subagents/react/e2e/subagents.spec.ts +++ b/cockpit/chat/subagents/react/e2e/subagents.spec.ts @@ -1,13 +1,15 @@ import { test, expect, type Page } from '@playwright/test'; -import { createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; +// Backend: aimock replays cockpit/chat/subagents/angular/e2e/fixtures (shared +// with the Angular example; React prompts key parent rounds on toolCallId and +// child calls on their role prompt + task text) into a real `langgraph dev`. +// The React static server proxies /api to it, recording requests and injecting +// holds and failures (scripts/react-cockpit/langgraph-proxy.mjs). test('a held real tool call exposes complete arguments before its result', async ({ page, request, }) => { - expect((await request.post('/__hold-tool')).status()).toBe(200); + expect((await request.post('/__hold/stream', { data: { beforeToolResult: true } })).status()).toBe(200); await page.goto(base); await submit(page, 'delegate sequential'); // The transcript shows progress while the run is held, not just the footer status. @@ -32,7 +34,7 @@ test('worker failure is generic and never retried', async ({ page, request, }) => { - expect((await request.post('/__worker-failure')).status()).toBe(200); + expect((await request.post('/__fail-stream')).status()).toBe(200); await page.goto(base); await submit(page, 'delegate sequential'); await blocked(page); @@ -41,7 +43,7 @@ test('worker failure is generic and never retried', async ({ wire.filter((item) => item.path.endsWith('/runs/stream')) ).toHaveLength(1); expect(wire.filter((item) => item.path.endsWith('/history'))).toHaveLength(0); - await expect(page.locator('body')).not.toContainText('Graph worker'); + await expect(page.locator('body')).not.toContainText('PRIVATE'); }); test('long literal tool arguments and data remain text on mobile', async ({ page, @@ -93,12 +95,13 @@ test('metadata failure preserves the real tool stream and hides private diagnost page, request, }) => { - await request.post('/__metadata-failure'); + // The title model call for this prompt fails (its own aimock error fixture); + // generate_title swallows it like any metadata failure. await page.goto(base); - await submit(page, 'delegate sequential'); + await submit(page, 'delegate sequential (title fails)'); await saved(page); await expect(cards(page)).toHaveCount(3); - await expect(page.locator('body')).not.toContainText('Authored metadata'); + await expect(page.locator('body')).not.toContainText('PRIVATE'); }); test('authored developer connection routes only to its configured target', async ({ page, @@ -170,21 +173,6 @@ async function blocked(page: Page) { test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); -test.afterEach(async ({ request }) => { - const proofs = await (await request.get('/__graph-proof')).json(); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - sourceSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/src/graph.py'))) - .digest('hex'), - lockSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/uv.lock'))) - .digest('hex'), - }); -}); test('installed SDK confirms sequential and parallel specialists on one saved conversation', async ({ page, request, @@ -234,7 +222,7 @@ test('installed SDK confirms sequential and parallel specialists on one saved co expect(wire.filter((item) => item.path.endsWith('/history'))).toHaveLength(2); expect(wire.filter((item) => item.method === 'GET')).toHaveLength(2); await expect(conversation(page)).not.toContainText( - 'Authored trip conversation' + 'Aviation snippet title' ); expect(errors).toEqual([]); }); @@ -310,9 +298,11 @@ for (const hold of ['stream', 'child', 'history']) { page, request, }) => { - await request.post( - hold === 'history' ? '/__history/hold' : `/__hold-${hold}` - ); + if (hold === 'history') await request.post('/__history/hold'); + else + await request.post('/__hold/stream', { + data: hold === 'child' ? { afterChildMessage: true } : {}, + }); await page.goto(base); await submit(page, 'delegate sequential'); if (hold === 'history') @@ -390,7 +380,7 @@ test('held child text is local and Stop labels it without claiming specialist su page, request, }) => { - await request.post('/__hold-child'); + await request.post('/__hold/stream', { data: { afterChildMessage: true } }); await page.goto(base); await submit(page, 'delegate sequential'); const disclosure = page.locator('details.child-observations'); diff --git a/cockpit/chat/subagents/react/project.json b/cockpit/chat/subagents/react/project.json index 7c4a4896c..2fd2c41c5 100644 --- a/cockpit/chat/subagents/react/project.json +++ b/cockpit/chat/subagents/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/subagents/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-subagents-angular", "cockpit-chat-subagents-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-subagents-python"] }], "options": { "config": "cockpit/chat/subagents/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-subagents-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-subagents-fixture.spec.mjs" - } } } } diff --git a/cockpit/chat/tool-calls/angular/e2e/fixtures/c-tool-calls.json b/cockpit/chat/tool-calls/angular/e2e/fixtures/c-tool-calls.json index c7527f58c..a444fc476 100644 --- a/cockpit/chat/tool-calls/angular/e2e/fixtures/c-tool-calls.json +++ b/cockpit/chat/tool-calls/angular/e2e/fixtures/c-tool-calls.json @@ -1,5 +1,26 @@ { "fixtures": [ + { + "match": { + "systemMessage": "In 3-5 words, summarize", + "userMessage": "(title fails)" + }, + "response": { + "error": { + "message": "PRIVATE title model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "In 3-5 words, summarize" + }, + "response": { + "content": "Aviation snippet title" + } + }, { "match": { "userMessage": "What's the status of UA123?", @@ -23,6 +44,194 @@ } ] } + }, + { + "match": { + "toolCallId": "call-status-0" + }, + "response": { + "content": "UA123 is on time from LAX to JFK." + } + }, + { + "match": { + "toolCallId": "call-compare-1" + }, + "response": { + "content": "LAX and JFK are both operating normally." + } + }, + { + "match": { + "toolCallId": "call-seq-0" + }, + "response": { + "toolCalls": [ + { + "name": "get_airport_info", + "arguments": "{\"airport_code\": \"JFK\"}", + "id": "call-seq-1" + } + ] + } + }, + { + "match": { + "toolCallId": "call-seq-1" + }, + "response": { + "content": "UA123 lands at JFK, which is operating normally." + } + }, + { + "match": { + "toolCallId": "call-routes-0" + }, + "response": { + "content": "There are nonstop LAX to JFK flights tomorrow." + } + }, + { + "match": { + "toolCallId": "call-unknown-0" + }, + "response": { + "content": "I could not find that flight." + } + }, + { + "match": { + "toolCallId": "call-malformed-0" + }, + "response": { + "content": "That flight number was not valid." + } + }, + { + "match": { + "toolCallId": "call-literal-0" + }, + "response": { + "content": "That airport code was not recognised." + } + }, + { + "match": { + "userMessage": "Status UA123", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "call-status-0" + } + ] + } + }, + { + "match": { + "userMessage": "Compare airports", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_airport_info", + "arguments": "{\"airport_code\": \"LAX\"}", + "id": "call-compare-0" + }, + { + "name": "get_airport_info", + "arguments": "{\"airport_code\": \"JFK\"}", + "id": "call-compare-1" + } + ] + } + }, + { + "match": { + "userMessage": "Sequential lookup", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_flight", + "arguments": "{\"flight_number\": \"UA123\"}", + "id": "call-seq-0" + } + ] + } + }, + { + "match": { + "userMessage": "Routes tomorrow", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "find_routes", + "arguments": "{\"from_code\": \"LAX\", \"to_code\": \"JFK\", \"date_offset_days\": 1}", + "id": "call-routes-0" + } + ] + } + }, + { + "match": { + "userMessage": "Unknown flight", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_flight", + "arguments": "{\"flight_number\": \"ZZ999\"}", + "id": "call-unknown-0" + } + ] + } + }, + { + "match": { + "userMessage": "Malformed flight", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_flight", + "arguments": "{\"flight_number\": 123}", + "id": "call-malformed-0" + } + ] + } + }, + { + "match": { + "userMessage": "Literal long airport", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_airport_info", + "arguments": "{\"airport_code\": \"\"}", + "id": "call-literal-0" + } + ] + } + }, + { + "match": { + "userMessage": "No tools" + }, + "response": { + "content": "A plain answer with no tools." + } } ] } diff --git a/cockpit/chat/tool-calls/react/e2e/global-setup-impl.ts b/cockpit/chat/tool-calls/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..5e84f80c8 --- /dev/null +++ b/cockpit/chat/tool-calls/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/tool-calls', port: 4623 }); diff --git a/cockpit/chat/tool-calls/react/e2e/playwright.config.ts b/cockpit/chat/tool-calls/react/e2e/playwright.config.ts index 250025137..02bb5449b 100644 --- a/cockpit/chat/tool-calls/react/e2e/playwright.config.ts +++ b/cockpit/chat/tool-calls/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4623', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/tool-calls/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-tool-calls', cwd: '../../../../..', diff --git a/cockpit/chat/tool-calls/react/e2e/tool-calls.spec.ts b/cockpit/chat/tool-calls/react/e2e/tool-calls.spec.ts index 02ca976f2..789767a2e 100644 --- a/cockpit/chat/tool-calls/react/e2e/tool-calls.spec.ts +++ b/cockpit/chat/tool-calls/react/e2e/tool-calls.spec.ts @@ -1,13 +1,15 @@ import { test, expect, type Page } from '@playwright/test'; -import { createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; +// Backend: aimock replays cockpit/chat/tool-calls/angular/e2e/fixtures (shared +// with the Angular example; React prompts key their tool rounds on +// toolCallId) into a real `langgraph dev`. The React static server proxies /api +// to it, recording requests and injecting holds and failures +// (scripts/react-cockpit/langgraph-proxy.mjs). test('a held real tool call exposes complete arguments before its result', async ({ page, request, }) => { - expect((await request.post('/__hold-tool')).status()).toBe(200); + expect((await request.post('/__hold/stream', { data: { beforeToolResult: true } })).status()).toBe(200); await page.goto(base); await submit(page, 'Status UA123'); const pending = page.locator('.tp-tool-activity[data-phase="pending"]'); @@ -25,7 +27,7 @@ test('worker failure is generic and never retried', async ({ page, request, }) => { - expect((await request.post('/__worker-failure')).status()).toBe(200); + expect((await request.post('/__fail-stream')).status()).toBe(200); await page.goto(base); await submit(page, 'Status UA123'); await blocked(page); @@ -34,7 +36,7 @@ test('worker failure is generic and never retried', async ({ wire.filter((item) => item.path.endsWith('/runs/stream')) ).toHaveLength(1); expect(wire.filter((item) => item.path.endsWith('/history'))).toHaveLength(0); - await expect(page.locator('body')).not.toContainText('Graph worker'); + await expect(page.locator('body')).not.toContainText('PRIVATE'); }); test('long literal tool arguments and data remain text on mobile', async ({ page, @@ -83,12 +85,13 @@ test('metadata failure preserves the real tool stream and hides private diagnost page, request, }) => { - await request.post('/__metadata-failure'); + // The title model call for this prompt fails (its own aimock error fixture); + // generate_title swallows it like any metadata failure. await page.goto(base); - await submit(page, 'Status UA123'); + await submit(page, 'Status UA123 (title fails)'); await saved(page); await expect(cards(page)).toHaveCount(1); - await expect(page.locator('body')).not.toContainText('Authored metadata'); + await expect(page.locator('body')).not.toContainText('PRIVATE'); }); test('authored developer connection routes only to its configured target', async ({ page, @@ -158,21 +161,6 @@ async function blocked(page: Page) { test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); -test.afterEach(async ({ request }) => { - const proofs = await (await request.get('/__graph-proof')).json(); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - sourceSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/src/graph.py'))) - .digest('hex'), - lockSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/uv.lock'))) - .digest('hex'), - }); -}); test('installed SDK confirms single and parallel tools on one saved conversation', async ({ page, request, @@ -219,7 +207,7 @@ test('installed SDK confirms single and parallel tools on one saved conversation expect(wire.filter((item) => item.path.endsWith('/history'))).toHaveLength(2); expect(wire.filter((item) => item.method === 'GET')).toHaveLength(2); await expect(conversation(page)).not.toContainText( - 'Authored aviation conversation' + 'Aviation snippet title' ); expect(errors).toEqual([]); }); @@ -293,7 +281,7 @@ for (const hold of ['stream', 'history']) { request, }) => { await request.post( - hold === 'stream' ? '/__hold-stream' : '/__history/hold' + hold === 'stream' ? '/__hold/stream' : '/__history/hold' ); await page.goto(base); await submit(page, 'Status UA123'); diff --git a/cockpit/chat/tool-calls/react/project.json b/cockpit/chat/tool-calls/react/project.json index 15a5356e1..920e859f6 100644 --- a/cockpit/chat/tool-calls/react/project.json +++ b/cockpit/chat/tool-calls/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/tool-calls/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-tool-calls-angular", "cockpit-chat-tool-calls-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-tool-calls-python"] }], "options": { "config": "cockpit/chat/tool-calls/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-tool-calls-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-tool-calls-fixture.spec.mjs" - } } } } diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 6244f8f72..bf7f377c0 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -65,6 +65,12 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | `GET /__graph-proof` | in-process proof | removed; see "Guarantees" | | `GET /__captures` (batch 1) | authored SSE, saved messages, title updates | per finished run: forwarded SSE text, saved `messages`/`next` from the real `/state`, and thread title before and after the run, all captured before the browser sees the stream end | | `GET /__lifetime` child reaping (batch 1) | owned wire child processes | no per-run graph processes exist any more, so `openResponses` (proxied browser connections still open) replaces the child-reaping check | + | `/__hold-tool`, `/__hold-child`, `/__hold-resume`, `/__hold-stream` (batch 2) | authored stop points | `POST /__hold/stream` with `{ beforeToolResult }`, `{ afterChildMessage }`, or nothing (the next stream: a resume is just the next run) | + | `/__worker-failure`, `/__fail-resume` (batch 2) | graph worker closed / resume 500 | `POST /__fail-stream`: the next run stream answers 500 without reaching the graph | + | `/__metadata-failure` (batch 2) | `threads.get` raises | a per-prompt aimock error fixture for the title model (`… (title fails)`). `generate_title` swallows it on the same path as a metadata read failure | + | `/__child/` (batch 2) | malformed child `values` / child `error` event | same control on the proxy: injected data, as before | + | `/__history/` (batch 2) | corrupted tool or approval evidence | the same rewrites, applied to the real history | + | `/__ready` (batch 2) | Python worker loaded | `langgraph dev` answers `/ok` | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -150,7 +156,8 @@ example needs that the pilot proxy doesn't yet provide. | --- | --- | --- | --- | | 0 (done) | chat/messages | none | pilot | | 1 (done) | chat/input, chat/theming | `/__captures`, `/__inject/reasoning` (theming) | | -| 2 | chat/tool-calls, chat/subagents, chat/interrupts, chat/generative-ui | hold on a predicate (tool call, child or subgraph event, resume), `/__fail-resume`, `/__worker-failure`, `/__metadata-failure`, `/__configure` | add `afterEvent` predicates to `/__hold/stream`, plus route-matched failure injection | +| 2 (done) | chat/tool-calls, chat/subagents, chat/interrupts | `/__hold/stream` `beforeToolResult` / `afterChildMessage`, `/__fail-stream`, `/__child/`, tool and approval `/__history` modes, `/__ready` | tool rounds key on aimock `toolCallId`; a mid-stream child model failure is aimock `truncateAfterChunks`; a title (metadata) failure is a per-prompt aimock error fixture (`… (title fails)`) | +| 2b | chat/generative-ui | `/__configure` faults that swap graph internals: a throwing KPI tool, malformed trend data, prose injected into the `admit` node's output, partial layouts | aimock can't produce tool-code failures or node-output rewrites, so this needs a decision: move the graph-level faults to Python tests, or keep a small authored layer | | 3 | chat/threads, chat/timeline, chat/debug | title modes and holds, state and checkpoint holds, mapped checkpoints | history and state rewriting, holds per endpoint | | 4 | langgraph/{streaming, persistence, memory, interrupts, subgraphs, client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-draft`, `/__hold-extraction`, `/__hold-plan`, `/__publish-*`, `/__bad-history`, `/__unsafe-source` | `durable-execution` `/__publish-*` injects authored events that a real graph can't emit, so it keeps a small authored layer on top of the proxy | | 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index e427e0316..e25f64444 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -149,6 +149,10 @@ test('Chat Subagents owns an exact native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('chat-subagents'), { topic: 'subagents', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/subagents/react', base: '/chat/subagents/react/', port: 4624, project: 'cockpit-chat-subagents-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/subagents/python', + fixtures: 'cockpit/chat/subagents/angular/e2e/fixtures', langgraphPort: 5624, + }, }); for (const key of ['subagents', 'chat/subagents', '../chat-subagents']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); @@ -159,6 +163,10 @@ test('Chat Tool Calls owns an exact native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('chat-tool-calls'), { topic: 'tool-calls', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/tool-calls/react', base: '/chat/tool-calls/react/', port: 4623, project: 'cockpit-chat-tool-calls-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/tool-calls/python', + fixtures: 'cockpit/chat/tool-calls/angular/e2e/fixtures', langgraphPort: 5623, + }, }); for (const key of ['tool-calls', 'chat/tool-calls', '../chat-tool-calls']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); @@ -169,6 +177,10 @@ test('Chat Interrupts owns an explicit native React identity separate from LangG assert.deepEqual(reactCockpitConfiguration('chat-interrupts'), { topic: 'interrupts', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/interrupts/react', base: '/chat/interrupts/react/', port: 4622, project: 'cockpit-chat-interrupts-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/interrupts/python', + fixtures: 'cockpit/chat/interrupts/angular/e2e/fixtures', langgraphPort: 5622, + }, }); assert.equal(reactCockpitConfiguration('interrupts').appPath, 'cockpit/langgraph/interrupts/react'); for (const key of ['chat/interrupts', '../chat-interrupts']) diff --git a/scripts/react-cockpit/chat-interrupts-fixture.mjs b/scripts/react-cockpit/chat-interrupts-fixture.mjs deleted file mode 100644 index 0a4833517..000000000 --- a/scripts/react-cockpit/chat-interrupts-fixture.mjs +++ /dev/null @@ -1,404 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, 30000); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatInterruptsFixture({ - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/interrupts/python', - 'python', - 'scripts/react-cockpit/chat-interrupts-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - let worker, - queue = Promise.resolve(), - epoch = 0, - closed = false, - closing; - let requests = [], - proofs = [], - heldRun, - heldHistory; - let holdStream = false, - holdResume = false, - failCreate = false, - failResume = false, - historyMode = 'safe'; - const threads = new Set(), - runs = new Map(), - operations = new Set(); - const json = (response, value, status = 200) => { - if (response.destroyed) return; - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - function operate(value) { - const captured = epoch, - ticket = {}; - operations.add(ticket); - const result = queue.then(async () => { - if (closed || epoch !== captured) - throw new Error('Stale fixture operation'); - worker ??= graphWorker(spawnWorker); - const result = await worker.send(value); - if (closed || epoch !== captured) throw new Error('Stale graph response'); - return result; - }); - queue = result.catch(() => undefined); - return result.finally(() => operations.delete(ticket)); - } - function endHeld() { - heldRun?.response.end(); - heldHistory?.response.end(); - heldRun = heldHistory = undefined; - } - async function reset() { - ++epoch; - endHeld(); - const previous = worker; - worker = undefined; - queue = Promise.resolve(); - operations.clear(); - requests = []; - proofs = []; - threads.clear(); - runs.clear(); - holdStream = holdResume = failCreate = failResume = false; - historyMode = 'safe'; - await previous?.close(); - } - const handle = async (request, response, pathname) => { - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pendingOperations: operations.size, - activeStream: Boolean(heldRun), - activeHistory: Boolean(heldHistory), - }); - return true; - } - if ( - !pathname.startsWith('/__') && - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - try { - if (closed) throw new Error('Fixture closed'); - if (pathname === '/__reset') { - await reset(); - json(response, {}); - return true; - } - if (pathname === '/__ready') { - await operate({ op: 'configure' }); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__hold-stream') { - holdStream = true; - json(response, {}); - return true; - } - if (pathname === '/__hold-resume') { - holdResume = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreate = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-resume') { - failResume = true; - json(response, {}); - return true; - } - if (pathname === '/__metadata-failure') { - await operate({ op: 'configure', failMetadata: true }); - json(response, {}); - return true; - } - if (pathname === '/__release') { - const held = heldRun; - heldRun = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname === '/__release-history') { - const held = heldHistory; - heldHistory = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__history/')) { - const mode = pathname.split('/').at(-1); - if ( - ![ - 'safe', - 'hold', - 'changed', - 'missing', - 'pending', - 'failed', - 'mismatch', - 'unsupported', - ].includes(mode) - ) { - json(response, {}, 400); - return true; - } - historyMode = mode; - json(response, {}); - return true; - } - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}, - captured = epoch; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads') && request.method === 'POST') { - if (failCreate) { - failCreate = false; - throw new Error('Creation failed'); - } - if (typeof body.thread_id !== 'string' || threads.has(body.thread_id)) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - if (!threads.has(threadId)) { - json(response, {}, 404); - return true; - } - if (pathname.endsWith('/history') && request.method === 'POST') { - if (historyMode === 'failed') throw new Error('History failed'); - const wire = await operate({ op: 'history', threadId }), - history = wire.history; - const latest = history[0]; - if (latest && historyMode === 'changed') - latest.values.messages.at(-1).content = 'Changed canonical content'; - if (latest && historyMode === 'missing') latest.values.messages.pop(); - if (latest && historyMode === 'pending') latest.next = ['unexpected']; - const approval = latest?.tasks?.[0]?.interrupts?.[0]?.value; - if (approval && historyMode === 'mismatch') - approval.flight.flight_number = 'DL999'; - if (approval && historyMode === 'unsupported') - approval.type = 'unsupported'; - const finish = () => { - if (captured === epoch && !closed) json(response, history); - }; - if (historyMode === 'hold') { - const held = { response, finish }; - heldHistory = held; - response.on('close', () => { - if (heldHistory === held) heldHistory = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream') && request.method === 'POST') { - const resume = body.command !== undefined; - if ( - body.assistant_id !== 'c-interrupts' || - (resume - ? !['confirm', 'cancel'].includes(body.command?.resume) - : body.input?.messages?.length !== 1 || - body.input.messages[0].type !== 'human') - ) { - json(response, {}, 400); - return true; - } - if (resume && failResume) { - failResume = false; - throw new Error('Resume failed'); - } - const wire = await operate( - resume - ? { op: 'resume', threadId, value: body.command.resume } - : { op: 'submit', threadId, messages: body.input.messages } - ); - proofs.push(wire.proof); - const runId = randomUUID(), - run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const finish = () => { - if (response.destroyed || captured !== epoch || closed) return; - while (sent < wire.events.length) - response.write(event(...wire.events[sent++])); - run.status = wire.state.next.length ? 'interrupted' : 'success'; - response.end(); - }; - if (holdStream || (resume && holdResume)) { - holdStream = false; - if (resume) holdResume = false; - while (sent < Math.min(2, wire.events.length)) - response.write(event(...wire.events[sent++])); - const held = { response, finish, run }; - heldRun = held; - response.on('close', () => { - if (heldRun === held) { - heldRun = undefined; - run.status = 'interrupted'; - } - }); - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if (cancelId && request.method === 'POST') { - const run = runs.get(cancelId); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - run.status = 'interrupted'; - if (heldRun?.run === run) { - heldRun.response.end(); - heldRun = undefined; - } - json(response, {}); - return true; - } - if (request.method === 'GET' && /\/runs\/[^/]+$/.test(pathname)) { - const run = runs.get(pathname.split('/').at(-1)); - json(response, run ?? {}, run?.thread_id === threadId ? 200 : 404); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => { - if (closing) return closing; - closed = true; - closing = reset(); - return closing; - }; - return handle; -} diff --git a/scripts/react-cockpit/chat-interrupts-fixture.spec.mjs b/scripts/react-cockpit/chat-interrupts-fixture.spec.mjs deleted file mode 100644 index eea9396a7..000000000 --- a/scripts/react-cockpit/chat-interrupts-fixture.spec.mjs +++ /dev/null @@ -1,270 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { test } from 'node:test'; -import { createChatInterruptsFixture } from './chat-interrupts-fixture.mjs'; - -test('readiness warms the graph without creating conversations or run evidence', async (t) => { - const f = await fixture(t); - assert.equal((await f.post('/__ready')).status, 200); - assert.deepEqual(await f.json('/__requests'), []); - assert.deepEqual(await f.json('/__graph-proof'), []); -}); - -async function fixture(t, options) { - const handle = createChatInterruptsFixture(options); - const server = createServer(async (request, response) => { - if ( - !(await handle( - request, - response, - new URL(request.url, 'http://localhost').pathname - )) - ) { - response.writeHead(404); - response.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const post = (path, body = {}) => - fetch(base + path, { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify(body), - signal: AbortSignal.timeout(30000), - }); - const json = async (path, body) => { - const response = await post(path, body); - assert.equal(response.status, 200); - return response.json(); - }; - const stream = async (thread, body) => { - const response = await post(`/api/threads/${thread}/runs/stream`, { - assistant_id: 'c-interrupts', - ...body, - }); - assert.equal(response.status, 200); - return response.text(); - }; - const create = (thread) => - json('/api/threads', { thread_id: thread, if_exists: 'raise' }); - const history = (thread) => - json(`/api/threads/${thread}/history`, { limit: 10 }); - const submit = (thread, content, id = 'human-' + thread) => - stream(thread, { input: { messages: [{ type: 'human', content, id }] } }); - return { handle, post, json, stream, create, history, submit }; -} - -test('real worker resumes saved checkpoints and preserves causal results across isolated threads', async (t) => { - const f = await fixture(t); - for (const decision of ['confirm', 'cancel']) { - await f.create(decision); - const events = await f.submit(decision, 'Book padded flight.'); - assert.match(events, /__interrupt__/); - const [pause] = await f.history(decision); - assert.deepEqual(pause.next, ['tools']); - assert.equal( - pause.tasks[0].interrupts[0].value.flight.flight_number, - 'UA123' - ); - assert.equal(pause.checkpoint.thread_id, decision); - await f.stream(decision, { command: { resume: decision } }); - const [saved] = await f.history(decision); - assert.deepEqual(saved.next, []); - assert.equal(saved.values.messages.length, 4); - assert.equal( - saved.values.messages[2].tool_call_id, - saved.values.messages[1].tool_calls[0].id - ); - assert.match( - saved.values.messages[3].content, - decision === 'confirm' ? /Booked/ : /Booking cancelled/ - ); - } - const proofs = await f.json('/__graph-proof'); - assert.equal(proofs.length, 4); - assert( - proofs.every( - (proof) => - proof.actualCompiledGraph && - proof.networkConnectAttempts === 0 && - proof.titleMessageCallbacks === 0 - ) - ); - const lifetime = await f.json('/__lifetime'); - assert.equal(lifetime.workers, 1); - const pid = lifetime.workerPid; - await f.handle.close(); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -}); - -test('caught title metadata diagnostics cannot corrupt protocol or canonical success', async (t) => { - const f = await fixture(t); - await f.json('/__metadata-failure'); - await f.create('metadata'); - const events = await f.submit('metadata', 'Hello'); - assert.doesNotMatch(events, /generate_title|Authored metadata read failure/); - const [saved] = await f.history('metadata'); - assert.deepEqual(saved.next, []); - assert.equal(saved.values.messages.length, 2); - assert.match(saved.values.messages[1].content, /authored aviation answer/); -}); - -test('reset fences held responses and replaces the real worker without leaking old checkpoints', async (t) => { - const f = await fixture(t); - await f.create('old'); - await f.json('/__hold-stream'); - const pending = f.submit('old', 'Book UA123.'); - while (!(await f.json('/__lifetime')).activeStream) - await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = (await f.json('/__lifetime')).workerPid; - await f.json('/__reset'); - await pending; - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('new'); - await f.submit('new', 'Book AA404.'); - const [pause] = await f.history('new'); - assert.equal(pause.checkpoint.thread_id, 'new'); - assert.equal(pause.values.messages.length, 2); - assert.equal( - pause.tasks[0].interrupts[0].value.flight.flight_number, - 'AA404' - ); - assert.notEqual((await f.json('/__lifetime')).workerPid, oldPid); -}); - -test('closing rejects queued operations and terminates an owned stalled worker', async (t) => { - let child; - const f = await fixture(t, { - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - await f.create('one'); - await f.create('two'); - const first = f.post('/api/threads/one/runs/stream', { - assistant_id: 'c-interrupts', - input: { messages: [{ type: 'human', content: 'Hello', id: 'one' }] }, - }); - const second = f.post('/api/threads/two/runs/stream', { - assistant_id: 'c-interrupts', - input: { messages: [{ type: 'human', content: 'Hello', id: 'two' }] }, - }); - while (!child?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const pid = child.pid; - await f.handle.close(); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -}); - -test('malformed worker output fails closed with no private diagnostic in HTTP', async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.write("PRIVATE bad protocol\\n"));', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('bad'); - const response = await f.post('/api/threads/bad/runs/stream', { - assistant_id: 'c-interrupts', - input: { messages: [{ type: 'human', content: 'Hello', id: 'bad' }] }, - }); - assert.equal(response.status, 500); - assert.doesNotMatch(await response.text(), /PRIVATE/); -}); - -test('stdout EOF rejects pending work promptly even when the worker stays alive', async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.end()); setInterval(() => {}, 1000);', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('eof'); - const pending = f.post('/api/threads/eof/runs/stream', { - assistant_id: 'c-interrupts', - input: { messages: [{ type: 'human', content: 'Hello', id: 'eof' }] }, - }); - let timer; - const result = await Promise.race([ - pending.then((response) => response.status), - new Promise((resolve) => { - timer = setTimeout(() => resolve('still pending'), 1000); - }), - ]); - clearTimeout(timer); - assert.equal(result, 500); -}); - -test('reset rejects pending worker work and admits only a fresh real graph epoch', async (t) => { - let first, - calls = 0; - const f = await fixture(t, { - spawnWorker: () => { - if (++calls === 1) - return (first = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )); - return spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/interrupts/python', - 'python', - 'scripts/react-cockpit/chat-interrupts-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - }, - }); - await f.create('old-pending'); - const pending = f.post('/api/threads/old-pending/runs/stream', { - assistant_id: 'c-interrupts', - input: { messages: [{ type: 'human', content: 'Book UA123.', id: 'old' }] }, - }); - while (!first?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = first.pid; - await f.json('/__reset'); - assert.equal((await pending).status, 500); - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('fresh'); - await f.submit('fresh', 'Book AA404.'); - const [pause] = await f.history('fresh'); - assert.equal(pause.values.messages.length, 2); - assert.equal( - pause.tasks[0].interrupts[0].value.flight.flight_number, - 'AA404' - ); - const requests = await f.json('/__requests'); - assert.equal(requests.length, 3); - assert(requests.every((item) => !item.path.includes('old-pending'))); - assert.equal((await f.json('/__lifetime')).pendingOperations, 0); -}); diff --git a/scripts/react-cockpit/chat-interrupts-wire.py b/scripts/react-cockpit/chat-interrupts-wire.py deleted file mode 100644 index eb3d3d8b4..000000000 --- a/scripts/react-cockpit/chat-interrupts-wire.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Persistent, network-free compiled graph worker with protocol-only stdout.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/interrupts/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, model_factory -from langgraph.checkpoint.memory import InMemorySaver -from langgraph.types import Command - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if hasattr(value, "id") and hasattr(value, "value"): - return {"id": value.id, "value": value.value} - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": state.config["configurable"], - "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - - -async def main(): - client = AuthoredClient() - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_interrupts_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_interrupts_graph().builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - if request["op"] == "configure": - client.threads.fail = request.get("failMetadata", False) - result = {} - else: - config = {"configurable": {"thread_id": request["threadId"]}} - if request["op"] == "history": - result = {"history": [checkpoint(state) for state in graph.get_state_history(config)]} - else: - if request["op"] == "submit": - value = {"messages": request["messages"]} - elif request["op"] == "resume" and request["value"] in ("confirm", "cancel"): - value = Command(resume=request["value"]) - else: - raise ValueError("Unsupported graph operation") - events = [] - async for kind, event in graph.astream(value, config, stream_mode=["messages", "values"]): - if kind == "messages" and event[1].get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into the answer stream") - events.append([kind, event]) - result = {"events": events, "state": checkpoint(graph.get_state(config))} - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0, - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/chat-subagents-fixture.mjs b/scripts/react-cockpit/chat-subagents-fixture.mjs deleted file mode 100644 index 7b6b882c6..000000000 --- a/scripts/react-cockpit/chat-subagents-fixture.mjs +++ /dev/null @@ -1,482 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, 30000); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatSubagentsFixture({ - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/subagents/python', - 'python', - 'scripts/react-cockpit/chat-subagents-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - let worker, - queue = Promise.resolve(), - epoch = 0, - closed = false, - closing; - let requests = [], - proofs = [], - heldRun, - heldHistory; - let holdStream = false, - holdTool = false, - holdChild = false, - failCreate = false, - historyMode = 'safe', - childMode = 'safe'; - const threads = new Set(), - runs = new Map(), - operations = new Set(); - const json = (response, value, status = 200) => { - if (response.destroyed) return; - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - function operate(value) { - const captured = epoch, - ticket = {}; - operations.add(ticket); - const result = queue.then(async () => { - if (closed || epoch !== captured) - throw new Error('Stale fixture operation'); - worker ??= graphWorker(spawnWorker); - const result = await worker.send(value); - if (closed || epoch !== captured) throw new Error('Stale graph response'); - return result; - }); - queue = result.catch(() => undefined); - return result.finally(() => operations.delete(ticket)); - } - function endHeld() { - heldRun?.response.end(); - heldHistory?.response.end(); - heldRun = heldHistory = undefined; - } - async function reset() { - ++epoch; - endHeld(); - const previous = worker; - worker = undefined; - queue = Promise.resolve(); - operations.clear(); - requests = []; - proofs = []; - threads.clear(); - runs.clear(); - holdStream = holdTool = holdChild = failCreate = false; - historyMode = 'safe'; - childMode = 'safe'; - await previous?.close(); - } - const handle = async (request, response, pathname) => { - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pendingOperations: operations.size, - activeStream: Boolean(heldRun), - activeHistory: Boolean(heldHistory), - }); - return true; - } - if ( - !pathname.startsWith('/__') && - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - try { - if (closed) throw new Error('Fixture closed'); - if (pathname === '/__reset') { - await reset(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__hold-stream') { - holdStream = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreate = true; - json(response, {}); - return true; - } - if (pathname === '/__metadata-failure') { - await operate({ op: 'configure', failMetadata: true }); - json(response, {}); - return true; - } - if (pathname === '/__hold-child') { - holdChild = true; - json(response, {}); - return true; - } - if (pathname === '/__hold-tool') { - holdTool = true; - json(response, {}); - return true; - } - if (pathname === '/__worker-failure') { - await operate({ op: 'configure' }); - await worker.close(); - json(response, {}); - return true; - } - if (pathname === '/__release') { - const held = heldRun; - heldRun = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname === '/__release-history') { - const held = heldHistory; - heldHistory = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__child/')) { - const mode = pathname.split('/').at(-1); - if (!['safe', 'role', 'task', 'error'].includes(mode)) { - json(response, {}, 400); - return true; - } - childMode = mode; - json(response, {}); - return true; - } - if (pathname.startsWith('/__history/')) { - const mode = pathname.split('/').at(-1); - if ( - ![ - 'safe', - 'hold', - 'changed', - 'missing', - 'pending', - 'failed', - 'args', - 'result', - 'name', - 'ids', - 'interrupt', - 'subgraph', - ].includes(mode) - ) { - json(response, {}, 400); - return true; - } - historyMode = mode; - json(response, {}); - return true; - } - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}, - captured = epoch; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads') && request.method === 'POST') { - if (failCreate) { - failCreate = false; - throw new Error('Creation failed'); - } - if (typeof body.thread_id !== 'string' || threads.has(body.thread_id)) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - if (!threads.has(threadId)) { - json(response, {}, 404); - return true; - } - if (pathname.endsWith('/history') && request.method === 'POST') { - if (historyMode === 'failed') throw new Error('History failed'); - const wire = await operate({ op: 'history', threadId }), - history = wire.history; - const latest = history[0]; - if (latest && historyMode === 'changed') - latest.values.messages.at(-1).content = 'Changed canonical content'; - if (latest && historyMode === 'missing') latest.values.messages.pop(); - if (latest && historyMode === 'pending') latest.next = ['unexpected']; - const assistant = latest?.values.messages.find( - (message) => message.tool_calls?.length - ); - const result = latest?.values.messages.find( - (message) => message.type === 'tool' - ); - if (assistant && historyMode === 'args') - assistant.tool_calls[0].args = { - subagent_type: 'research', - task_description: 'Changed', - }; - if (result && historyMode === 'result') - result.content = 'Changed tool observation'; - if (result && historyMode === 'name') result.name = 'unsupported'; - if (result && historyMode === 'ids') result.tool_call_id = 'orphan'; - if (latest && historyMode === 'interrupt') - latest.tasks = [ - { - id: 'unexpected', - name: 'tools', - interrupts: [{ id: 'unexpected', value: { type: 'unexpected' } }], - }, - ]; - if (latest && historyMode === 'subgraph') - latest.checkpoint.checkpoint_ns = 'foreign:checkpoint'; - const finish = () => { - if (captured === epoch && !closed) json(response, history); - }; - if (historyMode === 'hold') { - const held = { response, finish }; - heldHistory = held; - response.on('close', () => { - if (heldHistory === held) heldHistory = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream') && request.method === 'POST') { - if ( - body.assistant_id !== 'c-subagents' || - body.command !== undefined || - body.input?.messages?.length !== 1 || - body.input.messages[0].type !== 'human' - ) { - json(response, {}, 400); - return true; - } - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - if (childMode !== 'safe') { - const index = wire.events.findIndex(([kind]) => - kind.startsWith('values|') - ); - if (index < 0) throw new Error('Expected actual child event'); - const [kind, value] = wire.events[index]; - const injected = - childMode === 'error' - ? [ - 'error' + kind.slice('values'.length), - { - error: 'AuthoredChildError', - message: 'PRIVATE_CHILD_DIAGNOSTIC', - }, - ] - : [ - kind, - { - ...value, - ...(childMode === 'role' - ? { subagent_type: 'unsupported-specialist' } - : { task_description: null }), - }, - ]; - wire.events.splice(index, 0, injected); - wire.proof.injectedChildFault = childMode; - } - proofs.push(wire.proof); - const runId = randomUUID(), - run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const finish = () => { - if (response.destroyed || captured !== epoch || closed) return; - while (sent < wire.events.length) - response.write(event(...wire.events[sent++])); - run.status = wire.graphFailure - ? 'error' - : wire.state.next.length - ? 'interrupted' - : 'success'; - response.end(); - }; - if (holdStream || holdTool || holdChild) { - const limit = holdChild - ? Math.max( - 0, - wire.events.findIndex(([kind]) => - kind.startsWith('messages|') - ) + 1 - ) - : holdTool - ? Math.max( - 0, - wire.events.findIndex( - ([kind, value]) => - kind === 'messages' && value[0]?.type === 'tool' - ) - ) - : Math.min(2, wire.events.length); - holdStream = false; - holdTool = false; - holdChild = false; - while (sent < limit) response.write(event(...wire.events[sent++])); - const held = { response, finish, run }; - heldRun = held; - response.on('close', () => { - if (heldRun === held) { - heldRun = undefined; - run.status = 'interrupted'; - } - }); - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if (cancelId && request.method === 'POST') { - const run = runs.get(cancelId); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - run.status = 'interrupted'; - if (heldRun?.run === run) { - heldRun.response.end(); - heldRun = undefined; - } - json(response, {}); - return true; - } - if (request.method === 'GET' && /\/runs\/[^/]+$/.test(pathname)) { - const run = runs.get(pathname.split('/').at(-1)); - json(response, run ?? {}, run?.thread_id === threadId ? 200 : 404); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => { - if (closing) return closing; - closed = true; - closing = reset(); - return closing; - }; - return handle; -} diff --git a/scripts/react-cockpit/chat-subagents-fixture.spec.mjs b/scripts/react-cockpit/chat-subagents-fixture.spec.mjs deleted file mode 100644 index 5638d23c3..000000000 --- a/scripts/react-cockpit/chat-subagents-fixture.spec.mjs +++ /dev/null @@ -1,375 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { test } from 'node:test'; -const { createChatSubagentsFixture } = await import( - './chat-subagents-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createChatSubagentsFixture, - 'function', - 'Subagents fixture is available' - ); - const handle = createChatSubagentsFixture(options); - const server = createServer(async (request, response) => { - if ( - !(await handle( - request, - response, - new URL(request.url, 'http://localhost').pathname - )) - ) { - response.writeHead(404); - response.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const post = (path, body = {}) => - fetch(base + path, { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify(body), - signal: AbortSignal.timeout(30000), - }); - const json = async (path, body) => { - const response = await post(path, body); - assert.equal(response.status, 200); - return response.json(); - }; - const stream = async (thread, body) => { - const response = await post(`/api/threads/${thread}/runs/stream`, { - assistant_id: 'c-subagents', - ...body, - }); - assert.equal(response.status, 200); - return response.text(); - }; - const create = (thread) => - json('/api/threads', { thread_id: thread, if_exists: 'raise' }); - const history = (thread) => - json(`/api/threads/${thread}/history`, { limit: 10 }); - const submit = (thread, content, id = 'human-' + thread) => - stream(thread, { input: { messages: [{ type: 'human', content, id }] } }); - return { handle, post, json, stream, create, history, submit }; -} - -test( - 'real worker keeps single, parallel and sequential tool calls causal across isolated threads', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - for (const [thread, text, expected] of [ - ['single', 'delegate sequential', 3], - ['parallel', 'delegate parallel', 3], - ['sequential', 'repeated task', 2], - ['empty', 'empty-child', 3], - ['invalid', 'malformed task', 1], - ['text', 'direct answer', 0], - ]) { - await f.create(thread); - const events = await f.submit(thread, text); - assert.doesNotMatch(events, /authored-title/); - const messagesOnly = events - .split('\n\n') - .filter((frame) => frame.startsWith('event: messages')); - assert(messagesOnly.every((frame) => !frame.includes('generate_title'))); - const [saved] = await f.history(thread); - assert.deepEqual(saved.next, []); - assert.equal(saved.checkpoint.thread_id, thread); - const messages = saved.values.messages; - const calls = new Map( - messages.flatMap((message, index) => - (message.tool_calls ?? []).map((call) => [call.id, { call, index }]) - ) - ); - const results = messages.filter((message) => message.type === 'tool'); - assert.equal(calls.size, expected); - assert.equal(results.length, expected); - for (const result of results) { - const owner = calls.get(result.tool_call_id); - assert(owner.index < messages.indexOf(result)); - assert.equal(owner.call.name, result.name); - } - if (thread === 'parallel') { - assert.deepEqual( - new Set( - [...calls.values()].map(({ call }) => call.args.subagent_type) - ), - new Set(['research', 'booking', 'itinerary']) - ); - assert.match(events, /event: messages\|tools:/); - } - if (thread === 'empty') - assert( - results.some((result) => result.content === '(no subagent output)') - ); - if (thread === 'invalid') assert.equal(results[0].status, 'error'); - assert.equal(messages.at(-1).type, 'ai'); - } - await f.submit('single', 'delegate parallel', 'followup'); - assert.equal((await f.history('single'))[0].values.messages.length, 14); - const proofs = await f.json('/__graph-proof'); - assert.equal(proofs.length, 7); - assert( - proofs.every( - (proof) => - proof.actualCompiledGraph && - proof.networkConnectAttempts === 0 && - proof.titleMessageCallbacks === 0 - ) - ); - const lifetime = await f.json('/__lifetime'); - assert.equal(lifetime.workers, 1); - await f.handle.close(); - assert.throws(() => process.kill(lifetime.workerPid, 0), { code: 'ESRCH' }); - } -); - -test( - 'caught title metadata diagnostics cannot corrupt protocol or canonical success', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.json('/__metadata-failure'); - await f.create('metadata'); - const events = await f.submit('metadata', 'direct answer'); - assert.doesNotMatch( - events, - /authored-title|Authored metadata read failure/ - ); - const [saved] = await f.history('metadata'); - assert.deepEqual(saved.next, []); - assert.equal(saved.values.messages.length, 2); - assert.match( - saved.values.messages[1].content, - /Authored parent trip suggestion/ - ); - } -); - -test( - 'reset fences held responses and replaces the real worker without leaking old checkpoints', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.create('old'); - await f.json('/__hold-stream'); - const pending = f.submit('old', 'Book UA123.'); - while (!(await f.json('/__lifetime')).activeStream) - await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = (await f.json('/__lifetime')).workerPid; - await f.json('/__reset'); - await pending; - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('new'); - await f.submit('new', 'Book AA404.'); - const [pause] = await f.history('new'); - assert.equal(pause.checkpoint.thread_id, 'new'); - assert.equal(pause.values.messages.length, 8); - assert.match( - pause.values.messages[2].content, - /Research authored suggestion/ - ); - assert.notEqual((await f.json('/__lifetime')).workerPid, oldPid); - } -); - -test( - 'closing rejects queued operations and terminates an owned stalled worker', - { timeout: 30000 }, - async (t) => { - let child; - const f = await fixture(t, { - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - await f.create('one'); - await f.create('two'); - const first = f.post('/api/threads/one/runs/stream', { - assistant_id: 'c-subagents', - input: { messages: [{ type: 'human', content: 'Hello', id: 'one' }] }, - }); - const second = f.post('/api/threads/two/runs/stream', { - assistant_id: 'c-subagents', - input: { messages: [{ type: 'human', content: 'Hello', id: 'two' }] }, - }); - while (!child?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const pid = child.pid; - await f.handle.close(); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - } -); - -test( - 'malformed worker output fails closed with no private diagnostic in HTTP', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.write("PRIVATE bad protocol\\n"));', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('bad'); - const response = await f.post('/api/threads/bad/runs/stream', { - assistant_id: 'c-subagents', - input: { messages: [{ type: 'human', content: 'Hello', id: 'bad' }] }, - }); - assert.equal(response.status, 500); - assert.doesNotMatch(await response.text(), /PRIVATE/); - } -); - -test( - 'stdout EOF rejects pending work promptly even when the worker stays alive', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.end()); setInterval(() => {}, 1000);', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('eof'); - const pending = f.post('/api/threads/eof/runs/stream', { - assistant_id: 'c-subagents', - input: { messages: [{ type: 'human', content: 'Hello', id: 'eof' }] }, - }); - let timer; - const result = await Promise.race([ - pending.then((response) => response.status), - new Promise((resolve) => { - timer = setTimeout(() => resolve('still pending'), 1000); - }), - ]); - clearTimeout(timer); - assert.equal(result, 500); - } -); - -test( - 'reset rejects pending worker work and admits only a fresh real graph epoch', - { timeout: 30000 }, - async (t) => { - let first, - calls = 0; - const f = await fixture(t, { - spawnWorker: () => { - if (++calls === 1) - return (first = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )); - return spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/subagents/python', - 'python', - 'scripts/react-cockpit/chat-subagents-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - }, - }); - await f.create('old-pending'); - const pending = f.post('/api/threads/old-pending/runs/stream', { - assistant_id: 'c-subagents', - input: { - messages: [{ type: 'human', content: 'Book UA123.', id: 'old' }], - }, - }); - while (!first?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = first.pid; - await f.json('/__reset'); - assert.equal((await pending).status, 500); - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('fresh'); - await f.submit('fresh', 'Book AA404.'); - const [pause] = await f.history('fresh'); - assert.equal(pause.values.messages.length, 8); - assert.match( - pause.values.messages[2].content, - /Research authored suggestion/ - ); - const requests = await f.json('/__requests'); - assert.equal(requests.length, 3); - assert(requests.every((item) => !item.path.includes('old-pending'))); - assert.equal((await f.json('/__lifetime')).pendingOperations, 0); - } -); - -test('exact Chat Subagents key selects only its authored build and serve configuration', async () => { - const { reactCockpitConfiguration } = await import('./configuration.mjs'); - assert.deepEqual(reactCockpitConfiguration('chat-subagents'), { - topic: 'subagents', - library: 'chat', - adapter: 'langgraph', - appPath: 'cockpit/chat/subagents/react', - base: '/chat/subagents/react/', - port: 4624, - project: 'cockpit-chat-subagents-react', - }); - assert.throws(() => reactCockpitConfiguration('../chat-subagents')); -}); -test( - 'actual child failure preserves streamed partial evidence and ends with an error event', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.create('child-failure'); - const events = await f.submit('child-failure', 'fail-child'); - assert.match(events, /Authored partial child/); - assert.match(events, /event: error/); - const proofs = await f.json('/__graph-proof'); - assert.equal(proofs.length, 1); - assert.equal(proofs[0].networkConnectAttempts, 0); - } -); - -test( - 'child corruption retains an actual child namespace and later healthy root evidence', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.json('/__child/role'); - await f.create('child-malformed'); - const events = await f.submit('child-malformed', 'delegate sequential'); - assert.match(events, /unsupported-specialist/); - assert.match(events, /event: values\|tools:/); - assert.match(events, /Authored parent trip suggestion/); - const [saved] = await f.history('child-malformed'); - assert.equal(saved.values.messages.length, 8); - } -); diff --git a/scripts/react-cockpit/chat-subagents-wire.py b/scripts/react-cockpit/chat-subagents-wire.py deleted file mode 100644 index eb5571a6f..000000000 --- a/scripts/react-cockpit/chat-subagents-wire.py +++ /dev/null @@ -1,96 +0,0 @@ -"""Persistent, network-free compiled graph worker with protocol-only stdout.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/subagents/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, model_factory -from langgraph.checkpoint.memory import InMemorySaver - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if hasattr(value, "id") and hasattr(value, "value"): - return {"id": value.id, "value": value.value} - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": state.config["configurable"], - "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - - -async def main(): - client = AuthoredClient() - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_subagents_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_subagents_graph().builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - if request["op"] == "configure": - client.threads.fail = request.get("failMetadata", False) - result = {} - else: - config = {"configurable": {"thread_id": request["threadId"]}} - if request["op"] == "history": - result = {"history": [checkpoint(state) for state in graph.get_state_history(config)]} - else: - if request["op"] == "submit": - value = {"messages": request["messages"]} - else: - raise ValueError("Unsupported graph operation") - events = [] - failure = False - try: - async for namespace, kind, event in graph.astream(value, config, stream_mode=["messages", "values", "updates", "custom"], subgraphs=True): - if kind == "messages" and event[1].get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into the answer stream") - events.append([kind + ("|" + "|".join(namespace) if namespace else ""), event]) - except RuntimeError: - failure = True - events.append(["error", {"error": "GraphExecutionError", "message": "The specialist request failed."}]) - result = {"events": events, "state": checkpoint(graph.get_state(config)), "graphFailure": failure} - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0, - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/chat-tool-calls-fixture.mjs b/scripts/react-cockpit/chat-tool-calls-fixture.mjs deleted file mode 100644 index 567ec5a9c..000000000 --- a/scripts/react-cockpit/chat-tool-calls-fixture.mjs +++ /dev/null @@ -1,422 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, 30000); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatToolCallsFixture({ - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/tool-calls/python', - 'python', - 'scripts/react-cockpit/chat-tool-calls-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - let worker, - queue = Promise.resolve(), - epoch = 0, - closed = false, - closing; - let requests = [], - proofs = [], - heldRun, - heldHistory; - let holdStream = false, - holdTool = false, - failCreate = false, - historyMode = 'safe'; - const threads = new Set(), - runs = new Map(), - operations = new Set(); - const json = (response, value, status = 200) => { - if (response.destroyed) return; - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - function operate(value) { - const captured = epoch, - ticket = {}; - operations.add(ticket); - const result = queue.then(async () => { - if (closed || epoch !== captured) - throw new Error('Stale fixture operation'); - worker ??= graphWorker(spawnWorker); - const result = await worker.send(value); - if (closed || epoch !== captured) throw new Error('Stale graph response'); - return result; - }); - queue = result.catch(() => undefined); - return result.finally(() => operations.delete(ticket)); - } - function endHeld() { - heldRun?.response.end(); - heldHistory?.response.end(); - heldRun = heldHistory = undefined; - } - async function reset() { - ++epoch; - endHeld(); - const previous = worker; - worker = undefined; - queue = Promise.resolve(); - operations.clear(); - requests = []; - proofs = []; - threads.clear(); - runs.clear(); - holdStream = holdTool = failCreate = false; - historyMode = 'safe'; - await previous?.close(); - } - const handle = async (request, response, pathname) => { - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pendingOperations: operations.size, - activeStream: Boolean(heldRun), - activeHistory: Boolean(heldHistory), - }); - return true; - } - if ( - !pathname.startsWith('/__') && - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - try { - if (closed) throw new Error('Fixture closed'); - if (pathname === '/__reset') { - await reset(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__hold-stream') { - holdStream = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreate = true; - json(response, {}); - return true; - } - if (pathname === '/__metadata-failure') { - await operate({ op: 'configure', failMetadata: true }); - json(response, {}); - return true; - } - if (pathname === '/__hold-tool') { - holdTool = true; - json(response, {}); - return true; - } - if (pathname === '/__worker-failure') { - await operate({ op: 'configure' }); - await worker.close(); - json(response, {}); - return true; - } - if (pathname === '/__release') { - const held = heldRun; - heldRun = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname === '/__release-history') { - const held = heldHistory; - heldHistory = undefined; - held?.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__history/')) { - const mode = pathname.split('/').at(-1); - if ( - ![ - 'safe', - 'hold', - 'changed', - 'missing', - 'pending', - 'failed', - 'args', - 'result', - 'name', - 'ids', - 'interrupt', - 'subgraph', - ].includes(mode) - ) { - json(response, {}, 400); - return true; - } - historyMode = mode; - json(response, {}); - return true; - } - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}, - captured = epoch; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads') && request.method === 'POST') { - if (failCreate) { - failCreate = false; - throw new Error('Creation failed'); - } - if (typeof body.thread_id !== 'string' || threads.has(body.thread_id)) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - if (!threads.has(threadId)) { - json(response, {}, 404); - return true; - } - if (pathname.endsWith('/history') && request.method === 'POST') { - if (historyMode === 'failed') throw new Error('History failed'); - const wire = await operate({ op: 'history', threadId }), - history = wire.history; - const latest = history[0]; - if (latest && historyMode === 'changed') - latest.values.messages.at(-1).content = 'Changed canonical content'; - if (latest && historyMode === 'missing') latest.values.messages.pop(); - if (latest && historyMode === 'pending') latest.next = ['unexpected']; - const assistant = latest?.values.messages.find( - (message) => message.tool_calls?.length - ); - const result = latest?.values.messages.find( - (message) => message.type === 'tool' - ); - if (assistant && historyMode === 'args') - assistant.tool_calls[0].args = { flight_number: 'Changed' }; - if (result && historyMode === 'result') - result.content = 'Changed tool observation'; - if (result && historyMode === 'name') result.name = 'unsupported'; - if (result && historyMode === 'ids') result.tool_call_id = 'orphan'; - if (latest && historyMode === 'interrupt') - latest.tasks = [ - { - id: 'unexpected', - name: 'tools', - interrupts: [{ id: 'unexpected', value: { type: 'unexpected' } }], - }, - ]; - if (latest && historyMode === 'subgraph') - latest.checkpoint.checkpoint_ns = 'foreign:checkpoint'; - const finish = () => { - if (captured === epoch && !closed) json(response, history); - }; - if (historyMode === 'hold') { - const held = { response, finish }; - heldHistory = held; - response.on('close', () => { - if (heldHistory === held) heldHistory = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream') && request.method === 'POST') { - if ( - body.assistant_id !== 'c-tool-calls' || - body.command !== undefined || - body.input?.messages?.length !== 1 || - body.input.messages[0].type !== 'human' - ) { - json(response, {}, 400); - return true; - } - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - proofs.push(wire.proof); - const runId = randomUUID(), - run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const finish = () => { - if (response.destroyed || captured !== epoch || closed) return; - while (sent < wire.events.length) - response.write(event(...wire.events[sent++])); - run.status = wire.state.next.length ? 'interrupted' : 'success'; - response.end(); - }; - if (holdStream || holdTool) { - const limit = holdTool - ? Math.max( - 0, - wire.events.findIndex( - ([kind, value]) => - kind === 'messages' && value[0]?.type === 'tool' - ) - ) - : Math.min(2, wire.events.length); - holdStream = false; - holdTool = false; - while (sent < limit) response.write(event(...wire.events[sent++])); - const held = { response, finish, run }; - heldRun = held; - response.on('close', () => { - if (heldRun === held) { - heldRun = undefined; - run.status = 'interrupted'; - } - }); - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if (cancelId && request.method === 'POST') { - const run = runs.get(cancelId); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - run.status = 'interrupted'; - if (heldRun?.run === run) { - heldRun.response.end(); - heldRun = undefined; - } - json(response, {}); - return true; - } - if (request.method === 'GET' && /\/runs\/[^/]+$/.test(pathname)) { - const run = runs.get(pathname.split('/').at(-1)); - json(response, run ?? {}, run?.thread_id === threadId ? 200 : 404); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => { - if (closing) return closing; - closed = true; - closing = reset(); - return closing; - }; - return handle; -} diff --git a/scripts/react-cockpit/chat-tool-calls-fixture.spec.mjs b/scripts/react-cockpit/chat-tool-calls-fixture.spec.mjs deleted file mode 100644 index 08e48d54b..000000000 --- a/scripts/react-cockpit/chat-tool-calls-fixture.spec.mjs +++ /dev/null @@ -1,318 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { test } from 'node:test'; -const { createChatToolCallsFixture } = await import( - './chat-tool-calls-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createChatToolCallsFixture, - 'function', - 'Tool Calls fixture is available' - ); - const handle = createChatToolCallsFixture(options); - const server = createServer(async (request, response) => { - if ( - !(await handle( - request, - response, - new URL(request.url, 'http://localhost').pathname - )) - ) { - response.writeHead(404); - response.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const post = (path, body = {}) => - fetch(base + path, { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify(body), - signal: AbortSignal.timeout(30000), - }); - const json = async (path, body) => { - const response = await post(path, body); - assert.equal(response.status, 200); - return response.json(); - }; - const stream = async (thread, body) => { - const response = await post(`/api/threads/${thread}/runs/stream`, { - assistant_id: 'c-tool-calls', - ...body, - }); - assert.equal(response.status, 200); - return response.text(); - }; - const create = (thread) => - json('/api/threads', { thread_id: thread, if_exists: 'raise' }); - const history = (thread) => - json(`/api/threads/${thread}/history`, { limit: 10 }); - const submit = (thread, content, id = 'human-' + thread) => - stream(thread, { input: { messages: [{ type: 'human', content, id }] } }); - return { handle, post, json, stream, create, history, submit }; -} - -test( - 'real worker keeps single, parallel and sequential tool calls causal across isolated threads', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - for (const [thread, text, expected] of [ - ['single', 'Status UA123', 1], - ['parallel', 'Compare airports', 2], - ['sequential', 'Sequential lookup', 2], - ['routes', 'Routes tomorrow', 1], - ['domain', 'Unknown flight', 1], - ['invalid', 'Malformed flight', 1], - ['text', 'No tools', 0], - ]) { - await f.create(thread); - const events = await f.submit(thread, text); - assert.doesNotMatch(events, /generate_title|authored-title/); - const [saved] = await f.history(thread); - assert.deepEqual(saved.next, []); - assert.equal(saved.checkpoint.thread_id, thread); - const messages = saved.values.messages; - const calls = new Map( - messages.flatMap((message, index) => - (message.tool_calls ?? []).map((call) => [call.id, { call, index }]) - ) - ); - const results = messages.filter((message) => message.type === 'tool'); - assert.equal(calls.size, expected); - assert.equal(results.length, expected); - for (const result of results) { - const owner = calls.get(result.tool_call_id); - assert(owner.index < messages.indexOf(result)); - assert.equal(owner.call.name, result.name); - } - if (thread === 'parallel') - assert.deepEqual( - new Set(results.map((result) => JSON.parse(result.content).code)), - new Set(['LAX', 'JFK']) - ); - if (thread === 'domain') assert.equal(results[0].status, 'success'); - if (thread === 'invalid') assert.equal(results[0].status, 'error'); - assert.equal(messages.at(-1).type, 'ai'); - } - await f.submit('single', 'Compare airports', 'followup'); - assert.equal((await f.history('single'))[0].values.messages.length, 9); - const proofs = await f.json('/__graph-proof'); - assert.equal(proofs.length, 8); - assert( - proofs.every( - (proof) => - proof.actualCompiledGraph && - proof.networkConnectAttempts === 0 && - proof.titleMessageCallbacks === 0 - ) - ); - const lifetime = await f.json('/__lifetime'); - assert.equal(lifetime.workers, 1); - await f.handle.close(); - assert.throws(() => process.kill(lifetime.workerPid, 0), { code: 'ESRCH' }); - } -); - -test( - 'caught title metadata diagnostics cannot corrupt protocol or canonical success', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.json('/__metadata-failure'); - await f.create('metadata'); - const events = await f.submit('metadata', 'No tools'); - assert.doesNotMatch( - events, - /generate_title|Authored metadata read failure/ - ); - const [saved] = await f.history('metadata'); - assert.deepEqual(saved.next, []); - assert.equal(saved.values.messages.length, 2); - assert.match(saved.values.messages[1].content, /authored plain answer/); - } -); - -test( - 'reset fences held responses and replaces the real worker without leaking old checkpoints', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.create('old'); - await f.json('/__hold-stream'); - const pending = f.submit('old', 'Book UA123.'); - while (!(await f.json('/__lifetime')).activeStream) - await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = (await f.json('/__lifetime')).workerPid; - await f.json('/__reset'); - await pending; - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('new'); - await f.submit('new', 'Book AA404.'); - const [pause] = await f.history('new'); - assert.equal(pause.checkpoint.thread_id, 'new'); - assert.equal(pause.values.messages.length, 4); - assert.equal( - JSON.parse(pause.values.messages[2].content).flight_number, - 'UA123' - ); - assert.notEqual((await f.json('/__lifetime')).workerPid, oldPid); - } -); - -test( - 'closing rejects queued operations and terminates an owned stalled worker', - { timeout: 30000 }, - async (t) => { - let child; - const f = await fixture(t, { - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - await f.create('one'); - await f.create('two'); - const first = f.post('/api/threads/one/runs/stream', { - assistant_id: 'c-tool-calls', - input: { messages: [{ type: 'human', content: 'Hello', id: 'one' }] }, - }); - const second = f.post('/api/threads/two/runs/stream', { - assistant_id: 'c-tool-calls', - input: { messages: [{ type: 'human', content: 'Hello', id: 'two' }] }, - }); - while (!child?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const pid = child.pid; - await f.handle.close(); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - } -); - -test( - 'malformed worker output fails closed with no private diagnostic in HTTP', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.write("PRIVATE bad protocol\\n"));', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('bad'); - const response = await f.post('/api/threads/bad/runs/stream', { - assistant_id: 'c-tool-calls', - input: { messages: [{ type: 'human', content: 'Hello', id: 'bad' }] }, - }); - assert.equal(response.status, 500); - assert.doesNotMatch(await response.text(), /PRIVATE/); - } -); - -test( - 'stdout EOF rejects pending work promptly even when the worker stays alive', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data", () => process.stdout.end()); setInterval(() => {}, 1000);', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - await f.create('eof'); - const pending = f.post('/api/threads/eof/runs/stream', { - assistant_id: 'c-tool-calls', - input: { messages: [{ type: 'human', content: 'Hello', id: 'eof' }] }, - }); - let timer; - const result = await Promise.race([ - pending.then((response) => response.status), - new Promise((resolve) => { - timer = setTimeout(() => resolve('still pending'), 1000); - }), - ]); - clearTimeout(timer); - assert.equal(result, 500); - } -); - -test( - 'reset rejects pending worker work and admits only a fresh real graph epoch', - { timeout: 30000 }, - async (t) => { - let first, - calls = 0; - const f = await fixture(t, { - spawnWorker: () => { - if (++calls === 1) - return (first = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(() => {}, 1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )); - return spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/tool-calls/python', - 'python', - 'scripts/react-cockpit/chat-tool-calls-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - }, - }); - await f.create('old-pending'); - const pending = f.post('/api/threads/old-pending/runs/stream', { - assistant_id: 'c-tool-calls', - input: { - messages: [{ type: 'human', content: 'Book UA123.', id: 'old' }], - }, - }); - while (!first?.pid) await new Promise((resolve) => setTimeout(resolve, 10)); - const oldPid = first.pid; - await f.json('/__reset'); - assert.equal((await pending).status, 500); - assert.throws(() => process.kill(oldPid, 0), { code: 'ESRCH' }); - await f.create('fresh'); - await f.submit('fresh', 'Book AA404.'); - const [pause] = await f.history('fresh'); - assert.equal(pause.values.messages.length, 4); - assert.equal( - JSON.parse(pause.values.messages[2].content).flight_number, - 'UA123' - ); - const requests = await f.json('/__requests'); - assert.equal(requests.length, 3); - assert(requests.every((item) => !item.path.includes('old-pending'))); - assert.equal((await f.json('/__lifetime')).pendingOperations, 0); - } -); diff --git a/scripts/react-cockpit/chat-tool-calls-wire.py b/scripts/react-cockpit/chat-tool-calls-wire.py deleted file mode 100644 index e8fad06db..000000000 --- a/scripts/react-cockpit/chat-tool-calls-wire.py +++ /dev/null @@ -1,91 +0,0 @@ -"""Persistent, network-free compiled graph worker with protocol-only stdout.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/tool-calls/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, model_factory -from langgraph.checkpoint.memory import InMemorySaver - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if hasattr(value, "id") and hasattr(value, "value"): - return {"id": value.id, "value": value.value} - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": state.config["configurable"], - "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - - -async def main(): - client = AuthoredClient() - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_tool_calls_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_tool_calls_graph().builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - if request["op"] == "configure": - client.threads.fail = request.get("failMetadata", False) - result = {} - else: - config = {"configurable": {"thread_id": request["threadId"]}} - if request["op"] == "history": - result = {"history": [checkpoint(state) for state in graph.get_state_history(config)]} - else: - if request["op"] == "submit": - value = {"messages": request["messages"]} - else: - raise ValueError("Unsupported graph operation") - events = [] - async for kind, event in graph.astream(value, config, stream_mode=["messages", "values"]): - if kind == "messages" and event[1].get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into the answer stream") - events.append([kind, event]) - result = {"events": events, "state": checkpoint(graph.get_state(config))} - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0, - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 5ff3ea7a2..5dbc5389f 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -98,16 +98,19 @@ export function reactCockpitConfiguration(topic = 'streaming') { return Object.freeze({ topic: 'subagents', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/subagents/react', base: '/chat/subagents/react/', port: 4624, project: 'cockpit-chat-subagents-react', + backend: aimockLanggraph('chat/subagents', 4624), }); if (topic === 'chat-tool-calls') return Object.freeze({ topic: 'tool-calls', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/tool-calls/react', base: '/chat/tool-calls/react/', port: 4623, project: 'cockpit-chat-tool-calls-react', + backend: aimockLanggraph('chat/tool-calls', 4623), }); if (topic === 'chat-interrupts') return Object.freeze({ topic: 'interrupts', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/interrupts/react', base: '/chat/interrupts/react/', port: 4622, project: 'cockpit-chat-interrupts-react', + backend: aimockLanggraph('chat/interrupts', 4622), }); if (topic === 'chat-input') return Object.freeze({ diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 66b29571c..73d600970 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -8,17 +8,33 @@ * cannot produce on demand: * * GET /__requests every /api and /developer-api request, in order + * POST /__ready 200 once `langgraph dev` answers /ok (graph loaded; + * with `serve.mjs --backend`, after its lazy start) * GET /__lifetime { activeStream, activeHistory, openResponses } * GET /__captures per finished run: forwarded SSE text, saved * messages/next from the real server, title * before and after the run - * POST /__ready 200 once the backend is up (with `serve.mjs - * --backend`, after its lazy start) * POST /__reset forget requests, drop held responses, clear modes * POST /__fail-create next thread creation answers 500 without forwarding - * POST /__history/ safe | hold | changed | missing | pending | failed + * POST /__history/ safe | hold | changed | missing | pending | failed, + * or tool evidence: args | result | name | ids | + * interrupt | subgraph, or approval evidence: + * mismatch | unsupported * POST /__release-history finish a held history read - * POST /__hold/stream hold the next run stream; body { afterText? } + * POST /__hold/stream hold the next run stream; body { afterText? } holds + * after the first AI message chunk (or once the AI text + * contains afterText); { beforeToolResult: true } + * holds just before the first tool result event; + * { afterChildMessage: true } holds after the first + * subgraph (namespaced `messages|…`) text chunk + * POST /__child/ role | task | error: before the next run's first + * child `values|…` event, inject a malformed copy + * (unsupported subagent_type, null task_description) + * or a child `error|…` event carrying a private + * diagnostic. Injected data, as the authored + * subagents fixture did. + * POST /__fail-stream next run stream answers 500 without forwarding + * (the graph worker is unavailable) * POST /__release flush and resume a held run stream * POST /__inject/reasoning body { text }: until reset, every AI message the * browser receives (stream, history, state) carries @@ -32,7 +48,13 @@ */ import { request as httpRequest } from 'node:http'; -const HISTORY_MODES = ['safe', 'hold', 'changed', 'missing', 'pending', 'failed']; +// Saved-evidence rewrites of the latest history entry. Tool modes mirror the +// authored tool-calls fixture: corrupt one piece of tool evidence each. +const HISTORY_MODES = [ + 'safe', 'hold', 'changed', 'missing', 'pending', 'failed', + 'args', 'result', 'name', 'ids', 'interrupt', 'subgraph', + 'mismatch', 'unsupported', +]; const HOP_BY_HOP = new Set(['connection', 'keep-alive', 'transfer-encoding', 'content-length', 'host']); const json = (response, value, status = 200) => { @@ -80,6 +102,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const target = new URL(upstream); let requests = []; let failCreate = false; + let failStream = false; + let childFault = null; let historyMode = 'safe'; let holdNextStream = null; let heldStream; @@ -133,6 +157,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { reasoning = null; decoratedChunks.clear(); failCreate = false; + failStream = false; + childFault = null; historyMode = 'safe'; holdNextStream = null; } @@ -165,21 +191,27 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { ? input.map((state) => ({ ...state, values: decorateValues(state.values) })) : input; if (!Array.isArray(states) || states.length === 0) return states; - const [latest, ...rest] = states; - let messages = latest.values?.messages ?? []; - if (historyMode === 'changed') - messages = messages.map((m, i) => - i === messages.length - 1 ? { ...m, content: 'Changed saved answer' } : m, - ); - if (historyMode === 'missing') messages = messages.slice(0, -1); - return [ - { - ...latest, - values: { ...latest.values, messages }, - next: historyMode === 'pending' ? ['generate'] : latest.next, - }, - ...rest, - ]; + const latest = structuredClone(states[0]); + const messages = latest.values?.messages ?? []; + const assistant = messages.find((m) => m.tool_calls?.length); + const result = messages.find((m) => m.type === 'tool'); + if (historyMode === 'changed' && messages.length) + messages.at(-1).content = 'Changed saved answer'; + if (historyMode === 'missing') messages.pop(); + if (historyMode === 'pending') latest.next = ['unexpected']; + if (historyMode === 'args' && assistant) + assistant.tool_calls[0].args = { flight_number: 'Changed' }; + if (historyMode === 'result' && result) result.content = 'Changed tool observation'; + if (historyMode === 'name' && result) result.name = 'unsupported'; + if (historyMode === 'ids' && result) result.tool_call_id = 'orphan'; + if (historyMode === 'interrupt') + latest.tasks = [{ id: 'unexpected', name: 'tools', interrupts: [{ id: 'unexpected', value: { type: 'unexpected' } }] }]; + const approval = latest.tasks?.[0]?.interrupts?.[0]?.value; + if (historyMode === 'mismatch' && approval?.flight) approval.flight.flight_number = 'DL999'; + if (historyMode === 'unsupported' && approval) approval.type = 'unsupported'; + if (historyMode === 'subgraph' && latest.checkpoint) + latest.checkpoint = { ...latest.checkpoint, checkpoint_ns: 'foreign:checkpoint' }; + return [latest, ...states.slice(1)]; } async function proxyHistory(request, response, path, body) { @@ -219,6 +251,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { async function proxyStream(request, response, path, body) { const hold = holdNextStream; holdNextStream = null; + let fault = childFault; + childFault = null; const threadId = /\/threads\/([^/]+)\/runs\/stream/.exec(path)?.[1]; const titleBefore = threadId ? await threadTitle(threadId) : null; const upstreamResponse = await send(request.method, path, request.headers, body); @@ -279,11 +313,38 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const end = match.index + match[0].length; const raw = pending.slice(0, end); pending = pending.slice(end); + const eventName = /^event: *(.*)$/m.exec(raw)?.[1]?.trim() ?? ''; + if (fault && eventName.startsWith('values|')) { + const namespace = eventName.slice('values'.length); + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + const value = JSON.parse(dataLine.slice(5)); + const injected = fault === 'error' + ? ['error' + namespace, { error: 'AuthoredChildError', message: 'PRIVATE_CHILD_DIAGNOSTIC' }] + : ['values' + namespace, fault === 'role' + ? { ...value, subagent_type: 'unsupported-specialist' } + : { ...value, task_description: null }]; + response.write(`event: ${injected[0]}\ndata: ${JSON.stringify(injected[1])}\n\n`); + fault = null; + } + if (armed && hold.beforeToolResult && /"type": ?"tool"/.test(raw)) { + // Hold this event (the first carrying a tool result) and everything after. + armed = false; + holding = true; + heldStream = held; + pending = raw + pending; + break; + } response.write(decorateEvent(raw)); + if (armed && hold.afterChildMessage && eventName.startsWith('messages|') && aiText(raw)) { + armed = false; + holding = true; + heldStream = held; + continue; + } const delta = aiText(raw); if (delta === null) continue; text += delta; - if (armed && delta && (!hold.afterText || text.includes(hold.afterText))) { + if (armed && !hold.beforeToolResult && !hold.afterChildMessage && (hold.afterText ? text.includes(hold.afterText) : true)) { armed = false; holding = true; heldStream = held; @@ -315,8 +376,6 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { })], '/__reset': ['POST', () => { reset(); return {}; }], '/__fail-create': ['POST', () => { failCreate = true; return {}; }], - // Waits for the backend (with `serve.mjs --backend`, its lazy start). - '/__ready': ['POST', async () => { await ready(); return {}; }], '/__release': ['POST', () => { heldStream?.release(); return {}; }], '/__release-history': ['POST', () => { const held = heldHistory; @@ -325,10 +384,34 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return {}; }], '/__inject/reasoning': ['POST', (body) => { reasoning = String(body.text ?? ''); return {}; }], - '/__hold/stream': ['POST', (body) => { holdNextStream = { afterText: body.afterText ?? null }; return {}; }], + '/__hold/stream': ['POST', (body) => { + holdNextStream = { + afterText: body.afterText ?? null, + beforeToolResult: body.beforeToolResult === true, + afterChildMessage: body.afterChildMessage === true, + }; + return {}; + }], + '/__fail-stream': ['POST', () => { failStream = true; return {}; }], + '/__ready': ['POST', async () => { + await ready(); + const answer = await send('GET', '/ok', { accept: 'application/json' }, Buffer.alloc(0)); + await readAll(answer); + if (answer.statusCode !== 200) throw new Error('LangGraph is not ready'); + return {}; + }], }; return async function handle(request, response, pathname) { + if (pathname.startsWith('/__child/')) { + const mode = pathname.slice('/__child/'.length); + if (!['role', 'task', 'error'].includes(mode)) json(response, {}, 400); + else { + childFault = mode; + json(response, {}); + } + return true; + } if (pathname.startsWith('/__history/')) { const mode = pathname.slice('/__history/'.length); if (!HISTORY_MODES.includes(mode)) json(response, {}, 400); @@ -345,7 +428,11 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return true; } const raw = (await readAll(request)).toString('utf8'); - json(response, await run(raw ? JSON.parse(raw) : {})); + try { + json(response, await run(raw ? JSON.parse(raw) : {})); + } catch { + json(response, { error: 'Local proxy control failed' }, 500); + } return true; } const prefix = pathname.startsWith('/api/') @@ -377,8 +464,12 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } if (request.method === 'POST' && /\/threads\/[^/]+\/history$/.test(pathname)) await proxyHistory(request, response, path, body); - else if (request.method === 'POST' && /\/runs\/stream$/.test(pathname)) - await proxyStream(request, response, path, body); + else if (request.method === 'POST' && /\/runs\/stream$/.test(pathname)) { + if (failStream) { + failStream = false; + json(response, { error: 'PRIVATE graph worker failure' }, 500); + } else await proxyStream(request, response, path, body); + } else if (reasoning && request.method === 'GET' && /\/threads\/[^/]+\/state$/.test(pathname)) { const upstreamResponse = await send(request.method, path, request.headers, body); const raw = await readAll(upstreamResponse); diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index dbaf8b6ff..1c06dfe59 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -21,9 +21,6 @@ import { createDeploymentRuntimeFixture } from './deployment-runtime-fixture.mjs import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; -import { createChatInterruptsFixture } from './chat-interrupts-fixture.mjs'; -import { createChatToolCallsFixture } from './chat-tool-calls-fixture.mjs'; -import { createChatSubagentsFixture } from './chat-subagents-fixture.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; import { createChatTimelineFixture } from './chat-timeline-fixture.mjs'; import { createChatThreadsFixture } from './chat-threads-fixture.mjs'; @@ -76,9 +73,6 @@ const deepAgentsPlanningFixture = configuration.library === 'deep-agents' && con let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; const chatTimelineFixture = configuration.library === 'chat' && configuration.topic === 'timeline' ? createChatTimelineFixture() : null; const chatThreadsFixture = configuration.library === 'chat' && configuration.topic === 'threads' ? createChatThreadsFixture() : null; -const chatSubagentsFixture = configuration.library === 'chat' && configuration.topic === 'subagents' ? createChatSubagentsFixture() : null; -const chatToolCallsFixture = configuration.library === 'chat' && configuration.topic === 'tool-calls' ? createChatToolCallsFixture() : null; -const chatInterruptsFixture = configuration.library === 'chat' && configuration.topic === 'interrupts' ? createChatInterruptsFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by // the Playwright global setup); this server proxies /api to it. // `--backend` (Website previews) starts that backend here, lazily on the first @@ -254,9 +248,6 @@ const server = createServer(async (request, response) => { if (chatGenerativeUiFixture && (await chatGenerativeUiFixture(request, response, pathname))) return; if (chatTimelineFixture && (await chatTimelineFixture(request, response, pathname))) return; if (chatThreadsFixture && (await chatThreadsFixture(request, response, pathname))) return; - if (chatSubagentsFixture && (await chatSubagentsFixture(request, response, pathname))) return; - if (chatToolCallsFixture && (await chatToolCallsFixture(request, response, pathname))) return; - if (chatInterruptsFixture && (await chatInterruptsFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; if (configuration.adapter === 'none') { if (!['GET', 'HEAD'].includes(request.method)) { @@ -471,7 +462,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture || chatInterruptsFixture || chatToolCallsFixture || chatSubagentsFixture || chatThreadsFixture || chatTimelineFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture || chatThreadsFixture || chatTimelineFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -484,7 +475,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture ?? chatInterruptsFixture ?? chatToolCallsFixture ?? chatSubagentsFixture ?? chatThreadsFixture ?? chatTimelineFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture ?? chatThreadsFixture ?? chatTimelineFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 1299de23fcb24143cd1a3e7a1432d0cbb461d81e Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 15:02:05 -0800 Subject: [PATCH 05/41] test(cockpit): run React chat threads/timeline e2e on aimock Batch 3 of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md). - chat/threads and chat/timeline React e2e replay the shared Angular fixtures through a real `langgraph dev`; titles come from the real generate_title node, checkpoints/history/state from the real server. - Proxy: thread-metadata title modes and /__hold/title, checkpoint-state modes and /__hold/state, one-shot /__hold/history, mapped checkpoint representation, foreign/failure history modes. /__captures no longer delays the stream end (the browser closed it first); it awaits captures in flight instead. - Timeline stream evidence comes from /__captures[].sse: DevTools cannot return a streamed body the client closes. - Delete both fixture servers, wire workers and fixture specs. - chat/debug moves to batch 3b: its fixture also backs the packed-consumer native-session proof. Co-Authored-By: Claude Opus 5.5 --- .../chat/theming/react/e2e/theming.spec.ts | 2 +- .../angular/e2e/fixtures/c-threads.json | 14 + .../threads/react/e2e/global-setup-impl.ts | 6 + .../threads/react/e2e/playwright.config.ts | 3 + .../chat/threads/react/e2e/threads.spec.ts | 64 +- cockpit/chat/threads/react/project.json | 17 +- .../angular/e2e/fixtures/c-timeline.json | 31 + .../timeline/react/e2e/global-setup-impl.ts | 6 + .../timeline/react/e2e/playwright.config.ts | 3 + .../chat/timeline/react/e2e/timeline.spec.ts | 90 +- cockpit/chat/timeline/react/project.json | 17 +- .../2026-11-18-react-cockpit-aimock-design.md | 6 +- scripts/cockpit-frontend-matrix.spec.mjs | 8 + .../react-cockpit/chat-threads-fixture.mjs | 473 -------- .../chat-threads-fixture.spec.mjs | 661 ---------- scripts/react-cockpit/chat-threads-wire.py | 94 -- .../react-cockpit/chat-timeline-fixture.mjs | 624 ---------- .../chat-timeline-fixture.spec.mjs | 1071 ----------------- scripts/react-cockpit/chat-timeline-wire.py | 136 --- scripts/react-cockpit/configuration.mjs | 2 + scripts/react-cockpit/langgraph-proxy.mjs | 208 +++- scripts/react-cockpit/serve.mjs | 10 +- 22 files changed, 346 insertions(+), 3200 deletions(-) create mode 100644 cockpit/chat/threads/react/e2e/global-setup-impl.ts create mode 100644 cockpit/chat/timeline/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/chat-threads-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-threads-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-threads-wire.py delete mode 100644 scripts/react-cockpit/chat-timeline-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-timeline-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-timeline-wire.py diff --git a/cockpit/chat/theming/react/e2e/theming.spec.ts b/cockpit/chat/theming/react/e2e/theming.spec.ts index 7a6858c4b..6094a9387 100644 --- a/cockpit/chat/theming/react/e2e/theming.spec.ts +++ b/cockpit/chat/theming/react/e2e/theming.spec.ts @@ -74,7 +74,7 @@ test.afterEach(async ({ page, request }, info) => { await page.goto('about:blank'); await expect .poll(async () => (await request.get('/__lifetime')).json()) - .toEqual({ activeStream: false, activeHistory: false, openResponses: 0 }); + .toMatchObject({ activeStream: false, activeHistory: false, openResponses: 0 }); for (const path of ['/__requests', '/__captures', '/__lifetime']) { const body = JSON.stringify( await (await request.get(path)).json(), diff --git a/cockpit/chat/threads/angular/e2e/fixtures/c-threads.json b/cockpit/chat/threads/angular/e2e/fixtures/c-threads.json index 8c430436e..cd1f14d18 100644 --- a/cockpit/chat/threads/angular/e2e/fixtures/c-threads.json +++ b/cockpit/chat/threads/angular/e2e/fixtures/c-threads.json @@ -1,10 +1,24 @@ { "fixtures": [ + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Aviation snippet title" } + }, { "match": { "userMessage": "Hello" }, "response": { "content": "Hi! I'm the chat-threads demo. Switch between threads using the sidebar list to see independent conversation contexts." } + }, + { + "match": { "userMessage": "fail-stream" }, + "response": { "content": "A partial answer that never finishes streaming." }, + "chunkSize": 3, + "truncateAfterChunks": 4 + }, + { + "match": { "userMessage": "" }, + "response": { "content": "A saved answer.\n\n```typescript\nconst answer = 42;\n```" } } ] } diff --git a/cockpit/chat/threads/react/e2e/global-setup-impl.ts b/cockpit/chat/threads/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..08cd4dc4c --- /dev/null +++ b/cockpit/chat/threads/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/threads', port: 4625 }); diff --git a/cockpit/chat/threads/react/e2e/playwright.config.ts b/cockpit/chat/threads/react/e2e/playwright.config.ts index 75c17c75e..c8f85db1d 100644 --- a/cockpit/chat/threads/react/e2e/playwright.config.ts +++ b/cockpit/chat/threads/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4625', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/threads/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-threads', cwd: '../../../../..', diff --git a/cockpit/chat/threads/react/e2e/threads.spec.ts b/cockpit/chat/threads/react/e2e/threads.spec.ts index f08d6a750..fb65a76cc 100644 --- a/cockpit/chat/threads/react/e2e/threads.spec.ts +++ b/cockpit/chat/threads/react/e2e/threads.spec.ts @@ -4,9 +4,11 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; -import { createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; +// Backend: aimock replays cockpit/chat/threads/angular/e2e/fixtures (shared +// with the Angular example) into a real `langgraph dev`, whose generate_title +// node writes the real thread title. The React static server proxies /api to +// it, recording requests and injecting holds, title and history rewrites, and +// failures (scripts/react-cockpit/langgraph-proxy.mjs). type Wire = { path: string; method: string; @@ -59,21 +61,6 @@ async function newConversation(page: Page) { test.beforeEach(async ({ request }) => { expect((await request.post('/__reset')).status()).toBe(200); }); -test.afterEach(async ({ request }) => { - const proofs = await (await request.get('/__graph-proof')).json(); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - sourceSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/src/graph.py'))) - .digest('hex'), - lockSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/uv.lock'))) - .digest('hex'), - }); -}); test('installed SDK switches A/B/A and extends only the selected saved conversation', async ({ page, request, @@ -85,13 +72,13 @@ test('installed SDK switches A/B/A and extends only the selected saved conversat expect(await (await request.get('/__requests')).json()).toEqual([]); await submit(page, 'A first'); await saved(page); - await expect(picker(page).first()).toHaveText('Authored conversation title'); + await expect(picker(page).first()).toHaveText('Aviation snippet title'); await box(page).fill('A retained draft'); await newConversation(page); await expect(box(page)).toHaveValue(''); await submit(page, 'B first'); await saved(page); - await expect(picker(page).nth(1)).toHaveText('Authored conversation title'); + await expect(picker(page).nth(1)).toHaveText('Aviation snippet title'); await box(page).fill('B retained draft'); await picker(page).nth(0).click(); await loaded(page); @@ -118,7 +105,7 @@ test('installed SDK switches A/B/A and extends only the selected saved conversat await expect(conversation(page)).toContainText('B first'); await expect(conversation(page)).not.toContainText('A first'); await expect(conversation(page)).not.toContainText( - 'Authored conversation title' + 'Aviation snippet title' ); await expect(picker(page).nth(1)).toHaveAttribute('aria-pressed', 'true'); await expect @@ -307,7 +294,10 @@ test('actual graph failure is generic and never confirms or replays', async ({ await submit(page, 'fail-stream'); await blocked(page); await expect(page.locator('body')).not.toContainText('PRIVATE'); - await expect(page.locator('body')).not.toContainText('Authored stream failure'); + // aimock cuts the answer stream mid-way (truncateAfterChunks), so the model + // client raises inside the real graph; its transport error stays private. + await expect(page.locator('body')).not.toContainText('peer closed'); + await expect(page.locator('body')).not.toContainText('Connection error'); const wire: Wire[] = await (await request.get('/__requests')).json(); expect( wire.filter((item) => item.path.endsWith('/runs/stream')) @@ -318,7 +308,7 @@ test('held title does not block a new conversation and cannot relabel it after r page, request, }) => { - await request.post('/__hold-title'); + await request.post('/__hold/title'); await page.goto(base); await submit(page, 'A first'); await saved(page); @@ -337,7 +327,7 @@ test('held title does not block a new conversation and cannot relabel it after r await request.post('/__release-title'); await submit(page, 'B first'); await saved(page); - await expect(picker(page).nth(1)).toHaveText('Authored conversation title'); + await expect(picker(page).nth(1)).toHaveText('Aviation snippet title'); await expect(picker(page).first()).toHaveText('Conversation 1'); }); test('a late title from selected A cannot relabel B after switching', async ({ @@ -347,13 +337,13 @@ test('a late title from selected A cannot relabel B after switching', async ({ await page.goto(base); await submit(page, 'A first'); await saved(page); - await expect(picker(page).first()).toHaveText('Authored conversation title'); + await expect(picker(page).first()).toHaveText('Aviation snippet title'); await newConversation(page); await submit(page, 'B first'); await saved(page); - await expect(picker(page).nth(1)).toHaveText('Authored conversation title'); + await expect(picker(page).nth(1)).toHaveText('Aviation snippet title'); await request.post('/__title/literal'); - await request.post('/__hold-title'); + await request.post('/__hold/title'); await picker(page).first().click(); await loaded(page); await expect @@ -366,8 +356,8 @@ test('a late title from selected A cannot relabel B after switching', async ({ await loaded(page); await request.post('/__release-title'); await expect(picker(page).nth(1)).toHaveAttribute('aria-pressed', 'true'); - await expect(picker(page).nth(1)).toHaveText('Authored conversation title'); - await expect(picker(page).first()).toHaveText('Authored conversation title'); + await expect(picker(page).nth(1)).toHaveText('Aviation snippet title'); + await expect(picker(page).first()).toHaveText('Aviation snippet title'); await expect(conversation(page)).toContainText('B first'); await expect(conversation(page)).not.toContainText('A first'); }); @@ -378,7 +368,7 @@ test('failed metadata refresh retains the last valid title', async ({ await page.goto(base); await submit(page, 'First'); await saved(page); - await expect(picker(page).first()).toHaveText('Authored conversation title'); + await expect(picker(page).first()).toHaveText('Aviation snippet title'); await request.post('/__title/titlefailure'); await submit(page, 'Second'); await saved(page); @@ -390,7 +380,7 @@ test('failed metadata refresh retains the last valid title', async ({ ).length ) .toBe(2); - await expect(picker(page).first()).toHaveText('Authored conversation title'); + await expect(picker(page).first()).toHaveText('Aviation snippet title'); }); test('literal title markup remains button text', async ({ page, request }) => { await request.post('/__title/literal'); @@ -433,7 +423,7 @@ test('Stop fences held streaming and preserves other records', async ({ await submit(page, 'A first'); await saved(page); await newConversation(page); - await request.post('/__hold-stream'); + await request.post('/__hold/stream'); await submit(page, 'B interrupted'); await expect .poll( @@ -458,7 +448,9 @@ for (const kind of ['history', 'title', 'stream']) { page, request, }) => { - await request.post('/__hold-' + kind); + await request.post( + kind === 'history' ? '/__history/hold' : '/__hold/' + kind + ); await page.goto(base); await submit(page, 'First'); const field = 'active' + kind[0].toUpperCase() + kind.slice(1); @@ -487,11 +479,11 @@ test('worker failure stays generic and is not retried', async ({ page, request, }) => { - await request.post('/__worker-failure'); + await request.post('/__fail-stream'); await page.goto(base); await submit(page, 'First'); await blocked(page); - await expect(page.locator('body')).not.toContainText('Graph worker'); + await expect(page.locator('body')).not.toContainText('PRIVATE'); const wire: Wire[] = await (await request.get('/__requests')).json(); expect( wire.filter((item) => item.path.endsWith('/runs/stream')) @@ -523,7 +515,7 @@ test('developer endpoint credentials stay in headers for creation, stream, histo await expect(frame.getByRole('status')).toHaveText('Response saved.'); await expect( frame.getByRole('navigation', { name: 'Conversations' }).getByRole('button') - ).toHaveText('Authored conversation title'); + ).toHaveText('Aviation snippet title'); const wire: Wire[] = await (await request.get('/__requests')).json(); expect(wire).toHaveLength(5); expect( diff --git a/cockpit/chat/threads/react/project.json b/cockpit/chat/threads/react/project.json index c4271a580..610dd161f 100644 --- a/cockpit/chat/threads/react/project.json +++ b/cockpit/chat/threads/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/threads/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-threads-angular", "cockpit-chat-threads-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-threads-python"] }], "options": { "config": "cockpit/chat/threads/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-threads-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-threads-fixture.spec.mjs" - } } } } diff --git a/cockpit/chat/timeline/angular/e2e/fixtures/c-timeline.json b/cockpit/chat/timeline/angular/e2e/fixtures/c-timeline.json index 23c796efc..8aab41cb1 100644 --- a/cockpit/chat/timeline/angular/e2e/fixtures/c-timeline.json +++ b/cockpit/chat/timeline/angular/e2e/fixtures/c-timeline.json @@ -1,10 +1,41 @@ { "fixtures": [ + { + "match": { + "systemMessage": "In 3-5 words, summarize", + "userMessage": "(title fails)" + }, + "response": { + "error": { + "message": "PRIVATE title model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Aviation snippet title" } + }, { "match": { "userMessage": "Hello" }, "response": { "content": "Hi! I'm the chat-timeline demo. Each message creates a checkpoint you can scrub with the timeline slider in the sidebar." } + }, + { + "match": { "userMessage": "Title optional" }, + "response": { "content": "Answer: Title optional" } + }, + { + "match": { "userMessage": "fail-stream" }, + "response": { "content": "A partial answer that never finishes streaming." }, + "chunkSize": 3, + "truncateAfterChunks": 4 + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Answer: saved." } } ] } diff --git a/cockpit/chat/timeline/react/e2e/global-setup-impl.ts b/cockpit/chat/timeline/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..c4abace62 --- /dev/null +++ b/cockpit/chat/timeline/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/timeline', port: 4626 }); diff --git a/cockpit/chat/timeline/react/e2e/playwright.config.ts b/cockpit/chat/timeline/react/e2e/playwright.config.ts index 1fd4cd3ec..2bc452570 100644 --- a/cockpit/chat/timeline/react/e2e/playwright.config.ts +++ b/cockpit/chat/timeline/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4626', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/timeline/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-timeline', cwd: '../../../../..', diff --git a/cockpit/chat/timeline/react/e2e/timeline.spec.ts b/cockpit/chat/timeline/react/e2e/timeline.spec.ts index 7777abb86..ed98bbe79 100644 --- a/cockpit/chat/timeline/react/e2e/timeline.spec.ts +++ b/cockpit/chat/timeline/react/e2e/timeline.spec.ts @@ -4,9 +4,12 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; -import { createHash } from 'node:crypto'; -import { readFileSync, writeFileSync } from 'node:fs'; -import { resolve } from 'node:path'; +import { writeFileSync } from 'node:fs'; +// Backend: aimock replays cockpit/chat/timeline/angular/e2e/fixtures (shared +// with the Angular example) into a real `langgraph dev`, whose checkpoints, +// history and checkpoint state reads are the real server's. The React static +// server proxies /api to it, recording requests and injecting holds, state and +// history rewrites, and failures (scripts/react-cockpit/langgraph-proxy.mjs). const base = '/chat/timeline/react/'; const box = (page: Page) => page.getByLabel('Message', { exact: true }); const current = (page: Page) => @@ -37,8 +40,8 @@ async function refresh(page: Page) { } const requests = async (request: APIRequestContext) => (await request.get('/__requests')).json(); -const proofs = async (request: APIRequestContext) => - (await request.get('/__graph-proof')).json(); +const captures = async (request: APIRequestContext) => + (await request.get('/__captures')).json(); const browserErrors = new WeakMap(); test.beforeEach(async ({ request, page }) => { expect((await request.post('/__reset')).status()).toBe(200); @@ -60,18 +63,6 @@ test.afterEach(async ({ request, page }) => { item.path.match(/\/runs\/[0-9a-f-]+$/) ? 'GET' : 'POST' ); } - for (const proof of await proofs(request)) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - sourceSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/src/graph.py'))) - .digest('hex'), - lockSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/uv.lock'))) - .digest('hex'), - }); }); test('installed SDK previews A, sends C at tip, forks D from A, and extends fork with E', async ({ page, @@ -79,10 +70,6 @@ test('installed SDK previews A, sends C at tip, forks D from A, and extends fork }, testInfo) => { const errors: string[] = []; page.on('pageerror', (e) => errors.push(e.message)); - const streamed: Promise[] = []; - page.on('response', (response) => { - if (response.url().endsWith('/runs/stream')) streamed.push(response.text()); - }); await page.goto(base); await expect(box(page)).toBeEnabled(); expect(await requests(request)).toEqual([]); @@ -96,16 +83,13 @@ test('installed SDK previews A, sends C at tip, forks D from A, and extends fork ); expect(index).toBeGreaterThanOrEqual(0); const source = history[index].checkpoint; - const before = (await proofs(request)).filter( - (p: { op: string }) => p.op === 'submit' - ).length; + // Previewing a checkpoint reads state only: no graph run. + const before = (await captures(request)).length; await picker(page).nth(index).click(); await expect(preview(page).locator('article')).toHaveCount(2); await expect(current(page).locator('article')).toHaveCount(4); await expect(box(page)).toHaveValue('Retained draft'); - expect( - (await proofs(request)).filter((p: { op: string }) => p.op === 'submit') - ).toHaveLength(before); + expect(await captures(request)).toHaveLength(before); await send(page, 'C tip'); await expect(current(page).locator('article')).toHaveCount(6); await expect(preview(page).locator('article')).toHaveCount(2); @@ -140,7 +124,11 @@ test('installed SDK previews A, sends C at tip, forks D from A, and extends fork r.path.endsWith('/runs/stream') ); expect(runs).toHaveLength(5); - const streamBodies = await Promise.all(streamed); + // The exact SSE each run delivered, as the proxy forwarded it from the real + // server (DevTools cannot return streamed bodies the client closes itself). + const streamBodies: string[] = (await captures(request)).map( + (capture: { sse: string }) => capture.sse.replace(/\r\n/g, '\n') + ); expect(streamBodies).toHaveLength(5); for (let index = 0; index < 5; index++) { expect(runs[index].body.stream_mode).toEqual([ @@ -197,15 +185,15 @@ test('installed SDK previews A, sends C at tip, forks D from A, and extends fork JSON.stringify(wire, null, 2) ); writeFileSync( - testInfo.outputPath('graph-proof.json'), - JSON.stringify(await proofs(request), null, 2) + testInfo.outputPath('run-captures.json'), + JSON.stringify(await captures(request), null, 2) ); await testInfo.attach('request-ledger', { body: JSON.stringify(wire, null, 2), contentType: 'application/json', }); - await testInfo.attach('graph-proof', { - body: JSON.stringify(await proofs(request), null, 2), + await testInfo.attach('run-captures', { + body: JSON.stringify(await captures(request), null, 2), contentType: 'application/json', }); await page.screenshot({ @@ -237,7 +225,7 @@ test('held preview is read-only, cancellation fences its late response, and refr await send(page, 'A'); await send(page, 'B'); await box(page).fill('Kept draft'); - await request.post('/__hold-state'); + await request.post('/__hold/state'); await selectFirst(page); await held(request, 'activeState'); await expect(preview(page).getByRole('status')).toHaveText( @@ -255,7 +243,7 @@ test('held preview is read-only, cancellation fences its late response, and refr await expect(preview(page).locator('article')).toHaveCount(0); await selectFirst(page); await expect(preview(page).locator('article')).toHaveCount(2); - await request.post('/__hold-history'); + await request.post('/__hold/history'); await page .getByRole('button', { name: 'Refresh checkpoints', exact: true }) .click(); @@ -319,7 +307,7 @@ test('history failure and late history after New cannot restore old selection or ).toHaveText('Checkpoint history is unavailable.'); await expect(current(page).locator('article')).toHaveCount(2); await request.post('/__history/safe'); - await request.post('/__hold-history'); + await request.post('/__hold/history'); await page .getByRole('button', { name: 'Refresh checkpoints', exact: true }) .click(); @@ -341,7 +329,7 @@ for (const failure of ['stop', 'failure']) request, }) => { await page.goto(base); - if (failure === 'stop') await request.post('/__hold-stream'); + if (failure === 'stop') await request.post('/__hold/stream'); await box(page).fill(failure === 'stop' ? 'Held' : 'fail-stream'); await page.getByRole('button', { name: 'Send', exact: true }).click(); if (failure === 'stop') { @@ -378,7 +366,7 @@ test('disposing a page with held preview releases it and leaves the replacement }) => { await page.goto(base); await send(page, 'Old'); - await request.post('/__hold-state'); + await request.post('/__hold/state'); await selectFirst(page); await held(request, 'activeState'); await page.goto('about:blank'); @@ -394,17 +382,17 @@ test('disposing a page with held preview releases it and leaves the replacement await expect(preview(page).locator('article')).toHaveCount(0); }); -for (const control of ['metadata-failure', 'title-model-failure']) - test(`optional ${control} preserves graph answer without title message callbacks`, async ({ - page, - request, - }) => { - await request.post('/__' + control); - await page.goto(base); - await send(page, 'Title optional'); - await expect(current(page).locator('article')).toHaveCount(2); - await expect(current(page)).toContainText('Answer: Title optional'); - }); +test('optional title failure preserves the graph answer without title output', async ({ + page, +}) => { + // The title model call for this prompt fails (its own aimock error fixture); + // generate_title swallows it, on the same path as a metadata read failure. + await page.goto(base); + await send(page, 'Title optional (title fails)'); + await expect(current(page).locator('article')).toHaveCount(2); + await expect(current(page)).toContainText('Answer: Title optional'); + await expect(current(page)).not.toContainText('PRIVATE'); +}); test('literal and long content stays contained on desktop and mobile', async ({ page, @@ -476,7 +464,7 @@ test('developer bridge sends credentials only in headers across checkpoint opera JSON.stringify(wire.map((r: { body: unknown }) => r.body)) ).not.toContain('TEST_TIMELINE_KEY'); await expect(frame.locator('body')).not.toContainText('TEST_TIMELINE_KEY'); - await request.post('/__hold-state'); + await request.post('/__hold/state'); await frame .getByRole('button', { name: 'Refresh checkpoints', exact: true }) .click(); @@ -547,8 +535,8 @@ test('mapped HTTP representation carries the complete source unchanged through p JSON.stringify(wire, null, 2) ); writeFileSync( - testInfo.outputPath('mapped-graph-proof.json'), - JSON.stringify(await proofs(request), null, 2) + testInfo.outputPath('mapped-run-captures.json'), + JSON.stringify(await captures(request), null, 2) ); await testInfo.attach('mapped-representation-request-ledger', { body: JSON.stringify(wire, null, 2), diff --git a/cockpit/chat/timeline/react/project.json b/cockpit/chat/timeline/react/project.json index a4917df44..4c86e3d87 100644 --- a/cockpit/chat/timeline/react/project.json +++ b/cockpit/chat/timeline/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/timeline/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-timeline-angular", "cockpit-chat-timeline-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-timeline-python"] }], "options": { "config": "cockpit/chat/timeline/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-timeline-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-timeline-fixture.spec.mjs" - } } } } diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index bf7f377c0..c7a9a589a 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -71,6 +71,9 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | `/__child/` (batch 2) | malformed child `values` / child `error` event | same control on the proxy: injected data, as before | | `/__history/` (batch 2) | corrupted tool or approval evidence | the same rewrites, applied to the real history | | `/__ready` (batch 2) | Python worker loaded | `langgraph dev` answers `/ok` | + | `/__title/`, `/__hold-title` (batch 3) | authored thread metadata | `GET /threads/` rewrites (`missing`, `wrong-thread`, `invalid`, `titlefailure`, `literal`, `long`), with the mode captured when the read arrives; `/__hold/title` | + | `/__state/`, `/__hold-state`, `/__mapped-checkpoints` (batch 3) | authored checkpoint reads | the same rewrites on the real `POST /threads//state/checkpoint`; mapped mode adds the root `checkpoint_map` to history, state and `checkpoints` events | + | `/__metadata-failure` + `/__title-model-failure` (timeline) | two title faults | one test: a per-prompt title-model error. Both faults end on the same swallowed exception in `generate_title`; the metadata read itself runs inside `langgraph dev` and can't be failed from outside | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -158,7 +161,8 @@ example needs that the pilot proxy doesn't yet provide. | 1 (done) | chat/input, chat/theming | `/__captures`, `/__inject/reasoning` (theming) | | | 2 (done) | chat/tool-calls, chat/subagents, chat/interrupts | `/__hold/stream` `beforeToolResult` / `afterChildMessage`, `/__fail-stream`, `/__child/`, tool and approval `/__history` modes, `/__ready` | tool rounds key on aimock `toolCallId`; a mid-stream child model failure is aimock `truncateAfterChunks`; a title (metadata) failure is a per-prompt aimock error fixture (`… (title fails)`) | | 2b | chat/generative-ui | `/__configure` faults that swap graph internals: a throwing KPI tool, malformed trend data, prose injected into the `admit` node's output, partial layouts | aimock can't produce tool-code failures or node-output rewrites, so this needs a decision: move the graph-level faults to Python tests, or keep a small authored layer | -| 3 | chat/threads, chat/timeline, chat/debug | title modes and holds, state and checkpoint holds, mapped checkpoints | history and state rewriting, holds per endpoint | +| 3 (done) | chat/threads, chat/timeline | `/__title/`, `/__hold/title`, `/__state/`, `/__hold/state`, `/__hold/history`, `/__mapped-checkpoints`, `foreign`/`failure` history modes | a mid-stream model failure is aimock `truncateAfterChunks`; stream bodies come from `/__captures[].sse`, because DevTools can't return a streamed body the client closes | +| 3b | chat/debug | `/__hold/current`, `/__hold/checkpoint`, selected-read variants, `captures[].proof.providerVariant` | its fixture also backs the packed-consumer native-session proof (`chat-debug-native-session-probe.mjs`, its own CI step), so it migrates together with that proof | | 4 | langgraph/{streaming, persistence, memory, interrupts, subgraphs, client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-draft`, `/__hold-extraction`, `/__hold-plan`, `/__publish-*`, `/__bad-history`, `/__unsafe-source` | `durable-execution` `/__publish-*` injects authored events that a real graph can't emit, so it keeps a small authored layer on top of the proxy | | 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | | 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index e25f64444..cb8bf138d 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -120,6 +120,10 @@ test('Chat Timeline owns an exact native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('chat-timeline'), { topic: 'timeline', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/timeline/react', base: '/chat/timeline/react/', port: 4626, project: 'cockpit-chat-timeline-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/timeline/python', + fixtures: 'cockpit/chat/timeline/angular/e2e/fixtures', langgraphPort: 5626, + }, }); for (const key of ['timeline', 'chat/timeline', '../chat-timeline']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); @@ -139,6 +143,10 @@ test('Chat Threads owns an exact native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('chat-threads'), { topic: 'threads', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/threads/react', base: '/chat/threads/react/', port: 4625, project: 'cockpit-chat-threads-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/threads/python', + fixtures: 'cockpit/chat/threads/angular/e2e/fixtures', langgraphPort: 5625, + }, }); for (const key of ['threads', 'chat/threads', '../chat-threads']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); diff --git a/scripts/react-cockpit/chat-threads-fixture.mjs b/scripts/react-cockpit/chat-threads-fixture.mjs deleted file mode 100644 index 5f4e43e67..000000000 --- a/scripts/react-cockpit/chat-threads-fixture.mjs +++ /dev/null @@ -1,473 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, workerTimeoutMs) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, workerTimeoutMs); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatThreadsFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/threads/python', - 'python', - 'scripts/react-cockpit/chat-threads-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - let worker, - queue = Promise.resolve(), - epoch = 0, - closed = false, - closing; - let requests = [], - proofs = [], - injections = []; - let historyMode = 'safe', - titleMode = 'safe', - failCreate = false; - const held = new Map(), - holds = new Set(), - threads = new Set(), - runs = new Map(), - operations = new Set(); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - const json = (response, value, status = 200) => { - if (response.destroyed || response.writableEnded) return; - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - function operate(value) { - const captured = epoch, - ticket = {}; - operations.add(ticket); - const result = queue.then(async () => { - if (closed || epoch !== captured) - throw new Error('Stale fixture operation'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value); - if (closed || epoch !== captured) throw new Error('Stale graph response'); - if (!result.proof?.actualCompiledGraph) - throw new Error('Invalid graph proof'); - proofs.push(result.proof); - return result; - }); - queue = result.catch(() => undefined); - return result.finally(() => operations.delete(ticket)); - } - function release(kind) { - const entry = held.get(kind); - held.delete(kind); - entry?.finish(); - } - function hold(kind, response, finish, threadId, run) { - if (response.destroyed || response.writableEnded) return; - injections.push({ kind, mode: 'hold', threadId }); - const previous = held.get(kind); - if (previous?.run) previous.run.status = 'interrupted'; - previous?.response.end(); - const entry = { response, finish, run }; - held.set(kind, entry); - response.on('close', () => { - if (held.get(kind) === entry) { - held.delete(kind); - if (run) run.status = 'interrupted'; - } - }); - } - async function reset() { - ++epoch; - for (const entry of held.values()) entry.response.end(); - held.clear(); - holds.clear(); - const previous = worker; - worker = undefined; - queue = Promise.resolve(); - operations.clear(); - requests = []; - proofs = []; - injections = []; - threads.clear(); - runs.clear(); - historyMode = titleMode = 'safe'; - failCreate = false; - await previous?.close(); - } - const handle = async (request, response, pathname) => { - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pendingOperations: operations.size, - activeStream: held.has('stream'), - activeHistory: held.has('history'), - activeTitle: held.has('title'), - }); - return true; - } - if ( - !pathname.startsWith('/__') && - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - try { - if (closed) throw new Error('Fixture closed'); - if (pathname === '/__reset') { - await reset(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__injections') { - json(response, injections); - return true; - } - if (pathname === '/__fail-create') { - failCreate = true; - json(response, {}); - return true; - } - if (pathname === '/__worker-failure') { - await operate({ op: 'configure' }); - await worker.close(); - json(response, {}); - return true; - } - if (pathname === '/__metadata-failure') { - await operate({ op: 'configure', failMetadata: true }); - json(response, {}); - return true; - } - if ( - ['/__hold-stream', '/__hold-history', '/__hold-title'].includes( - pathname - ) - ) { - holds.add(pathname.slice('/__hold-'.length)); - json(response, {}); - return true; - } - if ( - ['/__release', '/__release-history', '/__release-title'].includes( - pathname - ) - ) { - release( - pathname === '/__release' - ? 'stream' - : pathname.slice('/__release-'.length) - ); - json(response, {}); - return true; - } - if ( - pathname.startsWith('/__history/') || - pathname.startsWith('/__title/') - ) { - const mode = pathname.split('/').at(-1), - history = pathname.startsWith('/__history/'); - const allowed = history - ? [ - 'safe', - 'hold', - 'changed', - 'missing', - 'pending', - 'foreign', - 'failure', - ] - : [ - 'safe', - 'hold', - 'missing', - 'wrong-thread', - 'invalid', - 'titlefailure', - 'literal', - 'long', - ]; - if (!allowed.includes(mode)) { - json(response, {}, 400); - return true; - } - if (history) historyMode = mode; - else titleMode = mode; - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}, - captured = epoch; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if ( - /^\/(api|developer-api)\/threads$/.test(pathname) && - request.method === 'POST' - ) { - if (failCreate) { - failCreate = false; - throw new Error('Creation failed'); - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - Object.keys(body).some( - (key) => !['thread_id', 'if_exists', 'metadata'].includes(key) - ) || - (body.metadata !== undefined && - (body.metadata === null || - typeof body.metadata !== 'object' || - Array.isArray(body.metadata) || - Object.keys(body.metadata).length !== 0)) || - (body.if_exists !== undefined && body.if_exists !== 'raise') - ) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/(api|developer-api)\/threads\/([^/]+)(.*)$/.exec( - pathname - ); - if (!route || !threads.has(route[2])) { - json(response, {}, 404); - return true; - } - const threadId = route[2], - suffix = route[3]; - if (suffix === '/history' && request.method === 'POST') { - const mode = historyMode, - wire = await operate({ op: 'history', threadId }), - history = wire.history, - latest = history[0]; - if (!['safe', 'hold'].includes(mode)) - injections.push({ kind: 'history', mode, threadId }); - if (mode === 'failure') throw new Error('History unavailable'); - if (latest && mode === 'changed') - latest.values.messages.at(-1).content = 'Changed canonical content'; - if (latest && mode === 'missing') latest.values.messages.pop(); - if (latest && mode === 'pending') latest.next = ['unexpected']; - if (latest && mode === 'foreign') - latest.checkpoint.thread_id = randomUUID(); - const finish = () => { - if (captured === epoch && !closed) json(response, history); - }; - if (holds.delete('history') || mode === 'hold') - hold('history', response, finish, threadId); - else finish(); - return true; - } - if (suffix === '' && request.method === 'GET') { - const mode = titleMode, - wire = await operate({ op: 'title', threadId }), - value = wire.thread; - if (!['safe', 'hold'].includes(mode)) - injections.push({ kind: 'title', mode, threadId }); - if (mode === 'titlefailure') throw new Error('Title unavailable'); - if (mode === 'missing') delete value.metadata.title; - if (mode === 'wrong-thread') value.thread_id = randomUUID(); - if (mode === 'invalid') value.metadata.title = { invalid: true }; - if (mode === 'literal') - value.metadata.title = ''; - if (mode === 'long') - value.metadata.title = 'Long authored title '.repeat(30); - const finish = () => { - if (captured === epoch && !closed) json(response, value); - }; - if (holds.delete('title') || mode === 'hold') - hold('title', response, finish, threadId); - else finish(); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - const message = body.input?.messages?.[0]; - if ( - body.assistant_id !== 'c-threads' || - [ - 'command', - 'checkpoint', - 'checkpoint_id', - 'checkpoint_during', - 'multitask_strategy', - ].some((key) => body[key] !== undefined) || - body.input?.messages?.length !== 1 || - message?.type !== 'human' || - typeof message.content !== 'string' || - typeof message.id !== 'string' - ) { - json(response, {}, 400); - return true; - } - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - const runId = randomUUID(), - run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const finish = () => { - if (response.destroyed || captured !== epoch || closed) return; - while (sent < wire.events.length) - response.write(event(...wire.events[sent++])); - run.status = wire.graphFailure - ? 'error' - : wire.state.next.length - ? 'interrupted' - : 'success'; - response.end(); - }; - if (holds.delete('stream')) { - const firstMessage = wire.events.findIndex( - ([kind]) => kind === 'messages' - ); - const limit = - firstMessage < 0 - ? Math.min(2, wire.events.length) - : firstMessage + 1; - while (sent < limit) response.write(event(...wire.events[sent++])); - hold('stream', response, finish, threadId, run); - } else finish(); - return true; - } - if (/^\/runs\/[^/]+$/.test(suffix) && request.method === 'GET') { - const run = runs.get(suffix.split('/').at(-1)); - json( - response, - run?.thread_id === threadId ? run : {}, - run?.thread_id === threadId ? 200 : 404 - ); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => { - if (closing) return closing; - closed = true; - closing = reset(); - return closing; - }; - return handle; -} diff --git a/scripts/react-cockpit/chat-threads-fixture.spec.mjs b/scripts/react-cockpit/chat-threads-fixture.spec.mjs deleted file mode 100644 index c2d3b0712..000000000 --- a/scripts/react-cockpit/chat-threads-fixture.spec.mjs +++ /dev/null @@ -1,661 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { randomUUID, createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { PassThrough } from 'node:stream'; -import { test } from 'node:test'; -const { createChatThreadsFixture } = await import( - './chat-threads-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createChatThreadsFixture, - 'function', - 'Threads fixture is available' - ); - const handle = createChatThreadsFixture(options); - const closedResponses = []; - const server = createServer(async (req, res) => { - res.on('close', () => closedResponses.push(req.url)); - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const request = ( - path, - body, - method = 'POST', - signal = AbortSignal.timeout(15000) - ) => - fetch(`http://127.0.0.1:${server.address().port}${path}`, { - method, - ...(method !== 'GET' ? { body: JSON.stringify(body ?? {}) } : {}), - headers: { 'content-type': 'application/json' }, - signal, - }); - const json = async (path, body, method) => { - const res = await request(path, body, method); - assert.equal(res.status, 200); - return res.json(); - }; - const create = async (id = randomUUID()) => { - await json('/api/threads', { - metadata: {}, - thread_id: id, - if_exists: 'raise', - }); - return id; - }; - const submit = (id, text = 'Hello', humanId = randomUUID()) => - request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-threads', - input: { messages: [{ type: 'human', content: text, id: humanId }] }, - }); - const history = (id) => json(`/api/threads/${id}/history`, { limit: 10 }); - const title = (id) => json(`/api/threads/${id}`, undefined, 'GET'); - const wait = async (key) => { - for (let n = 0; n < 500; n++) { - const state = await json('/__lifetime'); - if (state[key]) return state; - await new Promise((r) => setTimeout(r, 10)); - } - assert.fail('Timed out waiting for ' + key); - }; - return { - handle, - request, - json, - create, - submit, - history, - title, - wait, - closedResponses, - }; -} -function frames(text) { - return text - .trim() - .split('\n\n') - .map((frame) => { - const [kind, data] = frame.split('\n'); - return [kind.slice(7), JSON.parse(data.slice(6))]; - }); -} - -for (const kind of ['history', 'title']) - test(`aborted ${kind} before worker completion does not install a hold`, async (t) => { - let release; - let ready; - const workerReady = new Promise((resolve) => { - ready = resolve; - }); - const f = await fixture(t, { - spawnWorker: () => { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/threads/python', - 'python', - 'scripts/react-cockpit/chat-threads-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - const output = child.stdout, - delayed = new PassThrough(), - buffered = []; - child.stdout = delayed; - output.on('data', (chunk) => { - buffered.push(chunk); - if (Buffer.concat(buffered).includes(10)) ready(); - }); - output.on('end', () => delayed.end()); - release = () => { - for (const chunk of buffered) delayed.write(chunk); - }; - return child; - }, - }); - const id = await f.create(); - await f.json('/__hold-' + kind); - const path = `/api/threads/${id}${kind === 'history' ? '/history' : ''}`; - const controller = new AbortController(); - const pending = f - .request(path, {}, kind === 'history' ? 'POST' : 'GET', controller.signal) - .catch(() => null); - await workerReady; - controller.abort(); - await pending; - for (let i = 0; i < 100 && !f.closedResponses.includes(path); i++) - await new Promise((resolve) => setTimeout(resolve, 10)); - assert( - f.closedResponses.includes(path), - 'Server observed disconnect before graph response' - ); - release(); - for ( - let i = 0; - i < 100 && (await f.json('/__lifetime')).pendingOperations; - i++ - ) - await new Promise((resolve) => setTimeout(resolve, 10)); - const state = await f.json('/__lifetime'); - assert.equal(state.pendingOperations, 0); - assert.equal( - state[kind === 'history' ? 'activeHistory' : 'activeTitle'], - false - ); - assert.deepEqual(await f.json('/__injections'), []); - assert.equal((await f.json('/__graph-proof')).length, 1); - }); - -test('replacing a held stream interrupts its exact run and lets the replacement succeed', async (t) => { - const f = await fixture(t), - a = await f.create(), - b = await f.create(); - await f.json('/__hold-stream'); - const first = await f.submit(a), - firstText = first.text(); - const firstRun = first.headers.get('content-location').split('/').at(-1); - await f.wait('activeStream'); - await f.json('/__hold-stream'); - const second = await f.submit(b), - secondText = second.text(); - const secondRun = second.headers.get('content-location').split('/').at(-1); - await firstText; - assert.equal( - (await f.json(`/api/threads/${a}/runs/${firstRun}`, undefined, 'GET')) - .status, - 'interrupted' - ); - assert.equal( - (await f.json(`/api/threads/${b}/runs/${secondRun}`, undefined, 'GET')) - .status, - 'running' - ); - await f.json('/__release'); - await secondText; - assert.equal( - (await f.json(`/api/threads/${b}/runs/${secondRun}`, undefined, 'GET')) - .status, - 'success' - ); -}); - -test('actual compiled graph retains separate canonical histories, title metadata and unique answers', async (t) => { - const f = await fixture(t), - a = await f.create(), - b = await f.create(); - const first = frames(await (await f.submit(a, 'Same')).text()); - assert(first.some(([kind]) => kind === 'messages')); - assert(first.some(([kind]) => kind === 'values')); - assert(first.some(([kind]) => kind === 'updates')); - assert( - !first.some( - ([kind, data]) => - kind === 'messages' && data[1].langgraph_node === 'generate_title' - ) - ); - const [before] = await f.history(a); - await (await f.submit(b, 'Same')).text(); - await (await f.submit(a, 'Follow up')).text(); - const [after] = await f.history(a), - [other] = await f.history(b); - assert.deepEqual(after.values.messages.slice(0, 2), before.values.messages); - assert.equal(after.values.messages.length, 4); - assert.equal(other.values.messages.length, 2); - assert.notEqual(before.values.messages[1].id, other.values.messages[1].id); - assert.deepEqual(after.next, []); - assert.deepEqual(after.tasks, []); - assert.equal(after.checkpoint.thread_id, a); - assert.equal( - (await f.title(a)).metadata.title, - 'Authored conversation title' - ); - const run = first.find(([kind]) => kind === 'metadata')[1].run_id; - assert.equal( - (await f.json(`/api/threads/${a}/runs/${run}`, undefined, 'GET')).status, - 'success' - ); - assert.equal( - (await f.request(`/api/threads/${b}/runs/${run}`, undefined, 'GET')).status, - 404 - ); - const proofs = await f.json('/__graph-proof'); - assert(proofs.length >= 7); - for (const proof of proofs) { - assert.equal(proof.actualCompiledGraph, true); - assert.equal(proof.networkConnectAttempts, 0); - assert.equal(proof.titleMessageCallbacks, 0); - for (const [key, path] of [ - ['sourceSha256', 'src/graph.py'], - ['lockSha256', 'uv.lock'], - ]) - assert.equal( - proof[key], - createHash('sha256') - .update(readFileSync('cockpit/chat/threads/python/' + path)) - .digest('hex') - ); - } - assert.deepEqual(await f.json('/__injections'), []); -}); - -test('installed SDK creation accepts only empty plain metadata and rejects unknown fields', async (t) => { - const f = await fixture(t), - id = randomUUID(); - const response = await f.request('/api/threads', { - metadata: {}, - thread_id: id, - if_exists: 'raise', - }); - assert.equal(response.status, 200); - assert.deepEqual(await response.json(), { thread_id: id }); - for (const metadata of [null, [], '', false, { title: 'Unsupported' }]) { - assert.equal( - ( - await f.request('/api/threads', { - metadata, - thread_id: randomUUID(), - if_exists: 'raise', - }) - ).status, - 400 - ); - } - assert.equal( - ( - await f.request('/api/threads', { - metadata: {}, - thread_id: randomUUID(), - if_exists: 'raise', - unknown: true, - }) - ).status, - 400 - ); -}); - -test('only confirmed UUID creation and exact supported routes are admitted', async (t) => { - const f = await fixture(t), - id = await f.create(); - assert.equal( - (await f.request('/api/threads', { thread_id: 'not-uuid' })).status, - 400 - ); - assert.equal( - (await f.request('/api/threads', { thread_id: id })).status, - 400 - ); - for (const [path, method, body] of [ - ['/api/threads/search', 'POST', {}], - ['/api/threads', 'GET'], - [`/api/threads/${id}`, 'DELETE'], - [`/api/threads/${id}`, 'PATCH', {}], - [`/api/threads/${id}/runs/x/cancel`, 'POST', {}], - [`/api/threads/${id}/runs/x/stream`, 'GET'], - [`/api/threads/${id}/state`, 'GET'], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { assistant_id: 'wrong', input: { messages: [] } }, - ], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { assistant_id: 'c-threads', command: { resume: true } }, - ], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { - assistant_id: 'c-threads', - checkpoint_id: 'replay', - input: { messages: [{ type: 'human', content: 'Hi', id: 'h' }] }, - }, - ], - ]) - assert((await f.request(path, body, method)).status >= 400, path); - assert((await f.json('/__requests')).length >= 12); -}); - -test('history injections preserve separate actual proof and restore real saved data', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id)).text(); - const original = await f.history(id); - for (const mode of ['changed', 'missing', 'pending', 'foreign', 'failure']) { - await f.json('/__history/' + mode); - const response = await f.request(`/api/threads/${id}/history`, { - limit: 10, - }); - if (mode === 'failure') { - assert.equal(response.status, 500); - continue; - } - const data = await response.json(); - assert.notDeepEqual(data, original); - if (mode === 'foreign') assert.notEqual(data[0].checkpoint.thread_id, id); - } - await f.json('/__history/safe'); - assert.deepEqual(await f.history(id), original); - assert.equal( - (await f.json('/__injections')).filter((i) => i.kind === 'history').length, - 5 - ); - assert( - (await f.json('/__graph-proof')).every( - (p) => !('injectedHistoryFault' in p) - ) - ); -}); - -test('title modes exercise optional metadata independently of canonical messages', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id)).text(); - for (const mode of [ - 'missing', - 'wrong-thread', - 'invalid', - 'titlefailure', - 'literal', - 'long', - ]) { - await f.json('/__title/' + mode); - const response = await f.request(`/api/threads/${id}`, undefined, 'GET'); - if (mode === 'titlefailure') { - assert.equal(response.status, 500); - continue; - } - const data = await response.json(); - if (mode === 'missing') assert.equal(data.metadata.title, undefined); - if (mode === 'wrong-thread') assert.notEqual(data.thread_id, id); - if (mode === 'invalid') - assert.notEqual(typeof data.metadata.title, 'string'); - if (mode === 'literal') - assert.equal(data.metadata.title, ''); - if (mode === 'long') assert(data.metadata.title.length > 80); - } - assert.equal((await f.history(id))[0].values.messages.length, 2); - assert.equal( - (await f.json('/__injections')).filter((i) => i.kind === 'title').length, - 6 - ); -}); - -test('applied one-shot and persistent holds are recorded once beside actual graph proof', async (t) => { - const f = await fixture(t), - id = await f.create(), - expected = []; - await f.json('/__hold-stream'); - assert.deepEqual(await f.json('/__injections'), []); - const stream = f.submit(id).then((response) => response.text()); - await f.wait('activeStream'); - expected.push({ kind: 'stream', mode: 'hold', threadId: id }); - assert.deepEqual(await f.json('/__injections'), expected); - const streamProof = await f.json('/__graph-proof'); - await f.json('/__release'); - await stream; - assert.deepEqual(await f.json('/__graph-proof'), streamProof); - for (const kind of ['history', 'title']) { - await f.json('/__hold-' + kind); - for (let attempt = 0; attempt < 3; attempt++) { - if (attempt === 1) await f.json(`/__${kind}/hold`); - // One-shot plus persistent hold must still record a single applied hold. - if (attempt === 2) await f.json('/__hold-' + kind); - const pending = kind === 'history' ? f.history(id) : f.title(id); - await f.wait(kind === 'history' ? 'activeHistory' : 'activeTitle'); - expected.push({ kind, mode: 'hold', threadId: id }); - assert.deepEqual(await f.json('/__injections'), expected); - const proof = await f.json('/__graph-proof'); - assert( - proof.every((item) => !('mode' in item) && !('injections' in item)) - ); - await f.json('/__release-' + kind); - await pending; - assert.deepEqual(await f.json('/__graph-proof'), proof); - assert.deepEqual(await f.json('/__injections'), expected); - } - await f.json(`/__${kind}/safe`); - } -}); - -test('held stream, history and title release and disconnect without retaining responses', async (t) => { - const f = await fixture(t), - id = await f.create(); - await f.json('/__hold-stream'); - const streamed = f.submit(id).then((r) => r.text()); - await f.wait('activeStream'); - await f.json('/__release'); - assert.match(await streamed, /Authored/); - for (const kind of ['history', 'title']) { - await f.json('/__hold-' + kind); - const pending = kind === 'history' ? f.history(id) : f.title(id); - await f.wait(kind === 'history' ? 'activeHistory' : 'activeTitle'); - await f.json('/__release-' + kind); - await pending; - await f.json('/__hold-' + kind); - const controller = new AbortController(); - const request = f - .request( - `/api/threads/${id}${kind === 'history' ? '/history' : ''}`, - {}, - kind === 'history' ? 'POST' : 'GET', - controller.signal - ) - .catch(() => null); - await f.wait(kind === 'history' ? 'activeHistory' : 'activeTitle'); - controller.abort(); - await request; - for ( - let i = 0; - i < 100 && - (await f.json('/__lifetime'))[ - kind === 'history' ? 'activeHistory' : 'activeTitle' - ]; - i++ - ) - await new Promise((r) => setTimeout(r, 10)); - assert.equal( - (await f.json('/__lifetime'))[ - kind === 'history' ? 'activeHistory' : 'activeTitle' - ], - false - ); - } -}); - -test('reset fences held responses and closes the real process; disposal is idempotent', async (t) => { - const f = await fixture(t), - id = await f.create(); - await f.json('/__hold-stream'); - const pending = f.submit(id).then((r) => r.text()); - const { workerPid } = await f.wait('activeStream'); - await f.json('/__reset'); - await pending; - assert.throws(() => process.kill(workerPid, 0), { code: 'ESRCH' }); - const fresh = await f.create(); - assert.deepEqual(await f.history(fresh), []); - assert.equal( - (await f.request(`/api/threads/${id}`, undefined, 'GET')).status, - 404 - ); - const pid = (await f.json('/__lifetime')).workerPid; - await Promise.all([f.handle.close(), f.handle.close()]); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -}); - -for (const [name, program] of [ - [ - 'malformed', - 'process.stdin.once("data",()=>process.stdout.write("PRIVATE broken\\n"));', - ], - [ - 'unowned', - 'process.stdin.once("data",()=>process.stdout.write(JSON.stringify({requestId:99})+"\\n"));', - ], - [ - 'eof', - 'process.stdin.once("data",()=>process.stdout.end()); setInterval(()=>{},1000);', - ], - ['stall', 'process.stdin.resume(); setInterval(()=>{},1000);'], -]) - test( - name + ' worker fails closed without exposing diagnostics', - async (t) => { - const f = await fixture(t, { - workerTimeoutMs: 150, - spawnWorker: () => - spawn(process.execPath, ['-e', program], { - stdio: ['pipe', 'pipe', 'pipe'], - }), - }), - id = await f.create(); - const response = await f.submit(id); - assert.equal(response.status, 500); - assert.doesNotMatch(await response.text(), /PRIVATE|broken/); - } - ); - -test('close rejects queued worker operations and reset admits a fresh epoch', async (t) => { - let child, - calls = 0; - const f = await fixture(t, { - spawnWorker: () => - ++calls === 1 - ? (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(()=>{},1000);'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )) - : spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/threads/python', - 'python', - 'scripts/react-cockpit/chat-threads-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - const a = await f.create(), - b = await f.create(), - first = f.submit(a), - second = f.submit(b); - await f.wait('pendingOperations'); - await f.json('/__reset'); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - const fresh = await f.create(); - await (await f.submit(fresh)).text(); - assert.equal((await f.history(fresh))[0].values.messages.length, 2); - await f.handle.close(); - assert.equal((await f.submit(fresh)).status, 500); -}); - -test('actual graph failure streams partial evidence, records failed run and retains pending checkpoint', async (t) => { - const f = await fixture(t), - id = await f.create(); - const events = frames(await (await f.submit(id, 'fail-stream')).text()); - assert(events.some(([kind, data]) => kind === 'messages' && data[0].content)); - assert(events.some(([kind]) => kind === 'error')); - const run = events.find(([kind]) => kind === 'metadata')[1].run_id; - assert.equal( - (await f.json(`/api/threads/${id}/runs/${run}`, undefined, 'GET')).status, - 'error' - ); - assert((await f.history(id))[0].next.length > 0); - assert.deepEqual(await f.json('/__injections'), []); -}); - -test('closed chat-threads selection has its own build and server dispatch', async () => { - const { reactCockpitConfiguration } = await import('./configuration.mjs'); - assert.deepEqual(reactCockpitConfiguration('chat-threads'), { - topic: 'threads', - library: 'chat', - adapter: 'langgraph', - appPath: 'cockpit/chat/threads/react', - base: '/chat/threads/react/', - port: 4625, - project: 'cockpit-chat-threads-react', - }); - assert.throws(() => reactCockpitConfiguration('../chat-threads')); - const server = readFileSync('scripts/react-cockpit/serve.mjs', 'utf8'); - assert.match(server, /createChatThreadsFixture/); - assert.match( - server, - /await chatThreadsFixture\(request, response, pathname\)/ - ); - assert.match( - server, - /await\s*\(\s*(?:\w+\s*\?\?\s*)*chatThreadsFixture(?:\s*\?\?\s*\w+)*\s*\)\.close\(\)/ - ); -}); - -test('dispose rejects active and queued stalled work and kills its owned child', async (t) => { - let child; - const f = await fixture(t, { - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(()=>{},1000);'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - const a = await f.create(), - b = await f.create(); - const first = f.submit(a), - second = f.submit(b); - await f.wait('pendingOperations'); - await f.handle.close(); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); -}); - -test('creation and worker failure controls fail privately and reset permits recovery', async (t) => { - const f = await fixture(t), - id = randomUUID(); - await f.json('/__fail-create'); - const failed = await f.request('/api/threads', { - thread_id: id, - if_exists: 'raise', - }); - assert.equal(failed.status, 500); - assert.deepEqual(await failed.json(), { error: 'Graph fixture failed.' }); - assert.equal( - (await f.request(`/api/threads/${id}`, undefined, 'GET')).status, - 404 - ); - await f.create(id); - await f.json('/__worker-failure'); - assert.equal((await f.submit(id)).status, 500); - await f.json('/__reset'); - await f.create(id); - assert.match(await (await f.submit(id)).text(), /Authored/); -}); diff --git a/scripts/react-cockpit/chat-threads-wire.py b/scripts/react-cockpit/chat-threads-wire.py deleted file mode 100644 index 3830de48e..000000000 --- a/scripts/react-cockpit/chat-threads-wire.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Persistent network-free worker executing the actual compiled Threads graph.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/threads/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from providers import AuthoredClient, model_factory -from langgraph.checkpoint.memory import InMemorySaver - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": state.config["configurable"], - "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - - -async def main(): - client = AuthoredClient() - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_threads_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_threads_graph().builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - if request["op"] == "history": - result = {"history": [checkpoint(state) for state in graph.get_state_history(config)]} - elif request["op"] == "title": - result = {"thread": await client.threads.get(request["threadId"])} - elif request["op"] == "configure": - client.threads.fail_read = request.get("failMetadata", False) - result = {} - elif request["op"] == "submit": - events = [] - failure = False - try: - async for kind, event in graph.astream({"messages": request["messages"]}, config, stream_mode=["messages", "values", "updates"]): - if kind == "messages" and event[1].get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into answer stream") - events.append([kind, event]) - except RuntimeError: - failure = True - events.append(["error", {"error": "GraphExecutionError", "message": "The conversation request failed."}]) - result = {"events": events, "state": checkpoint(graph.get_state(config)), "graphFailure": failure} - else: - raise ValueError("Unsupported graph operation") - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "op": request["op"], "threadId": request.get("threadId"), - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0, - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/chat-timeline-fixture.mjs b/scripts/react-cockpit/chat-timeline-fixture.mjs deleted file mode 100644 index 20692052a..000000000 --- a/scripts/react-cockpit/chat-timeline-fixture.mjs +++ /dev/null @@ -1,624 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { readFileSync } from 'node:fs'; -import { randomUUID, createHash } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, workerTimeoutMs) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, workerTimeoutMs); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatTimelineFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/timeline/python', - 'python', - 'scripts/react-cockpit/chat-timeline-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const expectedProof = Object.fromEntries( - [ - ['sourceSha256', 'src/graph.py'], - ['lockSha256', 'uv.lock'], - ].map(([key, path]) => [ - key, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/chat/timeline/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - queue = Promise.resolve(), - epoch = 0, - closed = false, - closing; - let requests = [], - proofs = [], - injections = []; - let historyMode = 'safe', - stateMode = 'safe', - failCreate = false; - const held = new Map(), - holds = new Set(), - threads = new Set(), - runs = new Map(), - operations = new Set(); - const record = (value) => - value !== null && typeof value === 'object' && !Array.isArray(value); - const only = (value, keys) => - record(value) && Object.keys(value).every((key) => keys.includes(key)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - const json = (response, value, status = 200) => { - if (response.destroyed || response.writableEnded) return; - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - function operate(value) { - const captured = epoch, - ticket = {}; - operations.add(ticket); - const result = queue.then(async () => { - if (closed || epoch !== captured) - throw new Error('Stale fixture operation'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value); - if (closed || epoch !== captured) throw new Error('Stale graph response'); - if ( - !result.proof?.actualCompiledGraph || - result.proof.sourceSha256 !== expectedProof.sourceSha256 || - result.proof.lockSha256 !== expectedProof.lockSha256 || - result.proof.networkConnectAttempts !== 0 || - result.proof.titleMessageCallbacks !== 0 || - result.proof.op !== value.op || - result.proof.threadId !== (value.threadId ?? null) - ) - throw new Error('Invalid graph proof'); - proofs.push(result.proof); - return result; - }); - queue = result.catch(() => undefined); - return result.finally(() => operations.delete(ticket)); - } - function release(kind) { - const entry = held.get(kind); - held.delete(kind); - entry?.finish(); - } - function hold(kind, response, finish, threadId, run) { - if (response.destroyed || response.writableEnded) return; - injections.push({ kind, mode: 'hold', threadId }); - const previous = held.get(kind); - if (previous?.run) previous.run.status = 'interrupted'; - previous?.response.end(); - const entry = { response, finish, run }; - held.set(kind, entry); - response.on('close', () => { - if (held.get(kind) === entry) { - held.delete(kind); - if (run) run.status = 'interrupted'; - } - }); - } - async function reset() { - ++epoch; - for (const entry of held.values()) entry.response.end(); - held.clear(); - holds.clear(); - const previous = worker; - worker = undefined; - queue = Promise.resolve(); - operations.clear(); - requests = []; - proofs = []; - injections = []; - threads.clear(); - runs.clear(); - historyMode = stateMode = 'safe'; - failCreate = false; - await previous?.close(); - } - const handle = async (request, response, pathname) => { - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pendingOperations: operations.size, - activeStream: held.has('stream'), - activeHistory: held.has('history'), - activeState: held.has('state'), - }); - return true; - } - if ( - !pathname.startsWith('/__') && - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - try { - if (closed) throw new Error('Fixture closed'); - if (pathname === '/__reset') { - await reset(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__injections') { - json(response, injections); - return true; - } - if (pathname === '/__fail-create') { - failCreate = true; - json(response, {}); - return true; - } - if (pathname === '/__mapped-checkpoints') { - await operate({ op: 'configure', mapped: true }); - json(response, {}); - return true; - } - if (pathname === '/__worker-failure') { - await operate({ op: 'configure' }); - await worker.close(); - json(response, {}); - return true; - } - if ( - pathname === '/__metadata-failure' || - pathname === '/__title-model-failure' - ) { - await operate({ - op: 'configure', - failMetadata: pathname === '/__metadata-failure', - failTitle: pathname === '/__title-model-failure', - }); - json(response, {}); - return true; - } - if ( - ['/__hold-stream', '/__hold-history', '/__hold-state'].includes( - pathname - ) - ) { - holds.add(pathname.slice('/__hold-'.length)); - json(response, {}); - return true; - } - if ( - ['/__release', '/__release-history', '/__release-state'].includes( - pathname - ) - ) { - release( - pathname === '/__release' - ? 'stream' - : pathname.slice('/__release-'.length) - ); - json(response, {}); - return true; - } - if (pathname.startsWith('/__history/')) { - const mode = pathname.split('/').at(-1); - if ( - ![ - 'safe', - 'hold', - 'changed', - 'missing', - 'pending', - 'foreign', - 'failure', - ].includes(mode) - ) { - json(response, {}, 400); - return true; - } - historyMode = mode; - json(response, {}); - return true; - } - if (pathname.startsWith('/__state/')) { - const mode = pathname.split('/').at(-1); - if ( - ![ - 'safe', - 'hold', - 'failure', - 'changed', - 'foreign', - 'malformed', - ].includes(mode) - ) { - json(response, {}, 400); - return true; - } - stateMode = mode; - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}, - captured = epoch; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (!record(body)) { - json(response, {}, 400); - return true; - } - if ( - /^\/(api|developer-api)\/threads$/.test(pathname) && - request.method === 'POST' - ) { - if (failCreate) { - failCreate = false; - throw new Error('Creation failed'); - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - Object.keys(body).some( - (key) => !['thread_id', 'if_exists', 'metadata'].includes(key) - ) || - (body.metadata !== undefined && - (body.metadata === null || - typeof body.metadata !== 'object' || - Array.isArray(body.metadata) || - Object.keys(body.metadata).length !== 0)) || - (body.if_exists !== undefined && body.if_exists !== 'raise') - ) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/(api|developer-api)\/threads\/([^/]+)(.*)$/.exec( - pathname - ); - if (!route || !threads.has(route[2])) { - json(response, {}, 404); - return true; - } - const threadId = route[2], - suffix = route[3]; - async function knownCheckpoint(source) { - if ( - !source || - typeof source !== 'object' || - Array.isArray(source) || - source.thread_id !== threadId || - typeof source.checkpoint_id !== 'string' || - typeof source.checkpoint_ns !== 'string' || - Object.keys(source).some( - (k) => - ![ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ].includes(k) - ) - ) - return false; - if ( - source.checkpoint_map !== undefined && - (!source.checkpoint_map || - typeof source.checkpoint_map !== 'object' || - Array.isArray(source.checkpoint_map) || - Object.entries(source.checkpoint_map).some( - ([ns, id]) => - ns !== source.checkpoint_ns || id !== source.checkpoint_id - )) - ) - return false; - const wire = await operate({ op: 'history', threadId }); - return wire.history.some( - (s) => - s.checkpoint.checkpoint_id === source.checkpoint_id && - s.checkpoint.checkpoint_ns === source.checkpoint_ns && - (s.checkpoint.checkpoint_map === undefined - ? source.checkpoint_map === undefined || - Object.keys(source.checkpoint_map).length === 0 - : JSON.stringify(s.checkpoint.checkpoint_map) === - JSON.stringify(source.checkpoint_map)) - ); - } - if (suffix === '/state/checkpoint' && request.method === 'POST') { - if ( - !only(body, ['checkpoint']) || - !(await knownCheckpoint(body.checkpoint)) - ) { - json(response, {}, 400); - return true; - } - const mode = stateMode; - const wire = await operate({ - op: 'state', - threadId, - checkpoint: body.checkpoint, - }); - const state = wire.state; - if (!['safe', 'hold'].includes(mode)) - injections.push({ kind: 'state', mode, threadId }); - if (mode === 'failure') throw new Error('State unavailable'); - if (mode === 'changed' && state.values.messages?.length) - state.values.messages.at(-1).content = 'Changed canonical content'; - if (mode === 'foreign') state.checkpoint.thread_id = randomUUID(); - if (mode === 'malformed') - state.values.messages = [ - { type: 'human', content: { bad: true }, id: 'malformed' }, - ]; - const finish = () => { - if (captured === epoch && !closed) json(response, state); - }; - if (holds.delete('state') || mode === 'hold') - hold('state', response, finish, threadId); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit']) || - !Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100 - ) { - json(response, {}, 400); - return true; - } - const mode = historyMode, - wire = await operate({ op: 'history', threadId }), - history = wire.history.slice(0, body.limit), - latest = history[0]; - if (!['safe', 'hold'].includes(mode)) - injections.push({ kind: 'history', mode, threadId }); - if (mode === 'failure') throw new Error('History unavailable'); - if (latest && mode === 'changed') - latest.values.messages.at(-1).content = 'Changed canonical content'; - if (latest && mode === 'missing') latest.values.messages.pop(); - if (latest && mode === 'pending') latest.next = ['unexpected']; - if (latest && mode === 'foreign') - latest.checkpoint.thread_id = randomUUID(); - const finish = () => { - if (captured === epoch && !closed) json(response, history); - }; - if (holds.delete('history') || mode === 'hold') - hold('history', response, finish, threadId); - else finish(); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'checkpoint', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - body.assistant_id !== 'c-timeline' || - !only(body.input, ['messages']) || - !Array.isArray(body.input.messages) || - body.input.messages.length !== 1 || - !only(message, ['type', 'id', 'content']) || - message.type !== 'human' || - typeof message.content !== 'string' || - typeof message.id !== 'string' || - !message.id.trim() || - body.stream_subgraphs !== true || - body.stream_resumable !== true || - body.on_disconnect !== 'continue' || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'values', - 'messages-tuple', - 'updates', - 'custom', - 'checkpoints', - ].includes(mode) - ) - ) { - json(response, {}, 400); - return true; - } - if ( - body.checkpoint !== undefined && - !(await knownCheckpoint(body.checkpoint)) - ) { - json(response, {}, 400); - return true; - } - const runId = randomUUID(); - const wire = await operate({ - runId, - op: 'submit', - checkpoint: body.checkpoint, - threadId, - messages: body.input.messages, - }); - // Keep native checkpoints inside the worker for exact terminal identity, - // but emit only the modes this HTTP caller actually requested. - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const events = wire.events.filter( - ([kind]) => kind === 'error' || modes.has(kind) - ); - const run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - let sent = 0; - const finish = () => { - if (response.destroyed || captured !== epoch || closed) return; - while (sent < events.length) response.write(event(...events[sent++])); - run.status = wire.graphFailure - ? 'error' - : wire.state.next.length - ? 'interrupted' - : 'success'; - response.end(); - }; - if (holds.delete('stream')) { - const firstMessage = events.findIndex( - ([kind]) => kind === 'messages' - ); - const limit = - firstMessage < 0 ? Math.min(2, events.length) : firstMessage + 1; - while (sent < limit) response.write(event(...events[sent++])); - hold('stream', response, finish, threadId, run); - } else finish(); - return true; - } - if (/^\/runs\/[^/]+$/.test(suffix) && request.method === 'GET') { - const run = runs.get(suffix.split('/').at(-1)); - json( - response, - run?.thread_id === threadId ? run : {}, - run?.thread_id === threadId ? 200 : 404 - ); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => { - if (closing) return closing; - closed = true; - closing = reset(); - return closing; - }; - return handle; -} diff --git a/scripts/react-cockpit/chat-timeline-fixture.spec.mjs b/scripts/react-cockpit/chat-timeline-fixture.spec.mjs deleted file mode 100644 index f7253ec70..000000000 --- a/scripts/react-cockpit/chat-timeline-fixture.spec.mjs +++ /dev/null @@ -1,1071 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { randomUUID, createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { PassThrough } from 'node:stream'; -import { test } from 'node:test'; -const sdkRun = (body) => ({ - stream_mode: ['values', 'messages-tuple', 'updates', 'custom'], - stream_subgraphs: true, - stream_resumable: true, - on_disconnect: 'continue', - ...body, -}); -const { createChatTimelineFixture } = await import( - './chat-timeline-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createChatTimelineFixture, - 'function', - 'Timeline fixture is available' - ); - const handle = createChatTimelineFixture(options); - const closedResponses = []; - const server = createServer(async (req, res) => { - res.on('close', () => closedResponses.push(req.url)); - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const request = ( - path, - body, - method = 'POST', - signal = AbortSignal.timeout(15000) - ) => - fetch(`http://127.0.0.1:${server.address().port}${path}`, { - method, - ...(method !== 'GET' - ? { - body: JSON.stringify( - path.endsWith('/runs/stream') ? sdkRun(body) : body ?? {} - ), - } - : {}), - headers: { 'content-type': 'application/json' }, - signal, - }); - const json = async (path, body, method) => { - const res = await request(path, body, method); - assert.equal(res.status, 200); - return res.json(); - }; - const create = async (id = randomUUID()) => { - await json('/api/threads', { - metadata: {}, - thread_id: id, - if_exists: 'raise', - }); - return id; - }; - const submit = (id, text = 'Hello', humanId = randomUUID()) => - request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - input: { messages: [{ type: 'human', content: text, id: humanId }] }, - }); - const history = (id) => json(`/api/threads/${id}/history`, { limit: 10 }); - const wait = async (key) => { - for (let n = 0; n < 500; n++) { - const state = await json('/__lifetime'); - if (state[key]) return state; - await new Promise((r) => setTimeout(r, 10)); - } - assert.fail('Timed out waiting for ' + key); - }; - return { - handle, - request, - json, - create, - submit, - history, - wait, - closedResponses, - }; -} -function frames(text) { - return text - .trim() - .split('\n\n') - .map((frame) => { - const [kind, data] = frame.split('\n'); - return [kind.slice(7), JSON.parse(data.slice(6))]; - }); -} - -for (const kind of ['history']) - test(`aborted ${kind} before worker completion does not install a hold`, async (t) => { - let release; - let ready; - const workerReady = new Promise((resolve) => { - ready = resolve; - }); - const f = await fixture(t, { - spawnWorker: () => { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/timeline/python', - 'python', - 'scripts/react-cockpit/chat-timeline-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - const output = child.stdout, - delayed = new PassThrough(), - buffered = []; - child.stdout = delayed; - output.on('data', (chunk) => { - buffered.push(chunk); - if (Buffer.concat(buffered).includes(10)) ready(); - }); - output.on('end', () => delayed.end()); - release = () => { - for (const chunk of buffered) delayed.write(chunk); - }; - return child; - }, - }); - const id = await f.create(); - await f.json('/__hold-' + kind); - const path = `/api/threads/${id}/history`; - const controller = new AbortController(); - const pending = f - .request(path, { limit: 10 }, 'POST', controller.signal) - .catch(() => null); - await workerReady; - controller.abort(); - await pending; - for (let i = 0; i < 100 && !f.closedResponses.includes(path); i++) - await new Promise((resolve) => setTimeout(resolve, 10)); - assert( - f.closedResponses.includes(path), - 'Server observed disconnect before graph response' - ); - release(); - for ( - let i = 0; - i < 100 && (await f.json('/__lifetime')).pendingOperations; - i++ - ) - await new Promise((resolve) => setTimeout(resolve, 10)); - const state = await f.json('/__lifetime'); - assert.equal(state.pendingOperations, 0); - assert.equal(state['activeHistory'], false); - assert.deepEqual(await f.json('/__injections'), []); - assert.equal((await f.json('/__graph-proof')).length, 1); - }); - -test('replacing a held stream interrupts its exact run and lets the replacement succeed', async (t) => { - const f = await fixture(t), - a = await f.create(), - b = await f.create(); - await f.json('/__hold-stream'); - const first = await f.submit(a), - firstText = first.text(); - const firstRun = first.headers.get('content-location').split('/').at(-1); - await f.wait('activeStream'); - await f.json('/__hold-stream'); - const second = await f.submit(b), - secondText = second.text(); - const secondRun = second.headers.get('content-location').split('/').at(-1); - await firstText; - assert.equal( - (await f.json(`/api/threads/${a}/runs/${firstRun}`, undefined, 'GET')) - .status, - 'interrupted' - ); - assert.equal( - (await f.json(`/api/threads/${b}/runs/${secondRun}`, undefined, 'GET')) - .status, - 'running' - ); - await f.json('/__release'); - await secondText; - assert.equal( - (await f.json(`/api/threads/${b}/runs/${secondRun}`, undefined, 'GET')) - .status, - 'success' - ); -}); - -test('actual compiled graph retains separate canonical histories and unique answers', async (t) => { - const f = await fixture(t), - a = await f.create(), - b = await f.create(); - const first = frames(await (await f.submit(a, 'Same')).text()); - assert(first.some(([kind]) => kind === 'messages')); - assert(first.some(([kind]) => kind === 'values')); - assert(first.some(([kind]) => kind === 'updates')); - assert( - !first.some( - ([kind, data]) => - kind === 'messages' && data[1].langgraph_node === 'generate_title' - ) - ); - const [before] = await f.history(a); - await (await f.submit(b, 'Same')).text(); - await (await f.submit(a, 'Follow up')).text(); - const [after] = await f.history(a), - [other] = await f.history(b); - assert.deepEqual(after.values.messages.slice(0, 2), before.values.messages); - assert.equal(after.values.messages.length, 4); - assert.equal(other.values.messages.length, 2); - assert.notEqual(before.values.messages[1].id, other.values.messages[1].id); - assert.deepEqual(after.next, []); - assert.deepEqual(after.tasks, []); - assert.equal(after.checkpoint.thread_id, a); - const run = first.find(([kind]) => kind === 'metadata')[1].run_id; - assert.equal( - (await f.json(`/api/threads/${a}/runs/${run}`, undefined, 'GET')).status, - 'success' - ); - assert.equal( - (await f.request(`/api/threads/${b}/runs/${run}`, undefined, 'GET')).status, - 404 - ); - const proofs = await f.json('/__graph-proof'); - assert.equal(proofs.length, 6); - for (const proof of proofs) { - assert.equal(proof.actualCompiledGraph, true); - assert.equal(proof.networkConnectAttempts, 0); - assert.equal(proof.titleMessageCallbacks, 0); - for (const [key, path] of [ - ['sourceSha256', 'src/graph.py'], - ['lockSha256', 'uv.lock'], - ]) - assert.equal( - proof[key], - createHash('sha256') - .update(readFileSync('cockpit/chat/timeline/python/' + path)) - .digest('hex') - ); - } - assert.deepEqual(await f.json('/__injections'), []); -}); - -test('installed SDK creation accepts only empty plain metadata and rejects unknown fields', async (t) => { - const f = await fixture(t), - id = randomUUID(); - const response = await f.request('/api/threads', { - metadata: {}, - thread_id: id, - if_exists: 'raise', - }); - assert.equal(response.status, 200); - assert.deepEqual(await response.json(), { thread_id: id }); - for (const metadata of [null, [], '', false, { title: 'Unsupported' }]) { - assert.equal( - ( - await f.request('/api/threads', { - metadata, - thread_id: randomUUID(), - if_exists: 'raise', - }) - ).status, - 400 - ); - } - assert.equal( - ( - await f.request('/api/threads', { - metadata: {}, - thread_id: randomUUID(), - if_exists: 'raise', - unknown: true, - }) - ).status, - 400 - ); -}); - -test('only confirmed UUID creation and exact supported routes are admitted', async (t) => { - const f = await fixture(t), - id = await f.create(); - assert.equal( - (await f.request('/api/threads', { thread_id: 'not-uuid' })).status, - 400 - ); - assert.equal( - (await f.request('/api/threads', { thread_id: id })).status, - 400 - ); - for (const [path, method, body] of [ - ['/api/threads/search', 'POST', {}], - ['/api/threads', 'GET'], - [`/api/threads/${id}`, 'DELETE'], - [`/api/threads/${id}`, 'PATCH', {}], - [`/api/threads/${id}/runs/x/cancel`, 'POST', {}], - [`/api/threads/${id}/runs/x/stream`, 'GET'], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { assistant_id: 'wrong', input: { messages: [] } }, - ], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { assistant_id: 'c-timeline', command: { resume: true } }, - ], - [ - `/api/threads/${id}/runs/stream`, - 'POST', - { - assistant_id: 'c-timeline', - checkpoint_id: 'replay', - input: { messages: [{ type: 'human', content: 'Hi', id: 'h' }] }, - }, - ], - ]) - assert((await f.request(path, body, method)).status >= 400, path); - assert((await f.json('/__requests')).length >= 12); -}); - -test('history injections preserve separate actual proof and restore real saved data', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id)).text(); - const original = await f.history(id); - for (const mode of ['changed', 'missing', 'pending', 'foreign', 'failure']) { - await f.json('/__history/' + mode); - const response = await f.request(`/api/threads/${id}/history`, { - limit: 10, - }); - if (mode === 'failure') { - assert.equal(response.status, 500); - continue; - } - const data = await response.json(); - assert.notDeepEqual(data, original); - if (mode === 'foreign') assert.notEqual(data[0].checkpoint.thread_id, id); - } - await f.json('/__history/safe'); - assert.deepEqual(await f.history(id), original); - assert.equal( - (await f.json('/__injections')).filter((i) => i.kind === 'history').length, - 5 - ); - assert( - (await f.json('/__graph-proof')).every( - (p) => !('injectedHistoryFault' in p) - ) - ); -}); - -test('applied one-shot and persistent holds are recorded once beside actual graph proof', async (t) => { - const f = await fixture(t), - id = await f.create(), - expected = []; - await f.json('/__hold-stream'); - assert.deepEqual(await f.json('/__injections'), []); - const stream = f.submit(id).then((response) => response.text()); - await f.wait('activeStream'); - expected.push({ kind: 'stream', mode: 'hold', threadId: id }); - assert.deepEqual(await f.json('/__injections'), expected); - const streamProof = await f.json('/__graph-proof'); - await f.json('/__release'); - await stream; - assert.deepEqual(await f.json('/__graph-proof'), streamProof); - for (const kind of ['history']) { - await f.json('/__hold-' + kind); - for (let attempt = 0; attempt < 3; attempt++) { - if (attempt === 1) await f.json(`/__${kind}/hold`); - // One-shot plus persistent hold must still record a single applied hold. - if (attempt === 2) await f.json('/__hold-' + kind); - const pending = f.history(id); - await f.wait('activeHistory'); - expected.push({ kind, mode: 'hold', threadId: id }); - assert.deepEqual(await f.json('/__injections'), expected); - const proof = await f.json('/__graph-proof'); - assert( - proof.every((item) => !('mode' in item) && !('injections' in item)) - ); - await f.json('/__release-' + kind); - await pending; - assert.deepEqual(await f.json('/__graph-proof'), proof); - assert.deepEqual(await f.json('/__injections'), expected); - } - await f.json(`/__${kind}/safe`); - } -}); - -test('held stream, history release and disconnect without retaining responses', async (t) => { - const f = await fixture(t), - id = await f.create(); - await f.json('/__hold-stream'); - const streamed = f.submit(id).then((r) => r.text()); - await f.wait('activeStream'); - await f.json('/__release'); - assert.match(await streamed, /Answer/); - for (const kind of ['history']) { - await f.json('/__hold-' + kind); - const pending = f.history(id); - await f.wait('activeHistory'); - await f.json('/__release-' + kind); - await pending; - await f.json('/__hold-' + kind); - const controller = new AbortController(); - const request = f - .request( - `/api/threads/${id}/history`, - { limit: 10 }, - 'POST', - controller.signal - ) - .catch(() => null); - await f.wait('activeHistory'); - controller.abort(); - await request; - for ( - let i = 0; - i < 100 && (await f.json('/__lifetime'))['activeHistory']; - i++ - ) - await new Promise((r) => setTimeout(r, 10)); - assert.equal((await f.json('/__lifetime'))['activeHistory'], false); - } -}); - -test('reset fences held responses and closes the real process; disposal is idempotent', async (t) => { - const f = await fixture(t), - id = await f.create(); - await f.json('/__hold-stream'); - const pending = f.submit(id).then((r) => r.text()); - const { workerPid } = await f.wait('activeStream'); - await f.json('/__reset'); - await pending; - assert.throws(() => process.kill(workerPid, 0), { code: 'ESRCH' }); - const fresh = await f.create(); - assert.deepEqual(await f.history(fresh), []); - assert.equal( - (await f.request(`/api/threads/${id}`, undefined, 'GET')).status, - 404 - ); - const pid = (await f.json('/__lifetime')).workerPid; - await Promise.all([f.handle.close(), f.handle.close()]); - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -}); - -for (const [name, program] of [ - [ - 'malformed', - 'process.stdin.once("data",()=>process.stdout.write("PRIVATE broken\\n"));', - ], - [ - 'unowned', - 'process.stdin.once("data",()=>process.stdout.write(JSON.stringify({requestId:99})+"\\n"));', - ], - [ - 'eof', - 'process.stdin.once("data",()=>process.stdout.end()); setInterval(()=>{},1000);', - ], - ['stall', 'process.stdin.resume(); setInterval(()=>{},1000);'], -]) - test( - name + ' worker fails closed without exposing diagnostics', - async (t) => { - const f = await fixture(t, { - workerTimeoutMs: 150, - spawnWorker: () => - spawn(process.execPath, ['-e', program], { - stdio: ['pipe', 'pipe', 'pipe'], - }), - }), - id = await f.create(); - const response = await f.submit(id); - assert.equal(response.status, 500); - assert.doesNotMatch(await response.text(), /PRIVATE|broken/); - } - ); - -test('close rejects queued worker operations and reset admits a fresh epoch', async (t) => { - let child, - calls = 0; - const f = await fixture(t, { - spawnWorker: () => - ++calls === 1 - ? (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(()=>{},1000);'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )) - : spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/timeline/python', - 'python', - 'scripts/react-cockpit/chat-timeline-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - const a = await f.create(), - b = await f.create(), - first = f.submit(a), - second = f.submit(b); - await f.wait('pendingOperations'); - await f.json('/__reset'); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - const fresh = await f.create(); - await (await f.submit(fresh)).text(); - assert.equal((await f.history(fresh))[0].values.messages.length, 2); - await f.handle.close(); - assert.equal((await f.submit(fresh)).status, 500); -}); - -test('actual graph failure streams partial evidence, records failed run and retains pending checkpoint', async (t) => { - const f = await fixture(t), - id = await f.create(); - const events = frames(await (await f.submit(id, 'fail-stream')).text()); - assert(events.some(([kind, data]) => kind === 'messages' && data[0].content)); - assert(events.some(([kind]) => kind === 'error')); - const run = events.find(([kind]) => kind === 'metadata')[1].run_id; - assert.equal( - (await f.json(`/api/threads/${id}/runs/${run}`, undefined, 'GET')).status, - 'error' - ); - assert((await f.history(id))[0].next.length > 0); - assert.deepEqual(await f.json('/__injections'), []); -}); - -test('closed chat-timeline selection has its own build and server dispatch', async () => { - const { reactCockpitConfiguration } = await import('./configuration.mjs'); - assert.deepEqual(reactCockpitConfiguration('chat-timeline'), { - topic: 'timeline', - library: 'chat', - adapter: 'langgraph', - appPath: 'cockpit/chat/timeline/react', - base: '/chat/timeline/react/', - port: 4626, - project: 'cockpit-chat-timeline-react', - }); - assert.throws(() => reactCockpitConfiguration('../chat-timeline')); - const server = readFileSync('scripts/react-cockpit/serve.mjs', 'utf8'); - assert.match(server, /createChatTimelineFixture/); - assert.match( - server, - /await chatTimelineFixture\(request, response, pathname\)/ - ); - assert.match( - server, - /await\s*\(\s*(?:\w+\s*\?\?\s*)*chatTimelineFixture(?:\s*\?\?\s*\w+)*\s*\)\.close\(\)/ - ); -}); - -test('dispose rejects active and queued stalled work and kills its owned child', async (t) => { - let child; - const f = await fixture(t, { - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.stdin.resume(); setInterval(()=>{},1000);'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - const a = await f.create(), - b = await f.create(); - const first = f.submit(a), - second = f.submit(b); - await f.wait('pendingOperations'); - await f.handle.close(); - assert.equal((await first).status, 500); - assert.equal((await second).status, 500); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); -}); - -test('creation and worker failure controls fail privately and reset permits recovery', async (t) => { - const f = await fixture(t), - id = randomUUID(); - await f.json('/__fail-create'); - const failed = await f.request('/api/threads', { - thread_id: id, - if_exists: 'raise', - }); - assert.equal(failed.status, 500); - assert.deepEqual(await failed.json(), { error: 'Graph fixture failed.' }); - assert.equal( - (await f.request(`/api/threads/${id}`, undefined, 'GET')).status, - 404 - ); - await f.create(id); - await f.json('/__worker-failure'); - assert.equal((await f.submit(id)).status, 500); - await f.json('/__reset'); - await f.create(id); - assert.match(await (await f.submit(id)).text(), /Answer/); -}); - -test('read-only full checkpoint selection forks actual A+D and keeps later tip immutable', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id, 'A', 'human-a')).text(); - const a = (await f.history(id))[0]; - await (await f.submit(id, 'B', 'human-b')).text(); - const b = (await f.history(id))[0]; - const source = a.checkpoint; - const before = (await f.json('/__graph-proof')).filter( - (p) => p.op === 'submit' - ).length; - const selected = await f.json(`/api/threads/${id}/state/checkpoint`, { - checkpoint: source, - }); - assert.deepEqual(selected.values, a.values); - assert.deepEqual((await f.history(id))[0].values, b.values); - assert.equal( - (await f.json('/__graph-proof')).filter((p) => p.op === 'submit').length, - before - ); - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint: source, - input: { messages: [{ type: 'human', id: 'human-d', content: 'D' }] }, - }) - ).text(); - const d = (await f.history(id))[0]; - assert.equal(Object.hasOwn(d.metadata, 'run_id'), false); - const runEvents = frames( - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint: d.checkpoint, - input: { - messages: [ - { type: 'human', id: 'checkpoint-probe', content: 'Probe' }, - ], - }, - stream_mode: ['values', 'messages-tuple', 'updates', 'checkpoints'], - }) - ).text() - ); - const checkpoints = runEvents.filter(([kind]) => kind === 'checkpoints'); - assert( - checkpoints.length > 0, - 'actual compiled checkpoint events are streamed' - ); - assert.equal( - checkpoints.at(-1)[1].config.configurable.run_id, - runEvents[0][1].run_id - ); - - assert.deepEqual( - d.values.messages.map((m) => m.id), - ['human-a', 'answer-human-a', 'human-d', 'answer-human-d'] - ); - assert.deepEqual(d.values.messages.slice(0, 2), a.values.messages); - assert.deepEqual( - d.values.completed_message_ids, - d.values.messages.map((m) => m.id) - ); - assert.deepEqual( - ( - await f.json(`/api/threads/${id}/state/checkpoint`, { - checkpoint: b.checkpoint, - }) - ).values, - b.values - ); - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint: d.checkpoint, - input: { messages: [{ type: 'human', id: 'human-e', content: 'E' }] }, - }) - ).text(); - assert.deepEqual( - (await f.history(id))[0].values.messages - .filter((m) => m.type === 'human') - .map((m) => m.content), - ['A', 'D', 'E'] - ); - for (const checkpoint of [ - { ...source, thread_id: randomUUID() }, - { ...source, checkpoint_id: randomUUID() }, - { ...source, checkpoint_ns: 'foreign' }, - { ...source, checkpoint_map: { '': randomUUID() } }, - ]) { - assert.equal( - (await f.request(`/api/threads/${id}/state/checkpoint`, { checkpoint })) - .status, - 400 - ); - } -}); - -test('worker fork result names its actual terminal checkpoint rather than the original source', async (t) => { - const { createInterface } = await import('node:readline'); - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/timeline/python', - 'python', - 'scripts/react-cockpit/chat-timeline-wire.py', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ); - child.stderr.resume(); - const lines = createInterface({ input: child.stdout }); - const replies = lines[Symbol.asyncIterator](); - t.after(() => { - lines.close(); - child.stdin.end(); - child.kill(); - }); - let requestId = 0; - async function call(value) { - child.stdin.write( - JSON.stringify({ ...value, requestId: ++requestId }) + '\n' - ); - return JSON.parse((await replies.next()).value); - } - const threadId = randomUUID(); - const a = await call({ - op: 'submit', - threadId, - runId: randomUUID(), - messages: [{ type: 'human', id: 'a', content: 'A' }], - }); - const b = await call({ - op: 'submit', - threadId, - runId: randomUUID(), - messages: [{ type: 'human', id: 'b', content: 'B' }], - }); - const d = await call({ - op: 'submit', - threadId, - runId: randomUUID(), - checkpoint: a.state.checkpoint, - messages: [{ type: 'human', id: 'd', content: 'D' }], - }); - assert.notEqual( - d.state.checkpoint.checkpoint_id, - a.state.checkpoint.checkpoint_id - ); - assert.notEqual( - d.state.checkpoint.checkpoint_id, - b.state.checkpoint.checkpoint_id - ); - assert.deepEqual( - d.state.values.messages.map((m) => m.id), - ['a', 'answer-a', 'd', 'answer-d'] - ); - assert.equal( - d.state.checkpoint.checkpoint_id, - d.events.filter(([kind]) => kind === 'checkpoints').at(-1)[1].config - .configurable.checkpoint_id - ); -}); - -test('mapped HTTP representation preserves genuine checkpoint IDs and rejects map mutation', async (t) => { - const f = await fixture(t), - id = await f.create(); - await f.json('/__mapped-checkpoints'); - await (await f.submit(id, 'A', 'a')).text(); - const a = (await f.history(id))[0], - source = a.checkpoint; - assert.deepEqual(source.checkpoint_map, { '': source.checkpoint_id }); - assert.deepEqual( - ( - await f.json(`/api/threads/${id}/state/checkpoint`, { - checkpoint: source, - }) - ).checkpoint, - source - ); - for (const checkpoint of [ - { ...source, checkpoint_map: undefined }, - { ...source, checkpoint_map: {} }, - { ...source, checkpoint_map: { '': 'wrong' } }, - { - ...source, - checkpoint_map: { '': source.checkpoint_id, child: 'unexpected' }, - }, - ]) { - assert.equal( - (await f.request(`/api/threads/${id}/state/checkpoint`, { checkpoint })) - .status, - 400 - ); - assert.equal( - ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint, - input: { messages: [{ type: 'human', id: 'bad', content: 'Bad' }] }, - }) - ).status, - 400 - ); - } - const events = frames( - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint: source, - input: { messages: [{ type: 'human', id: 'd', content: 'D' }] }, - stream_mode: [ - 'values', - 'messages-tuple', - 'updates', - 'custom', - 'checkpoints', - ], - }) - ).text() - ); - const terminal = events.filter(([kind]) => kind === 'checkpoints').at(-1)[1] - .config.configurable; - assert.deepEqual(terminal.checkpoint_map, { '': terminal.checkpoint_id }); - const state = await f.json(`/api/threads/${id}/state/checkpoint`, { - checkpoint: { - thread_id: id, - checkpoint_ns: '', - checkpoint_id: terminal.checkpoint_id, - checkpoint_map: terminal.checkpoint_map, - }, - }); - assert.deepEqual( - state.values.messages.map((m) => m.id), - ['a', 'answer-a', 'd', 'answer-d'] - ); - assert.equal( - (await f.json('/__graph-proof')).filter((p) => p.op === 'submit').length, - 2 - ); -}); - -for (const field of ['sourceSha256', 'lockSha256']) - test(`worker proof with wrong ${field} fails closed`, async (t) => { - const expected = Object.fromEntries( - [ - ['sourceSha256', 'src/graph.py'], - ['lockSha256', 'uv.lock'], - ].map(([key, path]) => [ - key, - createHash('sha256') - .update(readFileSync('cockpit/chat/timeline/python/' + path)) - .digest('hex'), - ]) - ); - expected[field] = 'wrong'; - const f = await fixture(t, { - spawnWorker: () => - spawn( - process.execPath, - [ - '-e', - `process.stdin.on('data',line=>{const q=JSON.parse(line);process.stdout.write(JSON.stringify({requestId:q.requestId,history:[],proof:{actualCompiledGraph:true,...${JSON.stringify( - expected - )},op:q.op,threadId:q.threadId,networkConnectAttempts:0,titleMessageCallbacks:0}})+String.fromCharCode(10))})`, - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - ), - }); - const id = await f.create(); - assert.equal( - (await f.request(`/api/threads/${id}/history`, { limit: 10 })).status, - 500 - ); - assert.deepEqual(await f.json('/__graph-proof'), []); - }); - -test('fixture instances isolate fault controls, workers, and checkpoint ownership', async (t) => { - const a = await fixture(t), - b = await fixture(t), - id = await a.create(); - await b.create(id); - await a.json('/__history/failure'); - await Promise.all([ - (async () => { - await (await a.submit(id, 'A', 'a')).text(); - })(), - (async () => { - await (await b.submit(id, 'B', 'b')).text(); - })(), - ]); - assert.equal( - (await a.request(`/api/threads/${id}/history`, { limit: 10 })).status, - 500 - ); - assert.deepEqual( - (await b.history(id))[0].values.messages.map((m) => m.id), - ['b', 'answer-b'] - ); - assert.notEqual( - (await a.json('/__lifetime')).workerPid, - (await b.json('/__lifetime')).workerPid - ); - await a.handle.close(); - await (await b.submit(id, 'C', 'c')).text(); - assert.deepEqual( - (await b.history(id))[0].values.messages.map((m) => m.id), - ['b', 'answer-b', 'c', 'answer-c'] - ); -}); - -test('history honors bounded limits and rejects unimplemented routing fields', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id, 'A')).text(); - await (await f.submit(id, 'B')).text(); - const all = await f.history(id); - assert(all.length > 1); - assert.equal( - (await f.json(`/api/threads/${id}/history`, { limit: 1 })).length, - 1 - ); - for (const body of [ - {}, - { limit: 0 }, - { limit: -1 }, - { limit: 101 }, - { limit: 1.5 }, - { limit: '10' }, - { limit: 10, filter: {} }, - { limit: 10, before: {} }, - { limit: 10, checkpoint: {} }, - ]) - assert.equal( - (await f.request(`/api/threads/${id}/history`, body)).status, - 400, - JSON.stringify(body) - ); -}); - -test('checkpoint reads reject extra top-level state mutation and routing fields', async (t) => { - const f = await fixture(t), - id = await f.create(); - await (await f.submit(id)).text(); - const checkpoint = (await f.history(id))[0].checkpoint; - for (const extra of [ - { values: {} }, - { checkpoint_id: checkpoint.checkpoint_id }, - { subgraphs: true }, - { unknown: true }, - ]) - assert.equal( - ( - await f.request(`/api/threads/${id}/state/checkpoint`, { - checkpoint, - ...extra, - }) - ).status, - 400 - ); -}); - -test('run admission rejects unsupported SDK fields, malformed inputs, and mode controls before execution', async (t) => { - const f = await fixture(t), - id = await f.create(); - const input = { - messages: [{ type: 'human', id: 'authored', content: 'Hello' }], - }, - base = { assistant_id: 'c-timeline', input }; - const bad = [ - { config: {} }, - { interrupt_before: ['generate'] }, - { interrupt_after: [] }, - { metadata: {} }, - { stream_subgraphs: false }, - { stream_subgraphs: undefined }, - { stream_resumable: false }, - { stream_resumable: undefined }, - { on_disconnect: 'cancel' }, - { on_disconnect: undefined }, - { stream_mode: undefined }, - { stream_mode: [] }, - { stream_mode: ['messages'] }, - { stream_mode: ['values', 'values'] }, - { stream_mode: 'values' }, - { stream_mode: ['debug'] }, - { input: { ...input, other: 1 } }, - { input: { messages: [{ ...input.messages[0], id: ' ' }] } }, - { input: { messages: [{ ...input.messages[0], name: 'extra' }] } }, - { input: { messages: [{ ...input.messages[0], content: 3 }] } }, - ]; - for (const delta of bad) - assert.equal( - (await f.request(`/api/threads/${id}/runs/stream`, { ...base, ...delta })) - .status, - 400, - JSON.stringify(delta) - ); - assert.equal( - (await f.json('/__graph-proof')).filter((p) => p.op === 'submit').length, - 0 - ); -}); - -test('SSE emits only requested modes while real internal checkpoints still determine terminal state', async (t) => { - const f = await fixture(t), - id = await f.create(); - const ordinary = frames(await (await f.submit(id, 'A', 'a')).text()); - assert(!ordinary.some(([kind]) => kind === 'checkpoints')); - assert(ordinary.some(([kind]) => kind === 'messages')); - const a = (await f.history(id))[0]; - const branch = frames( - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - checkpoint: a.checkpoint, - input: { messages: [{ type: 'human', id: 'd', content: 'D' }] }, - stream_mode: ['values', 'checkpoints'], - }) - ).text() - ); - assert(branch.some(([kind]) => kind === 'checkpoints')); - assert( - !branch.some(([kind]) => ['messages', 'updates', 'custom'].includes(kind)) - ); - assert.deepEqual( - (await f.history(id))[0].values.messages.map((m) => m.id), - ['a', 'answer-a', 'd', 'answer-d'] - ); - const failed = frames( - await ( - await f.request(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-timeline', - input: { - messages: [{ type: 'human', id: 'failure', content: 'fail-stream' }], - }, - stream_mode: ['values'], - }) - ).text() - ); - assert(failed.some(([kind]) => kind === 'error')); - assert( - !failed.some(([kind]) => kind === 'messages' || kind === 'checkpoints') - ); -}); diff --git a/scripts/react-cockpit/chat-timeline-wire.py b/scripts/react-cockpit/chat-timeline-wire.py deleted file mode 100644 index 84ed3f54f..000000000 --- a/scripts/react-cockpit/chat-timeline-wire.py +++ /dev/null @@ -1,136 +0,0 @@ -"""Persistent network-free worker executing the actual compiled Timeline graph.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/timeline/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel - -class Metadata: - def __init__(self): - self.titles = {} - self.fail_read = False - self.fail_write = False - - async def get(self, thread_id): - if self.fail_read: - raise RuntimeError("Metadata unavailable") - return {"thread_id": thread_id, "metadata": self.titles.get(thread_id, {})} - - async def update(self, thread_id, metadata): - if self.fail_write: - raise RuntimeError("Metadata unavailable") - self.titles[thread_id] = dict(metadata) - -class AuthoredClient: - def __init__(self): - self.threads = Metadata() - -from langgraph.checkpoint.memory import InMemorySaver - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state, mapped=False): - result = { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() if k in ["thread_id", "checkpoint_ns", "checkpoint_id", "checkpoint_map"]}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - if mapped: - result["checkpoint"]["checkpoint_map"] = {"": result["checkpoint"]["checkpoint_id"]} - if result["parent_checkpoint"]: - result["parent_checkpoint"]["checkpoint_map"] = {"": result["parent_checkpoint"]["checkpoint_id"]} - return result - - -async def main(): - client = AuthoredClient() - options = {"fail_title": False, "mapped": False} - def model_factory(**kwargs): - return LocalModel(title=not kwargs.get("streaming", False), tags=kwargs.get("tags", []), fail_title=options["fail_title"]) - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_timeline_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_timeline_graph().builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - if request.get("checkpoint"): - config["configurable"].update(request["checkpoint"]) - if request["op"] == "state": - result = {"state": checkpoint(graph.get_state(config), options["mapped"])} - elif request["op"] == "history": - result = {"history": [checkpoint(state, options["mapped"]) for state in graph.get_state_history(config)]} - elif request["op"] == "configure": - client.threads.fail_read = request.get("failMetadata", False) - client.threads.fail_write = request.get("failWrite", False) - options["fail_title"] = request.get("failTitle", False) - options["mapped"] = request.get("mapped", options["mapped"]) - result = {} - elif request["op"] == "submit": - events = [] - terminal_config = config - failure = False - try: - async for kind, event in graph.astream({"messages": request["messages"]}, config, stream_mode=["messages", "values", "updates", "checkpoints"]): - if kind == "messages" and event[1].get("langgraph_node") == "generate_title": - raise AssertionError("Title callbacks leaked into answer stream") - if kind == "checkpoints": - event["config"]["configurable"]["run_id"] = request["runId"] - if options["mapped"]: - position = event["config"]["configurable"] - position["checkpoint_map"] = {"": position["checkpoint_id"]} - terminal_config = event["config"] - events.append([kind, event]) - except RuntimeError: - failure = True - events.append(["error", {"error": "GraphExecutionError", "message": "The conversation request failed."}]) - result = {"events": events, "state": checkpoint(graph.get_state(terminal_config), options["mapped"]), "graphFailure": failure} - else: - raise ValueError("Unsupported graph operation") - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "op": request["op"], "threadId": request.get("threadId"), - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "networkConnectAttempts": connect.call_count, "titleMessageCallbacks": 0, - "httpCheckpointRepresentation": "root-map" if options["mapped"] else "native", - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 5dbc5389f..765795554 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -88,11 +88,13 @@ export function reactCockpitConfiguration(topic = 'streaming') { return Object.freeze({ topic: 'timeline', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/timeline/react', base: '/chat/timeline/react/', port: 4626, project: 'cockpit-chat-timeline-react', + backend: aimockLanggraph('chat/timeline', 4626), }); if (topic === 'chat-threads') return Object.freeze({ topic: 'threads', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/threads/react', base: '/chat/threads/react/', port: 4625, project: 'cockpit-chat-threads-react', + backend: aimockLanggraph('chat/threads', 4625), }); if (topic === 'chat-subagents') return Object.freeze({ diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 73d600970..313d4dc03 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -10,16 +10,33 @@ * GET /__requests every /api and /developer-api request, in order * POST /__ready 200 once `langgraph dev` answers /ok (graph loaded; * with `serve.mjs --backend`, after its lazy start) - * GET /__lifetime { activeStream, activeHistory, openResponses } + * GET /__lifetime { activeStream, activeHistory, activeTitle, + * activeState, openResponses } * GET /__captures per finished run: forwarded SSE text, saved * messages/next from the real server, title - * before and after the run + * before and after the run (waits for captures + * still being read) * POST /__reset forget requests, drop held responses, clear modes * POST /__fail-create next thread creation answers 500 without forwarding * POST /__history/ safe | hold | changed | missing | pending | failed, * or tool evidence: args | result | name | ids | * interrupt | subgraph, or approval evidence: - * mismatch | unsupported + * mismatch | unsupported; foreign (checkpoint of + * another thread); failure (= failed) + * POST /__title/ thread metadata read: safe | hold | missing | + * wrong-thread | invalid | titlefailure | literal | + * long (mode captured when the read arrives) + * POST /__hold/title hold the next thread metadata read + * POST /__hold/history hold the next history read + * POST /__state/ checkpoint state reads: safe | hold | failure | + * changed | foreign | malformed + * POST /__hold/state hold the next checkpoint state read + * POST /__release-state finish a held checkpoint state read + * POST /__mapped-checkpoints until reset, checkpoints the browser receives + * (history, state, `checkpoints` events) carry the + * root map { "": checkpoint_id } — the HTTP shape + * some deployments return + * POST /__release-title finish a held metadata read * POST /__release-history finish a held history read * POST /__hold/stream hold the next run stream; body { afterText? } holds * after the first AI message chunk (or once the AI text @@ -46,6 +63,7 @@ * Nothing here is reachable from a production build: only * scripts/react-cockpit/serve.mjs imports it. */ +import { randomUUID } from 'node:crypto'; import { request as httpRequest } from 'node:http'; // Saved-evidence rewrites of the latest history entry. Tool modes mirror the @@ -53,8 +71,12 @@ import { request as httpRequest } from 'node:http'; const HISTORY_MODES = [ 'safe', 'hold', 'changed', 'missing', 'pending', 'failed', 'args', 'result', 'name', 'ids', 'interrupt', 'subgraph', - 'mismatch', 'unsupported', + 'mismatch', 'unsupported', 'foreign', 'failure', ]; +// Rewrites of checkpoint state reads (POST /threads//state/checkpoint). +const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed']; +// Rewrites of the thread metadata read (GET /threads/) the client uses for titles. +const TITLE_MODES = ['safe', 'hold', 'missing', 'wrong-thread', 'invalid', 'titlefailure', 'literal', 'long']; const HOP_BY_HOP = new Set(['connection', 'keep-alive', 'transfer-encoding', 'content-length', 'host']); const json = (response, value, status = 200) => { @@ -104,11 +126,20 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let failCreate = false; let failStream = false; let childFault = null; + let titleMode = 'safe'; + let holdNextTitle = false; + let heldTitle; + let holdNextHistory = false; + let stateMode = 'safe'; + let holdNextState = false; + let heldState; + let mapped = false; let historyMode = 'safe'; let holdNextStream = null; let heldStream; let heldHistory; let captures = []; + const pendingCaptures = new Set(); let openResponses = 0; let reasoning = null; @@ -130,9 +161,24 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { out[key] = key === 'messages' && Array.isArray(entry) ? entry.map(withReasoning) : decorateDeep(entry); return out; }; + const mapCheckpoint = (checkpoint) => + mapped && checkpoint?.checkpoint_id + ? { ...checkpoint, checkpoint_map: { '': checkpoint.checkpoint_id } } + : checkpoint; + const mapState = (state) => + mapped && state && typeof state === 'object' + ? { ...state, checkpoint: mapCheckpoint(state.checkpoint), parent_checkpoint: mapCheckpoint(state.parent_checkpoint) } + : state; /** Rewrite one SSE event for an armed injection; first AI chunk per message only. */ const decoratedChunks = new Set(); function decorateEvent(raw) { + if (mapped && /^event: *checkpoints/m.test(raw)) { + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + const value = JSON.parse(dataLine.slice(5)); + const config = value?.config?.configurable; + if (config?.checkpoint_id) value.config.configurable = mapCheckpoint(config); + raw = raw.replace(dataLine, 'data: ' + JSON.stringify(value)); + } if (!reasoning) return raw; const name = /^event: *(.*)$/m.exec(raw)?.[1]?.trim() ?? ''; const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); @@ -159,6 +205,16 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { failCreate = false; failStream = false; childFault = null; + titleMode = 'safe'; + holdNextTitle = false; + heldTitle?.response.destroy(); + heldTitle = undefined; + holdNextHistory = false; + stateMode = 'safe'; + holdNextState = false; + heldState?.response.destroy(); + heldState = undefined; + mapped = false; historyMode = 'safe'; holdNextStream = null; } @@ -188,7 +244,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { function transformHistory(input) { const states = Array.isArray(input) - ? input.map((state) => ({ ...state, values: decorateValues(state.values) })) + ? input.map((state) => mapState({ ...state, values: decorateValues(state.values) })) : input; if (!Array.isArray(states) || states.length === 0) return states; const latest = structuredClone(states[0]); @@ -209,13 +265,17 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const approval = latest.tasks?.[0]?.interrupts?.[0]?.value; if (historyMode === 'mismatch' && approval?.flight) approval.flight.flight_number = 'DL999'; if (historyMode === 'unsupported' && approval) approval.type = 'unsupported'; + if (historyMode === 'foreign' && latest.checkpoint) + latest.checkpoint = { ...latest.checkpoint, thread_id: randomUUID() }; if (historyMode === 'subgraph' && latest.checkpoint) latest.checkpoint = { ...latest.checkpoint, checkpoint_ns: 'foreign:checkpoint' }; return [latest, ...states.slice(1)]; } async function proxyHistory(request, response, path, body) { - if (historyMode === 'failed') { + const holdThis = holdNextHistory || historyMode === 'hold'; + holdNextHistory = false; + if (historyMode === 'failed' || historyMode === 'failure') { json(response, { error: 'PRIVATE history' }, 500); return; } @@ -230,7 +290,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } json(response, transformHistory(JSON.parse(raw.toString('utf8')))); }; - if (historyMode !== 'hold') return finish(); + if (!holdThis) return finish(); const held = { response, finish }; heldHistory = held; response.on('close', () => { @@ -238,6 +298,76 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { }); } + async function proxyTitle(request, response, path, body) { + const mode = titleMode; + const hold = holdNextTitle || mode === 'hold'; + holdNextTitle = false; + if (mode === 'titlefailure') { + json(response, { error: 'PRIVATE title' }, 500); + return; + } + const upstreamResponse = await send(request.method, path, request.headers, body); + const raw = await readAll(upstreamResponse); + const finish = () => { + if (response.destroyed) return; + if (upstreamResponse.statusCode !== 200) { + response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + response.end(raw); + return; + } + const thread = JSON.parse(raw.toString('utf8')); + const metadata = { ...thread.metadata }; + if (mode === 'missing') delete metadata.title; + if (mode === 'invalid') metadata.title = { invalid: true }; + if (mode === 'literal') metadata.title = ''; + if (mode === 'long') metadata.title = 'Long authored title '.repeat(30); + json(response, { + ...thread, + metadata, + ...(mode === 'wrong-thread' ? { thread_id: randomUUID() } : {}), + }); + }; + if (!hold) return finish(); + const held = { response, finish }; + heldTitle = held; + response.on('close', () => { + if (heldTitle === held) heldTitle = undefined; + }); + } + + async function proxyState(request, response, path, body) { + const mode = stateMode; + const hold = holdNextState || mode === 'hold'; + holdNextState = false; + if (mode === 'failure') { + json(response, { error: 'PRIVATE state' }, 500); + return; + } + const upstreamResponse = await send(request.method, path, request.headers, body); + const raw = await readAll(upstreamResponse); + const finish = () => { + if (response.destroyed) return; + if (upstreamResponse.statusCode !== 200) { + response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + response.end(raw); + return; + } + const state = mapState(structuredClone(JSON.parse(raw.toString('utf8')))); + state.values = decorateValues(state.values); + const messages = state.values?.messages; + if (mode === 'changed' && messages?.length) messages.at(-1).content = 'Changed canonical content'; + if (mode === 'foreign' && state.checkpoint) state.checkpoint.thread_id = randomUUID(); + if (mode === 'malformed') state.values = { ...state.values, messages: [{ type: 'human', content: { bad: true }, id: 'malformed' }] }; + json(response, state); + }; + if (!hold) return finish(); + const held = { response, finish }; + heldState = held; + response.on('close', () => { + if (heldState === held) heldState = undefined; + }); + } + async function upstreamJson(path) { const answer = await send('GET', path, { accept: 'application/json' }, Buffer.alloc(0)); const raw = await readAll(answer); @@ -267,10 +397,12 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let armed = Boolean(hold); let ended = false; const buffered = []; - const finish = async () => { - if (upstreamResponse.statusCode === 200 && threadId) { - // Saved state and title as the real server persisted them, captured - // before the browser sees the end of the stream so tests never race it. + const finish = () => { + if (!response.destroyed) response.end(); + if (upstreamResponse.statusCode !== 200 || !threadId) return; + // Saved state and title as the real server persisted them. /__captures + // waits for captures in flight, so tests never race the stream end. + const capture = (async () => { const state = await upstreamJson(`/threads/${threadId}/state`).catch(() => null); captures.push({ path: request.url && new URL(request.url, 'http://localhost').pathname, @@ -280,8 +412,9 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { titleBefore, titleAfter: await threadTitle(threadId).catch(() => null), }); - } - if (!response.destroyed) response.end(); + })(); + pendingCaptures.add(capture); + void capture.finally(() => pendingCaptures.delete(capture)); }; const held = { response, @@ -299,7 +432,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { if (ended) { if (pending) response.write(decorateEvent(pending)); pending = ''; - void finish(); + finish(); } }, }; @@ -359,7 +492,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { ended = true; if (holding) return; if (pending) response.write(decorateEvent(pending)); - void finish(); + finish(); }); response.on('close', () => { if (heldStream === held) heldStream = undefined; @@ -368,10 +501,15 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const controls = { '/__requests': ['GET', () => requests], - '/__captures': ['GET', () => captures], + '/__captures': ['GET', async () => { + await Promise.all(pendingCaptures); + return captures; + }], '/__lifetime': ['GET', () => ({ activeStream: Boolean(heldStream), activeHistory: Boolean(heldHistory), + activeTitle: Boolean(heldTitle), + activeState: Boolean(heldState), openResponses, })], '/__reset': ['POST', () => { reset(); return {}; }], @@ -393,6 +531,22 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return {}; }], '/__fail-stream': ['POST', () => { failStream = true; return {}; }], + '/__hold/title': ['POST', () => { holdNextTitle = true; return {}; }], + '/__hold/history': ['POST', () => { holdNextHistory = true; return {}; }], + '/__hold/state': ['POST', () => { holdNextState = true; return {}; }], + '/__mapped-checkpoints': ['POST', () => { mapped = true; return {}; }], + '/__release-state': ['POST', () => { + const held = heldState; + heldState = undefined; + held?.finish(); + return {}; + }], + '/__release-title': ['POST', () => { + const held = heldTitle; + heldTitle = undefined; + held?.finish(); + return {}; + }], '/__ready': ['POST', async () => { await ready(); const answer = await send('GET', '/ok', { accept: 'application/json' }, Buffer.alloc(0)); @@ -412,6 +566,24 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } return true; } + if (pathname.startsWith('/__state/')) { + const mode = pathname.slice('/__state/'.length); + if (!STATE_MODES.includes(mode)) json(response, {}, 400); + else { + stateMode = mode; + json(response, {}); + } + return true; + } + if (pathname.startsWith('/__title/')) { + const mode = pathname.slice('/__title/'.length); + if (!TITLE_MODES.includes(mode)) json(response, {}, 400); + else { + titleMode = mode; + json(response, {}); + } + return true; + } if (pathname.startsWith('/__history/')) { const mode = pathname.slice('/__history/'.length); if (!HISTORY_MODES.includes(mode)) json(response, {}, 400); @@ -470,6 +642,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { json(response, { error: 'PRIVATE graph worker failure' }, 500); } else await proxyStream(request, response, path, body); } + else if (request.method === 'POST' && /\/threads\/[^/]+\/state\/checkpoint$/.test(pathname)) + await proxyState(request, response, path, body); + else if (request.method === 'GET' && /^\/threads\/[^/]+$/.test(pathname.slice(prefix.length))) + await proxyTitle(request, response, path, body); else if (reasoning && request.method === 'GET' && /\/threads\/[^/]+\/state$/.test(pathname)) { const upstreamResponse = await send(request.method, path, request.headers, body); const raw = await readAll(upstreamResponse); diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 1c06dfe59..3d71d8a04 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -22,8 +22,6 @@ import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createChatTimelineFixture } from './chat-timeline-fixture.mjs'; -import { createChatThreadsFixture } from './chat-threads-fixture.mjs'; import { createAgUiStreamingFixture } from './ag-ui-streaming-fixture.mjs'; import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; @@ -71,8 +69,6 @@ const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && co const deepAgentsFilesystemFixture = configuration.library === 'deep-agents' && configuration.topic === 'filesystem' ? createDeepAgentsFilesystemFixture() : null; const deepAgentsPlanningFixture = configuration.library === 'deep-agents' && configuration.topic === 'planning' ? createDeepAgentsPlanningFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; -const chatTimelineFixture = configuration.library === 'chat' && configuration.topic === 'timeline' ? createChatTimelineFixture() : null; -const chatThreadsFixture = configuration.library === 'chat' && configuration.topic === 'threads' ? createChatThreadsFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by // the Playwright global setup); this server proxies /api to it. // `--backend` (Website previews) starts that backend here, lazily on the first @@ -246,8 +242,6 @@ const server = createServer(async (request, response) => { } } if (chatGenerativeUiFixture && (await chatGenerativeUiFixture(request, response, pathname))) return; - if (chatTimelineFixture && (await chatTimelineFixture(request, response, pathname))) return; - if (chatThreadsFixture && (await chatThreadsFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; if (configuration.adapter === 'none') { if (!['GET', 'HEAD'].includes(request.method)) { @@ -462,7 +456,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture || chatThreadsFixture || chatTimelineFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -475,7 +469,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture ?? chatThreadsFixture ?? chatTimelineFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 72aeb69399d96d7d59348fa4edae703c0c7d4fe6 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 15:47:41 -0800 Subject: [PATCH 06/41] test(cockpit): run React langgraph streaming/interrupts/memory/persistence/subgraphs e2e on aimock Batch 4a of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md). These React examples previously ran against pure-JS authored servers with no graph at all; they now run the real graphs under `langgraph dev`, replaying the shared Angular fixtures, and assertions follow the real graphs' output. - Proxy: immediate holds (resumes, pre-router), /__interrupt/, /__values (next run's values/updates/checkpoints state, with optional persist to the real thread), /__history-values, `paused` history mode, and /__stream/edit (drop/patch/omit/insertAfter ops; checkpoint ops address `values`) for stream shapes a real graph will not produce. - Fix: the React subgraphs app rejected every real nested route, because a real server streams the child's __start__ checkpoint with `values: {}`; only present-but-malformed child keys are unsafe now (unit-tested). - Delete the inline authored streaming handler from serve.mjs and the interrupts/memory/persistence/subgraphs fixture servers. Co-Authored-By: Claude Opus 5.5 --- .../angular/e2e/fixtures/interrupts.json | 88 ++++++ .../interrupts/react/e2e/global-setup-impl.ts | 6 + .../interrupts/react/e2e/interrupts.spec.ts | 41 ++- .../interrupts/react/e2e/playwright.config.ts | 3 + .../langgraph/interrupts/react/project.json | 2 + .../memory/angular/e2e/fixtures/memory.json | 63 +++++ .../memory/react/e2e/global-setup-impl.ts | 6 + .../langgraph/memory/react/e2e/memory.spec.ts | 32 ++- .../memory/react/e2e/playwright.config.ts | 3 + cockpit/langgraph/memory/react/project.json | 2 + .../angular/e2e/fixtures/persistence.json | 36 +++ .../react/e2e/global-setup-impl.ts | 6 + .../persistence/react/e2e/persistence.spec.ts | 28 +- .../react/e2e/playwright.config.ts | 3 + .../langgraph/persistence/react/project.json | 2 + .../angular/e2e/fixtures/streaming.json | 38 +++ .../streaming/react/e2e/global-setup-impl.ts | 6 + .../streaming/react/e2e/playwright.config.ts | 5 +- .../streaming/react/e2e/streaming.spec.ts | 16 +- .../langgraph/streaming/react/project.json | 2 + .../angular/e2e/fixtures/subgraphs.json | 57 ++++ .../subgraphs/react/e2e/global-setup-impl.ts | 6 + .../subgraphs/react/e2e/playwright.config.ts | 3 + .../subgraphs/react/e2e/subgraphs.spec.ts | 37 ++- .../langgraph/subgraphs/react/project.json | 2 + .../subgraphs/react/src/application.spec.ts | 36 +++ .../subgraphs/react/src/application.ts | 7 +- .../2026-11-18-react-cockpit-aimock-design.md | 8 +- scripts/cockpit-frontend-matrix.spec.mjs | 16 ++ scripts/react-cockpit/configuration.mjs | 28 +- scripts/react-cockpit/interrupts-fixture.mjs | 245 ----------------- scripts/react-cockpit/langgraph-proxy.mjs | 189 ++++++++++++- scripts/react-cockpit/memory-fixture.mjs | 198 -------------- scripts/react-cockpit/persistence-fixture.mjs | 224 --------------- scripts/react-cockpit/serve.mjs | 157 +---------- scripts/react-cockpit/subgraphs-fixture.mjs | 257 ------------------ 36 files changed, 729 insertions(+), 1129 deletions(-) create mode 100644 cockpit/langgraph/interrupts/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/memory/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/persistence/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/streaming/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/subgraphs/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/interrupts-fixture.mjs delete mode 100644 scripts/react-cockpit/memory-fixture.mjs delete mode 100644 scripts/react-cockpit/persistence-fixture.mjs delete mode 100644 scripts/react-cockpit/subgraphs-fixture.mjs diff --git a/cockpit/langgraph/interrupts/angular/e2e/fixtures/interrupts.json b/cockpit/langgraph/interrupts/angular/e2e/fixtures/interrupts.json index 7fcf25d0b..54d905164 100644 --- a/cockpit/langgraph/interrupts/angular/e2e/fixtures/interrupts.json +++ b/cockpit/langgraph/interrupts/angular/e2e/fixtures/interrupts.json @@ -31,6 +31,94 @@ "response": { "content": "Understood — a $129.00 refund to cus_z19fp for a chargeback. Pausing for operator approval; no refund is issued until a human reviews and approves." } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Refund" + }, + "response": { + "content": { + "customer_id": "cus_a8x2k", + "amount": 47.5, + "reason": "" + } + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Refund" + }, + "response": { "content": "Refund draft ready for review." }, + "chunkSize": 12, + "latency": 25 + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Unknown" + }, + "response": { + "content": { + "customer_id": "cus_a8x2k", + "amount": 47.5, + "reason": "" + } + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Unknown" + }, + "response": { "content": "Refund draft ready for review." }, + "chunkSize": 12, + "latency": 25 + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Malformed" + }, + "response": { + "content": { + "customer_id": "cus_a8x2k", + "amount": -1, + "reason": "" + } + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Malformed" + }, + "response": { "content": "Refund draft ready for review." }, + "chunkSize": 12, + "latency": 25 + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Multiple" + }, + "response": { + "content": { + "customer_id": "cus_a8x2k", + "amount": 47.5, + "reason": "" + } + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Multiple" + }, + "response": { "content": "Refund draft ready for review." }, + "chunkSize": 12, + "latency": 25 } ] } diff --git a/cockpit/langgraph/interrupts/react/e2e/global-setup-impl.ts b/cockpit/langgraph/interrupts/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..b5db21545 --- /dev/null +++ b/cockpit/langgraph/interrupts/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/interrupts', port: 4601 }); diff --git a/cockpit/langgraph/interrupts/react/e2e/interrupts.spec.ts b/cockpit/langgraph/interrupts/react/e2e/interrupts.spec.ts index 459f4729b..ee614bfa2 100644 --- a/cockpit/langgraph/interrupts/react/e2e/interrupts.spec.ts +++ b/cockpit/langgraph/interrupts/react/e2e/interrupts.spec.ts @@ -1,4 +1,10 @@ import { test, expect, type Page } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/interrupts/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`, which drafts +// the refund, raises the real approval interrupt and issues or cancels it on +// resume. The React static server proxies /api to it, recording requests and +// injecting holds, failures and unsupported interrupt batches +// (scripts/react-cockpit/langgraph-proxy.mjs). test.beforeEach(async ({ request }) => { await request.post('/__reset'); @@ -7,7 +13,7 @@ test('shows incremental draft text before publishing the complete approval batch page, request, }) => { - await request.post('/__hold-draft'); + await request.post('/__hold/stream', { data: { afterText: 'Refund draft' } }); await pause(page); await expect( page.getByRole('region', { name: 'Conversation' }) @@ -16,7 +22,7 @@ test('shows incremental draft text before publishing the complete approval batch page.getByRole('region', { name: 'Refund approval required' }) ).toHaveCount(0); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(true); await request.post('/__release'); await expect( @@ -60,7 +66,7 @@ test('uncertain thread creation is protected and never replayed', async ({ ).toHaveLength(0); }); for (const [label, response, final, record] of [ - ['Issue refund', { approved: true }, 'Simulated refund of $47.50', /Approved: Issue refund/], + ['Issue refund', { approved: true }, 'Refund of $47.50 issued', /Approved: Issue refund/], ["Don't refund", { approved: false }, 'Refund cancelled', /Declined: Issue refund/], ] as const) { test(`real SDK ${label} resumes the same confirmed thread without text`, async ({ @@ -131,7 +137,7 @@ test('edits an amount explicitly and rejects blank or negative values', async ({ await page.getByRole('button', { name: 'Approve edited amount' }).click(); await expect( page.getByRole('region', { name: 'Conversation' }) - ).toContainText('Simulated refund of $12.25'); + ).toContainText('Refund of $12.25 issued'); const proof = await (await request.get('/__requests')).json(); expect( proof.filter((entry: { path: string }) => @@ -143,8 +149,12 @@ test('held decision blocks duplicate clicks and Stop requires explicit fresh wor page, request, }) => { - await request.post('/__hold-resume'); await pause(page); + await expect( + page.getByRole('region', { name: 'Refund approval required' }) + ).toBeVisible(); + // The next stream is the resume; its graph emits no tokens before the result. + await request.post('/__hold/stream', { data: { immediate: true } }); const button = page.getByRole('button', { name: 'Issue refund' }); await button.evaluate((element: HTMLButtonElement) => { element.click(); @@ -158,7 +168,7 @@ test('held decision blocks duplicate clicks and Stop requires explicit fresh wor await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.locator('footer').getByRole('status')).toContainText('Stopped'); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); const proof = await (await request.get('/__requests')).json(); expect( @@ -184,6 +194,11 @@ for (const text of ['Unknown', 'Malformed', 'Multiple']) { page, request, }) => { + // Malformed: the extracted amount is -1, so the real interrupt carries it. + // Unknown/Multiple: a real graph raises one supported interrupt per pause, + // so the proxy rewrites the batch the browser receives (injected data). + if (text !== 'Malformed') + await request.post('/__interrupt/' + text.toLowerCase()); await pause(page, text); await expect(page.locator('footer').getByRole('status')).toHaveText( 'Start a new conversation to continue.' @@ -204,8 +219,11 @@ test('uncertain resume fails without replay or leaked diagnostics', async ({ page, request, }) => { - await request.post('/__fail-resume'); await pause(page); + await expect( + page.getByRole('region', { name: 'Refund approval required' }) + ).toBeVisible(); + await request.post('/__fail-stream'); await page.getByRole('button', { name: 'Issue refund' }).click(); await expect(page.getByRole('alert')).toHaveText( 'The LangGraph request failed.' @@ -222,11 +240,14 @@ test('pagehide disposes a held resume and fences late publication', async ({ page, request, }) => { - await request.post('/__hold-resume'); await pause(page); + await expect( + page.getByRole('region', { name: 'Refund approval required' }) + ).toBeVisible(); + await request.post('/__hold/stream', { data: { immediate: true } }); await page.getByRole('button', { name: 'Issue refund' }).click(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(true); await page.evaluate(() => window.dispatchEvent(new PageTransitionEvent('pagehide')) @@ -235,7 +256,7 @@ test('pagehide disposes a held resume and fences late publication', async ({ 0 ); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); await expect(page.getByRole('region', { name: 'Conversation' })).toHaveCount( diff --git a/cockpit/langgraph/interrupts/react/e2e/playwright.config.ts b/cockpit/langgraph/interrupts/react/e2e/playwright.config.ts index a62ea6e9f..9154f3fe2 100644 --- a/cockpit/langgraph/interrupts/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/interrupts/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4601', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/interrupts/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs interrupts', cwd: '../../../../..', diff --git a/cockpit/langgraph/interrupts/react/project.json b/cockpit/langgraph/interrupts/react/project.json index 5a527da7f..16c5c1a65 100644 --- a/cockpit/langgraph/interrupts/react/project.json +++ b/cockpit/langgraph/interrupts/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/interrupts/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-interrupts-angular", "cockpit-langgraph-interrupts-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -27,6 +28,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/interrupts/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/memory/angular/e2e/fixtures/memory.json b/cockpit/langgraph/memory/angular/e2e/fixtures/memory.json index 2ee8f2239..65c0c93d6 100644 --- a/cockpit/langgraph/memory/angular/e2e/fixtures/memory.json +++ b/cockpit/langgraph/memory/angular/e2e/fixtures/memory.json @@ -8,6 +8,69 @@ "content": "Hi \u2014 nice to meet you! How can I help today? If you like, tell me your name or anything you want me to remember for future chats." } }, + { + "match": { + "systemMessage": "HUMAN: Correct" + }, + "response": { + "content": "{\"favorite_drink\": \"coffee\"}" + } + }, + { + "match": { + "systemMessage": "HUMAN: Literal" + }, + "response": { + "content": "{\"__proto__\": \"literal\", \"a.b\": \"\", \"long_key_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\": \"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\"}" + } + }, + { + "match": { + "systemMessage": "HUMAN: Remember Mira" + }, + "response": { + "content": "{\"user_name\": \"Mira\", \"favorite_drink\": \"tea\"}" + } + }, + { + "match": { + "userMessage": "Recall", + "systemMessage": "user_name: Mira" + }, + "response": { + "content": "You are Mira and prefer tea." + } + }, + { + "match": { + "userMessage": "Recall" + }, + "response": { + "content": "No facts remembered." + } + }, + { + "match": { + "userMessage": "Error" + }, + "response": { + "error": { + "message": "PRIVATE response", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "userMessage": "" + }, + "response": { + "content": "Memory reply complete." + }, + "chunkSize": 12, + "latency": 25 + }, { "match": {}, "response": { diff --git a/cockpit/langgraph/memory/react/e2e/global-setup-impl.ts b/cockpit/langgraph/memory/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..61ed8260d --- /dev/null +++ b/cockpit/langgraph/memory/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/memory', port: 4602 }); diff --git a/cockpit/langgraph/memory/react/e2e/memory.spec.ts b/cockpit/langgraph/memory/react/e2e/memory.spec.ts index 04d7738e3..61df2c6eb 100644 --- a/cockpit/langgraph/memory/react/e2e/memory.spec.ts +++ b/cockpit/langgraph/memory/react/e2e/memory.spec.ts @@ -1,9 +1,27 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/memory/angular/e2e/fixtures (shared +// with the Angular example) into a real `langgraph dev`, whose extract_memory +// node merges the facts it learns. The React static server proxies /api to it, +// recording requests and injecting holds, failures and authoritative state the +// real graph never produces (scripts/react-cockpit/langgraph-proxy.mjs). +const holdExtraction = (request: APIRequestContext) => + request.post('/__hold/stream', { data: { afterText: 'Memory reply complete.' } }); +// The real graph only merges facts; replacement, malformed or absent memory +// and an unexpected pause are injected into what the browser receives (and +// persisted, so the next turn agrees). +const authoritative: Record = { + Replace: { set: { memory: { favorite_drink: 'coffee' } }, persist: { memory: { favorite_drink: 'coffee' } } }, + Malformed: { set: { memory: null }, persist: { memory: {} } }, + Absent: { omit: ['memory'], persist: { memory: {} } }, + Pause: { set: { __interrupt__: [{ id: 'unexpected', value: { kind: 'unknown' } }] } }, +}; test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); async function send(page: Page, text: string, navigate = true) { + if (text in authoritative) + await page.request.post('/__values', { data: authoritative[text] }); if (navigate) await page.goto('/langgraph/memory/react/'); await page.getByLabel('Message', { exact: true }).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -17,7 +35,7 @@ test('shows a streamed reply before held extraction publishes facts', async ({ page, request, }) => { - await request.post('/__hold-extraction'); + await holdExtraction(request); await send(page, 'Remember Mira'); await expect(transcript(page)).toContainText('Memory reply'); await expect(transcript(page)).toContainText('Memory reply complete.'); @@ -26,7 +44,7 @@ test('shows a streamed reply before held extraction publishes facts', async ({ page.getByRole('button', { name: 'New conversation' }) ).toBeDisabled(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(true); await request.post('/__release'); await expect(facts(page)).toContainText('Mira'); @@ -134,7 +152,7 @@ test('duplicate send admits one run and Stop releases held extraction', async ({ page, request, }) => { - await request.post('/__hold-extraction'); + await holdExtraction(request); await page.goto('/langgraph/memory/react/'); await page.getByLabel('Message', { exact: true }).fill('Remember Mira'); await page @@ -147,7 +165,7 @@ test('duplicate send admits one run and Stop releases held extraction', async ({ await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.locator('footer').getByRole('status')).toContainText('Stopped'); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); await expect(facts(page)).toContainText('No facts yet.'); @@ -187,7 +205,7 @@ test('pagehide disposes held extraction and fences late facts', async ({ page, request, }) => { - await request.post('/__hold-extraction'); + await holdExtraction(request); await send(page, 'Remember Mira'); await expect(transcript(page)).toContainText('Memory reply'); await page.evaluate(() => @@ -195,7 +213,7 @@ test('pagehide disposes held extraction and fences late facts', async ({ ); await expect(transcript(page)).toHaveCount(0); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); await expect(facts(page)).toHaveCount(0); diff --git a/cockpit/langgraph/memory/react/e2e/playwright.config.ts b/cockpit/langgraph/memory/react/e2e/playwright.config.ts index 3de898c39..1c2e74f0f 100644 --- a/cockpit/langgraph/memory/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/memory/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4602', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/memory/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs memory', cwd: '../../../../..', diff --git a/cockpit/langgraph/memory/react/project.json b/cockpit/langgraph/memory/react/project.json index 00e846974..ec1d2e815 100644 --- a/cockpit/langgraph/memory/react/project.json +++ b/cockpit/langgraph/memory/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/memory/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-memory-angular", "cockpit-langgraph-memory-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,6 +33,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/memory/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/persistence/angular/e2e/fixtures/persistence.json b/cockpit/langgraph/persistence/angular/e2e/fixtures/persistence.json index 0d91089db..e959c1db7 100644 --- a/cockpit/langgraph/persistence/angular/e2e/fixtures/persistence.json +++ b/cockpit/langgraph/persistence/angular/e2e/fixtures/persistence.json @@ -7,6 +7,42 @@ "response": { "content": "Hi \u2014 how can I help you today?" } + }, + { + "match": { + "userMessage": "Recall in a new conversation" + }, + "response": { + "content": "No facts remembered." + } + }, + { + "match": { + "userMessage": "Recall" + }, + "response": { + "content": "You are Avery and prefer tea." + } + }, + { + "match": { + "userMessage": "Error" + }, + "response": { + "error": { + "message": "PRIVATE response", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "userMessage": "" + }, + "response": { + "content": "Conversation saved." + } } ] } \ No newline at end of file diff --git a/cockpit/langgraph/persistence/react/e2e/global-setup-impl.ts b/cockpit/langgraph/persistence/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..540979aca --- /dev/null +++ b/cockpit/langgraph/persistence/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/persistence', port: 4604 }); diff --git a/cockpit/langgraph/persistence/react/e2e/persistence.spec.ts b/cockpit/langgraph/persistence/react/e2e/persistence.spec.ts index 9403fc501..ecb806851 100644 --- a/cockpit/langgraph/persistence/react/e2e/persistence.spec.ts +++ b/cockpit/langgraph/persistence/react/e2e/persistence.spec.ts @@ -1,4 +1,9 @@ import { test, expect, type Page } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/persistence/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`, which saves +// each thread's checkpoints. The React static server proxies /api to it, +// recording requests and injecting holds, failures and server-side history +// corrections (scripts/react-cockpit/langgraph-proxy.mjs). test.beforeEach(async ({ request }) => { await request.post('/__reset'); @@ -71,7 +76,16 @@ test('selection reads changed server history instead of cached rows; selecting t request, }) => { await two(page); - await request.post('/__replace-first'); + // The next history read (Conversation 1) returns a server-side correction. + await request.post('/__history-values', { + data: { + set: { + messages: [ + { type: 'human', id: 'server-correction', content: 'Server correction: Avery likes cocoa.' }, + ], + }, + }, + }); await page .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); @@ -98,7 +112,7 @@ test('new draft keeps confirmed entries, creates lazily, and reload has an empty await expect( page.getByRole('button', { name: 'Conversation 1', exact: true }) ).toBeVisible(); - await send(page, 'Recall'); + await send(page, 'Recall in a new conversation'); await expect(transcript(page)).toContainText('No facts remembered.'); const wire: Wire[] = await (await request.get('/__requests')).json(); const creates = wire.filter((entry) => entry.path.endsWith('/threads')); @@ -116,7 +130,7 @@ test('held history blocks competing commands; Stop quarantines only the selected request, }) => { await two(page); - await request.post('/__hold-history'); + await request.post('/__hold/history'); await page .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); @@ -158,7 +172,7 @@ for (const mode of ['fail', 'pause']) request, }) => { await two(page); - await request.post('/__' + mode + '-history'); + await request.post(mode === 'fail' ? '/__history/failed' : '/__history/paused'); await page .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); @@ -183,7 +197,7 @@ for (const mode of ['fail', 'pause']) test('pagehide releases a held history read', async ({ page, request }) => { await two(page); - await request.post('/__hold-history'); + await request.post('/__hold/history'); await page .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); @@ -192,7 +206,7 @@ test('pagehide releases a held history read', async ({ page, request }) => { ); await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeHistory) .toBe(false); await request.post('/__release'); await expect(page.locator('#root')).toBeEmpty(); @@ -203,7 +217,7 @@ test('an authoritative empty history clears previous rows', async ({ request, }) => { await two(page); - await request.post('/__empty-first'); + await request.post('/__history-values', { data: { set: { messages: [] } } }); await page .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); diff --git a/cockpit/langgraph/persistence/react/e2e/playwright.config.ts b/cockpit/langgraph/persistence/react/e2e/playwright.config.ts index 76b5ed592..723a690a8 100644 --- a/cockpit/langgraph/persistence/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/persistence/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4604', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/persistence/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs persistence', cwd: '../../../../..', diff --git a/cockpit/langgraph/persistence/react/project.json b/cockpit/langgraph/persistence/react/project.json index c6d790baa..66245d4a9 100644 --- a/cockpit/langgraph/persistence/react/project.json +++ b/cockpit/langgraph/persistence/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/persistence/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-persistence-angular", "cockpit-langgraph-persistence-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,6 +33,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/persistence/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/streaming/angular/e2e/fixtures/streaming.json b/cockpit/langgraph/streaming/angular/e2e/fixtures/streaming.json index d54869ff9..18dfecaef 100644 --- a/cockpit/langgraph/streaming/angular/e2e/fixtures/streaming.json +++ b/cockpit/langgraph/streaming/angular/e2e/fixtures/streaming.json @@ -7,6 +7,44 @@ "response": { "content": "Angular signals are a reactive primitive (signal, computed, effect) that track dependencies to provide fine-grained reactivity and more efficient change detection. They let you update state synchronously via set()/update() and ensure only consumers that read an affected signal are re\u2011evaluated." } + }, + { + "match": { + "userMessage": "First" + }, + "response": { + "content": "Live partial complete" + }, + "chunkSize": 12, + "latency": 25 + }, + { + "match": { + "userMessage": "Literal" + }, + "response": { + "content": "" + } + }, + { + "match": { + "userMessage": "Error" + }, + "response": { + "error": { + "message": "PRIVATE response", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "userMessage": "" + }, + "response": { + "content": "Second answer" + } } ] } diff --git a/cockpit/langgraph/streaming/react/e2e/global-setup-impl.ts b/cockpit/langgraph/streaming/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..391454641 --- /dev/null +++ b/cockpit/langgraph/streaming/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/streaming', port: 4600 }); diff --git a/cockpit/langgraph/streaming/react/e2e/playwright.config.ts b/cockpit/langgraph/streaming/react/e2e/playwright.config.ts index 78eec0ece..a647389dd 100644 --- a/cockpit/langgraph/streaming/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/streaming/react/e2e/playwright.config.ts @@ -6,8 +6,11 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4600', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/streaming/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { - command: 'node scripts/react-cockpit/serve.mjs', + command: 'node scripts/react-cockpit/serve.mjs streaming', cwd: '../../../../..', url: 'http://127.0.0.1:4600', reuseExistingServer: false, diff --git a/cockpit/langgraph/streaming/react/e2e/streaming.spec.ts b/cockpit/langgraph/streaming/react/e2e/streaming.spec.ts index 59d2b0207..2c679ae30 100644 --- a/cockpit/langgraph/streaming/react/e2e/streaming.spec.ts +++ b/cockpit/langgraph/streaming/react/e2e/streaming.spec.ts @@ -1,4 +1,10 @@ import { test, expect } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/streaming/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`; the React +// static server proxies /api to it, recording requests and injecting holds and +// failures (scripts/react-cockpit/langgraph-proxy.mjs). "First" streams +// "Live partial" + " complete" in two chunks; holds stop after "Live partial". +const holdAfterPartial = { data: { afterText: 'Live partial' } }; test.beforeEach(async ({ request }) => { await request.post('/__reset'); @@ -8,14 +14,15 @@ test('page exit releases the owned held stream without replay or late rendering' page, request, }) => { + await request.post('/__hold/stream', holdAfterPartial); await page.goto('/langgraph/streaming/react/'); await page.getByLabel('Message', { exact: true }).fill('First'); await page.getByRole('button', { name: 'Send', exact: true }).click(); await expect( page.getByRole('region', { name: 'Conversation' }) ).toContainText('Live partial'); - expect(await (await request.get('/__lifetime')).json()).toEqual({ - active: true, + expect(await (await request.get('/__lifetime')).json()).toMatchObject({ + activeStream: true, }); await page.evaluate(() => window.dispatchEvent(new PageTransitionEvent('pagehide')) @@ -24,7 +31,7 @@ test('page exit releases the owned held stream without replay or late rendering' 0 ); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); await expect(page.getByRole('region', { name: 'Conversation' })).toHaveCount( @@ -69,6 +76,7 @@ test('streams actual SDK requests, settles content, and keeps one conversation', page, request, }) => { + await request.post('/__hold/stream', holdAfterPartial); await page.goto('/langgraph/streaming/react/'); await page.getByLabel('Message', { exact: true }).fill('First'); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -102,6 +110,7 @@ test('Stop ends execution without replay and New conversation permits explicit f page, request, }) => { + await request.post('/__hold/stream', holdAfterPartial); await page.goto('/langgraph/streaming/react/'); await page.getByLabel('Message', { exact: true }).fill('First'); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -145,6 +154,7 @@ test('protects server diagnostics and renders backend markup literally', async ( await expect(page.getByRole('alert')).toHaveText( 'The LangGraph request failed.' ); + // aimock answers the model call for "Error" with a 500; the real graph run fails. await expect(page.locator('body')).not.toContainText('PRIVATE response'); }); diff --git a/cockpit/langgraph/streaming/react/project.json b/cockpit/langgraph/streaming/react/project.json index 2030d8279..eb3f7f718 100644 --- a/cockpit/langgraph/streaming/react/project.json +++ b/cockpit/langgraph/streaming/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/streaming/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-streaming-angular", "cockpit-langgraph-streaming-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -25,6 +26,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/streaming/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/subgraphs/angular/e2e/fixtures/subgraphs.json b/cockpit/langgraph/subgraphs/angular/e2e/fixtures/subgraphs.json index 52e417726..c8fc0975a 100644 --- a/cockpit/langgraph/subgraphs/angular/e2e/fixtures/subgraphs.json +++ b/cockpit/langgraph/subgraphs/angular/e2e/fixtures/subgraphs.json @@ -35,6 +35,63 @@ "response": { "content": "Hi! I answer questions, and when one needs a factual deep dive I hand the topic to a research subgraph before replying. Ask me something specific and you'll see it happen." } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Hello" + }, + "response": { + "content": { + "needs_research": false, + "topic": "" + } + } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Literal" + }, + "response": { + "content": { + "needs_research": true, + "topic": "" + } + } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "" + }, + "response": { + "content": { + "needs_research": true, + "topic": "Fictional checkpoint topic" + } + } + }, + { + "match": { + "systemMessage": "Research Subgraph", + "userMessage": "Topic: " + }, + "response": { "content": " CHILD-ONLY brief" } + }, + { + "match": { + "systemMessage": "Research Subgraph", + "userMessage": "Topic: Fictional checkpoint topic" + }, + "response": { "content": "CHILD-ONLY brief" } + }, + { + "match": { + "systemMessage": "Orchestrator Agent", + "userMessage": "" + }, + "response": { "content": "Parent answer" } } ] } diff --git a/cockpit/langgraph/subgraphs/react/e2e/global-setup-impl.ts b/cockpit/langgraph/subgraphs/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..a20a638e4 --- /dev/null +++ b/cockpit/langgraph/subgraphs/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/subgraphs', port: 4606 }); diff --git a/cockpit/langgraph/subgraphs/react/e2e/playwright.config.ts b/cockpit/langgraph/subgraphs/react/e2e/playwright.config.ts index f19ac699e..7dbd1b56e 100644 --- a/cockpit/langgraph/subgraphs/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/subgraphs/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4606', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/subgraphs/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs subgraphs', cwd: '../../../../..', diff --git a/cockpit/langgraph/subgraphs/react/e2e/subgraphs.spec.ts b/cockpit/langgraph/subgraphs/react/e2e/subgraphs.spec.ts index 051184b0b..fadc05f99 100644 --- a/cockpit/langgraph/subgraphs/react/e2e/subgraphs.spec.ts +++ b/cockpit/langgraph/subgraphs/react/e2e/subgraphs.spec.ts @@ -4,6 +4,26 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/subgraphs/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`: the router +// decides, the research child subgraph streams under its own namespace, and +// the answer node writes the completion markers. The React static server +// proxies /api to it, recording requests and injecting holds, failures and +// the route-authority faults below (scripts/react-cockpit/langgraph-proxy.mjs). +// Root state arrives in both `values` and `checkpoints` events. +const ROOT = '^(values|checkpoints)$'; +const routeFaults: Record = { + 'Missing marker': [{ event: ROOT, op: 'omit', keys: ['completed_turn_id'] }], + 'Stale marker': [{ event: ROOT, op: 'patch', ifKey: 'completed_turn_id', set: { completed_turn_id: 'old-human' } }], + 'Missing answer marker': [{ event: ROOT, op: 'omit', keys: ['completed_answer_id'] }], + 'Equal marker': [{ event: ROOT, op: 'patch', ifKey: 'completed_answer_id', set: { completed_answer_id: '$humanId' } }], + 'Missing child': [{ event: '\\|', op: 'drop' }], + 'Conflicting child': [{ event: '^(values|checkpoints)\\|', op: 'patch', ifKey: 'research_brief', set: { research_brief: 'Conflicting brief' } }], + 'Child pause': [{ event: '^values\\|', op: 'insertAfter', once: true, insert: { event: 'updates$ns', data: { __interrupt__: [{ value: 'PRIVATE pause' }] } } }], + 'Child error': [{ event: '^values\\|', op: 'insertAfter', once: true, insert: { event: 'error$ns', data: { message: 'PRIVATE child error' } } }], + 'Child tool': [{ event: '^messages\\|', op: 'insertAfter', once: true, insert: { event: 'messages$ns', data: [{ type: 'tool', id: 'tool-child', content: 'PRIVATE tool result', tool_call_id: 'unknown-tool' }, { langgraph_node: 'research' }] } }], + 'Root tool': [{ event: ROOT, op: 'patch', ifKey: 'completed_answer_id', path: 'messages.-1', set: { tool_calls: [{ id: 'unknown-tool', name: 'unknown', args: {} }] } }], +}; test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); @@ -14,6 +34,10 @@ const boundary = (page: Page) => const children = (page: Page) => page.getByRole('region', { name: 'Child observations', exact: true }); async function send(page: Page, text: string, navigate = true) { + // A real graph never emits these shapes, so the proxy edits the stream the + // browser receives (injected data, like the authored fixture's variants). + if (text in routeFaults) + await page.request.post('/__stream/edit', { data: { ops: routeFaults[text] } }); if (navigate) await page.goto('/langgraph/subgraphs/react/'); await page.getByLabel('Message', { exact: true }).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -90,10 +114,11 @@ test('a new admission clears the old route before its router runs', async ({ }) => { await send(page, 'Research first'); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); - await request.post('/__hold-start'); + // Hold before the router runs: right after the run's first event. + await request.post('/__hold/stream', { data: { immediate: true } }); await send(page, 'Hello', false); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(true); await expect(boundary(page)).toContainText('Awaiting the current route.'); await expect(boundary(page)).not.toContainText('CHILD-ONLY brief'); @@ -110,7 +135,7 @@ test('an observed child does not imply a completed route while held', async ({ page, request, }, testInfo) => { - await request.post('/__hold-child'); + await request.post('/__hold/stream', { data: { afterChildMessage: true } }); await send(page, 'Research first'); await expect(children(page)).toContainText('Observed child stream'); await expect(boundary(page)).toContainText('Awaiting the current route.'); @@ -160,7 +185,7 @@ for (const action of ['Stop', 'pagehide']) page, request, }) => { - await request.post('/__hold-child'); + await request.post('/__hold/stream', { data: { afterChildMessage: true } }); await send(page, 'Research first'); await expect(children(page)).toContainText('Observed child stream'); if (action === 'Stop') @@ -168,7 +193,7 @@ for (const action of ['Stop', 'pagehide']) else await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); await expect .poll( - async () => (await (await request.get('/__lifetime')).json()).active + async () => (await (await request.get('/__lifetime')).json()).activeStream ) .toBe(false); await request.post('/__release'); @@ -250,7 +275,7 @@ test('literal boundary text and full namespace fit a narrow screen', async ({ ) ).toBe(true); await expect(children(page).locator('code')).toHaveText( - /^research:00000000-0000-0000-0000-000000000001$/ + /^research:[0-9a-f-]{36}$/ ); await page.screenshot({ path: testInfo.outputPath('subgraphs-mobile.png') }); }); diff --git a/cockpit/langgraph/subgraphs/react/project.json b/cockpit/langgraph/subgraphs/react/project.json index 942b47f8d..c44b728f1 100644 --- a/cockpit/langgraph/subgraphs/react/project.json +++ b/cockpit/langgraph/subgraphs/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/subgraphs/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-subgraphs-angular", "cockpit-langgraph-subgraphs-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -25,6 +26,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/subgraphs/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/subgraphs/react/src/application.spec.ts b/cockpit/langgraph/subgraphs/react/src/application.spec.ts index 0040e7eca..01b4dae51 100644 --- a/cockpit/langgraph/subgraphs/react/src/application.spec.ts +++ b/cockpit/langgraph/subgraphs/react/src/application.spec.ts @@ -241,6 +241,42 @@ it('serializes creation and keeps child observations readonly until final bindin }); expect(contents(h)).toEqual(['Question', 'Parent answer']); }); +it('a child input checkpoint without fields precedes a confirmed nested route', async () => { + // A real LangGraph server streams the child's `__start__` checkpoint with + // `values: {}` before the child's first node writes topic and brief. + const h = setup(), + { pending, selected } = await h.start(); + selected.replace({ + subgraphs: [{ ...selected.child(), values: {} }], + }); + selected.research(); + selected.final(); + selected.runs[0].resolve('success'); + await pending; + expect(h.application.getSnapshot()).toMatchObject({ + route: 'nested', + topic: 'Topic', + brief: 'Child brief', + canSubmit: true, + }); +}); +it('a child with a malformed topic stays unconfirmed', async () => { + const h = setup(), + { pending, selected } = await h.start(); + selected.replace({ + subgraphs: [ + { ...selected.child(), values: { research_topic: 42, research_brief: '' } }, + ], + }); + selected.research(); + selected.final(); + selected.runs[0].resolve('success'); + await pending; + expect(h.application.getSnapshot()).toMatchObject({ + route: 'unconfirmed', + canSubmit: false, + }); +}); it('research then greeting clears stale boundary and children while preserving parent history', async () => { const h = setup(), selected = await h.complete(); diff --git a/cockpit/langgraph/subgraphs/react/src/application.ts b/cockpit/langgraph/subgraphs/react/src/application.ts index 3996891d9..04757ede7 100644 --- a/cockpit/langgraph/subgraphs/react/src/application.ts +++ b/cockpit/langgraph/subgraphs/react/src/application.ts @@ -175,9 +175,14 @@ export function createSubgraphsApplication(options: { const topic = own(child.state, 'research_topic'), brief = own(child.state, 'research_brief'); if (!valid()) return; + // A child's input checkpoint (`values: {}` before its first node) + // carries neither key yet; only present but malformed keys are + // unsafe. A nested route still needs the latest child values to + // match the parent's topic and brief before it is confirmed. if ( child.state !== undefined && - (typeof topic !== 'string' || typeof brief !== 'string') + ((topic !== undefined && typeof topic !== 'string') || + (brief !== undefined && typeof brief !== 'string')) ) selected.unsafe = true; selected.children.set( diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index c7a9a589a..af3a39a51 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -74,6 +74,11 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | `/__title/`, `/__hold-title` (batch 3) | authored thread metadata | `GET /threads/` rewrites (`missing`, `wrong-thread`, `invalid`, `titlefailure`, `literal`, `long`), with the mode captured when the read arrives; `/__hold/title` | | `/__state/`, `/__hold-state`, `/__mapped-checkpoints` (batch 3) | authored checkpoint reads | the same rewrites on the real `POST /threads//state/checkpoint`; mapped mode adds the root `checkpoint_map` to history, state and `checkpoints` events | | `/__metadata-failure` + `/__title-model-failure` (timeline) | two title faults | one test: a per-prompt title-model error. Both faults end on the same swallowed exception in `generate_title`; the metadata read itself runs inside `langgraph dev` and can't be failed from outside | + | `/__hold-draft`, `/__hold-extraction`, `/__hold-start`, `/__hold-resume`, `/__hold-history` (4a) | authored stop points | `/__hold/stream` with `{ afterText }` or `{ immediate }` (the next stream: a resume, or a run held before its router), and `/__hold/history` | + | interrupts `Unknown` / `Multiple` (4a) | authored interrupt batches | `/__interrupt/` rewrites the root interrupts the browser receives (injected data). `Malformed` is real: the extractor fixture returns amount -1 | + | memory `Replace` / `Malformed` / `Absent` / `Pause` (4a) | authored memory state | `/__values` sets or omits keys in the next run's `values`, `updates` and `checkpoints` state. `persist` writes the cleared memory to the real thread, so the next turn agrees (the real graph only merges facts) | + | persistence `/__replace-first`, `/__empty-first`, `/__pause-history` (4a) | authored server history | `/__history-values` (one-shot latest-values rewrite) and a `paused` history mode (pending + unknown interrupt task) | + | subgraphs route faults (4a) | ten authored stream variants | `/__stream/edit`: drop, patch, omit or insert-after on matching events. Ops on `checkpoints` events address their `values`, because the session reads root state from both | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -163,7 +168,8 @@ example needs that the pilot proxy doesn't yet provide. | 2b | chat/generative-ui | `/__configure` faults that swap graph internals: a throwing KPI tool, malformed trend data, prose injected into the `admit` node's output, partial layouts | aimock can't produce tool-code failures or node-output rewrites, so this needs a decision: move the graph-level faults to Python tests, or keep a small authored layer | | 3 (done) | chat/threads, chat/timeline | `/__title/`, `/__hold/title`, `/__state/`, `/__hold/state`, `/__hold/history`, `/__mapped-checkpoints`, `foreign`/`failure` history modes | a mid-stream model failure is aimock `truncateAfterChunks`; stream bodies come from `/__captures[].sse`, because DevTools can't return a streamed body the client closes | | 3b | chat/debug | `/__hold/current`, `/__hold/checkpoint`, selected-read variants, `captures[].proof.providerVariant` | its fixture also backs the packed-consumer native-session proof (`chat-debug-native-session-probe.mjs`, its own CI step), so it migrates together with that proof | -| 4 | langgraph/{streaming, persistence, memory, interrupts, subgraphs, client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-draft`, `/__hold-extraction`, `/__hold-plan`, `/__publish-*`, `/__bad-history`, `/__unsafe-source` | `durable-execution` `/__publish-*` injects authored events that a real graph can't emit, so it keeps a small authored layer on top of the proxy | +| 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | +| 4b | langgraph/{client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-calls`, `/__fail-state`, `/__threads`, `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__hold-plan`, `/__publish-*` | `durable-execution` `/__publish-*` injects authored events: candidates for `/__stream/edit` | | 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | | 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index cb8bf138d..2ffa72505 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -425,6 +425,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/subgraphs/react/', port: 4606, project: 'cockpit-langgraph-subgraphs-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/subgraphs/python', + fixtures: 'cockpit/langgraph/subgraphs/angular/e2e/fixtures', langgraphPort: 5606, + }, }); assert.deepEqual(reactCockpitConfiguration('durable-execution'), { topic: 'durable-execution', @@ -439,6 +443,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/persistence/react/', port: 4604, project: 'cockpit-langgraph-persistence-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/persistence/python', + fixtures: 'cockpit/langgraph/persistence/angular/e2e/fixtures', langgraphPort: 5604, + }, }); assert.deepEqual(reactCockpitConfiguration('interrupts'), { topic: 'interrupts', @@ -446,6 +454,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/interrupts/react/', port: 4601, project: 'cockpit-langgraph-interrupts-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/interrupts/python', + fixtures: 'cockpit/langgraph/interrupts/angular/e2e/fixtures', langgraphPort: 5601, + }, }); assert.deepEqual(reactCockpitConfiguration('memory'), { topic: 'memory', @@ -453,6 +465,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/memory/react/', port: 4602, project: 'cockpit-langgraph-memory-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/memory/python', + fixtures: 'cockpit/langgraph/memory/angular/e2e/fixtures', langgraphPort: 5602, + }, }); assert.deepEqual(reactCockpitConfiguration('client-tools'), { topic: 'client-tools', diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 765795554..7fedd5e8b 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -11,6 +11,8 @@ const aimockLanggraph = (example, port) => Object.freeze({ fixtures: `cockpit/${example}/angular/e2e/fixtures`, langgraphPort: port + 1000, }); +/** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ +const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs']); export function reactCockpitConfiguration(topic = 'streaming') { if (topic === 'ag-ui-a2ui') return Object.freeze({ @@ -240,21 +242,25 @@ export function reactCockpitConfiguration(topic = 'streaming') { ].includes(topic) ) throw new Error(`Unsupported React cockpit topic: ${topic}`); + const port = { + streaming: 4600, + interrupts: 4601, + memory: 4602, + 'client-tools': 4603, + persistence: 4604, + 'durable-execution': 4605, + subgraphs: 4606, + 'time-travel': 4607, + 'deployment-runtime': 4608, + }[topic]; return Object.freeze({ topic, appPath: `cockpit/langgraph/${topic}/react`, base: `/langgraph/${topic}/react/`, - port: { - streaming: 4600, - interrupts: 4601, - memory: 4602, - 'client-tools': 4603, - persistence: 4604, - 'durable-execution': 4605, - subgraphs: 4606, - 'time-travel': 4607, - 'deployment-runtime': 4608, - }[topic], + port, project: `cockpit-langgraph-${topic}-react`, + ...(AIMOCK_LANGGRAPH_TOPICS.has(topic) + ? { backend: aimockLanggraph(`langgraph/${topic}`, port) } + : {}), }); } diff --git a/scripts/react-cockpit/interrupts-fixture.mjs b/scripts/react-cockpit/interrupts-fixture.mjs deleted file mode 100644 index 44152c4f9..000000000 --- a/scripts/react-cockpit/interrupts-fixture.mjs +++ /dev/null @@ -1,245 +0,0 @@ -/** Local real-SDK HTTP/SSE proof. No fixture routes or keys are deployed. */ -export function createInterruptsFixture() { - let requests = []; - const runs = new Map(); - const threads = new Map(); - let sequence = 0; - let held; - let holdResume = false; - let holdDraft = false; - let failResume = false; - let failCreation = false; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - function release() { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - } - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - runs.clear(); - threads.clear(); - sequence = 0; - holdResume = holdDraft = failResume = failCreation = false; - json(response, {}); - return true; - } - if (pathname === '/__release') { - release(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__hold-resume') { - holdResume = true; - json(response, {}); - return true; - } - if (pathname === '/__hold-draft') { - holdDraft = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-resume') { - failResume = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreation = true; - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation response' }, 500); - return true; - } - threads.set(body.thread_id, { messages: [] }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - if (pathname.endsWith('/runs/stream')) { - const state = threads.get(threadId); - if (!state) { - json(response, { error: 'Unconfirmed thread' }, 400); - return true; - } - const resumed = Object.hasOwn(body, 'command'); - if (resumed && failResume) { - failResume = false; - json(response, { error: 'PRIVATE resume response' }, 500); - return true; - } - if ( - resumed && - (body.input !== null || !Object.hasOwn(body.command, 'resume')) - ) { - json(response, { error: 'Expected explicit resume' }, 400); - return true; - } - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - if (!resumed) { - const human = body.input.messages; - const text = human.at(-1).content; - const refund = { - kind: 'refund_approval', - amount: 47.5, - customer_id: 'cus_a8x2k', - reason: '', - }; - const value = - text === 'Unknown' - ? { kind: 'unknown' } - : text === 'Malformed' - ? { ...refund, amount: -1 } - : refund; - const interrupts = [ - { id: 'refund-1', value }, - ...(text === 'Multiple' ? [{ id: 'refund-2', value: refund }] : []), - ]; - state.messages = [...state.messages, ...human]; - response.write(event('values', { messages: state.messages })); - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'draft-' + runId, - content: 'Refund draft', - }, - { langgraph_node: 'draft' }, - ]) - ); - const finish = () => { - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'draft-' + runId, - content: ' ready for review.', - }, - { langgraph_node: 'draft' }, - ]) - ); - state.messages = [ - ...state.messages, - { - type: 'ai', - id: 'draft-' + runId, - content: 'Refund draft ready for review.', - }, - ]; - response.write( - event('values', { - messages: state.messages, - __interrupt__: interrupts, - }) - ); - runs.get(runId).status = 'interrupted'; - response.end(); - }; - if (holdDraft) { - holdDraft = false; - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - } else { - response.write(event('values', { messages: state.messages })); - const decision = body.command.resume; - const answer = decision.approved - ? `Simulated refund of $${(decision.amount ?? 47.5).toFixed( - 2 - )} issued.` - : 'Refund cancelled by operator.'; - const finish = () => { - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'result-' + runId, - content: answer, - }, - { langgraph_node: 'issue' }, - ]) - ); - state.messages = [ - ...state.messages, - { type: 'ai', id: 'result-' + runId, content: answer }, - ]; - response.write(event('values', { messages: state.messages })); - runs.get(runId).status = 'success'; - response.end(); - }; - if (holdResume) { - holdResume = false; - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - } - return true; - } - if (pathname.endsWith('/cancel')) { - held?.response.end(); - held = undefined; - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, {}); - return true; - }; -} diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 313d4dc03..fa554b93b 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -22,7 +22,8 @@ * or tool evidence: args | result | name | ids | * interrupt | subgraph, or approval evidence: * mismatch | unsupported; foreign (checkpoint of - * another thread); failure (= failed) + * another thread); failure (= failed); paused + * (pending with an unknown interrupt task) * POST /__title/ thread metadata read: safe | hold | missing | * wrong-thread | invalid | titlefailure | literal | * long (mode captured when the read arrives) @@ -38,6 +39,10 @@ * some deployments return * POST /__release-title finish a held metadata read * POST /__release-history finish a held history read + * POST /__history-values body { set?, omit? }: the next history read's + * latest values get keys set or removed (an + * authoritative server-side correction, e.g. + * replaced or emptied messages). Injected data. * POST /__hold/stream hold the next run stream; body { afterText? } holds * after the first AI message chunk (or once the AI text * contains afterText); { beforeToolResult: true } @@ -50,6 +55,32 @@ * or a child `error|…` event carrying a private * diagnostic. Injected data, as the authored * subagents fixture did. + * { immediate: true } holds right after the first + * event (e.g. a resume whose graph emits no tokens) + * POST /__interrupt/ unknown | multiple: until reset, root interrupts + * the browser receives are rewritten to an + * unsupported kind, or duplicated into a batch of + * two. Injected data: a real graph emits one + * supported interrupt per pause. + * POST /__values body { set?, omit?, persist? }: the next run's + * `values` events get keys set or removed (injected + * authoritative state a real graph won't produce: + * replaced or malformed fields, an unexpected + * pause). `persist` is written to the real thread + * before the stream ends so later turns agree. + * POST /__stream/edit body { ops }: edits to the next run stream, each + * { event: regex, op, ... } applied to matching SSE + * events: drop | patch { path?, set } | omit + * { path?, keys } | insertAfter { insert: { event, + * data } } (`$ns` in the inserted name is the + * matched event's `|namespace`; the string + * "$humanId" in data is the submitted message id). + * `once: true` applies an op to its first match only; + * `ifKey` only to events whose state has that key. + * For `checkpoints` events, ops address `values`. + * Injected data for stream shapes a real graph + * won't produce (missing/stale markers, child + * pauses, errors and tool calls). * POST /__fail-stream next run stream answers 500 without forwarding * (the graph worker is unavailable) * POST /__release flush and resume a held run stream @@ -71,7 +102,7 @@ import { request as httpRequest } from 'node:http'; const HISTORY_MODES = [ 'safe', 'hold', 'changed', 'missing', 'pending', 'failed', 'args', 'result', 'name', 'ids', 'interrupt', 'subgraph', - 'mismatch', 'unsupported', 'foreign', 'failure', + 'mismatch', 'unsupported', 'foreign', 'failure', 'paused', ]; // Rewrites of checkpoint state reads (POST /threads//state/checkpoint). const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed']; @@ -134,6 +165,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let holdNextState = false; let heldState; let mapped = false; + let interruptMode = null; + let nextValues = null; + let nextHistoryValues = null; + let nextStreamEdits = null; let historyMode = 'safe'; let holdNextStream = null; let heldStream; @@ -169,6 +204,19 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { mapped && state && typeof state === 'object' ? { ...state, checkpoint: mapCheckpoint(state.checkpoint), parent_checkpoint: mapCheckpoint(state.parent_checkpoint) } : state; + const rewriteInterrupts = (value) => { + if (Array.isArray(value)) return value.map(rewriteInterrupts); + if (!value || typeof value !== 'object') return value; + const out = {}; + for (const [key, entry] of Object.entries(value)) { + if (key === '__interrupt__' && Array.isArray(entry) && entry.length) { + out[key] = interruptMode === 'unknown' + ? entry.map((item) => ({ ...item, value: { kind: 'unknown' } })) + : [...entry, { ...entry[0], id: `${entry[0].id ?? 'interrupt'}-duplicate` }]; + } else out[key] = rewriteInterrupts(entry); + } + return out; + }; /** Rewrite one SSE event for an armed injection; first AI chunk per message only. */ const decoratedChunks = new Set(); function decorateEvent(raw) { @@ -179,6 +227,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { if (config?.checkpoint_id) value.config.configurable = mapCheckpoint(config); raw = raw.replace(dataLine, 'data: ' + JSON.stringify(value)); } + if (interruptMode && raw.includes('"__interrupt__"')) { + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + raw = raw.replace(dataLine, 'data: ' + JSON.stringify(rewriteInterrupts(JSON.parse(dataLine.slice(5))))); + } if (!reasoning) return raw; const name = /^event: *(.*)$/m.exec(raw)?.[1]?.trim() ?? ''; const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); @@ -215,6 +267,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { heldState?.response.destroy(); heldState = undefined; mapped = false; + interruptMode = null; + nextValues = null; + nextHistoryValues = null; + nextStreamEdits = null; historyMode = 'safe'; holdNextStream = null; } @@ -242,12 +298,16 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return Buffer.concat(chunks); } - function transformHistory(input) { + function transformHistory(input, rewrite) { const states = Array.isArray(input) ? input.map((state) => mapState({ ...state, values: decorateValues(state.values) })) : input; if (!Array.isArray(states) || states.length === 0) return states; const latest = structuredClone(states[0]); + if (rewrite) { + latest.values = { ...latest.values, ...(rewrite.set ?? {}) }; + for (const key of rewrite.omit ?? []) delete latest.values[key]; + } const messages = latest.values?.messages ?? []; const assistant = messages.find((m) => m.tool_calls?.length); const result = messages.find((m) => m.type === 'tool'); @@ -255,6 +315,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { messages.at(-1).content = 'Changed saved answer'; if (historyMode === 'missing') messages.pop(); if (historyMode === 'pending') latest.next = ['unexpected']; + if (historyMode === 'paused') { + latest.next = ['unknown']; + latest.tasks = [{ id: 'task', name: 'unknown', interrupts: [{ id: 'pause', value: { kind: 'unknown' } }] }]; + } if (historyMode === 'args' && assistant) assistant.tool_calls[0].args = { flight_number: 'Changed' }; if (historyMode === 'result' && result) result.content = 'Changed tool observation'; @@ -275,6 +339,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { async function proxyHistory(request, response, path, body) { const holdThis = holdNextHistory || historyMode === 'hold'; holdNextHistory = false; + const rewrite = nextHistoryValues; + nextHistoryValues = null; if (historyMode === 'failed' || historyMode === 'failure') { json(response, { error: 'PRIVATE history' }, 500); return; @@ -288,7 +354,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { response.end(raw); return; } - json(response, transformHistory(JSON.parse(raw.toString('utf8')))); + json(response, transformHistory(JSON.parse(raw.toString('utf8')), rewrite)); }; if (!holdThis) return finish(); const held = { response, finish }; @@ -383,6 +449,83 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { holdNextStream = null; let fault = childFault; childFault = null; + const values = nextValues; + nextValues = null; + const edits = (nextStreamEdits ?? []).map((edit) => ({ ...edit, pattern: new RegExp(edit.event), used: false })); + nextStreamEdits = null; + let humanId = null; + try { humanId = JSON.parse(body.toString('utf8')).input?.messages?.at(-1)?.id ?? null; } catch { /* no input */ } + const withHuman = (value) => JSON.parse(JSON.stringify(value).replaceAll('"$humanId"', JSON.stringify(humanId))); + const at = (root, path) => { + let target = root; + for (const part of (path ?? '').split('.').filter(Boolean)) { + if (target == null) return null; + target = Array.isArray(target) ? target.at(Number(part)) : target[part]; + } + return target; + }; + const edit = (raw) => { + if (!edits.length) return raw; + const name = /^event: *([^\r\n]*)/m.exec(raw)?.[1]?.trim() ?? ''; + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + const data = dataLine ? JSON.parse(dataLine.slice(5)) : null; + let out = null; + const after = []; + let changed = false; + for (const op of edits) { + if ((op.once && op.used) || !op.pattern.test(name)) continue; + // A `checkpoints` event carries state under `values`; ops address the state. + const state = name.startsWith('checkpoints') ? data?.values : data; + if (op.ifKey && !(state && typeof state === 'object' && op.ifKey in state)) continue; + op.used = true; + if (op.op === 'drop') return ''; + if (op.op === 'patch' || op.op === 'omit') { + const target = at(state, op.path); + if (!target || typeof target !== 'object') continue; + if (op.op === 'patch') Object.assign(target, withHuman(op.set ?? {})); + else for (const key of op.keys ?? []) delete target[key]; + changed = true; + } + if (op.op === 'insertAfter') { + const namespace = name.includes('|') ? name.slice(name.indexOf('|')) : ''; + after.push(`event: ${op.insert.event.replace('$ns', namespace)}\ndata: ${JSON.stringify(withHuman(op.insert.data))}\n\n`); + } + } + out = changed ? raw.replace(dataLine, 'data: ' + JSON.stringify(data)) : raw; + return out + after.join(''); + }; + const apply = (state) => { + if (!state || typeof state !== 'object' || Array.isArray(state)) return state; + const out = { ...state, ...(values.set ?? {}) }; + for (const key of values.omit ?? []) delete out[key]; + return out; + }; + const touched = (state) => + state && typeof state === 'object' && + [...Object.keys(values.set ?? {}), ...(values.omit ?? [])].some((key) => key in state); + const inject = (raw) => { + raw = edit(raw); + if (!raw) return raw; + if (raw.indexOf('event:') !== raw.lastIndexOf('event:')) + return raw.split(/(?<=\r?\n\r?\n)/).map((part) => inject2(part)).join(''); + return inject2(raw); + }; + const inject2 = (raw) => { + raw = decorateEvent(raw); + if (!values) return raw; + // Root graph only: `values`, node outputs in `updates`, and the values a + // `checkpoints` event carries, so every view of the state agrees. + const name = /^event: *([^\r\n]*)/m.exec(raw)?.[1]?.trim(); + if (!['values', 'updates', 'checkpoints'].includes(name)) return raw; + const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); + let value = JSON.parse(dataLine.slice(5)); + if (name === 'values') value = apply(value); + else if (name === 'checkpoints') value = { ...value, values: apply(value.values) }; + else + value = Object.fromEntries(Object.entries(value).map(([node, output]) => + [node, touched(output) ? apply(output) : output])); + return raw.replace(dataLine, 'data: ' + JSON.stringify(value)); + }; const threadId = /\/threads\/([^/]+)\/runs\/stream/.exec(path)?.[1]; const titleBefore = threadId ? await threadTitle(threadId) : null; const upstreamResponse = await send(request.method, path, request.headers, body); @@ -397,7 +540,16 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let armed = Boolean(hold); let ended = false; const buffered = []; + let persisted = false; const finish = () => { + if (values?.persist && threadId && !persisted) { + persisted = true; + const update = Buffer.from(JSON.stringify({ values: values.persist })); + void send('POST', `/threads/${threadId}/state`, { 'content-type': 'application/json' }, update) + .then(readAll, () => undefined) + .finally(finish); + return; + } if (!response.destroyed) response.end(); if (upstreamResponse.statusCode !== 200 || !threadId) return; // Saved state and title as the real server persisted them. /__captures @@ -425,12 +577,12 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let match; while ((match = /\r?\n\r?\n/.exec(rest))) { const end = match.index + match[0].length; - response.write(decorateEvent(rest.slice(0, end))); + response.write(inject(rest.slice(0, end))); rest = rest.slice(end); } pending = rest + pending; if (ended) { - if (pending) response.write(decorateEvent(pending)); + if (pending) response.write(inject(pending)); pending = ''; finish(); } @@ -467,7 +619,13 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { pending = raw + pending; break; } - response.write(decorateEvent(raw)); + response.write(inject(raw)); + if (armed && hold.immediate) { + armed = false; + holding = true; + heldStream = held; + continue; + } if (armed && hold.afterChildMessage && eventName.startsWith('messages|') && aiText(raw)) { armed = false; holding = true; @@ -477,7 +635,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const delta = aiText(raw); if (delta === null) continue; text += delta; - if (armed && !hold.beforeToolResult && !hold.afterChildMessage && (hold.afterText ? text.includes(hold.afterText) : true)) { + if (armed && !hold.beforeToolResult && !hold.afterChildMessage && !hold.immediate && (hold.afterText ? text.includes(hold.afterText) : true)) { armed = false; holding = true; heldStream = held; @@ -491,7 +649,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { upstreamResponse.on('end', () => { ended = true; if (holding) return; - if (pending) response.write(decorateEvent(pending)); + if (pending) response.write(inject(pending)); finish(); }); response.on('close', () => { @@ -527,10 +685,14 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { afterText: body.afterText ?? null, beforeToolResult: body.beforeToolResult === true, afterChildMessage: body.afterChildMessage === true, + immediate: body.immediate === true, }; return {}; }], '/__fail-stream': ['POST', () => { failStream = true; return {}; }], + '/__values': ['POST', (body) => { nextValues = body; return {}; }], + '/__stream/edit': ['POST', (body) => { nextStreamEdits = body.ops ?? []; return {}; }], + '/__history-values': ['POST', (body) => { nextHistoryValues = body; return {}; }], '/__hold/title': ['POST', () => { holdNextTitle = true; return {}; }], '/__hold/history': ['POST', () => { holdNextHistory = true; return {}; }], '/__hold/state': ['POST', () => { holdNextState = true; return {}; }], @@ -566,6 +728,15 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } return true; } + if (pathname.startsWith('/__interrupt/')) { + const mode = pathname.slice('/__interrupt/'.length); + if (!['unknown', 'multiple'].includes(mode)) json(response, {}, 400); + else { + interruptMode = mode; + json(response, {}); + } + return true; + } if (pathname.startsWith('/__state/')) { const mode = pathname.slice('/__state/'.length); if (!STATE_MODES.includes(mode)) json(response, {}, 400); diff --git a/scripts/react-cockpit/memory-fixture.mjs b/scripts/react-cockpit/memory-fixture.mjs deleted file mode 100644 index cf4296d7c..000000000 --- a/scripts/react-cockpit/memory-fixture.mjs +++ /dev/null @@ -1,198 +0,0 @@ -/** Local real-SDK HTTP/SSE proof. Never included in public deployment. */ -export function createMemoryFixture() { - let requests = []; - const threads = new Map(); - const runs = new Map(); - let sequence = 0; - let held; - let holdExtraction = false; - let failCreation = false; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - threads.clear(); - runs.clear(); - requests = []; - sequence = 0; - holdExtraction = failCreation = false; - json(response, {}); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__hold-extraction') { - holdExtraction = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreation = true; - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation response' }, 500); - return true; - } - threads.set(body.thread_id, { messages: [], memory: {} }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - if (pathname.endsWith('/runs/stream')) { - const state = threads.get(threadId); - if ( - !state || - body.assistant_id !== 'memory' || - body.command || - !Array.isArray(body.input?.messages) - ) { - json( - response, - { error: 'Expected confirmed memory message input' }, - 400 - ); - return true; - } - const text = body.input.messages.at(-1).content; - if (text === 'Error') { - json(response, { error: 'PRIVATE run response' }, 500); - return true; - } - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - state.messages = [...state.messages, ...body.input.messages]; - response.write(event('values', state)); - const answer = - text === 'Recall' - ? state.memory?.user_name - ? `You are ${state.memory.user_name} and prefer ${state.memory.favorite_drink}.` - : 'No facts remembered.' - : 'Memory reply complete.'; - const partial = text === 'Recall' ? answer : 'Memory reply'; - response.write( - event('messages', [ - { type: 'AIMessageChunk', id: 'answer-' + runId, content: partial }, - { langgraph_node: 'generate' }, - ]) - ); - if (text !== 'Recall') - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'answer-' + runId, - content: ' complete.', - }, - { langgraph_node: 'generate' }, - ]) - ); - state.messages = [ - ...state.messages, - { type: 'ai', id: 'answer-' + runId, content: answer }, - ]; - response.write(event('values', state)); - const finish = () => { - const memory = - text === 'Correct' - ? { ...state.memory, favorite_drink: 'coffee' } - : text === 'Replace' - ? { favorite_drink: 'coffee' } - : text === 'Literal' - ? JSON.parse( - '{"__proto__":"literal","a.b":"","long_key_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz":"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"}' - ) - : text === 'Malformed' - ? null - : text === 'Absent' - ? undefined - : text === 'Recall' || text === 'Pause' - ? state.memory - : { user_name: 'Mira', favorite_drink: 'tea' }; - state.memory = memory; - const interrupts = - text === 'Pause' - ? [{ id: 'unexpected', value: { kind: 'unknown' } }] - : []; - response.write( - event('values', { - ...state, - ...(interrupts.length ? { __interrupt__: interrupts } : {}), - }) - ); - runs.get(runId).status = interrupts.length ? 'interrupted' : 'success'; - response.end(); - }; - if (holdExtraction) { - holdExtraction = false; - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/cancel')) { - held?.response.end(); - held = undefined; - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, {}); - return true; - }; -} diff --git a/scripts/react-cockpit/persistence-fixture.mjs b/scripts/react-cockpit/persistence-fixture.mjs deleted file mode 100644 index 34a2c88c6..000000000 --- a/scripts/react-cockpit/persistence-fixture.mjs +++ /dev/null @@ -1,224 +0,0 @@ -/** Local installed-SDK HTTP/SSE proof; never assembled into public assets. */ -export function createPersistenceFixture() { - let requests = [], - sequence = 0, - held; - let holdHistory = false, - failHistory = false, - pauseHistory = false, - failCreation = false; - const threads = new Map(), - runs = new Map(); - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - threads.clear(); - runs.clear(); - requests = []; - sequence = 0; - holdHistory = failHistory = pauseHistory = failCreation = false; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (pathname === '/__hold-history') { - holdHistory = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-history') { - failHistory = true; - json(response, {}); - return true; - } - if (pathname === '/__pause-history') { - pauseHistory = true; - json(response, {}); - return true; - } - if (pathname === '/__fail-create') { - failCreation = true; - json(response, {}); - return true; - } - if (pathname === '/__empty-first') { - const state = threads.values().next().value; - if (state) state.messages = []; - json(response, {}); - return true; - } - if (pathname === '/__replace-first') { - const state = threads.values().next().value; - if (state) - state.messages = [ - { - type: 'ai', - id: 'server-correction', - content: 'Server correction: Avery likes cocoa.', - }, - ]; - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - threads.set(body.thread_id, { messages: [] }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - const state = threads.get(threadId); - if (pathname.endsWith('/history')) { - if (request.method !== 'POST' || !state) { - json(response, { error: 'Unknown thread' }, 404); - return true; - } - if (failHistory) { - failHistory = false; - json(response, { error: 'PRIVATE history' }, 500); - return true; - } - const paused = pauseHistory; - pauseHistory = false; - const finish = () => - json(response, [ - { - values: state, - next: paused ? ['unknown'] : [], - checkpoint: { - thread_id: threadId, - checkpoint_ns: '', - checkpoint_id: 'checkpoint-' + sequence, - }, - metadata: {}, - created_at: '2026-10-01T00:00:00Z', - parent_checkpoint: null, - tasks: paused - ? [ - { - id: 'task', - name: 'unknown', - interrupts: [{ id: 'pause', value: { kind: 'unknown' } }], - }, - ] - : [], - }, - ]); - if (holdHistory) { - holdHistory = false; - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream')) { - if ( - !state || - body.assistant_id !== 'persistence' || - body.command || - !Array.isArray(body.input?.messages) - ) { - json( - response, - { error: 'Expected confirmed persistence message input' }, - 400 - ); - return true; - } - const text = body.input.messages.at(-1).content; - if (text === 'Error') { - json(response, { error: 'PRIVATE run' }, 500); - return true; - } - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - const remembered = state.messages.find((message) => - /Remember (\w+) likes (\w+)/.test(message.content) - ); - const match = remembered?.content.match(/Remember (\w+) likes (\w+)/); - const answer = - text === 'Recall' - ? match - ? `You are ${match[1]} and prefer ${match[2]}.` - : 'No facts remembered.' - : 'Conversation saved.'; - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - state.messages = [...state.messages, ...body.input.messages]; - response.write(event('values', state)); - response.write( - event('messages', [ - { type: 'AIMessageChunk', id: 'answer-' + runId, content: answer }, - { langgraph_node: 'generate' }, - ]) - ); - state.messages = [ - ...state.messages, - { type: 'ai', id: 'answer-' + runId, content: answer }, - ]; - response.write(event('values', state)); - runs.get(runId).status = 'success'; - response.end(); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, {}); - return true; - }; -} diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 3d71d8a04..cbccb05a1 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -9,13 +9,9 @@ import { createAgUiA2uiFixture } from './ag-ui-a2ui-fixture.mjs'; import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; -import { createInterruptsFixture } from './interrupts-fixture.mjs'; -import { createMemoryFixture } from './memory-fixture.mjs'; import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; import { createClientToolsFixture } from './client-tools-fixture.mjs'; -import { createPersistenceFixture } from './persistence-fixture.mjs'; import { createDurableFixture } from './durable-fixture.mjs'; -import { createSubgraphsFixture } from './subgraphs-fixture.mjs'; import { createTimeTravelFixture } from './time-travel-fixture.mjs'; import { createDeploymentRuntimeFixture } from './deployment-runtime-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; @@ -111,18 +107,8 @@ const deploymentRuntimeFixture = : null; const timeTravelFixture = configuration.topic === 'time-travel' ? createTimeTravelFixture() : null; -const subgraphsFixture = - configuration.topic === 'subgraphs' ? createSubgraphsFixture() : null; const durableFixture = configuration.topic === 'durable-execution' ? createDurableFixture() : null; -const persistenceFixture = - configuration.topic === 'persistence' ? createPersistenceFixture() : null; -const interruptsFixture = - configuration.adapter !== 'ag-ui' && configuration.library !== 'chat' && configuration.topic === 'interrupts' - ? createInterruptsFixture() - : null; -const memoryFixture = - configuration.library !== 'deep-agents' && configuration.topic === 'memory' ? createMemoryFixture() : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const clientToolsFixture = configuration.adapter !== 'ag-ui' && configuration.topic === 'client-tools' ? createClientToolsFixture() : null; @@ -131,28 +117,10 @@ const root = resolve( dirname(fileURLToPath(import.meta.url)), '../../dist/' + configuration.appPath ); -let requests = []; -let held; -let finishHeld; -let failCreation = false; -const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; const json = (response, value, status = 200) => { response.writeHead(status, { 'content-type': 'application/json' }); response.end(JSON.stringify(value)); }; -const release = () => { - if (!held) return; - held.write( - event('messages', [ - { type: 'AIMessageChunk', id: 'answer-1', content: ' complete' }, - { langgraph_node: 'generate' }, - ]) - ); - finishHeld?.(); - held.end(); - held = undefined; -}; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; if (aimockBackend && pathname !== '/') void aimockBackend.ensure().catch(() => undefined); @@ -292,131 +260,22 @@ const server = createServer(async (request, response) => { (await timeTravelFixture(request, response, pathname)) ) return; - if (subgraphsFixture && (await subgraphsFixture(request, response, pathname))) - return; if (durableFixture && (await durableFixture(request, response, pathname))) return; - if ( - persistenceFixture && - (await persistenceFixture(request, response, pathname)) - ) - return; if ( clientToolsFixture && (await clientToolsFixture(request, response, pathname)) ) return; - if (memoryFixture && (await memoryFixture(request, response, pathname))) - return; if ( - interruptsFixture && - (await interruptsFixture(request, response, pathname)) - ) - return; - if (pathname === '/__reset') { - held?.end(); - held = undefined; - requests = []; - failCreation = false; - return json(response, {}); - } - if (pathname === '/__release') { - release(); - return json(response, {}); - } - if (pathname === '/__requests') return json(response, requests); - if (pathname === '/__lifetime') - return json(response, { active: Boolean(held) }); - if (pathname === '/__fail-create') { - failCreation = true; - return json(response, {}); - } - if (pathname.startsWith('/api/') || pathname.startsWith('/developer-api/')) { - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - return json(response, { error: 'PRIVATE creation response' }, 500); - } - return json(response, { thread_id: body.thread_id }); - } - if (pathname.endsWith('/runs/stream')) { - const text = body.input.messages.at(-1).content; - if (text === 'Error') - return json(response, { error: 'PRIVATE response' }, 500); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${ - pathname.split('/')[3] - }/runs/00000000-0000-0000-0000-000000000001`, - }); - response.write( - event('metadata', { run_id: '00000000-0000-0000-0000-000000000001' }) - ); - response.write(event('values', { messages: body.input.messages })); - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: text === 'Second' ? 'answer-2' : 'answer-1', - content: - text === 'Literal' - ? '' - : text === 'First' - ? 'Live partial' - : 'Second answer', - }, - { langgraph_node: 'generate' }, - ]) - ); - const answer = { - type: 'ai', - id: text === 'Second' ? 'answer-2' : 'answer-1', - content: - text === 'Literal' - ? '' - : text === 'First' - ? 'Live partial complete' - : 'Second answer', - }; - const finish = () => - response.write( - event('values', { messages: [...body.input.messages, answer] }) - ); - if (text === 'First') { - held = response; - finishHeld = finish; - response.on('close', () => { - if (held === response) held = undefined; - }); - } else { - finish(); - response.end(); - } - return; - } - if (pathname.endsWith('/cancel')) { - held?.end(); - held = undefined; - return json(response, {}); - } - if (request.method === 'GET' && /\/runs\/[^/]+$/.test(pathname)) { - return json(response, { - run_id: '00000000-0000-0000-0000-000000000001', - thread_id: pathname.split('/')[3], - status: 'success', - }); - } - return json(response, {}); + pathname.startsWith('/api/') || + pathname.startsWith('/developer-api/') || + pathname.startsWith('/__') + ) { + // Every runtime-backed example owns these paths through its fixture or the + // aimock proxy above; anything left is not served. + response.writeHead(404); + return response.end(); } if (pathname === '/') { if (request.headers.host?.endsWith(':' + configuration.port)) { diff --git a/scripts/react-cockpit/subgraphs-fixture.mjs b/scripts/react-cockpit/subgraphs-fixture.mjs deleted file mode 100644 index ebd9b3bc2..000000000 --- a/scripts/react-cockpit/subgraphs-fixture.mjs +++ /dev/null @@ -1,257 +0,0 @@ -/** Local installed-SDK proof only; never assembled into public assets. */ -export function createSubgraphsFixture() { - let requests = [], - sequence = 0, - held; - let holdStart = false, - holdChild = false, - failCreation = false; - const threads = new Map(), - runs = new Map(); - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - function hold(response, finish) { - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - threads.clear(); - runs.clear(); - requests = []; - sequence = 0; - holdStart = holdChild = failCreation = false; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - const controls = { - '/__hold-start': () => { - holdStart = true; - }, - '/__hold-child': () => { - holdChild = true; - }, - '/__fail-create': () => { - failCreation = true; - }, - }; - if (Object.hasOwn(controls, pathname)) { - controls[pathname](); - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - threads.set(body.thread_id, { values: { messages: [] } }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - entry = threads.get(threadId); - if (pathname.endsWith('/runs/stream')) { - if ( - !entry || - body.assistant_id !== 'subgraphs' || - body.command || - !body.stream_subgraphs || - !Array.isArray(body.input?.messages) - ) { - json( - response, - { error: 'Expected a confirmed subgraphs message request' }, - 400 - ); - return true; - } - const human = body.input.messages.at(-1), - text = human.content; - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - // The real parent's add_messages reducer retains canonical prior turns. - const previous = entry.values.messages.filter( - (message) => message.id !== human.id - ); - entry.values = { ...entry.values, messages: [...previous, human] }; - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - response.write(event('values', entry.values)); - const nested = text !== 'Hello', - namespace = 'research:' + runId; - const topic = - text === 'Literal' - ? '' - : 'Fictional checkpoint topic'; - const brief = - text === 'Literal' - ? ' CHILD-ONLY brief' - : 'CHILD-ONLY brief'; - const finish = () => { - if (nested && text !== 'Missing child') { - response.write( - event('messages|' + namespace, [ - { - type: 'AIMessageChunk', - id: 'research-' + runId, - content: brief, - }, - { langgraph_node: 'research' }, - ]) - ); - response.write( - event('values|' + namespace, { - research_topic: topic, - research_brief: - text === 'Conflicting child' ? 'Conflicting brief' : brief, - }) - ); - if (text === 'Child pause') - response.write( - event('updates|' + namespace, { - __interrupt__: [{ value: 'PRIVATE pause' }], - }) - ); - if (text === 'Child error') - response.write( - event('error|' + namespace, { message: 'PRIVATE child error' }) - ); - if (text === 'Child tool') - response.write( - event('messages|' + namespace, [ - { - type: 'tool', - id: 'tool-' + runId, - content: 'PRIVATE tool result', - tool_call_id: 'unknown-tool', - }, - { langgraph_node: 'research' }, - ]) - ); - } - entry.values = { - ...entry.values, - research_topic: nested ? topic : '', - research_brief: nested ? brief : '', - }; - response.write(event('values', entry.values)); - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'answer-chunk-' + runId, - content: 'Parent answer', - }, - { langgraph_node: 'answer' }, - ]) - ); - const answer = { - type: 'ai', - id: 'answer-final-' + runId, - content: 'Parent answer', - }; - entry.values = { - ...entry.values, - messages: [...previous, human, answer], - completed_turn_id: human.id, - completed_answer_id: answer.id, - }; - if (text === 'Missing marker') delete entry.values.completed_turn_id; - if (text === 'Stale marker') - entry.values.completed_turn_id = previous[0]?.id ?? 'old-human'; - if (text === 'Missing answer marker') - delete entry.values.completed_answer_id; - if (text === 'Equal marker') - entry.values.completed_answer_id = human.id; - if (text === 'Root tool') - answer.tool_calls = [ - { id: 'unknown-tool', name: 'unknown', args: {} }, - ]; - response.write(event('values', entry.values)); - runs.get(runId).status = 'success'; - response.end(); - }; - const route = () => { - entry.values = { - ...entry.values, - research_topic: nested ? topic : '', - research_brief: '', - }; - response.write(event('values', entry.values)); - if (nested && text !== 'Missing child') - response.write( - event('values|' + namespace, { - research_topic: topic, - research_brief: '', - }) - ); - if (holdChild) { - holdChild = false; - hold(response, finish); - } else finish(); - }; - if (holdStart) { - holdStart = false; - hold(response, route); - } else route(); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, {}); - return true; - }; -} From 0ab9c33225289fc313e8bd0861bb4b980eb02868 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:24:10 -0800 Subject: [PATCH 07/41] test(website): drive the langgraph streaming/interrupts/memory/persistence/subgraphs previews on aimock The Website previews (and the learning workbench's React session) for the examples batch 4a moved onto aimock now start their backend through `serve.mjs --backend` and use the proxy controls: holds via /__hold/stream (afterText, immediate for the resume, afterChildMessage) and /__hold/history, lifetime via activeStream / activeHistory, and /__ready before driving a preview. Also: serve.mjs reads a static file before answering, so a missing build output is a 404 instead of a crash. Co-Authored-By: Claude Opus 5.5 --- apps/website/e2e/learning-workbench.spec.ts | 17 ++++++++++++++--- .../e2e/react-interrupts-preview.spec.ts | 10 ++++++++-- apps/website/e2e/react-memory-preview.spec.ts | 9 +++++++-- .../e2e/react-persistence-preview.spec.ts | 8 ++++++-- .../e2e/react-streaming-preview.spec.ts | 8 +++++++- .../e2e/react-subgraphs-preview.spec.ts | 8 ++++++-- apps/website/playwright.config.ts | 10 +++++----- scripts/react-cockpit/serve.mjs | 19 +++++++++++-------- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/apps/website/e2e/learning-workbench.spec.ts b/apps/website/e2e/learning-workbench.spec.ts index 7969c3fdb..ec897c30f 100644 --- a/apps/website/e2e/learning-workbench.spec.ts +++ b/apps/website/e2e/learning-workbench.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { test, expect, type Page, type FrameLocator } from '@playwright/test'; import { selectFramework } from './fixtures/framework-controls'; @@ -208,8 +211,12 @@ for (const framework of ['react', 'angular'] as const) { if (request.method() === 'POST' && path.endsWith('/runs/stream')) submittedRuns.push(path); }); - if (framework === 'react') + if (framework === 'react') { await request.post('http://127.0.0.1:4600/__reset'); + await request.post('http://127.0.0.1:4600/__ready', { timeout: 180000 }); + // "First" streams "Live partial" + " complete"; hold after the first chunk. + await request.post('http://127.0.0.1:4600/__hold/stream', { data: { afterText: 'Live partial' } }); + } const caseId = `learning-${framework}-${testInfo.retry}`; await request.delete(`http://127.0.0.1:4399/__requests/${caseId}`); const host = await openWorkbench(page, framework); @@ -417,6 +424,8 @@ test('React stop and failure remain distinct from application call settlement', request, }) => { await request.post('http://127.0.0.1:4600/__reset'); + await request.post('http://127.0.0.1:4600/__ready', { timeout: 180000 }); + await request.post('http://127.0.0.1:4600/__hold/stream', { data: { afterText: 'Live partial' } }); const host = await openWorkbench(page, 'react'); await host .getByRole('button', { name: 'Live conversation', exact: true }) @@ -458,6 +467,8 @@ test('framework changes dispose an active React stream and restore opt-in replay request, }) => { await request.post('http://127.0.0.1:4600/__reset'); + await request.post('http://127.0.0.1:4600/__ready', { timeout: 180000 }); + await request.post('http://127.0.0.1:4600/__hold/stream', { data: { afterText: 'Live partial' } }); const host = await openWorkbench(page, 'react'); await host .getByRole('button', { name: 'Live conversation', exact: true }) @@ -471,7 +482,7 @@ test('framework changes dispose an active React stream and restore opt-in replay async () => ( await (await request.get('http://127.0.0.1:4600/__lifetime')).json() - ).active + ).activeStream ) .toBe(true); await selectFramework(page, 'angular'); @@ -481,7 +492,7 @@ test('framework changes dispose an active React stream and restore opt-in replay async () => ( await (await request.get('http://127.0.0.1:4600/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); }); diff --git a/apps/website/e2e/react-interrupts-preview.spec.ts b/apps/website/e2e/react-interrupts-preview.spec.ts index f4349740d..94baf60b4 100644 --- a/apps/website/e2e/react-interrupts-preview.spec.ts +++ b/apps/website/e2e/react-interrupts-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -58,11 +61,14 @@ test('switching away releases a held React decision and returning starts fresh', 'Local held decision lifetime is verified before deployment' ); await request.post('http://127.0.0.1:4601/__reset'); - await request.post('http://127.0.0.1:4601/__hold-resume'); + await request.post('http://127.0.0.1:4601/__ready', { timeout: 180000 }); await page.goto('/docs/react/langgraph/guides/interrupts?mode=run'); const frame = page.frameLocator('iframe'); await frame.getByLabel('Message', { exact: true }).fill('Refund'); await frame.getByRole('button', { name: 'Send', exact: true }).click(); + await expect(frame.getByRole('button', { name: 'Issue refund' })).toBeVisible(); + // Hold the resume (the next stream) right after its first event. + await request.post('http://127.0.0.1:4601/__hold/stream', { data: { immediate: true } }); await frame.getByRole('button', { name: 'Issue refund' }).click(); await expect(frame.getByRole('status')).toHaveText('Sending decision…'); const oldFrame = await page.locator('iframe').elementHandle(); @@ -73,7 +79,7 @@ test('switching away releases a held React decision and returning starts fresh', async () => ( await (await request.get('http://127.0.0.1:4601/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await selectFramework(page, 'react'); diff --git a/apps/website/e2e/react-memory-preview.spec.ts b/apps/website/e2e/react-memory-preview.spec.ts index e2e23ec50..bfdfeec06 100644 --- a/apps/website/e2e/react-memory-preview.spec.ts +++ b/apps/website/e2e/react-memory-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -62,7 +65,9 @@ test('switching away releases held React extraction and returning starts empty', 'Local extraction lifetime is verified before deployment' ); await request.post('http://127.0.0.1:4602/__reset'); - await request.post('http://127.0.0.1:4602/__hold-extraction'); + await request.post('http://127.0.0.1:4602/__ready', { timeout: 180000 }); + // Hold once the reply has streamed, before extraction saves facts. + await request.post('http://127.0.0.1:4602/__hold/stream', { data: { afterText: 'Memory reply complete.' } }); await page.goto('/docs/react/langgraph/guides/memory?mode=run'); const frame = page.frameLocator('iframe'); await frame.getByLabel('Message', { exact: true }).fill('Remember Mira'); @@ -81,7 +86,7 @@ test('switching away releases held React extraction and returning starts empty', async () => ( await (await request.get('http://127.0.0.1:4602/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await selectFramework(page, 'react'); diff --git a/apps/website/e2e/react-persistence-preview.spec.ts b/apps/website/e2e/react-persistence-preview.spec.ts index 89950fea9..d5b40d2c1 100644 --- a/apps/website/e2e/react-persistence-preview.spec.ts +++ b/apps/website/e2e/react-persistence-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -65,6 +68,7 @@ test('switching away releases held React history and returning starts with an em 'Local history lifetime is verified before deployment' ); await request.post('http://127.0.0.1:4604/__reset'); + await request.post('http://127.0.0.1:4604/__ready', { timeout: 180000 }); await page.goto('/docs/react/langgraph/guides/persistence?mode=run'); const frame = page.frameLocator('iframe'); for (const text of [ @@ -79,7 +83,7 @@ test('switching away releases held React history and returning starts with an em await expect(frame.getByRole('status')).toHaveText('Ready.'); } } - await request.post('http://127.0.0.1:4604/__hold-history'); + await request.post('http://127.0.0.1:4604/__hold/history'); await frame .getByRole('button', { name: 'Conversation 1', exact: true }) .click(); @@ -94,7 +98,7 @@ test('switching away releases held React history and returning starts with an em async () => ( await (await request.get('http://127.0.0.1:4604/__lifetime')).json() - ).active + ).activeHistory ) .toBe(false); await request.post('http://127.0.0.1:4604/__release'); diff --git a/apps/website/e2e/react-streaming-preview.spec.ts b/apps/website/e2e/react-streaming-preview.spec.ts index dc90a8391..24358f050 100644 --- a/apps/website/e2e/react-streaming-preview.spec.ts +++ b/apps/website/e2e/react-streaming-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { frameworkControl, selectFramework } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -83,6 +86,9 @@ test('the installed React frame streams and releases work when the public select 'Local fixture stream lifetime is verified before deployment' ); await request.post('http://127.0.0.1:4600/__reset'); + await request.post('http://127.0.0.1:4600/__ready', { timeout: 180000 }); + // "First" streams "Live partial" + " complete"; hold after the first chunk. + await request.post('http://127.0.0.1:4600/__hold/stream', { data: { afterText: 'Live partial' } }); await page.goto('/docs/react/langgraph/guides/streaming?mode=run'); const frame = page.frameLocator('iframe'); await frame.getByLabel('Message', { exact: true }).fill('First'); @@ -99,7 +105,7 @@ test('the installed React frame streams and releases work when the public select async () => ( await (await request.get('http://127.0.0.1:4600/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await selectFramework(page, 'react'); diff --git a/apps/website/e2e/react-subgraphs-preview.spec.ts b/apps/website/e2e/react-subgraphs-preview.spec.ts index 4b728efec..393363b49 100644 --- a/apps/website/e2e/react-subgraphs-preview.spec.ts +++ b/apps/website/e2e/react-subgraphs-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -65,7 +68,8 @@ test('switching away releases a held child and returning starts empty', async ({ ); const origin = 'http://127.0.0.1:4606'; await request.post(origin + '/__reset'); - await request.post(origin + '/__hold-child'); + await request.post(origin + '/__ready', { timeout: 180000 }); + await request.post(origin + '/__hold/stream', { data: { afterChildMessage: true } }); await page.goto('/docs/react/langgraph/guides/subgraphs?mode=run'); const frame = page.frameLocator('iframe'); await frame.getByLabel('Message', { exact: true }).fill('Research first'); @@ -84,7 +88,7 @@ test('switching away releases a held child and returning starts empty', async ({ async () => ( await (await request.get(origin + '/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await request.post(origin + '/__release'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index f1d405f1e..9e05d9c11 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -250,7 +250,7 @@ export const createWebsitePlaywrightConfig = ( // website:e2e builds every installed React example first, // outside the server-readiness budget. Direct local // Playwright runs require those build outputs too. - 'node scripts/react-cockpit/serve.mjs', + 'node scripts/react-cockpit/serve.mjs streaming --backend', cwd: '../..', url: 'http://127.0.0.1:4600', reuseExistingServer, @@ -258,7 +258,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs interrupts --no-parent', + 'node scripts/react-cockpit/serve.mjs interrupts --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4601', reuseExistingServer, @@ -266,7 +266,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs memory --no-parent', + 'node scripts/react-cockpit/serve.mjs memory --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4602', reuseExistingServer, @@ -282,7 +282,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs persistence --no-parent', + 'node scripts/react-cockpit/serve.mjs persistence --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4604', reuseExistingServer, @@ -298,7 +298,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent', + 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4606', reuseExistingServer, diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index cbccb05a1..f3fdfb5e3 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -298,18 +298,21 @@ const server = createServer(async (request, response) => { response.writeHead(404); return response.end(); } + let content; try { - response.writeHead(200, { - 'content-type': - { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css' }[ - extname(file) - ] ?? 'application/octet-stream', - }); - response.end(readFileSync(file)); + // Read before answering: a missing build output is a 404, not a crash. + content = readFileSync(file); } catch { response.writeHead(404); - response.end(); + return response.end(); } + response.writeHead(200, { + 'content-type': + { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css' }[ + extname(file) + ] ?? 'application/octet-stream', + }); + response.end(content); }); const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; From 165b541350bd928804567c0f4cbb2052811452e3 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:51:23 -0800 Subject: [PATCH 08/41] test(website): expect the langgraph previews' aimock backend flags in the config spec Co-Authored-By: Claude Opus 5.5 --- apps/website/src/playwright-config.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 6e0ea6aa2..aa52b5ed0 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -147,7 +147,7 @@ describe('Website Playwright configuration', () => { const nativeServer = servers.find( server => server.url === 'http://127.0.0.1:4600' ); - expect(nativeServer?.command).toBe('node scripts/react-cockpit/serve.mjs'); + expect(nativeServer?.command).toBe('node scripts/react-cockpit/serve.mjs streaming --backend'); expect(nativeServer?.timeout).toBe(180_000); expect(nativeServer?.reuseExistingServer).toBe(false); }); @@ -454,15 +454,15 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4399/health', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs', + command: 'node scripts/react-cockpit/serve.mjs streaming --backend', url: 'http://127.0.0.1:4600', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs interrupts --no-parent', + command: 'node scripts/react-cockpit/serve.mjs interrupts --no-parent --backend', url: 'http://127.0.0.1:4601', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs memory --no-parent', + command: 'node scripts/react-cockpit/serve.mjs memory --no-parent --backend', url: 'http://127.0.0.1:4602', }), expect.objectContaining({ @@ -471,7 +471,7 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4603', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs persistence --no-parent', + command: 'node scripts/react-cockpit/serve.mjs persistence --no-parent --backend', url: 'http://127.0.0.1:4604', }), expect.objectContaining({ @@ -480,7 +480,7 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4605', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent', + command: 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent --backend', url: 'http://127.0.0.1:4606', }), expect.objectContaining({ From c73bb3636eb0627a613b72bd438ef482bffe7af1 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 16:29:23 -0800 Subject: [PATCH 09/41] test(cockpit): run React langgraph client-tools/deployment-runtime e2e on aimock Batch 4b of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md). - Both examples run the real graphs under `langgraph dev` on the shared Angular fixtures; client tool rounds key on prompt + hasToolResult. - Proxy: hold/fail the client's real state write (POST /threads//state), /__thread/ unrecorded state read, and /__stream/edit list ops (append, truncate, duplicateLast, copy) for deployment-runtime's nine finalization faults. "No final values" also strips the markers from the history read the session falls back to. - Delete both fixture servers. time-travel and durable-execution move to batch 4c (their specs encode the authored checkpoint layout). Co-Authored-By: Claude Opus 5.5 --- .../angular/e2e/fixtures/client-tools.json | 197 +++++++++++++++++ .../react/e2e/client-tools.spec.ts | 23 +- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 3 + .../langgraph/client-tools/react/project.json | 2 + .../e2e/fixtures/deployment-runtime.json | 54 +++++ .../react/e2e/deployment-runtime.spec.ts | 47 +++- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 3 + .../deployment-runtime/react/project.json | 2 + .../2026-11-18-react-cockpit-aimock-design.md | 5 +- scripts/cockpit-frontend-matrix.spec.mjs | 8 + .../ag-ui-client-tools-preparation.spec.mjs | 3 +- .../react-cockpit/client-tools-fixture.mjs | 127 ----------- scripts/react-cockpit/configuration.mjs | 2 +- .../deployment-runtime-fixture.mjs | 208 ------------------ scripts/react-cockpit/langgraph-proxy.mjs | 73 +++++- scripts/react-cockpit/serve.mjs | 18 -- 18 files changed, 416 insertions(+), 371 deletions(-) create mode 100644 cockpit/langgraph/client-tools/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/deployment-runtime/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/client-tools-fixture.mjs delete mode 100644 scripts/react-cockpit/deployment-runtime-fixture.mjs diff --git a/cockpit/langgraph/client-tools/angular/e2e/fixtures/client-tools.json b/cockpit/langgraph/client-tools/angular/e2e/fixtures/client-tools.json index ac7c85d7b..f9b593de0 100644 --- a/cockpit/langgraph/client-tools/angular/e2e/fixtures/client-tools.json +++ b/cockpit/langgraph/client-tools/angular/e2e/fixtures/client-tools.json @@ -35,6 +35,203 @@ { "match": { "userMessage": "Plan a trip to Portland" }, "response": { "toolCalls": [ { "name": "get_weather", "arguments": { "location": "Portland" } }, { "name": "confirm_booking", "arguments": { "summary": "Fictional trip to Portland" } } ] } + }, + { + "match": { + "userMessage": "Weather again", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_weather", + "arguments": { "location": "Portland" }, + "id": "weather-2" + } + ] + } + }, + { + "match": { + "userMessage": "Weather", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_weather", + "arguments": { "location": "Portland" }, + "id": "weather-1" + } + ] + } + }, + { + "match": { + "userMessage": "Card", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { + "location": "", + "temperatureF": 68, + "conditions": "Sunny", + "humidity": 55, + "windMph": 8 + }, + "id": "card-1" + } + ] + } + }, + { + "match": { + "userMessage": "Snapshot", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_snapshot", + "arguments": { + "location": "Portland", + "temperatureF": 68, + "conditions": "Sunny", + "humidity": 55, + "windMph": 8 + }, + "id": "snapshot-1" + } + ] + } + }, + { + "match": { + "userMessage": "Mixed", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_snapshot", + "arguments": { + "location": "Portland", + "temperatureF": 68, + "conditions": "Sunny", + "humidity": 55, + "windMph": 8 + }, + "id": "snapshot-1" + }, + { + "name": "get_weather", + "arguments": { "location": "Portland" }, + "id": "weather-1" + } + ] + } + }, + { + "match": { + "userMessage": "Bookings", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "confirm_booking", + "arguments": { "summary": "Fictional booking" }, + "id": "booking-a" + }, + { + "name": "confirm_booking", + "arguments": { "summary": "Fictional booking" }, + "id": "booking-b" + } + ] + } + }, + { + "match": { + "userMessage": "Slow", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "slow_status_check", + "arguments": {}, + "id": "slow-1" + } + ] + } + }, + { + "match": { + "userMessage": "Trip", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_weather", + "arguments": { "location": "Portland" }, + "id": "trip-weather" + }, + { + "name": "confirm_booking", + "arguments": { "summary": "Fictional trip to Portland" }, + "id": "trip-booking" + } + ] + } + }, + { + "match": { + "userMessage": "Malformed", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { + "location": "Portland", + "temperatureF": 68, + "conditions": "Sunny", + "humidity": 101, + "windMph": 8 + }, + "id": "bad-1" + } + ] + } + }, + { + "match": { + "userMessage": "Unknown", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "constructor", + "arguments": {}, + "id": "unknown-1" + } + ] + } + }, + { + "match": { "hasToolResult": true }, + "response": { "content": "Browser result received." } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Browser result received." } } ] } diff --git a/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts b/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts index 279ac36db..8b06e3c6c 100644 --- a/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts +++ b/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts @@ -1,4 +1,9 @@ import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/client-tools/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`, which binds the +// browser's client_tools and ends each turn on a browser tool call. The React +// static server proxies /api to it, recording requests and injecting holds and +// failures (scripts/react-cockpit/langgraph-proxy.mjs). test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); async function send(page: Page, text: string, navigate = true) { if (navigate) await page.goto('/langgraph/client-tools/react/'); @@ -39,7 +44,7 @@ test('publishes a finalized literal weather card and its normal shown acknowledg expect(runs[1].body.input!.messages[0]).toMatchObject({ tool_call_id: 'card-1', content: '{"shown":true}' }); }); test('persists a standalone snapshot result without another model run and waits for acknowledgement', async ({ page, request }) => { - await request.post('/__hold-state'); + await request.post('/__hold/state-write'); await send(page, 'Snapshot'); await expect(page.getByRole('region', { name: 'Weather panels' })).toContainText('Portland'); await expect.poll(async () => (await proof(request)).filter((entry) => entry.path.endsWith('/state')).length).toBe(1); @@ -52,7 +57,8 @@ test('persists a standalone snapshot result without another model run and waits const write = entries.find((entry) => entry.path.endsWith('/state'))!; expect(write.path.replace(/\/state$/, '/runs/stream')).toBe(streams(entries)[0].path); expect(write.body.values!.messages[0]).toMatchObject({ tool_call_id: 'snapshot-1', content: '{"shown":true}' }); - expect((await (await request.get('/__threads')).json())[0].messages.at(-1)).toMatchObject({ tool_call_id: 'snapshot-1', content: '{"shown":true}' }); + const threadId = write.path.split('/')[3]; + expect((await (await request.get('/__thread/' + threadId)).json()).values.messages.at(-1)).toMatchObject({ tool_call_id: 'snapshot-1', content: '{"shown":true}' }); }); test('mixed snapshot and normal tools follow up as one batch with exact result identities', async ({ page, request }) => { await send(page, 'Mixed'); @@ -109,13 +115,16 @@ test('invalid authored arguments produce a terminal error result explained by mo await expect(page.getByRole('region', { name: 'Weather panels' })).toContainText('No weather panels yet.'); }); for (const text of ['Unknown', 'Pause']) test(`${text} requires reset without effects or follow-up`, async ({ page, request }) => { + // A real graph never pauses here: the unexpected pause is injected state. + if (text === 'Pause') + await request.post('/__values', { data: { set: { __interrupt__: [{ id: 'unexpected', value: null }] } } }); await send(page, text); await expect(status(page)).toContainText('new conversation'); await expect(page.getByLabel('Message', { exact: true })).toBeDisabled(); expect(streams(await proof(request))).toHaveLength(1); }); test('an uncertain snapshot write is not replayed and requires reset', async ({ page, request }) => { - await request.post('/__fail-state'); + await request.post('/__fail/state-write'); await send(page, 'Snapshot'); await expect(status(page)).toContainText('new conversation'); expect((await proof(request)).filter((entry) => entry.path.endsWith('/state'))).toHaveLength(1); @@ -123,9 +132,9 @@ test('an uncertain snapshot write is not replayed and requires reset', async ({ }); test('partial streamed arguments cannot publish a panel or acknowledge a tool before finalization', async ({ page, request }) => { - await request.post('/__hold-calls'); + await request.post('/__hold/stream'); await send(page, 'Card'); - await expect.poll(async () => (await (await request.get('/__lifetime')).json()).active).toBe(true); + await expect.poll(async () => (await (await request.get('/__lifetime')).json()).activeStream).toBe(true); await expect(page.getByRole('region', { name: 'Weather panels' })).toContainText('No weather panels yet.'); expect(streams(await proof(request))).toHaveLength(1); await request.post('/__release'); @@ -173,14 +182,14 @@ test('ambiguous thread creation never dispatches or retries and protects diagnos test('successful weather turns reuse the thread with distinct server tool invocation identities', async ({ page, request }) => { await send(page, 'Weather'); await expect(status(page)).toHaveText('Response complete.'); - await send(page, 'Weather', false); + await send(page, 'Weather again', false); await expect(status(page)).toHaveText('Response complete.'); const entries = await proof(request), runs = streams(entries); expect(runs).toHaveLength(4); expect(new Set(runs.map((entry) => entry.path)).size).toBe(1); expect(entries.filter((entry) => entry.path === '/api/threads')).toHaveLength(1); expect(runs[1].body.input!.messages[0].tool_call_id).toBe('weather-1'); - expect(runs[3].body.input!.messages[0].tool_call_id).toBe('weather-1-3'); + expect(runs[3].body.input!.messages[0].tool_call_id).toBe('weather-2'); }); test('an embedded developer target sends credentials only in SDK headers', async ({ page, request }) => { diff --git a/cockpit/langgraph/client-tools/react/e2e/global-setup-impl.ts b/cockpit/langgraph/client-tools/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..6b37bafbc --- /dev/null +++ b/cockpit/langgraph/client-tools/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/client-tools', port: 4603 }); diff --git a/cockpit/langgraph/client-tools/react/e2e/playwright.config.ts b/cockpit/langgraph/client-tools/react/e2e/playwright.config.ts index 612f8a53a..f354b4c3f 100644 --- a/cockpit/langgraph/client-tools/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/client-tools/react/e2e/playwright.config.ts @@ -3,6 +3,9 @@ export default defineConfig({ testDir: '.', workers: 1, fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4603', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/client-tools/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs client-tools', cwd: '../../../../..', url: 'http://127.0.0.1:4603', reuseExistingServer: false, timeout: 10000, diff --git a/cockpit/langgraph/client-tools/react/project.json b/cockpit/langgraph/client-tools/react/project.json index cf3b831cd..fbd17b58a 100644 --- a/cockpit/langgraph/client-tools/react/project.json +++ b/cockpit/langgraph/client-tools/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/client-tools/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-client-tools-angular", "cockpit-langgraph-client-tools-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,6 +33,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/client-tools/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/deployment-runtime/angular/e2e/fixtures/deployment-runtime.json b/cockpit/langgraph/deployment-runtime/angular/e2e/fixtures/deployment-runtime.json index 8766ff233..7454ec3fe 100644 --- a/cockpit/langgraph/deployment-runtime/angular/e2e/fixtures/deployment-runtime.json +++ b/cockpit/langgraph/deployment-runtime/angular/e2e/fixtures/deployment-runtime.json @@ -7,6 +7,60 @@ "response": { "content": "Hello! How can I help you today?" } + }, + { + "match": { + "userMessage": "Alpha" + }, + "response": { + "content": "Reply to Alpha" + } + }, + { + "match": { + "userMessage": "Beta" + }, + "response": { + "content": "Reply to Beta" + } + }, + { + "match": { + "userMessage": "Gamma" + }, + "response": { + "content": "Reply to Gamma" + } + }, + { + "match": { + "userMessage": "Hold" + }, + "response": { + "content": "Reply to Hold" + }, + "chunkSize": 8, + "latency": 25 + }, + { + "match": { + "userMessage": "Error" + }, + "response": { + "error": { + "message": "PRIVATE run", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "userMessage": "" + }, + "response": { + "content": "Reply." + } } ] } \ No newline at end of file diff --git a/cockpit/langgraph/deployment-runtime/react/e2e/deployment-runtime.spec.ts b/cockpit/langgraph/deployment-runtime/react/e2e/deployment-runtime.spec.ts index 0c5d9f3b5..d1cdc2b2b 100644 --- a/cockpit/langgraph/deployment-runtime/react/e2e/deployment-runtime.spec.ts +++ b/cockpit/langgraph/deployment-runtime/react/e2e/deployment-runtime.spec.ts @@ -1,10 +1,46 @@ import { test, expect, type Page } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/deployment-runtime/angular/e2e/ +// fixtures (shared with the Angular example) into a real `langgraph dev`, whose +// generate node writes the completion markers. The React static server proxies +// /api to it, recording requests and injecting holds, failures and the +// finalization faults below (scripts/react-cockpit/langgraph-proxy.mjs). +// Root state arrives in both `values` and `checkpoints`; final state carries +// the completion markers. +const FINAL = { event: '^(values|checkpoints)$', ifKey: 'completed_turn_id' }; +const MARKERS = ['completed_turn_id', 'completed_answer_id', 'completed_message_ids']; +const finalizationFaults: Record = { + Interrupt: [{ ...FINAL, op: 'patch', set: { __interrupt__: [{ id: 'pause', value: { kind: 'unsupported' } }] } }], + Tool: [{ ...FINAL, op: 'append', path: 'messages', value: { type: 'tool', id: 'unsupported-tool', tool_call_id: '', content: 'Unsupported tool result' } }], + 'Missing markers': [{ ...FINAL, op: 'omit', keys: MARKERS }], + 'Wrong pair': [{ ...FINAL, op: 'patch', set: { completed_turn_id: 'wrong-human' } }], + 'Truncated prefix': [ + { ...FINAL, op: 'truncate', path: 'messages', keep: 2 }, + { ...FINAL, op: 'truncate', path: 'completed_message_ids', keep: 2 }, + ], + 'Duplicate identity': [{ ...FINAL, op: 'duplicateLast', path: 'completed_message_ids' }], + // The previous turn's pair: the markers never advance to this turn. + 'Old pair': [ + { ...FINAL, op: 'copy', from: 'messages.-4.id', to: 'completed_turn_id' }, + { ...FINAL, op: 'copy', from: 'messages.-3.id', to: 'completed_answer_id' }, + ], + 'No final values': [{ ...FINAL, op: 'drop' }, { event: '^updates$', op: 'drop' }], +}; test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); const conversation = (page: Page) => page.getByRole('region', { name: 'Conversation', exact: true }); async function send(page: Page, text: string) { + // Shapes a real graph never finalizes with: the proxy edits the stream the + // browser receives (injected data, like the authored fixture's variants). + if (text in finalizationFaults) + await page.request.post('/__stream/edit', { data: { ops: finalizationFaults[text] } }); + // Without final values the session reads history: the saved turn is + // unfinalized there too. + if (text === 'No final values') + await page.request.post('/__history-values', { data: { omit: MARKERS } }); + if (text === 'Hold') + await page.request.post('/__hold/stream', { data: { afterText: 'Reply to' } }); await page.getByLabel('Message', { exact: true }).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); } @@ -59,7 +95,8 @@ test('held incremental content settles to one canonical answer without duplicate }) => { await page.goto('/langgraph/deployment-runtime/react/'); await send(page, 'Hold'); - await expect(conversation(page)).toContainText('Live partial'); + await expect(conversation(page)).toContainText('Reply to'); + await expect(conversation(page)).not.toContainText('Reply to Hold'); await expect( page.getByRole('button', { name: 'Stop', exact: true }) ).toBeVisible(); @@ -69,8 +106,8 @@ test('held incremental content settles to one canonical answer without duplicate await request.post('/__release'); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); await expect(conversation(page).locator('article')).toHaveCount(2); + // The streamed chunks and the saved answer are one message, not two. await expect(conversation(page)).toContainText('Reply to Hold'); - await expect(conversation(page)).not.toContainText('Live partial'); }); for (const text of [ 'Error', @@ -175,8 +212,8 @@ for (const exit of ['Stop', 'pagehide']) { }) => { await page.goto('/langgraph/deployment-runtime/react/'); await send(page, 'Hold'); - await expect(conversation(page)).toContainText('Live partial'); - expect((await (await request.get('/__lifetime')).json()).active).toBe(true); + await expect(conversation(page)).toContainText('Reply to'); + expect((await (await request.get('/__lifetime')).json()).activeStream).toBe(true); if (exit === 'Stop') { await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.locator('footer').getByRole('status')).toHaveText( @@ -190,7 +227,7 @@ for (const exit of ['Stop', 'pagehide']) { } await expect .poll( - async () => (await (await request.get('/__lifetime')).json()).active + async () => (await (await request.get('/__lifetime')).json()).activeStream ) .toBe(false); await request.post('/__release'); diff --git a/cockpit/langgraph/deployment-runtime/react/e2e/global-setup-impl.ts b/cockpit/langgraph/deployment-runtime/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..e963b5e37 --- /dev/null +++ b/cockpit/langgraph/deployment-runtime/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/deployment-runtime', port: 4608 }); diff --git a/cockpit/langgraph/deployment-runtime/react/e2e/playwright.config.ts b/cockpit/langgraph/deployment-runtime/react/e2e/playwright.config.ts index e8defe093..d7222d223 100644 --- a/cockpit/langgraph/deployment-runtime/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/deployment-runtime/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4608', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/deployment-runtime/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deployment-runtime', cwd: '../../../../..', diff --git a/cockpit/langgraph/deployment-runtime/react/project.json b/cockpit/langgraph/deployment-runtime/react/project.json index 70cbd61b2..f980021f8 100644 --- a/cockpit/langgraph/deployment-runtime/react/project.json +++ b/cockpit/langgraph/deployment-runtime/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/deployment-runtime/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-deployment-runtime-angular", "cockpit-langgraph-deployment-runtime-python"], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -42,6 +43,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": [ "build" ], diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index af3a39a51..b1afaa8e2 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -79,6 +79,8 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | memory `Replace` / `Malformed` / `Absent` / `Pause` (4a) | authored memory state | `/__values` sets or omits keys in the next run's `values`, `updates` and `checkpoints` state. `persist` writes the cleared memory to the real thread, so the next turn agrees (the real graph only merges facts) | | persistence `/__replace-first`, `/__empty-first`, `/__pause-history` (4a) | authored server history | `/__history-values` (one-shot latest-values rewrite) and a `paused` history mode (pending + unknown interrupt task) | | subgraphs route faults (4a) | ten authored stream variants | `/__stream/edit`: drop, patch, omit or insert-after on matching events. Ops on `checkpoints` events address their `values`, because the session reads root state from both | + | client-tools `/__hold-state`, `/__fail-state`, `/__threads` (4b) | authored client result writes | `/__hold/state-write` and `/__fail/state-write` act on the client's real `POST /threads//state`; `/__thread/` reads the real state without recording it | + | deployment-runtime's nine finalization faults (4b) | authored final values | `/__stream/edit` on the final state (`values` and `checkpoints` carrying `completed_turn_id`), plus `/__history-values` for "No final values", because the session then reads history | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -169,7 +171,8 @@ example needs that the pilot proxy doesn't yet provide. | 3 (done) | chat/threads, chat/timeline | `/__title/`, `/__hold/title`, `/__state/`, `/__hold/state`, `/__hold/history`, `/__mapped-checkpoints`, `foreign`/`failure` history modes | a mid-stream model failure is aimock `truncateAfterChunks`; stream bodies come from `/__captures[].sse`, because DevTools can't return a streamed body the client closes | | 3b | chat/debug | `/__hold/current`, `/__hold/checkpoint`, selected-read variants, `captures[].proof.providerVariant` | its fixture also backs the packed-consumer native-session proof (`chat-debug-native-session-probe.mjs`, its own CI step), so it migrates together with that proof | | 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | -| 4b | langgraph/{client-tools, deployment-runtime, time-travel, durable-execution} | `/__hold-calls`, `/__fail-state`, `/__threads`, `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__hold-plan`, `/__publish-*` | `durable-execution` `/__publish-*` injects authored events: candidates for `/__stream/edit` | +| 4b (done) | langgraph/{client-tools, deployment-runtime} | `/__hold/state-write`, `/__fail/state-write`, `/__thread/`, `/__stream/edit` list ops (`append`, `truncate`, `duplicateLast`, `copy`) | client tool rounds key on prompt + `hasToolResult`; a second same-thread turn uses its own prompt (`Weather again`), because aimock fixtures are stateless. "No final values" also strips the markers from the history read the session falls back to | +| 4c | langgraph/{time-travel, durable-execution} | authored checkpoint ids and layouts (`checkpoint-1`, "Select checkpoint 3"), `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__hold-plan`, `/__publish-*` | the specs encode the authored checkpoint layout, so they need rewriting against real checkpoints. A competing tip can become a real state write through the proxy | | 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | | 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 2ffa72505..0c658d9e9 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -411,6 +411,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/deployment-runtime/react/', port: 4608, project: 'cockpit-langgraph-deployment-runtime-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/deployment-runtime/python', + fixtures: 'cockpit/langgraph/deployment-runtime/angular/e2e/fixtures', langgraphPort: 5608, + }, }); assert.deepEqual(reactCockpitConfiguration('time-travel'), { topic: 'time-travel', @@ -476,6 +480,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/client-tools/react/', port: 4603, project: 'cockpit-langgraph-client-tools-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/client-tools/python', + fixtures: 'cockpit/langgraph/client-tools/angular/e2e/fixtures', langgraphPort: 5603, + }, }); for (const topic of [ 'unknown', diff --git a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs b/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs index 0cc32197c..f3fcba36c 100644 --- a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs +++ b/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs @@ -35,7 +35,8 @@ test('website browser job prepares its own canonical worker after existing pinne test('proof routing scopes native and retained LangGraph fixtures and shutdown to owned handles', () => { const serve=read('scripts/react-cockpit/serve.mjs'); assert.match(serve,/configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' \? createAgUiClientToolsFixture/); - assert.match(serve,/configuration.adapter !== 'ag-ui' && configuration.topic === 'client-tools' \? createClientToolsFixture/); + // LangGraph client-tools runs on aimock + `langgraph dev` through the proxy. + assert.match(serve,/configuration.backend\?\.kind === 'aimock-langgraph'\s*\? createLanggraphProxy/); assert.match(serve,/await \(chatDebugFixture \?\? nativeStrandsFixture \?\? nativeMafFixture \?\? nativeMastraFixture \?\? agUiClientToolsFixture \?\? [^\n]+\)\.close\(\);/); assert.match(read('cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts'),/prepare-ag-ui-client-tools-fixture.mjs --serve/); assert.match(read('scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs'),/'ag-ui-client-tools', '--no-parent'/); diff --git a/scripts/react-cockpit/client-tools-fixture.mjs b/scripts/react-cockpit/client-tools-fixture.mjs deleted file mode 100644 index 99c5e2910..000000000 --- a/scripts/react-cockpit/client-tools-fixture.mjs +++ /dev/null @@ -1,127 +0,0 @@ -/** Local real-SDK tool exchange proof; never included in public deployment. */ -export function createClientToolsFixture() { - let requests = []; - const threads = new Map(), runs = new Map(); - let sequence = 0, failCreation = false, holdState = false, failState = false, holdCalls = false, held; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - const weather = { location: 'Portland', temperatureF: 68, conditions: 'Sunny', humidity: 55, windMph: 8 }; - // Mirrors the deployed graph's plan (cockpit/langgraph/client-tools/python/src/graph.py). - const stepLabel = ({ name, args }) => { - const where = typeof args?.location === 'string' && args.location.trim() ? ` for ${args.location}` : ''; - return ({ get_weather: `Check the weather${where}`, weather_card: `Show the weather card${where}`, weather_snapshot: `Show a weather snapshot${where}`, confirm_booking: 'Confirm the booking with you', slow_status_check: 'Run the status check' })[name] ?? name; - }; - const names = ['get_weather', 'slow_status_check', 'weather_card', 'weather_snapshot', 'confirm_booking']; - function calls(text) { - const call = (id, name, args) => ({ id: sequence === 1 ? id : `${id}-${sequence}`, name, args, type: 'tool_call' }); - if (text === 'Weather') return [call('weather-1', 'get_weather', { location: 'Portland' })]; - if (text === 'Card') return [call('card-1', 'weather_card', { ...weather, location: '' })]; - if (text === 'Snapshot') return [call('snapshot-1', 'weather_snapshot', weather)]; - if (text === 'Mixed') return [call('snapshot-1', 'weather_snapshot', weather), call('weather-1', 'get_weather', { location: 'Portland' })]; - if (text === 'Bookings') return ['booking-a', 'booking-b'].map((id) => call(id, 'confirm_booking', { summary: 'Fictional booking' })); - if (text === 'Slow') return [call('slow-1', 'slow_status_check', {})]; - if (text === 'Trip') return [call('trip-weather', 'get_weather', { location: 'Portland' }), call('trip-booking', 'confirm_booking', { summary: 'Fictional trip to Portland' })]; - if (text === 'Malformed') return [call('bad-1', 'weather_card', { ...weather, humidity: 101 })]; - if (text === 'Unknown') return [call('unknown-1', 'constructor', {})]; - return []; - } - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); held = undefined; - requests = []; threads.clear(); runs.clear(); sequence = 0; - failCreation = holdState = failState = holdCalls = false; - json(response, {}); return true; - } - if (pathname === '/__requests') { json(response, requests); return true; } - if (pathname === '/__threads') { json(response, [...threads.values()]); return true; } - if (pathname === '/__lifetime') { json(response, { active: !!held }); return true; } - if (pathname === '/__fail-create') { failCreation = true; json(response, {}); return true; } - if (pathname === '/__hold-state') { holdState = true; json(response, {}); return true; } - if (pathname === '/__hold-calls') { holdCalls = true; json(response, {}); return true; } - if (pathname === '/__fail-state') { failState = true; json(response, {}); return true; } - if (pathname === '/__release') { - const current = held; held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); return true; - } - if (!pathname.startsWith('/api/') && !pathname.startsWith('/developer-api/')) return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ path: pathname, method: request.method, body, key: request.headers['x-api-key'] ?? null }); - if (pathname.endsWith('/threads')) { - if (failCreation) { failCreation = false; json(response, { error: 'PRIVATE creation response' }, 500); return true; } - threads.set(body.thread_id, { messages: [] }); - json(response, { thread_id: body.thread_id }); return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1]; - const state = threads.get(threadId); - if (pathname.endsWith('/state') && request.method === 'POST') { - if (!state || !Array.isArray(body.values?.messages)) { json(response, { error: 'Expected confirmed result write' }, 400); return true; } - // Model an ambiguous write: durable effect succeeded, acknowledgement failed. - state.messages.push(...body.values.messages); - const finish = () => { - if (failState) { failState = false; json(response, { error: 'PRIVATE uncertain acknowledgement' }, 500); } - else json(response, {}); - }; - if (holdState) { - holdState = false; const current = { response, finish }; held = current; - response.on('close', () => { if (held === current) held = undefined; }); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream')) { - const catalog = body.input?.client_tools; - if (!state || body.assistant_id !== 'client-tools' || body.command || - !Array.isArray(body.input?.messages) || !Array.isArray(catalog) || - JSON.stringify(catalog.map((tool) => tool.name)) !== JSON.stringify(names)) { - json(response, { error: 'Expected confirmed tool input and authored catalog' }, 400); return true; - } - const message = body.input.messages.at(-1); - if (message.content === 'Error') { json(response, { error: 'PRIVATE run response' }, 500); return true; } - const runId = '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - const run = { thread_id: threadId, status: 'running' }; runs.set(runId, run); - response.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache', 'content-location': `/threads/${threadId}/runs/${runId}` }); - response.write(event('metadata', { run_id: runId })); - state.messages.push(...body.input.messages); - response.write(event('values', state)); - const toolCalls = message.type === 'tool' || message.role === 'tool' ? [] : calls(message.content); - // A plan in the deep-agents `todos` shape, as the deployed graph writes it: several calls in one response - // declare it, the answer after their results completes it, and a new user turn clears it. - const isTool = message.type === 'tool' || message.role === 'tool'; - if (toolCalls.length >= 2) state.todos = [...toolCalls.map((call) => ({ content: stepLabel(call), status: 'in_progress' })), { content: 'Summarize the results', status: 'pending' }]; - else if (state.todos?.length && isTool && toolCalls.length === 0) state.todos = state.todos.map((todo) => ({ ...todo, status: 'completed' })); - else if (state.todos?.length && !isTool) state.todos = []; - const answer = { type: 'ai', id: 'answer-' + runId, content: toolCalls.length ? '' : 'Browser result received.', ...(toolCalls.length ? { tool_calls: toolCalls } : {}) }; - if (toolCalls.length) { - // This partial fragment is deliberately not actionable until final values. - const first = toolCalls[0]; - response.write(event('messages', [{ type: 'AIMessageChunk', id: answer.id, content: '', tool_call_chunks: [{ index: 0, id: first.id, name: first.name, args: '{', type: 'tool_call_chunk' }] }, { langgraph_node: 'generate' }])); - } else { - response.write(event('messages', [{ type: 'AIMessageChunk', id: answer.id, content: 'Browser result' }, { langgraph_node: 'generate' }])); - response.write(event('messages', [{ type: 'AIMessageChunk', id: answer.id, content: ' received.' }, { langgraph_node: 'generate' }])); - } - const finish = () => { - state.messages.push(answer); - const paused = message.content === 'Pause'; - response.write(event('values', { ...state, ...(paused ? { __interrupt__: [{ id: 'unexpected', value: null }] } : {}) })); - run.status = paused ? 'interrupted' : 'success'; response.end(); - }; - if (holdCalls) { - holdCalls = false; const current = { response, finish }; held = current; - response.on('close', () => { if (held === current) held = undefined; }); - } else finish(); - return true; - } - if (pathname.endsWith('/cancel')) { held?.response.end(); held = undefined; json(response, {}); return true; } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json(response, run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, run ? 200 : 404); return true; - } - json(response, {}); return true; - }; -} diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 7fedd5e8b..28e390db0 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -12,7 +12,7 @@ const aimockLanggraph = (example, port) => Object.freeze({ langgraphPort: port + 1000, }); /** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ -const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs']); +const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime']); export function reactCockpitConfiguration(topic = 'streaming') { if (topic === 'ag-ui-a2ui') return Object.freeze({ diff --git a/scripts/react-cockpit/deployment-runtime-fixture.mjs b/scripts/react-cockpit/deployment-runtime-fixture.mjs deleted file mode 100644 index a1ed6993e..000000000 --- a/scripts/react-cockpit/deployment-runtime-fixture.mjs +++ /dev/null @@ -1,208 +0,0 @@ -/** Local installed-SDK HTTP/SSE proof; never assembled into public assets. */ -export function createDeploymentRuntimeFixture() { - let requests = [], - sequence = 0, - held, - failCreation = false; - const threads = new Map(), - runs = new Map(); - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - sequence = 0; - failCreation = false; - threads.clear(); - runs.clear(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__fail-create') { - failCreation = true; - json(response, {}); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - if ( - request.method !== 'POST' || - typeof body.thread_id !== 'string' || - threads.has(body.thread_id) - ) { - json(response, { error: 'Invalid confirmed UUID' }, 400); - return true; - } - threads.set(body.thread_id, { messages: [] }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - state = threads.get(threadId); - if (pathname.endsWith('/runs/stream')) { - if ( - !state || - request.method !== 'POST' || - body.assistant_id !== 'deployment-runtime' || - body.command || - !Array.isArray(body.input?.messages) || - body.input.messages.length !== 1 || - body.input.messages[0].type !== 'human' - ) { - json( - response, - { - error: - 'Expected ordinary confirmed deployment-runtime message input', - }, - 400 - ); - return true; - } - const human = body.input.messages[0], - text = human.content; - if (text === 'Error') { - json(response, { error: 'PRIVATE run' }, 500); - return true; - } - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - const run = { thread_id: threadId, status: 'running' }; - runs.set(runId, run); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId })); - state.messages = [...state.messages, human]; - response.write(event('values', state)); - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'transient-' + runId, - content: text === 'Hold' ? 'Live partial' : 'Transient draft', - }, - { langgraph_node: 'generate' }, - ]) - ); - const finish = () => { - if (text === 'No final values') { - run.status = 'success'; - response.end(); - return; - } - const answer = { - type: 'ai', - id: 'answer-' + runId, - content: 'Reply to ' + text, - }; - state.messages = [...state.messages, answer]; - const values = { ...state }; - if (text === 'Truncated prefix') values.messages = [human, answer]; - if (text !== 'Missing markers' && text !== 'Old pair') - Object.assign(values, { - completed_turn_id: text === 'Wrong pair' ? 'wrong-human' : human.id, - completed_answer_id: answer.id, - completed_message_ids: values.messages.map((message) => message.id), - }); - if (text === 'Duplicate identity') - values.completed_message_ids = [ - ...values.completed_message_ids, - answer.id, - ]; - if (text === 'Tool') - values.messages = [ - ...state.messages, - { - type: 'tool', - id: 'unsupported-tool-' + runId, - tool_call_id: '', - content: 'Unsupported tool result', - }, - ]; - if (text === 'Interrupt') - values.__interrupt__ = [ - { id: 'pause', value: { kind: 'unsupported' } }, - ]; - response.write(event('values', values)); - Object.assign(state, values); - run.status = text === 'Interrupt' ? 'interrupted' : 'success'; - response.end(); - }; - if (text === 'Hold') { - const current = { response, finish, run }; - held = current; - response.on('close', () => { - if (held === current) { - held = undefined; - run.status = 'interrupted'; - } - }); - } else finish(); - return true; - } - const cancelId = /\/runs\/([^/]+)\/cancel$/.exec(pathname)?.[1]; - if (cancelId) { - if (held?.run === runs.get(cancelId)) { - held.response.end(); - held.run.status = 'interrupted'; - held = undefined; - } - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, { error: 'Unsupported runtime request' }, 404); - return true; - }; -} diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index fa554b93b..4141d637e 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -71,7 +71,9 @@ * POST /__stream/edit body { ops }: edits to the next run stream, each * { event: regex, op, ... } applied to matching SSE * events: drop | patch { path?, set } | omit - * { path?, keys } | insertAfter { insert: { event, + * { path?, keys } | append { path, value } | + * truncate { path, keep } | duplicateLast { path } | + * copy { from, to } | insertAfter { insert: { event, * data } } (`$ns` in the inserted name is the * matched event's `|namespace`; the string * "$humanId" in data is the submitted message id). @@ -81,9 +83,14 @@ * Injected data for stream shapes a real graph * won't produce (missing/stale markers, child * pauses, errors and tool calls). + * POST /__hold/state-write hold the next client state write (POST + * /threads//state, e.g. a client tool result) + * POST /__fail/state-write next client state write answers 500 unforwarded + * GET /__thread/ the real thread state (not recorded in /__requests) * POST /__fail-stream next run stream answers 500 without forwarding * (the graph worker is unavailable) - * POST /__release flush and resume a held run stream + * POST /__release flush and resume a held run stream (and finish a + * held client state write) * POST /__inject/reasoning body { text }: until reset, every AI message the * browser receives (stream, history, state) carries * additional_kwargs.reasoning_content = text. @@ -169,6 +176,9 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let nextValues = null; let nextHistoryValues = null; let nextStreamEdits = null; + let holdNextStateWrite = false; + let failNextStateWrite = false; + let heldStateWrite; let historyMode = 'safe'; let holdNextStream = null; let heldStream; @@ -271,6 +281,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { nextValues = null; nextHistoryValues = null; nextStreamEdits = null; + holdNextStateWrite = false; + failNextStateWrite = false; + heldStateWrite?.response.destroy(); + heldStateWrite = undefined; historyMode = 'safe'; holdNextStream = null; } @@ -486,6 +500,21 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { else for (const key of op.keys ?? []) delete target[key]; changed = true; } + if (['append', 'truncate', 'duplicateLast'].includes(op.op)) { + const list = at(state, op.path); + if (!Array.isArray(list)) continue; + if (op.op === 'append') list.push(withHuman(op.value)); + if (op.op === 'truncate') list.splice(0, Math.max(0, list.length - op.keep)); + if (op.op === 'duplicateLast' && list.length) list.push(structuredClone(list.at(-1))); + changed = true; + } + if (op.op === 'copy') { + const value = at(state, op.from); + const [parent, key] = [at(state, op.to.split('.').slice(0, -1).join('.')), op.to.split('.').at(-1)]; + if (value === undefined || !parent || typeof parent !== 'object') continue; + parent[key] = structuredClone(value); + changed = true; + } if (op.op === 'insertAfter') { const namespace = name.includes('|') ? name.slice(name.indexOf('|')) : ''; after.push(`event: ${op.insert.event.replace('$ns', namespace)}\ndata: ${JSON.stringify(withHuman(op.insert.data))}\n\n`); @@ -668,11 +697,18 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { activeHistory: Boolean(heldHistory), activeTitle: Boolean(heldTitle), activeState: Boolean(heldState), + activeStateWrite: Boolean(heldStateWrite), openResponses, })], '/__reset': ['POST', () => { reset(); return {}; }], '/__fail-create': ['POST', () => { failCreate = true; return {}; }], - '/__release': ['POST', () => { heldStream?.release(); return {}; }], + '/__release': ['POST', () => { + heldStream?.release(); + const write = heldStateWrite; + heldStateWrite = undefined; + write?.finish(); + return {}; + }], '/__release-history': ['POST', () => { const held = heldHistory; heldHistory = undefined; @@ -697,6 +733,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { '/__hold/history': ['POST', () => { holdNextHistory = true; return {}; }], '/__hold/state': ['POST', () => { holdNextState = true; return {}; }], '/__mapped-checkpoints': ['POST', () => { mapped = true; return {}; }], + '/__hold/state-write': ['POST', () => { holdNextStateWrite = true; return {}; }], + '/__fail/state-write': ['POST', () => { failNextStateWrite = true; return {}; }], '/__release-state': ['POST', () => { const held = heldState; heldState = undefined; @@ -728,6 +766,11 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } return true; } + if (pathname.startsWith('/__thread/') && request.method === 'GET') { + const threadId = pathname.slice('/__thread/'.length); + json(response, await upstreamJson(`/threads/${encodeURIComponent(threadId)}/state`)); + return true; + } if (pathname.startsWith('/__interrupt/')) { const mode = pathname.slice('/__interrupt/'.length); if (!['unknown', 'multiple'].includes(mode)) json(response, {}, 400); @@ -813,6 +856,30 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { json(response, { error: 'PRIVATE graph worker failure' }, 500); } else await proxyStream(request, response, path, body); } + else if (request.method === 'POST' && /\/threads\/[^/]+\/state$/.test(pathname)) { + if (failNextStateWrite) { + failNextStateWrite = false; + json(response, { error: 'PRIVATE state write' }, 500); + } else { + const hold = holdNextStateWrite; + holdNextStateWrite = false; + const upstreamResponse = await send(request.method, path, request.headers, body); + const raw = await readAll(upstreamResponse); + const finish = () => { + if (response.destroyed) return; + response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + response.end(raw); + }; + if (!hold) finish(); + else { + const held = { response, finish }; + heldStateWrite = held; + response.on('close', () => { + if (heldStateWrite === held) heldStateWrite = undefined; + }); + } + } + } else if (request.method === 'POST' && /\/threads\/[^/]+\/state\/checkpoint$/.test(pathname)) await proxyState(request, response, path, body); else if (request.method === 'GET' && /^\/threads\/[^/]+$/.test(pathname.slice(prefix.length))) diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index f3fdfb5e3..ed5a084a3 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -10,10 +10,8 @@ import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; -import { createClientToolsFixture } from './client-tools-fixture.mjs'; import { createDurableFixture } from './durable-fixture.mjs'; import { createTimeTravelFixture } from './time-travel-fixture.mjs'; -import { createDeploymentRuntimeFixture } from './deployment-runtime-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; @@ -101,17 +99,11 @@ const agUiInterruptsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'interrupts' ? createAgUiInterruptsFixture() : null; -const deploymentRuntimeFixture = - configuration.topic === 'deployment-runtime' - ? createDeploymentRuntimeFixture() - : null; const timeTravelFixture = configuration.topic === 'time-travel' ? createTimeTravelFixture() : null; const durableFixture = configuration.topic === 'durable-execution' ? createDurableFixture() : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; -const clientToolsFixture = - configuration.adapter !== 'ag-ui' && configuration.topic === 'client-tools' ? createClientToolsFixture() : null; const root = resolve( dirname(fileURLToPath(import.meta.url)), @@ -250,11 +242,6 @@ const server = createServer(async (request, response) => { (await agUiStreamingFixture(request, response, pathname)) ) return; - if ( - deploymentRuntimeFixture && - (await deploymentRuntimeFixture(request, response, pathname)) - ) - return; if ( timeTravelFixture && (await timeTravelFixture(request, response, pathname)) @@ -262,11 +249,6 @@ const server = createServer(async (request, response) => { return; if (durableFixture && (await durableFixture(request, response, pathname))) return; - if ( - clientToolsFixture && - (await clientToolsFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From cb36ed483206808813e2b6dc64a10be8a1ebd1a6 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:24:56 -0800 Subject: [PATCH 10/41] test(website): drive the langgraph client-tools/deployment-runtime previews on aimock Both previews start their backend through `serve.mjs --backend`, wait on /__ready, and use /__hold/stream and activeStream from the proxy. Co-Authored-By: Claude Opus 5.5 --- apps/website/e2e/react-client-tools-preview.spec.ts | 4 ++++ .../e2e/react-deployment-runtime-preview.spec.ts | 10 ++++++++-- apps/website/playwright.config.ts | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apps/website/e2e/react-client-tools-preview.spec.ts b/apps/website/e2e/react-client-tools-preview.spec.ts index 7f3999289..2987452e1 100644 --- a/apps/website/e2e/react-client-tools-preview.spec.ts +++ b/apps/website/e2e/react-client-tools-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -27,6 +30,7 @@ test('public React client tools aligns the canonical Chat guide, source, runtime test('switching away disposes pending browser decisions and returning starts empty', async ({ page, request }) => { test.skip(Boolean(process.env['BASE_URL']), 'Local browser decision ownership is verified before deployment'); await request.post('http://127.0.0.1:4603/__reset'); + await request.post('http://127.0.0.1:4603/__ready', { timeout: 180000 }); await page.goto('/docs/react/chat/guides/client-tools?mode=run'); const frame = page.frameLocator('iframe'); await frame.getByLabel('Message', { exact: true }).fill('Bookings'); diff --git a/apps/website/e2e/react-deployment-runtime-preview.spec.ts b/apps/website/e2e/react-deployment-runtime-preview.spec.ts index 111edaee4..de4ebff14 100644 --- a/apps/website/e2e/react-deployment-runtime-preview.spec.ts +++ b/apps/website/e2e/react-deployment-runtime-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -78,6 +81,9 @@ test('switching away releases the held Deployment Runtime stream and returning s ); const origin = 'http://127.0.0.1:4608'; await request.post(origin + '/__reset'); + await request.post(origin + '/__ready', { timeout: 180000 }); + // "Hold" streams "Reply to Hold" in chunks; hold after "Reply to". + await request.post(origin + '/__hold/stream', { data: { afterText: 'Reply to' } }); await page.goto('/docs/react/langgraph/guides/deployment?mode=run'); const frame = page.frameLocator('iframe'); await expect( @@ -87,7 +93,7 @@ test('switching away releases the held Deployment Runtime stream and returning s await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect( frame.getByRole('region', { name: 'Conversation', exact: true }) - ).toContainText('Live partial'); + ).toContainText('Reply to'); const oldFrame = await page.locator('iframe').elementHandle(); await selectFramework(page, 'angular'); await expect.poll(() => oldFrame?.evaluate((element) => element.isConnected)).toBe(false); @@ -96,7 +102,7 @@ test('switching away releases the held Deployment Runtime stream and returning s async () => ( await (await request.get(origin + '/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await request.post(origin + '/__release'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 9e05d9c11..0e3094dc2 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -274,7 +274,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs client-tools --no-parent', + 'node scripts/react-cockpit/serve.mjs client-tools --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4603', reuseExistingServer, @@ -314,7 +314,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent', + 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4608', reuseExistingServer, From ce74c143863a7b010a6f79985152ad21df46c5a5 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:51:25 -0800 Subject: [PATCH 11/41] test(website): expect the client-tools and deployment-runtime previews' aimock backend flags in the config spec Co-Authored-By: Claude Opus 5.5 --- apps/website/src/playwright-config.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index aa52b5ed0..62f755bd7 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -467,7 +467,7 @@ describe('Website Playwright configuration', () => { }), expect.objectContaining({ command: - 'node scripts/react-cockpit/serve.mjs client-tools --no-parent', + 'node scripts/react-cockpit/serve.mjs client-tools --no-parent --backend', url: 'http://127.0.0.1:4603', }), expect.objectContaining({ @@ -489,7 +489,7 @@ describe('Website Playwright configuration', () => { }), expect.objectContaining({ command: - 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent', + 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent --backend', url: 'http://127.0.0.1:4608', }), expect.objectContaining({ From a97cce3bc7401e2591a85a3c24daea59b5406307 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:07:30 -0800 Subject: [PATCH 12/41] test(cockpit): run React langgraph time-travel/durable-execution e2e on aimock Batch 4c of the React cockpit aimock migration (docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md); all nine langgraph React examples now run real graphs under `langgraph dev`. - time-travel reads checkpoint ids from real history and thread reads instead of authored ids (each turn saves input, start and done checkpoints); a competing tip is a real state write; fork result faults edit the stream and the fork's confirmation state read. - durable-execution: uncertain outcomes end the browser's stream after the first draft (optionally withholding the run identity or reporting a known run as running) and keep history inconclusive until a one-shot rewrite publishes the saved outcome. - Proxy: /__history-copies, /__state/unsafe, unrecorded thread state writes, /__stream/edit { state, anonymous, unlessKey }, /__hold/stream { afterPattern, end }, /__run-status/, /__history-values { entry }, mapped checkpoint extras (maps sent back are stripped before forwarding). The edit engine is shared between stream events and state reads. - client-tools: the slow-handler test waits 15s for the deliberate ~3s handler plus a real follow-up run. - Delete both fixture servers. Co-Authored-By: Claude Opus 5.5 --- .../react/e2e/client-tools.spec.ts | 3 +- .../e2e/fixtures/durable-execution.json | 24 ++ .../react/e2e/durable.spec.ts | 114 ++++- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 3 + .../durable-execution/react/project.json | 2 + .../angular/e2e/fixtures/time-travel.json | 26 ++ .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 3 + .../time-travel/react/e2e/time-travel.spec.ts | 164 +++++-- .../langgraph/time-travel/react/project.json | 2 + .../2026-11-18-react-cockpit-aimock-design.md | 4 +- scripts/cockpit-frontend-matrix.spec.mjs | 8 + scripts/react-cockpit/configuration.mjs | 2 +- scripts/react-cockpit/durable-fixture.mjs | 297 ------------- scripts/react-cockpit/langgraph-proxy.mjs | 211 ++++++--- scripts/react-cockpit/serve.mjs | 13 - scripts/react-cockpit/time-travel-fixture.mjs | 403 ------------------ 18 files changed, 461 insertions(+), 830 deletions(-) create mode 100644 cockpit/langgraph/durable-execution/react/e2e/global-setup-impl.ts create mode 100644 cockpit/langgraph/time-travel/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/durable-fixture.mjs delete mode 100644 scripts/react-cockpit/time-travel-fixture.mjs diff --git a/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts b/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts index 8b06e3c6c..9f3acd925 100644 --- a/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts +++ b/cockpit/langgraph/client-tools/react/e2e/client-tools.spec.ts @@ -154,7 +154,8 @@ test('page exit aborts pending browser decisions without a model continuation', test('a slow status handler completes normally and follows up once', async ({ page, request }) => { await send(page, 'Slow'); - await expect(status(page)).toHaveText('Response complete.'); + // The browser handler takes ~3s on purpose, then a real follow-up run. + await expect(status(page)).toHaveText('Response complete.', { timeout: 15000 }); const runs = streams(await proof(request)); expect(runs).toHaveLength(2); expect(runs[1].body.input!.messages[0]).toMatchObject({ tool_call_id: 'slow-1', content: '{"label":"status check","status":"complete"}' }); diff --git a/cockpit/langgraph/durable-execution/angular/e2e/fixtures/durable-execution.json b/cockpit/langgraph/durable-execution/angular/e2e/fixtures/durable-execution.json index 8c8b3198a..79e5279aa 100644 --- a/cockpit/langgraph/durable-execution/angular/e2e/fixtures/durable-execution.json +++ b/cockpit/langgraph/durable-execution/angular/e2e/fixtures/durable-execution.json @@ -7,6 +7,30 @@ "response": { "content": "The user sent a simple greeting (\"Hello\"), so the immediate goal is to acknowledge the greeting and invite them to continue (e.g., ask how I can help or what they'd like to discuss)." } + }, + { + "match": { + "systemMessage": "ANALYZE step" + }, + "response": { + "content": "Analysis draft" + } + }, + { + "match": { + "systemMessage": "PLAN step" + }, + "response": { + "content": "Plan draft" + } + }, + { + "match": { + "systemMessage": "GENERATE step" + }, + "response": { + "content": "Final answer" + } } ] } \ No newline at end of file diff --git a/cockpit/langgraph/durable-execution/react/e2e/durable.spec.ts b/cockpit/langgraph/durable-execution/react/e2e/durable.spec.ts index 4587cb7ce..2c83d4210 100644 --- a/cockpit/langgraph/durable-execution/react/e2e/durable.spec.ts +++ b/cockpit/langgraph/durable-execution/react/e2e/durable.spec.ts @@ -4,6 +4,60 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/durable-execution/angular/e2e/ +// fixtures (shared with the Angular example) into a real `langgraph dev` +// running analyze → plan → generate, each step checkpointed. The React static +// server proxies /api to it, recording requests and injecting holds, failures, +// uncertain outcomes and the finalization faults below +// (scripts/react-cockpit/langgraph-proxy.mjs). +const FINAL = { event: '^(values|checkpoints)$', ifKey: 'completed_turn_id' }; +const finalFaults: Record = { + 'Missing marker': [{ ...FINAL, op: 'omit', keys: ['completed_turn_id'] }], + 'Stale marker': [{ ...FINAL, op: 'patch', set: { completed_turn_id: 'previous-human' } }], + 'Wrong marker': [{ ...FINAL, op: 'patch', set: { completed_turn_id: 'wrong-human' } }], + 'Missing answer marker': [{ ...FINAL, op: 'omit', keys: ['completed_answer_id'] }], + 'Stale answer marker': [{ ...FINAL, op: 'patch', set: { completed_answer_id: 'previous-answer' } }], + 'Human answer marker': [{ ...FINAL, op: 'patch', set: { completed_answer_id: '$humanId' } }], + 'Unknown tool': [{ ...FINAL, op: 'patch', path: 'messages.1', set: { tool_calls: [{ id: 'unknown-call', name: 'unknown', args: {} }] } }], + // Only the marked terminal root arrives: no tokens, node updates or drafts. + 'Final only': [ + { event: '^messages', op: 'drop' }, + { event: '^updates$', op: 'drop' }, + { event: '^(values|checkpoints)$', op: 'drop', unlessKey: 'completed_turn_id' }, + ], +}; +/** The connection ends after the first draft chunk and saved history stays + * inconclusive. An unknown run also withholds its identity; a known run keeps + * reporting itself running. */ +async function prepareUncertain(request: APIRequestContext, known: boolean) { + if (known) await request.post('/__run-status/running'); + else await request.post('/__stream/edit', { data: { ops: [], anonymous: true } }); + await request.post('/__hold/stream', { data: { end: true } }); + await request.post('/__history/pending'); +} +const humanOf = async (request: APIRequestContext) => + ((await (await request.get('/__requests')).json()) as Wire[]) + .filter((entry) => entry.path.endsWith('/runs/stream')) + .at(-1)!.body.input!.messages[0].id; +/** The saved outcome of the retained turn becomes conclusive (one read). */ +async function publish(request: APIRequestContext, humanId: string, marker = humanId) { + const answer = 'final-answer'; + await request.post('/__history/safe'); + await request.post('/__history-values', { + data: { + set: { + messages: [ + { type: 'human', id: humanId, content: 'Uncertain' }, + { type: 'ai', id: answer, content: 'Final answer' }, + ], + step: 'generate', + completed_turn_id: marker, + completed_answer_id: answer, + }, + entry: { next: [], tasks: [] }, + }, + }); +} test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); @@ -12,6 +66,12 @@ const transcript = (page: Page) => const checkpoints = (page: Page) => page.getByRole('region', { name: 'Saved pipeline checkpoints', exact: true }); async function send(page: Page, text: string, navigate = true) { + // Shapes a real pipeline never finalizes with: the proxy edits the stream + // the browser receives (injected data, like the authored fixture's variants). + if (text in finalFaults) + await page.request.post('/__stream/edit', { data: { ops: finalFaults[text] } }); + if (text === 'Uncertain' || text === 'Known uncertain') + await prepareUncertain(page.request, text === 'Known uncertain'); if (navigate) await page.goto('/langgraph/durable-execution/react/'); await page.getByLabel('Message', { exact: true }).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -42,7 +102,10 @@ test('saves completed nodes while held, then replaces intermediate drafts with t page, request, }, testInfo) => { - await request.post('/__hold-plan'); + // Hold once the analyze step's state is saved. + await request.post('/__hold/stream', { + data: { afterPattern: 'event: values\\s+data: .*"step": ?"analyze"' }, + }); await send(page, 'A fictional project'); await expect(checkpoints(page)).toContainText('Analyze — complete'); await expect(checkpoints(page)).toContainText('Plan — pending'); @@ -75,10 +138,10 @@ test('second requests reuse one UUID and ignore the previous generate checkpoint }) => { await send(page, 'First'); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); - await request.post('/__hold-start'); + await request.post('/__hold/stream', { data: { immediate: true } }); await send(page, 'Second', false); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(true); await expect(checkpoints(page)).toContainText('Analyze — pending'); await expect(checkpoints(page)).toContainText('Generate — pending'); @@ -137,12 +200,15 @@ test('Stop releases a held pipeline run and fences its later final result', asyn page, request, }) => { - await request.post('/__hold-plan'); + // Hold once the analyze step's state is saved. + await request.post('/__hold/stream', { + data: { afterPattern: 'event: values\\s+data: .*"step": ?"analyze"' }, + }); await send(page, 'A fictional project'); await expect(checkpoints(page)).toContainText('Analyze — complete'); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); await expect(page.locator('footer').getByRole('status')).toContainText('Stopped'); @@ -178,12 +244,13 @@ test('developer credentials stay in SDK headers on creation, streaming and recon });`); await expect(page.locator('#state')).toHaveText('Ready'); const frame = page.frameLocator('#pilot'); + await prepareUncertain(request, false); await frame.getByLabel('Message', { exact: true }).fill('Uncertain'); await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect( frame.getByRole('button', { name: 'Check status', exact: true }) ).toBeVisible(); - await request.post('/__publish-final'); + await publish(request, await humanOf(request)); await frame .getByRole('button', { name: 'Check status', exact: true }) .click(); @@ -212,7 +279,7 @@ test('explicit reconciliation reads the exact retained turn without creating or expect( (await wire(request)).filter((entry) => entry.path.endsWith('/history')) ).toHaveLength(1); - await request.post('/__publish-final'); + await publish(request, await humanOf(request)); await page.getByRole('button', { name: 'Check status' }).click(); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); await expect(transcript(page).getByRole('article')).toHaveCount(2); @@ -248,7 +315,7 @@ test('a conclusive history with a wrong marker cannot enable text', async ({ request, }) => { await uncertain(page); - await request.post('/__publish-wrong-marker'); + await publish(request, await humanOf(request), 'wrong-human'); await page.getByRole('button', { name: 'Check status' }).click(); await expect(page.locator('footer').getByRole('status')).toHaveText( 'Start a new conversation to continue.' @@ -261,7 +328,22 @@ test('unrelated saved history cannot reconcile the retained request', async ({ request, }) => { await uncertain(page); - await request.post('/__publish-unrelated'); + // Saved history about some other turn: the retained request is absent. + await request.post('/__history/safe'); + await request.post('/__history-values', { + data: { + set: { + messages: [ + { type: 'human', id: 'unrelated-human', content: 'Unrelated' }, + { type: 'ai', id: 'unrelated-answer', content: 'Unrelated answer' }, + ], + step: 'generate', + completed_turn_id: 'unrelated-human', + completed_answer_id: 'unrelated-answer', + }, + entry: { next: [], tasks: [] }, + }, + }); await page.getByRole('button', { name: 'Check status' }).click(); await expect( page.getByRole('button', { name: 'Check status' }) @@ -274,7 +356,7 @@ test('a failed history read protects diagnostics without replay', async ({ request, }) => { await uncertain(page); - await request.post('/__fail-history'); + await request.post('/__history/failed'); await page.getByRole('button', { name: 'Check status' }).click(); await expect(page.getByRole('alert')).toHaveText( 'The saved outcome could not be confirmed.' @@ -288,8 +370,8 @@ for (const action of ['Stop', 'pagehide']) request, }) => { await uncertain(page); - await request.post('/__publish-final'); - await request.post('/__hold-history'); + await publish(request, await humanOf(request)); + await request.post('/__hold/history'); await page.getByRole('button', { name: 'Check status' }).click(); await expect(page.locator('footer').getByRole('status')).toHaveText( 'Checking saved outcome…' @@ -299,7 +381,7 @@ for (const action of ['Stop', 'pagehide']) ).toBeDisabled(); await expect .poll( - async () => (await (await request.get('/__lifetime')).json()).active + async () => (await (await request.get('/__lifetime')).json()).activeHistory ) .toBe(true); if (action === 'Stop') @@ -307,7 +389,7 @@ for (const action of ['Stop', 'pagehide']) else await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); await expect .poll( - async () => (await (await request.get('/__lifetime')).json()).active + async () => (await (await request.get('/__lifetime')).json()).activeHistory ) .toBe(false); await request.post('/__release'); @@ -346,8 +428,8 @@ test('a known physical run with an unknown outcome offers only a read-only statu ).toHaveLength(0); // Once the exact run has ended, the same check reads the saved outcome and // concludes the turn without creating or replaying anything. - await request.post('/__finish-run'); - await request.post('/__publish-final'); + await request.post('/__run-status/success'); + await publish(request, await humanOf(request)); await page.getByRole('button', { name: 'Check status' }).click(); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); await expect(transcript(page).getByRole('article')).toHaveCount(2); diff --git a/cockpit/langgraph/durable-execution/react/e2e/global-setup-impl.ts b/cockpit/langgraph/durable-execution/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..e82f9482d --- /dev/null +++ b/cockpit/langgraph/durable-execution/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/durable-execution', port: 4605 }); diff --git a/cockpit/langgraph/durable-execution/react/e2e/playwright.config.ts b/cockpit/langgraph/durable-execution/react/e2e/playwright.config.ts index fb54a1ed7..ac9dd9469 100644 --- a/cockpit/langgraph/durable-execution/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/durable-execution/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4605', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/durable-execution/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs durable-execution', cwd: '../../../../..', diff --git a/cockpit/langgraph/durable-execution/react/project.json b/cockpit/langgraph/durable-execution/react/project.json index ab60740ea..053441ccb 100644 --- a/cockpit/langgraph/durable-execution/react/project.json +++ b/cockpit/langgraph/durable-execution/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/durable-execution/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-durable-execution-angular", "cockpit-langgraph-durable-execution-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -34,6 +35,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build"], "options": { "config": "cockpit/langgraph/durable-execution/react/e2e/playwright.config.ts", diff --git a/cockpit/langgraph/time-travel/angular/e2e/fixtures/time-travel.json b/cockpit/langgraph/time-travel/angular/e2e/fixtures/time-travel.json index dcc32ca70..d13fa4390 100644 --- a/cockpit/langgraph/time-travel/angular/e2e/fixtures/time-travel.json +++ b/cockpit/langgraph/time-travel/angular/e2e/fixtures/time-travel.json @@ -15,6 +15,32 @@ { "match": { "userMessage": "Try a different approach from here." }, "response": { "content": "This reply belongs to the checkpoint fork." } + }, + { + "match": { "userMessage": "A" }, + "response": { "content": "Answer to A" } + }, + { + "match": { "userMessage": "B" }, + "response": { "content": "Answer to B" } + }, + { + "match": { "userMessage": "C" }, + "response": { "content": "Answer to C" }, + "chunkSize": 4, + "latency": 25 + }, + { + "match": { "userMessage": "D" }, + "response": { "content": "Answer to D" } + }, + { + "match": { "userMessage": "Literal" }, + "response": { "content": "" } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Answer." } } ] } diff --git a/cockpit/langgraph/time-travel/react/e2e/global-setup-impl.ts b/cockpit/langgraph/time-travel/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..1b01f77e2 --- /dev/null +++ b/cockpit/langgraph/time-travel/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'langgraph/time-travel', port: 4607 }); diff --git a/cockpit/langgraph/time-travel/react/e2e/playwright.config.ts b/cockpit/langgraph/time-travel/react/e2e/playwright.config.ts index dc977de8a..ea4bab1b9 100644 --- a/cockpit/langgraph/time-travel/react/e2e/playwright.config.ts +++ b/cockpit/langgraph/time-travel/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4607', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/langgraph/time-travel/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs time-travel', cwd: '../../../../..', diff --git a/cockpit/langgraph/time-travel/react/e2e/time-travel.spec.ts b/cockpit/langgraph/time-travel/react/e2e/time-travel.spec.ts index f40093733..13e51182c 100644 --- a/cockpit/langgraph/time-travel/react/e2e/time-travel.spec.ts +++ b/cockpit/langgraph/time-travel/react/e2e/time-travel.spec.ts @@ -1,4 +1,59 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/langgraph/time-travel/angular/e2e/fixtures +// (shared with the Angular example) into a real `langgraph dev`, so history, +// checkpoints and forks are the real server's. Each turn saves three root +// checkpoints (input, start, done); only completed ones are fork sources. The +// React static server proxies /api to it, recording requests and injecting +// holds, failures and the history/result faults below +// (scripts/react-cockpit/langgraph-proxy.mjs). +type HistoryEntry = { + checkpoint: { checkpoint_id: string }; + next: string[]; + values: { messages: { content: string }[] }; +}; +// Final state of a fork carries the completion markers; the session confirms +// the fork with a state read, so the same edits apply there (`state: true`). +const FINAL = { event: '^(values|checkpoints)$', ifKey: 'completed_turn_id' }; +const resultFaults: Record = { + 'Truncated history': [{ ...FINAL, op: 'truncate', path: 'completed_message_ids', keep: 2 }], + 'Missing marker': [{ ...FINAL, op: 'omit', keys: ['completed_turn_id'] }], + 'Stale marker': [{ ...FINAL, op: 'copy', from: 'messages.0.id', to: 'completed_turn_id' }], + 'Equal marker': [{ ...FINAL, op: 'patch', set: { completed_answer_id: '$humanId' } }], + 'Unknown tool': [{ ...FINAL, op: 'patch', path: 'messages.-1', set: { tool_calls: [{ id: 'unknown-tool', name: 'unknown', args: {} }] } }], +}; +const threadOf = async (request: APIRequestContext) => + ((await (await request.get('/__requests')).json()) as Wire[]) + .find((row) => row.path.endsWith('/runs/stream'))! + .path.split('/')[3]; +const tipOf = async (request: APIRequestContext, threadId: string) => + (await (await request.get('/__thread/' + threadId)).json()).checkpoint + .checkpoint_id as string; +/** Someone else commits a newer root tip; it must not redirect this fork. */ +const advanceTip = async (request: APIRequestContext, threadId: string) => + expect( + ( + await request.post('/__thread/' + threadId + '/state', { + data: { + values: { + messages: [ + { type: 'human', id: 'competing-human', content: 'Competing tip' }, + { type: 'ai', id: 'competing-answer', content: 'Competing tip answer' }, + ], + }, + }, + }) + ).ok() + ).toBe(true); +/** Refresh, returning the history page and the Select button for turn A. */ +async function refresh(page: Page) { + const response = page.waitForResponse((r) => r.url().endsWith('/history')); + await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); + const history: HistoryEntry[] = await (await response).json(); + const a = history.findIndex( + (entry) => entry.next.length === 0 && entry.values.messages.length === 2 + ); + return { history, a, selectA: `Select checkpoint ${a + 1}` }; +} test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); @@ -13,7 +68,7 @@ type Wire = { path: string; method: string; body: { - checkpoint?: { checkpoint_id: string }; + checkpoint?: { checkpoint_id: string; checkpoint_ns?: string; checkpoint_map?: Record }; stream_mode?: string[]; input?: { messages: { id: string; content: string }[] }; }; @@ -27,12 +82,11 @@ test('fork adopts A rather than B and retained continuation ignores a competing await page.goto('/langgraph/time-travel/react/'); await send(page, 'A'); await send(page, 'B'); - await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); - await expect( - page.getByRole('button', { name: 'Select checkpoint 3' }) - ).toBeVisible(); + const { history, a, selectA } = await refresh(page); + await expect(page.getByRole('button', { name: selectA })).toBeVisible(); + const source = history[a].checkpoint.checkpoint_id; const before: Wire[] = await (await request.get('/__requests')).json(); - await page.getByRole('button', { name: 'Select checkpoint 3' }).click(); + await page.getByRole('button', { name: selectA }).click(); expect(await (await request.get('/__requests')).json()).toEqual(before); await expect(transcript(page)).toContainText('Answer to B'); await page.getByLabel('Message', { exact: true }).fill('C'); @@ -42,8 +96,9 @@ test('fork adopts A rather than B and retained continuation ignores a competing await expect(transcript(page)).toContainText('Answer to A'); await expect(transcript(page)).toContainText('Answer to C'); await expect(transcript(page)).not.toContainText('Answer to B'); - await expect(transcript(page)).not.toContainText('Transient chunk'); - await request.post('/__advance-tip'); + const threadId = await threadOf(request); + const forkTip = await tipOf(request, threadId); + await advanceTip(request, threadId); await send(page, 'D'); await expect(transcript(page).getByRole('article')).toHaveCount(6); await expect(transcript(page)).toContainText('Answer to C'); @@ -52,37 +107,42 @@ test('fork adopts A rather than B and retained continuation ignores a competing const wire: Wire[] = await (await request.get('/__requests')).json(); const runs = wire.filter((row) => row.path.endsWith('/runs/stream')); expect(runs).toHaveLength(4); - expect(runs[2].body.checkpoint?.checkpoint_id).toBe('checkpoint-1'); - expect(runs[3].body.checkpoint?.checkpoint_id).toBe('checkpoint-3'); + expect(runs[2].body.checkpoint?.checkpoint_id).toBe(source); + expect(runs[3].body.checkpoint?.checkpoint_id).toBe(forkTip); expect(runs[2].body.stream_mode).toContain('checkpoints'); expect(new Set(runs.map((row) => row.path)).size).toBe(1); expect(wire.filter((row) => row.path.endsWith('/threads'))).toHaveLength(1); expect(wire.filter((row) => row.path.endsWith('/history'))).toHaveLength(1); const reads = wire.filter((row) => row.path.endsWith('/state/checkpoint')); expect(reads.map((row) => row.body.checkpoint?.checkpoint_id)).toEqual([ - 'checkpoint-1', - 'checkpoint-3', - 'checkpoint-4', + source, + forkTip, + await tipOf(request, threadId), + ]); + // The saved B turn is untouched by the fork. + const b = history.find( + (entry) => entry.next.length === 0 && entry.values.messages.length === 4 + )!; + expect(b.values.messages.map((message) => message.content)).toEqual([ + 'A', + 'Answer to A', + 'B', + 'Answer to B', ]); - const states = await (await request.get('/__states')).json(); - expect( - states - .find( - (state: { checkpoint: { checkpoint_id: string } }) => - state.checkpoint.checkpoint_id === 'checkpoint-2' - ) - .values.messages.map((message: { content: string }) => message.content) - ).toEqual(['A', 'Answer to A', 'B', 'Answer to B']); }); async function prepareFork(page: Page) { await page.goto('/langgraph/time-travel/react/'); await send(page, 'A'); await send(page, 'B'); - await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); - await page.getByRole('button', { name: 'Select checkpoint 3' }).click(); + const { selectA } = await refresh(page); + await page.getByRole('button', { name: selectA }).click(); } async function fork(page: Page, text = 'C') { + // A real fork never finalizes like this: the proxy edits its stream and the + // confirmation state read (injected data, like the authored variants). + if (text in resultFaults) + await page.request.post('/__stream/edit', { data: { ops: resultFaults[text], state: true } }); await page.getByLabel('Message', { exact: true }).fill(text); await page.getByRole('button', { name: 'Fork selected checkpoint' }).click(); } @@ -94,7 +154,9 @@ test('explicit secondary history refresh after a fork cannot redirect primary ex await prepareFork(page); await fork(page); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); - await request.post('/__advance-tip'); + const threadId = await threadOf(request); + const forkTip = await tipOf(request, threadId); + await advanceTip(request, threadId); await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); await expect( page.getByRole('button', { name: 'Refresh saved checkpoints' }) @@ -103,7 +165,7 @@ test('explicit secondary history refresh after a fork cannot redirect primary ex await send(page, 'D'); const wire: Wire[] = await (await request.get('/__requests')).json(); const runs = wire.filter((row) => row.path.endsWith('/runs/stream')); - expect(runs.at(-1)?.body.checkpoint?.checkpoint_id).toBe('checkpoint-3'); + expect(runs.at(-1)?.body.checkpoint?.checkpoint_id).toBe(forkTip); expect(wire.filter((row) => row.path.endsWith('/history'))).toHaveLength(2); await expect(transcript(page)).not.toContainText('Answer to B'); await expect(transcript(page).getByRole('article')).toHaveCount(6); @@ -115,7 +177,16 @@ test('duplicate, child, pending and malformed history references never receive S }) => { await page.goto('/langgraph/time-travel/react/'); await send(page, 'A'); - await request.post('/__bad-history'); + // Duplicate, child-namespace and malformed-map copies of the newest entry. + await request.post('/__history-copies', { + data: { + copies: [ + { index: 0 }, + { index: 0, set: { 'checkpoint.checkpoint_id': 'child-root', 'checkpoint.checkpoint_ns': 'child:literal' } }, + { index: 0, set: { 'checkpoint.checkpoint_id': 'invalid-map', 'checkpoint.checkpoint_map': null } }, + ], + }, + }); await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); await expect( page.getByRole('region', { name: 'Last loaded checkpoint page' }) @@ -143,7 +214,7 @@ test('metadata eligibility does not replace mandatory completed-source preflight request, }) => { await prepareFork(page); - await request.post('/__unsafe-source'); + await request.post('/__state/unsafe'); await fork(page); await expect(page.locator('footer').getByRole('status')).toHaveText( 'Start a new conversation to continue.' @@ -163,14 +234,14 @@ test('Stop during exact source preparation issues no fork POST and New creates o request, }) => { await prepareFork(page); - await request.post('/__hold-state'); + await request.post('/__hold/state'); await fork(page); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeState) .toBe(true); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeState) .toBe(false); await request.post('/__release'); let wire: Wire[] = await (await request.get('/__requests')).json(); @@ -195,12 +266,14 @@ test('page exit revokes a held fork stream without replaying it', async ({ request, }) => { await prepareFork(page); - await request.post('/__hold-stream'); + await request.post('/__hold/stream'); await fork(page); - await expect(transcript(page)).toContainText('Transient chunk'); + await expect + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) + .toBe(true); await page.goto('about:blank'); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) .toBe(false); await request.post('/__release'); const wire: Wire[] = await (await request.get('/__requests')).json(); @@ -215,7 +288,7 @@ test('failed readonly history is protected and safe primary can continue', async }) => { await page.goto('/langgraph/time-travel/react/'); await send(page, 'A'); - await request.post('/__fail-history'); + await request.post('/__history/failed'); await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); await expect(page.getByRole('alert')).toHaveText( 'The checkpoint page was not refreshed.' @@ -235,14 +308,14 @@ test('Stop of a held history read releases the secondary session and blocks late }) => { await page.goto('/langgraph/time-travel/react/'); await send(page, 'A'); - await request.post('/__hold-history'); + await request.post('/__hold/history'); await page.getByRole('button', { name: 'Refresh saved checkpoints' }).click(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeHistory) .toBe(true); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .poll(async () => (await (await request.get('/__lifetime')).json()).activeHistory) .toBe(false); await request.post('/__release'); await expect( @@ -380,22 +453,25 @@ test('full checkpoint maps survive exact source reads, fork POST and retained co page, request, }) => { - await request.post('/__mapped-checkpoints'); + await request.post('/__mapped-checkpoints', { + data: { extra: { child: 'literal-map-value' } }, + }); await prepareFork(page); await fork(page); await expect(page.locator('footer').getByRole('status')).toHaveText('Response complete.'); + const forkTip = await tipOf(request, await threadOf(request)); await send(page, 'D'); const entries: Wire[] = await (await request.get('/__requests')).json(); const runs = entries.filter((row) => row.path.endsWith('/runs/stream')); + const source = runs[2].body.checkpoint!.checkpoint_id; expect(runs[2].body.checkpoint).toMatchObject({ checkpoint_ns: '', - checkpoint_id: 'checkpoint-1', - checkpoint_map: { '': 'checkpoint-1', child: 'literal-map-value' }, + checkpoint_map: { '': source, child: 'literal-map-value' }, }); expect(runs[3].body.checkpoint).toMatchObject({ checkpoint_ns: '', - checkpoint_id: 'checkpoint-3', - checkpoint_map: { '': 'checkpoint-3', child: 'literal-map-value' }, + checkpoint_id: forkTip, + checkpoint_map: { '': forkTip, child: 'literal-map-value' }, }); const reads = entries.filter((row) => row.path.endsWith('/state/checkpoint')); expect(reads[0].body.checkpoint).toEqual(runs[2].body.checkpoint); diff --git a/cockpit/langgraph/time-travel/react/project.json b/cockpit/langgraph/time-travel/react/project.json index 977670b23..387dab18c 100644 --- a/cockpit/langgraph/time-travel/react/project.json +++ b/cockpit/langgraph/time-travel/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/langgraph/time-travel/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-langgraph-time-travel-angular", "cockpit-langgraph-time-travel-python"], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -42,6 +43,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": [ "build" ], diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index b1afaa8e2..6a370575f 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -81,6 +81,8 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | subgraphs route faults (4a) | ten authored stream variants | `/__stream/edit`: drop, patch, omit or insert-after on matching events. Ops on `checkpoints` events address their `values`, because the session reads root state from both | | client-tools `/__hold-state`, `/__fail-state`, `/__threads` (4b) | authored client result writes | `/__hold/state-write` and `/__fail/state-write` act on the client's real `POST /threads//state`; `/__thread/` reads the real state without recording it | | deployment-runtime's nine finalization faults (4b) | authored final values | `/__stream/edit` on the final state (`values` and `checkpoints` carrying `completed_turn_id`), plus `/__history-values` for "No final values", because the session then reads history | + | time-travel `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__states`, result faults (4c) | authored checkpoint store | a real competing write (`/__thread//state`), `/__history-copies`, `/__state/unsafe`, history reads, and `/__stream/edit { state: true }` (the fork's confirmation read gets the same edits) | + | durable `Uncertain` / `Known uncertain`, `/__publish-*`, `/__finish-run` (4c) | authored half-finished runs | `/__hold/stream { end }` ends the browser's stream after the first draft. `anonymous` withholds the run identity, or `/__run-status/running` keeps a known run in flight. `/__history/pending` stays inconclusive until a one-shot `/__history-values` publishes the outcome (wrong marker or unrelated turn as the fault) | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -172,7 +174,7 @@ example needs that the pilot proxy doesn't yet provide. | 3b | chat/debug | `/__hold/current`, `/__hold/checkpoint`, selected-read variants, `captures[].proof.providerVariant` | its fixture also backs the packed-consumer native-session proof (`chat-debug-native-session-probe.mjs`, its own CI step), so it migrates together with that proof | | 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | | 4b (done) | langgraph/{client-tools, deployment-runtime} | `/__hold/state-write`, `/__fail/state-write`, `/__thread/`, `/__stream/edit` list ops (`append`, `truncate`, `duplicateLast`, `copy`) | client tool rounds key on prompt + `hasToolResult`; a second same-thread turn uses its own prompt (`Weather again`), because aimock fixtures are stateless. "No final values" also strips the markers from the history read the session falls back to | -| 4c | langgraph/{time-travel, durable-execution} | authored checkpoint ids and layouts (`checkpoint-1`, "Select checkpoint 3"), `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__hold-plan`, `/__publish-*` | the specs encode the authored checkpoint layout, so they need rewriting against real checkpoints. A competing tip can become a real state write through the proxy | +| 4c (done) | langgraph/{time-travel, durable-execution} | `/__history-copies`, `/__state/unsafe`, `/__thread//state` writes, `/__stream/edit { state, anonymous, unlessKey }`, `/__hold/stream { afterPattern, end }`, `/__run-status/`, `/__history-values { entry }` | time-travel reads checkpoint ids from real history and thread reads, not authored ids (each turn saves input, start and done checkpoints). The competing tip is a real state write. durable-execution's uncertain outcomes end the browser's stream early, keep history inconclusive, and "publish" the saved outcome through a one-shot history rewrite | | 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | | 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 0c658d9e9..16426e29a 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -422,6 +422,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/time-travel/react/', port: 4607, project: 'cockpit-langgraph-time-travel-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/time-travel/python', + fixtures: 'cockpit/langgraph/time-travel/angular/e2e/fixtures', langgraphPort: 5607, + }, }); assert.deepEqual(reactCockpitConfiguration('subgraphs'), { topic: 'subgraphs', @@ -440,6 +444,10 @@ test('React cockpit configuration accepts only the nine authored topics', async base: '/langgraph/durable-execution/react/', port: 4605, project: 'cockpit-langgraph-durable-execution-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/langgraph/durable-execution/python', + fixtures: 'cockpit/langgraph/durable-execution/angular/e2e/fixtures', langgraphPort: 5605, + }, }); assert.deepEqual(reactCockpitConfiguration('persistence'), { topic: 'persistence', diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 28e390db0..a84613bd9 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -12,7 +12,7 @@ const aimockLanggraph = (example, port) => Object.freeze({ langgraphPort: port + 1000, }); /** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ -const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime']); +const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime', 'time-travel', 'durable-execution']); export function reactCockpitConfiguration(topic = 'streaming') { if (topic === 'ag-ui-a2ui') return Object.freeze({ diff --git a/scripts/react-cockpit/durable-fixture.mjs b/scripts/react-cockpit/durable-fixture.mjs deleted file mode 100644 index c554f80fc..000000000 --- a/scripts/react-cockpit/durable-fixture.mjs +++ /dev/null @@ -1,297 +0,0 @@ -/** Local installed-SDK proof only; never assembled into public assets. */ -export function createDurableFixture() { - let requests = [], - sequence = 0, - held, - lastThread; - let holdStart = false, - holdPlan = false, - holdHistory = false, - failHistory = false, - failCreation = false; - const threads = new Map(), - runs = new Map(); - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - function hold(response, finish) { - const current = { response, finish }; - held = current; - response.on('close', () => { - if (held === current) held = undefined; - }); - } - function final(entry, marker = entry.human.id, unrelated = false) { - entry.values = { - messages: [ - unrelated ? { ...entry.human, id: 'unrelated-human' } : entry.human, - { type: 'ai', id: 'answer-' + entry.runId, content: 'Final answer' }, - ], - step: 'generate', - ...(marker === undefined - ? {} - : { - completed_turn_id: marker, - completed_answer_id: 'answer-' + entry.runId, - }), - }; - entry.next = []; - } - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - lastThread = undefined; - threads.clear(); - runs.clear(); - requests = []; - sequence = 0; - holdStart = holdPlan = holdHistory = failHistory = failCreation = false; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - const controls = { - '/__hold-start': () => { - holdStart = true; - }, - '/__hold-plan': () => { - holdPlan = true; - }, - '/__hold-history': () => { - holdHistory = true; - }, - '/__fail-history': () => { - failHistory = true; - }, - '/__fail-create': () => { - failCreation = true; - }, - '/__publish-final': () => { - final(threads.get(lastThread)); - }, - '/__finish-run': () => { - const run = runs.get(threads.get(lastThread)?.runId); - if (run) run.status = 'success'; - }, - '/__publish-wrong-marker': () => { - final(threads.get(lastThread), 'wrong-human'); - }, - '/__publish-unrelated': () => { - final(threads.get(lastThread), 'unrelated-human', true); - }, - }; - if (Object.hasOwn(controls, pathname)) { - controls[pathname](); - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - threads.set(body.thread_id, { values: { messages: [] }, next: [] }); - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - entry = threads.get(threadId); - if (pathname.endsWith('/history')) { - if (request.method !== 'POST' || !entry) { - json(response, { error: 'Unknown thread' }, 404); - return true; - } - if (failHistory) { - failHistory = false; - json(response, { error: 'PRIVATE history' }, 500); - return true; - } - const finish = () => - json(response, [ - { - values: entry.values, - next: entry.next, - checkpoint: { - thread_id: threadId, - checkpoint_ns: '', - checkpoint_id: 'checkpoint-' + sequence, - }, - metadata: {}, - created_at: '2026-10-01T00:00:00Z', - parent_checkpoint: null, - tasks: [], - }, - ]); - if (holdHistory) { - holdHistory = false; - hold(response, finish); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream')) { - if ( - !entry || - body.assistant_id !== 'durable-execution' || - body.command || - !Array.isArray(body.input?.messages) - ) { - json( - response, - { error: 'Expected a confirmed durable message request' }, - 400 - ); - return true; - } - const human = body.input.messages.at(-1), - text = human.content; - const runId = - '00000000-0000-0000-0000-' + String(++sequence).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - lastThread = threadId; - entry.human = human; - entry.runId = runId; - entry.values = { ...entry.values, messages: [...body.input.messages] }; - entry.next = ['analyze']; - const uncertain = text === 'Uncertain', - knownUncertain = text === 'Known uncertain'; - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - ...(uncertain - ? {} - : { 'content-location': `/threads/${threadId}/runs/${runId}` }), - }); - if (!uncertain) response.write(event('metadata', { run_id: runId })); - response.write(event('values', entry.values)); - const chunk = (node, content) => - response.write( - event('messages', [ - { type: 'AIMessageChunk', id: node + '-' + runId, content }, - { langgraph_node: node }, - ]) - ); - if (uncertain || knownUncertain) { - chunk('analyze', 'Partial analysis'); - entry.values = { - ...entry.values, - messages: [ - human, - { type: 'ai', id: 'analyze-' + runId, content: 'Analysis draft' }, - ], - step: 'analyze', - }; - entry.next = ['plan']; - response.end(); - return true; - } - const finish = () => { - chunk('plan', 'Plan draft'); - entry.values = { - ...entry.values, - messages: [ - ...entry.values.messages, - { type: 'ai', id: 'plan-' + runId, content: 'Plan draft' }, - ], - step: 'plan', - }; - entry.next = ['generate']; - response.write(event('values', entry.values)); - chunk('generate', 'Final answer'); - final(entry); - if (text === 'Missing marker') delete entry.values.completed_turn_id; - if (text === 'Stale marker') - entry.values.completed_turn_id = 'previous-human'; - if (text === 'Wrong marker') - entry.values.completed_turn_id = 'wrong-human'; - if (text === 'Missing answer marker') - delete entry.values.completed_answer_id; - if (text === 'Stale answer marker') - entry.values.completed_answer_id = 'previous-answer'; - if (text === 'Human answer marker') - entry.values.completed_answer_id = human.id; - if (text === 'Unknown tool') - entry.values.messages[1].tool_calls = [ - { id: 'unknown-call', name: 'unknown', args: {} }, - ]; - response.write(event('values', entry.values)); - runs.get(runId).status = 'success'; - response.end(); - }; - const analyze = () => { - if (text === 'Final only') { - chunk('generate', 'Final answer'); - final(entry); - response.write(event('values', entry.values)); - runs.get(runId).status = 'success'; - response.end(); - return; - } - chunk('analyze', 'Analysis draft'); - entry.values = { - ...entry.values, - messages: [ - ...entry.values.messages, - { type: 'ai', id: 'analyze-' + runId, content: 'Analysis draft' }, - ], - step: 'analyze', - }; - entry.next = ['plan']; - response.write(event('values', entry.values)); - if (holdPlan) { - holdPlan = false; - hold(response, finish); - } else finish(); - }; - if (holdStart) { - holdStart = false; - hold(response, analyze); - } else analyze(); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, {}); - return true; - }; -} diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 4141d637e..7b7d64c17 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -30,17 +30,21 @@ * POST /__hold/title hold the next thread metadata read * POST /__hold/history hold the next history read * POST /__state/ checkpoint state reads: safe | hold | failure | - * changed | foreign | malformed + * changed | foreign | malformed | unsafe (a pending + * task on the source) * POST /__hold/state hold the next checkpoint state read * POST /__release-state finish a held checkpoint state read - * POST /__mapped-checkpoints until reset, checkpoints the browser receives - * (history, state, `checkpoints` events) carry the - * root map { "": checkpoint_id } — the HTTP shape - * some deployments return + * POST /__mapped-checkpoints body { extra? }: until reset, checkpoints the + * browser receives (history, state, `checkpoints` + * events) carry the map { "": checkpoint_id, + * ...extra } — the HTTP shape some deployments + * return. Maps the browser sends back are recorded + * as sent and stripped before forwarding. * POST /__release-title finish a held metadata read * POST /__release-history finish a held history read - * POST /__history-values body { set?, omit? }: the next history read's - * latest values get keys set or removed (an + * POST /__history-values body { set?, omit?, entry? }: the next history + * read's latest values get keys set or removed and + * `entry` fields (e.g. next) overwritten (an * authoritative server-side correction, e.g. * replaced or emptied messages). Injected data. * POST /__hold/stream hold the next run stream; body { afterText? } holds @@ -56,7 +60,13 @@ * diagnostic. Injected data, as the authored * subagents fixture did. * { immediate: true } holds right after the first - * event (e.g. a resume whose graph emits no tokens) + * event (e.g. a resume whose graph emits no tokens); + * { afterPattern } after the first event whose raw + * text matches the regex; { end: true } ends the + * browser's stream at that point instead (an + * uncertain outcome) and closes the upstream run + * POST /__run-status/ until reset, GET /runs/ reports status + * (running | success | …); `real` restores it * POST /__interrupt/ unknown | multiple: until reset, root interrupts * the browser receives are rewritten to an * unsupported kind, or duplicated into a batch of @@ -79,6 +89,12 @@ * "$humanId" in data is the submitted message id). * `once: true` applies an op to its first match only; * `ifKey` only to events whose state has that key. + * `unlessKey` skips events whose state has that key. + * `anonymous: true` withholds the run identity (the + * metadata event and Content-Location header). + * With `state: true`, the value ops also apply to + * later checkpoint state reads (until reset), so a + * fork's confirmation read agrees with its stream. * For `checkpoints` events, ops address `values`. * Injected data for stream shapes a real graph * won't produce (missing/stale markers, child @@ -87,6 +103,12 @@ * /threads//state, e.g. a client tool result) * POST /__fail/state-write next client state write answers 500 unforwarded * GET /__thread/ the real thread state (not recorded in /__requests) + * POST /__thread//state write the real thread state (unrecorded), e.g. a + * competing tip committed by someone else + * POST /__history-copies body { copies: [{ index?, set: { path: value } }] }: + * the next history read gains copies of entries, + * with dotted paths overwritten (duplicate, child + * namespace or malformed references). Injected. * POST /__fail-stream next run stream answers 500 without forwarding * (the graph worker is unavailable) * POST /__release flush and resume a held run stream (and finish a @@ -112,7 +134,7 @@ const HISTORY_MODES = [ 'mismatch', 'unsupported', 'foreign', 'failure', 'paused', ]; // Rewrites of checkpoint state reads (POST /threads//state/checkpoint). -const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed']; +const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed', 'unsafe']; // Rewrites of the thread metadata read (GET /threads/) the client uses for titles. const TITLE_MODES = ['safe', 'hold', 'missing', 'wrong-thread', 'invalid', 'titlefailure', 'literal', 'long']; const HOP_BY_HOP = new Set(['connection', 'keep-alive', 'transfer-encoding', 'content-length', 'host']); @@ -158,7 +180,45 @@ function aiText(rawEvent) { } } -export function createLanggraphProxy({ upstream, ready = async () => {} }) { +const at = (root, path) => { + let target = root; + for (const part of (path ?? '').split('.').filter(Boolean)) { + if (target == null) return null; + target = Array.isArray(target) ? target.at(Number(part)) : target[part]; + } + return target; +}; + +/** Applies one /__stream/edit value op to a state object; true when it changed. */ +function applyStateOp(op, state, humanId) { + const withHuman = (value) => JSON.parse(JSON.stringify(value).replaceAll('"$humanId"', JSON.stringify(humanId))); + if (op.ifKey && !(state && typeof state === 'object' && op.ifKey in state)) return false; + if (op.op === 'patch' || op.op === 'omit') { + const target = at(state, op.path); + if (!target || typeof target !== 'object') return false; + if (op.op === 'patch') Object.assign(target, withHuman(op.set ?? {})); + else for (const key of op.keys ?? []) delete target[key]; + return true; + } + if (['append', 'truncate', 'duplicateLast'].includes(op.op)) { + const list = at(state, op.path); + if (!Array.isArray(list)) return false; + if (op.op === 'append') list.push(withHuman(op.value)); + if (op.op === 'truncate') list.splice(0, Math.max(0, list.length - op.keep)); + if (op.op === 'duplicateLast' && list.length) list.push(structuredClone(list.at(-1))); + return true; + } + if (op.op === 'copy') { + const value = at(state, op.from); + const parent = at(state, op.to.split('.').slice(0, -1).join('.')); + if (value === undefined || !parent || typeof parent !== 'object') return false; + parent[op.to.split('.').at(-1)] = structuredClone(value); + return true; + } + return false; +} + +export function createLanggraphProxy({ upstream }) { const target = new URL(upstream); let requests = []; let failCreate = false; @@ -176,6 +236,9 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let nextValues = null; let nextHistoryValues = null; let nextStreamEdits = null; + let stateEdits = null; + let nextHistoryCopies = null; + let runStatus = null; let holdNextStateWrite = false; let failNextStateWrite = false; let heldStateWrite; @@ -208,7 +271,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { }; const mapCheckpoint = (checkpoint) => mapped && checkpoint?.checkpoint_id - ? { ...checkpoint, checkpoint_map: { '': checkpoint.checkpoint_id } } + ? { ...checkpoint, checkpoint_map: { '': checkpoint.checkpoint_id, ...mapped.extra } } : checkpoint; const mapState = (state) => mapped && state && typeof state === 'object' @@ -281,6 +344,9 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { nextValues = null; nextHistoryValues = null; nextStreamEdits = null; + stateEdits = null; + nextHistoryCopies = null; + runStatus = null; holdNextStateWrite = false; failNextStateWrite = false; heldStateWrite?.response.destroy(); @@ -312,15 +378,24 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return Buffer.concat(chunks); } - function transformHistory(input, rewrite) { + function transformHistory(input, rewrite, copyRequests) { const states = Array.isArray(input) ? input.map((state) => mapState({ ...state, values: decorateValues(state.values) })) : input; if (!Array.isArray(states) || states.length === 0) return states; const latest = structuredClone(states[0]); + const copies = (copyRequests ?? []).map((request) => { + const entry = structuredClone(states[request.index ?? 0]); + for (const [path, value] of Object.entries(request.set ?? {})) { + const parent = at(entry, path.split('.').slice(0, -1).join('.')); + if (parent && typeof parent === 'object') parent[path.split('.').at(-1)] = value; + } + return entry; + }); if (rewrite) { latest.values = { ...latest.values, ...(rewrite.set ?? {}) }; for (const key of rewrite.omit ?? []) delete latest.values[key]; + Object.assign(latest, rewrite.entry ?? {}); } const messages = latest.values?.messages ?? []; const assistant = messages.find((m) => m.tool_calls?.length); @@ -347,7 +422,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { latest.checkpoint = { ...latest.checkpoint, thread_id: randomUUID() }; if (historyMode === 'subgraph' && latest.checkpoint) latest.checkpoint = { ...latest.checkpoint, checkpoint_ns: 'foreign:checkpoint' }; - return [latest, ...states.slice(1)]; + return [latest, ...states.slice(1), ...copies]; } async function proxyHistory(request, response, path, body) { @@ -355,6 +430,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { holdNextHistory = false; const rewrite = nextHistoryValues; nextHistoryValues = null; + const copyRequests = nextHistoryCopies; + nextHistoryCopies = null; if (historyMode === 'failed' || historyMode === 'failure') { json(response, { error: 'PRIVATE history' }, 500); return; @@ -368,7 +445,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { response.end(raw); return; } - json(response, transformHistory(JSON.parse(raw.toString('utf8')), rewrite)); + json(response, transformHistory(JSON.parse(raw.toString('utf8')), rewrite, copyRequests)); }; if (!holdThis) return finish(); const held = { response, finish }; @@ -438,6 +515,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { if (mode === 'changed' && messages?.length) messages.at(-1).content = 'Changed canonical content'; if (mode === 'foreign' && state.checkpoint) state.checkpoint.thread_id = randomUUID(); if (mode === 'malformed') state.values = { ...state.values, messages: [{ type: 'human', content: { bad: true }, id: 'malformed' }] }; + if (mode === 'unsafe') state.tasks = [{ id: 'pending-task', name: 'generate', interrupts: [] }]; + for (const op of stateEdits?.ops ?? []) applyStateOp(op, state.values, stateEdits.humanId); json(response, state); }; if (!hold) return finish(); @@ -465,25 +544,19 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { childFault = null; const values = nextValues; nextValues = null; - const edits = (nextStreamEdits ?? []).map((edit) => ({ ...edit, pattern: new RegExp(edit.event), used: false })); + const edits = (nextStreamEdits?.ops ?? []).map((edit) => ({ ...edit, pattern: new RegExp(edit.event), used: false })); + const streamStateEdits = nextStreamEdits?.state ? { ops: edits.filter((op) => !['drop', 'insertAfter'].includes(op.op)) } : null; + const anonymous = nextStreamEdits?.anonymous === true; nextStreamEdits = null; let humanId = null; try { humanId = JSON.parse(body.toString('utf8')).input?.messages?.at(-1)?.id ?? null; } catch { /* no input */ } + if (streamStateEdits && streamStateEdits.ops.length) stateEdits = { ops: streamStateEdits.ops, humanId }; const withHuman = (value) => JSON.parse(JSON.stringify(value).replaceAll('"$humanId"', JSON.stringify(humanId))); - const at = (root, path) => { - let target = root; - for (const part of (path ?? '').split('.').filter(Boolean)) { - if (target == null) return null; - target = Array.isArray(target) ? target.at(Number(part)) : target[part]; - } - return target; - }; const edit = (raw) => { if (!edits.length) return raw; const name = /^event: *([^\r\n]*)/m.exec(raw)?.[1]?.trim() ?? ''; const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); const data = dataLine ? JSON.parse(dataLine.slice(5)) : null; - let out = null; const after = []; let changed = false; for (const op of edits) { @@ -491,37 +564,15 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { // A `checkpoints` event carries state under `values`; ops address the state. const state = name.startsWith('checkpoints') ? data?.values : data; if (op.ifKey && !(state && typeof state === 'object' && op.ifKey in state)) continue; + if (op.unlessKey && state && typeof state === 'object' && op.unlessKey in state) continue; op.used = true; if (op.op === 'drop') return ''; - if (op.op === 'patch' || op.op === 'omit') { - const target = at(state, op.path); - if (!target || typeof target !== 'object') continue; - if (op.op === 'patch') Object.assign(target, withHuman(op.set ?? {})); - else for (const key of op.keys ?? []) delete target[key]; - changed = true; - } - if (['append', 'truncate', 'duplicateLast'].includes(op.op)) { - const list = at(state, op.path); - if (!Array.isArray(list)) continue; - if (op.op === 'append') list.push(withHuman(op.value)); - if (op.op === 'truncate') list.splice(0, Math.max(0, list.length - op.keep)); - if (op.op === 'duplicateLast' && list.length) list.push(structuredClone(list.at(-1))); - changed = true; - } - if (op.op === 'copy') { - const value = at(state, op.from); - const [parent, key] = [at(state, op.to.split('.').slice(0, -1).join('.')), op.to.split('.').at(-1)]; - if (value === undefined || !parent || typeof parent !== 'object') continue; - parent[key] = structuredClone(value); - changed = true; - } if (op.op === 'insertAfter') { const namespace = name.includes('|') ? name.slice(name.indexOf('|')) : ''; after.push(`event: ${op.insert.event.replace('$ns', namespace)}\ndata: ${JSON.stringify(withHuman(op.insert.data))}\n\n`); - } + } else if (applyStateOp(op, state, humanId)) changed = true; } - out = changed ? raw.replace(dataLine, 'data: ' + JSON.stringify(data)) : raw; - return out + after.join(''); + return (changed ? raw.replace(dataLine, 'data: ' + JSON.stringify(data)) : raw) + after.join(''); }; const apply = (state) => { if (!state || typeof state !== 'object' || Array.isArray(state)) return state; @@ -533,6 +584,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { state && typeof state === 'object' && [...Object.keys(values.set ?? {}), ...(values.omit ?? [])].some((key) => key in state); const inject = (raw) => { + if (anonymous && /^event: *metadata\s*$/m.test(raw)) return ''; raw = edit(raw); if (!raw) return raw; if (raw.indexOf('event:') !== raw.lastIndexOf('event:')) @@ -558,7 +610,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const threadId = /\/threads\/([^/]+)\/runs\/stream/.exec(path)?.[1]; const titleBefore = threadId ? await threadTitle(threadId) : null; const upstreamResponse = await send(request.method, path, request.headers, body); - response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + const headers = forwardedHeaders(upstreamResponse.headers); + if (anonymous) + for (const name of Object.keys(headers)) if (name.toLowerCase() === 'content-location') delete headers[name]; + response.writeHead(upstreamResponse.statusCode, headers); response.flushHeaders(); response.on('close', () => upstreamResponse.destroy()); // Forward whole SSE events; when a hold is armed, buffer after its condition. @@ -649,6 +704,12 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { break; } response.write(inject(raw)); + if (armed && hold.afterPattern && new RegExp(hold.afterPattern).test(raw)) { + armed = false; + holding = true; + heldStream = held; + continue; + } if (armed && hold.immediate) { armed = false; holding = true; @@ -664,7 +725,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const delta = aiText(raw); if (delta === null) continue; text += delta; - if (armed && !hold.beforeToolResult && !hold.afterChildMessage && !hold.immediate && (hold.afterText ? text.includes(hold.afterText) : true)) { + if (armed && !hold.beforeToolResult && !hold.afterChildMessage && !hold.immediate && !hold.afterPattern && (hold.afterText ? text.includes(hold.afterText) : true)) { armed = false; holding = true; heldStream = held; @@ -674,6 +735,13 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { buffered.push(pending); pending = ''; } + if (holding && hold.end && !response.writableEnded) { + // An uncertain outcome: the browser's stream ends here, and the + // upstream connection closes without a terminal event. + if (heldStream === held) heldStream = undefined; + response.end(); + upstreamResponse.destroy(); + } }); upstreamResponse.on('end', () => { ended = true; @@ -722,17 +790,23 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { beforeToolResult: body.beforeToolResult === true, afterChildMessage: body.afterChildMessage === true, immediate: body.immediate === true, + afterPattern: typeof body.afterPattern === 'string' ? body.afterPattern : null, + end: body.end === true, }; return {}; }], '/__fail-stream': ['POST', () => { failStream = true; return {}; }], '/__values': ['POST', (body) => { nextValues = body; return {}; }], - '/__stream/edit': ['POST', (body) => { nextStreamEdits = body.ops ?? []; return {}; }], + '/__stream/edit': ['POST', (body) => { + nextStreamEdits = { ops: body.ops ?? [], state: body.state === true, anonymous: body.anonymous === true }; + return {}; + }], '/__history-values': ['POST', (body) => { nextHistoryValues = body; return {}; }], + '/__history-copies': ['POST', (body) => { nextHistoryCopies = body.copies ?? []; return {}; }], '/__hold/title': ['POST', () => { holdNextTitle = true; return {}; }], '/__hold/history': ['POST', () => { holdNextHistory = true; return {}; }], '/__hold/state': ['POST', () => { holdNextState = true; return {}; }], - '/__mapped-checkpoints': ['POST', () => { mapped = true; return {}; }], + '/__mapped-checkpoints': ['POST', (body) => { mapped = { extra: body.extra ?? {} }; return {}; }], '/__hold/state-write': ['POST', () => { holdNextStateWrite = true; return {}; }], '/__fail/state-write': ['POST', () => { failNextStateWrite = true; return {}; }], '/__release-state': ['POST', () => { @@ -766,11 +840,24 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } return true; } + if (pathname.startsWith('/__thread/') && pathname.endsWith('/state') && request.method === 'POST') { + const threadId = pathname.slice('/__thread/'.length, -'/state'.length); + const update = await readAll(request); + const answer = await send('POST', `/threads/${encodeURIComponent(threadId)}/state`, { 'content-type': 'application/json' }, update); + json(response, JSON.parse((await readAll(answer)).toString('utf8') || '{}'), answer.statusCode); + return true; + } if (pathname.startsWith('/__thread/') && request.method === 'GET') { const threadId = pathname.slice('/__thread/'.length); json(response, await upstreamJson(`/threads/${encodeURIComponent(threadId)}/state`)); return true; } + if (pathname.startsWith('/__run-status/')) { + const status = pathname.slice('/__run-status/'.length); + runStatus = status === 'real' ? null : status; + json(response, {}); + return true; + } if (pathname.startsWith('/__interrupt/')) { const mode = pathname.slice('/__interrupt/'.length); if (!['unknown', 'multiple'].includes(mode)) json(response, {}, 400); @@ -831,7 +918,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { openResponses += 1; response.once('close', () => { openResponses -= 1; }); await ready(); - const body = await readAll(request); + let body = await readAll(request); const search = new URL(request.url, 'http://localhost').search; const path = pathname.slice(prefix.length) + search; requests.push({ @@ -840,6 +927,13 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { body: body.length ? JSON.parse(body.toString('utf8')) : {}, key: request.headers['x-api-key'] ?? null, }); + if (mapped && body.length) { + const parsed = JSON.parse(body.toString('utf8')); + if (parsed?.checkpoint?.checkpoint_map) { + delete parsed.checkpoint.checkpoint_map; + body = Buffer.from(JSON.stringify(parsed)); + } + } try { if (request.method === 'POST' && /^\/threads\/?$/.test(pathname.slice(prefix.length))) { if (failCreate) { @@ -856,6 +950,15 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { json(response, { error: 'PRIVATE graph worker failure' }, 500); } else await proxyStream(request, response, path, body); } + else if (runStatus && request.method === 'GET' && /\/threads\/[^/]+\/runs\/[^/]+$/.test(pathname)) { + const upstreamResponse = await send(request.method, path, request.headers, body); + const raw = await readAll(upstreamResponse); + const run = upstreamResponse.statusCode === 200 ? JSON.parse(raw.toString('utf8')) : null; + if (!run) { + response.writeHead(upstreamResponse.statusCode, forwardedHeaders(upstreamResponse.headers)); + response.end(raw); + } else json(response, { ...run, status: runStatus }); + } else if (request.method === 'POST' && /\/threads\/[^/]+\/state$/.test(pathname)) { if (failNextStateWrite) { failNextStateWrite = false; diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index ed5a084a3..865265efa 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -10,8 +10,6 @@ import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; -import { createDurableFixture } from './durable-fixture.mjs'; -import { createTimeTravelFixture } from './time-travel-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatDebugFixture } from './chat-debug-fixture.mjs'; @@ -99,10 +97,6 @@ const agUiInterruptsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'interrupts' ? createAgUiInterruptsFixture() : null; -const timeTravelFixture = - configuration.topic === 'time-travel' ? createTimeTravelFixture() : null; -const durableFixture = - configuration.topic === 'durable-execution' ? createDurableFixture() : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( @@ -242,13 +236,6 @@ const server = createServer(async (request, response) => { (await agUiStreamingFixture(request, response, pathname)) ) return; - if ( - timeTravelFixture && - (await timeTravelFixture(request, response, pathname)) - ) - return; - if (durableFixture && (await durableFixture(request, response, pathname))) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || diff --git a/scripts/react-cockpit/time-travel-fixture.mjs b/scripts/react-cockpit/time-travel-fixture.mjs deleted file mode 100644 index 5b487c2bc..000000000 --- a/scripts/react-cockpit/time-travel-fixture.mjs +++ /dev/null @@ -1,403 +0,0 @@ -/** Local installed-SDK verification only; never assembled into public assets. */ -export function createTimeTravelFixture() { - let requests = [], - sequence = 0, - lastThread, - held; - const threads = new Map(), - runs = new Map(); - let holdState = false, - holdStream = false, - holdHistory = false; - let failHistory = false, - failCreation = false, - unsafeSource = false, - badHistory = false, - mappedCheckpoints = false; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (type, data) => - `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; - function hold(response, finish) { - const selected = { response, finish }; - held = selected; - response.on('close', () => { - if (held === selected) held = undefined; - }); - } - const position = (thread, id) => ({ - thread_id: thread, - checkpoint_ns: '', - checkpoint_id: id, - ...(mappedCheckpoints - ? { checkpoint_map: { '': id, child: 'literal-map-value' } } - : {}), - }); - function matchesReference(candidate, expected) { - if ( - !candidate || - candidate.thread_id !== expected.thread_id || - candidate.checkpoint_ns !== expected.checkpoint_ns || - candidate.checkpoint_id !== expected.checkpoint_id - ) - return false; - const actual = candidate.checkpoint_map ?? {}, - wanted = expected.checkpoint_map ?? {}; - if ( - candidate.checkpoint_map === null || - typeof actual !== 'object' || - Array.isArray(actual) - ) - return false; - const keys = Object.keys(actual).sort(), - expectedKeys = Object.keys(wanted).sort(); - return ( - JSON.stringify(keys) === JSON.stringify(expectedKeys) && - keys.every( - (key) => typeof actual[key] === 'string' && actual[key] === wanted[key] - ) - ); - } - const saved = (thread, id, values, parent, runId, next = [], tasks = []) => ({ - checkpoint: position(thread, id), - values, - parent_checkpoint: parent, - next, - tasks, - metadata: { run_id: runId }, - created_at: '2026-10-02T00:00:00Z', - }); - function checkpointEvent(state) { - return event('checkpoints', { - config: { - configurable: { ...state.checkpoint, run_id: state.metadata.run_id }, - }, - values: state.values, - next: state.next, - tasks: state.tasks.map(({ id, name }) => ({ id, name })), - }); - } - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - sequence = 0; - lastThread = undefined; - threads.clear(); - runs.clear(); - holdState = - holdStream = - holdHistory = - failHistory = - failCreation = - unsafeSource = - badHistory = - mappedCheckpoints = - false; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: !!held }); - return true; - } - if (pathname === '/__states') { - json(response, [...(threads.get(lastThread)?.states.values() ?? [])]); - return true; - } - if (pathname === '/__release') { - const selected = held; - held = undefined; - if (selected && !selected.response.destroyed) selected.finish(); - json(response, {}); - return true; - } - const controls = { - '/__hold-state': () => { - holdState = true; - }, - '/__hold-stream': () => { - holdStream = true; - }, - '/__hold-history': () => { - holdHistory = true; - }, - '/__fail-history': () => { - failHistory = true; - }, - '/__fail-create': () => { - failCreation = true; - }, - '/__unsafe-source': () => { - unsafeSource = true; - }, - '/__bad-history': () => { - badHistory = true; - }, - '/__mapped-checkpoints': () => { - mappedCheckpoints = true; - }, - '/__advance-tip': () => { - const thread = threads.get(lastThread); - if (!thread) throw new Error('No fixture conversation'); - const state = saved( - lastThread, - 'competing-tip', - { - messages: [ - { - type: 'human', - id: 'competing-human', - content: 'Competing tip', - }, - { - type: 'ai', - id: 'competing-answer', - content: 'Competing tip answer', - }, - ], - }, - thread.tip, - 'competing-run' - ); - thread.states.set('competing-tip', state); - thread.tip = state.checkpoint; - }, - }; - if (Object.hasOwn(controls, pathname)) { - controls[pathname](); - json(response, {}); - return true; - } - if ( - !pathname.startsWith('/api/') && - !pathname.startsWith('/developer-api/') - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ - path: pathname, - method: request.method, - body, - key: request.headers['x-api-key'] ?? null, - }); - if (pathname.endsWith('/threads')) { - if (failCreation) { - failCreation = false; - json(response, { error: 'PRIVATE creation' }, 500); - return true; - } - threads.set(body.thread_id, { states: new Map(), tip: null }); - lastThread = body.thread_id; - json(response, { thread_id: body.thread_id }); - return true; - } - const threadId = /\/threads\/([^/]+)/.exec(pathname)?.[1], - thread = threads.get(threadId); - if (!thread) { - json(response, { error: 'Unknown thread' }, 404); - return true; - } - if (pathname.endsWith('/history')) { - if (failHistory) { - failHistory = false; - json(response, { error: 'PRIVATE read' }, 500); - return true; - } - const finish = () => { - const page = [...thread.states.values()].reverse(); - json( - response, - badHistory - ? [ - ...page, - { ...page[0] }, - { - ...page[0], - checkpoint: { - ...page[0].checkpoint, - checkpoint_id: 'child-root', - checkpoint_ns: 'child:literal', - }, - }, - { - ...page[0], - checkpoint: { - ...page[0].checkpoint, - checkpoint_id: 'invalid-map', - checkpoint_map: null, - }, - }, - ] - : page - ); - }; - if (holdHistory) { - holdHistory = false; - hold(response, finish); - } else finish(); - return true; - } - if (pathname.endsWith('/state/checkpoint')) { - const finish = () => { - const state = thread.states.get(body.checkpoint?.checkpoint_id); - if (!state || !matchesReference(body.checkpoint, state.checkpoint)) { - json(response, { error: 'Unknown exact checkpoint' }, 404); - return; - } - json( - response, - unsafeSource - ? { - ...state, - tasks: [ - { id: 'pending-task', name: 'generate', interrupts: [] }, - ], - } - : state - ); - }; - if (holdState) { - holdState = false; - hold(response, finish); - } else finish(); - return true; - } - if (pathname.endsWith('/runs/stream')) { - if ( - body.assistant_id !== 'time-travel' || - body.command || - !Array.isArray(body.input?.messages) - ) { - json(response, { error: 'Expected a Time Travel message' }, 400); - return true; - } - const selected = body.checkpoint ?? thread.tip; - const source = selected - ? thread.states.get(selected.checkpoint_id) - : undefined; - if ( - selected && - (!source || !matchesReference(selected, source.checkpoint)) - ) { - json(response, { error: 'Unknown source' }, 400); - return true; - } - const human = body.input.messages.at(-1), - text = human.content; - const number = ++sequence, - runId = '00000000-0000-0000-0000-' + String(number).padStart(12, '0'); - runs.set(runId, { thread_id: threadId, status: 'running' }); - lastThread = threadId; - const messages = [...(source?.values.messages ?? []), human]; - const initial = saved( - threadId, - 'checkpoint-input-' + number, - { ...source?.values, messages }, - source?.checkpoint ?? null, - runId, - ['generate'], - [ - { - id: 'task-' + number, - name: 'generate', - interrupts: [], - result: null, - error: null, - }, - ] - ); - thread.states.set(initial.checkpoint.checkpoint_id, initial); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - response.write(event('metadata', { run_id: runId, thread_id: threadId })); - response.write(event('values', initial.values)); - response.write( - event('messages', [ - { - type: 'AIMessageChunk', - id: 'chunk-' + runId, - content: 'Transient chunk', - }, - { langgraph_node: 'generate' }, - ]) - ); - const finish = () => { - const answer = { - type: 'ai', - id: 'answer-' + runId, - content: - text === 'Literal' - ? '' - : 'Answer to ' + text, - }; - const canonical = [...messages, answer]; - const values = { - messages: canonical, - completed_turn_id: human.id, - completed_answer_id: answer.id, - completed_message_ids: canonical.map((message) => message.id), - }; - if (text === 'Truncated history') - values.completed_message_ids = [human.id, answer.id]; - if (text === 'Missing marker') delete values.completed_turn_id; - if (text === 'Stale marker') - values.completed_turn_id = - source?.values.messages[0]?.id ?? 'old-human'; - if (text === 'Equal marker') values.completed_answer_id = human.id; - if (text === 'Unknown tool') - answer.tool_calls = [ - { id: 'unknown-tool', name: 'unknown', args: {} }, - ]; - const final = saved( - threadId, - 'checkpoint-' + number, - values, - initial.checkpoint, - runId - ); - thread.states.set(final.checkpoint.checkpoint_id, final); - thread.tip = final.checkpoint; - response.write(event('values', values)); - response.write(checkpointEvent(final)); - runs.get(runId).status = 'success'; - response.end(); - }; - if (holdStream) { - holdStream = false; - hold(response, finish); - } else finish(); - return true; - } - if (pathname.endsWith('/cancel')) { - held?.response.end(); - held = undefined; - json(response, {}); - return true; - } - const runId = /\/runs\/([^/]+)$/.exec(pathname)?.[1]; - if (request.method === 'GET' && runId) { - const run = runs.get(runId); - json( - response, - run ? { run_id: runId, ...run } : { error: 'Unknown physical run' }, - run ? 200 : 404 - ); - return true; - } - json(response, { error: 'Unsupported fixture endpoint' }, 404); - return true; - }; -} From 880cb67a1a3071997ae2a6bd58c5d35a300a0efe Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:39:16 -0800 Subject: [PATCH 13/41] test(website): drive the langgraph time-travel/durable-execution previews on aimock - time-travel selects turn A's completed checkpoint from the history page the app just loaded (real history saves input, start and done checkpoints). - durable-execution sets up the uncertain run (anonymous, early end, pending history), waits for the app's own history read, then publishes the saved outcome with a one-shot history rewrite before holding the status read. - Restore the proxy's `ready` option lost in the restack (every /api request failed with it undefined). - aimock-backend close() stops the server processes under `uv run`, which does not forward signals. Co-Authored-By: Claude Opus 5.5 --- .../react-durable-execution-preview.spec.ts | 42 +++++++++++++++++-- .../e2e/react-time-travel-preview.spec.ts | 29 ++++++++++--- apps/website/playwright.config.ts | 4 +- scripts/react-cockpit/aimock-backend.mjs | 13 +++++- scripts/react-cockpit/langgraph-proxy.mjs | 2 +- 5 files changed, 76 insertions(+), 14 deletions(-) diff --git a/apps/website/e2e/react-durable-execution-preview.spec.ts b/apps/website/e2e/react-durable-execution-preview.spec.ts index 2fb265066..cd4b3240e 100644 --- a/apps/website/e2e/react-durable-execution-preview.spec.ts +++ b/apps/website/e2e/react-durable-execution-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -65,6 +68,12 @@ test('switching away releases a held status read and returning starts empty', as ); const origin = 'http://127.0.0.1:4605'; await request.post(origin + '/__reset'); + await request.post(origin + '/__ready', { timeout: 180000 }); + // An unknown run whose connection ends after the first draft, with saved + // history inconclusive until its outcome is published. + await request.post(origin + '/__stream/edit', { data: { ops: [], anonymous: true } }); + await request.post(origin + '/__hold/stream', { data: { end: true } }); + await request.post(origin + '/__history/pending'); await page.goto( '/docs/react/langgraph/guides/durable-execution?mode=run' ); @@ -74,8 +83,35 @@ test('switching away releases a held status read and returning starts empty', as await expect( frame.getByRole('button', { name: 'Check status', exact: true }) ).toBeVisible(); - await request.post(origin + '/__publish-final'); - await request.post(origin + '/__hold-history'); + // The app reads saved history once on its own; let that read finish before + // the next one is held. + await expect + .poll(async () => + ((await (await request.get(origin + '/__requests')).json()) as { path: string }[]) + .filter((entry) => entry.path.endsWith('/history')).length, + { timeout: 20000 } + ) + .toBe(1); + const runs: { path: string; body: { input?: { messages: { id: string }[] } } }[] = + await (await request.get(origin + '/__requests')).json(); + const humanId = runs.find((entry) => entry.path.endsWith('/runs/stream'))! + .body.input!.messages[0].id; + await request.post(origin + '/__history/safe'); + await request.post(origin + '/__history-values', { + data: { + set: { + messages: [ + { type: 'human', id: humanId, content: 'Uncertain' }, + { type: 'ai', id: 'final-answer', content: 'Final answer' }, + ], + step: 'generate', + completed_turn_id: humanId, + completed_answer_id: 'final-answer', + }, + entry: { next: [], tasks: [] }, + }, + }); + await request.post(origin + '/__hold/history'); await frame .getByRole('button', { name: 'Check status', exact: true }) .click(); @@ -88,7 +124,7 @@ test('switching away releases a held status read and returning starts empty', as async () => ( await (await request.get(origin + '/__lifetime')).json() - ).active + ).activeHistory ) .toBe(false); await request.post(origin + '/__release'); diff --git a/apps/website/e2e/react-time-travel-preview.spec.ts b/apps/website/e2e/react-time-travel-preview.spec.ts index c4f0125ff..582b7a815 100644 --- a/apps/website/e2e/react-time-travel-preview.spec.ts +++ b/apps/website/e2e/react-time-travel-preview.spec.ts @@ -1,3 +1,6 @@ +// The preview server (`serve.mjs --backend`) runs the real graph under +// `langgraph dev` on aimock fixtures shared with the Angular example; controls +// come from scripts/react-cockpit/langgraph-proxy.mjs. import { documentationModes } from './fixtures/workspace-controls'; import { selectFramework, expectFrameworkControl } from './fixtures/framework-controls'; import { test, expect } from '@playwright/test'; @@ -65,6 +68,7 @@ test('switching away releases a held fork and returning starts with an empty che ); const origin = 'http://127.0.0.1:4607'; await request.post(origin + '/__reset'); + await request.post(origin + '/__ready', { timeout: 180000 }); await page.goto('/docs/react/langgraph/guides/time-travel?mode=run'); const frame = page.frameLocator('iframe'); for (const text of ['A', 'B']) { @@ -72,16 +76,29 @@ test('switching away releases a held fork and returning starts with an empty che await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Response complete.'); } + // Real history saves input, start and done checkpoints per turn: select + // turn A's completed checkpoint from the page the app just loaded. + const loaded = page.waitForResponse((r) => r.url().endsWith('/history')); await frame .getByRole('button', { name: 'Refresh saved checkpoints' }) .click(); - await frame.getByRole('button', { name: 'Select checkpoint 3' }).click(); - await request.post(origin + '/__hold-stream'); + const history: { next: string[]; values: { messages: unknown[] } }[] = + await (await loaded).json(); + const a = history.findIndex( + (entry) => entry.next.length === 0 && entry.values.messages.length === 2 + ); + await frame + .getByRole('button', { name: `Select checkpoint ${a + 1}` }) + .click(); + await request.post(origin + '/__hold/stream'); await frame.getByLabel('Message', { exact: true }).fill('Held fork'); await frame.getByRole('button', { name: 'Fork selected checkpoint' }).click(); - await expect( - frame.getByRole('region', { name: 'Conversation', exact: true }) - ).toContainText('Transient chunk'); + await expect + .poll( + async () => + (await (await request.get(origin + '/__lifetime')).json()).activeStream + ) + .toBe(true); const oldFrame = await page.locator('iframe').elementHandle(); await selectFramework(page, 'angular'); await expect.poll(() => oldFrame?.evaluate((element) => element.isConnected)).toBe(false); @@ -90,7 +107,7 @@ test('switching away releases a held fork and returning starts with an empty che async () => ( await (await request.get(origin + '/__lifetime')).json() - ).active + ).activeStream ) .toBe(false); await request.post(origin + '/__release'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 0e3094dc2..aad6a6237 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -290,7 +290,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent', + 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4605', reuseExistingServer, @@ -306,7 +306,7 @@ export const createWebsitePlaywrightConfig = ( }, { command: - 'node scripts/react-cockpit/serve.mjs time-travel --no-parent', + 'node scripts/react-cockpit/serve.mjs time-travel --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4607', reuseExistingServer, diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index b94dedf16..070102974 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -9,7 +9,7 @@ * pays for the previews it actually drives. The child stays in this process's * group, so Playwright's webServer teardown stops it with the server. */ -import { spawn } from 'node:child_process'; +import { execFileSync, spawn } from 'node:child_process'; import { readFileSync, readdirSync } from 'node:fs'; import { join, resolve } from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; @@ -69,7 +69,16 @@ export function createAimockBackend({ root, backend }) { return starting; }, async close() { - child?.kill('SIGTERM'); + // `uv run` does not forward signals to the server it started: stop the + // server processes under it first, then uv itself. + if (child?.pid && child.exitCode === null) { + try { + execFileSync('pkill', ['-TERM', '-P', String(child.pid)]); + } catch { + // no children left + } + child.kill('SIGTERM'); + } await mock?.stop(); }, }; diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 7b7d64c17..90d5bd543 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -218,7 +218,7 @@ function applyStateOp(op, state, humanId) { return false; } -export function createLanggraphProxy({ upstream }) { +export function createLanggraphProxy({ upstream, ready = async () => {} }) { const target = new URL(upstream); let requests = []; let failCreate = false; From bf4d5c879bb58227edf1820b769765c27b82f81c Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:51:28 -0800 Subject: [PATCH 14/41] test(website): expect the time-travel and durable-execution previews' aimock backend flags in the config spec Co-Authored-By: Claude Opus 5.5 --- apps/website/src/playwright-config.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 62f755bd7..4ae002c15 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -476,7 +476,7 @@ describe('Website Playwright configuration', () => { }), expect.objectContaining({ command: - 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent', + 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent --backend', url: 'http://127.0.0.1:4605', }), expect.objectContaining({ @@ -484,7 +484,7 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4606', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs time-travel --no-parent', + command: 'node scripts/react-cockpit/serve.mjs time-travel --no-parent --backend', url: 'http://127.0.0.1:4607', }), expect.objectContaining({ From ecc82896602285171ae4e3e4adde7b7cbc5c424c Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:45:46 -0800 Subject: [PATCH 15/41] test(cockpit): run React deep-agents/filesystem e2e, integration and website preview on aimock Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- .../angular/e2e/fixtures/da-filesystem.json | 395 +++++++++ .../filesystem/python/tests/test_streaming.py | 3 - .../filesystem/react/e2e/filesystem.spec.ts | 18 +- .../filesystem/react/e2e/global-setup-impl.ts | 6 + .../filesystem/react/e2e/playwright.config.ts | 3 + .../deep-agents/filesystem/react/project.json | 31 +- .../react/src/connection.integration.spec.ts | 92 +-- scripts/cockpit-frontend-matrix.spec.mjs | 4 + scripts/examples/e2e-wiring.spec.ts | 7 +- scripts/react-cockpit/configuration.mjs | 2 +- ...p-agents-filesystem-configuration.spec.mjs | 21 - .../deep-agents-filesystem-fixture.mjs | 557 ------------- .../deep-agents-filesystem-fixture.spec.mjs | 771 ------------------ .../deep-agents-filesystem-wire.py | 211 ----- scripts/react-cockpit/serve.mjs | 7 +- 16 files changed, 478 insertions(+), 1652 deletions(-) create mode 100644 cockpit/deep-agents/filesystem/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/deep-agents-filesystem-configuration.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-filesystem-fixture.mjs delete mode 100644 scripts/react-cockpit/deep-agents-filesystem-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-filesystem-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index aad6a6237..6f8c09e42 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -486,7 +486,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-deep-agents-filesystem-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent', + command: 'node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4629', reuseExistingServer, diff --git a/cockpit/deep-agents/filesystem/angular/e2e/fixtures/da-filesystem.json b/cockpit/deep-agents/filesystem/angular/e2e/fixtures/da-filesystem.json index d1c774b7d..774e95fcf 100644 --- a/cockpit/deep-agents/filesystem/angular/e2e/fixtures/da-filesystem.json +++ b/cockpit/deep-agents/filesystem/angular/e2e/fixtures/da-filesystem.json @@ -58,6 +58,401 @@ } ] } + }, + { + "match": { "toolCallId": "fs-normal-note" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-normal-report" + } + ] + } + }, + { + "match": { "toolCallId": "fs-normal-report" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-read-only-ls" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-empty-empty" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-write-error-invalid" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-mixed-read" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-protected-edit-initial" }, + "response": { + "toolCalls": [ + { + "name": "edit_file", + "arguments": "{\"file_path\": \"/reports/existing.md\", \"old_string\": \"old\", \"new_string\": \"new\", \"replace_all\": true}", + "id": "fs-protected-edit-edit" + } + ] + } + }, + { + "match": { "toolCallId": "fs-protected-edit-edit" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-report-overwrite-initial" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/existing.md\", \"content\": \"Replacement report\"}", + "id": "fs-report-overwrite-replacement" + } + ] + } + }, + { + "match": { "toolCallId": "fs-report-overwrite-replacement" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-overwrite-note" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"Replacement\"}", + "id": "fs-overwrite-overwrite" + } + ] + } + }, + { + "match": { "toolCallId": "fs-overwrite-overwrite" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-edit-note" }, + "response": { + "toolCalls": [ + { + "name": "edit_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"old_string\": \"KASE\", \"new_string\": \"Aspen\", \"replace_all\": true}", + "id": "fs-edit-edit" + } + ] + } + }, + { + "match": { "toolCallId": "fs-edit-edit" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-delete-report" }, + "response": { + "toolCalls": [ + { + "name": "delete", + "arguments": "{\"file_path\": \"/reports\"}", + "id": "fs-delete-delete" + } + ] + } + }, + { + "match": { "toolCallId": "fs-delete-delete" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-batch-note" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-batch-report" + }, + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/second.md\", \"content\": \"Second report\"}", + "id": "fs-batch-second" + } + ] + } + }, + { + "match": { "toolCallId": "fs-batch-second" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-duplicates-note" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-duplicates-report" + }, + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"Second report\"}", + "id": "fs-duplicates-second" + } + ] + } + }, + { + "match": { "toolCallId": "fs-duplicates-second" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { "toolCallId": "fs-reject-reproposal-note" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-reject-reproposal-report" + } + ] + } + }, + { + "match": { "toolCallId": "fs-reject-reproposal-report" }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"Revised assessment\"}", + "id": "fs-reject-reproposal-reproposal" + } + ] + } + }, + { + "match": { "toolCallId": "fs-reject-reproposal-reproposal" }, + "response": { "content": "Files reconciled." } + }, + { + "match": { + "userMessage": "[scenario:normal]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-normal-note" + } + ] + } + }, + { + "match": { "userMessage": "[scenario:no-files]" }, + "response": { "content": "No file changes were needed." } + }, + { + "match": { "userMessage": "[scenario:unchanged]" }, + "response": { "content": "No file changes were needed." } + }, + { + "match": { + "userMessage": "[scenario:read-only]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "ls", + "arguments": "{\"path\": \"/\"}", + "id": "fs-read-only-ls" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:empty]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/empty.txt\", \"content\": \"\"}", + "id": "fs-empty-empty" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:write-error]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/invalid.txt\", \"content\": 42}", + "id": "fs-write-error-invalid" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:mixed]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-mixed-note" + }, + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-mixed-report" + }, + { + "name": "read_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\"}", + "id": "fs-mixed-read" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:protected-edit]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/existing.md\", \"content\": \"old old\"}", + "id": "fs-protected-edit-initial" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:report-overwrite]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/existing.md\", \"content\": \"Prior report\"}", + "id": "fs-report-overwrite-initial" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:overwrite]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-overwrite-note" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:edit]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-edit-note" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:delete]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/reports/kase.md\", \"content\": \"KASE assessment ready.\"}", + "id": "fs-delete-report" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:batch]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-batch-note" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:duplicates]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-duplicates-note" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:reject-reproposal]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/notes/kase.txt\", \"content\": \"KASE field elevation is 7820 ft.\"}", + "id": "fs-reject-reproposal-note" + } + ] + } } ] } diff --git a/cockpit/deep-agents/filesystem/python/tests/test_streaming.py b/cockpit/deep-agents/filesystem/python/tests/test_streaming.py index 3291e95ea..5ebb200b9 100644 --- a/cockpit/deep-agents/filesystem/python/tests/test_streaming.py +++ b/cockpit/deep-agents/filesystem/python/tests/test_streaming.py @@ -249,8 +249,5 @@ async def test_mixed_pending_batch_pauses_before_unrestricted_note_and_report(se self.assertEqual(terminal.values["files"]["/reports/a.txt"]["content"],"report") self.assertFalse(terminal.next) - def test_worker_contract_exists(self): - self.assertTrue((Path(__file__).parents[5]/"scripts/react-cockpit/deep-agents-filesystem-wire.py").is_file(), "Filesystem worker missing") - if __name__ == "__main__": unittest.main() diff --git a/cockpit/deep-agents/filesystem/react/e2e/filesystem.spec.ts b/cockpit/deep-agents/filesystem/react/e2e/filesystem.spec.ts index 78da5eca4..9c3d07e76 100644 --- a/cockpit/deep-agents/filesystem/react/e2e/filesystem.spec.ts +++ b/cockpit/deep-agents/filesystem/react/e2e/filesystem.spec.ts @@ -1,4 +1,8 @@ import { test, expect } from '@playwright/test'; +// Backend: aimock replays the recorded Angular fixtures +// (cockpit/deep-agents/filesystem/angular/e2e/fixtures) into a real +// `langgraph dev`; the suggestion prompt is the recorded one, so the files and +// the protected-write approval are the real graph's. const base = '/deep-agents/filesystem/react/'; test('installed native candidate keeps draft inert, shows actual pause, then approves complete batch', async ({ page, @@ -19,7 +23,7 @@ test('installed native candidate keeps draft inert, shows actual pause, then app page.getByRole('button', { name: 'Approve entire batch' }) ).toBeVisible(); await expect(page.getByRole('region', { name: 'Workspace' })).toContainText( - 'KASE field elevation is 7820 ft.' + 'KASE Field Elevation: 7,820 ft' ); await expect( page.getByRole('region', { name: 'Awaiting approval' }) @@ -41,10 +45,10 @@ test('installed native candidate keeps draft inert, shows actual pause, then app await page.getByRole('button', { name: 'Approve entire batch' }).click(); await expect(page.getByRole('status')).toContainText('Response saved.'); await page - .getByRole('button', { name: '/reports/kase.md', exact: true }) + .getByRole('button', { name: '/reports/kase-runway.md', exact: true }) .click(); await expect(page.getByRole('region', { name: 'Workspace' })).toContainText( - 'KASE assessment ready.' + 'Runway Suitability Note: KASE' ); await page.screenshot({ path: '/tmp/threadplane-filesystem-task3-terminal-mobile.png', @@ -60,13 +64,5 @@ test('installed native candidate keeps draft inert, shows actual pause, then app expect( requests.filter((x) => x.body?.command).map((x) => x.body.command) ).toEqual([{ resume: { decisions: [{ type: 'approve' }] } }]); - const proofs = await (await request.get('/__graph-proof')).json(); - expect(proofs.length).toBeGreaterThan(0); - expect( - proofs.filter((x) => ['submit', 'resume'].includes(x.op)).map((x) => x.op) - ).toEqual(['submit', 'resume']); expect(pageErrors).toEqual([]); - expect( - proofs.every((x) => x.actualCompiledGraph && x.networkConnectAttempts === 0) - ).toBe(true); }); diff --git a/cockpit/deep-agents/filesystem/react/e2e/global-setup-impl.ts b/cockpit/deep-agents/filesystem/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..505e9e10e --- /dev/null +++ b/cockpit/deep-agents/filesystem/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'deep-agents/filesystem', port: 4629 }); diff --git a/cockpit/deep-agents/filesystem/react/e2e/playwright.config.ts b/cockpit/deep-agents/filesystem/react/e2e/playwright.config.ts index 113cbecf3..5263fa9c3 100644 --- a/cockpit/deep-agents/filesystem/react/e2e/playwright.config.ts +++ b/cockpit/deep-agents/filesystem/react/e2e/playwright.config.ts @@ -6,6 +6,9 @@ export default defineConfig({ timeout: 30000, use: { baseURL: 'http://127.0.0.1:4629', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/deep-agents/filesystem/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent', diff --git a/cockpit/deep-agents/filesystem/react/project.json b/cockpit/deep-agents/filesystem/react/project.json index 7e0f4f0dc..db5e414ac 100644 --- a/cockpit/deep-agents/filesystem/react/project.json +++ b/cockpit/deep-agents/filesystem/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/deep-agents/filesystem/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-deep-agents-filesystem-angular", + "cockpit-deep-agents-filesystem-python" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -21,7 +25,12 @@ "react" ] }, - { "target": "react-langgraph", "projects": ["website-runtime-preparation"] } + { + "target": "react-langgraph", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/deep-agents/filesystem/react" @@ -30,10 +39,15 @@ "command": "node scripts/react-cockpit/build.mjs deep-agents-filesystem" } }, - "fixture-test": { - "executor": "nx:run-commands", + "e2e": { + "executor": "@nx/playwright:playwright", "cache": false, + "inputs": [ + "default", + "reactAimockBackend" + ], "dependsOn": [ + "build", { "target": "test", "projects": [ @@ -41,17 +55,6 @@ ] } ], - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-filesystem-fixture.spec.mjs scripts/react-cockpit/deep-agents-filesystem-configuration.spec.mjs" - } - }, - "e2e": { - "executor": "@nx/playwright:playwright", - "cache": false, - "dependsOn": [ - "build", - "fixture-test" - ], "options": { "config": "cockpit/deep-agents/filesystem/react/e2e/playwright.config.ts", "skipInstall": true diff --git a/cockpit/deep-agents/filesystem/react/src/connection.integration.spec.ts b/cockpit/deep-agents/filesystem/react/src/connection.integration.spec.ts index 255c327a7..97d940b26 100644 --- a/cockpit/deep-agents/filesystem/react/src/connection.integration.spec.ts +++ b/cockpit/deep-agents/filesystem/react/src/connection.integration.spec.ts @@ -1,36 +1,35 @@ -import { - createServer, - type Server, - type IncomingMessage, - type ServerResponse, -} from 'node:http'; +import { createServer, type Server } from 'node:http'; import { mkdtempSync, writeFileSync } from 'node:fs'; -import { afterEach, expect, it, vi } from 'vitest'; +import { resolve } from 'node:path'; +import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest'; import { createFilesystemClient } from './connection'; import { createFilesystemApplication } from './application'; -/* eslint-disable @nx/enforce-module-boundaries -- Local graph proof transport is never bundled. */ -// @ts-expect-error Local proof-only JavaScript has no published declaration. -import { createDeepAgentsFilesystemFixture as createFixture } from '../../../../../scripts/react-cockpit/deep-agents-filesystem-fixture.mjs'; +/* eslint-disable @nx/enforce-module-boundaries -- Local test backend tooling is never bundled. */ +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createAimockBackend } from '../../../../../scripts/react-cockpit/aimock-backend.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createLanggraphProxy } from '../../../../../scripts/react-cockpit/langgraph-proxy.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { reactCockpitConfiguration } from '../../../../../scripts/react-cockpit/configuration.mjs'; /* eslint-enable @nx/enforce-module-boundaries */ -interface LocalFixture { - ( - request: IncomingMessage, - response: ServerResponse, - pathname: string - ): Promise; - close(): Promise; -} -const createDeepAgentsFilesystemFixture = createFixture as () => LocalFixture; + +// The real graph runs under `langgraph dev` on aimock fixtures shared with the +// Angular example (cockpit/deep-agents/filesystem/angular/e2e/fixtures). Each +// scenario is a tagged prompt; the in-process proxy records requests and holds +// or fails the next call (scripts/react-cockpit/langgraph-proxy.mjs). +const { backend: config } = reactCockpitConfiguration('deep-agents-filesystem'); +const backend = createAimockBackend({ root: resolve(__dirname, '../../../../..'), backend: config }); +beforeAll(() => backend.ensure(), 180000); +afterAll(() => backend.close()); +type Handler = (request: unknown, response: unknown, pathname: string) => Promise; let server: Server | undefined, - fixture: ReturnType | undefined, application: ReturnType | undefined; afterEach(async () => { await application?.dispose(); - await fixture?.close(); if (server) - await new Promise((resolve) => { - server?.close(() => resolve()); + await new Promise((done) => { + server?.close(() => done()); server?.closeAllConnections(); }); }); @@ -43,16 +42,12 @@ writeFileSync( ); let evidenceNumber = 0; async function setup() { - const handle = createDeepAgentsFilesystemFixture(); - fixture = handle; + const handle: Handler = createLanggraphProxy({ + upstream: `http://127.0.0.1:${config.langgraphPort}`, + ready: () => backend.ensure(), + }); const http = createServer(async (req, res) => { - if ( - !(await handle( - req, - res, - new URL(req.url ?? '/', 'http://localhost').pathname - )) - ) { + if (!(await handle(req, res, new URL(req.url ?? '/', 'http://localhost').pathname))) { res.writeHead(404); res.end(); } @@ -98,7 +93,6 @@ async function setup() { lifecycle, rawCheckpoints, requests: await (await fetch(base + '/__requests')).json(), - proof: await (await fetch(base + '/__graph-proof')).json(), }, null, 2 @@ -108,8 +102,9 @@ async function setup() { evidenceNumber++; const submit = app.submit, decide = app.decide; - app.submit = async (...args) => { - const result = await submit(...args); + // The scenario tag selects the scripted model turns (shared aimock fixtures). + app.submit = async (text, ...rest) => { + const result = await submit(`${text} [scenario:${scenario}]`, ...rest); await record('submit'); return result; }; @@ -118,18 +113,21 @@ async function setup() { await record('decision'); return result; }; + const control = async (path: string) => + expect((await fetch(base + path, { method: 'POST' })).ok).toBe(true); const configure = async ( nextScenario: string, extra: Record = {} ) => { - const response = await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify({ scenario: nextScenario, ...extra }), - }); - expect(response.ok).toBe(true); + if (extra.failStream) await control('/__fail-stream'); + // The confirmation read of the saved checkpoint. + if (extra.holdCheckpoint) await control('/__hold/state'); scenario = nextScenario; }; - const release = () => fetch(base + '/__release', { method: 'POST' }); + const release = async () => { + await control('/__release-state'); + await control('/__release'); + }; return { app, client, read, base, configure, release }; } @@ -157,18 +155,6 @@ it('mount is inert and native submit pauses with actual note, exact whole batch expect( requests.filter((x) => x.body?.command).map((x) => x.body!.command!) ).toEqual([{ resume: { decisions: [{ type: 'approve' }] } }]); - const proof: { - actualCompiledGraph: boolean; - networkConnectAttempts: number; - op: string; - }[] = await (await fetch(f.base + '/__graph-proof')).json(); - expect(proof.length).toBeGreaterThan(0); - expect( - proof.filter((x) => ['submit', 'resume'].includes(x.op)).map((x) => x.op) - ).toEqual(['submit', 'resume']); - expect( - proof.every((x) => x.actualCompiledGraph && x.networkConnectAttempts === 0) - ).toBe(true); }, 30000); for (const scenario of [ 'batch', diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 16426e29a..df6f2cd48 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -93,6 +93,10 @@ test('Filesystem owns an exact closed native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('deep-agents-filesystem'), { topic: 'filesystem', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/filesystem/react', base: '/deep-agents/filesystem/react/', port: 4629, project: 'cockpit-deep-agents-filesystem-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/deep-agents/filesystem/python', + fixtures: 'cockpit/deep-agents/filesystem/angular/e2e/fixtures', langgraphPort: 5629, + }, }); for (const key of ['filesystem', 'deep-agents/filesystem', '../deep-agents-filesystem', 'deep-agents-filesystem/../memory']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index 5adc58891..ecd37f9da 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -53,17 +53,16 @@ it('wires native Subagents build, fixture and website coverage to the shared Pyt expect(config).toContain('cockpit-deep-agents-subagents-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent'); expect(config).toContain("url: 'http://127.0.0.1:4630'"); }); -it('wires native Filesystem build, fixture and website coverage to the shared Python graph', () => { +it('wires native Filesystem build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); const react = read('cockpit/deep-agents/filesystem/react/project.json'); expect(react.tags).toEqual(expect.arrayContaining(['framework:react', 'scope:cockpit-e2e', 'scope:cockpit-examples'])); expect(react.targets.build.options.command).toBe('node scripts/react-cockpit/build.mjs deep-agents-filesystem'); - expect(react.targets.e2e.dependsOn).toEqual(['build', 'fixture-test']); - expect(react.targets['fixture-test'].dependsOn).toContainEqual({ target: 'test', projects: ['cockpit-deep-agents-filesystem-python'] }); + expect(react.targets.e2e.dependsOn).toEqual(['build', { target: 'test', projects: ['cockpit-deep-agents-filesystem-python'] }]); const website = read('apps/website/project.json'); expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-filesystem-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); - expect(config).toContain('cockpit-deep-agents-filesystem-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent'); + expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4629'"); }); import { basename, dirname, join, relative, resolve } from 'node:path'; diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index a84613bd9..1e22e03f1 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -75,7 +75,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { if (topic === 'deep-agents-subagents') return Object.freeze({topic:'subagents',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/subagents/react',base:'/deep-agents/subagents/react/',port:4630,project:'cockpit-deep-agents-subagents-react'}); if (topic === 'deep-agents-filesystem') - return Object.freeze({topic:'filesystem',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/filesystem/react',base:'/deep-agents/filesystem/react/',port:4629,project:'cockpit-deep-agents-filesystem-react'}); + return Object.freeze({topic:'filesystem',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/filesystem/react',base:'/deep-agents/filesystem/react/',port:4629,project:'cockpit-deep-agents-filesystem-react',backend:aimockLanggraph('deep-agents/filesystem', 4629)}); if (topic === 'deep-agents-planning') return Object.freeze({ topic: 'planning', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/planning/react', diff --git a/scripts/react-cockpit/deep-agents-filesystem-configuration.spec.mjs b/scripts/react-cockpit/deep-agents-filesystem-configuration.spec.mjs deleted file mode 100644 index 8499fd9da..000000000 --- a/scripts/react-cockpit/deep-agents-filesystem-configuration.spec.mjs +++ /dev/null @@ -1,21 +0,0 @@ -import assert from 'node:assert/strict'; -import { test } from 'node:test'; -import { readFileSync } from 'node:fs'; -import { reactCockpitConfiguration } from './configuration.mjs'; -test('Filesystem has a closed native React selection and fixture dispatch precedes generic fallback', () => { - assert.deepEqual(reactCockpitConfiguration('deep-agents-filesystem'), { - topic: 'filesystem', - library: 'deep-agents', - adapter: 'langgraph', - appPath: 'cockpit/deep-agents/filesystem/react', - base: '/deep-agents/filesystem/react/', - port: 4629, - project: 'cockpit-deep-agents-filesystem-react', - }); - assert.throws(() => reactCockpitConfiguration('deep-agents/../../foreign')); - const serve = readFileSync(new URL('./serve.mjs', import.meta.url), 'utf8'); - assert.ok( - serve.indexOf('await deepAgentsFilesystemFixture(request') < - serve.indexOf("if (pathname === '/__reset')") - ); -}); diff --git a/scripts/react-cockpit/deep-agents-filesystem-fixture.mjs b/scripts/react-cockpit/deep-agents-filesystem-fixture.mjs deleted file mode 100644 index e9f93b221..000000000 --- a/scripts/react-cockpit/deep-agents-filesystem-fixture.mjs +++ /dev/null @@ -1,557 +0,0 @@ -/** Loopback-only SDK transport backed by the actual compiled Filesystem graph. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { readFileSync } from 'node:fs'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, timeout) { - const child = spawnWorker(); - const pending = new Map(); - let sequence = 0, - dead = false, - closing; - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const item of pending.values()) { - clearTimeout(item.timer); - item.reject(Error('Filesystem graph worker unavailable.')); - } - pending.clear(); - } - const terminate = () => { - fail(); - child.kill('SIGTERM'); - }; - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', terminate); - lines.once('close', () => { - if (!dead) terminate(); - }); - lines.on('line', (line) => { - try { - if (Buffer.byteLength(line) > 16 * 1024 * 1024) - throw Error('Oversized response'); - const result = JSON.parse(line), - item = pending.get(result.requestId); - if (!item) throw Error('Unowned response'); - pending.delete(result.requestId); - clearTimeout(item.timer); - if (result.error) - item.reject(Error('Filesystem graph operation failed.')); - else item.resolve(result); - } catch { - terminate(); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(Error('Filesystem graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const wire = JSON.stringify({ ...value, requestId }) + '\n'; - if (Buffer.byteLength(wire) > 1024 * 1024) { - reject(Error('Oversized request')); - return; - } - const timer = setTimeout(terminate, timeout); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(wire); - }); - }, - close() { - return (closing ??= (async () => { - fail(); - child.stdin.end(); - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })()); - }, - }; -} - -export function createDeepAgentsFilesystemFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/filesystem/python', - 'python', - 'scripts/react-cockpit/deep-agents-filesystem-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-filesystem-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const sourceSha256 = Object.fromEntries( - ['src/graph.py', 'prompts/filesystem.md', 'uv.lock'].map((path) => [ - path, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/deep-agents/filesystem/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - closed = false, - closing, - queue = Promise.resolve(); - // Real execution is buffered before delivery; transport cancellation does not roll back files. - const threads = new Set(), - runs = new Map(), - held = new Set(), - proofs = [], - requests = []; - const options = { - scenario: 'normal', - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - }; - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value), - proof = result.proof; - if ( - closed || - proof?.actualCompiledGraph !== true || - proof.networkConnectAttempts !== 0 || - proof.op !== value.op || - proof.threadId !== (value.threadId ?? null) || - Object.keys(proof.sourceSha256 ?? {}).length !== - Object.keys(sourceSha256).length || - Object.entries(sourceSha256).some( - ([path, hash]) => proof.sourceSha256[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - proofs.push(proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function hold(response, finish, run) { - const item = { response, finish, run }; - held.add(item); - response.once('close', () => { - if (held.delete(item) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - const chunks = []; - let byteLength = 0; - for await (const chunk of request) { - const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); - byteLength += bytes.length; - if (byteLength > 1024 * 1024) { - json(response, {}, 413); - return true; - } - chunks.push(bytes); - } - const input = Buffer.concat(chunks, byteLength).toString('utf8'); - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - if (request.method !== 'POST') { - json(response, {}, 405); - return true; - } - if ( - !only(body, [ - 'scenario', - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ]) || - ('scenario' in body && - ![ - 'normal', - 'no-files', - 'unchanged', - 'read-only', - 'empty', - 'overwrite', - 'report-overwrite', - 'edit', - 'delete', - 'batch', - 'duplicates', - 'reject-reproposal', - 'write-error', - 'protected-edit', - 'mixed', - ].includes(body.scenario)) || - [ - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ].some((k) => k in body && typeof body[k] !== 'boolean') - ) { - json(response, {}, 400); - return true; - } - if ('scenario' in body) - await operate({ op: 'configure', scenario: body.scenario }); - Object.assign(options, body); - json(response, {}); - return true; - } - if (pathname === '/__release') { - if (request.method !== 'POST' || !only(body, [])) { - json(response, {}, 400); - return true; - } - for (const item of held) { - held.delete(item); - item.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - if (requests.length >= 2000 || threads.size >= 100 || runs.size >= 1000) { - json(response, {}, 429); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if (options.failCreation) { - options.failCreation = false; - json(response, { error: 'Controlled creation failure.' }, 500); - return true; - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - !only(body, ['thread_id', 'if_exists', 'metadata']) || - (body.if_exists !== undefined && body.if_exists !== 'raise') || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - await operate({ op: 'create', threadId: body.thread_id }); - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if (suffix === '/state' && input.length !== 0) { - json(response, {}, 400); - return true; - } - if (suffix === '/state/checkpoint') { - if (options.failCheckpoint) { - options.failCheckpoint = false; - json(response, { error: 'Controlled exact read failure.' }, 500); - return true; - } - if ( - !only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - body.checkpoint.thread_id !== threadId || - typeof body.checkpoint.checkpoint_id !== 'string' || - typeof body.checkpoint.checkpoint_ns !== 'string' - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - if ( - !history.some( - (s) => - s.checkpoint.checkpoint_id === body.checkpoint.checkpoint_id && - s.checkpoint.checkpoint_ns === body.checkpoint.checkpoint_ns - ) || - (body.checkpoint.checkpoint_map !== undefined && - (!only(body.checkpoint.checkpoint_map, [ - body.checkpoint.checkpoint_ns, - ]) || - Object.values(body.checkpoint.checkpoint_map).some( - (id) => id !== body.checkpoint.checkpoint_id - ))) - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - ...(suffix === '/state/checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }); - const finish = () => json(response, state); - if ( - options.holdState || - (options.holdCheckpoint && suffix === '/state/checkpoint') - ) - hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit']) || - (body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100)) - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - json(response, history.slice(0, body.limit ?? 10)); - return true; - } - const runRoute = /^\/runs\/([^/]+)(\/cancel)?$/.exec(suffix); - if ( - runRoute && - (request.method === 'GET' || (runRoute[2] && request.method === 'POST')) - ) { - const run = runs.get(runRoute[1]); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - if (runRoute[2]) { - const search = new URL(request.url, 'http://localhost').searchParams; - if (search.get('action') !== 'interrupt') { - json(response, {}, 400); - return true; - } - for (const item of held) - if (item.run === run) { - held.delete(item); - run.status = 'interrupted'; - item.response.end(); - } - json(response, {}); - } else json(response, run); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - if (options.failStream) { - options.failStream = false; - json(response, { error: 'Controlled stream failure.' }, 500); - return true; - } - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'command', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !( - (body.command !== undefined && - (body.input === undefined || body.input === null) && - only(body.command, ['resume']) && - only(body.command.resume, ['decisions']) && - Array.isArray(body.command.resume.decisions) && - body.command.resume.decisions.length > 0 && - body.command.resume.decisions.every( - (d) => - only(d, ['type']) && ['approve', 'reject'].includes(d.type) - )) || - (body.command === undefined && - only(body.input, ['messages']) && - body.input.messages?.length === 1 && - only(message, ['type', 'id', 'content']) && - message.type === 'human' && - typeof message.id === 'string' && - message.id.trim() && - typeof message.content === 'string') - ) || - body.assistant_id !== 'da-filesystem' || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'checkpoints', - 'custom', - ].includes(mode) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: body.command ? 'resume' : 'submit', - threadId, - ...(body.command - ? { command: body.command } - : { messages: body.input.messages }), - }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const finish = () => { - if (response.destroyed || closed) return; - for (const [kind, data] of wire.events) - if (modes.has(kind)) response.write(event(kind, data)); - run.status = 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Filesystem graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const item of held) item.response.end(); - held.clear(); - await worker?.close(); - })()); - return handle; -} diff --git a/scripts/react-cockpit/deep-agents-filesystem-fixture.spec.mjs b/scripts/react-cockpit/deep-agents-filesystem-fixture.spec.mjs deleted file mode 100644 index c57d5aa1a..000000000 --- a/scripts/react-cockpit/deep-agents-filesystem-fixture.spec.mjs +++ /dev/null @@ -1,771 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer, request } from 'node:http'; -import { createInterface } from 'node:readline'; -import { fileURLToPath } from 'node:url'; -import { once } from 'node:events'; -import { randomUUID, createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { spawn } from 'node:child_process'; -import { test } from 'node:test'; -import { Client } from '@langchain/langgraph-sdk'; - -const { createDeepAgentsFilesystemFixture } = await import( - './deep-agents-filesystem-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createDeepAgentsFilesystemFixture, - 'function', - 'Filesystem compiled-graph fixture exists' - ); - const handle = createDeepAgentsFilesystemFixture(options); - const server = createServer(async (req, res) => { - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const client = new Client({ - apiUrl: base + '/api', - callerOptions: { maxRetries: 0 }, - }); - const thread = randomUUID(); - await client.threads.create({ threadId: thread, ifExists: 'raise' }); - const control = async (body) => { - const response = await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify(body), - }); - assert.equal(response.status, 200); - }; - const stream = ( - content = 'Plan the KSFO to KASE flight.', - id = randomUUID() - ) => - client.runs.stream(thread, 'da-filesystem', { - input: { messages: [{ type: 'human', id, content }] }, - streamMode: ['messages-tuple', 'values', 'updates', 'checkpoints'], - streamSubgraphs: true, - streamResumable: true, - onDisconnect: 'continue', - }); - const submit = async (content, id = randomUUID()) => { - const events = []; - for await (const event of stream(content, id)) events.push(event); - return { - events, - id, - run: events.find((e) => e.event === 'metadata').data.run_id, - }; - }; - return { handle, base, client, thread, control, stream, submit }; -} - -function toolResults(state, ids) { - return state.values.messages.filter( - (m) => m.type === 'tool' && ids.includes(m.tool_call_id) - ); -} - -test('installed SDK preserves actual pause, exact checkpoint, resume body and terminal files', async (t) => { - const f = await fixture(t); - const pausedRun = await f.submit(); - const paused = await f.client.threads.getState(f.thread); - assert.equal( - paused.values.files['/notes/kase.txt'].content, - 'KASE field elevation is 7820 ft.' - ); - assert.equal(Object.hasOwn(paused.values.files, '/reports/kase.md'), false); - assert.equal(paused.tasks.flatMap((t) => t.interrupts).length, 1); - const batch = paused.tasks.flatMap((t) => t.interrupts)[0].value; - assert.equal(batch.action_requests.length, 1); - assert.equal(batch.review_configs.length, 1); - assert.equal( - (await f.client.runs.get(f.thread, pausedRun.run)).status, - 'success' - ); - assert.deepEqual( - await f.client.threads.getState(f.thread, paused.checkpoint), - paused - ); - assert.equal( - (await f.client.threads.getHistory(f.thread, { limit: 100 }))[0].checkpoint - .checkpoint_id, - paused.checkpoint.checkpoint_id - ); - const events = []; - for await (const e of f.client.runs.stream(f.thread, 'da-filesystem', { - command: { resume: { decisions: [{ type: 'approve' }] } }, - streamMode: ['values', 'messages-tuple', 'updates', 'checkpoints'], - })) - events.push(e); - const terminal = await f.client.threads.getState(f.thread); - assert.equal( - terminal.values.files['/reports/kase.md'].content, - 'KASE assessment ready.' - ); - assert.deepEqual(terminal.next, []); - assert.deepEqual(terminal.tasks, []); - const requests = await (await fetch(f.base + '/__requests')).json(); - assert.deepEqual(requests.find((r) => r.body.command)?.body.command, { - resume: { decisions: [{ type: 'approve' }] }, - }); - assert.deepEqual( - requests.find((r) => r.path.endsWith('/state/checkpoint')).body, - { checkpoint: paused.checkpoint } - ); - const proofs = await (await fetch(f.base + '/__graph-proof')).json(); - assert( - proofs.every((p) => p.actualCompiledGraph && p.networkConnectAttempts === 0) - ); -}); - -test('reject, replay and mismatched decisions cannot optimistically write', async (t) => { - const f = await fixture(t); - await f.submit(); - const resume = async (decisions) => { - for await (const e of f.client.runs.stream(f.thread, 'da-filesystem', { - command: { resume: { decisions } }, - streamMode: ['values'], - })) { - } - }; - await assert.rejects(resume([])); - await resume([{ type: 'reject' }]); - assert.equal( - Object.hasOwn( - (await f.client.threads.getState(f.thread)).values.files, - '/reports/kase.md' - ), - false - ); - await assert.rejects(resume([{ type: 'approve' }])); - await assert.rejects(f.client.threads.getState(randomUUID())); -}); - -async function decide(f, types) { - const events = []; - for await (const e of f.client.runs.stream(f.thread, 'da-filesystem', { - command: { resume: { decisions: types.map((type) => ({ type })) } }, - streamMode: ['values', 'messages-tuple', 'updates', 'checkpoints'], - })) - events.push(e); - return events; -} -for (const scenario of [ - 'batch', - 'duplicates', - 'reject-reproposal', - 'delete', - 'overwrite', - 'edit', - 'write-error', - 'empty', - 'no-files', - 'read-only', - 'unchanged', -]) { - test(`actual SDK scenario ${scenario}`, async (t) => { - const f = await fixture(t); - await f.control({ scenario }); - await f.submit(); - let saved = await f.client.threads.getState(f.thread); - if (['batch', 'duplicates'].includes(scenario)) { - const b = saved.tasks.flatMap((t) => t.interrupts)[0].value; - assert.equal(b.action_requests.length, 2); - assert.equal(b.review_configs.length, 2); - if (scenario === 'duplicates') - assert.equal( - b.action_requests[0].args.file_path, - b.action_requests[1].args.file_path - ); - await assert.rejects(decide(f, ['approve'])); - await decide(f, ['approve', 'approve']); - saved = await f.client.threads.getState(f.thread); - assert(Object.hasOwn(saved.values.files, '/reports/kase.md')); - } else if (scenario === 'reject-reproposal') { - await decide(f, ['reject']); - saved = await f.client.threads.getState(f.thread); - assert.equal( - Object.hasOwn(saved.values.files, '/reports/kase.md'), - false - ); - assert.equal( - saved.tasks.flatMap((t) => t.interrupts)[0].value.action_requests[0] - .args.content, - 'Revised assessment' - ); - await decide(f, ['approve']); - saved = await f.client.threads.getState(f.thread); - assert.equal( - saved.values.files['/reports/kase.md'].content, - 'Revised assessment' - ); - } else if (scenario === 'delete') { - await decide(f, ['approve']); - saved = await f.client.threads.getState(f.thread); - assert.equal( - saved.tasks.flatMap((t) => t.interrupts)[0].value.action_requests[0] - .name, - 'delete' - ); - await decide(f, ['approve']); - saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.files, {}); - } else if (scenario === 'overwrite') - assert.equal( - saved.values.files['/notes/kase.txt'].content, - 'Replacement' - ); - else if (scenario === 'edit') - assert.equal( - saved.values.files['/notes/kase.txt'].content, - 'Aspen field elevation is 7820 ft.' - ); - else if (scenario === 'empty') - assert.equal(saved.values.files['/notes/empty.txt'].content, ''); - else if (scenario === 'write-error') { - assert.deepEqual(saved.values.files, {}); - assert( - saved.values.messages.some( - (m) => m.type === 'tool' && m.status === 'error' - ) - ); - } else assert.deepEqual(saved.values.files ?? {}, {}); - assert.deepEqual(saved.next, []); - assert.deepEqual(saved.tasks, []); - }); -} - -test('cancellation stops held delivery without rollback; cleanup closes owned child', async (t) => { - const f = await fixture(t); - await f.control({ holdStream: true }); - const stream = f.stream(); - const metadata = await stream.next(); - const run = metadata.value.data.run_id; - await f.client.runs.cancel(f.thread, run, true, 'interrupt'); - assert.equal((await stream.next()).done, true); - assert.equal((await f.client.runs.get(f.thread, run)).status, 'interrupted'); - assert.equal( - (await f.client.threads.getState(f.thread)).values.files['/notes/kase.txt'] - .content, - 'KASE field elevation is 7820 ft.' - ); - const life = await (await fetch(f.base + '/__lifetime')).json(); - assert.equal(life.held, 0); - await f.handle.close(); - assert.throws(() => process.kill(life.workerPid, 0), { code: 'ESRCH' }); -}); - -test('owned exact checkpoint and stream bodies reject foreign and unsupported inputs', async (t) => { - const f = await fixture(t); - await f.submit(); - const saved = await f.client.threads.getState(f.thread); - await assert.rejects( - f.client.threads.getState(f.thread, { - ...saved.checkpoint, - thread_id: randomUUID(), - }) - ); - await assert.rejects( - f.client.threads.getState(f.thread, { - ...saved.checkpoint, - checkpoint_id: randomUUID(), - }) - ); - await assert.rejects(async () => { - for await (const e of f.client.runs.stream(f.thread, 'da-filesystem', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'Override' }], - files: {}, - }, - streamMode: ['values'], - })) { - } - }); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values, - saved.values - ); -}); -test('current-state GET rejects foreign checkpoint bodies between owned threads', async (t) => { - const f = await fixture(t); - const other = randomUUID(); - await f.client.threads.create({ threadId: other, ifExists: 'raise' }); - await f.submit(); - for await (const unused of f.client.runs.stream(other, 'da-filesystem', { - input: { - messages: [ - { type: 'human', id: 'other-human', content: 'Other thread.' }, - ], - }, - streamMode: ['values'], - })) { - /* Consume the actual other thread graph. */ - } - const foreign = await f.client.threads.getState(other); - const current = await f.client.threads.getState(f.thread); - const body = JSON.stringify({ checkpoint: foreign.checkpoint }); - const status = await new Promise((resolve, reject) => { - const req = request( - f.base + `/api/threads/${f.thread}/state`, - { - method: 'GET', - headers: { - 'content-type': 'application/json', - 'content-length': Buffer.byteLength(body), - }, - }, - (res) => { - res.resume(); - res.once('end', () => resolve(res.statusCode)); - } - ); - req.once('error', reject); - req.end(body); - }); - assert.equal( - status, - 400, - 'current-state GET must reject a checkpoint request body' - ); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values, - current.values - ); -}); - -test('two paused owners retain independent deterministic responses across resumes', async (t) => { - const f = await fixture(t); - await f.control({ scenario: 'reject-reproposal' }); - const a = await f.submit('A', 'human-a'); - await f.control({ scenario: 'normal' }); - const b = randomUUID(); - await f.client.threads.create({ threadId: b, ifExists: 'raise' }); - for await (const e of f.client.runs.stream(b, 'da-filesystem', { - input: { messages: [{ type: 'human', id: 'human-b', content: 'B' }] }, - streamMode: ['values'], - })) { - } - await decide(f, ['reject']); - let state = await f.client.threads.getState(f.thread); - assert.equal( - state.tasks.flatMap((t) => t.interrupts)[0].value.action_requests[0].args - .content, - 'Revised assessment' - ); - assert.equal(state.values.messages.at(-1).id, 'human-a-model-2'); - await decide(f, ['approve']); - state = await f.client.threads.getState(f.thread); - assert.equal(state.values.messages.at(-1).id, 'human-a-answer'); - assert.equal( - (await f.client.threads.getState(b)).values.messages.at(-1).id, - 'human-b-model-1' - ); - const proofs = await (await fetch(f.base + '/__graph-proof')).json(); - assert( - proofs - .filter((p) => p.op === 'resume' && p.threadId === f.thread) - .every((p) => p.scenario === 'reject-reproposal') - ); -}); - -test('private controls validate bodies, release held state and fail creation/stream/checkpoint once', async (t) => { - const f = await fixture(t); - assert.equal( - ( - await fetch(f.base + '/__configure', { - method: 'POST', - body: JSON.stringify({ scenario: 'synthetic-malformed' }), - }) - ).status, - 400 - ); - await f.control({ failCreation: true }); - await assert.rejects( - f.client.threads.create({ threadId: randomUUID(), ifExists: 'raise' }) - ); - await f.control({ failStream: true }); - await assert.rejects(f.submit()); - await f.submit(); - const state = await f.client.threads.getState(f.thread); - await f.control({ failCheckpoint: true }); - await assert.rejects(f.client.threads.getState(f.thread, state.checkpoint)); - assert.deepEqual( - await f.client.threads.getState(f.thread, state.checkpoint), - state - ); - await f.control({ holdState: true }); - const held = f.client.threads.getState(f.thread); - // Poll private owned lifetime, bounded by wall time, without relying on arbitrary sleep. - const deadline = Date.now() + 1000; - let life; - do { - life = await (await fetch(f.base + '/__lifetime')).json(); - } while (life.held === 0 && Date.now() < deadline); - assert.equal(life.held, 1); - await fetch(f.base + '/__release', { method: 'POST' }); - assert.deepEqual(await held, state); -}); -for (const [name, source] of [ - ['timeout', 'process.stdin.resume(); setInterval(() => {}, 1000);'], - [ - 'malformed output', - 'process.stdin.once("data", () => { process.stdout.write("not-json\\n"); }); setInterval(() => {}, 1000);', - ], - [ - 'unowned response', - 'process.stdin.once("data", () => { process.stdout.write(JSON.stringify({requestId: 999}) + "\\n"); }); setInterval(() => {}, 1000);', - ], - [ - 'invalid graph proof', - 'process.stdin.once("data", data => { const {requestId} = JSON.parse(data); process.stdout.write(JSON.stringify({requestId, proof: {actualCompiledGraph: false}}) + "\\n"); }); setInterval(() => {}, 1000);', - ], -]) { - test(`worker ${name} fails closed and cleanup reaps the child`, async (t) => { - let child; - const handle = createDeepAgentsFilesystemFixture({ - workerTimeoutMs: 1000, - spawnWorker: () => - (child = spawn(process.execPath, ['-e', source], { - stdio: ['pipe', 'pipe', 'pipe'], - })), - }); - const server = createServer((req, res) => - handle(req, res, new URL(req.url, 'http://localhost').pathname) - ); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const client = new Client({ - apiUrl: `http://127.0.0.1:${server.address().port}/api`, - callerOptions: { maxRetries: 0 }, - }); - await assert.rejects( - client.threads.create({ threadId: randomUUID(), ifExists: 'raise' }) - ); - await handle.close(); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - }); -} - -test( - 'stdio worker independently rejects unknown owners, replay, foreign checkpoint and invalid resume', - { timeout: 30000 }, - async (t) => { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/filesystem/python', - 'python', - 'scripts/react-cockpit/deep-agents-filesystem-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ); - child.stderr.resume(); - const lines = createInterface({ input: child.stdout }); - t.after(async () => { - if (child.exitCode === null && child.signalCode === null) { - const ended = once(child, 'close'); - child.kill('SIGKILL'); - await ended; - } - lines.close(); - }); - let sequence = 0; - const send = async (value) => { - const line = once(lines, 'line'); - child.stdin.write( - JSON.stringify({ ...value, requestId: ++sequence }) + '\n' - ); - return JSON.parse((await line)[0]); - }; - assert.equal( - (await send({ op: 'state', threadId: 'unknown' })).error, - 'Graph operation failed' - ); - await send({ op: 'create', threadId: 'a' }); - await send({ op: 'create', threadId: 'b' }); - const input = { - op: 'submit', - threadId: 'a', - messages: [{ type: 'human', id: 'human-a', content: 'A' }], - }; - const paused = await send(input); - assert.equal(paused.state.tasks.flatMap((t) => t.interrupts).length, 1); - assert.equal( - ( - await send({ - ...input, - messages: [ - { type: 'human', id: 'override', content: 'Override pause' }, - ], - }) - ).error, - 'Graph operation failed' - ); - assert.equal( - ( - await send({ - op: 'state', - threadId: 'b', - checkpoint: paused.state.checkpoint, - }) - ).error, - 'Graph operation failed' - ); - assert.equal( - ( - await send({ - op: 'state', - threadId: 'a', - checkpoint: { thread_id: 'a' }, - }) - ).error, - 'Graph operation failed' - ); - assert.equal( - ( - await send({ - op: 'resume', - threadId: 'a', - checkpoint: paused.state.checkpoint, - command: { resume: { decisions: [{ type: 'approve' }] } }, - }) - ).error, - 'Graph operation failed' - ); - for (const checkpoint of [ - { ...paused.state.checkpoint, checkpoint_map: { foreign: 'unknown' } }, - { ...paused.state.checkpoint, checkpoint_map: [] }, - { thread_id: 'a', checkpoint_id: paused.state.checkpoint.checkpoint_id }, - ]) { - assert.equal( - (await send({ op: 'state', threadId: 'a', checkpoint })).error, - 'Graph operation failed' - ); - } - const invalidIdLine = once(lines, 'line'); - child.stdin.write( - JSON.stringify({ op: 'state', threadId: 'a', requestId: true }) + '\n' - ); - assert.equal( - JSON.parse((await invalidIdLine)[0]).error, - 'Graph operation failed' - ); - for (const decisions of [ - [], - [{ type: 'edit' }], - [{ type: 'approve' }, { type: 'approve' }], - ]) - assert.equal( - ( - await send({ - op: 'resume', - threadId: 'a', - command: { resume: { decisions } }, - }) - ).error, - 'Graph operation failed' - ); - const terminal = await send({ - op: 'resume', - threadId: 'a', - command: { resume: { decisions: [{ type: 'reject' }] } }, - }); - assert.deepEqual(terminal.state.next, []); - assert.equal((await send(input)).error, 'Graph operation failed'); - assert.equal( - ( - await send({ - op: 'resume', - threadId: 'a', - command: { resume: { decisions: [{ type: 'approve' }] } }, - }) - ).error, - 'Graph operation failed' - ); - assert.equal( - ( - await send({ - op: 'submit', - threadId: 'b', - messages: [ - { type: 'human', id: 'wrong-type', content: 'B', extra: 'no' }, - ], - }) - ).error, - 'Graph operation failed' - ); - } -); - -for (const choice of ['approve', 'reject']) { - test(`protected overwrite ${choice} binds current proposal and old saved text`, async (t) => { - const f = await fixture(t); - await f.control({ scenario: 'report-overwrite' }); - await f.submit(); - await decide(f, ['approve']); - const paused = await f.client.threads.getState(f.thread); - assert.equal( - paused.values.files['/reports/existing.md'].content, - 'Prior report' - ); - assert.deepEqual( - await f.client.threads.getState(f.thread, paused.checkpoint), - paused - ); - const batch = paused.tasks.flatMap((t) => t.interrupts)[0].value; - assert.equal(batch.action_requests.length, 1); - assert.equal(batch.action_requests[0].name, 'write_file'); - assert.deepEqual(batch.action_requests[0].args, { - file_path: '/reports/existing.md', - content: 'Replacement report', - }); - await decide(f, [choice]); - const terminal = await f.client.threads.getState(f.thread); - assert.equal( - terminal.values.files['/reports/existing.md'].content, - choice === 'approve' ? 'Replacement report' : 'Prior report' - ); - const result = terminal.values.messages.find( - (m) => m.type === 'tool' && m.tool_call_id.endsWith('-replacement') - ); - assert.equal(result.status, choice === 'approve' ? 'success' : 'error'); - assert.deepEqual(terminal.next, []); - assert.deepEqual(terminal.tasks, []); - }); -} - -async function fragmentedHandle(f, pathname, bytes, chunkSize = 1) { - const req = { - socket: { remoteAddress: '127.0.0.1' }, - method: 'POST', - url: pathname, - async *[Symbol.asyncIterator]() { - let i = 0; - for (; i < Math.min(32, bytes.length); i++) - yield bytes.subarray(i, i + 1); - for (; i < bytes.length; i += chunkSize) - yield bytes.subarray(i, i + chunkSize); - }, - }; - let status, - output = ''; - const res = { - destroyed: false, - writableEnded: false, - writeHead(value) { - status = value; - }, - write(value) { - output += value; - }, - end(value = '') { - output += value; - this.writableEnded = true; - }, - }; - assert.equal(await f.handle(req, res, pathname), true); - return { status, output }; -} - -test('actual handler preserves UTF8 JSON and human text across arbitrary buffer boundaries', async (t) => { - const f = await fixture(t); - await f.control({ scenario: 'no-files' }); - const human = { type: 'human', id: 'split-utf8-human', content: 'Café ✈️' }; - await f.submit(human.content, 'ordinary-utf8-human'); - assert.equal( - (await f.client.threads.getState(f.thread)).values.messages[0].content, - human.content - ); - const body = { - assistant_id: 'da-filesystem', - input: { messages: [human] }, - stream_mode: ['values'], - }; - const result = await fragmentedHandle( - f, - `/api/threads/${f.thread}/runs/stream`, - Buffer.from(JSON.stringify(body)) - ); - assert.equal(result.status, 200); - const requests = await (await fetch(f.base + '/__requests')).json(); - assert.deepEqual(requests.at(-1).body, body); - const saved = await f.client.threads.getState(f.thread); - assert.equal( - saved.values.messages.find((m) => m.id === human.id).content, - human.content - ); -}); - -test('raw request byte limit accepts exactly1MiB and rejects one additional byte before decoding', async (t) => { - const f = await fixture(t); - // JSON whitespace remains valid while testing exact byte counting with a split multibyte key. - const prefix = Buffer.from('{"é":true}'); - const exact = Buffer.concat([ - prefix, - Buffer.alloc(1024 * 1024 - prefix.length, 0x20), - ]); - const pathname = '/api/threads'; - const accepted = await fragmentedHandle(f, pathname, exact, 65536); - assert.equal( - accepted.status, - 400, - 'valid exact-limit body reaches route validation' - ); - const requests = await (await fetch(f.base + '/__requests')).json(); - assert.deepEqual(requests.at(-1).body, { é: true }); - const rejected = await fragmentedHandle( - f, - pathname, - Buffer.concat([exact, Buffer.from(' ')]), - 65536 - ); - assert.equal(rejected.status, 413); - assert.deepEqual( - await (await fetch(f.base + '/__requests')).json(), - requests, - 'oversized body never reaches route dispatch' - ); -}); - -test('native resume accepts the installed SDK null input with a command and no new human', async (t) => { - const f = await fixture(t); - await f.submit('Native null resume'); - const before = await f.client.threads.getState(f.thread); - const events = []; - for await (const item of f.client.runs.stream(f.thread, 'da-filesystem', { - input: null, - command: { resume: { decisions: [{ type: 'approve' }] } }, - streamMode: ['values', 'updates'], - })) events.push(item); - const after = await f.client.threads.getState(f.thread); - assert.equal(after.next.length, 0); - assert.equal(after.values.files['/reports/kase.md'].content, 'KASE assessment ready.'); - assert.deepEqual(after.values.messages.filter(m => m.type === 'human'), before.values.messages.filter(m => m.type === 'human')); - assert.ok(events.length); -}); diff --git a/scripts/react-cockpit/deep-agents-filesystem-wire.py b/scripts/react-cockpit/deep-agents-filesystem-wire.py deleted file mode 100644 index 7c9e108ab..000000000 --- a/scripts/react-cockpit/deep-agents-filesystem-wire.py +++ /dev/null @@ -1,211 +0,0 @@ -"""Bounded newline-JSON worker running the unchanged compiled Filesystem builder.""" -import asyncio -from dataclasses import asdict, is_dataclass -from contextlib import redirect_stdout -from copy import deepcopy -import hashlib -import json -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/deep-agents/filesystem/python" -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel, build, call, responses, write - -SOURCES = ("src/graph.py", "prompts/filesystem.md", "uv.lock") -MAX_REQUEST = 1024 * 1024 -MAX_RESPONSE = 16 * 1024 * 1024 - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if is_dataclass(value): - return asdict(value) - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() - if k in ("thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map")}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": t.id, "name": t.name, - **({"error": t.error} if t.error is not None else {}), - **({"state": t.state} if t.state is not None else {}), - "interrupts": [{"id": item.id, "value": item.value} for item in t.interrupts]} - for t in state.tasks], - } - - -SCENARIOS = ("normal", "no-files", "unchanged", "read-only", "empty", "overwrite", "report-overwrite", "edit", "delete", "batch", "duplicates", "reject-reproposal", "write-error", "protected-edit", "mixed") - -def batches(scenario, identity): - w = lambda path, text, suffix: write(path, text, identity + "-" + suffix) - note = w("/notes/kase.txt", "KASE field elevation is 7820 ft.", "note") - report = w("/reports/kase.md", "KASE assessment ready.", "report") - if scenario in ("no-files", "unchanged"): - return [] - if scenario == "read-only": - return [[call("ls", {"path":"/"}, identity+"-ls")]] - if scenario == "empty": - return [[w("/notes/empty.txt", "", "empty")]] - if scenario == "write-error": - return [[call("write_file", {"file_path":"/notes/invalid.txt", "content":42}, identity+"-invalid")]] - if scenario == "mixed": - return [[note, report, call("read_file", {"file_path":"/notes/kase.txt"}, identity+"-read")]] - if scenario == "protected-edit": - return [[w("/reports/existing.md", "old old", "initial")], [call("edit_file", {"file_path":"/reports/existing.md", "old_string":"old", "new_string":"new", "replace_all":True}, identity+"-edit")]] - if scenario == "report-overwrite": - return [[w("/reports/existing.md", "Prior report", "initial")], - [w("/reports/existing.md", "Replacement report", "replacement")]] - if scenario == "overwrite": - return [[note], [w("/notes/kase.txt", "Replacement", "overwrite")]] - if scenario == "edit": - return [[note], [call("edit_file", {"file_path":"/notes/kase.txt", "old_string":"KASE", "new_string":"Aspen", "replace_all":True}, identity+"-edit")]] - if scenario == "delete": - return [[report], [call("delete", {"file_path":"/reports"}, identity+"-delete")]] - if scenario in ("batch", "duplicates"): - path = "/reports/kase.md" if scenario == "duplicates" else "/reports/second.md" - return [[note], [report, w(path, "Second report", "second")]] - if scenario == "reject-reproposal": - return [[note], [report], [w("/reports/kase.md", "Revised assessment", "reproposal")]] - return [[note], [report]] - - -async def main(): - model = LocalModel() - scenario = "normal" - owners = set() - seen = set() - scripts = {} - request_ids = set() - provenance = {} - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as network: - with redirect_stdout(sys.stderr): - graph = build(model) - while line := sys.stdin.buffer.readline(MAX_REQUEST + 1): - request = {} - try: - if len(line) > MAX_REQUEST or not line.endswith(b"\n"): - raise ValueError("Oversized request") - request = json.loads(line) - if not isinstance(request, dict) or set(request) - {"requestId", "op", "threadId", "scenario", "checkpoint", "messages", "command"}: - raise ValueError("Malformed request") - if type(request.get("requestId")) is not int or request["requestId"] <= 0 or request["requestId"] in request_ids or len(request_ids) >= 2000: - raise ValueError("Invalid/replayed request id or lifetime limit") - request_ids.add(request["requestId"]) - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - op = request["op"] - fields = {"create": {"threadId"}, "configure": {"scenario"}, "state": {"threadId", "checkpoint"}, - "history": {"threadId"}, "submit": {"threadId", "messages"}, "resume": {"threadId", "command"}} - if op not in fields or set(request) - ({"requestId", "op"} | fields[op]): - raise ValueError("Unsupported operation fields") - if op == "create": - if not isinstance(request.get("threadId"), str) or not request["threadId"] or request["threadId"] in owners or len(owners) >= 100: - raise ValueError("Invalid or duplicate owner") - owners.add(request["threadId"]) - elif op != "configure" and request.get("threadId") not in owners: - raise ValueError("Unknown owner") - if "checkpoint" in request: - requested_checkpoint = request["checkpoint"] - if (not isinstance(requested_checkpoint, dict) - or requested_checkpoint.get("thread_id") != config["configurable"]["thread_id"] - or set(requested_checkpoint) - {"thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map"} - or not isinstance(requested_checkpoint.get("checkpoint_id"), str) - or not requested_checkpoint["checkpoint_id"] - or not isinstance(requested_checkpoint.get("checkpoint_ns"), str)): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - if "checkpoint_map" in requested_checkpoint and ( - not isinstance(requested_checkpoint["checkpoint_map"], dict) - or set(requested_checkpoint["checkpoint_map"]) != {requested_checkpoint["checkpoint_ns"]} - or requested_checkpoint["checkpoint_map"][requested_checkpoint["checkpoint_ns"]] != requested_checkpoint["checkpoint_id"]): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - known = [s async for s in graph.aget_state_history(config)] - if not any(s.config["configurable"].get("checkpoint_id") == requested_checkpoint.get("checkpoint_id") and s.config["configurable"].get("checkpoint_ns", "") == requested_checkpoint.get("checkpoint_ns") for s in known): - raise ValueError("Unknown checkpoint") - config["configurable"].update({k: v for k, v in requested_checkpoint.items() if k != "thread_id"}) - op = request["op"] - if op == "configure": - scenario = request["scenario"] - if scenario not in SCENARIOS: - raise ValueError("Unsupported scenario") - result = {} - elif op == "create": - result = {} - elif op == "state": - result = {"state": checkpoint(await graph.aget_state(config))} - elif op == "history": - result = {"history": [checkpoint(s) async for s in graph.aget_state_history(config)]} - elif op == "submit": - current = await graph.aget_state(config) - if any(t.interrupts for t in current.tasks): - raise ValueError("Cannot submit over paused batch") - messages = request.get("messages") - if not isinstance(messages, list) or len(messages) != 1: - raise ValueError("Expected one human message") - message = messages[0] - if not isinstance(message, dict) or set(message) != {"type", "id", "content"} or message["type"] != "human" or not isinstance(message["id"], str) or not message["id"].strip() or not isinstance(message["content"], str): - raise ValueError("Invalid human message") - identity = message["id"] - if (request["threadId"], identity) in seen: - raise ValueError("Replayed human message") - seen.add((request["threadId"], identity)) - model.responses = responses(batches(scenario, identity), identity) - model.cursor = 0 - provenance[request["threadId"]] = scenario - events = [] - async for kind, event in graph.astream( - {"messages": request["messages"]}, config, - stream_mode=["messages", "values", "updates", "checkpoints"]): - events.append([kind, deepcopy(event)]) - scripts[request["threadId"]] = (model.responses, model.cursor) - result = {"events": events, "state": checkpoint(await graph.aget_state(config))} - elif op == "resume": - from langgraph.types import Command - current = await graph.aget_state(config) - interrupts = [i for t in current.tasks for i in t.interrupts] - command = request.get("command") - if not isinstance(command, dict) or set(command) != {"resume"} or not isinstance(command["resume"], dict) or set(command["resume"]) != {"decisions"} or len(interrupts) != 1: - raise ValueError("No current batch or invalid command") - decisions = command["resume"]["decisions"] - batch = interrupts[0].value - if not isinstance(decisions, list) or len(decisions) != len(batch["action_requests"]): - raise ValueError("Decision count mismatch") - for decision, review in zip(decisions, batch["review_configs"]): - if not isinstance(decision, dict) or set(decision) != {"type"} or decision["type"] not in ("approve", "reject") or decision["type"] not in review["allowed_decisions"]: - raise ValueError("Unsupported decision") - model.responses, model.cursor = scripts[request["threadId"]] - events = [] - async for kind, event in graph.astream(Command(resume=command["resume"]), config, - stream_mode=["messages", "values", "updates", "checkpoints"]): - events.append([kind, deepcopy(event)]) - scripts[request["threadId"]] = (model.responses, model.cursor) - result = {"events": events, "state": checkpoint(await graph.aget_state(config))} - else: - raise ValueError("Unsupported operation") - network.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, "op": op, - "threadId": request.get("threadId"), "scenario": provenance.get(request.get("threadId"), scenario), - "sourceSha256": {p: hashlib.sha256((PROJECT / p).read_bytes()).hexdigest() for p in SOURCES}, - "networkConnectAttempts": network.call_count, - } - result["requestId"] = request["requestId"] - wire = json.dumps(result, default=encode) - if len(wire.encode()) > MAX_RESPONSE: - raise ValueError("Oversized response") - print(wire, flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": (request.get("requestId") if isinstance(request, dict) else None), "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 865265efa..5774d5d55 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -19,7 +19,6 @@ import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; -import { createDeepAgentsFilesystemFixture } from './deep-agents-filesystem-fixture.mjs'; import { createDeepAgentsPlanningFixture } from './deep-agents-planning-fixture.mjs'; import { createDeepAgentsMemoryFixture } from './deep-agents-memory-fixture.mjs'; @@ -58,7 +57,6 @@ const nativeMastraFixture = configuration.library === 'runtimes' && configuratio const deepAgentsMemoryFixture = configuration.library === 'deep-agents' && configuration.topic === 'memory' ? createDeepAgentsMemoryFixture() : null; const deepAgentsSkillsFixture = configuration.library === 'deep-agents' && configuration.topic === 'skills' ? createDeepAgentsSkillsFixture() : null; const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && configuration.topic === 'subagents' ? createDeepAgentsSubagentsFixture() : null; -const deepAgentsFilesystemFixture = configuration.library === 'deep-agents' && configuration.topic === 'filesystem' ? createDeepAgentsFilesystemFixture() : null; const deepAgentsPlanningFixture = configuration.library === 'deep-agents' && configuration.topic === 'planning' ? createDeepAgentsPlanningFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by @@ -183,7 +181,6 @@ const server = createServer(async (request, response) => { if (deepAgentsMemoryFixture && (await deepAgentsMemoryFixture(request, response, pathname))) return; if (deepAgentsSkillsFixture && (await deepAgentsSkillsFixture(request, response, pathname))) return; if (deepAgentsSubagentsFixture && (await deepAgentsSubagentsFixture(request, response, pathname))) return; - if (deepAgentsFilesystemFixture && (await deepAgentsFilesystemFixture(request, response, pathname))) return; if (deepAgentsPlanningFixture && (await deepAgentsPlanningFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { if (request.method !== 'POST') return json(response, {}, 405); @@ -287,7 +284,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsFilesystemFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -300,7 +297,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsFilesystemFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From d84f3303b9c1ac84ebede421fdec4e0266d51bc6 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 17:52:13 -0800 Subject: [PATCH 16/41] test(cockpit): run React deep-agents/planning e2e, integration and website preview on aimock Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- .../angular/e2e/fixtures/da-planning.json | 233 ++++++++ .../planning/react/e2e/global-setup-impl.ts | 6 + .../planning/react/e2e/planning.spec.ts | 45 +- .../planning/react/e2e/playwright.config.ts | 3 + .../deep-agents/planning/react/project.json | 17 +- .../react/src/connection.integration.spec.ts | 101 ++-- scripts/cockpit-frontend-matrix.spec.mjs | 4 + scripts/examples/e2e-wiring.spec.ts | 7 +- scripts/react-cockpit/configuration.mjs | 1 + ...eep-agents-planning-configuration.spec.mjs | 21 - .../deep-agents-planning-fixture.mjs | 517 ------------------ .../deep-agents-planning-fixture.spec.mjs | 392 ------------- .../deep-agents-planning-wire.py | 133 ----- scripts/react-cockpit/serve.mjs | 7 +- 15 files changed, 324 insertions(+), 1165 deletions(-) create mode 100644 cockpit/deep-agents/planning/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/deep-agents-planning-configuration.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-planning-fixture.mjs delete mode 100644 scripts/react-cockpit/deep-agents-planning-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-planning-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 6f8c09e42..e37a57f22 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -479,7 +479,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-deep-agents-planning-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent', + command: 'node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4628', reuseExistingServer, diff --git a/cockpit/deep-agents/planning/angular/e2e/fixtures/da-planning.json b/cockpit/deep-agents/planning/angular/e2e/fixtures/da-planning.json index 12717a17c..bdd727c97 100644 --- a/cockpit/deep-agents/planning/angular/e2e/fixtures/da-planning.json +++ b/cockpit/deep-agents/planning/angular/e2e/fixtures/da-planning.json @@ -126,6 +126,239 @@ } ] } + }, + { + "match": { "toolCallId": "plan-normal-initial" }, + "response": { + "toolCalls": [ + { + "name": "lookup_field_elevation", + "arguments": "{\"airport\": \"KASE\"}", + "id": "plan-normal-elevation" + } + ] + } + }, + { + "match": { "toolCallId": "plan-normal-elevation" }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Check departure weather\", \"status\": \"completed\"}, {\"content\": \"Check destination elevation\", \"status\": \"completed\"}, {\"content\": \"Check runway\", \"status\": \"completed\"}, {\"content\": \"Assess mountain conditions\", \"status\": \"in_progress\"}]}", + "id": "plan-normal-revision" + } + ] + } + }, + { + "match": { "toolCallId": "plan-normal-revision" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-empty-clear" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-duplicates-old" }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Duplicate\", \"status\": \"completed\"}, {\"content\": \"Duplicate\", \"status\": \"in_progress\"}, {\"content\": \"First\", \"status\": \"pending\"}]}", + "id": "plan-duplicates-replace" + } + ] + } + }, + { + "match": { "toolCallId": "plan-duplicates-replace" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-parallel-parallel-b" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-recovery-parallel-b" }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Recovered\", \"status\": \"in_progress\"}]}", + "id": "plan-recovery-recover" + } + ] + } + }, + { + "match": { "toolCallId": "plan-recovery-recover" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-schema-invalid" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-oversized-oversized" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-empty-content-empty-content" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { "toolCallId": "plan-long-content-long-content" }, + "response": { "content": "The plan is up to date." } + }, + { + "match": { + "userMessage": "[scenario:normal]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Check departure weather\", \"status\": \"pending\"}, {\"content\": \"Check destination elevation\", \"status\": \"pending\"}, {\"content\": \"Check runway\", \"status\": \"pending\"}, {\"content\": \"Assess mountain conditions\", \"status\": \"pending\"}]}", + "id": "plan-normal-initial" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:empty]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": []}", + "id": "plan-empty-clear" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:duplicates]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Old\", \"status\": \"pending\"}, {\"content\": \"Removed\", \"status\": \"pending\"}]}", + "id": "plan-duplicates-old" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:parallel]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Rejected A\", \"status\": \"pending\"}]}", + "id": "plan-parallel-parallel-a" + }, + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Rejected B\", \"status\": \"pending\"}]}", + "id": "plan-parallel-parallel-b" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:recovery]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Rejected A\", \"status\": \"pending\"}]}", + "id": "plan-recovery-parallel-a" + }, + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Rejected B\", \"status\": \"pending\"}]}", + "id": "plan-recovery-parallel-b" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:schema]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Invalid\", \"status\": \"invented\"}]}", + "id": "plan-schema-invalid" + } + ] + } + }, + { + "match": { "userMessage": "[scenario:no-write]" }, + "response": { "content": "An answer with no plan change." } + }, + { + "match": { + "userMessage": "[scenario:oversized]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"Item 0\", \"status\": \"pending\"}, {\"content\": \"Item 1\", \"status\": \"pending\"}, {\"content\": \"Item 2\", \"status\": \"pending\"}, {\"content\": \"Item 3\", \"status\": \"pending\"}, {\"content\": \"Item 4\", \"status\": \"pending\"}, {\"content\": \"Item 5\", \"status\": \"pending\"}, {\"content\": \"Item 6\", \"status\": \"pending\"}, {\"content\": \"Item 7\", \"status\": \"pending\"}, {\"content\": \"Item 8\", \"status\": \"pending\"}, {\"content\": \"Item 9\", \"status\": \"pending\"}, {\"content\": \"Item 10\", \"status\": \"pending\"}, {\"content\": \"Item 11\", \"status\": \"pending\"}, {\"content\": \"Item 12\", \"status\": \"pending\"}, {\"content\": \"Item 13\", \"status\": \"pending\"}, {\"content\": \"Item 14\", \"status\": \"pending\"}, {\"content\": \"Item 15\", \"status\": \"pending\"}, {\"content\": \"Item 16\", \"status\": \"pending\"}, {\"content\": \"Item 17\", \"status\": \"pending\"}, {\"content\": \"Item 18\", \"status\": \"pending\"}, {\"content\": \"Item 19\", \"status\": \"pending\"}, {\"content\": \"Item 20\", \"status\": \"pending\"}, {\"content\": \"Item 21\", \"status\": \"pending\"}, {\"content\": \"Item 22\", \"status\": \"pending\"}, {\"content\": \"Item 23\", \"status\": \"pending\"}, {\"content\": \"Item 24\", \"status\": \"pending\"}, {\"content\": \"Item 25\", \"status\": \"pending\"}, {\"content\": \"Item 26\", \"status\": \"pending\"}, {\"content\": \"Item 27\", \"status\": \"pending\"}, {\"content\": \"Item 28\", \"status\": \"pending\"}, {\"content\": \"Item 29\", \"status\": \"pending\"}, {\"content\": \"Item 30\", \"status\": \"pending\"}, {\"content\": \"Item 31\", \"status\": \"pending\"}, {\"content\": \"Item 32\", \"status\": \"pending\"}, {\"content\": \"Item 33\", \"status\": \"pending\"}, {\"content\": \"Item 34\", \"status\": \"pending\"}, {\"content\": \"Item 35\", \"status\": \"pending\"}, {\"content\": \"Item 36\", \"status\": \"pending\"}, {\"content\": \"Item 37\", \"status\": \"pending\"}, {\"content\": \"Item 38\", \"status\": \"pending\"}, {\"content\": \"Item 39\", \"status\": \"pending\"}, {\"content\": \"Item 40\", \"status\": \"pending\"}, {\"content\": \"Item 41\", \"status\": \"pending\"}, {\"content\": \"Item 42\", \"status\": \"pending\"}, {\"content\": \"Item 43\", \"status\": \"pending\"}, {\"content\": \"Item 44\", \"status\": \"pending\"}, {\"content\": \"Item 45\", \"status\": \"pending\"}, {\"content\": \"Item 46\", \"status\": \"pending\"}, {\"content\": \"Item 47\", \"status\": \"pending\"}, {\"content\": \"Item 48\", \"status\": \"pending\"}, {\"content\": \"Item 49\", \"status\": \"pending\"}, {\"content\": \"Item 50\", \"status\": \"pending\"}]}", + "id": "plan-oversized-oversized" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:empty-content]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"\", \"status\": \"pending\"}]}", + "id": "plan-empty-content-empty-content" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:long-content]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_todos", + "arguments": "{\"todos\": [{\"content\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\", \"status\": \"pending\"}]}", + "id": "plan-long-content-long-content" + } + ] + } } ] } diff --git a/cockpit/deep-agents/planning/react/e2e/global-setup-impl.ts b/cockpit/deep-agents/planning/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..e4bf0a274 --- /dev/null +++ b/cockpit/deep-agents/planning/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'deep-agents/planning', port: 4628 }); diff --git a/cockpit/deep-agents/planning/react/e2e/planning.spec.ts b/cockpit/deep-agents/planning/react/e2e/planning.spec.ts index 35c133630..7fd9272c6 100644 --- a/cockpit/deep-agents/planning/react/e2e/planning.spec.ts +++ b/cockpit/deep-agents/planning/react/e2e/planning.spec.ts @@ -5,27 +5,29 @@ import { type APIRequestContext, } from '@playwright/test'; const base = '/deep-agents/planning/react/'; +// Backend: aimock replays cockpit/deep-agents/planning/angular/e2e/fixtures +// (shared with the Angular example; React scenarios are tagged prompts) into a +// real `langgraph dev`. The proxy records requests and holds or fails the next +// call (scripts/react-cockpit/langgraph-proxy.mjs). +let scenario = 'normal'; async function configure( request: APIRequestContext, values: Record ) { - const response = await request.post('/__configure', { - data: { - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - scenario: 'normal', - ...values, - }, - }); - expect(response.ok()).toBe(true); + const control = async (path: string) => + expect((await request.post(path)).ok()).toBe(true); + scenario = typeof values.scenario === 'string' ? values.scenario : 'normal'; + if (values.holdStream) await control('/__hold/stream'); + // The saved-plan confirmation reads the exact checkpoint. + if (values.holdCheckpoint) await control('/__hold/state'); + if (values.failCreation) await control('/__fail-create'); + if (values.failStream) await control('/__fail-stream'); + await control(values.failCheckpoint ? '/__state/failure' : '/__state/safe'); } async function send(page: Page, text = 'Dispatch brief: KSFO to KASE') { const box = page.getByRole('textbox', { name: 'Message' }); - await box.fill(text); + // The scenario tag selects the scripted model turns (shared fixtures). + await box.fill(`${text} [scenario:${scenario}]`); await box.press('Enter'); } const plan = (page: Page) => page.getByRole('complementary', { name: 'Plan' }); @@ -63,6 +65,7 @@ for (const width of [375, 1280]) { }); } test.beforeEach(async ({ page, request }) => { + await request.post('/__reset'); await configure(request, {}); await page.goto(base); }); @@ -86,6 +89,7 @@ test('mount is inert; the welcome suggestion sends and native live replacement b await expect( plan(page).getByText('Live plan · waiting for confirmation') ).toBeVisible(); + await request.post('/__release-state'); await request.post('/__release'); await expect(page.getByText('Response saved.')).toBeVisible(); await expect( @@ -104,13 +108,6 @@ test('mount is inert; the welcome suggestion sends and native live replacement b () => document.documentElement.scrollWidth <= innerWidth ) ).toBe(true); - const proof = await (await request.get('/__graph-proof')).json(); - expect( - proof.every( - (row: { actualCompiledGraph: boolean; networkConnectAttempts: number }) => - row.actualCompiledGraph && row.networkConnectAttempts === 0 - ) - ).toBe(true); }); test('failed creation retries; native stream failure retains saved plan', async ({ page, @@ -166,12 +163,13 @@ test('stop holds saved authority, ignores late stream, and New clears without cr await send(page, 'Change'); await expect(page.getByRole('button', { name: 'Stop' })).toBeVisible(); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).held) - .toBe(1); + .poll(async () => (await (await request.get('/__lifetime')).json()).activeStream) + .toBe(true); await page.getByRole('button', { name: 'Stop' }).click(); await expect( page.getByText('Response stopped.', { exact: true }) ).toBeVisible(); + await request.post('/__release-state'); await request.post('/__release'); await expect(plan(page).getByText('3 of 4 completed')).toBeVisible(); const before = (await (await request.get('/__requests')).json()).filter( @@ -193,6 +191,7 @@ test('New during held checkpoint ignores stale owner; new send owns its conversa await send(page); await expect(page.getByText('Confirming saved plan…')).toBeVisible(); await page.getByRole('button', { name: 'New conversation' }).click(); + await request.post('/__release-state'); await request.post('/__release'); await expect(plan(page).getByText(/Ask the assistant/)).toBeVisible(); await expect(page.getByRole('article')).toHaveCount(0); diff --git a/cockpit/deep-agents/planning/react/e2e/playwright.config.ts b/cockpit/deep-agents/planning/react/e2e/playwright.config.ts index edc5d356d..13d89133e 100644 --- a/cockpit/deep-agents/planning/react/e2e/playwright.config.ts +++ b/cockpit/deep-agents/planning/react/e2e/playwright.config.ts @@ -6,6 +6,9 @@ export default defineConfig({ timeout: 30000, use: { baseURL: 'http://127.0.0.1:4628', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/deep-agents/planning/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent', diff --git a/cockpit/deep-agents/planning/react/project.json b/cockpit/deep-agents/planning/react/project.json index 40cc2be6d..f0dd5069e 100644 --- a/cockpit/deep-agents/planning/react/project.json +++ b/cockpit/deep-agents/planning/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/deep-agents/planning/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-deep-agents-planning-angular", "cockpit-deep-agents-planning-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -17,23 +18,11 @@ "command": "node scripts/react-cockpit/build.mjs deep-agents-planning" } }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-deep-agents-planning-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-planning-fixture.spec.mjs scripts/react-cockpit/deep-agents-planning-configuration.spec.mjs" - } - }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-deep-agents-planning-python"] }], "options": { "config": "cockpit/deep-agents/planning/react/e2e/playwright.config.ts", "skipInstall": true diff --git a/cockpit/deep-agents/planning/react/src/connection.integration.spec.ts b/cockpit/deep-agents/planning/react/src/connection.integration.spec.ts index 1653d1030..e0681e57e 100644 --- a/cockpit/deep-agents/planning/react/src/connection.integration.spec.ts +++ b/cockpit/deep-agents/planning/react/src/connection.integration.spec.ts @@ -1,49 +1,44 @@ -import { - createServer, - type Server, - type IncomingMessage, - type ServerResponse, -} from 'node:http'; -import { afterEach, expect, it, vi } from 'vitest'; +import { createServer, type Server } from 'node:http'; +import { resolve } from 'node:path'; +import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest'; import { createPlanningClient } from './connection'; import { createPlanningApplication } from './application'; -import { plainRecord } from './plan-state'; -/* eslint-disable @nx/enforce-module-boundaries -- Local graph proof transport is never bundled. */ -// @ts-expect-error Local proof-only JavaScript has no published declaration. -import { createDeepAgentsPlanningFixture as createFixture } from '../../../../../scripts/react-cockpit/deep-agents-planning-fixture.mjs'; + +/* eslint-disable @nx/enforce-module-boundaries -- Local test backend tooling is never bundled. */ +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createAimockBackend } from '../../../../../scripts/react-cockpit/aimock-backend.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createLanggraphProxy } from '../../../../../scripts/react-cockpit/langgraph-proxy.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { reactCockpitConfiguration } from '../../../../../scripts/react-cockpit/configuration.mjs'; /* eslint-enable @nx/enforce-module-boundaries */ -interface LocalFixture { - ( - request: IncomingMessage, - response: ServerResponse, - pathname: string - ): Promise; - close(): Promise; -} -const createDeepAgentsPlanningFixture = createFixture as () => LocalFixture; + +// The real graph runs under `langgraph dev` on aimock fixtures shared with the +// Angular example (cockpit/deep-agents/planning/angular/e2e/fixtures). Each +// scenario is a tagged prompt; the in-process proxy records requests and holds +// or fails the next call (scripts/react-cockpit/langgraph-proxy.mjs). +const { backend: config } = reactCockpitConfiguration('deep-agents-planning'); +const backend = createAimockBackend({ root: resolve(__dirname, '../../../../..'), backend: config }); +beforeAll(() => backend.ensure(), 180000); +afterAll(() => backend.close()); +type Handler = (request: unknown, response: unknown, pathname: string) => Promise; let server: Server | undefined, - fixture: ReturnType | undefined, application: ReturnType | undefined; afterEach(async () => { await application?.dispose(); - await fixture?.close(); if (server) - await new Promise((resolve) => { - server?.close(() => resolve()); + await new Promise((done) => { + server?.close(() => done()); server?.closeAllConnections(); }); }); async function setup() { - const handle = createDeepAgentsPlanningFixture(); - fixture = handle; + const handle: Handler = createLanggraphProxy({ + upstream: `http://127.0.0.1:${config.langgraphPort}`, + ready: () => backend.ensure(), + }); const http = createServer(async (req, res) => { - if ( - !(await handle( - req, - res, - new URL(req.url ?? '/', 'http://localhost').pathname - )) - ) { + if (!(await handle(req, res, new URL(req.url ?? '/', 'http://localhost').pathname))) { res.writeHead(404); res.end(); } @@ -55,19 +50,27 @@ async function setup() { const base = `http://127.0.0.1:${address.port}`, client = createPlanningClient({ apiUrl: base + '/api', headers: {} }); const read = vi.spyOn(client, 'readCheckpoint'), - app = createPlanningApplication(client); - application = app; + created = createPlanningApplication(client); + application = created; + let scenario = 'normal'; + const control = async (path: string) => + expect((await fetch(base + path, { method: 'POST' })).ok).toBe(true); + // The scenario tag selects the scripted model turns (shared fixtures). + const app = Object.assign(Object.create(created) as typeof created, { + submit: (text: string) => created.submit(`${text} [scenario:${scenario}]`), + }); const configure = async ( - scenario: string, - extra: Record = {} + next: string, + extra: { failStream?: boolean; holdCheckpoint?: boolean } = {} ) => { - const response = await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify({ scenario, ...extra }), - }); - expect(response.ok).toBe(true); + scenario = next; + if (extra.failStream) await control('/__fail-stream'); + if (extra.holdCheckpoint) await control('/__hold/state'); + }; + const release = async () => { + await control('/__release-state'); + await control('/__release'); }; - const release = () => fetch(base + '/__release', { method: 'POST' }); return { app, client, read, base, configure, release }; } it('first native transport failure before any checkpoint honestly requires New; a fresh conversation succeeds', async () => { @@ -99,18 +102,6 @@ it('native session confirms actual middleware writes, exact checkpoint, unfinish await f.configure('empty'); expect(await f.app.submit('Clear the plan')).toBe(true); expect(f.app.getSnapshot().savedPlan).toEqual({ kind: 'valid', items: [] }); - const proof: unknown[] = await ( - await fetch(f.base + '/__graph-proof') - ).json(); - expect(proof.length).toBeGreaterThan(0); - expect( - proof.every( - (p) => - plainRecord(p) && - p.actualCompiledGraph === true && - p.networkConnectAttempts === 0 - ) - ).toBe(true); }, 30000); for (const scenario of ['parallel', 'schema']) it(`actual ${scenario} rejection is a received core result, never saved authority; later native recovery succeeds`, async () => { diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index df6f2cd48..876994179 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -63,6 +63,10 @@ test('Planning owns an exact closed native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('deep-agents-planning'), { topic: 'planning', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/planning/react', base: '/deep-agents/planning/react/', port: 4628, project: 'cockpit-deep-agents-planning-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/deep-agents/planning/python', + fixtures: 'cockpit/deep-agents/planning/angular/e2e/fixtures', langgraphPort: 5628, + }, }); for (const key of ['planning', 'deep-agents/planning', '../deep-agents-planning', 'deep-agents-planning/../memory']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index ecd37f9da..4758a6662 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -13,17 +13,16 @@ function websiteBuildProjects(website: { )?.projects; } -it('wires native Planning build, fixture and website coverage to the shared Python graph', () => { +it('wires native Planning build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); const react = read('cockpit/deep-agents/planning/react/project.json'); expect(react.tags).toEqual(expect.arrayContaining(['framework:react', 'scope:cockpit-e2e', 'scope:cockpit-examples'])); expect(react.targets.build.options.command).toBe('node scripts/react-cockpit/build.mjs deep-agents-planning'); - expect(react.targets.e2e.dependsOn).toEqual(['build', 'fixture-test']); - expect(react.targets['fixture-test'].dependsOn).toContainEqual({ target: 'test', projects: ['cockpit-deep-agents-planning-python'] }); + expect(react.targets.e2e.dependsOn).toEqual(['build', { target: 'test', projects: ['cockpit-deep-agents-planning-python'] }]); const website = read('apps/website/project.json'); expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-planning-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); - expect(config).toContain('cockpit-deep-agents-planning-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent'); + expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4628'"); }); it('wires native Skills build, fixture and website coverage to the shared Python graph', () => { diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 1e22e03f1..e85d45586 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -80,6 +80,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { return Object.freeze({ topic: 'planning', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/planning/react', base: '/deep-agents/planning/react/', port: 4628, project: 'cockpit-deep-agents-planning-react', + backend: aimockLanggraph('deep-agents/planning', 4628), }); if (topic === 'chat-generative-ui') return Object.freeze({ diff --git a/scripts/react-cockpit/deep-agents-planning-configuration.spec.mjs b/scripts/react-cockpit/deep-agents-planning-configuration.spec.mjs deleted file mode 100644 index 409966dda..000000000 --- a/scripts/react-cockpit/deep-agents-planning-configuration.spec.mjs +++ /dev/null @@ -1,21 +0,0 @@ -import assert from 'node:assert/strict'; -import { test } from 'node:test'; -import { readFileSync } from 'node:fs'; -import { reactCockpitConfiguration } from './configuration.mjs'; -test('Planning has a closed native React selection and fixture dispatch precedes generic fallback', () => { - assert.deepEqual(reactCockpitConfiguration('deep-agents-planning'), { - topic: 'planning', - library: 'deep-agents', - adapter: 'langgraph', - appPath: 'cockpit/deep-agents/planning/react', - base: '/deep-agents/planning/react/', - port: 4628, - project: 'cockpit-deep-agents-planning-react', - }); - assert.throws(() => reactCockpitConfiguration('deep-agents/../../foreign')); - const serve = readFileSync(new URL('./serve.mjs', import.meta.url), 'utf8'); - assert.ok( - serve.indexOf('await deepAgentsPlanningFixture(request') < - serve.indexOf("if (pathname === '/__reset')") - ); -}); diff --git a/scripts/react-cockpit/deep-agents-planning-fixture.mjs b/scripts/react-cockpit/deep-agents-planning-fixture.mjs deleted file mode 100644 index b80ff4756..000000000 --- a/scripts/react-cockpit/deep-agents-planning-fixture.mjs +++ /dev/null @@ -1,517 +0,0 @@ -/** Loopback-only SDK transport backed by the actual compiled Planning graph. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { readFileSync } from 'node:fs'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, timeout) { - const child = spawnWorker(); - const pending = new Map(); - let sequence = 0, - dead = false, - closing; - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const item of pending.values()) { - clearTimeout(item.timer); - item.reject(Error('Planning graph worker unavailable.')); - } - pending.clear(); - } - const terminate = () => { - fail(); - child.kill('SIGTERM'); - }; - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', terminate); - lines.once('close', () => { - if (!dead) terminate(); - }); - lines.on('line', (line) => { - try { - if (Buffer.byteLength(line) > 16 * 1024 * 1024) - throw Error('Oversized response'); - const result = JSON.parse(line), - item = pending.get(result.requestId); - if (!item) throw Error('Unowned response'); - pending.delete(result.requestId); - clearTimeout(item.timer); - if (result.error) item.reject(Error('Planning graph operation failed.')); - else item.resolve(result); - } catch { - terminate(); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(Error('Planning graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const wire = JSON.stringify({ ...value, requestId }) + '\n'; - if (Buffer.byteLength(wire) > 1024 * 1024) { - reject(Error('Oversized request')); - return; - } - const timer = setTimeout(terminate, timeout); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(wire); - }); - }, - close() { - return (closing ??= (async () => { - fail(); - child.stdin.end(); - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })()); - }, - }; -} - -export function createDeepAgentsPlanningFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/planning/python', - 'python', - 'scripts/react-cockpit/deep-agents-planning-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-planning-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const sourceSha256 = Object.fromEntries( - ['src/graph.py', 'prompts/planning.md', 'uv.lock'].map((path) => [ - path, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/deep-agents/planning/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - closed = false, - closing, - queue = Promise.resolve(); - const threads = new Set(), - runs = new Map(), - held = new Set(), - proofs = [], - requests = []; - const options = { - scenario: 'normal', - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - }; - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value), - proof = result.proof; - if ( - closed || - proof?.actualCompiledGraph !== true || - proof.networkConnectAttempts !== 0 || - proof.op !== value.op || - proof.threadId !== (value.threadId ?? null) || - Object.keys(proof.sourceSha256 ?? {}).length !== - Object.keys(sourceSha256).length || - Object.entries(sourceSha256).some( - ([path, hash]) => proof.sourceSha256[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - proofs.push(proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function hold(response, finish, run) { - const item = { response, finish, run }; - held.add(item); - response.once('close', () => { - if (held.delete(item) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - let input = ''; - for await (const chunk of request) { - input += chunk; - if (Buffer.byteLength(input) > 1024 * 1024) { - json(response, {}, 413); - return true; - } - } - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - if ( - !only(body, [ - 'scenario', - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ]) || - ('scenario' in body && - ![ - 'normal', - 'empty', - 'duplicates', - 'parallel', - 'schema', - 'recovery', - 'no-write', - 'oversized', - 'empty-content', - 'long-content', - ].includes(body.scenario)) || - [ - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ].some((k) => k in body && typeof body[k] !== 'boolean') - ) { - json(response, {}, 400); - return true; - } - if ('scenario' in body) - await operate({ op: 'configure', scenario: body.scenario }); - Object.assign(options, body); - json(response, {}); - return true; - } - if (pathname === '/__release') { - for (const item of held) { - held.delete(item); - item.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if (options.failCreation) { - options.failCreation = false; - json(response, { error: 'Controlled creation failure.' }, 500); - return true; - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - !only(body, ['thread_id', 'if_exists', 'metadata']) || - (body.if_exists !== undefined && body.if_exists !== 'raise') || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if (suffix === '/state' && input.length !== 0) { - json(response, {}, 400); - return true; - } - if (suffix === '/state/checkpoint') { - if (options.failCheckpoint) { - options.failCheckpoint = false; - json(response, { error: 'Controlled exact read failure.' }, 500); - return true; - } - if ( - !only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - body.checkpoint.thread_id !== threadId || - typeof body.checkpoint.checkpoint_id !== 'string' || - typeof body.checkpoint.checkpoint_ns !== 'string' - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - if ( - !history.some( - (s) => - s.checkpoint.checkpoint_id === body.checkpoint.checkpoint_id && - s.checkpoint.checkpoint_ns === body.checkpoint.checkpoint_ns - ) || - (body.checkpoint.checkpoint_map !== undefined && - (!only(body.checkpoint.checkpoint_map, [ - body.checkpoint.checkpoint_ns, - ]) || - Object.values(body.checkpoint.checkpoint_map).some( - (id) => id !== body.checkpoint.checkpoint_id - ))) - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - ...(suffix === '/state/checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }); - const finish = () => json(response, state); - if ( - options.holdState || - (options.holdCheckpoint && suffix === '/state/checkpoint') - ) - hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit', 'before', 'metadata', 'checkpoint']) || - (body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100)) - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - json(response, history.slice(0, body.limit ?? 10)); - return true; - } - const runRoute = /^\/runs\/([^/]+)(\/cancel)?$/.exec(suffix); - if ( - runRoute && - (request.method === 'GET' || (runRoute[2] && request.method === 'POST')) - ) { - const run = runs.get(runRoute[1]); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - if (runRoute[2]) { - const search = new URL(request.url, 'http://localhost').searchParams; - if (search.get('action') !== 'interrupt') { - json(response, {}, 400); - return true; - } - for (const item of held) - if (item.run === run) { - held.delete(item); - run.status = 'interrupted'; - item.response.end(); - } - json(response, {}); - } else json(response, run); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - if (options.failStream) { - options.failStream = false; - json(response, { error: 'Controlled stream failure.' }, 500); - return true; - } - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !only(body.input, ['messages']) || - body.input.messages?.length !== 1 || - !only(message, ['type', 'id', 'content']) || - message.type !== 'human' || - typeof message.id !== 'string' || - !message.id.trim() || - typeof message.content !== 'string' || - body.assistant_id !== 'da-planning' || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'checkpoints', - 'custom', - ].includes(mode) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const finish = () => { - if (response.destroyed || closed) return; - for (const [kind, data] of wire.events) - if (modes.has(kind)) response.write(event(kind, data)); - run.status = 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Planning graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const item of held) item.response.end(); - held.clear(); - await worker?.close(); - })()); - return handle; -} diff --git a/scripts/react-cockpit/deep-agents-planning-fixture.spec.mjs b/scripts/react-cockpit/deep-agents-planning-fixture.spec.mjs deleted file mode 100644 index 95aac49dd..000000000 --- a/scripts/react-cockpit/deep-agents-planning-fixture.spec.mjs +++ /dev/null @@ -1,392 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer, request } from 'node:http'; -import { createInterface } from 'node:readline'; -import { fileURLToPath } from 'node:url'; -import { once } from 'node:events'; -import { randomUUID, createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; -import { spawn } from 'node:child_process'; -import { test } from 'node:test'; -import { Client } from '@langchain/langgraph-sdk'; - -const { createDeepAgentsPlanningFixture } = await import( - './deep-agents-planning-fixture.mjs' -).catch(() => ({})); - -async function fixture(t, options) { - assert.equal( - typeof createDeepAgentsPlanningFixture, - 'function', - 'Planning compiled-graph fixture exists' - ); - const handle = createDeepAgentsPlanningFixture(options); - const server = createServer(async (req, res) => { - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const client = new Client({ - apiUrl: base + '/api', - callerOptions: { maxRetries: 0 }, - }); - const thread = randomUUID(); - await client.threads.create({ threadId: thread, ifExists: 'raise' }); - const control = async (body) => { - const response = await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify(body), - }); - assert.equal(response.status, 200); - }; - const stream = ( - content = 'Plan the KSFO to KASE flight.', - id = randomUUID() - ) => - client.runs.stream(thread, 'da-planning', { - input: { messages: [{ type: 'human', id, content }] }, - streamMode: ['messages-tuple', 'values', 'updates', 'checkpoints'], - streamSubgraphs: true, - streamResumable: true, - onDisconnect: 'continue', - }); - const submit = async (content, id = randomUUID()) => { - const events = []; - for await (const event of stream(content, id)) events.push(event); - return { - events, - id, - run: events.find((e) => e.event === 'metadata').data.run_id, - }; - }; - return { handle, base, client, thread, control, stream, submit }; -} - -function toolResults(state, ids) { - return state.values.messages.filter( - (m) => m.type === 'tool' && ids.includes(m.tool_call_id) - ); -} - -test('installed SDK streams actual replacement checkpoints and exact canonical saved state', async (t) => { - const f = await fixture(t); - const { events, run, id } = await f.submit(); - const saved = await f.client.threads.getState(f.thread); - const terminal = events.filter((e) => e.event === 'checkpoints').at(-1).data; - assert.deepEqual( - events.filter((e) => e.event === 'values').at(-1).data, - saved.values - ); - assert.equal( - terminal.config.configurable.checkpoint_id, - saved.checkpoint.checkpoint_id - ); - assert.equal(terminal.config.configurable.thread_id, f.thread); - assert.deepEqual(terminal.values, saved.values); - assert.deepEqual(saved.next, []); - assert.deepEqual(saved.tasks, []); - assert.equal(saved.values.messages[0].id, id); - assert.equal(saved.values.messages.at(-1).type, 'ai'); - assert.equal(saved.values.todos.at(-1).status, 'in_progress'); - assert.equal( - saved.values.todos.filter((todo) => todo.status === 'completed').length, - 3 - ); - assert.equal(Object.hasOwn(saved.metadata, 'run_id'), false); - assert.equal(Object.hasOwn(terminal.metadata, 'run_id'), false); - assert.equal(Object.hasOwn(saved.values, 'completed_turn_id'), false); - assert.equal(Object.hasOwn(saved.values, 'operation_receipt'), false); - const exact = await f.client.threads.getState(f.thread, saved.checkpoint); - assert.deepEqual(exact, saved); - const history = await f.client.threads.getHistory(f.thread, { limit: 100 }); - assert.equal( - history[0].checkpoint.checkpoint_id, - saved.checkpoint.checkpoint_id - ); - assert(history.some((s) => s.next.length)); - assert(history.some((s) => s.tasks.length)); - assert( - history.every((s) => s.tasks.every((task) => task.interrupts.length === 0)) - ); - assert.equal((await f.client.runs.get(f.thread, run)).status, 'success'); - const writes = saved.values.messages.filter( - (m) => m.type === 'tool' && m.content.startsWith('Updated todo list to') - ); - assert.equal(writes.length, 2); - assert( - writes.every((m) => m.name === 'write_todos' && m.status === 'success') - ); - const lookup = saved.values.messages.find( - (m) => m.type === 'tool' && m.name === 'lookup_field_elevation' - ); - assert.equal(lookup.content, 'KASE field elevation is 7820 ft.'); - const proofs = await (await fetch(f.base + '/__graph-proof')).json(); - for (const proof of proofs) { - assert.equal(proof.actualCompiledGraph, true); - assert.equal(proof.networkConnectAttempts, 0); - for (const source of ['src/graph.py', 'prompts/planning.md', 'uv.lock']) { - const expected = createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/deep-agents/planning/python/' + source, - import.meta.url - ) - ) - ) - .digest('hex'); - assert.equal(proof.sourceSha256[source], expected); - } - } -}); - -test('SDK preserves duplicates/order, replaces the whole list, and accepts explicit empty clearing', async (t) => { - const f = await fixture(t); - await f.control({ scenario: 'duplicates' }); - await f.submit(); - const saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.todos, [ - { content: 'Duplicate', status: 'completed' }, - { content: 'Duplicate', status: 'in_progress' }, - { content: 'First', status: 'pending' }, - ]); - await f.control({ scenario: 'empty' }); - await f.submit('Clear plan.'); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values.todos, - [] - ); -}); - -test('SDK exposes middleware rejection, schema errors, and subsequent valid recovery truthfully', async (t) => { - const f = await fixture(t); - await f.submit(); - const prior = (await f.client.threads.getState(f.thread)).values.todos; - await f.control({ scenario: 'parallel' }); - const rejected = await f.submit('Parallel write.'); - let saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.todos, prior); - const errors = toolResults(saved, [ - rejected.id + '-parallel-a', - rejected.id + '-parallel-b', - ]); - assert.equal(errors.length, 2); - assert(errors.every((m) => m.status === 'error' && m.name === null)); - assert(errors.every((m) => m.content.includes('multiple times in parallel'))); - await f.control({ scenario: 'schema' }); - const invalid = await f.submit('Invalid write.'); - saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.todos, prior); - const invalidResult = toolResults(saved, [invalid.id + '-invalid'])[0]; - assert.equal(invalidResult.name, 'write_todos'); - assert.equal(invalidResult.status, 'error'); - await f.control({ scenario: 'recovery' }); - const recovered = await f.submit('Recover.'); - saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.todos, [ - { content: 'Recovered', status: 'in_progress' }, - ]); - assert.equal( - toolResults(saved, [recovered.id + '-recover'])[0].status, - 'success' - ); -}); - -test('SDK cancellation stops held delivery without forging rollback and cleanup reaps worker', async (t) => { - const f = await fixture(t); - await f.control({ holdStream: true }); - const stream = f.stream(); - const metadata = await stream.next(); - const run = metadata.value.data.run_id; - assert.equal((await f.client.runs.get(f.thread, run)).status, 'running'); - await f.client.runs.cancel(f.thread, run, true, 'interrupt'); - assert.equal((await f.client.runs.get(f.thread, run)).status, 'interrupted'); - assert.equal((await stream.next()).done, true); - // The graph executes before buffered delivery: transport cancellation is not rollback. - const saved = await f.client.threads.getState(f.thread); - assert.equal(saved.values.todos.length, 4); - const lifetime = await (await fetch(f.base + '/__lifetime')).json(); - assert.equal(lifetime.held, 0); - assert.equal(lifetime.workers, 1); - await f.handle.close(); - assert.throws(() => process.kill(lifetime.workerPid, 0), { code: 'ESRCH' }); -}); - -test('checkpoint ownership and graph-owned input cannot be overridden through SDK', async (t) => { - const f = await fixture(t); - await f.submit(); - const saved = await f.client.threads.getState(f.thread); - await assert.rejects( - f.client.threads.getState(f.thread, { - ...saved.checkpoint, - thread_id: randomUUID(), - }) - ); - await assert.rejects( - f.client.threads.getState(f.thread, { - ...saved.checkpoint, - checkpoint_id: randomUUID(), - }) - ); - await assert.rejects(async () => { - for await (const unused of f.client.runs.stream(f.thread, 'da-planning', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'Override.' }], - todos: [], - }, - streamMode: ['values'], - })) - assert.fail('Invalid graph-owned input streamed'); - }); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values, - saved.values - ); -}); - -test('current-state GET rejects foreign checkpoint bodies between owned threads', async (t) => { - const f = await fixture(t); - const other = randomUUID(); - await f.client.threads.create({ threadId: other, ifExists: 'raise' }); - await f.submit(); - for await (const unused of f.client.runs.stream(other, 'da-planning', { - input: { - messages: [ - { type: 'human', id: 'other-human', content: 'Other thread.' }, - ], - }, - streamMode: ['values'], - })) { - /* Consume the actual other thread graph. */ - } - const foreign = await f.client.threads.getState(other); - const current = await f.client.threads.getState(f.thread); - const body = JSON.stringify({ checkpoint: foreign.checkpoint }); - const status = await new Promise((resolve, reject) => { - const req = request( - f.base + `/api/threads/${f.thread}/state`, - { - method: 'GET', - headers: { - 'content-type': 'application/json', - 'content-length': Buffer.byteLength(body), - }, - }, - (res) => { - res.resume(); - res.once('end', () => resolve(res.statusCode)); - } - ); - req.once('error', reject); - req.end(body); - }); - assert.equal( - status, - 400, - 'current-state GET must reject a checkpoint request body' - ); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values, - current.values - ); -}); - -test( - 'worker rejects checkpoint thread overrides independently of HTTP validation', - { timeout: 30000 }, - async (t) => { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/planning/python', - 'python', - 'scripts/react-cockpit/deep-agents-planning-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ); - child.stderr.resume(); - const lines = createInterface({ input: child.stdout }); - t.after(async () => { - if (child.exitCode === null && child.signalCode === null) { - const ended = once(child, 'close'); - child.kill('SIGKILL'); - await ended; - } - lines.close(); - }); - const send = async (value) => { - const line = once(lines, 'line'); - child.stdin.write(JSON.stringify(value) + '\n'); - return JSON.parse((await line)[0]); - }; - const foreign = await send({ - op: 'submit', - threadId: 'worker-other', - requestId: 1, - messages: [ - { type: 'human', id: 'worker-other-human', content: 'Other owner.' }, - ], - }); - assert.equal(foreign.state.values.messages[0].id, 'worker-other-human'); - const response = await send({ - op: 'state', - threadId: 'worker-current', - requestId: 2, - checkpoint: foreign.state.checkpoint, - }); - assert.equal( - response.error, - 'Graph operation failed', - 'worker must reject foreign checkpoint owner' - ); - assert.equal(response.requestId, 2); - } -); - -for (const [name, source] of [ - ['timeout', 'process.stdin.resume(); setInterval(() => {}, 1000);'], - [ - 'malformed output', - 'process.stdin.once("data", () => { process.stdout.write("not-json\\n"); }); setInterval(() => {}, 1000);', - ], - [ - 'unowned response', - 'process.stdin.once("data", () => { process.stdout.write(JSON.stringify({requestId: 999}) + "\\n"); }); setInterval(() => {}, 1000);', - ], - [ - 'invalid graph proof', - 'process.stdin.once("data", data => { const {requestId} = JSON.parse(data); process.stdout.write(JSON.stringify({requestId, proof: {actualCompiledGraph: false}}) + "\\n"); }); setInterval(() => {}, 1000);', - ], -]) { - test(`worker ${name} fails closed and cleanup reaps the child`, async (t) => { - let child; - const f = await fixture(t, { - workerTimeoutMs: 1000, - spawnWorker: () => - (child = spawn(process.execPath, ['-e', source], { - stdio: ['pipe', 'pipe', 'pipe'], - })), - }); - await assert.rejects(f.client.threads.getState(f.thread)); - await f.handle.close(); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - }); -} diff --git a/scripts/react-cockpit/deep-agents-planning-wire.py b/scripts/react-cockpit/deep-agents-planning-wire.py deleted file mode 100644 index 65759a6a3..000000000 --- a/scripts/react-cockpit/deep-agents-planning-wire.py +++ /dev/null @@ -1,133 +0,0 @@ -"""Bounded newline-JSON worker running the unchanged compiled Planning builder.""" -import asyncio -from contextlib import redirect_stdout -from copy import deepcopy -import hashlib -import json -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/deep-agents/planning/python" -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel, build, call, responses, todo - -SOURCES = ("src/graph.py", "prompts/planning.md", "uv.lock") -MAX_REQUEST = 1024 * 1024 -MAX_RESPONSE = 16 * 1024 * 1024 - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() - if k in ("thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map")}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": t.id, "name": t.name, - **({"error": t.error} if t.error is not None else {}), - "interrupts": [{"id": item.id, "value": item.value} for item in t.interrupts]} - for t in state.tasks], - } - - -def batches(scenario, identity): - write = lambda items, suffix: call(items, identity + "-" + suffix) - if scenario == "no-write": - return [] - if scenario == "oversized": - return [[write([todo(f"Item {i}") for i in range(51)], "oversized")]] - if scenario == "empty-content": - return [[write([todo("")], "empty-content")]] - if scenario == "long-content": - return [[write([todo("" + "x" * 1976)], "long-content")]] - if scenario == "empty": - return [[write([], "clear")]] - if scenario == "duplicates": - return [[write([todo("Old"), todo("Removed")], "old")], - [write([todo("Duplicate", "completed"), todo("Duplicate", "in_progress"), todo("First")], "replace")]] - parallel = [write([todo("Rejected A")], "parallel-a"), write([todo("Rejected B")], "parallel-b")] - if scenario == "parallel": - return [parallel] - if scenario == "recovery": - return [parallel, [write([todo("Recovered", "in_progress")], "recover")]] - if scenario == "schema": - return [[write([todo("Invalid", "invented")], "invalid")]] - plan = [todo("Check departure weather"), todo("Check destination elevation"), - todo("Check runway"), todo("Assess mountain conditions")] - final = [todo(row["content"], "completed" if i < 3 else "in_progress") for i, row in enumerate(plan)] - lookup = {"name": "lookup_field_elevation", "args": {"airport": "KASE"}, - "id": identity + "-elevation", "type": "tool_call"} - return [[write(plan, "initial")], [lookup], [write(final, "revision")]] - - -async def main(): - model = LocalModel() - scenario = "normal" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as network: - with redirect_stdout(sys.stderr): - graph = build(model) - while line := sys.stdin.buffer.readline(MAX_REQUEST + 1): - request = {} - try: - if len(line) > MAX_REQUEST or not line.endswith(b"\n"): - raise ValueError("Oversized request") - request = json.loads(line) - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - if "checkpoint" in request: - requested_checkpoint = request["checkpoint"] - if (not isinstance(requested_checkpoint, dict) - or requested_checkpoint.get("thread_id") != config["configurable"]["thread_id"] - or set(requested_checkpoint) - {"thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map"}): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - config["configurable"].update({k: v for k, v in requested_checkpoint.items() if k != "thread_id"}) - op = request["op"] - if op == "configure": - scenario = request["scenario"] - if scenario not in ("normal", "empty", "duplicates", "parallel", "recovery", "schema", "no-write", "oversized", "empty-content", "long-content"): - raise ValueError("Unsupported scenario") - result = {} - elif op == "state": - result = {"state": checkpoint(await graph.aget_state(config))} - elif op == "history": - result = {"history": [checkpoint(s) async for s in graph.aget_state_history(config)]} - elif op == "submit": - identity = request["messages"][0]["id"] - model.responses = responses(batches(scenario, identity), identity) - model.cursor = 0 - events = [] - async for kind, event in graph.astream( - {"messages": request["messages"]}, config, - stream_mode=["messages", "values", "updates", "checkpoints"]): - events.append([kind, deepcopy(event)]) - result = {"events": events, "state": checkpoint(await graph.aget_state(config))} - else: - raise ValueError("Unsupported operation") - network.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, "op": op, - "threadId": request.get("threadId"), "scenario": scenario, - "sourceSha256": {p: hashlib.sha256((PROJECT / p).read_bytes()).hexdigest() for p in SOURCES}, - "networkConnectAttempts": network.call_count, - } - result["requestId"] = request["requestId"] - wire = json.dumps(result, default=encode) - if len(wire.encode()) > MAX_RESPONSE: - raise ValueError("Oversized response") - print(wire, flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request.get("requestId"), "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 5774d5d55..a7be7a201 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -19,7 +19,6 @@ import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; -import { createDeepAgentsPlanningFixture } from './deep-agents-planning-fixture.mjs'; import { createDeepAgentsMemoryFixture } from './deep-agents-memory-fixture.mjs'; import { createDeepAgentsSkillsFixture } from './deep-agents-skills-fixture.mjs'; @@ -57,7 +56,6 @@ const nativeMastraFixture = configuration.library === 'runtimes' && configuratio const deepAgentsMemoryFixture = configuration.library === 'deep-agents' && configuration.topic === 'memory' ? createDeepAgentsMemoryFixture() : null; const deepAgentsSkillsFixture = configuration.library === 'deep-agents' && configuration.topic === 'skills' ? createDeepAgentsSkillsFixture() : null; const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && configuration.topic === 'subagents' ? createDeepAgentsSubagentsFixture() : null; -const deepAgentsPlanningFixture = configuration.library === 'deep-agents' && configuration.topic === 'planning' ? createDeepAgentsPlanningFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by // the Playwright global setup); this server proxies /api to it. @@ -181,7 +179,6 @@ const server = createServer(async (request, response) => { if (deepAgentsMemoryFixture && (await deepAgentsMemoryFixture(request, response, pathname))) return; if (deepAgentsSkillsFixture && (await deepAgentsSkillsFixture(request, response, pathname))) return; if (deepAgentsSubagentsFixture && (await deepAgentsSubagentsFixture(request, response, pathname))) return; - if (deepAgentsPlanningFixture && (await deepAgentsPlanningFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { if (request.method !== 'POST') return json(response, {}, 405); try { @@ -284,7 +281,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || deepAgentsPlanningFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -297,7 +294,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? deepAgentsPlanningFixture ?? chatGenerativeUiFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 8b61d4339b9ea8801c3ae711bd2c72c116993fb2 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:01:28 -0800 Subject: [PATCH 17/41] test(cockpit): run React and Angular deep-agents/memory e2e and website preview on aimock The React authored memory fixture also backed the Angular memory e2e. Both now replay shared aimock fixtures into a real `langgraph dev` with a real store behind the recording proxy. Store-read failures inside the graph (fail-initial/model/terminal) become a proxy stream cut (/__stream/cut, injected); oversize stays a genuine failure. Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- .../memory/angular/e2e/da-memory.spec.ts | 71 +- .../angular/e2e/fixtures/da-memory.json | 120 +++ .../memory/angular/e2e/global-setup-impl.ts | 88 +-- .../memory/angular/e2e/lifecycle.spec.mjs | 83 --- .../memory/angular/e2e/owned-lifecycle.mjs | 73 -- .../memory/angular/e2e/playwright.config.ts | 2 +- .../deep-agents/memory/angular/project.json | 6 - .../memory/react/e2e/da-memory.spec.ts | 109 ++- .../memory/react/e2e/global-setup-impl.ts | 6 + .../memory/react/e2e/playwright.config.ts | 3 + cockpit/deep-agents/memory/react/project.json | 24 +- scripts/cockpit-frontend-matrix.spec.mjs | 7 +- scripts/examples/e2e-wiring.spec.ts | 302 +------- scripts/react-cockpit/configuration.mjs | 1 + .../deep-agents-memory-configuration.spec.mjs | 149 ---- .../deep-agents-memory-fixture.mjs | 649 ----------------- .../deep-agents-memory-fixture.spec.mjs | 684 ------------------ .../react-cockpit/deep-agents-memory-wire.py | 180 ----- scripts/react-cockpit/langgraph-proxy.mjs | 26 +- scripts/react-cockpit/serve.mjs | 7 +- 21 files changed, 331 insertions(+), 2261 deletions(-) delete mode 100644 cockpit/deep-agents/memory/angular/e2e/lifecycle.spec.mjs delete mode 100644 cockpit/deep-agents/memory/angular/e2e/owned-lifecycle.mjs create mode 100644 cockpit/deep-agents/memory/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-memory-fixture.mjs delete mode 100644 scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-memory-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index e37a57f22..43af40654 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -507,7 +507,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-deep-agents-memory-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent', + command: 'node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4632', reuseExistingServer, diff --git a/cockpit/deep-agents/memory/angular/e2e/da-memory.spec.ts b/cockpit/deep-agents/memory/angular/e2e/da-memory.spec.ts index e232299e5..e78505e47 100644 --- a/cockpit/deep-agents/memory/angular/e2e/da-memory.spec.ts +++ b/cockpit/deep-agents/memory/angular/e2e/da-memory.spec.ts @@ -3,16 +3,43 @@ import { mkdirSync, writeFileSync } from 'node:fs'; const backend = 'http://localhost:5313'; const key = 'threadplane.deep-agents-memory.scope.v1'; async function records(request: APIRequestContext) { return (await request.get(backend + '/__requests')).json(); } -async function configure(request: APIRequestContext, scenario: string) { - expect((await request.post(backend + '/__configure', { data: { scenario, holdStream: false, holdState: false, holdCheckpoint: false, failCreation: false, failStream: false, failCheckpoint: false } })).ok()).toBe(true); +// Backend: aimock replays ./fixtures (shared with the React example; scenarios +// are tagged prompts) into a real `langgraph dev` with a real store, behind the +// recording proxy (global-setup-impl.ts, scripts/react-cockpit/langgraph-proxy.mjs). +// The fail-* scenarios stand in for a store read failing inside the graph at +// one observation phase, which aimock can't cause: the browser's stream ends +// with the graph's own error just before that phase's memory event (injected; +// cockpit/deep-agents/memory/python/tests covers the real failure). +const failures: Record = { + 'fail-initial': ['recall', 'initial'], 'fail-model': ['recall', 'model'], 'fail-terminal': ['teach', 'terminal'], +}; +let scenario = 'teach'; +async function configure(request: APIRequestContext, next: string) { + const failure = failures[next]; + scenario = failure ? failure[0] : next; + if (failure) expect((await request.post(backend + '/__stream/cut', { data: { + beforePattern: `"phase": ?"${failure[1]}"`, + error: { error: 'MemoryObservationError', message: 'Memory observation unavailable' }, + } })).ok()).toBe(true); } -async function send(page: Page, text: string) { await page.getByRole('textbox', { name: /message|prompt/i }).fill(text); await page.getByRole('button', { name: /send message/i }).click(); } +async function send(page: Page, text: string) { + // The scenario tag selects the scripted model turns (shared fixtures). + await page.getByRole('textbox', { name: /message|prompt/i }).fill(`${text} [scenario:${scenario}]`); + await page.getByRole('button', { name: /send message/i }).click(); +} +/** Server-sent events as the real server framed them (CRLF or LF). */ +function events(text: string) { + return text.split(/\r?\n\r?\n/).filter(Boolean).map(block => { + const lines = block.split(/\r?\n/); + return { event: lines.find(line => line.startsWith('event: '))!.slice(7), data: JSON.parse(lines.find(line => line.startsWith('data: '))!.slice(6)) }; + }); +} +test.beforeEach(async ({ request }) => { await request.post(backend + '/__reset'); scenario = 'teach'; }); test.afterEach(async ({ request }, info) => { mkdirSync(info.outputDir, { recursive: true }); - for (const endpoint of ['requests', 'raw-events', 'graph-proof']) { + for (const endpoint of ['requests', 'captures']) { const data = await (await request.get(backend + '/__' + endpoint)).json(); writeFileSync(info.outputPath(endpoint + '.json'), JSON.stringify(data, null, 2)); - if (endpoint === 'graph-proof') for (const proof of data) { expect(proof.actualCompiledGraph).toBe(true); expect(proof.networkConnectAttempts).toBe(0); } } }); test('canonical Angular auto-send suggestion, provider-owned followup and scoped reload recall use real store', async ({ page, request }) => { @@ -20,26 +47,30 @@ test('canonical Angular auto-send suggestion, provider-owned followup and scoped expect(await records(request)).toEqual(before); const scope = await page.evaluate(k => localStorage.getItem(k), key); // The older Angular suggestion explicitly sends; React suggestions are drafts. + // Its recorded prompt replays the recorded model turns (ls, then write_file). await page.getByText('Tell it about your operation', { exact: true }).click(); - await expect(page.getByTestId('memory-line').first()).toHaveText('Fleet note', { timeout: 45000 }); + await expect(page.getByTestId('memory-line').first()).toHaveText('## Crew notes', { timeout: 45000 }); await expect(page.getByTestId('memory-source')).toHaveAttribute('data-source', 'live'); // Angular retains trimmed nonblank lines rather than React's literal pre. - await expect(page.getByTestId('memory-line')).toHaveText(['Fleet note', 'Ω literal whitespace']); + await expect(page.getByTestId('memory-line')).toHaveText(['## Crew notes', '- Aircraft: Citation CJ3', '- Home base: KASE', '- Briefing preference: Always provide briefings in bullet points']); await expect(page.getByRole('button', { name: /send message/i })).toBeVisible(); - await configure(request, 'edit'); await send(page, 'Edit the fleet note'); - await expect(page.getByTestId('memory-line').first()).toHaveText('Updated fleet note', { timeout: 45000 }); + await configure(request, 'edit-crew'); await send(page, 'Edit the crew notes'); + await expect(page.getByTestId('memory-line').first()).toHaveText('## Updated crew notes', { timeout: 45000 }); let own = (await records(request)).slice(before.length); const authored = own.filter((r: any) => r.path.endsWith('/runs/stream')); expect(authored).toHaveLength(2); expect(authored[0].path).toBe(authored[1].path); expect(own.filter((r: any) => r.path === '/api/threads' && r.method === 'POST')).toHaveLength(1); await page.reload(); expect(await page.evaluate(k => localStorage.getItem(k), key)).toBe(scope); - await configure(request, 'recall'); await send(page, 'Recall the fleet note'); - await expect(page.getByTestId('memory-line').first()).toHaveText('Updated fleet note', { timeout: 45000 }); + await configure(request, 'recall'); await send(page, 'Recall the crew notes'); + await expect(page.getByTestId('memory-line').first()).toHaveText('## Updated crew notes', { timeout: 45000 }); own = (await records(request)).slice(before.length); const runs = own.filter((r: any) => r.path.endsWith('/runs/stream')); expect(runs).toHaveLength(3); expect(runs[2].path).not.toBe(runs[0].path); for (const run of runs) expect(run.body.context).toEqual({ memory_scope: scope }); - const raw = await (await request.get(backend + '/__raw-events')).json(); - expect(raw.at(-1).modelPrompts.some((p: string) => p.includes('Updated fleet note'))).toBe(true); - expect(raw.some((r: any) => r.storeOperations.some((op: any) => op.type === 'PutOp'))).toBe(true); + // The recall reply is scripted per system prompt (shared fixtures): it names + // the notes only when the graph put the stored memory into the model prompt. + await expect(page.getByText('Offline prompt observation: Updated crew notes')).toBeVisible(); + // The writes landed in the real store, under this browser's scope. + const stored = await (await request.post(backend + '/store/items/search', { data: { namespace_prefix: ['cockpit', 'deep-agents-memory', 'browser-v1', scope] } })).json(); + expect(stored.items.length).toBeGreaterThan(0); await configure(request, 'empty'); await send(page, 'Empty the current store'); // Count zero also occurs during custom-event reset. Establish the actual // preceding author's terminal empty observation and provider settlement @@ -47,13 +78,13 @@ test('canonical Angular auto-send suggestion, provider-owned followup and scoped await expect.poll(async () => (await records(request)).slice(before.length).filter((r: any) => r.path.endsWith('/runs/stream')).length).toBe(4); const emptyRun = (await records(request)).slice(before.length).filter((r: any) => r.path.endsWith('/runs/stream')).at(-1); expect(emptyRun.path).toBe(runs[2].path); - expect(emptyRun.body.input.messages[0].content).toBe('Empty the current store'); + expect(emptyRun.body.input.messages[0].content).toBe('Empty the current store [scenario:empty]'); expect(emptyRun.body.context).toEqual({ memory_scope: scope }); await expect.poll(async () => { - const actual = (await (await request.get(backend + '/__raw-events')).json()).at(-1); - if (actual.threadId !== emptyRun.path.split('/')[3]) return undefined; - const events = actual.events; - return events.filter((event: any) => event[1] === 'custom' && event[2].data.phase === 'terminal').at(-1)?.[2].data.memory_contents; + // What the real server streamed for that run, as the proxy received it. + const actual = (await (await request.get(backend + '/__captures')).json()).at(-1); + if (!actual?.path.includes(`/threads/${emptyRun.path.split('/')[3]}/`)) return undefined; + return events(actual.sse).filter(event => event.event === 'custom' && event.data.data.phase === 'terminal').at(-1)?.data.data.memory_contents; }).toEqual({ '/memories/AGENTS.md': '' }); await expect.poll(() => page.evaluate(() => { const debug = (window as unknown as { ng: { getComponent(element: Element): { agent: { isLoading(): boolean } } } }).ng; @@ -70,7 +101,7 @@ test('canonical Angular auto-send suggestion, provider-owned followup and scoped debug.getComponent(document.querySelector('app-da-memory')!).agent.switchThread(thread); }, firstThread); await expect(page.getByTestId('memory-source')).toHaveAttribute('data-source', 'checkpoint'); - await expect(page.getByTestId('memory-line').first()).toHaveText('Updated fleet note'); + await expect(page.getByTestId('memory-line').first()).toHaveText('## Updated crew notes'); const restoredReads = (await records(request)).slice(beforeRestore.length); expect(restoredReads.length).toBeGreaterThan(0); expect(restoredReads.every((r: any) => !r.path.endsWith('/runs/stream') && !(r.path === '/api/threads' && r.method === 'POST'))).toBe(true); diff --git a/cockpit/deep-agents/memory/angular/e2e/fixtures/da-memory.json b/cockpit/deep-agents/memory/angular/e2e/fixtures/da-memory.json index b2b0de1ae..f0f16bc9e 100644 --- a/cockpit/deep-agents/memory/angular/e2e/fixtures/da-memory.json +++ b/cockpit/deep-agents/memory/angular/e2e/fixtures/da-memory.json @@ -49,6 +49,126 @@ "response": { "content": "Here’s what I have in my notes about your operation:\n\n- Aircraft: Citation CJ3\n- Home base: KASE (Aspen)\n- Briefing preference: Always provide briefings in bullet points\n\nIf anything has changed or you’d like to add more details (like crew names, typical routes, or special procedures), just let me know!" } + }, + { + "match": { "toolCallId": "mem-teach-write" }, + "response": { "content": "Saved the fleet note." } + }, + { + "match": { "toolCallId": "mem-edit-edit" }, + "response": { "content": "Updated the fleet note." } + }, + { + "match": { "toolCallId": "mem-empty-write" }, + "response": { "content": "Emptied the memory file." } + }, + { + "match": { "toolCallId": "mem-oversize-write" }, + "response": { "content": "Wrote the oversized note." } + }, + { + "match": { "toolCallId": "mem-edit-crew-edit" }, + "response": { "content": "Updated the crew notes." } + }, + { + "match": { + "userMessage": "[scenario:teach]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/memories/AGENTS.md\", \"content\": \"Fleet note\\n\\n Ω literal whitespace \\n\"}", + "id": "mem-teach-write" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:edit]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "edit_file", + "arguments": "{\"file_path\": \"/memories/AGENTS.md\", \"old_string\": \"Fleet note\", \"new_string\": \"Updated fleet note\"}", + "id": "mem-edit-edit" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:empty]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/memories/AGENTS.md\", \"content\": \"\"}", + "id": "mem-empty-write" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:oversize]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "write_file", + "arguments": "{\"file_path\": \"/memories/AGENTS.md\", \"content\": \"ΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩΩ\"}", + "id": "mem-oversize-write" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:edit-crew]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "edit_file", + "arguments": "{\"file_path\": \"/memories/AGENTS.md\", \"old_string\": \"## Crew notes\", \"new_string\": \"## Updated crew notes\"}", + "id": "mem-edit-crew-edit" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:recall]", + "systemMessage": "Updated fleet note" + }, + "response": { "content": "Offline prompt observation: Updated fleet note" } + }, + { + "match": { + "userMessage": "[scenario:recall]", + "systemMessage": "Fleet note" + }, + "response": { "content": "Offline prompt observation: Fleet note" } + }, + { + "match": { + "userMessage": "[scenario:recall]", + "systemMessage": "Updated crew notes" + }, + "response": { "content": "Offline prompt observation: Updated crew notes" } + }, + { + "match": { "userMessage": "[scenario:recall]" }, + "response": { "content": "Offline prompt observation: no memory recalled." } } ] } diff --git a/cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts b/cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts index 71ca3d408..e821ed6ec 100644 --- a/cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts +++ b/cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts @@ -1,54 +1,46 @@ -import { createServer, get } from 'node:http'; -import { createServer as probe } from 'node:net'; -import { spawn } from 'node:child_process'; +import { createServer } from 'node:http'; import { once } from 'node:events'; -import { mkdirSync, openSync, closeSync } from 'node:fs'; import { resolve } from 'node:path'; -import { createOwnedProcessGroup, settleOwnedCleanup } from './owned-lifecycle.mjs'; -// Capability-local transport: production proxy strips /api before forwarding. -// Fixture controls remain direct loopback endpoints and never enter public Code. +import { portsFor } from '../../../../../cockpit/ports.mjs'; +import { createGlobalSetup } from '@threadplane-internal/e2e-harness'; +// eslint-disable-next-line @nx/enforce-module-boundaries -- Local e2e proxy, never bundled. +import { createLanggraphProxy } from '../../../../../scripts/react-cockpit/langgraph-proxy.mjs'; + +const ports = portsFor('cockpit-deep-agents-memory-angular'); +// aimock replays the fixtures shared with the React example into a real +// `langgraph dev` (with a real store) on an upstream port. The recording proxy +// sits where the Angular dev server's /api proxy points (proxy.conf.mjs), so +// specs can read /__requests and /__captures and arm one-shot faults. +const upstreamPort = ports.langgraph + 1000; +const harness = createGlobalSetup({ + langgraphCwd: 'cockpit/deep-agents/memory/python', + langgraphPort: upstreamPort, + angularProject: 'cockpit-deep-agents-memory-angular', + angularPort: ports.angular, + fixturesDir: resolve(__dirname, 'fixtures'), +}); + export default async function setup() { - const root = resolve(__dirname, '../../../../..'); - for (const port of [4313, 5313]) { - const listener = probe(); listener.listen(port); - await once(listener, 'listening'); await new Promise(r => listener.close(() => r())); - } - const { createDeepAgentsMemoryFixture } = await import('../../../../../scripts/react-cockpit/deep-agents-memory-fixture.mjs'); - const fixture = createDeepAgentsMemoryFixture(); - const backend = createServer(async (req, res) => { - const path = new URL(req.url!, 'http://localhost:5313').pathname; + const proxy = createLanggraphProxy({ upstream: `http://127.0.0.1:${upstreamPort}` }); + // The Angular dev proxy strips /api; the recording proxy expects it. + const server = createServer(async (req, res) => { + const path = new URL(req.url ?? '/', 'http://localhost').pathname; try { - if (!await fixture(req, res, path.startsWith('/threads') ? '/api' + path : path)) { res.writeHead(404); res.end(); } - } catch { res.writeHead(500); res.end(); } - }); - let child: ReturnType | undefined; - let group: ReturnType | undefined; - let log: number | undefined; - let closing: Promise | undefined; - const cleanup = () => closing ??= settleOwnedCleanup([ - () => fixture.close(), - () => new Promise((done, reject) => { backend.closeAllConnections(); backend.close(error => error ? reject(error) : done()); }), - async () => { const result = await group?.close(); if (result) console.log('Memory owned group closed: ' + JSON.stringify(result)); }, - () => { if (log !== undefined) { closeSync(log); log = undefined; } }, - ]); - try { - backend.listen(5313); await once(backend, 'listening'); - mkdirSync('/tmp/threadplane-memory-task4b-angular', { recursive: true }); - log = openSync('/tmp/threadplane-memory-task4b-angular/serve.log', 'a', 0o600); - child = spawn(process.execPath, [resolve(root, 'node_modules/nx/bin/nx.js'), 'serve', 'cockpit-deep-agents-memory-angular', '--configuration=cockpit', '--port=4313', '--host=localhost'], { cwd: root, detached: true, stdio: ['ignore', log, log], env: { ...process.env, NX_DAEMON: 'false' } }); - group = createOwnedProcessGroup(child); - for (let n = 0; n < 360; n++) { - if (child.exitCode !== null) throw Error('Owned Angular serve failed; see private serve.log'); - const ready = await Promise.all(['http://[::1]:4313/', 'http://127.0.0.1:4313/'].map(url => new Promise(resolveReady => { - const req = get(url, response => { response.resume(); resolveReady(response.statusCode === 200); }); - req.setTimeout(2000, () => req.destroy()); req.on('error', () => resolveReady(false)); - }))); - if (ready.some(Boolean)) { console.log('Memory Angular and compiled backend ready.'); return cleanup; } - await new Promise(r => setTimeout(r, 500)); + if (!(await proxy(req, res, path.startsWith('/__') ? path : '/api' + path))) { + res.writeHead(404); + res.end(); + } + } catch { + if (!res.headersSent) res.writeHead(500); + res.end(); } - throw Error('Owned Angular serve readiness timed out'); - } catch (error) { - try { await cleanup(); } catch (cleanupError) { throw new AggregateError([error, cleanupError], 'Memory startup and cleanup failed'); } - throw error; - } + }); + server.listen(ports.langgraph); + await once(server, 'listening'); + await harness(); + return () => + new Promise((done) => { + server.closeAllConnections(); + server.close(() => done()); + }); } diff --git a/cockpit/deep-agents/memory/angular/e2e/lifecycle.spec.mjs b/cockpit/deep-agents/memory/angular/e2e/lifecycle.spec.mjs deleted file mode 100644 index dbfec530e..000000000 --- a/cockpit/deep-agents/memory/angular/e2e/lifecycle.spec.mjs +++ /dev/null @@ -1,83 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { spawn, execFileSync } from 'node:child_process'; -import { once } from 'node:events'; -import { createServer } from 'node:net'; -import { mkdtempSync, writeFileSync, openSync, closeSync, fstatSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; -import { createOwnedProcessGroup, settleOwnedCleanup } from './owned-lifecycle.mjs'; -const captures = mkdtempSync(join(tmpdir(), 'threadplane-memory-task4b-v2-lifecycle-')); -console.log('Memory lifecycle captures: ' + captures); -const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -async function until(check, timeout = 3000) { - const end = Date.now() + timeout; - while (!check()) { if (Date.now() > end) throw Error('Owned process condition timed out'); await sleep(20); } -} -function group(pid) { - try { return Number(execFileSync('ps', ['-p', String(pid), '-o', 'pgid='], { encoding: 'utf8' }).trim()); } catch { return undefined; } -} -async function bindable(port) { - const listener = createServer(); - try { listener.listen(port, '127.0.0.1'); await once(listener, 'listening'); await new Promise(resolve => listener.close(resolve)); return true; } catch { listener.close(); return false; } -} -async function owned(t, stubborn = false) { - const descendant = `const net=require('node:net');${stubborn ? "process.on('SIGTERM',()=>{});" : ''}const server=net.createServer();server.listen(0,'127.0.0.1',()=>process.send({pid:process.pid,port:server.address().port}));`; - const leader = spawn(process.execPath, ['-e', `const {spawn}=require('node:child_process');const child=spawn(process.execPath,['-e',${JSON.stringify(descendant)}],{stdio:['ignore','ignore','ignore','ipc']});child.once('message',value=>process.send(value));process.on('message',()=>{child.disconnect();child.unref();process.disconnect();process.exit(1);});`], { detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }); - const exited = once(leader, 'exit'); - const [member] = await once(leader, 'message'); - assert.equal(group(member.pid), leader.pid); - t.after(async () => { - // Only this freshly spawned, observed group's descendant can authorize - // fallback cleanup when a regression deliberately leaves it alive. - if (group(member.pid) === leader.pid) { try { process.kill(-leader.pid, 'SIGKILL'); } catch (error) { if (error.code !== 'ESRCH') throw error; } } - if (leader.exitCode === null && leader.signalCode === null) leader.kill('SIGKILL'); - await exited; - await until(() => group(member.pid) === undefined); - assert.equal(await bindable(member.port), true); - }); - return { leader, exited, member }; -} -test('cleanup terminates an owned surviving descendant after its detached leader exits', async t => { - const f = await owned(t); - const lease = createOwnedProcessGroup(f.leader, { graceMs: 100, killMs: 1000 }); - await until(() => lease.members().includes(f.member.pid)); - f.leader.send('exit'); await f.exited; - assert.equal(f.leader.exitCode, 1); - assert.equal(group(f.member.pid), f.leader.pid); - const closure = await lease.close(); - const receipt = { leaderPid: f.leader.pid, descendantPid: f.member.pid, port: f.member.port, leaderExitCode: f.leader.exitCode, descendantGroupAfterCleanup: group(f.member.pid) ?? null, listenerBindable: await bindable(f.member.port), closure }; - writeFileSync(join(captures, 'exited-leader.json'), JSON.stringify(receipt, null, 2)); - assert.equal(receipt.descendantGroupAfterCleanup, null); - assert.equal(receipt.listenerBindable, true); -}); -test('running leader cleanup escalates only its SIGTERM-ignoring descendant and is idempotent', async t => { - const f = await owned(t, true); - const lease = createOwnedProcessGroup(f.leader, { graceMs: 100, killMs: 1000 }); - const first = lease.close(); assert.equal(lease.close(), first); - const closure = await first; - assert.deepEqual(closure.signals, ['SIGTERM', 'SIGKILL']); - assert.deepEqual(closure.remainingPids, []); - assert.equal(group(f.member.pid), undefined); - assert.equal(await bindable(f.member.port), true); - writeFileSync(join(captures, 'running-leader-stubborn-descendant.json'), JSON.stringify({ leaderPid: f.leader.pid, descendantPid: f.member.pid, port: f.member.port, closure, listenerBindable: true }, null, 2)); -}); -test('resource close failure still closes the real owned group, backend listener and descriptor', async t => { - const f = await owned(t); - const lease = createOwnedProcessGroup(f.leader, { graceMs: 100, killMs: 1000 }); - const backend = createServer(); backend.listen(0, '127.0.0.1'); await once(backend, 'listening'); - const port = backend.address().port; - const descriptor = openSync(join(captures, 'owned-log.txt'), 'w'); - t.after(() => { backend.close(); try { closeSync(descriptor); } catch { /* verified closed */ } }); - await assert.rejects(settleOwnedCleanup([ - async () => { throw Error('Controlled first resource close failure'); }, - () => new Promise(resolve => backend.close(resolve)), - () => lease.close(), - () => closeSync(descriptor), - ]), AggregateError); - assert.equal(group(f.member.pid), undefined); - assert.equal(await bindable(port), true); - assert.equal(await bindable(f.member.port), true); - assert.throws(() => fstatSync(descriptor), { code: 'EBADF' }); - writeFileSync(join(captures, 'all-owned-resources-attempted.json'), JSON.stringify({ leaderPid: f.leader.pid, descendantPid: f.member.pid, descendantPort: f.member.port, backendPort: port, descendantAbsent: true, bothPortsBindable: true, descriptorClosed: true }, null, 2)); -}); diff --git a/cockpit/deep-agents/memory/angular/e2e/owned-lifecycle.mjs b/cockpit/deep-agents/memory/angular/e2e/owned-lifecycle.mjs deleted file mode 100644 index fd6855ab0..000000000 --- a/cockpit/deep-agents/memory/angular/e2e/owned-lifecycle.mjs +++ /dev/null @@ -1,73 +0,0 @@ -import { execFileSync } from 'node:child_process'; -const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); -function processes(groupId) { - return execFileSync('ps', ['-eo', 'pid=,ppid=,pgid=,stat=,lstart='], { encoding: 'utf8', timeout: 1000, maxBuffer: 1024 * 1024 }).split('\n').flatMap(line => { - const match = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.+?)\s*$/.exec(line); - return match && Number(match[3]) === groupId && !match[4].startsWith('Z') - ? [{ pid: Number(match[1]), parent: Number(match[2]), identity: match[1] + ':' + match[5] }] - : []; - }); -} -/** A lease for this freshly spawned detached group, never an arbitrary PGID. - * Observed process birth identities maintain ownership after leader exit. - * Once empty, ownership is permanently revoked to prevent PGID reuse kills. - * Unknown replacement membership fails closed instead of signalling it. */ -export function createOwnedProcessGroup(child, { graceMs = 1000, killMs = 1500 } = {}) { - const groupId = child.pid; - if (!Number.isInteger(groupId) || groupId <= 0) throw Error('Missing owned process group'); - const initial = processes(groupId); - if (!initial.some(member => member.pid === groupId && member.parent === process.pid)) throw Error('Fresh detached child ownership not established'); - const known = new Map(initial.map(member => [member.pid, member.identity])); - let current = initial, active = true, closing, scanError; - const ended = new Promise(resolve => child.once('close', resolve)); - const signals = []; - function refresh() { - if (!active) return []; - const found = processes(groupId); - if (!found.length) { current = []; active = false; return []; } - if (!found.some(member => known.get(member.pid) === member.identity)) { - active = false; - throw Error('Owned process group identity changed; signalling refused'); - } - for (const member of found) known.set(member.pid, member.identity); - current = found; - return found; - } - const monitor = setInterval(() => { try { refresh(); } catch (error) { scanError = error; } }, 25); - monitor.unref(); - async function waitForGroup(timeout) { - const deadline = Date.now() + timeout; - while (refresh().length) { - if (Date.now() >= deadline) return false; - await pause(25); - } - return true; - } - function signal(value) { - if (!refresh().length) return; - try { process.kill(-groupId, value); signals.push(value); } - catch (error) { if (error.code !== 'ESRCH') throw error; refresh(); } - } - return Object.freeze({ - members: () => refresh().map(member => member.pid), - close() { - return closing ??= (async () => { - clearInterval(monitor); - if (scanError) throw scanError; - signal('SIGTERM'); - if (!await waitForGroup(graceMs)) { - signal('SIGKILL'); - if (!await waitForGroup(killMs)) throw Error('Owned process group did not terminate within bounds'); - } - await ended; - return { groupId, ownedPids: [...known.keys()], signals, remainingPids: current.map(member => member.pid), leaderExitCode: child.exitCode, leaderSignalCode: child.signalCode }; - })(); - }, - }); -} -/** Every owned resource gets a close attempt even if another close rejects. */ -export async function settleOwnedCleanup(tasks) { - const results = await Promise.allSettled(tasks.map(async close => close())); - const failures = results.filter(result => result.status === 'rejected').map(result => result.reason); - if (failures.length) throw new AggregateError(failures, 'Owned Memory resources could not all close'); -} diff --git a/cockpit/deep-agents/memory/angular/e2e/playwright.config.ts b/cockpit/deep-agents/memory/angular/e2e/playwright.config.ts index f2c7bfd04..8dadfd0e0 100644 --- a/cockpit/deep-agents/memory/angular/e2e/playwright.config.ts +++ b/cockpit/deep-agents/memory/angular/e2e/playwright.config.ts @@ -3,7 +3,6 @@ import { portsFor } from '../../../../../cockpit/ports.mjs'; const { angular: angularPort } = portsFor('cockpit-deep-agents-memory-angular'); - export default defineConfig({ testDir: '.', testMatch: '**/*.spec.ts', @@ -17,4 +16,5 @@ export default defineConfig({ }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), }); diff --git a/cockpit/deep-agents/memory/angular/project.json b/cockpit/deep-agents/memory/angular/project.json index c3c1d6e92..14567f6f6 100644 --- a/cockpit/deep-agents/memory/angular/project.json +++ b/cockpit/deep-agents/memory/angular/project.json @@ -89,7 +89,6 @@ }, "e2e": { "executor": "@nx/playwright:playwright", - "dependsOn": ["lifecycle-test"], "options": { "config": "cockpit/deep-agents/memory/angular/e2e/playwright.config.ts" } @@ -99,11 +98,6 @@ "options": { "configFile": "cockpit/deep-agents/memory/angular/vite.config.mts" } - }, - "lifecycle-test": { - "executor": "nx:run-commands", - "cache": false, - "options": { "command": "node --test cockpit/deep-agents/memory/angular/e2e/lifecycle.spec.mjs" } } }, "tags": [ diff --git a/cockpit/deep-agents/memory/react/e2e/da-memory.spec.ts b/cockpit/deep-agents/memory/react/e2e/da-memory.spec.ts index 7d97867da..f28c990d6 100644 --- a/cockpit/deep-agents/memory/react/e2e/da-memory.spec.ts +++ b/cockpit/deep-agents/memory/react/e2e/da-memory.spec.ts @@ -7,34 +7,67 @@ const updated = 'Updated fleet note\n\n Ω literal whitespace \n'; const file = '/memories/AGENTS.md'; type Recorded = { path: string; method: string; body: Record }; async function records(request: APIRequestContext): Promise { return (await request.get('/__requests')).json(); } -async function configure(request: APIRequestContext, scenario = 'teach', extra: Record = {}) { - expect((await request.post('/__configure', { data: { scenario, holdStream: false, holdState: false, holdCheckpoint: false, failCreation: false, failStream: false, failCheckpoint: false, ...extra } })).ok()).toBe(true); +// Backend: aimock replays cockpit/deep-agents/memory/angular/e2e/fixtures +// (shared with the Angular example; React scenarios are tagged prompts) into a +// real `langgraph dev` with a real store. The proxy records requests and holds +// or fails the next call (scripts/react-cockpit/langgraph-proxy.mjs). +// The fail-* scenarios stand in for a store read failing inside the graph at +// one observation phase, which aimock can't cause: the browser's stream ends +// with the graph's own error just before that phase's memory event (injected; +// cockpit/deep-agents/memory/python/tests covers the real failure). +const failures: Record = { + 'fail-initial': ['recall', 'initial'], 'fail-model': ['recall', 'model'], 'fail-terminal': ['teach', 'terminal'], +}; +let scenario = 'teach'; +async function configure(request: APIRequestContext, next = 'teach', extra: Record = {}) { + const control = async (path: string, data: object = {}) => expect((await request.post(path, { data })).ok()).toBe(true); + const failure = failures[next]; + scenario = failure ? failure[0] : next; + if (failure) await control('/__stream/cut', { + beforePattern: `"phase": ?"${failure[1]}"`, + error: { error: 'MemoryObservationError', message: 'Memory observation unavailable' }, + }); + // The saved-conversation confirmation reads the exact checkpoint. + if (extra.holdCheckpoint) await control('/__hold/state'); + if (extra.holdStream) await control('/__hold/stream', { immediate: true }); + if (extra.failCreation) await control('/__fail-create'); + await control(extra.failCheckpoint ? '/__state/failure' : '/__state/safe'); +} +async function release(request: APIRequestContext) { + await request.post('/__release-state'); + await request.post('/__release'); +} +/** Server-sent events as the real server framed them (CRLF or LF). */ +function events(text: string) { + return text.split(/\r?\n\r?\n/).filter(Boolean).map(block => { + const lines = block.split(/\r?\n/); + return { event: lines.find(line => line.startsWith('event: '))!.slice(7), data: JSON.parse(lines.find(line => line.startsWith('data: '))!.slice(6)) }; + }); } async function send(page: Page, text = 'Remember this fleet note') { - await page.getByRole('textbox', { name: 'Message' }).fill(text); + // The scenario tag selects the scripted model turns (shared fixtures). + await page.getByRole('textbox', { name: 'Message' }).fill(`${text} [scenario:${scenario}]`); await page.getByRole('button', { name: 'Send', exact: true }).click(); } async function saved(page: Page) { await expect(page.getByRole('status')).toHaveText('Response saved.', { timeout: 45000 }); } async function capture(request: APIRequestContext, info: TestInfo) { mkdirSync(info.outputDir, { recursive: true }); - for (const endpoint of ['requests', 'graph-proof', 'raw-events']) { + for (const endpoint of ['requests', 'captures']) { const data = await (await request.get('/__' + endpoint)).json(); writeFileSync(info.outputPath(endpoint + '.json'), JSON.stringify(data, null, 2)); - if (endpoint === 'graph-proof') for (const proof of data) { - expect(proof.actualCompiledGraph).toBe(true); - expect(proof.networkConnectAttempts).toBe(0); - expect(Object.keys(proof.sourceSha256)).toHaveLength(6); - } } // Native snapshots are verified inside application confirmation; no extraction endpoint exists. writeFileSync(info.outputPath('native-evidence.json'), JSON.stringify({ nativeSnapshotsExtracted: false, evidence: 'Actual native app Saved plus independent current/newest/exact reads.' })); } +test.beforeEach(async ({ request }) => { await request.post('/__reset'); scenario = 'teach'; }); test.afterEach(async ({ request }, info) => { await capture(request, info); }); test('literal genuine teach, same-thread edit, new-thread scoped prompt recall and historical read-only restore', async ({ page, request }, info) => { - const streams: Promise<{ url: string; status: number; contentLocation: string | undefined; text: string }>[] = []; + // Chromium doesn't keep a long-lived event-stream body for response.text(); + // the stream's content is read from the proxy's capture of the real server. + const streams: Promise<{ url: string; status: number; contentLocation: string | undefined }>[] = []; page.on('response', response => { if (new URL(response.url()).pathname.endsWith('/runs/stream')) streams.push((async () => ({ - url: response.url(), status: response.status(), contentLocation: (await response.allHeaders())['content-location'], text: await response.text(), + url: response.url(), status: response.status(), contentLocation: (await response.allHeaders())['content-location'], }))()); }); await configure(request, 'teach', { holdCheckpoint: true }); @@ -49,10 +82,11 @@ test('literal genuine teach, same-thread edit, new-thread scoped prompt recall a const scope = await page.evaluate(k => localStorage.getItem(k), key); expect(scope).toMatch(/^[0-9a-f-]{36}$/); await send(page); - await expect(page.getByRole('status')).toHaveText('Confirming saved conversation…'); + // A real `langgraph dev` turn (model, tool, model) can take a few seconds. + await expect(page.getByRole('status')).toHaveText('Confirming saved conversation…', { timeout: 30000 }); await expect(page.getByTestId('memory-source')).toHaveText('store observed during this run'); await expect(page.getByTestId('memory-file').locator('pre')).toHaveText(literal, { useInnerText: false }); - await request.post('/__release', { data: {} }); + await release(request); await saved(page); await configure(request, 'teach'); const first = await page.getByLabel('Saved conversations').inputValue(); @@ -69,33 +103,27 @@ test('literal genuine teach, same-thread edit, new-thread scoped prompt recall a const firstRequest = (await records(request)).slice(before.length).find(r => r.path.endsWith('/runs/stream'))!; const human = firstRequest.body.input.messages[0]; expect(exact.values.messages.filter((message: any) => message.type === 'human')).toEqual([expect.objectContaining(human)]); - const firstRaw = (await (await request.get('/__raw-events')).json()).find((record: any) => record.threadId === first); - const firstProofs = (await (await request.get('/__graph-proof')).json()).filter((proof: any) => proof.threadId === first && proof.op === 'submit'); + // What the real server streamed for this run, as the proxy received it. + const firstRaw = (await (await request.get('/__captures')).json()).find((record: any) => record.path.includes(`/threads/${first}/`)); + const rawEvents = events(firstRaw.sse); const sse = await streams[0]; expect(sse.status).toBe(200); - expect(sse.contentLocation).toBe(`/threads/${first}/runs/${firstRaw.runId}`); - const metadata = JSON.parse(sse.text.split('\n\n')[0].split('data: ')[1]); - expect(metadata.run_id).toBe(firstRaw.runId); - expect(sse.text).toContain('event: custom'); - const decodedEvents = sse.text.split('\n\n').filter(Boolean).map(block => { - const lines = block.split('\n'); - return { event: lines.find(line => line.startsWith('event: '))!.slice(7), data: JSON.parse(lines.find(line => line.startsWith('data: '))!.slice(6)) }; - }); - const terminalCustom = decodedEvents.filter(event => event.event === 'custom' && event.data.name === 'deep_agents.memory' && event.data.data.phase === 'terminal').at(-1)!; - const actualRawTerminal = firstRaw.events.filter((event: any) => event[0].length === 0 && event[1] === 'custom' && event[2].data.phase === 'terminal').at(-1)[2]; - const terminalValues = decodedEvents.filter(event => event.event === 'values').at(-1)!; - const actualRawValues = firstRaw.events.filter((event: any) => event[0].length === 0 && event[1] === 'values').at(-1)[2]; + const metadata = rawEvents[0].data; + expect(sse.contentLocation).toMatch(new RegExp(`/threads/${first}/runs/${metadata.run_id}$`)); + expect(firstRaw.sse).toContain('event: custom'); + const terminalCustom = rawEvents.filter(event => event.event === 'custom' && event.data.name === 'deep_agents.memory' && event.data.data.phase === 'terminal').at(-1)!; + const terminalValues = rawEvents.filter(event => event.event === 'values').at(-1)!; mkdirSync(info.outputDir, { recursive: true }); writeFileSync(info.outputPath('whole-read-proof.json'), JSON.stringify({ nativeSnapshotsExtracted: false, owner: { threadId: first, memoryScope: scope, actualSdkHuman: human }, readResponses: { current, newest, exact }, rootCheckpoint: current.checkpoint, - actualBrowserSse: { ...sse, representation: 'Decoded Playwright response.text(), not raw UTF-8 wire bytes', parsedEvents: decodedEvents }, actualRawGraphRun: firstRaw, actualGraphProofs: firstProofs, + actualBrowserResponse: sse, actualServerStream: { ...firstRaw, parsedEvents: rawEvents }, }, null, 2)); - expect(sse.text).toContain('Ω'); - expect(terminalCustom.data).toEqual(actualRawTerminal); + expect(firstRaw.sse).toContain('Ω'); expect(terminalCustom.data.data.memory_contents).toEqual({ [file]: literal }); - expect(terminalValues.data).toEqual(actualRawValues); + // Values events omit the private memory channels; the saved checkpoint has them. + expect(terminalValues.data.memory_contents).toBeUndefined(); expect(exact.values.memory_contents).toEqual({ [file]: literal }); const write = exact.values.messages.find((m: any) => m.type === 'ai' && m.tool_calls?.some((c: any) => c.name === 'write_file')); expect(write.tool_calls[0].args).toEqual({ file_path: file, content: literal }); @@ -119,10 +147,12 @@ test('literal genuine teach, same-thread edit, new-thread scoped prompt recall a const creates = (await records(request)).slice(before.length).filter(r => r.path === '/api/threads' && r.method === 'POST'); expect(creates).toHaveLength(2); for (const creation of creates) expect(creation.body.if_exists).toBe('raise'); - const raw = await (await request.get('/__raw-events')).json(); - const recall = raw.filter((r: any) => r.threadId === second).at(-1); - expect(recall.modelPrompts.some((p: string) => p.includes('Updated fleet note'))).toBe(true); - expect(raw.some((r: any) => r.storeOperations.some((op: any) => op.type === 'PutOp'))).toBe(true); + // The recall reply is scripted per system prompt (shared fixtures): it names + // the note only when the graph put the stored memory into the model prompt. + await expect(page.getByRole('region', { name: 'Conversation' })).toContainText('Offline prompt observation: Updated fleet note'); + // The write landed in the real store, under this browser's scope. + const stored = await (await request.post('/api/store/items/search', { data: { namespace_prefix: ['cockpit', 'deep-agents-memory', 'browser-v1', scope] } })).json(); + expect(stored.items.length).toBeGreaterThan(0); await page.getByRole('button', { name: 'New conversation' }).click(); await configure(request, 'empty'); await send(page, 'Empty the current store'); await saved(page); @@ -175,8 +205,7 @@ for (const scenario of ['fail-initial', 'fail-model', 'fail-terminal', 'oversize await page.getByRole('button', { name: 'New conversation' }).click(); await configure(request, 'recall'); await send(page, 'Recall the write despite failed terminal read'); await saved(page); await expect(page.getByTestId('memory-file').locator('pre')).toHaveText(literal, { useInnerText: false }); - const raw = await (await request.get('/__raw-events')).json(); - expect(raw.at(-1).modelPrompts.some((p: string) => p.includes('Fleet note'))).toBe(true); + await expect(page.getByRole('region', { name: 'Conversation' })).toContainText('Offline prompt observation: Fleet note'); } }); for (const mode of ['corrupt', 'unavailable'] as const) test(`storage ${mode} blocks creation`, async ({ page, request }) => { @@ -192,7 +221,7 @@ test('active captured scope settles with warning and later replacement blocks fu await other.goto(base); await other.evaluate(k => localStorage.setItem(k, crypto.randomUUID()), key); await other.close(); - await request.post('/__release', { data: {} }); + await release(request); await expect(page.getByTestId('memory-scope-warning')).toBeVisible(); await expect(page.getByTestId('memory-source')).toHaveText('confirmed checkpoint at read time'); const before = await records(request); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(page.getByRole('textbox')).toBeDisabled(); expect(await records(request)).toEqual(before); @@ -207,13 +236,13 @@ test('Stop and New invalidate held delivery and never replay', async ({ page, re await configure(request, 'teach', { holdStream: true }); await page.goto(base); const before = await records(request); await send(page); await expect(page.getByRole('button', { name: 'Stop', exact: true })).toBeVisible(); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.getByRole('textbox')).toBeDisabled(); - await page.getByRole('button', { name: 'New conversation' }).click(); await request.post('/__release', { data: {} }); + await page.getByRole('button', { name: 'New conversation' }).click(); await release(request); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(page.getByTestId('memory-file')).toHaveCount(0); expect((await records(request)).slice(before.length).filter(r => r.path.endsWith('/runs/stream'))).toHaveLength(1); }); test('held exact read cannot resurrect the old view after New', async ({ page, request }) => { await configure(request, 'teach', { holdCheckpoint: true }); await page.goto(base); await send(page); await expect(page.getByRole('status')).toHaveText('Confirming saved conversation…'); - await page.getByRole('button', { name: 'New conversation' }).click(); await request.post('/__release', { data: {} }); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(page.getByTestId('memory-file')).toHaveCount(0); await expect(page.getByLabel('Saved conversations').locator('option')).toHaveCount(1); + await page.getByRole('button', { name: 'New conversation' }).click(); await release(request); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(page.getByTestId('memory-file')).toHaveCount(0); await expect(page.getByLabel('Saved conversations').locator('option')).toHaveCount(1); }); for (const control of ['failCheckpoint', 'failCreation'] as const) test(`${control} remains unavailable without retries`, async ({ page, request }) => { await configure(request, 'teach', { [control]: true }); await page.goto(base); const before = await records(request); await send(page); await expect(page.getByRole('alert')).toBeVisible({ timeout: 45000 }); await expect(page.getByRole('textbox')).toBeDisabled(); diff --git a/cockpit/deep-agents/memory/react/e2e/global-setup-impl.ts b/cockpit/deep-agents/memory/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..188e9bdb1 --- /dev/null +++ b/cockpit/deep-agents/memory/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'deep-agents/memory', port: 4632 }); diff --git a/cockpit/deep-agents/memory/react/e2e/playwright.config.ts b/cockpit/deep-agents/memory/react/e2e/playwright.config.ts index 36fabf47b..c538e1772 100644 --- a/cockpit/deep-agents/memory/react/e2e/playwright.config.ts +++ b/cockpit/deep-agents/memory/react/e2e/playwright.config.ts @@ -4,5 +4,8 @@ export default defineConfig({ reporter: [['list']], workers: 1, fullyParallel: false, retries: 0, timeout: 60000, use: { baseURL: 'http://127.0.0.1:4632', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/deep-agents/memory/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent', cwd: '../../../../..', url: 'http://127.0.0.1:4632/__lifetime', reuseExistingServer: false, timeout: 30000 }, }); diff --git a/cockpit/deep-agents/memory/react/project.json b/cockpit/deep-agents/memory/react/project.json index 616ef60b4..3c2a9ca52 100644 --- a/cockpit/deep-agents/memory/react/project.json +++ b/cockpit/deep-agents/memory/react/project.json @@ -3,12 +3,14 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/deep-agents/memory/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-deep-agents-memory-angular", "cockpit-deep-agents-memory-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": [ { "target": "build", "projects": ["cockpit-deep-agents-memory-react"] }, { "target": "fixture-test", "projects": ["cockpit-deep-agents-memory-react"] } ], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": [ { "target": "build", "projects": ["cockpit-deep-agents-memory-react"] }, { "target": "test", "projects": ["cockpit-deep-agents-memory-python"] } ], "options": { "config": "cockpit/deep-agents/memory/react/e2e/playwright.config.ts" } }, "test": { @@ -39,26 +41,6 @@ "options": { "eslintConfig": "cockpit/deep-agents/memory/react/eslint.config.mjs" } - }, - "configuration-test": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs" - } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-deep-agents-memory-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs" - } } } } diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 876994179..a444d776a 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -48,7 +48,12 @@ test('Memory backend changes select its two actual registered frontends and exac test('Memory owns exact closed native root metadata without changing ordinary LangGraph Memory', async () => { const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); const config = reactCockpitConfiguration('deep-agents-memory'); - assert.deepEqual(config, { topic: 'memory', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/memory/react', base: '/deep-agents/memory/react/', port: 4632, project: 'cockpit-deep-agents-memory-react' }); + assert.deepEqual(config, { topic: 'memory', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/memory/react', base: '/deep-agents/memory/react/', port: 4632, project: 'cockpit-deep-agents-memory-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/deep-agents/memory/python', + fixtures: 'cockpit/deep-agents/memory/angular/e2e/fixtures', langgraphPort: 5632, + }, + }); assert.equal(Object.isFrozen(config), true); assert.equal(reactCockpitConfiguration('memory').appPath, 'cockpit/langgraph/memory/react'); }); diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index 4758a6662..2f6f8132c 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -1,5 +1,4 @@ import { existsSync, readdirSync, readFileSync } from 'node:fs'; -import * as ts from 'typescript'; function websiteBuildProjects(website: { targets: { @@ -72,287 +71,7 @@ import { capabilities } from '@threadplane/cockpit-registry'; // eslint-disable-next-line @nx/enforce-module-boundaries -- repo-root port registry is intentionally outside an Nx project. import { portsFor } from '../../cockpit/ports.mjs'; -function compiledMemoryBackend( - project: string | undefined, - setup: string, - fixture: string -): string | undefined { - if (project !== 'cockpit-deep-agents-memory-angular') return undefined; - const signature = (node: ts.Node): string => { - const shape = (child: ts.Node): unknown => { - const children: unknown[] = []; - ts.forEachChild(child, (nested) => { - children.push(shape(nested)); - }); - return [child.kind, children.length ? children : child.getText().trim()]; - }; - return JSON.stringify(shape(node)); - }; - const has = (nodes: readonly ts.Node[], expected: string) => { - const grammar = ts.createSourceFile( - 'grammar.ts', - expected, - ts.ScriptTarget.Latest, - true - ); - const target = signature(grammar.statements[0]); - return nodes.filter((node) => signature(node) === target).length === 1; - }; - const declaration = ( - body: ts.Block, - name: string - ): ts.VariableDeclaration | undefined => { - const matches = body.statements - .filter(ts.isVariableStatement) - .flatMap((statement) => [...statement.declarationList.declarations]) - .filter((node) => ts.isIdentifier(node.name) && node.name.text === name); - return matches.length === 1 ? matches[0] : undefined; - }; - const setupAst = ts.createSourceFile( - 'setup.ts', - setup, - ts.ScriptTarget.Latest, - true - ); - const owners = setupAst.statements.filter( - (node): node is ts.FunctionDeclaration => - ts.isFunctionDeclaration(node) && node.name?.text === 'setup' - ); - if ( - owners.length !== 1 || - !owners[0].body || - !owners[0].modifiers?.some( - (modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword - ) - ) - return undefined; - const body = owners[0].body; - const required = [ - "const { createDeepAgentsMemoryFixture } = await import('../../../../../scripts/react-cockpit/deep-agents-memory-fixture.mjs');", - 'const fixture = createDeepAgentsMemoryFixture();', - `const backend = createServer(async (req, res) => { - const path = new URL(req.url!, 'http://localhost:5313').pathname; - try { if (!await fixture(req, res, path.startsWith('/threads') ? '/api' + path : path)) { res.writeHead(404); res.end(); } } - catch { res.writeHead(500); res.end(); } - });`, - ]; - if (!required.every((grammar) => has(body.statements, grammar))) - return undefined; - const startup = body.statements.filter(ts.isTryStatement); - if ( - startup.length !== 1 || - !has(startup[0].tryBlock.statements, 'backend.listen(5313);') || - !has( - startup[0].tryBlock.statements, - "child = spawn(process.execPath, [resolve(root, 'node_modules/nx/bin/nx.js'), 'serve', 'cockpit-deep-agents-memory-angular', '--configuration=cockpit', '--port=4313', '--host=localhost'], { cwd: root, detached: true, stdio: ['ignore', log, log], env: { ...process.env, NX_DAEMON: 'false' } });" - ) - ) - return undefined; - // The actual worker selects the canonical pinned Python project and wire script. - const parsedFixture = ts.createSourceFile( - 'fixture.mjs', - fixture, - ts.ScriptTarget.Latest, - true, - ts.ScriptKind.JS - ); - const factories = parsedFixture.statements.filter( - (node): node is ts.FunctionDeclaration => - ts.isFunctionDeclaration(node) && - node.name?.text === 'createDeepAgentsMemoryFixture' && - node.modifiers?.some( - (modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword - ) === true - ); - if ( - factories.length !== 1 || - !factories[0].body || - factories[0].parameters.length !== 1 - ) - return undefined; - const factory = factories[0], - parameter = factory.parameters[0]; - if (!ts.isObjectBindingPattern(parameter.name)) return undefined; - const spawnBinding = parameter.name.elements.filter( - (element) => element.name.getText() === 'spawnWorker' - ); - const arrow = - spawnBinding.length === 1 ? spawnBinding[0].initializer : undefined; - if ( - !arrow || - !ts.isArrowFunction(arrow) || - arrow.parameters.length !== 0 || - !ts.isCallExpression(arrow.body) || - arrow.body.expression.getText() !== 'spawn' || - arrow.body.arguments[0]?.getText() !== "'uv'" - ) - return undefined; - const args = arrow.body.arguments[1]; - if ( - !args || - !ts.isArrayLiteralExpression(args) || - JSON.stringify( - args.elements.map((element) => - ts.isStringLiteral(element) ? element.text : null - ) - ) !== - JSON.stringify([ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/memory/python', - 'python', - 'scripts/react-cockpit/deep-agents-memory-wire.py', - ]) - ) - return undefined; - const factoryBody = factory.body; - if (!factoryBody || !has(factoryBody.statements, 'return handle;')) - return undefined; - const handle = declaration(factoryBody, 'handle')?.initializer; - const operations = factoryBody.statements.filter( - (node): node is ts.FunctionDeclaration => - ts.isFunctionDeclaration(node) && node.name?.text === 'operate' - ); - const workers = parsedFixture.statements.filter( - (node): node is ts.FunctionDeclaration => - ts.isFunctionDeclaration(node) && node.name?.text === 'graphWorker' - ); - if ( - !handle || - !ts.isArrowFunction(handle) || - !ts.isBlock(handle.body) || - operations.length !== 1 || - !operations[0].body || - workers.length !== 1 || - !workers[0].body || - !has(workers[0].body.statements, 'const child = spawnWorker();') - ) - return undefined; - const queued = declaration(operations[0].body, 'result')?.initializer; - if ( - !queued || - !ts.isCallExpression(queued) || - queued.expression.getText() !== 'queue.then' || - queued.arguments.length !== 1 - ) - return undefined; - const execute = queued.arguments[0]; - if ( - !ts.isArrowFunction(execute) || - !ts.isBlock(execute.body) || - !has( - execute.body.statements, - 'worker ??= graphWorker(spawnWorker, workerTimeoutMs);' - ) || - !has( - execute.body.statements, - 'const result = await worker.send(value), proof = result.proof;' - ) || - !has(operations[0].body.statements, 'return result;') - ) - return undefined; - // Runtime calls in the returned handler count; nested uncalled helpers do not. - let operationCalls = 0; - const visitHandler = (node: ts.Node) => { - if ( - node !== handle.body && - (ts.isFunctionDeclaration(node) || - ts.isArrowFunction(node) || - ts.isFunctionExpression(node)) - ) - return; - if (ts.isCallExpression(node) && node.expression.getText() === 'operate') - operationCalls++; - ts.forEachChild(node, visitHandler); - }; - visitHandler(handle.body); - return operationCalls === 5 ? 'cockpit/deep-agents/memory/python' : undefined; -} - -it.each(['setup', 'fixture'])( - 'rejects unused canonical Memory %s subtrees', - (owner) => { - let setup = readRepoFile( - 'cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts' - ); - let fixture = readRepoFile( - 'scripts/react-cockpit/deep-agents-memory-fixture.mjs' - ); - if (owner === 'setup') - setup = setup - .replace( - 'export default async function setup() {', - 'export default async function setup() { async function neverCalled() {' - ) - .trimEnd() - .replace(/}$/, '} }'); - else - fixture = - fixture.replace( - 'export function createDeepAgentsMemoryFixture(', - 'function unusedFixture(' - ) + - '\nexport function createDeepAgentsMemoryFixture() { return async () => true; }'; - expect( - compiledMemoryBackend( - 'cockpit-deep-agents-memory-angular', - setup, - fixture - ) - ).toBeUndefined(); - } -); - -it('recognizes the actual compiled Memory backend and rejects project, fixture, port and proxy drift', () => { - const setup = readRepoFile( - 'cockpit/deep-agents/memory/angular/e2e/global-setup-impl.ts' - ); - const fixture = readRepoFile( - 'scripts/react-cockpit/deep-agents-memory-fixture.mjs' - ); - const parse = ( - text = setup, - implementation = fixture, - project = 'cockpit-deep-agents-memory-angular' - ) => compiledMemoryBackend(project, text, implementation); - expect(parse()).toBe('cockpit/deep-agents/memory/python'); - expect( - parse(setup, fixture, 'cockpit-deep-agents-skills-angular') - ).toBeUndefined(); - for (const changed of [ - setup.replace('deep-agents-memory-fixture.mjs', 'generic-fixture.mjs'), - setup.replace( - 'const fixture = createDeepAgentsMemoryFixture();', - 'const fixture = createGenericFixture();' - ), - setup.replace('backend.listen(5313)', 'backend.listen(9999)'), - setup.replace( - 'const backend = createServer', - 'const unused = createServer' - ), - setup.replace( - 'export default async function setup()', - 'async function deadSetup()' - ), - setup.replace("'/api' + path", 'path'), - setup.replace("'--port=4313'", "'--port=9999'"), - ]) - expect(parse(changed)).toBeUndefined(); - expect( - parse( - setup, - fixture.replace( - "'cockpit/deep-agents/memory/python'", - "'cockpit/deep-agents/skills/python'" - ) - ) - ).toBeUndefined(); -}); - -it('wires native Memory build, compiled fixture and website coverage to the shared Python graph', () => { +it('wires native Memory build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readRepoFile(path)); const react = read('cockpit/deep-agents/memory/react/project.json'); expect(react.tags).toEqual( @@ -367,24 +86,14 @@ it('wires native Memory build, compiled fixture and website coverage to the shar ); expect(react.targets.e2e.dependsOn).toEqual([ { target: 'build', projects: ['cockpit-deep-agents-memory-react'] }, - { target: 'fixture-test', projects: ['cockpit-deep-agents-memory-react'] }, - ]); - expect(react.targets['fixture-test'].dependsOn).toEqual([ { target: 'test', projects: ['cockpit-deep-agents-memory-python'] }, ]); - expect(react.targets['fixture-test'].options.command).toBe( - 'node --test scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs' - ); expect( websiteBuildProjects(read('apps/website/project.json')) ).toContain('cockpit-deep-agents-memory-react'); - expect( - read('cockpit/deep-agents/memory/angular/project.json').targets.e2e - .dependsOn - ).toContain('lifecycle-test'); const config = readRepoFile('apps/website/playwright.config.ts'); expect(config).toContain( - 'cockpit-deep-agents-memory-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent' + 'node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent --backend' ); expect(config).toContain("url: 'http://127.0.0.1:4632'"); }); @@ -482,12 +191,7 @@ function activeCockpitE2eWiring(): E2eWiring[] { const langgraphCwd = parseStringProperty(globalSetup, 'langgraphCwd') ?? parseStringProperty(globalSetup, 'pythonCwd') ?? - parseStringProperty(globalSetup, 'backendCwd') ?? - compiledMemoryBackend( - project.name, - globalSetup, - readRepoFile('scripts/react-cockpit/deep-agents-memory-fixture.mjs') - ); + parseStringProperty(globalSetup, 'backendCwd'); // Post-port-registry migration: ports are imported from // cockpit/ports.mjs rather than living as literals in diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index e85d45586..c57259847 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -64,6 +64,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { topic: 'memory', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/memory/react', base: '/deep-agents/memory/react/', port: 4632, project: 'cockpit-deep-agents-memory-react', + backend: aimockLanggraph('deep-agents/memory', 4632), }); if (topic === 'deep-agents-skills') return Object.freeze({ diff --git a/scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs b/scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs deleted file mode 100644 index 72acb9b0e..000000000 --- a/scripts/react-cockpit/deep-agents-memory-configuration.spec.mjs +++ /dev/null @@ -1,149 +0,0 @@ -import assert from 'node:assert/strict'; -import { readFileSync, existsSync } from 'node:fs'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { createServer } from 'node:net'; -import { randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; -import { Client } from '@langchain/langgraph-sdk'; -async function vacant(port) { - const listener = createServer(); - listener.listen(port, '127.0.0.1'); - await once(listener, 'listening'); - await new Promise((r) => listener.close(r)); -} -async function running(t, selection) { - const { port } = reactCockpitConfiguration(selection); - await vacant(port); - const child = spawn( - process.execPath, - ['scripts/react-cockpit/serve.mjs', selection, '--no-parent'], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['ignore', 'pipe', 'pipe'], - } - ); - let error = ''; - child.stderr.on('data', (b) => (error += b)); - const exited = once(child, 'exit'); - t.after(async () => { - if (child.exitCode === null && child.signalCode === null) - child.kill('SIGTERM'); - await exited; - }); - const base = `http://127.0.0.1:${port}`; - for (let attempt = 0; attempt < 100; attempt++) { - if (child.exitCode !== null) throw Error(error); - try { - await fetch(base + '/__lifetime'); - return { child, exited, base, port }; - } catch { - await new Promise((r) => setTimeout(r, 25)); - } - } - throw Error('Serve listener did not start: ' + error); -} -test( - 'serve dispatch keeps original LangGraph Memory separate and awaits actual Memory worker shutdown', - { timeout: 30000 }, - async (t) => { - const deep = await running(t, 'deep-agents-memory'); - const original = await running(t, 'memory'); - const sdk = new Client({ - apiUrl: deep.base + '/api', - callerOptions: { maxRetries: 0 }, - }); - const thread = (await sdk.threads.create()).thread_id; - const events = []; - for await (const e of sdk.runs.stream(thread, 'da-memory', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'offline' }], - }, - context: { memory_scope: randomUUID() }, - streamMode: ['values', 'custom'], - })) - events.push(e); - assert( - events.some( - (e) => e.event === 'custom' && e.data.name === 'deep_agents.memory' - ) - ); - const originalSdk = new Client({ - apiUrl: original.base + '/api', - callerOptions: { maxRetries: 0 }, - }); - const originalThread = randomUUID(); - await originalSdk.threads.create({ threadId: originalThread }); - const originalEvents = []; - for await (const e of originalSdk.runs.stream(originalThread, 'memory', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'Recall' }], - }, - streamMode: ['values'], - })) - originalEvents.push(e); - assert( - originalEvents.some( - (e) => e.event === 'values' && Object.hasOwn(e.data, 'memory') - ) - ); - assert.equal( - (await fetch(deep.base + '/__hold-extraction', { method: 'POST' })) - .status, - 404 - ); - const deepLife = await (await fetch(deep.base + '/__lifetime')).json(); - assert.equal(deepLife.workers, 1); - assert.deepEqual( - await (await fetch(original.base + '/__lifetime')).json(), - { active: false } - ); - deep.child.kill('SIGTERM'); - await deep.exited; - assert.throws(() => process.kill(deepLife.workerPid, 0), { code: 'ESRCH' }); - assert.throws(() => process.kill(deepLife.pythonPid, 0), { code: 'ESRCH' }); - await vacant(deep.port); - original.child.kill('SIGTERM'); - await original.exited; - await vacant(original.port); - } -); -import { test } from 'node:test'; -import { reactCockpitConfiguration } from './configuration.mjs'; -test('Memory browser target runs the actual compiled fixture after installed builds', () => { - const project = JSON.parse(readFileSync(new URL('../../cockpit/deep-agents/memory/react/project.json', import.meta.url))); - assert.ok(project.tags.includes('scope:cockpit-e2e')); - assert.equal(project.targets.e2e.executor, '@nx/playwright:playwright'); - assert.equal(project.targets.e2e.options.config, 'cockpit/deep-agents/memory/react/e2e/playwright.config.ts'); - assert.ok(existsSync(new URL('../../' + project.targets.e2e.options.config, import.meta.url))); - assert.ok(project.targets.e2e.dependsOn.some(d => d.target === 'build')); - assert.ok(project.targets.e2e.dependsOn.some(d => d.target === 'fixture-test')); -}); -test('Deep Agents Memory is an exact frozen closed native selection', () => { - const config = reactCockpitConfiguration('deep-agents-memory'); - assert.deepEqual(config, { - topic: 'memory', - library: 'deep-agents', - adapter: 'langgraph', - appPath: 'cockpit/deep-agents/memory/react', - base: '/deep-agents/memory/react/', - port: 4632, - project: 'cockpit-deep-agents-memory-react', - }); - assert.ok(Object.isFrozen(config)); - for (const invalid of [ - 'deep-agents/../../foreign', - 'deep-agents-memory/../../foreign', - '/deep-agents/memory/react/', - ]) - assert.throws(() => reactCockpitConfiguration(invalid)); -}); -test('original LangGraph Memory mapping is unchanged', () => { - assert.deepEqual(reactCockpitConfiguration('memory'), { - topic: 'memory', - appPath: 'cockpit/langgraph/memory/react', - base: '/langgraph/memory/react/', - port: 4602, - project: 'cockpit-langgraph-memory-react', - }); -}); diff --git a/scripts/react-cockpit/deep-agents-memory-fixture.mjs b/scripts/react-cockpit/deep-agents-memory-fixture.mjs deleted file mode 100644 index 8f61c5729..000000000 --- a/scripts/react-cockpit/deep-agents-memory-fixture.mjs +++ /dev/null @@ -1,649 +0,0 @@ -/** Loopback-only SDK transport backed by the actual compiled Memory graph. */ -import { spawn } from 'node:child_process'; -import { readFileSync } from 'node:fs'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, timeout) { - const child = spawnWorker(); - const pending = new Map(); - let sequence = 0, - dead = false, - closing, - killTimer; - const ended = new Promise((resolve) => child.once('close', resolve)); - const maxResponseBytes = 16 * 1024 * 1024; - let frame = Buffer.alloc(0), - frameBytes = 0; - child.stderr.resume(); - function fail() { - dead = true; - frame = Buffer.alloc(0); - frameBytes = 0; - for (const item of pending.values()) { - clearTimeout(item.timer); - item.reject(Error('Memory graph worker unavailable.')); - } - pending.clear(); - } - const terminate = () => { - fail(); - child.kill('SIGTERM'); - killTimer ??= setTimeout(() => child.kill('SIGKILL'), 1500); - }; - child.once('error', fail); - child.once('close', () => { - clearTimeout(killTimer); - fail(); - }); - child.stdin.on('error', terminate); - child.stdout.once('error', terminate); - child.stdout.once('end', () => { - if (!dead) terminate(); - }); - child.stdout.on('data', (bytes) => { - if (dead) return; - try { - let start = 0; - while (start < bytes.length && !dead) { - const newline = bytes.indexOf(0x0a, start); - const end = newline < 0 ? bytes.length : newline; - const needed = frameBytes + end - start; - // Count each raw frame before copying or decoding, including frames - // whose delimiter has not arrived. Never grow storage beyond the cap. - if (needed > maxResponseBytes) throw Error('Oversized response'); - if (needed > frame.length) { - const grown = Buffer.allocUnsafe( - Math.min(maxResponseBytes, Math.max(needed, frame.length * 2, 4096)) - ); - frame.copy(grown, 0, 0, frameBytes); - frame = grown; - } - bytes.copy(frame, frameBytes, start, end); - frameBytes = needed; - if (newline < 0) return; - const result = JSON.parse(frame.toString('utf8', 0, frameBytes)); - frameBytes = 0; - const item = pending.get(result.requestId); - if (!item) throw Error('Unowned response'); - pending.delete(result.requestId); - clearTimeout(item.timer); - if (result.error) item.reject(Error('Memory graph operation failed.')); - else item.resolve(result); - start = newline + 1; - } - } catch { - terminate(); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(Error('Memory graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const wire = JSON.stringify({ ...value, requestId }) + '\n'; - if (Buffer.byteLength(wire) > 1024 * 1024) { - reject(Error('Oversized request')); - return; - } - const timer = setTimeout(terminate, timeout); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(wire); - }); - }, - close() { - return (closing ??= (async () => { - child.stdin.end(); - terminate(); - await ended; - clearTimeout(killTimer); - child.stdout.removeAllListeners('data'); - })()); - }, - }; -} - -export function createDeepAgentsMemoryFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/memory/python', - 'python', - 'scripts/react-cockpit/deep-agents-memory-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-memory-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const sourceRoot = new URL( - '../../cockpit/deep-agents/memory/python/', - import.meta.url - ); - const sourcePaths = [ - 'src/graph.py', - 'src/memory_observation.py', - 'tests/test_streaming.py', - 'tests/model_support.py', - 'prompts/memory.md', - 'uv.lock', - ]; - const sourceSha256 = Object.fromEntries( - sourcePaths.map((path) => [ - path, - createHash('sha256') - .update(readFileSync(new URL(path, sourceRoot))) - .digest('hex'), - ]) - ); - const workerSha256 = createHash('sha256') - .update( - readFileSync(new URL('./deep-agents-memory-wire.py', import.meta.url)) - ) - .digest('hex'); - let requestCount = 0, - pythonPid; - let worker, - closed = false, - closing, - queue = Promise.resolve(); - // Real execution is buffered before delivery; transport cancellation does not roll back server graph work. - const threads = new Set(), - runs = new Map(), - held = new Set(), - rawEvents = [], - proofs = [], - requests = []; - const options = { - scenario: 'teach', - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - }; - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value), - proof = result.proof; - if ( - closed || - proof?.actualCompiledGraph !== true || - proof.networkConnectAttempts !== 0 || - proof.workerSha256 !== workerSha256 || - !Number.isInteger(proof.pythonPid) || - proof.pythonPid <= 0 || - (pythonPid !== undefined && proof.pythonPid !== pythonPid) || - (value.op === 'submit' && - proof.memoryScope !== value.context.memory_scope) || - proof.op !== value.op || - proof.threadId !== (value.threadId ?? null) || - Object.keys(proof.sourceSha256 ?? {}).length !== - Object.keys(sourceSha256).length || - Object.entries(sourceSha256).some( - ([path, hash]) => proof.sourceSha256[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - pythonPid = proof.pythonPid; - proofs.push(proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function hold(response, finish, run) { - const item = { response, finish, run }; - held.add(item); - response.once('close', () => { - if (held.delete(item) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - const host = request.headers.host; - if ( - !host || - !/^(127\.0\.0\.1|localhost|\[::1\]):[0-9]+$/.test(host) || - host.slice(host.lastIndexOf(':') + 1) !== - String(request.socket.localPort) || - request.url.length > 2048 - ) { - json(response, {}, 400); - return true; - } - if (++requestCount > 2000) { - json(response, {}, 429); - return true; - } - const chunks = []; - let byteLength = 0; - for await (const chunk of request) { - const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); - byteLength += bytes.length; - if (byteLength > 1024 * 1024) { - json(response, {}, 413); - return true; - } - chunks.push(bytes); - } - const input = Buffer.concat(chunks, byteLength).toString('utf8'); - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__raw-events') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, rawEvents); - return true; - } - if (pathname === '/__graph-proof') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, proofs); - return true; - } - if (pathname === '/__requests') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - pythonPid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - if (request.method !== 'POST') { - json(response, {}, 405); - return true; - } - if ( - !only(body, [ - 'scenario', - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ]) || - ('scenario' in body && - ![ - 'teach', - 'edit', - 'recall', - 'read', - 'empty', - 'boundary', - 'oversize', - 'fail-initial', - 'fail-model', - 'fail-terminal', - ].includes(body.scenario)) || - [ - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ].some((k) => k in body && typeof body[k] !== 'boolean') - ) { - json(response, {}, 400); - return true; - } - if ('scenario' in body) - await operate({ op: 'configure', scenario: body.scenario }); - Object.assign(options, body); - json(response, {}); - return true; - } - if (pathname === '/__release') { - if (request.method !== 'POST' || !only(body, [])) { - json(response, {}, 400); - return true; - } - for (const item of held) { - held.delete(item); - item.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - if (requests.length >= 2000 || runs.size >= 1000) { - json(response, {}, 429); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if (options.failCreation) { - options.failCreation = false; - json(response, { error: 'Controlled creation failure.' }, 500); - return true; - } - if ( - (body.thread_id !== undefined && - (!uuid.test(body.thread_id) || threads.has(body.thread_id))) || - threads.size >= 100 || - !only(body, ['thread_id', 'if_exists', 'metadata']) || - (body.if_exists !== undefined && body.if_exists !== 'raise') || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - const threadId = body.thread_id ?? randomUUID(); - await operate({ op: 'create', threadId }); - threads.add(threadId); - json(response, { thread_id: threadId }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if (suffix === '/state' && input.length !== 0) { - json(response, {}, 400); - return true; - } - if (suffix === '/state/checkpoint') { - if (options.failCheckpoint) { - options.failCheckpoint = false; - json(response, { error: 'Controlled exact read failure.' }, 500); - return true; - } - if ( - !only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - body.checkpoint.thread_id !== threadId || - typeof body.checkpoint.checkpoint_id !== 'string' || - body.checkpoint.checkpoint_ns !== '' - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - ...(suffix === '/state/checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }); - const finish = () => json(response, state); - if ( - options.holdState || - (options.holdCheckpoint && suffix === '/state/checkpoint') - ) - hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit']) || - (body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100)) - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ - op: 'history', - threadId, - limit: body.limit ?? 10, - }); - json(response, history.slice(0, body.limit ?? 10)); - return true; - } - const runRoute = /^\/runs\/([^/]+)(\/cancel)?$/.exec(suffix); - if ( - runRoute && - ((!runRoute[2] && request.method === 'GET') || - (runRoute[2] && request.method === 'POST')) - ) { - if (input.length !== 0) { - json(response, {}, 400); - return true; - } - const run = runs.get(runRoute[1]); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - if (runRoute[2]) { - const search = new URL(request.url, 'http://localhost').searchParams; - if (search.get('action') !== 'interrupt') { - json(response, {}, 400); - return true; - } - for (const item of held) - if (item.run === run) { - held.delete(item); - run.status = 'interrupted'; - item.response.end(); - } - json(response, {}); - } else json(response, run); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - if (options.failStream) { - options.failStream = false; - json(response, { error: 'Controlled stream failure.' }, 500); - return true; - } - const message = body.input?.messages?.[0]; - const nativeHuman = only(message, ['type', 'id', 'content']) && - Object.keys(message).length === 3 && typeof message.id === 'string' && - message.id.trim() && message.id.length <= 256; - const angularHuman = only(message, ['type', 'role', 'content']) && - Object.keys(message).length === 3 && message.role === 'human'; - if ( - !only(body, [ - 'assistant_id', - 'context', - 'config', - 'metadata', - 'input', - 'command', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !( - body.command === undefined && - only(body.input, ['messages']) && - Array.isArray(body.input.messages) && - body.input.messages?.length === 1 && - (nativeHuman || angularHuman) && - message.type === 'human' && - typeof message.content === 'string' && - Buffer.byteLength(message.content) <= 65536 - ) || - body.assistant_id !== 'da-memory' || - !only(body.context, ['memory_scope']) || - !uuid.test(body.context.memory_scope ?? '') || - (body.config !== undefined && !only(body.config, [])) || - (body.metadata !== undefined && !only(body.metadata, [])) || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'checkpoints', - 'custom', - ].includes(mode) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - // Canonical Angular's SDK omits id; LangGraph add_messages assigns - // UUIDv4 for missing IDs. Normalize once at this private boundary. - // Captured requests above preserve the original SDK wire unchanged. - const actualMessage = angularHuman - ? { type: 'human', id: randomUUID(), content: message.content } - : message; - const wire = await operate({ - op: 'submit', - threadId, - messages: [actualMessage], - context: body.context, - }); - rawEvents.push({ - threadId, - runId: run.run_id, - events: wire.events, - modelPrompts: wire.modelPrompts, - storeOperations: wire.storeOperations, - ...(wire.failure ? { failure: wire.failure } : {}), - }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream; charset=utf-8', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const finish = () => { - if (response.destroyed || closed) return; - for (const [namespace, kind, data] of wire.events) - if ( - modes.has(kind) && - (!namespace.length || body.stream_subgraphs === true) - ) - response.write(event([kind, ...namespace].join('|'), data)); - if (wire.failure) - response.write( - event('error', { message: 'Memory graph operation failed.' }) - ); - run.status = wire.failure ? 'error' : 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Memory graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const item of held) item.response.destroy(); - held.clear(); - await worker?.close(); - await queue; - })()); - return handle; -} diff --git a/scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs b/scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs deleted file mode 100644 index 8c6d8ad9e..000000000 --- a/scripts/react-cockpit/deep-agents-memory-fixture.spec.mjs +++ /dev/null @@ -1,684 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer, request as httpRequest } from 'node:http'; -import { once } from 'node:events'; -import { randomUUID } from 'node:crypto'; -import { spawn } from 'node:child_process'; -import { mkdtempSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; -import { Client } from '@langchain/langgraph-sdk'; -const { createDeepAgentsMemoryFixture } = await import( - './deep-agents-memory-fixture.mjs' -).catch(() => ({})); -const captures = mkdtempSync(join(tmpdir(), 'threadplane-memory-task4a-sdk-')); -console.log('Memory SDK captures: ' + captures); -const FILE = '/memories/AGENTS.md'; -const LITERAL = 'Fleet note\n\n Ω literal whitespace \n'; -const UPDATED = 'Updated fleet note\n\n Ω literal whitespace \n'; -async function fixture(t, options = {}) { - assert.equal( - typeof createDeepAgentsMemoryFixture, - 'function', - 'Missing actual compiled Memory fixture' - ); - const handle = createDeepAgentsMemoryFixture(options); - const server = createServer(async (req, res) => { - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - }); - const base = `http://127.0.0.1:${server.address().port}`; - const client = new Client({ - apiUrl: base + '/api', - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const control = async (body) => { - const r = await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify(body), - }); - assert.equal(r.status, 200); - }; - const diagnostics = async (name) => (await fetch(base + '/__' + name)).json(); - const scope = randomUUID(); - const thread = ( - await client.threads.create({ threadId: randomUUID(), ifExists: 'raise' }) - ).thread_id; - const stream = ( - target = thread, - memory_scope = scope, - id = randomUUID(), - streamMode = [ - 'values', - 'messages-tuple', - 'updates', - 'custom', - 'checkpoints', - ] - ) => - client.runs.stream(target, 'da-memory', { - input: { messages: [{ type: 'human', id, content: 'Offline dispatch' }] }, - context: { memory_scope }, - streamMode, - streamSubgraphs: true, - }); - const submit = async (...args) => { - const events = []; - for await (const e of stream(...args)) events.push(e); - return events; - }; - return { - handle, - server, - base, - client, - control, - diagnostics, - scope, - thread, - stream, - submit, - }; -} -const observations = (events) => - events.filter((e) => e.event === 'custom').map((e) => e.data.data); -test('a length-shaped object cannot become an authored compiled human array', async t => { - const f = await fixture(t); - const response = await fetch(f.base + '/api/threads/' + f.thread + '/runs/stream', { - method: 'POST', body: JSON.stringify({ assistant_id: 'da-memory', - input: { messages: { 0: { type: 'human', id: randomUUID(), content: 'Malformed nonarray input' }, length: 1 } }, - context: { memory_scope: f.scope }, stream_mode: ['values', 'custom'] }), - }); - await response.text(); - const raw = await f.diagnostics('raw-events'); - writeFileSync(join(captures, 'nonarray-rejection.json'), JSON.stringify({ status: response.status, requests: await f.diagnostics('requests'), raw, proofs: await f.diagnostics('graph-proof') }, null, 2)); - assert.equal(response.status, 400); - assert.deepEqual(raw, []); - assert.equal((await f.diagnostics('graph-proof')).filter(proof => proof.op === 'submit').length, 0); -}); -test('actual canonical Angular SDK human wire gets one fixture-assigned message identity and genuine write', async t => { - const f = await fixture(t); - const human = { type: 'human', role: 'human', content: 'Angular genuine input\n\n Ω ' }; - const events = []; - for await (const event of f.client.runs.stream(f.thread, 'da-memory', { input: { messages: [human] }, context: { memory_scope: f.scope }, streamMode: ['values', 'custom'] })) events.push(event); - assert.deepEqual(observations(events).at(-1).memory_contents, { [FILE]: LITERAL }); - const state = await f.client.threads.getState(f.thread); - const authored = state.values.messages.filter(m => m.type === 'human'); - assert.equal(authored.length, 1); assert.equal(authored[0].content, human.content); - assert.match(authored[0].id, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); - const captured = (await f.diagnostics('requests')).find(r => r.path.endsWith('/runs/stream')); - assert.deepEqual(captured.body.input.messages, [human]); assert.equal(Object.hasOwn(captured.body.input.messages[0], 'id'), false); - for (const bad of [{ ...human, role: 'assistant' }, { ...human, id: '' }, { ...human, extra: true }, { type: 'human', content: 'missing role' }]) { - const response = await fetch(f.base + '/api/threads/' + f.thread + '/runs/stream', { method: 'POST', body: JSON.stringify({ assistant_id: 'da-memory', input: { messages: [bad] }, context: { memory_scope: f.scope }, stream_mode: ['values'] }) }); - assert.equal(response.status, 400); - } - const raw = await f.diagnostics('raw-events'); assert.equal(raw.length, 1); - assert(raw[0].storeOperations.some(op => op.type === 'PutOp')); - writeFileSync(join(captures, 'angular-sdk-wire.json'), JSON.stringify({ requests: await f.diagnostics('requests'), raw, state, proofs: await f.diagnostics('graph-proof') }, null, 2)); -}); -test( - 'real SDK two threads share actual scoped writes edits and pinned prompt recall; raw exact newest agrees', - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - const first = await f.submit(); - assert.deepEqual( - observations(first).map((e) => e.phase), - ['initial', 'model', 'model', 'terminal'] - ); - assert.deepEqual(observations(first)[0].memory_contents, {}); - assert.deepEqual(observations(first).at(-1).memory_contents, { - [FILE]: LITERAL, - }); - assert( - first - .filter((e) => e.event === 'values') - .every( - (e) => - !Object.hasOwn(e.data, 'memory_contents') && - !Object.hasOwn(e.data, 'memory_scope_owner') - ) - ); - let saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.memory_contents, { [FILE]: LITERAL }); - assert.equal(saved.values.memory_scope_owner, f.scope); - assert.deepEqual( - first.filter((e) => e.event === 'checkpoints').at(-1).data.values - .memory_contents, - saved.values.memory_contents - ); - assert.deepEqual( - await f.client.threads.getState(f.thread, saved.checkpoint), - saved - ); - assert.deepEqual( - (await f.client.threads.getHistory(f.thread, { limit: 1 }))[0], - saved - ); - assert.equal( - saved.values.messages.filter((m) => m.type === 'tool')[0].status, - 'success' - ); - await f.control({ scenario: 'edit' }); - await f.submit(); - saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.memory_contents, { [FILE]: UPDATED }); - const second = ( - await f.client.threads.create({ ifExists: 'raise', metadata: {} }) - ).thread_id; - await f.control({ scenario: 'recall' }); - const recall = await f.submit(second); - assert.deepEqual(observations(recall)[0].memory_contents, { - [FILE]: UPDATED, - }); - const third = (await f.client.threads.create()).thread_id; - const otherScope = randomUUID(); - const isolated = await f.submit(third, otherScope); - assert( - observations(isolated).every( - (e) => - Object.keys(e.memory_contents).length === 0 && - e.memory_scope_owner === otherScope - ) - ); - const raw = await f.diagnostics('raw-events'); - assert(raw[2].modelPrompts.every((p) => p.includes('Updated fleet note'))); - assert(raw[3].modelPrompts.every((p) => !p.includes('Updated fleet note'))); - assert(raw[0].storeOperations.some((o) => o.type === 'PutOp')); - const proof = await f.diagnostics('graph-proof'); - assert( - proof.every( - (p) => - p.actualCompiledGraph && - p.networkConnectAttempts === 0 && - p.workerSha256?.length === 64 && - p.sourceSha256['tests/model_support.py'] - ) - ); - writeFileSync( - join(captures, 'scoped-sdk-proof.json'), - JSON.stringify({ first, saved, recall, isolated, raw, proof }, null, 2), - { mode: 0o400 } - ); - } -); -for (const [scenario, content] of [ - ['recall', null], - ['empty', ''], - ['boundary', 'Ω'.repeat(32768)], -]) { - test( - `actual ${scenario} distinguishes missing empty and UTF8 bound`, - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.control({ scenario }); - const events = await f.submit(); - const expected = content === null ? {} : { [FILE]: content }; - assert.deepEqual(observations(events).at(-1).memory_contents, expected); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values.memory_contents, - expected - ); - } - ); -} -for (const [scenario, phases] of [ - ['fail-initial', []], - ['fail-model', ['initial']], - ['fail-terminal', ['initial', 'model', 'model']], - ['oversize', ['initial', 'model']], -]) { - test( - `actual read failure ${scenario} forwards partial events without terminal confirmation`, - { timeout: 30000 }, - async (t) => { - const f = await fixture(t); - await f.control({ scenario }); - const events = await f.submit(); - assert.deepEqual( - observations(events).map((e) => e.phase), - phases - ); - assert.equal(events.at(-1).event, 'error'); - assert.equal( - events.at(-1).data.message, - 'Memory graph operation failed.' - ); - const raw = await f.diagnostics('raw-events'); - assert(raw[0].failure); - if (scenario === 'fail-terminal') { - const second = (await f.client.threads.create()).thread_id; - await f.control({ scenario: 'recall' }); - assert.deepEqual( - observations(await f.submit(second)).at(-1).memory_contents, - { [FILE]: LITERAL } - ); - } - } - ); -} -test('ownership replay malformed context foreign checkpoints controls and unsupported SDK bodies fail closed', async (t) => { - const f = await fixture(t); - await f.submit(f.thread, f.scope, 'owned-human'); - await assert.rejects(() => f.submit(f.thread, f.scope, 'owned-human')); - await f.control({ scenario: 'recall' }); - const mismatched = await f.submit(f.thread, randomUUID()); - assert.equal(mismatched.at(-1).event, 'error'); - const raw = await f.diagnostics('raw-events'); - assert.deepEqual(raw.at(-1).storeOperations, []); - assert.deepEqual(raw.at(-1).modelPrompts, []); - await assert.rejects(() => f.client.threads.getState(randomUUID())); - const state = await f.client.threads.getState(f.thread); - for (const cp of [ - { ...state.checkpoint, checkpoint_id: randomUUID() }, - { ...state.checkpoint, thread_id: randomUUID() }, - { ...state.checkpoint, checkpoint_ns: 'foreign' }, - ]) - await assert.rejects(() => f.client.threads.getState(f.thread, cp)); - for (const body of [ - { scenario: 'unknown' }, - { filePath: '/etc/passwd' }, - { holdStream: 'true' }, - ]) - assert.equal( - ( - await fetch(f.base + '/__configure', { - method: 'POST', - body: JSON.stringify(body), - }) - ).status, - 400 - ); - const body = { - assistant_id: 'da-memory', - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'offline' }], - }, - context: { memory_scope: f.scope }, - stream_mode: ['values'], - }; - for (const change of [ - { assistant_id: 'foreign' }, - { context: {} }, - { context: { memory_scope: f.scope, extra: true } }, - { command: { resume: true } }, - { input: { messages: body.input.messages, memory_contents: {} } }, - { stream_mode: ['unknown'] }, - ]) - assert.equal( - ( - await fetch(f.base + `/api/threads/${f.thread}/runs/stream`, { - method: 'POST', - body: JSON.stringify({ ...body, ...change }), - }) - ).status, - 400 - ); - assert.equal( - ( - await fetch(f.base + '/__configure', { - method: 'POST', - body: 'x'.repeat(1024 * 1024 + 1), - }) - ).status, - 413 - ); -}); -test('held stream cancellation and awaited worker shutdown preserve actual persistent write', async (t) => { - const f = await fixture(t); - await f.control({ holdStream: true }); - const stream = f.stream(); - const meta = await stream.next(); - await f.client.runs.cancel( - f.thread, - meta.value.data.run_id, - true, - 'interrupt' - ); - assert.equal((await stream.next()).done, true); - assert.deepEqual( - (await f.client.threads.getState(f.thread)).values.memory_contents, - { [FILE]: LITERAL } - ); - const life = await f.diagnostics('lifetime'); - await Promise.all([f.handle.close(), f.handle.close()]); - assert.throws(() => process.kill(life.workerPid, 0), { code: 'ESRCH' }); - assert.throws(() => process.kill(life.pythonPid, 0), { code: 'ESRCH' }); -}); -for (const [name, script, timeout] of [ - [ - 'oversized frame', - 'process.stdin.once("data",()=>process.stdout.write(Buffer.alloc(16*1024*1024+1,0x78)));setInterval(()=>{},1000)', - 6000, - ], - [ - 'malformed frame', - 'process.stdin.once("data",()=>process.stdout.write("invalid\\n"));setInterval(()=>{},1000)', - 6000, - ], - [ - 'unowned frame', - 'process.stdin.once("data",()=>process.stdout.write("{\\"requestId\\":9999}\\n"));setInterval(()=>{},1000)', - 6000, - ], - ['timeout', 'setInterval(()=>{},1000)', 100], -]) - test( - `worker ${name} fails closed and terminates owned process`, - { timeout: 12000 }, - async (t) => { - assert.equal(typeof createDeepAgentsMemoryFixture, 'function'); - let child; - const handle = createDeepAgentsMemoryFixture({ - workerTimeoutMs: timeout, - spawnWorker: () => - (child = spawn(process.execPath, ['-e', script], { - stdio: ['pipe', 'pipe', 'pipe'], - })), - }); - const server = createServer((req, res) => - handle(req, res, new URL(req.url, 'http://localhost').pathname) - ); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - }); - const sdk = new Client({ - apiUrl: `http://127.0.0.1:${server.address().port}/api`, - callerOptions: { maxRetries: 0 }, - }); - const started = performance.now(); - await assert.rejects(() => - sdk.threads.create({ threadId: randomUUID(), ifExists: 'raise' }) - ); - await handle.close(); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - if (timeout === 6000) assert(performance.now() - started < 3000); - } - ); -import { test } from 'node:test'; -test('actual read_file and mode filtering preserve captured payloads and tool arguments', async (t) => { - const f = await fixture(t); - await f.submit(); - await f.control({ scenario: 'read' }); - const events = await f.submit(f.thread, f.scope, randomUUID(), ['custom']); - assert(events.every((e) => ['metadata', 'custom'].includes(e.event))); - const raw = await f.diagnostics('raw-events'); - assert.deepEqual( - events.filter((e) => e.event === 'custom').map((e) => e.data), - raw - .at(-1) - .events.filter(([ns, kind]) => kind === 'custom' && !ns.length) - .map((e) => e[2]) - ); - const state = await f.client.threads.getState(f.thread); - const calls = state.values.messages.flatMap((m) => m.tool_calls ?? []); - assert.deepEqual( - calls.map((c) => [c.name, c.args]), - [ - ['write_file', { file_path: FILE, content: LITERAL }], - ['read_file', { file_path: FILE }], - ] - ); - const tools = state.values.messages.filter((m) => m.type === 'tool'); - assert(tools.every((m) => m.status === 'success')); - assert(tools.at(-1).content.includes('Fleet note')); -}); -test('close rejects pending graph requests and releases held current and exact reads', async (t) => { - const f = await fixture(t); - await f.submit(); - const state = await f.client.threads.getState(f.thread); - await f.control({ holdState: true, holdCheckpoint: true }); - const current = f.client.threads.getState(f.thread); - const exact = f.client.threads.getState(f.thread, state.checkpoint); - // Observe both actual reads reaching the held delivery boundary. - for (let i = 0; i < 100; i++) { - if ((await f.diagnostics('lifetime')).held === 2) break; - await new Promise((r) => setTimeout(r, 10)); - } - assert.equal((await f.diagnostics('lifetime')).held, 2); - const failures = Promise.allSettled([current, exact]); - await f.handle.close(); - assert((await failures).every((r) => r.status === 'rejected')); -}); -test( - 'close awaits worker exit while an actual request is pending', - { timeout: 10000 }, - async (t) => { - let child; - const handle = createDeepAgentsMemoryFixture({ - workerTimeoutMs: 6000, - spawnWorker: () => - (child = spawn( - process.execPath, - [ - '-e', - 'process.stdin.once("data",()=>process.stderr.write("ready"));setInterval(()=>{},1000)', - ], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - const server = createServer((req, res) => - handle(req, res, new URL(req.url, 'http://localhost').pathname) - ); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - }); - const client = new Client({ - apiUrl: `http://127.0.0.1:${server.address().port}/api`, - callerOptions: { maxRetries: 0 }, - }); - const pending = client.threads.create({ - threadId: randomUUID(), - ifExists: 'raise', - }); - const rejected = assert.rejects(() => pending); - for (let i = 0; i < 100 && !child; i++) - await new Promise((r) => setTimeout(r, 10)); - assert(child); - await handle.close(); - await rejected; - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - } -); -import { createInterface } from 'node:readline'; -import { fileURLToPath } from 'node:url'; -async function privateWorker(t) { - const child = spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/memory/python', - 'python', - 'scripts/react-cockpit/deep-agents-memory-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - stdio: ['pipe', 'pipe', 'pipe'], - } - ); - child.stderr.resume(); - const exit = once(child, 'exit'); - const lines = createInterface({ input: child.stdout }); - const iter = lines[Symbol.asyncIterator](); - t.after(async () => { - child.stdin.end(); - if (child.exitCode === null && child.signalCode === null) - child.kill('SIGTERM'); - await exit; - lines.close(); - }); - const send = async (body) => { - child.stdin.write(JSON.stringify(body) + '\n'); - const value = await iter.next(); - assert.equal(value.done, false); - return JSON.parse(value.value); - }; - return { child, exit, send }; -} -test( - 'private compiled worker rejects replayed request IDs unknown fields foreign roots and invalid bounded history', - { timeout: 15000 }, - async (t) => { - const w = await privateWorker(t), - threadId = randomUUID(); - assert.equal( - (await w.send({ requestId: 1, op: 'create', threadId })).proof - .actualCompiledGraph, - true - ); - for (const [requestId, body] of [ - [2, { op: 'history', threadId, limit: 0 }], - [ - 3, - { - op: 'state', - threadId, - checkpoint: { - thread_id: threadId, - checkpoint_id: randomUUID(), - checkpoint_ns: '', - }, - }, - ], - [4, { op: 'configure', scenario: 'recall', unexpected: true }], - [ - 5, - { - op: 'submit', - threadId, - messages: [{ type: 'human', id: 'x', content: 'offline' }], - context: { memory_scope: randomUUID(), extra: true }, - }, - ], - ]) - assert.equal( - (await w.send({ requestId, ...body })).error, - 'Graph operation failed' - ); - w.child.stdin.write( - JSON.stringify({ requestId: 5, op: 'state', threadId }) + '\n' - ); - await w.exit; - assert.throws(() => process.kill(w.child.pid, 0), { code: 'ESRCH' }); - } -); -test( - 'private compiled worker rejects an oversized unterminated request before JSON decode', - { timeout: 15000 }, - async (t) => { - const w = await privateWorker(t); - w.child.stdin.write(Buffer.alloc(1024 * 1024 + 1, 0x78)); - await w.exit; - assert.throws(() => process.kill(w.child.pid, 0), { code: 'ESRCH' }); - } -); -test('fixture rejects spoofed host, methods, paths and controls without graph submission', async (t) => { - const f = await fixture(t); - const initial = (await f.diagnostics('graph-proof')).length; - assert.equal( - await new Promise((resolve, reject) => { - const req = httpRequest( - f.base + '/__requests', - { - headers: { host: 'foreign.example' }, - }, - (res) => { - res.resume(); - resolve(res.statusCode); - } - ); - req.on('error', reject); - req.end(); - }), - 400 - ); - assert.equal( - (await fetch(f.base + '/__configure', { method: 'GET' })).status, - 405 - ); - assert.equal( - (await fetch(f.base + '/__unknown', { method: 'POST', body: '{}' })).status, - 404 - ); - assert.equal( - (await fetch(f.base + '/api/threads', { method: 'PUT', body: '{}' })) - .status, - 404 - ); - assert.equal( - ( - await fetch(f.base + '/__release', { - method: 'POST', - body: '{"extra":true}', - }) - ).status, - 400 - ); - assert.equal((await f.diagnostics('graph-proof')).length, initial); -}); -test( - 'timed out worker that ignores SIGTERM is forcibly terminated without fixture close', - { timeout: 10000 }, - async (t) => { - let child; - const handle = createDeepAgentsMemoryFixture({ - workerTimeoutMs: 100, - spawnWorker: () => - (child = spawn( - process.execPath, - ['-e', 'process.on("SIGTERM",()=>{});setInterval(()=>{},1000)'], - { stdio: ['pipe', 'pipe', 'pipe'] } - )), - }); - const server = createServer((req, res) => - handle(req, res, new URL(req.url, 'http://localhost').pathname) - ); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - t.after(async () => { - await handle.close(); - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - }); - const sdk = new Client({ - apiUrl: `http://127.0.0.1:${server.address().port}/api`, - callerOptions: { maxRetries: 0 }, - }); - await assert.rejects(() => - sdk.threads.create({ threadId: randomUUID(), ifExists: 'raise' }) - ); - await new Promise((r) => setTimeout(r, 1800)); - assert.throws(() => process.kill(child.pid, 0), { code: 'ESRCH' }); - } -); -test('Memory fixture exposes a real compiled worker SDK handler', () => { - assert.equal( - typeof createDeepAgentsMemoryFixture, - 'function', - 'Missing actual compiled Memory fixture' - ); -}); diff --git a/scripts/react-cockpit/deep-agents-memory-wire.py b/scripts/react-cockpit/deep-agents-memory-wire.py deleted file mode 100644 index 602393fba..000000000 --- a/scripts/react-cockpit/deep-agents-memory-wire.py +++ /dev/null @@ -1,180 +0,0 @@ -"""Private bounded worker: actual Memory builder, one owned store for its lifetime.""" -import os -os.environ['LANGSMITH_TRACING'] = 'false' -os.environ['LANGCHAIN_TRACING_V2'] = 'false' -import asyncio -from contextlib import redirect_stdout, nullcontext -from copy import deepcopy -from dataclasses import asdict, is_dataclass -import hashlib -import json -from pathlib import Path -import socket -import sys -from unittest.mock import patch -from uuid import UUID - -NETWORK_GUARD = patch.object(socket.socket, 'connect', side_effect=AssertionError('Remote socket forbidden')) -NETWORK = NETWORK_GUARD.start() -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / 'cockpit/deep-agents/memory/python' -sys.path[:0] = [str(PROJECT), str(PROJECT / 'tests')] -from test_streaming import MemoryModel, SCENARIOS -from model_support import CountingStore, load -from deepagents.backends import StoreBackend -from src.memory_observation import validate_scope - -SOURCES = ('src/graph.py', 'src/memory_observation.py', 'tests/test_streaming.py', 'tests/model_support.py', 'prompts/memory.md', 'uv.lock') -MAX_REQUEST = 1024 * 1024 -MAX_RESPONSE = 16 * 1024 * 1024 -SCENARIO_NAMES = set(SCENARIOS) | {'fail-initial', 'fail-model', 'fail-terminal'} - -def encode(value): - if hasattr(value, 'model_dump'): - return value.model_dump(mode='json') - if is_dataclass(value): - return asdict(value) - raise TypeError('Unsupported graph wire value') - -def checkpoint(state): - fields = ('thread_id', 'checkpoint_id', 'checkpoint_ns', 'checkpoint_map') - return { - 'values': state.values, 'next': list(state.next), - 'checkpoint': {k:v for k,v in state.config['configurable'].items() if k in fields}, - 'parent_checkpoint': {k:v for k,v in state.parent_config['configurable'].items() if k in fields} if state.parent_config else None, - 'created_at': state.created_at, 'metadata': state.metadata, - 'tasks': [{'id':t.id, 'name':t.name, - **({'error': t.error} if t.error is not None else {}), - **({'state': t.state} if t.state is not None else {}), - 'interrupts':[{'id':i.id,'value':i.value} for i in t.interrupts]} for t in state.tasks], - } - -def valid_uuid(value): - try: - return isinstance(value,str) and len(value)==36 and UUID(value).version==4 and str(UUID(value))==value - except ValueError: - return False - -async def main(): - # Block sockets before loading any actual graph modules/model constructors. - with nullcontext(NETWORK) as network: - model, store = MemoryModel(), CountingStore() - with redirect_stdout(sys.stderr): - _, graph = load(model, store=store) - owners, seen, request_ids = set(), set(), set() - known = {} - scenario = 'teach' - provenance = {} - while line := sys.stdin.buffer.readline(MAX_REQUEST + 1): - request = {} - try: - if len(line)>MAX_REQUEST or not line.endswith(b'\n'): - # A broken frame cannot be resynchronized safely. - raise EOFError('Invalid bounded request frame') - request = json.loads(line) - if not isinstance(request,dict): - raise ValueError('Malformed request') - request_id = request.get('requestId') - if type(request_id) is not int or request_id<=0 or request_id in request_ids or len(request_ids)>=2000: - raise EOFError('Invalid/replayed request identity') - request_ids.add(request_id) - op = request.get('op') - fields = {'create':{'threadId'}, 'configure':{'scenario'}, 'state':{'threadId','checkpoint'}, - 'history':{'threadId','limit'}, 'submit':{'threadId','messages','context'}} - if op not in fields or set(request)-({'requestId','op'}|fields[op]): - raise ValueError('Unsupported operation fields') - thread = request.get('threadId') - config = {'configurable': {'thread_id': thread}} - if op=='create': - if not valid_uuid(thread) or thread in owners or len(owners)>=100: - raise ValueError('Invalid/duplicate owner') - owners.add(thread) - known[thread] = set() - elif op!='configure' and thread not in owners: - raise ValueError('Unknown owner') - if 'checkpoint' in request: - cp = request['checkpoint'] - if (not isinstance(cp,dict) or set(cp)-{'thread_id','checkpoint_id','checkpoint_ns','checkpoint_map'} - or cp.get('thread_id')!=thread or cp.get('checkpoint_ns')!='' - or cp.get('checkpoint_id') not in known[thread] - or ('checkpoint_map' in cp and cp['checkpoint_map']!={'':cp['checkpoint_id']})): - raise ValueError('Unknown/foreign root checkpoint') - config['configurable'].update(cp) - with redirect_stdout(sys.stderr): - result = {} - scope = None - if op=='configure': - if request.get('scenario') not in SCENARIO_NAMES: - raise ValueError('Unsupported scenario') - scenario = request['scenario'] - elif op=='state': - result['state'] = checkpoint(await graph.aget_state(config)) - elif op=='history': - limit = request.get('limit',10) - if type(limit) is not int or not 1<=limit<=100: - raise ValueError('Invalid bounded history query') - result['history'] = [checkpoint(s) async for s in graph.aget_state_history(config,limit=limit)] - elif op=='submit': - scope = validate_scope(request.get('context')) - messages = request.get('messages') - if not isinstance(messages,list) or len(messages)!=1: - raise ValueError('Expected one actual human') - human = messages[0] - if (not isinstance(human,dict) or set(human)!={'type','id','content'} or human['type']!='human' - or not isinstance(human['id'],str) or not human['id'].strip() or len(human['id'])>256 - or not isinstance(human['content'],str) or len(human['content'].encode())>65536): - raise ValueError('Invalid actual human') - identity = (thread,human['id']) - if identity in seen: - raise ValueError('Replayed human') - seen.add(identity) - model.calls = SCENARIOS['teach' if scenario=='fail-terminal' else 'recall' if scenario.startswith('fail-') else scenario] - provenance[thread] = {'scenario':scenario,'memoryScope':scope} - events = [] - prompt_start, operation_start = len(model.prompts), len(store.operations) - read_count = 0 - original = StoreBackend.download_files - failure_read = {'fail-initial':1,'fail-model':2,'fail-terminal':4}.get(scenario) - def controlled_failure(backend,paths): - nonlocal read_count - read_count += 1 - if read_count==failure_read: - raise RuntimeError('Controlled offline backend read failure') - return original(backend,paths) - with patch.object(StoreBackend,'download_files',controlled_failure) if failure_read else nullcontext(): - try: - async for namespace,kind,event in graph.astream( - {'messages':messages},config,context=request['context'], - output_keys=graph.output_channels,subgraphs=True, - stream_mode=['messages','values','updates','checkpoints','custom']): - events.append([list(namespace),kind,deepcopy(event)]) - if kind=='checkpoints' and not namespace: - known[thread].add(event['config']['configurable']['checkpoint_id']) - except Exception as failure: - result['failure'] = type(failure).__name__ - state = await graph.aget_state(config) - if state.config['configurable'].get('checkpoint_id'): - known[thread].add(state.config['configurable']['checkpoint_id']) - result.update(events=events,state=checkpoint(state), - modelPrompts=model.prompts[prompt_start:], - storeOperations=[{'type':type(o).__name__,'namespace':getattr(o,'namespace',None),'key':getattr(o,'key',None)} for o in store.operations[operation_start:]]) - network.assert_not_called() - result['proof'] = { - 'actualCompiledGraph': True, 'op':op,'threadId':thread, 'pythonPid':os.getpid(), - **provenance.get(thread,{'scenario':scenario,'memoryScope':None}), - 'sourceSha256':{p:hashlib.sha256((PROJECT/p).read_bytes()).hexdigest() for p in SOURCES}, - 'workerSha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), - 'networkConnectAttempts':network.call_count, - } - result['requestId'] = request_id - wire = json.dumps(result,default=encode,ensure_ascii=False).encode() - if len(wire)>MAX_RESPONSE: - raise EOFError('Oversized response') - sys.stdout.buffer.write(wire+b'\n') - sys.stdout.buffer.flush() - except EOFError: - return - except Exception: - print(json.dumps({'requestId':request.get('requestId') if isinstance(request,dict) else None,'error':'Graph operation failed'}),flush=True) - -asyncio.run(main()) diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 90d5bd543..09e1b9918 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -109,6 +109,13 @@ * the next history read gains copies of entries, * with dotted paths overwritten (duplicate, child * namespace or malformed references). Injected. + * POST /__stream/cut body { beforePattern, error }: the next run's + * browser stream ends with `event: error` (data + * `error`) just before the first event whose raw + * text matches beforePattern; the real run upstream + * still finishes. Injected data: stands in for a + * failure inside the graph (e.g. a store read) at + * that point, which aimock can't cause. * POST /__fail-stream next run stream answers 500 without forwarding * (the graph worker is unavailable) * POST /__release flush and resume a held run stream (and finish a @@ -223,6 +230,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let requests = []; let failCreate = false; let failStream = false; + let nextStreamCut = null; let childFault = null; let titleMode = 'safe'; let holdNextTitle = false; @@ -329,6 +337,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { decoratedChunks.clear(); failCreate = false; failStream = false; + nextStreamCut = null; childFault = null; titleMode = 'safe'; holdNextTitle = false; @@ -540,6 +549,9 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { async function proxyStream(request, response, path, body) { const hold = holdNextStream; holdNextStream = null; + const cut = nextStreamCut; + nextStreamCut = null; + let cutDone = false; let fault = childFault; childFault = null; const values = nextValues; @@ -634,7 +646,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { .finally(finish); return; } - if (!response.destroyed) response.end(); + if (!response.destroyed && !response.writableEnded) response.end(); if (upstreamResponse.statusCode !== 200 || !threadId) return; // Saved state and title as the real server persisted them. /__captures // waits for captures in flight, so tests never race the stream end. @@ -675,6 +687,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { upstreamResponse.setEncoding('utf8'); upstreamResponse.on('data', (chunk) => { sse += chunk; + if (cutDone) return; if (holding) return void buffered.push(chunk); pending += chunk; let match; @@ -683,6 +696,12 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { const raw = pending.slice(0, end); pending = pending.slice(end); const eventName = /^event: *(.*)$/m.exec(raw)?.[1]?.trim() ?? ''; + if (cut && new RegExp(cut.beforePattern).test(raw)) { + cutDone = true; + response.end(`event: error\ndata: ${JSON.stringify(cut.error)}\n\n`); + pending = ''; + break; + } if (fault && eventName.startsWith('values|')) { const namespace = eventName.slice('values'.length); const dataLine = raw.split(/\r?\n/).find((line) => line.startsWith('data:')); @@ -746,6 +765,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { upstreamResponse.on('end', () => { ended = true; if (holding) return; + if (cutDone) return void finish(); if (pending) response.write(inject(pending)); finish(); }); @@ -796,6 +816,10 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { return {}; }], '/__fail-stream': ['POST', () => { failStream = true; return {}; }], + '/__stream/cut': ['POST', (body) => { + nextStreamCut = { beforePattern: String(body.beforePattern), error: body.error ?? {} }; + return {}; + }], '/__values': ['POST', (body) => { nextValues = body; return {}; }], '/__stream/edit': ['POST', (body) => { nextStreamEdits = { ops: body.ops ?? [], state: body.state === true, anonymous: body.anonymous === true }; diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index a7be7a201..8eabdd304 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -20,7 +20,6 @@ import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; -import { createDeepAgentsMemoryFixture } from './deep-agents-memory-fixture.mjs'; import { createDeepAgentsSkillsFixture } from './deep-agents-skills-fixture.mjs'; import { createDeepAgentsSubagentsFixture } from './deep-agents-subagents-fixture.mjs'; @@ -53,7 +52,6 @@ const chatDebugFixture = configuration.library === 'chat' && configuration.topic const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; -const deepAgentsMemoryFixture = configuration.library === 'deep-agents' && configuration.topic === 'memory' ? createDeepAgentsMemoryFixture() : null; const deepAgentsSkillsFixture = configuration.library === 'deep-agents' && configuration.topic === 'skills' ? createDeepAgentsSkillsFixture() : null; const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && configuration.topic === 'subagents' ? createDeepAgentsSubagentsFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; @@ -176,7 +174,6 @@ const server = createServer(async (request, response) => { if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; if (agUiClientToolsFixture && (await agUiClientToolsFixture(request, response, pathname))) return; - if (deepAgentsMemoryFixture && (await deepAgentsMemoryFixture(request, response, pathname))) return; if (deepAgentsSkillsFixture && (await deepAgentsSkillsFixture(request, response, pathname))) return; if (deepAgentsSubagentsFixture && (await deepAgentsSubagentsFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { @@ -281,7 +278,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsMemoryFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -294,7 +291,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsMemoryFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? chatGenerativeUiFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 4077e2205b4d35047b5d4e0104dbde6460e15e88 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:04:19 -0800 Subject: [PATCH 18/41] test(cockpit): run React deep-agents/skills e2e and website preview on aimock Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- .../angular/e2e/fixtures/da-skills.json | 126 ++++ .../skills/react/e2e/global-setup-impl.ts | 6 + .../skills/react/e2e/playwright.config.ts | 3 + .../skills/react/e2e/skills.spec.ts | 56 +- cockpit/deep-agents/skills/react/project.json | 44 +- scripts/cockpit-frontend-matrix.spec.mjs | 17 + scripts/examples/e2e-wiring.spec.ts | 8 +- scripts/react-cockpit/configuration.mjs | 1 + .../deep-agents-skills-configuration.spec.mjs | 38 -- .../deep-agents-skills-fixture.mjs | 604 ------------------ .../react-cockpit/deep-agents-skills-wire.py | 160 ----- scripts/react-cockpit/serve.mjs | 7 +- 13 files changed, 210 insertions(+), 862 deletions(-) create mode 100644 cockpit/deep-agents/skills/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-skills-fixture.mjs delete mode 100644 scripts/react-cockpit/deep-agents-skills-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 43af40654..cf1117fa3 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -500,7 +500,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-deep-agents-skills-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent', + command: 'node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4631', reuseExistingServer, diff --git a/cockpit/deep-agents/skills/angular/e2e/fixtures/da-skills.json b/cockpit/deep-agents/skills/angular/e2e/fixtures/da-skills.json index c4ad31b1a..2a933a82f 100644 --- a/cockpit/deep-agents/skills/angular/e2e/fixtures/da-skills.json +++ b/cockpit/deep-agents/skills/angular/e2e/fixtures/da-skills.json @@ -1,5 +1,131 @@ { "fixtures": [ + { + "match": { "toolCallId": "skills-reads-0" }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/reference/margins.md\"}", + "id": "skills-reads-1" + } + ] + } + }, + { + "match": { "toolCallId": "skills-reads-1" }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { + "userMessage": "[scenario:reads]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/SKILL.md\"}", + "id": "skills-reads-0" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:no-read]", + "hasToolResult": false + }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { "toolCallId": "skills-partial-0" }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { + "userMessage": "[scenario:partial]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/SKILL.md\", \"offset\": 0, \"limit\": 2}", + "id": "skills-partial-0" + } + ] + } + }, + { + "match": { "toolCallId": "skills-error-0" }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { + "userMessage": "[scenario:error]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/reference/missing.md\"}", + "id": "skills-error-0" + } + ] + } + }, + { + "match": { "toolCallId": "skills-alternate-0" }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { + "userMessage": "[scenario:alternate]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/weather-brief/SKILL.md\"}", + "id": "skills-alternate-0" + } + ] + } + }, + { + "match": { "toolCallId": "skills-reads-followup-0" }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/reference/margins.md\"}", + "id": "skills-reads-followup-1" + } + ] + } + }, + { + "match": { "toolCallId": "skills-reads-followup-1" }, + "response": { "content": "Offline assessment complete." } + }, + { + "match": { + "userMessage": "[scenario:reads-followup]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "read_file", + "arguments": "{\"file_path\": \"/skills/runway-analysis/SKILL.md\"}", + "id": "skills-reads-followup-0" + } + ] + } + }, { "match": { "userMessage": "Can a mid-size jet operate out of KASE?", diff --git a/cockpit/deep-agents/skills/react/e2e/global-setup-impl.ts b/cockpit/deep-agents/skills/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..7a29da1a2 --- /dev/null +++ b/cockpit/deep-agents/skills/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'deep-agents/skills', port: 4631 }); diff --git a/cockpit/deep-agents/skills/react/e2e/playwright.config.ts b/cockpit/deep-agents/skills/react/e2e/playwright.config.ts index 18035987e..f5fc3d69e 100644 --- a/cockpit/deep-agents/skills/react/e2e/playwright.config.ts +++ b/cockpit/deep-agents/skills/react/e2e/playwright.config.ts @@ -8,6 +8,9 @@ export default defineConfig({ timeout: 60000, use: { baseURL: 'http://127.0.0.1:4631', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/deep-agents/skills/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deep-agents-skills', cwd: '../../../../..', diff --git a/cockpit/deep-agents/skills/react/e2e/skills.spec.ts b/cockpit/deep-agents/skills/react/e2e/skills.spec.ts index 7381d8b65..c260e0e59 100644 --- a/cockpit/deep-agents/skills/react/e2e/skills.spec.ts +++ b/cockpit/deep-agents/skills/react/e2e/skills.spec.ts @@ -7,30 +7,40 @@ import { } from '@playwright/test'; import { mkdirSync, writeFileSync } from 'node:fs'; const base = '/deep-agents/skills/react/'; +// Backend: aimock replays cockpit/deep-agents/skills/angular/e2e/fixtures +// (shared with the Angular example; React scenarios are tagged prompts, listed +// first so they win over the recorded prompt they contain) into a real +// `langgraph dev`. The proxy records requests and holds or fails the next call +// (scripts/react-cockpit/langgraph-proxy.mjs). +let scenario = 'reads'; async function configure( request: APIRequestContext, - scenario = 'reads', + next = 'reads', extra: Record = {} ) { - expect( - ( - await request.post('/__configure', { - data: { - scenario, - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - ...extra, - }, - }) - ).ok() - ).toBe(true); + const control = async (path: string, data: object = {}) => + expect((await request.post(path, { data })).ok()).toBe(true); + scenario = next; + // The saved-conversation confirmation reads the exact checkpoint. + if (extra.holdCheckpoint) await control('/__hold/state'); + if (extra.holdStream) await control('/__hold/stream', { immediate: true }); + if (extra.failCreation) await control('/__fail-create'); + if (extra.failStream) await control('/__fail-stream'); + await control(extra.failCheckpoint ? '/__state/failure' : '/__state/safe'); +} +async function release(request: APIRequestContext) { + await request.post('/__release-state'); + await request.post('/__release'); } +test.beforeEach(async ({ request }) => { + await request.post('/__reset'); + scenario = 'reads'; +}); async function send(page: Page, text = 'What is the field data for KSFO?') { - await page.getByRole('textbox', { name: 'Message' }).fill(text); + // The scenario tag selects the scripted model turns (shared fixtures). + await page + .getByRole('textbox', { name: 'Message' }) + .fill(`${text} [scenario:${scenario}]`); await page.getByRole('button', { name: 'Send', exact: true }).click(); } async function saved(page: Page) { @@ -44,7 +54,7 @@ async function proof( testInfo: TestInfo ) { mkdirSync(testInfo.outputDir, { recursive: true }); - for (const endpoint of ['requests', 'graph-proof', 'raw-events']) + for (const endpoint of ['requests', 'captures']) writeFileSync( testInfo.outputPath(`${name}-${endpoint}.json`), JSON.stringify( @@ -83,6 +93,8 @@ test('actual Skills root index, successful literal reads, followup and exact ret '/skills/runway-analysis/reference/margins.md' ); const prior = await page.getByLabel('Saved conversations').inputValue(); + // Same reads, with their own tool call ids. + await configure(request, 'reads-followup'); await send(page, 'Followup'); await saved(page); await expect(page.locator('[data-read-kind="inspected"]')).toHaveCount(4); @@ -147,7 +159,7 @@ test('New rejects stale held checkpoint and clears draft without replay', async 'Confirming saved conversation…' ); await page.getByRole('button', { name: 'New conversation' }).click(); - await request.post('/__release', { data: {} }); + await release(request); await expect(page.getByRole('status')).toHaveText('Ready.'); await expect(page.locator('[data-read-kind]')).toHaveCount(0); await expect(page.getByRole('textbox')).toHaveValue(''); @@ -162,7 +174,7 @@ test('Stop and failed transport require New and never replay', async ({ await send(page); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.getByRole('textbox')).toBeDisabled(); - await request.post('/__release', { data: {} }); + await release(request); await page.getByRole('button', { name: 'New conversation' }).click(); await configure(request, 'reads', { failStream: true }); await send(page); @@ -180,7 +192,7 @@ test('duplicate Send and exact confirmation failure cannot silently replay', asy const before = (await (await request.get('/__requests')).json()).length; await page .getByRole('textbox') - .fill('Can a mid-size jet operate out of KASE?'); + .fill(`Can a mid-size jet operate out of KASE? [scenario:${scenario}]`); await page .getByRole('button', { name: 'Send', exact: true }) .evaluate((element) => { diff --git a/cockpit/deep-agents/skills/react/project.json b/cockpit/deep-agents/skills/react/project.json index 2d7a2d6af..4a4ff7f5b 100644 --- a/cockpit/deep-agents/skills/react/project.json +++ b/cockpit/deep-agents/skills/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/deep-agents/skills/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-deep-agents-skills-angular", + "cockpit-deep-agents-skills-python" + ], "tags": [ "scope:cockpit-examples", "framework:react", @@ -21,7 +25,12 @@ "react" ] }, - { "target": "react-langgraph", "projects": ["website-runtime-preparation"] } + { + "target": "react-langgraph", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/deep-agents/skills/react" @@ -30,13 +39,6 @@ "command": "node scripts/react-cockpit/build.mjs deep-agents-skills" } }, - "configuration-test": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs" - } - }, "test": { "executor": "@nx/vitest:test", "options": { @@ -52,34 +54,22 @@ "eslintConfig": "cockpit/deep-agents/skills/react/eslint.config.mjs" } }, - "fixture-test": { - "executor": "nx:run-commands", + "e2e": { + "executor": "@nx/playwright:playwright", "cache": false, + "inputs": [ + "default", + "reactAimockBackend" + ], "dependsOn": [ + "build", { "target": "test", "projects": [ "cockpit-deep-agents-skills-python" ] - }, - { - "target": "build", - "projects": [ - "core" - ] } ], - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-skills-fixture.spec.mjs scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs" - } - }, - "e2e": { - "executor": "@nx/playwright:playwright", - "cache": false, - "dependsOn": [ - "build", - "fixture-test" - ], "options": { "config": "cockpit/deep-agents/skills/react/e2e/playwright.config.ts", "skipInstall": true diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index a444d776a..f7559fd34 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -63,6 +63,23 @@ test('Planning backend changes select exactly both canonical frontends', () => { assert.deepEqual(selected.map(cap => cap.angular).sort(), ['cockpit-deep-agents-planning-angular', 'cockpit-deep-agents-planning-react']); assert.ok(selected.every(cap => cap.python === 'cockpit/deep-agents/planning/python')); }); +test('Skills and Subagents own exact closed native React identities', async () => { + const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); + for (const [topic, port] of [['skills', 4631]]) { + const configuration = reactCockpitConfiguration(`deep-agents-${topic}`); + assert.deepEqual(configuration, { + topic, library: 'deep-agents', adapter: 'langgraph', appPath: `cockpit/deep-agents/${topic}/react`, + base: `/deep-agents/${topic}/react/`, port, project: `cockpit-deep-agents-${topic}-react`, + backend: { + kind: 'aimock-langgraph', python: `cockpit/deep-agents/${topic}/python`, + fixtures: `cockpit/deep-agents/${topic}/angular/e2e/fixtures`, langgraphPort: port + 1000, + }, + }); + assert.ok(Object.isFrozen(configuration)); + for (const key of [topic, `deep-agents/${topic}`, `../deep-agents-${topic}`, `deep-agents-${topic}/../../foreign`]) + assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); + } +}); test('Planning owns an exact closed native React identity', async () => { const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); assert.deepEqual(reactCockpitConfiguration('deep-agents-planning'), { diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index 2f6f8132c..64a62fd0d 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -24,18 +24,16 @@ it('wires native Planning build, aimock e2e and website coverage to the shared P expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4628'"); }); -it('wires native Skills build, fixture and website coverage to the shared Python graph', () => { +it('wires native Skills build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); const react = read('cockpit/deep-agents/skills/react/project.json'); expect(react.tags).toEqual(expect.arrayContaining(['framework:react', 'scope:cockpit-e2e', 'scope:cockpit-examples'])); expect(react.targets.build.options.command).toBe('node scripts/react-cockpit/build.mjs deep-agents-skills'); - expect(react.targets.e2e.dependsOn).toEqual(['build', 'fixture-test']); - expect(react.targets['fixture-test'].dependsOn).toContainEqual({ target: 'test', projects: ['cockpit-deep-agents-skills-python'] }); - expect(react.targets['fixture-test'].dependsOn).toContainEqual({ target: 'build', projects: ['core'] }); + expect(react.targets.e2e.dependsOn).toEqual(['build', { target: 'test', projects: ['cockpit-deep-agents-skills-python'] }]); const website = read('apps/website/project.json'); expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-skills-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); - expect(config).toContain('cockpit-deep-agents-skills-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent'); + expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4631'"); }); it('wires native Subagents build, fixture and website coverage to the shared Python graph', () => { diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index c57259847..7ed59e4b5 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -72,6 +72,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { appPath: 'cockpit/deep-agents/skills/react', base: '/deep-agents/skills/react/', port: 4631, project: 'cockpit-deep-agents-skills-react', + backend: aimockLanggraph('deep-agents/skills', 4631), }); if (topic === 'deep-agents-subagents') return Object.freeze({topic:'subagents',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/subagents/react',base:'/deep-agents/subagents/react/',port:4630,project:'cockpit-deep-agents-subagents-react'}); diff --git a/scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs b/scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs deleted file mode 100644 index 91ea43676..000000000 --- a/scripts/react-cockpit/deep-agents-skills-configuration.spec.mjs +++ /dev/null @@ -1,38 +0,0 @@ -import assert from 'node:assert/strict'; -import { test } from 'node:test'; -import { reactCockpitConfiguration } from './configuration.mjs'; - -test('Skills has a closed native React selection', () => { - const configuration = reactCockpitConfiguration('deep-agents-skills'); - assert.deepEqual(configuration, { - topic: 'skills', - library: 'deep-agents', - adapter: 'langgraph', - appPath: 'cockpit/deep-agents/skills/react', - base: '/deep-agents/skills/react/', - port: 4631, - project: 'cockpit-deep-agents-skills-react', - }); - assert.ok(Object.isFrozen(configuration)); - assert.throws(() => reactCockpitConfiguration('deep-agents/../../foreign')); - assert.throws(() => - reactCockpitConfiguration('deep-agents-skills/../../foreign') - ); -}); - -import { readFileSync } from 'node:fs'; -test('Skills fixture uses only closed authored selection and awaited shutdown', () => { - const source = readFileSync(new URL('./serve.mjs', import.meta.url), 'utf8'); - assert.match( - source, - /configuration.library === 'deep-agents' && configuration.topic === 'skills' \? createDeepAgentsSkillsFixture\(\)/ - ); - assert.match( - source, - /if \((?:(?:\w+Fixture|isChatA2ui) \|\| )*deepAgentsSkillsFixture(?: \|\| (?:\w+Fixture|isChatA2ui))*\)/ - ); - assert.match( - source, - /await \((?:\w+Fixture \?\? )*deepAgentsSkillsFixture(?: \?\? \w+Fixture)*\)\.close\(\)/ - ); -}); diff --git a/scripts/react-cockpit/deep-agents-skills-fixture.mjs b/scripts/react-cockpit/deep-agents-skills-fixture.mjs deleted file mode 100644 index 5dd9ca109..000000000 --- a/scripts/react-cockpit/deep-agents-skills-fixture.mjs +++ /dev/null @@ -1,604 +0,0 @@ -/** Loopback-only SDK transport backed by the actual compiled Skills graph. */ -import { spawn } from 'node:child_process'; -import { readFileSync, readdirSync } from 'node:fs'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, timeout) { - const child = spawnWorker(); - const pending = new Map(); - let sequence = 0, - dead = false, - closing; - const ended = new Promise((resolve) => child.once('close', resolve)); - const maxResponseBytes = 16 * 1024 * 1024; - let frame = Buffer.alloc(0), - frameBytes = 0; - child.stderr.resume(); - function fail() { - dead = true; - frame = Buffer.alloc(0); - frameBytes = 0; - for (const item of pending.values()) { - clearTimeout(item.timer); - item.reject(Error('Skills graph worker unavailable.')); - } - pending.clear(); - } - const terminate = () => { - fail(); - child.kill('SIGTERM'); - }; - child.once('error', fail); - child.once('close', fail); - child.stdin.on('error', terminate); - child.stdout.once('error', terminate); - child.stdout.once('end', () => { - if (!dead) terminate(); - }); - child.stdout.on('data', (bytes) => { - if (dead) return; - try { - let start = 0; - while (start < bytes.length && !dead) { - const newline = bytes.indexOf(0x0a, start); - const end = newline < 0 ? bytes.length : newline; - const needed = frameBytes + end - start; - // Count each raw frame before copying or decoding, including frames - // whose delimiter has not arrived. Never grow storage beyond the cap. - if (needed > maxResponseBytes) throw Error('Oversized response'); - if (needed > frame.length) { - const grown = Buffer.allocUnsafe( - Math.min(maxResponseBytes, Math.max(needed, frame.length * 2, 4096)) - ); - frame.copy(grown, 0, 0, frameBytes); - frame = grown; - } - bytes.copy(frame, frameBytes, start, end); - frameBytes = needed; - if (newline < 0) return; - const result = JSON.parse(frame.toString('utf8', 0, frameBytes)); - frameBytes = 0; - const item = pending.get(result.requestId); - if (!item) throw Error('Unowned response'); - pending.delete(result.requestId); - clearTimeout(item.timer); - if (result.error) item.reject(Error('Skills graph operation failed.')); - else item.resolve(result); - start = newline + 1; - } - } catch { - terminate(); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(Error('Skills graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const wire = JSON.stringify({ ...value, requestId }) + '\n'; - if (Buffer.byteLength(wire) > 1024 * 1024) { - reject(Error('Oversized request')); - return; - } - const timer = setTimeout(terminate, timeout); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(wire); - }); - }, - close() { - return (closing ??= (async () => { - fail(); - child.stdin.end(); - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - })()); - }, - }; -} - -export function createDeepAgentsSkillsFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/skills/python', - 'python', - 'scripts/react-cockpit/deep-agents-skills-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-skills-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const sourceRoot = new URL( - '../../cockpit/deep-agents/skills/python/', - import.meta.url - ); - const skillAssets = readdirSync(new URL('skills/', sourceRoot), { - recursive: true, - withFileTypes: true, - }) - .filter((e) => e.isFile()) - .map( - (e) => - 'skills/' + - e.parentPath.slice(new URL('skills/', sourceRoot).pathname.length) + - (e.parentPath.endsWith('/') ? '' : '/') + - e.name - ); - const sourceSha256 = Object.fromEntries( - [ - 'src/graph.py', - 'src/skills_readonly.py', - 'tests/test_streaming.py', - 'prompts/skills.md', - 'uv.lock', - ...skillAssets, - ].map((path) => [ - path, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/deep-agents/skills/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - closed = false, - closing, - queue = Promise.resolve(); - // Real execution is buffered before delivery; transport cancellation does not roll back server graph work. - const threads = new Set(), - runs = new Map(), - held = new Set(), - rawEvents = [], - proofs = [], - requests = []; - const options = { - scenario: 'reads', - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - }; - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value), - proof = result.proof; - if ( - closed || - proof?.actualCompiledGraph !== true || - proof.networkConnectAttempts !== 0 || - proof.op !== value.op || - proof.threadId !== (value.threadId ?? null) || - Object.keys(proof.sourceSha256 ?? {}).length !== - Object.keys(sourceSha256).length || - Object.entries(sourceSha256).some( - ([path, hash]) => proof.sourceSha256[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - proofs.push(proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function hold(response, finish, run) { - const item = { response, finish, run }; - held.add(item); - response.once('close', () => { - if (held.delete(item) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - const chunks = []; - let byteLength = 0; - for await (const chunk of request) { - const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); - byteLength += bytes.length; - if (byteLength > 1024 * 1024) { - json(response, {}, 413); - return true; - } - chunks.push(bytes); - } - const input = Buffer.concat(chunks, byteLength).toString('utf8'); - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__raw-events') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, rawEvents); - return true; - } - if (pathname === '/__graph-proof') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, proofs); - return true; - } - if (pathname === '/__requests') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - if (request.method !== 'POST') { - json(response, {}, 405); - return true; - } - if ( - !only(body, [ - 'scenario', - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ]) || - ('scenario' in body && - !['reads', 'no-read', 'partial', 'error', 'alternate'].includes( - body.scenario - )) || - [ - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ].some((k) => k in body && typeof body[k] !== 'boolean') - ) { - json(response, {}, 400); - return true; - } - if ('scenario' in body) - await operate({ op: 'configure', scenario: body.scenario }); - Object.assign(options, body); - json(response, {}); - return true; - } - if (pathname === '/__release') { - if (request.method !== 'POST' || !only(body, [])) { - json(response, {}, 400); - return true; - } - for (const item of held) { - held.delete(item); - item.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - if (requests.length >= 2000 || threads.size >= 100 || runs.size >= 1000) { - json(response, {}, 429); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if (options.failCreation) { - options.failCreation = false; - json(response, { error: 'Controlled creation failure.' }, 500); - return true; - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - !only(body, ['thread_id', 'if_exists', 'metadata']) || - body.if_exists !== 'raise' || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - await operate({ op: 'create', threadId: body.thread_id }); - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if (suffix === '/state' && input.length !== 0) { - json(response, {}, 400); - return true; - } - if (suffix === '/state/checkpoint') { - if (options.failCheckpoint) { - options.failCheckpoint = false; - json(response, { error: 'Controlled exact read failure.' }, 500); - return true; - } - if ( - !only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - body.checkpoint.thread_id !== threadId || - typeof body.checkpoint.checkpoint_id !== 'string' || - typeof body.checkpoint.checkpoint_ns !== 'string' - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - if ( - !history.some( - (s) => - s.checkpoint.checkpoint_id === body.checkpoint.checkpoint_id && - s.checkpoint.checkpoint_ns === body.checkpoint.checkpoint_ns - ) || - (body.checkpoint.checkpoint_map !== undefined && - (!only(body.checkpoint.checkpoint_map, [ - body.checkpoint.checkpoint_ns, - ]) || - Object.values(body.checkpoint.checkpoint_map).some( - (id) => id !== body.checkpoint.checkpoint_id - ))) - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - ...(suffix === '/state/checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }); - const finish = () => json(response, state); - if ( - options.holdState || - (options.holdCheckpoint && suffix === '/state/checkpoint') - ) - hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit']) || - (body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100)) - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - json(response, history.slice(0, body.limit ?? 10)); - return true; - } - const runRoute = /^\/runs\/([^/]+)(\/cancel)?$/.exec(suffix); - if ( - runRoute && - ((!runRoute[2] && request.method === 'GET') || - (runRoute[2] && request.method === 'POST')) - ) { - if (input.length !== 0) { - json(response, {}, 400); - return true; - } - const run = runs.get(runRoute[1]); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - if (runRoute[2]) { - const search = new URL(request.url, 'http://localhost').searchParams; - if (search.get('action') !== 'interrupt') { - json(response, {}, 400); - return true; - } - for (const item of held) - if (item.run === run) { - held.delete(item); - run.status = 'interrupted'; - item.response.end(); - } - json(response, {}); - } else json(response, run); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - if (options.failStream) { - options.failStream = false; - json(response, { error: 'Controlled stream failure.' }, 500); - return true; - } - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'command', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !( - body.command === undefined && - only(body.input, ['messages']) && - body.input.messages?.length === 1 && - only(message, ['type', 'id', 'content']) && - message.type === 'human' && - typeof message.id === 'string' && - message.id.trim() && - typeof message.content === 'string' - ) || - body.assistant_id !== 'da-skills' || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'checkpoints', - 'custom', - ].includes(mode) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - rawEvents.push({ threadId, runId: run.run_id, events: wire.events }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const finish = () => { - if (response.destroyed || closed) return; - for (const [namespace, kind, data] of wire.events) - if ( - modes.has(kind) && - (!namespace.length || body.stream_subgraphs === true) - ) - response.write(event([kind, ...namespace].join('|'), data)); - run.status = 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Skills graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const item of held) item.response.end(); - held.clear(); - await worker?.close(); - })()); - return handle; -} diff --git a/scripts/react-cockpit/deep-agents-skills-wire.py b/scripts/react-cockpit/deep-agents-skills-wire.py deleted file mode 100644 index 7dcb0367e..000000000 --- a/scripts/react-cockpit/deep-agents-skills-wire.py +++ /dev/null @@ -1,160 +0,0 @@ -"""Bounded newline-JSON worker running the unchanged compiled Skills builder.""" -import asyncio -from dataclasses import asdict, is_dataclass -from contextlib import redirect_stdout -from copy import deepcopy -import hashlib -import json -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/deep-agents/skills/python" -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel, load - -SCENARIOS = { - "reads": [("read_file", {"file_path":"/skills/runway-analysis/SKILL.md"}), ("read_file", {"file_path":"/skills/runway-analysis/reference/margins.md"})], - "no-read": [], - "partial": [("read_file", {"file_path":"/skills/runway-analysis/SKILL.md", "offset":0,"limit":2})], - "error": [("read_file", {"file_path":"/skills/runway-analysis/reference/missing.md"})], - "alternate": [("read_file", {"file_path":"/skills/weather-brief/SKILL.md"})], -} - -SOURCES = ("src/graph.py", "src/skills_readonly.py", "tests/test_streaming.py", "prompts/skills.md", "uv.lock", *sorted(str(p.relative_to(PROJECT)) for p in (PROJECT/"skills").rglob("*") if p.is_file())) -MAX_REQUEST = 1024 * 1024 -MAX_RESPONSE = 16 * 1024 * 1024 - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if is_dataclass(value): - return asdict(value) - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() - if k in ("thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map")}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": t.id, "name": t.name, - **({"error": t.error} if t.error is not None else {}), - **({"state": t.state} if t.state is not None else {}), - "interrupts": [{"id": item.id, "value": item.value} for item in t.interrupts]} - for t in state.tasks], - } - - -async def main(): - model = LocalModel() - scenario = "reads" - owners = set() - seen = set() - request_ids = set() - provenance = {} - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as network: - with redirect_stdout(sys.stderr): - _, graph = load(model) - while line := sys.stdin.buffer.readline(MAX_REQUEST + 1): - request = {} - try: - if len(line) > MAX_REQUEST or not line.endswith(b"\n"): - raise ValueError("Oversized request") - request = json.loads(line) - if not isinstance(request, dict) or set(request) - {"requestId", "op", "threadId", "scenario", "checkpoint", "messages"}: - raise ValueError("Malformed request") - if type(request.get("requestId")) is not int or request["requestId"] <= 0 or request["requestId"] in request_ids or len(request_ids) >= 2000: - raise ValueError("Invalid/replayed request id or lifetime limit") - request_ids.add(request["requestId"]) - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - op = request["op"] - fields = {"create": {"threadId"}, "configure": {"scenario"}, "state": {"threadId", "checkpoint"}, - "history": {"threadId"}, "submit": {"threadId", "messages"}, "resume": {"threadId"}} - if op not in fields or set(request) - ({"requestId", "op"} | fields[op]): - raise ValueError("Unsupported operation fields") - if op == "create": - if not isinstance(request.get("threadId"), str) or not request["threadId"] or request["threadId"] in owners or len(owners) >= 100: - raise ValueError("Invalid or duplicate owner") - owners.add(request["threadId"]) - elif op != "configure" and request.get("threadId") not in owners: - raise ValueError("Unknown owner") - if "checkpoint" in request: - requested_checkpoint = request["checkpoint"] - if (not isinstance(requested_checkpoint, dict) - or requested_checkpoint.get("thread_id") != config["configurable"]["thread_id"] - or set(requested_checkpoint) - {"thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map"} - or not isinstance(requested_checkpoint.get("checkpoint_id"), str) - or not requested_checkpoint["checkpoint_id"] - or not isinstance(requested_checkpoint.get("checkpoint_ns"), str)): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - if "checkpoint_map" in requested_checkpoint and ( - not isinstance(requested_checkpoint["checkpoint_map"], dict) - or set(requested_checkpoint["checkpoint_map"]) != {requested_checkpoint["checkpoint_ns"]} - or requested_checkpoint["checkpoint_map"][requested_checkpoint["checkpoint_ns"]] != requested_checkpoint["checkpoint_id"]): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - known = [s async for s in graph.aget_state_history(config)] - if not any(s.config["configurable"].get("checkpoint_id") == requested_checkpoint.get("checkpoint_id") and s.config["configurable"].get("checkpoint_ns", "") == requested_checkpoint.get("checkpoint_ns") for s in known): - raise ValueError("Unknown checkpoint") - config["configurable"].update({k: v for k, v in requested_checkpoint.items() if k != "thread_id"}) - op = request["op"] - if op == "configure": - scenario = request["scenario"] - if scenario not in SCENARIOS: - raise ValueError("Unsupported scenario") - result = {} - elif op == "create": - result = {} - elif op == "state": - result = {"state": checkpoint(await graph.aget_state(config))} - elif op == "history": - result = {"history": [checkpoint(s) async for s in graph.aget_state_history(config)]} - elif op == "submit": - current = await graph.aget_state(config) - if any(t.interrupts for t in current.tasks): - raise ValueError("Cannot submit over paused batch") - messages = request.get("messages") - if not isinstance(messages, list) or len(messages) != 1: - raise ValueError("Expected one human message") - message = messages[0] - if not isinstance(message, dict) or set(message) != {"type", "id", "content"} or message["type"] != "human" or not isinstance(message["id"], str) or not message["id"].strip() or not isinstance(message["content"], str): - raise ValueError("Invalid human message") - identity = message["id"] - if (request["threadId"], identity) in seen: - raise ValueError("Replayed human message") - seen.add((request["threadId"], identity)) - model.calls = SCENARIOS[scenario] - provenance[request["threadId"]] = scenario - events = [] - async for namespace, kind, event in graph.astream( - {"messages": request["messages"]}, config, - subgraphs=True, stream_mode=["messages", "values", "updates", "checkpoints", "custom"]): - events.append([list(namespace), kind, deepcopy(event)]) - result = {"events": events, "state": checkpoint(await graph.aget_state(config))} - else: - raise ValueError("Unsupported operation") - network.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, "op": op, - "threadId": request.get("threadId"), "scenario": provenance.get(request.get("threadId"), scenario), - "sourceSha256": {p: hashlib.sha256((PROJECT / p).read_bytes()).hexdigest() for p in SOURCES}, - "networkConnectAttempts": network.call_count, - } - result["requestId"] = request["requestId"] - wire = json.dumps(result, default=encode) - if len(wire.encode()) > MAX_RESPONSE: - raise ValueError("Oversized response") - print(wire, flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": (request.get("requestId") if isinstance(request, dict) else None), "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 8eabdd304..8925a37e8 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -20,7 +20,6 @@ import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; -import { createDeepAgentsSkillsFixture } from './deep-agents-skills-fixture.mjs'; import { createDeepAgentsSubagentsFixture } from './deep-agents-subagents-fixture.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); @@ -52,7 +51,6 @@ const chatDebugFixture = configuration.library === 'chat' && configuration.topic const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; -const deepAgentsSkillsFixture = configuration.library === 'deep-agents' && configuration.topic === 'skills' ? createDeepAgentsSkillsFixture() : null; const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && configuration.topic === 'subagents' ? createDeepAgentsSubagentsFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by @@ -174,7 +172,6 @@ const server = createServer(async (request, response) => { if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; if (agUiClientToolsFixture && (await agUiClientToolsFixture(request, response, pathname))) return; - if (deepAgentsSkillsFixture && (await deepAgentsSkillsFixture(request, response, pathname))) return; if (deepAgentsSubagentsFixture && (await deepAgentsSubagentsFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { if (request.method !== 'POST') return json(response, {}, 405); @@ -278,7 +275,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsSubagentsFixture || deepAgentsSkillsFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsSubagentsFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -291,7 +288,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsSubagentsFixture ?? deepAgentsSkillsFixture ?? chatGenerativeUiFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsSubagentsFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 109dc1a861f8f2ad9db673375bfa76be020ed6b6 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:08:54 -0800 Subject: [PATCH 19/41] test(cockpit): run React deep-agents/subagents e2e and website preview on aimock Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- .../e2e/fixtures/da-subagents-fanout.json | 354 +++++++++++ .../subagents/react/e2e/global-setup-impl.ts | 6 + .../subagents/react/e2e/playwright.config.ts | 3 + .../subagents/react/e2e/subagents.spec.ts | 86 +-- .../deep-agents/subagents/react/project.json | 31 +- scripts/cockpit-frontend-matrix.spec.mjs | 6 +- scripts/examples/e2e-wiring.spec.ts | 7 +- scripts/react-cockpit/configuration.mjs | 2 +- ...ep-agents-subagents-configuration.spec.mjs | 19 - .../deep-agents-subagents-fixture.mjs | 589 ------------------ .../deep-agents-subagents-wire.py | 152 ----- scripts/react-cockpit/serve.mjs | 7 +- 13 files changed, 437 insertions(+), 827 deletions(-) create mode 100644 cockpit/deep-agents/subagents/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/deep-agents-subagents-configuration.spec.mjs delete mode 100644 scripts/react-cockpit/deep-agents-subagents-fixture.mjs delete mode 100644 scripts/react-cockpit/deep-agents-subagents-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index cf1117fa3..283b63c86 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -493,7 +493,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-deep-agents-subagents-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent', + command: 'node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4630', reuseExistingServer, diff --git a/cockpit/deep-agents/subagents/angular/e2e/fixtures/da-subagents-fanout.json b/cockpit/deep-agents/subagents/angular/e2e/fixtures/da-subagents-fanout.json index 2540c4d08..fd1591d6b 100644 --- a/cockpit/deep-agents/subagents/angular/e2e/fixtures/da-subagents-fanout.json +++ b/cockpit/deep-agents/subagents/angular/e2e/fixtures/da-subagents-fanout.json @@ -1,5 +1,359 @@ { "fixtures": [ + { + "match": { + "userMessage": "Research KSFO dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_field_elevation", + "arguments": "{\"airport\": \"KSFO\"}" + }, + { + "name": "lookup_runway_length", + "arguments": "{\"airport\": \"KSFO\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KSFO dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KSFO field elevation and runway length." } + }, + { + "match": { + "userMessage": "Research KSFO dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_weather", + "arguments": "{\"airport\": \"KSFO\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KSFO dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KSFO weather." } + }, + { + "match": { + "userMessage": "Research KASE dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_field_elevation", + "arguments": "{\"airport\": \"KASE\"}" + }, + { + "name": "lookup_runway_length", + "arguments": "{\"airport\": \"KASE\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KASE dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KASE field elevation and runway length." } + }, + { + "match": { + "userMessage": "Research KASE dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_weather", + "arguments": "{\"airport\": \"KASE\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KASE dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KASE weather." } + }, + { + "match": { + "userMessage": "Research KDEN dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_field_elevation", + "arguments": "{\"airport\": \"KDEN\"}" + }, + { + "name": "lookup_runway_length", + "arguments": "{\"airport\": \"KDEN\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KDEN dispatch.", + "systemMessage": "You research airport field data", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KDEN field elevation and runway length." } + }, + { + "match": { + "userMessage": "Research KDEN dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_weather", + "arguments": "{\"airport\": \"KDEN\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Research KDEN dispatch.", + "systemMessage": "You read weather for a dispatch desk", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: KDEN weather." } + }, + { + "match": { + "userMessage": "Invalid lookup KSFO dispatch.", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "lookup_field_elevation", + "arguments": "{}" + }, + { + "name": "lookup_runway_length", + "arguments": "{}" + } + ] + } + }, + { + "match": { + "userMessage": "Invalid lookup KSFO dispatch.", + "hasToolResult": true + }, + "response": { "content": "Observed lookup: Error invoking tool 'lookup_field_elevation' (airport: Field required)." } + }, + { + "match": { "userMessage": "General-purpose local report." }, + "response": { "content": "General-purpose local report ready." } + }, + { + "match": { + "userMessage": "[scenario:single]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:single]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"description\": \"Research KSFO dispatch.\", \"subagent_type\": \"field-researcher\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:parallel]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:parallel]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"field-researcher\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"weather-analyst\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KDEN dispatch.\", \"subagent_type\": \"field-researcher\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KDEN dispatch.\", \"subagent_type\": \"weather-analyst\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:duplicates]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:duplicates]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"field-researcher\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"field-researcher\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"field-researcher\"}" + }, + { + "name": "task", + "arguments": "{\"description\": \"Research KASE dispatch.\", \"subagent_type\": \"field-researcher\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:tool-error]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:tool-error]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"description\": \"Invalid lookup KSFO dispatch.\", \"subagent_type\": \"field-researcher\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:general-purpose]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:general-purpose]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"description\": \"General-purpose local report.\", \"subagent_type\": \"general-purpose\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:default-tool]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:default-tool]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "ls", + "arguments": "{\"path\": \"/\"}" + } + ] + } + }, + { + "match": { + "userMessage": "[scenario:root-tool-error]", + "hasToolResult": true + }, + "response": { "content": "Dispatch assessment: the specialists reported back." } + }, + { + "match": { + "userMessage": "[scenario:root-tool-error]", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"subagent_type\": \"field-researcher\"}" + } + ] + } + }, + { + "match": { "userMessage": "[scenario:no-task]" }, + "response": { "content": "No delegation requested." } + }, { "match": { "userMessage": "Analyze the current weather conditions at Denver International Airport (KDEN) and explain the operational impact for departures and arrivals.", diff --git a/cockpit/deep-agents/subagents/react/e2e/global-setup-impl.ts b/cockpit/deep-agents/subagents/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..b72a5c602 --- /dev/null +++ b/cockpit/deep-agents/subagents/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'deep-agents/subagents', port: 4630 }); diff --git a/cockpit/deep-agents/subagents/react/e2e/playwright.config.ts b/cockpit/deep-agents/subagents/react/e2e/playwright.config.ts index c3b472d0a..e41621481 100644 --- a/cockpit/deep-agents/subagents/react/e2e/playwright.config.ts +++ b/cockpit/deep-agents/subagents/react/e2e/playwright.config.ts @@ -8,6 +8,9 @@ export default defineConfig({ timeout: 60000, use: { baseURL: 'http://127.0.0.1:4630', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/deep-agents/subagents/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs deep-agents-subagents', cwd: '../../../../..', diff --git a/cockpit/deep-agents/subagents/react/e2e/subagents.spec.ts b/cockpit/deep-agents/subagents/react/e2e/subagents.spec.ts index 3520d1b7e..64582adcf 100644 --- a/cockpit/deep-agents/subagents/react/e2e/subagents.spec.ts +++ b/cockpit/deep-agents/subagents/react/e2e/subagents.spec.ts @@ -7,30 +7,40 @@ import { } from '@playwright/test'; import { mkdirSync, writeFileSync } from 'node:fs'; const base = '/deep-agents/subagents/react/'; +// Backend: aimock replays cockpit/deep-agents/subagents/angular/e2e/fixtures +// (shared with the Angular example; React scenarios are tagged prompts, listed +// first so they win over the recorded prompts they contain) into a real +// `langgraph dev`, whose real child graphs run each dispatched specialist. The +// proxy records requests and holds or fails the next call +// (scripts/react-cockpit/langgraph-proxy.mjs). +let scenario = 'single'; async function configure( request: APIRequestContext, - scenario = 'single', + next = 'single', extra: Record = {} ) { - expect( - ( - await request.post('/__configure', { - data: { - scenario, - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - ...extra, - }, - }) - ).ok() - ).toBe(true); + const control = async (path: string, data: object = {}) => + expect((await request.post(path, { data })).ok()).toBe(true); + scenario = next; + // The saved-conversation confirmation reads the exact checkpoint. + if (extra.holdCheckpoint) await control('/__hold/state'); + if (extra.holdStream) await control('/__hold/stream', { immediate: true }); + if (extra.failStream) await control('/__fail-stream'); + await control(extra.failCheckpoint ? '/__state/failure' : '/__state/safe'); +} +async function release(request: APIRequestContext) { + await request.post('/__release-state'); + await request.post('/__release'); } +test.beforeEach(async ({ request }) => { + await request.post('/__reset'); + scenario = 'single'; +}); async function send(page: Page, text = 'What is the field data for KSFO?') { - await page.getByRole('textbox', { name: 'Message' }).fill(text); + // The scenario tag selects the scripted model turns (shared fixtures). + await page + .getByRole('textbox', { name: 'Message' }) + .fill(`${text} [scenario:${scenario}]`); await page.getByRole('button', { name: 'Send', exact: true }).click(); } /** The application's own status under the composer; Chat's tool rows keep their own. */ @@ -47,7 +57,7 @@ async function proof( testInfo: TestInfo ) { mkdirSync(testInfo.outputDir, { recursive: true }); - for (const endpoint of ['requests', 'graph-proof', 'raw-events']) + for (const endpoint of ['requests', 'captures']) writeFileSync( testInfo.outputPath(`${name}-${endpoint}.json`), JSON.stringify( @@ -58,26 +68,18 @@ async function proof( ); } async function canonicalCards(page: Page, request: APIRequestContext) { - const runs = (await (await request.get('/__raw-events')).json()) as { - events: [ - string[], - string, - { - messages?: { - id: string; - type: string; - content: string; - tool_call_id?: string; - tool_calls?: { id: string; name: string; args: unknown }[]; - }[]; - } - ][]; + // The root messages the real server saved for the latest run. + const runs = (await (await request.get('/__captures')).json()) as { + savedMessages: { + id: string; + type: string; + content: string; + tool_call_id?: string; + tool_calls?: { id: string; name: string; args: unknown }[]; + }[]; }[]; - const messages = runs - .at(-1) - ?.events.filter((e) => e[0].length === 0 && e[1] === 'values') - .at(-1)?.[2].messages; - if (!messages) throw Error('Missing actual root events'); + const messages = runs.at(-1)?.savedMessages; + if (!messages?.length) throw Error('Missing actual root messages'); const calls = messages.flatMap((m) => m.tool_calls ?? []); // Chat's default rows: a subagent card in place of each task call, a tool row for any other. expect( @@ -250,7 +252,7 @@ test('Stop and New invalidate held delivery without hidden replay', async ({ await expect(status(page)).toContainText('stopped'); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(page.getByRole('textbox')).toBeEnabled(); - await request.post('/__release', { data: {} }); + await release(request); await expect(page.locator('.tp-subagent-card')).toHaveCount(0); const requests = (await (await request.get('/__requests')).json()).slice( before @@ -284,7 +286,7 @@ test('active New isolates a held saved checkpoint and cannot resurrect it', asyn ); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(page.getByRole('textbox')).toBeEnabled(); - await request.post('/__release', { data: {} }); + await release(request); await expect(page.locator('.tp-subagent-card')).toHaveCount(0); await expect(status(page)).toHaveText('Ready.'); await proof(request, 'new-pending-checkpoint', testInfo); @@ -316,7 +318,9 @@ test('duplicate Send and transport failure cannot cause automatic replay', async await configure(request, 'single', { failStream: true }); await page.goto(base); const before = (await (await request.get('/__requests')).json()).length; - await page.getByRole('textbox').fill('What is the field data for KSFO?'); + await page + .getByRole('textbox') + .fill(`What is the field data for KSFO? [scenario:${scenario}]`); await page .getByRole('button', { name: 'Send', exact: true }) .evaluate((element) => { diff --git a/cockpit/deep-agents/subagents/react/project.json b/cockpit/deep-agents/subagents/react/project.json index 814ccfd93..fb4a681c0 100644 --- a/cockpit/deep-agents/subagents/react/project.json +++ b/cockpit/deep-agents/subagents/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/deep-agents/subagents/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-deep-agents-subagents-angular", + "cockpit-deep-agents-subagents-python" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -21,7 +25,12 @@ "react" ] }, - { "target": "react-langgraph", "projects": ["website-runtime-preparation"] } + { + "target": "react-langgraph", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/deep-agents/subagents/react" @@ -30,10 +39,15 @@ "command": "node scripts/react-cockpit/build.mjs deep-agents-subagents" } }, - "fixture-test": { - "executor": "nx:run-commands", + "e2e": { + "executor": "@nx/playwright:playwright", "cache": false, + "inputs": [ + "default", + "reactAimockBackend" + ], "dependsOn": [ + "build", { "target": "test", "projects": [ @@ -41,17 +55,6 @@ ] } ], - "options": { - "command": "node --test scripts/react-cockpit/deep-agents-subagents-fixture.spec.mjs scripts/react-cockpit/deep-agents-subagents-configuration.spec.mjs" - } - }, - "e2e": { - "executor": "@nx/playwright:playwright", - "cache": false, - "dependsOn": [ - "build", - "fixture-test" - ], "options": { "config": "cockpit/deep-agents/subagents/react/e2e/playwright.config.ts", "skipInstall": true diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index f7559fd34..5ec7a27e1 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -63,7 +63,7 @@ test('Planning backend changes select exactly both canonical frontends', () => { assert.deepEqual(selected.map(cap => cap.angular).sort(), ['cockpit-deep-agents-planning-angular', 'cockpit-deep-agents-planning-react']); assert.ok(selected.every(cap => cap.python === 'cockpit/deep-agents/planning/python')); }); -test('Skills and Subagents own exact closed native React identities', async () => { +test('Skills owns an exact closed native React identity', async () => { const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); for (const [topic, port] of [['skills', 4631]]) { const configuration = reactCockpitConfiguration(`deep-agents-${topic}`); @@ -104,6 +104,10 @@ test('Subagents owns an exact closed native React identity', async () => { assert.deepEqual(reactCockpitConfiguration('deep-agents-subagents'), { topic: 'subagents', library: 'deep-agents', adapter: 'langgraph', appPath: 'cockpit/deep-agents/subagents/react', base: '/deep-agents/subagents/react/', port: 4630, project: 'cockpit-deep-agents-subagents-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/deep-agents/subagents/python', + fixtures: 'cockpit/deep-agents/subagents/angular/e2e/fixtures', langgraphPort: 5630, + }, }); for (const key of ['subagents', 'deep-agents/subagents', '../deep-agents-subagents', 'deep-agents-subagents/../memory']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index 64a62fd0d..c4c7a565f 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -36,17 +36,16 @@ it('wires native Skills build, aimock e2e and website coverage to the shared Pyt expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4631'"); }); -it('wires native Subagents build, fixture and website coverage to the shared Python graph', () => { +it('wires native Subagents build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); const react = read('cockpit/deep-agents/subagents/react/project.json'); expect(react.tags).toEqual(expect.arrayContaining(['framework:react', 'scope:cockpit-e2e', 'scope:cockpit-examples'])); expect(react.targets.build.options.command).toBe('node scripts/react-cockpit/build.mjs deep-agents-subagents'); - expect(react.targets.e2e.dependsOn).toEqual(['build', 'fixture-test']); - expect(react.targets['fixture-test'].dependsOn).toContainEqual({ target: 'test', projects: ['cockpit-deep-agents-subagents-python'] }); + expect(react.targets.e2e.dependsOn).toEqual(['build', { target: 'test', projects: ['cockpit-deep-agents-subagents-python'] }]); const website = read('apps/website/project.json'); expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-subagents-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); - expect(config).toContain('cockpit-deep-agents-subagents-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent'); + expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend'); expect(config).toContain("url: 'http://127.0.0.1:4630'"); }); it('wires native Filesystem build, aimock e2e and website coverage to the shared Python graph', () => { diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 7ed59e4b5..15959ab7b 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -75,7 +75,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { backend: aimockLanggraph('deep-agents/skills', 4631), }); if (topic === 'deep-agents-subagents') - return Object.freeze({topic:'subagents',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/subagents/react',base:'/deep-agents/subagents/react/',port:4630,project:'cockpit-deep-agents-subagents-react'}); + return Object.freeze({topic:'subagents',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/subagents/react',base:'/deep-agents/subagents/react/',port:4630,project:'cockpit-deep-agents-subagents-react',backend:aimockLanggraph('deep-agents/subagents', 4630)}); if (topic === 'deep-agents-filesystem') return Object.freeze({topic:'filesystem',library:'deep-agents',adapter:'langgraph',appPath:'cockpit/deep-agents/filesystem/react',base:'/deep-agents/filesystem/react/',port:4629,project:'cockpit-deep-agents-filesystem-react',backend:aimockLanggraph('deep-agents/filesystem', 4629)}); if (topic === 'deep-agents-planning') diff --git a/scripts/react-cockpit/deep-agents-subagents-configuration.spec.mjs b/scripts/react-cockpit/deep-agents-subagents-configuration.spec.mjs deleted file mode 100644 index fd8011ff8..000000000 --- a/scripts/react-cockpit/deep-agents-subagents-configuration.spec.mjs +++ /dev/null @@ -1,19 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { reactCockpitConfiguration } from './configuration.mjs'; -test('closed native Subagents selection', () => { - const c = reactCockpitConfiguration('deep-agents-subagents'); - assert.equal(c.library, 'deep-agents'); - assert.equal(c.topic, 'subagents'); - assert.equal(c.port, 4630); - assert.equal(c.base, '/deep-agents/subagents/react/'); - assert.equal(c.project, 'cockpit-deep-agents-subagents-react'); - assert.ok(Object.isFrozen(c)); - assert.throws(() => reactCockpitConfiguration('../subagents')); -}); -import { readFileSync } from 'node:fs'; -test('native fixture participates in awaited serve shutdown', () => { - const source = readFileSync(new URL('./serve.mjs', import.meta.url), 'utf8'); - assert.match(source, /if \((?:(?:\w+Fixture|isChatA2ui) \|\| )*deepAgentsSubagentsFixture(?: \|\| (?:\w+Fixture|isChatA2ui))*\)/); - assert.match(source, /await \((?:\w+Fixture \?\? )*deepAgentsSubagentsFixture(?: \?\? \w+Fixture)*\)\.close\(\)/); -}); diff --git a/scripts/react-cockpit/deep-agents-subagents-fixture.mjs b/scripts/react-cockpit/deep-agents-subagents-fixture.mjs deleted file mode 100644 index f4d6f33cb..000000000 --- a/scripts/react-cockpit/deep-agents-subagents-fixture.mjs +++ /dev/null @@ -1,589 +0,0 @@ -/** Loopback-only SDK transport backed by the actual compiled Subagents graph. */ -import { spawn } from 'node:child_process'; -import { readFileSync } from 'node:fs'; -import { createHash, randomUUID } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, timeout) { - const child = spawnWorker(); - const pending = new Map(); - let sequence = 0, - dead = false, - closing; - const ended = new Promise((resolve) => child.once('close', resolve)); - const maxResponseBytes = 16 * 1024 * 1024; - let frame = Buffer.alloc(0), - frameBytes = 0; - child.stderr.resume(); - function fail() { - dead = true; - frame = Buffer.alloc(0); - frameBytes = 0; - for (const item of pending.values()) { - clearTimeout(item.timer); - item.reject(Error('Subagents graph worker unavailable.')); - } - pending.clear(); - } - const terminate = () => { - fail(); - child.kill('SIGTERM'); - }; - child.once('error', fail); - child.once('close', fail); - child.stdin.on('error', terminate); - child.stdout.once('error', terminate); - child.stdout.once('end', () => { - if (!dead) terminate(); - }); - child.stdout.on('data', (bytes) => { - if (dead) return; - try { - let start = 0; - while (start < bytes.length && !dead) { - const newline = bytes.indexOf(0x0a, start); - const end = newline < 0 ? bytes.length : newline; - const needed = frameBytes + end - start; - // Count each raw frame before copying or decoding, including frames - // whose delimiter has not arrived. Never grow storage beyond the cap. - if (needed > maxResponseBytes) throw Error('Oversized response'); - if (needed > frame.length) { - const grown = Buffer.allocUnsafe( - Math.min(maxResponseBytes, Math.max(needed, frame.length * 2, 4096)) - ); - frame.copy(grown, 0, 0, frameBytes); - frame = grown; - } - bytes.copy(frame, frameBytes, start, end); - frameBytes = needed; - if (newline < 0) return; - const result = JSON.parse(frame.toString('utf8', 0, frameBytes)); - frameBytes = 0; - const item = pending.get(result.requestId); - if (!item) throw Error('Unowned response'); - pending.delete(result.requestId); - clearTimeout(item.timer); - if (result.error) - item.reject(Error('Subagents graph operation failed.')); - else item.resolve(result); - start = newline + 1; - } - } catch { - terminate(); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(Error('Subagents graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const wire = JSON.stringify({ ...value, requestId }) + '\n'; - if (Buffer.byteLength(wire) > 1024 * 1024) { - reject(Error('Oversized request')); - return; - } - const timer = setTimeout(terminate, timeout); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(wire); - }); - }, - close() { - return (closing ??= (async () => { - fail(); - child.stdin.end(); - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - })()); - }, - }; -} - -export function createDeepAgentsSubagentsFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/deep-agents/subagents/python', - 'python', - 'scripts/react-cockpit/deep-agents-subagents-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-subagents-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const sourceSha256 = Object.fromEntries( - ['src/graph.py', 'prompts/subagents.md', 'uv.lock'].map((path) => [ - path, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/deep-agents/subagents/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - closed = false, - closing, - queue = Promise.resolve(); - // Real execution is buffered before delivery; transport cancellation does not roll back server graph work. - const threads = new Set(), - runs = new Map(), - held = new Set(), - rawEvents = [], - proofs = [], - requests = []; - const options = { - scenario: 'single', - holdStream: false, - holdState: false, - holdCheckpoint: false, - failCreation: false, - failStream: false, - failCheckpoint: false, - }; - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value), - proof = result.proof; - if ( - closed || - proof?.actualCompiledGraph !== true || - proof.networkConnectAttempts !== 0 || - proof.op !== value.op || - proof.threadId !== (value.threadId ?? null) || - Object.keys(proof.sourceSha256 ?? {}).length !== - Object.keys(sourceSha256).length || - Object.entries(sourceSha256).some( - ([path, hash]) => proof.sourceSha256[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - proofs.push(proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function hold(response, finish, run) { - const item = { response, finish, run }; - held.add(item); - response.once('close', () => { - if (held.delete(item) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - const chunks = []; - let byteLength = 0; - for await (const chunk of request) { - const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); - byteLength += bytes.length; - if (byteLength > 1024 * 1024) { - json(response, {}, 413); - return true; - } - chunks.push(bytes); - } - const input = Buffer.concat(chunks, byteLength).toString('utf8'); - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__raw-events') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, rawEvents); - return true; - } - if (pathname === '/__graph-proof') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, proofs); - return true; - } - if (pathname === '/__requests') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - if (request.method !== 'GET' || input.length !== 0) { - json(response, {}, 400); - return true; - } - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - if (request.method !== 'POST') { - json(response, {}, 405); - return true; - } - if ( - !only(body, [ - 'scenario', - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ]) || - ('scenario' in body && - ![ - 'single', - 'parallel', - 'duplicates', - 'no-task', - 'tool-error', - 'general-purpose', - 'default-tool', - 'root-tool-error', - ].includes(body.scenario)) || - [ - 'holdStream', - 'holdState', - 'holdCheckpoint', - 'failCreation', - 'failStream', - 'failCheckpoint', - ].some((k) => k in body && typeof body[k] !== 'boolean') - ) { - json(response, {}, 400); - return true; - } - if ('scenario' in body) - await operate({ op: 'configure', scenario: body.scenario }); - Object.assign(options, body); - json(response, {}); - return true; - } - if (pathname === '/__release') { - if (request.method !== 'POST' || !only(body, [])) { - json(response, {}, 400); - return true; - } - for (const item of held) { - held.delete(item); - item.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - if (requests.length >= 2000 || threads.size >= 100 || runs.size >= 1000) { - json(response, {}, 429); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if (options.failCreation) { - options.failCreation = false; - json(response, { error: 'Controlled creation failure.' }, 500); - return true; - } - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - !only(body, ['thread_id', 'if_exists', 'metadata']) || - body.if_exists !== 'raise' || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - await operate({ op: 'create', threadId: body.thread_id }); - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if (suffix === '/state' && input.length !== 0) { - json(response, {}, 400); - return true; - } - if (suffix === '/state/checkpoint') { - if (options.failCheckpoint) { - options.failCheckpoint = false; - json(response, { error: 'Controlled exact read failure.' }, 500); - return true; - } - if ( - !only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - body.checkpoint.thread_id !== threadId || - typeof body.checkpoint.checkpoint_id !== 'string' || - typeof body.checkpoint.checkpoint_ns !== 'string' - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - if ( - !history.some( - (s) => - s.checkpoint.checkpoint_id === body.checkpoint.checkpoint_id && - s.checkpoint.checkpoint_ns === body.checkpoint.checkpoint_ns - ) || - (body.checkpoint.checkpoint_map !== undefined && - (!only(body.checkpoint.checkpoint_map, [ - body.checkpoint.checkpoint_ns, - ]) || - Object.values(body.checkpoint.checkpoint_map).some( - (id) => id !== body.checkpoint.checkpoint_id - ))) - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - ...(suffix === '/state/checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }); - const finish = () => json(response, state); - if ( - options.holdState || - (options.holdCheckpoint && suffix === '/state/checkpoint') - ) - hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - if ( - !only(body, ['limit']) || - (body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 100)) - ) { - json(response, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - json(response, history.slice(0, body.limit ?? 10)); - return true; - } - const runRoute = /^\/runs\/([^/]+)(\/cancel)?$/.exec(suffix); - if ( - runRoute && - ((!runRoute[2] && request.method === 'GET') || - (runRoute[2] && request.method === 'POST')) - ) { - if (input.length !== 0) { - json(response, {}, 400); - return true; - } - const run = runs.get(runRoute[1]); - if (run?.thread_id !== threadId) { - json(response, {}, 404); - return true; - } - if (runRoute[2]) { - const search = new URL(request.url, 'http://localhost').searchParams; - if (search.get('action') !== 'interrupt') { - json(response, {}, 400); - return true; - } - for (const item of held) - if (item.run === run) { - held.delete(item); - run.status = 'interrupted'; - item.response.end(); - } - json(response, {}); - } else json(response, run); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - if (options.failStream) { - options.failStream = false; - json(response, { error: 'Controlled stream failure.' }, 500); - return true; - } - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'command', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !( - body.command === undefined && - only(body.input, ['messages']) && - body.input.messages?.length === 1 && - only(message, ['type', 'id', 'content']) && - message.type === 'human' && - typeof message.id === 'string' && - message.id.trim() && - typeof message.content === 'string' - ) || - body.assistant_id !== 'subagents' || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (mode) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'checkpoints', - 'custom', - ].includes(mode) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - rawEvents.push({ threadId, runId: run.run_id, events: wire.events }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const modes = new Set( - body.stream_mode.map((mode) => - mode === 'messages-tuple' ? 'messages' : mode - ) - ); - const finish = () => { - if (response.destroyed || closed) return; - for (const [namespace, kind, data] of wire.events) - if ( - modes.has(kind) && - (!namespace.length || body.stream_subgraphs === true) - ) - response.write(event([kind, ...namespace].join('|'), data)); - run.status = 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Subagents graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const item of held) item.response.end(); - held.clear(); - await worker?.close(); - })()); - return handle; -} diff --git a/scripts/react-cockpit/deep-agents-subagents-wire.py b/scripts/react-cockpit/deep-agents-subagents-wire.py deleted file mode 100644 index 3164f2281..000000000 --- a/scripts/react-cockpit/deep-agents-subagents-wire.py +++ /dev/null @@ -1,152 +0,0 @@ -"""Bounded newline-JSON worker running the unchanged compiled Subagents builder.""" -import asyncio -from dataclasses import asdict, is_dataclass -from contextlib import redirect_stdout -from copy import deepcopy -import hashlib -import json -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/deep-agents/subagents/python" -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel, build, SCENARIOS - -SOURCES = ("src/graph.py", "prompts/subagents.md", "uv.lock") -MAX_REQUEST = 1024 * 1024 -MAX_RESPONSE = 16 * 1024 * 1024 - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if is_dataclass(value): - return asdict(value) - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state): - return { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() - if k in ("thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map")}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": t.id, "name": t.name, - **({"error": t.error} if t.error is not None else {}), - **({"state": t.state} if t.state is not None else {}), - "interrupts": [{"id": item.id, "value": item.value} for item in t.interrupts]} - for t in state.tasks], - } - - -async def main(): - model = LocalModel() - scenario = "single" - owners = set() - seen = set() - request_ids = set() - provenance = {} - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as network: - with redirect_stdout(sys.stderr): - graph = build(model) - while line := sys.stdin.buffer.readline(MAX_REQUEST + 1): - request = {} - try: - if len(line) > MAX_REQUEST or not line.endswith(b"\n"): - raise ValueError("Oversized request") - request = json.loads(line) - if not isinstance(request, dict) or set(request) - {"requestId", "op", "threadId", "scenario", "checkpoint", "messages"}: - raise ValueError("Malformed request") - if type(request.get("requestId")) is not int or request["requestId"] <= 0 or request["requestId"] in request_ids or len(request_ids) >= 2000: - raise ValueError("Invalid/replayed request id or lifetime limit") - request_ids.add(request["requestId"]) - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - op = request["op"] - fields = {"create": {"threadId"}, "configure": {"scenario"}, "state": {"threadId", "checkpoint"}, - "history": {"threadId"}, "submit": {"threadId", "messages"}, "resume": {"threadId"}} - if op not in fields or set(request) - ({"requestId", "op"} | fields[op]): - raise ValueError("Unsupported operation fields") - if op == "create": - if not isinstance(request.get("threadId"), str) or not request["threadId"] or request["threadId"] in owners or len(owners) >= 100: - raise ValueError("Invalid or duplicate owner") - owners.add(request["threadId"]) - elif op != "configure" and request.get("threadId") not in owners: - raise ValueError("Unknown owner") - if "checkpoint" in request: - requested_checkpoint = request["checkpoint"] - if (not isinstance(requested_checkpoint, dict) - or requested_checkpoint.get("thread_id") != config["configurable"]["thread_id"] - or set(requested_checkpoint) - {"thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map"} - or not isinstance(requested_checkpoint.get("checkpoint_id"), str) - or not requested_checkpoint["checkpoint_id"] - or not isinstance(requested_checkpoint.get("checkpoint_ns"), str)): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - if "checkpoint_map" in requested_checkpoint and ( - not isinstance(requested_checkpoint["checkpoint_map"], dict) - or set(requested_checkpoint["checkpoint_map"]) != {requested_checkpoint["checkpoint_ns"]} - or requested_checkpoint["checkpoint_map"][requested_checkpoint["checkpoint_ns"]] != requested_checkpoint["checkpoint_id"]): - raise ValueError("Checkpoint owner mismatch or unsupported fields") - known = [s async for s in graph.aget_state_history(config)] - if not any(s.config["configurable"].get("checkpoint_id") == requested_checkpoint.get("checkpoint_id") and s.config["configurable"].get("checkpoint_ns", "") == requested_checkpoint.get("checkpoint_ns") for s in known): - raise ValueError("Unknown checkpoint") - config["configurable"].update({k: v for k, v in requested_checkpoint.items() if k != "thread_id"}) - op = request["op"] - if op == "configure": - scenario = request["scenario"] - if scenario not in SCENARIOS: - raise ValueError("Unsupported scenario") - result = {} - elif op == "create": - result = {} - elif op == "state": - result = {"state": checkpoint(await graph.aget_state(config))} - elif op == "history": - result = {"history": [checkpoint(s) async for s in graph.aget_state_history(config)]} - elif op == "submit": - current = await graph.aget_state(config) - if any(t.interrupts for t in current.tasks): - raise ValueError("Cannot submit over paused batch") - messages = request.get("messages") - if not isinstance(messages, list) or len(messages) != 1: - raise ValueError("Expected one human message") - message = messages[0] - if not isinstance(message, dict) or set(message) != {"type", "id", "content"} or message["type"] != "human" or not isinstance(message["id"], str) or not message["id"].strip() or not isinstance(message["content"], str): - raise ValueError("Invalid human message") - identity = message["id"] - if (request["threadId"], identity) in seen: - raise ValueError("Replayed human message") - seen.add((request["threadId"], identity)) - model.scenario = scenario - provenance[request["threadId"]] = scenario - events = [] - async for namespace, kind, event in graph.astream( - {"messages": request["messages"]}, config, - subgraphs=True, stream_mode=["messages", "values", "updates", "checkpoints"]): - events.append([list(namespace), kind, deepcopy(event)]) - result = {"events": events, "state": checkpoint(await graph.aget_state(config))} - else: - raise ValueError("Unsupported operation") - network.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, "op": op, - "threadId": request.get("threadId"), "scenario": provenance.get(request.get("threadId"), scenario), - "sourceSha256": {p: hashlib.sha256((PROJECT / p).read_bytes()).hexdigest() for p in SOURCES}, - "networkConnectAttempts": network.call_count, - } - result["requestId"] = request["requestId"] - wire = json.dumps(result, default=encode) - if len(wire.encode()) > MAX_RESPONSE: - raise ValueError("Oversized response") - print(wire, flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": (request.get("requestId") if isinstance(request, dict) else None), "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 8925a37e8..75474d6bd 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -20,7 +20,6 @@ import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; -import { createDeepAgentsSubagentsFixture } from './deep-agents-subagents-fixture.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); const agUiA2uiFixture = configuration.library === 'ag-ui' && configuration.adapter === 'ag-ui' && configuration.topic === 'a2ui' @@ -51,7 +50,6 @@ const chatDebugFixture = configuration.library === 'chat' && configuration.topic const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; -const deepAgentsSubagentsFixture = configuration.library === 'deep-agents' && configuration.topic === 'subagents' ? createDeepAgentsSubagentsFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` (started by // the Playwright global setup); this server proxies /api to it. @@ -172,7 +170,6 @@ const server = createServer(async (request, response) => { if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; if (agUiClientToolsFixture && (await agUiClientToolsFixture(request, response, pathname))) return; - if (deepAgentsSubagentsFixture && (await deepAgentsSubagentsFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { if (request.method !== 'POST') return json(response, {}, 405); try { @@ -275,7 +272,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || deepAgentsSubagentsFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -288,7 +285,7 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? deepAgentsSubagentsFixture ?? chatGenerativeUiFixture).close(); + else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 87ee5bd098743c326ed9a02e8bced848670da00b Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:11:20 -0800 Subject: [PATCH 20/41] docs(cockpit): record batch 5 (deep-agents) aimock migration; select Angular memory e2e on proxy changes Co-Authored-By: Claude Opus 5.5 --- cockpit/deep-agents/memory/angular/project.json | 1 + .../specs/2026-11-18-react-cockpit-aimock-design.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cockpit/deep-agents/memory/angular/project.json b/cockpit/deep-agents/memory/angular/project.json index 14567f6f6..1339e1a6e 100644 --- a/cockpit/deep-agents/memory/angular/project.json +++ b/cockpit/deep-agents/memory/angular/project.json @@ -89,6 +89,7 @@ }, "e2e": { "executor": "@nx/playwright:playwright", + "inputs": ["default", "reactAimockBackend"], "options": { "config": "cockpit/deep-agents/memory/angular/e2e/playwright.config.ts" } diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 6a370575f..79a1e9e4f 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -83,6 +83,9 @@ Playwright webServer ──► serve.mjs ── /api, /developer-api ──► l | deployment-runtime's nine finalization faults (4b) | authored final values | `/__stream/edit` on the final state (`values` and `checkpoints` carrying `completed_turn_id`), plus `/__history-values` for "No final values", because the session then reads history | | time-travel `/__advance-tip`, `/__bad-history`, `/__unsafe-source`, `/__states`, result faults (4c) | authored checkpoint store | a real competing write (`/__thread//state`), `/__history-copies`, `/__state/unsafe`, history reads, and `/__stream/edit { state: true }` (the fork's confirmation read gets the same edits) | | durable `Uncertain` / `Known uncertain`, `/__publish-*`, `/__finish-run` (4c) | authored half-finished runs | `/__hold/stream { end }` ends the browser's stream after the first draft. `anonymous` withholds the run identity, or `/__run-status/running` keeps a known run in flight. `/__history/pending` stays inconclusive until a one-shot `/__history-values` publishes the outcome (wrong marker or unrelated turn as the fault) | + | deep-agents `/__configure` scenarios (batch 5) | worker-selected scripted model turns | tagged prompts (`… [scenario:]`) select shared aimock fixtures. Where the recorded Angular prompt is a substring of a tagged one (skills, subagents), the tagged fixtures go first in the file, because the first match wins. Tool call ids are fixed where a follow-up keys on `toolCallId` and left to aimock (random, unique) where repeated specialists or a second turn would otherwise share an id | + | deep-agents `/__raw-events`, `/__graph-proof` (batch 5) | the in-process graph's events, model prompts and store operations | `/__captures` (what the real server streamed and saved). Model prompts: the recall fixtures match on `systemMessage`, so the reply names the stored note only when the graph put it into the prompt. Store writes: langgraph's own `POST /store/items/search` under the browser's scope | + | memory `fail-initial` / `fail-model` / `fail-terminal` (batch 5) | the worker patched `StoreBackend.download_files` to fail the Nth read | **injected**: `/__stream/cut { beforePattern, error }` ends the browser's stream with the graph's own `MemoryObservationError` just before that phase's `deep_agents.memory` event; the real run upstream still finishes (so a terminal-phase failure keeps its write, as before). aimock can't fail a store read inside the graph; `cockpit/deep-agents/memory/python/tests` covers the real failure. `oversize` stays genuine: the write exceeds the observation limit and the next read really fails | | `POST /__inject/reasoning` (batch 1) | authored `reasoning_content` | **injected data, opt-in per test**: nothing is injected unless a spec calls it, and only the theming test that styles the Reasoning region does. langchain_openai's `ChatOpenAI` does not extract `reasoning_content` from Chat Completions responses (even though aimock can send it), so the real graph never surfaces reasoning. Until reset, the control adds `additional_kwargs.reasoning_content` to every AI message the browser receives (the first stream chunk, `values`/`updates`/`checkpoints` events, history and `/state`). Possible follow-up: switch the theming graph to the Responses API with reasoning summaries, so reasoning flows for real in both frameworks. That is a product change, so it is out of scope here | Holds are now explicit controls, not magic prompt text, so every test @@ -175,7 +178,7 @@ example needs that the pilot proxy doesn't yet provide. | 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | | 4b (done) | langgraph/{client-tools, deployment-runtime} | `/__hold/state-write`, `/__fail/state-write`, `/__thread/`, `/__stream/edit` list ops (`append`, `truncate`, `duplicateLast`, `copy`) | client tool rounds key on prompt + `hasToolResult`; a second same-thread turn uses its own prompt (`Weather again`), because aimock fixtures are stateless. "No final values" also strips the markers from the history read the session falls back to | | 4c (done) | langgraph/{time-travel, durable-execution} | `/__history-copies`, `/__state/unsafe`, `/__thread//state` writes, `/__stream/edit { state, anonymous, unlessKey }`, `/__hold/stream { afterPattern, end }`, `/__run-status/`, `/__history-values { entry }` | time-travel reads checkpoint ids from real history and thread reads, not authored ids (each turn saves input, start and done checkpoints). The competing tip is a real state write. durable-execution's uncertain outcomes end the browser's stream early, keep history inconclusive, and "publish" the saved outcome through a one-shot history rewrite | -| 5 | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__configure`, `/__raw-events`, proof-heavy `*-fixture.spec.mjs` | Angular `da-*.json` fixtures exist | +| 5 (done) | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__stream/cut` (memory) | scenarios are tagged prompts on the shared `da-*.json` fixtures. The planning and filesystem integration specs (vitest) run on `createAimockBackend` plus an in-process proxy. The React memory fixture also backed the Angular memory e2e: that now uses `createGlobalSetup` with `langgraph dev` on 6313 and the recording proxy on 5313, where the Angular dev server's `/api` proxy points, which retires its owned-process lifecycle helpers and the `compiledMemoryBackend` guard in `e2e-wiring.spec.ts`. The memory spec reads stream bodies from `/__captures[].sse` (Chromium doesn't keep a long-lived event-stream body) | | 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | | n/a | render/* (6) | none | `adapter: 'none'`, no model backend | From 1899b9a3ec9d380c6d0c7b628d3091396c421390 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 18:51:48 -0800 Subject: [PATCH 21/41] test(website): boot preview backends at startup only where specs drive them; warm next dev routes Previews whose Website specs send a message (or use proxy controls) now wait on GET /__ready as their webServer readiness URL, so aimock + `langgraph dev` boot before tests rather than mid-run beside other workers' page renders. Static asset requests no longer start a backend, so render-only previews never boot one. A globalSetup visits the shared docs route in every workspace mode once before workers start (`next dev` compiles on first request). The Agents startup pool follows the deep-agents preview commands batch 5 changed. Co-Authored-By: Claude Opus 5.5 --- apps/website/agents-startup.ts | 6 +-- apps/website/e2e/dev-warmup-setup.ts | 43 +++++++++++++++++++ ...react-deep-agents-planning-preview.spec.ts | 8 +++- ...eact-deep-agents-subagents-preview.spec.ts | 8 +++- apps/website/playwright.config.ts | 43 ++++++++++++------- .../2026-11-18-react-cockpit-aimock-design.md | 7 ++- scripts/examples/e2e-wiring.spec.ts | 4 +- scripts/react-cockpit/aimock-backend.mjs | 5 ++- scripts/react-cockpit/langgraph-proxy.mjs | 10 +++-- scripts/react-cockpit/serve.mjs | 1 - scripts/website-agents-startup.mjs | 11 ++++- scripts/website-agents-startup.spec.mjs | 6 +-- 12 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 apps/website/e2e/dev-warmup-setup.ts diff --git a/apps/website/agents-startup.ts b/apps/website/agents-startup.ts index 99e9e9397..2768af6cb 100644 --- a/apps/website/agents-startup.ts +++ b/apps/website/agents-startup.ts @@ -5,11 +5,11 @@ type WebServer = Exclude< unknown[] | undefined >; -/** Each original smoke must still succeed before its own runtime starts. */ +/** The five Deep Agents preview servers start as one pool with one readiness budget. */ export function poolAgentServers(servers: WebServer[]): WebServer[] { const matches = servers.flatMap((server, index) => { const match = - /^npx nx run cockpit-deep-agents-(planning|filesystem|subagents|skills|memory)-python:smoke && node scripts\/react-cockpit\/serve\.mjs deep-agents-\1 --no-parent$/.exec( + /^node scripts\/react-cockpit\/serve\.mjs deep-agents-(planning|filesystem|subagents|skills|memory) --no-parent --backend$/.exec( server.command ); return match ? [{ index, topic: match[1] }] : []; @@ -20,7 +20,7 @@ export function poolAgentServers(servers: WebServer[]): WebServer[] { matches.some((match, i) => match.index !== matches[0].index + i) ) { throw new Error( - 'Agents startup requires the original contiguous five unique smoke/server chains' + 'Agents startup requires the original contiguous five unique preview servers' ); } const first = matches[0].index; diff --git a/apps/website/e2e/dev-warmup-setup.ts b/apps/website/e2e/dev-warmup-setup.ts new file mode 100644 index 000000000..78e1a0154 --- /dev/null +++ b/apps/website/e2e/dev-warmup-setup.ts @@ -0,0 +1,43 @@ +import { chromium, type FullConfig } from '@playwright/test'; + +/** + * Playwright globalSetup for local runs against `next dev`, which compiles + * each route (and the client chunks it loads lazily) on first request. + * With several workers, the first tests all hit uncompiled routes at once; + * the compiles then compete for CPU and a hydration, frame or framework-switch + * check misses its window. Readiness of the dev server's port says nothing + * about that, so this visits the shared docs route once, in every workspace + * mode and both frameworks, before any worker starts. + */ +const pages = [ + '/', + '/docs/angular/deep-agents/capabilities/memory', + '/docs/react/deep-agents/capabilities/memory', + '/docs/react/deep-agents/capabilities/memory?mode=code', + '/docs/react/deep-agents/capabilities/memory?mode=run', +]; + +export default async function warmUp(config: FullConfig) { + const baseURL = config.projects[0]?.use.baseURL; + if (!baseURL) return; + const browser = await chromium.launch(); + try { + const page = await browser.newPage(); + for (const path of pages) { + await page.goto(new URL(path, baseURL).toString(), { + waitUntil: 'load', + timeout: 300_000, + }); + const shell = page.locator('[data-workspace-shell]'); + if (await shell.count()) + await shell.waitFor({ state: 'attached', timeout: 300_000 }); + await page + .locator('[data-workspace-shell][data-hydrated="true"]') + .or(page.locator('body:not(:has([data-workspace-shell]))')) + .first() + .waitFor({ timeout: 300_000 }); + } + } finally { + await browser.close(); + } +} diff --git a/apps/website/e2e/react-deep-agents-planning-preview.spec.ts b/apps/website/e2e/react-deep-agents-planning-preview.spec.ts index 83c041435..50dc7f6e3 100644 --- a/apps/website/e2e/react-deep-agents-planning-preview.spec.ts +++ b/apps/website/e2e/react-deep-agents-planning-preview.spec.ts @@ -208,13 +208,19 @@ test('reload clears an unsent React draft without runtime requests', async ({ expect(observations.get(page)?.requests).toEqual(before); }); -test('the aviation suggestion sends its exact prompt', async ({ page }) => { +test('the aviation suggestion sends its exact prompt', async ({ page, request }) => { // Sending starts a model run; deployed targets (preview and production) // keep previews request-free, so the send is verified locally only. test.skip( Boolean(process.env['BASE_URL']), 'Welcome suggestion sends are verified against the local agents suite' ); + test.setTimeout(240_000); + // The preview's aimock + `langgraph dev` start on its first API request; + // wait them out here rather than inside the send's response timeout. + expect( + (await request.post('http://127.0.0.1:4628/__ready', { timeout: 180_000 })).ok() + ).toBe(true); await page.goto(reactRoute + '?mode=run', { waitUntil: 'domcontentloaded', }); diff --git a/apps/website/e2e/react-deep-agents-subagents-preview.spec.ts b/apps/website/e2e/react-deep-agents-subagents-preview.spec.ts index ca087061a..decb42465 100644 --- a/apps/website/e2e/react-deep-agents-subagents-preview.spec.ts +++ b/apps/website/e2e/react-deep-agents-subagents-preview.spec.ts @@ -222,13 +222,19 @@ test('reload clears an unsent React draft without runtime requests', async ({ expect(observations.get(page)?.requests).toEqual(before); }); -test('the aviation suggestion sends its exact prompt', async ({ page }) => { +test('the aviation suggestion sends its exact prompt', async ({ page, request }) => { // Sending starts a model run; deployed targets (preview and production) // keep previews request-free, so the send is verified locally only. test.skip( Boolean(process.env['BASE_URL']), 'Welcome suggestion sends are verified against the local agents suite' ); + test.setTimeout(240_000); + // The preview's aimock + `langgraph dev` start on its first API request; + // wait them out here rather than inside the send's response timeout. + expect( + (await request.post('http://127.0.0.1:4630/__ready', { timeout: 180_000 })).ok() + ).toBe(true); await page.goto(reactRoute + '?mode=run', { waitUntil: 'domcontentloaded', }); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 283b63c86..0ef591aac 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -74,9 +74,16 @@ export const createWebsitePlaywrightConfig = ( ? '**/custom-runtime-bfcache.spec.ts' : undefined, testIgnore, - globalSetup: runtimeBypass - ? resolve(__dirname, 'e2e', 'runtime-bypass-setup.ts') - : undefined, + globalSetup: [ + ...(runtimeBypass + ? [resolve(__dirname, 'e2e', 'runtime-bypass-setup.ts')] + : []), + // `next dev` compiles routes on first request; compile the shared docs + // route once before parallel workers race for it. + ...(shouldStartLocalServer && !productionMode && !bfcacheRuntimeTest + ? [resolve(__dirname, 'e2e', 'dev-warmup-setup.ts')] + : []), + ], fullyParallel: true, // Match the cockpit configs: 2 retries on CI to absorb transient Next.js // dev-server startup flake; 0 locally for fast feedback. @@ -251,8 +258,12 @@ export const createWebsitePlaywrightConfig = ( // outside the server-readiness budget. Direct local // Playwright runs require those build outputs too. 'node scripts/react-cockpit/serve.mjs streaming --backend', + // Previews whose specs drive the backend wait on /__ready: + // its aimock + `langgraph dev` boot during startup, not + // mid-run beside other workers' page renders. Previews only + // rendered (no API traffic) never boot a backend. cwd: '../..', - url: 'http://127.0.0.1:4600', + url: 'http://127.0.0.1:4600/__ready', reuseExistingServer, timeout: 180_000, }, @@ -260,7 +271,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs interrupts --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4601', + url: 'http://127.0.0.1:4601/__ready', reuseExistingServer, timeout: 180_000, }, @@ -268,7 +279,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs memory --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4602', + url: 'http://127.0.0.1:4602/__ready', reuseExistingServer, timeout: 180_000, }, @@ -276,7 +287,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs client-tools --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4603', + url: 'http://127.0.0.1:4603/__ready', reuseExistingServer, timeout: 180_000, }, @@ -284,7 +295,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs persistence --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4604', + url: 'http://127.0.0.1:4604/__ready', reuseExistingServer, timeout: 180_000, }, @@ -292,7 +303,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4605', + url: 'http://127.0.0.1:4605/__ready', reuseExistingServer, timeout: 180_000, }, @@ -300,7 +311,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4606', + url: 'http://127.0.0.1:4606/__ready', reuseExistingServer, timeout: 180_000, }, @@ -308,7 +319,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs time-travel --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4607', + url: 'http://127.0.0.1:4607/__ready', reuseExistingServer, timeout: 180_000, }, @@ -316,7 +327,7 @@ export const createWebsitePlaywrightConfig = ( command: 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4608', + url: 'http://127.0.0.1:4608/__ready', reuseExistingServer, timeout: 180_000, }, @@ -436,7 +447,7 @@ export const createWebsitePlaywrightConfig = ( { command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4644', + url: 'http://127.0.0.1:4644/__ready', reuseExistingServer: false, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 5000 }, timeout: 180_000, @@ -481,7 +492,7 @@ export const createWebsitePlaywrightConfig = ( { command: 'node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4628', + url: 'http://127.0.0.1:4628/__ready', reuseExistingServer, timeout: 180_000, }, @@ -495,7 +506,7 @@ export const createWebsitePlaywrightConfig = ( { command: 'node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4630', + url: 'http://127.0.0.1:4630/__ready', reuseExistingServer, timeout: 180_000, }, @@ -593,7 +604,7 @@ export const createWebsitePlaywrightConfig = ( config.retries = 0; config.outputDir = resolve(__dirname, '../../output/playwright/chat-theming-website'); if (Array.isArray(config.webServer)) { - config.webServer = config.webServer.filter(server => [localURL, 'http://127.0.0.1:4644'].includes(server.url ?? '')).map(server => ({ ...server, reuseExistingServer: false, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 5000 } })); + config.webServer = config.webServer.filter(server => [localURL, 'http://127.0.0.1:4644/__ready'].includes(server.url ?? '')).map(server => ({ ...server, reuseExistingServer: false, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 5000 } })); } } if (environment['WEBSITE_E2E_REACT_TOPIC'] === 'chat-debug') { diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 79a1e9e4f..bdb7a1077 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -113,8 +113,11 @@ There is no second, authored backend. - `serve.mjs --backend` starts that example's aimock + `langgraph dev` itself (`scripts/react-cockpit/aimock-backend.mjs`), lazily on the - first request for the app or its API. Website suites already start only the - preview servers they own, so a suite pays only for the previews it drives. + first API or control request (the proxy awaits the start). Loading a preview + page boots nothing: an early version started it on asset requests too, and + under parallel workers those Python boots starved `next dev`'s first + compiles. Website suites already start only the preview servers they own, so + a suite pays only for the previews it actually drives. The backend stays in the server's process group and stops with it. - Preview specs call `POST /__ready` before driving a preview; it waits out the lazy start. diff --git a/scripts/examples/e2e-wiring.spec.ts b/scripts/examples/e2e-wiring.spec.ts index c4c7a565f..47ed581b2 100644 --- a/scripts/examples/e2e-wiring.spec.ts +++ b/scripts/examples/e2e-wiring.spec.ts @@ -22,7 +22,7 @@ it('wires native Planning build, aimock e2e and website coverage to the shared P expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-planning-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend'); - expect(config).toContain("url: 'http://127.0.0.1:4628'"); + expect(config).toContain("url: 'http://127.0.0.1:4628/__ready'"); }); it('wires native Skills build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); @@ -46,7 +46,7 @@ it('wires native Subagents build, aimock e2e and website coverage to the shared expect(websiteBuildProjects(website)).toContain('cockpit-deep-agents-subagents-react'); const config = readFileSync(join(repoRoot, 'apps/website/playwright.config.ts'), 'utf8'); expect(config).toContain('node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend'); - expect(config).toContain("url: 'http://127.0.0.1:4630'"); + expect(config).toContain("url: 'http://127.0.0.1:4630/__ready'"); }); it('wires native Filesystem build, aimock e2e and website coverage to the shared Python graph', () => { const read = (path: string) => JSON.parse(readFileSync(join(repoRoot, path), 'utf8')); diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index 070102974..43389580a 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -5,8 +5,9 @@ * Cockpit e2e starts the same backend from its Playwright global setup * (libs/e2e-harness). Website e2e reuses the React examples as previews and * has no per-example setup, so the static server starts the backend itself, - * lazily on the first request for the app or its API: a Website suite only - * pays for the previews it actually drives. The child stays in this process's + * lazily on the first API or control request (the proxy awaits it): loading a + * preview page boots nothing, so only specs that drive a preview pay for it, + * and a suite's page loads don't compete with a Python boot for CPU. The child stays in this process's * group, so Playwright's webServer teardown stops it with the server. */ import { execFileSync, spawn } from 'node:child_process'; diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 09e1b9918..17741ffc4 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -8,8 +8,10 @@ * cannot produce on demand: * * GET /__requests every /api and /developer-api request, in order - * POST /__ready 200 once `langgraph dev` answers /ok (graph loaded; - * with `serve.mjs --backend`, after its lazy start) + * GET|POST /__ready 200 once `langgraph dev` answers /ok (graph loaded; + * with `serve.mjs --backend`, after its lazy start). + * GET lets a Playwright webServer use it as its + * readiness URL, so the boot finishes before tests. * GET /__lifetime { activeStream, activeHistory, activeTitle, * activeState, openResponses } * GET /__captures per finished run: forwarded SSE text, saved @@ -845,7 +847,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { held?.finish(); return {}; }], - '/__ready': ['POST', async () => { + '/__ready': [['GET', 'POST'], async () => { await ready(); const answer = await send('GET', '/ok', { accept: 'application/json' }, Buffer.alloc(0)); await readAll(answer); @@ -920,7 +922,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { } if (Object.hasOwn(controls, pathname)) { const [method, run] = controls[pathname]; - if (request.method !== method) { + if (![method].flat().includes(request.method)) { json(response, {}, 405); return true; } diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 75474d6bd..3998c8d77 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -99,7 +99,6 @@ const json = (response, value, status = 200) => { }; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; - if (aimockBackend && pathname !== '/') void aimockBackend.ensure().catch(() => undefined); if (agUiA2uiFixture && (await agUiA2uiFixture(request, response, pathname))) return; if (chatA2uiFixture) { if (pathname.startsWith('/__')) { diff --git a/scripts/website-agents-startup.mjs b/scripts/website-agents-startup.mjs index bbdde7e0e..5414f779a 100644 --- a/scripts/website-agents-startup.mjs +++ b/scripts/website-agents-startup.mjs @@ -8,6 +8,13 @@ const repoRoot = fileURLToPath(new URL('../', import.meta.url)).replace( '' ); +// Previews whose Website specs send a message wait for their aimock + +// `langgraph dev` during startup (GET /__ready); the others only render and +// never boot a backend. +const backendDriven = new Set(['planning', 'subagents']); +export const agentServerUrl = (topic) => + `http://127.0.0.1:${4628 + topics.indexOf(topic)}${backendDriven.has(topic) ? '/__ready' : ''}`; + /** Validate the complete original Playwright descriptors before launching any chain. */ export function parseAgentServers(value) { const invalid = () => { @@ -35,7 +42,7 @@ export function parseAgentServers(value) { ) invalid(); const match = - /^npx nx run cockpit-deep-agents-(planning|filesystem|subagents|skills|memory)-python:smoke && node scripts\/react-cockpit\/serve\.mjs deep-agents-\1 --no-parent$/.exec( + /^node scripts\/react-cockpit\/serve\.mjs deep-agents-(planning|filesystem|subagents|skills|memory) --no-parent --backend$/.exec( server.command ); if (!match) invalid(); @@ -43,7 +50,7 @@ export function parseAgentServers(value) { if ( seen.has(topic) || server.cwd !== '../..' || - server.url !== `http://127.0.0.1:${4628 + topics.indexOf(topic)}` || + server.url !== agentServerUrl(topic) || server.reuseExistingServer !== false || server.timeout !== 180000 ) diff --git a/scripts/website-agents-startup.spec.mjs b/scripts/website-agents-startup.spec.mjs index 099929ce9..d2eb3aff4 100644 --- a/scripts/website-agents-startup.spec.mjs +++ b/scripts/website-agents-startup.spec.mjs @@ -22,9 +22,9 @@ const options = { const topics = ['planning', 'filesystem', 'subagents', 'skills', 'memory']; const manifest = () => topics.map((topic, index) => ({ - command: `npx nx run cockpit-deep-agents-${topic}-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-${topic} --no-parent`, + command: `node scripts/react-cockpit/serve.mjs deep-agents-${topic} --no-parent --backend`, cwd: '../..', - url: `http://127.0.0.1:${4628 + index}`, + url: `http://127.0.0.1:${4628 + index}${['planning', 'subagents'].includes(topic) ? '/__ready' : ''}`, reuseExistingServer: false, timeout: 180000, })); @@ -53,7 +53,7 @@ test('manifest fails closed on missing, duplicate, mismatched or changed descrip { command: manifest()[0].command + '; echo unsafe' }, { command: manifest()[0].command + '\n' }, { command: manifest()[1].command }, - { command: manifest()[0].command.replace(' && ', ' ; ') }, + { command: manifest()[0].command.replace(' --backend', '') }, { cwd: '.' }, { reuseExistingServer: true }, { timeout: 179999 }, From 811251717417c17fdf44d572125e360357e8c2f2 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 19:09:15 -0800 Subject: [PATCH 22/41] test(website): config specs follow the deep-agents preview commands and /__ready readiness URLs Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 7 ++- apps/website/src/e2e-suites.spec.ts | 8 ++-- apps/website/src/playwright-config.spec.ts | 50 +++++++++++----------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 0ef591aac..6d719c93f 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -6,6 +6,9 @@ type WebsitePlaywrightEnvironment = Readonly< Record >; +const oneOrMore = (files: string[]) => + files.length === 0 ? undefined : files.length === 1 ? files[0] : files; + export const createWebsitePlaywrightConfig = ( environment: WebsitePlaywrightEnvironment = process.env ) => { @@ -74,7 +77,7 @@ export const createWebsitePlaywrightConfig = ( ? '**/custom-runtime-bfcache.spec.ts' : undefined, testIgnore, - globalSetup: [ + globalSetup: oneOrMore([ ...(runtimeBypass ? [resolve(__dirname, 'e2e', 'runtime-bypass-setup.ts')] : []), @@ -83,7 +86,7 @@ export const createWebsitePlaywrightConfig = ( ...(shouldStartLocalServer && !productionMode && !bfcacheRuntimeTest ? [resolve(__dirname, 'e2e', 'dev-warmup-setup.ts')] : []), - ], + ]), fullyParallel: true, // Match the cockpit configs: 2 retries on CI to absorb transient Next.js // dev-server startup flake; 0 locally for fast feedback. diff --git a/apps/website/src/e2e-suites.spec.ts b/apps/website/src/e2e-suites.spec.ts index 1b9f597f0..ebd4e2851 100644 --- a/apps/website/src/e2e-suites.spec.ts +++ b/apps/website/src/e2e-suites.spec.ts @@ -185,10 +185,10 @@ describe('bounded Angular startup', () => { }); describe('bounded Agents startup', () => { - it('rejects missing, duplicate or interleaved smoke/server chains', () => { + it('rejects missing, duplicate or interleaved preview servers', () => { const config = createWebsiteSuitePlaywrightConfig('agents', { CI: 'true' }); const servers = Array.isArray(config.webServer) ? config.webServer : []; - const first = servers.findIndex(s => s.command.startsWith('npx nx run cockpit-deep-agents-')); + const first = servers.findIndex(s => s.command.startsWith('node scripts/react-cockpit/serve.mjs deep-agents-')); expect(first).toBeGreaterThan(0); expect(() => poolAgentServers(servers.filter((_, i) => i !== first))).toThrow(/five/); const duplicates = [...servers]; @@ -199,7 +199,7 @@ describe('bounded Agents startup', () => { expect(() => poolAgentServers(interleaved)).toThrow(/contiguous/); }); - it('preserves all five complete smoke/server commands, startup barriers and browser settings', () => { + it('preserves all five complete preview server commands, startup barriers and browser settings', () => { const environment = { CI: 'true', WEBSITE_E2E_DEPLOYED_OWNER: 'true' }; const original = createWebsiteSuitePlaywrightConfig('agents', environment); const pooled = createWebsiteSuitePlaywrightConfig('agents', { @@ -212,7 +212,7 @@ describe('bounded Agents startup', () => { expect(children).toHaveLength(5); expect(servers.flatMap(s => s === pool ? children : [s])).toEqual(original.webServer); expect(children.map((s: { url: string }) => new URL(s.url).port).sort()).toEqual(['4628', '4629', '4630', '4631', '4632']); - expect(children.every((s: { command: string; timeout: number }) => s.command.includes(':smoke && node ') && s.timeout === 180_000)).toBe(true); + expect(children.every((s: { command: string; timeout: number }) => s.command.endsWith(' --no-parent --backend') && s.timeout === 180_000)).toBe(true); expect(pool).toMatchObject({ cwd: '../..', timeout: 900_000, wait: { stdout: /Website Agents servers ready/ }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 4ae002c15..33a9ddffd 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -83,7 +83,7 @@ it('scopes Chat Theming Website to two owned servers with zero retries and focus expect(config.retries).toBe(0); expect(config.outputDir).toContain('chat-theming-website'); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4308', 'http://127.0.0.1:4644']); + expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4308', 'http://127.0.0.1:4644/__ready']); expect(servers.every(server => server.reuseExistingServer === false)).toBe(true); expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend', gracefulShutdown: { signal: 'SIGTERM', timeout: 5000 } }); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); @@ -145,7 +145,7 @@ describe('Website Playwright configuration', () => { const config = createWebsitePlaywrightConfig({ CI: 'true' }); const servers = Array.isArray(config.webServer) ? config.webServer : []; const nativeServer = servers.find( - server => server.url === 'http://127.0.0.1:4600' + server => server.url === 'http://127.0.0.1:4600/__ready' ); expect(nativeServer?.command).toBe('node scripts/react-cockpit/serve.mjs streaming --backend'); expect(nativeServer?.timeout).toBe(180_000); @@ -185,8 +185,10 @@ describe('Website Playwright configuration', () => { const config = createWebsitePlaywrightConfig({ CI: 'true' }); const servers = Array.isArray(config.webServer) ? config.webServer : []; const expectedUrls = getCockpitFrontends().filter(frontend => frontend.frontend === 'react').map(frontend => `http://127.0.0.1:${frontend.port}`).sort(); - const reactServers = servers.filter(server => expectedUrls.includes(server.url ?? '')); - expect(reactServers.map(server => server.url).sort()).toEqual(expectedUrls); + // Previews whose specs drive a backend wait on `/__ready`. + const origin = (url = '') => (URL.canParse(url) ? new URL(url).origin : url); + const reactServers = servers.filter(server => expectedUrls.includes(origin(server.url))); + expect(reactServers.map(server => origin(server.url)).sort()).toEqual(expectedUrls); expect(reactServers.every(server => server.reuseExistingServer === false)).toBe(true); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4610')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent'); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); @@ -215,11 +217,11 @@ describe('Website Playwright configuration', () => { expect(projects).toContain('cockpit-deep-agents-subagents-react'); expect(projects).toContain('cockpit-deep-agents-skills-react'); expect(projects).toContain('cockpit-deep-agents-memory-react'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4632')?.command).toBe('npx nx run cockpit-deep-agents-memory-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4631')?.command).toBe('npx nx run cockpit-deep-agents-skills-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4630')?.command).toBe('npx nx run cockpit-deep-agents-subagents-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4629')?.command).toBe('npx nx run cockpit-deep-agents-filesystem-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4628')?.command).toBe('npx nx run cockpit-deep-agents-planning-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4632')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent --backend'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4631')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4630/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4629')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent --backend'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4628/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4627')?.command).toBe('npx nx run cockpit-chat-generative-ui-python:smoke && node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4626')?.command).toBe('npx nx run cockpit-chat-timeline-python:smoke && node scripts/react-cockpit/serve.mjs chat-timeline --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4625')?.command).toBe('npx nx run cockpit-chat-threads-python:smoke && node scripts/react-cockpit/serve.mjs chat-threads --no-parent'); @@ -455,42 +457,42 @@ describe('Website Playwright configuration', () => { }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs streaming --backend', - url: 'http://127.0.0.1:4600', + url: 'http://127.0.0.1:4600/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs interrupts --no-parent --backend', - url: 'http://127.0.0.1:4601', + url: 'http://127.0.0.1:4601/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs memory --no-parent --backend', - url: 'http://127.0.0.1:4602', + url: 'http://127.0.0.1:4602/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs client-tools --no-parent --backend', - url: 'http://127.0.0.1:4603', + url: 'http://127.0.0.1:4603/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs persistence --no-parent --backend', - url: 'http://127.0.0.1:4604', + url: 'http://127.0.0.1:4604/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs durable-execution --no-parent --backend', - url: 'http://127.0.0.1:4605', + url: 'http://127.0.0.1:4605/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs subgraphs --no-parent --backend', - url: 'http://127.0.0.1:4606', + url: 'http://127.0.0.1:4606/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs time-travel --no-parent --backend', - url: 'http://127.0.0.1:4607', + url: 'http://127.0.0.1:4607/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs deployment-runtime --no-parent --backend', - url: 'http://127.0.0.1:4608', + url: 'http://127.0.0.1:4608/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming --no-parent', @@ -517,17 +519,17 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-element-rendering --no-parent',url:'http://127.0.0.1:4618'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-computed-functions --no-parent',url:'http://127.0.0.1:4619'}), expect.objectContaining({command:'npx nx run cockpit-chat-messages-python:smoke && node scripts/react-cockpit/serve.mjs chat-messages --no-parent',url:'http://127.0.0.1:4620'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend',url:'http://127.0.0.1:4644'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend',url:'http://127.0.0.1:4644/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-debug --no-parent',url:'http://127.0.0.1:4645'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent',url:'http://127.0.0.1:4646'}), expect.objectContaining({command:'npx nx run cockpit-chat-input-python:smoke && node scripts/react-cockpit/serve.mjs chat-input --no-parent',url:'http://127.0.0.1:4621'}), expect.objectContaining({command:'npx nx run cockpit-chat-interrupts-python:smoke && node scripts/react-cockpit/serve.mjs chat-interrupts --no-parent',url:'http://127.0.0.1:4622'}), expect.objectContaining({command:'npx nx run cockpit-chat-tool-calls-python:smoke && node scripts/react-cockpit/serve.mjs chat-tool-calls --no-parent',url:'http://127.0.0.1:4623'}), - expect.objectContaining({command:'npx nx run cockpit-deep-agents-planning-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent',url:'http://127.0.0.1:4628'}), - expect.objectContaining({command:'npx nx run cockpit-deep-agents-filesystem-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent',url:'http://127.0.0.1:4629'}), - expect.objectContaining({command:'npx nx run cockpit-deep-agents-subagents-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent',url:'http://127.0.0.1:4630'}), - expect.objectContaining({command:'npx nx run cockpit-deep-agents-skills-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent',url:'http://127.0.0.1:4631'}), - expect.objectContaining({command:'npx nx run cockpit-deep-agents-memory-python:smoke && node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent',url:'http://127.0.0.1:4632'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend',url:'http://127.0.0.1:4628/__ready'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent --backend',url:'http://127.0.0.1:4629'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend',url:'http://127.0.0.1:4630/__ready'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend',url:'http://127.0.0.1:4631'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent --backend',url:'http://127.0.0.1:4632'}), expect.objectContaining({command:'npx nx run cockpit-chat-generative-ui-python:smoke && node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent',url:'http://127.0.0.1:4627'}), expect.objectContaining({command:'npx nx run cockpit-chat-timeline-python:smoke && node scripts/react-cockpit/serve.mjs chat-timeline --no-parent',url:'http://127.0.0.1:4626'}), expect.objectContaining({command:'npx nx run cockpit-chat-threads-python:smoke && node scripts/react-cockpit/serve.mjs chat-threads --no-parent',url:'http://127.0.0.1:4625'}), From 610689a328137f1e302099a8ad44e21a2d79779d Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 19:30:31 -0800 Subject: [PATCH 23/41] test(cockpit): run React chat/debug e2e, website preview and native-session probe on aimock (batch 3b) The debug fixture server and its Python wire are deleted. The cockpit e2e and the website preview replay shared c-debug.json fixtures into a real `langgraph dev` behind the recording proxy. The packed-consumer probe runs the installed @threadplane/langgraph against the same backend: two submits, two loads, exact checkpoints, code-point analysis of an astral answer, and a transport omission of the process callback. It now proves the `langgraph dev` it started is gone after cleanup, in place of the fixture's worker journal. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 +- apps/website/e2e/react-debug-preview.spec.ts | 39 +- apps/website/playwright.config.ts | 6 +- apps/website/src/playwright-config.spec.ts | 2 +- .../debug/angular/e2e/fixtures/c-debug.json | 32 + cockpit/chat/debug/react/e2e/debug.spec.ts | 60 +- .../chat/debug/react/e2e/global-setup-impl.ts | 6 + .../chat/debug/react/e2e/playwright.config.ts | 5 +- cockpit/chat/debug/react/project.json | 29 +- .../2026-11-18-react-cockpit-aimock-design.md | 2 +- .../chat-debug-native-session.mjs | 236 ++-- scripts/ci-scope.spec.mjs | 2 +- scripts/cockpit-matrix.spec.mjs | 6 +- scripts/react-cockpit/aimock-backend.mjs | 12 +- .../chat-debug-configuration.spec.mjs | 201 +--- scripts/react-cockpit/chat-debug-fixture.mjs | 654 ----------- .../react-cockpit/chat-debug-fixture.spec.mjs | 1006 ----------------- .../chat-debug-native-session-probe.mjs | 189 +--- scripts/react-cockpit/chat-debug-wire.py | 288 ----- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/langgraph-proxy.mjs | 8 +- scripts/react-cockpit/serve.mjs | 32 +- 22 files changed, 279 insertions(+), 2539 deletions(-) create mode 100644 cockpit/chat/debug/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/chat-debug-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-debug-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-debug-wire.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 25508a5c5..815937f88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1153,7 +1153,7 @@ jobs: if: matrix.cap.angular == 'cockpit-chat-debug-react' run: | npx nx run cockpit-chat-debug-python:test --skip-nx-cache - node --test --test-concurrency=1 scripts/react-cockpit/chat-debug-configuration.spec.mjs scripts/react-cockpit/chat-debug-fixture.spec.mjs + node --test --test-concurrency=1 scripts/react-cockpit/chat-debug-configuration.spec.mjs npx nx build core --skip-nx-cache node scripts/react-cockpit/chat-debug-native-session-probe.mjs - name: nx e2e ${{ matrix.cap.angular }} diff --git a/apps/website/e2e/react-debug-preview.spec.ts b/apps/website/e2e/react-debug-preview.spec.ts index ee0b79d4e..6231e648d 100644 --- a/apps/website/e2e/react-debug-preview.spec.ts +++ b/apps/website/e2e/react-debug-preview.spec.ts @@ -2,7 +2,6 @@ import { selectWorkspaceMode } from './fixtures/workspace-controls'; import { selectFramework } from './fixtures/framework-controls'; import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; import { writeFile } from 'node:fs/promises'; -import { createHash } from 'node:crypto'; import { fromPageOrReactPreview } from './fixtures/react-preview-requests'; const origin = 'http://127.0.0.1:4645'; @@ -49,17 +48,11 @@ test.describe('native Debug Website', () => { await page.goto('about:blank'); expect((await request.post(origin + '/__release')).ok()).toBe(true); expect((await request.post(origin + '/__reset')).ok()).toBe(true); - await expect.poll(async () => (await read(request, '/__lifetime')).active).toEqual({ children: 0, streams: 0, reads: 0, handlers: 0, responses: 0 }); + // No proxied connection, held read or held stream outlives the test. + await expect.poll(async () => (await read(request, '/__lifetime')).openResponses).toBe(0); const lifetime = await read(request, '/__lifetime'); - for (const child of lifetime.children) { - expect(child.reaped).toBe(true); - for (const id of [child.pid, -child.pgid]) { - let code; - try { process.kill(id, 0); } catch (error) { code = (error as NodeJS.ErrnoException).code; } - expect(code).toBe('ESRCH'); - } - } - await writeFile(info.outputPath('observed-child-absence.json'), JSON.stringify(lifetime, null, 2)); + expect(lifetime).toMatchObject({ activeStream: false, activeHistory: false, activeState: false }); + await writeFile(info.outputPath('observed-lifetime.json'), JSON.stringify(lifetime, null, 2)); expect(observations.get(page)?.errors).toEqual([]); }); @@ -146,16 +139,16 @@ test.describe('native Debug Website', () => { const captures = await read(request, '/__captures'); expect(captures).toHaveLength(1); const capture = captures[0]; - expect(capture.proof).toMatchObject({ actualCompiledGraph: true, networkAttempts: 0, providerVariant: 'natural' }); - expect(capture.localCapturedReplay).toBe(true); - expect(capture.sseSha256).toBe(createHash('sha256').update(capture.sse).digest('hex')); - expect(capture.state.values.messages.filter((message: { type: string }) => message.type === 'ai')).toHaveLength(3); - const answer = capture.state.values.messages[1].content; - expect(capture.state.values.analysis).toEqual({ characters: [...answer].length, words: answer.split(' ').length }); + // The real server's stream and saved state (scripts/react-cockpit/langgraph-proxy.mjs). + const runId = /"run_id": ?"([^"]+)"/.exec(capture.sse)?.[1]; + expect(runId).toBeTruthy(); + expect(capture.values.messages.filter((message: { type: string }) => message.type === 'ai')).toHaveLength(3); + const answer = capture.values.messages[1].content; + expect(capture.values.analysis).toEqual({ characters: [...answer].length, words: answer.split(' ').length }); await frame.getByLabel('Message', { exact: true }).fill('Retained follow-up draft ✈ café'); await frame.getByRole('button', { name: 'Open debug panel', exact: true }).click(); await frame.getByRole('tab', { name: 'State', exact: true }).click(); - await expect.poll(async () => JSON.parse(await frame.locator('.tp-debug__value pre').first().textContent() ?? 'null')).toEqual({ analysis: capture.state.values.analysis }); + await expect.poll(async () => JSON.parse(await frame.locator('.tp-debug__value pre').first().textContent() ?? 'null')).toEqual({ analysis: capture.values.analysis }); const historyResponse = page.waitForResponse(response => frameURL.test(response.frame().url()) && response.url().endsWith('/history')); await frame.getByRole('button', { name: 'Refresh checkpoints' }).click(); const history = await (await historyResponse).json(); @@ -172,15 +165,15 @@ test.describe('native Debug Website', () => { await expect(region.locator('.tp-debug__value pre')).toHaveText('{}'); await region.getByText('Compare selected checkpoint with current state', { exact: true }).click(); await expect.poll(async () => JSON.parse(await region.getByLabel('Selected checkpoint to current state JSON', { exact: true }).textContent() ?? 'null')).toEqual([ - { kind: 'added', path: '/analysis', after: capture.state.values.analysis }, + { kind: 'added', path: '/analysis', after: capture.values.analysis }, ]); - await expect.poll(async () => JSON.parse(await frame.locator('.tp-debug__value pre').first().textContent() ?? 'null')).toEqual({ analysis: capture.state.values.analysis }); + await expect.poll(async () => JSON.parse(await frame.locator('.tp-debug__value pre').first().textContent() ?? 'null')).toEqual({ analysis: capture.values.analysis }); const ledger = await read(request, '/__requests'); - const thread = capture.state.checkpoint.thread_id; + const thread = capture.checkpoint.thread_id; expect(ledger.map((entry: { method: string; path: string }) => [entry.method, entry.path])).toEqual([ ['POST', '/api/threads'], ['POST', `/api/threads/${thread}/runs/stream`], - ['GET', `/api/threads/${thread}/runs/${capture.runId}`], + ['GET', `/api/threads/${thread}/runs/${runId}`], ['GET', `/api/threads/${thread}/state`], ['POST', `/api/threads/${thread}/state/checkpoint`], ['POST', `/api/threads/${thread}/history`], @@ -191,7 +184,7 @@ test.describe('native Debug Website', () => { expect(ledger[1].body.input.messages[0].content).toBe('Explain lift ✈ café'); expect(ledger[6].body).toMatchObject({ limit: 10 }); expect(ledger[7].body.checkpoint).toEqual(history[early].checkpoint); - await writeFile(info.outputPath('observed-selection.json'), JSON.stringify({ history, selected, current: capture.state, ledger }, null, 2)); + await writeFile(info.outputPath('observed-selection.json'), JSON.stringify({ history, selected, current: { values: capture.values, checkpoint: capture.checkpoint }, ledger }, null, 2)); await page.screenshot({ path: info.outputPath('run-inspection.png') }); await frame.getByRole('button', { name: 'Close debug panel', exact: true }).click(); for (const name of ['Code', 'Docs', 'Run'] as const) await mode(page, name); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 6d719c93f..d124c332b 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -456,9 +456,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs chat-debug --no-parent', + command: 'node scripts/react-cockpit/serve.mjs chat-debug --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4645', + url: 'http://127.0.0.1:4645/__ready', reuseExistingServer: false, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 5000 }, timeout: 180_000, @@ -618,7 +618,7 @@ export const createWebsitePlaywrightConfig = ( config.outputDir = resolve(__dirname, '../../output/playwright/chat-debug-website'); if (Array.isArray(config.webServer)) { config.webServer = config.webServer - .filter(server => [localURL, 'http://127.0.0.1:4645'].includes(server.url ?? '')) + .filter(server => [localURL, 'http://127.0.0.1:4645/__ready'].includes(server.url ?? '')) .map(server => ({ ...server, reuseExistingServer: false, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 5000 } })); } } diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 33a9ddffd..47a339304 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -520,7 +520,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-computed-functions --no-parent',url:'http://127.0.0.1:4619'}), expect.objectContaining({command:'npx nx run cockpit-chat-messages-python:smoke && node scripts/react-cockpit/serve.mjs chat-messages --no-parent',url:'http://127.0.0.1:4620'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend',url:'http://127.0.0.1:4644/__ready'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-debug --no-parent',url:'http://127.0.0.1:4645'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-debug --no-parent --backend',url:'http://127.0.0.1:4645/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent',url:'http://127.0.0.1:4646'}), expect.objectContaining({command:'npx nx run cockpit-chat-input-python:smoke && node scripts/react-cockpit/serve.mjs chat-input --no-parent',url:'http://127.0.0.1:4621'}), expect.objectContaining({command:'npx nx run cockpit-chat-interrupts-python:smoke && node scripts/react-cockpit/serve.mjs chat-interrupts --no-parent',url:'http://127.0.0.1:4622'}), diff --git a/cockpit/chat/debug/angular/e2e/fixtures/c-debug.json b/cockpit/chat/debug/angular/e2e/fixtures/c-debug.json index 33d8cd90e..e95a2ff6f 100644 --- a/cockpit/chat/debug/angular/e2e/fixtures/c-debug.json +++ b/cockpit/chat/debug/angular/e2e/fixtures/c-debug.json @@ -377,6 +377,38 @@ "metadata": { "systemHash": "05dfe622" } + }, + { + "match": { "systemMessage": "Provide a brief one-sentence summary" }, + "response": { "content": "The traveler asked an aviation question and received a short explanation." } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Aviation debug question" } + }, + { + "match": { "userMessage": "Explain lift ✈ café" }, + "response": { "content": "Lift is the upward force a wing makes as air flows faster over its curved top than beneath it." } + }, + { + "match": { "userMessage": "Continue lift — aérodrome" }, + "response": { "content": "At a high aérodrome the thinner air means a wing needs more speed to make the same lift." } + }, + { + "match": { "userMessage": "first input 🛩️" }, + "response": { "content": "A first answer about small aircraft and their wings." } + }, + { + "match": { "userMessage": "second input" }, + "response": { "content": "A second answer that follows the first one." } + }, + { + "match": { "userMessage": "labelled generated Unicode extension" }, + "response": { "content": "Cleared for departure 🛩️ at the café gate." } + }, + { + "match": { "userMessage": "labelled omitted B callback projection" }, + "response": { "content": "An answer whose processing callback the transport omits." } } ] } \ No newline at end of file diff --git a/cockpit/chat/debug/react/e2e/debug.spec.ts b/cockpit/chat/debug/react/e2e/debug.spec.ts index 1334e78ed..8443c278a 100644 --- a/cockpit/chat/debug/react/e2e/debug.spec.ts +++ b/cockpit/chat/debug/react/e2e/debug.spec.ts @@ -101,24 +101,14 @@ test.afterEach(async ({ page, request }, info) => { await page.goto('about:blank'); await control(request, '/__release'); await control(request, '/__reset'); + // No proxied browser connection, held read or held stream outlives the test. await expect - .poll(async () => (await read(request, '/__lifetime')).active) - .toEqual({ children: 0, streams: 0, reads: 0, handlers: 0, responses: 0 }); + .poll(async () => (await read(request, '/__lifetime')).openResponses) + .toBe(0); const lifetime = await read(request, '/__lifetime'); - for (const child of lifetime.children) { - expect(child.reaped).toBe(true); - for (const id of [child.pid, -child.pgid]) { - let code; - try { - process.kill(id, 0); - } catch (error) { - code = (error as NodeJS.ErrnoException).code; - } - expect(code).toBe('ESRCH'); - } - } + expect(lifetime).toMatchObject({ activeStream: false, activeHistory: false, activeState: false }); await writeFile( - info.outputPath('observed-child-absence.json'), + info.outputPath('observed-lifetime.json'), JSON.stringify(lifetime, null, 2) ); await writeFile( @@ -142,18 +132,14 @@ for (const width of [1280, 390]) await confirmed(page); await expect(page.locator('article')).toHaveCount(4); const first = await page.locator('article').allTextContents(); + // The real server's saved state for the run (scripts/react-cockpit/langgraph-proxy.mjs). const captures = await read(request, '/__captures'); - const a = captures[0].state.values.messages[1].content; - expect(captures[0].state.values.analysis).toEqual({ + const a = captures[0].values.messages[1].content; + // The graph's `process` node counts Python code points. + expect(captures[0].values.analysis).toEqual({ characters: [...a].length, words: a.split(' ').length, }); - expect(captures[0].proof).toMatchObject({ - actualCompiledGraph: true, - networkAttempts: 0, - providerVariant: 'natural', - }); - expect(captures[0].localCapturedReplay).toBe(true); await expect( page.getByLabel('Observed analysis', { exact: true }) ).toContainText(`${[...a].length} characters`); @@ -206,7 +192,7 @@ for (const width of [1280, 390]) displayed ); expect(JSON.parse(displayed ?? '{}')).toEqual({ - analysis: captures[0].state.values.analysis, + analysis: captures[0].values.analysis, }); const history = await refresh(page); const afterRefresh = await wire(request); @@ -236,26 +222,26 @@ for (const width of [1280, 390]) .getByText('Compare selected checkpoint with current state', { exact: true }) .click(); await expect.poll(async () => JSON.parse(await selected.getByLabel('Selected checkpoint to current state JSON', { exact: true }).textContent() ?? 'null')).toEqual([ - { kind: 'added', path: '/analysis', after: captures[0].state.values.analysis }, + { kind: 'added', path: '/analysis', after: captures[0].values.analysis }, ]); const laterIndex = history.findIndex( (row) => row.values.analysis !== undefined && (row.values.messages?.length ?? 0) < - captures[0].state.values.messages.length && + captures[0].values.messages.length && row.checkpoint.checkpoint_id !== - captures[0].state.checkpoint.checkpoint_id + captures[0].checkpoint.checkpoint_id ); expect(laterIndex).toBeGreaterThanOrEqual(0); const observedLater = await inspect(page, laterIndex); expect(observedLater.checkpoint).toEqual(history[laterIndex].checkpoint); expect(observedLater.checkpoint.checkpoint_id).not.toBe( - captures[0].state.checkpoint.checkpoint_id + captures[0].checkpoint.checkpoint_id ); expect(observedLater.values.analysis).toEqual( - captures[0].state.values.analysis + captures[0].values.analysis ); - expect(observedLater.values).not.toEqual(captures[0].state.values); + expect(observedLater.values).not.toEqual(captures[0].values); await selected .getByText('Displayed checkpoint values', { exact: true }) .click(); @@ -417,8 +403,8 @@ for (const action of ['Stop', 'New conversation', 'unmount']) await control(request, '/__hold/history'); await page.getByRole('button', { name: 'Refresh checkpoints' }).click(); await expect - .poll(async () => (await read(request, '/__lifetime')).active.reads) - .toBe(1); + .poll(async () => (await read(request, '/__lifetime')).activeHistory) + .toBe(true); if (action === 'New conversation') await page .getByRole('button', { name: 'Close debug panel', exact: true }) @@ -426,7 +412,7 @@ for (const action of ['Stop', 'New conversation', 'unmount']) if (action === 'unmount') await page.goto('about:blank'); else await page.getByRole('button', { name: action, exact: true }).click(); await expect - .poll(async () => (await read(request, '/__lifetime')).active.reads) + .poll(async () => (await read(request, '/__lifetime')).openResponses) .toBe(0); await control(request, '/__release'); const ledger = await wire(request); @@ -595,14 +581,14 @@ for (const action of ['Stop', 'New conversation', 'unmount']) await ready(page); await send(page); await expect - .poll(async () => (await read(request, '/__lifetime')).active.streams) - .toBe(1); + .poll(async () => (await read(request, '/__lifetime')).activeStream) + .toBe(true); await expect(mainStatus(page)).toHaveText('Receiving response…'); await expect(page.locator('article')).toHaveCount(2); if (action === 'unmount') await page.goto('about:blank'); else await page.getByRole('button', { name: action, exact: true }).click(); await expect - .poll(async () => (await read(request, '/__lifetime')).active.streams) + .poll(async () => (await read(request, '/__lifetime')).openResponses) .toBe(0); await control(request, '/__release'); const ledger = await wire(request); @@ -615,7 +601,7 @@ for (const action of ['Stop', 'New conversation', 'unmount']) JSON.stringify( { label: - 'Captured actual graph SSE replay; compiled graph already completed before HTTP replay hold', + 'Real langgraph dev stream held by the recording proxy after the first answer chunk', action, ledger, }, diff --git a/cockpit/chat/debug/react/e2e/global-setup-impl.ts b/cockpit/chat/debug/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..5c03a3adf --- /dev/null +++ b/cockpit/chat/debug/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/debug', port: 4645 }); diff --git a/cockpit/chat/debug/react/e2e/playwright.config.ts b/cockpit/chat/debug/react/e2e/playwright.config.ts index 353ff150c..c2142fd32 100644 --- a/cockpit/chat/debug/react/e2e/playwright.config.ts +++ b/cockpit/chat/debug/react/e2e/playwright.config.ts @@ -5,11 +5,14 @@ export default defineConfig({ retries: 0, fullyParallel: false, timeout: 45000, - // The first command includes cold startup within the owned worker's 10s bound. + // A real `langgraph dev` turn runs three nodes and two model calls. expect: { timeout: 15000 }, outputDir: '../../../../../output/playwright/chat-debug-react', use: { baseURL: 'http://127.0.0.1:4645', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/debug/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-debug --no-parent', cwd: '../../../../..', diff --git a/cockpit/chat/debug/react/project.json b/cockpit/chat/debug/react/project.json index b561d3749..398cc52f4 100644 --- a/cockpit/chat/debug/react/project.json +++ b/cockpit/chat/debug/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/debug/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-chat-debug-angular", + "cockpit-chat-debug-python" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -27,7 +31,12 @@ "react" ] }, - { "target": "react-langgraph", "projects": ["website-runtime-preparation"] } + { + "target": "react-langgraph", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/chat/debug/react" @@ -44,7 +53,8 @@ "cache": false, "inputs": [ "default", - "debugFixture" + "reactAimockBackend", + "debugProbe" ], "dependsOn": [ "build", @@ -62,22 +72,11 @@ } }, "namedInputs": { - "debugFixture": [ + "debugProbe": [ "{workspaceRoot}/scripts/react-cockpit/chat-debug-configuration.spec.mjs", - "{workspaceRoot}/scripts/react-cockpit/configuration.mjs", - "{workspaceRoot}/scripts/react-cockpit/serve.mjs", - "{workspaceRoot}/scripts/react-cockpit/chat-debug-fixture.mjs", - "{workspaceRoot}/scripts/react-cockpit/chat-debug-wire.py", - "{workspaceRoot}/scripts/react-cockpit/chat-debug-fixture.spec.mjs", "{workspaceRoot}/scripts/react-cockpit/chat-debug-native-session-probe.mjs", - "{workspaceRoot}/cockpit/chat/debug/python/src/graph.py", - "{workspaceRoot}/cockpit/chat/debug/python/tests/providers.py", - "{workspaceRoot}/cockpit/chat/debug/python/pyproject.toml", - "{workspaceRoot}/cockpit/chat/debug/python/uv.lock", "{workspaceRoot}/fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs", - "{workspaceRoot}/cockpit/chat/debug/python/tests/test_streaming.py", - "{workspaceRoot}/cockpit/chat/debug/python/prompts/debug.md", - "{workspaceRoot}/cockpit/chat/debug/python/langgraph.json" + "{workspaceRoot}/cockpit/chat/debug/angular/e2e/fixtures/c-debug.json" ] } } diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index bdb7a1077..a4b364283 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -177,7 +177,7 @@ example needs that the pilot proxy doesn't yet provide. | 2 (done) | chat/tool-calls, chat/subagents, chat/interrupts | `/__hold/stream` `beforeToolResult` / `afterChildMessage`, `/__fail-stream`, `/__child/`, tool and approval `/__history` modes, `/__ready` | tool rounds key on aimock `toolCallId`; a mid-stream child model failure is aimock `truncateAfterChunks`; a title (metadata) failure is a per-prompt aimock error fixture (`… (title fails)`) | | 2b | chat/generative-ui | `/__configure` faults that swap graph internals: a throwing KPI tool, malformed trend data, prose injected into the `admit` node's output, partial layouts | aimock can't produce tool-code failures or node-output rewrites, so this needs a decision: move the graph-level faults to Python tests, or keep a small authored layer | | 3 (done) | chat/threads, chat/timeline | `/__title/`, `/__hold/title`, `/__state/`, `/__hold/state`, `/__hold/history`, `/__mapped-checkpoints`, `foreign`/`failure` history modes | a mid-stream model failure is aimock `truncateAfterChunks`; stream bodies come from `/__captures[].sse`, because DevTools can't return a streamed body the client closes | -| 3b | chat/debug | `/__hold/current`, `/__hold/checkpoint`, selected-read variants, `captures[].proof.providerVariant` | its fixture also backs the packed-consumer native-session proof (`chat-debug-native-session-probe.mjs`, its own CI step), so it migrates together with that proof | +| 3b (done) | chat/debug | `/__captures` now also carries the saved `values` and `checkpoint`; `/__stream/edit` ops take `dataPattern` | summary and title calls share the answer's last user message, so their fixtures match on their own system prompts first. The packed-consumer native-session probe (`chat-debug-native-session-probe.mjs`, its own CI step) now runs the installed `@threadplane/langgraph` against `createAimockBackend` + an in-process proxy: two submits, two loads, exact checkpoints, a scripted astral answer (code-point analysis), and a transport omission of the `process` callback via `/__stream/edit { dataPattern }`. Its per-request worker acquisition journal and failure-lifetime mode described the deleted fixture's Python workers; it now proves the `langgraph dev` it started is gone after cleanup | | 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | | 4b (done) | langgraph/{client-tools, deployment-runtime} | `/__hold/state-write`, `/__fail/state-write`, `/__thread/`, `/__stream/edit` list ops (`append`, `truncate`, `duplicateLast`, `copy`) | client tool rounds key on prompt + `hasToolResult`; a second same-thread turn uses its own prompt (`Weather again`), because aimock fixtures are stateless. "No final values" also strips the markers from the history read the session falls back to | | 4c (done) | langgraph/{time-travel, durable-execution} | `/__history-copies`, `/__state/unsafe`, `/__thread//state` writes, `/__stream/edit { state, anonymous, unlessKey }`, `/__hold/stream { afterPattern, end }`, `/__run-status/`, `/__history-values { entry }` | time-travel reads checkpoint ids from real history and thread reads, not authored ids (each turn saves input, start and done checkpoints). The competing tip is a real state write. durable-execution's uncertain outcomes end the browser's stream early, keep history inconclusive, and "publish" the saved outcome through a one-shot history rewrite | diff --git a/fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs b/fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs index 53d3646b4..f28e83dd8 100644 --- a/fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs +++ b/fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs @@ -2,12 +2,9 @@ import assert from 'node:assert/strict'; import { createRequire } from 'node:module'; import { createHash, randomUUID } from 'node:crypto'; -import { - appendFileSync, - readFileSync, - realpathSync, - writeFileSync, -} from 'node:fs'; +import { createServer } from 'node:http'; +import { once } from 'node:events'; +import { realpathSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import test from 'node:test'; @@ -18,28 +15,17 @@ const entry = require.resolve('@threadplane/langgraph'); const candidateRequire = createRequire(entry); const sdkEntry = candidateRequire.resolve('@langchain/langgraph-sdk'); const { Client } = await import(sdkEntry); -const fixtureURL = new URL(process.env.DEBUG_FIXTURE_MODULE_URL); +// The repository that owns the backend: aimock replays the debug example's +// shared fixtures into a real `langgraph dev`, behind the recording proxy. +const rootURL = new URL(process.env.DEBUG_REPO_ROOT_URL); assert.equal( process.env.DEBUG_NATIVE_RECEIPT_PATH, join(realpathSync(process.cwd()), 'debug-native-receipt.json') ); -assert.equal( - process.env.DEBUG_NATIVE_ACQUISITIONS_PATH, - join(realpathSync(process.cwd()), 'debug-native-acquisitions.jsonl') -); -assert.ok( - [undefined, 'exit-after-worker-acquired'].includes( - process.env.DEBUG_NATIVE_PROBE_FAULT - ) -); -assert.equal(fixtureURL.protocol, 'file:'); -assert.ok( - fixtureURL.pathname.endsWith('/scripts/react-cockpit/chat-debug-fixture.mjs') -); -assert.equal( - realpathSync(fileURLToPath(fixtureURL)), - fileURLToPath(fixtureURL) -); +assert.equal(rootURL.protocol, 'file:'); +assert.ok(rootURL.pathname.endsWith('/')); +const root = fileURLToPath(rootURL); +assert.equal(realpathSync(root) + '/', root); assert.ok( realpathSync(entry).startsWith( realpathSync(process.cwd()) + '/node_modules/@threadplane/langgraph/' @@ -50,18 +36,31 @@ assert.ok( realpathSync(process.cwd()) + '/node_modules/' ) ); -const { createChatDebugFixture } = await import(fixtureURL.href); -const sourceHash = createHash('sha256') - .update(readFileSync(fixtureURL)) - .digest('hex'); -const onChild = ({ pid, pgid }) => { - appendFileSync( - process.env.DEBUG_NATIVE_ACQUISITIONS_PATH, - JSON.stringify({ pid, pgid, sourceHash }) + '\n' - ); - if (process.env.DEBUG_NATIVE_PROBE_FAULT === 'exit-after-worker-acquired') - process.exit(7); -}; +const { createAimockBackend } = await import( + new URL('scripts/react-cockpit/aimock-backend.mjs', rootURL).href +); +const { createLanggraphProxy } = await import( + new URL('scripts/react-cockpit/langgraph-proxy.mjs', rootURL).href +); +const { reactCockpitConfiguration } = await import( + new URL('scripts/react-cockpit/configuration.mjs', rootURL).href +); +const { backend: backendConfig } = reactCockpitConfiguration('chat-debug'); +const sha256 = (text) => createHash('sha256').update(text).digest('hex'); +/** SSE events as the real server framed them (CRLF or LF). */ +const events = (text) => + text + .split(/\r?\n\r?\n/) + .filter(Boolean) + .map((block) => { + const lines = block.split(/\r?\n/); + return { + event: lines.find((line) => line.startsWith('event: '))?.slice(7), + data: JSON.parse( + lines.find((line) => line.startsWith('data: '))?.slice(6) ?? 'null' + ), + }; + }); const neutral = (messages) => messages.map((message) => { @@ -87,16 +86,24 @@ const assertEmptyExtras = (snapshot) => { }; test('installed native session proves two real live generations and two explicit checkpoint-backed loads', async (t) => { - let fixture, + let backend, + server, session, unsubscribe, - unicodeFixture, unicodeSession, - projectionFixture, projectionSession, cleaning; const controllers = []; const report = { entry, sdkEntry, turns: [], cleanup: null }; + const control = async (path, body) => { + const response = await fetch(base + path, { + method: body === undefined ? 'GET' : 'POST', + ...(body === undefined ? {} : { body: JSON.stringify(body) }), + }); + assert.equal(response.status, 200, path); + return response.json(); + }; + let base; const cleanup = () => (cleaning ??= (async () => { unsubscribe?.(); @@ -104,44 +111,28 @@ test('installed native session proves two real live generations and two explicit await session?.dispose(); await unicodeSession?.dispose(); await projectionSession?.dispose(); - await unicodeFixture?.close(); - await projectionFixture?.close(); - fixture?.release(); - await fixture?.close(); - report.cleanup = fixture?.receipt(); - if (report.cleanup) { - assert.deepEqual(report.cleanup.active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, + const lifetime = base ? await control('/__lifetime').catch(() => null) : null; + if (server) + await new Promise((done) => { + server.closeAllConnections(); + server.close(() => done()); }); - for (const child of report.cleanup.children) { - assert.equal(child.reaped, true); - if (child.pid) - for (const id of [child.pid, -child.pgid]) - assert.throws(() => process.kill(id, 0), { code: 'ESRCH' }); - } + const pid = backend?.pid ?? null; + await backend?.close(); + report.cleanup = { lifetime, langgraph: { pid, absent: false } }; + if (lifetime) + assert.deepEqual( + { + activeStream: lifetime.activeStream, + activeHistory: lifetime.activeHistory, + activeState: lifetime.activeState, + }, + { activeStream: false, activeHistory: false, activeState: false } + ); + if (pid) { + assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); + report.cleanup.langgraph.absent = true; } - report.unicodeCleanup = unicodeFixture?.receipt(); - if (report.unicodeCleanup) - assert.deepEqual(report.unicodeCleanup.active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); - report.projectionCleanup = projectionFixture?.receipt(); - if (report.projectionCleanup) - assert.deepEqual(report.projectionCleanup.active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); writeFileSync( process.env.DEBUG_NATIVE_RECEIPT_PATH, JSON.stringify(report) + '\n', @@ -163,8 +154,22 @@ test('installed native session proves two real live generations and two explicit process.off('SIGTERM', onTerminate); } }); - fixture = createChatDebugFixture({ onChild }); - const apiUrl = await fixture.listen(); + backend = createAimockBackend({ root, backend: backendConfig }); + await backend.ensure(); + const proxy = createLanggraphProxy({ + upstream: `http://127.0.0.1:${backendConfig.langgraphPort}`, + }); + server = createServer(async (request, response) => { + const path = new URL(request.url ?? '/', 'http://localhost').pathname; + if (!(await proxy(request, response, path))) { + response.writeHead(404); + response.end(); + } + }); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); + base = `http://127.0.0.1:${server.address().port}`; + const apiUrl = base + '/api'; const client = new Client({ apiUrl, apiKey: null, @@ -312,14 +317,17 @@ test('installed native session proves two real live generations and two explicit first ); else first = structuredClone(exact); - const capture = fixture.captures.at(-1); - const processCallbacks = capture.events.filter( - ([kind, value]) => - kind === 'messages' && value[1].langgraph_node === 'process' + // What the real server streamed and saved for this run. + const capture = (await control('/__captures')).at(-1); + const rawCallbacks = events(capture.sse).filter( + ({ event }) => event === 'messages' + ); + const processCallbacks = rawCallbacks.filter( + ({ data }) => data[1].langgraph_node === 'process' ); assert.equal(processCallbacks.length, 1); - assert.equal(processCallbacks[0][1][0].id, turn[2].id); - assert.equal(answer, capture.state.values.messages.at(-3).content); + assert.equal(processCallbacks[0].data[0].id, turn[2].id); + assert.equal(answer, capture.values.messages.at(-3).content); report.turns.push({ input, generation, @@ -328,10 +336,9 @@ test('installed native session proves two real live generations and two explicit analysis, checkpoint: cp, rawExact: exact, - rawCallbacks: capture.events.filter(([kind]) => kind === 'messages'), + rawCallbacks, processCallback: processCallbacks[0], - proof: capture.proof, - sseSha256: capture.sseSha256, + sseSha256: sha256(capture.sse), historyOrder: loaded.history.map((row) => row.checkpoint), liveDelivery: turn.map((m) => m.delivery), reloadDelivery: loaded.messages.slice(-4).map((m) => m.delivery), @@ -339,32 +346,28 @@ test('installed native session proves two real live generations and two explicit prefix = frozen; previousGeneration = generation; } - report.requests = structuredClone(fixture.requests); - assert.equal(fixture.requests.length, 12); + const requests = await control('/__requests'); + report.requests = requests; + assert.equal(requests.length, 12); assert.equal( - fixture.requests.filter( + requests.filter( (r) => r.method === 'GET' && /\/runs\/[^/]+$/.test(r.path) ).length, 2, 'Native submit confirms each exact physical run status' ); assert.equal( - fixture.requests.filter((r) => r.path.endsWith('/runs/stream')).length, + requests.filter((r) => r.path.endsWith('/runs/stream')).length, 2 ); assert.equal( - fixture.requests.filter((r) => r.path.endsWith('/history')).length, + requests.filter((r) => r.path.endsWith('/history')).length, 2 ); - // Separate local provider extension: the natural two-turn gate above stays - // unchanged. Actual graph process computes analysis from generated astral A. - unicodeFixture = createChatDebugFixture({ - providerVariant: 'unicode', - onChild, - }); - const unicodeUrl = await unicodeFixture.listen(); + // A scripted answer with astral characters (shared fixtures): the real + // graph's process node computes analysis from it in Python code points. const unicodeClient = new Client({ - apiUrl: unicodeUrl, + apiUrl, apiKey: null, callerOptions: { maxRetries: 0 }, defaultHeaders: {}, @@ -377,7 +380,7 @@ test('installed native session proves two real live generations and two explicit unicodeSession = createSession({ assistantId: 'c-debug', threadId: unicodeThread, - apiUrl: unicodeUrl, + apiUrl, clientOptions: { maxRetries: 0, defaultHeaders: {} }, }); assert.equal( @@ -410,20 +413,21 @@ test('installed native session proves two real live generations and two explicit unicodeLive.state.analysis ); report.unicodeExtension = { - label: 'AuthoredModel _begin extension, real graph analysis', + label: 'Scripted astral answer, real graph analysis', answer: unicodeAnswer, utf16Length: unicodeAnswer.length, codepoints: [...unicodeAnswer].length, raw: unicodeExact, - proof: unicodeFixture.captures[0].proof, }; - projectionFixture = createChatDebugFixture({ - omitProcessCallback: true, - onChild, + // Transport projection: the proxy drops the process node's message callback + // from the stream the session receives; the real run still emits it. + await control('/__stream/edit', { + ops: [ + { event: '^messages$', dataPattern: '"langgraph_node": ?"process"', op: 'drop' }, + ], }); - const projectionUrl = await projectionFixture.listen(); const projectionClient = new Client({ - apiUrl: projectionUrl, + apiUrl, apiKey: null, callerOptions: { maxRetries: 0 }, defaultHeaders: {}, @@ -436,7 +440,7 @@ test('installed native session proves two real live generations and two explicit projectionSession = createSession({ assistantId: 'c-debug', threadId: projectionThread, - apiUrl: projectionUrl, + apiUrl, clientOptions: { maxRetries: 0, defaultHeaders: {} }, }); assert.equal( @@ -459,15 +463,17 @@ test('installed native session proves two real live generations and two explicit neutral(projectionLive.messages), neutral(projectionRaw.values.messages) ); - const projected = projectionFixture.captures[0]; - assert.equal(projected.omission.id, processMessage.id); - assert.equal(projected.omission.count, 1); - assert.notEqual(projected.originalSseSha256, projected.sseSha256); + const projected = (await control('/__captures')).at(-1); + const naturalCallbacks = events(projected.sse).filter( + ({ event, data }) => + event === 'messages' && data[1].langgraph_node === 'process' + ); + assert.equal(naturalCallbacks.length, 1); + assert.equal(naturalCallbacks[0].data[0].id, processMessage.id); report.projectionRegression = { label: 'Transport omission only; natural backend callback was present', - originalSseSha256: projected.originalSseSha256, - projectedSseSha256: projected.sseSha256, - omission: projected.omission, + naturalSseSha256: sha256(projected.sse), + omission: { id: processMessage.id, count: naturalCallbacks.length }, semantic: neutral(projectionLive.messages), delivery: processMessage.delivery, raw: projectionRaw, diff --git a/scripts/ci-scope.spec.mjs b/scripts/ci-scope.spec.mjs index c795f1f57..6f3971741 100644 --- a/scripts/ci-scope.spec.mjs +++ b/scripts/ci-scope.spec.mjs @@ -261,7 +261,7 @@ describe('Debug actual CI owners', () => { assert.ok(debug, 'conditional Debug proof'); const commands = [ 'npx nx run cockpit-chat-debug-python:test --skip-nx-cache', - 'node --test --test-concurrency=1 scripts/react-cockpit/chat-debug-configuration.spec.mjs scripts/react-cockpit/chat-debug-fixture.spec.mjs', + 'node --test --test-concurrency=1 scripts/react-cockpit/chat-debug-configuration.spec.mjs', 'npx nx build core --skip-nx-cache', 'node scripts/react-cockpit/chat-debug-native-session-probe.mjs', ]; diff --git a/scripts/cockpit-matrix.spec.mjs b/scripts/cockpit-matrix.spec.mjs index e5710eb76..d9790d049 100644 --- a/scripts/cockpit-matrix.spec.mjs +++ b/scripts/cockpit-matrix.spec.mjs @@ -231,10 +231,10 @@ describe('Debug proof files through actual Nx attribution and classification', ( const root = fileURLToPath(new URL('../', import.meta.url)); const project = JSON.parse(readFileSync(new URL('../cockpit/chat/debug/react/project.json', import.meta.url), 'utf8')); for (const file of [ - 'scripts/react-cockpit/chat-debug-fixture.mjs', - 'scripts/react-cockpit/chat-debug-wire.py', - 'scripts/react-cockpit/chat-debug-fixture.spec.mjs', 'scripts/react-cockpit/chat-debug-configuration.spec.mjs', + 'scripts/react-cockpit/aimock-backend.mjs', + 'scripts/react-cockpit/langgraph-proxy.mjs', + 'cockpit/chat/debug/angular/e2e/fixtures/c-debug.json', 'scripts/react-cockpit/configuration.mjs', 'scripts/react-cockpit/serve.mjs', 'scripts/react-cockpit/chat-debug-native-session-probe.mjs', diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index 43389580a..5b59b9347 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -69,16 +69,24 @@ export function createAimockBackend({ root, backend }) { starting ??= start(); return starting; }, + /** The `uv run langgraph dev` process, once started. */ + get pid() { + return child?.pid ?? null; + }, async close() { // `uv run` does not forward signals to the server it started: stop the - // server processes under it first, then uv itself. - if (child?.pid && child.exitCode === null) { + // server processes under it first, then uv itself, and wait for it. + if (child?.pid && child.exitCode === null && child.signalCode === null) { + const exited = new Promise((done) => child.once('exit', done)); try { execFileSync('pkill', ['-TERM', '-P', String(child.pid)]); } catch { // no children left } child.kill('SIGTERM'); + const timeout = setTimeout(() => child.kill('SIGKILL'), 10_000); + await exited; + clearTimeout(timeout); } await mock?.stop(); }, diff --git a/scripts/react-cockpit/chat-debug-configuration.spec.mjs b/scripts/react-cockpit/chat-debug-configuration.spec.mjs index 0e2fdda39..8be9f2aeb 100644 --- a/scripts/react-cockpit/chat-debug-configuration.spec.mjs +++ b/scripts/react-cockpit/chat-debug-configuration.spec.mjs @@ -3,195 +3,6 @@ import test from 'node:test'; import { readFileSync, readdirSync, statSync } from 'node:fs'; import { resolve } from 'node:path'; import { reactCockpitConfiguration } from './configuration.mjs'; -import { spawn } from 'node:child_process'; -import { randomUUID } from 'node:crypto'; -import { setTimeout as delay } from 'node:timers/promises'; -import { once } from 'node:events'; -import { createServer } from 'node:http'; - -const origin = 'http://127.0.0.1:4645'; -async function eventually(read, accept) { - const until = Date.now() + 8000; - while (Date.now() < until) { - const value = await read(); - if (accept(value)) return value; - await delay(20); - } - assert.fail('Owned server milestone deadline'); -} -const absent = (pid) => - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -const empty = { children: 0, streams: 0, reads: 0, handlers: 0, responses: 0 }; - -for (const phase of ['current', 'stream']) { - for (const signal of ['SIGINT', 'SIGTERM']) { - test( - `real serve drains held ${phase}, reset and ${signal} ownership`, - { timeout: 20000 }, - async () => { - // Refuse to take over any existing listener. - await assert.rejects( - fetch(origin, { signal: AbortSignal.timeout(200) }) - ); - const child = spawn( - process.execPath, - ['scripts/react-cockpit/serve.mjs', 'chat-debug', '--no-parent'], - { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'pipe'] } - ); - let output = '', - errors = ''; - child.stdout.on('data', (chunk) => { - output += chunk; - }); - child.stderr.on('data', (chunk) => { - errors += chunk; - }); - const exited = once(child, 'exit'); - const get = async (path) => (await fetch(origin + path)).json(); - const post = async (path, body) => - fetch(origin + path, { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify(body ?? {}), - }); - try { - await eventually( - async () => { - if (child.exitCode !== null) - assert.fail(`Server exited: ${errors}`); - try { - return await get('/__lifetime'); - } catch { - return null; - } - }, - (value) => value?.active !== undefined - ); - assert.equal((await post('/__hold/unsupported')).status, 404); - assert.equal((await fetch(origin + '/__reset')).status, 405); - assert.deepEqual(await get('/__requests'), []); - for (const action of ['reset', 'shutdown']) { - const id = randomUUID(); - assert.equal( - ( - await post('/api/threads', { - thread_id: id, - if_exists: 'raise', - }) - ).status, - 200 - ); - await post('/__hold/' + phase); - const pending = - phase === 'stream' - ? post(`/api/threads/${id}/runs/stream`, { - assistant_id: 'c-debug', - input: { - messages: [ - { - type: 'human', - id: randomUUID(), - content: 'Explain lift', - }, - ], - }, - }) - .then(async (res) => res.text()) - .catch(() => '') - : fetch(origin + `/api/threads/${id}/state`).catch(() => null); - const lifetime = await eventually( - () => get('/__lifetime'), - (value) => - value.active[phase === 'stream' ? 'streams' : 'reads'] === 1 - ); - assert.equal(lifetime.active.children, 1); - if (action === 'reset') { - assert.equal((await post('/__reset')).status, 200); - await pending; - const reset = await eventually( - () => get('/__lifetime'), - (value) => Object.values(value.active).every((n) => n === 0) - ); - assert.deepEqual(reset.active, empty); - for (const worker of reset.children) { - assert.equal(worker.reaped, true); - absent(worker.pid); - absent(-worker.pgid); - } - } else { - child.kill(signal); - const [code] = await exited; - await pending; - assert.equal(code, 0, errors); - } - } - const closed = JSON.parse(output.trim().split('\n').at(-1)); - assert.equal(closed.listenersClosed, true); - assert.equal(closed.parentServerOwned, false); - assert.equal(closed.chatDebugFixtureClosed.closed, true); - assert.deepEqual(closed.chatDebugFixtureClosed.active, empty); - for (const worker of closed.chatDebugFixtureClosed.children) { - assert.equal(worker.reaped, true); - absent(worker.pid); - absent(-worker.pgid); - } - absent(child.pid); - await assert.rejects(fetch(origin)); - assert.equal(errors, ''); - } finally { - if (child.exitCode === null) { - child.kill('SIGTERM'); - await exited; - } - } - } - ); - } -} - -test( - 'owned bind error exits nonzero after awaited fixture close', - { timeout: 10000 }, - async () => { - const occupied = createServer((_request, response) => - response.end('Owned occupancy sentinel') - ); - await new Promise((resolve, reject) => { - occupied.once('error', reject); - occupied.listen(4645, '127.0.0.1', resolve); - }); - try { - const child = spawn( - process.execPath, - ['scripts/react-cockpit/serve.mjs', 'chat-debug', '--no-parent'], - { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'pipe'] } - ); - let output = '', - errors = ''; - child.stdout.on('data', (chunk) => { - output += chunk; - }); - child.stderr.on('data', (chunk) => { - errors += chunk; - }); - const [code] = await once(child, 'close'); - assert.equal(code, 1, errors); - const closed = JSON.parse(output.trim()); - assert.equal(closed.listenersClosed, true); - assert.equal(closed.parentServerOwned, false); - assert.equal(closed.chatDebugFixtureClosed.closed, true); - assert.deepEqual(closed.chatDebugFixtureClosed.active, empty); - absent(child.pid); - assert.equal( - await (await fetch(origin)).text(), - 'Owned occupancy sentinel' - ); - } finally { - occupied.closeAllConnections(); - await new Promise((resolve) => occupied.close(resolve)); - } - } -); test('debug has one exact closed native LangGraph allocation', () => { assert.deepEqual(reactCockpitConfiguration('chat-debug'), { @@ -202,6 +13,12 @@ test('debug has one exact closed native LangGraph allocation', () => { base: '/chat/debug/react/', port: 4645, project: 'cockpit-chat-debug-react', + backend: { + kind: 'aimock-langgraph', + python: 'cockpit/chat/debug/python', + fixtures: 'cockpit/chat/debug/angular/e2e/fixtures', + langgraphPort: 5645, + }, }); assert.ok(Object.isFrozen(reactCockpitConfiguration('chat-debug'))); for (const key of [ @@ -225,7 +42,7 @@ test('debug has one exact closed native LangGraph allocation', () => { ); }); -test('debug production inputs exclude local wire, fixture and controls', () => { +test('debug production inputs exclude local test backends and controls', () => { const visit = (dir) => readdirSync(dir).flatMap((name) => { const path = resolve(dir, name); @@ -238,10 +55,10 @@ test('debug production inputs exclude local wire, fixture and controls', () => { for (const path of visit(resolve('cockpit/chat/debug/react/src'))) assert.doesNotMatch( readFileSync(path, 'utf8'), - /chat-debug-(?:wire|fixture)|\/__(?:release|hold|reset|requests|captures|lifetime)/ + /aimock-backend|langgraph-proxy|\/__(?:release|hold|reset|requests|captures|lifetime)/ ); assert.doesNotMatch( readFileSync('scripts/react-cockpit/build.mjs', 'utf8'), - /chat-debug-fixture|chat-debug-wire/ + /aimock-backend|langgraph-proxy/ ); }); diff --git a/scripts/react-cockpit/chat-debug-fixture.mjs b/scripts/react-cockpit/chat-debug-fixture.mjs deleted file mode 100644 index 3810a8bfd..000000000 --- a/scripts/react-cockpit/chat-debug-fixture.mjs +++ /dev/null @@ -1,654 +0,0 @@ -/** Local captured replay of the actual graph/InMemorySaver. No provider timing - * or platform durability claim. This module is excluded from application src. */ -import { spawn } from 'node:child_process'; -import { createHash, randomUUID } from 'node:crypto'; -import { createServer } from 'node:http'; -import { fileURLToPath } from 'node:url'; - -const ROOT = fileURLToPath(new URL('../../', import.meta.url)); -const PYTHON = fileURLToPath( - new URL('../../cockpit/chat/debug/python/.venv/bin/python', import.meta.url) -); -const FRAME = 65536, - TOTAL = 2 * 1024 * 1024; -const GRACEFUL_EXIT_MS = 2000; -const encode = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; -const hash = (value) => createHash('sha256').update(value).digest('hex'); -const abortError = () => - Object.assign(Error('Local command aborted'), { name: 'AbortError' }); - -/** Exactly one directly spawned Python PID/process group, synchronously owned. */ -export function createDebugWorker({ - timeoutMs = 10000, - outputBytes = TOTAL, - fault, - providerVariant = 'natural', - metadataFailure, -} = {}) { - if ( - !Number.isInteger(timeoutMs) || - timeoutMs < 1 || - timeoutMs > 10000 || - !Number.isInteger(outputBytes) || - outputBytes < 1 || - outputBytes > TOTAL - ) - throw Error('Invalid fixed worker bounds'); - if ( - ![ - undefined, - 'malformed', - 'partial-oversize', - 'stderr-overflow', - 'ignore-term', - 'late-result', - 'delayed-close', - 'hung-close', - 'spawn-failure', - ].includes(fault) || - !['natural', 'unicode'].includes(providerVariant) || - ![undefined, 'read', 'update'].includes(metadataFailure) || - (fault && metadataFailure !== undefined) - ) - throw Error('Invalid closed worker control'); - const child = spawn( - fault === 'spawn-failure' ? PYTHON + '.deliberately-missing' : PYTHON, - [ - '-E', - '-B', - 'scripts/react-cockpit/chat-debug-wire.py', - ...(fault && fault !== 'spawn-failure' - ? ['--fault=' + fault] - : providerVariant === 'unicode' - ? ['--variant=unicode'] - : []), - ...(metadataFailure ? ['--metadata-failure=' + metadataFailure] : []), - ], - { - cwd: ROOT, - detached: true, - stdio: ['pipe', 'pipe', 'pipe'], - env: { - PATH: '/usr/bin:/bin', - LANG: 'en_US.UTF-8', - LANGSMITH_TRACING: 'false', - LANGCHAIN_TRACING_V2: 'false', - PYTHONDONTWRITEBYTECODE: '1', - }, - } - ); - const receipt = { - pid: child.pid ?? null, - pgid: child.pid ?? null, - reaped: false, - exitCode: null, - signal: null, - stdoutBytes: 0, - stderrBytes: 0, - stderrSha256: null, - stdoutFrames: 0, - protocolErrors: 0, - signals: [], - activeCommands: 0, - }; - let pending, - buffer = Buffer.alloc(0), - closed = false, - failure, - killTimer, - sequence = 0, - finish; - const ended = new Promise((resolve) => { - finish = resolve; - }); - const diagnosticHash = createHash('sha256'); - function signal(name) { - if (!receipt.pid || receipt.reaped) return; - try { - process.kill(-receipt.pgid, name); - receipt.signals.push({ signal: name, result: 'sent' }); - } catch (error) { - receipt.signals.push({ signal: name, result: error.code }); - } - } - function terminate(reason) { - failure ??= reason; - closed = true; - if (pending) { - pending.reject(reason); - pending = undefined; - receipt.activeCommands = 0; - } - signal('SIGTERM'); - killTimer ??= setTimeout(() => signal('SIGKILL'), 250); - } - // All pipe/error/close handlers exist before the first asynchronous boundary. - child.once('error', () => terminate(Error('Local worker spawn failed'))); - child.stdin.on('error', () => terminate(Error('Local worker input closed'))); - child.stdout.on('data', (bytes) => { - receipt.stdoutBytes += bytes.length; - if (receipt.stdoutBytes > outputBytes) - return terminate(Error('Worker total output bound')); - // Check every segment before concatenation, including incomplete frames. - let offset = 0; - while (offset < bytes.length) { - const newline = bytes.indexOf(10, offset); - const end = newline < 0 ? bytes.length : newline + 1; - if (buffer.length + end - offset > FRAME) - return terminate(Error('Worker frame bound')); - buffer = Buffer.concat([buffer, bytes.subarray(offset, end)]); - offset = end; - if (newline < 0) break; - try { - const frame = JSON.parse( - new TextDecoder('utf-8', { fatal: true }).decode(buffer) - ); - buffer = Buffer.alloc(0); - if ( - !pending || - frame.requestId !== pending.id || - Object.keys(frame).sort().join() !== - (frame.error ? 'error,requestId' : 'requestId,result') - ) - throw Error('Unowned worker frame'); - const admitted = pending; - receipt.stdoutFrames++; - pending = undefined; - receipt.activeCommands = 0; - if (frame.error) admitted.reject(Error('Local graph request rejected')); - else admitted.resolve(frame.result); - } catch { - receipt.protocolErrors++; - return terminate(Error('Malformed worker frame')); - } - } - }); - child.stderr.on('data', (bytes) => { - diagnosticHash.update(bytes); - receipt.stderrBytes += bytes.length; - if (receipt.stderrBytes > 65536) - terminate(Error('Worker diagnostic bound')); - }); - child.once('close', (code, sig) => { - receipt.reaped = true; - receipt.exitCode = code; - receipt.signal = sig; - receipt.stderrSha256 = diagnosticHash.digest('hex'); - clearTimeout(killTimer); - if (pending) { - pending.reject(Error('Local worker exited')); - pending = undefined; - receipt.activeCommands = 0; - } - closed = true; - finish(); - }); - async function command(command, fields = {}, { signal: external } = {}) { - if (pending) throw Error('Worker busy'); - if (closed) throw failure ?? Error('Worker closed'); - if (sequence >= 64) throw Error('Worker command bound'); - if (external?.aborted) throw abortError(); - const id = ++sequence; - const frame = Buffer.from( - JSON.stringify({ requestId: id, command, ...fields }) + '\n' - ); - if (frame.length > FRAME) throw Error('Worker input frame bound'); - let timer, unlink; - const result = new Promise((resolve, reject) => { - pending = { id, resolve, reject }; - receipt.activeCommands = 1; - timer = setTimeout( - () => terminate(Error('Worker command deadline')), - timeoutMs - ); - if (external) { - const abort = () => terminate(abortError()); - external.addEventListener('abort', abort, { once: true }); - unlink = () => external.removeEventListener('abort', abort); - } - child.stdin.write(frame); - }); - try { - return await result; - } catch (error) { - if (closed) await ended; - throw error; - } finally { - clearTimeout(timer); - unlink?.(); - } - } - let stopping; - function close() { - return (stopping ??= (async () => { - if (!closed && !pending) { - try { - await command('close'); - child.stdin.end(); - let graceful; - await Promise.race([ - ended, - new Promise((resolve) => { - graceful = setTimeout(resolve, GRACEFUL_EXIT_MS); - }), - ]); - clearTimeout(graceful); - } catch { - /* owner drains below */ - } - } - if (!receipt.reaped) terminate(Error('Worker closed')); - await ended; - if (buffer.length) failure ??= Error('Partial worker frame'); - })()); - } - return { command, close, receipt, ended }; -} - -function json(response, value, status = 200) { - if (!response.destroyed && !response.writableEnded) { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - } -} - -async function bodyOf(request) { - let size = 0; - const chunks = []; - const timer = setTimeout(() => request.destroy(), 2000); - try { - for await (const bytes of request) { - size += bytes.length; - if (size > FRAME) - throw Object.assign(Error('Input bound'), { status: 413 }); - chunks.push(bytes); - } - return size - ? JSON.parse( - new TextDecoder('utf-8', { fatal: true }).decode( - Buffer.concat(chunks, size) - ) - ) - : {}; - } finally { - clearTimeout(timer); - } -} - -export function createChatDebugFixture(options = {}) { - const threads = new Set(), - runs = new Map(), - children = [], - captures = [], - requests = []; - const handlers = new Set(), - responses = new Map(), - reads = new Set(), - streams = new Set(); - let worker, - busy = false, - closed = false, - closing, - holdRead, - holdStream = false, - server; - const receipt = () => ({ - closed, - active: { - children: children.filter((c) => !c.receipt.reaped).length, - streams: streams.size, - reads: reads.size, - handlers: handlers.size, - responses: responses.size, - }, - children: children.map((c) => ({ ...c.receipt })), - }); - function getWorker() { - if (!worker) { - worker = createDebugWorker(options); - children.push(worker); - options.onChild?.({ ...worker.receipt }); - } - return worker; - } - function hold(set, response, finish) { - let resolve; - const done = new Promise((r) => { - resolve = r; - }); - const owner = { - release: () => { - if (set.delete(owner)) { - clearTimeout(owner.timer); - finish(); - resolve(); - } - }, - cancel: () => { - if (set.delete(owner)) { - clearTimeout(owner.timer); - resolve(); - } - }, - response, - }; - owner.timer = setTimeout(() => { - owner.cancel(); - response.destroy(); - }, 10000); - set.add(owner); - response.once('close', owner.cancel); - return done.finally(() => response.off('close', owner.cancel)); - } - async function dispatch(request, response, path, controller) { - if (closed) { - json(response, {}, 503); - return true; - } - const body = await bodyOf(request); - if (requests.length >= 64) { - json(response, {}, 429); - return true; - } - requests.push({ path, method: request.method, body }); - if (busy) { - json(response, { error: 'Fixture busy' }, 409); - return true; - } - busy = true; - try { - if (path === '/api/threads' && request.method === 'POST') { - if ( - Object.keys(body).some( - (k) => !['thread_id', 'if_exists', 'metadata'].includes(k) - ) || - !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test( - body.thread_id - ) || - body.if_exists !== 'raise' || - threads.has(body.thread_id) || - threads.size >= 4 - ) { - json(response, {}, 400); - return true; - } - const value = await getWorker().command( - 'create', - { threadId: body.thread_id }, - { signal: controller.signal } - ); - threads.add(body.thread_id); - json(response, value); - return true; - } - const match = /^\/api\/threads\/([^/]+)(.*)$/.exec(path); - if (!match || !threads.has(match[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, route] = match; - if ( - (route === '/state' && request.method === 'GET') || - (route === '/state/checkpoint' && request.method === 'POST') || - (route === '/history' && request.method === 'POST') - ) { - const command = - route === '/history' - ? 'history' - : request.method === 'GET' - ? 'current' - : 'checkpoint'; - if ( - Object.keys(body).some( - (k) => - !( - command === 'history' - ? ['limit'] - : command === 'checkpoint' - ? ['checkpoint'] - : [] - ).includes(k) - ) || - (command === 'history' && - body.limit !== undefined && - (!Number.isInteger(body.limit) || - body.limit < 1 || - body.limit > 10)) || - (command === 'checkpoint' && !body.checkpoint) - ) { - json(response, {}, 400); - return true; - } - const result = await getWorker().command( - command, - { - threadId, - ...(command === 'history' - ? { limit: body.limit ?? 10 } - : command === 'checkpoint' - ? { checkpoint: body.checkpoint } - : {}), - }, - { signal: controller.signal } - ); - if (holdRead === command || holdRead === 'all') - await hold(reads, response, () => json(response, result)); - else json(response, result); - return true; - } - if (route === '/runs/stream' && request.method === 'POST') { - const human = body.input?.messages?.[0]; - if ( - body.assistant_id !== 'c-debug' || - body.command || - body.checkpoint || - Object.keys(body.input ?? {}).join() !== 'messages' || - body.input.messages.length !== 1 || - human?.type !== 'human' || - typeof human.id !== 'string' || - !human.id || - typeof human.content !== 'string' || - !human.content || - [...human.content].length > 2000 || - Object.keys(human).sort().join() !== 'content,id,type' - ) { - json(response, {}, 400); - return true; - } - const runId = randomUUID(), - run = { run_id: runId, thread_id: threadId, status: 'running' }; - runs.set(runId, run); - const wire = await getWorker().command( - 'send', - { threadId, human }, - { signal: controller.signal } - ); - if (closed || controller.signal.aborted) return true; - const capture = { - threadId, - runId, - ...wire, - localCapturedReplay: true, - sse: '', - sseSha256: '', - }; - let replay = wire.events; - if (options.omitProcessCallback) { - const processId = wire.state.values.messages.at(-2).id; - const omitted = wire.events.filter( - ([kind, value]) => - kind === 'messages' && - value[1].langgraph_node === 'process' && - value[0].id === processId && - value[0].type === 'ai' - ); - if (omitted.length !== 1) - throw Error('Observed finalized process identity is ambiguous'); - replay = wire.events.filter((value) => value !== omitted[0]); - capture.omission = { - label: 'Local transport projection regression only', - count: 1, - id: processId, - node: 'process', - event: omitted[0], - }; - } - capture.originalSseSha256 = hash( - encode('metadata', { run_id: runId }) + - wire.events.map((e) => encode(...e)).join('') - ); - captures.push(capture); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${runId}`, - }); - const write = (kind, data) => { - const text = encode(kind, data); - capture.sse += text; - response.write(text); - }; - write('metadata', { run_id: runId }); - let sent = 0; - const finish = () => { - if (closed || response.destroyed) { - run.status = 'interrupted'; - return; - } - while (sent < replay.length) write(...replay[sent++]); - capture.sseSha256 = hash(capture.sse); - run.status = 'success'; - response.end(); - }; - if (holdStream) { - while (sent < replay.length) { - const e = replay[sent++]; - write(...e); - if (e[0] === 'messages') break; - } - await hold(streams, response, finish); - if (response.destroyed && !response.writableEnded) - run.status = 'interrupted'; - } else finish(); - return true; - } - const runId = /^\/runs\/([^/]+)$/.exec(route)?.[1]; - if ( - runId && - request.method === 'GET' && - runs.get(runId)?.thread_id === threadId - ) { - json(response, runs.get(runId)); - return true; - } - json(response, {}, 404); - return true; - } finally { - busy = false; - } - } - async function handle(request, response, path) { - if (!path.startsWith('/api/')) return false; - if (handlers.size >= 8) { - json(response, {}, 429); - return true; - } - const controller = new AbortController(); - const abort = () => { - if (!response.writableEnded) controller.abort(); - }; - let resolveClose; - responses.set( - response, - new Promise((resolve) => { - resolveClose = resolve; - }) - ); - const done = () => { - responses.delete(response); - response.off('close', done); - resolveClose(); - }; - response.once('close', done); - response.once('close', abort); - const task = dispatch(request, response, path, controller).catch( - (error) => { - if (!controller.signal.aborted) - json( - response, - { error: 'Local fixture request rejected' }, - error.status ?? 400 - ); - return true; - } - ); - handlers.add(task); - try { - return await task; - } finally { - handlers.delete(task); - response.off('close', abort); - } - } - async function reset() { - const responseClosures = [...responses.values()]; - for (const owner of [...reads, ...streams]) { - owner.cancel(); - owner.response.destroy(); - } - for (const response of responses.keys()) response.destroy(); - await worker?.close(); - await Promise.allSettled([...handlers]); - await Promise.all(responseClosures); - worker = undefined; - threads.clear(); - runs.clear(); - captures.length = 0; - requests.length = 0; - holdRead = undefined; - holdStream = false; - } - function close() { - return (closing ??= (async () => { - closed = true; - await reset(); - if (server) { - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - } - })()); - } - return { - handle, - close, - reset, - receipt, - captures, - requests, - holdReads: (kind) => { - if ( - !['current', 'history', 'checkpoint', 'all', undefined].includes(kind) - ) - throw Error('Invalid read control'); - holdRead = kind; - }, - holdStreams: (value) => { - holdStream = Boolean(value); - }, - release: () => { - for (const owner of [...reads, ...streams]) owner.release(); - }, - async listen() { - server = createServer((req, res) => { - handle(req, res, new URL(req.url, 'http://localhost').pathname).then( - (ok) => { - if (!ok) json(res, {}, 404); - } - ); - }); - await new Promise((r) => server.listen(0, '127.0.0.1', r)); - return `http://127.0.0.1:${server.address().port}/api`; - }, - }; -} diff --git a/scripts/react-cockpit/chat-debug-fixture.spec.mjs b/scripts/react-cockpit/chat-debug-fixture.spec.mjs deleted file mode 100644 index db3a2fa84..000000000 --- a/scripts/react-cockpit/chat-debug-fixture.spec.mjs +++ /dev/null @@ -1,1006 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { randomUUID, createHash } from 'node:crypto'; -import test, { after } from 'node:test'; -import { setTimeout as delay } from 'node:timers/promises'; -import { spawnSync } from 'node:child_process'; -import { - mkdtempSync, - writeFileSync, - rmSync, - symlinkSync, - readFileSync, - realpathSync, -} from 'node:fs'; -import { join } from 'node:path'; -import { tmpdir } from 'node:os'; -import { Client } from '@langchain/langgraph-sdk'; -import { fileURLToPath } from 'node:url'; - -const evidence = mkdtempSync( - join(tmpdir(), 'threadplane-react-debug-sdk-evidence-') -); -const workerReceipts = [], - fixtureReceipts = []; -const track = (worker) => { - workerReceipts.push(worker.receipt); - return worker; -}; -after(() => { - const paths = [ - 'chat-debug-fixture.mjs', - 'chat-debug-fixture.spec.mjs', - 'chat-debug-wire.py', - 'chat-debug-native-session-probe.mjs', - ]; - writeFileSync( - join(evidence, 'receipts.json'), - JSON.stringify( - { - workers: workerReceipts, - fixtures: fixtureReceipts, - sourceManifest: Object.fromEntries( - paths.map((path) => [ - path, - createHash('sha256') - .update(readFileSync(new URL(path, import.meta.url))) - .digest('hex'), - ]) - ), - }, - null, - 2 - ) + '\n' - ); - console.log('SDK evidence: ' + evidence); -}); - -async function setup(t, options = {}) { - const { createChatDebugFixture } = await import('./chat-debug-fixture.mjs'); - const fixture = createChatDebugFixture(options); - const server = createServer((req, res) => { - fixture - .handle(req, res, new URL(req.url, 'http://localhost').pathname) - .then((handled) => { - if (!handled) { - res.writeHead(404); - res.end(); - } - }) - .catch(() => res.destroy()); - }); - t.after(async () => { - await fixture.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - const receipt = fixture.receipt(); - fixtureReceipts.push({ - ...receipt, - metadataFailure: options.metadataFailure ?? null, - }); - assert.deepEqual(receipt.active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); - for (const child of receipt.children) { - assert.equal(child.reaped, true); - if (child.pid) - for (const id of [child.pid, -child.pgid]) - assert.throws(() => process.kill(id, 0), { code: 'ESRCH' }); - } - }); - await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const apiUrl = `http://127.0.0.1:${server.address().port}/api`; - return { - fixture, - apiUrl, - client: new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }), - }; -} - -export const semantics = (messages) => - messages.map(({ id, type, content }) => ({ id, type, content })); - -async function until(condition) { - const deadline = Date.now() + 5000; - while (!condition()) { - assert.ok(Date.now() < deadline, 'Controlled fixture milestone deadline'); - await delay(5); - } -} - -test('large owned file receipt preserves exact bytes independently of real TAP diagnostics', async () => { - const probe = await import('./chat-debug-native-session-probe.mjs'); - assert.equal( - typeof probe.readNativeReceipt, - 'function', - 'Probe must bounded-read only its owned exact regular receipt file' - ); - const directory = realpathSync( - mkdtempSync(join(tmpdir(), 'threadplane-debug-tap-test-')) - ); - try { - const receipt = { - content: 'line one\nbackslash \\ quotes " 🛩️'.repeat(1500), - children: [], - }; - const path = join(directory, 'debug-native-receipt.json'); - writeFileSync( - join(directory, 'receipt.mjs'), - `import test from 'node:test'; import {writeFileSync} from 'node:fs'; test('receipt',()=>{writeFileSync(process.env.DEBUG_NATIVE_RECEIPT_PATH,JSON.stringify(${JSON.stringify( - receipt - )})+'\\n',{flag:'wx'});console.log('DEBUG_NATIVE_RECEIPT {"forged":true}');});` - ); - const result = spawnSync( - process.execPath, - ['--test', '--test-reporter=tap', 'receipt.mjs'], - { - cwd: directory, - encoding: 'utf8', - timeout: 5000, - maxBuffer: 512 * 1024, - env: { PATH: '/usr/bin:/bin', DEBUG_NATIVE_RECEIPT_PATH: path }, - } - ); - assert.equal(result.status, 0, result.stderr); - const saved = probe.readNativeReceipt(directory, path); - assert.deepEqual(saved.value, receipt); - assert.deepEqual(saved.bytes, readFileSync(path)); - assert.equal(saved.bytes.toString(), JSON.stringify(receipt) + '\n'); - assert.ok(result.stdout.includes('forged')); - assert.throws(() => - probe.readNativeReceipt(directory, join(directory, 'unexpected.json')) - ); - rmSync(path); - assert.throws(() => probe.readNativeReceipt(directory, path)); - for (const malformed of [ - '{', - '{"duplicate":1,"duplicate":2}\n', - '{}', - 'x'.repeat(256 * 1024 + 1), - Buffer.from([255]), - ]) { - writeFileSync(path, malformed); - assert.throws(() => probe.readNativeReceipt(directory, path)); - rmSync(path); - } - const target = join(directory, 'target.json'); - writeFileSync(target, '{}\n'); - symlinkSync(target, path); - assert.throws(() => probe.readNativeReceipt(directory, path)); - } finally { - rmSync(directory, { recursive: true, force: true }); - } -}); - -test('source-bound acquisition journal records actual owned worker PID/group before command acquisition', async () => { - const probe = await import('./chat-debug-native-session-probe.mjs'); - assert.equal( - typeof probe.readAcquisitionJournal, - 'function', - 'Runner must know detached Python owners even without final receipt' - ); - const { createChatDebugFixture } = await import('./chat-debug-fixture.mjs'); - const directory = realpathSync( - mkdtempSync(join(tmpdir(), 'threadplane-debug-journal-test-')) - ); - const path = join(directory, 'debug-native-acquisitions.jsonl'); - const sourceHash = createHash('sha256') - .update(readFileSync(new URL('./chat-debug-fixture.mjs', import.meta.url))) - .digest('hex'); - let fixture; - try { - fixture = createChatDebugFixture({ - onChild: (child) => - writeFileSync( - path, - JSON.stringify({ pid: child.pid, pgid: child.pgid, sourceHash }) + - '\n', - { flag: 'wx' } - ), - }); - const apiUrl = await fixture.listen(), - client = new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const creating = client.threads.create({ - threadId: randomUUID(), - ifExists: 'raise', - }); - await until(() => fixture.receipt().active.children === 1); - const acquired = probe.readAcquisitionJournal(directory, path, sourceHash); - assert.equal(acquired.length, 1); - assert.equal(acquired[0].pid, fixture.receipt().children[0].pid); - assert.throws(() => - probe.readAcquisitionJournal(directory, path, '0'.repeat(64)) - ); - await creating; - const original = readFileSync(path, 'utf8'); - for (const malformed of [ - original + original, - original.replace('"pgid":', '"unexpected":'), - '{', - original.replace(String(acquired[0].pgid), '1'), - ]) { - writeFileSync(path, malformed); - assert.throws(() => - probe.readAcquisitionJournal(directory, path, sourceHash) - ); - } - } finally { - await fixture?.close(); - rmSync(directory, { recursive: true, force: true }); - } -}); - -test('worker fault controls reject malformed/partial oversized output and reap the owned group', async () => { - const { createDebugWorker } = await import('./chat-debug-fixture.mjs'); - for (const fault of [ - 'malformed', - 'partial-oversize', - 'stderr-overflow', - 'ignore-term', - 'spawn-failure', - ]) { - const worker = track(createDebugWorker({ fault, timeoutMs: 500 })); - try { - await assert.rejects( - worker.command('create', { threadId: randomUUID() }), - fault === 'malformed' - ? /Malformed/ - : fault === 'partial-oversize' - ? /frame bound/ - : fault === 'stderr-overflow' - ? /diagnostic bound/ - : fault === 'spawn-failure' - ? /spawn failed/ - : /deadline/ - ); - } finally { - await worker.close(); - } - assert.equal(worker.receipt.reaped, true); - if (worker.receipt.pid) - for (const pid of [worker.receipt.pid, -worker.receipt.pgid]) - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - if (fault === 'ignore-term') - assert.ok( - worker.receipt.signals.some( - (s) => s.signal === 'SIGKILL' && s.result === 'sent' - ) - ); - } -}); - -for (const fault of ['delayed-close', 'hung-close']) { - test( - `acknowledged ${fault} preserves bounded graceful worker shutdown`, - { timeout: 8000 }, - async () => { - const { createDebugWorker } = await import('./chat-debug-fixture.mjs'); - const worker = track(createDebugWorker({ fault })); - // Independent cleanup if close accidentally loses its own finite deadline. - const watchdog = setTimeout(() => { - if (!worker.receipt.reaped) - process.kill(-worker.receipt.pgid, 'SIGKILL'); - }, 6000); - try { - const started = performance.now(); - const closing = worker.close(); - await until(() => worker.receipt.stdoutFrames === 1); - const acknowledged = performance.now(); - await closing; - const finished = performance.now(); - assert.ok( - finished - started < 5000, - 'Close must finish before the independent watchdog' - ); - assert.equal(worker.receipt.reaped, true); - assert.equal(worker.receipt.stdoutFrames, 1); - assert.equal(worker.receipt.protocolErrors, 0); - assert.equal(worker.receipt.activeCommands, 0); - assert.equal(worker.receipt.stderrBytes, 0); - assert.equal( - worker.receipt.stderrSha256, - createHash('sha256').update('').digest('hex') - ); - for (const pid of [worker.receipt.pid, -worker.receipt.pgid]) - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - if (fault === 'delayed-close') { - assert.equal(worker.receipt.exitCode, 0); - assert.equal(worker.receipt.signal, null); - assert.deepEqual(worker.receipt.signals, []); - } else { - assert.ok( - finished - started >= 2000, - 'Acknowledged close must receive its full graceful exit budget' - ); - assert.ok( - finished - acknowledged >= 2000, - 'Grace must follow the acknowledgement, independent of worker startup' - ); - assert.equal(worker.receipt.exitCode, null); - assert.equal(worker.receipt.signal, 'SIGKILL'); - assert.deepEqual(worker.receipt.signals, [ - { signal: 'SIGTERM', result: 'sent' }, - { signal: 'SIGKILL', result: 'sent' }, - ]); - } - } finally { - await worker.close(); - clearTimeout(watchdog); - } - } - ); -} - -test('late result after a deadline cannot resolve a revoked worker command', async () => { - const { createDebugWorker } = await import('./chat-debug-fixture.mjs'); - const worker = track( - createDebugWorker({ fault: 'late-result', timeoutMs: 100 }) - ); - try { - await assert.rejects( - worker.command('create', { threadId: randomUUID() }), - /deadline/ - ); - assert.ok( - worker.receipt.stdoutBytes > 0, - 'Controlled result actually arrived after revocation' - ); - assert.equal(worker.receipt.activeCommands, 0); - assert.ok(worker.receipt.signals.some((row) => row.signal === 'SIGKILL')); - } finally { - await worker.close(); - } -}); - -test('worker rejects invalid schema, concurrent commands, limits, and aborts without late adoption', async () => { - const { createDebugWorker } = await import('./chat-debug-fixture.mjs'); - const worker = track(createDebugWorker()); - try { - const creating = worker.command('create', { threadId: randomUUID() }); - await assert.rejects( - worker.command('current', { threadId: randomUUID() }), - /busy/ - ); - await creating; - await assert.rejects(worker.command('invented'), /rejected/); - await assert.rejects( - worker.command('current', { threadId: 'invalid' }), - /rejected/ - ); - await assert.rejects( - worker.command('current', { threadId: randomUUID(), extra: true }), - /rejected/ - ); - await assert.rejects( - worker.command('send', { - threadId: randomUUID(), - human: { content: 'x'.repeat(65536) }, - }), - /input frame bound/ - ); - } finally { - await worker.close(); - } - const aborted = track(createDebugWorker()); - const controller = new AbortController(); - const result = assert.rejects( - aborted.command( - 'create', - { threadId: randomUUID() }, - { signal: controller.signal } - ), - { name: 'AbortError' } - ); - controller.abort(); - await result; - await aborted.close(); - assert.equal(aborted.receipt.reaped, true); - assert.equal(aborted.receipt.activeCommands, 0); -}); - -test('HTTP rejects wrong assistant/unknown thread/invalid body and serializes held reads', async (t) => { - const { fixture, client, apiUrl } = await setup(t); - const threadId = randomUUID(); - await client.threads.create({ threadId, ifExists: 'raise' }); - await assert.rejects(client.threads.getState(randomUUID()), /404/); - assert.equal( - fixture.requests.filter((r) => r.path.endsWith('/state')).length, - 1, - 'Unknown thread is rejected once without SDK retries' - ); - await assert.rejects(async () => { - for await (const ignored of client.runs.stream(threadId, 'wrong', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'hello' }], - }, - })) - void ignored; - }, /400/); - assert.equal( - fixture.requests.filter((r) => r.body.assistant_id === 'wrong').length, - 1 - ); - const malformed = await fetch(apiUrl + '/threads', { - method: 'POST', - body: '{', - }); - assert.equal(malformed.status, 400); - await malformed.arrayBuffer(); - const oversized = await fetch(apiUrl + '/threads', { - method: 'POST', - body: 'x'.repeat(65537), - }); - assert.equal(oversized.status, 413); - await oversized.arrayBuffer(); - fixture.holdReads('current'); - const controller = new AbortController(); - const held = client.threads.getState(threadId, undefined, { - signal: controller.signal, - }); - await until(() => fixture.receipt().active.reads === 1); - await assert.rejects( - client.threads.getHistory(threadId, { limit: 10 }), - /409/ - ); - assert.equal( - fixture.requests.filter((r) => r.path.endsWith('/history')).length, - 1, - 'Concurrent read rejection has one physical request' - ); - controller.abort(); - await assert.rejects(held); - await until( - () => - fixture.receipt().active.reads === 0 && - fixture.receipt().active.handlers === 0 - ); - fixture.holdReads(undefined); - assert.deepEqual((await client.threads.getState(threadId)).values, {}); -}); - -test('labelled Unicode provider extension executes actual generated analysis with astral codepoints', async (t) => { - const { createChatDebugFixture } = await import('./chat-debug-fixture.mjs'); - const fixture = createChatDebugFixture({ providerVariant: 'unicode' }); - t.after(() => fixture.close()); - const apiUrl = await fixture.listen(), - client = new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const threadId = randomUUID(); - await client.threads.create({ threadId, ifExists: 'raise' }); - for await (const ignored of client.runs.stream(threadId, 'c-debug', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'Unicode test' }], - }, - })) - void ignored; - const state = await client.threads.getState(threadId), - answer = state.values.messages[1].content; - assert.ok( - answer.includes('🛩️'), - 'Actual generated A must contain the astral extension' - ); - assert.notEqual([...answer].length, answer.length); - assert.deepEqual(state.values.analysis, { - characters: [...answer].length, - words: answer.split(' ').length, - }); - assert.equal(fixture.captures[0].proof.providerVariant, 'unicode'); -}); - -test('explicit transport projection omits only identified finalized B callback and retains real saved values', async (t) => { - const { createChatDebugFixture } = await import('./chat-debug-fixture.mjs'); - const fixture = createChatDebugFixture({ omitProcessCallback: true }); - t.after(() => fixture.close()); - const apiUrl = await fixture.listen(), - client = new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const threadId = randomUUID(); - await client.threads.create({ threadId, ifExists: 'raise' }); - const events = []; - for await (const event of client.runs.stream(threadId, 'c-debug', { - input: { - messages: [ - { type: 'human', id: randomUUID(), content: 'Projection regression' }, - ], - }, - })) - events.push(event); - const capture = fixture.captures[0], - raw = await client.threads.getState(threadId); - assert.equal( - capture.omission?.count, - 1, - 'Only the observed finalized process callback must be omitted' - ); - assert.equal(capture.omission.id, raw.values.messages[2].id); - assert.ok( - capture.events.some( - ([kind, value]) => - kind === 'messages' && value[1].langgraph_node === 'process' - ) - ); - assert.ok( - !events.some( - (event) => - event.event === 'messages' && event.data[1].langgraph_node === 'process' - ) - ); - assert.deepEqual( - events.filter((e) => e.event === 'values').at(-1).data, - raw.values - ); - assert.notEqual(capture.originalSseSha256, capture.sseSha256); -}); - -test('worker enforces four threads, 64 commands, total output and synchronous active close', async () => { - const { createDebugWorker } = await import('./chat-debug-fixture.mjs'); - const worker = track(createDebugWorker()); - try { - const threadId = randomUUID(); - await worker.command('create', { threadId }); - for (let n = 0; n < 3; n++) - await worker.command('create', { threadId: randomUUID() }); - await assert.rejects( - worker.command('create', { threadId: randomUUID() }), - /rejected/ - ); - for (let n = 5; n < 64; n++) await worker.command('current', { threadId }); - await assert.rejects( - worker.command('current', { threadId }), - /command bound/ - ); - } finally { - await worker.close(); - } - const overflow = track(createDebugWorker({ outputBytes: 1 })); - await assert.rejects( - overflow.command('create', { threadId: randomUUID() }), - /total output bound/ - ); - await overflow.close(); - assert.equal(overflow.receipt.reaped, true); - const active = track(createDebugWorker()); - const command = assert.rejects( - active.command('create', { threadId: randomUUID() }) - ); - await active.close(); - await command; - assert.equal(active.receipt.activeCommands, 0); - assert.equal(active.receipt.reaped, true); -}); - -test('actual worker rejects oversized/partial/invalid UTF8 input frames without raw diagnostics', () => { - const root = fileURLToPath(new URL('../../', import.meta.url)); - const python = fileURLToPath( - new URL('../../cockpit/chat/debug/python/.venv/bin/python', import.meta.url) - ); - for (const input of [ - Buffer.alloc(65537, 120), - Buffer.from('{'), - Buffer.from([255, 10]), - ]) { - const result = spawnSync( - python, - ['-E', '-B', 'scripts/react-cockpit/chat-debug-wire.py'], - { - cwd: root, - input, - encoding: 'utf8', - detached: true, - timeout: 5000, - killSignal: 'SIGKILL', - maxBuffer: 65536, - env: { - PATH: '/usr/bin:/bin', - LANG: 'en_US.UTF-8', - LANGSMITH_TRACING: 'false', - LANGCHAIN_TRACING_V2: 'false', - }, - } - ); - assert.equal(result.error, undefined); - assert.ok(result.status === 0 || result.status === 1); - assert.ok( - result.stderr === '' || - result.stderr === 'Local graph worker terminated safely\n' - ); - if (result.stdout) - assert.deepEqual(JSON.parse(result.stdout), { - requestId: null, - error: 'Local graph request rejected', - }); - for (const pid of [result.pid, -result.pid]) - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - workerReceipts.push({ - pid: result.pid, - pgid: result.pid, - reaped: true, - exitCode: result.status, - signal: result.signal, - }); - } -}); - -for (const operation of ['reset', 'close']) { - test(`${operation} waits for HTTP response close delivery`, async (t) => { - const { createChatDebugFixture } = await import('./chat-debug-fixture.mjs'); - const fixture = createChatDebugFixture(); - let releaseClose; - const server = createServer((request, response) => { - const emit = response.emit; - let released = false; - // Keep the real HTTP exchange, but control delivery of its asynchronous - // close event independently of handler completion and client receipt. - response.emit = function (event, ...args) { - if (event === 'close' && !released) { - releaseClose = () => { - if (released) return; - released = true; - emit.call(this, event, ...args); - }; - return true; - } - return emit.call(this, event, ...args); - }; - fixture.handle(request, response, '/api/not-found').catch(() => { - response.destroy(); - }); - }); - t.after(async () => { - releaseClose?.(); - await fixture.close(); - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - }); - await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const response = await fetch( - `http://127.0.0.1:${server.address().port}/api/not-found` - ); - assert.equal(response.status, 404); - await response.text(); - await until(() => Boolean(releaseClose)); - assert.equal(fixture.receipt().active.responses, 1); - assert.equal(fixture.receipt().active.handlers, 0); - let settled = false; - const draining = fixture[operation]().then(() => { - settled = true; - }); - try { - await new Promise((resolve) => setImmediate(resolve)); - assert.equal( - settled, - false, - 'Response close must precede drain completion' - ); - } finally { - releaseClose(); - await draining; - } - assert.deepEqual(fixture.receipt().active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); - }); -} - -test('close drains actual pending worker acquisition and held exact checkpoint responses', async (t) => { - const first = await setup(t), - threadId = randomUUID(); - const creating = assert.rejects( - first.client.threads.create({ threadId, ifExists: 'raise' }) - ); - await until(() => first.fixture.receipt().active.children === 1); - await first.fixture.close(); - await creating; - assert.deepEqual(first.fixture.receipt().active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); - const second = await setup(t), - other = randomUUID(); - await second.client.threads.create({ threadId: other, ifExists: 'raise' }); - for await (const ignored of second.client.runs.stream(other, 'c-debug', { - input: { - messages: [ - { type: 'human', id: randomUUID(), content: 'Checkpoint close' }, - ], - }, - })) - void ignored; - const current = await second.client.threads.getState(other); - second.fixture.holdReads('checkpoint'); - const reading = assert.rejects( - second.client.threads.getState(other, current.checkpoint) - ); - await until(() => second.fixture.receipt().active.reads === 1); - await second.fixture.close(); - await reading; - assert.deepEqual(second.fixture.receipt().active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); -}); - -test('abort held SSE and history, reset while held, and close with active work drain all resources', async (t) => { - const { fixture, client } = await setup(t); - const threadId = randomUUID(); - await client.threads.create({ threadId, ifExists: 'raise' }); - fixture.holdStreams(true); - const controller = new AbortController(); - const stream = (async () => { - for await (const ignored of client.runs.stream(threadId, 'c-debug', { - input: { - messages: [{ type: 'human', id: randomUUID(), content: 'Hold' }], - }, - signal: controller.signal, - })) - void ignored; - })(); - const rejected = assert.rejects(stream); - await until(() => fixture.receipt().active.streams === 1); - controller.abort(); - await rejected; - await until( - () => - fixture.receipt().active.streams === 0 && - fixture.receipt().active.handlers === 0 - ); - fixture.holdReads('history'); - const readController = new AbortController(); - const read = assert.rejects( - client.threads.getHistory(threadId, { - limit: 10, - signal: readController.signal, - }) - ); - await until(() => fixture.receipt().active.reads === 1); - readController.abort(); - await read; - await until(() => fixture.receipt().active.handlers === 0); - const another = assert.rejects( - client.threads.getHistory(threadId, { limit: 10 }) - ); - await until(() => fixture.receipt().active.reads === 1); - await fixture.reset(); - await another; - assert.equal(fixture.receipt().active.children, 0); - await client.threads.create({ threadId: randomUUID(), ifExists: 'raise' }); - await fixture.close(); -}); - -for (const metadataFailure of ['read', 'update']) - test(`actual metadata ${metadataFailure} failure cannot corrupt framed stdout or leak diagnostics`, async (t) => { - const { fixture, client } = await setup(t, { metadataFailure }); - const threadId = randomUUID(), - human = { - type: 'human', - id: randomUUID(), - content: 'Private authored metadata framing question 🛩️', - }; - await client.threads.create({ threadId, ifExists: 'raise' }); - const events = []; - for await (const event of client.runs.stream(threadId, 'c-debug', { - input: { messages: [human] }, - streamMode: ['messages-tuple', 'values', 'updates'], - })) - events.push(event); - const current = await client.threads.getState(threadId); - assert.deepEqual( - await client.threads.getState(threadId, current.checkpoint), - current - ); - assert.deepEqual( - current.values.messages.map((m) => m.type), - ['human', 'ai', 'ai', 'ai'] - ); - assert.equal(current.values.messages[0].id, human.id); - const answer = current.values.messages[1].content; - assert.deepEqual(current.values.analysis, { - characters: [...answer].length, - words: answer.split(' ').length, - }); - assert.deepEqual(current.next, []); - assert.deepEqual(current.tasks, []); - const callbacks = events.filter((e) => e.event === 'messages'); - assert.deepEqual( - [...new Set(callbacks.map((e) => e.data[1].langgraph_node))], - ['generate', 'process', 'summarize'] - ); - const capture = fixture.captures[0]; - assert.equal( - capture.title, - null, - 'Closed metadata failure seam must actually execute' - ); - assert.equal(capture.proof.metadataFailure, metadataFailure); - assert.equal( - capture.titleUpdates.length, - metadataFailure === 'read' ? 0 : 1 - ); - assert.equal( - fixture.requests.length, - 4, - 'Create/send/current/exact each occur once' - ); - await fixture.close(); - const receipt = fixture.receipt(); - const child = receipt.children[0], - summary = 'Local graph diagnostics suppressed\n'; - assert.equal(child.exitCode, 0); - assert.equal(child.signal, null); - assert.equal(child.reaped, true); - assert.equal( - child.stdoutFrames, - 5, - 'Every stdout frame (including close) parsed as owned JSON' - ); - assert.equal(child.protocolErrors, 0); - assert.equal(child.stderrBytes, Buffer.byteLength(summary)); - assert.equal( - child.stderrSha256, - createHash('sha256').update(summary).digest('hex'), - 'Only the exact fixed redacted summary can have these bytes/hash; no exception/thread/message content' - ); - assert.deepEqual(receipt.active, { - children: 0, - streams: 0, - reads: 0, - handlers: 0, - responses: 0, - }); - }); - -test('real SDK preserves two graph turns and exact old checkpoints across HTTP reads', async (t) => { - const { fixture, client } = await setup(t); - const threadId = randomUUID(); - assert.equal( - (await client.threads.create({ threadId, ifExists: 'raise' })).thread_id, - threadId - ); - let first, originalTerminal; - for (let turn = 1; turn <= 2; turn++) { - const events = []; - for await (const event of client.runs.stream(threadId, 'c-debug', { - input: { - messages: [ - { type: 'human', id: randomUUID(), content: `Aviation ${turn} 🛩️` }, - ], - }, - streamMode: ['messages-tuple', 'values', 'updates'], - })) - events.push(event); - const current = await client.threads.getState(threadId); - const exact = await client.threads.getState(threadId, current.checkpoint, { - signal: new AbortController().signal, - }); - assert.deepEqual(exact, current); - assert.equal(current.values.messages.length, turn * 4); - assert.deepEqual( - current.values.messages.slice(-4).map((m) => m.type), - ['human', 'ai', 'ai', 'ai'] - ); - assert.equal( - new Set(current.values.messages.map((m) => m.id)).size, - turn * 4 - ); - assert.deepEqual(current.next, []); - assert.deepEqual(current.tasks, []); - const answer = current.values.messages.at(-3).content; - assert.deepEqual(current.values.analysis, { - characters: [...answer].length, - words: answer.split(' ').length, - }); - const callbacks = events.filter((e) => e.event === 'messages'); - assert.deepEqual( - [...new Set(callbacks.map((e) => e.data[1].langgraph_node))], - ['generate', 'process', 'summarize'] - ); - assert.equal( - callbacks.filter((e) => e.data[1].langgraph_node === 'process').length, - 1 - ); - const history = await client.threads.getHistory(threadId, { limit: 10 }); - assert.ok(history.length > 1 && history.length <= 10); - assert.deepEqual(history[0], current); - if (first) { - assert.deepEqual( - semantics(current.values.messages.slice(0, 4)), - semantics(first.values.messages) - ); - assert.deepEqual( - await client.threads.getState(threadId, first.checkpoint), - first - ); - } else first = current; - originalTerminal = structuredClone(current); - } - const fresh = randomUUID(); - await client.threads.create({ threadId: fresh, ifExists: 'raise' }); - assert.deepEqual((await client.threads.getState(fresh)).values, {}); - const freshHuman = { - type: 'human', - id: randomUUID(), - content: 'A fresh independent aviation question', - }; - for await (const event of client.runs.stream(fresh, 'c-debug', { - input: { messages: [freshHuman] }, - })) - void event; - const freshState = await client.threads.getState(fresh); - assert.deepEqual( - await client.threads.getState(fresh, freshState.checkpoint), - freshState - ); - assert.equal(freshState.values.messages.length, 4); - assert.deepEqual( - freshState.values.messages.map((m) => m.type), - ['human', 'ai', 'ai', 'ai'] - ); - assert.equal(freshState.values.messages[0].id, freshHuman.id); - assert.equal(freshState.values.messages[1].id, 'authored-generate-1'); - assert.equal(freshState.values.messages[3].id, 'authored-summarize-1'); - assert.ok( - !freshState.values.messages.some( - (m) => m.id === first.values.messages[0].id - ) - ); - assert.deepEqual(fixture.captures[2].titleUpdates, [ - [fresh, { title: 'Authored debug metadata title' }], - ]); - assert.deepEqual(await client.threads.getState(threadId), originalTerminal); - assert.deepEqual( - await client.threads.getState(threadId, first.checkpoint), - first - ); - assert.equal( - fixture.receipt().children.length, - 1, - 'Fresh thread uses the same persistent worker' - ); - assert.equal(fixture.captures.length, 3); - assert.equal(fixture.captures[0].title, 'Authored debug metadata title'); - assert.equal(fixture.captures[1].titleUpdates.length, 1); - assert.equal(fixture.captures[0].proof.networkAttempts, 0); - assert.equal( - fixture.requests.length, - 17, - 'Three runs, explicit reads and original-terminal/old-checkpoint rechecks have no retries' - ); - assert.equal( - fixture.requests.filter((r) => r.path.endsWith('/runs/stream')).length, - 3 - ); - assert.deepEqual( - fixture.requests - .filter((r) => r.path.endsWith('/history')) - .map((r) => r.body), - [{ limit: 10 }, { limit: 10 }] - ); -}); diff --git a/scripts/react-cockpit/chat-debug-native-session-probe.mjs b/scripts/react-cockpit/chat-debug-native-session-probe.mjs index 01c0979ab..ca3bf3653 100644 --- a/scripts/react-cockpit/chat-debug-native-session-probe.mjs +++ b/scripts/react-cockpit/chat-debug-native-session-probe.mjs @@ -1,6 +1,6 @@ /** Reproducible private packed-consumer proof; build core before running. */ import assert from 'node:assert/strict'; -import { spawn } from 'node:child_process'; +import { execFileSync, spawn } from 'node:child_process'; import { createHash } from 'node:crypto'; import { constants, @@ -18,7 +18,6 @@ import { } from 'node:fs'; import { dirname, join } from 'node:path'; import { tmpdir } from 'node:os'; -import { setTimeout as delay } from 'node:timers/promises'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { emitCandidate, @@ -80,90 +79,7 @@ export function readNativeReceipt(consumer, path) { } } -export function readAcquisitionJournal(consumer, path, sourceHash) { - assert.equal( - path, - join(realpathSync(consumer), 'debug-native-acquisitions.jsonl') - ); - const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW); - try { - const stat = fstatSync(fd); - assert.ok(stat.isFile() && stat.size <= 4096, 'Acquisition journal bound'); - const buffer = Buffer.alloc(4097); - let length = 0, - n; - while ( - length < buffer.length && - (n = readSync(fd, buffer, length, buffer.length - length, null)) - ) - length += n; - assert.equal(length, stat.size); - assert.ok(length <= 4096); - const text = new TextDecoder('utf-8', { fatal: true }).decode( - buffer.subarray(0, length) - ); - if (!text) return []; - assert.ok(text.endsWith('\n')); - const rows = text - .slice(0, -1) - .split('\n') - .map((line) => { - const row = JSON.parse(line); - assert.equal(line, JSON.stringify(row)); - assert.equal(Object.keys(row).sort().join(), 'pgid,pid,sourceHash'); - assert.equal(row.sourceHash, sourceHash); - assert.ok( - Number.isSafeInteger(row.pid) && - row.pid > 1 && - row.pid < 2147483648 && - row.pgid === row.pid - ); - return row; - }); - assert.ok( - rows.length <= 4 && new Set(rows.map((r) => r.pid)).size === rows.length - ); - return rows; - } finally { - closeSync(fd); - } -} - -async function drainAcquiredWorkers(acquisitions) { - const receipts = []; - const absent = (id) => { - try { - process.kill(id, 0); - return false; - } catch (error) { - if (error.code === 'ESRCH') return true; - throw error; - } - }; - for (const row of acquisitions) { - const receipt = { ...row, signals: [], absent: false }; - receipts.push(receipt); - const gone = () => absent(row.pid) && absent(-row.pgid); - if (!gone()) { - for (const signal of ['SIGTERM', 'SIGKILL']) { - try { - process.kill(-row.pgid, signal); - receipt.signals.push(signal); - } catch (error) { - if (error.code !== 'ESRCH') throw error; - } - const deadline = Date.now() + (signal === 'SIGTERM' ? 250 : 2000); - while (!gone() && Date.now() < deadline) await delay(5); - if (gone()) break; - } - } - assert.ok(gone(), 'Owned detached worker PID/group must be absent'); - receipt.absent = true; - } - return receipts; -} - -export async function runNativeProbe({ failureLifetimeProof = false } = {}) { +export async function runNativeProbe() { const root = realpathSync(fileURLToPath(new URL('../../', import.meta.url))); const node = realpathSync(process.execPath); assert.equal( @@ -171,8 +87,10 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { '22', 'Run the locked Node22 toolchain' ); + // The consumer starts aimock + `langgraph dev` through `uv`. + const uv = execFileSync('which', ['uv'], { encoding: 'utf8' }).trim(); const environment = { - PATH: `${dirname(node)}:/usr/bin:/bin`, + PATH: `${dirname(node)}:${dirname(realpathSync(uv))}:/usr/bin:/bin`, LANG: 'en_US.UTF-8', HOME: process.env.HOME, COREPACK_ENABLE_AUTO_PIN: '0', @@ -203,16 +121,11 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { const report = { temporary, candidate, consumer, node, root, evidence }; async function run() { - const fixture = realpathSync( - join(root, 'scripts/react-cockpit/chat-debug-fixture.mjs') - ); const source = join( root, 'fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs' ); copyFileSync(source, join(consumer, 'chat-debug-native-session.mjs')); - const journal = join(consumer, 'debug-native-acquisitions.jsonl'); - writeFileSync(journal, '', { flag: 'wx', mode: 0o600 }); const child = spawn( node, ['--test', '--test-concurrency=1', 'chat-debug-native-session.mjs'], @@ -222,15 +135,11 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { stdio: ['ignore', 'pipe', 'pipe'], env: { ...environment, - DEBUG_FIXTURE_MODULE_URL: pathToFileURL(fixture).href, + DEBUG_REPO_ROOT_URL: pathToFileURL(root + '/').href, DEBUG_NATIVE_RECEIPT_PATH: join( consumer, 'debug-native-receipt.json' ), - DEBUG_NATIVE_ACQUISITIONS_PATH: journal, - ...(failureLifetimeProof - ? { DEBUG_NATIVE_PROBE_FAULT: 'exit-after-worker-acquired' } - : {}), }, } ); @@ -285,9 +194,10 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { terminate(Error('Native stderr bound')); else stderr += bytes; }); + // Includes the cold `langgraph dev` start. const timer = setTimeout( - () => terminate(Error('Native consumer 60s deadline')), - 60000 + () => terminate(Error('Native consumer 240s deadline')), + 240000 ); child.once('close', (code, signal) => { clearTimeout(timer); @@ -298,37 +208,9 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { }); writeFileSync(privatePrefix + '.stdout.log', stdout); writeFileSync(privatePrefix + '.stderr.log', stderr); - report.acquisitions = readAcquisitionJournal( - consumer, - journal, - sha(fixture) - ); - writeFileSync( - privatePrefix + '.acquisitions.jsonl', - readFileSync(journal), - { flag: 'wx', mode: 0o600 } - ); - report.knownWorkerCleanup = await drainAcquiredWorkers(report.acquisitions); if (childReceipt.pid) for (const pid of [childReceipt.pid, -childReceipt.pgid]) assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - if (failureLifetimeProof) { - assert.equal( - childReceipt.exitCode, - 1, - 'Node test runner reports the intentionally failed test worker' - ); - assert.match( - stdout, - /exitCode: 7/, - 'Actual native test worker exits after owned Python acquisition' - ); - assert.equal(report.acquisitions.length, 1); - assert.ok(report.knownWorkerCleanup[0].signals.length > 0); - report.failureLifetimeProven = true; - safelyClosed = true; - return; - } try { const receipt = readNativeReceipt( consumer, @@ -343,27 +225,18 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { } catch (error) { failure ??= error; } + // The consumer stopped the `langgraph dev` it started and saw no held + // proxy read or stream outlive the test. const cleanup = report.native?.cleanup; - const cleanups = [ - cleanup, - report.native?.unicodeCleanup, - report.native?.projectionCleanup, - ]; const complete = childReceipt.reaped && childReceipt.exitCode === 0 && - cleanups.every( - (c) => - c && - Object.values(c.active).every((n) => n === 0) && - c.children.every((child) => child.reaped) - ); - for (const cleanup of cleanups) - for (const child of cleanup?.children ?? []) { - if (child.pid) - for (const pid of [child.pid, -child.pgid]) - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - } + cleanup?.langgraph?.pid > 1 && + cleanup.langgraph.absent === true; + if (cleanup?.langgraph?.pid) + assert.throws(() => process.kill(cleanup.langgraph.pid, 0), { + code: 'ESRCH', + }); if (childReceipt.pid) for (const pid of [childReceipt.pid, -childReceipt.pgid]) assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); @@ -373,11 +246,6 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { complete, 'Native consumer must provide complete teardown receipt' ); - assert.deepEqual( - report.acquisitions.map((c) => c.pid).sort(), - cleanups.flatMap((c) => c.children.map((child) => child.pid)).sort(), - 'Journal and cleanup must name exactly the same acquired worker owners' - ); assert.equal(report.native.turns.length, 2); safelyClosed = true; } @@ -401,10 +269,7 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { ); await run(); console.log( - (failureLifetimeProof - ? 'Installed consumer failure lifetime proof' - : 'Installed native Debug gate: two submits, two loads, exact checkpoints and cleanup verified') + - '. Evidence: ' + + 'Installed native Debug gate: two submits, two loads, exact checkpoints and cleanup verified. Evidence: ' + evidence ); } finally { @@ -413,12 +278,10 @@ export async function runNativeProbe({ failureLifetimeProof = false } = {}) { report.sourceManifest = Object.fromEntries( [ 'cockpit/chat/debug/python/src/graph.py', - 'cockpit/chat/debug/python/tests/providers.py', - 'cockpit/chat/debug/python/tests/test_streaming.py', 'cockpit/chat/debug/python/uv.lock', - 'scripts/react-cockpit/chat-debug-wire.py', - 'scripts/react-cockpit/chat-debug-fixture.mjs', - 'scripts/react-cockpit/chat-debug-fixture.spec.mjs', + 'cockpit/chat/debug/angular/e2e/fixtures/c-debug.json', + 'scripts/react-cockpit/aimock-backend.mjs', + 'scripts/react-cockpit/langgraph-proxy.mjs', 'scripts/react-cockpit/chat-debug-native-session-probe.mjs', 'fixtures/react-parity/langgraph-candidate/chat-debug-native-session.mjs', ].map((path) => [path, sha(join(root, path))]) @@ -439,12 +302,6 @@ if ( process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url) ) { - assert.ok( - process.argv.slice(2).length === 0 || - process.argv.slice(2).join() === '--failure-lifetime-proof', - 'Unknown probe option' - ); - await runNativeProbe({ - failureLifetimeProof: process.argv[2] === '--failure-lifetime-proof', - }); + assert.equal(process.argv.length, 2, 'Unknown probe option'); + await runNativeProbe(); } diff --git a/scripts/react-cockpit/chat-debug-wire.py b/scripts/react-cockpit/chat-debug-wire.py deleted file mode 100644 index 0c5e96f97..000000000 --- a/scripts/react-cockpit/chat-debug-wire.py +++ /dev/null @@ -1,288 +0,0 @@ -"""Local JSONL adapter of the actual Debug graph, never a platform server. - -StateSnapshot.config.configurable is the SDK checkpoint object; parent_config, -created_at, next and tasks come from the real saver snapshot. Message model_dump -is the same public LangChain JSON shape consumed by the installed JS SDK. -The messages stream is replayed as SSE `messages` tuples (SDK messages-tuple). -No callback/title filtering or graph-authored authority is introduced here. -""" -import sys - -# Closed local failure fixtures run before importing the graph stack. They never -# inspect environment, open sockets, or execute caller-supplied code. -if len(sys.argv) == 2 and sys.argv[1].startswith("--fault="): - import signal - import time - fault = sys.argv[1].split("=", 1)[1] - if fault == "malformed": - sys.stdout.write("not-json\n") - sys.stdout.flush() - elif fault == "partial-oversize": - sys.stdout.write("x" * 65537) - sys.stdout.flush() - elif fault == "stderr-overflow": - sys.stderr.write("x" * 65537) - sys.stderr.flush() - elif fault == "ignore-term": - signal.signal(signal.SIGTERM, signal.SIG_IGN) - elif fault == "late-result": - signal.signal(signal.SIGTERM, signal.SIG_IGN) - sys.stdin.buffer.readline(65537) - time.sleep(0.2) - sys.stdout.write('{"requestId":1,"result":{"late":true}}\n') - sys.stdout.flush() - elif fault in {"delayed-close", "hung-close"}: - import json - if fault == "hung-close": - signal.signal(signal.SIGTERM, signal.SIG_IGN) - raw = sys.stdin.buffer.readline(65537) - if len(raw) > 65536 or not raw.endswith(b"\n"): - sys.exit(2) - request = json.loads(raw) - if (not isinstance(request, dict) - or set(request) != {"requestId", "command"} - or request["command"] != "close" - or type(request["requestId"]) is not int - or request["requestId"] < 1): - sys.exit(2) - sys.stdout.write(json.dumps({"requestId": request["requestId"], "result": {}}) + "\n") - sys.stdout.flush() - if fault == "delayed-close": - time.sleep(0.75) - sys.exit(0) - else: - sys.exit(2) - time.sleep(30) - sys.exit(0) - -import asyncio -from contextlib import ExitStack, redirect_stdout, redirect_stderr -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/debug/python" -SOURCE = PROJECT / "src/graph.py" -MAX_FRAME = 65536 -attempts = 0 - - -class BoundedSink: - """Discard content, retain bounded counts; never echo exception/secret text.""" - def __init__(self): - self.bytes = 0 - - def write(self, text): - self.bytes = min(65537, self.bytes + len(text.encode("utf-8"))) - if self.bytes > 65536: - raise ValueError("diagnostics bound") - return len(text) - - def flush(self): - pass - - -def reject_network(*args, **kwargs): - global attempts - attempts += 1 - raise AssertionError("network forbidden") - - -def serialize(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - if hasattr(value, "_asdict"): - return {k: serialize(v) for k, v in value._asdict().items()} - if isinstance(value, dict): - return {k: serialize(v) for k, v in value.items()} - if isinstance(value, (list, tuple)): - return [serialize(v) for v in value] - return value - - -def state_wire(state): - return { - "values": serialize(state.values), "next": list(state.next), - "checkpoint": state.config["configurable"], - "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, - "created_at": state.created_at, "metadata": serialize(state.metadata), - "tasks": serialize(state.tasks), - } - - -def validate(request): - if not isinstance(request, dict): - raise ValueError("schema") - command = request.get("command") - keys = { - "create": {"threadId"}, "send": {"threadId", "human"}, - "current": {"threadId"}, "history": {"threadId", "limit"}, - "checkpoint": {"threadId", "checkpoint"}, "reset": set(), "close": set(), - } - if command not in keys or set(request) != {"requestId", "command"} | keys[command]: - raise ValueError("schema") - if not isinstance(request["requestId"], int) or isinstance(request["requestId"], bool) or request["requestId"] < 1: - raise ValueError("request identity") - if "threadId" in request: - from uuid import UUID - if str(UUID(request["threadId"])) != request["threadId"]: - raise ValueError("thread identity") - if command == "send": - human = request["human"] - if not isinstance(human, dict) or set(human) != {"type", "id", "content"}: - raise ValueError("human") - if human["type"] != "human" or not isinstance(human["id"], str) or not 1 <= len(human["id"]) <= 128: - raise ValueError("human identity") - if not isinstance(human["content"], str) or not 1 <= len(human["content"]) <= 2000: - raise ValueError("human content") - if command == "history" and (type(request["limit"]) is not int or not 1 <= request["limit"] <= 10): - raise ValueError("history bound") - if command == "checkpoint": - cp = request["checkpoint"] - if not isinstance(cp, dict) or set(cp) - {"thread_id", "checkpoint_ns", "checkpoint_id", "checkpoint_map"}: - raise ValueError("checkpoint") - if cp.get("thread_id") != request["threadId"] or cp.get("checkpoint_ns") != "" or not isinstance(cp.get("checkpoint_id"), str): - raise ValueError("checkpoint identity") - - -async def main(): - threads = {} - count = 0 - seen = set() - with ExitStack() as stack: - for owner, name in ((socket.socket, "connect"), (socket.socket, "connect_ex"), - (socket, "getaddrinfo"), (socket, "gethostbyname"), - (socket, "gethostbyname_ex"), (socket, "gethostbyaddr"), - (socket, "getnameinfo")): - stack.enter_context(patch.object(owner, name, reject_network)) - sink = BoundedSink() - with redirect_stdout(sink), redirect_stderr(sink): - sys.path.insert(0, str(PROJECT / "tests")) - from providers import AuthoredClient, AuthoredProvider, AuthoredModel, Invocation - from langgraph.checkpoint.memory import InMemorySaver - from langchain_core.messages import HumanMessage - arguments = sys.argv[1:] - if (len(arguments) != len(set(arguments)) - or any(arg not in {"--variant=unicode", "--metadata-failure=read", "--metadata-failure=update"} - for arg in arguments) - or sum(arg.startswith("--metadata-failure=") for arg in arguments) > 1): - raise ValueError("invalid worker variant") - variant = "unicode" if "--variant=unicode" in arguments else "natural" - metadata_failure = next((arg.split("=", 1)[1] for arg in arguments - if arg.startswith("--metadata-failure=")), None) - - class UnicodeModel(AuthoredModel): - """Explicit provider seam: only generated A gains an astral prefix. - - BaseChatModel still owns callbacks/streaming and the actual graph - computes analysis from this generated content. Natural runs use - the accepted AuthoredProvider unchanged. - """ - def _begin(self, messages): - call = super()._begin(messages) - if call.node == "generate": - call = Invocation(call.node, "🛩️ " + call.content, call.id) - self._provider.calls[-1] = call - return call - - class UnicodeProvider(AuthoredProvider): - def model_factory(self, **kwargs): - model = UnicodeModel(title=not kwargs.get("streaming", False), tags=kwargs.get("tags", [])) - model._provider = self - return model - while True: - raw = sys.stdin.buffer.readline(MAX_FRAME + 1) - if not raw: - break - if len(raw) > MAX_FRAME or not raw.endswith(b"\n"): - raise ValueError("frame bound") - request_id = None - close = False - try: - request = json.loads(raw) - if isinstance(request, dict): - request_id = request.get("requestId") - validate(request) - request_id = request["requestId"] - if request_id in seen or count >= 64: - raise ValueError("command bound or duplicate") - seen.add(request_id) - count += 1 - command = request["command"] - with redirect_stdout(sink), redirect_stderr(sink): - if command == "create": - thread_id = request["threadId"] - if thread_id in threads or len(threads) >= 4: - raise ValueError("thread bound or duplicate") - provider = UnicodeProvider() if variant == "unicode" else AuthoredProvider() - client = AuthoredClient(fail_read=metadata_failure == "read", fail_update=metadata_failure == "update") - with patch("langchain_openai.ChatOpenAI", provider.model_factory), patch("langgraph_sdk.get_client", return_value=client): - spec = importlib.util.spec_from_file_location(f"local_debug_{len(threads)}", SOURCE) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.build_debug_graph().builder.compile(checkpointer=InMemorySaver()) - threads[thread_id] = (graph, provider, client) - result = {"thread_id": thread_id} - elif command in {"reset", "close"}: - threads.clear() - close = command == "close" - result = {} - else: - graph, provider, client = threads[request["threadId"]] - config = {"configurable": {"thread_id": request["threadId"]}} - with patch("langchain_openai.ChatOpenAI", provider.model_factory), patch("langgraph_sdk.get_client", return_value=client): - if command == "send": - human = request["human"] - if any(m.id == human["id"] for m in graph.get_state(config).values.get("messages", [])): - raise ValueError("duplicate human") - events = [] - async for kind, value in graph.astream({"messages": [HumanMessage(content=human["content"], id=human["id"])]}, config, - stream_mode=["messages", "values", "updates"]): - events.append([kind, serialize(value)]) - result = {"events": events, "state": state_wire(graph.get_state(config)), - "title": client.threads.title, "titleUpdates": serialize(client.threads.updates), - "proof": {"actualCompiledGraph": True, "networkAttempts": attempts, - "providerVariant": variant, - "metadataFailure": metadata_failure, - "sourceSha256": hashlib.sha256(SOURCE.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest()}} - elif command == "history": - result = [state_wire(s) for s in graph.get_state_history(config, limit=request["limit"])] - else: - if command == "checkpoint": - config = {"configurable": request["checkpoint"]} - result = state_wire(graph.get_state(config)) - if command == "checkpoint" and result["checkpoint"] != request["checkpoint"]: - raise ValueError("exact checkpoint mismatch") - if attempts: - raise AssertionError("network attempt swallowed") - response = {"requestId": request_id, "result": result} - except Exception: - response = {"requestId": request_id, "error": "Local graph request rejected"} - finally: - if attempts: - raise AssertionError("network attempt swallowed") - encoded = json.dumps(response, ensure_ascii=False).encode("utf-8") + b"\n" - if len(encoded) > MAX_FRAME: - raise ValueError("response frame bound") - sys.stdout.buffer.write(encoded) - sys.stdout.buffer.flush() - if close: - break - if sink.bytes: - sys.stderr.write("Local graph diagnostics suppressed\n") - - -if __name__ == "__main__": - try: - asyncio.run(main()) - except BaseException: - sys.stderr.write("Local graph worker terminated safely\n") - sys.exit(1) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 15959ab7b..b58e065c0 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -31,6 +31,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { topic: 'debug', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/debug/react', base: '/chat/debug/react/', port: 4645, project: 'cockpit-chat-debug-react', + backend: aimockLanggraph('chat/debug', 4645), }); if (topic === 'chat-theming') return Object.freeze({ diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 17741ffc4..2fd496d04 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -15,7 +15,8 @@ * GET /__lifetime { activeStream, activeHistory, activeTitle, * activeState, openResponses } * GET /__captures per finished run: forwarded SSE text, saved - * messages/next from the real server, title + * messages/values/checkpoint/next from the real + * server, title * before and after the run (waits for captures * still being read) * POST /__reset forget requests, drop held responses, clear modes @@ -91,6 +92,8 @@ * "$humanId" in data is the submitted message id). * `once: true` applies an op to its first match only; * `ifKey` only to events whose state has that key. + * `dataPattern` only to events whose raw data + * line matches that regex. * `unlessKey` skips events whose state has that key. * `anonymous: true` withholds the run identity (the * metadata event and Content-Location header). @@ -575,6 +578,7 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { let changed = false; for (const op of edits) { if ((op.once && op.used) || !op.pattern.test(name)) continue; + if (op.dataPattern && !new RegExp(op.dataPattern).test(dataLine ?? '')) continue; // A `checkpoints` event carries state under `values`; ops address the state. const state = name.startsWith('checkpoints') ? data?.values : data; if (op.ifKey && !(state && typeof state === 'object' && op.ifKey in state)) continue; @@ -658,6 +662,8 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { path: request.url && new URL(request.url, 'http://localhost').pathname, sse, savedMessages: state?.values?.messages ?? [], + values: state?.values ?? null, + checkpoint: state?.checkpoint ?? null, next: state?.next ?? [], titleBefore, titleAfter: await threadTitle(threadId).catch(() => null), diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 3998c8d77..865f7774f 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -12,7 +12,6 @@ import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; -import { createChatDebugFixture } from './chat-debug-fixture.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; import { createAgUiStreamingFixture } from './ag-ui-streaming-fixture.mjs'; import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; @@ -46,7 +45,6 @@ const releaseA2uiStreams = (cancel = false) => { } } }; -const chatDebugFixture = configuration.library === 'chat' && configuration.topic === 'debug' ? createChatDebugFixture() : null; const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; @@ -142,29 +140,6 @@ const server = createServer(async (request, response) => { if (await chatA2uiFixture(request, response, pathname)) return; if (pathname.startsWith('/developer-api/')) return json(response, {}, 404); } - if (chatDebugFixture) { - // Closed local controls, excluded from application inputs and bundles. - const controls = { - '/__requests': ['GET', () => chatDebugFixture.requests], - '/__captures': ['GET', () => chatDebugFixture.captures], - '/__lifetime': ['GET', () => chatDebugFixture.receipt()], - '/__reset': ['POST', async () => { await chatDebugFixture.reset(); return chatDebugFixture.receipt(); }], - '/__release': ['POST', () => { chatDebugFixture.release(); return {}; }], - '/__hold/stream': ['POST', () => { chatDebugFixture.holdStreams(true); return {}; }], - '/__hold/current': ['POST', () => { chatDebugFixture.holdReads('current'); return {}; }], - '/__hold/history': ['POST', () => { chatDebugFixture.holdReads('history'); return {}; }], - '/__hold/checkpoint': ['POST', () => { chatDebugFixture.holdReads('checkpoint'); return {}; }], - }; - if (pathname.startsWith('/__')) { - const control = Object.hasOwn(controls, pathname) ? controls[pathname] : undefined; - if (!control) return json(response, {}, 404); - if (request.method !== control[0]) return json(response, {}, 405); - try { return json(response, await control[1]()); } - catch { return json(response, { error: 'Local fixture control failed' }, 500); } - } - if (await chatDebugFixture.handle(request, response, pathname)) return; - if (pathname.startsWith('/developer-api/')) return json(response, {}, 404); - } if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; @@ -271,7 +246,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -284,17 +259,16 @@ if (agUiA2uiFixture || isChatA2ui || chatDebugFixture || nativeStrandsFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (chatDebugFixture ?? nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? chatGenerativeUiFixture).close(); + else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); }))); - if (chatDebugFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatDebugFixtureClosed: chatDebugFixture.receipt(), listenersClosed: true })); if (chatA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatA2uiFixtureClosed: a2uiReceipt(), listenersClosed: true })); if (agUiA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), agUiA2uiFixtureClosed: agUiA2uiFixture.receipt(), bufferedCanonicalWire: true, listenersClosed: true })); if (a2uiFailure) throw a2uiFailure; })(); - if (agUiA2uiFixture || isChatA2ui || chatDebugFixture) void closing.then(() => process.exit(code), () => process.exit(1)); + if (agUiA2uiFixture || isChatA2ui) void closing.then(() => process.exit(code), () => process.exit(1)); else void closing.finally(() => process.exit(code)); }; process.once('SIGINT', () => shutdown()); From c885adc9f7a542ecc1c56b47dbb6b7dc8467802d Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:15:04 -0800 Subject: [PATCH 24/41] test(ci): repin the cockpit e2e job steps after the debug proof step change The debug proof step no longer runs the deleted fixture spec. Co-Authored-By: Claude Opus 5.5 --- scripts/ci-workflow.spec.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index edf8bc29a..19a23f1ba 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - '7b4549c58f77e6432e7788891dc793c976c5072b54f669d323503baaf517bde2', + '72009a0617afde5507d49260dd17f29f4b8b80f504eeeb91b954a86d053aec6a', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); From c98004389c50071e8a2a1158e642d78691824ce1 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 19:44:07 -0800 Subject: [PATCH 25/41] test(cockpit): run React ag-ui/streaming e2e and website preview on aimock + the real AG-UI server Adds the AG-UI flavor of the backend and proxy: createAimockBackend runs the example's uvicorn src/server.py for `aimock-ag-ui` backends, the e2e harness gains a backend-only AG-UI global setup, and scripts/react-cockpit/ag-ui-proxy.mjs records agent POSTs and holds, fails or edits the next run (protocol fault shapes are injected; a model failure is real and drops the stream). Co-Authored-By: Claude Opus 5.5 --- .../e2e/react-ag-ui-streaming-preview.spec.ts | 4 +- apps/website/playwright.config.ts | 4 +- apps/website/src/playwright-config.spec.ts | 4 +- .../angular/e2e/fixtures/streaming.json | 35 +++ .../streaming/react/e2e/global-setup-impl.ts | 6 + .../streaming/react/e2e/playwright.config.ts | 3 + .../streaming/react/e2e/streaming.spec.ts | 65 +++- cockpit/ag-ui/streaming/react/project.json | 17 +- cockpit/shared/react-aimock-setup.ts | 25 +- .../src/ag-ui-global-setup-factory.ts | 128 +++++--- libs/e2e-harness/src/index.ts | 7 +- nx.json | 1 + scripts/react-cockpit/ag-ui-proxy.mjs | 285 ++++++++++++++++++ .../react-cockpit/ag-ui-streaming-fixture.mjs | 157 ---------- scripts/react-cockpit/aimock-backend.mjs | 19 +- scripts/react-cockpit/configuration.mjs | 12 + scripts/react-cockpit/serve.mjs | 28 +- 17 files changed, 571 insertions(+), 229 deletions(-) create mode 100644 cockpit/ag-ui/streaming/react/e2e/global-setup-impl.ts create mode 100644 scripts/react-cockpit/ag-ui-proxy.mjs delete mode 100644 scripts/react-cockpit/ag-ui-streaming-fixture.mjs diff --git a/apps/website/e2e/react-ag-ui-streaming-preview.spec.ts b/apps/website/e2e/react-ag-ui-streaming-preview.spec.ts index 38e3ca4e5..46d3a1f1c 100644 --- a/apps/website/e2e/react-ag-ui-streaming-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-streaming-preview.spec.ts @@ -78,6 +78,8 @@ test('switching away releases the held AG-UI Streaming stream and returning stay ); const origin = 'http://127.0.0.1:4609'; await request.post(origin + '/__reset'); + // Hold the real stream after its first answer chunk (scripts/react-cockpit/ag-ui-proxy.mjs). + await request.post(origin + '/__hold/stream'); await page.goto( '/docs/react/ag-ui/reference/event-mapping?mode=run' ); @@ -89,7 +91,7 @@ test('switching away releases the held AG-UI Streaming stream and returning stay await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect( frame.getByRole('region', { name: 'Conversation', exact: true }) - ).toContainText('Live partial'); + ).toContainText('Reply to'); const oldFrame = await page.locator('iframe').elementHandle(); await selectFramework(page, 'angular'); await expect.poll(() => oldFrame?.evaluate((element) => element.isConnected)).toBe(false); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index d124c332b..cc2bdf848 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -335,9 +335,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming --no-parent', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4609', + url: 'http://127.0.0.1:4609/__ready', reuseExistingServer, timeout: 180_000, }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 47a339304..2bf892fce 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -495,8 +495,8 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4608/__ready', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming --no-parent', - url: 'http://127.0.0.1:4609', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming --no-parent --backend', + url: 'http://127.0.0.1:4609/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent', diff --git a/cockpit/ag-ui/streaming/angular/e2e/fixtures/streaming.json b/cockpit/ag-ui/streaming/angular/e2e/fixtures/streaming.json index d54869ff9..c83afe81d 100644 --- a/cockpit/ag-ui/streaming/angular/e2e/fixtures/streaming.json +++ b/cockpit/ag-ui/streaming/angular/e2e/fixtures/streaming.json @@ -7,6 +7,41 @@ "response": { "content": "Angular signals are a reactive primitive (signal, computed, effect) that track dependencies to provide fine-grained reactivity and more efficient change detection. They let you update state synchronously via set()/update() and ensure only consumers that read an affected signal are re\u2011evaluated." } + }, + { + "match": { "userMessage": "Alpha" }, + "response": { "content": "Reply to Alpha" } + }, + { + "match": { "userMessage": "Beta" }, + "response": { "content": "Reply to Beta" } + }, + { + "match": { "userMessage": "Gamma" }, + "response": { "content": "Reply to Gamma" } + }, + { + "match": { "userMessage": "Recovered" }, + "response": { "content": "Reply to Recovered" } + }, + { + "match": { "userMessage": "Hold" }, + "response": { "content": "Reply to Hold, settled." }, + "chunkSize": 9 + }, + { + "match": { "userMessage": "Model failure" }, + "response": { + "error": { + "message": "PRIVATE model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Reply recorded." } } ] } diff --git a/cockpit/ag-ui/streaming/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/streaming/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..9466c1959 --- /dev/null +++ b/cockpit/ag-ui/streaming/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/streaming', port: 4609 }); diff --git a/cockpit/ag-ui/streaming/react/e2e/playwright.config.ts b/cockpit/ag-ui/streaming/react/e2e/playwright.config.ts index d31d2119d..c7d5c8a4e 100644 --- a/cockpit/ag-ui/streaming/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/streaming/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4609', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/streaming/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs ag-ui-streaming', cwd: '../../../../..', diff --git a/cockpit/ag-ui/streaming/react/e2e/streaming.spec.ts b/cockpit/ag-ui/streaming/react/e2e/streaming.spec.ts index 84742003a..35aadd7f0 100644 --- a/cockpit/ag-ui/streaming/react/e2e/streaming.spec.ts +++ b/cockpit/ag-ui/streaming/react/e2e/streaming.spec.ts @@ -1,7 +1,49 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/streaming/angular/e2e/fixtures into +// the example's real AG-UI server (ag-ui-langgraph). The recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs) holds, fails or edits the next run. test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); +/** + * Arms the protocol fault a scenario needs on the next run. All but + * `HTTP error` and `Run error` are injected data: stream shapes a correct + * server won't produce, for the client's fault handling. + */ +async function fault(request: APIRequestContext, text: string) { + const edit = (ops: object[]) => request.post('/__stream/edit', { data: { ops } }); + const finished = (set: object) => edit([{ type: '^RUN_FINISHED$', op: 'patch', set }]); + if (text === 'HTTP error') await request.post('/__fail-stream'); + // `Model failure` is real: its fixture fails the model call, and the server + // drops the stream with no terminal event. + if (text === 'Run error') + await edit([ + { type: '^TEXT_MESSAGE_START$', op: 'insertBefore', event: { type: 'RUN_ERROR', message: 'PRIVATE model failure', code: 'TEST' } }, + { type: '^TEXT_MESSAGE_START$', op: 'end' }, + ]); + if (text === 'Interrupt') + await finished({ outcome: { type: 'interrupt', interrupts: [{ id: 'pause', reason: 'approval' }] } }); + if (text === 'Cancelled') await finished({ outcome: { type: 'cancelled' } }); + if (text === 'Pending tools') + await finished({ outcome: { type: 'success', pendingToolCallIds: ['call'] } }); + if (text === 'Wrong terminal') await finished({ runId: 'different-run' }); + if (text === 'Notice EOF') + await edit([ + { type: '^MESSAGES_SNAPSHOT$', op: 'insertBefore', event: { type: 'CUSTOM', name: 'on_interrupt', value: { note: 'notice' } } }, + { type: '^MESSAGES_SNAPSHOT$', op: 'end' }, + ]); + if (text === 'Child') + await edit([ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'SUBAGENT_STARTED', subagentRunId: 'child', name: 'worker' } }, + ]); + const snapshot = { + Tool: 'tool-call', + 'Duplicate identity': 'duplicate-identity', + 'Truncated prefix': 'truncated-prefix', + 'Old pair': 'old-pair', + }[text]; + if (snapshot) await edit([{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: snapshot }]); +} const conversation = (page: Page) => page.getByRole('region', { name: 'Conversation', exact: true }); async function send(page: Page, text: string) { @@ -36,13 +78,17 @@ test('installed AG-UI client sends full native history on one lazy UUID and New await expect(conversation(page).locator('li')).toHaveCount(4); const before: Wire[] = await (await request.get('/__requests')).json(); expect(before).toHaveLength(2); + // The client returns the server's last STATE_SNAPSHOT as the next run's state. + const [firstRun] = await (await request.get('/__captures')).json(); + const snapshot = firstRun.events.filter((e: { type: string }) => e.type === 'STATE_SNAPSHOT').at(-1).snapshot; + expect(before[0].body.state).toEqual({}); + expect(before[1].body.state).toEqual(snapshot); for (const entry of before) { expect(entry.path).toBe('/ag-ui/streaming/agent'); expect(entry.method).toBe('POST'); expect(entry.body.protocolVersion).toBe('1.0'); expect(entry.body.tools).toEqual([]); expect(entry.body.context).toEqual([]); - expect(entry.body.state).toEqual({}); expect(entry.body.forwardedProps).toEqual({}); } expect(before[0].body.messages.map((x) => [x.role, x.content])).toEqual([ @@ -73,21 +119,24 @@ test('native incremental partial settles to one canonical final row', async ({ request, }) => { await page.goto('/ag-ui/streaming/react/'); + // The answer streams in 9-character chunks; hold after the first one. + await request.post('/__hold/stream'); await send(page, 'Hold'); - await expect(conversation(page)).toContainText('Live partial'); + await expect(conversation(page)).toContainText('Reply to'); + await expect(conversation(page)).not.toContainText('settled'); await expect( page.getByRole('button', { name: 'New conversation' }) ).toBeDisabled(); await request.post('/__release'); await expect(page.getByRole('status')).toHaveText('Response complete.'); await expect(conversation(page).locator('li')).toHaveCount(2); - await expect(conversation(page)).toContainText('Reply to Hold'); - await expect(conversation(page)).not.toContainText('Live partial'); + await expect(conversation(page)).toContainText('Reply to Hold, settled.'); }); for (const text of [ 'HTTP error', 'Run error', + 'Model failure', 'Interrupt', 'Cancelled', 'Notice EOF', @@ -108,6 +157,7 @@ for (const text of [ await send(page, 'Alpha'); await expect(page.getByRole('status')).toHaveText('Response complete.'); } + await fault(request, text); await send(page, text); await expect(page.getByRole('status')).toHaveText( 'Start a new conversation to continue.' @@ -134,8 +184,9 @@ for (const exit of ['Stop', 'pagehide']) { request, }) => { await page.goto('/ag-ui/streaming/react/'); + await request.post('/__hold/stream'); await send(page, 'Hold'); - await expect(conversation(page)).toContainText('Live partial'); + await expect(conversation(page)).toContainText('Reply to'); expect((await (await request.get('/__lifetime')).json()).active).toBe(true); if (exit === 'Stop') { await page.getByRole('button', { name: 'Stop', exact: true }).click(); @@ -156,7 +207,7 @@ for (const exit of ['Stop', 'pagehide']) { await request.post('/__release'); expect(await (await request.get('/__requests')).json()).toHaveLength(1); if (exit === 'Stop') - await expect(conversation(page)).not.toContainText('Reply to Hold'); + await expect(conversation(page)).not.toContainText('settled'); else await expect(conversation(page)).toHaveCount(0); }); } diff --git a/cockpit/ag-ui/streaming/react/project.json b/cockpit/ag-ui/streaming/react/project.json index 2c5ec25b9..40a78e62a 100644 --- a/cockpit/ag-ui/streaming/react/project.json +++ b/cockpit/ag-ui/streaming/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/streaming/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-ag-ui-streaming-angular", + "cockpit-ag-ui-streaming-python" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -27,7 +31,12 @@ "react" ] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/ag-ui/streaming/react" @@ -48,7 +57,11 @@ "options": { "config": "cockpit/ag-ui/streaming/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/cockpit/shared/react-aimock-setup.ts b/cockpit/shared/react-aimock-setup.ts index 62cccb238..b98315b11 100644 --- a/cockpit/shared/react-aimock-setup.ts +++ b/cockpit/shared/react-aimock-setup.ts @@ -1,5 +1,8 @@ import { resolve } from 'node:path'; -import { createLanggraphGlobalSetup } from '@threadplane-internal/e2e-harness'; +import { + createAgUiBackendGlobalSetup, + createLanggraphGlobalSetup, +} from '@threadplane-internal/e2e-harness'; /** * Playwright global setup for an aimock-backed React cockpit example: aimock @@ -25,3 +28,23 @@ export function reactAimockGlobalSetup(options: { fixturesDir: resolve(__dirname, '..', options.example, 'angular/e2e/fixtures'), }); } + +/** + * The AG-UI flavor: aimock replays the Angular example's fixtures into the + * example's uvicorn AG-UI server (`src/server.py`); the static server proxies + * the agent endpoint to it through scripts/react-cockpit/ag-ui-proxy.mjs. + * Mirrors `aimockAgUi()` in scripts/react-cockpit/configuration.mjs. + */ +export function reactAimockAgUiGlobalSetup(options: { + /** e.g. 'ag-ui/streaming' */ + example: string; + /** The React app port from configuration.mjs. */ + port: number; +}): () => Promise { + return createAgUiBackendGlobalSetup({ + stateKey: `cockpit-${options.example.replace('/', '-')}-react`, + pythonCwd: `cockpit/${options.example}/python`, + backendPort: options.port + 1000, + fixturesDir: resolve(__dirname, '..', options.example, 'angular/e2e/fixtures'), + }); +} diff --git a/libs/e2e-harness/src/ag-ui-global-setup-factory.ts b/libs/e2e-harness/src/ag-ui-global-setup-factory.ts index d6c1b08bf..38db03b8b 100644 --- a/libs/e2e-harness/src/ag-ui-global-setup-factory.ts +++ b/libs/e2e-harness/src/ag-ui-global-setup-factory.ts @@ -70,41 +70,100 @@ function repoRoot(opts: CreateAgUiGlobalSetupOpts): string { throw new Error('repo root not found from fixturesDir; passed: ' + opts.fixturesDir); } +interface AgUiBackend { + aimock: AimockHandle; + backend: ChildProcess; + backendPort: number; +} + +/** aimock + the example's uvicorn AG-UI server (src/server.py), ready on /ok. */ +async function startAgUiBackend(opts: { + root: string; + pythonCwd: string; + backendPort: number; + fixturesDir: string; + backendReadyTimeoutMs?: number; +}): Promise { + const launch = resolveAimockLaunch(opts.fixturesDir); + const aimock = await startAimock(launch.startOptions); + // eslint-disable-next-line no-console + console.log(`[ag-ui-harness] aimock listening at ${aimock.baseUrl}`); + + const backend = spawn( + 'uv', + ['run', 'uvicorn', 'src.server:app', '--port', String(opts.backendPort)], + { + cwd: resolve(opts.root, opts.pythonCwd), + env: { + ...process.env, + OPENAI_BASE_URL: aimock.baseUrl, + OPENAI_API_KEY: launch.openaiApiKey, + }, + stdio: 'pipe', + // detached:true puts the process in its own group so teardown can + // kill the whole tree (uv → python → uvicorn) via process.kill(-pid). + // Without this, SIGTERM to `uv` doesn't propagate to the Python child + // and the uvicorn server keeps holding the port through teardown. + detached: true, + }, + ); + backend.stdout?.on('data', (b) => process.stdout.write(`[ag-ui-backend] ${b}`)); + backend.stderr?.on('data', (b) => process.stderr.write(`[ag-ui-backend] ${b}`)); + + await waitForPort( + `http://localhost:${opts.backendPort}/ok`, + opts.backendReadyTimeoutMs ?? 90_000, + 'ag-ui-backend', + ); + // eslint-disable-next-line no-console + console.log(`[ag-ui-harness] backend ready on :${opts.backendPort}`); + return { aimock, backend, backendPort: opts.backendPort }; +} + +function registerState(key: string, state: SharedState): void { + if (!globalThis.__AIMOCK_HARNESS_STATE__) { + globalThis.__AIMOCK_HARNESS_STATE__ = new Map(); + } + globalThis.__AIMOCK_HARNESS_STATE__.set(key, state); +} + +export interface CreateAgUiBackendGlobalSetupOpts { + /** Teardown state key, e.g. the React project name. */ + stateKey: string; + pythonCwd: string; + backendPort: number; + fixturesDir: string; + backendReadyTimeoutMs?: number; +} + +/** Backend only: the React cockpit serves its own build and proxies to it. */ +export function createAgUiBackendGlobalSetup( + opts: CreateAgUiBackendGlobalSetupOpts, +): () => Promise { + return async function globalSetup(): Promise { + const backend = await startAgUiBackend({ + root: repoRoot({ fixturesDir: opts.fixturesDir } as CreateAgUiGlobalSetupOpts), + pythonCwd: opts.pythonCwd, + backendPort: opts.backendPort, + fixturesDir: opts.fixturesDir, + backendReadyTimeoutMs: opts.backendReadyTimeoutMs, + }); + registerState(opts.stateKey, backend); + }; +} + export function createAgUiGlobalSetup(opts: CreateAgUiGlobalSetupOpts): () => Promise { - const backendTimeout = opts.backendReadyTimeoutMs ?? 90_000; const angularTimeout = opts.angularReadyTimeoutMs ?? 120_000; return async function globalSetup(): Promise { const root = repoRoot(opts); - const launch = resolveAimockLaunch(opts.fixturesDir); - const aimock = await startAimock(launch.startOptions); - // eslint-disable-next-line no-console - console.log(`[ag-ui-harness] aimock listening at ${aimock.baseUrl}`); - - const backend = spawn( - 'uv', - ['run', 'uvicorn', 'src.server:app', '--port', String(opts.backendPort)], - { - cwd: resolve(root, opts.pythonCwd), - env: { - ...process.env, - OPENAI_BASE_URL: aimock.baseUrl, - OPENAI_API_KEY: launch.openaiApiKey, - }, - stdio: 'pipe', - // detached:true puts the process in its own group so teardown can - // kill the whole tree (uv → python → uvicorn) via process.kill(-pid). - // Without this, SIGTERM to `uv` doesn't propagate to the Python child - // and the uvicorn server keeps holding the port through teardown. - detached: true, - }, - ); - backend.stdout?.on('data', (b) => process.stdout.write(`[ag-ui-backend] ${b}`)); - backend.stderr?.on('data', (b) => process.stderr.write(`[ag-ui-backend] ${b}`)); - - await waitForPort(`http://localhost:${opts.backendPort}/ok`, backendTimeout, 'ag-ui-backend'); - // eslint-disable-next-line no-console - console.log(`[ag-ui-harness] backend ready on :${opts.backendPort}`); + const backend = await startAgUiBackend({ + root, + pythonCwd: opts.pythonCwd, + backendPort: opts.backendPort, + fixturesDir: opts.fixturesDir, + backendReadyTimeoutMs: opts.backendReadyTimeoutMs, + }); const angular = spawn( 'npx', @@ -125,13 +184,8 @@ export function createAgUiGlobalSetup(opts: CreateAgUiGlobalSetupOpts): () => Pr // eslint-disable-next-line no-console console.log(`[ag-ui-harness] angular ready on :${opts.angularPort} (${opts.angularProject})`); - if (!globalThis.__AIMOCK_HARNESS_STATE__) { - globalThis.__AIMOCK_HARNESS_STATE__ = new Map(); - } - globalThis.__AIMOCK_HARNESS_STATE__.set(opts.angularProject, { - aimock, - backend, - backendPort: opts.backendPort, + registerState(opts.angularProject, { + ...backend, angular, angularPort: opts.angularPort, }); diff --git a/libs/e2e-harness/src/index.ts b/libs/e2e-harness/src/index.ts index 9b360f124..266392397 100644 --- a/libs/e2e-harness/src/index.ts +++ b/libs/e2e-harness/src/index.ts @@ -13,4 +13,9 @@ export { type CreateLanggraphGlobalSetupOpts, type LanggraphBackend, } from './global-setup-factory'; -export { createAgUiGlobalSetup, type CreateAgUiGlobalSetupOpts } from './ag-ui-global-setup-factory'; +export { + createAgUiGlobalSetup, + createAgUiBackendGlobalSetup, + type CreateAgUiGlobalSetupOpts, + type CreateAgUiBackendGlobalSetupOpts, +} from './ag-ui-global-setup-factory'; diff --git a/nx.json b/nx.json index 0a19da480..8a65d2fdf 100644 --- a/nx.json +++ b/nx.json @@ -19,6 +19,7 @@ "reactAimockBackend": [ "{workspaceRoot}/scripts/react-cockpit/langgraph-proxy.mjs", "{workspaceRoot}/scripts/react-cockpit/aimock-backend.mjs", + "{workspaceRoot}/scripts/react-cockpit/ag-ui-proxy.mjs", "{workspaceRoot}/scripts/react-cockpit/serve.mjs", "{workspaceRoot}/scripts/react-cockpit/configuration.mjs", "{workspaceRoot}/cockpit/shared/react-aimock-setup.ts" diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs new file mode 100644 index 000000000..43acd19e0 --- /dev/null +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -0,0 +1,285 @@ +/** + * Local e2e only: a recording proxy in front of an AG-UI example's real + * server (uvicorn `src/server.py`, ag-ui-langgraph, on aimock fixtures shared + * with the Angular example). The AG-UI flavor of langgraph-proxy.mjs. + * + * The graph, the AG-UI HTTP/SSE wire and the model replies are real. This + * layer only adds what a browser contract test needs and a live server cannot + * produce on demand: + * + * GET /__requests every agent POST, in order: { path, method, body } + * GET /__captures per finished run: { path, sse, events } as the + * real server sent them (before any edit) + * GET /__lifetime { active (a held stream), openResponses } + * GET|POST /__ready 200 once the server answers /ok (with + * `serve.mjs --backend`, after its lazy start) + * POST /__reset forget requests and captures, drop one-shot + * controls, end a held stream + * POST /__hold/stream hold the next run stream after the first event + * whose type matches `afterType` (default + * TEXT_MESSAGE_CONTENT) or whose raw JSON matches + * `afterPattern`; buffers the rest + * POST /__release flush and resume a held stream + * POST /__fail-stream next agent POST answers 500 unforwarded (the + * server is unavailable) + * POST /__stream/edit body { ops }: edits to the next run stream, each + * { type: regex on the event type, op, once? }: + * drop remove the event + * patch { set } merge fields into it + * insertBefore|insertAfter { event } + * end end the stream just + * before the event + * snapshot { mode } rewrite a + * MESSAGES_SNAPSHOT: + * duplicate-identity, + * truncated-prefix, + * old-pair, tool-call + * "$threadId", "$runId", "$humanId" and + * "$answerId" in values are the run's own ids. + * Injected data: protocol shapes a correct server + * won't produce, for the client's fault handling. + * + * Nothing here is reachable from a production build: only + * scripts/react-cockpit/serve.mjs and local tests import it. + */ +import { request as httpRequest } from 'node:http'; + +const json = (response, value, status = 200) => { + response.writeHead(status, { 'content-type': 'application/json' }); + response.end(JSON.stringify(value)); +}; +const readAll = async (stream) => { + const chunks = []; + for await (const chunk of stream) chunks.push(chunk); + return Buffer.concat(chunks); +}; + +/** The four snapshot shapes the authored AG-UI fixtures produced. */ +function rewriteSnapshot(mode, event, body) { + const sent = body.messages ?? []; + const human = sent.at(-1); + const messages = [...event.messages]; + const answer = messages.at(-1); + if (mode === 'duplicate-identity') messages[messages.length - 1] = { ...answer, id: human.id }; + else if (mode === 'truncated-prefix') return { ...event, messages: [human, answer] }; + else if (mode === 'old-pair') return { ...event, messages: sent.slice(0, -1) }; + else if (mode === 'tool-call') + messages[messages.length - 1] = { + ...answer, + toolCalls: [{ id: 'call', type: 'function', function: { name: 'weather', arguments: '{}' } }], + }; + else throw new Error('Unsupported snapshot mode'); + return { ...event, messages }; +} + +export function createAgUiProxy({ upstream, agentPaths, ready = async () => {} }) { + let requests = []; + let captures = []; + const pendingCaptures = new Set(); + let holdNext = null; + let held; + let failNext = false; + let nextEdits = null; + let openResponses = 0; + + function reset() { + requests = []; + captures = []; + holdNext = null; + failNext = false; + nextEdits = null; + held?.end(); + held = undefined; + } + + async function proxyRun(request, response, pathname, raw) { + const body = raw.length ? JSON.parse(raw.toString('utf8')) : {}; + requests.push({ path: pathname, method: request.method, body }); + if (failNext) { + failNext = false; + json(response, { error: 'PRIVATE AG-UI server failure' }, 500); + return; + } + const hold = holdNext; + holdNext = null; + const edits = (nextEdits ?? []).map((op) => ({ ...op, pattern: new RegExp(op.type ?? '.*'), used: false })); + nextEdits = null; + const ids = { threadId: body.threadId, runId: body.runId, humanId: body.messages?.at(-1)?.id, answerId: null }; + const fill = (value) => + JSON.parse( + JSON.stringify(value).replace(/"\$(threadId|runId|humanId|answerId)"/g, (_, key) => JSON.stringify(ids[key])) + ); + const upstreamResponse = await new Promise((resolve, reject) => { + const outgoing = httpRequest( + new URL('/agent', upstream), + { method: 'POST', headers: { 'content-type': 'application/json', accept: 'text/event-stream' } }, + resolve + ); + outgoing.once('error', reject); + outgoing.end(raw); + }); + response.writeHead(upstreamResponse.statusCode, { + 'content-type': upstreamResponse.headers['content-type'] ?? 'text/event-stream', + 'cache-control': 'no-cache', + }); + response.flushHeaders(); + response.on('close', () => upstreamResponse.destroy()); + let settle; + const settled = new Promise((done) => { settle = done; }); + pendingCaptures.add(settled); + void settled.then(() => pendingCaptures.delete(settled)); + response.once('close', () => settle()); + let sse = ''; + let pending = ''; + let holding = false; + let armed = Boolean(hold); + let ended = false; + const buffered = []; + const events = []; + const write = (event) => { + if (!response.writableEnded) response.write(`data: ${JSON.stringify(event)}\n\n`); + }; + /** Apply edits to one real event; returns false once the stream must end. */ + const forward = (event) => { + if (event.type === 'TEXT_MESSAGE_START' && !ids.answerId) ids.answerId = event.messageId; + const after = []; + let current = event; + for (const op of edits) { + if ((op.once && op.used) || !op.pattern.test(event.type)) continue; + op.used = true; + if (op.op === 'drop') return true; + if (op.op === 'end') { + response.end(); + return false; + } + if (op.op === 'patch') current = { ...current, ...fill(op.set) }; + if (op.op === 'snapshot') current = rewriteSnapshot(op.mode, current, body); + if (op.op === 'insertBefore') write(fill(op.event)); + if (op.op === 'insertAfter') after.push(fill(op.event)); + } + write(current); + for (const extra of after) write(extra); + return true; + }; + let finished = false; + const finish = () => { + if (finished) return; + finished = true; + if (!response.writableEnded) response.end(); + captures.push({ path: pathname, sse, events }); + settle(); + }; + const held_ = { + release() { + if (held === held_) held = undefined; + holding = false; + for (const event of buffered.splice(0)) if (!forward(event)) return; + if (ended) finish(); + }, + end() { + if (!response.writableEnded) response.end(); + }, + }; + upstreamResponse.setEncoding('utf8'); + upstreamResponse.on('data', (chunk) => { + sse += chunk; + pending += chunk; + let match; + while ((match = /\r?\n\r?\n/.exec(pending))) { + const block = pending.slice(0, match.index); + pending = pending.slice(match.index + match[0].length); + const line = block.split(/\r?\n/).find((l) => l.startsWith('data:')); + if (!line) continue; + const event = JSON.parse(line.slice(5)); + events.push(event); + if (response.writableEnded) continue; + if (holding) { + buffered.push(event); + continue; + } + if (!forward(event)) continue; + if ( + armed && + (hold.afterPattern ? new RegExp(hold.afterPattern).test(line) : event.type === (hold.afterType ?? 'TEXT_MESSAGE_CONTENT')) + ) { + armed = false; + holding = true; + held = held_; + } + } + }); + upstreamResponse.on('end', () => { + ended = true; + if (!holding) finish(); + }); + // The real server drops the connection when the graph raises mid-run: + // end the browser's stream there too, without a terminal event. + upstreamResponse.on('aborted', () => { + ended = true; + if (!holding) finish(); + }); + upstreamResponse.on('error', () => { + ended = true; + if (!holding) finish(); + }); + response.on('close', () => { + if (held === held_) held = undefined; + }); + } + + const controls = { + '/__requests': [['GET'], () => requests], + '/__captures': [['GET'], async () => { + await Promise.all(pendingCaptures); + return captures; + }], + '/__lifetime': [['GET'], () => ({ active: Boolean(held), openResponses })], + '/__ready': [['GET', 'POST'], async () => { + await ready(); + const answer = await fetch(new URL('/ok', upstream)); + if (!answer.ok) throw new Error('AG-UI server is not ready'); + return {}; + }], + '/__reset': [['POST'], () => { reset(); return {}; }], + '/__hold/stream': [['POST'], (body) => { + holdNext = { afterType: body.afterType, afterPattern: body.afterPattern }; + return {}; + }], + '/__release': [['POST'], () => { held?.release(); return {}; }], + '/__fail-stream': [['POST'], () => { failNext = true; return {}; }], + '/__stream/edit': [['POST'], (body) => { nextEdits = body.ops ?? []; return {}; }], + }; + + return async function handle(request, response, pathname) { + if (Object.hasOwn(controls, pathname)) { + const [methods, run] = controls[pathname]; + if (!methods.includes(request.method)) { + json(response, {}, 405); + return true; + } + const raw = (await readAll(request)).toString('utf8'); + try { + json(response, await run(raw ? JSON.parse(raw) : {})); + } catch { + json(response, { error: 'Local proxy control failed' }, 500); + } + return true; + } + if (!agentPaths.includes(pathname)) return false; + if (request.method !== 'POST') { + json(response, {}, 405); + return true; + } + openResponses += 1; + response.once('close', () => { openResponses -= 1; }); + await ready(); + const raw = await readAll(request); + try { + await proxyRun(request, response, pathname, raw); + } catch { + if (!response.headersSent) json(response, { error: 'PRIVATE AG-UI proxy failure' }, 502); + else response.end(); + } + return true; + }; +} diff --git a/scripts/react-cockpit/ag-ui-streaming-fixture.mjs b/scripts/react-cockpit/ag-ui-streaming-fixture.mjs deleted file mode 100644 index c4399b715..000000000 --- a/scripts/react-cockpit/ag-ui-streaming-fixture.mjs +++ /dev/null @@ -1,157 +0,0 @@ -/** Local installed AG-UI client HTTP/SSE proof; never assembled into public assets. */ -export function createAgUiStreamingFixture() { - let requests = [], - sequence = 0, - held; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (response, value) => - response.write(`data: ${JSON.stringify(value)}\n\n`); - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - sequence = 0; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (!['/ag-ui/streaming/agent', '/developer-agent'].includes(pathname)) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ path: pathname, method: request.method, body }); - const human = body.messages?.at(-1); - if ( - request.method !== 'POST' || - body.protocolVersion !== '1.0' || - typeof body.threadId !== 'string' || - typeof body.runId !== 'string' || - human?.role !== 'user' || - typeof human.content !== 'string' - ) { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - const answer = { - id: `answer-${++sequence}`, - role: 'assistant', - content: 'Reply to ' + human.content, - }; - const mode = human.content; - if (mode === 'HTTP error') { - json(response, { error: 'PRIVATE upstream failure' }, 500); - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - }); - event(response, { - type: 'RUN_STARTED', - threadId: body.threadId, - runId: body.runId, - }); - if (mode === 'Run error') { - event(response, { - type: 'RUN_ERROR', - message: 'PRIVATE model failure', - code: 'TEST', - }); - response.end(); - return true; - } - if (mode === 'Child') - event(response, { - type: 'SUBAGENT_STARTED', - subagentRunId: 'child', - name: 'worker', - }); - event(response, { - type: 'TEXT_MESSAGE_START', - messageId: answer.id, - role: 'assistant', - }); - event(response, { - type: 'TEXT_MESSAGE_CONTENT', - messageId: answer.id, - delta: human.content === 'Hold' ? 'Live partial' : answer.content, - }); - const finish = () => { - event(response, { type: 'TEXT_MESSAGE_END', messageId: answer.id }); - if (mode === 'Notice EOF') { - event(response, { - type: 'CUSTOM', - name: 'on_interrupt', - value: { note: 'notice' }, - }); - response.end(); - return; - } - let messages = [...body.messages, answer]; - if (mode === 'Duplicate identity') - messages = [...body.messages, { ...answer, id: human.id }]; - if (mode === 'Truncated prefix') messages = [human, answer]; - if (mode === 'Old pair') messages = body.messages.slice(0, -1); - if (mode === 'Tool') - messages = [ - ...body.messages, - { - ...answer, - toolCalls: [ - { - id: 'call', - type: 'function', - function: { name: 'weather', arguments: '{}' }, - }, - ], - }, - ]; - event(response, { type: 'MESSAGES_SNAPSHOT', messages }); - const outcome = - mode === 'Pending tools' - ? { type: 'success', pendingToolCallIds: ['call'] } - : mode === 'Interrupt' - ? { - type: 'interrupt', - interrupts: [{ id: 'pause', reason: 'approval' }], - } - : mode === 'Cancelled' - ? { type: 'cancelled' } - : undefined; - event(response, { - type: 'RUN_FINISHED', - threadId: body.threadId, - runId: mode === 'Wrong terminal' ? 'different-run' : body.runId, - ...(outcome ? { outcome } : {}), - }); - response.end(); - }; - if (human.content === 'Hold') { - const current = { response, finish }; - held = current; - response.once('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - }; -} diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index 5b59b9347..b7d29b48c 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -1,6 +1,8 @@ /** - * Local e2e only: aimock + a real `langgraph dev` for one aimock-backed React - * cockpit example, owned by `serve.mjs --backend`. + * Local e2e only: aimock + the real server for one aimock-backed React cockpit + * example, owned by `serve.mjs --backend`: `langgraph dev` for LangGraph + * examples (`aimock-langgraph`), the example's uvicorn `src/server.py` for + * AG-UI examples (`aimock-ag-ui`). * * Cockpit e2e starts the same backend from its Playwright global setup * (libs/e2e-harness). Website e2e reuses the React examples as previews and @@ -22,6 +24,7 @@ export function createAimockBackend({ root, backend }) { let child; let stderr = ''; + const port = backend.kind === 'aimock-ag-ui' ? backend.backendPort : backend.langgraphPort; async function start() { const dir = resolve(root, backend.fixtures); const fixtures = readdirSync(dir) @@ -29,12 +32,18 @@ export function createAimockBackend({ root, backend }) { .sort() .flatMap((file) => JSON.parse(readFileSync(join(dir, file), 'utf8')).fixtures); // Same replay defaults as libs/e2e-harness/src/aimock-runner.ts. + // A leftover server on the port would answer /ok with a dead aimock behind it. + const stale = await fetch(`http://127.0.0.1:${port}/ok`).then(() => true, () => false); + if (stale) throw new Error(`Port ${port} is already serving; stop the leftover server first`); mock = new LLMock({ port: 0, chunkSize: 4096 }); mock.addFixturesFromJSON(fixtures); await mock.start(); + const agUi = backend.kind === 'aimock-ag-ui'; child = spawn( 'uv', - ['run', 'langgraph', 'dev', '--port', String(backend.langgraphPort), '--no-browser'], + agUi + ? ['run', 'uvicorn', 'src.server:app', '--port', String(port)] + : ['run', 'langgraph', 'dev', '--port', String(port), '--no-browser'], { cwd: resolve(root, backend.python), env: { @@ -53,14 +62,14 @@ export function createAimockBackend({ root, backend }) { while (Date.now() < deadline) { if (child.exitCode !== null) break; try { - if ((await fetch(`http://127.0.0.1:${backend.langgraphPort}/ok`)).ok) return; + if ((await fetch(`http://127.0.0.1:${port}/ok`)).ok) return; } catch { // not listening yet } await delay(500); } process.stderr.write(stderr); - throw new Error(`langgraph dev for ${backend.python} did not become ready`); + throw new Error(`${agUi ? 'uvicorn' : 'langgraph dev'} for ${backend.python} did not become ready`); } return { diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index b58e065c0..cf665d690 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -11,6 +11,17 @@ const aimockLanggraph = (example, port) => Object.freeze({ fixtures: `cockpit/${example}/angular/e2e/fixtures`, langgraphPort: port + 1000, }); +/** + * aimock-backed AG-UI example: the example's uvicorn `src/server.py` + * (ag-ui-langgraph) against the Angular example's aimock fixtures. + * Convention: backend = port + 1000. + */ +const aimockAgUi = (example, port) => Object.freeze({ + kind: 'aimock-ag-ui', + python: `cockpit/${example}/python`, + fixtures: `cockpit/${example}/angular/e2e/fixtures`, + backendPort: port + 1000, +}); /** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime', 'time-travel', 'durable-execution']); export function reactCockpitConfiguration(topic = 'streaming') { @@ -231,6 +242,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/ag-ui/streaming/react/', port: 4609, project: 'cockpit-ag-ui-streaming-react', + backend: aimockAgUi('ag-ui/streaming', 4609), }); if ( ![ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 865f7774f..ed0504ab3 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -11,9 +11,9 @@ import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; +import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createAgUiStreamingFixture } from './ag-ui-streaming-fixture.mjs'; import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; @@ -49,11 +49,12 @@ const nativeStrandsFixture = configuration.library === 'runtimes' && configurati const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; -// aimock-backed examples: the real graph runs under `langgraph dev` (started by -// the Playwright global setup); this server proxies /api to it. +// aimock-backed examples: the real graph runs under `langgraph dev` or, for +// AG-UI examples, the example's uvicorn server (started by the Playwright +// global setup); this server proxies the API or agent endpoint to it. // `--backend` (Website previews) starts that backend here, lazily on the first -// request for the app or its API; cockpit e2e starts it from global setup. -const aimockBackend = configuration.backend?.kind === 'aimock-langgraph' && process.argv.includes('--backend') +// API or control request; cockpit e2e starts it from global setup. +const aimockBackend = configuration.backend && process.argv.includes('--backend') ? createAimockBackend({ root: resolve(dirname(fileURLToPath(import.meta.url)), '../..'), backend: configuration.backend, @@ -65,6 +66,13 @@ const langgraphProxy = configuration.backend?.kind === 'aimock-langgraph' ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) : null; +const agUiProxy = configuration.backend?.kind === 'aimock-ag-ui' + ? createAgUiProxy({ + upstream: `http://127.0.0.1:${configuration.backend.backendPort}`, + agentPaths: [`/ag-ui/${configuration.topic}/agent`, '/developer-agent'], + ready: aimockBackend ? () => aimockBackend.ensure() : undefined, + }) + : null; const agUiSubagentsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'subagents' ? createAgUiSubagentsFixture() @@ -77,10 +85,6 @@ const agUiToolViewsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'tool-views' ? createAgUiToolViewsFixture() : null; -const agUiStreamingFixture = - configuration.adapter === 'ag-ui' && configuration.topic === 'streaming' - ? createAgUiStreamingFixture() - : null; const agUiInterruptsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'interrupts' ? createAgUiInterruptsFixture() @@ -156,6 +160,7 @@ const server = createServer(async (request, response) => { } if (chatGenerativeUiFixture && (await chatGenerativeUiFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; + if (agUiProxy && (await agUiProxy(request, response, pathname))) return; if (configuration.adapter === 'none') { if (!['GET', 'HEAD'].includes(request.method)) { response.writeHead(405, { allow: 'GET, HEAD' }); @@ -190,11 +195,6 @@ const server = createServer(async (request, response) => { (await agUiInterruptsFixture(request, response, pathname)) ) return; - if ( - agUiStreamingFixture && - (await agUiStreamingFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From fd5c396216b9b154a38e03d314b96e5a781cf818 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 19:48:40 -0800 Subject: [PATCH 26/41] test(cockpit): run React ag-ui/interrupts e2e and website preview on aimock + the real native AG-UI endpoint The real /agent/native endpoint pauses with an AG-UI 1.0 interrupt outcome and resumes from `resume`; the spec reads the real interrupt id from the draft run. The proxy gains `omit`, `outcome` (multiple, malformed, expired, child) and two more snapshot modes for the injected protocol faults. Co-Authored-By: Claude Opus 5.5 --- .../react-ag-ui-interrupts-preview.spec.ts | 6 +- apps/website/playwright.config.ts | 4 +- apps/website/src/playwright-config.spec.ts | 6 +- .../angular/e2e/fixtures/interrupts.json | 54 ++++ .../interrupts/react/e2e/global-setup-impl.ts | 6 + .../interrupts/react/e2e/interrupts.spec.ts | 99 +++++++- .../interrupts/react/e2e/playwright.config.ts | 3 + cockpit/ag-ui/interrupts/react/project.json | 37 ++- .../ag-ui-interrupts-fixture.mjs | 240 ------------------ scripts/react-cockpit/ag-ui-proxy.mjs | 35 ++- scripts/react-cockpit/configuration.mjs | 5 +- scripts/react-cockpit/serve.mjs | 13 +- 12 files changed, 229 insertions(+), 279 deletions(-) create mode 100644 cockpit/ag-ui/interrupts/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-interrupts-fixture.mjs diff --git a/apps/website/e2e/react-ag-ui-interrupts-preview.spec.ts b/apps/website/e2e/react-ag-ui-interrupts-preview.spec.ts index 5a9808471..65352249d 100644 --- a/apps/website/e2e/react-ag-ui-interrupts-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-interrupts-preview.spec.ts @@ -71,9 +71,11 @@ for (const phase of ['paused', 'resume']) { await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Awaiting your decision.'); if (phase === 'resume') { + // The resume run streams no text: hold it right after it starts + // (scripts/react-cockpit/ag-ui-proxy.mjs). + await request.post(origin + '/__hold/stream', { data: { afterType: 'RUN_STARTED' } }); await frame.getByRole('button', { name: 'Issue refund', exact: true }).click(); - await expect(frame.getByRole('region', { name: 'Conversation', exact: true })).toContainText('Live partial'); - expect((await (await request.get(origin + '/__lifetime')).json()).active).toBe(true); + await expect.poll(async () => (await (await request.get(origin + '/__lifetime')).json()).active).toBe(true); } const oldFrame = await page.locator('iframe').elementHandle(); await selectFramework(page, 'angular'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index cc2bdf848..bf585bfb0 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -342,9 +342,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4610', + url: 'http://127.0.0.1:4610/__ready', reuseExistingServer, timeout: 180_000, }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 2bf892fce..909b2e332 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -190,7 +190,7 @@ describe('Website Playwright configuration', () => { const reactServers = servers.filter(server => expectedUrls.includes(origin(server.url))); expect(reactServers.map(server => origin(server.url)).sort()).toEqual(expectedUrls); expect(reactServers.every(server => server.reuseExistingServer === false)).toBe(true); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4610')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4610/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent --backend'); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); const projects = project.targets.e2e.dependsOn.find( (dependency: { target?: string }) => dependency.target === 'build' @@ -499,8 +499,8 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4609/__ready', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent', - url: 'http://127.0.0.1:4610', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts --no-parent --backend', + url: 'http://127.0.0.1:4610/__ready', }), expect.objectContaining({ command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent', diff --git a/cockpit/ag-ui/interrupts/angular/e2e/fixtures/interrupts.json b/cockpit/ag-ui/interrupts/angular/e2e/fixtures/interrupts.json index 7fcf25d0b..76fef3e3f 100644 --- a/cockpit/ag-ui/interrupts/angular/e2e/fixtures/interrupts.json +++ b/cockpit/ag-ui/interrupts/angular/e2e/fixtures/interrupts.json @@ -31,6 +31,60 @@ "response": { "content": "Understood — a $129.00 refund to cus_z19fp for a chargeback. Pausing for operator approval; no refund is issued until a human reviews and approves." } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "Negative amount" + }, + "response": { + "content": { + "customer_id": "cus_demo", + "amount": -1, + "reason": "Fictional damaged item" + } + } + }, + { + "match": { + "responseFormat": "json_schema", + "userMessage": "" + }, + "response": { + "content": { + "customer_id": "cus_demo", + "amount": 25, + "reason": "Fictional damaged item" + } + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Hold" + }, + "response": { "content": "Draft ready." }, + "chunkSize": 3 + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "Model failure" + }, + "response": { + "error": { + "message": "PRIVATE model failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "Refund Authorization Assistant", + "userMessage": "" + }, + "response": { "content": "Draft ready." } } ] } diff --git a/cockpit/ag-ui/interrupts/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/interrupts/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..eb7637b7f --- /dev/null +++ b/cockpit/ag-ui/interrupts/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/interrupts', port: 4610 }); diff --git a/cockpit/ag-ui/interrupts/react/e2e/interrupts.spec.ts b/cockpit/ag-ui/interrupts/react/e2e/interrupts.spec.ts index b5d4ac6de..2061e57bd 100644 --- a/cockpit/ag-ui/interrupts/react/e2e/interrupts.spec.ts +++ b/cockpit/ag-ui/interrupts/react/e2e/interrupts.spec.ts @@ -1,7 +1,73 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/interrupts/angular/e2e/fixtures into +// the example's real AG-UI server, whose native endpoint (/agent/native) +// pauses with an AG-UI 1.0 interrupt outcome and resumes from `resume`. The +// recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) holds, fails or +// edits the next run. test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); +const edit = (request: APIRequestContext, ops: object[]) => + request.post('/__stream/edit', { data: { ops } }); +const runError = [ + { type: '^(TEXT_MESSAGE_START|STEP_STARTED)$', op: 'insertBefore', event: { type: 'RUN_ERROR', message: 'PRIVATE model failure', code: 'TEST' } }, + { type: '^(TEXT_MESSAGE_START|STEP_STARTED)$', op: 'end' }, +]; +/** The approval the real graph pauses with, as the legacy notice carries it. */ +const legacyNotice = { + type: 'CUSTOM', + name: 'on_interrupt', + value: JSON.stringify([{ kind: 'refund_approval', amount: 25, customer_id: 'cus_demo', reason: 'Fictional damaged item' }]), +}; +/** + * Arms a draft-run fault. `Negative amount` is real (the extractor fixture + * returns -1); the rest are injected protocol shapes a correct server won't + * produce, for the client's fault handling. + */ +async function draftFault(request: APIRequestContext, mode: string) { + if (mode === 'HTTP error') await request.post('/__fail-stream'); + if (mode === 'Run error') await edit(request, runError); + if (mode === 'Legacy') + await edit(request, [ + { type: '^RUN_FINISHED$', op: 'insertBefore', event: legacyNotice }, + { type: '^RUN_FINISHED$', op: 'omit', keys: ['outcome'] }, + ]); + if (mode === 'Notice EOF') + await edit(request, [ + { type: '^RUN_FINISHED$', op: 'insertBefore', event: legacyNotice }, + { type: '^RUN_FINISHED$', op: 'end' }, + ]); + const outcome = { Multiple: 'multiple', Expired: 'expired', Malformed: 'malformed', 'Child interrupt': 'child' }[mode]; + if (outcome) await edit(request, [{ type: '^RUN_FINISHED$', op: 'outcome', mode: outcome }]); + if (mode === 'Child') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'SUBAGENT_STARTED', subagentRunId: 'child', name: 'worker' } }, + ]); + if (mode === 'Tool') await edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: 'tool-call' }]); + if (mode === 'Duplicate identity') + await edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: 'duplicate-identity' }]); + if (mode === 'Wrong terminal') await edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set: { runId: 'wrong-run' } }]); +} +/** Arms a resume-run fault (injected protocol shapes). */ +async function resumeFault(request: APIRequestContext, mode: string) { + if (mode === 'Resume error') await edit(request, runError); + if (mode === 'Resume interrupted') + await edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set: { outcome: { type: 'cancelled' } } }]); + if (mode === 'Resume wrong terminal') + await edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set: { runId: 'wrong-run' } }]); + const snapshot = { + 'Resume truncated prefix': 'answer-only', + 'Resume old state': 'sent', + 'Resume duplicate identity': 'duplicate-identity', + }[mode]; + if (snapshot) await edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: snapshot }]); + if (mode === 'Resume second pause') { + // The draft run's real pause, offered again after the decision. + const [draft] = await (await request.get('/__captures')).json(); + const finished = draft.events.find((event: { type: string }) => event.type === 'RUN_FINISHED'); + await edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set: { outcome: finished.outcome } }]); + } +} const conversation = (page: Page) => page.getByRole('region', { name: 'Conversation', exact: true }); async function send(page: Page, text: string) { @@ -28,6 +94,7 @@ for (const mode of [ request, }) => { await page.goto('/ag-ui/interrupts/react/'); + await draftFault(request, mode); await send(page, mode); await expect(page.getByRole('status')).toHaveText( 'Start a new conversation to continue.' @@ -69,6 +136,7 @@ for (const mode of [ await expect(page.getByRole('status')).toHaveText( 'Awaiting your decision.' ); + await resumeFault(request, mode); await page .getByRole('button', { name: 'Issue refund', exact: true }) .click(); @@ -93,19 +161,22 @@ for (const phase of ['draft', 'resume']) request, }) => { await page.goto('/ag-ui/interrupts/react/'); + // The draft streams in 3-character chunks: hold after the first. The + // resume run streams no text: hold right after it starts. + if (phase === 'draft') await request.post('/__hold/stream'); await send(page, phase === 'draft' ? 'Hold' : 'Resume hold'); if (phase === 'resume') { await expect(page.getByRole('status')).toHaveText( 'Awaiting your decision.' ); + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED' } }); await page .getByRole('button', { name: 'Issue refund', exact: true }) .click(); - } - await expect(conversation(page)).toContainText('Live partial'); - expect((await (await request.get('/__lifetime')).json()).active).toBe( - true - ); + } else await expect(conversation(page)).toContainText('Dra'); + await expect + .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .toBe(true); if (exit === 'Stop') { await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.getByRole('status')).toHaveText( @@ -133,19 +204,24 @@ test('an incremental draft settles to the current pause and a held resume permit request, }) => { await page.goto('/ag-ui/interrupts/react/'); + await request.post('/__hold/stream'); await send(page, 'Hold'); - await expect(conversation(page)).toContainText('Live partial'); + await expect(conversation(page)).toContainText('Dra'); + await expect(conversation(page)).not.toContainText('Draft ready.'); await expect( page.getByRole('button', { name: 'Issue refund', exact: true }) ).toHaveCount(0); await request.post('/__release'); await expect(page.getByRole('status')).toHaveText('Awaiting your decision.'); - await expect(conversation(page)).not.toContainText('Live partial'); + await expect(conversation(page)).toContainText('Draft ready.'); await page.getByRole('button', { name: 'New conversation' }).click(); await send(page, 'Resume hold'); await expect(page.getByRole('status')).toHaveText('Awaiting your decision.'); + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED' } }); await page.getByRole('button', { name: 'Issue refund', exact: true }).click(); - await expect(conversation(page)).toContainText('Live partial'); + await expect + .poll(async () => (await (await request.get('/__lifetime')).json()).active) + .toBe(true); await expect( page.getByRole('button', { name: 'Issue refund', exact: true }) ).toBeDisabled(); @@ -297,9 +373,12 @@ for (const action of ['Issue refund', 'Edit and approve', "Don't refund"]) { ' Fictional refund ', 'Draft ready.', ]); + // The real graph's interrupt id, as its draft run reported it. + const [draft] = await (await request.get('/__captures')).json(); + const pause = draft.events.find((event: { type: string }) => event.type === 'RUN_FINISHED').outcome.interrupts[0]; expect(before[1].body.resume).toEqual([ { - interruptId: 'interrupt-1', + interruptId: pause.id, status: 'resolved', payload: action === "Don't refund" diff --git a/cockpit/ag-ui/interrupts/react/e2e/playwright.config.ts b/cockpit/ag-ui/interrupts/react/e2e/playwright.config.ts index b881aa8fc..7958a7ed9 100644 --- a/cockpit/ag-ui/interrupts/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/interrupts/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4610', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/interrupts/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs ag-ui-interrupts', cwd: '../../../../..', diff --git a/cockpit/ag-ui/interrupts/react/project.json b/cockpit/ag-ui/interrupts/react/project.json index 8f27266cb..f516bf0eb 100644 --- a/cockpit/ag-ui/interrupts/react/project.json +++ b/cockpit/ag-ui/interrupts/react/project.json @@ -3,7 +3,15 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/interrupts/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-ag-ui-interrupts-angular", + "cockpit-ag-ui-interrupts-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { "test": { "executor": "@nx/vitest:test", @@ -17,11 +25,22 @@ "dependsOn": [ { "target": "build", - "projects": ["core", "content", "react"] + "projects": [ + "core", + "content", + "react" + ] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/ag-ui/interrupts/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/ag-ui/interrupts/react"], "options": { "command": "node scripts/react-cockpit/build.mjs ag-ui-interrupts" } @@ -32,11 +51,17 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build"], + "dependsOn": [ + "build" + ], "options": { "config": "cockpit/ag-ui/interrupts/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/scripts/react-cockpit/ag-ui-interrupts-fixture.mjs b/scripts/react-cockpit/ag-ui-interrupts-fixture.mjs deleted file mode 100644 index 634014428..000000000 --- a/scripts/react-cockpit/ag-ui-interrupts-fixture.mjs +++ /dev/null @@ -1,240 +0,0 @@ -/** Frozen native AG-UI refund wire proof; excluded from public assembly. */ -export function createAgUiInterruptsFixture() { - let requests = [], - sequence = 0, - held; - const conversations = new Map(); - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (response, value) => - response.write(`data: ${JSON.stringify(value)}\n\n`); - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - sequence = 0; - conversations.clear(); - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if ( - !['/ag-ui/interrupts/agent/native', '/developer-agent'].includes(pathname) - ) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ path: pathname, method: request.method, body }); - if ( - request.method !== 'POST' || - body.protocolVersion !== '1.0' || - typeof body.threadId !== 'string' || - !body.threadId || - typeof body.runId !== 'string' || - !body.runId || - !Array.isArray(body.messages) - ) { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - const previous = conversations.get(body.threadId); - const resumed = body.resume !== undefined; - const human = body.messages.at(-1); - if ( - (!resumed && - (human?.role !== 'user' || typeof human.content !== 'string')) || - (resumed && - (!previous || - !Array.isArray(body.resume) || - body.resume.length !== 1 || - body.resume[0].interruptId !== previous.interrupt.id || - body.resume[0].status !== 'resolved' || - typeof body.resume[0].payload?.approved !== 'boolean' || - JSON.stringify(body.messages) !== JSON.stringify(previous.messages))) - ) { - json(response, { error: 'Invalid current native decision' }, 400); - return true; - } - const mode = resumed ? previous.mode : human.content; - if (mode === 'HTTP error') { - json(response, { error: 'PRIVATE upstream failure' }, 500); - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - }); - event(response, { - type: 'RUN_STARTED', - threadId: body.threadId, - runId: body.runId, - }); - if (mode === 'Run error' || (resumed && mode === 'Resume error')) { - event(response, { - type: 'RUN_ERROR', - message: 'PRIVATE provider failure', - code: 'TEST', - }); - response.end(); - return true; - } - if (mode === 'Child') - event(response, { - type: 'SUBAGENT_STARTED', - subagentRunId: 'child', - name: 'worker', - }); - const answer = { - id: `answer-${++sequence}`, - role: 'assistant', - content: resumed - ? body.resume[0].payload.approved - ? `Refund of $${Number( - body.resume[0].payload.amount ?? - previous.interrupt.metadata.langgraph.raw.amount - ).toFixed(2)} issued to cus_demo.` - : 'Refund cancelled by operator. No charge issued.' - : 'Draft ready.', - }; - event(response, { - type: 'TEXT_MESSAGE_START', - messageId: answer.id, - role: 'assistant', - }); - const hold = - (!resumed && mode === 'Hold') || (resumed && mode === 'Resume hold'); - event(response, { - type: 'TEXT_MESSAGE_CONTENT', - messageId: answer.id, - delta: hold ? 'Live partial' : answer.content, - }); - const finish = () => { - event(response, { type: 'TEXT_MESSAGE_END', messageId: answer.id }); - let messages = [...body.messages, answer]; - if ( - mode === 'Duplicate identity' || - (resumed && mode === 'Resume duplicate identity') - ) - messages = [...body.messages, { ...answer, id: body.messages[0].id }]; - if (mode === 'Tool') - messages = [ - ...body.messages, - { - ...answer, - toolCalls: [ - { - id: 'call', - type: 'function', - function: { name: 'private', arguments: '{}' }, - }, - ], - }, - ]; - if (resumed && mode === 'Resume truncated prefix') messages = [answer]; - if (resumed && mode === 'Resume old state') messages = body.messages; - event(response, { type: 'MESSAGES_SNAPSHOT', messages }); - if (resumed) { - conversations.delete(body.threadId); - event(response, { - type: 'STATE_SNAPSHOT', - snapshot: { - decision_approved: body.resume[0].payload.approved, - refund_id: body.resume[0].payload.approved ? 're_demo_fake' : null, - }, - }); - event(response, { - type: 'RUN_FINISHED', - threadId: body.threadId, - runId: mode === 'Resume wrong terminal' ? 'wrong-run' : body.runId, - ...(mode === 'Resume interrupted' - ? { outcome: { type: 'cancelled' } } - : {}), - ...(mode === 'Resume second pause' - ? { - outcome: { - type: 'interrupt', - interrupts: [previous.interrupt], - }, - } - : {}), - }); - } else { - const interrupt = { - id: `interrupt-${sequence}`, - reason: 'human_approval', - metadata: { - langgraph: { - raw: { - kind: 'refund_approval', - amount: 25, - customer_id: 'cus_demo', - reason: 'Fictional damaged item', - }, - }, - }, - }; - if (mode === 'Malformed') - interrupt.metadata.langgraph.raw.kind = 'other'; - if (mode === 'Negative amount') - interrupt.metadata.langgraph.raw.amount = -1; - if (mode === 'Expired') interrupt.expiresAt = '2020-01-01T00:00:00Z'; - if (mode === 'Child interrupt') interrupt.subagentRunId = 'child'; - conversations.set(body.threadId, { interrupt, messages, mode }); - if (mode === 'Legacy' || mode === 'Notice EOF') { - event(response, { - type: 'CUSTOM', - name: 'on_interrupt', - value: JSON.stringify([interrupt.metadata.langgraph.raw]), - }); - if (mode === 'Notice EOF') { - response.end(); - return; - } - } - event(response, { - type: 'RUN_FINISHED', - threadId: body.threadId, - runId: mode === 'Wrong terminal' ? 'wrong-run' : body.runId, - ...(mode === 'Legacy' - ? {} - : { - outcome: { - type: 'interrupt', - interrupts: - mode === 'Multiple' - ? [interrupt, { ...interrupt, id: 'second' }] - : [interrupt], - }, - }), - }); - } - response.end(); - }; - if (hold) { - const current = { response, finish }; - held = current; - response.once('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - }; -} diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index 43acd19e0..927498872 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -26,6 +26,12 @@ * { type: regex on the event type, op, once? }: * drop remove the event * patch { set } merge fields into it + * omit { keys } remove fields + * outcome { mode } rewrite the real + * interrupt of a + * RUN_FINISHED outcome: + * multiple, malformed, + * expired, child * insertBefore|insertAfter { event } * end end the stream just * before the event @@ -33,7 +39,8 @@ * MESSAGES_SNAPSHOT: * duplicate-identity, * truncated-prefix, - * old-pair, tool-call + * old-pair, answer-only, + * sent, tool-call * "$threadId", "$runId", "$humanId" and * "$answerId" in values are the run's own ids. * Injected data: protocol shapes a correct server @@ -54,7 +61,7 @@ const readAll = async (stream) => { return Buffer.concat(chunks); }; -/** The four snapshot shapes the authored AG-UI fixtures produced. */ +/** The snapshot shapes the authored AG-UI fixtures produced. */ function rewriteSnapshot(mode, event, body) { const sent = body.messages ?? []; const human = sent.at(-1); @@ -63,6 +70,8 @@ function rewriteSnapshot(mode, event, body) { if (mode === 'duplicate-identity') messages[messages.length - 1] = { ...answer, id: human.id }; else if (mode === 'truncated-prefix') return { ...event, messages: [human, answer] }; else if (mode === 'old-pair') return { ...event, messages: sent.slice(0, -1) }; + else if (mode === 'answer-only') return { ...event, messages: [answer] }; + else if (mode === 'sent') return { ...event, messages: sent }; else if (mode === 'tool-call') messages[messages.length - 1] = { ...answer, @@ -72,7 +81,20 @@ function rewriteSnapshot(mode, event, body) { return { ...event, messages }; } -export function createAgUiProxy({ upstream, agentPaths, ready = async () => {} }) { +/** Rewrites the real interrupt in a RUN_FINISHED interrupt outcome. */ +function rewriteOutcome(mode, event) { + const [first] = event.outcome?.interrupts ?? []; + if (!first) return event; + const copy = structuredClone(first); + if (mode === 'multiple') return { ...event, outcome: { ...event.outcome, interrupts: [first, { ...copy, id: 'second' }] } }; + if (mode === 'malformed') copy.metadata.langgraph.raw.kind = 'other'; + else if (mode === 'expired') copy.expiresAt = '2020-01-01T00:00:00Z'; + else if (mode === 'child') copy.subagentRunId = 'child'; + else throw new Error('Unsupported outcome mode'); + return { ...event, outcome: { ...event.outcome, interrupts: [copy] } }; +} + +export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', ready = async () => {} }) { let requests = []; let captures = []; const pendingCaptures = new Set(); @@ -111,7 +133,7 @@ export function createAgUiProxy({ upstream, agentPaths, ready = async () => {} } ); const upstreamResponse = await new Promise((resolve, reject) => { const outgoing = httpRequest( - new URL('/agent', upstream), + new URL(agentPath, upstream), { method: 'POST', headers: { 'content-type': 'application/json', accept: 'text/event-stream' } }, resolve ); @@ -153,6 +175,11 @@ export function createAgUiProxy({ upstream, agentPaths, ready = async () => {} } return false; } if (op.op === 'patch') current = { ...current, ...fill(op.set) }; + if (op.op === 'omit') { + current = { ...current }; + for (const key of op.keys ?? []) delete current[key]; + } + if (op.op === 'outcome') current = rewriteOutcome(op.mode, current); if (op.op === 'snapshot') current = rewriteSnapshot(op.mode, current, body); if (op.op === 'insertBefore') write(fill(op.event)); if (op.op === 'insertAfter') after.push(fill(op.event)); diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index cf665d690..fa16cc434 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -16,11 +16,13 @@ const aimockLanggraph = (example, port) => Object.freeze({ * (ag-ui-langgraph) against the Angular example's aimock fixtures. * Convention: backend = port + 1000. */ -const aimockAgUi = (example, port) => Object.freeze({ +const aimockAgUi = (example, port, agentPath = '/agent') => Object.freeze({ kind: 'aimock-ag-ui', python: `cockpit/${example}/python`, fixtures: `cockpit/${example}/angular/e2e/fixtures`, backendPort: port + 1000, + // The server route the React example talks to (src/server.py). + agentPath, }); /** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime', 'time-travel', 'durable-execution']); @@ -233,6 +235,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/ag-ui/interrupts/react/', port: 4610, project: 'cockpit-ag-ui-interrupts-react', + backend: aimockAgUi('ag-ui/interrupts', 4610, '/agent/native'), }); if (topic === 'ag-ui-streaming') return Object.freeze({ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index ed0504ab3..5d88d968c 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -14,7 +14,6 @@ import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createAgUiInterruptsFixture } from './ag-ui-interrupts-fixture.mjs'; import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; @@ -69,7 +68,8 @@ const langgraphProxy = configuration.backend?.kind === 'aimock-langgraph' const agUiProxy = configuration.backend?.kind === 'aimock-ag-ui' ? createAgUiProxy({ upstream: `http://127.0.0.1:${configuration.backend.backendPort}`, - agentPaths: [`/ag-ui/${configuration.topic}/agent`, '/developer-agent'], + agentPaths: [`/ag-ui/${configuration.topic}${configuration.backend.agentPath}`, '/developer-agent'], + agentPath: configuration.backend.agentPath, ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) : null; @@ -85,10 +85,6 @@ const agUiToolViewsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'tool-views' ? createAgUiToolViewsFixture() : null; -const agUiInterruptsFixture = - configuration.adapter === 'ag-ui' && configuration.topic === 'interrupts' - ? createAgUiInterruptsFixture() - : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( @@ -190,11 +186,6 @@ const server = createServer(async (request, response) => { (await agUiToolViewsFixture(request, response, pathname)) ) return; - if ( - agUiInterruptsFixture && - (await agUiInterruptsFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From 44c7f755835392d46f72ca2e64ae900391313347 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 19:57:38 -0800 Subject: [PATCH 27/41] test(cockpit): run React ag-ui/tool-views e2e and website preview on aimock + the real AG-UI server The real graph calls the real weather tool; an unknown tool and a call without a location are real tool failures. The proxy gains snapshot modes for the injected result faults, and `persist` for a backend that saves results under another identity (mapped back on the way upstream). Co-Authored-By: Claude Opus 5.5 --- .../react-ag-ui-tool-views-preview.spec.ts | 2 + apps/website/playwright.config.ts | 4 +- apps/website/src/playwright-config.spec.ts | 6 +- .../angular/e2e/fixtures/tool-views.json | 526 ++++++++++++++++++ .../tool-views/react/e2e/global-setup-impl.ts | 6 + .../tool-views/react/e2e/playwright.config.ts | 3 + .../tool-views/react/e2e/tool-views.spec.ts | 66 ++- cockpit/ag-ui/tool-views/react/project.json | 37 +- scripts/react-cockpit/ag-ui-proxy.mjs | 56 +- .../ag-ui-tool-views-fixture.mjs | 251 --------- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/serve.mjs | 10 - 12 files changed, 690 insertions(+), 278 deletions(-) create mode 100644 cockpit/ag-ui/tool-views/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-tool-views-fixture.mjs diff --git a/apps/website/e2e/react-ag-ui-tool-views-preview.spec.ts b/apps/website/e2e/react-ag-ui-tool-views-preview.spec.ts index c2d437f10..87e2f92a8 100644 --- a/apps/website/e2e/react-ag-ui-tool-views-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-tool-views-preview.spec.ts @@ -69,6 +69,8 @@ test('switching away disposes a held server-tool stream and returning stays lazy ); const origin = 'http://127.0.0.1:4611'; await request.post(origin + '/__reset'); + // Hold the real stream after the tool result (scripts/react-cockpit/ag-ui-proxy.mjs). + await request.post(origin + '/__hold/stream', { data: { afterType: 'TOOL_CALL_RESULT' } }); await page.goto('/docs/react/ag-ui/guides/tool-views?mode=run'); const frame = page.frameLocator('iframe'); await expect(frame.getByRole('status', { name: 'Conversation status' })).toHaveText('Ready.'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index bf585bfb0..0b6e14543 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -349,9 +349,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4611', + url: 'http://127.0.0.1:4611/__ready', reuseExistingServer, timeout: 180_000, }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 909b2e332..77feefec7 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -177,7 +177,7 @@ describe('Website Playwright configuration', () => { const config = createWebsitePlaywrightConfig({CI:'true'}); const servers = Array.isArray(config.webServer) ? config.webServer : []; expect(servers.find(server => server.url === 'http://localhost:4322')?.command).toBe('npx nx run cockpit-ag-ui-tool-views-angular:serve:cockpit --port 4322'); - expect(servers.find(server => server.url === 'http://127.0.0.1:4611')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent'); + expect(servers.find(server => server.url === 'http://127.0.0.1:4611/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent --backend'); const project = JSON.parse(readFileSync(resolve(import.meta.dirname,'../project.json'),'utf8')); expect(project.implicitDependencies).toContain('cockpit-ag-ui-tool-views-angular'); }); @@ -503,8 +503,8 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4610/__ready', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent', - url: 'http://127.0.0.1:4611', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent --backend', + url: 'http://127.0.0.1:4611/__ready', }), expect.objectContaining({command:'node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs --serve',url:'http://127.0.0.1:4640'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent',url:'http://127.0.0.1:4641'}), diff --git a/cockpit/ag-ui/tool-views/angular/e2e/fixtures/tool-views.json b/cockpit/ag-ui/tool-views/angular/e2e/fixtures/tool-views.json index 2069a0412..7f0bebb4a 100644 --- a/cockpit/ag-ui/tool-views/angular/e2e/fixtures/tool-views.json +++ b/cockpit/ag-ui/tool-views/angular/e2e/fixtures/tool-views.json @@ -23,6 +23,532 @@ } ] } + }, + { + "match": { "userMessage": "Zero" }, + "response": { "content": "Reply to Zero" } + }, + { + "match": { + "userMessage": "One", + "hasToolResult": true + }, + "response": { "content": "Reply to One" } + }, + { + "match": { + "userMessage": "One", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Hold args", + "hasToolResult": true + }, + "response": { "content": "Reply to Hold args" } + }, + { + "match": { + "userMessage": "Hold args", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Hold call", + "hasToolResult": true + }, + "response": { "content": "Reply to Hold call" } + }, + { + "match": { + "userMessage": "Hold call", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Hold result", + "hasToolResult": true + }, + "response": { "content": "Reply to Hold result" } + }, + { + "match": { + "userMessage": "Hold result", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Hold final", + "hasToolResult": true + }, + "response": { "content": "Reply to Hold final" } + }, + { + "match": { + "userMessage": "Hold final", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Unstable result identity", + "hasToolResult": true + }, + "response": { "content": "Reply to Unstable result identity" } + }, + { + "match": { + "userMessage": "Unstable result identity", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "HTTP error", + "hasToolResult": true + }, + "response": { "content": "Reply to HTTP error" } + }, + { + "match": { + "userMessage": "HTTP error", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Run error", + "hasToolResult": true + }, + "response": { "content": "Reply to Run error" } + }, + { + "match": { + "userMessage": "Run error", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Child", + "hasToolResult": true + }, + "response": { "content": "Reply to Child" } + }, + { + "match": { + "userMessage": "Child", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Decision", + "hasToolResult": true + }, + "response": { "content": "Reply to Decision" } + }, + { + "match": { + "userMessage": "Decision", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Pending tools", + "hasToolResult": true + }, + "response": { "content": "Reply to Pending tools" } + }, + { + "match": { + "userMessage": "Pending tools", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Wrong terminal", + "hasToolResult": true + }, + "response": { "content": "Reply to Wrong terminal" } + }, + { + "match": { + "userMessage": "Wrong terminal", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Truncated prefix", + "hasToolResult": true + }, + "response": { "content": "Reply to Truncated prefix" } + }, + { + "match": { + "userMessage": "Truncated prefix", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Duplicate result", + "hasToolResult": true + }, + "response": { "content": "Reply to Duplicate result" } + }, + { + "match": { + "userMessage": "Duplicate result", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Orphan result", + "hasToolResult": true + }, + "response": { "content": "Reply to Orphan result" } + }, + { + "match": { + "userMessage": "Orphan result", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Malformed weather", + "hasToolResult": true + }, + "response": { "content": "Reply to Malformed weather" } + }, + { + "match": { + "userMessage": "Malformed weather", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Legacy notice", + "hasToolResult": true + }, + "response": { "content": "Reply to Legacy notice" } + }, + { + "match": { + "userMessage": "Legacy notice", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Notice EOF", + "hasToolResult": true + }, + "response": { "content": "Reply to Notice EOF" } + }, + { + "match": { + "userMessage": "Notice EOF", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "EOF", + "hasToolResult": true + }, + "response": { "content": "Reply to EOF" } + }, + { + "match": { + "userMessage": "EOF", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Batch", + "hasToolResult": true + }, + "response": { "content": "Reply to Batch" } + }, + { + "match": { + "userMessage": "Batch", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" } + }, + { + "name": "weather_card", + "arguments": { "location": "Boston" } + } + ] + } + }, + { + "match": { "toolCallId": "rounds-second" }, + "response": { "content": "Reply to Rounds" } + }, + { + "match": { "toolCallId": "rounds-first" }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "Boston" }, + "id": "rounds-second" + } + ] + } + }, + { + "match": { + "userMessage": "Rounds", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "San Francisco" }, + "id": "rounds-first" + } + ] + } + }, + { + "match": { + "userMessage": "Unknown tool", + "hasToolResult": true + }, + "response": { "content": "Reply to Unknown tool" } + }, + { + "match": { + "userMessage": "Unknown tool", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "unknown", + "arguments": { "location": "San Francisco" } + } + ] + } + }, + { + "match": { + "userMessage": "Malformed args", + "hasToolResult": true + }, + "response": { "content": "Reply to Malformed args" } + }, + { + "match": { + "userMessage": "Malformed args", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": {} + } + ] + } + }, + { + "match": { + "userMessage": "Literal", + "hasToolResult": true + }, + "response": { "content": "" } + }, + { + "match": { + "userMessage": "Literal", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": { "location": "" } + } + ] + } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Reply recorded." } } ] } diff --git a/cockpit/ag-ui/tool-views/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/tool-views/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..c5e922c7b --- /dev/null +++ b/cockpit/ag-ui/tool-views/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/tool-views', port: 4611 }); diff --git a/cockpit/ag-ui/tool-views/react/e2e/playwright.config.ts b/cockpit/ag-ui/tool-views/react/e2e/playwright.config.ts index 28d83addf..14f56d0b4 100644 --- a/cockpit/ag-ui/tool-views/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/tool-views/react/e2e/playwright.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ fullyParallel: false, use: { baseURL: 'http://127.0.0.1:4611', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/tool-views/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views', cwd: '../../../../..', diff --git a/cockpit/ag-ui/tool-views/react/e2e/tool-views.spec.ts b/cockpit/ag-ui/tool-views/react/e2e/tool-views.spec.ts index 126c8ac28..47bffb1b3 100644 --- a/cockpit/ag-ui/tool-views/react/e2e/tool-views.spec.ts +++ b/cockpit/ag-ui/tool-views/react/e2e/tool-views.spec.ts @@ -1,7 +1,67 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/tool-views/angular/e2e/fixtures into +// the example's real AG-UI server, whose graph calls the real weather tool. +// The recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) holds, fails or +// edits the next run. test.beforeEach(async ({ request }) => { await request.post('/__reset'); }); +const edit = (request: APIRequestContext, ops: object[]) => + request.post('/__stream/edit', { data: { ops } }); +/** Where each hold stops the real stream. */ +const holds: Record = { + 'Hold args': { afterType: 'TOOL_CALL_START' }, + 'Hold call': { afterType: 'TOOL_CALL_END' }, + 'Hold result': { afterType: 'TOOL_CALL_RESULT' }, + 'Hold final': { afterType: 'MESSAGES_SNAPSHOT' }, +}; +/** + * Arms a fault. `Unknown tool` and `Malformed args` are real (the model calls + * a tool that does not exist, or the weather tool without a location); the + * rest are injected protocol shapes a correct server won't produce. + */ +async function fault(request: APIRequestContext, mode: string) { + const finished = (set: object) => edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set }]); + const snapshot = (name: string) => edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: name }]); + if (mode === 'HTTP error') await request.post('/__fail-stream'); + if (mode === 'Run error') + await edit(request, [ + { type: '^TOOL_CALL_START$', op: 'insertBefore', event: { type: 'RUN_ERROR', message: 'PRIVATE model failure', code: 'TEST' } }, + { type: '^TOOL_CALL_START$', op: 'end' }, + ]); + if (mode === 'Child') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'SUBAGENT_STARTED', subagentRunId: 'child', name: 'worker' } }, + ]); + if (mode === 'Decision') await finished({ outcome: { type: 'interrupt', interrupts: [{ id: 'pause', reason: 'approval' }] } }); + if (mode === 'Pending tools') await finished({ outcome: { type: 'success', pendingToolCallIds: ['unobserved'] } }); + if (mode === 'Wrong terminal') await finished({ runId: 'different-run' }); + if (mode === 'Truncated prefix') await snapshot('drop-sent'); + if (mode === 'Duplicate result') await snapshot('duplicate-result'); + if (mode === 'Orphan result') await snapshot('orphan-result'); + // The backend saves every result under another identity from then on. + if (mode === 'Unstable result identity') + await request.post('/__stream/edit', { + data: { persist: true, ops: [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: 'unstable-result' }] }, + }); + if (mode === 'Malformed weather') { + const content = JSON.stringify({ location: 'San Francisco', temperatureF: 'hot', conditions: 'Sunny', humidity: 55, windMph: 8 }); + await edit(request, [ + { type: '^TOOL_CALL_RESULT$', op: 'patch', set: { content } }, + { type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: 'result-content', content }, + ]); + } + if (mode === 'Legacy notice') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'CUSTOM', name: 'on_interrupt', value: { note: 'legacy pause' } } }, + ]); + if (mode === 'Notice EOF') + await edit(request, [ + { type: '^MESSAGES_SNAPSHOT$', op: 'insertBefore', event: { type: 'CUSTOM', name: 'unsupported', value: {} } }, + { type: '^MESSAGES_SNAPSHOT$', op: 'end' }, + ]); + if (mode === 'EOF') await edit(request, [{ type: '^RUN_FINISHED$', op: 'end' }]); +} const rows = (page: Page) => page.locator('[data-weather-message]'); async function send(page: Page, text: string) { await page.getByLabel('Message', { exact: true }).fill(text); @@ -88,6 +148,7 @@ for (const mode of ['Hold args', 'Hold call', 'Hold result', 'Hold final']) { request, }) => { await page.goto('/ag-ui/tool-views/react/'); + await request.post('/__hold/stream', { data: holds[mode] }); await send(page, mode); await expect .poll( @@ -121,6 +182,7 @@ for (const mode of ['Hold args', 'Hold call', 'Hold result', 'Hold final']) { request, }) => { await page.goto('/ag-ui/tool-views/react/'); + await request.post('/__hold/stream', { data: holds[mode] }); await send(page, mode); await expect .poll( @@ -170,6 +232,7 @@ for (const mode of [ await send(page, 'One'); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText('Response complete.'); } + await fault(request, mode); await send(page, mode); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText( 'Start a new conversation to continue.' @@ -194,6 +257,7 @@ test('a result saved under another identity keeps its card and its place in the // The stream names the result after its call; the final snapshot saves it // under another identifier, as graph backends do. await page.goto('/ag-ui/tool-views/react/'); + await fault(request, 'Unstable result identity'); await send(page, 'Unstable result identity'); await expect( page.getByRole('status', { name: 'Conversation status' }) diff --git a/cockpit/ag-ui/tool-views/react/project.json b/cockpit/ag-ui/tool-views/react/project.json index d06ebddd7..1bb700abd 100644 --- a/cockpit/ag-ui/tool-views/react/project.json +++ b/cockpit/ag-ui/tool-views/react/project.json @@ -3,7 +3,15 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/tool-views/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-ag-ui-tool-views-angular", + "cockpit-ag-ui-tool-views-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { "test": { "executor": "@nx/vitest:test", @@ -17,11 +25,22 @@ "dependsOn": [ { "target": "build", - "projects": ["core", "content", "react"] + "projects": [ + "core", + "content", + "react" + ] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/ag-ui/tool-views/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/ag-ui/tool-views/react"], "options": { "command": "node scripts/react-cockpit/build.mjs ag-ui-tool-views" } @@ -32,11 +51,17 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build"], + "dependsOn": [ + "build" + ], "options": { "config": "cockpit/ag-ui/tool-views/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index 927498872..db0b35fb5 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -40,7 +40,16 @@ * duplicate-identity, * truncated-prefix, * old-pair, answer-only, - * sent, tool-call + * sent, drop-sent, + * duplicate-result, + * orphan-result, + * unstable-result, + * result-content + * { content }, + * tool-call + * `persist: true` applies the ops to every later + * run until reset (a backend property, not a + * one-off fault). * "$threadId", "$runId", "$humanId" and * "$answerId" in values are the run's own ids. * Injected data: protocol shapes a correct server @@ -62,7 +71,7 @@ const readAll = async (stream) => { }; /** The snapshot shapes the authored AG-UI fixtures produced. */ -function rewriteSnapshot(mode, event, body) { +function rewriteSnapshot(mode, event, body, options = {}) { const sent = body.messages ?? []; const human = sent.at(-1); const messages = [...event.messages]; @@ -71,6 +80,26 @@ function rewriteSnapshot(mode, event, body) { else if (mode === 'truncated-prefix') return { ...event, messages: [human, answer] }; else if (mode === 'old-pair') return { ...event, messages: sent.slice(0, -1) }; else if (mode === 'answer-only') return { ...event, messages: [answer] }; + else if (mode === 'drop-sent') { + const sentIds = new Set(sent.map((message) => message.id)); + return { ...event, messages: messages.filter((message) => !sentIds.has(message.id)) }; + } else if (mode === 'duplicate-result') { + const index = messages.findLastIndex((message) => message.role === 'tool'); + messages.splice(messages.length - 1, 0, { ...messages[index], id: 'duplicate-result' }); + } else if (mode === 'unstable-result') { + // Every saved result under another identity (with `persist`, the proxy + // maps them back on the way upstream, so the server's history agrees). + return { + ...event, + messages: messages.map((message) => + message.role === 'tool' ? { ...message, id: `graph-${message.toolCallId}` } : message + ), + }; + } else if (mode === 'result-content') { + const index = messages.findLastIndex((message) => message.role === 'tool'); + messages[index] = { ...messages[index], content: options.content }; + } else if (mode === 'orphan-result') + messages.splice(messages.length - 1, 0, { id: 'orphan', role: 'tool', toolCallId: 'orphan-call', content: '{}' }); else if (mode === 'sent') return { ...event, messages: sent }; else if (mode === 'tool-call') messages[messages.length - 1] = { @@ -102,6 +131,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re let held; let failNext = false; let nextEdits = null; + let stickyEdits = []; let openResponses = 0; function reset() { @@ -110,6 +140,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re holdNext = null; failNext = false; nextEdits = null; + stickyEdits = []; held?.end(); held = undefined; } @@ -117,6 +148,17 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re async function proxyRun(request, response, pathname, raw) { const body = raw.length ? JSON.parse(raw.toString('utf8')) : {}; requests.push({ path: pathname, method: request.method, body }); + if (stickyEdits.some((op) => op.mode === 'unstable-result') && Array.isArray(body.messages)) + raw = Buffer.from( + JSON.stringify({ + ...body, + messages: body.messages.map((message) => + message.role === 'tool' && message.id === `graph-${message.toolCallId}` + ? { ...message, id: message.toolCallId } + : message + ), + }) + ); if (failNext) { failNext = false; json(response, { error: 'PRIVATE AG-UI server failure' }, 500); @@ -124,7 +166,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re } const hold = holdNext; holdNext = null; - const edits = (nextEdits ?? []).map((op) => ({ ...op, pattern: new RegExp(op.type ?? '.*'), used: false })); + const edits = [...stickyEdits, ...(nextEdits ?? [])].map((op) => ({ ...op, pattern: new RegExp(op.type ?? '.*'), used: false })); nextEdits = null; const ids = { threadId: body.threadId, runId: body.runId, humanId: body.messages?.at(-1)?.id, answerId: null }; const fill = (value) => @@ -180,7 +222,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re for (const key of op.keys ?? []) delete current[key]; } if (op.op === 'outcome') current = rewriteOutcome(op.mode, current); - if (op.op === 'snapshot') current = rewriteSnapshot(op.mode, current, body); + if (op.op === 'snapshot') current = rewriteSnapshot(op.mode, current, body, op); if (op.op === 'insertBefore') write(fill(op.event)); if (op.op === 'insertAfter') after.push(fill(op.event)); } @@ -274,7 +316,11 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re }], '/__release': [['POST'], () => { held?.release(); return {}; }], '/__fail-stream': [['POST'], () => { failNext = true; return {}; }], - '/__stream/edit': [['POST'], (body) => { nextEdits = body.ops ?? []; return {}; }], + '/__stream/edit': [['POST'], (body) => { + if (body.persist) stickyEdits = body.ops ?? []; + else nextEdits = body.ops ?? []; + return {}; + }], }; return async function handle(request, response, pathname) { diff --git a/scripts/react-cockpit/ag-ui-tool-views-fixture.mjs b/scripts/react-cockpit/ag-ui-tool-views-fixture.mjs deleted file mode 100644 index 06aa9ecdc..000000000 --- a/scripts/react-cockpit/ag-ui-tool-views-fixture.mjs +++ /dev/null @@ -1,251 +0,0 @@ -/** Local installed-client proof server. Never included in public assets. */ -export function createAgUiToolViewsFixture() { - let requests = [], - sequence = 0, - held; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - const event = (response, value) => - response.write(`data: ${JSON.stringify(value)}\n\n`); - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - sequence = 0; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (!['/ag-ui/tool-views/agent', '/developer-agent'].includes(pathname)) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - const body = input ? JSON.parse(input) : {}; - requests.push({ path: pathname, method: request.method, body }); - const human = body.messages?.at(-1); - if ( - request.method !== 'POST' || - body.protocolVersion !== '1.0' || - typeof body.threadId !== 'string' || - typeof body.runId !== 'string' || - human?.role !== 'user' || - typeof human.content !== 'string' - ) { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - const mode = human.content, - turn = ++sequence; - if (mode === 'HTTP error') { - json(response, { error: 'PRIVATE upstream failure' }, 500); - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - }); - const emit = (value) => event(response, value); - emit({ type: 'RUN_STARTED', threadId: body.threadId, runId: body.runId }); - if (mode === 'Run error') { - emit({ type: 'RUN_ERROR', message: 'PRIVATE provider failure' }); - response.end(); - return true; - } - if (mode === 'Child') - emit({ - type: 'SUBAGENT_STARTED', - subagentRunId: 'child', - name: 'worker', - }); - if (mode === 'Legacy notice') - emit({ - type: 'CUSTOM', - name: 'on_interrupt', - value: { note: 'legacy pause' }, - }); - const additions = []; - const hold = (finish) => { - const current = { response, finish }; - held = current; - response.once('close', () => { - if (held === current) held = undefined; - }); - }; - const finish = () => { - const answer = { - id: `answer-${turn}`, - role: 'assistant', - content: - mode === 'Literal' - ? '' - : `Reply to ${mode}`, - }; - emit({ - type: 'TEXT_MESSAGE_START', - messageId: answer.id, - role: 'assistant', - }); - emit({ - type: 'TEXT_MESSAGE_CONTENT', - messageId: answer.id, - delta: answer.content, - }); - emit({ type: 'TEXT_MESSAGE_END', messageId: answer.id }); - let messages = [...body.messages, ...additions, answer]; - if (mode === 'Truncated prefix') messages = [...additions, answer]; - if (mode === 'Duplicate result') - messages.splice(messages.length - 1, 0, { - ...additions[1], - id: 'duplicate-result', - }); - if (mode === 'Orphan result') - messages.splice(messages.length - 1, 0, { - id: 'orphan', - role: 'tool', - toolCallId: 'orphan-call', - content: '{}', - }); - if (mode === 'Notice EOF') { - emit({ type: 'CUSTOM', name: 'unsupported', value: {} }); - response.end(); - return; - } - emit({ type: 'MESSAGES_SNAPSHOT', messages }); - if (mode === 'EOF') { - response.end(); - return; - } - const outcome = - mode === 'Pending tools' - ? { type: 'success', pendingToolCallIds: ['unobserved'] } - : mode === 'Decision' - ? { - type: 'interrupt', - interrupts: [{ id: 'pause', reason: 'approval' }], - } - : { type: 'success' }; - const terminal = () => { - emit({ - type: 'RUN_FINISHED', - threadId: body.threadId, - runId: mode === 'Wrong terminal' ? 'different-run' : body.runId, - outcome, - }); - response.end(); - }; - if (mode === 'Hold final') hold(terminal); - else terminal(); - }; - const round = (index, count, next) => { - const owner = { - id: `owner-${turn}-${index}`, - role: 'assistant', - content: '', - toolCalls: [], - }; - const readings = []; - for (let position = 0; position < count; position++) { - const id = `call-${turn}-${index}-${position}`, - location = - mode === 'Literal' - ? '' - : position - ? 'Boston' - : 'San Francisco'; - const name = mode === 'Unknown tool' ? 'unknown' : 'weather_card'; - const args = - mode === 'Malformed args' ? '{}' : JSON.stringify({ location }); - owner.toolCalls.push({ - id, - type: 'function', - function: { name, arguments: args }, - }); - readings.push({ id, location, args, name }); - } - emit({ - type: 'TEXT_MESSAGE_START', - messageId: owner.id, - role: 'assistant', - }); - const argumentTail = () => { - for (const reading of readings) { - emit({ - type: 'TOOL_CALL_ARGS', - toolCallId: reading.id, - delta: reading.args.slice(10), - }); - emit({ type: 'TOOL_CALL_END', toolCallId: reading.id }); - } - emit({ type: 'TEXT_MESSAGE_END', messageId: owner.id }); - additions.push(owner); - const results = () => { - for (const reading of readings) { - const content = JSON.stringify({ - location: reading.location, - temperatureF: mode === 'Malformed weather' ? 'hot' : 68, - conditions: 'Sunny', - humidity: 55, - windMph: 8, - }); - emit({ - type: 'TOOL_CALL_RESULT', - messageId: reading.id, - toolCallId: reading.id, - content, - role: 'tool', - }); - additions.push({ - id: - mode === 'Unstable result identity' - ? `graph-${reading.id}` - : reading.id, - role: 'tool', - toolCallId: reading.id, - content, - }); - } - if (mode === 'Hold result') hold(next); - else next(); - }; - if (mode === 'Hold call') hold(results); - else results(); - }; - for (const reading of readings) { - emit({ - type: 'TOOL_CALL_START', - toolCallId: reading.id, - toolCallName: reading.name, - parentMessageId: owner.id, - }); - emit({ - type: 'TOOL_CALL_ARGS', - toolCallId: reading.id, - delta: reading.args.slice(0, 10), - }); - } - if (mode === 'Hold args') hold(argumentTail); - else argumentTail(); - }; - if (mode === 'Zero') finish(); - else if (mode === 'Rounds') round(0, 1, () => round(1, 1, finish)); - else round(0, mode === 'Batch' ? 2 : 1, finish); - return true; - }; -} diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index fa16cc434..61b107ef0 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -226,6 +226,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/ag-ui/tool-views/react/', port: 4611, project: 'cockpit-ag-ui-tool-views-react', + backend: aimockAgUi('ag-ui/tool-views', 4611), }); if (topic === 'ag-ui-interrupts') return Object.freeze({ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 5d88d968c..478ba4d46 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -14,7 +14,6 @@ import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createAgUiToolViewsFixture } from './ag-ui-tool-views-fixture.mjs'; import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; @@ -81,10 +80,6 @@ const agUiJsonRenderFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'json-render' ? createAgUiJsonRenderFixture() : null; -const agUiToolViewsFixture = - configuration.adapter === 'ag-ui' && configuration.topic === 'tool-views' - ? createAgUiToolViewsFixture() - : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( @@ -181,11 +176,6 @@ const server = createServer(async (request, response) => { (await agUiJsonRenderFixture(request, response, pathname)) ) return; - if ( - agUiToolViewsFixture && - (await agUiToolViewsFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From db2bba20f5153c16fc2beb3fff6100f8ba554436 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:02:13 -0800 Subject: [PATCH 28/41] test(cockpit): run React ag-ui/json-render e2e and website preview on aimock + the real AG-UI server The scripted dashboard model of the Python tests becomes shared aimock fixtures; the spec reads each run's final snapshot from /__captures. The proxy gains setPath, replace and four more snapshot modes for the injected faults the authored fixture applied to the real graph's events. Co-Authored-By: Claude Opus 5.5 --- .../react-ag-ui-json-render-preview.spec.ts | 5 +- apps/website/playwright.config.ts | 4 +- apps/website/src/playwright-config.spec.ts | 4 +- .../angular/e2e/fixtures/json-render.json | 365 ++++++++++++++++++ .../react/e2e/global-setup-impl.ts | 6 + .../json-render/react/e2e/json-render.spec.ts | 82 +++- .../react/e2e/playwright.config.ts | 3 + cockpit/ag-ui/json-render/react/project.json | 47 ++- .../ag-ui-json-render-fixture.mjs | 212 ---------- .../react-cockpit/ag-ui-json-render-wire.py | 20 - scripts/react-cockpit/ag-ui-proxy.mjs | 32 +- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/serve.mjs | 10 - 13 files changed, 526 insertions(+), 265 deletions(-) create mode 100644 cockpit/ag-ui/json-render/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-json-render-fixture.mjs delete mode 100644 scripts/react-cockpit/ag-ui-json-render-wire.py diff --git a/apps/website/e2e/react-ag-ui-json-render-preview.spec.ts b/apps/website/e2e/react-ag-ui-json-render-preview.spec.ts index 9c6d4df73..2fe910eb6 100644 --- a/apps/website/e2e/react-ag-ui-json-render-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-json-render-preview.spec.ts @@ -19,7 +19,7 @@ test('canonical JSON Render Run keeps a retained layout bound to current native }) => { test.skip( Boolean(process.env['BASE_URL']), - 'The compiled offline producer is verified before deployment' + 'The real AG-UI server is verified before deployment' ); const origin = 'http://127.0.0.1:4612'; await request.post(origin + '/__reset'); @@ -175,7 +175,8 @@ test('switching away disposes a held server-tool stream and returning stays lazy await page.goto('/docs/react/ag-ui/guides/json-render?mode=run'); const frame = page.frameLocator('iframe'); await expect(frame.getByRole('status', { name: 'Conversation status' })).toHaveText('Ready.'); - await request.post(origin + '/__hold/result'); + // Hold the real stream after the first tool result (scripts/react-cockpit/ag-ui-proxy.mjs). + await request.post(origin + '/__hold/stream', { data: { afterType: 'TOOL_CALL_RESULT' } }); await frame.getByLabel('Message', { exact: true }).fill('full'); const response = page.waitForResponse( (candidate) => diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 0b6e14543..31fbef93c 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -385,9 +385,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-ag-ui-json-render-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4612', + url: 'http://127.0.0.1:4612/__ready', reuseExistingServer, timeout: 180_000, }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 77feefec7..98258a1bb 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -236,7 +236,7 @@ describe('Website Playwright configuration', () => { expect(reactServers.find(server => server.url === 'http://127.0.0.1:4619')?.command).toBe('node scripts/react-cockpit/serve.mjs render-computed-functions --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4614')?.command).toBe('node scripts/react-cockpit/serve.mjs render-spec-rendering --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4613')?.command).toBe('npx nx run cockpit-ag-ui-subagents-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent'); - expect(reactServers.find(server=>server.url==='http://127.0.0.1:4612')?.command).toBe('npx nx run cockpit-ag-ui-json-render-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent'); + expect(reactServers.find(server=>server.url==='http://127.0.0.1:4612/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend'); }); it('sends the Vercel automation bypass only when CI supplies it', () => { const withoutSecret = createWebsitePlaywrightConfig({}); @@ -510,7 +510,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent',url:'http://127.0.0.1:4641'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent',url:'http://127.0.0.1:4642'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent',url:'http://127.0.0.1:4643'}), - expect.objectContaining({command:'npx nx run cockpit-ag-ui-json-render-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent',url:'http://127.0.0.1:4612'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend',url:'http://127.0.0.1:4612/__ready'}), expect.objectContaining({command:'npx nx run cockpit-ag-ui-subagents-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent',url:'http://127.0.0.1:4613'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-spec-rendering --no-parent',url:'http://127.0.0.1:4614'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-state-management --no-parent',url:'http://127.0.0.1:4615'}), diff --git a/cockpit/ag-ui/json-render/angular/e2e/fixtures/json-render.json b/cockpit/ag-ui/json-render/angular/e2e/fixtures/json-render.json index 85d43fc47..33f7d50a9 100644 --- a/cockpit/ag-ui/json-render/angular/e2e/fixtures/json-render.json +++ b/cockpit/ag-ui/json-render/angular/e2e/fixtures/json-render.json @@ -42,6 +42,371 @@ } ] } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Airline operations dashboard" } + }, + { + "match": { "systemMessage": "Provide a brief (1-2 sentence) conversational summary" }, + "response": { "content": "The fictional dashboard is ready." } + }, + { + "match": { + "userMessage": "full", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { + "userMessage": "full", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "render_spec", + "arguments": "{\"root\": \"dashboard\", \"elements\": {\"dashboard\": {\"type\": \"dashboard_grid\", \"children\": [\"row\", \"table\"]}, \"row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"card\", \"trend\", \"airlines\"]}, \"card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On time\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"trend\": {\"type\": \"line_chart\", \"props\": {\"title\": \"Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airlines\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Airlines\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}, \"table\": {\"type\": \"data_grid\", \"props\": {\"title\": \"Disruptions\", \"rows\": {\"$state\": \"/recent_disruptions\"}, \"columns\": [\"flight_number\", \"type\", \"minutes\", \"route\", \"date\"]}}}}" + }, + { + "name": "query_airline_kpis", + "arguments": "{}" + }, + { + "name": "query_on_time_trend", + "arguments": "{\"months\": 6}" + }, + { + "name": "query_flights_by_airline", + "arguments": "{\"airlines\": [\"United\", \"Delta\"]}" + }, + { + "name": "query_recent_disruptions", + "arguments": "{\"limit\": 5}" + } + ] + } + }, + { + "match": { + "userMessage": "prose", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { + "userMessage": "prose", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "render_spec", + "arguments": "{\"root\": \"dashboard\", \"elements\": {\"dashboard\": {\"type\": \"dashboard_grid\", \"children\": [\"row\", \"table\"]}, \"row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"card\", \"trend\", \"airlines\"]}, \"card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On time\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"trend\": {\"type\": \"line_chart\", \"props\": {\"title\": \"Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airlines\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Airlines\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}, \"table\": {\"type\": \"data_grid\", \"props\": {\"title\": \"Disruptions\", \"rows\": {\"$state\": \"/recent_disruptions\"}, \"columns\": [\"flight_number\", \"type\", \"minutes\", \"route\", \"date\"]}}}}" + }, + { + "name": "query_airline_kpis", + "arguments": "{}" + }, + { + "name": "query_on_time_trend", + "arguments": "{\"months\": 6}" + }, + { + "name": "query_flights_by_airline", + "arguments": "{\"airlines\": [\"United\", \"Delta\"]}" + }, + { + "name": "query_recent_disruptions", + "arguments": "{\"limit\": 5}" + } + ], + "content": "Preparing a fictional dashboard." + } + }, + { + "match": { + "userMessage": "filter", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { + "userMessage": "filter", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "query_recent_disruptions", + "arguments": "{\"type\": \"cancelled\", \"limit\": 5}" + } + ] + } + }, + { + "match": { + "userMessage": "all-airlines", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { + "userMessage": "all-airlines", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "query_flights_by_airline", + "arguments": "{\"airlines\": []}" + } + ] + } + }, + { + "match": { + "userMessage": "structural", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { + "userMessage": "structural", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "render_spec", + "arguments": "{\"root\": \"new-card\", \"elements\": {\"new-card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Updated flights\", \"value\": {\"$state\": \"/flights_today/value\"}}}}}" + } + ] + } + }, + { + "match": { "userMessage": "zero" }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { "toolCallId": "rounds-2" }, + "response": { "content": "Dashboard prepared." } + }, + { + "match": { "toolCallId": "rounds-1" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "rounds-2" + } + ] + } + }, + { + "match": { + "userMessage": "rounds", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "rounds-1" + } + ] + } + }, + { + "match": { "toolCallId": "cap-1" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-2" + } + ] + } + }, + { + "match": { "toolCallId": "cap-2" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-3" + } + ] + } + }, + { + "match": { "toolCallId": "cap-3" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-4" + } + ] + } + }, + { + "match": { "toolCallId": "cap-4" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-5" + } + ] + } + }, + { + "match": { "toolCallId": "cap-5" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-6" + } + ] + } + }, + { + "match": { "toolCallId": "cap-6" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-7" + } + ] + } + }, + { + "match": { "toolCallId": "cap-7" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-8" + } + ] + } + }, + { + "match": { "toolCallId": "cap-8" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-9" + } + ] + } + }, + { + "match": { "toolCallId": "cap-9" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-10" + } + ] + } + }, + { + "match": { "toolCallId": "cap-10" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-11" + } + ] + } + }, + { + "match": { "toolCallId": "cap-11" }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-12" + } + ] + } + }, + { + "match": { + "userMessage": "cap", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "query_airline_kpis", + "arguments": "{}", + "id": "cap-1" + } + ] + } + }, + { + "match": { + "userMessage": "", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "render_spec", + "arguments": "{\"root\": \"dashboard\", \"elements\": {\"dashboard\": {\"type\": \"dashboard_grid\", \"children\": [\"row\", \"table\"]}, \"row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"card\", \"trend\", \"airlines\"]}, \"card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On time\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"trend\": {\"type\": \"line_chart\", \"props\": {\"title\": \"Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airlines\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Airlines\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}, \"table\": {\"type\": \"data_grid\", \"props\": {\"title\": \"Disruptions\", \"rows\": {\"$state\": \"/recent_disruptions\"}, \"columns\": [\"flight_number\", \"type\", \"minutes\", \"route\", \"date\"]}}}}" + }, + { + "name": "query_airline_kpis", + "arguments": "{}" + }, + { + "name": "query_on_time_trend", + "arguments": "{\"months\": 6}" + }, + { + "name": "query_flights_by_airline", + "arguments": "{\"airlines\": [\"United\", \"Delta\"]}" + }, + { + "name": "query_recent_disruptions", + "arguments": "{\"limit\": 5}" + } + ] + } + }, + { + "match": { + "userMessage": "", + "hasToolResult": true + }, + "response": { "content": "Dashboard prepared." } } ] } diff --git a/cockpit/ag-ui/json-render/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/json-render/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..cfb7820c0 --- /dev/null +++ b/cockpit/ag-ui/json-render/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/json-render', port: 4612 }); diff --git a/cockpit/ag-ui/json-render/react/e2e/json-render.spec.ts b/cockpit/ag-ui/json-render/react/e2e/json-render.spec.ts index 046d9b047..9416881e2 100644 --- a/cockpit/ag-ui/json-render/react/e2e/json-render.spec.ts +++ b/cockpit/ag-ui/json-render/react/e2e/json-render.spec.ts @@ -1,4 +1,66 @@ -import { test, expect, type Page } from '@playwright/test'; +import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/json-render/angular/e2e/fixtures (the +// scripted dashboard model of the Python tests) into the example's real AG-UI +// server. The recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) holds, +// fails or edits the next run; /__captures has what the server sent. +const edit = (request: APIRequestContext, ops: object[]) => + request.post('/__stream/edit', { data: { ops } }); +/** Where each held stage stops the real stream. */ +async function hold(request: APIRequestContext, stage: string) { + const afterType = { state: 'STATE_SNAPSHOT', result: 'TOOL_CALL_RESULT', final: 'MESSAGES_SNAPSHOT' }[stage]; + await request.post('/__hold/stream', { data: { afterType } }); +} +/** The final snapshot of each finished run, as the real server sent it. */ +async function finals(request: APIRequestContext) { + const captures: { events: { type: string; messages?: unknown[] }[] }[] = await ( + await request.get('/__captures') + ).json(); + return captures.map((capture) => capture.events.findLast((event) => event.type === 'MESSAGES_SNAPSHOT')!.messages!); +} +/** Arms a fault on the next run: injected protocol shapes a correct server won't produce. */ +async function fault(request: APIRequestContext, mode: string) { + const finished = (set: object) => edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set }]); + const snapshot = (name: string) => edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: name }]); + if (mode === 'http-error') await request.post('/__fail-stream'); + if (mode === 'run-error') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'RUN_ERROR', message: 'PRIVATE provider failure' } }, + { type: '^(?!RUN_STARTED$)', op: 'end' }, + ]); + if (mode === 'wrong-run') await finished({ runId: 'wrong-run' }); + if (mode === 'wrong-thread') await finished({ threadId: 'wrong-thread' }); + if (mode === 'eof') await edit(request, [{ type: '^RUN_FINISHED$', op: 'drop' }]); + if (mode === 'child') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'SUBAGENT_STARTED', subagentRunId: 'child', name: 'worker' } }, + ]); + if (mode === 'legacy') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'CUSTOM', name: 'on_interrupt', value: { note: 'legacy pause' } } }, + ]); + if (mode === 'notice-eof') + await edit(request, [ + { type: '^RUN_FINISHED$', op: 'insertBefore', event: { type: 'CUSTOM', name: 'unsupported', value: {} } }, + { type: '^RUN_FINISHED$', op: 'end' }, + ]); + if (mode === 'bad-state') + await edit(request, [ + { type: '^STATE_SNAPSHOT$', op: 'setPath', path: 'snapshot.on_time', value: { value: { invalid: true }, delta: null } }, + ]); + const snapshotModes: Record = { + 'truncated-prefix': 'from-human', + 'duplicate-result': 'duplicate-result', + 'orphan-result': 'orphan-result', + 'missing-result': 'missing-result', + 'wrong-result-id': 'wrong-result-id', + 'unknown-tool': 'unknown-tool', + }; + if (snapshotModes[mode]) await snapshot(snapshotModes[mode]); + if (mode === 'malformed-spec') + await edit(request, [ + { type: '^(TOOL_CALL_ARGS|MESSAGES_SNAPSHOT)$', op: 'replace', from: 'dashboard_grid', to: 'custom_html' }, + ]); +} const base = '/ag-ui/json-render/react/'; const rows = (page: Page) => page.locator('[data-dashboard-message]'); async function send(page: Page, text: string) { @@ -66,6 +128,7 @@ test('actual compiled graph and installed SDK retain all six views, canonical ba } const requests = await (await request.get('/__requests')).json(); expect(requests).toHaveLength(8); + const final = await finals(request); expect( new Set( requests.map( @@ -88,10 +151,10 @@ test('actual compiled graph and installed SDK retain all six views, canonical ba index ? nativeIds[index - 1] : [] ); if (index) { - expect(prefix).toHaveLength(requests[index - 1].final.length); + expect(prefix).toHaveLength(final[index - 1].length); for (const message of prefix) expect(message).toEqual( - requests[index - 1].final.find( + final[index - 1].find( (row: { id: string }) => row.id === message.id ) ); @@ -100,7 +163,7 @@ test('actual compiled graph and installed SDK retain all six views, canonical ba ); } const calls = new Set(); - for (const message of entry.final) { + for (const message of final[index] as { role: string; id: string; toolCallId?: string; toolCalls?: { id: string }[] }[]) { if (message.role === 'assistant') for (const call of message.toolCalls ?? []) calls.add(call.id); if (message.role === 'tool') { @@ -126,7 +189,7 @@ for (const stage of ['state', 'result', 'final']) request, }) => { await page.goto(base); - await request.post('/__hold/' + stage); + await hold(request, stage); await send(page, 'full'); await expect .poll( @@ -155,7 +218,7 @@ test('retained layout reads valid streamed shared-state updates while native com const chartData = page.locator('.dashboard-chart-data').first(); await chartData.locator('summary').click(); const originalChart = await chartData.elementHandle(); - await request.post('/__hold/final'); + await hold(request, 'final'); await send(page, 'filter'); await expect .poll(async () => (await (await request.get('/__lifetime')).json()).active) @@ -215,7 +278,7 @@ for (const mode of [ await page.goto(base); await send(page, 'full'); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText('Response complete.'); - await request.post('/__mode/' + mode); + await fault(request, mode); await send(page, 'full'); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText( 'Start a new conversation to continue.' @@ -235,7 +298,7 @@ for (const action of ['Stop', 'pagehide']) request, }) => { await page.goto(base); - await request.post('/__hold/result'); + await hold(request, 'result'); await send(page, 'full'); await expect .poll( @@ -351,9 +414,10 @@ test('the authored developer endpoint routes all native requests and stays out o .slice(0, -1) .map((message: { id: string }) => message.id) ).toEqual(priorIds); + const [first] = await finals(request); for (const message of wire[1].body.messages.slice(0, -1)) expect(message).toEqual( - wire[0].final.find((row: { id: string }) => row.id === message.id) + (first as { id: string }[]).find((row) => row.id === message.id) ); await expect(frame.locator('body')).not.toContainText('/developer-agent'); }); diff --git a/cockpit/ag-ui/json-render/react/e2e/playwright.config.ts b/cockpit/ag-ui/json-render/react/e2e/playwright.config.ts index 3c4d57fea..d5f5ecdfe 100644 --- a/cockpit/ag-ui/json-render/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/json-render/react/e2e/playwright.config.ts @@ -7,6 +7,9 @@ export default defineConfig({ expect: { timeout: 15000 }, use: { baseURL: 'http://127.0.0.1:4612', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/json-render/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs ag-ui-json-render', cwd: '../../../../..', diff --git a/cockpit/ag-ui/json-render/react/project.json b/cockpit/ag-ui/json-render/react/project.json index 8b87101cc..be021b4e6 100644 --- a/cockpit/ag-ui/json-render/react/project.json +++ b/cockpit/ag-ui/json-render/react/project.json @@ -3,7 +3,15 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/json-render/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-ag-ui-json-render-angular", + "cockpit-ag-ui-json-render-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { "test": { "executor": "@nx/vitest:test", @@ -15,26 +23,51 @@ "executor": "nx:run-commands", "cache": false, "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react"] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react" + ] + }, + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/ag-ui/json-render/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/ag-ui/json-render/react"], "options": { "command": "node scripts/react-cockpit/build.mjs ag-ui-json-render" } }, - "lint": { "executor": "@nx/eslint:lint" }, + "lint": { + "executor": "@nx/eslint:lint" + }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, "dependsOn": [ "build", - { "target": "smoke", "projects": ["cockpit-ag-ui-json-render-python"] } + { + "target": "smoke", + "projects": [ + "cockpit-ag-ui-json-render-python" + ] + } ], "options": { "config": "cockpit/ag-ui/json-render/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/scripts/react-cockpit/ag-ui-json-render-fixture.mjs b/scripts/react-cockpit/ag-ui-json-render-fixture.mjs deleted file mode 100644 index 088ff2dd6..000000000 --- a/scripts/react-cockpit/ag-ui-json-render-fixture.mjs +++ /dev/null @@ -1,212 +0,0 @@ -/** Offline installed-client proof server; never assembled into public assets. */ -import { execFile } from 'node:child_process'; -import { promisify } from 'node:util'; -import { resolve } from 'node:path'; -const run = promisify(execFile); -export function createAgUiJsonRenderFixture() { - let requests = [], - held, - mode = 'healthy', - holding = null; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - mode = 'healthy'; - holding = null; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__mode/')) { - mode = decodeURIComponent(pathname.slice(8)); - json(response, {}); - return true; - } - if (pathname.startsWith('/__hold/')) { - holding = pathname.slice(8); - json(response, {}); - return true; - } - if (!['/ag-ui/json-render/agent', '/developer-agent'].includes(pathname)) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - let body; - try { - body = JSON.parse(input); - } catch { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - requests.push({ path: pathname, method: request.method, body }); - const human = body.messages?.at(-1); - if ( - request.method !== 'POST' || - body.protocolVersion !== '1.0' || - typeof body.threadId !== 'string' || - typeof body.runId !== 'string' || - human?.role !== 'user' || - typeof human.content !== 'string' - ) { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - if (mode === 'http-error') { - json(response, { error: 'PRIVATE upstream failure' }, 500); - return true; - } - let wire; - try { - const source = resolve('cockpit/ag-ui/json-render/python'); - const pending = run( - resolve(source, '.venv/bin/python'), - [resolve('scripts/react-cockpit/ag-ui-json-render-wire.py')], - { cwd: source, maxBuffer: 32 * 1024 * 1024 } - ); - // The child receives the exact native request; no history or state is reconstructed. - pending.child.stdin.end(JSON.stringify(body)); - const result = await pending; - wire = JSON.parse(result.stdout); - } catch { - json(response, { error: 'Offline producer failed' }, 500); - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - }); - const emit = (event) => - response.write(`data: ${JSON.stringify(event)}\n\n`); - const final = wire.findLast((event) => event.type === 'MESSAGES_SNAPSHOT'); - const tail = final?.messages ?? []; - const tools = tail.filter((row) => row.role === 'tool'); - if (mode === 'run-error') - wire = [ - wire[0], - { type: 'RUN_ERROR', message: 'PRIVATE provider failure' }, - ]; - if (mode === 'wrong-run') - wire = wire.map((event) => - event.type === 'RUN_FINISHED' ? { ...event, runId: 'wrong-run' } : event - ); - if (mode === 'wrong-thread') - wire = wire.map((event) => - event.type === 'RUN_FINISHED' - ? { ...event, threadId: 'wrong-thread' } - : event - ); - if (mode === 'eof') - wire = wire.filter((event) => event.type !== 'RUN_FINISHED'); - if (mode === 'child') - wire.splice(1, 0, { - type: 'SUBAGENT_STARTED', - subagentRunId: 'child', - name: 'worker', - }); - if (mode === 'legacy') - wire.splice(1, 0, { - type: 'CUSTOM', - name: 'on_interrupt', - value: { note: 'legacy pause' }, - }); - if (mode === 'notice-eof') - wire = [ - ...wire.filter((event) => event.type !== 'RUN_FINISHED'), - { type: 'CUSTOM', name: 'unsupported', value: {} }, - ]; - if (mode === 'bad-state') - for (const event of wire) - if (event.type === 'STATE_SNAPSHOT') - event.snapshot.on_time = { value: { invalid: true }, delta: null }; - if (final && mode === 'truncated-prefix') - final.messages = tail.slice(body.messages.length - 1); - if (final && mode === 'duplicate-result') - final.messages = [ - ...tail.slice(0, -1), - { ...tools[0], id: 'duplicate-result' }, - tail.at(-1), - ]; - if (final && mode === 'orphan-result') - final.messages = [ - ...tail.slice(0, -1), - { id: 'orphan', role: 'tool', toolCallId: 'orphan', content: '{}' }, - tail.at(-1), - ]; - if (final && mode === 'missing-result') - final.messages = tail.filter((row) => row !== tools[0]); - if (final && mode === 'wrong-result-id') tools[0].id = 'wrong-result'; - if (final && mode === 'unknown-tool') { - const owner = tail.find( - (row) => row.role === 'assistant' && row.toolCalls?.length - ); - owner.toolCalls[0].function.name = 'unknown_dashboard_tool'; - } - if (mode === 'malformed-spec') - for (const event of wire) { - if ( - event.type === 'TOOL_CALL_ARGS' && - event.delta.includes('dashboard_grid') - ) - event.delta = event.delta.replace('dashboard_grid', 'custom_html'); - if (event.type === 'MESSAGES_SNAPSHOT') - for (const row of event.messages) - if (row.role === 'assistant') { - for (const call of row.toolCalls ?? []) - if (call.function.name === 'render_spec') - call.function.arguments = call.function.arguments.replace( - 'dashboard_grid', - 'custom_html' - ); - } - } - requests.at(-1).final = final?.messages; - requests.at(-1).state = wire.findLast( - (event) => event.type === 'STATE_SNAPSHOT' - )?.snapshot; - const finish = () => { - for (const event of wire) emit(event); - response.end(); - }; - if (holding) { - const stage = holding; - holding = null; - let split = - stage === 'state' - ? wire.findIndex((event) => event.type === 'STATE_SNAPSHOT') + 1 - : stage === 'result' - ? wire.findIndex((event) => event.type === 'TOOL_CALL_RESULT') + 1 - : stage === 'final' - ? wire.findLastIndex((event) => event.type === 'RUN_FINISHED') - : 1; - if (split < 1) split = 1; - for (const event of wire.slice(0, split)) emit(event); - wire = wire.slice(split); - const current = { response, finish }; - held = current; - response.once('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - }; -} diff --git a/scripts/react-cockpit/ag-ui-json-render-wire.py b/scripts/react-cockpit/ag-ui-json-render-wire.py deleted file mode 100644 index bd480f48e..000000000 --- a/scripts/react-cockpit/ag-ui-json-render-wire.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Offline browser proof: actual compiled graph, frozen providers and AG-UI bridge.""" -import asyncio -import json -from pathlib import Path -import sys - -sys.path.insert(0, str(Path('tests').resolve())) -from test_streaming import actual_graph -from ag_ui.core.types import RunAgentInput -from ag_ui_langgraph import LangGraphAgent - - -async def main(): - request = RunAgentInput.model_validate(json.loads(sys.stdin.read())) - events = [event.model_dump(mode='json', by_alias=True, exclude_none=True) - async for event in LangGraphAgent(name='json-render', graph=actual_graph()).run(request)] - print(json.dumps(events)) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index db0b35fb5..8f558523f 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -27,6 +27,8 @@ * drop remove the event * patch { set } merge fields into it * omit { keys } remove fields + * setPath { path, value } set a dotted path + * replace { from, to } replace text in it * outcome { mode } rewrite the real * interrupt of a * RUN_FINISHED outcome: @@ -41,6 +43,10 @@ * truncated-prefix, * old-pair, answer-only, * sent, drop-sent, + * from-human, + * missing-result, + * wrong-result-id, + * unknown-tool, * duplicate-result, * orphan-result, * unstable-result, @@ -83,8 +89,24 @@ function rewriteSnapshot(mode, event, body, options = {}) { else if (mode === 'drop-sent') { const sentIds = new Set(sent.map((message) => message.id)); return { ...event, messages: messages.filter((message) => !sentIds.has(message.id)) }; + } else if (mode === 'from-human') { + return { ...event, messages: messages.slice(sent.length - 1) }; + } else if (mode === 'missing-result') { + const index = messages.findIndex((message) => message.role === 'tool' && !sent.some((old) => old.id === message.id)); + messages.splice(index, 1); + } else if (mode === 'wrong-result-id') { + const index = messages.findIndex((message) => message.role === 'tool' && !sent.some((old) => old.id === message.id)); + messages[index] = { ...messages[index], id: 'wrong-result' }; + } else if (mode === 'unknown-tool') { + const index = messages.findIndex((message) => message.toolCalls?.length && !sent.some((old) => old.id === message.id)); + const [first, ...rest] = messages[index].toolCalls; + messages[index] = { + ...messages[index], + toolCalls: [{ ...first, function: { ...first.function, name: 'unknown_dashboard_tool' } }, ...rest], + }; } else if (mode === 'duplicate-result') { - const index = messages.findLastIndex((message) => message.role === 'tool'); + // A copy of this run's first result, just before the answer. + const index = messages.findIndex((message) => message.role === 'tool' && !sent.some((old) => old.id === message.id)); messages.splice(messages.length - 1, 0, { ...messages[index], id: 'duplicate-result' }); } else if (mode === 'unstable-result') { // Every saved result under another identity (with `persist`, the proxy @@ -222,6 +244,14 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re for (const key of op.keys ?? []) delete current[key]; } if (op.op === 'outcome') current = rewriteOutcome(op.mode, current); + if (op.op === 'setPath') { + current = structuredClone(current); + const keys = op.path.split('.'); + let target = current; + for (const key of keys.slice(0, -1)) target = target[key] ??= {}; + target[keys.at(-1)] = fill(op.value); + } + if (op.op === 'replace') current = JSON.parse(JSON.stringify(current).replaceAll(op.from, op.to)); if (op.op === 'snapshot') current = rewriteSnapshot(op.mode, current, body, op); if (op.op === 'insertBefore') write(fill(op.event)); if (op.op === 'insertAfter') after.push(fill(op.event)); diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 61b107ef0..0b511a83f 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -210,6 +210,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/ag-ui/json-render/react/', port: 4612, project: 'cockpit-ag-ui-json-render-react', + backend: aimockAgUi('ag-ui/json-render', 4612), }); if (topic === 'ag-ui-client-tools') return Object.freeze({ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 478ba4d46..a055305be 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -14,7 +14,6 @@ import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createAgUiJsonRenderFixture } from './ag-ui-json-render-fixture.mjs'; import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; @@ -76,10 +75,6 @@ const agUiSubagentsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'subagents' ? createAgUiSubagentsFixture() : null; -const agUiJsonRenderFixture = - configuration.adapter === 'ag-ui' && configuration.topic === 'json-render' - ? createAgUiJsonRenderFixture() - : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( @@ -171,11 +166,6 @@ const server = createServer(async (request, response) => { (await agUiSubagentsFixture(request, response, pathname)) ) return; - if ( - agUiJsonRenderFixture && - (await agUiJsonRenderFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From af87fbefc7276cb45181cc1e619db335f65abcaa Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:13:13 -0800 Subject: [PATCH 29/41] test(cockpit): run React ag-ui/subagents e2e and website preview on aimock + the real AG-UI server The scripted orchestrator and specialists of the Python tests become shared aimock fixtures (the orchestrator keys on each specialist's result); the Booking specialist's failure is a real provider error. The proxy gains dataPattern on ops, and a held run no longer blocks /__captures. Co-Authored-By: Claude Opus 5.5 --- .../e2e/react-ag-ui-subagents-preview.spec.ts | 5 +- apps/website/playwright.config.ts | 4 +- apps/website/src/playwright-config.spec.ts | 4 +- .../angular/e2e/fixtures/subagents.json | 292 ++++++++++++++++++ .../subagents/react/e2e/global-setup-impl.ts | 6 + .../subagents/react/e2e/playwright.config.ts | 3 + .../subagents/react/e2e/subagents.spec.ts | 93 +++++- cockpit/ag-ui/subagents/react/project.json | 47 ++- scripts/react-cockpit/ag-ui-proxy.mjs | 8 +- .../react-cockpit/ag-ui-subagents-fixture.mjs | 242 --------------- scripts/react-cockpit/ag-ui-subagents-wire.py | 31 -- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/serve.mjs | 10 - 13 files changed, 443 insertions(+), 303 deletions(-) create mode 100644 cockpit/ag-ui/subagents/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-subagents-fixture.mjs delete mode 100644 scripts/react-cockpit/ag-ui-subagents-wire.py diff --git a/apps/website/e2e/react-ag-ui-subagents-preview.spec.ts b/apps/website/e2e/react-ag-ui-subagents-preview.spec.ts index 88be65c51..1a67bdf79 100644 --- a/apps/website/e2e/react-ag-ui-subagents-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-subagents-preview.spec.ts @@ -137,7 +137,10 @@ test('switching away disposes a held child stream and returning stays lazy', asy await page.goto('/docs/react/ag-ui/guides/subagents?mode=run'); const frame = page.frameLocator('iframe'); await expect(frame.getByRole('status')).toHaveText('Ready.'); - await request.post(origin + '/__hold/child-text'); + // Hold the real stream after the first specialist text (scripts/react-cockpit/ag-ui-proxy.mjs). + await request.post(origin + '/__hold/stream', { + data: { afterPattern: '"type": ?"TEXT_MESSAGE_CONTENT".*"subagentRunId"|"subagentRunId".*"type": ?"TEXT_MESSAGE_CONTENT"' }, + }); await frame.getByLabel('Message', { exact: true }).fill('sequential'); const response = page.waitForResponse( (candidate) => diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 31fbef93c..3840349b2 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -392,9 +392,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-ag-ui-subagents-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4613', + url: 'http://127.0.0.1:4613/__ready', reuseExistingServer, timeout: 180_000, }, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 98258a1bb..bcc90b32f 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -235,7 +235,7 @@ describe('Website Playwright configuration', () => { expect(reactServers.find(server => server.url === 'http://127.0.0.1:4622')?.command).toBe('npx nx run cockpit-chat-interrupts-python:smoke && node scripts/react-cockpit/serve.mjs chat-interrupts --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4619')?.command).toBe('node scripts/react-cockpit/serve.mjs render-computed-functions --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4614')?.command).toBe('node scripts/react-cockpit/serve.mjs render-spec-rendering --no-parent'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4613')?.command).toBe('npx nx run cockpit-ag-ui-subagents-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4613/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent --backend'); expect(reactServers.find(server=>server.url==='http://127.0.0.1:4612/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend'); }); it('sends the Vercel automation bypass only when CI supplies it', () => { @@ -511,7 +511,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent',url:'http://127.0.0.1:4642'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent',url:'http://127.0.0.1:4643'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend',url:'http://127.0.0.1:4612/__ready'}), - expect.objectContaining({command:'npx nx run cockpit-ag-ui-subagents-python:smoke && node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent',url:'http://127.0.0.1:4613'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent --backend',url:'http://127.0.0.1:4613/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-spec-rendering --no-parent',url:'http://127.0.0.1:4614'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-state-management --no-parent',url:'http://127.0.0.1:4615'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-repeat-loops --no-parent',url:'http://127.0.0.1:4616'}), diff --git a/cockpit/ag-ui/subagents/angular/e2e/fixtures/subagents.json b/cockpit/ag-ui/subagents/angular/e2e/fixtures/subagents.json index 418c0e01c..5ffb3e010 100644 --- a/cockpit/ag-ui/subagents/angular/e2e/fixtures/subagents.json +++ b/cockpit/ag-ui/subagents/angular/e2e/fixtures/subagents.json @@ -87,6 +87,298 @@ "response": { "content": "All set — I researched both airports, found flights, and drafted an itinerary. Recommended plan: book the balanced mid-morning nonstop from LAX to JFK, arrive about two hours early, and take the AirTrain into the city. Pack for mild spring weather in New York." } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Trip planning" } + }, + { + "match": { + "systemMessage": "Booking Agent", + "userMessage": "failure:" + }, + "response": { + "error": { + "message": "private-provider-marker-14", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "Research Agent", + "userMessage": "(round 2)" + }, + "response": { "content": "A fictional specialist observation. (research, round 2)" } + }, + { + "match": { "systemMessage": "Research Agent" }, + "response": { "content": "A fictional specialist observation. (research, round 1)" } + }, + { + "match": { + "systemMessage": "Booking Agent", + "userMessage": "(round 2)" + }, + "response": { "content": "A fictional specialist observation. (booking, round 2)" } + }, + { + "match": { "systemMessage": "Booking Agent" }, + "response": { "content": "A fictional specialist observation. (booking, round 1)" } + }, + { + "match": { + "systemMessage": "Itinerary Agent", + "userMessage": "(round 2)" + }, + "response": { "content": "A fictional specialist observation. (itinerary, round 2)" } + }, + { + "match": { "systemMessage": "Itinerary Agent" }, + "response": { "content": "A fictional specialist observation. (itinerary, round 1)" } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "direct" + }, + "response": { "content": "A fictional parent summary." } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "batch", + "hasToolResult": true + }, + "response": { "content": "A fictional parent summary." } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "batch", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"research\", \"task_description\": \"batch: a fictional LAX to JFK trip.\"}" + }, + { + "name": "task", + "arguments": "{\"role\": \"booking\", \"task_description\": \"batch: a fictional LAX to JFK trip.\"}" + }, + { + "name": "task", + "arguments": "{\"role\": \"itinerary\", \"task_description\": \"batch: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "sequential", + "toolResultContains": "(itinerary, round 1)" + }, + "response": { "content": "A fictional parent summary." } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "sequential", + "toolResultContains": "(booking, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"itinerary\", \"task_description\": \"sequential: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "sequential", + "toolResultContains": "(research, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"booking\", \"task_description\": \"sequential: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "sequential", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"research\", \"task_description\": \"sequential: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "failure", + "toolResultContains": "(itinerary, round 1)" + }, + "response": { "content": "A fictional parent summary." } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "failure", + "toolResultContains": "(booking, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"itinerary\", \"task_description\": \"failure: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "failure", + "toolResultContains": "(research, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"booking\", \"task_description\": \"failure: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "failure", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"research\", \"task_description\": \"failure: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(itinerary, round 2)" + }, + "response": { "content": "A fictional parent summary." } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(booking, round 2)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"itinerary\", \"task_description\": \"rounds: a fictional LAX to JFK trip. (round 2)\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(research, round 2)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"booking\", \"task_description\": \"rounds: a fictional LAX to JFK trip. (round 2)\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(itinerary, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"research\", \"task_description\": \"rounds: a fictional LAX to JFK trip. (round 2)\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(booking, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"itinerary\", \"task_description\": \"rounds: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "toolResultContains": "(research, round 1)" + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"booking\", \"task_description\": \"rounds: a fictional LAX to JFK trip.\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Trip Planner Orchestrator", + "userMessage": "rounds", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "task", + "arguments": "{\"role\": \"research\", \"task_description\": \"rounds: a fictional LAX to JFK trip.\"}" + } + ] + } } ] } diff --git a/cockpit/ag-ui/subagents/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/subagents/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..eeae8e7de --- /dev/null +++ b/cockpit/ag-ui/subagents/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/subagents', port: 4613 }); diff --git a/cockpit/ag-ui/subagents/react/e2e/playwright.config.ts b/cockpit/ag-ui/subagents/react/e2e/playwright.config.ts index 319aabf13..017cec7bc 100644 --- a/cockpit/ag-ui/subagents/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/subagents/react/e2e/playwright.config.ts @@ -7,6 +7,9 @@ export default defineConfig({ expect: { timeout: 15000 }, use: { baseURL: 'http://127.0.0.1:4613', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/subagents/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs ag-ui-subagents', cwd: '../../../../..', diff --git a/cockpit/ag-ui/subagents/react/e2e/subagents.spec.ts b/cockpit/ag-ui/subagents/react/e2e/subagents.spec.ts index 110287b89..4eda3af2f 100644 --- a/cockpit/ag-ui/subagents/react/e2e/subagents.spec.ts +++ b/cockpit/ag-ui/subagents/react/e2e/subagents.spec.ts @@ -4,6 +4,77 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/subagents/angular/e2e/fixtures (the +// scripted orchestrator and specialists of the Python tests) into the +// example's real AG-UI server, whose SubagentEmittingAgent runs real child +// graphs. The recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) holds, +// fails or edits the next run; /__captures has what the server sent. +const edit = (request: APIRequestContext, ops: object[]) => + request.post('/__stream/edit', { data: { ops } }); +const childEvent = '"subagentRunId"'; +/** Where each held stage stops the real stream. */ +async function hold(request: APIRequestContext, stage: string) { + const data = { + 'child-text': { afterPattern: `"type": ?"TEXT_MESSAGE_CONTENT".*${childEvent}|${childEvent}.*"type": ?"TEXT_MESSAGE_CONTENT"` }, + 'child-finished': { afterType: 'SUBAGENT_FINISHED' }, + final: { afterType: 'MESSAGES_SNAPSHOT' }, + }[stage]; + await request.post('/__hold/stream', { data }); +} +/** + * Arms a scenario on the next run. `without-child-tokens` stands in for a + * nonstreaming specialist provider (no child text events, only the final + * result); the rest are injected protocol shapes a correct server won't + * produce. + */ +async function arm(request: APIRequestContext, name: string) { + const finished = (set: object) => edit(request, [{ type: '^RUN_FINISHED$', op: 'patch', set }]); + const snapshot = (snapshotMode: string) => edit(request, [{ type: '^MESSAGES_SNAPSHOT$', op: 'snapshot', mode: snapshotMode }]); + const firstChild = (set: object) => edit(request, [{ type: '^SUBAGENT_STARTED$', op: 'patch', once: true, set }]); + if (name === 'without-child-tokens') + await edit(request, [{ type: '^TEXT_MESSAGE_(START|CONTENT|END)$', dataPattern: childEvent, op: 'drop' }]); + if (name === 'http-error') await request.post('/__fail-stream'); + if (name === 'run-error') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'RUN_ERROR', message: 'PRIVATE provider failure' } }, + { type: '^(?!RUN_STARTED$)', op: 'end' }, + ]); + if (name === 'orphan-text') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'TEXT_MESSAGE_START', messageId: 'orphan-m1', role: 'assistant', subagentRunId: 'orphan' } }, + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'TEXT_MESSAGE_CONTENT', messageId: 'orphan-m1', delta: 'Unknown child', subagentRunId: 'orphan' } }, + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'TEXT_MESSAGE_END', messageId: 'orphan-m1', subagentRunId: 'orphan' } }, + ]); + if (name === 'wrong-run') await finished({ runId: 'wrong-run' }); + if (name === 'wrong-thread') await finished({ threadId: 'wrong-thread' }); + if (name === 'eof') await edit(request, [{ type: '^RUN_FINISHED$', op: 'drop' }]); + if (name === 'child') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'SUBAGENT_STARTED', subagentRunId: 'child', name: 'worker' } }, + ]); + if (name === 'legacy') + await edit(request, [ + { type: '^RUN_STARTED$', op: 'insertAfter', event: { type: 'CUSTOM', name: 'on_interrupt', value: { note: 'legacy pause' } } }, + ]); + if (name === 'notice-eof') + await edit(request, [ + { type: '^RUN_FINISHED$', op: 'insertBefore', event: { type: 'CUSTOM', name: 'unsupported', value: {} } }, + { type: '^RUN_FINISHED$', op: 'end' }, + ]); + const snapshotModes: Record = { + 'truncated-prefix': 'from-human', + 'duplicate-result': 'duplicate-result', + 'orphan-result': 'orphan-result', + 'missing-result': 'missing-result', + 'wrong-result-id': 'wrong-result-id', + 'unknown-tool': 'unknown-tool', + }; + if (snapshotModes[name]) await snapshot(snapshotModes[name]); + if (name === 'unfinished-child') await edit(request, [{ type: '^SUBAGENT_FINISHED$', op: 'drop', once: true }]); + if (name === 'wrong-parent') await firstChild({ parentToolCallId: 'foreign-call' }); + if (name === 'unknown-role') await firstChild({ name: 'unknown' }); + if (name === 'wrong-child-id') await firstChild({ subagentRunId: 'foreign-child' }); +} test('the authored developer endpoint routes all native requests and stays out of visible text', async ({ page, request, @@ -41,7 +112,7 @@ test('the authored developer endpoint routes all native requests and stays out o ) ); } - const wire = await (await request.get('/__requests')).json(); + const wire = await requests(request); expect(wire).toHaveLength(2); expect( wire.every( @@ -72,8 +143,16 @@ async function send(page: Page, text: string) { await page.getByRole('textbox', { name: 'Message', exact: true }).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); } +/** Agent requests, each with the final snapshot the real server sent for it. */ async function requests(request: APIRequestContext) { - return (await request.get('/__requests')).json(); + const wire = await (await request.get('/__requests')).json(); + const captures: { events: { type: string; messages?: unknown[] }[] }[] = await ( + await request.get('/__captures') + ).json(); + return wire.map((entry: object, index: number) => ({ + ...entry, + final: captures[index]?.events.findLast((event) => event.type === 'MESSAGES_SNAPSHOT')?.messages, + })); } async function nativeRows(page: Page) { return page @@ -158,7 +237,7 @@ test('real nonstreaming child providers retain final result fallback', async ({ page, request, }) => { - await request.post('/__mode/without-child-tokens'); + await arm(request, 'without-child-tokens'); await ready(page); await send(page, 'sequential'); await completed(page); @@ -172,7 +251,7 @@ for (const stage of ['child-text', 'child-finished', 'final']) page, request, }) => { - await request.post('/__hold/' + stage); + await hold(request, stage); await ready(page); await send(page, 'sequential'); await expect(page.locator('[data-child-id]').first()).toBeVisible(); @@ -211,7 +290,7 @@ for (const mode of [ 'orphan-text', ]) test(`${mode} requires New with no replay`, async ({ page, request }) => { - await request.post('/__mode/' + mode); + await arm(request, mode); await ready(page); await send(page, 'sequential'); await expect(page.getByRole('status')).toHaveText( @@ -244,7 +323,7 @@ for (const action of ['Stop', 'pagehide']) page, request, }) => { - await request.post('/__hold/child-text'); + await hold(request, 'child-text'); await ready(page); await send(page, 'sequential'); await expect(page.locator('[data-child-id]')).toHaveCount(1); @@ -275,7 +354,7 @@ for (const action of ['Stop', 'pagehide']) }); test('a truncated final snapshot cannot replace a previously confirmed canonical prefix', async ({ page, request }) => { await ready(page); await send(page, 'sequential'); await completed(page); - await request.post('/__mode/truncated-prefix'); await send(page, 'sequential'); + await arm(request, 'truncated-prefix'); await send(page, 'sequential'); await expect(page.getByRole('status')).toHaveText('Start a new conversation to continue.'); await expect(page.getByRole('button', { name: 'Send' })).toBeDisabled(); expect(await requests(request)).toHaveLength(2); diff --git a/cockpit/ag-ui/subagents/react/project.json b/cockpit/ag-ui/subagents/react/project.json index 14ce58dd3..5be6d5947 100644 --- a/cockpit/ag-ui/subagents/react/project.json +++ b/cockpit/ag-ui/subagents/react/project.json @@ -3,7 +3,15 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/subagents/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-ag-ui-subagents-angular", + "cockpit-ag-ui-subagents-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { "test": { "executor": "@nx/vitest:test", @@ -15,26 +23,51 @@ "executor": "nx:run-commands", "cache": false, "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react"] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react" + ] + }, + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/ag-ui/subagents/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/ag-ui/subagents/react"], "options": { "command": "node scripts/react-cockpit/build.mjs ag-ui-subagents" } }, - "lint": { "executor": "@nx/eslint:lint" }, + "lint": { + "executor": "@nx/eslint:lint" + }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, "dependsOn": [ "build", - { "target": "smoke", "projects": ["cockpit-ag-ui-subagents-python"] } + { + "target": "smoke", + "projects": [ + "cockpit-ag-ui-subagents-python" + ] + } ], "options": { "config": "cockpit/ag-ui/subagents/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index 8f558523f..26fd42ea6 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -23,7 +23,8 @@ * POST /__fail-stream next agent POST answers 500 unforwarded (the * server is unavailable) * POST /__stream/edit body { ops }: edits to the next run stream, each - * { type: regex on the event type, op, once? }: + * { type: regex on the event type, op, once?, + * dataPattern? (regex on the event's JSON) }: * drop remove the event * patch { set } merge fields into it * omit { keys } remove fields @@ -232,6 +233,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re let current = event; for (const op of edits) { if ((op.once && op.used) || !op.pattern.test(event.type)) continue; + if (op.dataPattern && !new RegExp(op.dataPattern).test(JSON.stringify(event))) continue; op.used = true; if (op.op === 'drop') return true; if (op.op === 'end') { @@ -272,6 +274,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re release() { if (held === held_) held = undefined; holding = false; + pendingCaptures.add(settled); for (const event of buffered.splice(0)) if (!forward(event)) return; if (ended) finish(); }, @@ -304,6 +307,9 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re armed = false; holding = true; held = held_; + // A held run is not "in flight" for /__captures: it waits on a + // release the test sends later. + pendingCaptures.delete(settled); } } }); diff --git a/scripts/react-cockpit/ag-ui-subagents-fixture.mjs b/scripts/react-cockpit/ag-ui-subagents-fixture.mjs deleted file mode 100644 index ab2751c07..000000000 --- a/scripts/react-cockpit/ag-ui-subagents-fixture.mjs +++ /dev/null @@ -1,242 +0,0 @@ -/** Offline installed-client proof server; never assembled into public assets. */ -import { execFile } from 'node:child_process'; -import { promisify } from 'node:util'; -import { resolve } from 'node:path'; -const run = promisify(execFile); -export function createAgUiSubagentsFixture() { - let requests = [], - held, - mode = 'healthy', - holding = null; - const json = (response, value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value)); - }; - return async function handle(request, response, pathname) { - if (pathname === '/__reset') { - held?.response.end(); - held = undefined; - requests = []; - mode = 'healthy'; - holding = null; - json(response, {}); - return true; - } - if (pathname === '/__requests') { - json(response, requests); - return true; - } - if (pathname === '/__lifetime') { - json(response, { active: Boolean(held) }); - return true; - } - if (pathname === '/__release') { - const current = held; - held = undefined; - if (current && !current.response.destroyed) current.finish(); - json(response, {}); - return true; - } - if (pathname.startsWith('/__mode/')) { - mode = decodeURIComponent(pathname.slice(8)); - json(response, {}); - return true; - } - if (pathname.startsWith('/__hold/')) { - holding = pathname.slice(8); - json(response, {}); - return true; - } - if (!['/ag-ui/subagents/agent', '/developer-agent'].includes(pathname)) - return false; - let input = ''; - for await (const chunk of request) input += chunk; - let body; - try { - body = JSON.parse(input); - } catch { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - requests.push({ path: pathname, method: request.method, body }); - const human = body.messages?.at(-1); - if ( - request.method !== 'POST' || - body.protocolVersion !== '1.0' || - typeof body.threadId !== 'string' || - typeof body.runId !== 'string' || - human?.role !== 'user' || - typeof human.content !== 'string' - ) { - json(response, { error: 'Invalid native request' }, 400); - return true; - } - if (mode === 'http-error') { - json(response, { error: 'PRIVATE upstream failure' }, 500); - return true; - } - let wire; - try { - const source = resolve('cockpit/ag-ui/subagents/python'); - const pending = run( - resolve(source, '.venv/bin/python'), - [ - resolve('scripts/react-cockpit/ag-ui-subagents-wire.py'), - ...(mode === 'without-child-tokens' - ? ['--without-child-tokens'] - : []), - ], - { cwd: source, maxBuffer: 32 * 1024 * 1024 } - ); - // The child receives the exact native request; no history or state is reconstructed. - pending.child.stdin.end(JSON.stringify(body)); - const result = await pending; - wire = JSON.parse(result.stdout); - } catch { - json(response, { error: 'Offline producer failed' }, 500); - return true; - } - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - }); - const emit = (event) => - response.write(`data: ${JSON.stringify(event)}\n\n`); - const final = wire.findLast((event) => event.type === 'MESSAGES_SNAPSHOT'); - const tail = final?.messages ?? []; - const tools = tail.filter((row) => row.role === 'tool'); - if (mode === 'run-error') - wire = [ - wire[0], - { type: 'RUN_ERROR', message: 'PRIVATE provider failure' }, - ]; - if (mode === 'orphan-text') - wire.splice( - 1, - 0, - { - type: 'TEXT_MESSAGE_START', - messageId: 'orphan-m1', - role: 'assistant', - subagentRunId: 'orphan', - }, - { - type: 'TEXT_MESSAGE_CONTENT', - messageId: 'orphan-m1', - delta: 'Unknown child', - subagentRunId: 'orphan', - }, - { - type: 'TEXT_MESSAGE_END', - messageId: 'orphan-m1', - subagentRunId: 'orphan', - } - ); - if (mode === 'wrong-run') - wire = wire.map((event) => - event.type === 'RUN_FINISHED' ? { ...event, runId: 'wrong-run' } : event - ); - if (mode === 'wrong-thread') - wire = wire.map((event) => - event.type === 'RUN_FINISHED' - ? { ...event, threadId: 'wrong-thread' } - : event - ); - if (mode === 'eof') - wire = wire.filter((event) => event.type !== 'RUN_FINISHED'); - if (mode === 'child') - wire.splice(1, 0, { - type: 'SUBAGENT_STARTED', - subagentRunId: 'child', - name: 'worker', - }); - if (mode === 'legacy') - wire.splice(1, 0, { - type: 'CUSTOM', - name: 'on_interrupt', - value: { note: 'legacy pause' }, - }); - if (mode === 'notice-eof') - wire = [ - ...wire.filter((event) => event.type !== 'RUN_FINISHED'), - { type: 'CUSTOM', name: 'unsupported', value: {} }, - ]; - if (final && mode === 'truncated-prefix') - final.messages = tail.slice(body.messages.length - 1); - if (final && mode === 'duplicate-result') - final.messages = [ - ...tail.slice(0, -1), - { ...tools[0], id: 'duplicate-result' }, - tail.at(-1), - ]; - if (final && mode === 'orphan-result') - final.messages = [ - ...tail.slice(0, -1), - { id: 'orphan', role: 'tool', toolCallId: 'orphan', content: '{}' }, - tail.at(-1), - ]; - if (final && mode === 'missing-result') - final.messages = tail.filter((row) => row !== tools[0]); - if (final && mode === 'wrong-result-id') tools[0].id = 'wrong-result'; - if (final && mode === 'unknown-tool') { - const owner = tail.find( - (row) => row.role === 'assistant' && row.toolCalls?.length - ); - owner.toolCalls[0].function.name = 'unknown_dashboard_tool'; - } - if (mode === 'unfinished-child') { - const first = wire.findIndex( - (event) => event.type === 'SUBAGENT_FINISHED' - ); - if (first >= 0) wire.splice(first, 1); - } - if ( - mode === 'wrong-parent' || - mode === 'unknown-role' || - mode === 'wrong-child-id' - ) { - const child = wire.find((event) => event.type === 'SUBAGENT_STARTED'); - if (child) { - if (mode === 'wrong-parent') child.parentToolCallId = 'foreign-call'; - if (mode === 'unknown-role') child.name = 'unknown'; - if (mode === 'wrong-child-id') child.subagentRunId = 'foreign-child'; - } - } - requests.at(-1).final = final?.messages; - requests.at(-1).state = wire.findLast( - (event) => event.type === 'STATE_SNAPSHOT' - )?.snapshot; - const finish = () => { - for (const event of wire) emit(event); - response.end(); - }; - if (holding) { - const stage = holding; - holding = null; - let split = - stage === 'state' - ? wire.findIndex((event) => event.type === 'STATE_SNAPSHOT') + 1 - : stage === 'child-text' - ? wire.findIndex( - (event) => - event.type === 'TEXT_MESSAGE_CONTENT' && event.subagentRunId - ) + 1 - : stage === 'child-finished' - ? wire.findIndex((event) => event.type === 'SUBAGENT_FINISHED') + 1 - : stage === 'result' - ? wire.findIndex((event) => event.type === 'TOOL_CALL_RESULT') + 1 - : stage === 'final' - ? wire.findLastIndex((event) => event.type === 'RUN_FINISHED') - : 1; - if (split < 1) split = 1; - for (const event of wire.slice(0, split)) emit(event); - wire = wire.slice(split); - const current = { response, finish }; - held = current; - response.once('close', () => { - if (held === current) held = undefined; - }); - } else finish(); - return true; - }; -} diff --git a/scripts/react-cockpit/ag-ui-subagents-wire.py b/scripts/react-cockpit/ag-ui-subagents-wire.py deleted file mode 100644 index 5c4f609fe..000000000 --- a/scripts/react-cockpit/ag-ui-subagents-wire.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Offline proof: exact native request through the real graph and child bridge.""" -import asyncio -from contextlib import nullcontext -import json -from pathlib import Path -import sys -from unittest.mock import patch - -sys.path.insert(0, str(Path('tests').resolve())) -from test_streaming import actual_modules, run_actual, FrozenSubagentModel -from ag_ui.core import RunAgentInput - - -async def main(): - body = RunAgentInput.model_validate(json.load(sys.stdin)) - module, emitter, _ = actual_modules() - agent = emitter.SubagentEmittingAgent(name='subagents', graph=module.graph) - events = [] - provider = patch.object(module, 'ChatOpenAI', side_effect=lambda **kw: FrozenSubagentModel(disable_streaming=True)) if '--without-child-tokens' in sys.argv else nullcontext() - with provider: - try: - await run_actual(agent, body, captured=events) - except ValueError: - # Actual child failure evidence is emitted before the provider - # exception propagates. Never fabricate a successful root terminal. - if not any(event['type'] == 'SUBAGENT_ERROR' for event in events): - raise - print(json.dumps(events)) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 0b511a83f..c1ceb0204 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -201,6 +201,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { base: '/ag-ui/subagents/react/', port: 4613, project: 'cockpit-ag-ui-subagents-react', + backend: aimockAgUi('ag-ui/subagents', 4613), }); if (topic === 'ag-ui-json-render') return Object.freeze({ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index a055305be..f0df05f75 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -14,7 +14,6 @@ import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; -import { createAgUiSubagentsFixture } from './ag-ui-subagents-fixture.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); @@ -71,10 +70,6 @@ const agUiProxy = configuration.backend?.kind === 'aimock-ag-ui' ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) : null; -const agUiSubagentsFixture = - configuration.adapter === 'ag-ui' && configuration.topic === 'subagents' - ? createAgUiSubagentsFixture() - : null; const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( @@ -161,11 +156,6 @@ const server = createServer(async (request, response) => { return response.end(); } } - if ( - agUiSubagentsFixture && - (await agUiSubagentsFixture(request, response, pathname)) - ) - return; if ( pathname.startsWith('/api/') || pathname.startsWith('/developer-api/') || From 87f655d0095c97afd2a9c7af0dbfd5c9663c5dae Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:15:14 -0800 Subject: [PATCH 30/41] docs(cockpit): record batch 6a (AG-UI) aimock migration and the 6b split Co-Authored-By: Claude Opus 5.5 --- .../specs/2026-11-18-react-cockpit-aimock-design.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index a4b364283..ebe505e90 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -182,7 +182,8 @@ example needs that the pilot proxy doesn't yet provide. | 4b (done) | langgraph/{client-tools, deployment-runtime} | `/__hold/state-write`, `/__fail/state-write`, `/__thread/`, `/__stream/edit` list ops (`append`, `truncate`, `duplicateLast`, `copy`) | client tool rounds key on prompt + `hasToolResult`; a second same-thread turn uses its own prompt (`Weather again`), because aimock fixtures are stateless. "No final values" also strips the markers from the history read the session falls back to | | 4c (done) | langgraph/{time-travel, durable-execution} | `/__history-copies`, `/__state/unsafe`, `/__thread//state` writes, `/__stream/edit { state, anonymous, unlessKey }`, `/__hold/stream { afterPattern, end }`, `/__run-status/`, `/__history-values { entry }` | time-travel reads checkpoint ids from real history and thread reads, not authored ids (each turn saves input, start and done checkpoints). The competing tip is a real state write. durable-execution's uncertain outcomes end the browser's stream early, keep history inconclusive, and "publish" the saved outcome through a one-shot history rewrite | | 5 (done) | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__stream/cut` (memory) | scenarios are tagged prompts on the shared `da-*.json` fixtures. The planning and filesystem integration specs (vitest) run on `createAimockBackend` plus an in-process proxy. The React memory fixture also backed the Angular memory e2e: that now uses `createGlobalSetup` with `langgraph dev` on 6313 and the recording proxy on 5313, where the Angular dev server's `/api` proxy points, which retires its owned-process lifecycle helpers and the `compiledMemoryBackend` guard in `e2e-wiring.spec.ts`. The memory spec reads stream bodies from `/__captures[].sse` (Chromium doesn't keep a long-lived event-stream body) | -| 6 | ag-ui/{streaming, interrupts, tool-views, json-render, subagents, client-tools, a2ui}, chat/a2ui | AG-UI wire (`createAgUiGlobalSetup`, uvicorn), `/__mode/*`, `/__fail-transport`, A2UI lifetime receipts | needs an AG-UI flavor of the proxy; the A2UI fixtures also feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`) and move last | +| 6a (done) | ag-ui/{streaming, interrupts, tool-views, json-render, subagents} | the AG-UI flavor: `aimock-ag-ui` backends run the example's uvicorn `src/server.py` (`agentPath` picks the route, e.g. interrupts' `/agent/native`), `createAgUiBackendGlobalSetup` in the harness, and `scripts/react-cockpit/ag-ui-proxy.mjs` (`/__requests`, `/__captures`, `/__lifetime`, `/__hold/stream { afterType, afterPattern }`, `/__release`, `/__fail-stream`, `/__stream/edit` with drop, patch, omit, setPath, replace, insertBefore/After, end, `outcome` and `snapshot` modes, `dataPattern`, `once`, `persist`) | streaming's fixture was pure authored JavaScript; the others ran the real graph through a wire script and rewrote its events, so their scripted models (from the Python tests) become shared fixtures and their rewrites become proxy edits (injected data). Real behavior replaces several: a model failure drops the stream (no RUN_ERROR), native interrupts carry LangGraph's own interrupt id, unknown tools and argument-less calls fail in the real ToolNode, and a Booking specialist's provider error is a real child failure. The client returns the server's STATE_SNAPSHOT as the next run's `state`; specs assert that instead of `{}` | +| 6b | ag-ui/{client-tools, a2ui}, chat/a2ui | client-tools' fixture fronts the shared react-parity server (`fixtures/react-parity/native-ag-ui-client-tools`, also used by `scripts/react-parity` verification), whose causal checks read the model's received messages: needs a fixed aimock port and the aimock journal (`GET /__aimock/journal`). The A2UI fixtures feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`, the native probes) | | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | | n/a | render/* (6) | none | `adapter: 'none'`, no model backend | From 05150f788379160e5126785a2f6fec5488ccd285 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:43:21 -0800 Subject: [PATCH 31/41] test(cockpit): AG-UI identities and e2e setups carry their aimock backends in the frontend matrix spec Co-Authored-By: Claude Opus 5.5 --- scripts/cockpit-frontend-matrix.spec.mjs | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 5ec7a27e1..add728ab2 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -330,14 +330,14 @@ test('Spec Rendering has a closed local fifteenth React identity', async () => { }); test('Subagents has a closed fourteenth React identity', async () => { const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); - assert.deepEqual(reactCockpitConfiguration('ag-ui-subagents'), { topic: 'subagents', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/subagents/react', base: '/ag-ui/subagents/react/', port: 4613, project: 'cockpit-ag-ui-subagents-react' }); + assert.deepEqual(reactCockpitConfiguration('ag-ui-subagents'), { topic: 'subagents', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/subagents/react', base: '/ag-ui/subagents/react/', port: 4613, project: 'cockpit-ag-ui-subagents-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/subagents/python', 'fixtures': 'cockpit/ag-ui/subagents/angular/e2e/fixtures', 'backendPort': 5613, 'agentPath': '/agent'} }); for (const key of ['subagents', 'ag-ui/subagents', '../ag-ui-subagents']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); }); test('JSON Render has a closed thirteenth React identity and all routes remain unique',async()=>{ const {reactCockpitConfiguration}=await import('./react-cockpit/configuration.mjs'); assert.deepEqual(reactCockpitConfiguration('ag-ui-json-render'),{ - topic:'json-render',adapter:'ag-ui',appPath:'cockpit/ag-ui/json-render/react',base:'/ag-ui/json-render/react/',port:4612,project:'cockpit-ag-ui-json-render-react', + topic:'json-render',adapter:'ag-ui',appPath:'cockpit/ag-ui/json-render/react',base:'/ag-ui/json-render/react/',port:4612,project:'cockpit-ag-ui-json-render-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/json-render/python', 'fixtures': 'cockpit/ag-ui/json-render/angular/e2e/fixtures', 'backendPort': 5612, 'agentPath': '/agent'}, }); const configurations=['streaming','interrupts','memory','client-tools','persistence','durable-execution','subgraphs','time-travel','deployment-runtime','ag-ui-streaming','ag-ui-interrupts','ag-ui-tool-views','ag-ui-json-render','ag-ui-subagents'].map(reactCockpitConfiguration); for(const field of ['appPath','base','port','project'])assert.equal(new Set(configurations.map(configuration=>configuration[field])).size,14); @@ -348,7 +348,7 @@ test('AG-UI Tool Views has a closed twelfth identity with no overlapping runtime const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); assert.deepEqual(reactCockpitConfiguration('ag-ui-tool-views'), { topic: 'tool-views', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/tool-views/react', - base: '/ag-ui/tool-views/react/', port: 4611, project: 'cockpit-ag-ui-tool-views-react', + base: '/ag-ui/tool-views/react/', port: 4611, project: 'cockpit-ag-ui-tool-views-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/tool-views/python', 'fixtures': 'cockpit/ag-ui/tool-views/angular/e2e/fixtures', 'backendPort': 5611, 'agentPath': '/agent'}, }); const configurations = [ 'streaming', 'interrupts', 'memory', 'client-tools', 'persistence', @@ -371,7 +371,7 @@ test('AG-UI Streaming has a closed React identity distinct from LangGraph Stream appPath: 'cockpit/ag-ui/streaming/react', base: '/ag-ui/streaming/react/', port: 4609, - project: 'cockpit-ag-ui-streaming-react', + project: 'cockpit-ag-ui-streaming-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/streaming/python', 'fixtures': 'cockpit/ag-ui/streaming/angular/e2e/fixtures', 'backendPort': 5609, 'agentPath': '/agent'}, }); assert.equal( reactCockpitConfiguration('streaming').appPath, @@ -398,7 +398,7 @@ test('AG-UI Interrupts has a closed identity and all eleven React previews remai appPath: 'cockpit/ag-ui/interrupts/react', base: '/ag-ui/interrupts/react/', port: 4610, - project: 'cockpit-ag-ui-interrupts-react', + project: 'cockpit-ag-ui-interrupts-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/interrupts/python', 'fixtures': 'cockpit/ag-ui/interrupts/angular/e2e/fixtures', 'backendPort': 5610, 'agentPath': '/agent/native'}, }); assert.equal( reactCockpitConfiguration('interrupts').appPath, @@ -723,18 +723,20 @@ test('aimock-backed React e2e setups match their closed configuration backends', const setups = walk('cockpit'); assert.ok(setups.length > 0); for (const file of setups) { - const call = /reactAimockGlobalSetup\(\{ example: '([^']+)', port: (\d+) \}\)/.exec(readFileSync(file, 'utf8')); - assert.ok(call, `${file} uses reactAimockGlobalSetup`); - const [, example, port] = call; + const call = /reactAimock(AgUi)?GlobalSetup\(\{ example: '([^']+)', port: (\d+) \}\)/.exec(readFileSync(file, 'utf8')); + assert.ok(call, `${file} uses reactAimockGlobalSetup or reactAimockAgUiGlobalSetup`); + const [, agUi, example, port] = call; const serve = /serve\.mjs ([a-z0-9-]+)/.exec(readFileSync(file.replace('global-setup-impl.ts', 'playwright.config.ts'), 'utf8'))[1]; const configuration = reactCockpitConfiguration(serve); assert.equal(configuration.port, Number(port), file); - assert.deepEqual(configuration.backend, { - kind: 'aimock-langgraph', - python: `cockpit/${example}/python`, - fixtures: `cockpit/${example}/angular/e2e/fixtures`, - langgraphPort: Number(port) + 1000, - }, file); + const shared = { python: `cockpit/${example}/python`, fixtures: `cockpit/${example}/angular/e2e/fixtures` }; + if (agUi) { + const { aimockPort, agentPath, ...rest } = configuration.backend; + assert.deepEqual(rest, { kind: 'aimock-ag-ui', ...shared, backendPort: Number(port) + 1000 }, file); + if (aimockPort !== undefined) assert.equal(aimockPort, Number(port) + 2000, file); + assert.match(agentPath, /^\/agent(\/[a-z]+)?$/, file); + } else + assert.deepEqual(configuration.backend, { kind: 'aimock-langgraph', ...shared, langgraphPort: Number(port) + 1000 }, file); assert.ok(existsSync(configuration.backend.fixtures), configuration.backend.fixtures); } }); From 5264a4733fd1c7177a949cf14bde507790efb163 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:28:35 -0800 Subject: [PATCH 32/41] test(cockpit): run React ag-ui/client-tools e2e and website preview on aimock + the real AG-UI server The cockpit scenario of the react-parity client-tools server becomes shared aimock fixtures; that scenario and the React fixture wrapper, its preparation script, target, specs and CI steps are removed (the parity server itself stays for scripts/react-parity verification). The causal checks read what the model received from aimock's request journal: AG-UI backends get a fixed aimock port (app port + 2000) and the proxy serves /__model-requests. The proxy's /__requests entries now carry live events, held and closed, and holds take beforeType, persist and fromRequest. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 10 +- .../docs/react/ag-ui/guides/client-tools.mdx | 2 +- .../react-ag-ui-client-tools-preview.spec.ts | 28 ++- apps/website/playwright.config.ts | 7 +- apps/website/src/playwright-config.spec.ts | 10 +- .../angular/e2e/fixtures/client-tools.json | 195 ++++++++++++++++++ cockpit/ag-ui/client-tools/react/README.md | 2 +- .../react/e2e/client-tools.spec.ts | 46 +++-- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 7 +- cockpit/ag-ui/client-tools/react/project.json | 33 ++- cockpit/shared/react-aimock-setup.ts | 5 +- .../compiled_worker.py | 70 +------ .../native-ag-ui-client-tools/server.mjs | 4 - .../src/ag-ui-global-setup-factory.ts | 10 +- libs/e2e-harness/src/aimock-runner.ts | 7 +- scripts/ci-workflow.spec.mjs | 2 +- .../ag-ui-client-tools-configuration.spec.mjs | 8 + .../ag-ui-client-tools-fixture.mjs | 76 ------- .../ag-ui-client-tools-fixture.spec.mjs | 98 --------- .../ag-ui-client-tools-preparation.spec.mjs | 43 ---- scripts/react-cockpit/ag-ui-proxy.mjs | 59 +++++- scripts/react-cockpit/aimock-backend.mjs | 2 +- scripts/react-cockpit/configuration.mjs | 5 +- .../prepare-ag-ui-client-tools-fixture.mjs | 23 --- scripts/react-cockpit/serve.mjs | 8 +- 26 files changed, 363 insertions(+), 403 deletions(-) create mode 100644 cockpit/ag-ui/client-tools/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-client-tools-fixture.mjs delete mode 100644 scripts/react-cockpit/ag-ui-client-tools-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs delete mode 100644 scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 815937f88..8da92d33c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -176,7 +176,7 @@ jobs: cache: npm - run: npm ci - name: Test CI scope classifier - run: node --test scripts/configure-ci-apt.spec.mjs scripts/install-playwright-browser.spec.mjs scripts/commit-staged-via-api.spec.mjs scripts/ci-queue-evidence.spec.mjs scripts/ci-scope.spec.mjs scripts/cockpit-dependencies.spec.mjs scripts/cockpit-matrix.spec.mjs scripts/cockpit-frontend-matrix.spec.mjs scripts/cockpit-ports.spec.mjs scripts/cockpit-runtime-bridge-coverage.spec.mjs scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs scripts/react-cockpit/chat-theming-configuration.spec.mjs scripts/react-cockpit/consumer-preparation.spec.mjs scripts/website-angular-startup.spec.mjs scripts/website-agents-startup.spec.mjs + run: node --test scripts/configure-ci-apt.spec.mjs scripts/install-playwright-browser.spec.mjs scripts/commit-staged-via-api.spec.mjs scripts/ci-queue-evidence.spec.mjs scripts/ci-scope.spec.mjs scripts/cockpit-dependencies.spec.mjs scripts/cockpit-matrix.spec.mjs scripts/cockpit-frontend-matrix.spec.mjs scripts/cockpit-ports.spec.mjs scripts/cockpit-runtime-bridge-coverage.spec.mjs scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs scripts/react-cockpit/chat-theming-configuration.spec.mjs scripts/react-cockpit/consumer-preparation.spec.mjs scripts/website-angular-startup.spec.mjs scripts/website-agents-startup.spec.mjs - name: Test Debug static configuration without Python workers run: node --test --test-name-pattern='^debug ' scripts/react-cockpit/chat-debug-configuration.spec.mjs - name: Validate CI workflow guards @@ -1112,11 +1112,6 @@ jobs: run: | npx nx test "${{ matrix.cap.angular }}" --reporters=default --skip-nx-cache npx nx lint "${{ matrix.cap.angular }}" --skip-nx-cache - - name: Verify canonical client tools fixture ownership - if: matrix.cap.angular == 'cockpit-ag-ui-client-tools-react' - run: | - npx nx run cockpit-ag-ui-client-tools-react:prepare-fixture --skip-nx-cache - node --test scripts/react-cockpit/ag-ui-client-tools-fixture.spec.mjs - name: Prepare native A2UI proof ownership if: matrix.cap.angular == 'cockpit-ag-ui-a2ui-react' run: | @@ -1227,9 +1222,6 @@ jobs: uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 with: python-version: '3.12' - - name: Prepare frozen canonical client tools browser producer - if: matrix.suite == 'ag-ui' - run: node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs - name: Prepare frozen canonical A2UI browser producer if: matrix.suite == 'ag-ui' run: node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs diff --git a/apps/website/content/docs/react/ag-ui/guides/client-tools.mdx b/apps/website/content/docs/react/ag-ui/guides/client-tools.mdx index b9f2e4dce..0c4bc1c0f 100644 --- a/apps/website/content/docs/react/ag-ui/guides/client-tools.mdx +++ b/apps/website/content/docs/react/ag-ui/guides/client-tools.mdx @@ -73,6 +73,6 @@ npx nx build cockpit-ag-ui-client-tools-react npx nx e2e cockpit-ag-ui-client-tools-react ~~~ -The e2e target depends on frozen canonical fixture preparation. For explicit preparation, run **npx nx run cockpit-ag-ui-client-tools-react:prepare-fixture**. The build installs the real private package artifacts with strict types. Deterministic offline browser checks use the actual canonical graph and locked AG-UI adapter with scripted model responses over HTTP/SSE. They verify local execution, physical continuations, and owned cleanup; they do not prove a live model-provider request. Docs, Code, and Run select this same source implementation. +The build installs the real private package artifacts with strict types. Deterministic offline browser checks run the actual graph behind the locked AG-UI adapter, with scripted model responses replayed by aimock, over HTTP/SSE. They verify local execution and physical continuations, and that the model receives each exact local result; they do not prove a live model-provider request. Docs, Code, and Run select this same source implementation. diff --git a/apps/website/e2e/react-ag-ui-client-tools-preview.spec.ts b/apps/website/e2e/react-ag-ui-client-tools-preview.spec.ts index 4b3121568..1a7905647 100644 --- a/apps/website/e2e/react-ag-ui-client-tools-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-client-tools-preview.spec.ts @@ -10,10 +10,14 @@ interface Message { id: string; role: string; content: string; toolCallId?: stri interface RecordRow { body: { threadId: string; runId: string; messages: Message[]; tools: { name: string }[] }; events: { type: string; messages?: Message[] }[]; - observations: { socketAttempts: string[]; modelMessages: { type: string; id?: string; tool_call_id?: string; content: string }[][] }; + /** Every model call so far, from aimock's request journal (scripts/react-cockpit/ag-ui-proxy.mjs). */ + modelMessages: { role: string; tool_call_id?: string; content: string }[][]; } async function records(request: APIRequestContext): Promise { - return (await request.get(origin + '/__requests')).json(); + const wire = await (await request.get(origin + '/__requests')).json(); + const journal: { body: { messages?: RecordRow['modelMessages'][number] } }[] = await (await request.get(origin + '/__model-requests')).json(); + const modelMessages = journal.map(entry => entry.body.messages ?? []); + return wire.map((row: RecordRow) => ({ ...row, modelMessages })); } async function hydrated(page: Page, mode: string) { await expect(page.locator('[data-workspace-shell]')).toHaveAttribute('data-hydrated', 'true'); @@ -31,12 +35,11 @@ function native(wire: RecordRow[], count: number) { expect(new Set(wire.map(row => row.body.runId)).size).toBe(count); for (const row of wire) { expect(row.body.tools.map(tool => tool.name)).toEqual(names); - expect(row.observations.socketAttempts).toEqual([]); - const model = row.observations.modelMessages.at(-1) ?? []; const snapshot = row.events.filter(event => event.type === 'MESSAGES_SNAPSHOT').at(-1)?.messages ?? []; for (const result of row.body.messages.filter(message => message.role === 'tool')) { expect(row.body.messages.filter(message => message.toolCalls?.some(call => call.id === result.toolCallId))).toHaveLength(1); - expect(model.filter(message => message.type === 'tool' && message.id === result.id && message.tool_call_id === result.toolCallId && message.content === result.content)).toHaveLength(1); + // The model received the browser's exact result for that call. + expect(row.modelMessages.some(messages => messages.filter(message => message.role === 'tool' && message.tool_call_id === result.toolCallId && message.content === result.content).length === 1)).toBe(true); expect(snapshot.filter(message => message.role === 'tool' && message.id === result.id && message.content === result.content)).toHaveLength(1); } } @@ -44,21 +47,14 @@ function native(wire: RecordRow[], count: number) { test.describe('native React AG-UI Client Tools workspace', () => { test.describe.configure({ mode: 'serial' }); test.beforeEach(async ({ request }) => { - test.skip(Boolean(process.env['BASE_URL']), 'Canonical offline ownership is verified locally'); + test.skip(Boolean(process.env['BASE_URL']), 'The real AG-UI server on aimock is verified locally'); expect((await request.post(origin + '/__reset')).ok()).toBe(true); }); test.afterEach(async ({ page, request }, info) => { if (process.env['BASE_URL']) return; - // Both success and assertion failure dispose the iframe before closing its exact worker owner. - try { - await page.goto('about:blank'); - const wire = await records(request); - await writeFile(info.outputPath('native-wire.json'), JSON.stringify(wire, null, 2)); - } finally { - const stats = await (await request.post(origin + '/__finish')).json(); - await writeFile(info.outputPath('owned-fixture-receipt.json'), JSON.stringify(stats, null, 2)); - expect(Object.values(stats.closed.at(-1).closure).every(Boolean)).toBe(true); - } + await page.goto('about:blank'); + const wire = await records(request); + await writeFile(info.outputPath('native-wire.json'), JSON.stringify(wire, null, 2)); }); test('actual Docs, Code and Run retain native identity, source bytes and six inert presets', async ({ page, request }, info) => { await page.goto(reactRoute + '?topicProof=b4'); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 3840349b2..3055eb7b2 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -356,10 +356,9 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs --serve', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-client-tools --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4640', - // A reused listener cannot establish canonical worker ownership. + url: 'http://127.0.0.1:4640/__ready', reuseExistingServer: false, timeout: 180_000, }, @@ -564,7 +563,7 @@ export const createWebsitePlaywrightConfig = ( config.testMatch = '**/react-ag-ui-client-tools-preview.spec.ts'; if (Array.isArray(config.webServer)) { config.webServer = config.webServer.filter(server => - [localURL, 'http://localhost:4325', 'http://127.0.0.1:4640'].includes(server.url ?? '') + [localURL, 'http://localhost:4325', 'http://127.0.0.1:4640/__ready'].includes(server.url ?? '') ); } } diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index bcc90b32f..a4788de16 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -506,7 +506,7 @@ describe('Website Playwright configuration', () => { command: 'node scripts/react-cockpit/serve.mjs ag-ui-tool-views --no-parent --backend', url: 'http://127.0.0.1:4611/__ready', }), - expect.objectContaining({command:'node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs --serve',url:'http://127.0.0.1:4640'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-client-tools --no-parent --backend',url:'http://127.0.0.1:4640/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent',url:'http://127.0.0.1:4641'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent',url:'http://127.0.0.1:4642'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent',url:'http://127.0.0.1:4643'}), @@ -577,11 +577,11 @@ describe('Website Playwright configuration', () => { }); }); -it('owns frozen AG-UI Client Tools preparation without reusing a foreign fixture', () => { +it('owns the AG-UI Client Tools preview and its aimock backend without reusing a foreign server', () => { const config = createWebsitePlaywrightConfig({ PLAYWRIGHT_REUSE_EXISTING_SERVER: 'true' }); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.find(server => server.url === 'http://127.0.0.1:4640')).toMatchObject({ - command: 'node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs --serve', + expect(servers.find(server => server.url === 'http://127.0.0.1:4640/__ready')).toMatchObject({ + command: 'node scripts/react-cockpit/serve.mjs ag-ui-client-tools --no-parent --backend', timeout: 180_000, reuseExistingServer: false, }); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); @@ -620,6 +620,6 @@ it('scopes native AG-UI A2UI checks to Website and the prepared owned React serv it('scopes native client-tools checks to the Website and exact two owned frontend servers', () => { const config = createWebsitePlaywrightConfig({ WEBSITE_E2E_REACT_TOPIC: 'ag-ui-client-tools', WEBSITE_E2E_PORT: '4390' }); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://localhost:4325', 'http://127.0.0.1:4640']); + expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://localhost:4325', 'http://127.0.0.1:4640/__ready']); expect(config.testMatch).toBe('**/react-ag-ui-client-tools-preview.spec.ts'); }); diff --git a/cockpit/ag-ui/client-tools/angular/e2e/fixtures/client-tools.json b/cockpit/ag-ui/client-tools/angular/e2e/fixtures/client-tools.json index ac7c85d7b..6273d3773 100644 --- a/cockpit/ag-ui/client-tools/angular/e2e/fixtures/client-tools.json +++ b/cockpit/ag-ui/client-tools/angular/e2e/fixtures/client-tools.json @@ -35,6 +35,201 @@ { "match": { "userMessage": "Plan a trip to Portland" }, "response": { "toolCalls": [ { "name": "get_weather", "arguments": { "location": "Portland" } }, { "name": "confirm_booking", "arguments": { "summary": "Fictional trip to Portland" } } ] } + }, + { + "match": { "toolCallId": "rounds-2" }, + "response": { "content": "Canonical local results received." } + }, + { + "match": { "toolCallId": "rounds-1" }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": "{\"location\": \"San Francisco\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 55, \"windMph\": 8}", + "id": "rounds-2" + } + ] + } + }, + { + "match": { + "userMessage": "Rounds", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_weather", + "arguments": "{\"location\": \"San Francisco\"}", + "id": "rounds-1" + } + ] + } + }, + { + "match": { + "userMessage": "Malformed", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": "{\"location\": \"San Francisco\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 101, \"windMph\": 8}" + } + ] + } + }, + { + "match": { + "userMessage": "Unknown", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "unknown_weather", + "arguments": "{}" + } + ] + } + }, + { + "match": { + "userMessage": "Mixed", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_snapshot", + "arguments": "{\"location\": \"San Francisco\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 55, \"windMph\": 8}" + }, + { + "name": "get_weather", + "arguments": "{\"location\": \"San Francisco\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Bookings", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "confirm_booking", + "arguments": "{\"summary\": \"Fictional dinner A\"}" + }, + { + "name": "confirm_booking", + "arguments": "{\"summary\": \"Fictional dinner B\"}" + } + ] + } + }, + { + "match": { + "userMessage": "confirm_booking", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "confirm_booking", + "arguments": "{\"summary\": \"Fictional dinner booking\"}" + } + ] + } + }, + { + "match": { + "userMessage": "slow_status_check", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "slow_status_check", + "arguments": "{\"label\": \"demo check\"}" + } + ] + } + }, + { + "match": { + "userMessage": "Literal", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": "{\"location\": \"\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 55, \"windMph\": 8}" + } + ] + } + }, + { + "match": { + "userMessage": "weather_snapshot", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_snapshot", + "arguments": "{\"location\": \"San Francisco\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 55, \"windMph\": 8}" + } + ] + } + }, + { + "match": { + "userMessage": "weather_card", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "weather_card", + "arguments": "{\"location\": \"San Francisco\", \"temperatureF\": 68, \"conditions\": \"Sunny\", \"humidity\": 55, \"windMph\": 8}" + } + ] + }, + "chunkSize": 12 + }, + { + "match": { + "userMessage": "get_weather", + "hasToolResult": false + }, + "response": { + "toolCalls": [ + { + "name": "get_weather", + "arguments": "{\"location\": \"San Francisco\"}" + } + ] + } + }, + { + "match": { + "hasToolResult": true, + "userMessage": "" + }, + "response": { "content": "Canonical local results received." } + }, + { + "match": { "userMessage": "Hello canonical" }, + "response": { "content": "Canonical plain answer: Hello canonical" } + }, + { + "match": { "userMessage": "" }, + "response": { "content": "Canonical plain answer." } } ] } diff --git a/cockpit/ag-ui/client-tools/react/README.md b/cockpit/ag-ui/client-tools/react/README.md index bef352eea..c608e18b6 100644 --- a/cockpit/ag-ui/client-tools/react/README.md +++ b/cockpit/ag-ui/client-tools/react/README.md @@ -58,6 +58,6 @@ npx nx build cockpit-ag-ui-client-tools-react npx nx e2e cockpit-ag-ui-client-tools-react ~~~ -The e2e target depends on frozen canonical fixture preparation. For explicit preparation, run **npx nx run cockpit-ag-ui-client-tools-react:prepare-fixture**. The build installs the real private package artifacts with strict types. Deterministic offline browser checks use the actual canonical graph and locked AG-UI adapter with scripted model responses over HTTP/SSE. They verify local execution, physical continuations, and owned cleanup; they do not prove a live model-provider request. Docs, Code, and Run select this same source implementation. +The build installs the real private package artifacts with strict types. Deterministic offline browser checks run the actual graph behind the locked AG-UI adapter, with scripted model responses replayed by aimock, over HTTP/SSE. They verify local execution and physical continuations, and that the model receives each exact local result; they do not prove a live model-provider request. Docs, Code, and Run select this same source implementation. Source: `cockpit/ag-ui/client-tools/react/project.json` diff --git a/cockpit/ag-ui/client-tools/react/e2e/client-tools.spec.ts b/cockpit/ag-ui/client-tools/react/e2e/client-tools.spec.ts index 062188e8b..4d501aa1a 100644 --- a/cockpit/ag-ui/client-tools/react/e2e/client-tools.spec.ts +++ b/cockpit/ag-ui/client-tools/react/e2e/client-tools.spec.ts @@ -1,17 +1,28 @@ -import { writeFile } from 'node:fs/promises'; import { test, expect, type Page, type APIRequestContext } from '@playwright/test'; +// Backend: aimock replays cockpit/ag-ui/client-tools/angular/e2e/fixtures (the +// cockpit model of the react-parity client-tools server, as fixtures) into +// the example's real AG-UI server; the browser executes the client tools. The +// recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) records each run's +// real events live, holds runs, and serves aimock's request journal: what the +// model actually received. interface WireMessage { id: string; role: string; content: string; toolCallId?: string; toolCalls?: {id: string}[] } -interface ModelMessage { id?: string; type: string; content: string; tool_call_id?: string } +interface ModelMessage { role: string; content: string; tool_call_id?: string } interface WireRecord { body: {threadId: string; runId: string; messages: WireMessage[]; tools: {name: string}[]}; events: {type: string; messages?: WireMessage[]; delta?: string}[]; - observations: {socketAttempts: string[]; graphIdentity: number; checkpointerIdentity: number; modelIdentity: number; modelMessages: ModelMessage[][]}; + /** Every model call so far, oldest first. */ + modelMessages: ModelMessage[][]; held: boolean; closed: boolean; } const base = '/ag-ui/client-tools/react/'; const names = ['get_weather', 'slow_status_check', 'weather_card', 'weather_snapshot', 'confirm_booking']; const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; -async function records(request: APIRequestContext) { return (await request.get('/__requests')).json() as Promise; } +async function records(request: APIRequestContext): Promise { + const wire = await (await request.get('/__requests')).json(); + const journal: { body: { messages?: ModelMessage[] } }[] = await (await request.get('/__model-requests')).json(); + const modelMessages = journal.map(entry => entry.body.messages ?? []); + return wire.map((row: WireRecord) => ({ ...row, modelMessages })); +} async function send(page: Page, text: string) { await page.getByLabel('Message', {exact:true}).fill(text); await page.getByRole('button', {name:'Send', exact:true}).click(); @@ -31,29 +42,18 @@ function causal(wire: WireRecord[], count: number) { expect(row.body.runId).toMatch(uuid); expect(row.body.threadId).toMatch(uuid); expect(row.body.tools.map((tool: {name: string}) => tool.name)).toEqual(names); - expect(row.observations.socketAttempts).toEqual([]); - expect(row.observations.graphIdentity).toBe(wire[0].observations.graphIdentity); - expect(row.observations.checkpointerIdentity).toBe(wire[0].observations.checkpointerIdentity); - expect(row.observations.modelIdentity).toBe(wire[0].observations.modelIdentity); - const model = row.observations.modelMessages.at(-1) ?? []; const snapshot = row.events.filter(event => event.type === 'MESSAGES_SNAPSHOT').at(-1)?.messages ?? []; for (const result of row.body.messages.filter(message => message.role === 'tool')) { const caller = row.body.messages.find(message => message.toolCalls?.some(call => call.id === result.toolCallId)); expect(caller).toBeDefined(); expect(row.body.messages.filter(message => message.id === result.id)).toHaveLength(1); - expect(model.filter(message => message.type === 'tool' && message.id === result.id && message.tool_call_id === result.toolCallId && message.content === result.content)).toHaveLength(1); + // The model received the browser's exact result for that call. + expect(row.modelMessages.some(messages => messages.filter(message => message.role === 'tool' && message.tool_call_id === result.toolCallId && message.content === result.content).length === 1)).toBe(true); expect(snapshot.filter(message => message.role === 'tool' && message.id === result.id && message.toolCallId === result.toolCallId && message.content === result.content)).toHaveLength(1); } } } test.beforeEach(async ({request}) => { expect((await request.post('/__reset')).ok()).toBe(true); }); -test.afterEach(async ({request}, info) => { - const stats = await (await request.post('/__finish')).json(); - expect(Object.values(stats.closed.at(-1).closure).every(Boolean)).toBe(true); - const receipt = info.outputPath('canonical-owned-fixture-receipt.json'); - await writeFile(receipt, JSON.stringify(stats, null, 2)); - await info.attach('canonical-owned-fixture-receipt', {path: receipt, contentType:'application/json'}); -}); test('mount issues no physical request and offers six example prompts', async ({page,request}) => { await page.goto(base); await expect(page.getByRole('group', {name:'Example prompts'}).getByRole('button')).toHaveCount(6); @@ -95,7 +95,7 @@ for (const tool of ['get_weather','weather_card']) { const wire = await records(request); causal(wire, 2); const result = wire[1].body.messages.filter((message:WireMessage) => message.role === 'tool'); expect(result).toHaveLength(1); - expect(wire[1].observations.modelMessages.at(-1)?.filter((message:ModelMessage) => message.type === 'tool')).toHaveLength(1); + expect(wire[1].modelMessages.at(-1)?.filter((message:ModelMessage) => message.role === 'tool')).toHaveLength(1); expect(wire[1].body.tools).toEqual(wire[0].body.tools); expect(result[0].content).toBe(tool === 'get_weather' ? JSON.stringify({location:'San Francisco',temperatureF:68,conditions:'Sunny',humidity:55,windMph:8}) : JSON.stringify({displayed:true})); await expect(page.locator('[data-native-call]')).toHaveCount(1); @@ -169,7 +169,9 @@ for (const text of ['Unknown','Malformed']) { } for (const action of ['release','Stop','page exit']) { test(`actual streamed arguments held before terminal: ${action} respects execution boundary`, async ({page,request}) => { - await request.post('/__reset',{data:{mode:'stream',holdTerminal:true}}); + // Hold every run just before its terminal event; weather_card's arguments + // stream in several chunks (fixture chunkSize). + await request.post('/__hold/stream',{data:{beforeType:'RUN_FINISHED',persist:true}}); await page.goto(base); await send(page,'Use weather_card'); await expect.poll(async()=> (await records(request))[0]?.held).toBe(true); await expect(page.locator('.weather-card, .tp-approval-card')).toHaveCount(0); @@ -197,7 +199,7 @@ test('multiple native groups keep distinct causal identities on retained history await expect(page.locator('[data-native-call]')).toHaveCount(3); const ids=wire[3].body.messages.flatMap((message:WireMessage)=>message.toolCalls?.map((call:{id:string})=>call.id)??[]); expect(new Set(ids).size).toBe(3); - expect(wire[3].observations.modelMessages.at(-1)?.filter((message:ModelMessage)=>message.type==='tool')).toHaveLength(3); + expect(wire[3].modelMessages.at(-1)?.filter((message:ModelMessage)=>message.role==='tool')).toHaveLength(3); }); test('mobile literal content and weather readings remain safe', async ({page,request}) => { await page.setViewportSize({width:390,height:844}); await page.goto(base); @@ -241,7 +243,9 @@ for(const action of ['Stop','page exit']) { }); } test('Stop during actual held continuation preserves complete sent-unconfirmed local evidence without replay',async({page,request})=>{ - await request.post('/__reset',{data:{holdTerminal:true,holdAfterRequest:1,holdReceipt:true}}); + // Hold the continuation (the second run) before the server acknowledges the + // result: right after it starts. + await request.post('/__hold/stream',{data:{afterType:'RUN_STARTED',persist:true,fromRequest:1}}); await page.goto(base); await send(page,'Use get_weather'); await expect.poll(async()=> (await records(request))[1]?.held).toBe(true); await expect(page.getByRole('region',{name:'Local tool evidence'})).toContainText('Local execution: complete'); diff --git a/cockpit/ag-ui/client-tools/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/client-tools/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..a51593a02 --- /dev/null +++ b/cockpit/ag-ui/client-tools/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/client-tools', port: 4640 }); diff --git a/cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts b/cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts index a15ad2115..bd0002689 100644 --- a/cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts @@ -3,9 +3,12 @@ export default defineConfig({ testDir: '.', outputDir: './test-results', workers: 1, fullyParallel: false, timeout: 30000, use: { baseURL: 'http://127.0.0.1:4640', trace: 'retain-on-failure' }, projects: [{name:'chromium', use: {...devices['Desktop Chrome']}}], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/client-tools/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { - command: 'node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs --serve', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-client-tools --no-parent', cwd: '../../../../..', - url: 'http://127.0.0.1:4640', reuseExistingServer: false, timeout: 180000, + url: 'http://127.0.0.1:4640', reuseExistingServer: false, timeout: 30000, }, }); diff --git a/cockpit/ag-ui/client-tools/react/project.json b/cockpit/ag-ui/client-tools/react/project.json index 85c507c82..3a3606abb 100644 --- a/cockpit/ag-ui/client-tools/react/project.json +++ b/cockpit/ag-ui/client-tools/react/project.json @@ -3,6 +3,10 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/client-tools/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-ag-ui-client-tools-angular", + "cockpit-ag-ui-client-tools-python" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -30,7 +34,12 @@ "react" ] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/ag-ui/client-tools/react" @@ -39,24 +48,26 @@ "command": "node scripts/react-cockpit/build.mjs ag-ui-client-tools" } }, - "prepare-fixture": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs" - } - }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, "dependsOn": [ - "prepare-fixture", - "build" + "build", + { + "target": "test", + "projects": [ + "cockpit-ag-ui-client-tools-python" + ] + } ], "options": { "config": "cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/cockpit/shared/react-aimock-setup.ts b/cockpit/shared/react-aimock-setup.ts index b98315b11..03f1f4761 100644 --- a/cockpit/shared/react-aimock-setup.ts +++ b/cockpit/shared/react-aimock-setup.ts @@ -33,7 +33,8 @@ export function reactAimockGlobalSetup(options: { * The AG-UI flavor: aimock replays the Angular example's fixtures into the * example's uvicorn AG-UI server (`src/server.py`); the static server proxies * the agent endpoint to it through scripts/react-cockpit/ag-ui-proxy.mjs. - * Mirrors `aimockAgUi()` in scripts/react-cockpit/configuration.mjs. + * Mirrors `aimockAgUi()` in scripts/react-cockpit/configuration.mjs + * (backend port + 1000, aimock port + 2000). */ export function reactAimockAgUiGlobalSetup(options: { /** e.g. 'ag-ui/streaming' */ @@ -45,6 +46,8 @@ export function reactAimockAgUiGlobalSetup(options: { stateKey: `cockpit-${options.example.replace('/', '-')}-react`, pythonCwd: `cockpit/${options.example}/python`, backendPort: options.port + 1000, + // The proxy reads model requests from aimock's journal on this port. + aimockPort: options.port + 2000, fixturesDir: resolve(__dirname, '..', options.example, 'angular/e2e/fixtures'), }); } diff --git a/fixtures/react-parity/native-ag-ui-client-tools/compiled_worker.py b/fixtures/react-parity/native-ag-ui-client-tools/compiled_worker.py index 9938368a2..e5e13e5b4 100644 --- a/fixtures/react-parity/native-ag-ui-client-tools/compiled_worker.py +++ b/fixtures/react-parity/native-ag-ui-client-tools/compiled_worker.py @@ -80,70 +80,6 @@ async def _astream(self, messages, stop=None, run_manager=None, **kwargs): yield ChatGenerationChunk(message=AIMessageChunk(id='scripted-weather-call', content='', tool_call_chunks=[{'id': None, 'name': None, 'args': '"KASE"}', 'index': 0}])) yield ChatGenerationChunk(message=AIMessageChunk(id='scripted-weather-call', content='', response_metadata={'finish_reason': 'tool_calls'})) -class CockpitModel(ScriptedModel): - """History-driven BaseChatModel only; canonical graph/adapter author all events.""" - sequence: int = 0 - - def answer(self, messages): - self.sequence += 1 - identity = f'cockpit-assistant-{self.sequence}' - last = messages[-1] - if isinstance(last, ToolMessage): - latest_human = next((m for m in reversed(messages) if m.type == 'human'), None) - if latest_human and 'Rounds' in str(latest_human.content): - after_human = messages[messages.index(latest_human) + 1:] - if len([m for m in after_human if isinstance(m, ToolMessage)]) == 1: - return AIMessage(id=identity, content='', tool_calls=[{'id': f'cockpit-call-{self.sequence}-0', 'name': 'weather_card', 'args': {'location': 'San Francisco', 'temperatureF': 68, 'conditions': 'Sunny', 'humidity': 55, 'windMph': 8}, 'type': 'tool_call'}]) - receipts = [m.content for m in messages if isinstance(m, ToolMessage)] - return AIMessage(id=identity, content='Canonical local results received: ' + json.dumps(receipts)) - text = str(last.content) - reading = {'location': 'San Francisco', 'temperatureF': 68, 'conditions': 'Sunny', 'humidity': 55, 'windMph': 8} - if 'Literal' in text: - reading['location'] = '' - if 'Malformed' in text: - names = [('weather_card', {**reading, 'humidity': 101})] - elif 'Unknown' in text: - names = [('unknown_weather', {})] - elif 'Mixed' in text: - names = [('weather_snapshot', reading), ('get_weather', {'location': 'San Francisco'})] - elif 'Plan a trip' in text: - names = [('get_weather', {'location': 'Portland'}), ('confirm_booking', {'summary': 'Fictional trip to Portland'})] - elif 'Bookings' in text: - names = [('confirm_booking', {'summary': 'Fictional dinner A'}), ('confirm_booking', {'summary': 'Fictional dinner B'})] - elif 'confirm_booking' in text: - names = [('confirm_booking', {'summary': 'Fictional dinner booking'})] - elif 'slow_status_check' in text: - names = [('slow_status_check', {'label': 'demo check'})] - elif 'weather_snapshot' in text: - names = [('weather_snapshot', reading)] - elif 'weather_card' in text: - names = [('weather_card', reading)] - elif 'get_weather' in text: - names = [('get_weather', {'location': 'San Francisco'})] - else: - return AIMessage(id=identity, content='Canonical plain answer: ' + text) - return AIMessage(id=identity, content='', tool_calls=[{'id': f'cockpit-call-{self.sequence}-{index}', 'name': name, 'args': args, 'type': 'tool_call'} for index, (name, args) in enumerate(names)]) - - def _generate(self, messages, stop=None, run_manager=None, **kwargs): - self.observed_messages.append([m.model_dump(mode='json') for m in messages]) - return ChatResult(generations=[ChatGeneration(message=self.answer(messages))]) - -class CockpitStreamingModel(CockpitModel): - streaming: bool = False - async def _astream(self, messages, stop=None, run_manager=None, **kwargs): - self.observed_messages.append([m.model_dump(mode='json') for m in messages]) - answer = self.answer(messages) - if not answer.tool_calls: - yield ChatGenerationChunk(message=AIMessageChunk(id=answer.id, content=answer.content)) - return - for index, call in enumerate(answer.tool_calls): - arguments = json.dumps(call['args']) - split = max(1, len(arguments) // 2) - yield ChatGenerationChunk(message=AIMessageChunk(id=answer.id, content='', tool_call_chunks=[{'id': call['id'], 'name': call['name'], 'args': '', 'index': index}])) - yield ChatGenerationChunk(message=AIMessageChunk(id=answer.id, content='', tool_call_chunks=[{'id': None, 'name': None, 'args': arguments[:split], 'index': index}])) - yield ChatGenerationChunk(message=AIMessageChunk(id=answer.id, content='', tool_call_chunks=[{'id': None, 'name': None, 'args': arguments[split:], 'index': index}])) - yield ChatGenerationChunk(message=AIMessageChunk(id=answer.id, content='', response_metadata={'finish_reason': 'tool_calls'})) - def emit(value): encoded = json.dumps(value, separators=(',', ':')) if len(encoded.encode()) > MAX_BYTES: @@ -156,17 +92,13 @@ async def main(): assert mode in ['nonstream', 'stream', 'unknown'] module = importlib.import_module('src.graph') model = ScriptedStreamingModel() if mode == 'stream' else ScriptedModel(call_name='unknown_weather' if mode == 'unknown' else 'get_weather') - scenario = sys.argv[2] if len(sys.argv) > 2 else None - assert scenario in [None, 'cockpit'] - if scenario == 'cockpit': - model = CockpitStreamingModel(streaming=True) if mode == 'stream' else CockpitModel() module._base_llm = model graph = module.build_client_tools_graph() agent = LangGraphAgent(name='offline-canonical-client-tools', graph=graph) paths = [Path(__file__), ROOT / 'cockpit/ag-ui/client-tools/python/src/graph.py', ROOT / 'cockpit/ag-ui/client-tools/python/prompts/client-tools.md', ROOT / 'cockpit/ag-ui/client-tools/python/uv.lock', ROOT / 'cockpit/ag-ui/client-tools/python/pyproject.toml'] modules = {name: str(Path(sys.modules[name].__file__).resolve()) for name in ['src.graph', 'ag_ui_langgraph.agent', 'threadplane.middleware.langgraph.middleware']} paths.extend(Path(value) for value in modules.values()) - identity = {'pid': os.getpid(), 'python': sys.version.split()[0], 'mode': mode, 'scenario': scenario, 'environment': sys.prefix, 'modules': modules, 'socketAttempts': socket_attempts, 'blockedSocketConstructions': blocked_socket_constructions, 'versions': {name: importlib.metadata.version(name) for name in ['ag-ui-langgraph', 'ag-ui-protocol', 'langgraph', 'langchain-core']}, 'sources': [{'path': str(path), 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} for path in dict.fromkeys(paths)]} + identity = {'pid': os.getpid(), 'python': sys.version.split()[0], 'mode': mode, 'environment': sys.prefix, 'modules': modules, 'socketAttempts': socket_attempts, 'blockedSocketConstructions': blocked_socket_constructions, 'versions': {name: importlib.metadata.version(name) for name in ['ag-ui-langgraph', 'ag-ui-protocol', 'langgraph', 'langchain-core']}, 'sources': [{'path': str(path), 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} for path in dict.fromkeys(paths)]} emit({'ready': identity}) count = 0 while True: diff --git a/fixtures/react-parity/native-ag-ui-client-tools/server.mjs b/fixtures/react-parity/native-ag-ui-client-tools/server.mjs index b223fe70e..f919a6b53 100644 --- a/fixtures/react-parity/native-ag-ui-client-tools/server.mjs +++ b/fixtures/react-parity/native-ag-ui-client-tools/server.mjs @@ -130,7 +130,6 @@ export async function createClientToolsServer({ root = defaultRoot, mode = 'nonstream', holdTerminal = false, - scenario, holdAfterRequest = 0, holdReceipt = false, environment = process.env.THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT ?? @@ -138,7 +137,6 @@ export async function createClientToolsServer({ } = {}) { root = realpathSync(root); assert.ok(['nonstream', 'stream', 'unknown'].includes(mode)); - assert.ok(scenario === undefined || scenario === 'cockpit'); assert.equal(typeof holdReceipt, 'boolean'); assert.ok(Number.isSafeInteger(holdAfterRequest) && holdAfterRequest >= 0 && holdAfterRequest < 12); const env = Object.fromEntries( @@ -167,7 +165,6 @@ export async function createClientToolsServer({ 'python', worker, mode, - ...(scenario ? [scenario] : []), ], { cwd: root, env, stdio: ['pipe', 'pipe', 'pipe'], detached: true } ); @@ -426,7 +423,6 @@ export async function createClientToolsServer({ } ownedGroupMembers(inventory, child.pid, ownedProcesses); assert.equal(ready.mode, mode); - assert.equal(ready.scenario, scenario ?? null); assert.equal(realpathSync(ready.environment), realpathSync(environment)); assert.match(ready.python, /^3\.12\./); assert.equal(ready.versions['ag-ui-langgraph'], '0.0.45'); diff --git a/libs/e2e-harness/src/ag-ui-global-setup-factory.ts b/libs/e2e-harness/src/ag-ui-global-setup-factory.ts index 38db03b8b..32bb5cc0b 100644 --- a/libs/e2e-harness/src/ag-ui-global-setup-factory.ts +++ b/libs/e2e-harness/src/ag-ui-global-setup-factory.ts @@ -83,9 +83,14 @@ async function startAgUiBackend(opts: { backendPort: number; fixturesDir: string; backendReadyTimeoutMs?: number; + aimockPort?: number; }): Promise { const launch = resolveAimockLaunch(opts.fixturesDir); - const aimock = await startAimock(launch.startOptions); + const aimock = await startAimock( + opts.aimockPort && launch.startOptions.mode === 'replay' + ? { ...launch.startOptions, port: opts.aimockPort } + : launch.startOptions, + ); // eslint-disable-next-line no-console console.log(`[ag-ui-harness] aimock listening at ${aimock.baseUrl}`); @@ -134,6 +139,8 @@ export interface CreateAgUiBackendGlobalSetupOpts { backendPort: number; fixturesDir: string; backendReadyTimeoutMs?: number; + /** A fixed aimock port, for specs that read its request journal. */ + aimockPort?: number; } /** Backend only: the React cockpit serves its own build and proxies to it. */ @@ -147,6 +154,7 @@ export function createAgUiBackendGlobalSetup( backendPort: opts.backendPort, fixturesDir: opts.fixturesDir, backendReadyTimeoutMs: opts.backendReadyTimeoutMs, + aimockPort: opts.aimockPort, }); registerState(opts.stateKey, backend); }; diff --git a/libs/e2e-harness/src/aimock-runner.ts b/libs/e2e-harness/src/aimock-runner.ts index 3293a9bf6..b8225b6c6 100644 --- a/libs/e2e-harness/src/aimock-runner.ts +++ b/libs/e2e-harness/src/aimock-runner.ts @@ -19,6 +19,11 @@ export interface AimockStartOptions { fixturePath?: string; /** Record: directory where captured fixtures are written. Required in record mode. */ recordDir?: string; + /** + * Replay: a fixed port, for tests that read aimock's request journal + * (`GET /__aimock/journal`). Default: any free port. + */ + port?: number; } // Raw JSON entry shape passes through to aimock's FixtureFileEntry — the @@ -73,7 +78,7 @@ export async function startAimock(opts: AimockStartOptions): Promise 0) { mock.addFixturesFromJSON(entries as never); } diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index 19a23f1ba..cab503a96 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - '72009a0617afde5507d49260dd17f29f4b8b80f504eeeb91b954a86d053aec6a', + '2937b601bc61b77ad17af8e943750ffaee0efbf11c9859ea0576b50d86d79481', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); diff --git a/scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs b/scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs index d7af71476..b0b265812 100644 --- a/scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs +++ b/scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs @@ -10,6 +10,14 @@ test('strict authored native client-tools entry is separate from LangGraph with base: '/ag-ui/client-tools/react/', port: 4640, project: 'cockpit-ag-ui-client-tools-react', + backend: { + kind: 'aimock-ag-ui', + python: 'cockpit/ag-ui/client-tools/python', + fixtures: 'cockpit/ag-ui/client-tools/angular/e2e/fixtures', + backendPort: 5640, + aimockPort: 6640, + agentPath: '/agent', + }, }); assert.equal( reactCockpitConfiguration('client-tools').appPath, diff --git a/scripts/react-cockpit/ag-ui-client-tools-fixture.mjs b/scripts/react-cockpit/ag-ui-client-tools-fixture.mjs deleted file mode 100644 index 2ef6f2400..000000000 --- a/scripts/react-cockpit/ag-ui-client-tools-fixture.mjs +++ /dev/null @@ -1,76 +0,0 @@ -/** Offline browser proof only. Every positive event comes from the canonical worker. */ -import assert from 'node:assert/strict'; -import { request as httpRequest } from 'node:http'; -import { dirname, resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { createClientToolsServer } from '../../fixtures/react-parity/native-ag-ui-client-tools/server.mjs'; -const defaultRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); -export function createAgUiClientToolsFixture({ root = defaultRoot } = {}) { - const environment = resolve(root, 'cockpit/ag-ui/client-tools/python/.venv'); - let owner, closed = false, operations = Promise.resolve(); - const receipts = []; - const serialize = (operation) => { - const result = operations.then(operation); - operations = result.catch(() => undefined); - return result; - }; - async function closeOwner() { - const previous = owner; - owner = undefined; - if (!previous) return; - try { await previous.close(); } - catch (error) { - const { closure, ownership } = previous.stats(); - console.error('Canonical fixture close failed', error.message, JSON.stringify({ closure, ownership })); - throw error; - } - const receipt = previous.stats(); - assert.ok(Object.values(receipt.closure).every(Boolean)); - receipts.push(receipt); - } - const reset = (options = {}) => serialize(async () => { - assert.ok(!closed, 'Fixture closed'); - await closeOwner(); - assert.ok(['nonstream', 'stream', 'unknown'].includes(options.mode ?? 'nonstream')); - assert.ok(options.holdTerminal === undefined || typeof options.holdTerminal === 'boolean'); - owner = await createClientToolsServer({ root, environment, scenario: 'cockpit', mode: options.mode ?? 'nonstream', holdTerminal: options.holdTerminal ?? false, holdAfterRequest: options.holdAfterRequest ?? 0, holdReceipt: options.holdReceipt ?? false }); - }); - const stats = () => structuredClone({ current: owner?.stats(), closed: receipts }); - const json = (response, value, status = 200) => response.writeHead(status, {'content-type':'application/json'}).end(JSON.stringify(value)); - async function handle(request, response, pathname) { - if (!['/__reset', '/__stats', '/__requests', '/__release', '/__close', '/__finish', '/ag-ui/client-tools/agent'].includes(pathname)) return false; - try { - if (pathname === '/__stats') { json(response, stats()); return true; } - if (pathname === '/__requests') { json(response, owner?.stats().requests ?? []); return true; } - if (pathname === '/__finish') { await serialize(closeOwner); json(response, stats()); return true; } - if (pathname === '/__close') { await close(); json(response, stats()); return true; } - if (pathname === '/__reset') { - assert.equal(request.method, 'POST'); - let body = ''; - for await (const chunk of request) { body += chunk; assert.ok(body.length <= 2000000); } - await reset(body ? JSON.parse(body) : {}); - json(response, stats()); return true; - } - const current = owner; - assert.ok(current && !closed, 'Explicit prepared fixture reset required'); - if (pathname === '/__release') { - await fetch(current.url + '/release', {method:'POST'}); - json(response, {}); return true; - } - assert.equal(request.method, 'POST'); - // Preserve SSE timing and cancellation; never synthesize a positive event. - const upstream = httpRequest(current.url + '/agent', {method: 'POST', headers: {'content-type': 'application/json'}}, incoming => { - if (owner !== current || closed || response.destroyed) { incoming.destroy(); return; } - response.writeHead(incoming.statusCode, {'content-type': incoming.headers['content-type'] ?? 'application/json', 'cache-control': 'no-store'}); - incoming.pipe(response); - incoming.on('error', () => response.destroy()); - }); - upstream.on('error', () => { if (!response.headersSent && !response.destroyed) json(response, {error:'Canonical fixture unavailable'}, 502); else response.destroy(); }); - response.once('close', () => upstream.destroy()); - request.pipe(upstream); - return true; - } catch { if (!response.headersSent) json(response, {error:'Canonical fixture request failed'}, 500); else response.destroy(); return true; } - } - function close() { closed = true; return serialize(closeOwner); } - return Object.assign(handle, { reset, stats, close }); -} diff --git a/scripts/react-cockpit/ag-ui-client-tools-fixture.spec.mjs b/scripts/react-cockpit/ag-ui-client-tools-fixture.spec.mjs deleted file mode 100644 index be15fe265..000000000 --- a/scripts/react-cockpit/ag-ui-client-tools-fixture.spec.mjs +++ /dev/null @@ -1,98 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { resolve } from 'node:path'; -import { createClientToolsServer } from '../../fixtures/react-parity/native-ag-ui-client-tools/server.mjs'; -const root = process.cwd(); -const environment = resolve(root, 'cockpit/ag-ui/client-tools/python/.venv'); -const tools = ['get_weather', 'slow_status_check', 'weather_card', 'weather_snapshot', 'confirm_booking'].map(name => ({name, description: name, parameters: {type: 'object'}})); -function input(text, runId = 'run-1', messages) { - return { threadId: 'canonical-cockpit-thread', runId, state: {}, messages: messages ?? [{id: 'human-'+runId, role: 'user', content: text}], tools, context: [], forwardedProps: {} }; -} -test('cockpit scenario chooses actual human request and unique calls in retained canonical graph', {timeout: 30000}, async () => { - const server = await createClientToolsServer({root, environment, scenario: 'cockpit'}); - try { - const first = input('Show weather_snapshot'); - const response = await fetch(server.url+'/agent', {method:'POST', body: JSON.stringify(first)}); - assert.equal(response.status, 200); await response.text(); - const record = server.stats().requests[0]; - const call = record.events.filter(event => event.type === 'MESSAGES_SNAPSHOT').at(-1).messages.find(message => message.toolCalls?.length).toolCalls[0]; - assert.equal(call.function.name, 'weather_snapshot'); - assert.equal(record.observations.modelMessages[0].filter(m => m.type === 'human').length, 1); - assert.equal(record.observations.modelCatalogs[0].length, 5); - assert.deepEqual(record.observations.socketAttempts, []); - } finally { await server.close(); assert.ok(Object.values(server.stats().closure).every(Boolean)); } -}); -test('native wrapper reset closes exact prior owner and retains independent closure receipt', {timeout: 30000}, async () => { - const { createAgUiClientToolsFixture } = await import('./ag-ui-client-tools-fixture.mjs'); - const fixture = createAgUiClientToolsFixture({root}); - try { - await fixture.reset(); - const first = fixture.stats().current.ready.pid; - await fixture.reset({mode: 'stream', holdTerminal: true}); - assert.notEqual(fixture.stats().current.ready.pid, first); - assert.equal(fixture.stats().closed.length, 1); - assert.ok(Object.values(fixture.stats().closed[0].closure).every(Boolean)); - assert.equal(fixture.stats().current.ready.mode, 'stream'); - assert.deepEqual(fixture.stats().current.requests, []); - } finally { await fixture.close(); } - assert.ok(Object.values(fixture.stats().closed.at(-1).closure).every(Boolean)); - await assert.rejects(fixture.reset(), /closed/); -}); -test('concurrent resets serialize and shutdown revokes queued startup without leaking an owner', {timeout: 30000}, async () => { - const { createAgUiClientToolsFixture } = await import('./ag-ui-client-tools-fixture.mjs'); - const fixture=createAgUiClientToolsFixture({root}); - try { - await Promise.all([fixture.reset(),fixture.reset({mode:'stream'})]); - assert.equal(fixture.stats().closed.length,1); - const starting=fixture.reset(); - const closing=fixture.close(); - await assert.rejects(starting,/closed/); - await closing; - assert.equal(fixture.stats().current,undefined); - assert.equal(fixture.stats().closed.length,2); - for(const receipt of fixture.stats().closed) assert.ok(Object.values(receipt.closure).every(Boolean)); - } finally { await fixture.close(); } -}); -test('holding only a continuation never holds first actual canonical terminal', {timeout:30000}, async () => { - const server=await createClientToolsServer({root,environment,scenario:'cockpit',holdTerminal:true,holdAfterRequest:1}); - try { - const controller=new AbortController(); - const response=await fetch(server.url+'/agent',{method:'POST',body:JSON.stringify(input('Use get_weather')),signal:controller.signal}); - assert.equal(server.stats().requests[0].held,false); - controller.abort(); - } finally {await server.close();} -}); -test('one authored human drives two distinct canonical client-tool groups before final answer', {timeout:30000}, async () => { - const server=await createClientToolsServer({root,environment,scenario:'cockpit'}); - try { - await (await fetch(server.url+'/agent',{method:'POST',body:JSON.stringify(input('Rounds get_weather'))})).text(); - const first=server.stats().requests[0].events.filter(event=>event.type==='MESSAGES_SNAPSHOT').at(-1).messages; - const call=first.find(message=>message.toolCalls?.length).toolCalls[0]; - const continuation=input('', 'run-2',[...first,{id:call.id,role:'tool',toolCallId:call.id,content:JSON.stringify({location:'San Francisco',temperatureF:68,conditions:'Sunny',humidity:55,windMph:8})}]); - await (await fetch(server.url+'/agent',{method:'POST',body:JSON.stringify(continuation)})).text(); - const messages=server.stats().requests[1].events.filter(event=>event.type==='MESSAGES_SNAPSHOT').at(-1).messages; - assert.equal(messages.at(-1).toolCalls?.[0]?.function.name,'weather_card'); - assert.notEqual(messages.at(-1).toolCalls[0].id,call.id); - } finally {await server.close();} -}); -test('actual cockpit astream emits two partial canonical argument deltas after separate call header', {timeout:30000}, async () => { - const server=await createClientToolsServer({root,environment,scenario:'cockpit',mode:'stream'}); - try { - await (await fetch(server.url+'/agent',{method:'POST',body:JSON.stringify(input('Use weather_card'))})).text(); - const deltas=server.stats().requests[0].events.filter(event=>event.type==='TOOL_CALL_ARGS').map(event=>event.delta); - assert.equal(deltas.length,2); - assert.equal(JSON.parse(deltas.join('')).temperatureF,68); - assert.throws(()=>JSON.parse(deltas[0])); - } finally {await server.close();} -}); -test('receipt boundary holds actual final snapshot and terminal together without fabricating events', {timeout:30000}, async () => { - const server=await createClientToolsServer({root,environment,scenario:'cockpit',holdTerminal:true,holdReceipt:true}); - try { - const controller=new AbortController(); - const response=await fetch(server.url+'/agent',{method:'POST',body:JSON.stringify(input('Use get_weather')),signal:controller.signal}); - const chunk=await response.body.getReader().read(); - const text=new TextDecoder().decode(chunk.value); - assert.equal(text.includes('"type":"MESSAGES_SNAPSHOT"'),false); - controller.abort(); - } finally {await server.close();} -}); diff --git a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs b/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs deleted file mode 100644 index f3fcba36c..000000000 --- a/scripts/react-cockpit/ag-ui-client-tools-preparation.spec.mjs +++ /dev/null @@ -1,43 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { readFileSync } from 'node:fs'; -import { execFileSync } from 'node:child_process'; -import { resolve } from 'node:path'; -import { root, project, environment } from './prepare-ag-ui-client-tools-fixture.mjs'; -const read = path => readFileSync(resolve(root,path),'utf8'); -test('native cockpit frozen prerequisite is uncached, absolute and independent of Library temp environment', () => { - assert.equal(project,resolve(root,'cockpit/ag-ui/client-tools/python')); - assert.equal(environment,resolve(project,'.venv')); - const config=JSON.parse(read('cockpit/ag-ui/client-tools/react/project.json')); - assert.equal(config.targets['prepare-fixture'].cache,false); - assert.deepEqual(config.targets.e2e.dependsOn,['prepare-fixture','build']); - assert.equal(config.targets.e2e.executor,'@nx/playwright:playwright'); - const source=read('scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs'); - assert.match(source,/\['sync', '--frozen', '--no-install-project', '--project', project\]/); - assert.match(source,/UV_PYTHON: '3.12', UV_PROJECT_ENVIRONMENT: environment/); - assert.doesNotMatch(source,/THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT|\/tmp\//); - assert.match(read('fixtures/react-parity/native-ag-ui-client-tools/server.mjs'),/process.env.THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT \?\?/); - assert.match(read('scripts/react-cockpit/ag-ui-client-tools-fixture.mjs'),/createClientToolsServer\(\{ root, environment, scenario: 'cockpit'/); - assert.match(execFileSync('git',['check-ignore','cockpit/ag-ui/client-tools/python/.venv/pyvenv.cfg'],{cwd:root,encoding:'utf8'}),/\.venv/); -}); -test('website browser job prepares its own canonical worker after existing pinned Python setup', () => { - const workflow=read('.github/workflows/ci.yml'); - const website=workflow.split('\n website-e2e:\n')[1].split(/^ {2}[A-Za-z0-9_-]+:\s*$/m)[0]; - assert.equal((website.match(/astral-sh\/setup-uv@/g)??[]).length,1); - assert.match(website,/Prepare frozen dashboard browser producer[\s\S]*python-version: '3.12'/); - assert.match(website,/Prepare frozen canonical client tools browser producer\n if: matrix.suite == 'ag-ui'\n run: node scripts\/react-cockpit\/prepare-ag-ui-client-tools-fixture.mjs/); - assert.match(website,/suite: \[agents, ag-ui, chat, render\]/); - assert.match(website,/- \{ suite: control-plane, shard: 1 \}/); - assert.ok(website.indexOf('prepare-ag-ui-client-tools-fixture.mjs') { - const serve=read('scripts/react-cockpit/serve.mjs'); - assert.match(serve,/configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' \? createAgUiClientToolsFixture/); - // LangGraph client-tools runs on aimock + `langgraph dev` through the proxy. - assert.match(serve,/configuration.backend\?\.kind === 'aimock-langgraph'\s*\? createLanggraphProxy/); - assert.match(serve,/await \(chatDebugFixture \?\? nativeStrandsFixture \?\? nativeMafFixture \?\? nativeMastraFixture \?\? agUiClientToolsFixture \?\? [^\n]+\)\.close\(\);/); - assert.match(read('cockpit/ag-ui/client-tools/react/e2e/playwright.config.ts'),/prepare-ag-ui-client-tools-fixture.mjs --serve/); - assert.match(read('scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs'),/'ag-ui-client-tools', '--no-parent'/); -}); diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index 26fd42ea6..a1612f8d1 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -7,18 +7,26 @@ * layer only adds what a browser contract test needs and a live server cannot * produce on demand: * - * GET /__requests every agent POST, in order: { path, method, body } + * GET /__requests every agent POST, in order: { path, method, + * body, events (real, so far), held, closed } * GET /__captures per finished run: { path, sse, events } as the * real server sent them (before any edit) * GET /__lifetime { active (a held stream), openResponses } * GET|POST /__ready 200 once the server answers /ok (with * `serve.mjs --backend`, after its lazy start) * POST /__reset forget requests and captures, drop one-shot - * controls, end a held stream + * controls, end a held stream (and clear the + * model journal) + * GET /__model-requests every request the model (aimock) received, from + * its journal (`modelJournal`) * POST /__hold/stream hold the next run stream after the first event * whose type matches `afterType` (default * TEXT_MESSAGE_CONTENT) or whose raw JSON matches - * `afterPattern`; buffers the rest + * `afterPattern`, or just before the first event + * of type `beforeType`; buffers the rest. + * `persist` holds every later run the same way; + * `fromRequest: n` starts with the nth request + * (0-based). /__requests marks a held run. * POST /__release flush and resume a held stream * POST /__fail-stream next agent POST answers 500 unforwarded (the * server is unavailable) @@ -146,7 +154,7 @@ function rewriteOutcome(mode, event) { return { ...event, outcome: { ...event.outcome, interrupts: [copy] } }; } -export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', ready = async () => {} }) { +export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', modelJournal, ready = async () => {} }) { let requests = []; let captures = []; const pendingCaptures = new Set(); @@ -170,7 +178,11 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re async function proxyRun(request, response, pathname, raw) { const body = raw.length ? JSON.parse(raw.toString('utf8')) : {}; - requests.push({ path: pathname, method: request.method, body }); + // Live while the run streams: the real events so far, and whether the + // proxy holds it or the browser closed it. + const entry = { path: pathname, method: request.method, body, events: [], held: false, closed: false }; + requests.push(entry); + response.once('close', () => { entry.closed = true; }); if (stickyEdits.some((op) => op.mode === 'unstable-result') && Array.isArray(body.messages)) raw = Buffer.from( JSON.stringify({ @@ -187,8 +199,9 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re json(response, { error: 'PRIVATE AG-UI server failure' }, 500); return; } - const hold = holdNext; - holdNext = null; + let hold = holdNext; + if (hold?.fromRequest !== undefined && requests.length - 1 < hold.fromRequest) hold = null; + else if (!hold?.persist) holdNext = null; const edits = [...stickyEdits, ...(nextEdits ?? [])].map((op) => ({ ...op, pattern: new RegExp(op.type ?? '.*'), used: false })); nextEdits = null; const ids = { threadId: body.threadId, runId: body.runId, humanId: body.messages?.at(-1)?.id, answerId: null }; @@ -274,6 +287,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re release() { if (held === held_) held = undefined; holding = false; + entry.held = false; pendingCaptures.add(settled); for (const event of buffered.splice(0)) if (!forward(event)) return; if (ended) finish(); @@ -294,18 +308,30 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re if (!line) continue; const event = JSON.parse(line.slice(5)); events.push(event); + entry.events.push(event); if (response.writableEnded) continue; if (holding) { buffered.push(event); continue; } + if (armed && hold.beforeType && event.type === hold.beforeType) { + armed = false; + holding = true; + entry.held = true; + held = held_; + pendingCaptures.delete(settled); + buffered.push(event); + continue; + } if (!forward(event)) continue; if ( armed && + !hold.beforeType && (hold.afterPattern ? new RegExp(hold.afterPattern).test(line) : event.type === (hold.afterType ?? 'TEXT_MESSAGE_CONTENT')) ) { armed = false; holding = true; + entry.held = true; held = held_; // A held run is not "in flight" for /__captures: it waits on a // release the test sends later. @@ -345,9 +371,24 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', re if (!answer.ok) throw new Error('AG-UI server is not ready'); return {}; }], - '/__reset': [['POST'], () => { reset(); return {}; }], + '/__reset': [['POST'], async () => { + reset(); + if (modelJournal) await fetch(new URL('/__aimock/reset/journal', modelJournal), { method: 'POST' }).catch(() => undefined); + return {}; + }], + // What the model received, from aimock's request journal. + '/__model-requests': [['GET'], async () => { + if (!modelJournal) throw new Error('No model journal configured'); + return (await fetch(modelJournal)).json(); + }], '/__hold/stream': [['POST'], (body) => { - holdNext = { afterType: body.afterType, afterPattern: body.afterPattern }; + holdNext = { + afterType: body.afterType, + afterPattern: body.afterPattern, + beforeType: body.beforeType, + persist: body.persist === true, + fromRequest: body.fromRequest, + }; return {}; }], '/__release': [['POST'], () => { held?.release(); return {}; }], diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index b7d29b48c..f58935244 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -35,7 +35,7 @@ export function createAimockBackend({ root, backend }) { // A leftover server on the port would answer /ok with a dead aimock behind it. const stale = await fetch(`http://127.0.0.1:${port}/ok`).then(() => true, () => false); if (stale) throw new Error(`Port ${port} is already serving; stop the leftover server first`); - mock = new LLMock({ port: 0, chunkSize: 4096 }); + mock = new LLMock({ port: backend.aimockPort ?? 0, chunkSize: 4096 }); mock.addFixturesFromJSON(fixtures); await mock.start(); const agUi = backend.kind === 'aimock-ag-ui'; diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index c1ceb0204..7ead93b3c 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -14,13 +14,15 @@ const aimockLanggraph = (example, port) => Object.freeze({ /** * aimock-backed AG-UI example: the example's uvicorn `src/server.py` * (ag-ui-langgraph) against the Angular example's aimock fixtures. - * Convention: backend = port + 1000. + * Convention: backend = port + 1000, aimock = port + 2000. */ const aimockAgUi = (example, port, agentPath = '/agent') => Object.freeze({ kind: 'aimock-ag-ui', python: `cockpit/${example}/python`, fixtures: `cockpit/${example}/angular/e2e/fixtures`, backendPort: port + 1000, + // Fixed, so the proxy can read aimock's request journal (/__model-requests). + aimockPort: port + 2000, // The server route the React example talks to (src/server.py). agentPath, }); @@ -219,6 +221,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { appPath: 'cockpit/ag-ui/client-tools/react', base: '/ag-ui/client-tools/react/', port: 4640, project: 'cockpit-ag-ui-client-tools-react', + backend: aimockAgUi('ag-ui/client-tools', 4640), }); if (topic === 'ag-ui-tool-views') return Object.freeze({ diff --git a/scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs b/scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs deleted file mode 100644 index c4280ca9a..000000000 --- a/scripts/react-cockpit/prepare-ag-ui-client-tools-fixture.mjs +++ /dev/null @@ -1,23 +0,0 @@ -/** Shared local/CI prerequisite; no inherited Library fixture environment. */ -import { execFileSync, spawn } from 'node:child_process'; -import { dirname, resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -export const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); -export const project = resolve(root, 'cockpit/ag-ui/client-tools/python'); -export const environment = resolve(project, '.venv'); -export function prepareClientToolsFixture() { - execFileSync('uv', ['sync', '--frozen', '--no-install-project', '--project', project], { - cwd: root, stdio: 'inherit', timeout: 180000, - env: {...process.env, UV_PYTHON: '3.12', UV_PROJECT_ENVIRONMENT: environment}, - }); -} -if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { - prepareClientToolsFixture(); - if (process.argv.includes('--serve')) { - const child = spawn(process.execPath, [resolve(root, 'scripts/react-cockpit/serve.mjs'), 'ag-ui-client-tools', '--no-parent'], {cwd: root, stdio:'inherit'}); - process.once('SIGTERM', () => child.kill('SIGTERM')); - process.once('SIGINT', () => child.kill('SIGINT')); - child.once('exit', code => process.exit(code ?? 1)); - child.once('error', () => process.exit(1)); - } -} diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index f0df05f75..ac622dd63 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -9,7 +9,6 @@ import { createAgUiA2uiFixture } from './ag-ui-a2ui-fixture.mjs'; import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; -import { createAgUiClientToolsFixture } from './ag-ui-client-tools-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; @@ -67,10 +66,10 @@ const agUiProxy = configuration.backend?.kind === 'aimock-ag-ui' upstream: `http://127.0.0.1:${configuration.backend.backendPort}`, agentPaths: [`/ag-ui/${configuration.topic}${configuration.backend.agentPath}`, '/developer-agent'], agentPath: configuration.backend.agentPath, + modelJournal: `http://127.0.0.1:${configuration.backend.aimockPort}/__aimock/journal`, ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) : null; -const agUiClientToolsFixture = configuration.adapter === 'ag-ui' && configuration.topic === 'client-tools' ? createAgUiClientToolsFixture() : null; const root = resolve( dirname(fileURLToPath(import.meta.url)), @@ -128,7 +127,6 @@ const server = createServer(async (request, response) => { if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; - if (agUiClientToolsFixture && (await agUiClientToolsFixture(request, response, pathname))) return; if (chatGenerativeUiFixture && pathname === '/__reset') { if (request.method !== 'POST') return json(response, {}, 405); try { @@ -207,7 +205,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || agUiClientToolsFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || chatGenerativeUiFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -220,7 +218,7 @@ if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? agUiClientToolsFixture ?? chatGenerativeUiFixture).close(); + else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? chatGenerativeUiFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From d9ac3e2e703da797c2919fa75f68b47b7cabe9a9 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:29:03 -0800 Subject: [PATCH 33/41] docs(cockpit): record batch 6b (ag-ui/client-tools) and split the A2UI examples into 6c Co-Authored-By: Claude Opus 5.5 --- .../specs/2026-11-18-react-cockpit-aimock-design.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index ebe505e90..c2e3a18fb 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -183,7 +183,8 @@ example needs that the pilot proxy doesn't yet provide. | 4c (done) | langgraph/{time-travel, durable-execution} | `/__history-copies`, `/__state/unsafe`, `/__thread//state` writes, `/__stream/edit { state, anonymous, unlessKey }`, `/__hold/stream { afterPattern, end }`, `/__run-status/`, `/__history-values { entry }` | time-travel reads checkpoint ids from real history and thread reads, not authored ids (each turn saves input, start and done checkpoints). The competing tip is a real state write. durable-execution's uncertain outcomes end the browser's stream early, keep history inconclusive, and "publish" the saved outcome through a one-shot history rewrite | | 5 (done) | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__stream/cut` (memory) | scenarios are tagged prompts on the shared `da-*.json` fixtures. The planning and filesystem integration specs (vitest) run on `createAimockBackend` plus an in-process proxy. The React memory fixture also backed the Angular memory e2e: that now uses `createGlobalSetup` with `langgraph dev` on 6313 and the recording proxy on 5313, where the Angular dev server's `/api` proxy points, which retires its owned-process lifecycle helpers and the `compiledMemoryBackend` guard in `e2e-wiring.spec.ts`. The memory spec reads stream bodies from `/__captures[].sse` (Chromium doesn't keep a long-lived event-stream body) | | 6a (done) | ag-ui/{streaming, interrupts, tool-views, json-render, subagents} | the AG-UI flavor: `aimock-ag-ui` backends run the example's uvicorn `src/server.py` (`agentPath` picks the route, e.g. interrupts' `/agent/native`), `createAgUiBackendGlobalSetup` in the harness, and `scripts/react-cockpit/ag-ui-proxy.mjs` (`/__requests`, `/__captures`, `/__lifetime`, `/__hold/stream { afterType, afterPattern }`, `/__release`, `/__fail-stream`, `/__stream/edit` with drop, patch, omit, setPath, replace, insertBefore/After, end, `outcome` and `snapshot` modes, `dataPattern`, `once`, `persist`) | streaming's fixture was pure authored JavaScript; the others ran the real graph through a wire script and rewrote its events, so their scripted models (from the Python tests) become shared fixtures and their rewrites become proxy edits (injected data). Real behavior replaces several: a model failure drops the stream (no RUN_ERROR), native interrupts carry LangGraph's own interrupt id, unknown tools and argument-less calls fail in the real ToolNode, and a Booking specialist's provider error is a real child failure. The client returns the server's STATE_SNAPSHOT as the next run's `state`; specs assert that instead of `{}` | -| 6b | ag-ui/{client-tools, a2ui}, chat/a2ui | client-tools' fixture fronts the shared react-parity server (`fixtures/react-parity/native-ag-ui-client-tools`, also used by `scripts/react-parity` verification), whose causal checks read the model's received messages: needs a fixed aimock port and the aimock journal (`GET /__aimock/journal`). The A2UI fixtures feed dedicated CI proof steps (`chat-a2ui-*.spec.mjs`, `verify-ag-ui-a2ui.mjs`, the native probes) | | +| 6b (done) | ag-ui/client-tools | `aimockPort` (app port + 2000) on AG-UI backends and `/__model-requests` (aimock's `GET /__aimock/journal`); `/__requests` entries carry live `events`, `held` and `closed`; holds take `beforeType`, `persist` and `fromRequest` | the React fixture wrapped the shared react-parity server's `cockpit` scenario; that scenario becomes aimock fixtures and is removed from the parity worker, which stays for `scripts/react-parity` verification. The `prepare-fixture` target, its scripts, specs and CI steps go: `uv run` syncs the environment. The causal checks read the model's received messages from the journal. Graph, checkpointer and model identity and socket-attempt checks were the parity worker's own proofs; the process boundary replaces them, as for chat/messages | +| 6c | ag-ui/a2ui, chat/a2ui | the same parity-server pattern, plus each example's packed-application provenance proof (`ag-ui-a2ui-native-probe.mjs`, `chat-a2ui-native-probe.mjs`), their CI proof steps and retained application artifacts | each probe needs the debug probe's treatment (installed package against `createAimockBackend` + the proxy), so they migrate as their own batch | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | | n/a | render/* (6) | none | `adapter: 'none'`, no model backend | From 179a681b556c89eaf7e2fa5a91691c267fc0a182 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:43:33 -0800 Subject: [PATCH 34/41] test(cockpit): AG-UI identities carry the fixed aimock port Co-Authored-By: Claude Opus 5.5 --- scripts/cockpit-frontend-matrix.spec.mjs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index add728ab2..3d468cba1 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -330,14 +330,14 @@ test('Spec Rendering has a closed local fifteenth React identity', async () => { }); test('Subagents has a closed fourteenth React identity', async () => { const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); - assert.deepEqual(reactCockpitConfiguration('ag-ui-subagents'), { topic: 'subagents', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/subagents/react', base: '/ag-ui/subagents/react/', port: 4613, project: 'cockpit-ag-ui-subagents-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/subagents/python', 'fixtures': 'cockpit/ag-ui/subagents/angular/e2e/fixtures', 'backendPort': 5613, 'agentPath': '/agent'} }); + assert.deepEqual(reactCockpitConfiguration('ag-ui-subagents'), { topic: 'subagents', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/subagents/react', base: '/ag-ui/subagents/react/', port: 4613, project: 'cockpit-ag-ui-subagents-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/subagents/python', 'fixtures': 'cockpit/ag-ui/subagents/angular/e2e/fixtures', 'backendPort': 5613, 'aimockPort': 6613, 'agentPath': '/agent'} }); for (const key of ['subagents', 'ag-ui/subagents', '../ag-ui-subagents']) assert.throws(() => reactCockpitConfiguration(key), /Unsupported React cockpit topic/); }); test('JSON Render has a closed thirteenth React identity and all routes remain unique',async()=>{ const {reactCockpitConfiguration}=await import('./react-cockpit/configuration.mjs'); assert.deepEqual(reactCockpitConfiguration('ag-ui-json-render'),{ - topic:'json-render',adapter:'ag-ui',appPath:'cockpit/ag-ui/json-render/react',base:'/ag-ui/json-render/react/',port:4612,project:'cockpit-ag-ui-json-render-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/json-render/python', 'fixtures': 'cockpit/ag-ui/json-render/angular/e2e/fixtures', 'backendPort': 5612, 'agentPath': '/agent'}, + topic:'json-render',adapter:'ag-ui',appPath:'cockpit/ag-ui/json-render/react',base:'/ag-ui/json-render/react/',port:4612,project:'cockpit-ag-ui-json-render-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/json-render/python', 'fixtures': 'cockpit/ag-ui/json-render/angular/e2e/fixtures', 'backendPort': 5612, 'aimockPort': 6612, 'agentPath': '/agent'}, }); const configurations=['streaming','interrupts','memory','client-tools','persistence','durable-execution','subgraphs','time-travel','deployment-runtime','ag-ui-streaming','ag-ui-interrupts','ag-ui-tool-views','ag-ui-json-render','ag-ui-subagents'].map(reactCockpitConfiguration); for(const field of ['appPath','base','port','project'])assert.equal(new Set(configurations.map(configuration=>configuration[field])).size,14); @@ -348,7 +348,7 @@ test('AG-UI Tool Views has a closed twelfth identity with no overlapping runtime const { reactCockpitConfiguration } = await import('./react-cockpit/configuration.mjs'); assert.deepEqual(reactCockpitConfiguration('ag-ui-tool-views'), { topic: 'tool-views', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/tool-views/react', - base: '/ag-ui/tool-views/react/', port: 4611, project: 'cockpit-ag-ui-tool-views-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/tool-views/python', 'fixtures': 'cockpit/ag-ui/tool-views/angular/e2e/fixtures', 'backendPort': 5611, 'agentPath': '/agent'}, + base: '/ag-ui/tool-views/react/', port: 4611, project: 'cockpit-ag-ui-tool-views-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/tool-views/python', 'fixtures': 'cockpit/ag-ui/tool-views/angular/e2e/fixtures', 'backendPort': 5611, 'aimockPort': 6611, 'agentPath': '/agent'}, }); const configurations = [ 'streaming', 'interrupts', 'memory', 'client-tools', 'persistence', @@ -371,7 +371,7 @@ test('AG-UI Streaming has a closed React identity distinct from LangGraph Stream appPath: 'cockpit/ag-ui/streaming/react', base: '/ag-ui/streaming/react/', port: 4609, - project: 'cockpit-ag-ui-streaming-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/streaming/python', 'fixtures': 'cockpit/ag-ui/streaming/angular/e2e/fixtures', 'backendPort': 5609, 'agentPath': '/agent'}, + project: 'cockpit-ag-ui-streaming-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/streaming/python', 'fixtures': 'cockpit/ag-ui/streaming/angular/e2e/fixtures', 'backendPort': 5609, 'aimockPort': 6609, 'agentPath': '/agent'}, }); assert.equal( reactCockpitConfiguration('streaming').appPath, @@ -398,7 +398,7 @@ test('AG-UI Interrupts has a closed identity and all eleven React previews remai appPath: 'cockpit/ag-ui/interrupts/react', base: '/ag-ui/interrupts/react/', port: 4610, - project: 'cockpit-ag-ui-interrupts-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/interrupts/python', 'fixtures': 'cockpit/ag-ui/interrupts/angular/e2e/fixtures', 'backendPort': 5610, 'agentPath': '/agent/native'}, + project: 'cockpit-ag-ui-interrupts-react', backend: {'kind': 'aimock-ag-ui', 'python': 'cockpit/ag-ui/interrupts/python', 'fixtures': 'cockpit/ag-ui/interrupts/angular/e2e/fixtures', 'backendPort': 5610, 'aimockPort': 6610, 'agentPath': '/agent/native'}, }); assert.equal( reactCockpitConfiguration('interrupts').appPath, From 694193466405e6d94b12abe29cded7b88803f031 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:43:33 -0800 Subject: [PATCH 35/41] test(cockpit): run React chat/generative-ui e2e, integration and website preview on aimock (batch 2b) The decision, interpretation and title models of the old wire script become shared aimock fixtures, keyed on each node's system prompt; admission, the real dashboard tools, assessment and publication run in the real graph. Missing and invalid decisions and model failures are real model output. Faults no model output reaches (a throwing tool, malformed tool data, prose on the action parent) are dropped here: admission validates every argument and binding before execution and creates an empty parent. The Python tests that cover them are named in the specs. Saved-turn faults on the confirmation read are new proxy state modes (injected data). Co-Authored-By: Claude Opus 5.5 --- apps/website/playwright.config.ts | 2 +- apps/website/src/playwright-config.spec.ts | 4 +- .../angular/e2e/fixtures/c-generative-ui.json | 122 ++++ .../react/e2e/generative-ui.spec.ts | 178 ++---- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 3 + cockpit/chat/generative-ui/react/project.json | 17 +- .../react/src/connection.integration.spec.ts | 126 ++--- scripts/cockpit-frontend-matrix.spec.mjs | 4 + .../chat-generative-ui-fixture.mjs | 534 ------------------ .../react-cockpit/chat-generative-ui-wire.py | 222 -------- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/langgraph-proxy.mjs | 28 +- scripts/react-cockpit/serve.mjs | 17 +- 14 files changed, 260 insertions(+), 1004 deletions(-) create mode 100644 cockpit/chat/generative-ui/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/chat-generative-ui-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-generative-ui-wire.py diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 3055eb7b2..ada8a04e6 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -527,7 +527,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'npx nx run cockpit-chat-generative-ui-python:smoke && node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent', + command: 'node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4627', reuseExistingServer, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index a4788de16..2d906f743 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -222,7 +222,7 @@ describe('Website Playwright configuration', () => { expect(reactServers.find(server => server.url === 'http://127.0.0.1:4630/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4629')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-filesystem --no-parent --backend'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4628/__ready')?.command).toBe('node scripts/react-cockpit/serve.mjs deep-agents-planning --no-parent --backend'); - expect(reactServers.find(server => server.url === 'http://127.0.0.1:4627')?.command).toBe('npx nx run cockpit-chat-generative-ui-python:smoke && node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent'); + expect(reactServers.find(server => server.url === 'http://127.0.0.1:4627')?.command).toBe('node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent --backend'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4626')?.command).toBe('npx nx run cockpit-chat-timeline-python:smoke && node scripts/react-cockpit/serve.mjs chat-timeline --no-parent'); expect(reactServers.find(server => server.url === 'http://127.0.0.1:4625')?.command).toBe('npx nx run cockpit-chat-threads-python:smoke && node scripts/react-cockpit/serve.mjs chat-threads --no-parent'); expect(projects).toContain('cockpit-chat-subagents-react'); @@ -530,7 +530,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-subagents --no-parent --backend',url:'http://127.0.0.1:4630/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-skills --no-parent --backend',url:'http://127.0.0.1:4631'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs deep-agents-memory --no-parent --backend',url:'http://127.0.0.1:4632'}), - expect.objectContaining({command:'npx nx run cockpit-chat-generative-ui-python:smoke && node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent',url:'http://127.0.0.1:4627'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent --backend',url:'http://127.0.0.1:4627'}), expect.objectContaining({command:'npx nx run cockpit-chat-timeline-python:smoke && node scripts/react-cockpit/serve.mjs chat-timeline --no-parent',url:'http://127.0.0.1:4626'}), expect.objectContaining({command:'npx nx run cockpit-chat-threads-python:smoke && node scripts/react-cockpit/serve.mjs chat-threads --no-parent',url:'http://127.0.0.1:4625'}), expect.objectContaining({command:'npx nx run cockpit-chat-subagents-python:smoke && node scripts/react-cockpit/serve.mjs chat-subagents --no-parent',url:'http://127.0.0.1:4624'}), diff --git a/cockpit/chat/generative-ui/angular/e2e/fixtures/c-generative-ui.json b/cockpit/chat/generative-ui/angular/e2e/fixtures/c-generative-ui.json index aa1de776b..371a8259c 100644 --- a/cockpit/chat/generative-ui/angular/e2e/fixtures/c-generative-ui.json +++ b/cockpit/chat/generative-ui/angular/e2e/fixtures/c-generative-ui.json @@ -27,6 +27,128 @@ } ] } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Local dashboard title" } + }, + { + "match": { + "systemMessage": "Answer the user's interpretive question", + "userMessage": "(interpreter down)" + }, + "response": { + "error": { + "message": "PRIVATE interpretation failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { "systemMessage": "Answer the user's interpretive question" }, + "response": { "content": "On-time performance needs operational context to explain." } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "(no decision)" + }, + "response": { "content": "I will just describe the dashboard." } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "(invalid decision)" + }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"data_update\", \"calls\": [{\"name\": \"query_recent_disruptions\", \"args\": {\"type\": \"invented\"}}], \"interpretation\": \"\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "(provider down)" + }, + "response": { + "error": { + "message": "PRIVATE decision failure", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "cancelled" + }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"data_update\", \"calls\": [{\"name\": \"query_recent_disruptions\", \"args\": {\"limit\": 5, \"type\": \"cancelled\"}}], \"interpretation\": \"\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "Remove the" + }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"restructure\", \"calls\": [{\"name\": \"render_spec\", \"args\": {\"elements\": {\"root\": {\"type\": \"dashboard_grid\", \"children\": [\"stats_row\", \"charts_row\"]}, \"stats_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"on_time_card\", \"flights_card\", \"delay_card\", \"load_card\"]}, \"on_time_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On-time %\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"flights_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Flights Today\", \"value\": {\"$state\": \"/flights_today/value\"}, \"delta\": {\"$state\": \"/flights_today/delta\"}}}, \"delay_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Avg Delay\", \"value\": {\"$state\": \"/avg_delay/value\"}, \"delta\": {\"$state\": \"/avg_delay/delta\"}}}, \"load_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Load Factor\", \"value\": {\"$state\": \"/load_factor/value\"}, \"delta\": {\"$state\": \"/load_factor/delta\"}}}, \"charts_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"trend_chart\", \"airline_chart\"]}, \"trend_chart\": {\"type\": \"line_chart\", \"props\": {\"title\": \"On-time % Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airline_chart\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Flights by Airline\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}}, \"root\": \"root\"}}], \"interpretation\": \"\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "Why is on-time" + }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"interpret\", \"calls\": [], \"interpretation\": \"why is on-time performance changing?\"}" + } + ] + } + }, + { + "match": { + "systemMessage": "Confirmed checkpoint:", + "userMessage": "Show another dashboard" + }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"restructure\", \"calls\": [{\"name\": \"render_spec\", \"args\": {\"elements\": {\"root\": {\"type\": \"dashboard_grid\", \"children\": [\"stats_row\", \"charts_row\", \"table_section\"]}, \"stats_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"on_time_card\", \"flights_card\", \"delay_card\", \"load_card\"]}, \"on_time_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On-time %\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"flights_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Flights Today\", \"value\": {\"$state\": \"/flights_today/value\"}, \"delta\": {\"$state\": \"/flights_today/delta\"}}}, \"delay_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Avg Delay\", \"value\": {\"$state\": \"/avg_delay/value\"}, \"delta\": {\"$state\": \"/avg_delay/delta\"}}}, \"load_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Load Factor\", \"value\": {\"$state\": \"/load_factor/value\"}, \"delta\": {\"$state\": \"/load_factor/delta\"}}}, \"charts_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"trend_chart\", \"airline_chart\"]}, \"trend_chart\": {\"type\": \"line_chart\", \"props\": {\"title\": \"On-time % Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airline_chart\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Flights by Airline\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}, \"table_section\": {\"type\": \"data_grid\", \"props\": {\"title\": \"Recent Disruptions\", \"rows\": {\"$state\": \"/recent_disruptions\"}, \"columns\": [\"flight_number\", \"type\", \"minutes\", \"route\", \"date\"]}}}, \"root\": \"root\"}}], \"interpretation\": \"\"}" + } + ] + } + }, + { + "match": { "systemMessage": "Confirmed checkpoint:" }, + "response": { + "toolCalls": [ + { + "name": "plan_dashboard_operation", + "arguments": "{\"kind\": \"create\", \"calls\": [{\"name\": \"render_spec\", \"args\": {\"elements\": {\"root\": {\"type\": \"dashboard_grid\", \"children\": [\"stats_row\", \"charts_row\", \"table_section\"]}, \"stats_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"on_time_card\", \"flights_card\", \"delay_card\", \"load_card\"]}, \"on_time_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"On-time %\", \"value\": {\"$state\": \"/on_time/value\"}, \"delta\": {\"$state\": \"/on_time/delta\"}}}, \"flights_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Flights Today\", \"value\": {\"$state\": \"/flights_today/value\"}, \"delta\": {\"$state\": \"/flights_today/delta\"}}}, \"delay_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Avg Delay\", \"value\": {\"$state\": \"/avg_delay/value\"}, \"delta\": {\"$state\": \"/avg_delay/delta\"}}}, \"load_card\": {\"type\": \"stat_card\", \"props\": {\"label\": \"Load Factor\", \"value\": {\"$state\": \"/load_factor/value\"}, \"delta\": {\"$state\": \"/load_factor/delta\"}}}, \"charts_row\": {\"type\": \"container\", \"props\": {\"direction\": \"row\"}, \"children\": [\"trend_chart\", \"airline_chart\"]}, \"trend_chart\": {\"type\": \"line_chart\", \"props\": {\"title\": \"On-time % Trend\", \"data\": {\"$state\": \"/on_time_trend\"}, \"xKey\": \"month\", \"yKey\": \"on_time_pct\"}}, \"airline_chart\": {\"type\": \"bar_chart\", \"props\": {\"title\": \"Flights by Airline\", \"data\": {\"$state\": \"/flights_by_airline\"}, \"labelKey\": \"airline\", \"valueKey\": \"count\"}}, \"table_section\": {\"type\": \"data_grid\", \"props\": {\"title\": \"Recent Disruptions\", \"rows\": {\"$state\": \"/recent_disruptions\"}, \"columns\": [\"flight_number\", \"type\", \"minutes\", \"route\", \"date\"]}}}, \"root\": \"root\"}}, {\"name\": \"query_airline_kpis\", \"args\": {}}, {\"name\": \"query_on_time_trend\", \"args\": {}}, {\"name\": \"query_flights_by_airline\", \"args\": {}}, {\"name\": \"query_recent_disruptions\", \"args\": {}}], \"interpretation\": \"\"}" + } + ] + } } ] } diff --git a/cockpit/chat/generative-ui/react/e2e/generative-ui.spec.ts b/cockpit/chat/generative-ui/react/e2e/generative-ui.spec.ts index f5504caab..80e6eb987 100644 --- a/cockpit/chat/generative-ui/react/e2e/generative-ui.spec.ts +++ b/cockpit/chat/generative-ui/react/e2e/generative-ui.spec.ts @@ -4,19 +4,31 @@ import { type Page, type APIRequestContext, } from '@playwright/test'; -import { createHash } from 'node:crypto'; -import { readFileSync, writeFileSync } from 'node:fs'; -import { resolve } from 'node:path'; +import { writeFileSync } from 'node:fs'; +// Backend: aimock replays cockpit/chat/generative-ui/angular/e2e/fixtures (the +// decision, interpretation and title models, keyed on their system prompts) +// into a real `langgraph dev`, so admission, the real dashboard tools, +// assessment and publication all run in the graph. The recording proxy +// (scripts/react-cockpit/langgraph-proxy.mjs) holds, fails or rewrites reads. +// +// Faults inside the graph that no model output can reach are covered by the +// Python tests in cockpit/chat/generative-ui/python/tests/test_streaming.py, +// not here: a throwing tool or malformed tool data rolling back the whole +// batch (test_malformed_failed_missing_or_uncorrelated_results_roll_back_entire_batch, +// test_mixed_genuine_success_and_runtime_error_is_atomic), and prose already +// on the action parent (test_genuine_nonempty_parent_fallback_no_wrapper_or_data). +// Admission validates every query argument and binding before execution, and +// the parent it creates is always empty. const base = '/chat/generative-ui/react/'; const box = (page: Page) => page.getByRole('textbox', { name: 'Message', exact: true }); const boards = (page: Page) => page.locator('[data-dashboard-spec-owner]'); const requests = async (request: APIRequestContext) => (await request.get('/__requests')).json(); -const configure = async ( - request: APIRequestContext, - data: Record -) => expect((await request.post('/__configure', { data })).status()).toBe(200); +const control = async (request: APIRequestContext, path: string) => + expect((await request.post(path)).status()).toBe(200); +const lifetime = async (request: APIRequestContext) => + (await request.get('/__lifetime')).json(); async function send(page: Page, text: string) { await box(page).fill(text); await page.getByRole('button', { name: 'Send', exact: true }).click(); @@ -27,8 +39,6 @@ async function send(page: Page, text: string) { const errors = new WeakMap(); test.beforeEach(async ({ page, request }) => { expect((await request.post('/__reset')).status()).toBe(200); - // Prepare the real graph before UI assertions, without creating a conversation. - expect((await request.post('/__configure', { data: { failAgent: false } })).status()).toBe(200); const list: string[] = []; errors.set(page, list); page.on('pageerror', (e) => list.push(e.message)); @@ -38,39 +48,8 @@ test.beforeEach(async ({ page, request }) => { }); test.afterEach(async ({ page, request }) => { expect(errors.get(page)).toEqual([]); - const proofs = await (await request.get('/__graph-proof')).json(); - for (const proof of proofs) - expect(proof).toMatchObject({ - actualCompiledGraph: true, - networkConnectAttempts: 0, - titleMessageCallbacks: 0, - decisionMessageCallbacks: 0, - interpretationMessageCallbacks: 0, - productionSourceSha256: Object.fromEntries( - [ - 'src/graph.py', - 'src/dashboard_tools.py', - 'src/operations.py', - 'src/dashboard_contract.py', - 'prompts/generative-ui.md', - ].map((path) => [ - path, - createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/' + path))) - .digest('hex'), - ]) - ), - sourceSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/src/graph.py'))) - .digest('hex'), - lockSha256: createHash('sha256') - .update(readFileSync(resolve(__dirname, '../../python/uv.lock'))) - .digest('hex'), - }); await page.goto('about:blank'); - await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).held) - .toBe(0); + await expect.poll(async () => (await lifetime(request)).openResponses).toBe(0); }); test('mount is inert; four confirmed turns retain ownership and share refreshed data', async ({ page, @@ -155,12 +134,9 @@ test('mount is inert; four confirmed turns retain ownership and share refreshed captured.filter((r: { path: string }) => r.path === '/api/threads') ).toHaveLength(1); writeFileSync( - testInfo.outputPath('graph-proof.json'), + testInfo.outputPath('wire.json'), JSON.stringify( - { - requests: captured, - proofs: await (await request.get('/__graph-proof')).json(), - }, + { requests: captured, captures: await (await request.get('/__captures')).json() }, null, 2 ) @@ -176,35 +152,6 @@ test('mount is inert; four confirmed turns retain ownership and share refreshed fullPage: true, }); }); -test('nonempty parent preserves prose with inspectable raw result and notice', async ({ - page, - request, -}) => { - await page.goto(base); - await send(page, 'prose render'); - await expect(boards(page)).toHaveCount(0); - await expect(page.getByText('Here is the layout.')).toBeVisible(); - await expect( - page.getByText( - 'This layout was not applied because the assistant already supplied prose. The tool result is available for inspection.' - ) - ).toBeVisible(); - await page.locator('details').filter({ has: page.getByText('Server tool result', { exact: true }) }).locator('summary').first().click(); - await expect( - page - .locator('details[open] > pre') - .filter({ hasText: 'dashboard_grid' }) - .first() - ).toBeVisible(); - expect(await requests(request)).toHaveLength(5); - const proofs = await (await request.get('/__graph-proof')).json(); - expect( - proofs.some( - (p: { op: string; fixtureControls: { prose_parent: boolean } }) => - p.op === 'submit' && p.fixtureControls.prose_parent - ) - ).toBe(true); -}); for (const decisionFault of ['missing', 'invalid']) test(`saved ${decisionFault} decision honestly leaves the prior board unchanged`, async ({ page, @@ -214,8 +161,8 @@ for (const decisionFault of ['missing', 'invalid']) await send(page, 'Show dashboard'); const old = await boards(page).innerText(); const owner = await boards(page).getAttribute('data-dashboard-spec-owner'); - await configure(request, { decisionFault }); - await send(page, 'Filter to cancelled flights'); + // Real model output that admission rejects: no decision, or an invented filter. + await send(page, `Filter to cancelled flights (${decisionFault === 'missing' ? 'no' : 'invalid'} decision)`); await expect(boards(page)).toHaveCount(1); expect(await boards(page).innerText()).toBe(old); expect(await boards(page).getAttribute('data-dashboard-spec-owner')).toBe( @@ -233,47 +180,6 @@ for (const decisionFault of ['missing', 'invalid']) ) ).toHaveLength(2); }); -for (const toolFault of ['failKpis', 'malformedTrend']) - test(`real ${toolFault} batch keeps successful peers inspectable but cannot replace the confirmed board`, async ({ - page, - request, - }) => { - await configure(request, { partialDashboard: true }); - await page.goto(base); - await send(page, 'Show dashboard'); - const old = await boards(page).innerText(); - const owner = await boards(page).getAttribute('data-dashboard-spec-owner'); - await configure(request, { partialDashboard: false, [toolFault]: true }); - await box(page).fill('Show another dashboard'); - await box(page).press('Enter'); - await expect(page.getByRole('alert')).toContainText( - 'could not be confirmed' - ); - await expect(boards(page)).toHaveCount(1); - expect(await boards(page).innerText()).toBe(old); - expect(await boards(page).getAttribute('data-dashboard-spec-owner')).toBe( - owner - ); - await expect(box(page)).toBeDisabled(); - await expect( - page.getByText('Start a new conversation to continue.', { exact: true }) - ).toBeVisible(); - await page.locator('details').filter({ has: page.getByText('Server tool result', { exact: true }) }).locator('summary').last().click(); - await expect( - page - .locator('details[open] > pre') - .filter({ hasText: 'on_time_pct' }) - .last() - ).toBeVisible(); - expect( - (await requests(request)).filter((r: { path: string }) => - r.path.endsWith('/runs/stream') - ) - ).toHaveLength(2); - await page.getByRole('button', { name: 'New conversation' }).click(); - await expect(boards(page)).toHaveCount(0); - await expect(box(page)).toBeEnabled(); - }); test('failed read-only interpretation retains the confirmed board and requires New', async ({ page, request, @@ -281,8 +187,8 @@ test('failed read-only interpretation retains the confirmed board and requires N await page.goto(base); await send(page, 'Show dashboard'); const old = await boards(page).innerText(); - await configure(request, { failInterpretation: true }); - await box(page).fill('Why is on-time low?'); + // The interpretation model fails for this prompt (fixture). + await box(page).fill('Why is on-time low? (interpreter down)'); await box(page).press('Enter'); await expect(page.getByRole('alert')).toBeVisible(); await expect(boards(page)).toHaveCount(1); @@ -301,14 +207,16 @@ test('pending update keeps the old board; double submit, Stop and New cannot pub await page.goto(base); await send(page, 'Show dashboard'); const old = await boards(page).innerText(); - await configure(request, { holdStream: true }); + // The decision is not streamed and a data update writes no model text: + // hold the run right after its first event. + expect((await request.post('/__hold/stream', { data: { immediate: true } })).status()).toBe(200); await box(page).fill('Filter to only the cancelled flights.'); await box(page).press('Enter'); await box(page).press('Enter'); await expect(box(page)).toHaveValue(''); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).held) - .toBe(1); + .poll(async () => (await lifetime(request)).activeStream) + .toBe(true); expect(await boards(page).innerText()).toBe(old); expect( (await requests(request)).filter((r: { path: string }) => @@ -323,17 +231,17 @@ test('pending update keeps the old board; double submit, Stop and New cannot pub await request.post('/__release'); await page.getByRole('button', { name: 'New conversation' }).click(); await expect(boards(page)).toHaveCount(0); - await configure(request, { holdStream: false, holdState: true }); + // The confirmation reads the exact checkpoint. + await control(request, '/__hold/state'); await box(page).fill('New dashboard'); await box(page).press('Enter'); await expect - .poll(async () => (await (await request.get('/__lifetime')).json()).held) - .toBe(1); + .poll(async () => (await lifetime(request)).activeState) + .toBe(true); await page.getByRole('button', { name: 'New conversation' }).click(); - await request.post('/__release'); + await request.post('/__release-state'); await expect(boards(page)).toHaveCount(0); await expect(box(page)).toBeEnabled(); - await configure(request, { holdState: false }); await send(page, 'Fresh dashboard'); await expect(boards(page)).toHaveCount(1); expect( @@ -355,7 +263,8 @@ for (const fault of [ await page.goto(base); await send(page, 'Show dashboard'); const old = await boards(page).innerText(); - await configure(request, { stateFault: fault }); + // The confirmation's checkpoint read is rewritten (injected data). + await control(request, '/__state/' + (fault === 'pending-tasks' ? 'unsafe' : fault)); await box(page).fill('Show another dashboard'); await box(page).press('Enter'); await expect(page.getByRole('alert')).toContainText( @@ -370,13 +279,12 @@ for (const fault of [ ) ).toHaveLength(2); }); -for (const options of [{ runStatus: 'error' }, { failAgent: true }]) - test(`run failure ${JSON.stringify( - options - )} leaves no provisional layout`, async ({ page, request }) => { +for (const failure of ['run status error', 'decision provider down']) + test(`run failure (${failure}) leaves no provisional layout`, async ({ page, request }) => { await page.goto(base); - await configure(request, options); - await box(page).fill('Show dashboard'); + // The run reports status error; or the decision model fails (fixture). + if (failure === 'run status error') await control(request, '/__run-status/error'); + await box(page).fill(failure === 'run status error' ? 'Show dashboard' : 'Show dashboard (provider down)'); await box(page).press('Enter'); await expect(page.getByRole('alert')).toBeVisible(); await expect(boards(page)).toHaveCount(0); diff --git a/cockpit/chat/generative-ui/react/e2e/global-setup-impl.ts b/cockpit/chat/generative-ui/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..9bd41ff6d --- /dev/null +++ b/cockpit/chat/generative-ui/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/generative-ui', port: 4627 }); diff --git a/cockpit/chat/generative-ui/react/e2e/playwright.config.ts b/cockpit/chat/generative-ui/react/e2e/playwright.config.ts index 531cb9f5c..38f7b2e59 100644 --- a/cockpit/chat/generative-ui/react/e2e/playwright.config.ts +++ b/cockpit/chat/generative-ui/react/e2e/playwright.config.ts @@ -6,6 +6,9 @@ export default defineConfig({ timeout: 30000, use: { baseURL: 'http://127.0.0.1:4627', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/generative-ui/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-generative-ui --no-parent', diff --git a/cockpit/chat/generative-ui/react/project.json b/cockpit/chat/generative-ui/react/project.json index cec3d64fc..00e58a2ec 100644 --- a/cockpit/chat/generative-ui/react/project.json +++ b/cockpit/chat/generative-ui/react/project.json @@ -3,6 +3,7 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/chat/generative-ui/react/src", "projectType": "application", + "implicitDependencies": ["cockpit-chat-generative-ui-angular", "cockpit-chat-generative-ui-python"], "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], "targets": { "build": { @@ -32,24 +33,12 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["build", "fixture-test"], + "inputs": ["default", "reactAimockBackend"], + "dependsOn": ["build", { "target": "test", "projects": ["cockpit-chat-generative-ui-python"] }], "options": { "config": "cockpit/chat/generative-ui/react/e2e/playwright.config.ts", "skipInstall": true } - }, - "fixture-test": { - "executor": "nx:run-commands", - "cache": false, - "dependsOn": [ - { - "target": "test", - "projects": ["cockpit-chat-generative-ui-python"] - } - ], - "options": { - "command": "node --test scripts/react-cockpit/chat-generative-ui-fixture.spec.mjs" - } } } } diff --git a/cockpit/chat/generative-ui/react/src/connection.integration.spec.ts b/cockpit/chat/generative-ui/react/src/connection.integration.spec.ts index 444628199..4a431402b 100644 --- a/cockpit/chat/generative-ui/react/src/connection.integration.spec.ts +++ b/cockpit/chat/generative-ui/react/src/connection.integration.spec.ts @@ -1,25 +1,40 @@ -import { createServer } from 'node:http'; +import { createServer, type Server } from 'node:http'; import { once } from 'node:events'; import type { AddressInfo } from 'node:net'; -import { expect, it, vi, onTestFinished } from 'vitest'; +import { resolve } from 'node:path'; +import { afterAll, beforeAll, expect, it, vi, onTestFinished } from 'vitest'; import { createConnectedApplication } from './connection'; + +/* eslint-disable @nx/enforce-module-boundaries -- Local test backend tooling is never bundled. */ +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createAimockBackend } from '../../../../../scripts/react-cockpit/aimock-backend.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createLanggraphProxy } from '../../../../../scripts/react-cockpit/langgraph-proxy.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { reactCockpitConfiguration } from '../../../../../scripts/react-cockpit/configuration.mjs'; +/* eslint-enable @nx/enforce-module-boundaries */ + +// The real graph runs under `langgraph dev` on aimock fixtures shared with the +// Angular example (cockpit/chat/generative-ui/angular/e2e/fixtures); the +// in-process proxy records requests and holds or fails the next call +// (scripts/react-cockpit/langgraph-proxy.mjs). Faults no model output can +// reach (a throwing tool, malformed tool data, prose on the action parent) +// are covered by cockpit/chat/generative-ui/python/tests/test_streaming.py: +// test_malformed_failed_missing_or_uncorrelated_results_roll_back_entire_batch, +// test_mixed_genuine_success_and_runtime_error_is_atomic and +// test_genuine_nonempty_parent_fallback_no_wrapper_or_data. +const { backend: config } = reactCockpitConfiguration('chat-generative-ui'); +const backend = createAimockBackend({ root: resolve(__dirname, '../../../../..'), backend: config }); +beforeAll(() => backend.ensure(), 180000); +afterAll(() => backend.close()); +type Handler = (request: unknown, response: unknown, pathname: string) => Promise; async function fixture() { - const location = new URL( - '../../../../../scripts/react-cockpit/chat-generative-ui-fixture.mjs', - import.meta.url - ).href; - const { createChatGenerativeUiFixture } = await import( - /* @vite-ignore */ location - ); - const handle = createChatGenerativeUiFixture(); - const server = createServer(async (req, res) => { - if ( - !(await handle( - req, - res, - new URL(req.url ?? '', 'http://localhost').pathname - )) - ) { + const handle: Handler = createLanggraphProxy({ + upstream: `http://127.0.0.1:${config.langgraphPort}`, + ready: () => backend.ensure(), + }); + const server: Server = createServer(async (req, res) => { + if (!(await handle(req, res, new URL(req.url ?? '', 'http://localhost').pathname))) { res.writeHead(404); res.end(); } @@ -33,18 +48,12 @@ async function fixture() { }); onTestFinished(async () => { await app.dispose(); - await handle.close(); server.closeAllConnections(); await new Promise((r) => server.close(() => r())); }); - const control = async (body: object) => { + const control = async (path: string, body?: object) => { expect( - ( - await fetch(base + '/__configure', { - method: 'POST', - body: JSON.stringify(body), - }) - ).status + (await fetch(base + path, { method: 'POST', ...(body ? { body: JSON.stringify(body) } : {}) })).status ).toBe(200); }; const requests = async () => @@ -93,24 +102,16 @@ it('actual default session confirms initial, cancelled filter, structural and pr requests.filter((r) => r.path.endsWith('/state/checkpoint')) ).toHaveLength(4); }, 60000); -it('actual nonempty-parent render keeps prose and raw result without mounting a new layout', async () => { - const f = await fixture(); - expect(await f.app.submit('Show dashboard')).toBe(true); - const prior = f.app.getSnapshot(); - expect(await f.app.submit('prose render')).toBe(true); - expect(f.app.getSnapshot().surfaces).toEqual(prior.surfaces); - expect(f.app.getSnapshot().notices).toHaveLength(1); - const last = f.app.getSnapshot().toolCalls.at(-1); - expect(last?.status === 'complete' && last.result).not.toBe('rendered'); -}, 60000); it.each(['missing', 'invalid'])( 'actual %s decision confirms an honest saved no-op with the prior dashboard', async (decisionFault) => { const f = await fixture(); expect(await f.app.submit('Show dashboard')).toBe(true); const prior = f.app.getSnapshot(); - await f.control({ decisionFault }); - expect(await f.app.submit('Filter to cancelled flights')).toBe(true); + // Real model output that admission rejects (fixtures). + expect( + await f.app.submit(`Filter to cancelled flights (${decisionFault === 'missing' ? 'no' : 'invalid'} decision)`) + ).toBe(true); const saved = f.app.getSnapshot(); expect(saved.dashboard).toEqual(prior.dashboard); expect(saved.surfaces).toEqual(prior.surfaces); @@ -125,47 +126,12 @@ it.each(['missing', 'invalid'])( }, 60000 ); -it.each(['failKpis', 'malformedTrend'])( - 'actual %s structural rollback stays unconfirmed with real successful peers', - async (toolFault) => { - const f = await fixture(); - await f.control({ partialDashboard: true }); - expect(await f.app.submit('Show dashboard')).toBe(true); - const prior = f.app.getSnapshot(); - expect(Object.keys(prior.dashboard).sort()).toEqual([ - 'flights_by_airline', - 'recent_disruptions', - ]); - await f.control({ partialDashboard: false, [toolFault]: true }); - expect(await f.app.submit('Show another dashboard')).toBe(false); - const failed = f.app.getSnapshot(); - expect(failed.dashboard).toBe(prior.dashboard); - expect(failed.surfaces).toBe(prior.surfaces); - expect(failed.canSubmit).toBe(false); - const render = failed.toolCalls - .filter((c) => c.name === 'render_spec') - .at(-1); - expect(render?.status === 'complete' && render.result).not.toBe('rendered'); - const peer = failed.toolCalls.find((c) => c.name === 'query_on_time_trend'); - expect(peer?.status).toBe('complete'); - const count = (await f.requests()).length; - expect(await f.app.submit('Try again')).toBe(false); - expect(await f.requests()).toHaveLength(count); - await f.app.newConversation(); - expect(f.app.getSnapshot()).toMatchObject({ - canSubmit: true, - surfaces: [], - dashboard: {}, - }); - }, - 60000 -); it('actual failed read-only interpretation retains the confirmed dashboard and requires New', async () => { const f = await fixture(); expect(await f.app.submit('Show dashboard')).toBe(true); const prior = f.app.getSnapshot(); - await f.control({ failInterpretation: true }); - expect(await f.app.submit('Why is on-time low?')).toBe(false); + // The interpretation model fails for this prompt (fixture). + expect(await f.app.submit('Why is on-time low? (interpreter down)')).toBe(false); expect(f.app.getSnapshot().dashboard).toBe(prior.dashboard); expect(f.app.getSnapshot().surfaces).toBe(prior.surfaces); expect(f.app.getSnapshot().canSubmit).toBe(false); @@ -177,13 +143,14 @@ it('actual pending task rejects confirmation and Stop preserves the confirmed bo const f = await fixture(); expect(await f.app.submit('Show dashboard')).toBe(true); const prior = f.app.getSnapshot(); - await f.control({ stateFault: 'pending-tasks' }); + await f.control('/__state/unsafe'); expect(await f.app.submit('Filter to cancelled flights')).toBe(false); expect(f.app.getSnapshot().surfaces).toBe(prior.surfaces); expect(f.app.getSnapshot().dashboard).toBe(prior.dashboard); expect(f.app.getSnapshot().canSubmit).toBe(false); await f.app.newConversation(); - await f.control({ stateFault: null, holdStream: true }); + await f.control('/__state/safe'); + await f.control('/__hold/stream', { immediate: true }); const pending = f.app.submit('Show dashboard'); await vi.waitFor(async () => expect( @@ -214,7 +181,8 @@ it.each([ const f = await fixture(); expect(await f.app.submit('Show dashboard')).toBe(true); const prior = f.app.getSnapshot(); - await f.control({ stateFault: fault }); + // The confirmation's checkpoint read is rewritten (injected data). + await f.control('/__state/' + fault); expect( await f.app.submit( fault === 'malformed-layout' @@ -235,7 +203,7 @@ it.each(['running', 'error'])( 'actual independent exact run status %s cannot grant confirmation', async (runStatus) => { const f = await fixture(); - await f.control({ runStatus }); + await f.control('/__run-status/' + runStatus); expect(await f.app.submit('Show dashboard')).toBe(false); expect(f.app.getSnapshot()).toMatchObject({ canSubmit: false, diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 3d468cba1..00c8c2125 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -686,6 +686,10 @@ test('native chat generative UI has an exact isolated key and route', async () = base: '/chat/generative-ui/react/', port: 4627, project: 'cockpit-chat-generative-ui-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/generative-ui/python', + fixtures: 'cockpit/chat/generative-ui/angular/e2e/fixtures', langgraphPort: 5627, + }, }); for (const key of [ 'generative-ui', diff --git a/scripts/react-cockpit/chat-generative-ui-fixture.mjs b/scripts/react-cockpit/chat-generative-ui-fixture.mjs deleted file mode 100644 index c675b3dad..000000000 --- a/scripts/react-cockpit/chat-generative-ui-fixture.mjs +++ /dev/null @@ -1,534 +0,0 @@ -/** Local installed-SDK fixture. Real graph checkpoints never become public assets. */ -import { spawn } from 'node:child_process'; -import { createInterface } from 'node:readline'; -import { readFileSync } from 'node:fs'; -import { randomUUID, createHash } from 'node:crypto'; -import { fileURLToPath } from 'node:url'; - -function graphWorker(spawnWorker, workerTimeoutMs) { - const child = spawnWorker(); - let sequence = 0, - dead = false, - closing; - const pending = new Map(); - const ended = new Promise((resolve) => child.once('close', resolve)); - const lines = createInterface({ input: child.stdout }); - child.stderr.resume(); - function fail() { - dead = true; - for (const entry of pending.values()) { - clearTimeout(entry.timer); - entry.reject(new Error('Graph worker unavailable.')); - } - pending.clear(); - } - child.once('error', fail); - child.once('close', () => { - fail(); - lines.close(); - }); - child.stdin.on('error', fail); - lines.once('close', () => { - if (!dead) { - fail(); - child.kill('SIGTERM'); - } - }); - lines.on('line', (line) => { - try { - if (line.length > 16 * 1024 * 1024) - throw new Error('Oversized worker response'); - const result = JSON.parse(line), - entry = pending.get(result.requestId); - if (!entry) throw new Error('Unowned worker response'); - pending.delete(result.requestId); - clearTimeout(entry.timer); - if (result.error) entry.reject(new Error('Graph operation failed.')); - else entry.resolve(result); - } catch { - fail(); - child.kill('SIGTERM'); - } - }); - return { - pid: child.pid, - send(value) { - if (dead) return Promise.reject(new Error('Graph worker closed.')); - return new Promise((resolve, reject) => { - const requestId = ++sequence; - const timer = setTimeout(() => { - fail(); - child.kill('SIGTERM'); - }, workerTimeoutMs); - pending.set(requestId, { resolve, reject, timer }); - child.stdin.write(JSON.stringify({ ...value, requestId }) + '\n'); - }); - }, - close() { - if (closing) return closing; - fail(); - child.stdin.end(); - closing = (async () => { - const timer = setTimeout(() => child.kill('SIGKILL'), 1500); - await ended; - clearTimeout(timer); - lines.close(); - })(); - return closing; - }, - }; -} - -export function createChatGenerativeUiFixture({ - workerTimeoutMs = 30000, - spawnWorker = () => - spawn( - 'uv', - [ - 'run', - '--frozen', - '--python', - '3.12', - '--project', - 'cockpit/chat/generative-ui/python', - 'python', - 'scripts/react-cockpit/chat-generative-ui-wire.py', - ], - { - cwd: fileURLToPath(new URL('../../', import.meta.url)), - env: { - ...process.env, - UV_CACHE_DIR: - process.env.UV_CACHE_DIR ?? '/tmp/threadplane-genui-uv-cache', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ), -} = {}) { - const expected = Object.fromEntries( - [ - ['sourceSha256', 'src/graph.py'], - ['lockSha256', 'uv.lock'], - ].map(([key, path]) => [ - key, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/chat/generative-ui/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - const productionSourceSha256 = Object.fromEntries( - [ - 'src/graph.py', - 'src/dashboard_tools.py', - 'src/operations.py', - 'src/dashboard_contract.py', - 'prompts/generative-ui.md', - ].map((path) => [ - path, - createHash('sha256') - .update( - readFileSync( - new URL( - '../../cockpit/chat/generative-ui/python/' + path, - import.meta.url - ) - ) - ) - .digest('hex'), - ]) - ); - let worker, - closed = false, - closing, - queue = Promise.resolve(); - const threads = new Set(), - runs = new Map(), - requests = [], - proofs = [], - held = new Set(); - const options = { - stateFault: null, - streamFault: null, - runStatus: null, - holdStream: false, - holdState: false, - }; - const json = (res, data, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(data)); - } - }; - const event = (kind, data) => - `event: ${kind}\ndata: ${JSON.stringify(data)}\n\n`; - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; - const only = (value, keys) => - value !== null && - typeof value === 'object' && - !Array.isArray(value) && - Object.keys(value).every((k) => keys.includes(k)); - function operate(value) { - const result = queue.then(async () => { - if (closed) throw Error('Closed fixture'); - worker ??= graphWorker(spawnWorker, workerTimeoutMs); - const result = await worker.send(value); - if ( - closed || - !result.proof?.actualCompiledGraph || - result.proof.networkConnectAttempts !== 0 || - result.proof.titleMessageCallbacks !== 0 || - result.proof.decisionMessageCallbacks !== 0 || - result.proof.interpretationMessageCallbacks !== 0 || - result.proof.op !== value.op || - result.proof.threadId !== (value.threadId ?? null) || - Object.entries(expected).some( - ([key, hash]) => result.proof[key] !== hash - ) || - Object.keys(result.proof.productionSourceSha256 ?? {}).length !== - Object.keys(productionSourceSha256).length || - Object.entries(productionSourceSha256).some( - ([path, hash]) => result.proof.productionSourceSha256?.[path] !== hash - ) - ) - throw Error('Invalid graph proof'); - proofs.push(result.proof); - return result; - }); - queue = result.catch(() => undefined); - return result; - } - function distort(state, fault) { - const s = structuredClone(state), - v = s.values; - if (!fault || !v) return s; - if (fault === 'missing-terminal') delete v.completed_turn_id; - if (fault === 'stale-terminal') - v.completed_turn_id = - v.messages.find((m) => m.type === 'human')?.id + '-stale'; - if (fault === 'foreign-terminal') v.completed_answer_id = randomUUID(); - if (fault === 'old-message' && v.messages.length) - v.messages[0].content = 'Changed prior content'; - if (fault === 'old-data' && v.dashboard?.on_time) - v.dashboard.on_time.value = '999%'; - if (fault === 'incomplete-tools') - v.messages = v.messages.filter((m) => m.name !== 'render_spec'); - if (fault === 'malformed-layout') { - const m = v.messages.find((m) => - m.tool_calls?.some((c) => c.name === 'render_spec') - ); - if (m) m.content = '{"elements":{},"root":"missing"}'; - } - if (fault === 'missing-dashboard') delete v.dashboard; - if (fault === 'disagreeing-dashboard' && v.dashboard?.recent_disruptions) - v.dashboard.recent_disruptions = []; - if (fault === 'pending-tasks') - s.tasks = [{ id: 'pending', name: 'respond', interrupts: [] }]; - if (fault === 'task-error') - s.tasks = [ - { - id: 'failed', - name: 'respond', - error: 'Local failure', - interrupts: [], - }, - ]; - return s; - } - function hold(response, finish, run) { - const entry = { response, finish, run }; - held.add(entry); - response.once('close', () => { - if (held.delete(entry) && run) run.status = 'interrupted'; - }); - } - const handle = async (request, response, pathname) => { - if (!pathname.startsWith('/api/') && !pathname.startsWith('/__')) - return false; - try { - if (closed) throw Error('Closed fixture'); - if ( - !['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes( - request.socket.remoteAddress - ) - ) { - json(response, {}, 403); - return true; - } - let input = ''; - for await (const chunk of request) { - input += chunk; - if (input.length > 1024 * 1024) throw Error('Oversized request'); - } - const body = input ? JSON.parse(input) : {}; - if (!only(body, Object.keys(body ?? {}))) { - json(response, {}, 400); - return true; - } - if (pathname === '/__requests') { - json(response, structuredClone(requests)); - return true; - } - if (pathname === '/__graph-proof') { - json(response, proofs); - return true; - } - if (pathname === '/__lifetime') { - json(response, { - workers: worker ? 1 : 0, - workerPid: worker?.pid, - held: held.size, - }); - return true; - } - if (pathname === '/__configure') { - Object.assign(options, body); - if ( - [ - 'failTitle', - 'failAgent', - 'failInterpretation', - 'failMetadata', - 'failWrite', - 'mapped', - 'decisionFault', - 'partialDashboard', - 'failKpis', - 'malformedTrend', - ].some((key) => key in body) - ) - await operate({ - op: 'configure', - failTitle: options.failTitle ?? false, - failAgent: options.failAgent ?? false, - failInterpretation: options.failInterpretation ?? false, - failMetadata: options.failMetadata ?? false, - failWrite: options.failWrite ?? false, - mapped: options.mapped ?? false, - decisionFault: options.decisionFault ?? null, - partialDashboard: options.partialDashboard ?? false, - failKpis: options.failKpis ?? false, - malformedTrend: options.malformedTrend ?? false, - }); - json(response, {}); - return true; - } - if (pathname === '/__release') { - for (const entry of held) { - held.delete(entry); - entry.finish(); - } - json(response, {}); - return true; - } - if (pathname.startsWith('/__')) { - json(response, {}, 404); - return true; - } - requests.push( - structuredClone({ path: pathname, method: request.method, body }) - ); - if (pathname === '/api/threads' && request.method === 'POST') { - if ( - !uuid.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - Object.keys(body).some( - (k) => !['thread_id', 'if_exists', 'metadata'].includes(k) - ) || - (body.if_exists !== undefined && body.if_exists !== 'raise') || - (body.metadata !== undefined && !only(body.metadata, [])) - ) { - json(response, {}, 400); - return true; - } - threads.add(body.thread_id); - json(response, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(pathname); - if (!route || !threads.has(route[1])) { - json(response, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && request.method === 'GET') || - (suffix === '/state/checkpoint' && request.method === 'POST') - ) { - if ( - suffix === '/state/checkpoint' && - (!only(body, ['checkpoint']) || - !only(body.checkpoint, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - typeof body.checkpoint.checkpoint_id !== 'string' || - !body.checkpoint.checkpoint_id || - typeof body.checkpoint.checkpoint_ns !== 'string' || - body.checkpoint.thread_id !== threadId || - (body.checkpoint.checkpoint_map !== undefined && - (!only(body.checkpoint.checkpoint_map, [ - body.checkpoint.checkpoint_ns, - ]) || - Object.values(body.checkpoint.checkpoint_map).some( - (id) => id !== body.checkpoint.checkpoint_id - )))) - ) { - json(response, {}, 400); - return true; - } - if (body.checkpoint) { - const { history } = await operate({ op: 'history', threadId }); - if ( - body.checkpoint.thread_id !== threadId || - !history.some( - (s) => - s.checkpoint.checkpoint_id === body.checkpoint.checkpoint_id && - s.checkpoint.checkpoint_ns === body.checkpoint.checkpoint_ns - ) - ) { - json(response, {}, 400); - return true; - } - } - const { state } = await operate({ - op: 'state', - threadId, - checkpoint: body.checkpoint, - }); - const delivered = distort(state, options.stateFault); - const finish = () => json(response, delivered); - if (options.holdState) hold(response, finish); - else finish(); - return true; - } - if (suffix === '/history' && request.method === 'POST') { - const { history } = await operate({ op: 'history', threadId }); - json( - response, - history - .slice(0, body.limit ?? 10) - .map((s) => distort(s, options.stateFault)) - ); - return true; - } - if (/^\/runs\/[^/]+$/.test(suffix) && request.method === 'GET') { - const run = runs.get(suffix.split('/').at(-1)); - json( - response, - run?.thread_id === threadId - ? { ...run, status: options.runStatus ?? run.status } - : {}, - run?.thread_id === threadId ? 200 : 404 - ); - return true; - } - if (suffix === '/runs/stream' && request.method === 'POST') { - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !only(body.input, ['messages']) || - !only(message, ['type', 'id', 'content']) || - body.assistant_id !== 'c-generative-ui' || - body.input?.messages?.length !== 1 || - message.type !== 'human' || - typeof message.content !== 'string' || - typeof message.id !== 'string' || - !message.id.trim() || - !Array.isArray(body.stream_mode) || - !body.stream_mode.length || - new Set(body.stream_mode).size !== body.stream_mode.length || - body.stream_mode.some( - (m) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'custom', - 'checkpoints', - ].includes(m) - ) || - (body.stream_subgraphs !== undefined && - body.stream_subgraphs !== true) || - (body.stream_resumable !== undefined && - body.stream_resumable !== true) || - (body.on_disconnect !== undefined && - body.on_disconnect !== 'continue') - ) { - json(response, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - if (response.destroyed) { - run.status = 'interrupted'; - return true; - } - const modes = new Set( - body.stream_mode.map((m) => (m === 'messages-tuple' ? 'messages' : m)) - ); - const events = wire.events - .filter(([kind]) => modes.has(kind) || kind === 'error') - .map(([kind, data]) => [ - kind, - kind === 'values' - ? distort({ values: data }, options.streamFault).values - : data, - ]); - response.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - response.write(event('metadata', { run_id: run.run_id })); - const finish = () => { - if (response.destroyed || closed) return; - for (const [kind, data] of events) response.write(event(kind, data)); - run.status = wire.graphFailure ? 'error' : 'success'; - response.end(); - }; - if (options.holdStream) hold(response, finish, run); - else finish(); - return true; - } - json(response, {}, 404); - return true; - } catch { - json(response, { error: 'Graph fixture failed.' }, 500); - return true; - } - }; - handle.close = () => - (closing ??= (async () => { - closed = true; - for (const entry of held) entry.response.end(); - held.clear(); - await worker?.close(); - })()); - return handle; -} diff --git a/scripts/react-cockpit/chat-generative-ui-wire.py b/scripts/react-cockpit/chat-generative-ui-wire.py deleted file mode 100644 index 7f5a60f72..000000000 --- a/scripts/react-cockpit/chat-generative-ui-wire.py +++ /dev/null @@ -1,222 +0,0 @@ -"""Persistent network-free worker executing the actual compiled Generative UI graph.""" -import asyncio -from contextlib import redirect_stdout -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -os.environ["LANGSMITH_TRACING"] = "false" -os.environ["LANGCHAIN_TRACING_V2"] = "false" -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/generative-ui/python" -sys.path.insert(0, str(PROJECT)) -sys.path.insert(0, str(PROJECT / "tests")) -from test_streaming import LocalModel, call -from langchain_core.messages import AIMessage -from copy import deepcopy -from langchain_core.runnables import RunnableLambda -from langchain_core.tools import StructuredTool -from langgraph.prebuilt import ToolNode -from src.dashboard_contract import SLOT_TO_TOOL, bindings, resolve - -PRODUCTION_SOURCES = ("src/graph.py", "src/dashboard_tools.py", "src/operations.py", "src/dashboard_contract.py", "prompts/generative-ui.md") - -class Metadata: - def __init__(self): - self.titles = {} - self.fail_read = False - self.fail_write = False - - async def get(self, thread_id): - if self.fail_read: - raise RuntimeError("Metadata unavailable") - return {"thread_id": thread_id, "metadata": self.titles.get(thread_id, {})} - - async def update(self, thread_id, metadata): - if self.fail_write: - raise RuntimeError("Metadata unavailable") - self.titles[thread_id] = dict(metadata) - -class AuthoredClient: - def __init__(self): - self.threads = Metadata() - -from langgraph.checkpoint.memory import InMemorySaver - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - raise TypeError("Unsupported graph wire value") - - -def checkpoint(state, mapped=False): - result = { - "values": state.values, "next": list(state.next), - "checkpoint": {k: v for k, v in state.config["configurable"].items() if k in ["thread_id", "checkpoint_ns", "checkpoint_id", "checkpoint_map"]}, - "parent_checkpoint": dict(state.parent_config["configurable"]) if state.parent_config else None, - "created_at": state.created_at, "metadata": state.metadata, - "tasks": [{"id": task.id, "name": task.name, - **({"error": task.error} if task.error is not None else {}), - "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts]} - for task in state.tasks], - } - if mapped: - result["checkpoint"]["checkpoint_map"] = {"": result["checkpoint"]["checkpoint_id"]} - if result["parent_checkpoint"]: - result["parent_checkpoint"]["checkpoint_map"] = {"": result["parent_checkpoint"]["checkpoint_id"]} - return result - - -async def main(): - client = AuthoredClient() - options = {"fail_title": False, "mapped": False, "decisionFault": None, - "partialDashboard": False, "failKpis": False, "malformedTrend": False, - "prose_parent": False} - callbacks = {"titleMessageCallbacks": 0, "decisionMessageCallbacks": 0, "interpretationMessageCallbacks": 0} - agent_model, respond_model = LocalModel(), LocalModel() - layout = json.loads((PROJECT / "prompts/generative-ui.md").read_text().split('For "show me the dashboard":')[1].strip()) - def model_factory(**kwargs): - if kwargs.get("streaming"): - model = agent_model if kwargs["model"] == "gpt-5" else respond_model - model.tags = kwargs.get("tags", []) - return model - return LocalModel(responses=[AIMessage(id="title", content="Local dashboard title")], tags=kwargs.get("tags", []), fail=options["fail_title"]) - source = PROJECT / "src/graph.py" - with patch.object(socket.socket, "connect", side_effect=AssertionError("Remote socket forbidden")) as connect, patch( - "langchain_openai.ChatOpenAI", model_factory, - ), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("authored_generative_ui_wire", source) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - # Explicit test-only node intervention proves the production wrapper's - # nonempty-parent fallback. Normal decisions never supply parent prose. - def fixture_admit(state): - output = module.admit_operation(state) - if options["prose_parent"] and output.get("messages"): - output["messages"][0] = output["messages"][0].model_copy(update={"content": "Here is the layout."}) - return output - module.graph.builder.nodes["admit"].runnable = RunnableLambda(fixture_admit) - graph = module.graph.builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = json.loads(line) - try: - with redirect_stdout(sys.stderr): - config = {"configurable": {"thread_id": request.get("threadId", "")}} - if request.get("checkpoint"): - config["configurable"].update(request["checkpoint"]) - if request["op"] == "state": - result = {"state": checkpoint(graph.get_state(config), options["mapped"])} - elif request["op"] == "history": - result = {"history": [checkpoint(state, options["mapped"]) for state in graph.get_state_history(config)]} - elif request["op"] == "configure": - agent_model.fail = request.get("failAgent", agent_model.fail) - client.threads.fail_read = request.get("failMetadata", False) - client.threads.fail_write = request.get("failWrite", False) - options["fail_title"] = request.get("failTitle", False) - options["mapped"] = request.get("mapped", options["mapped"]) - for key in ("decisionFault", "partialDashboard", "failKpis", "malformedTrend"): - options[key] = request.get(key, options[key]) - respond_model.fail = request.get("failInterpretation", respond_model.fail) - result = {} - elif request["op"] == "submit": - identity = request["messages"][0]["id"] - text = request["messages"][0]["content"].lower() - prior = graph.get_state(config).values - accepted = prior.get("_owned_layout") - spec_layout = deepcopy(layout) - options["prose_parent"] = "prose render" in text - if options["prose_parent"] and not accepted: - for element in spec_layout["elements"].values(): - for key, value in element.get("props", {}).items(): - if isinstance(value, dict) and "$state" in value: - element["props"][key] = None - elif options["partialDashboard"] and not accepted: - for element in spec_layout["elements"].values(): - for key, value in element.get("props", {}).items(): - if isinstance(value, dict) and value.get("$state", "").split("/")[1] in ("on_time", "flights_today", "avg_delay", "load_factor", "on_time_trend"): - element["props"][key] = None - if "cancelled" in text: - kind, batch = "data_update", [call("query_recent_disruptions", {"type": "cancelled"})] - elif "remove" in text or "structure" in text: - spec_layout = deepcopy(accepted or spec_layout) - if "table_section" in spec_layout["elements"]: - spec_layout["elements"]["root"]["children"].remove("table_section") - del spec_layout["elements"]["table_section"] - kind, batch = "restructure" if accepted else "create", [call("render_spec", spec_layout)] - elif "why" in text or "interpret" in text: - kind, batch = "interpret", [] - else: - kind, batch = "restructure" if accepted else "create", [call("render_spec", spec_layout)] - if batch and batch[0]["name"] == "render_spec": - prior_data = prior.get("dashboard", {}) if accepted else {} - necessary = {SLOT_TO_TOOL[pointer.split("/")[1]] for pointer in bindings(spec_layout) if not resolve(pointer, prior_data)} - batch += [call(tool.name) for tool in module._DATA_TOOLS if tool.name in necessary] - selected = call("plan_dashboard_operation", {"kind": kind, "calls": [{"name": c["name"], "args": c["args"]} for c in batch], "interpretation": text if kind == "interpret" else ""}) - if options["decisionFault"] == "invalid": - selected["args"]["calls"] = [{"name": "query_recent_disruptions", "args": {"type": "invented"}}] - agent_model.responses = [AIMessage(id=identity + "-decision", content="", tool_calls=[] if options["decisionFault"] == "missing" else [selected])] - agent_model.cursor = 0 - respond_model.responses = [AIMessage(id=identity + "-interpretation", content="On-time performance needs operational context to explain.")] - respond_model.cursor = 0 - def failed_kpis(): - raise RuntimeError("Explicit local KPI tool failure") - def malformed_trend(months=12): - return [{"month": "Invalid", "on_time_pct": 101}] - tools = [module.render_spec, *module._DATA_TOOLS] - if options["failKpis"]: - tools = [StructuredTool.from_function(failed_kpis, name="query_airline_kpis", description="Test-only throwing KPI tool") if tool.name == "query_airline_kpis" else tool for tool in tools] - if options["malformedTrend"]: - tools = [StructuredTool.from_function(malformed_trend, name="query_on_time_trend", description="Test-only malformed trend tool") if tool.name == "query_on_time_trend" else tool for tool in tools] - module._tool_node = ToolNode(tools, handle_tool_errors="The dashboard tool failed.") - events = [] - terminal_config = config - failure = False - try: - async for kind, event in graph.astream({"messages": request["messages"]}, config, stream_mode=["messages", "values", "updates", "custom", "checkpoints"]): - if kind == "messages": - node, message = event[1].get("langgraph_node"), event[0] - key = ("titleMessageCallbacks" if node == "generate_title" else - "decisionMessageCallbacks" if node == "decide" else - "interpretationMessageCallbacks" if message.id == identity + "-interpretation" else None) - if key: - callbacks[key] += 1 - raise AssertionError("Private model callbacks leaked into answer stream") - if kind == "checkpoints": - if options["mapped"]: - position = event["config"]["configurable"] - position["checkpoint_map"] = {"": position["checkpoint_id"]} - terminal_config = event["config"] - events.append([kind, deepcopy(event)]) - except RuntimeError: - failure = True - events.append(["error", {"error": "GraphExecutionError", "message": "The conversation request failed."}]) - result = {"events": events, "state": checkpoint(graph.get_state(terminal_config), options["mapped"]), "graphFailure": failure} - else: - raise ValueError("Unsupported graph operation") - connect.assert_not_called() - result["proof"] = { - "actualCompiledGraph": True, - "op": request["op"], "threadId": request.get("threadId"), - "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), - "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), - "productionSourceSha256": {path: hashlib.sha256((PROJECT / path).read_bytes()).hexdigest() for path in PRODUCTION_SOURCES}, - "networkConnectAttempts": connect.call_count, **callbacks, - "fixtureControls": {key: options[key] for key in ("decisionFault", "partialDashboard", "failKpis", "malformedTrend", "prose_parent")}, - "httpCheckpointRepresentation": "root-map" if options["mapped"] else "native", - } - result["requestId"] = request["requestId"] - print(json.dumps(result, default=encode), flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request["requestId"], "error": "Graph operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 7ead93b3c..912ac33e9 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -104,6 +104,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { return Object.freeze({ topic: 'generative-ui', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/generative-ui/react', base: '/chat/generative-ui/react/', port: 4627, project: 'cockpit-chat-generative-ui-react', + backend: aimockLanggraph('chat/generative-ui', 4627), }); if (topic === 'chat-timeline') return Object.freeze({ diff --git a/scripts/react-cockpit/langgraph-proxy.mjs b/scripts/react-cockpit/langgraph-proxy.mjs index 2fd496d04..59da34651 100644 --- a/scripts/react-cockpit/langgraph-proxy.mjs +++ b/scripts/react-cockpit/langgraph-proxy.mjs @@ -34,7 +34,13 @@ * POST /__hold/history hold the next history read * POST /__state/ checkpoint state reads: safe | hold | failure | * changed | foreign | malformed | unsafe (a pending - * task on the source) + * task on the source) | task-error (a failed task) + * | missing-terminal (no completed_turn_id) | + * malformed-layout (the render owner's content) | + * stale-terminal | foreign-terminal | old-message | + * old-data | incomplete-tools | missing-dashboard | + * disagreeing-dashboard (chat/generative-ui's + * saved-turn markers and dashboard) * POST /__hold/state hold the next checkpoint state read * POST /__release-state finish a held checkpoint state read * POST /__mapped-checkpoints body { extra? }: until reset, checkpoints the @@ -146,7 +152,9 @@ const HISTORY_MODES = [ 'mismatch', 'unsupported', 'foreign', 'failure', 'paused', ]; // Rewrites of checkpoint state reads (POST /threads//state/checkpoint). -const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed', 'unsafe']; +const STATE_MODES = ['safe', 'hold', 'failure', 'changed', 'foreign', 'malformed', 'unsafe', 'missing-terminal', 'malformed-layout', 'task-error', + 'stale-terminal', 'foreign-terminal', 'old-message', 'old-data', 'incomplete-tools', 'missing-dashboard', + 'disagreeing-dashboard']; // Rewrites of the thread metadata read (GET /threads/) the client uses for titles. const TITLE_MODES = ['safe', 'hold', 'missing', 'wrong-thread', 'invalid', 'titlefailure', 'literal', 'long']; const HOP_BY_HOP = new Set(['connection', 'keep-alive', 'transfer-encoding', 'content-length', 'host']); @@ -530,6 +538,22 @@ export function createLanggraphProxy({ upstream, ready = async () => {} }) { if (mode === 'foreign' && state.checkpoint) state.checkpoint.thread_id = randomUUID(); if (mode === 'malformed') state.values = { ...state.values, messages: [{ type: 'human', content: { bad: true }, id: 'malformed' }] }; if (mode === 'unsafe') state.tasks = [{ id: 'pending-task', name: 'generate', interrupts: [] }]; + if (mode === 'task-error') state.tasks = [{ id: 'failed-task', name: 'respond', error: 'Local failure', interrupts: [] }]; + if (mode === 'missing-terminal' && state.values) delete state.values.completed_turn_id; + const values = state.values; + if (mode === 'stale-terminal' && values) + values.completed_turn_id = values.messages?.find((message) => message.type === 'human')?.id + '-stale'; + if (mode === 'foreign-terminal' && values) values.completed_answer_id = randomUUID(); + if (mode === 'old-message' && values?.messages?.length) values.messages[0].content = 'Changed prior content'; + if (mode === 'old-data' && values?.dashboard?.on_time) values.dashboard.on_time.value = '999%'; + if (mode === 'incomplete-tools' && values?.messages) + values.messages = values.messages.filter((message) => message.name !== 'render_spec'); + if (mode === 'missing-dashboard' && values) delete values.dashboard; + if (mode === 'disagreeing-dashboard' && values?.dashboard?.recent_disruptions) values.dashboard.recent_disruptions = []; + if (mode === 'malformed-layout') { + const owner = state.values?.messages?.find((message) => message.tool_calls?.some((call) => call.name === 'render_spec')); + if (owner) owner.content = '{"elements":{},"root":"missing"}'; + } for (const op of stateEdits?.ops ?? []) applyStateOp(op, state.values, stateEdits.humanId); json(response, state); }; diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index ac622dd63..6671e8d97 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -12,7 +12,6 @@ import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; -import { createChatGenerativeUiFixture } from './chat-generative-ui-fixture.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); @@ -43,7 +42,6 @@ const releaseA2uiStreams = (cancel = false) => { const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; -let chatGenerativeUiFixture = configuration.library === 'chat' && configuration.topic === 'generative-ui' ? createChatGenerativeUiFixture() : null; // aimock-backed examples: the real graph runs under `langgraph dev` or, for // AG-UI examples, the example's uvicorn server (started by the Playwright // global setup); this server proxies the API or agent endpoint to it. @@ -127,17 +125,6 @@ const server = createServer(async (request, response) => { if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; - if (chatGenerativeUiFixture && pathname === '/__reset') { - if (request.method !== 'POST') return json(response, {}, 405); - try { - await chatGenerativeUiFixture.close(); - chatGenerativeUiFixture = createChatGenerativeUiFixture(); - return json(response, {}); - } catch { - return json(response, { error: 'Fixture reset failed.' }, 500); - } - } - if (chatGenerativeUiFixture && (await chatGenerativeUiFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; if (agUiProxy && (await agUiProxy(request, response, pathname))) return; if (configuration.adapter === 'none') { @@ -205,7 +192,7 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture || chatGenerativeUiFixture) { +if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; @@ -218,7 +205,7 @@ if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } } - else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture ?? chatGenerativeUiFixture).close(); + else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); From 56bd06786523d7b0346e5e90151ad02613c0fc78 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:43:56 -0800 Subject: [PATCH 36/41] docs(cockpit): record batch 2b (chat/generative-ui) Co-Authored-By: Claude Opus 5.5 --- .../superpowers/specs/2026-11-18-react-cockpit-aimock-design.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index c2e3a18fb..b01a10179 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -175,7 +175,7 @@ example needs that the pilot proxy doesn't yet provide. | 0 (done) | chat/messages | none | pilot | | 1 (done) | chat/input, chat/theming | `/__captures`, `/__inject/reasoning` (theming) | | | 2 (done) | chat/tool-calls, chat/subagents, chat/interrupts | `/__hold/stream` `beforeToolResult` / `afterChildMessage`, `/__fail-stream`, `/__child/`, tool and approval `/__history` modes, `/__ready` | tool rounds key on aimock `toolCallId`; a mid-stream child model failure is aimock `truncateAfterChunks`; a title (metadata) failure is a per-prompt aimock error fixture (`… (title fails)`) | -| 2b | chat/generative-ui | `/__configure` faults that swap graph internals: a throwing KPI tool, malformed trend data, prose injected into the `admit` node's output, partial layouts | aimock can't produce tool-code failures or node-output rewrites, so this needs a decision: move the graph-level faults to Python tests, or keep a small authored layer | +| 2b (done) | chat/generative-ui | proxy state modes for the saved-turn faults on the confirmation read (`missing-terminal`, `stale-terminal`, `foreign-terminal`, `old-message`, `old-data`, `incomplete-tools`, `missing-dashboard`, `disagreeing-dashboard`, `malformed-layout`, `task-error`; injected data) | the old wire's decision, interpretation and title models become fixtures keyed on each node's system prompt, so admission, the real dashboard tools, assessment and publication run in the graph. Missing and invalid decisions and provider failures are real model output. A throwing tool, malformed tool data and prose on the action parent are unreachable from model output (admission validates every argument and binding before execution, and creates an empty parent): those cases are dropped from the browser tests and covered by `test_malformed_failed_missing_or_uncorrelated_results_roll_back_entire_batch`, `test_mixed_genuine_success_and_runtime_error_is_atomic` and `test_genuine_nonempty_parent_fallback_no_wrapper_or_data` in `cockpit/chat/generative-ui/python/tests/test_streaming.py` | | 3 (done) | chat/threads, chat/timeline | `/__title/`, `/__hold/title`, `/__state/`, `/__hold/state`, `/__hold/history`, `/__mapped-checkpoints`, `foreign`/`failure` history modes | a mid-stream model failure is aimock `truncateAfterChunks`; stream bodies come from `/__captures[].sse`, because DevTools can't return a streamed body the client closes | | 3b (done) | chat/debug | `/__captures` now also carries the saved `values` and `checkpoint`; `/__stream/edit` ops take `dataPattern` | summary and title calls share the answer's last user message, so their fixtures match on their own system prompts first. The packed-consumer native-session probe (`chat-debug-native-session-probe.mjs`, its own CI step) now runs the installed `@threadplane/langgraph` against `createAimockBackend` + an in-process proxy: two submits, two loads, exact checkpoints, a scripted astral answer (code-point analysis), and a transport omission of the `process` callback via `/__stream/edit { dataPattern }`. Its per-request worker acquisition journal and failure-lifetime mode described the deleted fixture's Python workers; it now proves the `langgraph dev` it started is gone after cleanup | | 4a (done) | langgraph/{streaming, interrupts, memory, persistence, subgraphs} | `/__hold/stream { immediate }`, `/__interrupt/`, `/__values`, `/__history-values`, `paused` history mode, `/__stream/edit` | These fixtures were pure authored JS with no graph, so their specs asserted text the real graph never writes. Assertions now follow the real graph (for example "Refund of $47.50 issued"). The real stream also surfaced a client bug, fixed here: subgraphs rejected the child's `__start__` checkpoint (`values: {}`) as malformed | From e113e8daa4d03ea115d18e86e61c9cd653be71dc Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 20:58:39 -0800 Subject: [PATCH 37/41] test(cockpit): run React ag-ui/a2ui e2e and website preview on aimock + the real AG-UI server The search, select and modify turns replay the react-parity A2UI model's own outputs for the flow (captured from its traces); the opening form is the recorded Angular fixture. The React fixture wrapper, its spec, the preparation script's --serve mode and its manager stage are removed. The installed-application proof and the parity server stay unchanged. The proxy gains /__hold/off for persistent holds. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 5 +- .../e2e/react-ag-ui-a2ui-preview.spec.ts | 8 +- apps/website/playwright.config.ts | 5 +- apps/website/src/playwright-config.spec.ts | 6 +- .../ag-ui/a2ui/angular/e2e/fixtures/a2ui.json | 46 ++ cockpit/ag-ui/a2ui/react/README.md | 4 +- cockpit/ag-ui/a2ui/react/e2e/a2ui.spec.ts | 179 ++--- .../ag-ui/a2ui/react/e2e/global-setup-impl.ts | 6 + .../ag-ui/a2ui/react/e2e/playwright.config.ts | 10 +- cockpit/ag-ui/a2ui/react/project.json | 72 +- scripts/ci-workflow.spec.mjs | 2 +- scripts/cockpit-matrix.spec.mjs | 2 +- .../ag-ui-a2ui-configuration.spec.mjs | 24 +- scripts/react-cockpit/ag-ui-a2ui-fixture.mjs | 637 ------------------ .../react-cockpit/ag-ui-a2ui-fixture.spec.mjs | 453 ------------- .../ag-ui-a2ui-preparation.spec.mjs | 98 +-- scripts/react-cockpit/ag-ui-proxy.mjs | 2 + scripts/react-cockpit/configuration.mjs | 1 + .../prepare-ag-ui-a2ui-fixture.mjs | 62 +- scripts/react-cockpit/serve.mjs | 15 +- 20 files changed, 222 insertions(+), 1415 deletions(-) create mode 100644 cockpit/ag-ui/a2ui/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/ag-ui-a2ui-fixture.mjs delete mode 100644 scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8da92d33c..c5eb9b38f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1131,7 +1131,7 @@ jobs: mkdirSync(retain, { recursive: true }); writeFileSync(join(retain, 'provenance.json'), JSON.stringify(provenance, null, 2) + '\n', { flag: 'wx' }); NODE - timeout 1200s node --test --test-concurrency=1 --test-timeout=120000 scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs scripts/react-cockpit/ag-ui-a2ui-native-probe.spec.mjs + timeout 1200s node --test --test-concurrency=1 --test-timeout=120000 scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs scripts/react-cockpit/ag-ui-a2ui-native-probe.spec.mjs - name: Verify native MAF fixture ownership if: matrix.cap.angular == 'cockpit-runtimes-microsoft-agent-framework-react' run: | @@ -1222,9 +1222,6 @@ jobs: uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 with: python-version: '3.12' - - name: Prepare frozen canonical A2UI browser producer - if: matrix.suite == 'ag-ui' - run: node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs - name: Configure bounded Ubuntu package downloads run: sudo "$(command -v node)" scripts/configure-ci-apt.mjs - name: Install Chromium diff --git a/apps/website/e2e/react-ag-ui-a2ui-preview.spec.ts b/apps/website/e2e/react-ag-ui-a2ui-preview.spec.ts index 8ecad4c63..758464d06 100644 --- a/apps/website/e2e/react-ag-ui-a2ui-preview.spec.ts +++ b/apps/website/e2e/react-ag-ui-a2ui-preview.spec.ts @@ -37,10 +37,10 @@ test.afterEach(async ({ page, request }, testInfo) => { }); expect(observed?.requests).toEqual([]); expect(observed?.errors).toEqual([]); - const lifetimeReceipt = await ( - await request.get('http://127.0.0.1:4647/__lifetime') - ).json(); - expect(lifetimeReceipt.current.requests).toEqual([]); + // The recording proxy saw no agent request (scripts/react-cockpit/ag-ui-proxy.mjs). + expect( + await (await request.get('http://127.0.0.1:4647/__requests')).json() + ).toEqual([]); expect((await request.post('http://127.0.0.1:4647/__reset')).ok()).toBe(true); }); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index ada8a04e6..35d3b84a1 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -176,9 +176,10 @@ export const createWebsitePlaywrightConfig = ( reuseExistingServer, }, ...(!productionMode && !bfcacheRuntimeTest ? [{ - command: 'node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs --serve', + // Render-only preview: its aimock + AG-UI server never boot. + command: 'node scripts/react-cockpit/serve.mjs ag-ui-a2ui --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4647', - reuseExistingServer: false, timeout: 330_000, + reuseExistingServer: false, timeout: 180_000, gracefulShutdown: { signal: 'SIGTERM' as const, timeout: 15000 }, }] : []), ...(!bfcacheRuntimeTest diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 2d906f743..0c96382a3 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -410,10 +410,10 @@ describe('Website Playwright configuration', () => { url: 'http://localhost:4300', }), expect.objectContaining({ - command: 'node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs --serve', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-a2ui --no-parent --backend', url: 'http://127.0.0.1:4647', reuseExistingServer: false, - timeout: 330_000, + timeout: 180_000, }), expect.objectContaining({ command: expect.stringContaining( @@ -605,7 +605,7 @@ it('scopes native AG-UI A2UI checks to Website and the prepared owned React serv const servers = Array.isArray(config.webServer) ? config.webServer : []; expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4647']); expect(servers.every(server => server.reuseExistingServer === false)).toBe(true); - expect(servers[1]?.command).toBe('node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs --serve'); + expect(servers[1]?.command).toBe('node scripts/react-cockpit/serve.mjs ag-ui-a2ui --no-parent --backend'); expect(config.testMatch).toBe('**/react-ag-ui-a2ui-preview.spec.ts'); expect(config.workers).toBe(1); expect(config.retries).toBe(0); diff --git a/cockpit/ag-ui/a2ui/angular/e2e/fixtures/a2ui.json b/cockpit/ag-ui/a2ui/angular/e2e/fixtures/a2ui.json index 84d191e97..e433bab9c 100644 --- a/cockpit/ag-ui/a2ui/angular/e2e/fixtures/a2ui.json +++ b/cockpit/ag-ui/a2ui/angular/e2e/fixtures/a2ui.json @@ -27,6 +27,52 @@ } ] } + }, + { + "match": { "systemMessage": "In 3-5 words, summarize" }, + "response": { "content": "Local booking title" } + }, + { + "match": { + "systemMessage": "find_routes() tool returned the following flights", + "userMessage": "SFO→SEA" + }, + "response": { + "toolCalls": [ + { + "name": "FlightResultsSpec", + "arguments": "{\"surface_id\": \"results\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card_0\"]}, {\"id\": \"card_0\", \"component\": \"Card\", \"child\": \"col_0\"}, {\"id\": \"col_0\", \"component\": \"Column\", \"children\": [\"title_0\", \"route_0\", \"time_0\", \"btn_0\"]}, {\"id\": \"title_0\", \"component\": \"Text\", \"text\": \"UA flight UA204\", \"variant\": \"h3\"}, {\"id\": \"route_0\", \"component\": \"Text\", \"text\": \"SFO → SEA • 120 min • Boeing 737\"}, {\"id\": \"time_0\", \"component\": \"Text\", \"text\": \"Depart 09:00 • Arrive 11:00 • Gate F11\"}, {\"id\": \"btn_0\", \"component\": \"Button\", \"child\": \"btn_label_0\", \"action\": {\"event\": {\"name\": \"flightSelect\", \"context\": {\"flightId\": \"UA204\"}}}}, {\"id\": \"btn_label_0\", \"component\": \"Text\", \"text\": \"Select\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "from the flight results surface", + "userMessage": "flight UA204" + }, + "response": { + "toolCalls": [ + { + "name": "ConfirmationSpec", + "arguments": "{\"surface_id\": \"confirmation\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"route_text\", \"time_text\", \"gate_text\", \"party_text\", \"modify\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"UA flight UA204\", \"variant\": \"h2\"}, {\"id\": \"route_text\", \"component\": \"Text\", \"text\": \"SFO → SEA • Boeing 737\"}, {\"id\": \"time_text\", \"component\": \"Text\", \"text\": \"09:00 → 11:00\"}, {\"id\": \"gate_text\", \"component\": \"Text\", \"text\": \"Gate F11\"}, {\"id\": \"party_text\", \"component\": \"Text\", \"text\": \"3 passengers • Business\"}, {\"id\": \"modify\", \"component\": \"Button\", \"child\": \"modify_label\", \"action\": {\"event\": {\"name\": \"modifySearch\", \"context\": {\"formId\": \"booking\"}}}}, {\"id\": \"modify_label\", \"component\": \"Text\", \"text\": \"Modify search\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "booking-form designer", + "userMessage": "\"modifySearch\"" + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"SFO\"], \"dest\": [\"SEA\"], \"date\": \"2027-02-17\", \"passengers\": 3, \"fare_class\": [\"Business\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}]}" + } + ] + } } ] } diff --git a/cockpit/ag-ui/a2ui/react/README.md b/cockpit/ag-ui/a2ui/react/README.md index 0082227a4..8e5fb5f79 100644 --- a/cockpit/ag-ui/a2ui/react/README.md +++ b/cockpit/ag-ui/a2ui/react/README.md @@ -4,6 +4,6 @@ This private experimental frontend shares the existing AG-UI A2UI Python graph a Run `npx nx e2e cockpit-ag-ui-a2ui-react` for uncached frozen preparation, one isolated installed-application proof, and the browser suite. Existing Python 3.12 and uv must be available; explicit absolute executable overrides are `THREADPLANE_AGUI_A2UI_PYTHON` and `THREADPLANE_AGUI_A2UI_UV`. `THREADPLANE_AGUI_A2UI_APPLICATION_ROOT` retains all attempted proof consumers, bytes and receipts. Python, caches and HOME are owned outside that uploaded subtree. -The private loopback server buffers actual canonical encoded AG-UI bytes before browser delivery. Each browser conversation owns a separate canonical worker and checkpointer, limited to four real POSTs; generations admit at most eight owners and 32 real POSTs. Reset revokes authority before bounded cleanup, archives receipts and opens fresh routing only after verified closure. Hold/release and explicitly labeled fake negative transport controls exist only in the local proof server. They are excluded from the application bundle and public Code assets. +The browser suite runs against the example's real AG-UI server (`src/server.py`), with model replies replayed by aimock from the fixtures shared with the Angular example (`cockpit/ag-ui/a2ui/angular/e2e/fixtures`). A local recording proxy (`scripts/react-cockpit/ag-ui-proxy.mjs`) records each agent POST with the real events and can hold, release or fail the next run; those controls exist only in local tests and are excluded from the application bundle and public Code assets. -The browser flow exercises Request, edited Search, Select and server-restored Modify. Native idle state and the original successful run plus matching root completion/transcript are necessary before presentation admission. Stop, error and New synchronously quarantine stale work. Historical form values remain read-only. Positive canonical wire evidence and fake negative transport requests are labeled separately. Local deterministic graph proof does not establish live-provider behavior or full A2UI catalog support. +The browser flow exercises Request, edited Search, Select and server-restored Modify. Native idle state and the original successful run plus matching root completion/transcript are necessary before presentation admission. Stop, error and New synchronously quarantine stale work. Historical form values remain read-only. Injected transport failures are labeled in the specs. Replayed model output does not establish live-provider behavior or full A2UI catalog support. diff --git a/cockpit/ag-ui/a2ui/react/e2e/a2ui.spec.ts b/cockpit/ag-ui/a2ui/react/e2e/a2ui.spec.ts index cecc325c9..3f589f92d 100644 --- a/cockpit/ag-ui/a2ui/react/e2e/a2ui.spec.ts +++ b/cockpit/ag-ui/a2ui/react/e2e/a2ui.spec.ts @@ -6,61 +6,27 @@ import { } from '@playwright/test'; import { writeFile } from 'node:fs/promises'; -interface CanonicalPost { - kind: 'actual-canonical-post'; - threadId: string; - runId: string; - body: { messages: { content: string }[] }; - rawBody: string; - response: { rawWire: string }; -} -interface OwnerReceipt { - stats: { - ready: { pid: number }; - closure: { - worker: boolean; - listener: boolean; - sockets: boolean; - responses: boolean; - }; - requests: { - body: { protocolVersion: string }; - rawBody: string; - sentWire: string[]; - worker: { - events: unknown[]; - observations: { - requestCount: number; - deniedNetwork: unknown[]; - state: { values: { messages: unknown[] } }; - }; - }; - }[]; - }; -} -interface GenerationReceipt { - holds: number; +// Backend: aimock replays cockpit/ag-ui/a2ui/angular/e2e/fixtures (the +// react-parity A2UI model's outputs for this flow) into the example's real +// AG-UI server, whose real tools search flights and look one up. The +// recording proxy (scripts/react-cockpit/ag-ui-proxy.mjs) records each agent +// POST with the real events, and holds or fails the next run. +interface AgentPost { + path: string; + body: { threadId: string; runId: string; protocolVersion: string; messages: { content: string }[] }; + events: { type: string; threadId?: string; runId?: string }[]; + held: boolean; closed: boolean; - proxyResponsesClosed: boolean; - owners: OwnerReceipt[]; - requests: (CanonicalPost | { kind: 'fake-negative-transport' })[]; } -interface ManagerReceipt { - current: GenerationReceipt; - archived: GenerationReceipt[]; -} - const base = '/ag-ui/a2ui/react/'; const current = (page: Page) => page.getByRole('region', { name: 'Current flight preview', exact: true }); const earlier = (page: Page) => page.getByRole('region', { name: 'Earlier flight preview', exact: true }); -const lifetime = async (request: APIRequestContext): Promise => +const lifetime = async (request: APIRequestContext): Promise<{ active: boolean; openResponses: number }> => (await request.get('/__lifetime')).json(); -const posts = (receipt: ManagerReceipt) => - receipt.current.requests.filter( - (record): record is CanonicalPost => record.kind === 'actual-canonical-post' - ); +const posts = async (request: APIRequestContext): Promise => + (await request.get('/__requests')).json(); async function form(page: Page) { await page.getByRole('button', { name: 'Try a flight request' }).click(); await page @@ -93,37 +59,22 @@ test.beforeEach(async ({ page, request }) => { ).toBeVisible(); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText('Ready.'); }); -test.afterEach(async ({ request }, info) => { - const receipt = await lifetime(request), - path = info.outputPath('canonical-manager-receipt.json'); - await writeFile(path, JSON.stringify(receipt, null, 2)); - await info.attach('canonical-manager-receipt', { - path, - contentType: 'application/json', - }); - // Archive and verify all canonical children before another browser case starts. - expect((await request.post('/__reset')).ok()).toBe(true); - const closed = await lifetime(request); - for (const generation of closed.archived) { - expect(generation.closed).toBe(true); - expect(generation.proxyResponsesClosed).toBe(true); - for (const owner of generation.owners) - expect(owner.stats.closure).toEqual({ - worker: true, - listener: true, - sockets: true, - responses: true, - }); - } +test.afterEach(async ({ page, request }, info) => { + const path = info.outputPath('agent-requests.json'); + await writeFile(path, JSON.stringify(await posts(request), null, 2)); + await info.attach('agent-requests', { path, contentType: 'application/json' }); + // No proxied agent stream outlives the case. + await page.goto('about:blank'); + await expect.poll(async () => (await lifetime(request)).openResponses).toBe(0); }); test('four actual native turns preserve edited Search, Select and server-restored Modify', async ({ page, request, }, info) => { - expect(posts(await lifetime(request))).toHaveLength(0); + expect(await posts(request)).toHaveLength(0); await page.getByRole('button', { name: 'Try a flight request' }).click(); - expect(posts(await lifetime(request))).toHaveLength(0); + expect(await posts(request)).toHaveLength(0); await page .getByRole('textbox', { name: 'Message', exact: true }) .press('Enter'); @@ -169,13 +120,10 @@ test('four actual native turns preserve edited Search, Select and server-restore value ); await expect(earlier(page)).toHaveCount(3); - const receipt = await lifetime(request), - requests = posts(receipt); + const requests = await posts(request); expect(requests).toHaveLength(4); - expect(receipt.current.owners).toHaveLength(1); - const owner = receipt.current.owners[0]; - expect(new Set(requests.map((record) => record.threadId)).size).toBe(1); - expect(new Set(requests.map((record) => record.runId)).size).toBe(4); + expect(new Set(requests.map((record) => record.body.threadId)).size).toBe(1); + expect(new Set(requests.map((record) => record.body.runId)).size).toBe(4); const action = (turn: number) => { const message = requests[turn].body.messages.at(-1); expect(message).toBeDefined(); @@ -199,19 +147,12 @@ test('four actual native turns preserve edited Search, Select and server-restore context: { flightId: 'UA204' }, }); expect(action(3).name).toBe('modifySearch'); - for (const [index, record] of owner.stats.requests.entries()) { - const observation = record.worker.observations; - expect(observation.requestCount).toBe(index + 1); - expect(observation.state.values.messages).toHaveLength((index + 1) * 2); - expect(observation.deniedNetwork).toEqual([]); + // Each turn is one real run that the server finished for that thread and run. + for (const record of requests) { expect(record.body.protocolVersion).toBe('1.0'); - expect(record.rawBody).toBe(requests[index].rawBody); - expect(record.sentWire.join('')).toBe(requests[index].response.rawWire); - expect(record.worker.events).toContainEqual({ - type: 'RUN_FINISHED', - threadId: requests[index].threadId, - runId: requests[index].runId, - }); + expect(record.events).toContainEqual( + expect.objectContaining({ type: 'RUN_FINISHED', threadId: record.body.threadId, runId: record.body.runId }) + ); } await page.setViewportSize({ width: 390, height: 844 }); expect( @@ -240,10 +181,11 @@ test('invalid drafts focus the field and repeated busy Search sends exactly one await expect(passengers).toHaveValue(value); await expect(passengers).toBeFocused(); await expect(passengers).toHaveAttribute('aria-invalid', 'true'); - expect(posts(await lifetime(request))).toHaveLength(1); + expect(await posts(request)).toHaveLength(1); } await edit(page); - await request.post('/__hold/stream'); + // A2UI runs stream structured tool calls, no text: hold right after the start. + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED' } }); await current(page) .getByRole('button', { name: 'Search flights', exact: true }) .evaluate((button: HTMLButtonElement) => { @@ -251,9 +193,9 @@ test('invalid drafts focus the field and repeated busy Search sends exactly one button.click(); }); await expect - .poll(async () => (await lifetime(request)).current.holds) - .toBe(1); - expect(posts(await lifetime(request))).toHaveLength(2); + .poll(async () => (await lifetime(request)).active) + .toBe(true); + expect(await posts(request)).toHaveLength(2); await expect( page.getByRole('button', { name: 'Send', exact: true }) ).toBeDisabled(); @@ -264,26 +206,31 @@ test('invalid drafts focus the field and repeated busy Search sends exactly one await expect( current(page).getByRole('button', { name: 'Select', exact: true }) ).toBeVisible(); - expect(posts(await lifetime(request))).toHaveLength(2); + expect(await posts(request)).toHaveLength(2); }); -test('Stop, fake transport error, New and held old completion cannot regain authority', async ({ +test('Stop, failed transport, New and a held old run cannot regain authority', async ({ page, request, }) => { await form(page); await edit(page); - await request.post('/__hold/stream'); + // Hold every run right after it starts until /__hold/off. + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED', persist: true } }); await current(page) .getByRole('button', { name: 'Search flights', exact: true }) .click(); await expect - .poll(async () => (await lifetime(request)).current.holds) - .toBe(1); + .poll(async () => (await lifetime(request)).active) + .toBe(true); await page.getByRole('button', { name: 'Stop', exact: true }).click(); await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText( 'Start a new conversation to continue.' ); + // Stop closes the held stream itself: nothing late can reach the page. + await expect + .poll(async () => (await lifetime(request)).active) + .toBe(false); await expect( earlier(page).getByLabel('Passengers', { exact: true }) ).toHaveValue('003'); @@ -295,21 +242,16 @@ test('Stop, fake transport error, New and held old completion cannot regain auth .click(); await request.post('/__hold/off'); await form(page); - // The new owner completes while the old encoded response is still held. - await expect - .poll(async () => (await lifetime(request)).current.holds) - .toBe(1); await request.post('/__release'); - const first = await lifetime(request); - expect(first.current.owners).toHaveLength(2); - expect( - new Set(first.current.owners.map((owner) => owner.stats.ready.pid)).size - ).toBe(2); await expect(earlier(page)).toHaveCount(0); + const threads = new Set((await posts(request)).map((record) => record.body.threadId)); + expect(threads.size).toBe(2); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); - await request.post('/__fail-transport'); + // The next agent POST answers 500 without reaching the server. + await request.post('/__fail-stream'); + const beforeFailure = (await posts(request)).length; await page.getByRole('button', { name: 'Try a flight request' }).click(); await page .getByRole('textbox', { name: 'Message', exact: true }) @@ -317,34 +259,33 @@ test('Stop, fake transport error, New and held old completion cannot regain auth await expect(page.getByRole('status', { name: 'Conversation status' })).toHaveText( 'Start a new conversation to continue.' ); - expect( - (await lifetime(request)).current.requests.filter( - (record) => record.kind === 'fake-negative-transport' - ) - ).toHaveLength(1); + expect(await posts(request)).toHaveLength(beforeFailure + 1); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); await form(page); - await request.post('/__hold/stream'); + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED', persist: true } }); await edit(page); await current(page) .getByRole('button', { name: 'Search flights', exact: true }) .click(); await expect - .poll(async () => (await lifetime(request)).current.holds) - .toBe(1); + .poll(async () => (await lifetime(request)).active) + .toBe(true); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); + await expect + .poll(async () => (await lifetime(request)).active) + .toBe(false); await request.post('/__hold/off'); await form(page); await request.post('/__release'); - const before = posts(await lifetime(request)).length; + const before = (await posts(request)).length; await page.evaluate(() => window.dispatchEvent(new PageTransitionEvent('pagehide')) ); await request.post('/__release'); - expect(posts(await lifetime(request))).toHaveLength(before); + expect(await posts(request)).toHaveLength(before); await expect(page.locator('main')).toHaveCount(0); }); diff --git a/cockpit/ag-ui/a2ui/react/e2e/global-setup-impl.ts b/cockpit/ag-ui/a2ui/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..f988ecd6d --- /dev/null +++ b/cockpit/ag-ui/a2ui/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'ag-ui/a2ui', port: 4647 }); diff --git a/cockpit/ag-ui/a2ui/react/e2e/playwright.config.ts b/cockpit/ag-ui/a2ui/react/e2e/playwright.config.ts index 7a11c474b..1e3faa5f2 100644 --- a/cockpit/ag-ui/a2ui/react/e2e/playwright.config.ts +++ b/cockpit/ag-ui/a2ui/react/e2e/playwright.config.ts @@ -8,14 +8,14 @@ export default defineConfig({ outputDir: '../../../../../output/playwright/ag-ui-a2ui-react', use: { baseURL: 'http://127.0.0.1:4647', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/ag-ui/a2ui/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { - command: - 'node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs --serve', + command: 'node scripts/react-cockpit/serve.mjs ag-ui-a2ui --no-parent', cwd: '../../../../..', url: 'http://127.0.0.1:4647/ag-ui/a2ui/react/', reuseExistingServer: false, - stdout: 'pipe', - timeout: 330000, - gracefulShutdown: { signal: 'SIGTERM', timeout: 15000 }, + timeout: 30000, }, }); diff --git a/cockpit/ag-ui/a2ui/react/project.json b/cockpit/ag-ui/a2ui/react/project.json index 4c30861ba..46c046740 100644 --- a/cockpit/ag-ui/a2ui/react/project.json +++ b/cockpit/ag-ui/a2ui/react/project.json @@ -3,13 +3,17 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/ag-ui/a2ui/react/src", "projectType": "application", - "implicitDependencies": ["cockpit-a2ui-react"], - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-a2ui-react" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "namedInputs": { "nativeA2uiFixture": [ "{workspaceRoot}/fixtures/react-parity/native-ag-ui-a2ui/**/*", - "{workspaceRoot}/scripts/react-cockpit/ag-ui-a2ui-fixture.mjs", - "{workspaceRoot}/scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs", "{workspaceRoot}/scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs", "{workspaceRoot}/scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs", "{workspaceRoot}/scripts/react-cockpit/ag-ui-a2ui-native-probe.mjs", @@ -27,7 +31,10 @@ "prepare-fixture": { "executor": "nx:run-commands", "cache": false, - "inputs": ["default", "nativeA2uiFixture"], + "inputs": [ + "default", + "nativeA2uiFixture" + ], "options": { "command": "node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs" } @@ -35,9 +42,20 @@ "fixture-test": { "executor": "nx:run-commands", "cache": false, - "inputs": ["default", "nativeA2uiFixture"], + "inputs": [ + "default", + "nativeA2uiFixture" + ], "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react", "a2ui"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react", + "a2ui" + ] + } ], "options": { "command": "node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs --verify-application" @@ -46,8 +64,15 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "inputs": ["default", "nativeA2uiFixture"], - "dependsOn": ["prepare-fixture", "build", "fixture-test"], + "inputs": [ + "default", + "reactAimockBackend", + "nativeA2uiFixture" + ], + "dependsOn": [ + "build", + "fixture-test" + ], "options": { "config": "cockpit/ag-ui/a2ui/react/e2e/playwright.config.ts", "skipInstall": true @@ -57,21 +82,40 @@ "executor": "@nx/vitest:test", "options": { "configFile": "cockpit/ag-ui/a2ui/react/vite.config.mts", - "reporters": ["default"] + "reporters": [ + "default" + ] } }, "build": { "executor": "nx:run-commands", "cache": false, "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react", "a2ui"] }, - { "target": "react-native-a2ui-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react", + "a2ui" + ] + }, + { + "target": "react-native-a2ui-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/ag-ui/a2ui/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/ag-ui/a2ui/react"], "options": { "command": "node scripts/react-cockpit/build.mjs ag-ui-a2ui" } }, - "lint": { "executor": "@nx/eslint:lint" } + "lint": { + "executor": "@nx/eslint:lint" + } } } diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index cab503a96..e97c7a735 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - '2937b601bc61b77ad17af8e943750ffaee0efbf11c9859ea0576b50d86d79481', + 'b755ced4e3ab6a10144425498ab416fb7b23a09421a8154f9dc42930611a4cc7', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); diff --git a/scripts/cockpit-matrix.spec.mjs b/scripts/cockpit-matrix.spec.mjs index d9790d049..3d64fc87b 100644 --- a/scripts/cockpit-matrix.spec.mjs +++ b/scripts/cockpit-matrix.spec.mjs @@ -184,7 +184,7 @@ describe('native AG-UI A2UI actual Nx proof ownership', () => { for (const file of [ 'fixtures/react-parity/native-ag-ui-a2ui/server.mjs', 'fixtures/react-parity/native-ag-ui-a2ui/compiled_worker.py', - 'scripts/react-cockpit/ag-ui-a2ui-fixture.mjs', + 'scripts/react-cockpit/ag-ui-proxy.mjs', 'scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs', 'scripts/react-cockpit/ag-ui-a2ui-native-probe.mjs', 'libs/cockpit-a2ui-react/src/surface.ts', diff --git a/scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs b/scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs index c25f92618..83f0e1e1f 100644 --- a/scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs +++ b/scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs @@ -12,6 +12,14 @@ test('native AG-UI A2UI has one closed authored frontend allocation', () => { base: '/ag-ui/a2ui/react/', port: 4647, project: 'cockpit-ag-ui-a2ui-react', + backend: { + kind: 'aimock-ag-ui', + python: 'cockpit/ag-ui/a2ui/python', + fixtures: 'cockpit/ag-ui/a2ui/angular/e2e/fixtures', + backendPort: 5647, + aimockPort: 6647, + agentPath: '/agent', + }, }); assert.ok(Object.isFrozen(reactCockpitConfiguration('ag-ui-a2ui'))); for (const key of [ @@ -43,11 +51,9 @@ test('native A2UI owns uncached preparation and exactly one installed proof depe assert.deepEqual(project.targets['fixture-test'].dependsOn, [ { target: 'build', projects: ['core', 'content', 'react', 'a2ui'] }, ]); - assert.deepEqual(project.targets.e2e.dependsOn, [ - 'prepare-fixture', - 'build', - 'fixture-test', - ]); + // The browser e2e runs on aimock + the real AG-UI server; the installed + // application proof (fixture-test) still runs first. + assert.deepEqual(project.targets.e2e.dependsOn, ['build', 'fixture-test']); assert.ok( project.namedInputs?.nativeA2uiFixture?.includes( '{workspaceRoot}/fixtures/react-parity/native-ag-ui-a2ui/**/*' @@ -99,12 +105,10 @@ test('native A2UI capability CI owns one installed proof with retained partial a assert.equal(artifact.with['include-hidden-files'], true); assert.equal(artifact.with['retention-days'], 7); const website = workflow.jobs['website-e2e'].steps; + // The Website preview runs on aimock + the real AG-UI server (`uv run` + // syncs its environment), so the Website job no longer prepares A2UI. assert.ok( - website.some( - (step) => - step.run === 'node scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs' && - step.if === "matrix.suite == 'ag-ui'" - ) + !website.some((step) => step.run?.includes('prepare-ag-ui-a2ui-fixture.mjs')) ); assert.ok( !website.some((step) => diff --git a/scripts/react-cockpit/ag-ui-a2ui-fixture.mjs b/scripts/react-cockpit/ag-ui-a2ui-fixture.mjs deleted file mode 100644 index 3d6c9ab6b..000000000 --- a/scripts/react-cockpit/ag-ui-a2ui-fixture.mjs +++ /dev/null @@ -1,637 +0,0 @@ -/** Private browser proof: positive bytes come only from owned canonical children. */ -import assert from 'node:assert/strict'; -import { createHash } from 'node:crypto'; -import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'; -import { tmpdir as systemTmpdir } from 'node:os'; -import { join } from 'node:path'; -import { createA2uiServer } from '../../fixtures/react-parity/native-ag-ui-a2ui/server.mjs'; - -const MAX_BODY = 2_000_000, - MAX_WIRE = 16 * 1024 * 1024; -const fields = new Set([ - 'threadId', - 'runId', - 'protocolVersion', - 'messages', - 'tools', - 'context', - 'state', - 'forwardedProps', -]); -const immutable = (value) => structuredClone(value); -const closedResource = (receipt) => - ['worker', 'listener', 'sockets', 'responses'].every( - (key) => receipt?.closure?.[key] === true - ); -function deadline(operation, timeoutMs, label) { - let timer; - return Promise.race([ - operation, - new Promise((_, reject) => { - timer = setTimeout(() => reject(Error(label)), timeoutMs); - }), - ]).finally(() => clearTimeout(timer)); -} -function inputBody(raw) { - assert.ok( - Buffer.isBuffer(raw) && raw.length <= MAX_BODY, - 'Request body bound' - ); - const body = JSON.parse(raw.toString('utf8')); - assert.ok( - body && typeof body === 'object' && !Array.isArray(body), - 'Request object required' - ); - assert.ok( - Object.keys(body).every((key) => fields.has(key)), - 'Unreviewed request field' - ); - assert.ok( - body.protocolVersion === undefined || body.protocolVersion === '1.0', - 'Unsupported native protocolVersion' - ); - assert.ok( - typeof body.threadId === 'string' && - body.threadId.length > 0 && - body.threadId.length <= 256 && - !body.threadId.includes('\0'), - 'Exact nonempty threadId required' - ); - assert.ok( - typeof body.runId === 'string' && - body.runId.length > 0 && - body.runId.length <= 256, - 'Exact runId required' - ); - assert.ok(Array.isArray(body.messages), 'Messages required'); - for (const key of ['tools', 'context']) - assert.ok( - body[key] === undefined || Array.isArray(body[key]), - 'Invalid ' + key - ); - return body; -} -async function encodedBody(response) { - if (!response.body) return Buffer.alloc(0); - const reader = response.body.getReader(), - parts = []; - let bytes = 0; - try { - for (;;) { - const next = await reader.read(); - if (next.done) break; - bytes += next.value.byteLength; - assert.ok(bytes <= MAX_WIRE, 'Canonical wire bound'); - parts.push(Buffer.from(next.value)); - } - return Buffer.concat(parts); - } catch (error) { - await reader.cancel().catch(() => undefined); - throw error; - } finally { - reader.releaseLock(); - } -} - -export function createAgUiA2uiFixture({ - retain = mkdtempSync( - join(systemTmpdir(), 'threadplane-native-a2ui-browser-') - ), - acquire = createA2uiServer, - python, - environment, - home, - tmpdir, - timeoutMs = 20000, - cleanupTimeoutMs = 3000, -} = {}) { - mkdirSync(retain, { recursive: true }); - const generations = [], - archived = [], - usedThreads = new Set(); - let current, - serial = Promise.resolve(), - closed = false, - unavailable = true, - closing, - resetting; - function ownerReceipt(owner) { - return { - threadId: owner.threadId, - retain: owner.retain, - active: owner.active, - posts: owner.posts, - acquisitionFailure: owner.acquisitionFailure, - stats: owner.resource?.stats(), - errors: [...owner.errors], - }; - } - function generationReceipt(generation) { - return { - generation: generation.id, - revoked: generation.revoked, - posts: generation.posts, - holds: generation.holds.size, - proxyResponsesClosed: generation.responses.size === 0, - owners: [...generation.owners.values()].map(ownerReceipt), - requests: immutable(generation.requests), - controls: immutable(generation.controls), - closed: generation.closed, - }; - } - const receipt = () => - immutable({ - kind: 'private canonical per-thread A2UI browser fixture', - unavailable, - closed, - current: current && generationReceipt(current), - archived, - }); - function save(generation) { - writeFileSync( - join(generation.retain, 'receipt.json'), - JSON.stringify(generationReceipt(generation), null, 2) + '\n' - ); - } - function revoke(generation) { - if (!generation || generation.revoked) return; - generation.revoked = true; - generation.hold = false; - for (const hold of generation.holds) { - hold.cleanup(); - hold.reject(Error('Generation revoked')); - } - generation.holds.clear(); - for (const controller of generation.controllers) - controller.abort(Error('Generation revoked')); - for (const [response, resource] of generation.responses) { - resource.request.destroy(); - response.destroy(); - } - } - async function closeGeneration(generation) { - if (!generation) return; - return (generation.closing ??= (async () => { - const failures = []; - for (const owner of generation.owners.values()) { - try { - await deadline( - owner.acquiring, - timeoutMs + cleanupTimeoutMs, - 'Acquisition closure deadline' - ); - } catch (error) { - owner.errors.push(String(error).slice(0, 2048)); - failures.push(error); - } - try { - if (owner.resource) { - await deadline( - owner.resource.close(), - timeoutMs + cleanupTimeoutMs, - 'Owner closure deadline' - ); - assert.ok( - closedResource(owner.resource.stats()), - 'Owner closure unknown' - ); - } else - assert.ok( - closedResource(owner.acquisitionFailure), - 'Partial acquisition closure unknown' - ); - } catch (error) { - owner.errors.push(String(error).slice(0, 2048)); - failures.push(error); - } - } - await deadline( - Promise.allSettled([ - ...generation.operations, - ...[...generation.responses.values()].map( - (resource) => resource.closed - ), - ]), - timeoutMs + cleanupTimeoutMs, - 'Proxy closure deadline' - ).catch((error) => failures.push(error)); - if (generation.responses.size) - failures.push(Error('Browser response closure unknown')); - generation.closed = failures.length === 0; - const snapshot = generationReceipt(generation); - archived.push(immutable(snapshot)); - writeFileSync( - join(generation.retain, 'archived.json'), - JSON.stringify(snapshot, null, 2) + '\n', - { flag: 'wx' } - ); - save(generation); - if (failures.length) - throw new AggregateError( - failures, - 'Canonical generation closure failed' - ); - })()); - } - function reset() { - assert.ok(!closed, 'Fixture closed'); - if (resetting) return resetting; - unavailable = true; - const previous = current; - revoke(previous); // Synchronous authority revocation precedes all cleanup. - const operation = serial.then(async () => { - await closeGeneration(previous); - assert.ok(!closed, 'Fixture closed'); - const id = generations.length + 1, - directory = join(retain, 'generation-' + id); - mkdirSync(directory); - current = { - id, - retain: directory, - revoked: false, - closed: false, - posts: 0, - hold: false, - fail: false, - owners: new Map(), - requests: [], - controls: [{ kind: 'private-reset' }], - holds: new Set(), - controllers: new Set(), - operations: new Set(), - responses: new Map(), - }; - generations.push(current); - unavailable = false; - save(current); - return receipt(); - }); - serial = operation.catch(() => undefined); - resetting = operation; - void operation - .finally(() => { - resetting = undefined; - }) - .catch(() => undefined); - return resetting; - } - async function perform(generation, raw, body, signal) { - let owner = generation.owners.get(body.threadId); - if (!owner) { - assert.ok( - !usedThreads.has(body.threadId), - 'Thread identity reused across generations' - ); - assert.ok(generation.owners.size < 8, 'Canonical owner bound'); - usedThreads.add(body.threadId); - const directory = join( - generation.retain, - 'owner-' + createHash('sha256').update(body.threadId).digest('hex') - ); - mkdirSync(directory); - owner = { - threadId: body.threadId, - retain: directory, - active: false, - posts: 0, - runs: new Set(), - errors: [], - }; - generation.owners.set(body.threadId, owner); - owner.acquiring = (async () => { - try { - owner.resource = await acquire({ - python, - environment, - home, - tmpdir, - threadId: body.threadId, - retain: directory, - timeoutMs, - }); - assert.match( - owner.resource.url, - /^http:\/\/127\.0\.0\.1:\d+\/agent$/, - 'Exact loopback agent required' - ); - writeFileSync( - join(directory, 'start.json'), - JSON.stringify(ownerReceipt(owner), null, 2) + '\n' - ); - if (generation.revoked || closed) { - await deadline( - owner.resource.close(), - timeoutMs + cleanupTimeoutMs, - 'Late acquired owner closure deadline' - ); - const stats = owner.resource.stats(); - writeFileSync( - join(directory, 'late-acquisition-closure.json'), - JSON.stringify(stats, null, 2) + '\n' - ); - assert.ok( - closedResource(stats), - 'Late acquired owner closure unknown' - ); - } - } catch (error) { - owner.acquisitionFailure = immutable( - error.fixtureClosure ?? { closure: {} } - ); - owner.errors.push(String(error).slice(0, 2048)); - save(generation); - throw error; - } - })(); - } - assert.ok(!owner.active, 'Canonical owner request active'); - assert.ok(owner.posts < 4 && generation.posts < 32, 'Canonical POST bound'); - assert.ok(!owner.runs.has(body.runId), 'Duplicate run identity'); - owner.active = true; - owner.posts++; - generation.posts++; - owner.runs.add(body.runId); - const record = { - kind: 'actual-canonical-post', - generation: generation.id, - threadId: body.threadId, - runId: body.runId, - body, - rawBody: raw.toString('utf8'), - rawBase64: raw.toString('base64'), - completed: false, - }; - generation.requests.push(record); - const controller = new AbortController(); - generation.controllers.add(controller); - const abort = () => controller.abort(signal.reason); - signal?.addEventListener('abort', abort, { once: true }); - if (signal?.aborted) abort(); - const timer = setTimeout( - () => controller.abort(Error('Proxy operation deadline')), - timeoutMs - ); - try { - await deadline( - owner.acquiring, - timeoutMs, - 'Canonical acquisition deadline' - ); - assert.ok(!generation.revoked && !closed, 'Generation revoked'); - const resource = owner.resource, - url = resource.url; - const upstream = await deadline( - resource.fetch(url, { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: raw, - redirect: 'error', - signal: controller.signal, - }), - timeoutMs, - 'Canonical request deadline' - ); - const bytes = await deadline( - encodedBody(upstream), - timeoutMs, - 'Canonical wire deadline' - ); - assert.ok(!generation.revoked && !closed, 'Generation revoked'); - const result = { - status: upstream.status, - headers: Object.fromEntries(upstream.headers.entries()), - bytes, - }; - record.response = { - status: result.status, - headers: result.headers, - rawBase64: bytes.toString('base64'), - rawWire: bytes.toString('utf8'), - }; - save(generation); - clearTimeout(timer); // Canonical operation settled. A held browser delivery has separate ownership. - if (generation.hold) { - await new Promise((resolveHold, rejectHold) => { - const hold = { record, resolve: resolveHold, reject: rejectHold }; - generation.holds.add(hold); - record.held = true; - const abortHold = () => { - record.clientClosed = true; /* Preserve captured bytes until explicit release/reset. */ - }; - signal?.addEventListener('abort', abortHold, { once: true }); - if (signal?.aborted) abortHold(); - hold.cleanup = () => signal?.removeEventListener('abort', abortHold); - const heldDeadline = setTimeout(() => { - generation.holds.delete(hold); - hold.cleanup(); - rejectHold(Error('Held delivery deadline')); - }, timeoutMs); - const cleanup = hold.cleanup; - hold.cleanup = () => { - clearTimeout(heldDeadline); - cleanup(); - }; - save(generation); - }); - } - assert.ok(!generation.revoked && !closed, 'Generation revoked'); - record.completed = true; - return result; - } catch (error) { - record.error = String(error).slice(0, 2048); - if (!owner.resource) { - unavailable = true; - revoke(generation); - } - throw error; - } finally { - clearTimeout(timer); - owner.active = false; - generation.controllers.delete(controller); - signal?.removeEventListener('abort', abort); - save(generation); - } - } - function forward(raw, { signal } = {}) { - let body; - try { - assert.ok( - !closed && !unavailable && current && !current.revoked, - 'Fixture unavailable' - ); - body = inputBody(raw); - if (current.fail) { - current.fail = false; - current.requests.push({ - kind: 'fake-negative-transport', - threadId: body.threadId, - body, - status: 503, - }); - save(current); - return Promise.resolve({ - status: 503, - headers: { 'content-type': 'application/json' }, - bytes: Buffer.from( - '{"error":"Deliberate private negative transport failure"}' - ), - }); - } - } catch (error) { - return Promise.reject(error); - } - const generation = current, - operation = perform(generation, raw, body, signal); - generation.operations.add(operation); - void operation - .finally(() => generation.operations.delete(operation)) - .catch(() => undefined); - return operation; - } - function hold(enabled = true) { - assert.ok(current && !unavailable, 'Fixture unavailable'); - current.hold = enabled; - current.controls.push({ kind: 'private-hold', enabled }); - save(current); - } - function release() { - assert.ok(current && !unavailable, 'Fixture unavailable'); - current.hold = false; - for (const hold of current.holds) { - hold.cleanup(); - hold.record.delivery = hold.record.clientClosed - ? 'clientclosed-drop' - : 'captured-old-response'; - hold.resolve(); - } - current.holds.clear(); - current.controls.push({ kind: 'private-release' }); - save(current); - } - function failTransport() { - assert.ok(current && !unavailable, 'Fixture unavailable'); - current.fail = true; - current.controls.push({ kind: 'fake-negative-transport-next' }); - save(current); - } - function close() { - closed = true; - unavailable = true; - for (const generation of generations) revoke(generation); - return (closing ??= serial.then(async () => { - const results = await Promise.allSettled( - generations.map(closeGeneration) - ); - const failures = results.filter((result) => result.status === 'rejected'); - if (failures.length) - throw new AggregateError( - failures.map((result) => result.reason), - 'Fixture closure failed' - ); - return receipt(); - })); - } - async function handle(request, response, pathname) { - const controls = { - '/__reset': ['POST', reset], - '/__lifetime': ['GET', receipt], - '/__requests': ['GET', () => receipt().current?.requests ?? []], - '/__hold/stream': ['POST', () => hold()], - '/__hold/off': ['POST', () => hold(false)], - '/__release': ['POST', release], - '/__fail-transport': ['POST', failTransport], - }; - const json = (value, status = 200) => { - response.writeHead(status, { 'content-type': 'application/json' }); - response.end(JSON.stringify(value ?? {})); - }; - if (pathname.startsWith('/__')) { - const control = Object.hasOwn(controls, pathname) - ? controls[pathname] - : undefined; - if (!control) { - json({}, 404); - return true; - } - if (request.method !== control[0]) { - json({}, 405); - return true; - } - try { - json(await control[1]()); - } catch { - json({ error: 'Private fixture control failed' }, 503); - } - return true; - } - if (pathname !== '/ag-ui/a2ui/agent') return false; - if (request.method !== 'POST') { - json({}, 405); - return true; - } - if (!current || unavailable || current.revoked) { - json({}, 503); - return true; - } - const generation = current; - let settle; - const resource = { - request, - closed: new Promise((resolveClosed) => { - settle = resolveClosed; - }), - }; - generation.responses.set(response, resource); - const settled = () => { - generation.responses.delete(response); - response.removeListener('finish', settled); - response.removeListener('close', settled); - settle(); - }; - response.once('finish', settled); - response.once('close', settled); - const controller = new AbortController(); - const cancel = () => { - if (!response.writableEnded) - controller.abort(Error('Browser reader closed')); - }; - response.once('close', cancel); - const timer = setTimeout(() => { - request.destroy(); - controller.abort(Error('Browser body deadline')); - }, timeoutMs); - try { - let count = 0; - const parts = []; - for await (const chunk of request) { - count += chunk.length; - assert.ok(count <= MAX_BODY, 'Request body bound'); - parts.push(chunk); - } - clearTimeout(timer); - const result = await forward(Buffer.concat(parts), { - signal: controller.signal, - }); - if (response.destroyed) return true; - response.writeHead(result.status, result.headers); - response.end(result.bytes); - } catch { - if (!response.destroyed && !response.headersSent) - json({ error: 'Private canonical fixture request rejected' }, 422); - else response.destroy(); - } finally { - clearTimeout(timer); - response.removeListener('close', cancel); - } - return true; - } - return Object.assign(handle, { - forward, - reset, - receipt, - hold, - release, - failTransport, - close, - }); -} diff --git a/scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs b/scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs deleted file mode 100644 index 847d39ea0..000000000 --- a/scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs +++ /dev/null @@ -1,453 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { mkdtemp } from 'node:fs/promises'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; -import { EventEmitter } from 'node:events'; -import { Readable } from 'node:stream'; -import { randomUUID } from 'node:crypto'; -import { pidAbsent } from '../../fixtures/react-parity/native-ag-ui-a2ui/server.mjs'; - -const module = await import('./ag-ui-a2ui-fixture.mjs').catch(() => ({})); -test('native A2UI exports an import-inert owned fixture manager', () => { - assert.equal(typeof module.createAgUiA2uiFixture, 'function'); -}); - -const body = (threadId, turn = 0) => - Buffer.from( - JSON.stringify({ - threadId, - runId: `${threadId}-${turn}`, - messages: [], - tools: [], - context: [], - state: {}, - forwardedProps: {}, - }) - ); -async function setup(options = {}) { - const acquired = []; - const fixture = module.createAgUiA2uiFixture({ - retain: await mkdtemp(join(tmpdir(), 'native-a2ui-manager-test-')), - acquire: async ({ threadId, retain }) => { - const calls = []; - let closed = false; - const resource = { - threadId, - retain, - calls, - closed: () => closed, - url: 'http://127.0.0.1:12345/agent', - fetch: async (_url, init) => { - calls.push(Buffer.from(init.body)); - return new Response('canonical encoded bytes\r\n', { - status: 200, - headers: { 'content-type': 'text/event-stream' }, - }); - }, - stats: () => ({ - ready: { pid: 1000 + acquired.indexOf(resource) }, - requests: calls.map((raw) => ({ rawBody: raw.toString() })), - closure: { - worker: closed, - listener: closed, - sockets: closed, - responses: closed, - }, - }), - close: async () => { - closed = true; - }, - }; - acquired.push(resource); - return resource; - }, - ...options, - }); - await fixture.reset(); - return { fixture, acquired }; -} -const lifecycle = - typeof module.createAgUiA2uiFixture === 'function' ? test : test.skip; -lifecycle( - 'sequential conversations each acquire one canonical child and preserve all four raw requests', - async () => { - const { fixture, acquired } = await setup(); - try { - for (const thread of ['first', 'new']) - for (let turn = 0; turn < 4; turn++) { - const raw = body(thread, turn); - const result = await fixture.forward(raw); - assert.equal(result.status, 200); - assert.deepEqual( - result.bytes, - Buffer.from('canonical encoded bytes\r\n') - ); - assert.deepEqual(acquired.at(-1).calls.at(-1), raw); - } - assert.equal(acquired.length, 2); - assert.notEqual(acquired[0].retain, acquired[1].retain); - await assert.rejects(fixture.forward(body('first', 5)), /bound/); - } finally { - await fixture.close(); - } - assert.ok(acquired.every((owner) => owner.closed())); - } -); -lifecycle( - 'reset closes all old owners before fresh generation and archives immutable receipts', - async () => { - const { fixture, acquired } = await setup(); - await fixture.forward(body('first')); - await fixture.forward(body('second')); - await fixture.reset(); - assert.ok(acquired.every((owner) => owner.closed())); - const before = fixture.receipt(); - assert.equal(before.archived.length, 1); - assert.equal(before.archived[0].owners.length, 2); - await assert.rejects(fixture.forward(body('first')), /reused/); - await fixture.forward(body('third')); - assert.deepEqual(fixture.receipt().archived, before.archived); - await fixture.close(); - } -); -lifecycle( - 'held response belongs only to captured old request while New uses a fresh child', - async () => { - const { fixture, acquired } = await setup(); - fixture.hold(); - const old = fixture.forward(body('old')); - await new Promise((resolve) => setImmediate(resolve)); - assert.equal(fixture.receipt().current.holds, 1); - fixture.hold(false); - const fresh = await fixture.forward(body('new')); - assert.equal(fresh.status, 200); - assert.equal(acquired.length, 2); - fixture.release(); - assert.equal((await old).status, 200); - assert.equal(fixture.receipt().current.requests[0].threadId, 'old'); - await fixture.close(); - } -); -lifecycle( - 'reset cancels held old response and rejects new work until acquisition and cleanup finish', - async () => { - const { fixture } = await setup(); - fixture.hold(); - const old = fixture.forward(body('old')); - const rejected = assert.rejects(old, /revoked/); - await new Promise((resolve) => setImmediate(resolve)); - await fixture.reset(); - await rejected; - assert.equal(fixture.receipt().current.holds, 0); - await fixture.close(); - } -); -lifecycle( - 'concurrent same-owner request and owner/generation bounds reject before forwarding', - async () => { - const { fixture, acquired } = await setup(); - fixture.hold(); - const held = fixture.forward(body('one')); - await new Promise((resolve) => setImmediate(resolve)); - await assert.rejects(fixture.forward(body('one', 1)), /active/); - fixture.hold(false); - fixture.release(); - await held; - for (let owner = 2; owner <= 8; owner++) - for (let turn = 0; turn < 4; turn++) - await fixture.forward(body(String(owner), turn)); - await assert.rejects(fixture.forward(body('ninth')), /owner bound/); - for (let turn = 1; turn < 4; turn++) - await fixture.forward(body('one', turn)); - assert.equal(acquired.flatMap((owner) => owner.calls).length, 32); - await assert.rejects(fixture.forward(body('one', 5)), /bound/); - await fixture.close(); - } -); -lifecycle( - 'partial acquisition retains its closure receipt and reset closure failure keeps routes unavailable', - async () => { - const partial = { - closure: { - worker: false, - listener: true, - sockets: true, - responses: true, - }, - }; - const { fixture } = await setup({ - acquire: async () => { - throw Object.assign(new Error('partial startup'), { - fixtureClosure: partial, - }); - }, - }); - await assert.rejects(fixture.forward(body('broken')), /partial startup/); - assert.deepEqual( - fixture.receipt().current.owners[0].acquisitionFailure, - partial - ); - await assert.rejects(fixture.reset(), /closure/); - await assert.rejects(fixture.forward(body('new')), /unavailable/); - await assert.rejects(fixture.close(), /closure/); - } -); -lifecycle( - 'explicit fake negative transport is labeled separately and New can still use a real canonical child', - async () => { - const { fixture, acquired } = await setup(); - fixture.failTransport(); - const failed = await fixture.forward(body('failed')); - assert.equal(failed.status, 503); - assert.equal(acquired.length, 0); - assert.equal( - fixture.receipt().current.requests[0].kind, - 'fake-negative-transport' - ); - assert.equal((await fixture.forward(body('new'))).status, 200); - assert.equal(acquired.length, 1); - await fixture.close(); - } -); -lifecycle( - 'reviewed identity and fields reject before canonical acquisition', - async () => { - const { fixture, acquired } = await setup(); - for (const raw of [ - Buffer.from('{}'), - body(''), - Buffer.from( - JSON.stringify({ ...JSON.parse(body('x')), surprise: 'denied' }) - ), - Buffer.alloc(2_000_001), - ]) - await assert.rejects(fixture.forward(raw)); - assert.equal(acquired.length, 0); - await fixture.close(); - } -); - -lifecycle( - 'forwards the measured installed SDK protocolVersion 1.0 request field unchanged', - async () => { - const { fixture, acquired } = await setup(); - const raw = Buffer.from( - JSON.stringify({ - ...JSON.parse(body('native-sdk')), - protocolVersion: '1.0', - }) - ); - try { - for (const protocolVersion of ['0.9', '2.0', 1, null]) - await assert.rejects( - fixture.forward( - Buffer.from( - JSON.stringify({ - ...JSON.parse(body('native-sdk')), - protocolVersion, - }) - ) - ), - /protocolVersion/ - ); - assert.equal(acquired.length, 0); - await fixture.forward(raw); - assert.deepEqual(acquired[0].calls[0], raw); - } finally { - await fixture.close(); - } - } -); - -lifecycle( - 'held delivery has its own bounded deadline and leaves no held authority', - async () => { - const { fixture } = await setup({ timeoutMs: 10, cleanupTimeoutMs: 20 }); - fixture.hold(); - const pending = fixture.forward(body('held-deadline')); - const result = await Promise.race([ - pending.then( - () => 'delivered', - (error) => String(error) - ), - new Promise((resolve) => setTimeout(() => resolve('unbounded'), 100)), - ]); - fixture.release(); - await pending.catch(() => undefined); - await fixture.close(); - assert.match(result, /Held delivery deadline/); - } -); - -lifecycle( - 'partial acquisition cleanup is bounded and late acquired resources are closed', - async () => { - let acquire; - const acquired = new Promise((resolve) => { - acquire = resolve; - }); - const { fixture } = await setup({ - acquire: () => acquired, - timeoutMs: 10, - cleanupTimeoutMs: 20, - }); - const pending = fixture - .forward(body('slow-acquisition')) - .catch((error) => String(error)); - const reset = fixture.reset(); - const result = await Promise.race([ - reset.then( - () => 'reset', - (error) => String(error) - ), - new Promise((resolve) => setTimeout(() => resolve('unbounded'), 100)), - ]); - let closed = false; - acquire({ - url: 'http://127.0.0.1:12345/agent', - close: async () => { - closed = true; - }, - stats: () => ({ - closure: { - worker: closed, - listener: closed, - sockets: closed, - responses: closed, - }, - }), - }); - await pending; - await reset.catch(() => undefined); - await fixture.close().catch(() => undefined); - assert.match(result, /closure/); - assert.equal(closed, true); - } -); - -lifecycle( - 'concurrent resets coalesce and cannot close a fresh unrevoked generation', - async () => { - const { fixture } = await setup(); - await fixture.forward(body('prior')); - const first = fixture.reset(), - second = fixture.reset(); - await Promise.allSettled([first, second]); - const receipt = fixture.receipt(); - await fixture.close(); - assert.equal(first, second); - assert.equal(receipt.current.generation, 2); - assert.equal(receipt.current.revoked, false); - assert.equal(receipt.archived.length, 1); - } -); - -lifecycle( - 'reset owns and closes buffered browser responses before exposing a fresh generation', - async () => { - const { fixture } = await setup(); - const request = Readable.from([body('browser-response')]); - request.method = 'POST'; - const response = new EventEmitter(); - response.writeHead = () => { - response.headersSent = true; - }; - response.end = () => { - /* Deliberately retain a pending browser write. */ - }; - response.destroy = () => { - response.destroyed = true; - setImmediate(() => response.emit('close')); - }; - await fixture(request, response, '/ag-ui/a2ui/agent'); - await fixture.reset(); - const destroyed = response.destroyed; - const prior = fixture.receipt().archived[0]; - await fixture.close(); - assert.equal(destroyed, true); - assert.equal(prior.proxyResponsesClosed, true); - } -); - -lifecycle( - 'an empty resource closure never authorizes a fresh generation', - async () => { - const { fixture, acquired } = await setup(); - await fixture.forward(body('empty-closure')); - acquired[0].stats = () => ({ closure: {} }); - const result = await fixture.reset().then( - () => 'reset', - (error) => String(error) - ); - await fixture.close().catch(() => undefined); - assert.match(result, /closure/); - } -); - -test( - 'actual canonical manager uses distinct child PIDs and exact encoded wire with verified closure', - { skip: !process.env.THREADPLANE_AGUI_A2UI_MANAGER_OPTIONS }, - async () => { - const options = JSON.parse( - process.env.THREADPLANE_AGUI_A2UI_MANAGER_OPTIONS - ); - const fixture = module.createAgUiA2uiFixture(options), - pids = []; - await fixture.reset(); - try { - for (const name of ['first', 'new']) { - const threadId = name + '-' + randomUUID(); - const input = { - ...JSON.parse(body(threadId)), - protocolVersion: '1.0', - messages: [ - { - id: randomUUID(), - role: 'user', - content: 'I want to fly LAX to JFK', - }, - ], - }; - const raw = Buffer.from(JSON.stringify(input)); - const result = await fixture.forward(raw); - assert.equal(result.status, 200); - const owner = fixture.receipt().current.owners.at(-1), - record = owner.stats.requests[0]; - pids.push(owner.stats.ready.pid); - assert.deepEqual(Buffer.from(record.rawBody), raw); - assert.deepEqual(result.bytes, Buffer.from(record.sentWire.join(''))); - assert.equal( - record.worker.observations.state.values.messages.length, - 2 - ); - assert.deepEqual(record.worker.observations.deniedNetwork, []); - assert.ok( - record.worker.events.some( - (event) => - event.type === 'RUN_FINISHED' && - event.threadId === threadId && - event.runId === input.runId - ) - ); - } - assert.equal(new Set(pids).size, 2); - } finally { - await fixture.close(); - } - const receipt = fixture.receipt(); - for (const generation of receipt.archived) - for (const owner of generation.owners) { - assert.deepEqual(owner.stats.closure, { - worker: true, - listener: true, - sockets: true, - responses: true, - }); - assert.equal(owner.stats.child.childClosed, true); - assert.equal(owner.stats.child.stdoutClosed, true); - assert.equal(owner.stats.child.stderrClosed, true); - } - assert.ok(pids.every((pid) => pidAbsent(pid))); - } -); diff --git a/scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs b/scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs index e15328e4a..732a4f59b 100644 --- a/scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs +++ b/scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs @@ -16,83 +16,14 @@ test('native A2UI preparation exports an import-inert closed entry point', () => const preparation = typeof module.prepareAgUiA2uiFixture === 'function' ? test : test.skip; -preparation('prepared browser service has its own finite lease and exact owned inputs', async () => { - const prepared = { env: { HOME: '/owned/home' }, python: '/owned/python', environment: '/owned/environment', home: '/owned/home', tmpdir: '/owned/tmp', attempt: '/owned/attempt' }; - let specification; - await module.servePreparedAgUiA2uiFixture(prepared, { run: async (value) => { specification = value; } }); - assert.equal(specification.timeoutMs, 35 * 60 * 1000); - assert.equal(specification.allowInterrupt, true); - assert.equal(specification.name, 'browser-server'); - assert.equal(specification.retain, prepared.attempt); - assert.equal(specification.executable, process.execPath); - assert.deepEqual(specification.args, [join(module.root, 'scripts/react-cockpit/serve.mjs'), 'ag-ui-a2ui', '--no-parent']); - assert.deepEqual(specification.env, { ...prepared.env, THREADPLANE_AGUI_A2UI_BROWSER_RUNTIME: JSON.stringify({ python: prepared.python, environment: prepared.environment, home: prepared.home, tmpdir: prepared.tmpdir }), THREADPLANE_AGUI_A2UI_BROWSER_RETAIN: join(prepared.attempt, 'browser') }); -}); - -for (const mode of ['parent-completion', 'fallback-expiry']) preparation(`actual browser stage preserves ${mode} lifetime with a fake clock`, async (context) => { - const retain = await mkdtemp(join(tmpdir(), 'a2ui-service-clock-')); - const child = new EventEmitter(); - child.pid = 12345; - child.stdout = new PassThrough(); - child.stderr = new PassThrough(); - let exited = false; - const signals = []; - const finish = () => { - if (exited) return; - exited = true; - child.stdout.end(); - child.stderr.end(); - setImmediate(() => child.emit('close', 0, null)); - }; - context.mock.timers.enable({ apis: ['setTimeout'] }); - const stage = module.servePreparedAgUiA2uiFixture({ env: {}, attempt: retain }, { run: (specification) => module.runPreparationStage({ ...specification, spawnChild: () => child, observeIdentity: () => exited ? null : '12345 1 12345 captured', observeGroup: () => exited ? [] : ['12345 1 12345 captured'], signalGroup: (_pid, signal) => { signals.push(signal); finish(); } }) }).then(value => ({ value }), error => ({ error })); - try { - // The old fixed lease expires here while the modeled 30-minute owner is still active. - context.mock.timers.tick(20 * 60 * 1000); - await new Promise(resolve => setImmediate(resolve)); - assert.deepEqual(signals, [], 'service must remain alive at the old 20-minute deadline'); - context.mock.timers.tick(10 * 60 * 1000); - assert.deepEqual(signals, [], 'service must outlive the full parent setup/test deadline'); - if (mode === 'parent-completion') process.emit('SIGTERM'); - else context.mock.timers.tick(5 * 60 * 1000); - const outcome = await stage; - if (mode === 'parent-completion') { - assert.equal(outcome.error, undefined); - assert.equal(outcome.value.code, 0); - } else assert.match(String(outcome.error), /Preparation stage deadline: browser-server/); - const receipt = JSON.parse(await readFile(join(retain, 'browser-server-result.json'), 'utf8')); - assert.equal(receipt.closure, true); - assert.equal(receipt.reaped, true); - assert.equal(receipt.stdoutEOF, true); - assert.equal(receipt.stderrEOF, true); - assert.equal(receipt.knownChildAbsent, true); - assert.deepEqual(receipt.groupMembers, []); - assert.deepEqual(signals, ['SIGTERM']); - } finally { - // Always release the injected child, even when the causal assertion fails. - finish(); - await stage; - context.mock.timers.reset(); - } -}); - -preparation('CLI accepts only explicit serve or installed proof modes', () => { - assert.deepEqual(module.parsePreparationArguments([]), { - serve: false, - verifyApplication: false, - }); - assert.deepEqual(module.parsePreparationArguments(['--serve']), { - serve: true, - verifyApplication: false, - }); - assert.deepEqual(module.parsePreparationArguments(['--verify-application']), { - serve: false, - verifyApplication: true, - }); +preparation('CLI accepts only preparation or the installed proof mode', () => { + assert.deepEqual(module.parsePreparationArguments([]), { verifyApplication: false }); + assert.deepEqual(module.parsePreparationArguments(['--verify-application']), { verifyApplication: true }); + // The browser server is serve.mjs on aimock now; preparation no longer serves. for (const flags of [ - ['--serve', '--verify-application'], + ['--serve'], + ['--verify-application', '--verify-application'], ['--root', '/tmp'], - ['--serve', '--serve'], ['--unknown'], ]) assert.throws(() => module.parsePreparationArguments(flags)); @@ -187,20 +118,9 @@ preparation( assert.ok(cache.args.includes('--fetch-retries=0')); assert.equal(cache.env.npm_config_cache, result.env.npm_config_cache); assert.ok(!cache.env.npm_config_cache.startsWith(result.attempt + '/')); - const manager = calls.filter( - (call) => call.name === 'actual-canonical-manager' - ); - assert.equal(manager.length, 1); - assert.deepEqual( - JSON.parse(manager[0].env.THREADPLANE_AGUI_A2UI_MANAGER_OPTIONS), - { - retain: join(result.attempt, 'canonical-manager'), - python: result.python, - environment: result.environment, - home: result.home, - tmpdir: result.tmpdir, - } - ); + // The React fixture's manager is gone (the browser e2e runs on aimock), so + // preparation no longer runs its proof. + assert.equal(calls.some((call) => call.name === 'actual-canonical-manager'), false); const failed = new Error('frozen failure'); await assert.rejects( module.prepareAgUiA2uiFixture({ diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index a1612f8d1..17a3d0a87 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -28,6 +28,7 @@ * `fromRequest: n` starts with the nth request * (0-based). /__requests marks a held run. * POST /__release flush and resume a held stream + * POST /__hold/off cancel a pending (or persistent) hold * POST /__fail-stream next agent POST answers 500 unforwarded (the * server is unavailable) * POST /__stream/edit body { ops }: edits to the next run stream, each @@ -392,6 +393,7 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', mo return {}; }], '/__release': [['POST'], () => { held?.release(); return {}; }], + '/__hold/off': [['POST'], () => { holdNext = null; return {}; }], '/__fail-stream': [['POST'], () => { failNext = true; return {}; }], '/__stream/edit': [['POST'], (body) => { if (body.persist) stickyEdits = body.ops ?? []; diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 912ac33e9..a0f8a6ac8 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -34,6 +34,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { topic: 'a2ui', library: 'ag-ui', adapter: 'ag-ui', appPath: 'cockpit/ag-ui/a2ui/react', base: '/ag-ui/a2ui/react/', port: 4647, project: 'cockpit-ag-ui-a2ui-react', + backend: aimockAgUi('ag-ui/a2ui', 4647), }); if (topic === 'chat-a2ui') return Object.freeze({ diff --git a/scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs b/scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs index bf3429723..c61c5baac 100644 --- a/scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs +++ b/scripts/react-cockpit/prepare-ag-ui-a2ui-fixture.mjs @@ -20,13 +20,10 @@ export function parsePreparationArguments(args) { assert.ok( Array.isArray(args) && args.length <= 1 && - args.every((flag) => ['--serve', '--verify-application'].includes(flag)), - 'Choose preparation, --serve or --verify-application' + args.every((flag) => flag === '--verify-application'), + 'Choose preparation or --verify-application' ); - return { - serve: args[0] === '--serve', - verifyApplication: args[0] === '--verify-application', - }; + return { verifyApplication: args[0] === '--verify-application' }; } function tool(name, override) { const choices = override @@ -497,29 +494,6 @@ export async function prepareAgUiA2uiFixture({ retain: attempt, name: 'public-package-cache', }); - // This opt-in child is the bounded actual manager proof; pure specs keep it skipped. - await execute({ - executable: process.execPath, - args: [ - '--test', - '--test-timeout=60000', - '--test-name-pattern=^actual canonical manager', - join(root, 'scripts/react-cockpit/ag-ui-a2ui-fixture.spec.mjs'), - ], - env: { - ...env, - THREADPLANE_AGUI_A2UI_MANAGER_OPTIONS: JSON.stringify({ - retain: join(attempt, 'canonical-manager'), - python: paths.python, - environment: paths.environment, - home: paths.home, - tmpdir: paths.tmpdir, - }), - }, - timeoutMs: 90000, - retain: attempt, - name: 'actual-canonical-manager', - }); const options = { root, retain: paths.retain, @@ -560,33 +534,6 @@ export async function prepareAgUiA2uiFixture({ } } -/** Own only the already-prepared browser service; preparation has its own deadline. */ -export const BROWSER_SERVER_FALLBACK_MS = 35 * 60 * 1000; -export function servePreparedAgUiA2uiFixture(result, { run = runPreparationStage } = {}) { - return run({ - executable: process.execPath, - args: [ - join(root, 'scripts/react-cockpit/serve.mjs'), - 'ag-ui-a2ui', - '--no-parent', - ], - env: { - ...result.env, - THREADPLANE_AGUI_A2UI_BROWSER_RUNTIME: JSON.stringify({ - python: result.python, - environment: result.environment, - home: result.home, - tmpdir: result.tmpdir, - }), - THREADPLANE_AGUI_A2UI_BROWSER_RETAIN: join(result.attempt, 'browser'), - }, - timeoutMs: BROWSER_SERVER_FALLBACK_MS, - retain: result.attempt, - name: 'browser-server', - allowInterrupt: true, - }); -} - if ( process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url) @@ -601,9 +548,6 @@ if ( installedApplication: mode.verifyApplication, }) ); - if (mode.serve) { - await servePreparedAgUiA2uiFixture(result); - } } catch { console.error( 'Native A2UI preparation or owned closure failed; retained attempt contains diagnostics.' diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 6671e8d97..5ec136963 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -5,7 +5,6 @@ import { dirname, extname, resolve, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; import { reactCockpitConfiguration } from './configuration.mjs'; import { createChatA2uiFixture, fixtureConfiguration } from './chat-a2ui-fixture.mjs'; -import { createAgUiA2uiFixture } from './ag-ui-a2ui-fixture.mjs'; import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; @@ -15,8 +14,6 @@ import { createAimockBackend } from './aimock-backend.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); -const agUiA2uiFixture = configuration.library === 'ag-ui' && configuration.adapter === 'ag-ui' && configuration.topic === 'a2ui' - ? createAgUiA2uiFixture({ ...JSON.parse(process.env.THREADPLANE_AGUI_A2UI_BROWSER_RUNTIME ?? '{}'), retain: process.env.THREADPLANE_AGUI_A2UI_BROWSER_RETAIN }) : null; const isChatA2ui = configuration.library === 'chat' && configuration.topic === 'a2ui'; let chatA2uiFixture = null; let chatA2uiConfiguration; @@ -79,7 +76,6 @@ const json = (response, value, status = 200) => { }; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; - if (agUiA2uiFixture && (await agUiA2uiFixture(request, response, pathname))) return; if (chatA2uiFixture) { if (pathname.startsWith('/__')) { const controls = { @@ -192,15 +188,11 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { +if (isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { shutdown = (code = 0) => { closing ??= (async () => { let a2uiFailure; - if (agUiA2uiFixture) { - try { await agUiA2uiFixture.close(); } - catch (error) { a2uiFailure = error; } - } - else if (isChatA2ui) { + if (isChatA2ui) { releaseA2uiStreams(true); try { await chatA2uiFixture?.close(); } catch (error) { a2uiFailure = error; } @@ -211,10 +203,9 @@ if (agUiA2uiFixture || isChatA2ui || nativeStrandsFixture || nativeMafFixture || owned.closeAllConnections(); }))); if (chatA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatA2uiFixtureClosed: a2uiReceipt(), listenersClosed: true })); - if (agUiA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), agUiA2uiFixtureClosed: agUiA2uiFixture.receipt(), bufferedCanonicalWire: true, listenersClosed: true })); if (a2uiFailure) throw a2uiFailure; })(); - if (agUiA2uiFixture || isChatA2ui) void closing.then(() => process.exit(code), () => process.exit(1)); + if (isChatA2ui) void closing.then(() => process.exit(code), () => process.exit(1)); else void closing.finally(() => process.exit(code)); }; process.once('SIGINT', () => shutdown()); From 64a624a47fb60a5edda641674378fdd381280165 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 21:27:06 -0800 Subject: [PATCH 38/41] test(cockpit): run React chat/a2ui e2e, packed-consumer probe and website preview on aimock (batch 6c) The old wire's model calls become aimock fixtures keyed on each node's full system prompt, replayed into a real `langgraph dev` behind the recording proxy. The packed-consumer probe (fixture-test and the Library lane step) runs the installed package and the compiled admission helpers against the same backend; its harness tests move to chat-a2ui-native-probe.spec.mjs. The fixture spec's graph cases (swallowed title failure, one structured-output retry, fallback after three invalid answers) are reached through tagged model output and run in the React e2e. The fixture server, its Python wire, and its fixture, host and readiness specs are deleted. createAimockBackend.close() now stops uv's whole process tree. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 7 +- apps/website/playwright.config.ts | 2 +- apps/website/src/playwright-config.spec.ts | 4 +- .../a2ui/angular/e2e/fixtures/c-a2ui.json | 170 ++++++ cockpit/chat/a2ui/react/README.md | 2 +- cockpit/chat/a2ui/react/e2e/a2ui.spec.ts | 103 +++- .../chat/a2ui/react/e2e/global-setup-impl.ts | 6 + .../chat/a2ui/react/e2e/playwright.config.ts | 3 + .../fixtures/terminal-admission.spec.mjs | 8 +- cockpit/chat/a2ui/react/project.json | 1 + .../react/src/connection.integration.spec.ts | 90 ++-- .../2026-11-18-react-cockpit-aimock-design.md | 2 +- scripts/ci-scope.mjs | 12 +- scripts/ci-scope.spec.mjs | 33 +- .../fixtures/library-validation-steps.json | 4 +- scripts/react-cockpit/aimock-backend.mjs | 51 +- .../chat-a2ui-configuration.spec.mjs | 19 +- scripts/react-cockpit/chat-a2ui-fixture.mjs | 469 ----------------- .../react-cockpit/chat-a2ui-fixture.spec.mjs | 495 ------------------ scripts/react-cockpit/chat-a2ui-host.spec.mjs | 224 -------- .../react-cockpit/chat-a2ui-native-probe.mjs | 126 +++-- .../chat-a2ui-native-probe.spec.mjs | 219 ++++++++ .../chat-a2ui-readiness.spec.mjs | 129 ----- scripts/react-cockpit/chat-a2ui-wire.py | 179 ------- scripts/react-cockpit/configuration.mjs | 1 + scripts/react-cockpit/serve.mjs | 87 +-- 26 files changed, 698 insertions(+), 1748 deletions(-) create mode 100644 cockpit/chat/a2ui/react/e2e/global-setup-impl.ts delete mode 100644 scripts/react-cockpit/chat-a2ui-fixture.mjs delete mode 100644 scripts/react-cockpit/chat-a2ui-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/chat-a2ui-host.spec.mjs create mode 100644 scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs delete mode 100644 scripts/react-cockpit/chat-a2ui-readiness.spec.mjs delete mode 100644 scripts/react-cockpit/chat-a2ui-wire.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c5eb9b38f..7b8ac0647 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -328,7 +328,7 @@ jobs: - name: Verify production builds strip the devtools hook if: matrix.lane == 'packages' run: npx nx run chat:test-devtools-bundle - - name: Verify canonical A2UI native fixtures + - name: Verify canonical A2UI native graph if: matrix.lane == 'packages' timeout-minutes: 15 env: @@ -351,10 +351,9 @@ jobs: PYTHONDONTWRITEBYTECODE=1 LANGSMITH_TRACING=false LANGCHAIN_TRACING_V2=false \ "$A2UI_FIXTURE_ENV/bin/python" -E -B -m unittest discover -s cockpit/chat/a2ui/python/tests -p test_booking_form.py env -i HOME="$fixture_home" PATH="$fixture_path" LANG=C.UTF-8 \ - A2UI_FIXTURE_UV="$uv_path" A2UI_FIXTURE_PYTHON="$python_path" \ - A2UI_FIXTURE_ENV="$A2UI_FIXTURE_ENV" A2UI_FIXTURE_CACHE="$A2UI_FIXTURE_CACHE" \ + UV_CACHE_DIR="$A2UI_FIXTURE_CACHE" UV_PYTHON="$python_path" \ PYTHONDONTWRITEBYTECODE=1 LANGSMITH_TRACING=false LANGCHAIN_TRACING_V2=false \ - "$node_path" --test --test-concurrency=1 scripts/react-cockpit/chat-a2ui-fixture.spec.mjs scripts/react-cockpit/chat-a2ui-configuration.spec.mjs scripts/react-cockpit/chat-a2ui-host.spec.mjs scripts/react-cockpit/chat-a2ui-readiness.spec.mjs + "$node_path" --test --test-concurrency=1 --test-force-exit scripts/react-cockpit/chat-a2ui-configuration.spec.mjs scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs - name: Configure bounded Ubuntu package downloads if: matrix.lane == 'packages' run: sudo "$(command -v node)" scripts/configure-ci-apt.mjs diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index 35d3b84a1..bfdc169f9 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -464,7 +464,7 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent', + command: 'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent --backend', cwd: '../..', url: 'http://127.0.0.1:4646', reuseExistingServer: false, diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 0c96382a3..6f95e99e5 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -521,7 +521,7 @@ describe('Website Playwright configuration', () => { expect.objectContaining({command:'npx nx run cockpit-chat-messages-python:smoke && node scripts/react-cockpit/serve.mjs chat-messages --no-parent',url:'http://127.0.0.1:4620'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-theming --no-parent --backend',url:'http://127.0.0.1:4644/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-debug --no-parent --backend',url:'http://127.0.0.1:4645/__ready'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent',url:'http://127.0.0.1:4646'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent --backend',url:'http://127.0.0.1:4646'}), expect.objectContaining({command:'npx nx run cockpit-chat-input-python:smoke && node scripts/react-cockpit/serve.mjs chat-input --no-parent',url:'http://127.0.0.1:4621'}), expect.objectContaining({command:'npx nx run cockpit-chat-interrupts-python:smoke && node scripts/react-cockpit/serve.mjs chat-interrupts --no-parent',url:'http://127.0.0.1:4622'}), expect.objectContaining({command:'npx nx run cockpit-chat-tool-calls-python:smoke && node scripts/react-cockpit/serve.mjs chat-tool-calls --no-parent',url:'http://127.0.0.1:4623'}), @@ -594,7 +594,7 @@ it('scopes native Chat A2UI checks to Website and the exact owned React server', const servers = Array.isArray(config.webServer) ? config.webServer : []; expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4646']); expect(servers.every(server => server.reuseExistingServer === false)).toBe(true); - expect(servers[1]?.command).toBe('node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent'); + expect(servers[1]?.command).toBe('node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent --backend'); expect(config.testMatch).toBe('**/react-chat-a2ui-preview.spec.ts'); expect(config.workers).toBe(1); expect(config.retries).toBe(0); diff --git a/cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json b/cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json index 84d191e97..0922c62f8 100644 --- a/cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json +++ b/cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json @@ -1,5 +1,63 @@ { "fixtures": [ + { + "match": { + "systemMessage": "In 3-5 words, summarize", + "userMessage": "[scenario:title-down]" + }, + "response": { + "error": { + "message": "Injected: title provider down", + "type": "server_error" + }, + "status": 500 + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer", + "userMessage": "[scenario:retry]", + "sequenceIndex": 0 + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"LAX\"], \"dest\": [\"JFK\"], \"date\": \"\", \"passengers\": 1, \"fare_class\": [\"Economy\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economomy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer", + "userMessage": "[scenario:retry]", + "sequenceIndex": 1 + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\":\"booking\",\"data_model\":{\"origin\":[\"LAX\"],\"dest\":[\"JFK\"],\"date\":\"\",\"passengers\":1,\"fare_class\":[\"Economy\"]},\"components\":[{\"id\":\"root\",\"component\":\"Column\",\"children\":[\"card\"]},{\"id\":\"card\",\"component\":\"Card\",\"child\":\"card_col\"},{\"id\":\"card_col\",\"component\":\"Column\",\"children\":[\"title\",\"origin\",\"dest\",\"date\",\"passengers\",\"fare\",\"submit\"]},{\"id\":\"title\",\"component\":\"Text\",\"text\":\"Book a flight\",\"variant\":\"h2\"},{\"id\":\"origin\",\"component\":\"ChoicePicker\",\"label\":\"Origin\",\"options\":[{\"label\":\"LAX\",\"value\":\"LAX\"},{\"label\":\"JFK\",\"value\":\"JFK\"},{\"label\":\"SFO\",\"value\":\"SFO\"},{\"label\":\"ORD\",\"value\":\"ORD\"},{\"label\":\"BOS\",\"value\":\"BOS\"},{\"label\":\"ATL\",\"value\":\"ATL\"},{\"label\":\"DFW\",\"value\":\"DFW\"},{\"label\":\"SEA\",\"value\":\"SEA\"},{\"label\":\"MIA\",\"value\":\"MIA\"},{\"label\":\"DEN\",\"value\":\"DEN\"}],\"value\":{\"path\":\"/origin\"},\"variant\":\"mutuallyExclusive\"},{\"id\":\"dest\",\"component\":\"ChoicePicker\",\"label\":\"Destination\",\"options\":[{\"label\":\"LAX\",\"value\":\"LAX\"},{\"label\":\"JFK\",\"value\":\"JFK\"},{\"label\":\"SFO\",\"value\":\"SFO\"},{\"label\":\"ORD\",\"value\":\"ORD\"},{\"label\":\"BOS\",\"value\":\"BOS\"},{\"label\":\"ATL\",\"value\":\"ATL\"},{\"label\":\"DFW\",\"value\":\"DFW\"},{\"label\":\"SEA\",\"value\":\"SEA\"},{\"label\":\"MIA\",\"value\":\"MIA\"},{\"label\":\"DEN\",\"value\":\"DEN\"}],\"value\":{\"path\":\"/dest\"},\"variant\":\"mutuallyExclusive\"},{\"id\":\"date\",\"component\":\"DateTimeInput\",\"label\":\"Departure date (YYYY-MM-DD)\",\"value\":{\"path\":\"/date\"},\"enableDate\":true},{\"id\":\"passengers\",\"component\":\"TextField\",\"label\":\"Passengers\",\"value\":{\"path\":\"/passengers\"},\"variant\":\"number\"},{\"id\":\"fare\",\"component\":\"ChoicePicker\",\"label\":\"Fare class\",\"options\":[{\"label\":\"Economy\",\"value\":\"Economy\"},{\"label\":\"Premium\",\"value\":\"Premium\"},{\"label\":\"Business\",\"value\":\"Business\"},{\"label\":\"First\",\"value\":\"First\"}],\"value\":{\"path\":\"/fare_class\"},\"variant\":\"mutuallyExclusive\"},{\"id\":\"submit_label\",\"component\":\"Text\",\"text\":\"Search flights\"},{\"id\":\"submit\",\"component\":\"Button\",\"child\":\"submit_label\",\"variant\":\"primary\",\"action\":{\"event\":{\"name\":\"bookingSubmit\",\"context\":{\"formId\":\"booking\",\"origin\":{\"path\":\"/origin\"},\"dest\":{\"path\":\"/dest\"},\"date\":{\"path\":\"/date\"},\"passengers\":{\"path\":\"/passengers\"},\"fare_class\":{\"path\":\"/fare_class\"}}}}}]}", + "id": "call_DiKu9KXnOKtbRcNBtDlXwTzx" + } + ] + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer", + "userMessage": "[scenario:invalid]" + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"LAX\"], \"dest\": [\"JFK\"], \"date\": \"\", \"passengers\": 1, \"fare_class\": [\"Economy\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economomy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}]}" + } + ] + } + }, { "match": { "userMessage": "I want to fly LAX to JFK" @@ -27,6 +85,118 @@ } ] } + }, + { + "match": { + "systemMessage": "In 3-5 words, summarize" + }, + "response": { + "content": "Local booking title" + } + }, + { + "match": { + "systemMessage": "You just received a booking submission. The find_routes() tool returned the following flights:\n\n[\n {\n \"flight_number\": \"UA204\",\n \"airline\": \"UA\",\n \"from\": \"SFO\",\n \"to\": \"SEA\",\n \"depart_local\": \"09:00\",\n \"arrive_local\": \"11:00\",\n \"duration_min\": 120,\n \"status\": \"on_time\",\n \"gate\": \"F11\",\n \"aircraft\": \"Boeing 737\"\n }\n]\n\nForm data (for context): {\n \"formId\": \"booking\",\n \"origin\": \"SFO\",\n \"dest\": \"SEA\",\n \"date\": \"2026-11-03\",\n \"passengers\": 3,\n \"fare_class\": \"Business\"\n}\n\nEmit an A2UI v0.9 results surface using the FlightResultsSpec schema.\n\nA2UI format (CRITICAL): every component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string; props sit at the top level of the same object. Dynamic values are bare literals or `{\"path\": \"/ptr\"}`.\n\nAllowed component names: Column, Row, Card, Text, TextField, Button, Divider, List.\n\nPer-component shapes you'll need:\n Column / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"}\n Text: {\"text\": \"literal\", \"variant\": \"h2\"} (or h1/h3/body/caption)\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"flightId\": \"\"}}}}\n Divider: {}\n\nSurface constraints:\n surface_id MUST be \"results\"\n data_model can be {}\n Exactly ONE component MUST have id \"root\": a Column whose children lists every flight Card id (or just [\"no_flights\"] when empty)\n\nBuild pattern (one per flight):\n card_ (Card, child=col_)\n col_ (Column, children = [title_, route_, time_, btn_])\n title_ (Text, text=\" flight \", variant=\"h3\")\n route_ (Text, text=\" \u2192 \u2022 min \u2022 \", variant=\"body\")\n time_ (Text, text=\"Depart \u2022 Arrive \u2022 Gate \", variant=\"caption\")\n btn_ (Button, child=btn_label_, variant=\"primary\",\n action={\"event\":{\"name\":\"flightSelect\",\"context\":{\"flightId\":\"\"}}})\n btn_label_ (Text, text=\"Select\")\n\nEmpty case: components = [\n {\"id\":\"root\", \"component\":\"Column\", \"children\":[\"no_flights\"]},\n {\"id\":\"no_flights\", \"component\":\"Card\", \"child\":\"empty_col\"},\n {\"id\":\"empty_col\", \"component\":\"Column\", \"children\":[\"empty_msg\",\"modify_btn\"]},\n {\"id\":\"empty_msg\", \"component\":\"Text\", \"text\":\"No flights found\", \"variant\":\"h3\"},\n {\"id\":\"modify_btn\", \"component\":\"Button\", \"child\":\"modify_label\", \"action\":{\"event\":{\"name\":\"modifySearch\",\"context\":{\"formId\":\"booking\"}}}},\n {\"id\":\"modify_label\", \"component\":\"Text\", \"text\":\"Modify search\"}\n]\n\nUse unique ids for every component." + }, + "response": { + "toolCalls": [ + { + "name": "FlightResultsSpec", + "arguments": "{\"surface_id\": \"results\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card_0\"]}, {\"id\": \"card_0\", \"component\": \"Card\", \"child\": \"col_0\"}, {\"id\": \"col_0\", \"component\": \"Column\", \"children\": [\"title_0\", \"route_0\", \"time_0\", \"btn_0\"]}, {\"id\": \"title_0\", \"component\": \"Text\", \"text\": \"UA flight UA204\", \"variant\": \"h3\"}, {\"id\": \"route_0\", \"component\": \"Text\", \"text\": \"SFO \u2192 SEA \u2022 120 min \u2022 Boeing 737\", \"variant\": \"body\"}, {\"id\": \"time_0\", \"component\": \"Text\", \"text\": \"Depart 09:00 \u2022 Arrive 11:00 \u2022 Gate F11\", \"variant\": \"caption\"}, {\"id\": \"btn_0\", \"component\": \"Button\", \"child\": \"btn_label_0\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"flightSelect\", \"context\": {\"flightId\": \"UA204\"}}}}, {\"id\": \"btn_label_0\", \"component\": \"Text\", \"text\": \"Select\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You just received a Select event from the flight results surface. The user picked a flight; here are its details from lookup_flight():\n\n{\n \"flight_number\": \"UA204\",\n \"airline\": \"UA\",\n \"from\": \"SFO\",\n \"to\": \"SEA\",\n \"depart_local\": \"09:00\",\n \"arrive_local\": \"11:00\",\n \"duration_min\": 120,\n \"status\": \"on_time\",\n \"gate\": \"F11\",\n \"aircraft\": \"Boeing 737\"\n}\n\nThe user's prior search context (party_text, derived from passengers + fare_class on the original booking submission): 3 passengers \u2022 Business\n\nThe selected flight number (from the Select button's action context, used in the fallback title if flight_json is null): UA204\n\nEmit an A2UI v0.9 confirmation surface using the ConfirmationSpec schema.\n\nA2UI format (CRITICAL): every component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string; props sit at the top level of the same object. Dynamic values are bare literals or `{\"path\": \"/ptr\"}`. Exactly ONE component MUST have id \"root\".\n\nAllowed component names: Column, Card, Text, Button, Divider.\n\nPer-component shapes you'll need:\n Column: {\"children\": [\"id1\",\"id2\"]}\n Card: {\"child\": \"\"}\n Text: {\"text\": \"literal\", \"variant\": \"h2\"} (h1/h2/h3/body/caption)\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"formId\": \"booking\"}}}}\n Divider: {}\n\nSurface constraints:\n surface_id MUST be \"confirmation\"\n data_model = {}\n\nBuild this component tree exactly (use these ids):\n root (Column, children = [card])\n card (Card, child = card_col)\n card_col (Column, children = [title, route_text, time_text, gate_text, divider, party_text, modify])\n title (Text, \" flight \", variant=\"h2\")\n route_text (Text, \" \u2192 \u2022 min \u2022 \", variant=\"body\")\n time_text (Text, \"Depart \u2022 Arrive \", variant=\"caption\")\n gate_text (Text, \"Gate \", variant=\"caption\")\n divider (Divider, {})\n party_text (Text, \"3 passengers \u2022 Business\", variant=\"body\") \u2190 use the supplied string verbatim\n modify (Button, child=modify_label, variant=\"primary\",\n action={\"event\":{\"name\":\"modifySearch\",\"context\":{\"formId\":\"booking\"}}})\n modify_label (Text, \"Modify search\")\n\nIf flight_json is null, omit route_text, time_text, gate_text from card_col's children and set the title to \"Flight UA204 selected\" (or \"Booking selected\" if UA204 is empty). Always include party_text + modify." + }, + "response": { + "toolCalls": [ + { + "name": "ConfirmationSpec", + "arguments": "{\"surface_id\": \"confirmation\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"party_text\", \"modify\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"UA flight UA204\", \"variant\": \"h2\"}, {\"id\": \"party_text\", \"component\": \"Text\", \"text\": \"3 passengers \u2022 Business\", \"variant\": \"body\"}, {\"id\": \"modify\", \"component\": \"Button\", \"child\": \"modify_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"modifySearch\", \"context\": {\"formId\": \"booking\"}}}}, {\"id\": \"modify_label\", \"component\": \"Text\", \"text\": \"Modify search\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer. Emit an A2UI v0.9 booking form using the structured output schema.\n\nA2UI FORMAT (CRITICAL): each component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string and the props sit at the top level of the same object. ComponentName must be one of:\n Column, Row, Card, Text, TextField, ChoicePicker, DateTimeInput, CheckBox, Button, Divider, List, Image, Icon, Modal, Slider, Tabs\n\nDynamic values are bare literals (\"LAX\", 3, true) or data bindings `{\"path\": \"/json/pointer\"}`. Never wrap literals.\n\nPer-component shapes:\n Column / Row / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"} \u2190 single child only\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"formId\": \"booking\"}}}}\n Text: {\"text\": \"literal string\", \"variant\": \"h2\"} (h1/h2/h3/h4/h5/caption/body)\n TextField: {\"label\": \"Field\", \"value\": {\"path\": \"/p\"}, \"variant\": \"shortText\"} (shortText/longText/number/obscured)\n DateTimeInput: {\"label\": \"Date\", \"value\": {\"path\": \"/p\"}, \"enableDate\": true}\n ChoicePicker: {\"label\": \"Origin\", \"options\": [{\"label\":\"LAX\",\"value\":\"LAX\"}], \"value\": {\"path\":\"/origin\"}, \"variant\": \"mutuallyExclusive\"}\n CheckBox: {\"label\": \"...\", \"value\": {\"path\":\"/p\"}}\n Divider: {}\n\nExactly ONE component MUST have id \"root\" \u2014 it is the root of the tree.\n\nRequired form composition for THIS task:\n surface_id MUST be \"booking\"\n data_model MUST be {\"origin\": [\"SFO\"], \"dest\": [\"SEA\"], \"date\": \"2026-11-03\", \"passengers\": 3, \"fare_class\": [\"Business\"]} \u2190 use these values verbatim as the field defaults\n\n Build this component tree:\n root (Column, children=[\"card\"])\n card (Card, child=card_col)\n card_col (Column, children=[title, origin, dest, date, passengers, fare, submit])\n title (Text, text=\"Book a flight\", variant=\"h2\")\n origin (ChoicePicker, label=\"Origin\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/origin\"}, variant=\"mutuallyExclusive\")\n dest (ChoicePicker, label=\"Destination\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/dest\"}, variant=\"mutuallyExclusive\")\n date (DateTimeInput, label=\"Departure date (YYYY-MM-DD)\", value={\"path\":\"/date\"}, enableDate=true)\n passengers (TextField, label=\"Passengers\", value={\"path\":\"/passengers\"}, variant=\"number\")\n fare (ChoicePicker, label=\"Fare class\", options=[{'label': 'Economy', 'value': 'Economy'}, {'label': 'Premium', 'value': 'Premium'}, {'label': 'Business', 'value': 'Business'}, {'label': 'First', 'value': 'First'}], value={\"path\":\"/fare_class\"}, variant=\"mutuallyExclusive\")\n submit (Button, child=submit_label, variant=\"primary\", action={\"event\":{\"name\":\"bookingSubmit\",\"context\":{\n \"formId\":\"booking\",\n \"origin\":{\"path\":\"/origin\"},\n \"dest\":{\"path\":\"/dest\"},\n \"date\":{\"path\":\"/date\"},\n \"passengers\":{\"path\":\"/passengers\"},\n \"fare_class\":{\"path\":\"/fare_class\"}\n }}})\n submit_label (Text, text=\"Search flights\")\n\nUse these exact ids." + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"SFO\"], \"dest\": [\"SEA\"], \"date\": \"2026-11-03\", \"passengers\": 3, \"fare_class\": [\"Business\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You just received a booking submission. The find_routes() tool returned the following flights:\n\n[\n {\n \"flight_number\": \"UA123\",\n \"airline\": \"UA\",\n \"from\": \"LAX\",\n \"to\": \"JFK\",\n \"depart_local\": \"08:00\",\n \"arrive_local\": \"16:30\",\n \"duration_min\": 330,\n \"status\": \"on_time\",\n \"gate\": \"B14\",\n \"aircraft\": \"Boeing 787\"\n }\n]\n\nForm data (for context): {\n \"formId\": \"booking\",\n \"origin\": \"LAX\",\n \"dest\": \"JFK\",\n \"date\": \"2026-11-03\",\n \"passengers\": 2,\n \"fare_class\": \"Business\"\n}\n\nEmit an A2UI v0.9 results surface using the FlightResultsSpec schema.\n\nA2UI format (CRITICAL): every component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string; props sit at the top level of the same object. Dynamic values are bare literals or `{\"path\": \"/ptr\"}`.\n\nAllowed component names: Column, Row, Card, Text, TextField, Button, Divider, List.\n\nPer-component shapes you'll need:\n Column / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"}\n Text: {\"text\": \"literal\", \"variant\": \"h2\"} (or h1/h3/body/caption)\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"flightId\": \"\"}}}}\n Divider: {}\n\nSurface constraints:\n surface_id MUST be \"results\"\n data_model can be {}\n Exactly ONE component MUST have id \"root\": a Column whose children lists every flight Card id (or just [\"no_flights\"] when empty)\n\nBuild pattern (one per flight):\n card_ (Card, child=col_)\n col_ (Column, children = [title_, route_, time_, btn_])\n title_ (Text, text=\" flight \", variant=\"h3\")\n route_ (Text, text=\" \u2192 \u2022 min \u2022 \", variant=\"body\")\n time_ (Text, text=\"Depart \u2022 Arrive \u2022 Gate \", variant=\"caption\")\n btn_ (Button, child=btn_label_, variant=\"primary\",\n action={\"event\":{\"name\":\"flightSelect\",\"context\":{\"flightId\":\"\"}}})\n btn_label_ (Text, text=\"Select\")\n\nEmpty case: components = [\n {\"id\":\"root\", \"component\":\"Column\", \"children\":[\"no_flights\"]},\n {\"id\":\"no_flights\", \"component\":\"Card\", \"child\":\"empty_col\"},\n {\"id\":\"empty_col\", \"component\":\"Column\", \"children\":[\"empty_msg\",\"modify_btn\"]},\n {\"id\":\"empty_msg\", \"component\":\"Text\", \"text\":\"No flights found\", \"variant\":\"h3\"},\n {\"id\":\"modify_btn\", \"component\":\"Button\", \"child\":\"modify_label\", \"action\":{\"event\":{\"name\":\"modifySearch\",\"context\":{\"formId\":\"booking\"}}}},\n {\"id\":\"modify_label\", \"component\":\"Text\", \"text\":\"Modify search\"}\n]\n\nUse unique ids for every component." + }, + "response": { + "toolCalls": [ + { + "name": "FlightResultsSpec", + "arguments": "{\"surface_id\": \"results\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card_0\"]}, {\"id\": \"card_0\", \"component\": \"Card\", \"child\": \"col_0\"}, {\"id\": \"col_0\", \"component\": \"Column\", \"children\": [\"title_0\", \"route_0\", \"time_0\", \"btn_0\"]}, {\"id\": \"title_0\", \"component\": \"Text\", \"text\": \"UA flight UA123\", \"variant\": \"h3\"}, {\"id\": \"route_0\", \"component\": \"Text\", \"text\": \"LAX \u2192 JFK \u2022 330 min \u2022 Boeing 787\", \"variant\": \"body\"}, {\"id\": \"time_0\", \"component\": \"Text\", \"text\": \"Depart 08:00 \u2022 Arrive 16:30 \u2022 Gate B14\", \"variant\": \"caption\"}, {\"id\": \"btn_0\", \"component\": \"Button\", \"child\": \"btn_label_0\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"flightSelect\", \"context\": {\"flightId\": \"UA123\"}}}}, {\"id\": \"btn_label_0\", \"component\": \"Text\", \"text\": \"Select\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You just received a Select event from the flight results surface. The user picked a flight; here are its details from lookup_flight():\n\n{\n \"flight_number\": \"UA123\",\n \"airline\": \"UA\",\n \"from\": \"LAX\",\n \"to\": \"JFK\",\n \"depart_local\": \"08:00\",\n \"arrive_local\": \"16:30\",\n \"duration_min\": 330,\n \"status\": \"on_time\",\n \"gate\": \"B14\",\n \"aircraft\": \"Boeing 787\"\n}\n\nThe user's prior search context (party_text, derived from passengers + fare_class on the original booking submission): 2 passengers \u2022 Business\n\nThe selected flight number (from the Select button's action context, used in the fallback title if flight_json is null): UA123\n\nEmit an A2UI v0.9 confirmation surface using the ConfirmationSpec schema.\n\nA2UI format (CRITICAL): every component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string; props sit at the top level of the same object. Dynamic values are bare literals or `{\"path\": \"/ptr\"}`. Exactly ONE component MUST have id \"root\".\n\nAllowed component names: Column, Card, Text, Button, Divider.\n\nPer-component shapes you'll need:\n Column: {\"children\": [\"id1\",\"id2\"]}\n Card: {\"child\": \"\"}\n Text: {\"text\": \"literal\", \"variant\": \"h2\"} (h1/h2/h3/body/caption)\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"formId\": \"booking\"}}}}\n Divider: {}\n\nSurface constraints:\n surface_id MUST be \"confirmation\"\n data_model = {}\n\nBuild this component tree exactly (use these ids):\n root (Column, children = [card])\n card (Card, child = card_col)\n card_col (Column, children = [title, route_text, time_text, gate_text, divider, party_text, modify])\n title (Text, \" flight \", variant=\"h2\")\n route_text (Text, \" \u2192 \u2022 min \u2022 \", variant=\"body\")\n time_text (Text, \"Depart \u2022 Arrive \", variant=\"caption\")\n gate_text (Text, \"Gate \", variant=\"caption\")\n divider (Divider, {})\n party_text (Text, \"2 passengers \u2022 Business\", variant=\"body\") \u2190 use the supplied string verbatim\n modify (Button, child=modify_label, variant=\"primary\",\n action={\"event\":{\"name\":\"modifySearch\",\"context\":{\"formId\":\"booking\"}}})\n modify_label (Text, \"Modify search\")\n\nIf flight_json is null, omit route_text, time_text, gate_text from card_col's children and set the title to \"Flight UA123 selected\" (or \"Booking selected\" if UA123 is empty). Always include party_text + modify." + }, + "response": { + "toolCalls": [ + { + "name": "ConfirmationSpec", + "arguments": "{\"surface_id\": \"confirmation\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"party_text\", \"modify\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"UA flight UA123\", \"variant\": \"h2\"}, {\"id\": \"party_text\", \"component\": \"Text\", \"text\": \"2 passengers \u2022 Business\", \"variant\": \"body\"}, {\"id\": \"modify\", \"component\": \"Button\", \"child\": \"modify_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"modifySearch\", \"context\": {\"formId\": \"booking\"}}}}, {\"id\": \"modify_label\", \"component\": \"Text\", \"text\": \"Modify search\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer. Emit an A2UI v0.9 booking form using the structured output schema.\n\nA2UI FORMAT (CRITICAL): each component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string and the props sit at the top level of the same object. ComponentName must be one of:\n Column, Row, Card, Text, TextField, ChoicePicker, DateTimeInput, CheckBox, Button, Divider, List, Image, Icon, Modal, Slider, Tabs\n\nDynamic values are bare literals (\"LAX\", 3, true) or data bindings `{\"path\": \"/json/pointer\"}`. Never wrap literals.\n\nPer-component shapes:\n Column / Row / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"} \u2190 single child only\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"formId\": \"booking\"}}}}\n Text: {\"text\": \"literal string\", \"variant\": \"h2\"} (h1/h2/h3/h4/h5/caption/body)\n TextField: {\"label\": \"Field\", \"value\": {\"path\": \"/p\"}, \"variant\": \"shortText\"} (shortText/longText/number/obscured)\n DateTimeInput: {\"label\": \"Date\", \"value\": {\"path\": \"/p\"}, \"enableDate\": true}\n ChoicePicker: {\"label\": \"Origin\", \"options\": [{\"label\":\"LAX\",\"value\":\"LAX\"}], \"value\": {\"path\":\"/origin\"}, \"variant\": \"mutuallyExclusive\"}\n CheckBox: {\"label\": \"...\", \"value\": {\"path\":\"/p\"}}\n Divider: {}\n\nExactly ONE component MUST have id \"root\" \u2014 it is the root of the tree.\n\nRequired form composition for THIS task:\n surface_id MUST be \"booking\"\n data_model MUST be {\"origin\": [\"LAX\"], \"dest\": [\"JFK\"], \"date\": \"2026-11-03\", \"passengers\": 2, \"fare_class\": [\"Business\"]} \u2190 use these values verbatim as the field defaults\n\n Build this component tree:\n root (Column, children=[\"card\"])\n card (Card, child=card_col)\n card_col (Column, children=[title, origin, dest, date, passengers, fare, submit])\n title (Text, text=\"Book a flight\", variant=\"h2\")\n origin (ChoicePicker, label=\"Origin\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/origin\"}, variant=\"mutuallyExclusive\")\n dest (ChoicePicker, label=\"Destination\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/dest\"}, variant=\"mutuallyExclusive\")\n date (DateTimeInput, label=\"Departure date (YYYY-MM-DD)\", value={\"path\":\"/date\"}, enableDate=true)\n passengers (TextField, label=\"Passengers\", value={\"path\":\"/passengers\"}, variant=\"number\")\n fare (ChoicePicker, label=\"Fare class\", options=[{'label': 'Economy', 'value': 'Economy'}, {'label': 'Premium', 'value': 'Premium'}, {'label': 'Business', 'value': 'Business'}, {'label': 'First', 'value': 'First'}], value={\"path\":\"/fare_class\"}, variant=\"mutuallyExclusive\")\n submit (Button, child=submit_label, variant=\"primary\", action={\"event\":{\"name\":\"bookingSubmit\",\"context\":{\n \"formId\":\"booking\",\n \"origin\":{\"path\":\"/origin\"},\n \"dest\":{\"path\":\"/dest\"},\n \"date\":{\"path\":\"/date\"},\n \"passengers\":{\"path\":\"/passengers\"},\n \"fare_class\":{\"path\":\"/fare_class\"}\n }}})\n submit_label (Text, text=\"Search flights\")\n\nUse these exact ids." + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"LAX\"], \"dest\": [\"JFK\"], \"date\": \"2026-11-03\", \"passengers\": 2, \"fare_class\": [\"Business\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You just received a booking submission. The find_routes() tool returned the following flights:\n\n[\n {\n \"flight_number\": \"UA204\",\n \"airline\": \"UA\",\n \"from\": \"SFO\",\n \"to\": \"SEA\",\n \"depart_local\": \"09:00\",\n \"arrive_local\": \"11:00\",\n \"duration_min\": 120,\n \"status\": \"on_time\",\n \"gate\": \"F11\",\n \"aircraft\": \"Boeing 737\"\n }\n]\n\nForm data (for context): {\n \"formId\": \"booking\",\n \"origin\": \"SFO\",\n \"dest\": \"SEA\",\n \"date\": \"2027-02-17\",\n \"passengers\": 3,\n \"fare_class\": \"Business\"\n}\n\nEmit an A2UI v0.9 results surface using the FlightResultsSpec schema.\n\nA2UI format (CRITICAL): every component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string; props sit at the top level of the same object. Dynamic values are bare literals or `{\"path\": \"/ptr\"}`.\n\nAllowed component names: Column, Row, Card, Text, TextField, Button, Divider, List.\n\nPer-component shapes you'll need:\n Column / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"}\n Text: {\"text\": \"literal\", \"variant\": \"h2\"} (or h1/h3/body/caption)\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"flightId\": \"\"}}}}\n Divider: {}\n\nSurface constraints:\n surface_id MUST be \"results\"\n data_model can be {}\n Exactly ONE component MUST have id \"root\": a Column whose children lists every flight Card id (or just [\"no_flights\"] when empty)\n\nBuild pattern (one per flight):\n card_ (Card, child=col_)\n col_ (Column, children = [title_, route_, time_, btn_])\n title_ (Text, text=\" flight \", variant=\"h3\")\n route_ (Text, text=\" \u2192 \u2022 min \u2022 \", variant=\"body\")\n time_ (Text, text=\"Depart \u2022 Arrive \u2022 Gate \", variant=\"caption\")\n btn_ (Button, child=btn_label_, variant=\"primary\",\n action={\"event\":{\"name\":\"flightSelect\",\"context\":{\"flightId\":\"\"}}})\n btn_label_ (Text, text=\"Select\")\n\nEmpty case: components = [\n {\"id\":\"root\", \"component\":\"Column\", \"children\":[\"no_flights\"]},\n {\"id\":\"no_flights\", \"component\":\"Card\", \"child\":\"empty_col\"},\n {\"id\":\"empty_col\", \"component\":\"Column\", \"children\":[\"empty_msg\",\"modify_btn\"]},\n {\"id\":\"empty_msg\", \"component\":\"Text\", \"text\":\"No flights found\", \"variant\":\"h3\"},\n {\"id\":\"modify_btn\", \"component\":\"Button\", \"child\":\"modify_label\", \"action\":{\"event\":{\"name\":\"modifySearch\",\"context\":{\"formId\":\"booking\"}}}},\n {\"id\":\"modify_label\", \"component\":\"Text\", \"text\":\"Modify search\"}\n]\n\nUse unique ids for every component." + }, + "response": { + "toolCalls": [ + { + "name": "FlightResultsSpec", + "arguments": "{\"surface_id\": \"results\", \"data_model\": {}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card_0\"]}, {\"id\": \"card_0\", \"component\": \"Card\", \"child\": \"col_0\"}, {\"id\": \"col_0\", \"component\": \"Column\", \"children\": [\"title_0\", \"route_0\", \"time_0\", \"btn_0\"]}, {\"id\": \"title_0\", \"component\": \"Text\", \"text\": \"UA flight UA204\", \"variant\": \"h3\"}, {\"id\": \"route_0\", \"component\": \"Text\", \"text\": \"SFO \u2192 SEA \u2022 120 min \u2022 Boeing 737\", \"variant\": \"body\"}, {\"id\": \"time_0\", \"component\": \"Text\", \"text\": \"Depart 09:00 \u2022 Arrive 11:00 \u2022 Gate F11\", \"variant\": \"caption\"}, {\"id\": \"btn_0\", \"component\": \"Button\", \"child\": \"btn_label_0\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"flightSelect\", \"context\": {\"flightId\": \"UA204\"}}}}, {\"id\": \"btn_label_0\", \"component\": \"Text\", \"text\": \"Select\"}]}" + } + ] + } + }, + { + "match": { + "systemMessage": "You are an aviation booking-form designer. Emit an A2UI v0.9 booking form using the structured output schema.\n\nA2UI FORMAT (CRITICAL): each component is FLAT \u2014 `{\"id\": \"...\", \"component\": \"\", }`. The component name is a plain string and the props sit at the top level of the same object. ComponentName must be one of:\n Column, Row, Card, Text, TextField, ChoicePicker, DateTimeInput, CheckBox, Button, Divider, List, Image, Icon, Modal, Slider, Tabs\n\nDynamic values are bare literals (\"LAX\", 3, true) or data bindings `{\"path\": \"/json/pointer\"}`. Never wrap literals.\n\nPer-component shapes:\n Column / Row / List: {\"children\": [\"id1\", \"id2\"]}\n Card: {\"child\": \"\"} \u2190 single child only\n Button: {\"child\": \"\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"\", \"context\": {\"formId\": \"booking\"}}}}\n Text: {\"text\": \"literal string\", \"variant\": \"h2\"} (h1/h2/h3/h4/h5/caption/body)\n TextField: {\"label\": \"Field\", \"value\": {\"path\": \"/p\"}, \"variant\": \"shortText\"} (shortText/longText/number/obscured)\n DateTimeInput: {\"label\": \"Date\", \"value\": {\"path\": \"/p\"}, \"enableDate\": true}\n ChoicePicker: {\"label\": \"Origin\", \"options\": [{\"label\":\"LAX\",\"value\":\"LAX\"}], \"value\": {\"path\":\"/origin\"}, \"variant\": \"mutuallyExclusive\"}\n CheckBox: {\"label\": \"...\", \"value\": {\"path\":\"/p\"}}\n Divider: {}\n\nExactly ONE component MUST have id \"root\" \u2014 it is the root of the tree.\n\nRequired form composition for THIS task:\n surface_id MUST be \"booking\"\n data_model MUST be {\"origin\": [\"SFO\"], \"dest\": [\"SEA\"], \"date\": \"2027-02-17\", \"passengers\": 3, \"fare_class\": [\"Business\"]} \u2190 use these values verbatim as the field defaults\n\n Build this component tree:\n root (Column, children=[\"card\"])\n card (Card, child=card_col)\n card_col (Column, children=[title, origin, dest, date, passengers, fare, submit])\n title (Text, text=\"Book a flight\", variant=\"h2\")\n origin (ChoicePicker, label=\"Origin\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/origin\"}, variant=\"mutuallyExclusive\")\n dest (ChoicePicker, label=\"Destination\", options=[{'label': 'LAX', 'value': 'LAX'}, {'label': 'JFK', 'value': 'JFK'}, {'label': 'SFO', 'value': 'SFO'}, {'label': 'ORD', 'value': 'ORD'}, {'label': 'BOS', 'value': 'BOS'}, {'label': 'ATL', 'value': 'ATL'}, {'label': 'DFW', 'value': 'DFW'}, {'label': 'SEA', 'value': 'SEA'}, {'label': 'MIA', 'value': 'MIA'}, {'label': 'DEN', 'value': 'DEN'}], value={\"path\":\"/dest\"}, variant=\"mutuallyExclusive\")\n date (DateTimeInput, label=\"Departure date (YYYY-MM-DD)\", value={\"path\":\"/date\"}, enableDate=true)\n passengers (TextField, label=\"Passengers\", value={\"path\":\"/passengers\"}, variant=\"number\")\n fare (ChoicePicker, label=\"Fare class\", options=[{'label': 'Economy', 'value': 'Economy'}, {'label': 'Premium', 'value': 'Premium'}, {'label': 'Business', 'value': 'Business'}, {'label': 'First', 'value': 'First'}], value={\"path\":\"/fare_class\"}, variant=\"mutuallyExclusive\")\n submit (Button, child=submit_label, variant=\"primary\", action={\"event\":{\"name\":\"bookingSubmit\",\"context\":{\n \"formId\":\"booking\",\n \"origin\":{\"path\":\"/origin\"},\n \"dest\":{\"path\":\"/dest\"},\n \"date\":{\"path\":\"/date\"},\n \"passengers\":{\"path\":\"/passengers\"},\n \"fare_class\":{\"path\":\"/fare_class\"}\n }}})\n submit_label (Text, text=\"Search flights\")\n\nUse these exact ids." + }, + "response": { + "toolCalls": [ + { + "name": "BookingFormSpec", + "arguments": "{\"surface_id\": \"booking\", \"data_model\": {\"origin\": [\"SFO\"], \"dest\": [\"SEA\"], \"date\": \"2027-02-17\", \"passengers\": 3, \"fare_class\": [\"Business\"]}, \"components\": [{\"id\": \"root\", \"component\": \"Column\", \"children\": [\"card\"]}, {\"id\": \"card\", \"component\": \"Card\", \"child\": \"card_col\"}, {\"id\": \"card_col\", \"component\": \"Column\", \"children\": [\"title\", \"origin\", \"dest\", \"date\", \"passengers\", \"fare\", \"submit\"]}, {\"id\": \"title\", \"component\": \"Text\", \"text\": \"Book a flight\", \"variant\": \"h2\"}, {\"id\": \"origin\", \"component\": \"ChoicePicker\", \"label\": \"Origin\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/origin\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"dest\", \"component\": \"ChoicePicker\", \"label\": \"Destination\", \"options\": [{\"label\": \"LAX\", \"value\": \"LAX\"}, {\"label\": \"JFK\", \"value\": \"JFK\"}, {\"label\": \"SFO\", \"value\": \"SFO\"}, {\"label\": \"ORD\", \"value\": \"ORD\"}, {\"label\": \"BOS\", \"value\": \"BOS\"}, {\"label\": \"ATL\", \"value\": \"ATL\"}, {\"label\": \"DFW\", \"value\": \"DFW\"}, {\"label\": \"SEA\", \"value\": \"SEA\"}, {\"label\": \"MIA\", \"value\": \"MIA\"}, {\"label\": \"DEN\", \"value\": \"DEN\"}], \"value\": {\"path\": \"/dest\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"fare\", \"component\": \"ChoicePicker\", \"label\": \"Fare class\", \"options\": [{\"label\": \"Economy\", \"value\": \"Economy\"}, {\"label\": \"Premium\", \"value\": \"Premium\"}, {\"label\": \"Business\", \"value\": \"Business\"}, {\"label\": \"First\", \"value\": \"First\"}], \"value\": {\"path\": \"/fare_class\"}, \"variant\": \"mutuallyExclusive\"}, {\"id\": \"date\", \"component\": \"DateTimeInput\", \"label\": \"Departure date (YYYY-MM-DD)\", \"value\": {\"path\": \"/date\"}, \"enableDate\": true}, {\"id\": \"passengers\", \"component\": \"TextField\", \"label\": \"Passengers\", \"value\": {\"path\": \"/passengers\"}, \"variant\": \"number\"}, {\"id\": \"submit\", \"component\": \"Button\", \"child\": \"submit_label\", \"variant\": \"primary\", \"action\": {\"event\": {\"name\": \"bookingSubmit\", \"context\": {\"formId\": \"booking\", \"origin\": {\"path\": \"/origin\"}, \"dest\": {\"path\": \"/dest\"}, \"date\": {\"path\": \"/date\"}, \"passengers\": {\"path\": \"/passengers\"}, \"fare_class\": {\"path\": \"/fare_class\"}}}}}, {\"id\": \"submit_label\", \"component\": \"Text\", \"text\": \"Search flights\"}]}" + } + ] + } } ] } diff --git a/cockpit/chat/a2ui/react/README.md b/cockpit/chat/a2ui/react/README.md index f1754ba74..bd3c06aec 100644 --- a/cockpit/chat/a2ui/react/README.md +++ b/cockpit/chat/a2ui/react/README.md @@ -19,4 +19,4 @@ WEBSITE_E2E_REACT_TOPIC=chat-a2ui npx nx e2e website --grep 'native Chat A2UI' - The app e2e target builds the actual private packages and runs its installed terminal admission suite first. Website e2e retains its full registered React build prerequisites even when the topic selector narrows the running servers. Local serving uses `node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent` on port 4646 after preparation. -Local verification runs the actual compiled canonical graph with deterministic providers, the real SDK and saved checkpoints. These fixture results establish local interoperability, not live-provider timing or durable deployment behavior. Production sources and public Code exclude fixture controls, testing helpers and the private probe. Runtime settings choose one configured shared backend; this example adds no service or public API. +Local verification runs the canonical graph in a real `langgraph dev`, with aimock replaying the model's recorded outputs (`cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json`), the real SDK and saved checkpoints. These results establish local interoperability, not live-provider timing or durable deployment behavior. Production sources and public Code exclude the local proxy controls, testing helpers and the private probe. Runtime settings choose one configured shared backend; this example adds no service or public API. diff --git a/cockpit/chat/a2ui/react/e2e/a2ui.spec.ts b/cockpit/chat/a2ui/react/e2e/a2ui.spec.ts index 8cb00b41f..ea0a75e27 100644 --- a/cockpit/chat/a2ui/react/e2e/a2ui.spec.ts +++ b/cockpit/chat/a2ui/react/e2e/a2ui.spec.ts @@ -4,11 +4,20 @@ import { writeFile } from 'node:fs/promises'; const base = '/chat/a2ui/react/'; const current = (page: Page) => page.getByRole('region', { name: 'Current flight preview', exact: true }); const earlier = (page: Page) => page.getByRole('region', { name: 'Earlier flight preview', exact: true }); -const lifetime = async (request: APIRequestContext) => (await request.get('/__lifetime')).json(); +// Backend: aimock replays cockpit/chat/a2ui/angular/e2e/fixtures (the A2UI +// model's outputs for these flows, keyed on each node's full system prompt) +// into a real `langgraph dev`, whose real tools find routes and look flights +// up. The recording proxy (scripts/react-cockpit/langgraph-proxy.mjs) records +// /api requests and holds the next run stream. +/** Every /api request so far, as the proxy recorded it. */ +const lifetime = async (request: APIRequestContext) => ({ requests: await (await request.get('/__requests')).json() }); +/** Whether the proxy holds a run stream right now. */ +const held = async (request: APIRequestContext) => (await (await request.get('/__lifetime')).json()).activeStream; +const hold = (request: APIRequestContext) => request.post('/__hold/stream', { data: { immediate: true } }); const streamRequests = (receipt: any) => receipt.requests.filter((r: any) => r.method === 'POST' && r.path.endsWith('/runs/stream')); async function save(request: APIRequestContext, info: TestInfo, extra: unknown = {}) { const path = info.outputPath('actual-graph-and-transport.json'); - await writeFile(path, JSON.stringify({ receipt: await lifetime(request), extra }, null, 2)); + await writeFile(path, JSON.stringify({ receipt: await lifetime(request), captures: await (await request.get('/__captures')).json(), extra }, null, 2)); await info.attach('actual-graph-and-transport.json', { path, contentType: 'application/json' }); } async function form(page: Page) { @@ -78,9 +87,10 @@ test('installed native application saves request, edited Search, Select and serv expect(search.action).toMatchObject({ name: 'bookingSubmit', surfaceId: 'booking', context: { formId: 'booking', origin: ['SFO'], dest: ['SEA'], date: '2027-02-17', passengers: 3, fare_class: ['Business'] } }); expect(JSON.parse(runs[2].body.input.messages[0].content).action).toMatchObject({ name: 'flightSelect', context: { flightId: 'UA204' } }); expect(JSON.parse(runs[3].body.input.messages[0].content).action.name).toBe('modifySearch'); - expect(receipt.runReads).toHaveLength(4); - expect(receipt.runReads.every((r: any) => r.run.status === 'success' && r.threadId === threadId)).toBe(true); - expect(new Set(receipt.runReads.map((r: any) => r.run.run_id)).size).toBe(4); + const runReads = receipt.requests.filter((r: any) => r.method === 'GET' && /\/runs\/[^/]+$/.test(r.path)); + expect(runReads).toHaveLength(4); + expect(runReads.every((r: any) => r.path.startsWith(`/api/threads/${threadId}/runs/`))).toBe(true); + expect(new Set(runReads.map((r: any) => r.path)).size).toBe(4); await expect.poll(() => exactReads.length).toBe(4); for (const [index, state] of exactReads.entries()) { expect(state.checkpoint).toMatchObject({ thread_id: threadId, checkpoint_ns: '' }); @@ -89,10 +99,10 @@ test('installed native application saves request, edited Search, Select and serv expect(state.values.messages).toHaveLength((index + 1) * 2); expect(state.values.messages.at(-2).id).toBe(runs[index].body.input.messages[0].id); } - expect(receipt.proofs.every((p: any) => p.actualCompiledGraph && p.networkConnectAttempts === 0 && p.sourceSha256 === '9a54abf5d03dfd7d285265fc4f3c37ca3b5a830dee319cf52093584907ea8462' && p.lockSha256 === '56247b7f8c8a4cd27f43238a3c99f37f251c8e3a850a94d1761d7ac472abf303')).toBe(true); - expect(JSON.stringify(receipt.proofs)).toContain('_find_routes_impl'); - expect(JSON.stringify(receipt.events)).toContain('generate_title'); - expect(JSON.stringify(receipt.events)).toContain('messages'); + // What the real server streamed: title and message callbacks included. + const streamed = JSON.stringify(await (await request.get('/__captures')).json()); + expect(streamed).toContain('generate_title'); + expect(streamed).toContain('event: messages'); await save(request, info, { exactReads }); }); @@ -116,9 +126,9 @@ test('invalid lexical drafts and direct user activation send zero actions; rapid await expect(current(page).getByText('Enter a real date as YYYY-MM-DD.')).toBeVisible(); expect(streamRequests(await lifetime(request))).toHaveLength(1); await edit(page); - await request.post('/__hold/stream'); + await hold(request); await search.evaluate((button: HTMLButtonElement) => { button.click(); button.click(); }); - await expect.poll(async () => (await lifetime(request)).heldCompleted).toBe(1); + await expect.poll(async () => held(request)).toBe(true); expect(streamRequests(await lifetime(request))).toHaveLength(2); await request.post('/__release'); await expect(current(page).getByRole('button', { name: 'Select', exact: true })).toBeVisible(); @@ -173,16 +183,16 @@ test('untrusted saved boundaries fail closed, retain prior cards, and late held for (const command of ['Stop', 'New conversation']) { await page.getByRole('button', { name: 'New conversation', exact: true }).click(); await form(page); await edit(page); - await request.post('/__hold/stream'); + await hold(request); await current(page).getByRole('button', { name: 'Search flights', exact: true }).click(); - await expect.poll(async () => (await lifetime(request)).heldCompleted).toBe(1); + await expect.poll(async () => held(request)).toBe(true); await page.getByRole('button', { name: command, exact: true }).click(); - await expect.poll(async () => (await lifetime(request)).heldStreams).toBe(0); + await expect.poll(async () => held(request)).toBe(false); await request.post('/__release'); await expect(current(page)).toHaveCount(0); await expect(page.getByRole('button', { name: 'Select', exact: true })).toHaveCount(0); } - await save(request, info, { failures, lifetimeMeaning: 'Actual graph completed; HTTP delivery was held. No server-side graph-cancellation claim.' }); + await save(request, info, { failures, lifetimeMeaning: 'The proxy held the real run stream after its first event; Stop or New closed it.' }); }); test('390px keyboard workflow remains readable and pagehide revokes held HTTP delivery and views', async ({ page, request }, info) => { @@ -207,16 +217,65 @@ test('390px keyboard workflow remains readable and pagehide revokes held HTTP de await expect(current(page).getByRole('button', { name: 'Modify search', exact: true })).toBeVisible(); await noOverflow(page); await page.screenshot({ path: info.outputPath('mobile-confirmation.png'), fullPage: true }); - await request.post('/__hold/stream'); + await hold(request); await current(page).getByRole('button', { name: 'Modify search', exact: true }).click(); - await expect.poll(async () => (await lifetime(request)).heldCompleted).toBe(1); + await expect.poll(async () => held(request)).toBe(true); await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); await expect(page.locator('#root')).toBeEmpty(); - await expect.poll(async () => (await lifetime(request)).heldStreams).toBe(0); + await expect.poll(async () => held(request)).toBe(false); await request.post('/__release'); await expect(page.locator('#root')).toBeEmpty(); - const receipt = await lifetime(request); - expect(receipt.streamCloses.some((closed: any) => closed.held && closed.destroyed)).toBe(true); - expect(streamRequests(receipt)).toHaveLength(4); - await save(request, info, { scope: 'Chromium desktop/mobile viewport, labels, Tab/Enter, synthetic pagehide; graph completed before held HTTP close.' }); + expect(streamRequests(await lifetime(request))).toHaveLength(4); + await save(request, info, { scope: 'Chromium desktop/mobile viewport, labels, Tab/Enter, synthetic pagehide closing a held run stream.' }); +}); + +// The real graph's own model-failure paths, reached through model output. +// Injected data: aimock fixtures tagged `[scenario:]` in +// cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json answer the title prompt +// with a provider error, or the booking-form designer with a form whose fare +// option is `Economomy` (outside the schema's literals). +async function graphTurn(request: APIRequestContext, content: string) { + const thread = await (await request.post('/api/threads', { data: {} })).json(); + const stream = await request.post(`/api/threads/${thread.thread_id}/runs/stream`, { + data: { assistant_id: 'c-a2ui', input: { messages: [{ type: 'human', content }] }, stream_mode: ['values', 'messages-tuple'] }, + }); + expect(stream.status()).toBe(200); + const sse = await stream.text(); + const runId = /"run_id":\s*"([^"]+)"/.exec(sse)?.[1]; + expect(runId).toBeTruthy(); + const run = await (await request.get(`/api/threads/${thread.thread_id}/runs/${runId}`)).json(); + const state = await (await request.get(`/api/threads/${thread.thread_id}/state`)).json(); + const captures = await (await request.get('/__captures')).json(); + return { sse, run, state, capture: captures.at(-1) }; +} +const formMessage = (state: any) => state.values.messages.at(-1).content as string; + +test('a failed title model call is swallowed: the form still saves and the thread keeps no title', async ({ request }) => { + const { sse, run, state, capture } = await graphTurn(request, 'I want to fly LAX to JFK [scenario:title-down]'); + expect(run.status).toBe('success'); + expect(state.next).toEqual([]); + expect(state.values.messages).toHaveLength(2); + expect(formMessage(state)).toMatch(/^---a2ui_JSON---\n/); + expect(formMessage(state)).not.toMatch(/Book a flight \(fallback\)/); + expect(sse).not.toContain('generate_title'); + expect(capture.titleAfter ?? null).toBeNull(); +}); + +test('one invalid structured form is retried and the corrected form saves', async ({ request }) => { + const { run, state } = await graphTurn(request, 'I want to fly LAX to JFK [scenario:retry]'); + expect(run.status).toBe('success'); + expect(state.values.messages).toHaveLength(2); + // Without the retry the invalid first answer would have ended in the fallback form. + expect(formMessage(state)).toMatch(/^---a2ui_JSON---\n/); + expect(formMessage(state)).not.toMatch(/Book a flight \(fallback\)/); + expect(formMessage(state)).not.toContain('Economomy'); +}); + +test('three invalid structured forms end in the graph\'s fallback form, seeded from the request', async ({ request }) => { + const { run, state } = await graphTurn(request, 'I want to fly LAX to JFK [scenario:invalid]'); + expect(run.status).toBe('success'); + expect(state.values.messages).toHaveLength(2); + expect(formMessage(state)).toMatch(/Book a flight \(fallback\)/); + const data = formMessage(state).split('\n').slice(1).filter(Boolean).map((line) => JSON.parse(line)).find((e) => e.updateDataModel)?.updateDataModel.value; + expect(data).toMatchObject({ origin: ['LAX'], dest: ['JFK'], passengers: 1, fare_class: ['Economy'] }); }); diff --git a/cockpit/chat/a2ui/react/e2e/global-setup-impl.ts b/cockpit/chat/a2ui/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..8405016e7 --- /dev/null +++ b/cockpit/chat/a2ui/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into a real `langgraph dev`; +// the static server (webServer) proxies /api to it. +export default reactAimockGlobalSetup({ example: 'chat/a2ui', port: 4646 }); diff --git a/cockpit/chat/a2ui/react/e2e/playwright.config.ts b/cockpit/chat/a2ui/react/e2e/playwright.config.ts index 211d97b03..b9cce7b30 100644 --- a/cockpit/chat/a2ui/react/e2e/playwright.config.ts +++ b/cockpit/chat/a2ui/react/e2e/playwright.config.ts @@ -8,6 +8,9 @@ export default defineConfig({ outputDir: '../../../../../output/playwright/chat-a2ui-react', use: { baseURL: 'http://127.0.0.1:4646', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + real `langgraph dev`, shared with cockpit/chat/a2ui/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs chat-a2ui --no-parent', cwd: '../../../../..', diff --git a/cockpit/chat/a2ui/react/fixtures/terminal-admission.spec.mjs b/cockpit/chat/a2ui/react/fixtures/terminal-admission.spec.mjs index baa685767..56379ff6b 100644 --- a/cockpit/chat/a2ui/react/fixtures/terminal-admission.spec.mjs +++ b/cockpit/chat/a2ui/react/fixtures/terminal-admission.spec.mjs @@ -6,7 +6,7 @@ test( 'installed actual application helpers admit four canonical native graph turns', { timeout: 180000 }, async () => { - // runNativeProbe explicitly prepares the existing frozen Python fixture configuration. + // The installed consumer runs against aimock + a real `langgraph dev`, behind the recording proxy. const report = await runNativeProbe({ mode: 'application-admission' }); assert.equal(report.mode, 'application-admission'); assert.equal(report.compiler.claim, 'actual application admission helpers'); @@ -38,6 +38,10 @@ test( assert.ok(report.receipt.cleanup.every((row) => row.closed)); assert.equal(report.closure.processAbsent, true); assert.equal(report.closure.groupAbsent, true); - assert.equal(report.receipt.fixture.worker.pythonAbsent, true); + // The real backend (aimock + `langgraph dev`) was the one the consumer + // drove, and its process tree was gone when the consumer closed it. + assert.equal(report.receipt.backend.kind, 'aimock-langgraph'); + assert.equal(report.receipt.backend.python, 'cockpit/chat/a2ui/python'); + assert.ok(report.receipt.cleanup.some((row) => row.name === 'backend' && row.closed)); } ); diff --git a/cockpit/chat/a2ui/react/project.json b/cockpit/chat/a2ui/react/project.json index b9a5c0629..3e13b4f40 100644 --- a/cockpit/chat/a2ui/react/project.json +++ b/cockpit/chat/a2ui/react/project.json @@ -18,6 +18,7 @@ "e2e": { "executor": "@nx/playwright:playwright", "cache": false, + "inputs": ["default", "reactAimockBackend"], "dependsOn": ["build", "fixture-test"], "options": { "config": "cockpit/chat/a2ui/react/e2e/playwright.config.ts", diff --git a/cockpit/chat/a2ui/react/src/connection.integration.spec.ts b/cockpit/chat/a2ui/react/src/connection.integration.spec.ts index 222018a83..254d8c842 100644 --- a/cockpit/chat/a2ui/react/src/connection.integration.spec.ts +++ b/cockpit/chat/a2ui/react/src/connection.integration.spec.ts @@ -1,63 +1,53 @@ -import { createServer } from 'node:http'; +import { createServer, type Server } from 'node:http'; import { once } from 'node:events'; -import { mkdtempSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; +import { resolve } from 'node:path'; import type { AddressInfo } from 'node:net'; -import { expect, it, onTestFinished } from 'vitest'; +import { afterAll, beforeAll, expect, it, onTestFinished } from 'vitest'; import { createConnectedApplication } from './connection'; import type { A2uiApplication } from './application'; // eslint-disable-next-line @nx/enforce-module-boundaries -- Borrow the moved private presentation type. import type { SurfaceView } from '../../../../../libs/cockpit-a2ui-react/src/surface'; +/* eslint-disable @nx/enforce-module-boundaries -- Local test backend tooling is never bundled. */ +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createAimockBackend } from '../../../../../scripts/react-cockpit/aimock-backend.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { createLanggraphProxy } from '../../../../../scripts/react-cockpit/langgraph-proxy.mjs'; +// @ts-expect-error Local test-only JavaScript has no published declaration. +import { reactCockpitConfiguration } from '../../../../../scripts/react-cockpit/configuration.mjs'; +/* eslint-enable @nx/enforce-module-boundaries */ + +// The real graph runs under `langgraph dev` on aimock fixtures shared with the +// Angular example (cockpit/chat/a2ui/angular/e2e/fixtures); the in-process +// proxy records each /api request (scripts/react-cockpit/langgraph-proxy.mjs). +const { backend: config } = reactCockpitConfiguration('chat-a2ui'); +const backend = createAimockBackend({ root: resolve(__dirname, '../../../../..'), backend: config }); +beforeAll(() => backend.ensure(), 180000); +afterAll(() => backend.close()); +type Handler = (request: unknown, response: unknown, pathname: string) => Promise; async function fixture() { - const location = new URL( - '../../../../../scripts/react-cockpit/chat-a2ui-fixture.mjs', - import.meta.url - ).href; - const { createChatA2uiFixture, fixtureConfiguration } = await import( - /* @vite-ignore */ location - ); - // Existing fixture prepares one frozen isolated Python environment; no model credentials. - const evidence = mkdtempSync( - join(tmpdir(), 'threadplane-chat-a2ui-task3-sdk-') - ); - const handle = createChatA2uiFixture({ - configuration: fixtureConfiguration(), + const proxy: Handler = createLanggraphProxy({ + upstream: `http://127.0.0.1:${config.langgraphPort}`, + ready: () => backend.ensure(), }); - const server = createServer(async (req, res) => { - if ( - !(await handle( - req, - res, - new URL(req.url ?? '', 'http://localhost').pathname - )) - ) { + const server: Server = createServer(async (req, res) => { + if (!(await proxy(req, res, new URL(req.url ?? '', 'http://localhost').pathname))) { res.writeHead(404); res.end(); } }); server.listen(0, '127.0.0.1'); await once(server, 'listening'); - const app = createConnectedApplication({ - apiUrl: `http://127.0.0.1:${(server.address() as AddressInfo).port}/api`, - headers: {}, - }); + const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + const app = createConnectedApplication({ apiUrl: base + '/api', headers: {} }); onTestFinished(async () => { await app.dispose(); - await handle.close(); server.closeAllConnections(); await new Promise((r) => server.close(() => r())); - const receipt = handle.receipt(); - writeFileSync( - join(evidence, 'receipt.json'), - JSON.stringify(receipt, null, 2) + '\n', - { mode: 0o600 } - ); - console.log('Task 3 actual SDK evidence: ' + evidence); - expect(receipt.worker.reaped).toBe(true); - expect(receipt.worker.pythonAbsent).toBe(true); }); + const handle = { + receipt: async () => ({ requests: await (await fetch(base + '/__requests')).json() }), + }; return { app, handle }; } async function action(app: A2uiApplication, surface: SurfaceView, id: string) { @@ -75,7 +65,7 @@ async function action(app: A2uiApplication, surface: SurfaceView, id: string) { } it('actual default SDK session admits the compiled graph four-turn edited Search, Select and Modify flow', async () => { const f = await fixture(); - expect(f.handle.receipt().requests).toEqual([]); + expect((await f.handle.receipt()).requests).toEqual([]); const publications: ReturnType[] = []; f.app.subscribe(() => publications.push(f.app.getSnapshot())); expect(await f.app.submit('I want to fly LAX to JFK')).toBe(true); @@ -116,7 +106,7 @@ it('actual default SDK session admits the compiled graph four-turn edited Search expect(state.messages.length).toBe(state.surfaces.length * 2); if (state.busy) expect(state.activeSurface).toBeNull(); } - const receipt = f.handle.receipt(); + const receipt = await f.handle.receipt(); expect( receipt.requests.filter((r: { path: string }) => r.path === '/api/threads') ).toHaveLength(1); @@ -160,18 +150,10 @@ it('actual default SDK session admits the compiled graph four-turn edited Search r.body.checkpoint.thread_id === f.app.getSnapshot().threadId ) ).toBe(true); + // Each turn's run status was read once from the real server. expect( - receipt.runReads.map((r: { run: { status: string } }) => r.run.status) - ).toEqual(['success', 'success', 'success', 'success']); - expect( - receipt.proofs.every( - (p: { actualCompiledGraph: boolean; networkConnectAttempts: number }) => - p.actualCompiledGraph && p.networkConnectAttempts === 0 - ) - ).toBe(true); - expect( - receipt.proofs.some((p: { tools?: { name: string }[] }) => - p.tools?.some((t) => t.name === '_lookup_flight_impl') + receipt.requests.filter( + (r: { method: string; path: string }) => r.method === 'GET' && /\/runs\/[^/]+$/.test(r.path) ) - ).toBe(true); + ).toHaveLength(4); }, 60000); diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index b01a10179..8197df850 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -184,7 +184,7 @@ example needs that the pilot proxy doesn't yet provide. | 5 (done) | deep-agents/{planning, filesystem, memory, skills, subagents} | `/__stream/cut` (memory) | scenarios are tagged prompts on the shared `da-*.json` fixtures. The planning and filesystem integration specs (vitest) run on `createAimockBackend` plus an in-process proxy. The React memory fixture also backed the Angular memory e2e: that now uses `createGlobalSetup` with `langgraph dev` on 6313 and the recording proxy on 5313, where the Angular dev server's `/api` proxy points, which retires its owned-process lifecycle helpers and the `compiledMemoryBackend` guard in `e2e-wiring.spec.ts`. The memory spec reads stream bodies from `/__captures[].sse` (Chromium doesn't keep a long-lived event-stream body) | | 6a (done) | ag-ui/{streaming, interrupts, tool-views, json-render, subagents} | the AG-UI flavor: `aimock-ag-ui` backends run the example's uvicorn `src/server.py` (`agentPath` picks the route, e.g. interrupts' `/agent/native`), `createAgUiBackendGlobalSetup` in the harness, and `scripts/react-cockpit/ag-ui-proxy.mjs` (`/__requests`, `/__captures`, `/__lifetime`, `/__hold/stream { afterType, afterPattern }`, `/__release`, `/__fail-stream`, `/__stream/edit` with drop, patch, omit, setPath, replace, insertBefore/After, end, `outcome` and `snapshot` modes, `dataPattern`, `once`, `persist`) | streaming's fixture was pure authored JavaScript; the others ran the real graph through a wire script and rewrote its events, so their scripted models (from the Python tests) become shared fixtures and their rewrites become proxy edits (injected data). Real behavior replaces several: a model failure drops the stream (no RUN_ERROR), native interrupts carry LangGraph's own interrupt id, unknown tools and argument-less calls fail in the real ToolNode, and a Booking specialist's provider error is a real child failure. The client returns the server's STATE_SNAPSHOT as the next run's `state`; specs assert that instead of `{}` | | 6b (done) | ag-ui/client-tools | `aimockPort` (app port + 2000) on AG-UI backends and `/__model-requests` (aimock's `GET /__aimock/journal`); `/__requests` entries carry live `events`, `held` and `closed`; holds take `beforeType`, `persist` and `fromRequest` | the React fixture wrapped the shared react-parity server's `cockpit` scenario; that scenario becomes aimock fixtures and is removed from the parity worker, which stays for `scripts/react-parity` verification. The `prepare-fixture` target, its scripts, specs and CI steps go: `uv run` syncs the environment. The causal checks read the model's received messages from the journal. Graph, checkpointer and model identity and socket-attempt checks were the parity worker's own proofs; the process boundary replaces them, as for chat/messages | -| 6c | ag-ui/a2ui, chat/a2ui | the same parity-server pattern, plus each example's packed-application provenance proof (`ag-ui-a2ui-native-probe.mjs`, `chat-a2ui-native-probe.mjs`), their CI proof steps and retained application artifacts | each probe needs the debug probe's treatment (installed package against `createAimockBackend` + the proxy), so they migrate as their own batch | +| 6c (done) | ag-ui/a2ui, chat/a2ui | ag-ui proxy `/__hold/off`; `createAimockBackend.close()` stops uv's whole process tree (`langgraph dev` starts workers of its own) | **ag-ui/a2ui:** the search, select and modify turns replay the react-parity A2UI model's outputs for the flow (captured from its traces), keyed on each node's system prompt; the opening form is the recorded Angular fixture. The installed-application proof (`ag-ui-a2ui-native-probe.mjs`) and the parity server stay: they verify `scripts/react-parity`, not the cockpit e2e. **chat/a2ui:** the old wire's model calls become fixtures keyed on each node's full system prompt (captured from the wire). The packed-consumer probe (`chat-a2ui-native-probe.mjs`, the `fixture-test` target and the Library lane's "Verify canonical A2UI native graph" step) runs the installed `@threadplane/langgraph` and the compiled application admission helpers against `createAimockBackend` + an in-process proxy; its harness tests move to `chat-a2ui-native-probe.spec.mjs`. The fixture spec's graph cases are reached through model output (injected data: `[scenario:title-down]` answers the title prompt with a provider error, `[scenario:retry]` and `[scenario:invalid]` answer the form designer with a fare option outside the schema, once or always) and run in the React e2e against the real graph: a swallowed title failure, one structured-output retry, and the fallback form after three invalid answers. The host and readiness specs tested the deleted fixture's worker lifecycle | | 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | | n/a | render/* (6) | none | `adapter: 'none'`, no model backend | diff --git a/scripts/ci-scope.mjs b/scripts/ci-scope.mjs index d6d13c198..bf39db00c 100644 --- a/scripts/ci-scope.mjs +++ b/scripts/ci-scope.mjs @@ -127,13 +127,15 @@ const LINT_SCOPE_KEYS = [ * this regex only decides which CI scope a change to them lights up. */ const COCKPIT_ROOTLESS_SPEC = /^cockpit\/[^/]+\/[^/]+\.spec\.ts$/; -// The Library lane's installed A2UI regression consumes only these scripts and -// the canonical Python source/lock/tests, independently of their Nx owner tags. +// The Library lane's installed A2UI regression consumes only these scripts, the +// model fixtures aimock replays and the canonical Python source/lock/tests, +// independently of their Nx owner tags. const A2UI_NATIVE_FIXTURE_FILES = new Set([ - 'scripts/react-cockpit/chat-a2ui-wire.py', - 'scripts/react-cockpit/chat-a2ui-fixture.mjs', - 'scripts/react-cockpit/chat-a2ui-fixture.spec.mjs', 'scripts/react-cockpit/chat-a2ui-native-probe.mjs', + 'scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs', + 'scripts/react-cockpit/aimock-backend.mjs', + 'scripts/react-cockpit/langgraph-proxy.mjs', + 'cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json', 'cockpit/chat/a2ui/python/uv.lock', 'cockpit/chat/a2ui/python/pyproject.toml', 'cockpit/ag-ui/a2ui/python/src/graph.py', diff --git a/scripts/ci-scope.spec.mjs b/scripts/ci-scope.spec.mjs index 6f3971741..393dfb73a 100644 --- a/scripts/ci-scope.spec.mjs +++ b/scripts/ci-scope.spec.mjs @@ -197,7 +197,7 @@ it('pins CI runners to the installed browser recipe and canonical Python baselin assert.match(setup ?? '', /python-version: '3\.12'/); const provider = await readFile('cockpit/chat/a2ui/python/pyproject.toml', 'utf8'); assert.match(provider, /requires-python = ">=3\.12"/); - const proof = library.split(' - name: Verify canonical A2UI native fixtures')[1]?.split('\n - name:')[0]; + const proof = library.split(' - name: Verify canonical A2UI native graph')[1]?.split('\n - name:')[0]; assert.match(proof ?? '', /python find --no-project --no-config --no-python-downloads 3\.12/); assert.match(proof ?? '', /env -i HOME=/); assert.doesNotMatch(proof ?? '', /UV_PYTHON_INSTALL_DIR=/); @@ -377,10 +377,11 @@ describe('native canonical provider inputs', () => { describe('canonical A2UI native regression CI ownership', () => { const inputs = [ - 'scripts/react-cockpit/chat-a2ui-wire.py', - 'scripts/react-cockpit/chat-a2ui-fixture.mjs', - 'scripts/react-cockpit/chat-a2ui-fixture.spec.mjs', 'scripts/react-cockpit/chat-a2ui-native-probe.mjs', + 'scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs', + 'scripts/react-cockpit/aimock-backend.mjs', + 'scripts/react-cockpit/langgraph-proxy.mjs', + 'cockpit/chat/a2ui/angular/e2e/fixtures/c-a2ui.json', 'cockpit/chat/a2ui/python/src/graph.py', 'cockpit/chat/a2ui/python/src/nested/helper.py', 'cockpit/chat/a2ui/python/tests/test_booking_form.py', @@ -411,12 +412,12 @@ describe('canonical A2UI native regression CI ownership', () => { }); } for (const file of [ - 'scripts/react-cockpit/chat-a2ui-wire.py.bak', - 'scripts/react-cockpit/chat-a2ui-fixture-other.mjs', - 'scripts/react-cockpit/chat-a2ui-fixture.spec.mjs/extra', + 'scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs/extra', 'scripts/react-cockpit/chat-a2ui-native-probe.mjs.bak', - 'scripts/react-cockpit/chat-debug-fixture.mjs', - 'other/scripts/react-cockpit/chat-a2ui-wire.py', + 'scripts/react-cockpit/aimock-backend.mjs.bak', + 'scripts/react-cockpit/chat-debug-native-session-probe.mjs', + 'cockpit/chat/a2ui/angular/e2e/fixtures/other.json', + 'other/scripts/react-cockpit/chat-a2ui-native-probe.mjs', 'cockpit/chat/a2ui/python-other/src/graph.py', 'cockpit/chat/a2ui/python/src-other/graph.py', 'cockpit/chat/a2ui/python/uv.lock.bak', @@ -448,7 +449,7 @@ describe('canonical A2UI native regression CI ownership', () => { assert.match(library, /LIBS: chat,langgraph,ag-ui,render,a2ui,telemetry,middleware/); const builds = library.indexOf('npx nx run-many -t build --projects=$LIBS --configuration=production'); assert.ok(builds >= 0, 'all-library production build prerequisite is present'); - const marker = ' - name: Verify canonical A2UI native fixtures'; + const marker = ' - name: Verify canonical A2UI native graph'; const position = library.indexOf(marker); assert.ok(position > setup && position > foundations && position > builds, 'A2UI regression follows Python and all required builds'); const proof = library.slice(position).split('\n - name:')[0]; @@ -461,19 +462,21 @@ describe('canonical A2UI native regression CI ownership', () => { assert.equal((proof.match(/env -i /g) ?? []).length, 3, 'preparation, unit tests and native fixtures each close inherited environment'); const prepare = proof.indexOf('"$uv_path" run --frozen --python "$python_path" --no-env-file --no-config --no-python-downloads'); const unit = proof.indexOf('"$A2UI_FIXTURE_ENV/bin/python" -E -B -m unittest discover -s cockpit/chat/a2ui/python/tests -p test_booking_form.py'); - const execute = proof.indexOf('"$node_path" --test --test-concurrency=1 scripts/react-cockpit/chat-a2ui-fixture.spec.mjs'); + const execute = proof.indexOf('"$node_path" --test --test-concurrency=1 --test-force-exit scripts/react-cockpit/chat-a2ui-configuration.spec.mjs scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs'); assert.ok(prepare >= 0 && unit > prepare && execute > unit, 'frozen preparation precedes exact unit discovery and then the maintained native regression'); const unitBlock = proof.slice(proof.lastIndexOf('env -i ', unit), unit); assert.match(unitBlock, /^env -i HOME="\$fixture_home" PATH="\$fixture_path" LANG=C.UTF-8 \\\n/); assert.match(unitBlock, /PYTHONDONTWRITEBYTECODE=1 LANGSMITH_TRACING=false LANGCHAIN_TRACING_V2=false \\\n/); assert.doesNotMatch(unitBlock, /\b(?:OPENAI|ANTHROPIC|LANGSMITH_API|LANGCHAIN_API)[A-Z_]*=/); assert.match(proof, /--project cockpit\/chat\/a2ui\/python python -E -B -c 'import langgraph'/); + // The native probe runs aimock + `langgraph dev` from the frozen cache and + // interpreter the preparation step used, with no provider credentials. for (const configuration of [ - 'A2UI_FIXTURE_UV="$uv_path"', - 'A2UI_FIXTURE_PYTHON="$python_path"', - 'A2UI_FIXTURE_ENV="$A2UI_FIXTURE_ENV"', - 'A2UI_FIXTURE_CACHE="$A2UI_FIXTURE_CACHE"', + 'UV_CACHE_DIR="$A2UI_FIXTURE_CACHE"', + 'UV_PYTHON="$python_path"', ]) assert.ok(proof.includes(configuration), configuration); + const executeBlock = proof.slice(proof.lastIndexOf('env -i ', execute), execute); + assert.doesNotMatch(executeBlock, /\b(?:OPENAI|ANTHROPIC|LANGSMITH_API|LANGCHAIN_API)[A-Z_]*=/); }); }); diff --git a/scripts/fixtures/library-validation-steps.json b/scripts/fixtures/library-validation-steps.json index cd143a0f3..653c05015 100644 --- a/scripts/fixtures/library-validation-steps.json +++ b/scripts/fixtures/library-validation-steps.json @@ -142,11 +142,11 @@ "sha256": "04a604dbc1efa86d0c4f8ace3da7949977928887bf7d884399b764b3888b7f48" }, { - "step": "- name: Verify canonical A2UI native fixtures", + "step": "- name: Verify canonical A2UI native graph", "lanes": [ "packages" ], - "sha256": "eb0c97a21ac19f77ba597c684a0db23d513ba27fc7b9475781a270e63d6923bf" + "sha256": "c57fb15056d86cbeeff0a65fcd6ebb2c41b66151fa04de48c06ba0c83bee6c27" }, { "step": "- name: Configure bounded Ubuntu package downloads", diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index f58935244..c3bffdde5 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -18,6 +18,39 @@ import { join, resolve } from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; import { LLMock } from 'aimock'; +/** Every process below `pid`, deepest first. */ +function descendants(pid) { + let children = []; + try { + children = execFileSync('pgrep', ['-P', String(pid)], { encoding: 'utf8' }) + .split('\n') + .filter(Boolean) + .map(Number); + } catch { + // no children + } + return children.flatMap((child) => [...descendants(child), child]); +} + +function signal(pids, name) { + for (const pid of pids) { + try { + process.kill(pid, name); + } catch { + // already gone + } + } +} + +function alive(pid) { + try { + process.kill(pid, 0); + return true; + } catch (error) { + return error.code === 'EPERM'; + } +} + export function createAimockBackend({ root, backend }) { let starting; let mock; @@ -83,18 +116,20 @@ export function createAimockBackend({ root, backend }) { return child?.pid ?? null; }, async close() { - // `uv run` does not forward signals to the server it started: stop the - // server processes under it first, then uv itself, and wait for it. + // `uv run` does not forward signals to the server it started, and the + // server starts workers of its own: stop every process under uv, then + // uv itself, and wait until all of them are gone. if (child?.pid && child.exitCode === null && child.signalCode === null) { const exited = new Promise((done) => child.once('exit', done)); - try { - execFileSync('pkill', ['-TERM', '-P', String(child.pid)]); - } catch { - // no children left - } + const tree = descendants(child.pid); + signal(tree, 'SIGTERM'); child.kill('SIGTERM'); - const timeout = setTimeout(() => child.kill('SIGKILL'), 10_000); + const timeout = setTimeout(() => { + signal(tree, 'SIGKILL'); + child.kill('SIGKILL'); + }, 10_000); await exited; + while (tree.some(alive)) await delay(25); clearTimeout(timeout); } await mock?.stop(); diff --git a/scripts/react-cockpit/chat-a2ui-configuration.spec.mjs b/scripts/react-cockpit/chat-a2ui-configuration.spec.mjs index 487435d8e..20b7ad6c5 100644 --- a/scripts/react-cockpit/chat-a2ui-configuration.spec.mjs +++ b/scripts/react-cockpit/chat-a2ui-configuration.spec.mjs @@ -17,6 +17,10 @@ test('A2UI has one frozen authored native frontend allocation', () => { topic: 'a2ui', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/a2ui/react', base: '/chat/a2ui/react/', port: 4646, project: 'cockpit-chat-a2ui-react', + backend: { + kind: 'aimock-langgraph', python: 'cockpit/chat/a2ui/python', + fixtures: 'cockpit/chat/a2ui/angular/e2e/fixtures', langgraphPort: 5646, + }, }); assert.ok(Object.isFrozen(reactCockpitConfiguration('chat-a2ui'))); for (const key of ['a2ui', '../chat-a2ui', 'chat-a2ui/../debug', '/chat/a2ui/react/', 'chat-a2ui-remote']) @@ -46,24 +50,19 @@ test('only the A2UI topic packs its optional peer and selects the native A2UI pr assert.ok(build.includes(file), 'Emitted implementation assertion: ' + file); assert.match(build, /module\.renderedLength > 0/); }); -test('production source copying excludes testing and preserves private fixture isolation', () => { +test('production source copying excludes testing and the local backend controls', () => { const visit = (directory) => readdirSync(directory).flatMap((name) => { const path = join(directory, name); return statSync(path).isDirectory() ? visit(path) : /\.spec\.|fixture\.json$/.test(path) ? [] : [path]; }); for (const path of visit('cockpit/chat/a2ui/react/src')) - assert.doesNotMatch(read(path), /chat-a2ui-(?:wire|fixture)|\/__(?:configure|reset|requests|captures|lifetime)/); + assert.doesNotMatch(read(path), /aimock|langgraph-proxy|\/__(?:configure|reset|requests|captures|lifetime|hold)/); const build = read('scripts/react-cockpit/build.mjs'); - assert.doesNotMatch(build, /chat-a2ui-(?:fixture|wire)/); + assert.doesNotMatch(build, /aimock|langgraph-proxy/); assert.match(build, /testing/); + // serve.mjs has no A2UI-specific backend: the shared aimock + proxy path serves it. const serve = read('scripts/react-cockpit/serve.mjs'); - assert.match(serve, /const isChatA2ui = configuration.library === 'chat' && configuration.topic === 'a2ui'/); - assert.equal((serve.match(/createChatA2uiFixture\(\{ configuration: chatA2uiConfiguration \}\)/g) ?? []).length, 2); - assert.match(serve, /chatA2uiConfiguration = Object.freeze\(fixtureConfiguration\(\)\)/); - assert.match(serve, /await chatA2uiFixture.close\(\)/); - assert.match(serve, /'\/__hold\/stream'/); - assert.match(serve, /heldStreams/); - assert.match(serve, /closedWorkers/); + assert.doesNotMatch(serve, /chat-a2ui-fixture|isChatA2ui|chatA2ui/); }); test('shared aviation presentation is package-less and copied at its authored layout', () => { diff --git a/scripts/react-cockpit/chat-a2ui-fixture.mjs b/scripts/react-cockpit/chat-a2ui-fixture.mjs deleted file mode 100644 index 6da4e8165..000000000 --- a/scripts/react-cockpit/chat-a2ui-fixture.mjs +++ /dev/null @@ -1,469 +0,0 @@ -/** Private faithful SDK fixture; never imported by a public build. */ -import { spawn, execFileSync } from 'node:child_process'; -import { createHash, randomUUID } from 'node:crypto'; -import { - readFileSync, - mkdtempSync, - realpathSync, - accessSync, - constants, - mkdirSync, -} from 'node:fs'; -import { join, isAbsolute, delimiter, dirname } from 'node:path'; -import { tmpdir } from 'node:os'; -import { fileURLToPath } from 'node:url'; - -const root = fileURLToPath(new URL('../../', import.meta.url)); -const digest = (path) => - createHash('sha256') - .update( - readFileSync( - new URL('../../cockpit/chat/a2ui/python/' + path, import.meta.url) - ) - ) - .digest('hex'); -function executable(name) { - for (const directory of (process.env.PATH ?? '').split(delimiter)) { - if (!isAbsolute(directory)) continue; - const path = join(directory, name); - try { - accessSync(path, constants.X_OK); - return realpathSync(path); - } catch { - /* Search configured tool paths. */ - } - } - throw Error(`Missing fixture executable: ${name}`); -} -function absoluteConfig(name, fallback) { - const value = process.env[name] || fallback; - if (!isAbsolute(value) || value.includes('\0') || value.includes('\n')) - throw Error(`Invalid ${name}: absolute path required`); - return value; -} -/** Explicit inputs are nonsecret tooling paths; no dotenv or credential environment. */ -export function fixtureConfiguration() { - const owned = mkdtempSync(join(tmpdir(), 'threadplane-a2ui-config-')); - const uv = realpathSync( - absoluteConfig( - 'A2UI_FIXTURE_UV', - process.env.A2UI_FIXTURE_UV || executable('uv') - ) - ); - const cache = absoluteConfig('A2UI_FIXTURE_CACHE', join(owned, 'cache')); - const environment = absoluteConfig( - 'A2UI_FIXTURE_ENV', - join(owned, 'environment') - ); - mkdirSync(cache, { recursive: true }); - const env = { - HOME: owned, - PATH: `${dirname(uv)}:/usr/bin:/bin`, - LANG: 'C.UTF-8', - UV_CACHE_DIR: cache, - }; - // Discovery alone uses the caller's home to find an already-installed uv Python. - const python = realpathSync( - absoluteConfig( - 'A2UI_FIXTURE_PYTHON', - process.env.A2UI_FIXTURE_PYTHON || - execFileSync( - uv, - [ - 'python', - 'find', - '--no-project', - '--no-config', - '--no-python-downloads', - '3.12', - ], - { - env: { ...env, HOME: process.env.HOME ?? owned }, - encoding: 'utf8', - timeout: 10000, - } - ).trim() - ) - ); - const pythonVersion = execFileSync(python, ['--version'], { - env, - encoding: 'utf8', - timeout: 10000, - }).trim(); - if (!/^Python 3\.12\./.test(pythonVersion)) - throw Error('Fixture requires Python 3.12'); - const uvVersion = execFileSync(uv, ['--version'], { - env, - encoding: 'utf8', - timeout: 10000, - }).trim(); - return { uv, python, pythonVersion, uvVersion, environment, cache }; -} -export function createChatA2uiFixture({ - workerTimeoutMs = 60000, - configuration, -} = {}) { - const expected = { - sourceSha256: digest('src/graph.py'), - lockSha256: digest('uv.lock'), - }; - const owned = mkdtempSync(join(tmpdir(), 'threadplane-a2ui-worker-')); - const threads = new Set(), - runs = new Map(), - requests = [], - proofs = []; - let child, - sequence = 0, - pending, - buffer = Buffer.alloc(0), - closed = false, - closing, - ended, - killTimer; - let queue = Promise.resolve(); - const receipt = { worker: null, requests, proofs, stderr: '' }; - function terminate(error) { - if (pending) { - clearTimeout(pending.timer); - pending.reject(error); - pending = undefined; - } - if (child && !receipt.worker.reaped) { - child.kill('SIGTERM'); - killTimer ??= setTimeout(() => child.kill('SIGKILL'), 1500); - } - } - function start() { - const config = configuration ?? fixtureConfiguration(); - receipt.configuration = config; - child = spawn( - config.uv, - [ - 'run', - '--frozen', - '--python', - config.python, - '--no-env-file', - '--no-config', - '--no-python-downloads', - '--project', - 'cockpit/chat/a2ui/python', - 'python', - 'scripts/react-cockpit/chat-a2ui-wire.py', - ], - { - cwd: root, - env: { - HOME: owned, - PATH: `${dirname(config.uv)}:/usr/bin:/bin`, - LANG: 'C.UTF-8', - UV_PROJECT_ENVIRONMENT: config.environment, - UV_CACHE_DIR: config.cache, - PYTHONDONTWRITEBYTECODE: '1', - LANGSMITH_TRACING: 'false', - LANGCHAIN_TRACING_V2: 'false', - }, - stdio: ['pipe', 'pipe', 'pipe'], - } - ); - receipt.worker = { - pid: child.pid, - reaped: false, - code: null, - signal: null, - owned, - }; - ended = new Promise((resolve) => - child.once('close', (code, signal) => { - receipt.worker.reaped = true; - receipt.worker.code = code; - receipt.worker.signal = signal; - clearTimeout(killTimer); - terminate(Error('Graph worker closed')); - resolve(); - }) - ); - child.once('error', (e) => terminate(e)); - child.stdin.on('error', (e) => terminate(e)); - child.stderr.on('data', (bytes) => { - receipt.stderr += bytes.toString(); - if (Buffer.byteLength(receipt.stderr) > 262144) - terminate(Error('Worker diagnostics bound')); - }); - child.stdout.on('data', (bytes) => { - buffer = Buffer.concat([buffer, bytes]); - if (buffer.length > 16 * 1024 * 1024) - return terminate(Error('Worker frame bound')); - for (;;) { - const end = buffer.indexOf(10); - if (end < 0) break; - const line = buffer.subarray(0, end); - buffer = buffer.subarray(end + 1); - try { - const result = JSON.parse( - new TextDecoder('utf-8', { fatal: true }).decode(line) - ); - if (!pending || result.requestId !== pending.id) - throw Error('Unowned worker response'); - const entry = pending; - pending = undefined; - clearTimeout(entry.timer); - if (result.error) entry.reject(Error(result.error)); - else entry.resolve(result); - } catch (e) { - terminate(e); - } - } - }); - } - async function operate(value) { - const operation = queue.then(async () => { - if (closed || receipt.worker?.reaped) throw Error('Closed fixture'); - if (!child) start(); - const result = await new Promise((resolve, reject) => { - const id = ++sequence; - const bytes = Buffer.from( - JSON.stringify({ ...value, requestId: id }) + '\n' - ); - if (bytes.length > 1024 * 1024) - return reject(Error('Worker request bound')); - pending = { - id, - resolve, - reject, - timer: setTimeout( - () => terminate(Error('Graph worker deadline')), - workerTimeoutMs - ), - }; - child.stdin.write(bytes); - }); - if ( - !result.proof?.actualCompiledGraph || - result.proof.networkConnectAttempts !== 0 || - result.proof.op !== value.op || - result.proof.threadId !== (value.threadId ?? null) || - Object.entries(expected).some(([k, v]) => result.proof[k] !== v) - ) - throw Error('Invalid actual graph proof'); - proofs.push(result.proof); - return result; - }); - queue = operation.catch(() => {}); - return operation; - } - const json = (res, value, status = 200) => { - if (!res.destroyed && !res.writableEnded) { - res.writeHead(status, { 'content-type': 'application/json' }); - res.end(JSON.stringify(value)); - } - }; - const event = (kind, value) => - `event: ${kind}\ndata: ${JSON.stringify(value)}\n\n`; - const only = (obj, keys) => - obj && - typeof obj === 'object' && - !Array.isArray(obj) && - Object.keys(obj).every((k) => keys.includes(k)); - const handle = async (req, res, path) => { - if (!path.startsWith('/api/')) return false; - try { - let bytes = 0, - text = ''; - for await (const chunk of req) { - bytes += chunk.length; - if (bytes > 1024 * 1024) throw Error('Request body bound'); - text += chunk.toString(); - } - const body = text ? JSON.parse(text) : {}; - requests.push(structuredClone({ method: req.method, path, body })); - if (path === '/api/threads' && req.method === 'POST') { - if ( - !only(body, ['thread_id', 'if_exists', 'metadata']) || - !/^[a-f0-9-]{36}$/i.test(body.thread_id ?? '') || - threads.has(body.thread_id) || - (body.if_exists && body.if_exists !== 'raise') - ) { - json(res, {}, 400); - return true; - } - threads.add(body.thread_id); - json(res, { thread_id: body.thread_id }); - return true; - } - const route = /^\/api\/threads\/([^/]+)(.*)$/.exec(path); - if (!route || !threads.has(route[1])) { - json(res, {}, 404); - return true; - } - const [, threadId, suffix] = route; - if ( - (suffix === '/state' && req.method === 'GET') || - (suffix === '/state/checkpoint' && req.method === 'POST') - ) { - if (suffix === '/state/checkpoint') { - const cp = body.checkpoint; - if ( - !only(body, ['checkpoint']) || - !only(cp, [ - 'thread_id', - 'checkpoint_id', - 'checkpoint_ns', - 'checkpoint_map', - ]) || - cp.thread_id !== threadId || - cp.checkpoint_ns !== '' || - typeof cp.checkpoint_id !== 'string' || - !cp.checkpoint_id || - (cp.checkpoint_map && - (!only(cp.checkpoint_map, ['']) || - cp.checkpoint_map[''] !== cp.checkpoint_id)) - ) { - json(res, {}, 400); - return true; - } - const { history } = await operate({ op: 'history', threadId }); - if ( - !history.some( - (s) => s.checkpoint.checkpoint_id === cp.checkpoint_id - ) - ) { - json(res, {}, 404); - return true; - } - } - json( - res, - ( - await operate({ - op: 'state', - threadId, - ...(body.checkpoint ? { checkpoint: body.checkpoint } : {}), - }) - ).state - ); - return true; - } - if (suffix === '/history' && req.method === 'POST') { - json( - res, - (await operate({ op: 'history', threadId })).history.slice( - 0, - body.limit ?? 10 - ) - ); - return true; - } - if (/^\/runs\/[^/]+$/.test(suffix) && req.method === 'GET') { - const run = runs.get(suffix.split('/').at(-1)); - receipt.runReads ??= []; - receipt.runReads.push({ threadId, run: structuredClone(run ?? null) }); - json( - res, - run?.thread_id === threadId ? run : {}, - run?.thread_id === threadId ? 200 : 404 - ); - return true; - } - if (suffix === '/runs/stream' && req.method === 'POST') { - const message = body.input?.messages?.[0]; - if ( - !only(body, [ - 'assistant_id', - 'input', - 'stream_mode', - 'stream_subgraphs', - 'stream_resumable', - 'on_disconnect', - ]) || - !only(body.input, ['messages']) || - !only(message, ['type', 'id', 'content']) || - body.assistant_id !== 'c-a2ui' || - body.input.messages.length !== 1 || - message.type !== 'human' || - typeof message.content !== 'string' || - typeof message.id !== 'string' || - !message.id.trim() || - !Array.isArray(body.stream_mode) || - body.stream_mode.some( - (m) => - ![ - 'messages-tuple', - 'values', - 'updates', - 'custom', - 'checkpoints', - ].includes(m) - ) - ) { - json(res, {}, 400); - return true; - } - const run = { - run_id: randomUUID(), - thread_id: threadId, - status: 'running', - }; - runs.set(run.run_id, run); - const wire = await operate({ - op: 'submit', - threadId, - messages: body.input.messages, - }); - receipt.events ??= []; - receipt.events.push({ - threadId, - runId: run.run_id, - events: wire.events, - }); - const modes = new Set( - body.stream_mode.map((m) => (m === 'messages-tuple' ? 'messages' : m)) - ); - res.writeHead(200, { - 'content-type': 'text/event-stream', - 'cache-control': 'no-cache', - 'content-location': `/threads/${threadId}/runs/${run.run_id}`, - }); - res.write(event('metadata', { run_id: run.run_id })); - // Mode selection is the SDK request contract. Never suppress model/title callbacks. - for (const [kind, value] of wire.events) - if (modes.has(kind) || kind === 'error') - res.write(event(kind, value)); - run.status = wire.graphFailure ? 'error' : 'success'; - res.end(); - return true; - } - json(res, {}, 404); - return true; - } catch (e) { - receipt.error = String(e); - json(res, { error: 'Graph fixture failed' }, 500); - return true; - } - }; - handle.configure = (options) => operate({ op: 'configure', options }); - handle.receipt = () => structuredClone(receipt); - handle.close = () => - (closing ??= (async () => { - closed = true; - await queue; - if (child && !receipt.worker.reaped) { - child.stdin.end(); - killTimer = setTimeout(() => child.kill('SIGKILL'), 1500); - } - await ended; - clearTimeout(killTimer); - const pythonPid = proofs.find((proof) => proof.pythonPid)?.pythonPid; - if (pythonPid) { - try { - process.kill(pythonPid, 0); - receipt.worker.pythonAbsent = false; - } catch (error) { - if (error.code !== 'ESRCH') throw error; - receipt.worker.pythonAbsent = true; - } - receipt.worker.pythonPid = pythonPid; - } - })()); - return handle; -} diff --git a/scripts/react-cockpit/chat-a2ui-fixture.spec.mjs b/scripts/react-cockpit/chat-a2ui-fixture.spec.mjs deleted file mode 100644 index ba9e7f78f..000000000 --- a/scripts/react-cockpit/chat-a2ui-fixture.spec.mjs +++ /dev/null @@ -1,495 +0,0 @@ -import assert from 'node:assert/strict'; -import { createServer } from 'node:http'; -import { once } from 'node:events'; -import { randomUUID } from 'node:crypto'; -import test from 'node:test'; -import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; -import * as probe from './chat-a2ui-native-probe.mjs'; -import { Client } from '@langchain/langgraph-sdk'; -import { createChatA2uiFixture } from './chat-a2ui-fixture.mjs'; - -export async function openFixture(t) { - const handle = createChatA2uiFixture(); - const evidence = mkdtempSync(join(tmpdir(), 'threadplane-a2ui-sdk-')); - const server = createServer(async (req, res) => { - if ( - !(await handle(req, res, new URL(req.url, 'http://localhost').pathname)) - ) { - res.writeHead(404); - res.end(); - } - }); - const cleanup = {}; - t.after(() => - probe.withOwnedResources( - async (own) => { - own('listener', async () => { - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - }); - own('fixture', () => handle.close()); - }, - cleanup, - () => { - writeFileSync( - join(evidence, 'receipt.json'), - JSON.stringify({ fixture: handle.receipt(), cleanup }, null, 2) + - '\n', - { mode: 0o600 } - ); - console.log('Private c-a2ui SDK evidence: ' + evidence); - } - ) - ); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - const apiUrl = `http://127.0.0.1:${server.address().port}/api`; - const client = new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }); - const thread = randomUUID(); - await client.threads.create({ threadId: thread, ifExists: 'raise' }); - return { handle, apiUrl, client, thread }; -} -export const action = (name, context) => - JSON.stringify({ - version: 'v0.9', - action: { - name, - surfaceId: - name === 'bookingSubmit' - ? 'booking' - : name === 'flightSelect' - ? 'results' - : 'confirmation', - sourceComponentId: 'button', - timestamp: '2026-10-16T00:00:00Z', - context, - }, - }); -export const inputs = [ - 'I want to fly LAX to JFK', - action('bookingSubmit', { - formId: 'booking', - origin: ['LAX'], - dest: ['JFK'], - date: '2026-11-03', - passengers: 2, - fare_class: ['Business'], - }), - action('flightSelect', { flightId: 'UA123' }), - action('modifySearch', { formId: 'booking' }), -]; -const envelopes = (state) => - state.values.messages - .at(-1) - .content.split('\n') - .slice(1) - .filter(Boolean) - .map(JSON.parse); - -test( - 'exported native probe preserves caller environment on failure', - { timeout: 180000 }, - async () => { - const identity = process.env; - const before = { ...process.env }; - try { - await probe.runNativeProbe({ workerScenario: 'fail-setup' }); - } catch { - /* Expected controlled failure. */ - } - assert.equal( - process.env, - identity, - 'Probe must never replace the caller environment' - ); - assert.deepEqual({ ...process.env }, before); - } -); - -for (const phase of ['emit', 'pack', 'install', 'compiler']) - test( - `deadline interrupts synchronous ${phase} in owned worker group`, - { timeout: 10000 }, - async () => { - const evidence = mkdtempSync(join(tmpdir(), 'a2ui-deadline-')); - const started = Date.now(); - await assert.rejects( - probe.runNativeProbe({ - evidence, - deadlineMs: 1500, - workerScenario: `sync-${phase}`, - }), - /deadline/ - ); - assert.ok(Date.now() - started < 5000); - const report = JSON.parse( - readFileSync(join(evidence, 'report.json'), 'utf8') - ); - assert.equal(report.child.reaped, true); - assert.equal(report.closure.processAbsent, true); - assert.equal(report.closure.groupAbsent, true); - assert.equal(report.closure.controlledChild.absent, true); - assert.equal( - JSON.parse(readFileSync(join(evidence, 'phase.json'), 'utf8')).phase, - phase - ); - } - ); - -test('setup failure closes acquired listener and worker before writing receipt', async () => { - const calls = [], - receipt = {}; - await assert.rejects( - probe.withOwnedResources( - async (own) => { - const server = createServer(); - own('listener', async () => { - server.closeAllConnections(); - await new Promise((r) => server.close(r)); - calls.push('listener'); - }); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - const { spawn } = await import('node:child_process'); - const child = spawn(process.execPath, [ - '-e', - 'setInterval(()=>{},1000)', - ]); - const ended = once(child, 'close'); - own('worker', async () => { - child.kill(); - await ended; - calls.push('worker'); - }); - throw Error('setup failed'); - }, - receipt, - () => calls.push('receipt') - ), - /setup failed/ - ); - assert.deepEqual(calls, ['worker', 'listener', 'receipt']); - assert.deepEqual( - receipt.cleanup.map((x) => x.name), - ['worker', 'listener'] - ); -}); - -test('rejected dispose and worker close preserve primary failure and run remaining cleanup and receipt', async () => { - const calls = [], - receipt = {}; - await assert.rejects( - probe.withOwnedResources( - async (own) => { - own('listener', () => calls.push('listener')); - own('worker', () => { - calls.push('worker'); - throw Error('worker close failed'); - }); - own('session', () => { - calls.push('session'); - throw Error('dispose failed'); - }); - throw Error('original failure'); - }, - receipt, - () => calls.push('receipt') - ), - /original failure/ - ); - assert.deepEqual(calls, ['session', 'worker', 'listener', 'receipt']); - assert.deepEqual( - receipt.cleanup.filter((x) => x.error).map((x) => x.name), - ['session', 'worker'] - ); -}); - -test( - 'real SDK four-turn c-a2ui graph saves only canonical suffix and restores prior arrays', - { timeout: 120000 }, - async (t) => { - const f = await openFixture(t); - let prior = []; - const allEvents = []; - for (const [index, content] of inputs.entries()) { - const id = randomUUID(), - events = []; - for await (const e of f.client.runs.stream(f.thread, 'c-a2ui', { - input: { messages: [{ type: 'human', id, content }] }, - streamMode: ['messages-tuple', 'values', 'updates', 'checkpoints'], - streamSubgraphs: true, - streamResumable: true, - onDisconnect: 'continue', - })) - events.push(e); - allEvents.push(...events); - const saved = await f.client.threads.getState(f.thread); - assert.deepEqual(saved.values.messages.slice(0, prior.length), prior); - assert.equal(saved.values.messages.length, prior.length + 2); - assert.equal(saved.values.messages.at(-2).id, id); - assert.equal(saved.values.messages.at(-2).content, content); - assert.equal(saved.values.messages.at(-1).type, 'ai'); - assert.equal(saved.checkpoint.thread_id, f.thread); - assert.equal(saved.checkpoint.checkpoint_ns, ''); - assert.ok(saved.checkpoint.checkpoint_id); - assert.deepEqual(saved.tasks, []); - assert.deepEqual(saved.next, []); - assert.deepEqual( - await f.client.threads.getState(f.thread, saved.checkpoint), - saved - ); - assert.deepEqual( - (await f.client.threads.getHistory(f.thread, { limit: 100 }))[0], - saved - ); - assert.deepEqual(Object.keys(saved.values), ['messages']); - const data = envelopes(saved).find((e) => e.updateDataModel) - ?.updateDataModel.value; - if (index === 0) - assert.deepEqual(data, { - origin: ['LAX'], - dest: ['JFK'], - date: '', - passengers: 1, - fare_class: ['Economy'], - }); - if (index === 3) - assert.deepEqual(data, { - origin: ['LAX'], - dest: ['JFK'], - date: '2026-11-03', - passengers: 2, - fare_class: ['Business'], - }); - if (index === 1) - assert.match(saved.values.messages.at(-1).content, /UA123/); - if (index === 2) - assert.match( - saved.values.messages.at(-1).content, - /2 passengers.*Business/ - ); - const run = events.find((e) => e.event === 'metadata').data.run_id; - assert.equal((await f.client.runs.get(f.thread, run)).status, 'success'); - prior = saved.values.messages; - } - assert.ok( - allEvents.some( - (e) => - e.event === 'messages' && - e.data[1].langgraph_node === 'generate_title' - ) - ); - assert.ok( - allEvents.some( - (e) => e.event === 'messages' && e.data[0].tool_call_chunks?.length - ) - ); - const receipt = f.handle.receipt(); - assert.deepEqual( - receipt.runReads.map((row) => row.run.status), - ['success', 'success', 'success', 'success'] - ); - assert.ok( - receipt.proofs.every( - (p) => p.actualCompiledGraph && p.networkConnectAttempts === 0 - ) - ); - assert.ok( - receipt.proofs.every( - (p) => - p.pythonPid > 0 && - /^3\.12\./.test(p.pythonVersion) && - p.pythonExecutable.startsWith('/') - ) - ); - assert.deepEqual( - receipt.proofs.find((p) => p.tools?.length)?.tools[0].output[0] - .flight_number, - 'UA123' - ); - assert.ok( - receipt.proofs.some((p) => - p.tools?.some( - (tool) => - tool.name === '_lookup_flight_impl' && - tool.output.flight_number === 'UA123' - ) - ) - ); - } -); - -for (const [name, options, attempts] of [ - ['swallowed title failure', { failTitle: true, invalidAttempts: 0 }, 1], - [ - 'one structured validation retry', - { failTitle: false, invalidAttempts: 1 }, - 2, - ], - [ - 'actual sentinel after three invalid attempts', - { failTitle: false, invalidAttempts: 3 }, - 3, - ], -]) - test( - `real compiled c-a2ui preserves ${name}`, - { timeout: 120000 }, - async (t) => { - const f = await openFixture(t); - await f.handle.configure(options); - const content = inputs[0], - id = randomUUID(), - events = []; - for await (const event of f.client.runs.stream(f.thread, 'c-a2ui', { - input: { messages: [{ type: 'human', id, content }] }, - streamMode: ['messages-tuple', 'values', 'updates', 'checkpoints'], - streamSubgraphs: true, - streamResumable: true, - onDisconnect: 'continue', - })) - events.push(event); - const run = await f.client.runs.get( - f.thread, - events.find((e) => e.event === 'metadata').data.run_id - ); - assert.equal(run.status, 'success'); - const state = await f.client.threads.getState(f.thread); - assert.deepEqual( - await f.client.threads.getState(f.thread, state.checkpoint), - state - ); - assert.deepEqual(state.tasks, []); - assert.deepEqual(state.next, []); - assert.equal(state.values.messages.length, 2); - assert.equal(state.values.messages[0].id, id); - assert.equal(state.values.messages[0].content, content); - assert.match(state.values.messages[1].content, /^---a2ui_JSON---\n/); - const proof = f.handle.receipt().proofs.find((p) => p.op === 'submit'); - assert.equal(proof.models.filter((m) => !m.title).length, attempts); - assert.ok(proof.models.some((m) => m.title)); - assert.ok(proof.actualCompiledGraph); - assert.equal(proof.networkConnectAttempts, 0); - if (attempts > 1) - assert.ok( - proof.models - .filter((m) => !m.title) - .slice(1) - .every((m) => m.retry) - ); - if (attempts === 3) - assert.match( - state.values.messages[1].content, - /Book a flight \(fallback\)/ - ); - else - assert.doesNotMatch( - state.values.messages[1].content, - /Book a flight \(fallback\)/ - ); - if (!options.failTitle) - assert.ok( - events.some( - (e) => - e.event === 'messages' && - e.data[1].langgraph_node === 'generate_title' - ) - ); - } - ); - -test( - 'installed native createSession settles actual unsuppressed c-a2ui callbacks before exact canonical load', - { timeout: 180000 }, - async () => { - const env = process.env, - before = { ...process.env }; - const { runNativeProbe } = await import('./chat-a2ui-native-probe.mjs'); - const report = await runNativeProbe(); - assert.equal(process.env, env); - assert.deepEqual({ ...process.env }, before); - assert.equal(report.receipt.turns.length, 4); - assert.ok(report.receipt.turns.every((turn) => turn.outcome === 'success')); - assert.equal(report.compiler.exit, 0); - assert.equal(report.compiler.claim, 'small public API scaffold only'); - assert.deepEqual(report.compiler.files, ['contract.ts']); - assert.equal(report.application, undefined); - assert.equal(report.consumerChild.reaped, true); - assert.equal(report.receipt.fixture.worker.reaped, true); - assert.equal(report.receipt.fixture.worker.pythonAbsent, true); - assert.ok(report.receipt.cleanup.every((row) => row.closed)); - assert.equal(report.closure.processAbsent, true); - assert.equal(report.closure.groupAbsent, true); - } -); - -test('native probe rejects unknown admission modes and mixed controls before setup', async () => { - for (const mode of ['unknown', '', true, {}, ['application-admission']]) - await assert.rejects( - probe.runNativeProbe({ mode, workerScenario: 'fail-setup' }), - /Unknown native probe mode/ - ); - await assert.rejects( - probe.runNativeProbe({ - mode: 'application-admission', - workerScenario: 'fail-setup', - }), - /Cannot combine admission mode/ - ); -}); - -test('native probe rejects malformed option records and worker CLI combinations', async () => { - const { spawnSync } = await import('node:child_process'); - for (const options of [ - null, - [], - Object.assign(Object.create({ mode: 'application-admission' }), { - workerScenario: 'fail-setup', - }), - { source: '/tmp/foreign.ts' }, - ]) - await assert.rejects( - probe.runNativeProbe(options), - /Unknown native probe options/ - ); - let reads = 0; - await assert.rejects( - probe.runNativeProbe( - Object.defineProperty({}, 'mode', { - get() { - reads++; - return 'application-admission'; - }, - }) - ), - /Unknown native probe options/ - ); - assert.equal(reads, 0); - for (const args of [ - ['--worker', '/tmp', '--mode', 'unknown'], - [ - '--worker', - '/tmp', - '--mode', - 'application-admission', - '--scenario', - 'fail-setup', - ], - ['--worker', '/tmp', '--mode', 'settlement', '--scenario', 'unknown'], - ['--worker', '/tmp', '--mode', 'settlement', 'extra'], - ]) { - const result = spawnSync( - process.execPath, - ['scripts/react-cockpit/chat-a2ui-native-probe.mjs', ...args], - { encoding: 'utf8' } - ); - assert.equal(result.status, 1); - assert.match(result.stderr, /Unknown native probe options/); - } -}); diff --git a/scripts/react-cockpit/chat-a2ui-host.spec.mjs b/scripts/react-cockpit/chat-a2ui-host.spec.mjs deleted file mode 100644 index 2420bf6fb..000000000 --- a/scripts/react-cockpit/chat-a2ui-host.spec.mjs +++ /dev/null @@ -1,224 +0,0 @@ -import assert from 'node:assert/strict'; -import { spawn } from 'node:child_process'; -import { once } from 'node:events'; -import { existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; -import { createServer } from 'node:http'; -import { tmpdir } from 'node:os'; -import { basename, join } from 'node:path'; -import { pathToFileURL } from 'node:url'; -import { setTimeout as delay } from 'node:timers/promises'; -import test from 'node:test'; - -async function until(predicate, message) { - const deadline = Date.now() + 4000; - while (!predicate()) { - assert.ok(Date.now() < deadline, message); - await delay(20); - } -} -async function listening(port) { - try { - await fetch(`http://127.0.0.1:${port}/`, { - redirect: 'manual', - signal: AbortSignal.timeout(200), - }); - return true; - } catch { - return false; - } -} -function absent(pid) { - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); -} -async function freePort() { - const server = createServer(); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - const port = server.address().port; - await new Promise((resolve) => server.close(resolve)); - return port; -} -async function host(t, mode, portOverride) { - const port = portOverride ?? (await freePort()), - parentPort = await freePort(); - const owned = mkdtempSync(join(tmpdir(), 'a2ui-host-')); - // Library CI runs before static assets are built. Keep readiness independent of dist. - const unbuiltAppPath = 'test-only-unbuilt/' + basename(owned); - assert.equal(existsSync(join('dist', unbuiltAppPath)), false); - const marker = join(owned, 'worker.json'); - const release = join(owned, 'release'); - const closed = join(owned, 'closed.json'); - const stub = join(owned, 'fixture.mjs'); - // A module-loader seam keeps controlled workers out of production options/environment. - writeFileSync( - stub, - ` - import {spawn} from 'node:child_process'; - import {once} from 'node:events'; - import {existsSync,writeFileSync} from 'node:fs'; - import {setTimeout as delay} from 'node:timers/promises'; - export const fixtureConfiguration = () => Object.freeze({environment:'/controlled/environment',cache:'/controlled/cache'}); - export function createChatA2uiFixture({configuration}={}) { - const child=spawn(process.execPath,['-e','setInterval(()=>{},1000)']); - const ended=once(child,'close'); - writeFileSync(${JSON.stringify( - marker - )},JSON.stringify({pid:child.pid,configuration})); - const handle=async()=>false; - let preparation; - handle.configure=()=>preparation??=(async()=>{ - const deadline=Date.now()+600; - while(!existsSync(${JSON.stringify( - release - )}) && Date.now(){ - await preparation?.catch(()=>{}); - child.kill(); await ended; - writeFileSync(${JSON.stringify( - closed - )},JSON.stringify({pid:child.pid,reaped:true})); - }; - handle.receipt=()=>({worker:{pid:child.pid},proofs:[],requests:[]}); - return handle; - } - ` - ); - const loader = join(owned, 'loader.mjs'); - writeFileSync( - loader, - `export async function resolve(specifier,context,next){ - if(specifier==='./chat-a2ui-fixture.mjs' && context.parentURL.endsWith('/serve.mjs')) - return {url:${JSON.stringify( - pathToFileURL(stub).href - )},shortCircuit:true}; - return next(specifier,context); - } - export async function load(url,context,next){ - const result=await next(url,context); - if(url.endsWith('/scripts/react-cockpit/serve.mjs')) return {...result,source:String(result.source).replaceAll('configuration.appPath',${JSON.stringify( - JSON.stringify(unbuiltAppPath) - )}).replaceAll('configuration.port',String(${port})).replaceAll(\"listen(3000,\",\"listen(${parentPort},\")}; - return result; - }` - ); - const child = spawn( - process.execPath, - ['--loader', loader, 'scripts/react-cockpit/serve.mjs', 'chat-a2ui'], - { stdio: ['ignore', 'pipe', 'pipe'] } - ); - let output = ''; - child.stdout.on('data', (bytes) => (output += bytes)); - child.stderr.on('data', (bytes) => (output += bytes)); - let result; - const ended = once(child, 'close').then( - ([code, signal]) => (result = { code, signal }) - ); - t.after(async () => { - if (!result) child.kill('SIGTERM'); - await Promise.race([ended, delay(2000)]); - if (!result) { - child.kill('SIGKILL'); - await ended; - } - if (existsSync(marker)) { - const { pid } = JSON.parse(readFileSync(marker)); - try { - process.kill(pid, 'SIGKILL'); - } catch {} - } - }); - await until(() => existsSync(marker) || result, 'worker acquired'); - assert.equal(result, undefined, output); - return { - child, - ended, - marker, - release, - closed, - port, - parentPort, - get result() { - return result; - }, - get output() { - return output; - }, - }; -} - -test( - 'chat A2UI exposes neither listener until initial configuration proof completes', - { timeout: 10000 }, - async (t) => { - const h = await host(t, 'success'); - assert.equal( - await listening(h.port), - false, - 'browser listener must await graph readiness' - ); - assert.equal( - await listening(h.parentPort), - false, - 'parent listener must await graph readiness' - ); - writeFileSync(h.release, 'ready'); - const deadline = Date.now() + 4000; - while (!(await listening(h.port))) { - assert.ok(Date.now() < deadline, h.output); - await delay(20); - } - assert.equal(await listening(h.parentPort), true); - h.child.kill('SIGTERM'); - await h.ended; - assert.equal(h.result.code, 0, h.output); - absent(JSON.parse(readFileSync(h.marker)).pid); - } -); - -for (const mode of ['failure', 'signal', 'signal-success']) - test( - `chat A2UI ${mode} during preparation reaps worker without late readiness`, - { timeout: 10000 }, - async (t) => { - const h = await host(t, mode); - assert.equal( - await listening(h.port), - false, - 'no readiness while preparation pending' - ); - assert.equal(await listening(h.parentPort), false); - if (mode.startsWith('signal')) h.child.kill('SIGTERM'); - await h.ended; - assert.equal(h.result.code, mode === 'failure' ? 1 : 0, h.output); - assert.equal( - existsSync(h.closed), - true, - 'preparation owns worker cleanup' - ); - absent(JSON.parse(readFileSync(h.marker)).pid); - assert.equal(await listening(h.port), false); - assert.equal(await listening(h.parentPort), false); - } - ); - -test( - 'chat A2UI listener bind failure closes the prepared worker', - { timeout: 10000 }, - async (t) => { - const occupied = createServer(); - occupied.listen(0, '127.0.0.1'); - await once(occupied, 'listening'); - t.after(() => new Promise((resolve) => occupied.close(resolve))); - const h = await host(t, 'success', occupied.address().port); - writeFileSync(h.release, 'ready'); - await h.ended; - assert.equal(h.result.code, 1, h.output); - assert.equal(existsSync(h.closed), true); - absent(JSON.parse(readFileSync(h.marker)).pid); - assert.equal(await listening(h.parentPort), false); - } -); diff --git a/scripts/react-cockpit/chat-a2ui-native-probe.mjs b/scripts/react-cockpit/chat-a2ui-native-probe.mjs index ba99c8d03..4e3529250 100644 --- a/scripts/react-cockpit/chat-a2ui-native-probe.mjs +++ b/scripts/react-cockpit/chat-a2ui-native-probe.mjs @@ -63,17 +63,42 @@ export async function withOwnedResources(work, report = {}, finish = () => {}) { async function installedConsumer(mode) { const assert = (await import('node:assert/strict')).default; - const { createServer } = await import('node:http'); + const { createServer, get } = await import('node:http'); + // One-shot reads on their own connection: the SDK's pooled keep-alive + // sockets to the same server must not carry the receipt reads. + const getJson = (url) => + new Promise((resolve, reject) => { + get(url, { agent: false }, (answer) => { + let raw = ''; + answer.setEncoding('utf8'); + answer.on('data', (chunk) => (raw += chunk)); + answer.on('end', () => { + try { + resolve(JSON.parse(raw)); + } catch (error) { + reject(error); + } + }); + }).on('error', reject); + }); const { once } = await import('node:events'); const { randomUUID } = await import('node:crypto'); const { writeFileSync, readFileSync, realpathSync } = await import('node:fs'); const { createRequire } = await import('node:module'); + const { fileURLToPath } = await import('node:url'); const require = createRequire(import.meta.url); const entry = require.resolve('@threadplane/langgraph'); const sdkEntry = createRequire(entry).resolve('@langchain/langgraph-sdk'); const { createSession } = await import(entry); const { Client } = await import(sdkEntry); - const { createChatA2uiFixture } = await import(process.env.A2UI_FIXTURE_URL); + // The repository that owns the backend: aimock replays chat/a2ui's shared + // fixtures into a real `langgraph dev`, behind the recording proxy. + const rootURL = new URL(process.env.A2UI_REPO_ROOT_URL); + assert.equal(rootURL.protocol, 'file:'); + const { createAimockBackend } = await import(new URL('scripts/react-cockpit/aimock-backend.mjs', rootURL).href); + const { createLanggraphProxy } = await import(new URL('scripts/react-cockpit/langgraph-proxy.mjs', rootURL).href); + const { reactCockpitConfiguration } = await import(new URL('scripts/react-cockpit/configuration.mjs', rootURL).href); + const { backend: backendConfig } = reactCockpitConfiguration('chat-a2ui'); assert.ok( realpathSync(entry).startsWith( realpathSync(process.cwd()) + '/node_modules/@threadplane/langgraph/' @@ -94,16 +119,16 @@ async function installedConsumer(mode) { let fixture; await withOwnedResources( async (own) => { - fixture = createChatA2uiFixture(); - own('fixture', () => fixture.close()); + const backend = createAimockBackend({ root: fileURLToPath(rootURL), backend: backendConfig }); + own('backend', async () => { + const pid = backend.pid; + await backend.close(); + if (pid) assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); + }); + await backend.ensure(); + const proxy = createLanggraphProxy({ upstream: `http://127.0.0.1:${backendConfig.langgraphPort}` }); const server = createServer(async (req, res) => { - if ( - !(await fixture( - req, - res, - new URL(req.url, 'http://localhost').pathname - )) - ) { + if (!(await proxy(req, res, new URL(req.url, 'http://localhost').pathname))) { res.writeHead(404); res.end(); } @@ -114,7 +139,15 @@ async function installedConsumer(mode) { }); server.listen(0, '127.0.0.1'); await once(server, 'listening'); - const apiUrl = `http://127.0.0.1:${server.address().port}/api`; + const base = `http://127.0.0.1:${server.address().port}`; + const apiUrl = base + '/api'; + // What the proxy recorded and what the real server streamed. + fixture = { + receipt: async () => ({ + requests: await getJson(base + '/__requests'), + captures: await getJson(base + '/__captures'), + }), + }; const client = new Client({ apiUrl, apiKey: null, @@ -205,9 +238,7 @@ async function installedConsumer(mode) { assert.deepEqual(exact.next, []); assert.deepEqual(exact.values.messages.slice(0, prior.length), prior); assert.equal(exact.values.messages.length, prior.length + 2); - const request = fixture - .receipt() - .requests.filter((r) => r.path.endsWith('/runs/stream')) + const request = (await fixture.receipt()).requests.filter((r) => r.path.endsWith('/runs/stream')) .at(-1); assert.deepEqual(request.body.stream_mode, [ 'values', @@ -344,7 +375,7 @@ async function installedConsumer(mode) { } prior = exact.values.messages; } - const captured = fixture.receipt(); + const captured = await fixture.receipt(); assert.equal( captured.requests.filter((r) => r.path.endsWith('/runs/stream')).length, 4 @@ -355,11 +386,29 @@ async function installedConsumer(mode) { ).length, 4 ); + // Every run the consumer read back finished on the real server. + const runReads = await Promise.all( + captured.requests + .filter((r) => r.method === 'GET' && /\/runs\/[^/]+$/.test(r.path)) + .map((r) => + getJson(`http://127.0.0.1:${backendConfig.langgraphPort}` + r.path.replace(/^\/api/, '')) + ) + ); assert.deepEqual( - captured.runReads.map((row) => row.run.status), + runReads.map((run) => run.status), ['success', 'success', 'success', 'success'] ); - const callbacks = captured.events.flatMap((row) => row.events); + // The callbacks the real server streamed, as [event, data] pairs. + const callbacks = captured.captures.flatMap((row) => + row.sse + .split(/\r?\n\r?\n/) + .map((frame) => { + const event = /^event: (.*)$/m.exec(frame)?.[1]; + const data = /^data: (.*)$/m.exec(frame)?.[1]; + return event && data ? [event.split('|')[0], JSON.parse(data)] : null; + }) + .filter(Boolean) + ); assert.ok( callbacks.some( ([kind, value]) => @@ -389,7 +438,7 @@ async function installedConsumer(mode) { }, report, () => { - report.fixture = fixture?.receipt(); + report.backend = backendConfig; writeFileSync('native-receipt.json', JSON.stringify(report) + '\n', { flag: 'wx', mode: 0o600, @@ -554,19 +603,18 @@ async function runWorker(evidence, scenario, mode) { fileURLToPath(import.meta.url), join(evidence, 'probe-source.mjs') ); - const fixture = join(root, 'scripts/react-cockpit/chat-a2ui-fixture.mjs'); - copyFileSync(fixture, join(evidence, 'fixture-source.mjs')); - copyFileSync( - join(root, 'scripts/react-cockpit/chat-a2ui-wire.py'), - join(evidence, 'wire-source.py') - ); - report.fixtureSha256 = sha(fixture); - report.wireSha256 = sha( - join(root, 'scripts/react-cockpit/chat-a2ui-wire.py') - ); + // The backend the consumer runs against: aimock + `langgraph dev`, behind + // the recording proxy. Keep the exact sources as evidence. + for (const [name, relative] of [ + ['aimock-backend-source.mjs', 'scripts/react-cockpit/aimock-backend.mjs'], + ['proxy-source.mjs', 'scripts/react-cockpit/langgraph-proxy.mjs'], + ]) { + copyFileSync(join(root, relative), join(evidence, name)); + report[name] = sha(join(root, relative)); + } const child = spawn(node, ['native-consumer.mjs'], { cwd: consumer, - env: { ...env, A2UI_FIXTURE_URL: pathToFileURL(fixture).href }, + env: { ...env, A2UI_REPO_ROOT_URL: pathToFileURL(root + '/').href }, stdio: ['ignore', 'pipe', 'pipe'], }); let stdout = '', @@ -756,21 +804,17 @@ export async function runNativeProbe(options = {}) { } }; try { - // Only explicit nonsecret fixture configuration crosses the worker boundary. + // Only the nonsecret locations `uv run langgraph dev` needs cross the + // worker boundary: uv, its cache and an explicit interpreter (the worker + // HOME is empty). if (!workerScenario) { - const { fixtureConfiguration } = await import('./chat-a2ui-fixture.mjs'); - const config = fixtureConfiguration(); + const uv = realpathSync(execFileSync('which', ['uv'], { encoding: 'utf8' }).trim()); Object.assign(env, { - A2UI_FIXTURE_UV: config.uv, - A2UI_FIXTURE_PYTHON: config.python, - A2UI_FIXTURE_ENV: config.environment, - A2UI_FIXTURE_CACHE: config.cache, + PATH: `${dirname(node)}:${dirname(uv)}:/usr/bin:/bin`, + UV_CACHE_DIR: execFileSync(uv, ['cache', 'dir'], { encoding: 'utf8' }).trim(), + ...(process.env.UV_PYTHON ? { UV_PYTHON: process.env.UV_PYTHON } : {}), }); report.environmentKeys = Object.keys(env).sort(); - writeFileSync( - join(evidence, 'fixture-config.json'), - JSON.stringify(config, null, 2) + '\n' - ); } child = spawn( node, diff --git a/scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs b/scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs new file mode 100644 index 000000000..786194310 --- /dev/null +++ b/scripts/react-cockpit/chat-a2ui-native-probe.spec.mjs @@ -0,0 +1,219 @@ +import assert from 'node:assert/strict'; +import { createServer } from 'node:http'; +import { once } from 'node:events'; +import test from 'node:test'; +import { mkdtempSync, readFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import * as probe from './chat-a2ui-native-probe.mjs'; + +// The installed-package probe's own harness: option checks, owned cleanup, +// deadlines, and a settlement run against the real backend (aimock + +// `langgraph dev`, behind the recording proxy). +test( + 'exported native probe preserves caller environment on failure', + { timeout: 180000 }, + async () => { + const identity = process.env; + const before = { ...process.env }; + try { + await probe.runNativeProbe({ workerScenario: 'fail-setup' }); + } catch { + /* Expected controlled failure. */ + } + assert.equal( + process.env, + identity, + 'Probe must never replace the caller environment' + ); + assert.deepEqual({ ...process.env }, before); + } +); + +for (const phase of ['emit', 'pack', 'install', 'compiler']) + test( + `deadline interrupts synchronous ${phase} in owned worker group`, + { timeout: 10000 }, + async () => { + const evidence = mkdtempSync(join(tmpdir(), 'a2ui-deadline-')); + const started = Date.now(); + await assert.rejects( + probe.runNativeProbe({ + evidence, + deadlineMs: 1500, + workerScenario: `sync-${phase}`, + }), + /deadline/ + ); + assert.ok(Date.now() - started < 5000); + const report = JSON.parse( + readFileSync(join(evidence, 'report.json'), 'utf8') + ); + assert.equal(report.child.reaped, true); + assert.equal(report.closure.processAbsent, true); + assert.equal(report.closure.groupAbsent, true); + assert.equal(report.closure.controlledChild.absent, true); + assert.equal( + JSON.parse(readFileSync(join(evidence, 'phase.json'), 'utf8')).phase, + phase + ); + } + ); + +test('setup failure closes acquired listener and worker before writing receipt', async () => { + const calls = [], + receipt = {}; + await assert.rejects( + probe.withOwnedResources( + async (own) => { + const server = createServer(); + own('listener', async () => { + server.closeAllConnections(); + await new Promise((r) => server.close(r)); + calls.push('listener'); + }); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); + const { spawn } = await import('node:child_process'); + const child = spawn(process.execPath, [ + '-e', + 'setInterval(()=>{},1000)', + ]); + const ended = once(child, 'close'); + own('worker', async () => { + child.kill(); + await ended; + calls.push('worker'); + }); + throw Error('setup failed'); + }, + receipt, + () => calls.push('receipt') + ), + /setup failed/ + ); + assert.deepEqual(calls, ['worker', 'listener', 'receipt']); + assert.deepEqual( + receipt.cleanup.map((x) => x.name), + ['worker', 'listener'] + ); +}); + +test('rejected dispose and worker close preserve primary failure and run remaining cleanup and receipt', async () => { + const calls = [], + receipt = {}; + await assert.rejects( + probe.withOwnedResources( + async (own) => { + own('listener', () => calls.push('listener')); + own('worker', () => { + calls.push('worker'); + throw Error('worker close failed'); + }); + own('session', () => { + calls.push('session'); + throw Error('dispose failed'); + }); + throw Error('original failure'); + }, + receipt, + () => calls.push('receipt') + ), + /original failure/ + ); + assert.deepEqual(calls, ['session', 'worker', 'listener', 'receipt']); + assert.deepEqual( + receipt.cleanup.filter((x) => x.error).map((x) => x.name), + ['session', 'worker'] + ); +}); + + +test( + 'installed native createSession settles actual unsuppressed c-a2ui callbacks before exact canonical load', + { timeout: 180000 }, + async () => { + const env = process.env, + before = { ...process.env }; + const { runNativeProbe } = await import('./chat-a2ui-native-probe.mjs'); + const report = await runNativeProbe(); + assert.equal(process.env, env); + assert.deepEqual({ ...process.env }, before); + assert.equal(report.receipt.turns.length, 4); + assert.ok(report.receipt.turns.every((turn) => turn.outcome === 'success')); + assert.equal(report.compiler.exit, 0); + assert.equal(report.compiler.claim, 'small public API scaffold only'); + assert.deepEqual(report.compiler.files, ['contract.ts']); + assert.equal(report.application, undefined); + assert.equal(report.consumerChild.reaped, true); + assert.equal(report.receipt.backend.kind, 'aimock-langgraph'); + assert.ok(report.receipt.cleanup.every((row) => row.closed)); + assert.equal(report.closure.processAbsent, true); + assert.equal(report.closure.groupAbsent, true); + } +); + +test('native probe rejects unknown admission modes and mixed controls before setup', async () => { + for (const mode of ['unknown', '', true, {}, ['application-admission']]) + await assert.rejects( + probe.runNativeProbe({ mode, workerScenario: 'fail-setup' }), + /Unknown native probe mode/ + ); + await assert.rejects( + probe.runNativeProbe({ + mode: 'application-admission', + workerScenario: 'fail-setup', + }), + /Cannot combine admission mode/ + ); +}); + +test('native probe rejects malformed option records and worker CLI combinations', async () => { + const { spawnSync } = await import('node:child_process'); + for (const options of [ + null, + [], + Object.assign(Object.create({ mode: 'application-admission' }), { + workerScenario: 'fail-setup', + }), + { source: '/tmp/foreign.ts' }, + ]) + await assert.rejects( + probe.runNativeProbe(options), + /Unknown native probe options/ + ); + let reads = 0; + await assert.rejects( + probe.runNativeProbe( + Object.defineProperty({}, 'mode', { + get() { + reads++; + return 'application-admission'; + }, + }) + ), + /Unknown native probe options/ + ); + assert.equal(reads, 0); + for (const args of [ + ['--worker', '/tmp', '--mode', 'unknown'], + [ + '--worker', + '/tmp', + '--mode', + 'application-admission', + '--scenario', + 'fail-setup', + ], + ['--worker', '/tmp', '--mode', 'settlement', '--scenario', 'unknown'], + ['--worker', '/tmp', '--mode', 'settlement', 'extra'], + ]) { + const result = spawnSync( + process.execPath, + ['scripts/react-cockpit/chat-a2ui-native-probe.mjs', ...args], + { encoding: 'utf8' } + ); + assert.equal(result.status, 1); + assert.match(result.stderr, /Unknown native probe options/); + } +}); diff --git a/scripts/react-cockpit/chat-a2ui-readiness.spec.mjs b/scripts/react-cockpit/chat-a2ui-readiness.spec.mjs deleted file mode 100644 index 0e6da0a89..000000000 --- a/scripts/react-cockpit/chat-a2ui-readiness.spec.mjs +++ /dev/null @@ -1,129 +0,0 @@ -import assert from 'node:assert/strict'; -import { createHash, randomUUID } from 'node:crypto'; -import { once } from 'node:events'; -import { readFileSync } from 'node:fs'; -import { createServer } from 'node:http'; -import test from 'node:test'; -import { Client } from '@langchain/langgraph-sdk'; -import { - createChatA2uiFixture, - fixtureConfiguration, -} from './chat-a2ui-fixture.mjs'; - -const hash = (path) => - createHash('sha256').update(readFileSync(path)).digest('hex'); - -test( - 'shared explicit tooling preserves fresh actual graphs and reaps both Python children', - { timeout: 120000 }, - async (t) => { - const configuration = Object.freeze(fixtureConfiguration()); - const fixtures = []; - const listeners = []; - t.after(async () => { - for (const server of listeners) { - server.closeAllConnections(); - await new Promise((resolve) => server.close(resolve)); - } - for (const fixture of fixtures) await fixture.close(); - }); - async function open() { - const fixture = createChatA2uiFixture({ configuration }); - fixtures.push(fixture); - await fixture.configure({}); - const receipt = fixture.receipt(); - assert.deepEqual( - receipt.requests, - [], - 'readiness creates no conversation or run' - ); - assert.equal(receipt.proofs.length, 1); - assert.deepEqual(receipt.proofs[0].models, []); - assert.deepEqual(receipt.proofs[0].tools, []); - assert.equal(receipt.proofs[0].threadId, null); - const server = createServer(async (req, res) => { - if ( - !(await fixture( - req, - res, - new URL(req.url, 'http://localhost').pathname - )) - ) { - res.writeHead(404); - res.end(); - } - }); - listeners.push(server); - server.listen(0, '127.0.0.1'); - await once(server, 'listening'); - const apiUrl = `http://127.0.0.1:${server.address().port}/api`; - return { - fixture, - apiUrl, - client: new Client({ - apiUrl, - apiKey: null, - callerOptions: { maxRetries: 0 }, - }), - }; - } - const first = await open(), - threadId = randomUUID(); - await first.client.threads.create({ threadId, ifExists: 'raise' }); - for await (const event of first.client.runs.stream(threadId, 'c-a2ui', { - input: { - messages: [ - { - type: 'human', - id: randomUUID(), - content: 'I want to fly LAX to JFK', - }, - ], - }, - streamMode: ['messages-tuple', 'values', 'updates', 'checkpoints'], - streamSubgraphs: true, - streamResumable: true, - onDisconnect: 'continue', - })) - assert.notEqual(event.event, 'error'); - assert.equal( - (await first.client.threads.getState(threadId)).values.messages.length, - 2 - ); - await first.fixture.close(); - const second = await open(); - assert.equal( - (await fetch(`${second.apiUrl}/threads/${threadId}/state`)).status, - 404 - ); - await second.client.threads.create({ threadId, ifExists: 'raise' }); - assert.deepEqual( - (await second.client.threads.getState(threadId)).values, - {} - ); - await second.fixture.close(); - const receipts = fixtures.map((f) => f.receipt()); - assert.notEqual(receipts[0].worker.pid, receipts[1].worker.pid); - assert.notEqual(receipts[0].worker.pythonPid, receipts[1].worker.pythonPid); - assert.notEqual(receipts[0].worker.owned, receipts[1].worker.owned); - for (const receipt of receipts) { - assert.deepEqual(receipt.configuration, configuration); - for (const proof of receipt.proofs) { - assert.equal(proof.actualCompiledGraph, true); - assert.equal(proof.networkConnectAttempts, 0); - assert.equal( - proof.sourceSha256, - hash('cockpit/chat/a2ui/python/src/graph.py') - ); - assert.equal( - proof.lockSha256, - hash('cockpit/chat/a2ui/python/uv.lock') - ); - } - assert.equal(receipt.worker.reaped, true); - assert.equal(receipt.worker.pythonAbsent, true); - for (const pid of [receipt.worker.pid, receipt.worker.pythonPid]) - assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }); - } - } -); diff --git a/scripts/react-cockpit/chat-a2ui-wire.py b/scripts/react-cockpit/chat-a2ui-wire.py deleted file mode 100644 index ca048429f..000000000 --- a/scripts/react-cockpit/chat-a2ui-wire.py +++ /dev/null @@ -1,179 +0,0 @@ -"""Private network-free fixture for the unchanged current c-a2ui graph.""" -import asyncio -from contextlib import redirect_stdout -from copy import deepcopy -import hashlib -import importlib.util -import json -import os -from pathlib import Path -import socket -import sys -import traceback -from unittest.mock import patch - -from langchain_core.language_models.chat_models import BaseChatModel -from langchain_core.messages import AIMessage, AIMessageChunk -from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.utils.function_calling import convert_to_openai_tool -from langgraph.checkpoint.memory import InMemorySaver - -ROOT = Path(__file__).resolve().parents[2] -PROJECT = ROOT / "cockpit/chat/a2ui/python" -SOURCE = PROJECT / "src/graph.py" -OPTIONS = {"failTitle": False, "invalidAttempts": 0} -EVIDENCE = {"models": [], "tools": []} - - -def component(identity, kind, **props): - return {"id": identity, "component": kind, **props} - - -def specification(schema, messages): - """Deterministic model responses from actual production prompts, not nodes.""" - prompt = messages[0].content - if schema == "BookingFormSpec": - defaults = json.JSONDecoder().raw_decode(prompt.split("data_model MUST be ", 1)[1])[0] - children = ["title", "origin", "dest", "date", "passengers", "fare", "submit"] - components = [component("root", "Column", children=["card"]), component("card", "Card", child="card_col"), component("card_col", "Column", children=children), component("title", "Text", text="Book a flight", variant="h2")] - for identity, key, label, values in [("origin", "origin", "Origin", module.AIRPORT_CODES), ("dest", "dest", "Destination", module.AIRPORT_CODES), ("fare", "fare_class", "Fare class", module.FARE_CLASSES)]: - components.append(component(identity, "ChoicePicker", label=label, options=[{"label": value, "value": value} for value in values], value={"path": "/" + key}, variant="mutuallyExclusive")) - components += [component("date", "DateTimeInput", label="Departure date (YYYY-MM-DD)", value={"path": "/date"}, enableDate=True), component("passengers", "TextField", label="Passengers", value={"path": "/passengers"}, variant="number"), component("submit", "Button", child="submit_label", variant="primary", action={"event": {"name": "bookingSubmit", "context": {"formId": "booking", **{key: {"path": "/" + key} for key in defaults}}}}), component("submit_label", "Text", text="Search flights")] - return {"surface_id": "booking", "data_model": defaults, "components": components} - if schema == "FlightResultsSpec": - flights = json.JSONDecoder().raw_decode(prompt.split("following flights:\n\n", 1)[1])[0] - components, cards = [], [] - for index, flight in enumerate(flights): - key = str(index) - cards.append("card_" + key) - components += [component("card_" + key, "Card", child="col_" + key), component("col_" + key, "Column", children=["title_" + key, "route_" + key, "time_" + key, "btn_" + key]), component("title_" + key, "Text", text=f'{flight["airline"]} flight {flight["flight_number"]}', variant="h3"), component("route_" + key, "Text", text=f'{flight["from"]} → {flight["to"]} • {flight["duration_min"]} min • {flight["aircraft"]}', variant="body"), component("time_" + key, "Text", text=f'Depart {flight["depart_local"]} • Arrive {flight["arrive_local"]} • Gate {flight["gate"]}', variant="caption"), component("btn_" + key, "Button", child="btn_label_" + key, variant="primary", action={"event": {"name": "flightSelect", "context": {"flightId": flight["flight_number"]}}}), component("btn_label_" + key, "Text", text="Select")] - if not cards: - cards = ["empty"] - components = [component("empty", "Text", text="No flights found"), component("modify", "Button", child="modify_label", action={"event": {"name": "modifySearch", "context": {"formId": "booking"}}}), component("modify_label", "Text", text="Modify search")] - cards += ["modify"] - return {"surface_id": "results", "data_model": {}, "components": [component("root", "Column", children=cards), *components]} - flight = json.JSONDecoder().raw_decode(prompt.split("lookup_flight():\n\n", 1)[1])[0] - party = prompt.split("original booking submission): ", 1)[1].split("\n", 1)[0] - return {"surface_id": "confirmation", "data_model": {}, "components": [component("root", "Column", children=["card"]), component("card", "Card", child="card_col"), component("card_col", "Column", children=["title", "party_text", "modify"]), component("title", "Text", text=f'{flight["airline"]} flight {flight["flight_number"]}', variant="h2"), component("party_text", "Text", text=party, variant="body"), component("modify", "Button", child="modify_label", variant="primary", action={"event": {"name": "modifySearch", "context": {"formId": "booking"}}}), component("modify_label", "Text", text="Modify search")]} - - -class FixtureModel(BaseChatModel): - title: bool = False - - @property - def _llm_type(self): - return "local-c-a2ui-fixture" - - def bind_tools(self, tools, **kwargs): - schema = convert_to_openai_tool(tools[0])["function"]["name"] - return self.bind(fixture_schema=schema) - - def _stream(self, messages, stop=None, run_manager=None, **kwargs): - schema = kwargs.get("fixture_schema") - EVIDENCE["models"].append({"schema": schema, "title": self.title, "retry": len(messages) > 1 and "Previous attempt failed" in messages[-1].content}) - if self.title: - if OPTIONS["failTitle"]: - raise RuntimeError("Fixture title failure") - yield ChatGenerationChunk(message=AIMessageChunk(content="Local booking title")) - return - value = specification(schema, messages) - if OPTIONS["invalidAttempts"]: - OPTIONS["invalidAttempts"] -= 1 - value["components"][0]["component"] = "UnsupportedFixtureComponent" - text = json.dumps(value) - for index, fragment in enumerate([text[:len(text)//2], text[len(text)//2:]]): - yield ChatGenerationChunk(message=AIMessageChunk(content="", tool_call_chunks=[{"name": schema if index == 0 else None, "args": fragment, "id": "fixture-call-" + str(len(EVIDENCE["models"])) if index == 0 else None, "index": 0, "type": "tool_call_chunk"}])) - - def _generate(self, messages, stop=None, run_manager=None, **kwargs): - chunks = list(self._stream(messages, stop, run_manager, **kwargs)) - message = chunks[0].message - for chunk in chunks[1:]: - message += chunk.message - return ChatResult(generations=[ChatGeneration(message=AIMessage(**{key: value for key, value in message.model_dump().items() if key not in ["type", "tool_call_chunks"]}))]) - - -class Metadata: - def __init__(self): - self.rows = {} - - async def get(self, thread_id): - return {"thread_id": thread_id, "metadata": self.rows.get(thread_id, {})} - - async def update(self, thread_id, metadata): - self.rows[thread_id] = dict(metadata) - - -class FixtureChatOpenAI(FixtureModel): - def __init__(self, **kwargs): - super().__init__(title=kwargs.get("model") == "gpt-5-mini") - - -def encode(value): - if hasattr(value, "model_dump"): - return value.model_dump(mode="json") - raise TypeError("Unsupported wire value") - - -def checkpoint(state): - return {"values": state.values, "next": list(state.next), "checkpoint": {key: value for key, value in state.config["configurable"].items() if key in ["thread_id", "checkpoint_id", "checkpoint_ns", "checkpoint_map"]}, "parent_checkpoint": state.parent_config["configurable"] if state.parent_config else None, "created_at": state.created_at, "metadata": state.metadata, "tasks": [{"id": task.id, "name": task.name, **({"error": task.error} if task.error is not None else {}), "interrupts": [{"id": item.id, "value": item.value} for item in task.interrupts], **({"state": task.state} if task.state is not None else {})} for task in state.tasks]} - - -def profile(frame, event, arg): - if event == "return" and frame.f_code.co_filename == str(SOURCE) and frame.f_code.co_name in ["_find_routes_impl", "_lookup_flight_impl"]: - EVIDENCE["tools"].append({"name": frame.f_code.co_name, "args": deepcopy({key: frame.f_locals[key] for key in frame.f_code.co_varnames[:frame.f_code.co_argcount]}), "output": deepcopy(arg)}) - - -async def main(): - global module - client = type("LocalClient", (), {"threads": Metadata()})() - with patch.object(socket.socket, "connect", side_effect=AssertionError("Socket connect forbidden")) as connect, patch.object(socket.socket, "connect_ex", side_effect=AssertionError("Socket connect forbidden")) as connect_ex, patch("langchain_openai.ChatOpenAI", FixtureChatOpenAI), patch("langgraph_sdk.get_client", return_value=client): - with redirect_stdout(sys.stderr): - spec = importlib.util.spec_from_file_location("actual_c_a2ui_graph", SOURCE) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - graph = module.graph.builder.compile(checkpointer=InMemorySaver()) - for line in sys.stdin: - request = {} - try: - if len(line.encode()) > 1024 * 1024: - raise ValueError("Request bound") - request = json.loads(line) - config = {"configurable": {"thread_id": request.get("threadId", ""), **request.get("checkpoint", {})}} - EVIDENCE["tools"].clear() - EVIDENCE["models"].clear() - with redirect_stdout(sys.stderr): - if request["op"] == "configure": - OPTIONS.update(request["options"]) - result = {} - elif request["op"] == "state": - result = {"state": checkpoint(graph.get_state(config))} - elif request["op"] == "history": - result = {"history": [checkpoint(state) for state in graph.get_state_history(config)]} - elif request["op"] == "submit": - events, failed = [], False - sys.setprofile(profile) - try: - async for kind, value in graph.astream({"messages": request["messages"]}, config, stream_mode=["messages", "values", "updates", "checkpoints"]): - events.append([kind, deepcopy(value)]) - except Exception as error: - failed = True - events.append(["error", {"error": type(error).__name__, "message": str(error)}]) - finally: - sys.setprofile(None) - result = {"events": events, "graphFailure": failed, "state": checkpoint(graph.get_state(config))} - else: - raise ValueError("Unsupported operation") - result["proof"] = {"actualCompiledGraph": True, "pythonPid": os.getpid(), "pythonExecutable": str(Path(sys.executable).resolve()), "pythonVersion": sys.version.split()[0], "nodes": sorted(graph.nodes), "op": request["op"], "threadId": request.get("threadId"), "sourceSha256": hashlib.sha256(SOURCE.read_bytes()).hexdigest(), "lockSha256": hashlib.sha256((PROJECT / "uv.lock").read_bytes()).hexdigest(), "networkConnectAttempts": connect.call_count + connect_ex.call_count, **deepcopy(EVIDENCE)} - if connect.call_count or connect_ex.call_count: - raise AssertionError("Network attempted") - result["requestId"] = request["requestId"] - encoded = json.dumps(result, default=encode) - if len(encoded.encode()) > 16 * 1024 * 1024: - raise ValueError("Response bound") - print(encoded, flush=True) - except Exception: - traceback.print_exc(file=sys.stderr) - print(json.dumps({"requestId": request.get("requestId"), "error": "Graph fixture operation failed"}), flush=True) - - -asyncio.run(main()) diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index a0f8a6ac8..660359100 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -41,6 +41,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { topic: 'a2ui', library: 'chat', adapter: 'langgraph', appPath: 'cockpit/chat/a2ui/react', base: '/chat/a2ui/react/', port: 4646, project: 'cockpit-chat-a2ui-react', + backend: aimockLanggraph('chat/a2ui', 4646), }); if (topic === 'chat-debug') return Object.freeze({ diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 5ec136963..17d9d4dcc 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -4,7 +4,6 @@ import { readFileSync } from 'node:fs'; import { dirname, extname, resolve, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; import { reactCockpitConfiguration } from './configuration.mjs'; -import { createChatA2uiFixture, fixtureConfiguration } from './chat-a2ui-fixture.mjs'; import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; import { createNativeMafFixture } from './native-maf-fixture.mjs'; @@ -14,28 +13,6 @@ import { createAimockBackend } from './aimock-backend.mjs'; const configuration = reactCockpitConfiguration(process.argv[2]); -const isChatA2ui = configuration.library === 'chat' && configuration.topic === 'a2ui'; -let chatA2uiFixture = null; -let chatA2uiConfiguration; -// Local failure controls own only these responses; production builds never import this server. -let holdA2uiStreams = false; -const heldA2uiStreams = new Map(); -const a2uiStreamCloses = []; -const a2uiClosedWorkers = []; -const a2uiReceipt = () => ({ ...chatA2uiFixture.receipt(), closedWorkers: [...a2uiClosedWorkers], heldStreams: heldA2uiStreams.size, heldCompleted: [...heldA2uiStreams.values()].filter(held => held.finished !== null).length, streamCloses: [...a2uiStreamCloses] }); -const releaseA2uiStreams = (cancel = false) => { - holdA2uiStreams = false; - for (const [response, held] of heldA2uiStreams) { - if (cancel) response.destroy(); - else { - heldA2uiStreams.delete(response); - response.write = held.write; - response.end = held.end; - for (const chunk of held.chunks) response.write(...chunk); - if (held.finished) response.end(...held.finished); - } - } -}; const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; @@ -76,48 +53,6 @@ const json = (response, value, status = 200) => { }; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; - if (chatA2uiFixture) { - if (pathname.startsWith('/__')) { - const controls = { - '/__requests': ['GET', () => chatA2uiFixture.receipt().requests], - '/__captures': ['GET', () => chatA2uiFixture.receipt().proofs], - '/__lifetime': ['GET', a2uiReceipt], - '/__hold/stream': ['POST', () => { holdA2uiStreams = true; return {}; }], - '/__release': ['POST', () => { releaseA2uiStreams(); return {}; }], - '/__reset': ['POST', async () => { - releaseA2uiStreams(true); - await chatA2uiFixture.close(); - a2uiClosedWorkers.push(chatA2uiFixture.receipt()); - chatA2uiFixture = createChatA2uiFixture({ configuration: chatA2uiConfiguration }); - return {}; - }], - }; - const control = Object.hasOwn(controls, pathname) ? controls[pathname] : undefined; - if (!control) return json(response, {}, 404); - if (request.method !== control[0]) return json(response, {}, 405); - try { return json(response, await control[1]()); } - catch { return json(response, { error: 'Local fixture control failed' }, 500); } - } - if (holdA2uiStreams && request.method === 'POST' && /^\/api\/threads\/[^/]+\/runs\/stream$/.test(pathname)) { - const held = { write: response.write, end: response.end, chunks: [], finished: null, bytes: 0 }; - heldA2uiStreams.set(response, held); - response.write = (...args) => { - held.bytes += Buffer.byteLength(args[0]); - if (held.bytes > 32 * 1024 * 1024) { response.destroy(); return false; } - if (response.destroyed) return false; - response.flushHeaders(); - held.chunks.push(args); - return true; - }; - response.end = (...args) => { held.finished = args; return response; }; - response.once('close', () => { - const wasHeld = heldA2uiStreams.delete(response); - a2uiStreamCloses.push({ path: pathname, held: wasHeld, destroyed: response.destroyed }); - }); - } - if (await chatA2uiFixture(request, response, pathname)) return; - if (pathname.startsWith('/developer-api/')) return json(response, {}, 404); - } if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; @@ -188,25 +123,16 @@ const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; let closing; let shutdown; -if (isChatA2ui || nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { +if (nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { shutdown = (code = 0) => { closing ??= (async () => { - let a2uiFailure; - if (isChatA2ui) { - releaseA2uiStreams(true); - try { await chatA2uiFixture?.close(); } - catch (error) { a2uiFailure = error; } - } - else await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture).close(); + await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture).close(); await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { owned.close(resolve); owned.closeAllConnections(); }))); - if (chatA2uiFixture) console.log(JSON.stringify({ serverPid: process.pid, parentServerOwned: Boolean(parentServer), chatA2uiFixtureClosed: a2uiReceipt(), listenersClosed: true })); - if (a2uiFailure) throw a2uiFailure; })(); - if (isChatA2ui) void closing.then(() => process.exit(code), () => process.exit(1)); - else void closing.finally(() => process.exit(code)); + void closing.finally(() => process.exit(code)); }; process.once('SIGINT', () => shutdown()); process.once('SIGTERM', () => shutdown()); @@ -223,13 +149,6 @@ if (aimockBackend && !shutdown) { // Own signals, errors, workers and both listeners before tooling discovery or proof. // Only dependency tooling is reused; each reset still owns a fresh graph process. try { - if (isChatA2ui) { - chatA2uiConfiguration = Object.freeze(fixtureConfiguration()); - if (!closing) { - chatA2uiFixture = createChatA2uiFixture({ configuration: chatA2uiConfiguration }); - await chatA2uiFixture.configure({}); - } - } if (!closing) { server.listen(configuration.port, '127.0.0.1'); parentServer?.listen(3000, '127.0.0.1'); From c9327f45684ff958ecb17d2009f006050cabdea5 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Wed, 18 Nov 2026 22:08:35 -0800 Subject: [PATCH 39/41] test(cockpit): run React runtime e2e and website previews on aimock + the real servers (batch 7) Strands and Microsoft Agent Framework run the example's own uvicorn server and Mastra the deployments/ag-ui-mastra service (new aimock-mastra backend; the proxy adds its internal token), each with aimock replaying the model. The react-parity loopback models become fixtures keyed on the React preset; scenario tags replace /__reset { scenario }; terminal corruptions and holds are proxy edits. The native fixture servers, their parity dirs, preparation targets and CI steps are deleted. The AG-UI proxy records what the browser got (`sent`) beside what the server streamed. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 14 +- apps/website/e2e/react-maf-preview.spec.ts | 60 +- apps/website/e2e/react-mastra-preview.spec.ts | 19 +- .../website/e2e/react-strands-preview.spec.ts | 84 ++- apps/website/playwright.config.ts | 18 +- apps/website/project.json | 10 +- apps/website/src/playwright-config.spec.ts | 30 +- .../angular/e2e/fixtures/aws-strands.json | 14 +- cockpit/runtimes/aws-strands/react/README.md | 2 +- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/playwright.config.ts | 5 +- .../aws-strands/react/e2e/strands.spec.ts | 300 ++++++---- .../runtimes/aws-strands/react/project.json | 59 +- cockpit/runtimes/mastra/react/README.md | 3 +- .../mastra/react/e2e/global-setup-impl.ts | 6 + .../runtimes/mastra/react/e2e/mastra.spec.ts | 142 +++-- .../mastra/react/e2e/playwright.config.ts | 2 + cockpit/runtimes/mastra/react/project.json | 24 +- .../fixtures/microsoft-agent-framework.json | 9 + .../microsoft-agent-framework/react/README.md | 4 +- .../react/e2e/global-setup-impl.ts | 6 + .../react/e2e/maf.spec.ts | 315 ++++++---- .../react/e2e/playwright.config.ts | 5 +- .../react/project.json | 47 +- cockpit/shared/react-aimock-setup.ts | 43 ++ .../2026-11-18-react-cockpit-aimock-design.md | 2 +- .../native-maf-react/configuration.mjs | 16 - .../react-parity/native-maf-react/model.py | 134 ----- .../react-parity/native-maf-react/server.mjs | 163 ------ .../native-maf-react/server.spec.mjs | 86 --- .../react-parity/native-maf-react/service.py | 64 -- .../native-maf-react/test_model.py | 47 -- .../native-mastra-react/model.mjs | 276 --------- .../native-mastra-react/model.spec.mjs | 77 --- .../native-mastra-react/server.mjs | 484 ---------------- .../native-mastra-react/server.spec.mjs | 123 ---- .../native-strands-react/configuration.mjs | 23 - .../native-strands-react/model.py | 281 --------- .../native-strands-react/server.mjs | 211 ------- .../native-strands-react/server.spec.mjs | 254 -------- .../native-strands-react/service.py | 239 -------- .../native-strands-react/test_model.py | 267 --------- scripts/ci-scope.spec.mjs | 2 - scripts/ci-workflow.spec.mjs | 36 +- scripts/cockpit-frontend-matrix.spec.mjs | 14 +- scripts/cockpit-matrix.spec.mjs | 84 +-- scripts/react-cockpit/ag-ui-proxy.mjs | 16 +- scripts/react-cockpit/aimock-backend.mjs | 59 +- scripts/react-cockpit/configuration.mjs | 21 + scripts/react-cockpit/native-maf-fixture.mjs | 272 --------- .../react-cockpit/native-maf-fixture.spec.mjs | 90 --- .../react-cockpit/native-mastra-fixture.mjs | 110 ---- .../react-cockpit/native-strands-fixture.mjs | 335 ----------- .../native-strands-fixture.spec.mjs | 547 ------------------ .../prepare-native-maf-fixture.mjs | 29 - .../prepare-native-maf-fixture.spec.mjs | 58 -- .../prepare-native-mastra-fixture.mjs | 31 - .../prepare-native-mastra-fixture.spec.mjs | 35 -- .../prepare-native-strands-fixture.mjs | 41 -- .../prepare-native-strands-fixture.spec.mjs | 53 -- ...untimes-aws-strands-configuration.spec.mjs | 19 +- .../runtimes-mastra-configuration.spec.mjs | 13 + ...oft-agent-framework-configuration.spec.mjs | 16 +- scripts/react-cockpit/serve.mjs | 61 +- 64 files changed, 887 insertions(+), 5029 deletions(-) create mode 100644 cockpit/runtimes/aws-strands/react/e2e/global-setup-impl.ts create mode 100644 cockpit/runtimes/mastra/react/e2e/global-setup-impl.ts create mode 100644 cockpit/runtimes/microsoft-agent-framework/react/e2e/global-setup-impl.ts delete mode 100644 fixtures/react-parity/native-maf-react/configuration.mjs delete mode 100644 fixtures/react-parity/native-maf-react/model.py delete mode 100644 fixtures/react-parity/native-maf-react/server.mjs delete mode 100644 fixtures/react-parity/native-maf-react/server.spec.mjs delete mode 100644 fixtures/react-parity/native-maf-react/service.py delete mode 100644 fixtures/react-parity/native-maf-react/test_model.py delete mode 100644 fixtures/react-parity/native-mastra-react/model.mjs delete mode 100644 fixtures/react-parity/native-mastra-react/model.spec.mjs delete mode 100644 fixtures/react-parity/native-mastra-react/server.mjs delete mode 100644 fixtures/react-parity/native-mastra-react/server.spec.mjs delete mode 100644 fixtures/react-parity/native-strands-react/configuration.mjs delete mode 100644 fixtures/react-parity/native-strands-react/model.py delete mode 100644 fixtures/react-parity/native-strands-react/server.mjs delete mode 100644 fixtures/react-parity/native-strands-react/server.spec.mjs delete mode 100644 fixtures/react-parity/native-strands-react/service.py delete mode 100644 fixtures/react-parity/native-strands-react/test_model.py delete mode 100644 scripts/react-cockpit/native-maf-fixture.mjs delete mode 100644 scripts/react-cockpit/native-maf-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/native-mastra-fixture.mjs delete mode 100644 scripts/react-cockpit/native-strands-fixture.mjs delete mode 100644 scripts/react-cockpit/native-strands-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/prepare-native-maf-fixture.mjs delete mode 100644 scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/prepare-native-mastra-fixture.mjs delete mode 100644 scripts/react-cockpit/prepare-native-mastra-fixture.spec.mjs delete mode 100644 scripts/react-cockpit/prepare-native-strands-fixture.mjs delete mode 100644 scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b8ac0647..f44c9deed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -176,7 +176,7 @@ jobs: cache: npm - run: npm ci - name: Test CI scope classifier - run: node --test scripts/configure-ci-apt.spec.mjs scripts/install-playwright-browser.spec.mjs scripts/commit-staged-via-api.spec.mjs scripts/ci-queue-evidence.spec.mjs scripts/ci-scope.spec.mjs scripts/cockpit-dependencies.spec.mjs scripts/cockpit-matrix.spec.mjs scripts/cockpit-frontend-matrix.spec.mjs scripts/cockpit-ports.spec.mjs scripts/cockpit-runtime-bridge-coverage.spec.mjs scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs scripts/react-cockpit/chat-theming-configuration.spec.mjs scripts/react-cockpit/consumer-preparation.spec.mjs scripts/website-angular-startup.spec.mjs scripts/website-agents-startup.spec.mjs + run: node --test scripts/configure-ci-apt.spec.mjs scripts/install-playwright-browser.spec.mjs scripts/commit-staged-via-api.spec.mjs scripts/ci-queue-evidence.spec.mjs scripts/ci-scope.spec.mjs scripts/cockpit-dependencies.spec.mjs scripts/cockpit-matrix.spec.mjs scripts/cockpit-frontend-matrix.spec.mjs scripts/cockpit-ports.spec.mjs scripts/cockpit-runtime-bridge-coverage.spec.mjs scripts/react-cockpit/ag-ui-client-tools-configuration.spec.mjs scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs scripts/react-cockpit/chat-theming-configuration.spec.mjs scripts/react-cockpit/consumer-preparation.spec.mjs scripts/website-angular-startup.spec.mjs scripts/website-agents-startup.spec.mjs - name: Test Debug static configuration without Python workers run: node --test --test-name-pattern='^debug ' scripts/react-cockpit/chat-debug-configuration.spec.mjs - name: Validate CI workflow guards @@ -1131,18 +1131,6 @@ jobs: writeFileSync(join(retain, 'provenance.json'), JSON.stringify(provenance, null, 2) + '\n', { flag: 'wx' }); NODE timeout 1200s node --test --test-concurrency=1 --test-timeout=120000 scripts/react-cockpit/ag-ui-a2ui-configuration.spec.mjs scripts/react-cockpit/ag-ui-a2ui-preparation.spec.mjs scripts/react-cockpit/ag-ui-a2ui-native-probe.spec.mjs - - name: Verify native MAF fixture ownership - if: matrix.cap.angular == 'cockpit-runtimes-microsoft-agent-framework-react' - run: | - npx nx run cockpit-runtimes-microsoft-agent-framework-react:prepare-fixture --skip-nx-cache - node --test --test-concurrency=1 scripts/react-cockpit/native-maf-fixture.spec.mjs fixtures/react-parity/native-maf-react/server.spec.mjs - python -m unittest discover -s fixtures/react-parity/native-maf-react -p test_model.py - - name: Verify native Strands fixture ownership - if: matrix.cap.angular == 'cockpit-runtimes-aws-strands-react' - run: | - npx nx run cockpit-runtimes-aws-strands-react:prepare-fixture --skip-nx-cache - node --test --test-concurrency=1 scripts/react-cockpit/native-strands-fixture.spec.mjs fixtures/react-parity/native-strands-react/server.spec.mjs - python -m unittest discover -s fixtures/react-parity/native-strands-react -p test_model.py - name: Verify native Debug fixture and installed session ownership if: matrix.cap.angular == 'cockpit-chat-debug-react' run: | diff --git a/apps/website/e2e/react-maf-preview.spec.ts b/apps/website/e2e/react-maf-preview.spec.ts index 28c27bd78..cf906a9e4 100644 --- a/apps/website/e2e/react-maf-preview.spec.ts +++ b/apps/website/e2e/react-maf-preview.spec.ts @@ -1,14 +1,18 @@ import { selectWorkspaceMode } from './fixtures/workspace-controls'; import { writeFile } from 'node:fs/promises'; -import { test, expect, type Page, type Locator } from '@playwright/test'; +import { test, expect, type Page, type Locator, type APIRequestContext } from '@playwright/test'; const origin = 'http://127.0.0.1:4642'; const approved = 'Expense recorded: $220.00 to Blue Finch Bistro (meals) — queued for reimbursement.'; const rejected = 'Error: Tool call invocation was rejected by user.'; const browserErrors = new WeakMap(); -function events(base64: string) { - return [...Buffer.from(base64, 'base64').toString().matchAll(/^data: (.+)$/gm)].map(match => JSON.parse(match[1])); -} +// The preview's backend: aimock replays cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures +// into the example's real Agent Framework AG-UI server, behind the recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs): /__requests lists each agent POST with the +// events the server streamed (`events`) and what the browser got (`sent`). +const runs = async (request: APIRequestContext) => (await request.get(origin + '/__requests')).json(); +const model = async (request: APIRequestContext) => (await request.get(origin + '/__model-requests')).json(); +const submitted = (run: { events: { type: string; toolCallId?: string }[] }) => run.events.filter(event => event.type === 'TOOL_CALL_RESULT' && event.toolCallId === 'c0_submit_expense'); async function mode(page: Page, name: 'Docs' | 'Code' | 'Run') { await selectWorkspaceMode(page, name); } @@ -23,26 +27,22 @@ async function fits(locator: Locator) { test.describe('native MAF Website', () => { test.describe.configure({ mode: 'serial' }); - test.skip(!!process.env['BASE_URL'], 'Owned actual-service fixture is local only.'); + test.skip(!!process.env['BASE_URL'], 'The aimock-backed preview is local only.'); test.beforeEach(async ({ page, request }) => { const errors: string[] = []; browserErrors.set(page, errors); page.on('pageerror', error => errors.push(error.message)); page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); }); - expect((await request.post(origin + '/__reset', { data: { scenario: 'approval' } })).ok()).toBe(true); + expect((await request.post(origin + '/__reset')).ok()).toBe(true); }); test.afterEach(async ({ page, request }, info) => { await writeFile(info.outputPath('browser-errors.json'), JSON.stringify(browserErrors.get(page), null, 2)); await writeFile(info.outputPath('dom.html'), await page.content()); await page.screenshot({ path: info.outputPath('final-page.png') }); await page.goto('about:blank'); - // These receipts establish owned fixture closure after page exit. The - // connection integration spec separately proves synchronous native abort - // and revoked retained callbacks on unmount. - await writeFile(info.outputPath('actual-service-evidence.json'), JSON.stringify(await (await request.get(origin + '/__stats')).json(), null, 2)); - const receipt = await (await request.post(origin + '/__finish')).json(); - await writeFile(info.outputPath('owned-cleanup.json'), JSON.stringify(receipt, null, 2)); - for (const owner of receipt.closed) expect(Object.values(owner.closure).every(Boolean)).toBe(true); + // The connection integration spec proves synchronous native abort and + // revoked retained callbacks on unmount. + await writeFile(info.outputPath('actual-service-evidence.json'), JSON.stringify({ runs: await runs(request), model: await model(request) }, null, 2)); }); for (const width of [1280, 390]) for (const decision of ['Approve', 'Decline'] as const) { test(`Docs Code Run and actual native ${decision} at ${width}`, async ({ page, request }, info) => { @@ -85,21 +85,21 @@ test.describe('native MAF Website', () => { expect(await page.locator('iframe').evaluate((current, retained) => current === retained, iframe)).toBe(true); await expect(frame.getByRole('textbox', { name: 'Message', exact: true })).toHaveValue(draft); await expect(frame.getByRole('status')).toHaveText('Ready.'); - expect((await (await request.get(origin + '/__stats')).json()).current.requests).toHaveLength(0); + expect(await runs(request)).toHaveLength(0); await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Awaiting your decision.'); for (const control of [frame.getByRole('button', { name: 'Approve', exact: true }), frame.getByRole('button', { name: 'Decline', exact: true }), frame.getByRole('textbox', { name: 'Message', exact: true }), frame.getByRole('status')]) await fits(control); await expect(frame.locator('.tp-approval-card')).toContainText('Blue Finch Bistro'); await fits(frame.locator('.tp-approval-card')); await page.screenshot({ path: info.outputPath('approval.png') }); - const before = (await (await request.get(origin + '/__stats')).json()).current; - expect(before.requests).toHaveLength(1); - const initial = before.requests[0]; - expect(initial.upstreamBase64).toBe(initial.sentBase64); - const pause = events(initial.upstreamBase64).find(event => event.type === 'RUN_FINISHED'); + const before = await runs(request); + expect(before).toHaveLength(1); + const initial = before[0]; + expect(initial.sent).toEqual(initial.events); + const pause = initial.events.find((event: { type: string }) => event.type === 'RUN_FINISHED'); expect(pause).toMatchObject({ runId: initial.body.runId, threadId: initial.body.threadId, outcome: { type: 'interrupt' } }); expect(pause.outcome.interrupts).toHaveLength(1); - expect(before.observed.entries).toBe(0); + expect(submitted(initial)).toHaveLength(0); await frame.getByRole('button', { name: decision, exact: true }).click(); await expect(frame.getByRole('status')).toHaveText(decision === 'Approve' ? 'Fictional expense approved.' : 'Expense declined.'); const literal = decision === 'Approve' ? approved : rejected; @@ -107,19 +107,19 @@ test.describe('native MAF Website', () => { await expect(result).toHaveText(literal); for (const control of [result, frame.getByRole('textbox', { name: 'Message', exact: true }), frame.getByRole('button', { name: 'Send', exact: true }), frame.getByRole('button', { name: 'New conversation', exact: true }), frame.getByRole('status')]) await fits(control); await page.screenshot({ path: info.outputPath('result.png') }); - const after = (await (await request.get(origin + '/__stats')).json()).current; - expect(after.requests).toHaveLength(2); - const resumed = after.requests[1]; + const after = await runs(request); + expect(after).toHaveLength(2); + const resumed = after[1]; expect(resumed.body.threadId).toBe(initial.body.threadId); expect(resumed.body.resume).toEqual([{ interruptId: pause.outcome.interrupts[0].id, status: 'resolved', payload: { approved: decision === 'Approve' } }]); expect(resumed.body.forwardedProps).not.toHaveProperty('command'); - expect(resumed.upstreamBase64).toBe(resumed.sentBase64); - expect(events(resumed.upstreamBase64).find(event => event.type === 'RUN_FINISHED')).toMatchObject({ runId: resumed.body.runId, threadId: resumed.body.threadId }); - expect(after.observed.entries).toBe(decision === 'Approve' ? 1 : 0); - expect(after.observed.model.httpSeen).toBe(3); - expect(after.observed.model.records[2].actualContinuationToolContent).toBe(literal); - expect(after.observed.profile.kind).toContain('no body replacement'); - for (const module of Object.values(after.observed.modules) as { sha256: string }[]) expect(module.sha256).toMatch(/^[a-f0-9]{64}$/); + expect(resumed.sent).toEqual(resumed.events); + expect(resumed.events.find((event: { type: string }) => event.type === 'RUN_FINISHED')).toMatchObject({ runId: resumed.body.runId, threadId: resumed.body.threadId }); + // The real submit_expense ran only when approved. + expect(submitted(resumed)).toHaveLength(decision === 'Approve' ? 1 : 0); + const calls = await model(request); + expect(calls).toHaveLength(3); + expect(calls[2].body.messages.filter((row: { role: string; tool_call_id?: string }) => row.role === 'tool' && row.tool_call_id === 'c0_submit_expense').map((row: { content: string }) => row.content)).toEqual([literal]); }); } }); diff --git a/apps/website/e2e/react-mastra-preview.spec.ts b/apps/website/e2e/react-mastra-preview.spec.ts index 7162f6d7c..eb465e7da 100644 --- a/apps/website/e2e/react-mastra-preview.spec.ts +++ b/apps/website/e2e/react-mastra-preview.spec.ts @@ -1,17 +1,18 @@ import { documentationModes } from './fixtures/workspace-controls'; import { writeFile } from 'node:fs/promises'; -import { test, expect } from '@playwright/test'; +import { test, expect, type APIRequestContext } from '@playwright/test'; const origin = 'http://127.0.0.1:4641'; +// The preview's backend: aimock replays cockpit/runtimes/mastra/angular/e2e/fixtures into the +// real deployments/ag-ui-mastra service, behind the recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs), which lists each agent POST at /__requests. +const runs = async (request: APIRequestContext) => (await request.get(origin + '/__requests')).json(); test.describe('native Mastra Website', () => { - test.skip(!!process.env['BASE_URL'], 'Owned actual-service fixture is local only.'); - test.beforeEach(async ({ request }) => { await request.post(origin + '/__reset', { data: { mode: 'normal' } }); }); + test.skip(!!process.env['BASE_URL'], 'The aimock-backed preview is local only.'); + test.beforeEach(async ({ request }) => { await request.post(origin + '/__reset'); }); test.afterEach(async ({ page, request }, info) => { await page.goto('about:blank'); - await writeFile(info.outputPath('private-actual-service-evidence.json'), JSON.stringify(await (await request.get(origin + '/__stats')).json(), null, 2)); - const receipt = await (await request.get(origin + '/__finish')).json(); - await writeFile(info.outputPath('private-owned-cleanup.json'), JSON.stringify(receipt, null, 2)); - for (const owner of receipt.closed) expect(Object.values(owner.closure).every(Boolean)).toBe(true); + await writeFile(info.outputPath('private-actual-service-evidence.json'), JSON.stringify({ runs: await runs(request), model: await (await request.get(origin + '/__model-requests')).json() }, null, 2)); }); test('Docs Code and Run select the authored reservation implementation', async ({ page, request }, info) => { await page.goto('/docs/react/runtimes/mastra/overview'); @@ -36,14 +37,14 @@ test.describe('native Mastra Website', () => { const frame = page.frameLocator('iframe'); await expect(frame.getByRole('status')).toHaveText('Ready.'); await frame.getByRole('button', { name: 'Reserve the campsite', exact: true }).click(); - expect((await (await request.get(origin + '/__stats')).json()).current.requests).toHaveLength(0); + expect(await runs(request)).toHaveLength(0); await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('button', { name: 'Approve', exact: true })).toBeEnabled(); await expect(frame.getByRole('region', { name: 'Reservation approval' })).toContainText('North Pines'); await frame.getByRole('button', { name: 'Approve', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Fictional reservation approved.'); await expect(frame.getByRole('region', { name: 'Observed backend result' }).locator('pre')).toHaveText(JSON.stringify('Reserved North Pines for 2 night(s) — total $90. Confirmation TP-0288.')); - const wire = (await (await request.get(origin + '/__stats')).json()).current.requests; + const wire = await runs(request); expect(wire).toHaveLength(2); expect(wire[1].body.resume[0]).toMatchObject({ status: 'resolved', payload: { approved: true } }); expect(wire[1].body.forwardedProps).not.toHaveProperty('command'); diff --git a/apps/website/e2e/react-strands-preview.spec.ts b/apps/website/e2e/react-strands-preview.spec.ts index f4cba8847..e64426cfd 100644 --- a/apps/website/e2e/react-strands-preview.spec.ts +++ b/apps/website/e2e/react-strands-preview.spec.ts @@ -1,14 +1,17 @@ import { selectWorkspaceMode } from './fixtures/workspace-controls'; import { writeFile } from 'node:fs/promises'; -import { test, expect, type Page, type Locator } from '@playwright/test'; +import { test, expect, type Page, type Locator, type APIRequestContext } from '@playwright/test'; const origin = 'http://127.0.0.1:4643'; const approved = 'Meeting booked for Tuesday 10:00: Q3 roadmap review'; const rejected = 'The human declined. Meeting NOT booked: Q3 roadmap review'; const browserErrors = new WeakMap(); -function events(base64: string) { - return [...Buffer.from(base64, 'base64').toString().matchAll(/^data: (.+)$/gm)].map(match => JSON.parse(match[1])); -} +// The preview's backend: aimock replays cockpit/runtimes/aws-strands/angular/e2e/fixtures +// into the example's real Strands AG-UI server, behind the recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs): /__requests lists each agent POST with the +// events the server streamed (`events`) and what the browser got (`sent`). +const runs = async (request: APIRequestContext) => (await request.get(origin + '/__requests')).json(); +const model = async (request: APIRequestContext) => (await request.get(origin + '/__model-requests')).json(); async function mode(page: Page, name: 'Docs' | 'Code' | 'Run') { await selectWorkspaceMode(page, name); } @@ -23,27 +26,20 @@ async function fits(locator: Locator) { test.describe('native Strands Website', () => { test.describe.configure({ mode: 'serial' }); - test.skip(!!process.env['BASE_URL'], 'Owned actual-service fixture is local only.'); + test.skip(!!process.env['BASE_URL'], 'The aimock-backed preview is local only.'); test.beforeEach(async ({ page, request }) => { const errors: string[] = []; browserErrors.set(page, errors); page.on('pageerror', error => errors.push(error.message)); page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); }); - expect((await request.post(origin + '/__reset', { data: { scenario: 'approval' } })).ok()).toBe(true); + expect((await request.post(origin + '/__reset')).ok()).toBe(true); }); test.afterEach(async ({ page, request }, info) => { - try { - await writeFile(info.outputPath('browser-errors.json'), JSON.stringify(browserErrors.get(page), null, 2)); - await writeFile(info.outputPath('dom.html'), await page.content()); - await page.screenshot({ path: info.outputPath('final-page.png') }); - await page.goto('about:blank'); - await writeFile(info.outputPath('actual-service-evidence.json'), JSON.stringify(await (await request.get(origin + '/__stats')).json(), null, 2)); - } finally { - const receipt = await (await request.post(origin + '/__finish')).json(); - await writeFile(info.outputPath('owned-cleanup.json'), JSON.stringify(receipt, null, 2)); - expect(receipt.closed.length).toBeGreaterThan(0); - for (const owner of receipt.closed) expect(Object.values(owner.closure).every(Boolean)).toBe(true); - } + await writeFile(info.outputPath('browser-errors.json'), JSON.stringify(browserErrors.get(page), null, 2)); + await writeFile(info.outputPath('dom.html'), await page.content()); + await page.screenshot({ path: info.outputPath('final-page.png') }); + await page.goto('about:blank'); + await writeFile(info.outputPath('actual-service-evidence.json'), JSON.stringify({ runs: await runs(request), model: await model(request) }, null, 2)); expect(browserErrors.get(page)).toEqual([]); }); for (const width of [1280, 390]) for (const decision of ['Approve', 'Decline'] as const) { @@ -85,21 +81,21 @@ test.describe('native Strands Website', () => { expect(await page.locator('iframe').evaluate((element, original) => element === original, iframe)).toBe(true); } await expect(frame.getByRole('textbox', { name: 'Message', exact: true })).toHaveValue(draft); - expect((await (await request.get(origin + '/__stats')).json()).current.requests).toHaveLength(0); + expect(await runs(request)).toHaveLength(0); await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Awaiting your decision.'); for (const control of [frame.getByRole('button', { name: 'Approve', exact: true }), frame.getByRole('button', { name: 'Decline', exact: true }), frame.getByRole('textbox', { name: 'Message', exact: true }), frame.getByRole('status')]) await fits(control); await expect(frame.locator('.tp-approval-card')).toContainText('Q3 roadmap review'); await fits(frame.locator('.tp-approval-card')); await page.screenshot({ path: info.outputPath('approval.png') }); - const before = (await (await request.get(origin + '/__stats')).json()).current; - expect(before.requests).toHaveLength(1); - const initial = before.requests[0]; - expect(initial.upstreamBase64).toBe(initial.sentBase64); - const pause = events(initial.upstreamBase64).find(event => event.type === 'RUN_FINISHED'); + const before = await runs(request); + expect(before).toHaveLength(1); + const initial = before[0]; + expect(initial.sent).toEqual(initial.events); + const pause = initial.events.find((event: { type: string }) => event.type === 'RUN_FINISHED'); expect(pause).toMatchObject({ runId: initial.body.runId, threadId: initial.body.threadId, outcome: { type: 'interrupt' } }); expect(pause.outcome.interrupts).toHaveLength(1); - expect(before.observed.completedReturns).toHaveLength(0); + expect(initial.events.some((event: { type: string; content?: string }) => event.type === 'TOOL_CALL_RESULT' && /booked/i.test(event.content ?? ''))).toBe(false); expect(pause.outcome.interrupts[0]).toMatchObject({ reason: 'book_meeting', metadata: { reason: { topic: 'Q3 roadmap review', slot: 'Tuesday 10:00' } } }); await frame.getByRole('button', { name: decision, exact: true }).click(); await expect(frame.getByRole('status')).toHaveText(decision === 'Approve' ? 'Fictional meeting booked.' : 'Meeting declined.'); @@ -108,34 +104,31 @@ test.describe('native Strands Website', () => { await expect(result).toHaveText(literal); for (const control of [result, frame.getByRole('textbox', { name: 'Message', exact: true }), frame.getByRole('button', { name: 'Send', exact: true }), frame.getByRole('button', { name: 'New conversation', exact: true }), frame.getByRole('status')]) await fits(control); await page.screenshot({ path: info.outputPath('result.png') }); - const after = (await (await request.get(origin + '/__stats')).json()).current; - expect(after.requests).toHaveLength(2); - const resumed = after.requests[1]; + const after = await runs(request); + expect(after).toHaveLength(2); + const resumed = after[1]; expect(resumed.body.threadId).toBe(initial.body.threadId); expect(resumed.body.resume).toEqual([{ interruptId: pause.outcome.interrupts[0].id, status: 'resolved', payload: { approved: decision === 'Approve' } }]); expect(resumed.body.forwardedProps).not.toHaveProperty('command'); - expect(resumed.upstreamBase64).toBe(resumed.sentBase64); - expect(events(resumed.upstreamBase64).find(event => event.type === 'RUN_FINISHED')).toMatchObject({ runId: resumed.body.runId, threadId: resumed.body.threadId }); - expect(after.observed.completedReturns).toHaveLength(1); + expect(resumed.sent).toEqual(resumed.events); + expect(resumed.events.find((event: { type: string }) => event.type === 'RUN_FINISHED')).toMatchObject({ runId: resumed.body.runId, threadId: resumed.body.threadId }); expect(resumed.body.runId).not.toBe(initial.body.runId); - const canonical = events(resumed.upstreamBase64).filter(event => event.type === 'MESSAGES_SNAPSHOT').at(-1).messages; + const canonical = resumed.events.filter((event: { type: string }) => event.type === 'MESSAGES_SNAPSHOT').at(-1).messages; const bookingCall = canonical.flatMap((row: { toolCalls?: { id: string; function: { name: string } }[] }) => row.toolCalls ?? []).find((call: { function: { name: string } }) => call.function.name === 'book_meeting'); const results = canonical.filter((row: { role: string; toolCallId?: string }) => row.role === 'tool' && row.toolCallId === bookingCall.id); expect(results).toHaveLength(1); expect(results[0].content).toBe(JSON.stringify(literal)); await expect(frame.getByRole('region', { name: 'Last observed schedule' })).toContainText('Observed booking status: pending'); - expect(after.observed.model.httpSeen).toBe(3); - const toolResults = after.observed.model.records.at(-1).body.messages.filter((row: { role: string; tool_call_id?: string }) => row.role === 'tool' && row.tool_call_id === bookingCall.id); + const calls = await model(request); + expect(calls).toHaveLength(3); + const toolResults = calls.at(-1).body.messages.filter((row: { role: string; tool_call_id?: string }) => row.role === 'tool' && row.tool_call_id === bookingCall.id); expect(toolResults).toHaveLength(1); expect(toolResults[0].content).toBe(literal); - - for (const module of Object.values(after.observed.modules) as { sha256: string }[]) expect(module.sha256).toMatch(/^[a-f0-9]{64}$/); }); } for (const width of [1280, 390]) { test(`unconfirmed native result preserves revoked authority across modes at ${width}`, async ({ page, request }) => { test.setTimeout(90000); - expect((await request.post(origin + '/__reset', { data: { scenario: 'missing-terminal' } })).ok()).toBe(true); await page.setViewportSize({ width, height: width === 390 ? 844 : 900 }); await page.goto('/docs/react/runtimes/aws-strands/overview'); await expect(page.locator('[data-workspace-shell]')).toHaveAttribute('data-hydrated', 'true', { timeout: 30000 }); @@ -145,22 +138,25 @@ test.describe('native Strands Website', () => { await frame.getByRole('button', { name: 'Q3 roadmap review', exact: true }).click(); await frame.getByRole('button', { name: 'Send', exact: true }).click(); await expect(frame.getByRole('button', { name: 'Approve', exact: true })).toBeEnabled(); + // Injected data: the proxy drops the resumed run's real terminal on its way to the browser. + expect((await request.post(origin + '/__stream/edit', { data: { ops: [{ type: 'RUN_FINISHED', op: 'drop' }] } })).ok()).toBe(true); await frame.getByRole('button', { name: 'Approve', exact: true }).click(); await expect(frame.getByRole('status')).toHaveText('Meeting unconfirmed. Start a new conversation.'); await expect(frame.getByRole('region', { name: 'Observed backend result' })).toHaveCount(0); await expect(frame.getByRole('button', { name: 'Send', exact: true })).toBeDisabled(); await expect(frame.getByRole('button', { name: 'Approve', exact: true })).toHaveCount(0); - const evidence = (await (await request.get(origin + '/__stats')).json()).current; - expect(evidence.requests).toHaveLength(2); - expect(evidence.observed.completedReturns).toHaveLength(1); - const resumed = evidence.requests[1]; - expect(events(resumed.upstreamBase64).some(event => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId)).toBe(true); - expect(events(resumed.sentBase64).some(event => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId)).toBe(false); + const evidence = await runs(request); + expect(evidence).toHaveLength(2); + const resumed = evidence[1]; + // The real booking completed upstream; only the browser lost the terminal. + expect(resumed.events.some((event: { type: string; content?: string }) => event.type === 'TOOL_CALL_RESULT' && (event.content ?? '').includes(approved))).toBe(true); + expect(resumed.events.some((event: { type: string; runId?: string }) => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId)).toBe(true); + expect(resumed.sent.some((event: { type: string; runId?: string }) => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId)).toBe(false); await mode(page, 'Docs'); await expect(page.locator('iframe')).toHaveCount(1); await mode(page, 'Run'); await expect(page.frameLocator('iframe').getByRole('status')).toHaveText('Meeting unconfirmed. Start a new conversation.'); - expect((await (await request.get(origin + '/__stats')).json()).current.requests).toHaveLength(2); + expect(await runs(request)).toHaveLength(2); }); } }); diff --git a/apps/website/playwright.config.ts b/apps/website/playwright.config.ts index bfdc169f9..f37d77db4 100644 --- a/apps/website/playwright.config.ts +++ b/apps/website/playwright.config.ts @@ -364,23 +364,23 @@ export const createWebsitePlaywrightConfig = ( timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent', + command: 'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4641', + url: 'http://127.0.0.1:4641/__ready', reuseExistingServer: false, timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent', + command: 'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4642', + url: 'http://127.0.0.1:4642/__ready', reuseExistingServer: false, timeout: 180_000, }, { - command: 'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent', + command: 'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent --backend', cwd: '../..', - url: 'http://127.0.0.1:4643', + url: 'http://127.0.0.1:4643/__ready', reuseExistingServer: false, timeout: 180_000, }, @@ -572,7 +572,7 @@ export const createWebsitePlaywrightConfig = ( config.testMatch = '**/react-mastra-preview.spec.ts'; if (Array.isArray(config.webServer)) { config.webServer = config.webServer.filter(server => - [localURL, 'http://127.0.0.1:4641'].includes(server.url ?? '') + [localURL, 'http://127.0.0.1:4641/__ready'].includes(server.url ?? '') ); } } @@ -584,7 +584,7 @@ export const createWebsitePlaywrightConfig = ( config.outputDir = resolve(__dirname, '../../test-results/native-maf-website'); if (Array.isArray(config.webServer)) { config.webServer = config.webServer.filter(server => - [localURL, 'http://127.0.0.1:4642'].includes(server.url ?? '') + [localURL, 'http://127.0.0.1:4642/__ready'].includes(server.url ?? '') ); } } @@ -596,7 +596,7 @@ export const createWebsitePlaywrightConfig = ( config.outputDir = resolve(__dirname, '../../test-results/native-strands-website'); if (Array.isArray(config.webServer)) { config.webServer = config.webServer.filter(server => - [localURL, 'http://127.0.0.1:4643'].includes(server.url ?? '') + [localURL, 'http://127.0.0.1:4643/__ready'].includes(server.url ?? '') ); } } diff --git a/apps/website/project.json b/apps/website/project.json index 9372127c0..07eddd3e7 100644 --- a/apps/website/project.json +++ b/apps/website/project.json @@ -256,14 +256,6 @@ "executor": "@nx/playwright:playwright", "cache": false, "dependsOn": [ - { - "target": "prepare-fixture", - "projects": [ - "cockpit-runtimes-mastra-react", - "cockpit-runtimes-microsoft-agent-framework-react", - "cockpit-runtimes-aws-strands-react" - ] - }, { "target": "build", "projects": [ @@ -327,7 +319,7 @@ }, { "target": "prepare-fixture", - "projects": ["cockpit-runtimes-mastra-react", "cockpit-runtimes-microsoft-agent-framework-react", "cockpit-runtimes-aws-strands-react", "cockpit-ag-ui-a2ui-react"] + "projects": ["cockpit-ag-ui-a2ui-react"] }, { "target": "build", diff --git a/apps/website/src/playwright-config.spec.ts b/apps/website/src/playwright-config.spec.ts index 6f95e99e5..02acc502d 100644 --- a/apps/website/src/playwright-config.spec.ts +++ b/apps/website/src/playwright-config.spec.ts @@ -92,39 +92,41 @@ it('scopes Chat Theming Website to two owned servers with zero retries and focus expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'test').projects).toContain('cockpit-chat-theming-python'); }); -it('scopes native MAF Website checks to the owned fixture after frozen preparation and build', () => { +it('scopes native MAF Website checks to the aimock-backed preview after its build', () => { const config = createWebsitePlaywrightConfig({ WEBSITE_E2E_REACT_TOPIC: 'runtimes-microsoft-agent-framework', WEBSITE_E2E_PORT: '4390', PLAYWRIGHT_REUSE_EXISTING_SERVER: 'true' }); expect(config.testMatch).toBe('**/react-maf-preview.spec.ts'); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4642']); - expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent', reuseExistingServer: false }); + expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4642/__ready']); + expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent --backend', reuseExistingServer: false }); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); const name = 'cockpit-runtimes-microsoft-agent-framework-react'; expect(project.implicitDependencies).toContain(name); - for (const target of ['build', 'prepare-fixture']) expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === target).projects).toContain(name); + expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'build').projects).toContain(name); + expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'prepare-fixture').projects).not.toContain(name); }); -it('scopes native Strands Website checks to the owned fixture after frozen preparation and build', () => { +it('scopes native Strands Website checks to the aimock-backed preview after its build', () => { const config = createWebsitePlaywrightConfig({ WEBSITE_E2E_REACT_TOPIC: 'runtimes-aws-strands', WEBSITE_E2E_PORT: '4390', PLAYWRIGHT_REUSE_EXISTING_SERVER: 'true' }); expect(config.workers).toBe(1); expect(config.fullyParallel).toBe(false); expect(config.retries).toBe(0); expect(config.testMatch).toBe('**/react-strands-preview.spec.ts'); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4643']); - expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent', reuseExistingServer: false }); + expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4643/__ready']); + expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent --backend', reuseExistingServer: false }); const project = JSON.parse(readFileSync(resolve(import.meta.dirname, '../project.json'), 'utf8')); const name = 'cockpit-runtimes-aws-strands-react'; expect(project.implicitDependencies).toContain(name); - for (const target of ['build', 'prepare-fixture']) expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === target).projects).toContain(name); + expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'build').projects).toContain(name); + expect(project.targets.e2e.dependsOn.find((dependency: { target?: string }) => dependency.target === 'prepare-fixture').projects).not.toContain(name); }); -it('scopes native Mastra Website checks to the owned actual service fixture', () => { +it('scopes native Mastra Website checks to the aimock-backed preview', () => { const config = createWebsitePlaywrightConfig({ WEBSITE_E2E_REACT_TOPIC: 'runtimes-mastra', WEBSITE_E2E_PORT: '4390' }); expect(config.testMatch).toBe('**/react-mastra-preview.spec.ts'); const servers = Array.isArray(config.webServer) ? config.webServer : []; - expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4641']); - expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent', reuseExistingServer: false }); + expect(servers.map(server => server.url)).toEqual(['http://127.0.0.1:4390', 'http://127.0.0.1:4641/__ready']); + expect(servers[1]).toMatchObject({ command: 'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent --backend', reuseExistingServer: false }); }); describe('Website Playwright configuration', () => { @@ -507,9 +509,9 @@ describe('Website Playwright configuration', () => { url: 'http://127.0.0.1:4611/__ready', }), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-client-tools --no-parent --backend',url:'http://127.0.0.1:4640/__ready'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent',url:'http://127.0.0.1:4641'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent',url:'http://127.0.0.1:4642'}), - expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent',url:'http://127.0.0.1:4643'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent --backend',url:'http://127.0.0.1:4641/__ready'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent --backend',url:'http://127.0.0.1:4642/__ready'}), + expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent --backend',url:'http://127.0.0.1:4643/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-json-render --no-parent --backend',url:'http://127.0.0.1:4612/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs ag-ui-subagents --no-parent --backend',url:'http://127.0.0.1:4613/__ready'}), expect.objectContaining({command:'node scripts/react-cockpit/serve.mjs render-spec-rendering --no-parent',url:'http://127.0.0.1:4614'}), diff --git a/cockpit/runtimes/aws-strands/angular/e2e/fixtures/aws-strands.json b/cockpit/runtimes/aws-strands/angular/e2e/fixtures/aws-strands.json index 5c93c8bc5..5b73acb95 100644 --- a/cockpit/runtimes/aws-strands/angular/e2e/fixtures/aws-strands.json +++ b/cockpit/runtimes/aws-strands/angular/e2e/fixtures/aws-strands.json @@ -1,13 +1,21 @@ { "fixtures": [ + {"match": {"userMessage": "[scenario:batch]", "toolResultContains": "Meeting booked for"}, "response": {"content": "The Q3 roadmap review booking decision is complete."}}, + {"match": {"userMessage": "[scenario:batch]", "toolResultContains": "Meeting NOT booked"}, "response": {"content": "The Q3 roadmap review booking decision is complete."}}, + {"match": {"userMessage": "[scenario:batch]"}, "response": {"toolCalls": [{"name": "check_availability", "arguments": "{\"day\": \"Tuesday\"}"}, {"name": "book_meeting", "arguments": "{\"topic\": \"Q3 roadmap review\", \"slot\": \"Tuesday 10:00\"}"}]}}, + {"match": {"userMessage": "[scenario:cancel]"}, "response": {"toolCalls": [{"name": "check_availability", "arguments": "{\"day\": \"Tuesday\"}"}]}, "latency": 30000}, + {"match": {"userMessage": "fictional demo meeting", "toolResultContains": "Meeting booked for"}, "response": {"content": "The Q3 roadmap review booking decision is complete."}}, + {"match": {"userMessage": "fictional demo meeting", "toolResultContains": "Meeting NOT booked"}, "response": {"content": "The Q3 roadmap review booking decision is complete."}}, + {"match": {"userMessage": "fictional demo meeting", "hasToolResult": true}, "response": {"toolCalls": [{"name": "book_meeting", "arguments": "{\"topic\": \"Q3 roadmap review\", \"slot\": \"Tuesday 10:00\"}"}]}}, + {"match": {"userMessage": "fictional demo meeting"}, "response": {"toolCalls": [{"name": "check_availability", "arguments": "{\"day\": \"Tuesday\"}"}]}}, { - "match": { "userMessage": "roadmap review", "hasToolResult": true, "turnIndex": 2 }, + "match": { "userMessage": "roadmap review meeting", "hasToolResult": true, "turnIndex": 2 }, "response": { "content": "Your Q3 roadmap review is booked for Tuesday 10:00." } }, { - "match": { "userMessage": "roadmap review", "hasToolResult": true, "turnIndex": 1 }, + "match": { "userMessage": "roadmap review meeting", "hasToolResult": true, "turnIndex": 1 }, "response": { "toolCalls": [ { @@ -21,7 +29,7 @@ } }, { - "match": { "userMessage": "roadmap review" }, + "match": { "userMessage": "roadmap review meeting" }, "response": { "toolCalls": [ { diff --git a/cockpit/runtimes/aws-strands/react/README.md b/cockpit/runtimes/aws-strands/react/README.md index fe1a74eb0..d06353870 100644 --- a/cockpit/runtimes/aws-strands/react/README.md +++ b/cockpit/runtimes/aws-strands/react/README.md @@ -17,4 +17,4 @@ npx nx build cockpit-runtimes-aws-strands-react --skip-nx-cache npx nx e2e cockpit-runtimes-aws-strands-react --skip-nx-cache ``` -The e2e target prepares the isolated Python fixture and builds before serving port 4643. Its loopback model seam drives the unchanged actual Strands service; positive AG-UI bytes are untouched, with original upstream/sent bodies retained as browser evidence. No external model credentials or real bookings are used. Negative terminal scenarios are explicitly corrupted local-only streams. Preparation requires Python 3.12 and the frozen uv environment. Local serving: `node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent` after build (browser tests explicitly reset the fixture before Send). +The e2e target builds, then its global setup runs the example's real Strands server (`cockpit/runtimes/aws-strands/python`, through `uv`) with aimock replaying the model's answers (`cockpit/runtimes/aws-strands/angular/e2e/fixtures`), behind the recording proxy on port 4643. Positive AG-UI bytes are untouched; the proxy keeps what the server streamed and what the browser got as evidence. No external model credentials or real bookings are used. Negative terminal scenarios are explicitly corrupted local-only streams. Local serving with the backend: `node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent --backend` after build. diff --git a/cockpit/runtimes/aws-strands/react/e2e/global-setup-impl.ts b/cockpit/runtimes/aws-strands/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..db390da31 --- /dev/null +++ b/cockpit/runtimes/aws-strands/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'runtimes/aws-strands', port: 4643 }); diff --git a/cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts b/cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts index 3f471bd63..73a6cb020 100644 --- a/cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts +++ b/cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts @@ -4,7 +4,7 @@ export default defineConfig({ testDir: '.', outputDir: resolve( __dirname, - '../../../../../test-results/native-strands-react' + '../../../../../test-results/cockpit-runtimes-aws-strands-react' ), workers: 1, fullyParallel: false, @@ -13,6 +13,9 @@ export default defineConfig({ reporter: 'list', use: { baseURL: 'http://127.0.0.1:4643', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/runtimes/aws-strands/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs runtimes-aws-strands --no-parent', diff --git a/cockpit/runtimes/aws-strands/react/e2e/strands.spec.ts b/cockpit/runtimes/aws-strands/react/e2e/strands.spec.ts index c0cdbceea..09bda66b3 100644 --- a/cockpit/runtimes/aws-strands/react/e2e/strands.spec.ts +++ b/cockpit/runtimes/aws-strands/react/e2e/strands.spec.ts @@ -19,6 +19,7 @@ interface Wire { subagentRunId?: string; toolCallId?: string; content?: string; + snapshot?: unknown; outcome?: { type: string; interrupts?: { id: string; reason: string; metadata: unknown }[]; @@ -32,31 +33,55 @@ interface Wire { toolCalls?: { id: string; function: { name: string; arguments: string } }[]; }[]; } -function events(base64: string): Wire[] { - return [ - ...Buffer.from(base64, 'base64') - .toString() - .matchAll(/^data: (.+)$/gm), - ].map((m) => JSON.parse(m[1])); +// Backend: aimock replays cockpit/runtimes/aws-strands/angular/e2e/fixtures +// (the meeting model's answers, keyed on the React preset prompt) into the +// example's real Strands AG-UI server, behind the recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs). Scenarios are tags the spec +// appends to the preset (`[scenario:batch]`, `[scenario:cancel]`). +/** Every agent POST: { body, events (real), sent (to the browser), closed }. */ +interface Run { + body: { + threadId: string; + runId: string; + resume?: unknown; + messages: { role: string }[]; + }; + events: Wire[]; + sent: Wire[]; + closed: boolean; } -async function stats(request: APIRequestContext) { - return (await request.get('/__stats')).json(); +async function runs(request: APIRequestContext): Promise { + return (await request.get('/__requests')).json(); } -async function start( - page: Page, - request: APIRequestContext, - scenario = 'approval' -) { - expect((await request.post('/__reset', { data: { scenario } })).ok()).toBe( - true - ); +/** What the model received (aimock's request journal). */ +interface ModelCall { + headers: Record; + body: { + messages: { role: string; tool_call_id?: string; content?: string }[]; + }; + response: { status: number; interrupted?: boolean }; +} +async function model(request: APIRequestContext): Promise { + return (await request.get('/__model-requests')).json(); +} +async function start(page: Page, request: APIRequestContext) { + expect((await request.post('/__reset')).ok()).toBe(true); await page.goto(path); await expect(page.getByRole('status')).toHaveText('Ready.'); } -async function send(page: Page) { +async function send(page: Page, scenario = 'approval') { await page .getByRole('button', { name: 'Q3 roadmap review', exact: true }) .click(); + if (scenario !== 'approval') + await page + .getByRole('textbox', { name: 'Message', exact: true }) + .press('End') + .then(() => + page + .getByRole('textbox', { name: 'Message', exact: true }) + .pressSequentially(` [scenario:${scenario}]`) + ); await page.getByRole('button', { name: 'Send', exact: true }).click(); } async function withinViewport(locator: Locator) { @@ -78,16 +103,13 @@ async function withinViewport(locator: Locator) { test.afterEach(async ({ page, request }) => { await writeFile( test.info().outputPath('actual-service-evidence.json'), - JSON.stringify(await stats(request), null, 2) + JSON.stringify( + { runs: await runs(request), model: await model(request) }, + null, + 2 + ) ); await writeFile(test.info().outputPath('dom.html'), await page.content()); - const receipt = await (await request.post('/__finish')).json(); - await writeFile( - test.info().outputPath('owned-cleanup.json'), - JSON.stringify(receipt, null, 2) - ); - for (const owner of receipt.closed) - expect(Object.values(owner.closure).every(Boolean)).toBe(true); }); test('mount, editable preset and New remain lazy until explicit Send', async ({ page, @@ -106,13 +128,13 @@ test('mount, editable preset and New remain lazy until explicit Send', async ({ await expect( page.getByRole('textbox', { name: 'Message', exact: true }) ).toHaveValue(''); - expect((await stats(request)).current.requests).toHaveLength(0); - expect((await stats(request)).current.observed.model.httpSeen).toBe(0); + expect(await runs(request)).toHaveLength(0); + expect(await model(request)).toHaveLength(0); await send(page); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); - expect((await stats(request)).current.requests).toHaveLength(1); + expect(await runs(request)).toHaveLength(1); }); for (const scenario of ['approval', 'batch']) for (const width of [1280, 390]) @@ -125,8 +147,8 @@ for (const scenario of ['approval', 'batch']) width, height: width === 390 ? 844 : 900, }); - await start(page, request, scenario); - await send(page); + await start(page, request); + await send(page, scenario); await expect(page.getByRole('status')).toHaveText( 'Awaiting your decision.' ); @@ -145,10 +167,22 @@ for (const scenario of ['approval', 'batch']) await expect(observed).toContainText( 'Observed booking status: pending' ); + // The panel shows the last snapshot the server sent. Approval looked + // availability up in this run; a batch pauses before its lookup + // result, so its snapshot carries whatever availability the server's + // per-process demo state already held (src/agent.py keeps one state + // for the whole process). + const snapshot = (await runs(request))[0].events + .filter((e) => e.type === 'STATE_SNAPSHOT') + .at(-1)?.snapshot as + | { availability: { slots: string[] } | null } + | undefined; + if (scenario === 'approval') + expect(snapshot?.availability?.slots).toEqual(['10:00', '15:00']); await expect(observed).toContainText( - scenario === 'batch' - ? 'No availability observed yet.' - : '10:00, 15:00' + snapshot?.availability + ? snapshot.availability.slots.join(', ') + : 'No availability observed yet.' ); await expect( page.getByRole('textbox', { name: 'Message', exact: true }) @@ -173,19 +207,18 @@ for (const scenario of ['approval', 'batch']) await card.screenshot({ path: test.info().outputPath('canonical-approval-card.png'), }); - const before = await stats(request), - initial = before.current.requests[0], - terminal = events(initial.upstreamBase64).find( - (e) => e.type === 'RUN_FINISHED' - ); - expect(before.current.requests).toHaveLength(1); - expect(initial.upstreamBase64).toBe(initial.sentBase64); + const before = await runs(request), + initial = before[0], + terminal = initial.events.find((e) => e.type === 'RUN_FINISHED'); + expect(before).toHaveLength(1); + // The browser got exactly what the real server streamed. + expect(initial.sent).toEqual(initial.events); expect(terminal?.threadId).toBe(initial.body.threadId); expect(terminal?.runId).toBe(initial.body.runId); expect(terminal?.outcome?.type).toBe('interrupt'); expect(terminal?.outcome?.interrupts).toHaveLength(1); expect(terminal?.outcome?.interrupts?.[0].reason).toBe('book_meeting'); - const pauseRows = events(initial.upstreamBase64) + const pauseRows = initial.events .filter((e) => e.type === 'MESSAGES_SNAPSHOT') .at(-1)?.messages; expect(pauseRows).toBeDefined(); @@ -200,8 +233,12 @@ for (const scenario of ['approval', 'batch']) expect(pauseRows!.filter((m) => m.role === 'tool')).toHaveLength( scenario === 'batch' ? 0 : 1 ); - expect(before.current.observed.completedReturns).toHaveLength(0); - expect(before.current.observed.interruptedUnwinds).toBe(1); + // Paused: the booking tool has not returned a result yet. + expect( + initial.events.some( + (e) => e.type === 'TOOL_CALL_RESULT' && e.toolCallId === book.id + ) + ).toBe(false); // Two synchronous DOM clicks simulate competing choices before a rerender; only the native owner may claim once. await page .getByRole('button', { name: decision, exact: true }) @@ -258,10 +295,10 @@ for (const scenario of ['approval', 'batch']) await result.screenshot({ path: test.info().outputPath('literal-result-region.png'), }); - const after = await stats(request), - records = after.current.requests; + const records = await runs(request), + calls_ = await model(request); expect(records).toHaveLength(2); - expect(records[1].upstreamBase64).toBe(records[1].sentBase64); + expect(records[1].sent).toEqual(records[1].events); expect(records[1].body.threadId).toBe(initial.body.threadId); expect(records[1].body.runId).not.toBe(initial.body.runId); expect(records[1].body.resume).toEqual([ @@ -271,7 +308,7 @@ for (const scenario of ['approval', 'batch']) payload: { approved: decision === 'Approve' }, }, ]); - const final = events(records[1].upstreamBase64), + const final = records[1].events, end = final.find((e) => e.type === 'RUN_FINISHED'); expect(end?.threadId).toBe(initial.body.threadId); expect(end?.runId).toBe(records[1].body.runId); @@ -291,63 +328,73 @@ for (const scenario of ['approval', 'batch']) expect(found[0].content).toBe( JSON.stringify(literal(decision === 'Approve')) ); - const provider = after.current.observed.model.records - .at(-1) + const provider = calls_ + .at(-1)! .body.messages.filter( - (m: { role: string; tool_call_id?: string }) => - m.role === 'tool' && m.tool_call_id === call.id + (m) => m.role === 'tool' && m.tool_call_id === call.id ); expect(provider).toHaveLength(1); if (call === book) { expect(provider[0].content).toBe(literal(decision === 'Approve')); } else { // Provider JSON and AG-UI JSON have different whitespace serialization. - expect(JSON.parse(provider[0].content)).toEqual( + expect(JSON.parse(provider[0].content!)).toEqual( JSON.parse(found[0].content!) ); } } - expect(after.current.observed.model.httpSeen).toBe( - scenario === 'batch' ? 2 : 3 - ); + expect(calls_).toHaveLength(scenario === 'batch' ? 2 : 3); + // The booking tool returned exactly once, with the decision's literal. expect( - after.current.observed.model.records.at(-1).canonicalContinuation - ).toBe(literal(decision === 'Approve')); - expect(after.current.observed.completedReturns).toHaveLength(1); - expect(after.current.observed.backendState.booking.status).toBe( - decision === 'Approve' ? 'booked' : 'declined' - ); - expect(after.current.observed.observedState.booking.status).toBe( - scenario === 'approval' - ? 'pending' - : decision === 'Approve' - ? 'booked' - : 'declined' - ); + final.filter( + (e) => e.type === 'TOOL_CALL_RESULT' && e.toolCallId === book.id + ) + ).toHaveLength(1); + // No provider retries. expect( - after.current.observed.model.records.every( - (r: { requestHeaders: string[][] }) => - r.requestHeaders.some( - ([k, v]) => - k.toLowerCase() === 'x-stainless-retry-count' && v === '0' - ) + calls_.every( + (r) => r.headers['x-stainless-retry-count'] === '0' ) ).toBe(true); }); -for (const scenario of [ - 'missing-terminal', - 'wrong-terminal', - 'child-only-terminal', -]) +const corruptions: Record = { + 'missing-terminal': [{ type: 'RUN_FINISHED', op: 'drop' }], + 'wrong-terminal': [ + { type: 'RUN_FINISHED', op: 'patch', set: { runId: 'negative-wrong-run' } }, + ], + 'child-only-terminal': [ + { + type: 'RUN_FINISHED', + op: 'patch', + set: { + type: 'SUBAGENT_FINISHED', + subagentRunId: 'negative-child', + outcome: { type: 'suspended' }, + }, + }, + { type: 'RUN_FINISHED', op: 'omit', keys: ['runId', 'threadId'] }, + ], +}; +for (const scenario of Object.keys(corruptions)) test(`negative ${scenario} cannot confirm genuine booking result`, async ({ page, request, }) => { - await start(page, request, scenario); + await start(page, request); await send(page); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); + // Injected data: the proxy corrupts the resumed run's real terminal on + // its way to the browser (dropped, another run's id, or a child-only + // terminal); the real server still finishes the run. + expect( + ( + await request.post('/__stream/edit', { + data: { ops: corruptions[scenario] }, + }) + ).ok() + ).toBe(true); await page.getByRole('button', { name: 'Approve', exact: true }).click(); await expect(page.getByRole('status')).toHaveText( 'Meeting unconfirmed. Start a new conversation.' @@ -361,49 +408,49 @@ for (const scenario of [ await expect( page.getByRole('button', { name: 'Send', exact: true }) ).toBeDisabled(); - const evidence = await stats(request), - resumed = evidence.current.requests[1]; - expect(evidence.current.requests).toHaveLength(2); + const evidence = await runs(request), + resumed = evidence[1]; + expect(evidence).toHaveLength(2); expect( - events(resumed.upstreamBase64).some( + resumed.events.some( (e) => e.type === 'RUN_FINISHED' && e.runId === resumed.body.runId ) ).toBe(true); expect( - events(resumed.sentBase64).some( + resumed.sent.some( (e) => e.type === 'RUN_FINISHED' && e.runId === resumed.body.runId ) ).toBe(false); - expect(evidence.current.observed.completedReturns).toHaveLength(1); + // The real booking still completed upstream. + expect( + resumed.events.some( + (e) => + e.type === 'TOOL_CALL_RESULT' && + e.content === JSON.stringify(literal(true)) + ) + ).toBe(true); }); for (const action of ['Stop', 'New conversation', 'unmount']) - test(`held genuine model ${action} closes actual peer before cleanup`, async ({ + test(`held genuine model ${action} closes the run before cleanup`, async ({ page, request, }) => { - await start(page, request, 'cancel'); - await send(page); - await expect - .poll( - async () => (await stats(request)).current.observed.model.heldActive - ) - .toBe(1); + // The cancel fixture answers only after 30 s, so the model call is in + // flight when the browser acts. + await start(page, request); + await send(page, 'cancel'); + await expect.poll(async () => (await model(request)).length).toBe(1); if (action === 'unmount') await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); else await page.getByRole('button', { name: action, exact: true }).click(); - await expect - .poll( - async () => (await stats(request)).current.observed.model.heldActive - ) - .toBe(0); - const evidence = await stats(request); - expect(evidence.current.requests).toHaveLength(1); - expect(evidence.current.observed.model.httpSeen).toBe(1); - expect(evidence.current.observed.model.records[0].peerClosed).toBe(true); - expect(evidence.current.observed.model.records[0].cleanupReleased).toBe( - false - ); - expect(evidence.current.observed.completedReturns).toHaveLength(0); + // The browser's run stream is closed; no second run starts. + await expect.poll(async () => (await runs(request))[0]?.closed).toBe(true); + const evidence = await runs(request); + expect(evidence).toHaveLength(1); + expect(await model(request)).toHaveLength(1); + expect( + evidence[0].sent.some((e) => e.type === 'TOOL_CALL_RESULT') + ).toBe(false); }); test('paused New removes authority and only explicit Send allocates fresh thread', async ({ page, @@ -414,7 +461,7 @@ test('paused New removes authority and only explicit Send allocates fresh thread await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); - const before = await stats(request); + const before = await runs(request); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); @@ -422,17 +469,18 @@ test('paused New removes authority and only explicit Send allocates fresh thread await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toHaveCount(0); - expect((await stats(request)).current.requests).toHaveLength(1); + expect(await runs(request)).toHaveLength(1); await send(page); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); - const after = await stats(request); - expect(after.current.requests).toHaveLength(2); - expect(after.current.requests[1].body.threadId).not.toBe( - before.current.requests[0].body.threadId - ); - expect(after.current.observed.completedReturns).toHaveLength(0); + const after = await runs(request); + expect(after).toHaveLength(2); + expect(after[1].body.threadId).not.toBe(before[0].body.threadId); + // Neither paused booking ever returned. + expect( + after.flatMap((r) => r.events).some((e) => e.type === 'TOOL_CALL_RESULT' && /booked/i.test(e.content ?? '')) + ).toBe(false); }); test('two completed explicit turns retain prior canonicals in the same Session', async ({ page, @@ -453,17 +501,19 @@ test('two completed explicit turns retain prior canonicals in the same Session', ).toBeEnabled(); await page.getByRole('button', { name: 'Decline', exact: true }).click(); await expect(page.getByRole('status')).toHaveText('Meeting declined.'); - const evidence = await stats(request), - requests = evidence.current.requests; + const requests = await runs(request); expect(requests).toHaveLength(4); + expect(new Set(requests.map((r) => r.body.threadId)).size).toBe(1); + // Each resumed run returned its booking result once. + for (const resumed of [requests[1], requests[3]]) + expect( + resumed.events.filter( + (e) => + e.type === 'TOOL_CALL_RESULT' && /booked/i.test(e.content ?? '') + ) + ).toHaveLength(1); + expect(await model(request)).toHaveLength(6); expect( - new Set( - requests.map((r: { body: { threadId: string } }) => r.body.threadId) - ).size - ).toBe(1); - expect(evidence.current.observed.completedReturns).toHaveLength(2); - expect(evidence.current.observed.model.httpSeen).toBe(6); - expect( - requests[2].body.messages.filter((m: { role: string }) => m.role === 'tool') + requests[2].body.messages.filter((m) => m.role === 'tool') ).toHaveLength(2); }); diff --git a/cockpit/runtimes/aws-strands/react/project.json b/cockpit/runtimes/aws-strands/react/project.json index 66b18b7de..4cd5d4e5e 100644 --- a/cockpit/runtimes/aws-strands/react/project.json +++ b/cockpit/runtimes/aws-strands/react/project.json @@ -3,20 +3,26 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/runtimes/aws-strands/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-runtimes-aws-strands-angular", + "cockpit-runtimes-aws-strands-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { - "prepare-fixture": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node scripts/react-cockpit/prepare-native-strands-fixture.mjs" - } - }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "inputs": ["default", "awsStrandsFixture"], - "dependsOn": ["prepare-fixture", "build"], + "inputs": [ + "default", + "reactAimockBackend" + ], + "dependsOn": [ + "build" + ], "options": { "config": "cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts", "skipInstall": true @@ -28,27 +34,34 @@ "configFile": "cockpit/runtimes/aws-strands/react/vite.config.mts" } }, - "lint": { "executor": "@nx/eslint:lint" }, + "lint": { + "executor": "@nx/eslint:lint" + }, "build": { "executor": "nx:run-commands", "cache": false, "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react"] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react" + ] + }, + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } + ], + "outputs": [ + "{workspaceRoot}/dist/cockpit/runtimes/aws-strands/react" ], - "outputs": ["{workspaceRoot}/dist/cockpit/runtimes/aws-strands/react"], "options": { "command": "node scripts/react-cockpit/build.mjs runtimes-aws-strands" } } - }, - "namedInputs": { - "awsStrandsFixture": [ - "{workspaceRoot}/fixtures/react-parity/native-strands-react/**/*", - "{workspaceRoot}/scripts/react-cockpit/native-strands-fixture.mjs", - "{workspaceRoot}/scripts/react-cockpit/native-strands-fixture.spec.mjs", - "{workspaceRoot}/scripts/react-cockpit/prepare-native-strands-fixture.mjs", - "{workspaceRoot}/scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs" - ] } } diff --git a/cockpit/runtimes/mastra/react/README.md b/cockpit/runtimes/mastra/react/README.md index f8180e502..921e32860 100644 --- a/cockpit/runtimes/mastra/react/README.md +++ b/cockpit/runtimes/mastra/react/README.md @@ -15,8 +15,7 @@ npx nx test cockpit-runtimes-mastra-react --reporters=default npx nx lint cockpit-runtimes-mastra-react npx nx build cockpit-runtimes-mastra-react npx nx e2e cockpit-runtimes-mastra-react -node --test fixtures/react-parity/native-mastra-react/*.spec.mjs scripts/react-cockpit/prepare-native-mastra-fixture.spec.mjs node --test scripts/react-cockpit/runtimes-mastra-configuration.spec.mjs ``` -Presentation units use a mocked application store. Policy and lifetime units exercise the real application with native-session doubles. The e2e target first installs the frozen Mastra service dependencies and builds the installed app. Its offline loopback Responses seam drives the unchanged real Mastra service and a fresh temporary LibSQL database. Browser evidence and cleanup receipts stay in ignored test-results. This proves local interoperability; it does not prove a public provider. Run service dependency preparation serially with other Mastra suites because npm ci mutates the shared service node_modules. +Presentation units use a mocked application store. Policy and lifetime units exercise the real application with native-session doubles. The e2e target builds the installed app; its global setup starts the unchanged real Mastra service (installing its frozen dependencies on first use) with a fresh temporary LibSQL database, and aimock replays the Angular example's model fixtures (`cockpit/runtimes/mastra/angular/e2e/fixtures`) through the Responses API. Browser evidence stays in ignored test-results. This proves local interoperability; it does not prove a public provider. Run service dependency preparation serially with other Mastra suites because npm ci mutates the shared service node_modules. diff --git a/cockpit/runtimes/mastra/react/e2e/global-setup-impl.ts b/cockpit/runtimes/mastra/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..dfcaa6e20 --- /dev/null +++ b/cockpit/runtimes/mastra/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockMastraGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the deployments/ag-ui-mastra +// service; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockMastraGlobalSetup({ example: 'runtimes/mastra', port: 4641 }); diff --git a/cockpit/runtimes/mastra/react/e2e/mastra.spec.ts b/cockpit/runtimes/mastra/react/e2e/mastra.spec.ts index 9596ad6d3..c9e791a86 100644 --- a/cockpit/runtimes/mastra/react/e2e/mastra.spec.ts +++ b/cockpit/runtimes/mastra/react/e2e/mastra.spec.ts @@ -6,14 +6,29 @@ import { } from '@playwright/test'; import { writeFile } from 'node:fs/promises'; const path = '/runtimes/mastra/react/'; -async function stats(request: APIRequestContext) { - return (await request.get('/__stats')).json(); -} +// Backend: aimock replays cockpit/runtimes/mastra/angular/e2e/fixtures (shared +// with the Angular example, which sends the same reservation prompt) into the +// real deployments/ag-ui-mastra service, behind the recording proxy +// (scripts/react-cockpit/ag-ui-proxy.mjs). +/** Every agent POST: what the service streamed (`events`) and the browser got (`sent`). */ async function requests(request: APIRequestContext) { - return (await stats(request)).current.requests; + return (await request.get('/__requests')).json(); +} +/** What the model received and which fixture answered (aimock's journal). */ +async function model(request: APIRequestContext) { + return (await request.get('/__model-requests')).json(); } -async function start(page: Page, request: APIRequestContext, mode = 'normal') { - await request.post('/__reset', { data: { mode } }); +async function start( + page: Page, + request: APIRequestContext, + ops?: { edit?: unknown[]; hold?: boolean } +) { + await request.post('/__reset'); + // Injected data, per test: proxy edits or a hold on the browser's stream. + if (ops?.edit) + await request.post('/__stream/edit', { data: { ops: ops.edit } }); + if (ops?.hold) + await request.post('/__hold/stream', { data: { afterType: 'RUN_STARTED' } }); await page.goto(path); await expect(page.getByRole('status')).toHaveText('Ready.'); expect(await requests(request)).toHaveLength(0); @@ -23,33 +38,27 @@ async function start(page: Page, request: APIRequestContext, mode = 'normal') { expect(await requests(request)).toHaveLength(0); await page.getByRole('button', { name: 'Send', exact: true }).click(); } -async function finish(request: APIRequestContext) { - const evidence = await stats(request); +test.afterEach(async ({ page, request }) => { + await writeFile( + test.info().outputPath('private-dom.html'), + await page.content() + ); const evidencePath = test .info() .outputPath('private-actual-service-evidence.json'); - await writeFile(evidencePath, JSON.stringify(evidence, null, 2)); + await writeFile( + evidencePath, + JSON.stringify( + { requests: await requests(request), model: await model(request) }, + null, + 2 + ) + ); await test.info().attach('private-actual-service-evidence.json', { path: evidencePath, contentType: 'application/json', }); - const receipt = await (await request.get('/__finish')).json(); - expect(receipt.closed.length).toBeGreaterThan(0); - for (const owner of receipt.closed) - expect(Object.values(owner.closure).every(Boolean)).toBe(true); - const cleanupPath = test.info().outputPath('private-owned-cleanup.json'); - await writeFile(cleanupPath, JSON.stringify(receipt, null, 2)); - await test.info().attach('private-owned-cleanup.json', { - path: cleanupPath, - contentType: 'application/json', - }); -} -test.afterEach(async ({ page, request }) => { - await writeFile( - test.info().outputPath('private-dom.html'), - await page.content() - ); - await finish(request); + await request.post('/__release'); }); for (const decision of ['Approve', 'Decline'] as const) { test(`actual native ${decision.toLowerCase()} binds pause, resume, tool result and truthful model reply`, async ({ @@ -154,8 +163,8 @@ for (const decision of ['Approve', 'Decline'] as const) { await expect( page.getByText( decision === 'Approve' - ? 'The fictional North Pines reservation is approved for 2 nights.' - : 'The North Pines reservation was declined. Nothing was booked.', + ? 'North Pines is reserved for 2 nights — confirmation TP-0288.' + : 'The reservation for the North Pines campsite was not booked.', { exact: true } ) ).toBeVisible(); @@ -213,24 +222,15 @@ for (const decision of ['Approve', 'Decline'] as const) { runId: resume.runId, }); expect(terminal.outcome).toBeUndefined(); - const current = await stats(request); - expect(current.current.modelRequests).toHaveLength(2); - expect( - current.current.modelRequests[1].body.input.filter( - (item: { type: string }) => item.type === 'function_call_output' - ) - ).toHaveLength(1); - expect(current.current.modelRequests[1].match.toolCalls).toBeUndefined(); - expect(current.current.modelRequests[1].match.content).toContain( - decision === 'Approve' ? 'approved' : 'Nothing was booked' + // The continuation answered the real tool result: the fixture keyed on + // the decision's literal answered with text, not another tool call. + const calls = await model(request); + expect(calls).toHaveLength(2); + expect(calls[1].response.fixture.match.toolResultContains).toBe( + decision === 'Approve' ? 'Reserved North Pines' : 'Nothing was booked.' ); - expect(current.current.networkDenials).toEqual([]); - expect( - records.every( - (r: { closed: boolean; upstreamClosed: boolean }) => - r.closed && r.upstreamClosed - ) - ).toBe(true); + expect(calls[1].response.fixture.response.toolCalls).toBeUndefined(); + expect(records.every((r: { closed: boolean }) => r.closed)).toBe(true); await page.screenshot({ path: test.info().outputPath('desktop.png'), fullPage: true, @@ -259,16 +259,29 @@ test('informational notice without native terminal grants no decision', async ({ page, request, }) => { - await start(page, request, 'notice-only'); + // Injected data: the proxy drops the pause's real interrupt terminal. + await start(page, request, { + edit: [{ type: 'RUN_FINISHED', op: 'drop' }], + }); await expect(page.getByRole('status')).toContainText('unconfirmed'); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toHaveCount(0); - expect(await requests(request)).toHaveLength(1); - const current = await stats(request); - expect(current.current.faults).toHaveLength(1); + const records = await requests(request); + expect(records).toHaveLength(1); + // The service streamed its interrupt terminal; the browser never got it. + expect( + records[0].events.filter( + (event: { type: string }) => event.type === 'RUN_FINISHED' + ) + ).toHaveLength(1); expect( - current.current.requests[0].events.some( + records[0].sent.some( + (event: { type: string }) => event.type === 'RUN_FINISHED' + ) + ).toBe(false); + expect( + records[0].sent.some( (event: { type: string; name?: string }) => event.type === 'CUSTOM' && event.name === 'on_interrupt' ) @@ -278,10 +291,14 @@ test('accepted resume with actual result but lost root terminal cannot retry', a page, request, }) => { - await start(page, request, 'lost-terminal'); + await start(page, request); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); + // Injected data: the proxy drops the resumed run's real root terminal. + await request.post('/__stream/edit', { + data: { ops: [{ type: 'RUN_FINISHED', op: 'drop' }] }, + }); await page.getByRole('button', { name: 'Approve', exact: true }).click(); await expect(page.getByRole('status')).toContainText('unconfirmed'); await expect( @@ -290,15 +307,19 @@ test('accepted resume with actual result but lost root terminal cannot retry', a await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toHaveCount(0); - const current = await stats(request); - expect(current.current.requests).toHaveLength(2); - expect(current.current.faults).toHaveLength(1); + const records = await requests(request); + expect(records).toHaveLength(2); expect( - current.current.requests[1].events.some( + records[1].events.some( (event: { type: string; name?: string }) => event.type === 'TOOL_CALL_RESULT' ) ).toBe(true); + expect( + records[1].sent.some( + (event: { type: string }) => event.type === 'RUN_FINISHED' + ) + ).toBe(false); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); @@ -310,24 +331,21 @@ for (const action of ['Stop', 'New conversation', 'unmount']) page, request, }) => { - await start(page, request, 'held'); + // Injected data: the proxy holds the browser's stream after RUN_STARTED. + await start(page, request, { hold: true }); await expect .poll(async () => (await requests(request))[0]?.held) .toBe(true); if (action === 'unmount') await page.goto('about:blank'); else await page.getByRole('button', { name: action, exact: true }).click(); await expect - .poll(async () => { - const record = (await requests(request))[0]; - return record.closed && record.upstreamClosed && record.upstreamAborted; - }) + .poll(async () => (await requests(request))[0].closed) .toBe(true); const records = await requests(request); expect(records).toHaveLength(1); expect(records[0].body.resume).toBeUndefined(); - expect(records[0].upstreamEnded).toBeUndefined(); expect( - records[0].events.some( + records[0].sent.some( (event: { type: string; name?: string }) => event.type === 'RUN_FINISHED' ) diff --git a/cockpit/runtimes/mastra/react/e2e/playwright.config.ts b/cockpit/runtimes/mastra/react/e2e/playwright.config.ts index 6995e9252..5a5b48ef5 100644 --- a/cockpit/runtimes/mastra/react/e2e/playwright.config.ts +++ b/cockpit/runtimes/mastra/react/e2e/playwright.config.ts @@ -7,6 +7,8 @@ export default defineConfig({ timeout: 30000, use: { baseURL: 'http://127.0.0.1:4641', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real Mastra service, shared with cockpit/runtimes/mastra/angular. + globalSetup: './global-setup-impl.ts', webServer: { command: 'node scripts/react-cockpit/serve.mjs runtimes-mastra --no-parent', cwd: '../../../../..', diff --git a/cockpit/runtimes/mastra/react/project.json b/cockpit/runtimes/mastra/react/project.json index fe464fc0c..46304f6b2 100644 --- a/cockpit/runtimes/mastra/react/project.json +++ b/cockpit/runtimes/mastra/react/project.json @@ -3,6 +3,9 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/runtimes/mastra/react/src", "projectType": "application", + "implicitDependencies": [ + "cockpit-runtimes-mastra-angular" + ], "tags": [ "scope:cockpit-examples", "scope:cockpit-e2e", @@ -30,7 +33,12 @@ "react" ] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/runtimes/mastra/react" @@ -39,24 +47,20 @@ "command": "node scripts/react-cockpit/build.mjs runtimes-mastra" } }, - "prepare-fixture": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node scripts/react-cockpit/prepare-native-mastra-fixture.mjs" - } - }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, "dependsOn": [ - "prepare-fixture", "build" ], "options": { "config": "cockpit/runtimes/mastra/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] } } } diff --git a/cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures/microsoft-agent-framework.json b/cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures/microsoft-agent-framework.json index a441c5ab7..2b1386327 100644 --- a/cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures/microsoft-agent-framework.json +++ b/cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures/microsoft-agent-framework.json @@ -1,5 +1,14 @@ { "fixtures": [ + {"match": {"userMessage": "[scenario:batch]", "toolResultContains": "Expense recorded"}, "response": {"content": "The fictional $220.00 Blue Finch Bistro expense was submitted for reimbursement."}}, + {"match": {"userMessage": "[scenario:batch]", "toolResultContains": "rejected by user"}, "response": {"content": "The fictional Blue Finch Bistro expense was not submitted."}}, + {"match": {"userMessage": "[scenario:batch]", "hasToolResult": true}, "response": {"content": "The Blue Finch Bistro expense decision is complete."}}, + {"match": {"userMessage": "[scenario:batch]"}, "response": {"toolCalls": [{"id": "c0_lookup_expense_policy", "name": "lookup_expense_policy", "arguments": "{\"category\": \"meals\"}"}, {"id": "c0_submit_expense", "name": "submit_expense", "arguments": "{\"expense\":{\"vendor\":\"Blue Finch Bistro\",\"category\":\"meals\",\"amount_usd\":220,\"memo\":\"Team dinner for 4 attendees; receipt attached.\"}}"}]}}, + {"match": {"userMessage": "[scenario:cancel]"}, "response": {"toolCalls": [{"id": "c0_lookup_expense_policy", "name": "lookup_expense_policy", "arguments": "{\"category\": \"meals\"}"}]}, "latency": 30000}, + {"match": {"userMessage": "fictional demo expense", "toolResultContains": "Expense recorded"}, "response": {"content": "The fictional $220.00 Blue Finch Bistro expense was submitted for reimbursement."}}, + {"match": {"userMessage": "fictional demo expense", "toolResultContains": "rejected by user"}, "response": {"content": "The fictional Blue Finch Bistro expense was not submitted."}}, + {"match": {"userMessage": "fictional demo expense", "hasToolResult": true}, "response": {"toolCalls": [{"id": "c0_submit_expense", "name": "submit_expense", "arguments": "{\"expense\":{\"vendor\":\"Blue Finch Bistro\",\"category\":\"meals\",\"amount_usd\":220,\"memo\":\"Team dinner for 4 attendees; receipt attached.\"}}"}]}}, + {"match": {"userMessage": "fictional demo expense"}, "response": {"toolCalls": [{"id": "c0_lookup_expense_policy", "name": "lookup_expense_policy", "arguments": "{\"category\": \"meals\"}"}]}}, { "match": { "userMessage": "team dinner", "hasToolResult": true, "turnIndex": 2 }, "response": { diff --git a/cockpit/runtimes/microsoft-agent-framework/react/README.md b/cockpit/runtimes/microsoft-agent-framework/react/README.md index 356e1c165..d1eb53145 100644 --- a/cockpit/runtimes/microsoft-agent-framework/react/README.md +++ b/cockpit/runtimes/microsoft-agent-framework/react/README.md @@ -8,6 +8,6 @@ Shared runtime uses the current origin's `/runtimes/microsoft-agent-framework/ag Build with `npx nx build cockpit-runtimes-microsoft-agent-framework-react`. The target builds core, content, and React, then builds an isolated consumer from packed packages and the emitted native AG-UI candidate. Its output includes `build-proof.json`. There is no per-example package manifest. -Run project tests with `npx nx test cockpit-runtimes-microsoft-agent-framework-react` and lint with `npx nx lint cockpit-runtimes-microsoft-agent-framework-react`. Run real-service browser coverage with `npx nx e2e cockpit-runtimes-microsoft-agent-framework-react`; the target prepares frozen Python 3.12 dependencies serially in an isolated temporary environment before building and running the unchanged generated service against a deterministic offline OpenAI model seam. Preparation and active fixture lifetimes share an exclusive owner lock. Fixture cleanup closes its owned HTTP/model processes; it never mutates the canonical environment or backend. This proves local interoperability, not a live model-provider request. +Run project tests with `npx nx test cockpit-runtimes-microsoft-agent-framework-react` and lint with `npx nx lint cockpit-runtimes-microsoft-agent-framework-react`. Run real-service browser coverage with `npx nx e2e cockpit-runtimes-microsoft-agent-framework-react`; the target builds, then its global setup runs the example's real Agent Framework server (`cockpit/runtimes/microsoft-agent-framework/python`, through `uv`) with aimock replaying the model's answers (`cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures`), behind the recording proxy. This proves local interoperability, not a live model-provider request. -Website Docs, Code, and Run select the same authored implementation. After fresh selected build and fixture preparation, focused local website coverage uses `WEBSITE_E2E_REACT_TOPIC=runtimes-microsoft-agent-framework WEBSITE_E2E_PORT=4390 npx playwright test --config apps/website/playwright.config.ts`. The ordinary `npx nx e2e website` target prepares fixtures and builds every registered React frontend first. The native packages are private source-build artifacts; no public npm availability is claimed. +Website Docs, Code, and Run select the same authored implementation. After a fresh selected build, focused local website coverage uses `WEBSITE_E2E_REACT_TOPIC=runtimes-microsoft-agent-framework WEBSITE_E2E_PORT=4390 npx playwright test --config apps/website/playwright.config.ts`. The ordinary `npx nx e2e website` target builds every registered React frontend first. The native packages are private source-build artifacts; no public npm availability is claimed. diff --git a/cockpit/runtimes/microsoft-agent-framework/react/e2e/global-setup-impl.ts b/cockpit/runtimes/microsoft-agent-framework/react/e2e/global-setup-impl.ts new file mode 100644 index 000000000..d05338673 --- /dev/null +++ b/cockpit/runtimes/microsoft-agent-framework/react/e2e/global-setup-impl.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries -- Shared aimock setup for the React cockpit examples. +import { reactAimockAgUiGlobalSetup } from '../../../../shared/react-aimock-setup'; + +// aimock replays the Angular example's fixtures into the example's real AG-UI +// server; the static server (webServer) proxies the agent endpoint to it. +export default reactAimockAgUiGlobalSetup({ example: 'runtimes/microsoft-agent-framework', port: 4642 }); diff --git a/cockpit/runtimes/microsoft-agent-framework/react/e2e/maf.spec.ts b/cockpit/runtimes/microsoft-agent-framework/react/e2e/maf.spec.ts index 9b90728b7..c3f100322 100644 --- a/cockpit/runtimes/microsoft-agent-framework/react/e2e/maf.spec.ts +++ b/cockpit/runtimes/microsoft-agent-framework/react/e2e/maf.spec.ts @@ -10,9 +10,6 @@ const path = '/runtimes/microsoft-agent-framework/react/'; const approved = 'Expense recorded: $220.00 to Blue Finch Bistro (meals) — queued for reimbursement.'; const rejected = 'Error: Tool call invocation was rejected by user.'; -async function stats(request: APIRequestContext) { - return (await request.get('/__stats')).json(); -} interface WireEvent { readonly type: string; readonly runId?: string; @@ -36,28 +33,70 @@ interface WireMessage { readonly function: { readonly name: string; readonly arguments: string }; }[]; } -function events(base64: string): WireEvent[] { - return [ - ...Buffer.from(base64, 'base64') - .toString() - .matchAll(/^data: (.+)$/gm), - ].map((match) => JSON.parse(match[1])); +// Backend: aimock replays cockpit/runtimes/microsoft-agent-framework/angular/ +// e2e/fixtures (the expense model's answers, keyed on the React preset prompt) +// into the example's real Agent Framework AG-UI server, behind the recording +// proxy (scripts/react-cockpit/ag-ui-proxy.mjs). Scenarios are tags the spec +// appends to the preset (`[scenario:batch]`, `[scenario:cancel]`). +interface Run { + body: { + threadId: string; + runId: string; + resume?: unknown; + messages: WireMessage[]; + }; + events: WireEvent[]; + sent: WireEvent[]; + closed: boolean; +} +/** Every agent POST: what the server streamed (`events`) and the browser got (`sent`). */ +async function runs(request: APIRequestContext): Promise { + return (await request.get('/__requests')).json(); +} +interface ModelCall { + body: { + messages: { role: string; tool_call_id?: string; content?: string }[]; + }; + response: { + fixture: { + response: { + content?: string; + toolCalls?: { id: string; name: string; arguments: string }[]; + }; + } | null; + }; } -async function start( - page: Page, - request: APIRequestContext, - scenario = 'approval' -) { - expect((await request.post('/__reset', { data: { scenario } })).ok()).toBe( - true +/** What the model received and which fixture answered (aimock's journal). */ +async function model(request: APIRequestContext): Promise { + return (await request.get('/__model-requests')).json(); +} +/** The tool calls (or the text) each model call answered with. */ +const answers = (calls: ModelCall[]) => + calls.map((call) => + call.response.fixture?.response.toolCalls?.map((c) => c.name).join('+') ?? + call.response.fixture?.response.content + ); +/** Results of the real submit_expense tool in a run. */ +const submitted = (run: Run) => + run.events.filter( + (event) => + event.type === 'TOOL_CALL_RESULT' && + (event as { toolCallId?: string }).toolCallId === 'c0_submit_expense' ); +async function start(page: Page, request: APIRequestContext) { + expect((await request.post('/__reset')).ok()).toBe(true); await page.goto(path); await expect(page.getByRole('status')).toHaveText('Ready.'); } -async function send(page: Page) { +async function send(page: Page, scenario = 'approval') { await page .getByRole('button', { name: 'Team dinner for 4', exact: true }) .click(); + if (scenario !== 'approval') { + const box = page.getByRole('textbox', { name: 'Message', exact: true }); + await box.press('End'); + await box.pressSequentially(` [scenario:${scenario}]`); + } await page.getByRole('button', { name: 'Send', exact: true }).click(); } async function withinViewport(locator: Locator) { @@ -78,19 +117,16 @@ async function withinViewport(locator: Locator) { ).toBe(true); } test.afterEach(async ({ page, request }) => { - // Capture failure attempts too; stats are assertions, never application input. + // Capture failure attempts too; evidence is for assertions, never application input. await writeFile( test.info().outputPath('actual-service-evidence.json'), - JSON.stringify(await stats(request), null, 2) + JSON.stringify( + { runs: await runs(request), model: await model(request) }, + null, + 2 + ) ); await writeFile(test.info().outputPath('dom.html'), await page.content()); - const receipt = await (await request.post('/__finish')).json(); - await writeFile( - test.info().outputPath('owned-cleanup.json'), - JSON.stringify(receipt, null, 2) - ); - for (const owner of receipt.closed) - expect(Object.values(owner.closure).every(Boolean)).toBe(true); }); test('mount, preset, edit and New allocate no requests; only explicit Send reaches service', async ({ @@ -110,16 +146,14 @@ test('mount, preset, edit and New allocate no requests; only explicit Send reach await expect( page.getByRole('textbox', { name: 'Message', exact: true }) ).toHaveValue(''); - let evidence = await stats(request); - expect(evidence.current.requests).toHaveLength(0); - expect(evidence.current.observed.model.httpSeen).toBe(0); + expect(await runs(request)).toHaveLength(0); + expect(await model(request)).toHaveLength(0); await send(page); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); - evidence = await stats(request); - expect(evidence.current.requests).toHaveLength(1); - expect(evidence.current.observed.model.httpSeen).toBe(2); + expect(await runs(request)).toHaveLength(1); + expect(await model(request)).toHaveLength(2); }); for (const scenario of ['approval', 'batch']) @@ -133,8 +167,8 @@ for (const scenario of ['approval', 'batch']) width, height: width === 390 ? 844 : 900, }); - await start(page, request, scenario); - await send(page); + await start(page, request); + await send(page, scenario); await expect(page.getByRole('status')).toHaveText( 'Awaiting your decision.' ); @@ -195,11 +229,11 @@ for (const scenario of ['approval', 'batch']) await card.screenshot({ path: test.info().outputPath('canonical-approval-card.png'), }); - const before = await stats(request); - expect(before.current.requests).toHaveLength(1); - const initial = before.current.requests[0]; - expect(initial.upstreamBase64).toBe(initial.sentBase64); - const terminal = events(initial.upstreamBase64).find( + const before = await runs(request); + expect(before).toHaveLength(1); + const initial = before[0]; + expect(initial.sent).toEqual(initial.events); + const terminal = initial.events.find( (event) => event.type === 'RUN_FINISHED' ); expect(terminal?.runId).toBe(initial.body.runId); @@ -207,23 +241,17 @@ for (const scenario of ['approval', 'batch']) expect(terminal?.outcome?.type).toBe('interrupt'); expect(terminal?.outcome?.interrupts).toHaveLength(1); if (scenario === 'batch') { - expect(before.current.observed.model.httpSeen).toBe(1); - const response = Buffer.from( - before.current.observed.model.records[0].responseBase64, - 'base64' - ).toString(); - const declarations = [ - ...response.matchAll(/^data: (\{.+\})$/gm), - ].flatMap( - (match) => JSON.parse(match[1]).choices[0].delta.tool_calls ?? [] - ); + // One model answer declared both calls. + const calls = await model(request); + expect(calls).toHaveLength(1); + const declarations = calls[0].response.fixture!.response.toolCalls!; expect( - declarations.map((call) => [call.id, call.function.name]) + declarations.map((call) => [call.id, call.name]) ).toEqual([ ['c0_lookup_expense_policy', 'lookup_expense_policy'], ['c0_submit_expense', 'submit_expense'], ]); - const pausedRows = events(initial.upstreamBase64) + const pausedRows = initial.events .filter((event) => event.type === 'MESSAGES_SNAPSHOT') .at(-1)?.messages; expect(pausedRows).toBeDefined(); @@ -241,15 +269,16 @@ for (const scenario of ['approval', 'batch']) ).toEqual( declarations.map((call) => [ call.id, - call.function.name, - JSON.parse(call.function.arguments), + call.name, + JSON.parse(call.arguments), ]) ); expect( pausedRows?.filter((message) => message.role === 'tool') ).toHaveLength(0); } - expect(before.current.observed.entries).toBe(0); + // Paused: the real submit_expense has not run. + expect(submitted(initial)).toHaveLength(0); await page.getByRole('button', { name: decision, exact: true }).click(); await expect(page.getByRole('status')).toHaveText( decision === 'Approve' @@ -292,10 +321,10 @@ for (const scenario of ['approval', 'batch']) await result.screenshot({ path: test.info().outputPath('literal-result-region.png'), }); - const after = await stats(request), - records = after.current.requests; + const records = await runs(request), + calls = await model(request); expect(records).toHaveLength(2); - expect(records[1].upstreamBase64).toBe(records[1].sentBase64); + expect(records[1].sent).toEqual(records[1].events); expect(records[1].body.threadId).toBe(initial.body.threadId); expect(records[1].body.runId).not.toBe(initial.body.runId); expect(records[1].body.resume).toEqual([ @@ -305,14 +334,14 @@ for (const scenario of ['approval', 'batch']) payload: { approved: decision === 'Approve' }, }, ]); - const final = events(records[1].upstreamBase64).find( + const final = records[1].events.find( (event) => event.type === 'RUN_FINISHED' ); expect(final?.runId).toBe(records[1].body.runId); expect(final?.threadId).toBe(records[1].body.threadId); expect(final?.subagentRunId).toBeUndefined(); if (scenario === 'batch') { - const finalRows = events(records[1].upstreamBase64) + const finalRows = records[1].events .filter((event) => event.type === 'MESSAGES_SNAPSHOT') .at(-1)?.messages; expect(finalRows).toBeDefined(); @@ -334,13 +363,13 @@ for (const scenario of ['approval', 'batch']) ); // The lookup is compared with the genuine continuation, never used as expense authority. expect( - after.current.observed.model.records - .at(-1) + calls + .at(-1)! .body.messages.filter( - (message: { role: string; tool_call_id?: string }) => + (message) => message.role === 'tool' && message.tool_call_id === callId ) - .map((message: { content: string }) => message.content) + .map((message) => message.content) ).toEqual([results?.[0].content]); } expect( @@ -349,41 +378,75 @@ for (const scenario of ['approval', 'batch']) ) ).toHaveLength(0); } - expect(after.current.observed.model.httpSeen).toBe( - scenario === 'batch' ? 2 : 3 - ); - expect( - after.current.observed.model.records.map( - (record: Record) => record.branch - ) - ).toEqual([ + // The model answered lookup, submit (or both at once), then the + // continuation. Its fixtures key on the last tool result it received: + // the decision's text when that is the submit result, a neutral text + // when the batch's lookup result comes last. + const lastTool = calls.at(-1)!.body.messages.at(-1)!; + expect(answers(calls)).toEqual([ ...(scenario === 'batch' - ? ['batch-policy-submit'] - : ['lookup-policy', 'submit-expense']), - decision === 'Approve' - ? 'continuation-approve' - : 'continuation-decline', + ? ['lookup_expense_policy+submit_expense'] + : ['lookup_expense_policy', 'submit_expense']), + lastTool.tool_call_id !== 'c0_submit_expense' + ? 'The Blue Finch Bistro expense decision is complete.' + : decision === 'Approve' + ? 'The fictional $220.00 Blue Finch Bistro expense was submitted for reimbursement.' + : 'The fictional Blue Finch Bistro expense was not submitted.', ]); expect( - after.current.observed.model.records.at(-1) - .actualContinuationToolContent - ).toBe(decision === 'Approve' ? approved : rejected); - expect(after.current.observed.entries).toBe( - decision === 'Approve' ? 1 : 0 - ); + calls + .at(-1)! + .body.messages.filter( + (message) => + message.role === 'tool' && + message.tool_call_id === 'c0_submit_expense' + ) + .map((message) => message.content) + ).toEqual([decision === 'Approve' ? approved : rejected]); + // The real submit_expense ran only when approved. + expect(submitted(records[1])).toHaveLength(decision === 'Approve' ? 1 : 0); }); } +const corruptions: Record = { + 'missing-terminal': [{ type: 'RUN_FINISHED', op: 'drop' }], + 'wrong-terminal': [ + { type: 'RUN_FINISHED', op: 'patch', set: { runId: 'negative-wrong-run' } }, + ], + // The pause's terminal without its interrupt outcome. + 'draft-only': [{ type: 'RUN_FINISHED', op: 'omit', keys: ['outcome'] }], + // The pause's terminal downgraded to a child-only observation. + 'child-pause': [ + { + type: 'RUN_FINISHED', + op: 'patch', + set: { + type: 'SUBAGENT_FINISHED', + subagentRunId: 'negative-child', + outcome: { type: 'suspended' }, + }, + }, + { type: 'RUN_FINISHED', op: 'omit', keys: ['runId', 'threadId'] }, + ], +}; for (const scenario of ['missing-terminal', 'wrong-terminal']) test(`negative-only ${scenario} leaves actual result unconfirmed`, async ({ page, request, }) => { - await start(page, request, scenario); + await start(page, request); await send(page); await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); + // Injected data: the proxy corrupts the resumed run's real terminal on its way to the browser. + expect( + ( + await request.post('/__stream/edit', { + data: { ops: corruptions[scenario] }, + }) + ).ok() + ).toBe(true); await page.getByRole('button', { name: 'Approve', exact: true }).click(); await expect(page.getByRole('status')).toHaveText( 'Expense unconfirmed. Start a new conversation.' @@ -391,22 +454,25 @@ for (const scenario of ['missing-terminal', 'wrong-terminal']) await expect( page.getByRole('region', { name: 'Observed backend result' }) ).toHaveCount(0); - const evidence = await stats(request), - resumed = evidence.current.requests[1]; - expect(evidence.current.requests).toHaveLength(2); + const evidence = await runs(request), + resumed = evidence[1]; + expect(evidence).toHaveLength(2); expect( - events(resumed.upstreamBase64).some( + resumed.events.some( (event) => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId ) ).toBe(true); expect( - events(resumed.sentBase64).some( + resumed.sent.some( (event) => event.type === 'RUN_FINISHED' && event.runId === resumed.body.runId ) ).toBe(false); - expect(evidence.current.observed.entries).toBe(1); + // The real expense was still recorded upstream. + expect( + submitted(resumed).map((event) => (event as { content?: string }).content) + ).toEqual([approved]); }); for (const scenario of ['draft-only', 'child-pause']) @@ -414,7 +480,15 @@ for (const scenario of ['draft-only', 'child-pause']) page, request, }) => { - await start(page, request, scenario); + await start(page, request); + // Injected data: the proxy corrupts the pause's real terminal on its way to the browser. + expect( + ( + await request.post('/__stream/edit', { + data: { ops: corruptions[scenario] }, + }) + ).ok() + ).toBe(true); await send(page); await expect(page.getByRole('status')).toHaveText( 'Expense unconfirmed. Start a new conversation.' @@ -422,44 +496,31 @@ for (const scenario of ['draft-only', 'child-pause']) await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toHaveCount(0); - const evidence = await stats(request); - expect(evidence.current.requests).toHaveLength(1); - expect(evidence.current.observed.entries).toBe(0); - expect(evidence.current.observed.model.httpSeen).toBe(2); - expect(evidence.current.requests[0].upstreamBase64).not.toBe( - evidence.current.requests[0].sentBase64 - ); + const evidence = await runs(request); + expect(evidence).toHaveLength(1); + expect(submitted(evidence[0])).toHaveLength(0); + expect(await model(request)).toHaveLength(2); + expect(evidence[0].sent).not.toEqual(evidence[0].events); }); for (const action of ['Stop', 'New conversation', 'unmount']) - test(`held actual model ${action} closes peer before fixture cleanup`, async ({ + test(`held actual model ${action} closes the run`, async ({ page, request, }) => { - await start(page, request, 'cancel'); - await send(page); - await expect - .poll( - // The model worker may not exist yet on the first poll. - async () => (await stats(request)).current?.observed.model.heldActive ?? 0 - ) - .toBe(1); + // The cancel fixture answers only after 30 s, so the model call is in + // flight when the browser acts. + await start(page, request); + await send(page, 'cancel'); + await expect.poll(async () => (await model(request)).length).toBe(1); if (action === 'unmount') await page.evaluate(() => window.dispatchEvent(new Event('pagehide'))); else await page.getByRole('button', { name: action, exact: true }).click(); - await expect - .poll( - async () => (await stats(request)).current.observed.model.heldActive - ) - .toBe(0); - const evidence = await stats(request); - expect(evidence.current.requests).toHaveLength(1); - expect(evidence.current.observed.model.httpSeen).toBe(1); - expect(evidence.current.observed.model.records[0].peerClosed).toBe(true); - expect( - evidence.current.observed.model.records[0].heldReleasedByCleanup - ).toBe(false); - expect(evidence.current.observed.entries).toBe(0); + await expect.poll(async () => (await runs(request))[0]?.closed).toBe(true); + const evidence = await runs(request); + expect(evidence).toHaveLength(1); + expect(await model(request)).toHaveLength(1); + expect(submitted(evidence[0])).toHaveLength(0); }); test('paused New discards authority without replay; second explicit Send owns a fresh thread', async ({ @@ -471,7 +532,7 @@ test('paused New discards authority without replay; second explicit Send owns a await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toBeEnabled(); - const before = await stats(request); + const before = await runs(request); await page .getByRole('button', { name: 'New conversation', exact: true }) .click(); @@ -479,16 +540,14 @@ test('paused New discards authority without replay; second explicit Send owns a await expect( page.getByRole('button', { name: 'Approve', exact: true }) ).toHaveCount(0); - expect((await stats(request)).current.requests).toHaveLength(1); + expect(await runs(request)).toHaveLength(1); await send(page); await expect( page.getByRole('button', { name: 'Decline', exact: true }) ).toBeEnabled(); - const after = await stats(request); - expect(after.current.requests).toHaveLength(2); - expect(after.current.requests[1].body.threadId).not.toBe( - before.current.requests[0].body.threadId - ); - expect(after.current.observed.model.httpSeen).toBe(4); - expect(after.current.observed.entries).toBe(0); + const after = await runs(request); + expect(after).toHaveLength(2); + expect(after[1].body.threadId).not.toBe(before[0].body.threadId); + expect(await model(request)).toHaveLength(4); + expect(after.flatMap(submitted)).toHaveLength(0); }); diff --git a/cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts b/cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts index f434db83f..3c0f986aa 100644 --- a/cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts +++ b/cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts @@ -2,7 +2,7 @@ import { defineConfig, devices } from '@playwright/test'; import { resolve } from 'node:path'; export default defineConfig({ testDir: '.', - outputDir: resolve(__dirname, '../../../../../test-results/native-maf-react'), + outputDir: resolve(__dirname, '../../../../../test-results/cockpit-runtimes-microsoft-agent-framework-react'), workers: 1, fullyParallel: false, timeout: 45000, @@ -10,6 +10,9 @@ export default defineConfig({ reporter: 'list', use: { baseURL: 'http://127.0.0.1:4642', trace: 'retain-on-failure' }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + // aimock + the real AG-UI server, shared with cockpit/runtimes/microsoft-agent-framework/angular. + globalSetup: './global-setup-impl.ts', + globalTeardown: require.resolve('../../../../../libs/e2e-harness/src/global-teardown'), webServer: { command: 'node scripts/react-cockpit/serve.mjs runtimes-microsoft-agent-framework --no-parent', diff --git a/cockpit/runtimes/microsoft-agent-framework/react/project.json b/cockpit/runtimes/microsoft-agent-framework/react/project.json index 220c3b329..90d7a453a 100644 --- a/cockpit/runtimes/microsoft-agent-framework/react/project.json +++ b/cockpit/runtimes/microsoft-agent-framework/react/project.json @@ -3,23 +3,30 @@ "$schema": "../../../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "cockpit/runtimes/microsoft-agent-framework/react/src", "projectType": "application", - "tags": ["scope:cockpit-examples", "scope:cockpit-e2e", "framework:react"], + "implicitDependencies": [ + "cockpit-runtimes-microsoft-agent-framework-angular", + "cockpit-runtimes-microsoft-agent-framework-python" + ], + "tags": [ + "scope:cockpit-examples", + "scope:cockpit-e2e", + "framework:react" + ], "targets": { - "prepare-fixture": { - "executor": "nx:run-commands", - "cache": false, - "options": { - "command": "node scripts/react-cockpit/prepare-native-maf-fixture.mjs" - } - }, "e2e": { "executor": "@nx/playwright:playwright", "cache": false, - "dependsOn": ["prepare-fixture", "build"], + "dependsOn": [ + "build" + ], "options": { "config": "cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts", "skipInstall": true - } + }, + "inputs": [ + "default", + "reactAimockBackend" + ] }, "test": { "executor": "@nx/vitest:test", @@ -27,13 +34,27 @@ "configFile": "cockpit/runtimes/microsoft-agent-framework/react/vite.config.mts" } }, - "lint": { "executor": "@nx/eslint:lint" }, + "lint": { + "executor": "@nx/eslint:lint" + }, "build": { "executor": "nx:run-commands", "cache": false, "dependsOn": [ - { "target": "build", "projects": ["core", "content", "react"] }, - { "target": "react-ag-ui", "projects": ["website-runtime-preparation"] } + { + "target": "build", + "projects": [ + "core", + "content", + "react" + ] + }, + { + "target": "react-ag-ui", + "projects": [ + "website-runtime-preparation" + ] + } ], "outputs": [ "{workspaceRoot}/dist/cockpit/runtimes/microsoft-agent-framework/react" diff --git a/cockpit/shared/react-aimock-setup.ts b/cockpit/shared/react-aimock-setup.ts index 03f1f4761..aa7b60316 100644 --- a/cockpit/shared/react-aimock-setup.ts +++ b/cockpit/shared/react-aimock-setup.ts @@ -1,4 +1,5 @@ import { resolve } from 'node:path'; +import { pathToFileURL } from 'node:url'; import { createAgUiBackendGlobalSetup, createLanggraphGlobalSetup, @@ -51,3 +52,45 @@ export function reactAimockAgUiGlobalSetup(options: { fixturesDir: resolve(__dirname, '..', options.example, 'angular/e2e/fixtures'), }); } + +/** + * The Mastra flavor: the backend is the deployments/ag-ui-mastra Node service + * (the Angular example's backend), not a Python server, so this starts it + * through scripts/react-cockpit/aimock-backend.mjs (`aimock-mastra`), the + * code `serve.mjs --backend` runs for Website previews. Mirrors + * `aimockMastra()` in scripts/react-cockpit/configuration.mjs. Returns its + * own teardown. + */ +export function reactAimockMastraGlobalSetup(options: { + /** e.g. 'runtimes/mastra' */ + example: string; + /** The React app port from configuration.mjs. */ + port: number; +}): () => Promise<() => Promise> { + return async () => { + const root = resolve(__dirname, '../..'); + const { createAimockBackend } = (await import( + pathToFileURL(resolve(root, 'scripts/react-cockpit/aimock-backend.mjs')).href + )) as { + createAimockBackend(options: { root: string; backend: unknown }): { + ensure(): Promise; + close(): Promise; + }; + }; + const backend = createAimockBackend({ + root, + backend: { + kind: 'aimock-mastra', + service: 'deployments/ag-ui-mastra', + fixtures: `cockpit/${options.example}/angular/e2e/fixtures`, + backendPort: options.port + 1000, + aimockPort: options.port + 2000, + agentPath: '/agent/mastra', + publicAgentPath: '/agent', + headers: { 'x-internal-token': 'dev-local-token' }, + }, + }); + await backend.ensure(); + return () => backend.close(); + }; +} diff --git a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md index 8197df850..666629d37 100644 --- a/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md +++ b/docs/superpowers/specs/2026-11-18-react-cockpit-aimock-design.md @@ -185,7 +185,7 @@ example needs that the pilot proxy doesn't yet provide. | 6a (done) | ag-ui/{streaming, interrupts, tool-views, json-render, subagents} | the AG-UI flavor: `aimock-ag-ui` backends run the example's uvicorn `src/server.py` (`agentPath` picks the route, e.g. interrupts' `/agent/native`), `createAgUiBackendGlobalSetup` in the harness, and `scripts/react-cockpit/ag-ui-proxy.mjs` (`/__requests`, `/__captures`, `/__lifetime`, `/__hold/stream { afterType, afterPattern }`, `/__release`, `/__fail-stream`, `/__stream/edit` with drop, patch, omit, setPath, replace, insertBefore/After, end, `outcome` and `snapshot` modes, `dataPattern`, `once`, `persist`) | streaming's fixture was pure authored JavaScript; the others ran the real graph through a wire script and rewrote its events, so their scripted models (from the Python tests) become shared fixtures and their rewrites become proxy edits (injected data). Real behavior replaces several: a model failure drops the stream (no RUN_ERROR), native interrupts carry LangGraph's own interrupt id, unknown tools and argument-less calls fail in the real ToolNode, and a Booking specialist's provider error is a real child failure. The client returns the server's STATE_SNAPSHOT as the next run's `state`; specs assert that instead of `{}` | | 6b (done) | ag-ui/client-tools | `aimockPort` (app port + 2000) on AG-UI backends and `/__model-requests` (aimock's `GET /__aimock/journal`); `/__requests` entries carry live `events`, `held` and `closed`; holds take `beforeType`, `persist` and `fromRequest` | the React fixture wrapped the shared react-parity server's `cockpit` scenario; that scenario becomes aimock fixtures and is removed from the parity worker, which stays for `scripts/react-parity` verification. The `prepare-fixture` target, its scripts, specs and CI steps go: `uv run` syncs the environment. The causal checks read the model's received messages from the journal. Graph, checkpointer and model identity and socket-attempt checks were the parity worker's own proofs; the process boundary replaces them, as for chat/messages | | 6c (done) | ag-ui/a2ui, chat/a2ui | ag-ui proxy `/__hold/off`; `createAimockBackend.close()` stops uv's whole process tree (`langgraph dev` starts workers of its own) | **ag-ui/a2ui:** the search, select and modify turns replay the react-parity A2UI model's outputs for the flow (captured from its traces), keyed on each node's system prompt; the opening form is the recorded Angular fixture. The installed-application proof (`ag-ui-a2ui-native-probe.mjs`) and the parity server stay: they verify `scripts/react-parity`, not the cockpit e2e. **chat/a2ui:** the old wire's model calls become fixtures keyed on each node's full system prompt (captured from the wire). The packed-consumer probe (`chat-a2ui-native-probe.mjs`, the `fixture-test` target and the Library lane's "Verify canonical A2UI native graph" step) runs the installed `@threadplane/langgraph` and the compiled application admission helpers against `createAimockBackend` + an in-process proxy; its harness tests move to `chat-a2ui-native-probe.spec.mjs`. The fixture spec's graph cases are reached through model output (injected data: `[scenario:title-down]` answers the title prompt with a provider error, `[scenario:retry]` and `[scenario:invalid]` answer the form designer with a fare option outside the schema, once or always) and run in the React e2e against the real graph: a swallowed title failure, one structured-output retry, and the fallback form after three invalid answers. The host and readiness specs tested the deleted fixture's worker lifecycle | -| 7 | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `/__finish`, `/__stats` | native non-LangGraph backends. Angular already replays `aws-strands.json`, `mastra.json` and `microsoft-agent-framework.json` through their own servers | +| 7 (done) | runtimes/{aws-strands, mastra, microsoft-agent-framework} | `aimock-mastra` backends (the deployments/ag-ui-mastra Node service, with the proxy adding its internal token; `reactAimockMastraGlobalSetup`); the AG-UI proxy's `/__requests` entries carry `sent` (what the browser got, after edits) beside `events` | each React example wrapped a react-parity loopback model and service tap (`fixtures/react-parity/native-{strands,maf,mastra}-react`, used by nothing else; deleted with their fixture servers and preparation targets). Strands and MAF now run the example's own uvicorn server, Mastra the Angular example's service. Their scripted models become fixtures keyed on the React preset prompt (Mastra's prompt is the Angular one, so it shares the Angular fixtures); `[scenario:batch]` and `[scenario:cancel]` tags (a 30 s `latency`) replace `/__reset { scenario }`. Terminal corruptions (missing, wrong run, child-only, draft-only, lost) are proxy edits (injected data), and the held run is `/__hold/stream`. Model evidence comes from aimock's journal: call counts, which fixture answered, the tool results the model received and `x-stainless-retry-count`. The taps' Python profiler counts (completed returns, interrupted unwinds), the service's internal booking state and ledger, the loopback model's socket-closure records and module hashes have no counterpart: the real tool's own `TOOL_CALL_RESULT` (present only after approval) and the closed proxy request stand in. Strands keeps one demo state per process (src/agent.py), so its batch pause shows whichever availability the server last held. The Angular Strands fixtures now key on their own prompt ("roadmap review meeting"), since aimock prefers a fixture whose `turnIndex` fits over the first match | | n/a | render/* (6) | none | `adapter: 'none'`, no model backend | Each batch deletes the fixture servers (`*-fixture.mjs`, `*-wire.py`, diff --git a/fixtures/react-parity/native-maf-react/configuration.mjs b/fixtures/react-parity/native-maf-react/configuration.mjs deleted file mode 100644 index b264c3b5f..000000000 --- a/fixtures/react-parity/native-maf-react/configuration.mjs +++ /dev/null @@ -1,16 +0,0 @@ -/** Neutral fixture ownership paths shared by preparation and active runtimes. */ -import { createHash } from 'node:crypto'; -import { tmpdir } from 'node:os'; -import { dirname, resolve, join } from 'node:path'; -import { fileURLToPath } from 'node:url'; -export const root = resolve(dirname(fileURLToPath(import.meta.url)), '../../..'); -export const project = resolve(root, 'deployments/ag-ui-maf/deps/microsoft_agent_framework'); -export const environment = join(tmpdir(), 'threadplane-native-maf-react-' + createHash('sha256').update(root).digest('hex').slice(0, 16)); -export const preparationLock = environment + '.owner'; -export function fixtureEnvironment() { - return Object.fromEntries( - ['PATH', 'HOME', 'TMPDIR', 'SSL_CERT_FILE', 'UV_CACHE_DIR'] - .filter(key => process.env[key]) - .map(key => [key, process.env[key]]) - ); -} diff --git a/fixtures/react-parity/native-maf-react/model.py b/fixtures/react-parity/native-maf-react/model.py deleted file mode 100644 index e171353b3..000000000 --- a/fixtures/react-parity/native-maf-react/model.py +++ /dev/null @@ -1,134 +0,0 @@ -"""Local OpenAI chat.completions seam. Positive AG-UI events come only from the installed generated service.""" -import base64 -import http.server -import json -import select -import socket -import threading -import time -from urllib.request import Request, urlopen - -EXPENSE = {'vendor':'Blue Finch Bistro','category':'meals','amount_usd':220,'memo':'Team dinner for 4 attendees; receipt attached.'} -APPROVED = 'Expense recorded: $220.00 to Blue Finch Bistro (meals) — queued for reimbursement.' -REJECTED = 'Error: Tool call invocation was rejected by user.' -POLICY = 'Policy for meals: limit $300 per expense, receipts required over $25. Team meals require attendee count in the memo.' -class Model(http.server.ThreadingHTTPServer): - daemon_threads=True - def __init__(self,mode): - super().__init__(('127.0.0.1',0),Handler) - self.mode=mode; self.physical=[]; self.http_seen=0; self.parse_errors=[]; self.records=[]; self.errors=[]; self.active=0; self.held_active=0; self.connections=0; self.closing=threading.Event(); self.lock=threading.Lock() - def stats(self): - return {'physical':list(self.physical),'requests':len(self.records),'httpSeen':self.http_seen,'parseErrors':list(self.parse_errors),'active':self.active,'heldActive':self.held_active,'connections':self.connections,'errors':self.errors,'records':[dict(r) for r in self.records]} -class Handler(http.server.BaseHTTPRequestHandler): - protocol_version='HTTP/1.1' - def setup(self): - super().setup() - self.server.connections+=1 - def finish(self): - try: super().finish() - finally:self.server.connections-=1 - def log_message(self,*args): pass - def do_GET(self): - if self.path!='/stats': self.send_error(404); return - raw=json.dumps(self.server.stats()).encode() - self.send_response(200); self.send_header('Content-Type','application/json'); self.send_header('Content-Length',str(len(raw))); self.end_headers(); self.wfile.write(raw) - def do_POST(self): - model=self.server - # Count the HTTP request before path/body/JSON validation. The parent owns - # this counter; the append-only checkpoint also survives partial attempts. - with model.lock: - model.http_seen+=1 - model.physical.append({'sequence':model.http_seen,'path':self.path,'time':time.monotonic()}) - if self.path!='/v1/chat/completions': self.send_error(404); return - raw=b'' - try: - raw=self.rfile.read(int(self.headers.get('Content-Length','0'))) - parsed=json.loads(raw) - except Exception as exc: - failure={'path':self.path,'requestBase64':base64.b64encode(raw).decode(),'stage':'body-read-or-json','error':repr(exc)} - with model.lock: - model.parse_errors.append(failure) - - self.send_error(400,'Invalid model request body');return - record={'path':self.path,'requestBase64':base64.b64encode(raw).decode(),'body':parsed,'responseBase64':'','closed':False,'receivedAtMonotonic':time.monotonic(),'peerAddress':self.client_address,'localAddress':self.connection.getsockname()} - with model.lock: model.records.append(record); model.active+=1 - try: - if len(model.records)>30:raise RuntimeError('local fixture model request safety bound exceeded') - body=record['body']; messages=body['messages']; tools=[message for message in messages if message.get('role')=='tool'] - submit=[message for message in tools if message.get('tool_call_id')=='c0_submit_expense'] - if model.mode=='cancel': - record['branch']='hold'; model.held_active+=1;record['heldCounterReleased']=False - self.send_response(200); self.send_header('Content-Type','text/event-stream'); self.send_header('Connection','close'); self.end_headers() - chunk=self.chunk({'role':'assistant','content':''}) - data=('data: '+json.dumps(chunk)+'\n\n').encode(); self.wfile.write(data);self.wfile.flush();record['responseBase64']=base64.b64encode(data).decode() - deadline=time.monotonic()+30 - while not model.closing.is_set() and time.monotonic() new Promise((resolve) => setTimeout(resolve, ms)); -async function freePort() { - const socket = createServer(); - await new Promise((resolve, reject) => { - socket.once('error', reject); - socket.listen(0, '127.0.0.1', resolve); - }); - const port = socket.address().port; - await new Promise((resolve) => socket.close(resolve)); - return port; -} -export async function createNativeMafServer({ - mode = 'approval', - python = resolve(environment, 'bin/python'), - spawnProcess = spawn, -} = {}) { - assert.ok(['approval', 'batch', 'cancel'].includes(mode)); - let child, - exiting, - closing, - ownsLock = false, - observed, - stdout = '', - stderr = '', - final, - startupError, - serviceUrl; - const closure = { process: false, model: false, held: false, lock: false }; - const stats = () => - structuredClone({ serviceUrl, observed, closure, final, stderr }); - // Defined before any resource acquisition; repeated and failed-start closes share one settlement. - const close = () => - (closing ??= (async () => { - try { - if (child) { - child.kill('SIGTERM'); - let timer; - try { - await Promise.race([ - exiting, - new Promise((_, reject) => { - timer = setTimeout( - () => reject(Error('Service shutdown exceeded bound')), - 8000 - ); - }), - ]); - } catch (error) { - child.kill('SIGKILL'); - await exiting; - throw error; - } finally { - clearTimeout(timer); - closure.process = - child.exitCode !== null || child.signalCode !== null; - } - for (const line of stdout.trim().split('\n')) { - try { - const value = JSON.parse(line); - if (value.fixtureClosed) final = value; - } catch {} - } - closure.model = Boolean( - final?.modelThreadClosed && - final.model.active === 0 && - final.model.connections === 0 - ); - closure.held = final?.model.heldActive === 0; - } - } finally { - if (ownsLock) { - rmSync(ownershipLock, { recursive: true }); - ownsLock = false; - closure.lock = true; - } - } - })()); - try { - mkdirSync(ownershipLock); - ownsLock = true; - const port = await freePort(); - const url = `http://127.0.0.1:${port}`; - serviceUrl = url; - child = spawnProcess( - python, - [resolve(root, 'fixtures/react-parity/native-maf-react/service.py')], - { - cwd: root, - stdio: ['ignore', 'pipe', 'pipe'], - env: { - ...fixtureEnvironment(), - PYTHONDONTWRITEBYTECODE: '1', - MAF_FIXTURE_ROOT: root, - MAF_FIXTURE_PORT: String(port), - MAF_FIXTURE_MODE: mode, - AG_UI_INTERNAL_TOKEN: token, - OPENAI_API_KEY: 'fictional-local-only', - OPENAI_CHAT_MODEL: 'fixture-loopback', - NO_PROXY: '127.0.0.1,localhost', - }, - } - ); - exiting = new Promise((resolve) => { - // 'exit' can precede the last stdout bytes. Seal receipts only after - // all child stdio is closed, including failed-spawn error settlement. - child.once('close', resolve); - child.once('error', (error) => { - startupError = error; - }); - }); - // Keep startup failure observed even if the readiness request has not completed. - void exiting.catch(() => undefined); - child.stdout.on('data', (bytes) => { - stdout += bytes; - }); - child.stderr.on('data', (bytes) => { - stderr += bytes; - }); - const observe = async () => { - const response = await fetch(url + '/fixture-stats', { - headers: { 'x-internal-token': token }, - signal: AbortSignal.timeout(2000), - }); - assert.equal(response.status, 200); - observed = await response.json(); - return structuredClone(observed); - }; - const deadline = Date.now() + 20000; - while (true) { - if (startupError) throw startupError; - if (child.exitCode !== null || child.signalCode !== null) - throw Error('Generated service exited during startup: ' + stderr); - try { - await observe(); - break; - } catch (error) { - if (Date.now() > deadline) throw error; - await delay(50); - } - } - return { url, observe, stats, close }; - } catch (error) { - try { - await close(); - } catch (cleanupError) { - error.cleanupError = cleanupError; - } - throw error; - } -} diff --git a/fixtures/react-parity/native-maf-react/server.spec.mjs b/fixtures/react-parity/native-maf-react/server.spec.mjs deleted file mode 100644 index ff081bcc5..000000000 --- a/fixtures/react-parity/native-maf-react/server.spec.mjs +++ /dev/null @@ -1,86 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { spawn } from 'node:child_process'; -import { EventEmitter } from 'node:events'; -import { PassThrough } from 'node:stream'; -import { createNativeMafServer } from './server.mjs'; -test( - 'shutdown waits for delayed stdout pipe closure after actual process exit', - { timeout: 30000 }, - async (t) => { - let owner; - t.after(async () => owner?.close()); - owner = await createNativeMafServer({ - spawnProcess: (...args) => { - const actual = spawn(...args), - observed = new EventEmitter(); - observed.stdout = new PassThrough(); - observed.stderr = actual.stderr; - Object.defineProperties(observed, { - exitCode: { get: () => actual.exitCode }, - signalCode: { get: () => actual.signalCode }, - }); - observed.kill = (signal) => actual.kill(signal); - actual.once('error', (error) => observed.emit('error', error)); - const buffered = []; - actual.once('exit', (...args) => { - observed.emit('exit', ...args); - setTimeout(() => { - for (const bytes of buffered) observed.stdout.write(bytes); - }, 100); - }); - actual.stdout.on('data', (bytes) => buffered.push(bytes)); - actual.once('close', (...args) => - setTimeout(() => { - observed.stdout.end(); - observed.emit('close', ...args); - }, 200) - ); - return observed; - }, - }); - await owner.close(); - assert.equal(owner.stats().final?.fixtureClosed, true); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - } -); -test( - 'failed process startup releases ownership for the next real acquisition', - { timeout: 30000 }, - async (t) => { - await assert.rejects( - createNativeMafServer({ python: '/does-not-exist-native-maf-fixture' }), - /ENOENT/ - ); - let owner; - t.after(async () => owner?.close()); - owner = await createNativeMafServer(); - await owner.close(); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - } -); -test( - 'installed generated service is directly mounted, pinned, isolated and exactly closed', - { timeout: 30000 }, - async (t) => { - let owner; - t.after(async () => { - await owner?.close(); - }); - owner = await createNativeMafServer(); - const stats = await owner.observe(); - assert.equal(stats.entries, 0); - assert.equal(stats.model.httpSeen, 0); - assert.equal(stats.versions['agent-framework-ag-ui'], '1.2.1'); - assert.equal(stats.versions['agent-framework-core'], '1.16.0'); - assert.equal(stats.versions['agent-framework-openai'], '1.14.1'); - assert.equal(stats.versions.openai, '2.54.0'); - assert.match( - stats.modules.generated_server.path, - /deployments\/ag-ui-maf\/server.py$/ - ); - await assert.rejects(createNativeMafServer(), /EEXIST/); - await Promise.all([owner.close(), owner.close()]); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - } -); diff --git a/fixtures/react-parity/native-maf-react/service.py b/fixtures/react-parity/native-maf-react/service.py deleted file mode 100644 index 8c70e4de5..000000000 --- a/fixtures/react-parity/native-maf-react/service.py +++ /dev/null @@ -1,64 +0,0 @@ -"""Test-only direct mount of unchanged generated service with passive instrumentation.""" -import hashlib -import importlib.metadata -import json -import os -from pathlib import Path -import socket -import signal -import sys -import threading -from contextlib import nullcontext -from model import Model - -ROOT = Path(os.environ['MAF_FIXTURE_ROOT']) -model = Model(os.environ['MAF_FIXTURE_MODE']) -model_thread = threading.Thread(target=model.serve_forever, daemon=True) -original_connect = socket.socket.connect -original_connect_ex = socket.socket.connect_ex -def checked_address(address): - if not isinstance(address, tuple) or address[:2] != ('127.0.0.1', model.server_port): - raise RuntimeError('Fixture blocked non-owned model destination') -def connect(self, address): - checked_address(address); return original_connect(self, address) -def connect_ex(self, address): - checked_address(address); return original_connect_ex(self, address) -socket.socket.connect = connect -socket.socket.connect_ex = connect_ex -os.environ['OPENAI_BASE_URL'] = f'http://127.0.0.1:{model.server_port}/v1' -sys.path.insert(0, str(ROOT / 'deployments/ag-ui-maf')) -import server -from deps.microsoft_agent_framework.src import agent as agent_module -target = agent_module.submit_expense.func.__code__ -entries = 0 -def profile(frame, event, arg): - global entries - if event == 'call' and frame.f_code is target: entries += 1 -sys.setprofile(profile) -threading.setprofile(profile) -def sha(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() -@server.app.get('/fixture-stats') -def stats(): - return {'entries':entries, 'model':model.stats(), 'pid':os.getpid(), 'python':sys.version, - 'modelPort':model.server_port, - 'modules':{name:{'path':module.__file__,'sha256':sha(module.__file__)} for name,module in - [('generated_server',server),('generated_agent',agent_module)]}, - 'profile':{'filename':target.co_filename,'firstlineno':target.co_firstlineno,'sourceSha256':sha(target.co_filename),'kind':'passive sys.setprofile; no body replacement'}, - 'versions':{name:importlib.metadata.version(name) for name in - ['agent-framework-ag-ui','agent-framework-core','agent-framework-openai','openai','anyio','sse-starlette']}} - -if __name__ == '__main__': - import uvicorn - # Own the model before starting the service. The outer finally covers failed startup too. - model_thread.start() - try: - owned_server = uvicorn.Server(uvicorn.Config(server.app, host='127.0.0.1', port=int(os.environ['MAF_FIXTURE_PORT']), log_level='warning', timeout_graceful_shutdown=2)) - # Uvicorn's default capture_signals re-raises SIGTERM after shutdown, before - # our final model receipt. Own the signal settlement explicitly instead. - owned_server.capture_signals = nullcontext - for name in (signal.SIGINT, signal.SIGTERM): - signal.signal(name, lambda *_: setattr(owned_server, 'should_exit', True)) - owned_server.run() - finally: - model.closing.set(); model.shutdown(); model.server_close(); model_thread.join(timeout=3) - print(json.dumps({'fixtureClosed':True,'model':model.stats(),'modelThreadClosed':not model_thread.is_alive()}),flush=True) diff --git a/fixtures/react-parity/native-maf-react/test_model.py b/fixtures/react-parity/native-maf-react/test_model.py deleted file mode 100644 index ef708cd61..000000000 --- a/fixtures/react-parity/native-maf-react/test_model.py +++ /dev/null @@ -1,47 +0,0 @@ -"""The fixture is an OpenAI model seam, never an AG-UI event producer.""" -import json -import threading -import unittest -from urllib.request import Request, urlopen -from model import Model - -class ModelTest(unittest.TestCase): - def test_batch_declares_both_calls_in_one_openai_response(self): - model = Model('batch') - thread = threading.Thread(target=model.serve_forever, daemon=True); thread.start() - try: - body = {'messages':[{'role':'user','content':'Blue Finch Bistro'}], 'stream':False} - response=json.load(urlopen(Request(f'http://127.0.0.1:{model.server_port}/v1/chat/completions',data=json.dumps(body).encode(),headers={'Content-Type':'application/json'}),timeout=2)) - calls=response['choices'][0]['message']['tool_calls'] - self.assertEqual([call['function']['name'] for call in calls], ['lookup_expense_policy','submit_expense']) - self.assertEqual(json.loads(calls[1]['function']['arguments'])['expense']['amount_usd'],220) - self.assertEqual(model.stats()['httpSeen'],1) - finally: - model.closing.set(); model.shutdown(); model.server_close(); thread.join() - - def test_physical_invalid_request_is_counted(self): - model = Model('approval') - thread = threading.Thread(target=model.serve_forever, daemon=True) - thread.start() - try: - with self.assertRaises(Exception): - urlopen(Request(f'http://127.0.0.1:{model.server_port}/wrong', data=b'{}'), timeout=2) - self.assertEqual(model.stats()['httpSeen'], 1) - self.assertEqual(model.stats()['requests'], 0) - finally: - model.closing.set(); model.shutdown(); model.server_close(); thread.join() - - def test_actual_typed_openai_lookup_response(self): - model = Model('approval') - thread = threading.Thread(target=model.serve_forever, daemon=True); thread.start() - body = {'messages':[{'role':'user','content':'Blue Finch Bistro'}], 'stream':False} - try: - data=json.dumps(body).encode() - response=json.load(urlopen(Request(f'http://127.0.0.1:{model.server_port}/v1/chat/completions',data=data,headers={'Content-Type':'application/json'}),timeout=2)) - self.assertEqual(response['choices'][0]['message']['tool_calls'][0]['function']['name'],'lookup_expense_policy') - self.assertEqual(model.stats()['records'][0]['body'],body) - self.assertEqual(model.stats()['httpSeen'],1) - finally: - model.closing.set(); model.shutdown(); model.server_close(); thread.join() - -if __name__ == '__main__': unittest.main() diff --git a/fixtures/react-parity/native-mastra-react/model.mjs b/fixtures/react-parity/native-mastra-react/model.mjs deleted file mode 100644 index 3cc7f9409..000000000 --- a/fixtures/react-parity/native-mastra-react/model.mjs +++ /dev/null @@ -1,276 +0,0 @@ -/** Offline Responses seam. Positive AG-UI events are authored by the unchanged service. */ -import http from 'node:http'; -export function selectResponse(body) { - const items = Array.isArray(body.input) ? body.input : []; - const outputs = items.filter((i) => i.type === 'function_call_output'); - if (outputs.length) { - if ( - outputs.length !== 1 || - items.filter((i) => i.type === 'function_call').length !== 1 - ) - throw new Error('Unmatched ambiguous continuation'); - const output = outputs.at(-1); - const calls = items.filter( - (i) => i.type === 'function_call' && i.call_id === output.call_id - ); - if (calls.length !== 1 || calls[0].name !== 'reserve_campsite') - throw new Error('Unmatched continuation correlation'); - let args; - try { - args = JSON.parse(calls[0].arguments); - } catch { - throw new Error('Unmatched malformed reservation arguments'); - } - if (args.site !== 'North Pines' || args.nights !== 2) - throw new Error('Unmatched reservation arguments'); - const approved = - 'Reserved North Pines for 2 night(s) — total $90. Confirmation TP-0288.'; - const declined = - 'Reservation for North Pines was declined by the user. Nothing was booked.'; - if ( - output.output === JSON.stringify(approved) || - output.output === approved - ) - return { - content: - 'The fictional North Pines reservation is approved for 2 nights.', - }; - if ( - output.output === JSON.stringify(declined) || - output.output === declined - ) - return { - content: - 'The North Pines reservation was declined. Nothing was booked.', - }; - throw new Error('Unmatched reservation output'); - } - if (items.some((i) => i.type === 'function_call')) - throw new Error('Unmatched incomplete continuation'); - const users = items.filter((i) => i.role === 'user'); - const last = users.at(-1); - const text = - typeof last?.content === 'string' - ? last.content - : (last?.content ?? []).map((p) => p.text ?? '').join(''); - if (users.length !== 1 || !/reserve.*North Pines.*2 nights/i.test(text)) - throw new Error('Unmatched initial request'); - return { - toolCalls: [ - { - name: 'reserve_campsite', - arguments: { site: 'North Pines', nights: 2 }, - }, - ], - }; -} -let mockCall = 0; -function responsesOutput(entry) { - mockCall++; - if (entry.response.toolCalls) { - return entry.response.toolCalls.map((t, i) => ({ - id: `fc_mock${mockCall}_${i}`, - call_id: `call_mock${mockCall}_${i}`, - type: 'function_call', - name: t.name, - arguments: JSON.stringify(t.arguments ?? {}), - status: 'completed', - })); - } - return [ - { - id: `msg_mock${mockCall}`, - type: 'message', - role: 'assistant', - status: 'completed', - content: [ - { - type: 'output_text', - text: String(entry.response.content), - annotations: [], - logprobs: [], - }, - ], - }, - ]; -} - -function responsesEnvelope(id, model, out, status) { - const completed = status === 'completed'; - return { - id, - object: 'response', - created_at: 1, - completed_at: completed ? 1 : null, - status, - background: false, - error: null, - incomplete_details: null, - instructions: null, - max_output_tokens: null, - max_tool_calls: null, - metadata: null, - model, - output: completed ? out : [], - parallel_tool_calls: true, - previous_response_id: null, - prompt: null, - reasoning: null, - service_tier: 'default', - temperature: null, - text: { format: { type: 'text' } }, - tool_choice: 'auto', - tools: [], - top_p: null, - truncation: 'disabled', - usage: completed - ? { - input_tokens: 0, - input_tokens_details: { cached_tokens: 0 }, - output_tokens: 0, - output_tokens_details: { reasoning_tokens: 0 }, - total_tokens: 0, - } - : null, - }; -} - -export function createMockOpenAi(records, { hold = false } = {}) { - return http.createServer(async (req, res) => { - req.setTimeout(15000, () => req.destroy()); - res.setTimeout(25000, () => res.destroy()); - if (records.length >= 12) { - res.writeHead(429).end(); - return; - } - let body = ''; - for await (const c of req) { - body += c; - if (body.length > 2000000) { - req.destroy(); - return; - } - } - let parsed; - try { - parsed = body ? JSON.parse(body) : {}; - } catch { - res.writeHead(400).end(); - return; - } - if (req.method !== 'POST' || !req.url?.endsWith('/responses')) { - res.writeHead(500).end('unexpected endpoint ' + req.url); - return; - } - records.push({ bytes: body, body: parsed }); - let entry; - try { - entry = { response: selectResponse(parsed) }; - records.at(-1).match = entry.response; - } catch (error) { - records.at(-1).error = String(error); - res.writeHead(422).end(String(error)); - return; - } - if (!entry) { - res.writeHead(500).end('no script entry matched: ' + 'unmatched'); - return; - } - const model = parsed.model ?? 'mock'; - const id = `resp_mock${mockCall}`; - const out = responsesOutput(entry); - let seq = 0; - res.writeHead(200, { 'content-type': 'text/event-stream' }); - const send = (ev) => - res.write( - `data: ${JSON.stringify({ ...ev, sequence_number: seq++ })}\n\n` - ); - send({ - type: 'response.created', - response: responsesEnvelope(id, model, out, 'in_progress'), - }); - if (hold) { - records.at(-1).held = true; - await new Promise((resolve) => res.once('close', resolve)); - records.at(-1).closed = true; - return; - } - for (let oi = 0; oi < out.length; oi++) { - const item = out[oi]; - if (item.type === 'function_call') { - send({ - type: 'response.output_item.added', - output_index: oi, - item: { ...item, arguments: '', status: 'in_progress' }, - }); - // Two argument chunks so partial-args paths (STATE_DELTA) exercise. - const mid = Math.ceil(item.arguments.length / 2); - send({ - type: 'response.function_call_arguments.delta', - output_index: oi, - item_id: item.id, - delta: item.arguments.slice(0, mid), - }); - send({ - type: 'response.function_call_arguments.delta', - output_index: oi, - item_id: item.id, - delta: item.arguments.slice(mid), - }); - send({ - type: 'response.function_call_arguments.done', - output_index: oi, - item_id: item.id, - name: item.name, - arguments: item.arguments, - }); - send({ type: 'response.output_item.done', output_index: oi, item }); - continue; - } - send({ - type: 'response.output_item.added', - output_index: oi, - item: { ...item, status: 'in_progress', content: [] }, - }); - const content = item.content[0]; - send({ - type: 'response.content_part.added', - output_index: oi, - content_index: 0, - item_id: item.id, - part: { ...content, text: '' }, - }); - for (const word of content.text.split(/(?<= )/)) { - send({ - type: 'response.output_text.delta', - output_index: oi, - content_index: 0, - item_id: item.id, - delta: word, - logprobs: [], - }); - } - send({ - type: 'response.output_text.done', - output_index: oi, - content_index: 0, - item_id: item.id, - text: content.text, - logprobs: [], - }); - send({ - type: 'response.content_part.done', - output_index: oi, - content_index: 0, - item_id: item.id, - part: content, - }); - send({ type: 'response.output_item.done', output_index: oi, item }); - } - send({ - type: 'response.completed', - response: responsesEnvelope(id, model, out, 'completed'), - }); - res.end('data: [DONE]\n\n'); - }); -} diff --git a/fixtures/react-parity/native-mastra-react/model.spec.mjs b/fixtures/react-parity/native-mastra-react/model.spec.mjs deleted file mode 100644 index 4de337a76..000000000 --- a/fixtures/react-parity/native-mastra-react/model.spec.mjs +++ /dev/null @@ -1,77 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { selectResponse } from './model.mjs'; -const call = { - type: 'function_call', - call_id: 'opaque', - name: 'reserve_campsite', - arguments: JSON.stringify({ site: 'North Pines', nights: 2 }), -}; -const input = (output) => ({ - input: [ - { role: 'user', content: 'Please reserve North Pines for 2 nights.' }, - call, - { type: 'function_call_output', call_id: 'opaque', output }, - ], -}); -test('initial request calls the actual reservation tool', () => - assert.equal( - selectResponse({ - input: [ - { role: 'user', content: 'Please reserve North Pines for 2 nights.' }, - ], - }).toolCalls[0].name, - 'reserve_campsite' - )); -test('approval continuation matches exact correlated output', () => - assert.match( - selectResponse( - input( - JSON.stringify( - 'Reserved North Pines for 2 night(s) — total $90. Confirmation TP-0288.' - ) - ) - ).content, - /approved/ - )); -test('decline continuation is truthful and matched before initial text', () => - assert.match( - selectResponse( - input( - JSON.stringify( - 'Reservation for North Pines was declined by the user. Nothing was booked.' - ) - ) - ).content, - /Nothing was booked/ - )); -test('unmatched and incorrectly correlated continuations fail closed', () => { - assert.throws(() => selectResponse(input('Booked')), /Unmatched/); - const body = input( - JSON.stringify( - 'Reservation for North Pines was declined by the user. Nothing was booked.' - ) - ); - body.input[2].call_id = 'other'; - assert.throws(() => selectResponse(body), /Unmatched/); - assert.throws( - () => selectResponse({ input: [{ role: 'user', content: 'weather' }] }), - /Unmatched/ - ); -}); -test('ambiguous tool-output histories cannot grant a truthful continuation', () => { - const body = input( - JSON.stringify( - 'Reservation for North Pines was declined by the user. Nothing was booked.' - ) - ); - body.input.push({ ...body.input[2] }); - assert.throws(() => selectResponse(body), /Unmatched/); - const duplicate = input( - JSON.stringify( - 'Reservation for North Pines was declined by the user. Nothing was booked.' - ) - ); - duplicate.input.push({ ...call, call_id: 'unrelated' }); - assert.throws(() => selectResponse(duplicate), /Unmatched/); -}); diff --git a/fixtures/react-parity/native-mastra-react/server.mjs b/fixtures/react-parity/native-mastra-react/server.mjs deleted file mode 100644 index b70de28bf..000000000 --- a/fixtures/react-parity/native-mastra-react/server.mjs +++ /dev/null @@ -1,484 +0,0 @@ -/** Test-only owned process; service imports occur after all local configuration. */ -import assert from 'node:assert/strict'; -import { fork, execFileSync } from 'node:child_process'; -import http from 'node:http'; -import net from 'node:net'; -import dns from 'node:dns'; -import { createHash } from 'node:crypto'; -import { mkdtempSync, rmSync, existsSync, readFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { resolve, join, dirname } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { setTimeout as delay } from 'node:timers/promises'; -import { createMockOpenAi } from './model.mjs'; -const source = fileURLToPath(import.meta.url); -const defaultRoot = resolve(dirname(source), '../../..'); -const MAX = 2_000_000; -export async function bounded(promise, label, timeout = 15000) { - let timer; - try { - return await Promise.race([ - promise, - new Promise((_, reject) => { - timer = setTimeout( - () => reject(new Error(label + ' timed out')), - timeout - ); - }), - ]); - } finally { - clearTimeout(timer); - } -} -async function listen(server, port = 0) { - await bounded( - new Promise((yes, no) => { - server.once('error', no); - server.listen(port, '127.0.0.1', () => { - server.removeListener('error', no); - yes(); - }); - }), - 'listen' - ); - return server.address().port; -} -async function closeServer(server) { - if (!server) return; - await bounded( - new Promise((yes) => { - server.close(yes); - server.closeAllConnections(); - }), - 'server close' - ); -} -async function portAbsent(port) { - return new Promise((yes) => { - const socket = net.connect({ host: '127.0.0.1', port }); - socket.setTimeout(1000, () => { - socket.destroy(); - yes(false); - }); - socket.once('connect', () => { - socket.destroy(); - yes(false); - }); - socket.once('error', (error) => { - socket.destroy(); - yes(error.code === 'ECONNREFUSED'); - }); - }); -} -function processAbsent(pid) { - try { - process.kill(pid, 0); - return false; - } catch (error) { - if (error.code === 'ESRCH') return true; - throw error; - } -} -function identity(pid) { - return execFileSync('ps', ['-o', 'lstart=,args=', '-p', String(pid)], { - encoding: 'utf8', - timeout: 1000, - }).trim(); -} -export async function createNativeMastraServer({ - root = defaultRoot, - mode = 'normal', - failStartup = false, - port = 0, -} = {}) { - assert.ok(Number.isInteger(port) && port >= 0 && port <= 65535); - assert.ok(['normal', 'lost-terminal', 'notice-only', 'held'].includes(mode)); - const database = mkdtempSync(join(tmpdir(), 'threadplane-native-mastra-')); - const env = Object.fromEntries( - Object.entries(process.env).filter( - ([key]) => - !/(?:KEY|TOKEN|SECRET|LANGSMITH|LANGCHAIN|OPENAI|MASTRA|FIXTURE|LIBRARY)/i.test( - key - ) - ) - ); - Object.assign(env, { - AG_UI_MASTRA_DB_PATH: join(database, 'mastra.db'), - AG_UI_INTERNAL_TOKEN: 'offline-native-mastra', - OPENAI_API_KEY: 'sk-offline-unused', - LANGSMITH_TRACING: 'false', - LANGCHAIN_TRACING_V2: 'false', - }); - const child = fork( - source, - [ - '--worker', - root, - mode, - String(port), - ...(failStartup ? ['--fail-startup'] : []), - ], - { cwd: root, env, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] } - ); - let state = { requests: [], modelRequests: [], faults: [] }, - stderr = '', - exited = false, - closing, - ownedIdentity; - const closure = { - childAbsent: false, - databaseAbsent: false, - responsesClosed: false, - socketsClosed: false, - serversClosed: false, - portsAbsent: false, - }; - let readyResolve, readyReject; - const ready = new Promise((yes, no) => { - readyResolve = yes; - readyReject = no; - }); - child.stderr.on('data', (chunk) => { - stderr = (stderr + chunk).slice(-MAX); - }); - child.stdout.on('data', () => undefined); - child.on('error', readyReject); - child.on('exit', (code, signal) => { - exited = true; - readyReject(new Error(`worker exited ${code}/${signal}: ${stderr}`)); - }); - child.on('message', (message) => { - if (message.stats) state = message.stats; - if (message.ready) readyResolve(message.ready); - if (message.error) readyReject(new Error(message.error)); - }); - const stats = () => - structuredClone({ - ...state, - stderr, - closure, - ownership: { pid: child.pid, identity: ownedIdentity, database, source }, - }); - async function close() { - return (closing ??= (async () => { - if (!exited && child.connected) child.send({ close: true }); - const until = Date.now() + 5000; - while (!exited && Date.now() < until) await delay(20); - if (!exited) { - assert.equal( - identity(child.pid), - ownedIdentity, - 'exact owned process identity' - ); - child.kill('SIGTERM'); - } - const final = Date.now() + 5000; - while (!exited && Date.now() < final) await delay(20); - if (!exited) { - assert.equal(identity(child.pid), ownedIdentity); - child.kill('SIGKILL'); - await bounded( - new Promise((yes) => child.once('exit', yes)), - 'owned child kill' - ); - } - rmSync(database, { recursive: true, force: true }); - closure.childAbsent = exited && processAbsent(child.pid); - closure.databaseAbsent = !existsSync(database); - closure.responsesClosed = - state.closed?.responsesClosed ?? state.requests.every((r) => r.closed); - closure.socketsClosed = state.closed?.socketsClosed ?? exited; - closure.serversClosed = state.closed?.serversClosed ?? exited; - closure.portsAbsent = ( - await Promise.all((state.ownedPorts ?? []).map(portAbsent)) - ).every(Boolean); - assert.ok( - Object.values(closure).every(Boolean), - 'owned resource closure' - ); - })()); - } - try { - ownedIdentity = identity(child.pid); - assert.ok(ownedIdentity.includes(source), 'exact launched worker command'); - const endpoint = await bounded(ready, 'worker ready'); - return { url: endpoint.url, pid: child.pid, stats, close }; - } catch (error) { - await close(); - error.fixtureClosure = stats(); - throw error; - } -} -async function worker() { - const root = process.argv[3], - mode = process.argv[4], - requestedPort = Number(process.argv[5]); - const state = { - requests: [], - serviceRequests: [], - ownedPorts: [], - modelRequests: [], - faults: [], - networkDenials: [], - closed: null, - }; - const responses = new Set(), - sockets = new Set(); - let mock, service, proxy, closing; - const publish = () => { - if (process.connected) process.send({ stats: state }); - }; - // Defense in depth: configuration is loopback; deny outbound sockets, DNS and fetch. - const local = (host) => - host === '127.0.0.1' || host === 'localhost' || host === '::1'; - const connect = net.Socket.prototype.connect; - net.Socket.prototype.connect = function (...args) { - const first = Array.isArray(args[0]) ? args[0][0] : args[0]; - const options = - typeof first === 'object' - ? first - : { host: typeof args[1] === 'string' ? args[1] : 'localhost' }; - if (options.path || !local(options.host ?? 'localhost')) { - state.networkDenials.push(String(options.host ?? options.path)); - throw new Error('Offline fixture denies foreign connection'); - } - return connect.apply(this, args); - }; - const lookup = dns.lookup; - dns.lookup = function (host, ...args) { - if (!local(host)) { - state.networkDenials.push(host); - throw new Error('Offline fixture denies DNS'); - } - return lookup.call(this, host, ...args); - }; - const originalFetch = globalThis.fetch; - globalThis.fetch = (input, options) => { - const url = new URL( - typeof input === 'string' || input instanceof URL ? input : input.url - ); - if (!local(url.hostname)) throw new Error('Offline fixture denies fetch'); - return originalFetch(input, options); - }; - function track(server) { - server.on('connection', (socket) => { - sockets.add(socket); - socket.once('close', () => sockets.delete(socket)); - }); - server.on('request', (_, response) => { - responses.add(response); - response.once('close', () => responses.delete(response)); - }); - } - async function close() { - return (closing ??= (async () => { - for (const response of responses) response.destroy(); - for (const socket of sockets) socket.destroy(); - await closeServer(proxy); - await closeServer(service); - await closeServer(mock); - await delay(20); - state.closed = { - responsesClosed: responses.size === 0, - socketsClosed: sockets.size === 0, - serversClosed: true, - }; - publish(); - process.disconnect(); - })()); - } - process.once('SIGTERM', () => void close().then(() => process.exit(0))); - process.once('SIGINT', () => void close().then(() => process.exit(0))); - process.on('message', (message) => { - if (message.close) void close().then(() => process.exit(0)); - }); - try { - mock = createMockOpenAi(state.modelRequests, { hold: mode === 'held' }); - track(mock); - const modelPort = await listen(mock); - state.ownedPorts.push(modelPort); - process.env.OPENAI_BASE_URL = `http://127.0.0.1:${modelPort}/v1`; - state.configuration = { - modelUrl: process.env.OPENAI_BASE_URL, - database: process.env.AG_UI_MASTRA_DB_PATH, - offline: true, - mode, - }; - state.codePins = Object.fromEntries( - ['server.mjs', 'agents.mjs', 'package-lock.json'].map((name) => [ - name, - createHash('sha256') - .update(readFileSync(resolve(root, 'deployments/ag-ui-mastra', name))) - .digest('hex'), - ]) - ); - if (process.argv.includes('--fail-startup')) - throw new Error('deliberate startup failure'); - const { createAgUiServer } = await import( - resolve(root, 'deployments/ag-ui-mastra/server.mjs') - ); - service = createAgUiServer(); - service.prependListener('request', (request, response) => { - if (request.method === 'POST') { - const record = { path: request.url, closed: false }; - state.serviceRequests.push(record); - response.once('close', () => { - record.closed = true; - publish(); - }); - } - }); - track(service); - const servicePort = await listen(service); - state.ownedPorts.push(servicePort); - proxy = http.createServer(async (request, response) => { - request.setTimeout(15000, () => request.destroy()); - response.setTimeout(25000, () => response.destroy()); - if (request.url === '/stats') { - response - .writeHead(200, { 'content-type': 'application/json' }) - .end(JSON.stringify(state)); - return; - } - if (request.url !== '/agent' || request.method !== 'POST') { - response.writeHead(404).end(); - return; - } - if (state.requests.length >= 12) { - response.writeHead(429).end(); - return; - } - const record = { - body: null, - bytes: '', - upstreamBytes: '', - sentBytes: '', - events: [], - closed: false, - upstreamClosed: false, - faults: [], - }; - state.requests.push(record); - let incoming; - const upstream = http.request( - { - host: '127.0.0.1', - port: servicePort, - path: '/agent/mastra', - method: 'POST', - headers: { - 'content-type': 'application/json', - 'x-internal-token': process.env.AG_UI_INTERNAL_TOKEN, - }, - }, - (actual) => { - incoming = actual; - response.writeHead(actual.statusCode, { - 'content-type': - actual.headers['content-type'] ?? 'application/json', - 'cache-control': 'no-store', - }); - let buffer = ''; - actual.on('data', (chunk) => { - record.upstreamBytes += chunk; - if (record.upstreamBytes.length > MAX) { - actual.destroy(); - response.destroy(); - return; - } - buffer += chunk; - while (buffer.includes('\n\n')) { - const at = buffer.indexOf('\n\n'), - frame = buffer.slice(0, at + 2); - buffer = buffer.slice(at + 2); - let event; - try { - event = JSON.parse(frame.replace(/^data: /, '')); - } catch { - response.destroy(); - return; - } - record.events.push(event); - const terminal = - event.type === 'RUN_FINISHED' && - event.runId === record.body.runId && - event.threadId === record.body.threadId; - const drop = - (mode === 'lost-terminal' && record.body.resume && terminal) || - (mode === 'notice-only' && - terminal && - event.outcome?.type === 'interrupt'); - if (drop) { - const fault = { - type: 'removed-real-root-terminal', - event, - bytes: frame, - }; - record.faults.push(fault); - state.faults.push(fault); - } else if (!response.destroyed) { - response.write(frame); - record.sentBytes += frame; - } - if (mode === 'held' && event.type === 'RUN_STARTED') { - actual.pause(); - record.held = true; - state.faults.push({ - type: 'held-real-root-start', - event, - bytes: frame, - }); - publish(); - return; - } - } - publish(); - }); - actual.once('end', () => { - record.upstreamEnded = true; - if (!record.held) response.end(); - publish(); - }); - actual.once('close', () => { - record.upstreamClosed = true; - record.upstreamAborted = !record.upstreamEnded; - publish(); - }); - actual.on('error', () => response.destroy()); - } - ); - response.once('close', () => { - record.closed = true; - incoming?.destroy(); - upstream.destroy(); - publish(); - }); - upstream.on('error', () => { - if (!response.headersSent) response.writeHead(502).end(); - else response.destroy(); - }); - try { - for await (const chunk of request) { - record.bytes += chunk; - assert.ok(record.bytes.length <= MAX); - } - record.body = JSON.parse(record.bytes); - upstream.end(record.bytes); - publish(); - } catch { - upstream.destroy(); - response.writeHead(400).end(); - } - }); - track(proxy); - const port = await listen(proxy, requestedPort); - state.ownedPorts.push(port); - publish(); - process.send({ ready: { url: `http://127.0.0.1:${port}` } }); - } catch (error) { - if (process.connected) process.send({ error: String(error) }); - await close(); - process.exit(1); - } -} -if (process.argv.includes('--worker')) await worker(); diff --git a/fixtures/react-parity/native-mastra-react/server.spec.mjs b/fixtures/react-parity/native-mastra-react/server.spec.mjs deleted file mode 100644 index 0d53c99b5..000000000 --- a/fixtures/react-parity/native-mastra-react/server.spec.mjs +++ /dev/null @@ -1,123 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { createNativeMastraServer } from './server.mjs'; -test('fresh actual service uses isolated database and closes exact owned child', async () => { - const owner = await createNativeMastraServer(); - const response = await fetch(owner.url + '/agent', { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify({ - threadId: crypto.randomUUID(), - runId: crypto.randomUUID(), - protocolVersion: '1.0', - messages: [ - { - id: crypto.randomUUID(), - role: 'user', - content: 'Please reserve North Pines for 2 nights.', - }, - ], - state: {}, - tools: [], - context: [], - forwardedProps: {}, - }), - }); - const bytes = await response.text(); - assert.match(bytes, /mastra:tool_suspend/); - assert.equal(owner.stats().requests.length, 1); - await owner.close(); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - assert.equal(owner.stats().closure.portsAbsent, true); -}); -test('startup failure closes owned process and database', async () => { - await assert.rejects( - createNativeMastraServer({ failStartup: true }), - (error) => { - assert.ok(Object.values(error.fixtureClosure.closure).every(Boolean)); - return true; - } - ); -}); -test('listen failure removes exact temporary database and owned child', async () => { - const { createServer } = await import('node:http'); - const blocker = createServer(); - await new Promise((yes) => blocker.listen(0, '127.0.0.1', yes)); - try { - await assert.rejects( - async () => { - const unexpected = await createNativeMastraServer({ - port: blocker.address().port, - }); - await unexpected.close(); - }, - (error) => { - assert.match(error.message, /EADDRINUSE/); - assert.ok(Object.values(error.fixtureClosure.closure).every(Boolean)); - return true; - } - ); - } finally { - await new Promise((yes) => blocker.close(yes)); - } -}); -test('SIGTERM during held actual stream closes upstream before cleanup', async () => { - const owner = await createNativeMastraServer({ mode: 'held' }); - const response = await fetch(owner.url + '/agent', { - method: 'POST', - headers: { 'content-type': 'application/json' }, - body: JSON.stringify({ - threadId: crypto.randomUUID(), - runId: crypto.randomUUID(), - protocolVersion: '1.0', - messages: [ - { - id: crypto.randomUUID(), - role: 'user', - content: 'Please reserve North Pines for 2 nights.', - }, - ], - state: {}, - tools: [], - context: [], - forwardedProps: {}, - }), - }); - const reader = response.body.getReader(); - await reader.read(); - process.kill(owner.pid, 'SIGTERM'); - await reader.cancel(); - await owner.close(); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - assert.equal(owner.stats().requests[0].closed, true); - assert.equal(owner.stats().requests[0].upstreamClosed, true); -}); -test('inherited Library and provider environment cannot contaminate actual fixture', async () => { - const names = [ - 'OPENAI_BASE_URL', - 'AG_UI_MASTRA_DB_PATH', - 'THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT', - ]; - const saved = names.map((name) => process.env[name]); - for (const name of names) - process.env[name] = 'https://foreign.invalid/library'; - let owner; - try { - owner = await createNativeMastraServer(); - assert.match( - owner.stats().configuration.modelUrl, - /^http:\/\/127\.0\.0\.1:/ - ); - assert.ok( - owner - .stats() - .configuration.database.startsWith(owner.stats().ownership.database) - ); - } finally { - await owner?.close(); - names.forEach((name, i) => { - if (saved[i] === undefined) delete process.env[name]; - else process.env[name] = saved[i]; - }); - } -}); diff --git a/fixtures/react-parity/native-strands-react/configuration.mjs b/fixtures/react-parity/native-strands-react/configuration.mjs deleted file mode 100644 index a86160201..000000000 --- a/fixtures/react-parity/native-strands-react/configuration.mjs +++ /dev/null @@ -1,23 +0,0 @@ -/** Fixture ownership shared by frozen preparation and active runtimes. */ -import { createHash } from 'node:crypto'; -import { tmpdir } from 'node:os'; -import { dirname, resolve, join } from 'node:path'; -import { fileURLToPath } from 'node:url'; -export const root = resolve( - dirname(fileURLToPath(import.meta.url)), - '../../..' -); -export const project = resolve(root, 'deployments/ag-ui-dev/deps/aws_strands'); -export const environment = join( - tmpdir(), - 'threadplane-native-strands-react-' + - createHash('sha256').update(root).digest('hex').slice(0, 16) -); -export const preparationLock = environment + '.owner'; -export function fixtureEnvironment() { - return Object.fromEntries( - ['PATH', 'HOME', 'TMPDIR', 'SSL_CERT_FILE', 'UV_CACHE_DIR'] - .filter((key) => process.env[key]) - .map((key) => [key, process.env[key]]) - ); -} diff --git a/fixtures/react-parity/native-strands-react/model.py b/fixtures/react-parity/native-strands-react/model.py deleted file mode 100644 index 0a22e125b..000000000 --- a/fixtures/react-parity/native-strands-react/model.py +++ /dev/null @@ -1,281 +0,0 @@ -"""Local OpenAI-only seam. Positive AG-UI always comes from the real bridge. - -Every physical request is retained before validation. Expected successful request -counts are rejection thresholds, not a claimed physical-attempt cap. The real -SDK retains its own retry defaults; invalid input receives non-retryable HTTP 400. -At most eight explicit user turns/fresh histories per owner; no operation retry. -""" - -import base64 -import copy -import http.server -import json -import select -import socket -import threading -import time -import uuid - -TOPIC = "Q3 roadmap review" -SLOT = "Tuesday 10:00" -BOOKED = f"Meeting booked for {SLOT}: {TOPIC}" -DECLINED = f"The human declined. Meeting NOT booked: {TOPIC}" -LOOKUP = {"day": "Tuesday", "slots": ["10:00", "15:00"]} - - -class Model(http.server.ThreadingHTTPServer): - daemon_threads = False - block_on_close = True - - def __init__(self, mode): - if mode not in ("approval", "batch", "cancel"): - raise ValueError("Unknown fixture mode") - super().__init__(("127.0.0.1", 0), Handler) - self.mode = mode - self.records = [] - self.closing = threading.Event() - self.lock = threading.RLock() - self.identity = uuid.uuid4().hex - self.turn = 0 - self.users = [] - self.phase = "new" - self.calls = [] - self.failed = False - self.connections = 0 - - def update_record(self, record, **values): - with self.lock: - record.update(values) - - def stats(self): - with self.lock: - return copy.deepcopy( - { - "httpSeen": len(self.records), - "records": self.records, - "active": sum(not r["closed"] for r in self.records), - "heldActive": sum( - r.get("held", False) and not r["closed"] for r in self.records - ), - "connections": self.connections, - } - ) - - def plan(self, body): - if self.failed: - raise ValueError("Scenario already failed; explicit reset required") - if not isinstance(body, dict) or body.get("stream") is not True: - raise ValueError("Expected streaming OpenAI request") - messages = body.get("messages") - if not isinstance(messages, list) or not messages: - raise ValueError("Missing messages") - users = [m for m in messages if m.get("role") == "user"] - if not users: - raise ValueError("Missing user turn") - current = messages[ - max(i for i, m in enumerate(messages) if m.get("role") == "user") + 1 : - ] - # OpenAI does not receive native threadId. An empty current-turn history - # starts a new explicit invocation; never infer thread identity from text. - # Nonempty continuation must retain the exact owned user-history prefix. - if not current: - if self.turn >= 8: - raise ValueError("Explicit turn budget exceeded") - self.turn += 1 - self.users = copy.deepcopy(users) - self.phase = "lookup" - self.calls = [ - { - "id": f"fixture_{self.identity}_{self.turn}_{name}", - "type": "function", - "function": {"name": name, "arguments": json.dumps(args)}, - } - for name, args in [ - ("check_availability", {"day": "Tuesday"}), - ("book_meeting", {"topic": TOPIC, "slot": SLOT}), - ] - ] - elif users != self.users: - raise ValueError("Continuation conversation changed") - tools = [m for m in current if m.get("role") == "tool"] - declarations = [ - call - for m in current - if m.get("role") == "assistant" - for call in m.get("tool_calls", []) - ] - expected = 0 if self.phase == "lookup" else (1 if self.phase == "book" else 2) - if self.phase == "complete": - raise ValueError("Repeated completed operation") - if len(tools) != expected or len(declarations) != expected: - raise ValueError("Unexpected tool result/declaration count") - for index, call in enumerate(declarations): - target = self.calls[index] - if ( - call.get("id") != target["id"] - or call.get("function", {}).get("name") != target["function"]["name"] - or json.loads(call["function"]["arguments"]) - != json.loads(target["function"]["arguments"]) - ): - raise ValueError("Unrecognized tool declaration") - if tools[index].get("tool_call_id") != target["id"]: - raise ValueError("Unrecognized tool result") - if tools and json.loads(tools[0]["content"]) != LOOKUP: - raise ValueError("Unexpected read-only lookup result") - if expected == 2: - canonical = tools[1]["content"] - if canonical not in (BOOKED, DECLINED): - raise ValueError("Unexpected canonical booking result") - self.phase = "complete" - return ( - [ - {"role": "assistant", "content": ""}, - {"content": "The Q3 roadmap review booking decision is complete."}, - ], - "stop", - canonical, - ) - if self.mode == "cancel": - return None, None, None - selected = ( - self.calls - if self.mode == "batch" - else [self.calls[0 if self.phase == "lookup" else 1]] - ) - self.phase = ( - "finish" if self.mode == "batch" or self.phase == "book" else "book" - ) - return ( - [ - {"role": "assistant", "content": ""}, - { - "tool_calls": [ - {"index": i, **call} for i, call in enumerate(selected) - ] - }, - ], - "tool_calls", - None, - ) - - -class Handler(http.server.BaseHTTPRequestHandler): - protocol_version = "HTTP/1.1" - - def setup(self): - super().setup() - self.connection.settimeout(3) - with self.server.lock: - self.server.connections += 1 - - def finish(self): - try: - super().finish() - finally: - with self.server.lock: - self.server.connections -= 1 - - def log_message(self, *args): - pass - - def chunk(self, delta, finish=None): - return { - "id": "chatcmpl-fixture", - "object": "chat.completion.chunk", - "created": int(time.time()), - "model": "fixture-loopback", - "choices": [{"index": 0, "delta": delta, "finish_reason": finish}], - } - - def do_POST(self): - model = self.server - record = { - "path": self.path, - "method": self.command, - "requestHeaders": list(self.headers.items()), - "requestBase64": "", - "responseBase64": "", - "received": time.monotonic(), - "closed": False, - } - with model.lock: - record["index"] = len(model.records) + 1 - model.records.append(record) - try: - length = int(self.headers.get("Content-Length", "0")) - if length < 0 or length > 2_000_000: - raise ValueError("Request body bound exceeded") - raw = self.rfile.read(length) - model.update_record(record, requestBase64=base64.b64encode(raw).decode()) - if self.command != "POST" or self.path != "/v1/chat/completions": - raise ValueError("Unexpected provider route") - if record["index"] > 24: - raise ValueError("Total expected-success request budget exceeded") - body = json.loads(raw) - model.update_record(record, body=body) - with model.lock: - deltas, finish, canonical = model.plan(body) - if canonical: - model.update_record(record, canonicalContinuation=canonical) - if deltas is None: - self.send_response(200) - self.send_header("Content-Type", "text/event-stream") - self.send_header("Connection", "close") - self.end_headers() - data = ( - "data: " - + json.dumps(self.chunk({"role": "assistant", "content": ""})) - + "\n\n" - ).encode() - model.update_record( - record, responseBase64=base64.b64encode(data).decode() - ) - self.wfile.write(data) - self.wfile.flush() - model.update_record(record, held=True) - deadline = time.monotonic() + 20 - while not model.closing.is_set() and time.monotonic() < deadline: - ready, _, _ = select.select([self.connection], [], [], 0.02) - if ready: - try: - closed = self.connection.recv(1, socket.MSG_PEEK) == b"" - except (ConnectionResetError, OSError): - closed = True - if closed: - model.update_record(record, peerClosed=True) - model.update_record(record, peerClosedAt=time.monotonic()) - break - model.update_record(record, cleanupReleased=model.closing.is_set()) - model.update_record( - record, - heldDeadlineExceeded=not record.get("peerClosed", False) - and not model.closing.is_set(), - ) - return - data = b"".join( - ("data: " + json.dumps(self.chunk(delta)) + "\n\n").encode() - for delta in deltas + [{}] - ) - data += ( - "data: " + json.dumps(self.chunk({}, finish)) + "\n\ndata: [DONE]\n\n" - ).encode() - model.update_record(record, responseBase64=base64.b64encode(data).decode()) - self.send_response(200) - self.send_header("Content-Type", "text/event-stream") - self.send_header("Content-Length", str(len(data))) - self.end_headers() - self.wfile.write(data) - self.wfile.flush() - except Exception as error: - model.update_record(record, error=repr(error)) - with model.lock: - model.failed = True - try: - self.send_error(400, "Invalid bounded fixture request") - except OSError: - pass - finally: - model.update_record(record, closed=True) - self.close_connection = True - - do_GET = do_HEAD = do_PUT = do_PATCH = do_DELETE = do_OPTIONS = do_POST diff --git a/fixtures/react-parity/native-strands-react/server.mjs b/fixtures/react-parity/native-strands-react/server.mjs deleted file mode 100644 index 6dd6e08d8..000000000 --- a/fixtures/react-parity/native-strands-react/server.mjs +++ /dev/null @@ -1,211 +0,0 @@ -/** Owns one isolated installed generated-service process and its loopback model. */ -import assert from 'node:assert/strict'; -import { spawn } from 'node:child_process'; -import { mkdirSync, rmSync } from 'node:fs'; -import { resolve } from 'node:path'; -import { environment, fixtureEnvironment, root } from './configuration.mjs'; -export const token = 'local-fictional-meeting-fixture'; -const ownershipLock = environment + '.owner'; -const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); -const readinessPrefix = 'STRANDS_FIXTURE_READY '; -export async function createNativeStrandsServer({ - mode = 'approval', - python = resolve(environment, 'bin/python'), - spawnProcess = spawn, - startupMs = 20000, -} = {}) { - assert.ok(['approval', 'batch', 'cancel'].includes(mode)); - assert.ok(Number.isFinite(startupMs) && startupMs > 0); - const deadline = performance.now() + startupMs; - let child, - exiting, - childClosed = false, - closing, - ownsLock = false, - observed, - stdout = '', - pendingStdout = '', - stderr = '', - final, - startupError, - serviceUrl; - const closure = { process: false, model: false, held: false, lock: false }; - const stats = () => - structuredClone({ serviceUrl, observed, closure, final, stderr }); - // Defined before any resource acquisition; repeated and failed-start closes share one settlement. - const close = () => - (closing ??= (async () => { - try { - if (child) { - child.kill('SIGTERM'); - let timer; - try { - await Promise.race([ - exiting, - new Promise((_, reject) => { - timer = setTimeout( - () => reject(Error('Service shutdown exceeded bound')), - 8000 - ); - }), - ]); - } catch (error) { - child.kill('SIGKILL'); - let killTimer; - try { - await Promise.race([ - exiting, - new Promise((_, reject) => { - killTimer = setTimeout( - () => reject(Error('Service kill exceeded bound')), - 3000 - ); - }), - ]); - } finally { - clearTimeout(killTimer); - } - throw error; - } finally { - clearTimeout(timer); - closure.process = - child.exitCode !== null || child.signalCode !== null; - } - for (const line of stdout.trim().split('\n')) { - try { - const value = JSON.parse(line); - if (value.fixtureClosed) final = value; - } catch {} - } - closure.model = Boolean( - final?.modelThreadClosed && - final.model.active === 0 && - final.model.connections === 0 - ); - closure.held = final?.model.heldActive === 0; - } - } finally { - // Never hand ownership to a replacement until actual child stdio close. - if (ownsLock && (!child || childClosed)) { - rmSync(ownershipLock, { recursive: true }); - ownsLock = false; - closure.lock = true; - } - } - })()); - try { - mkdirSync(ownershipLock); - ownsLock = true; - child = spawnProcess( - python, - [resolve(root, 'fixtures/react-parity/native-strands-react/service.py')], - { - cwd: root, - stdio: ['ignore', 'pipe', 'pipe'], - env: { - ...fixtureEnvironment(), - PYTHONDONTWRITEBYTECODE: '1', - STRANDS_FIXTURE_ROOT: root, - STRANDS_FIXTURE_MODE: mode, - AG_UI_INTERNAL_TOKEN: token, - OPENAI_API_KEY: 'fictional-local-only', - OPENAI_CHAT_MODEL: 'fixture-loopback', - NO_PROXY: '127.0.0.1,localhost', - }, - } - ); - exiting = new Promise((resolve) => { - // 'exit' can precede the last stdout bytes. Seal receipts only after - // all child stdio is closed, including failed-spawn error settlement. - child.once('close', (...args) => { - childClosed = true; - resolve(...args); - }); - child.once('error', (error) => { - startupError = error; - }); - }); - // Keep startup failure observed even if the readiness request has not completed. - void exiting.catch(() => undefined); - child.stdout.on('data', (bytes) => { - stdout += bytes; - pendingStdout += bytes; - let newline; - while ((newline = pendingStdout.indexOf('\n')) !== -1) { - const line = pendingStdout.slice(0, newline); - pendingStdout = pendingStdout.slice(newline + 1); - if (!line.startsWith(readinessPrefix)) continue; - try { - assert.equal(serviceUrl, undefined, 'Duplicate readiness record'); - const ready = JSON.parse(line.slice(readinessPrefix.length)); - assert.deepEqual(Object.keys(ready).sort(), ['host', 'pid', 'port']); - assert.equal(ready.host, '127.0.0.1'); - assert.ok( - Number.isSafeInteger(ready.port) && - ready.port > 0 && - ready.port <= 65535 - ); - assert.ok(Number.isSafeInteger(ready.pid) && ready.pid > 0); - // Test proxies may defer spawn; require the actual child PID at receipt. - assert.equal(ready.pid, child.pid); - serviceUrl = `http://127.0.0.1:${ready.port}`; - } catch (cause) { - startupError ??= Error('Invalid native service readiness record', { - cause, - }); - } - } - }); - child.stderr.on('data', (bytes) => { - stderr += bytes; - }); - const observe = async (timeoutMs = 2000) => { - const response = await fetch(serviceUrl + '/fixture-stats', { - headers: { 'x-internal-token': token }, - signal: AbortSignal.timeout(Math.max(1, timeoutMs)), - }); - assert.equal(response.status, 200); - observed = await response.json(); - if (observed.pid !== child.pid) { - startupError ??= Error('Native service stats PID mismatch'); - throw startupError; - } - return structuredClone(observed); - }; - - while (true) { - if (startupError) throw startupError; - if (childClosed || child.exitCode !== null || child.signalCode !== null) - throw Error('Generated service exited during startup: ' + stderr); - const remaining = deadline - performance.now(); - if (remaining <= 0) - throw Error('Native service startup deadline exceeded'); - if (!serviceUrl) { - await delay(Math.min(50, Math.ceil(remaining))); - continue; - } - try { - await observe(Math.min(2000, Math.ceil(remaining))); - if (startupError) throw startupError; - if (performance.now() > deadline) - throw Error('Native service startup deadline exceeded'); - break; - } catch (error) { - if (startupError) throw startupError; - if (performance.now() >= deadline) - throw Error('Native service startup deadline exceeded', { - cause: error, - }); - await delay(Math.min(50, Math.max(0, deadline - performance.now()))); - } - } - return { url: serviceUrl, observe, stats, close }; - } catch (error) { - try { - await close(); - } catch (cleanupError) { - error.cleanupError = cleanupError; - } - throw error; - } -} diff --git a/fixtures/react-parity/native-strands-react/server.spec.mjs b/fixtures/react-parity/native-strands-react/server.spec.mjs deleted file mode 100644 index 4928c2472..000000000 --- a/fixtures/react-parity/native-strands-react/server.spec.mjs +++ /dev/null @@ -1,254 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { spawn } from 'node:child_process'; -import { existsSync, readFileSync } from 'node:fs'; -import { EventEmitter } from 'node:events'; -import { PassThrough } from 'node:stream'; -import { createServer, connect } from 'node:net'; -import { preparationLock, root } from './configuration.mjs'; -const load = async () => { - const module = await import('./server.mjs').catch(() => null); - assert.ok(module, 'Native Strands owner must exist'); - return module; -}; -test( - 'retains atomic listener ownership while a foreign listener occupies a released candidate', - { timeout: 30000 }, - async (t) => { - const { createNativeStrandsServer } = await load(); - const foreign = createServer((socket) => socket.end('foreign-listener\n')); - t.after(async () => { - if (foreign.listening) - await new Promise((resolve, reject) => - foreign.close((error) => (error ? reject(error) : resolve())) - ); - }); - let actual; - const owner = await createNativeStrandsServer({ - spawnProcess: (...args) => { - const proxy = new EventEmitter(); - proxy.stdout = new PassThrough(); - proxy.stderr = new PassThrough(); - Object.defineProperties(proxy, { - pid: { get: () => actual?.pid }, - exitCode: { get: () => actual?.exitCode ?? null }, - signalCode: { get: () => actual?.signalCode ?? null }, - }); - let pendingSignal; - proxy.kill = (signal) => { - if (actual) return actual.kill(signal); - pendingSignal = signal; - return true; - }; - foreign.once('error', (error) => { - proxy.emit('error', error); - proxy.stdout.end(); - proxy.stderr.end(); - proxy.emit('close', 1, null); - }); - // Before the fix this is the exact port Node released for Python. - // Bind it before spawning the real service, making the race deterministic. - // After the fix Python selects its own retained socket, independently. - const candidate = args[2].env.STRANDS_FIXTURE_PORT; - foreign.listen( - candidate === undefined ? 0 : Number(candidate), - '127.0.0.1', - () => { - actual = spawn(...args); - actual.stdout.pipe(proxy.stdout); - actual.stderr.pipe(proxy.stderr); - actual.once('error', (error) => proxy.emit('error', error)); - actual.once('close', (...closed) => proxy.emit('close', ...closed)); - if (pendingSignal) actual.kill(pendingSignal); - } - ); - return proxy; - }, - }); - t.after(() => owner.close()); - const observed = await owner.observe(); - assert.equal(observed.pid, actual.pid); - assert.notEqual(Number(new URL(owner.url).port), foreign.address().port); - assert.notEqual(observed.modelPort, foreign.address().port); - assert.equal(observed.model.httpSeen, 0); - assert.deepEqual(observed.blocked, []); - await owner.close(); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - assert.ok(foreign.listening, 'closing the fixture preserves the foreign owner'); - const greeting = await new Promise((resolve, reject) => { - const socket = connect(foreign.address().port, '127.0.0.1'); - const chunks = []; - socket.setTimeout(1000, () => socket.destroy(Error('Foreign listener deadline'))); - socket.on('data', (bytes) => chunks.push(bytes)); - socket.once('error', reject); - socket.once('end', () => resolve(Buffer.concat(chunks).toString())); - }); - assert.equal(greeting, 'foreign-listener\n'); - } -); -test( - 'failed lock acquisition preserves existing owner; failed spawn releases only its own lock', - { timeout: 30000 }, - async (t) => { - const { createNativeStrandsServer } = await load(); - await assert.rejects( - createNativeStrandsServer({ - spawnProcess: () => { - throw Error('synchronous spawn failure'); - }, - }), - /synchronous spawn failure/ - ); - assert.equal(existsSync(preparationLock), false); - await assert.rejects( - createNativeStrandsServer({ python: '/does-not-exist-native-strands' }), - /ENOENT/ - ); - assert.equal(existsSync(preparationLock), false); - const owner = await createNativeStrandsServer(); - t.after(() => owner.close()); - await assert.rejects(createNativeStrandsServer(), /EEXIST/); - assert.ok(existsSync(preparationLock)); - const observed = await owner.observe(); - assert.equal(observed.model.httpSeen, 0); - assert.equal(observed.versions['ag-ui-strands'], '0.4.1'); - for (const name of ['agent', 'server', 'subagent_emitter']) { - const actual = observed.modules[name]; - assert.match( - actual.path, - /deployments\/ag-ui-dev\/deps\/aws_strands\/src\// - ); - assert.deepEqual( - readFileSync(actual.path), - readFileSync( - root + '/cockpit/runtimes/aws-strands/python/src/' + name + '.py' - ) - ); - } - assert.deepEqual(observed.blocked, []); - assert.equal(observed.entries, 0); - await Promise.all([owner.close(), owner.close()]); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - await assert.rejects( - fetch(owner.url + '/ok', { signal: AbortSignal.timeout(500) }) - ); - await assert.rejects( - fetch(`http://127.0.0.1:${observed.modelPort}/`, { - signal: AbortSignal.timeout(500), - }) - ); - } -); -test( - 'absolute startup deadline reaps a hanging startup and releases ownership', - { timeout: 10000 }, - async () => { - const { createNativeStrandsServer } = await load(); - let actual; - const started = Date.now(); - await assert.rejects( - createNativeStrandsServer({ - startupMs: 150, - spawnProcess: (python, _args, options) => { - actual = spawn( - python, - ['-c', 'import time; time.sleep(20)'], - options - ); - return actual; - }, - }), - /startup deadline/ - ); - assert.ok(Date.now() - started < 3000); - assert.ok(actual.exitCode !== null || actual.signalCode !== null); - assert.equal(existsSync(preparationLock), false); - } -); -test( - 'closure waits for stdout after process exit before sealing final receipts', - { timeout: 30000 }, - async (t) => { - const { createNativeStrandsServer } = await load(); - const owner = await createNativeStrandsServer({ - spawnProcess: (...args) => { - const actual = spawn(...args), - proxy = new EventEmitter(); - proxy.stdout = new PassThrough(); - proxy.stderr = actual.stderr; - Object.defineProperties(proxy, { - pid: { get: () => actual.pid }, - exitCode: { get: () => actual.exitCode }, - signalCode: { get: () => actual.signalCode }, - }); - proxy.kill = (signal) => actual.kill(signal); - const buffers = []; - let pending = ''; - actual.stdout.on('data', (bytes) => { - pending += bytes; - let newline; - while ((newline = pending.indexOf('\n')) !== -1) { - const line = pending.slice(0, newline + 1); - pending = pending.slice(newline + 1); - if (line.startsWith('STRANDS_FIXTURE_READY ')) - proxy.stdout.write(line); - else buffers.push(line); - } - }); - actual.once('error', (error) => proxy.emit('error', error)); - actual.once('close', (...args) => - setTimeout(() => { - for (const bytes of buffers) proxy.stdout.write(bytes); - proxy.stdout.write(pending); - proxy.stdout.end(); - proxy.emit('close', ...args); - }, 100) - ); - return proxy; - }, - }); - t.after(() => owner.close()); - await owner.close(); - assert.equal(owner.stats().final.fixtureClosed, true); - assert.ok(Object.values(owner.stats().closure).every(Boolean)); - } -); -test( - 'invalid or conflicting readiness records reap the actual child and release ownership', - { timeout: 15000 }, - async () => { - const { createNativeStrandsServer } = await load(); - const records = [ - "'{'", - "json.dumps({'host': 'localhost', 'port': 1234, 'pid': os.getpid()})", - "json.dumps({'host': '127.0.0.1', 'port': 0, 'pid': os.getpid()})", - "json.dumps({'host': '127.0.0.1', 'port': 65536, 'pid': os.getpid()})", - "json.dumps({'host': '127.0.0.1', 'port': '1234', 'pid': os.getpid()})", - "json.dumps({'host': '127.0.0.1', 'port': 1234, 'pid': os.getpid() + 1})", - "json.dumps({'host': '127.0.0.1', 'port': 1234, 'pid': os.getpid(), 'extra': True})", - "json.dumps({'host': '127.0.0.1', 'port': 1, 'pid': os.getpid()}) + '\\nSTRANDS_FIXTURE_READY ' + json.dumps({'host': '127.0.0.1', 'port': 2, 'pid': os.getpid()})", - ]; - for (const record of records) { - let actual; - await assert.rejects( - createNativeStrandsServer({ - startupMs: 1000, - spawnProcess: (python, _args, options) => { - actual = spawn( - python, - [ - '-c', - `import json,os,time; print('STRANDS_FIXTURE_READY ' + ${record}, flush=True); time.sleep(20)`, - ], - options - ); - return actual; - }, - }), - /Invalid native service readiness record/ - ); - assert.ok(actual.exitCode !== null || actual.signalCode !== null); - assert.equal(existsSync(preparationLock), false); - } - } -); diff --git a/fixtures/react-parity/native-strands-react/service.py b/fixtures/react-parity/native-strands-react/service.py deleted file mode 100644 index 1cb313898..000000000 --- a/fixtures/react-parity/native-strands-react/service.py +++ /dev/null @@ -1,239 +0,0 @@ -"""Direct unchanged generated Strands ASGI service; passive body/profile taps. - -Captured bytes are ASGI bodies, not HTTP framing or TCP packets. Profile None -returns at interrupt are unwinds, never counted as completed bookings. -""" - -import base64 -import hashlib -import importlib.metadata -import json -import os -import re -from pathlib import Path -import signal -import socket -import sys -import threading -from contextlib import nullcontext -from model import Model, BOOKED, DECLINED - -ROOT = Path(os.environ["STRANDS_FIXTURE_ROOT"]) -model = Model(os.environ["STRANDS_FIXTURE_MODE"]) -model_thread = threading.Thread(target=model.serve_forever) -original_connect = socket.socket.connect -original_connect_ex = socket.socket.connect_ex -blocked = [] - - -def checked_address(address): - if not isinstance(address, tuple) or address[:2] != ( - "127.0.0.1", - model.server_port, - ): - blocked.append(repr(address)) - raise RuntimeError("Fixture blocked non-owned model destination") - - -def connect(self, address): - checked_address(address) - return original_connect(self, address) - - -def connect_ex(self, address): - checked_address(address) - return original_connect_ex(self, address) - - -socket.socket.connect = connect -socket.socket.connect_ex = connect_ex -os.environ["OPENAI_BASE_URL"] = f"http://127.0.0.1:{model.server_port}/v1" -sys.path.insert(0, str(ROOT / "deployments/ag-ui-dev/deps/aws_strands")) -from src import server, agent, subagent_emitter # noqa: E402 - redirect/guard before provider imports - -target = agent.book_meeting.__wrapped__.__code__ -profile_events = [] - - -def profile(frame, event, arg): - if frame.f_code is target and event in ("call", "return"): - profile_events.append( - { - "event": event, - "line": frame.f_lineno, - "result": arg if isinstance(arg, str) else None, - } - ) - - -sys.setprofile(profile) -threading.setprofile(profile) -requests = [] -observed_snapshots = [] - - -class Tap: - def __init__(self, app): - self.app = app - - async def __call__(self, scope, receive, send): - if scope["type"] != "http": - return await self.app(scope, receive, send) - if scope["path"] == "/fixture-stats": - if ( - dict(scope["headers"]).get(b"x-internal-token") - != os.environ["AG_UI_INTERNAL_TOKEN"].encode() - ): - await send( - {"type": "http.response.start", "status": 403, "headers": []} - ) - await send({"type": "http.response.body", "body": b""}) - return - if scope["path"] != "/agent": - return await self.app(scope, receive, send) - incoming = bytearray() - outgoing = bytearray() - pending = bytearray() - record = { - "requestBase64": "", - "responseBase64": "", - "closed": False, - "headers": [(k.decode(), v.decode()) for k, v in scope["headers"]], - } - requests.append(record) - - async def recv(): - message = await receive() - if message["type"] == "http.request": - incoming.extend(message.get("body", b"")) - record["requestBase64"] = base64.b64encode(incoming).decode() - return message - - async def emit(message): - if message["type"] == "http.response.start": - record["status"] = message["status"] - if message["type"] == "http.response.body": - outgoing.extend(message.get("body", b"")) - record["responseBase64"] = base64.b64encode(outgoing).decode() - # Read only after retaining the untouched ASGI body bytes. - pending.extend(message.get("body", b"")) - while match := re.search(rb"\r?\n\r?\n", pending): - frame = bytes(pending[: match.start()]) - del pending[: match.end()] - for line in frame.splitlines(): - if line.startswith(b"data: "): - try: - event = json.loads(line[6:]) - except (ValueError, UnicodeDecodeError): - continue - if event.get("type") == "STATE_SNAPSHOT": - observed_snapshots.append(event["snapshot"]) - - await send(message) - - try: - await self.app(scope, recv, emit) - finally: - record["closed"] = True - - -def sha(path): - return hashlib.sha256(Path(path).read_bytes()).hexdigest() - - -@server.app.get("/fixture-stats") -def stats(): - return { - "entries": sum(e["event"] == "call" for e in profile_events), - "interruptedUnwinds": sum( - e["event"] == "return" and e["result"] is None for e in profile_events - ), - "completedReturns": [ - e - for e in profile_events - if e["event"] == "return" and e["result"] in (BOOKED, DECLINED) - ], - "profile": profile_events, - "backendState": agent._state, - "observedState": observed_snapshots[-1] if observed_snapshots else None, - "requests": requests, - "blocked": blocked, - "model": model.stats(), - "pid": os.getpid(), - "python": sys.version, - "modelPort": model.server_port, - "modules": { - name: {"path": module.__file__, "sha256": sha(module.__file__)} - for name, module in [ - ("server", server), - ("agent", agent), - ("subagent_emitter", subagent_emitter), - ] - }, - "versions": { - name: importlib.metadata.version(name) - for name in [ - "ag-ui-strands", - "ag-ui-protocol", - "strands-agents", - "openai", - "fastapi", - "uvicorn", - ] - }, - } - - -if __name__ == "__main__": - import uvicorn - - listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - model_started = False - try: - # Allocate and retain the socket; no unowned port crosses processes. - listener.bind(("127.0.0.1", 0)) - listener.listen() - owned = uvicorn.Server( - uvicorn.Config( - Tap(server.app), - host="127.0.0.1", - port=0, - log_level="warning", - timeout_graceful_shutdown=2, - ) - ) - owned.capture_signals = nullcontext - for sig in (signal.SIGTERM, signal.SIGINT): - signal.signal(sig, lambda *_: setattr(owned, "should_exit", True)) - model_thread.start() - model_started = True - host, port = listener.getsockname() - print( - "STRANDS_FIXTURE_READY " - + json.dumps({"host": host, "port": port, "pid": os.getpid()}), - flush=True, - ) - owned.run(sockets=[listener]) - finally: - listener.close() - model.closing.set() - if model_started: - model.shutdown() - model.server_close() - if model_started: - model_thread.join(3) - print( - "\n" - + json.dumps( - { - "fixtureClosed": True, - "modelThreadClosed": not model_thread.is_alive(), - "model": model.stats(), - "backendState": agent._state, - "observedState": observed_snapshots[-1] - if observed_snapshots - else None, - } - ), - flush=True, - ) diff --git a/fixtures/react-parity/native-strands-react/test_model.py b/fixtures/react-parity/native-strands-react/test_model.py deleted file mode 100644 index e90f5c55d..000000000 --- a/fixtures/react-parity/native-strands-react/test_model.py +++ /dev/null @@ -1,267 +0,0 @@ -"""Exercise real loopback provider requests, including rejected physical attempts.""" - -import importlib.util -import json -import socket -import threading -import time -import unittest -from urllib.error import HTTPError -from urllib.request import Request, urlopen - - -class ModelTest(unittest.TestCase): - def setUp(self): - spec = importlib.util.find_spec("model") - self.assertIsNotNone(spec, "Native Strands model seam must exist") - from model import Model - - self.Model = Model - self.owners = [] - - def tearDown(self): - for model, thread in self.owners: - model.closing.set() - model.shutdown() - model.server_close() - thread.join(2) - self.assertFalse(thread.is_alive()) - - def start(self, mode="approval"): - model = self.Model(mode) - thread = threading.Thread(target=model.serve_forever) - thread.start() - self.owners.append((model, thread)) - return model - - def post(self, model, messages, path="/v1/chat/completions"): - raw = json.dumps({"messages": messages, "stream": True}).encode() - with urlopen( - Request(f"http://127.0.0.1:{model.server_port}" + path, data=raw), timeout=2 - ) as response: - data = response.read().decode() - return [ - json.loads(line[6:]) - for line in data.splitlines() - if line.startswith("data: {") - ] - - def initial(self): - return [{"role": "user", "content": "Book Q3 roadmap review on Tuesday"}] - - def assistant(self, calls): - return {"role": "assistant", "tool_calls": calls} - - def calls(self, chunks): - return chunks[1]["choices"][0]["delta"]["tool_calls"] - - def lookup(self, call_id): - return { - "role": "tool", - "tool_call_id": call_id, - "content": json.dumps({"day": "Tuesday", "slots": ["10:00", "15:00"]}), - } - - def test_sequential_requires_actual_lookup_and_canonical_decision_results(self): - for approved in (True, False): - model = self.start() - history = self.initial() - calls = self.calls(self.post(model, history)) - self.assertEqual(calls[0]["function"]["name"], "check_availability") - history += [self.assistant(calls), self.lookup(calls[0]["id"])] - calls = self.calls(self.post(model, history)) - self.assertEqual(calls[0]["function"]["name"], "book_meeting") - canonical = ( - "Meeting booked for Tuesday 10:00: Q3 roadmap review" - if approved - else "The human declined. Meeting NOT booked: Q3 roadmap review" - ) - history += [ - self.assistant(calls), - {"role": "tool", "tool_call_id": calls[0]["id"], "content": canonical}, - ] - final = self.post(model, history) - self.assertEqual( - final[1]["choices"][0]["delta"]["content"], - "The Q3 roadmap review booking decision is complete.", - ) - self.assertEqual( - model.stats()["records"][-1]["canonicalContinuation"], canonical - ) - self.assertEqual(model.stats()["httpSeen"], 3) - - def test_batch_requires_both_original_results_before_continuation(self): - model = self.start("batch") - history = self.initial() - calls = self.calls(self.post(model, history)) - self.assertEqual( - [x["function"]["name"] for x in calls], - ["check_availability", "book_meeting"], - ) - history += [ - self.assistant(calls), - self.lookup(calls[0]["id"]), - { - "role": "tool", - "tool_call_id": calls[1]["id"], - "content": "The human declined. Meeting NOT booked: Q3 roadmap review", - }, - ] - self.post(model, history) - self.assertEqual(model.stats()["httpSeen"], 2) - - def test_rejected_requests_keep_raw_physical_ledger_and_are_non_retryable(self): - for invalid in ( - "wrong-path", - "unknown-tool", - "wrong-lookup", - "quoted-result", - "overflow", - ): - model = self.start() - history = self.initial() - calls = self.calls(self.post(model, history)) - history += [self.assistant(calls), self.lookup(calls[0]["id"])] - path = "/v1/chat/completions" - if invalid == "wrong-path": - path = "/wrong" - elif invalid == "unknown-tool": - history[-1]["tool_call_id"] = "alien" - elif invalid == "wrong-lookup": - history[-1]["content"] = "invented slots" - elif invalid == "quoted-result": - calls = self.calls(self.post(model, history)) - history += [ - self.assistant(calls), - { - "role": "tool", - "tool_call_id": calls[0]["id"], - "content": json.dumps( - "Meeting booked for Tuesday 10:00: Q3 roadmap review" - ), - }, - ] - elif invalid == "overflow": - calls = self.calls(self.post(model, history)) - history += [ - self.assistant(calls), - { - "role": "tool", - "tool_call_id": calls[0]["id"], - "content": "Meeting booked for Tuesday 10:00: Q3 roadmap review", - }, - ] - self.post(model, history) - before = model.stats()["httpSeen"] - with self.assertRaises(HTTPError) as error: - self.post(model, history, path) - self.assertEqual(error.exception.code, 400) - error.exception.close() - stats = model.stats() - self.assertEqual(stats["httpSeen"], before + 1) - self.assertTrue(stats["records"][-1]["requestBase64"]) - self.assertTrue(stats["records"][-1]["error"]) - - def test_second_user_turn_has_new_ids_and_ignores_prior_completed_results(self): - model = self.start("batch") - history = self.initial() - first = self.calls(self.post(model, history)) - history += [ - self.assistant(first), - self.lookup(first[0]["id"]), - { - "role": "tool", - "tool_call_id": first[1]["id"], - "content": "Meeting booked for Tuesday 10:00: Q3 roadmap review", - }, - ] - self.post(model, history) - history += [ - {"role": "assistant", "content": "Decision complete."}, - {"role": "user", "content": "Book again"}, - ] - second = self.calls(self.post(model, history)) - self.assertTrue(set(c["id"] for c in first).isdisjoint(c["id"] for c in second)) - history += [ - self.assistant(second), - self.lookup(second[0]["id"]), - { - "role": "tool", - "tool_call_id": second[1]["id"], - "content": "The human declined. Meeting NOT booked: Q3 roadmap review", - }, - ] - self.post(model, history) - self.assertEqual(model.stats()["httpSeen"], 4) - - def test_fresh_history_after_pause_issues_new_owned_calls(self): - model = self.start("batch") - first = self.calls(self.post(model, self.initial())) - second = self.calls(self.post(model, self.initial())) - self.assertTrue(set(c["id"] for c in first).isdisjoint(c["id"] for c in second)) - - def test_unexpected_method_is_ledgered_and_rejected_without_retry_status(self): - model = self.start() - with self.assertRaises(HTTPError) as error: - urlopen( - Request( - f"http://127.0.0.1:{model.server_port}/v1/chat/completions", - method="GET", - ), - timeout=2, - ) - self.assertEqual(error.exception.code, 400) - error.exception.close() - self.assertEqual(model.stats()["httpSeen"], 1) - self.assertEqual(model.stats()["records"][0]["method"], "GET") - - def test_expected_success_budget_rejects_but_retains_all_excess_physical_attempts( - self, - ): - model = self.start() - for _ in range(8): - history = self.initial() - calls = self.calls(self.post(model, history)) - history += [self.assistant(calls), self.lookup(calls[0]["id"])] - calls = self.calls(self.post(model, history)) - history += [ - self.assistant(calls), - { - "role": "tool", - "tool_call_id": calls[0]["id"], - "content": "Meeting booked for Tuesday 10:00: Q3 roadmap review", - }, - ] - self.post(model, history) - for _ in range(2): - with self.assertRaises(HTTPError) as error: - self.post(model, self.initial()) - self.assertEqual(error.exception.code, 400) - error.exception.close() - self.assertEqual(model.stats()["httpSeen"], 26) - self.assertTrue( - all(record.get("error") for record in model.stats()["records"][24:]) - ) - - def test_held_peer_closure_is_observed_before_cleanup(self): - model = self.start("cancel") - peer = socket.create_connection(("127.0.0.1", model.server_port)) - raw = json.dumps({"messages": self.initial(), "stream": True}).encode() - peer.sendall( - b"POST /v1/chat/completions HTTP/1.1\r\nHost: localhost\r\nContent-Length: " - + str(len(raw)).encode() - + b"\r\n\r\n" - + raw - ) - self.assertIn(b"200", peer.recv(4096)) - peer.close() - deadline = time.monotonic() + 2 - while not model.stats()["records"][0]["closed"] and time.monotonic() < deadline: - time.sleep(0.01) - record = model.stats()["records"][0] - self.assertTrue(record["peerClosed"]) - self.assertFalse(record["cleanupReleased"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/scripts/ci-scope.spec.mjs b/scripts/ci-scope.spec.mjs index 393dfb73a..1f761c19f 100644 --- a/scripts/ci-scope.spec.mjs +++ b/scripts/ci-scope.spec.mjs @@ -680,8 +680,6 @@ describe('React migration baseline scope', () => { 'scripts/react-parity/ag-ui-candidate-package.mjs', 'scripts/react-parity/verify-ag-ui-candidate.mjs', 'scripts/react-parity/verify-markdown.mjs', - 'fixtures/react-parity/native-strands-react/model.py', - 'fixtures/react-parity/native-strands-react/server.mjs', 'fixtures/react-parity/markdown/browser.tsx', 'fixtures/react-parity/markdown/installed-types.ts', 'libs/content/src/markdown/create-markdown.ts', diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index e97c7a735..0984f4d04 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - 'b755ced4e3ab6a10144425498ab416fb7b23a09421a8154f9dc42930611a4cc7', + 'f36512856a3f4071b4a4542cab88703cdc3fa4e4958410c2b970e067487b8198', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); @@ -608,36 +608,18 @@ it('requires every preview build shard before scoped PR success and preserves fo assert.match(step, /RESULT_WEBSITE_PREVIEW_BUILD: \$\{\{ needs.website-preview-build.result \}\}/); }); -it('runs native MAF Node configuration, preparation, actual fixture and model checks in scoped CI', async () => { +it('runs the MAF React e2e on aimock + the real server, with no fixture preparation step', async () => { const workflow = await readFile('.github/workflows/ci.yml', 'utf8'); const scope = readNamedStep(readJobBlock(workflow, 'ci-guards'), 'Test CI scope classifier'); - for (const file of ['runtimes-microsoft-agent-framework-configuration.spec.mjs', 'prepare-native-maf-fixture.spec.mjs']) assert.ok(scope.includes(`scripts/react-cockpit/${file}`)); - const fleet = readJobBlock(workflow, 'cockpit-e2e'); - const step = readNamedStep(fleet, 'Verify native MAF fixture ownership'); - assert.match(step, /if: matrix\.cap\.angular == 'cockpit-runtimes-microsoft-agent-framework-react'/); - const preparation = step.indexOf('npx nx run cockpit-runtimes-microsoft-agent-framework-react:prepare-fixture --skip-nx-cache'); - const fixture = step.indexOf('node --test --test-concurrency=1 scripts/react-cockpit/native-maf-fixture.spec.mjs'); - assert.ok(preparation >= 0 && fixture > preparation); - assert.match(step, /python -m unittest discover -s fixtures\/react-parity\/native-maf-react -p test_model\.py/); - assert.match(step, /fixtures\/react-parity\/native-maf-react\/server\.spec\.mjs/); - assert.ok(fleet.includes('python-version: \'3.12\'')); - assert.ok(!step.includes('THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT')); + assert.ok(scope.includes('scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs')); + assert.doesNotMatch(workflow, /native-maf|cockpit-runtimes-microsoft-agent-framework-react:prepare-fixture/); }); -it('runs native Strands Node configuration, preparation, actual fixture and model checks in scoped CI', async () => { +it('runs the Strands React e2e on aimock + the real server, with no fixture preparation step', async () => { const workflow = await readFile('.github/workflows/ci.yml', 'utf8'); const scope = readNamedStep(readJobBlock(workflow, 'ci-guards'), 'Test CI scope classifier'); - for (const file of ['runtimes-aws-strands-configuration.spec.mjs', 'prepare-native-strands-fixture.spec.mjs']) assert.ok(scope.includes(`scripts/react-cockpit/${file}`)); - const fleet = readJobBlock(workflow, 'cockpit-e2e'); - const step = readNamedStep(fleet, 'Verify native Strands fixture ownership'); - assert.match(step, /if: matrix\.cap\.angular == 'cockpit-runtimes-aws-strands-react'/); - const preparation = step.indexOf('npx nx run cockpit-runtimes-aws-strands-react:prepare-fixture --skip-nx-cache'); - const fixture = step.indexOf('node --test --test-concurrency=1 scripts/react-cockpit/native-strands-fixture.spec.mjs'); - assert.ok(preparation >= 0 && fixture > preparation); - assert.match(step, /python -m unittest discover -s fixtures\/react-parity\/native-strands-react -p test_model\.py/); - assert.match(step, /fixtures\/react-parity\/native-strands-react\/server\.spec\.mjs/); - assert.ok(fleet.includes('python-version: \'3.12\'')); - assert.ok(!step.includes('THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT')); + assert.ok(scope.includes('scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs')); + assert.doesNotMatch(workflow, /native-strands|cockpit-runtimes-aws-strands-react:prepare-fixture/); }); it('bounds the Website promotion budget without changing other deploy budgets', async () => { @@ -647,7 +629,7 @@ it('bounds the Website promotion budget without changing other deploy budgets', it('installs pinned Python before local fixture preparation and preview assembly', async () => { const website = JSON.parse(await readFile('apps/website/project.json', 'utf8')); - for (const name of ['cockpit-runtimes-microsoft-agent-framework-react', 'cockpit-runtimes-aws-strands-react']) { + for (const name of ['cockpit-ag-ui-a2ui-react']) { assert.ok(website.targets.e2e.dependsOn.some((dependency) => dependency.target === 'prepare-fixture' && dependency.projects.includes(name) ), `the legacy full local suite retains its native fixture proof for ${name}`); @@ -667,7 +649,7 @@ it('installs pinned Python before local fixture preparation and preview assembly for (const [jobName, setupName, command] of [ ...websiteCallers, - ['cockpit-e2e', 'Install uv', 'npx nx run cockpit-runtimes-microsoft-agent-framework-react:prepare-fixture'], + ['cockpit-e2e', 'Install uv', 'npx nx run cockpit-chat-debug-python:test'], ]) { const job = readJobBlock(workflow, jobName); assert.ok(job.includes(`- name: ${setupName}`), `${jobName} must install pinned Python with step ${setupName}`); diff --git a/scripts/cockpit-frontend-matrix.spec.mjs b/scripts/cockpit-frontend-matrix.spec.mjs index 00c8c2125..a05d10120 100644 --- a/scripts/cockpit-frontend-matrix.spec.mjs +++ b/scripts/cockpit-frontend-matrix.spec.mjs @@ -727,14 +727,20 @@ test('aimock-backed React e2e setups match their closed configuration backends', const setups = walk('cockpit'); assert.ok(setups.length > 0); for (const file of setups) { - const call = /reactAimock(AgUi)?GlobalSetup\(\{ example: '([^']+)', port: (\d+) \}\)/.exec(readFileSync(file, 'utf8')); - assert.ok(call, `${file} uses reactAimockGlobalSetup or reactAimockAgUiGlobalSetup`); - const [, agUi, example, port] = call; + const call = /reactAimock(AgUi|Mastra)?GlobalSetup\(\{ example: '([^']+)', port: (\d+) \}\)/.exec(readFileSync(file, 'utf8')); + assert.ok(call, `${file} uses a reactAimock*GlobalSetup`); + const [, flavor, example, port] = call; const serve = /serve\.mjs ([a-z0-9-]+)/.exec(readFileSync(file.replace('global-setup-impl.ts', 'playwright.config.ts'), 'utf8'))[1]; const configuration = reactCockpitConfiguration(serve); assert.equal(configuration.port, Number(port), file); const shared = { python: `cockpit/${example}/python`, fixtures: `cockpit/${example}/angular/e2e/fixtures` }; - if (agUi) { + if (flavor === 'Mastra') + assert.deepEqual(configuration.backend, { + kind: 'aimock-mastra', service: 'deployments/ag-ui-mastra', fixtures: shared.fixtures, + backendPort: Number(port) + 1000, aimockPort: Number(port) + 2000, + agentPath: '/agent/mastra', publicAgentPath: '/agent', headers: { 'x-internal-token': 'dev-local-token' }, + }, file); + else if (flavor === 'AgUi') { const { aimockPort, agentPath, ...rest } = configuration.backend; assert.deepEqual(rest, { kind: 'aimock-ag-ui', ...shared, backendPort: Number(port) + 1000 }, file); if (aimockPort !== undefined) assert.equal(aimockPort, Number(port) + 2000, file); diff --git a/scripts/cockpit-matrix.spec.mjs b/scripts/cockpit-matrix.spec.mjs index 3d64fc87b..01e00574a 100644 --- a/scripts/cockpit-matrix.spec.mjs +++ b/scripts/cockpit-matrix.spec.mjs @@ -28,7 +28,7 @@ test('shared dependency setup changes qualify every existing cockpit browser tar }), caps); } for (const paths of [[], ['scripts/cockpit-dependencies-other.mjs'], ['cockpit/chat/messages/react/src/app.tsx'], - ['fixtures/react-parity/native-strands-react/package.json'], ['libs/chat/src/package.json']]) { + ['fixtures/react-parity/native-ag-ui/package.json'], ['libs/chat/src/package.json']]) { assert.equal(matrixPolicy.requiresFullCockpitFleet(paths), false); } }); @@ -104,76 +104,22 @@ test('actual CLI expands shared setup diffs without Nx and keeps ordinary discov assert.equal(existsSync(marker), false, 'lifecycle-only changes must qualify reuse before Nx'); }); -describe('Strands native fixture CI ownership', () => { +describe('runtime React e2e on shared aimock fixtures', () => { const root = fileURLToPath(new URL('../', import.meta.url)); - const project = JSON.parse(readFileSync(new URL('../cockpit/runtimes/aws-strands/react/project.json', import.meta.url), 'utf8')); const caps = deriveCockpitCaps(root); - const fixture = 'fixtures/react-parity/native-strands-react/'; - const fixtureInputs = ['configuration.mjs', 'model.py', 'server.mjs', 'server.spec.mjs', 'service.py', 'test_model.py'].map(file => fixture + file); - const inputs = [ - ...fixtureInputs, - 'scripts/react-cockpit/native-strands-fixture.mjs', - 'scripts/react-cockpit/native-strands-fixture.spec.mjs', - 'scripts/react-cockpit/prepare-native-strands-fixture.mjs', - 'scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs', - ]; - const affectedFor = files => new Set(JSON.parse(execFileSync(process.execPath, [ - 'node_modules/nx/bin/nx.js', 'show', 'projects', '--affected', `--files=${files.join(',')}`, '--json', - ], { cwd: root, encoding: 'utf8', timeout: 45000, maxBuffer: 4 * 1024 * 1024, env: { ...process.env, NX_DAEMON: 'false', NX_ISOLATE_PLUGINS: 'false', NX_LOAD_DOT_ENV_FILES: 'false' } }))); - const selectedFor = affected => { - const fullFleet = !caps.some(cap => isCapAffected(cap, affected)); - return { fullFleet, selected: selectCockpitCaps(caps, affected, { fullFleet }) }; - }; - - test('actual fixture-only inputs select their React native lane without the fleet', { timeout: 60000 }, () => { - const affected = affectedFor(fixtureInputs); - assert.ok(affected.has(project.name)); - const scope = classifyFromAffected(fixtureInputs, [project]); - assert.equal(scope.library, true); - assert.equal(scope.cockpit_e2e, true); - const { fullFleet, selected } = selectedFor(affected); - assert.equal(fullFleet, false); - // Website depends on Strands; scripts depends on Website. Nx therefore - // also selects the existing cockpit importers of scripts for fixture files. - assert.equal(selected.filter(cap => cap.angular === project.name).length, 1); - assert.equal(selected.some(cap => cap.angular === 'cockpit-runtimes-aws-strands-angular'), false); - assert.ok(selected.length < caps.length); - }); - - test('actual grouped fixture inputs select their React native lane without the fleet', { timeout: 60000 }, () => { - const affected = affectedFor(inputs); - assert.ok(affected.has(project.name), 'Actual Nx must attribute the Strands native proof to its React owner'); - const scope = classifyFromAffected(inputs, [project]); - assert.equal(scope.library, true, 'Parity fixture ownership remains required'); - assert.equal(scope.cockpit_e2e, true, 'The native proof job must run'); - const { fullFleet, selected } = selectedFor(affected); - assert.equal(fullFleet, false); - // Native preparation scripts belong to the shared scripts Nx project. - // Its existing A2UI, Filesystem, Memory and Planning importers also run. - assert.equal(selected.filter(cap => cap.angular === project.name).length, 1); - assert.equal(selected.some(cap => cap.angular === 'cockpit-runtimes-aws-strands-angular'), false); - assert.ok(selected.length < caps.length); - }); - - test('a near-prefix fixture stays outside the Strands native lane', { timeout: 60000 }, () => { - const files = ['fixtures/react-parity/native-strands-react-other/server.mjs']; - const affected = affectedFor(files); - assert.equal(affected.has(project.name), false); - const scope = classifyFromAffected(files, [{ name: 'root', tags: [] }]); - assert.equal(scope.library, true); - assert.equal(scope.cockpit_e2e, false); - assert.equal(selectCockpitCaps(caps, affected, { fullFleet: false }).some(cap => cap.angular === project.name), false); - }); - - test('mixed actual changes preserve the other cap and avoid Strands Angular fanout', { timeout: 60000 }, () => { - const affected = affectedFor([fixture + 'server.mjs', 'cockpit/chat/messages/angular/src/main.ts']); - assert.ok(affected.has(project.name)); - assert.ok(affected.has('cockpit-chat-messages-angular')); - const { fullFleet, selected } = selectedFor(affected); - assert.equal(fullFleet, false); - assert.equal(selected.filter(cap => cap.angular === project.name).length, 1); - assert.ok(selected.some(cap => cap.angular === 'cockpit-chat-messages-angular')); - assert.equal(selected.some(cap => cap.angular === 'cockpit-runtimes-aws-strands-angular'), false); + // The React runtimes replay the Angular examples' fixtures into the real + // servers; a fixture change must select the React lane without the fleet. + for (const [fixture, project] of [ + ['cockpit/runtimes/aws-strands/angular/e2e/fixtures/aws-strands.json', 'cockpit-runtimes-aws-strands-react'], + ['cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures/microsoft-agent-framework.json', 'cockpit-runtimes-microsoft-agent-framework-react'], + ['cockpit/runtimes/mastra/angular/e2e/fixtures/mastra.json', 'cockpit-runtimes-mastra-react'], + ]) test(`${fixture} selects ${project}`, { timeout: 60000 }, () => { + const affected = new Set(JSON.parse(execFileSync(process.execPath, [ + 'node_modules/nx/bin/nx.js', 'show', 'projects', '--affected', `--files=${fixture}`, '--json', + ], { cwd: root, encoding: 'utf8', timeout: 45000, maxBuffer: 4 * 1024 * 1024, env: { ...process.env, NX_DAEMON: 'false', NX_ISOLATE_PLUGINS: 'false', NX_LOAD_DOT_ENV_FILES: 'false' } }))); + assert.ok(affected.has(project)); + const selected = selectCockpitCaps(caps, affected, { fullFleet: false }); + assert.equal(selected.filter(cap => cap.angular === project).length, 1); assert.ok(selected.length < caps.length); }); }); diff --git a/scripts/react-cockpit/ag-ui-proxy.mjs b/scripts/react-cockpit/ag-ui-proxy.mjs index 17a3d0a87..e770798ac 100644 --- a/scripts/react-cockpit/ag-ui-proxy.mjs +++ b/scripts/react-cockpit/ag-ui-proxy.mjs @@ -8,7 +8,8 @@ * produce on demand: * * GET /__requests every agent POST, in order: { path, method, - * body, events (real, so far), held, closed } + * body, events (real, so far), sent (what + * the browser got, after edits), held, closed } * GET /__captures per finished run: { path, sse, events } as the * real server sent them (before any edit) * GET /__lifetime { active (a held stream), openResponses } @@ -155,7 +156,7 @@ function rewriteOutcome(mode, event) { return { ...event, outcome: { ...event.outcome, interrupts: [copy] } }; } -export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', modelJournal, ready = async () => {} }) { +export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', headers = {}, modelJournal, ready = async () => {} }) { let requests = []; let captures = []; const pendingCaptures = new Set(); @@ -181,7 +182,9 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', mo const body = raw.length ? JSON.parse(raw.toString('utf8')) : {}; // Live while the run streams: the real events so far, and whether the // proxy holds it or the browser closed it. - const entry = { path: pathname, method: request.method, body, events: [], held: false, closed: false }; + // `events`: what the real server streamed; `sent`: what the browser got + // after any /__stream/edit ops. + const entry = { path: pathname, method: request.method, body, events: [], sent: [], held: false, closed: false }; requests.push(entry); response.once('close', () => { entry.closed = true; }); if (stickyEdits.some((op) => op.mode === 'unstable-result') && Array.isArray(body.messages)) @@ -213,7 +216,8 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', mo const upstreamResponse = await new Promise((resolve, reject) => { const outgoing = httpRequest( new URL(agentPath, upstream), - { method: 'POST', headers: { 'content-type': 'application/json', accept: 'text/event-stream' } }, + // `headers`: what the backend requires of every caller (Mastra's internal token). + { method: 'POST', headers: { ...headers, 'content-type': 'application/json', accept: 'text/event-stream' } }, resolve ); outgoing.once('error', reject); @@ -238,7 +242,9 @@ export function createAgUiProxy({ upstream, agentPaths, agentPath = '/agent', mo const buffered = []; const events = []; const write = (event) => { - if (!response.writableEnded) response.write(`data: ${JSON.stringify(event)}\n\n`); + if (response.writableEnded) return; + entry.sent.push(event); + response.write(`data: ${JSON.stringify(event)}\n\n`); }; /** Apply edits to one real event; returns false once the stream must end. */ const forward = (event) => { diff --git a/scripts/react-cockpit/aimock-backend.mjs b/scripts/react-cockpit/aimock-backend.mjs index c3bffdde5..fd293034d 100644 --- a/scripts/react-cockpit/aimock-backend.mjs +++ b/scripts/react-cockpit/aimock-backend.mjs @@ -2,7 +2,8 @@ * Local e2e only: aimock + the real server for one aimock-backed React cockpit * example, owned by `serve.mjs --backend`: `langgraph dev` for LangGraph * examples (`aimock-langgraph`), the example's uvicorn `src/server.py` for - * AG-UI examples (`aimock-ag-ui`). + * AG-UI examples (`aimock-ag-ui`), and the deployments/ag-ui-mastra Node + * service for the Mastra runtime (`aimock-mastra`). * * Cockpit e2e starts the same backend from its Playwright global setup * (libs/e2e-harness). Website e2e reuses the React examples as previews and @@ -13,7 +14,8 @@ * group, so Playwright's webServer teardown stops it with the server. */ import { execFileSync, spawn } from 'node:child_process'; -import { readFileSync, readdirSync } from 'node:fs'; +import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; import { LLMock } from 'aimock'; @@ -57,7 +59,9 @@ export function createAimockBackend({ root, backend }) { let child; let stderr = ''; - const port = backend.kind === 'aimock-ag-ui' ? backend.backendPort : backend.langgraphPort; + let database; + const port = backend.kind === 'aimock-langgraph' ? backend.langgraphPort : backend.backendPort; + const label = { 'aimock-langgraph': 'langgraph dev', 'aimock-ag-ui': 'uvicorn', 'aimock-mastra': 'ag-ui-mastra' }[backend.kind]; async function start() { const dir = resolve(root, backend.fixtures); const fixtures = readdirSync(dir) @@ -71,23 +75,41 @@ export function createAimockBackend({ root, backend }) { mock = new LLMock({ port: backend.aimockPort ?? 0, chunkSize: 4096 }); mock.addFixturesFromJSON(fixtures); await mock.start(); - const agUi = backend.kind === 'aimock-ag-ui'; - child = spawn( - 'uv', - agUi - ? ['run', 'uvicorn', 'src.server:app', '--port', String(port)] - : ['run', 'langgraph', 'dev', '--port', String(port), '--no-browser'], - { - cwd: resolve(root, backend.python), + const model = { + OPENAI_BASE_URL: `${mock.url}/v1`, + OPENAI_API_KEY: 'test-not-used', + LANGSMITH_TRACING: 'false', + }; + if (backend.kind === 'aimock-mastra') { + // The service is self-contained (its own package.json and lockfile). + const service = resolve(root, backend.service); + if (!existsSync(join(service, 'node_modules'))) + execFileSync('npm', ['ci', '--no-audit', '--no-fund'], { cwd: service, stdio: 'ignore' }); + // A fresh LibSQL file per backend: no suspended runs or memory leak in. + database = mkdtempSync(join(tmpdir(), 'ag-ui-mastra-e2e-')); + child = spawn(process.execPath, ['server.mjs'], { + cwd: service, env: { ...process.env, - OPENAI_BASE_URL: `${mock.url}/v1`, - OPENAI_API_KEY: 'test-not-used', - LANGSMITH_TRACING: 'false', + ...model, + PORT: String(port), + AG_UI_INTERNAL_TOKEN: backend.headers['x-internal-token'], + AG_UI_MASTRA_DB_PATH: join(database, 'mastra.db'), }, stdio: ['ignore', 'ignore', 'pipe'], - }, - ); + }); + } else + child = spawn( + 'uv', + backend.kind === 'aimock-ag-ui' + ? ['run', 'uvicorn', 'src.server:app', '--port', String(port)] + : ['run', 'langgraph', 'dev', '--port', String(port), '--no-browser'], + { + cwd: resolve(root, backend.python), + env: { ...process.env, ...model }, + stdio: ['ignore', 'ignore', 'pipe'], + }, + ); child.stderr.on('data', (chunk) => { stderr = (stderr + chunk).slice(-8192); }); @@ -102,7 +124,7 @@ export function createAimockBackend({ root, backend }) { await delay(500); } process.stderr.write(stderr); - throw new Error(`${agUi ? 'uvicorn' : 'langgraph dev'} for ${backend.python} did not become ready`); + throw new Error(`${label} for ${backend.python ?? backend.service} did not become ready`); } return { @@ -111,7 +133,7 @@ export function createAimockBackend({ root, backend }) { starting ??= start(); return starting; }, - /** The `uv run langgraph dev` process, once started. */ + /** The backend process (`uv run …` or the Mastra service), once started. */ get pid() { return child?.pid ?? null; }, @@ -133,6 +155,7 @@ export function createAimockBackend({ root, backend }) { clearTimeout(timeout); } await mock?.stop(); + if (database) rmSync(database, { recursive: true, force: true }); }, }; } diff --git a/scripts/react-cockpit/configuration.mjs b/scripts/react-cockpit/configuration.mjs index 660359100..6170542e3 100644 --- a/scripts/react-cockpit/configuration.mjs +++ b/scripts/react-cockpit/configuration.mjs @@ -26,6 +26,24 @@ const aimockAgUi = (example, port, agentPath = '/agent') => Object.freeze({ // The server route the React example talks to (src/server.py). agentPath, }); +/** + * aimock-backed Mastra example: the deployments/ag-ui-mastra Node service + * (the Angular example's backend) against the Angular example's aimock + * fixtures. The service serves /agent/ and requires X-Internal-Token + * on every route but /ok; the proxy adds the local token. Convention as for + * aimockAgUi. + */ +const aimockMastra = (example, port) => Object.freeze({ + kind: 'aimock-mastra', + service: 'deployments/ag-ui-mastra', + fixtures: `cockpit/${example}/angular/e2e/fixtures`, + backendPort: port + 1000, + aimockPort: port + 2000, + agentPath: '/agent/mastra', + // The route the React example posts to, under its base path. + publicAgentPath: '/agent', + headers: Object.freeze({ 'x-internal-token': 'dev-local-token' }), +}); /** LangGraph examples whose React e2e run on aimock + `langgraph dev`. */ const AIMOCK_LANGGRAPH_TOPICS = new Set(['streaming', 'interrupts', 'memory', 'persistence', 'subgraphs', 'client-tools', 'deployment-runtime', 'time-travel', 'durable-execution']); export function reactCockpitConfiguration(topic = 'streaming') { @@ -63,6 +81,7 @@ export function reactCockpitConfiguration(topic = 'streaming') { appPath: 'cockpit/runtimes/aws-strands/react', base: '/runtimes/aws-strands/react/', port: 4643, project: 'cockpit-runtimes-aws-strands-react', + backend: aimockAgUi('runtimes/aws-strands', 4643), }); if (topic === 'runtimes-microsoft-agent-framework') return Object.freeze({ @@ -70,12 +89,14 @@ export function reactCockpitConfiguration(topic = 'streaming') { appPath: 'cockpit/runtimes/microsoft-agent-framework/react', base: '/runtimes/microsoft-agent-framework/react/', port: 4642, project: 'cockpit-runtimes-microsoft-agent-framework-react', + backend: aimockAgUi('runtimes/microsoft-agent-framework', 4642), }); if (topic === 'runtimes-mastra') return Object.freeze({ topic: 'mastra', library: 'runtimes', adapter: 'ag-ui', appPath: 'cockpit/runtimes/mastra/react', base: '/runtimes/mastra/react/', port: 4641, project: 'cockpit-runtimes-mastra-react', + backend: aimockMastra('runtimes/mastra', 4641), }); if (topic === 'deep-agents-memory') return Object.freeze({ diff --git a/scripts/react-cockpit/native-maf-fixture.mjs b/scripts/react-cockpit/native-maf-fixture.mjs deleted file mode 100644 index eed8922f6..000000000 --- a/scripts/react-cockpit/native-maf-fixture.mjs +++ /dev/null @@ -1,272 +0,0 @@ -/** Local-only raw HTTP tap. All positive AG-UI bytes originate in the generated service. */ -import assert from 'node:assert/strict'; -import { request as httpRequest } from 'node:http'; -import { - createNativeMafServer, - token, -} from '../../fixtures/react-parity/native-maf-react/server.mjs'; -export function transformFrame(frame, scenario) { - const match = frame.match(/^data: (.+)$/m); - if (!match) return frame; - let event; - try { - event = JSON.parse(match[1]); - } catch { - return frame; - } - if (event.type !== 'RUN_FINISHED') return frame; - if (scenario === 'missing-terminal') return ''; - if (scenario === 'wrong-terminal') event.runId = 'negative-wrong-run'; - else if (scenario === 'draft-only') delete event.outcome; - else if (scenario === 'child-pause') { - // Negative corruption: downgrade the actual root terminal to child-only - // observation. The original root bytes remain in the upstream tap. - event.type = 'SUBAGENT_FINISHED'; - event.subagentRunId = 'negative-child'; - event.outcome = { type: 'suspended' }; - delete event.runId; - delete event.threadId; - } else return frame; - return frame.replace(match[0], 'data: ' + JSON.stringify(event)); -} -export function createNativeMafFixture() { - let owner, - closed = false, - operations = Promise.resolve(); - const receipts = []; - const serialize = (operation) => { - const result = operations.then(operation); - operations = result.catch(() => undefined); - return result; - }; - const currentStats = () => - owner - ? { - ...owner.server.stats(), - requests: structuredClone(owner.requests), - scenario: owner.scenario, - active: owner.active.size, - } - : null; - const stats = () => - structuredClone({ current: currentStats(), closed: receipts }); - async function closeOwner() { - const previous = owner; - owner = undefined; - if (!previous) return; - // Close physical streams before service shutdown; closure is independently observed. - for (const { upstream, request, response } of previous.active) { - upstream?.destroy(); - request.destroy(); - response.destroy(); - } - try { - await previous.server.observe(); - } catch {} - await previous.server.close(); - receipts.push({ - ...previous.server.stats(), - requests: previous.requests, - scenario: previous.scenario, - active: previous.active.size, - }); - } - const reset = (options = {}) => - serialize(async () => { - assert.ok(!closed, 'Fixture closed'); - assert.ok( - [ - 'approval', - 'batch', - 'cancel', - 'missing-terminal', - 'wrong-terminal', - 'draft-only', - 'child-pause', - ].includes(options.scenario ?? 'approval') - ); - await closeOwner(); - // createNativeMafServer owns failed acquisition cleanup before returning. - const server = await createNativeMafServer({ - mode: ['cancel', 'batch'].includes(options.scenario) - ? options.scenario - : 'approval', - }); - owner = { - server, - scenario: options.scenario ?? 'approval', - requests: [], - active: new Set(), - }; - }); - const close = () => { - closed = true; - return serialize(closeOwner); - }; - const json = (response, value, status = 200) => - response - .writeHead(status, { 'content-type': 'application/json' }) - .end(JSON.stringify(value)); - async function handle(request, response, path) { - if ( - ![ - '/__reset', - '/__stats', - '/__requests', - '/__finish', - '/runtimes/microsoft-agent-framework/agent', - ].includes(path) - ) - return false; - if ( - request.method !== - (['/__stats', '/__requests'].includes(path) ? 'GET' : 'POST') - ) { - json(response, {}, 405); - return true; - } - let admitted, active; - try { - if (path === '/__stats' || path === '/__requests') { - if (owner) await owner.server.observe(); - json(response, path === '/__stats' ? stats() : owner?.requests ?? []); - return true; - } - if (path === '/__finish') { - await serialize(closeOwner); - json(response, stats()); - return true; - } - const current = owner; - if (path !== '/__reset') { - assert.ok(current && !closed, 'Explicit fixture reset required'); - admitted = { - sequence: current.requests.length + 1, - path, - method: request.method, - requestBase64: '', - body: null, - parseFailed: false, - upstreamBase64: '', - sentBase64: '', - upstreamEnd: false, - downstreamClosed: false, - }; - current.requests.push(admitted); - active = { request, response, upstream: undefined }; - current.active.add(active); - response.once('close', () => { - admitted.downstreamClosed = true; - current.active.delete(active); - active.upstream?.destroy(); - }); - } - let raw = Buffer.alloc(0); - for await (const chunk of request) { - raw = Buffer.concat([raw, chunk]); - if (admitted) admitted.requestBase64 = raw.toString('base64'); - assert.ok(raw.length <= 2000000); - } - if (path === '/__reset') { - await reset(raw.length ? JSON.parse(raw) : {}); - json(response, stats()); - return true; - } - assert.ok( - owner === current && !closed, - 'Fixture replaced during caller body' - ); - const entry = admitted; - try { - entry.body = JSON.parse(raw); - } catch (error) { - entry.parseFailed = true; - throw error; - } - const input = [], - output = []; - let pending = ''; - const record = () => { - entry.upstreamBase64 = Buffer.concat(input).toString('base64'); - entry.sentBase64 = Buffer.concat(output).toString('base64'); - }; - const send = (bytes) => { - if (bytes && !response.destroyed) { - const data = Buffer.from(bytes); - output.push(data); - response.write(data); - record(); - } - }; - const resumed = Boolean(entry.body.resume?.length); - const negative = - ['draft-only', 'child-pause'].includes(current.scenario) || resumed; - const scenario = negative ? current.scenario : 'approval'; - const decoder = new TextDecoder(); - const upstream = httpRequest( - current.server.url + '/agent/microsoft-agent-framework', - { - method: 'POST', - headers: { - 'content-type': 'application/json', - 'x-internal-token': token, - 'content-length': raw.length, - }, - }, - (incoming) => { - if (owner !== current || closed || response.destroyed) { - incoming.destroy(); - return; - } - response.writeHead(incoming.statusCode, { - 'content-type': - incoming.headers['content-type'] ?? 'application/json', - 'cache-control': 'no-store', - }); - incoming.on('data', (chunk) => { - input.push(Buffer.from(chunk)); - record(); - if (['approval', 'batch', 'cancel'].includes(scenario)) { - send(chunk); - return; - } - pending += decoder.decode(chunk, { stream: true }); - let match; - while ((match = /\r?\n\r?\n/.exec(pending))) { - const end = match.index + match[0].length; - send(transformFrame(pending.slice(0, end), scenario)); - pending = pending.slice(end); - } - }); - incoming.once('end', () => { - entry.upstreamEnd = true; - pending += decoder.decode(); - if (pending) send(transformFrame(pending, scenario)); - record(); - response.end(); - }); - incoming.once('error', () => response.destroy()); - } - ); - active.upstream = upstream; - response.once('close', () => { - record(); - }); - upstream.once('error', () => { - if (!response.headersSent && !response.destroyed) - json(response, { error: 'Native fixture unavailable' }, 502); - else response.destroy(); - }); - upstream.end(raw); - return true; - } catch { - if (admitted) admitted.downstreamClosed = true; - if (response.destroyed) return true; - if (!response.headersSent) - json(response, { error: 'Native fixture request failed' }, 500); - else response.destroy(); - return true; - } - } - return Object.assign(handle, { reset, stats, close }); -} diff --git a/scripts/react-cockpit/native-maf-fixture.spec.mjs b/scripts/react-cockpit/native-maf-fixture.spec.mjs deleted file mode 100644 index 2154b7ac1..000000000 --- a/scripts/react-cockpit/native-maf-fixture.spec.mjs +++ /dev/null @@ -1,90 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { createServer, request as httpRequest } from 'node:http'; -import { SubagentFinishedEventSchema } from '@ag-ui/core/schemas'; -import { prepareNativeMafFixture } from './prepare-native-maf-fixture.mjs'; -import { - createNativeMafFixture, - transformFrame, -} from './native-maf-fixture.mjs'; -test('negative wire transformations preserve positive frames and remove/corrupt only terminal authority', () => { - const start = 'data: {"type":"RUN_STARTED","runId":"r"}\n\n'; - const terminal = - 'data: {"type":"RUN_FINISHED","runId":"r","threadId":"t"}\n\n'; - assert.equal(transformFrame(start, 'missing-terminal'), start); - assert.equal(transformFrame(terminal, 'missing-terminal'), ''); - assert.match( - transformFrame(terminal, 'wrong-terminal'), - /negative-wrong-run/ - ); - assert.equal(transformFrame(terminal, 'approval'), terminal); - assert.equal(transformFrame(terminal, 'batch'), terminal); - const child = transformFrame(terminal, 'child-pause'); - const parsed = JSON.parse(child.match(/^data: (.+)$/m)[1]); - assert.equal(SubagentFinishedEventSchema.safeParse(parsed).success, true); - assert.equal(parsed.type, 'SUBAGENT_FINISHED'); - assert.equal(parsed.outcome.type, 'suspended'); -}); -test( - 'serialized resets, unknown-route guard, method guard and exact installed owner closure', - { timeout: 30000 }, - async (t) => { - const fixture = createNativeMafFixture(); - const server = createServer(async (req, res) => { - if (!(await fixture(req, res, new URL(req.url, 'http://local').pathname))) - res.writeHead(404).end(); - }); - t.after(async () => { - await fixture.close(); - await new Promise((resolve) => { - server.close(resolve); - server.closeAllConnections(); - }); - }); - await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const url = `http://127.0.0.1:${server.address().port}`; - assert.equal((await fetch(url + '/other')).status, 404); - assert.equal((await fetch(url + '/__reset')).status, 405); - await fixture.reset(); - assert.throws(() => prepareNativeMafFixture({ run: () => {} }), /EEXIST/); - assert.equal(fixture.stats().current.requests.length, 0); - assert.equal( - ( - await fetch(url + '/runtimes/microsoft-agent-framework/agent', { - method: 'POST', - body: '{broken', - }) - ).status, - 500 - ); - assert.equal(fixture.stats().current.requests.length, 1); - assert.equal( - Buffer.from( - fixture.stats().current.requests[0].requestBase64, - 'base64' - ).toString(), - '{broken' - ); - assert.equal(fixture.stats().current.requests[0].parseFailed, true); - const partial = httpRequest( - url + '/runtimes/microsoft-agent-framework/agent', - { method: 'POST' } - ); - partial.on('error', () => undefined); - partial.write('{partial'); - const deadline = Date.now() + 1000; - while (fixture.stats().current.requests.length < 2 && Date.now() < deadline) - await new Promise((resolve) => setTimeout(resolve, 5)); - assert.equal(fixture.stats().current.requests.length, 2); - assert.equal(fixture.stats().current.active, 1); - await Promise.all([fixture.close(), fixture.close()]); - partial.destroy(); - assert.equal(fixture.stats().closed[0].active, 0); - assert.equal(fixture.stats().closed[0].requests[1].downstreamClosed, true); - assert.ok(Object.values(fixture.stats().closed[0].closure).every(Boolean)); - assert.equal( - (await fetch(url + '/__reset', { method: 'POST' })).status, - 500 - ); - } -); diff --git a/scripts/react-cockpit/native-mastra-fixture.mjs b/scripts/react-cockpit/native-mastra-fixture.mjs deleted file mode 100644 index b6e9bf6bc..000000000 --- a/scripts/react-cockpit/native-mastra-fixture.mjs +++ /dev/null @@ -1,110 +0,0 @@ -/** Browser proof only; never included in Code or deployment assets. */ -import assert from 'node:assert/strict'; -import { request as httpRequest } from 'node:http'; -import { createNativeMastraServer } from '../../fixtures/react-parity/native-mastra-react/server.mjs'; -export function createNativeMastraFixture() { - let owner, - closed = false, - operations = Promise.resolve(); - const receipts = []; - const serialize = (operation) => { - const result = operations.then(operation); - operations = result.catch(() => undefined); - return result; - }; - const stats = () => - structuredClone({ current: owner?.stats(), closed: receipts }); - const closeOwner = async () => { - const previous = owner; - owner = undefined; - if (previous) { - await previous.close(); - receipts.push(previous.stats()); - } - }; - const reset = (options) => - serialize(async () => { - assert.ok(!closed); - await closeOwner(); - owner = await createNativeMastraServer(options); - }); - const json = (response, value, status = 200) => - response - .writeHead(status, { 'content-type': 'application/json' }) - .end(JSON.stringify(value)); - async function handle(request, response, path) { - if ( - ![ - '/__reset', - '/__stats', - '/__requests', - '/__finish', - '/runtimes/mastra/agent', - ].includes(path) - ) - return false; - try { - if (path === '/__stats') { - json(response, stats()); - return true; - } - if (path === '/__requests') { - json(response, owner?.stats().requests ?? []); - return true; - } - if (path === '/__finish') { - await serialize(closeOwner); - json(response, stats()); - return true; - } - if (path === '/__reset') { - assert.equal(request.method, 'POST'); - let bytes = ''; - for await (const chunk of request) { - bytes += chunk; - assert.ok(bytes.length <= 2000000); - } - await reset(bytes ? JSON.parse(bytes) : {}); - json(response, stats()); - return true; - } - const current = owner; - assert.ok(current && !closed, 'Explicit fixture reset required'); - assert.equal(request.method, 'POST'); - const upstream = httpRequest( - current.url + '/agent', - { method: 'POST', headers: { 'content-type': 'application/json' } }, - (incoming) => { - if (response.destroyed || owner !== current) { - incoming.destroy(); - return; - } - response.writeHead(incoming.statusCode, { - 'content-type': - incoming.headers['content-type'] ?? 'application/json', - }); - incoming.pipe(response); - incoming.on('error', () => response.destroy()); - } - ); - upstream.on('error', () => { - if (!response.headersSent && !response.destroyed) - json(response, { error: 'Native fixture unavailable' }, 502); - else response.destroy(); - }); - response.once('close', () => upstream.destroy()); - request.pipe(upstream); - return true; - } catch { - if (!response.headersSent) - json(response, { error: 'Native fixture request failed' }, 500); - else response.destroy(); - return true; - } - } - const close = () => { - closed = true; - return serialize(closeOwner); - }; - return Object.assign(handle, { reset, stats, close }); -} diff --git a/scripts/react-cockpit/native-strands-fixture.mjs b/scripts/react-cockpit/native-strands-fixture.mjs deleted file mode 100644 index 58aa96975..000000000 --- a/scripts/react-cockpit/native-strands-fixture.mjs +++ /dev/null @@ -1,335 +0,0 @@ -/** Local-only raw HTTP tap. All positive AG-UI bytes originate in the generated service. */ -import assert from 'node:assert/strict'; -import { request as httpRequest } from 'node:http'; -import { - createNativeStrandsServer, - token, -} from '../../fixtures/react-parity/native-strands-react/server.mjs'; -export function transformFrame(frame, scenario) { - const match = frame.match(/^data: (.+)$/m); - if (!match) return frame; - let event; - try { - event = JSON.parse(match[1]); - } catch { - return frame; - } - if (event.type !== 'RUN_FINISHED') return frame; - if (scenario === 'missing-terminal') return ''; - if (scenario === 'wrong-terminal') event.runId = 'negative-wrong-run'; - else if (scenario === 'child-only-terminal') { - // Negative corruption: downgrade the actual root terminal to child-only - // observation. The original root bytes remain in the upstream tap. - event.type = 'SUBAGENT_FINISHED'; - event.subagentRunId = 'negative-child'; - event.outcome = { type: 'suspended' }; - delete event.runId; - delete event.threadId; - } else return frame; - return frame.replace(match[0], 'data: ' + JSON.stringify(event)); -} -export function createNativeStrandsFixture({ operationMs = 25000 } = {}) { - assert.ok(Number.isFinite(operationMs) && operationMs > 0); - let owner, - closed = false, - operations = Promise.resolve(); - const receipts = []; - // Reset callers can exist before any native backend owner is acquired. - const controlCallers = new Set(); - const controlRequests = []; - const serialize = (operation) => { - const result = operations.then(operation); - operations = result.catch(() => undefined); - return result; - }; - const currentStats = () => - owner - ? { - ...owner.server.stats(), - requests: structuredClone(owner.requests), - scenario: owner.scenario, - active: owner.active.size, - } - : null; - const stats = () => - structuredClone({ - current: currentStats(), - closed: receipts, - activeControls: controlCallers.size, - controlRequests, - }); - async function closeOwner() { - const previous = owner; - owner = undefined; - if (!previous) return; - // Close physical streams before service shutdown; closure is independently observed. - for (const { upstream, request, response } of previous.active) { - upstream?.destroy(); - request.destroy(); - response.destroy(); - } - try { - await previous.server.observe(); - } catch {} - await previous.server.close(); - receipts.push({ - ...previous.server.stats(), - requests: previous.requests, - scenario: previous.scenario, - active: previous.active.size, - }); - } - const reset = (options = {}) => - serialize(async () => { - assert.ok(!closed, 'Fixture closed'); - assert.ok( - [ - 'approval', - 'batch', - 'cancel', - 'missing-terminal', - 'wrong-terminal', - 'child-only-terminal', - ].includes(options.scenario ?? 'approval') - ); - await closeOwner(); - // createNativeStrandsServer owns failed acquisition cleanup before returning. - const server = await createNativeStrandsServer({ - mode: ['cancel', 'batch'].includes(options.scenario) - ? options.scenario - : 'approval', - }); - owner = { - server, - scenario: options.scenario ?? 'approval', - requests: [], - active: new Set(), - }; - }); - const close = () => { - closed = true; - const pendingControls = [...controlCallers]; - for (const { request, response } of pendingControls) { - request.destroy(); - response.destroy(); - } - return serialize(async () => { - await closeOwner(); - await Promise.all(pendingControls.map((caller) => caller.settled)); - }); - }; - const json = (response, value, status = 200) => - response - .writeHead(status, { 'content-type': 'application/json' }) - .end(JSON.stringify(value)); - async function handle(request, response, path) { - if ( - ![ - '/__reset', - '/__stats', - '/__requests', - '/__finish', - '/runtimes/aws-strands/agent', - ].includes(path) - ) - return false; - if ( - request.method !== - (['/__stats', '/__requests'].includes(path) ? 'GET' : 'POST') - ) { - json(response, {}, 405); - return true; - } - let admitted, active, control; - try { - if (path === '/__stats' || path === '/__requests') { - if (owner) await owner.server.observe(); - json(response, path === '/__stats' ? stats() : owner?.requests ?? []); - return true; - } - if (path === '/__finish') { - await serialize(closeOwner); - json(response, stats()); - return true; - } - if (path === '/__reset') { - assert.ok(!closed, 'Fixture closed'); - const receipt = { - path, - method: request.method, - requestBase64: '', - deadlineExceeded: false, - downstreamClosed: false, - }; - controlRequests.push(receipt); - let settle; - control = { - request, - response, - receipt, - settled: new Promise((resolve) => { - settle = resolve; - }), - settle: () => settle(), - }; - control.onClose = () => { - receipt.downstreamClosed = true; - request.destroy(); - }; - control.timer = setTimeout(() => { - receipt.deadlineExceeded = true; - request.destroy(); - response.destroy(); - }, operationMs); - response.once('close', control.onClose); - controlCallers.add(control); - } - const current = owner; - if (path !== '/__reset') { - assert.ok(current && !closed, 'Explicit fixture reset required'); - admitted = { - sequence: current.requests.length + 1, - path, - method: request.method, - requestBase64: '', - body: null, - parseFailed: false, - upstreamBase64: '', - sentBase64: '', - upstreamEnd: false, - downstreamClosed: false, - }; - current.requests.push(admitted); - active = { request, response, upstream: undefined }; - active.timer = setTimeout(() => { - admitted.deadlineExceeded = true; - active.upstream?.destroy(); - request.destroy(); - response.destroy(); - }, operationMs); - current.active.add(active); - response.once('close', () => { - clearTimeout(active.timer); - admitted.downstreamClosed = true; - current.active.delete(active); - active.upstream?.destroy(); - }); - } - let raw = Buffer.alloc(0); - for await (const chunk of request) { - raw = Buffer.concat([raw, chunk]); - if (admitted) admitted.requestBase64 = raw.toString('base64'); - if (control) control.receipt.requestBase64 = raw.toString('base64'); - assert.ok(raw.length <= 2000000); - } - if (path === '/__reset') { - assert.ok(!closed && !response.destroyed, 'Reset caller closed'); - await reset(raw.length ? JSON.parse(raw) : {}); - json(response, stats()); - return true; - } - assert.ok( - owner === current && !closed, - 'Fixture replaced during caller body' - ); - const entry = admitted; - try { - entry.body = JSON.parse(raw); - } catch (error) { - entry.parseFailed = true; - throw error; - } - const input = [], - output = []; - let pending = ''; - const record = () => { - entry.upstreamBase64 = Buffer.concat(input).toString('base64'); - entry.sentBase64 = Buffer.concat(output).toString('base64'); - }; - const send = (bytes) => { - if (bytes && !response.destroyed) { - const data = Buffer.from(bytes); - output.push(data); - response.write(data); - record(); - } - }; - const resumed = Boolean(entry.body.resume?.length); - const negative = resumed; - const scenario = negative ? current.scenario : 'approval'; - const decoder = new TextDecoder(); - const upstream = httpRequest( - current.server.url + '/agent', - { - method: 'POST', - headers: { - 'content-type': 'application/json', - 'x-internal-token': token, - 'content-length': raw.length, - }, - }, - (incoming) => { - if (owner !== current || closed || response.destroyed) { - incoming.destroy(); - return; - } - response.writeHead(incoming.statusCode, { - 'content-type': - incoming.headers['content-type'] ?? 'application/json', - 'cache-control': 'no-store', - }); - incoming.on('data', (chunk) => { - input.push(Buffer.from(chunk)); - record(); - if (['approval', 'batch', 'cancel'].includes(scenario)) { - send(chunk); - return; - } - pending += decoder.decode(chunk, { stream: true }); - let match; - while ((match = /\r?\n\r?\n/.exec(pending))) { - const end = match.index + match[0].length; - send(transformFrame(pending.slice(0, end), scenario)); - pending = pending.slice(end); - } - }); - incoming.once('end', () => { - entry.upstreamEnd = true; - pending += decoder.decode(); - if (pending) send(transformFrame(pending, scenario)); - record(); - response.end(); - }); - incoming.once('error', () => response.destroy()); - } - ); - active.upstream = upstream; - response.once('close', () => { - record(); - }); - upstream.once('error', () => { - if (!response.headersSent && !response.destroyed) - json(response, { error: 'Native fixture unavailable' }, 502); - else response.destroy(); - }); - upstream.end(raw); - return true; - } catch { - if (admitted) admitted.downstreamClosed = true; - if (response.destroyed) return true; - if (!response.headersSent) - json(response, { error: 'Native fixture request failed' }, 500); - else response.destroy(); - return true; - } finally { - if (control) { - clearTimeout(control.timer); - response.removeListener('close', control.onClose); - control.receipt.downstreamClosed = response.destroyed; - controlCallers.delete(control); - control.settle(); - } - } - } - return Object.assign(handle, { reset, stats, close }); -} diff --git a/scripts/react-cockpit/native-strands-fixture.spec.mjs b/scripts/react-cockpit/native-strands-fixture.spec.mjs deleted file mode 100644 index cfb428b90..000000000 --- a/scripts/react-cockpit/native-strands-fixture.spec.mjs +++ /dev/null @@ -1,547 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { createServer, request as httpRequest } from 'node:http'; -import { PassThrough } from 'node:stream'; -import { build } from 'esbuild'; -import { mkdtemp, rm } from 'node:fs/promises'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; -import { pathToFileURL } from 'node:url'; -import { - root, - preparationLock, -} from '../../fixtures/react-parity/native-strands-react/configuration.mjs'; -import { existsSync } from 'node:fs'; -const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); -const bounded = async (promise, ms = 16000) => { - let timer; - try { - return await Promise.race([ - promise, - new Promise( - (_, reject) => - (timer = setTimeout(() => reject(Error('Operation deadline')), ms)) - ), - ]); - } finally { - clearTimeout(timer); - } -}; -async function setup(t, options) { - const module = await import('./native-strands-fixture.mjs').catch(() => null); - assert.ok(module, 'Native Strands tap must exist'); - const fixture = module.createNativeStrandsFixture(options); - const server = createServer(async (req, res) => { - if (!(await fixture(req, res, new URL(req.url, 'http://local').pathname))) - res.writeHead(404).end(); - }); - t.after(async () => { - await fixture.close(); - await new Promise((resolve) => { - server.close(resolve); - server.closeAllConnections(); - }); - }); - await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - return { fixture, url: `http://127.0.0.1:${server.address().port}`, module }; -} -let createSession, bundle; -test.before(async () => { - bundle = await mkdtemp(join(tmpdir(), 'strands-session-test-')); - await build({ - stdin: { - contents: `export {createSession} from ${JSON.stringify( - root + '/libs/ag-ui/src/runtime/create-session.ts' - )}`, - resolveDir: root, - }, - bundle: true, - platform: 'node', - format: 'esm', - tsconfig: root + '/tsconfig.base.json', - outfile: bundle + '/session.mjs', - }); - ({ createSession } = await import(pathToFileURL(bundle + '/session.mjs'))); -}); -test.after(async () => { - if (bundle) await rm(bundle, { recursive: true, force: true }); -}); -const events = (entry) => - Buffer.from(entry.upstreamBase64, 'base64') - .toString() - .split(/\r?\n/) - .filter((line) => line.startsWith('data: {')) - .map((line) => JSON.parse(line.slice(6))); -for (const scenario of ['approval', 'batch']) - for (const approved of [true, false]) - test( - `genuine ${scenario} ${ - approved ? 'approve' : 'decline' - } preserves original Session pause, bytes and canonical authority`, - { timeout: 40000 }, - async (t) => { - const { fixture, url } = await setup(t); - await fixture.reset({ scenario }); - const session = createSession({ - url: url + '/runtimes/aws-strands/agent', - threadId: crypto.randomUUID(), - }); - t.after(() => session.dispose()); - const paused = await bounded( - session.submit( - 'Book the fictional Q3 roadmap review on Tuesday at 10:00 after checking availability.' - ) - ); - assert.equal(paused, 'paused'); - const decision = session.getSnapshot().decision; - assert.equal(decision.kind, 'native'); - assert.equal(decision.interrupts[0].reason, 'book_meeting'); - assert.deepEqual(decision.interrupts[0].metadata.reason, { - topic: 'Q3 roadmap review', - slot: 'Tuesday 10:00', - }); - const before = await (await fetch(url + '/__stats')).json(); - assert.equal(before.current.observed.entries, 1); - assert.equal(before.current.observed.completedReturns.length, 0); - assert.equal( - before.current.observed.backendState.booking.status, - 'pending' - ); - const outcome = await bounded( - session.resume( - decision.id, - decision.interrupts.map((i) => ({ - interruptId: i.id, - status: 'resolved', - payload: { approved }, - })) - ) - ); - assert.equal(outcome, 'success'); - const stats = await (await fetch(url + '/__stats')).json(), - current = stats.current; - assert.equal(current.requests.length, 2); - assert.equal( - current.observed.model.httpSeen, - scenario === 'batch' ? 2 : 3 - ); - assert.equal(current.observed.entries, 2); - assert.equal(current.observed.interruptedUnwinds, 1); - assert.equal(current.observed.completedReturns.length, 1); - assert.equal( - current.observed.backendState.booking.status, - approved ? 'booked' : 'declined' - ); - assert.equal( - current.observed.observedState.booking.status, - scenario === 'batch' ? (approved ? 'booked' : 'declined') : 'pending' - ); - const canonical = approved - ? 'Meeting booked for Tuesday 10:00: Q3 roadmap review' - : 'The human declined. Meeting NOT booked: Q3 roadmap review'; - const all = current.requests.flatMap(events), - result = all.find( - (e) => - e.type === 'TOOL_CALL_RESULT' && - JSON.parse(e.content) === canonical - ); - assert.ok(result); - const call = all.find( - (e) => - e.type === 'TOOL_CALL_START' && e.toolCallName === 'book_meeting' - ); - assert.equal(result.toolCallId, call.toolCallId); - const terminal = events(current.requests[1]).find( - (e) => e.type === 'RUN_FINISHED' - ); - assert.equal(terminal.outcome.type, 'success'); - assert.equal(terminal.runId, current.requests[1].body.runId); - const snapshots = all - .filter((e) => e.type === 'STATE_SNAPSHOT') - .map((e) => e.snapshot.booking?.status) - .filter(Boolean); - assert.ok(snapshots.includes('pending')); - assert.equal( - snapshots.at(-1), - scenario === 'batch' ? (approved ? 'booked' : 'declined') : 'pending' - ); - for (const entry of current.requests) - assert.equal(entry.upstreamBase64, entry.sentBase64); - assert.equal( - current.observed.model.records.at(-1).canonicalContinuation, - canonical - ); - assert.ok( - current.observed.model.records.every( - (r) => - r.requestHeaders.find( - ([key]) => key.toLowerCase() === 'x-stainless-retry-count' - )?.[1] === '0' - ) - ); - await session.dispose(); - await fixture.close(); - const receipt = fixture.stats().closed[0]; - assert.ok(Object.values(receipt.closure).every(Boolean)); - assert.equal(existsSync(preparationLock), false); - } - ); -test( - 'two explicit booking turns in the same native Session use distinct current tool identities', - { timeout: 40000 }, - async (t) => { - const { fixture, url } = await setup(t); - await fixture.reset({ scenario: 'batch' }); - const session = createSession({ - url: url + '/runtimes/aws-strands/agent', - threadId: crypto.randomUUID(), - }); - t.after(() => session.dispose()); - for (const approved of [true, false]) { - await bounded( - session.submit( - 'Book the fictional Q3 roadmap review on Tuesday at 10:00 after checking availability.' - ) - ); - const d = session.getSnapshot().decision; - assert.equal(d.kind, 'native'); - await bounded( - session.resume( - d.id, - d.interrupts.map((i) => ({ - interruptId: i.id, - status: 'resolved', - payload: { approved }, - })) - ) - ); - } - const stats = await (await fetch(url + '/__stats')).json(); - const starts = stats.current.requests - .flatMap(events) - .filter( - (e) => e.type === 'TOOL_CALL_START' && e.toolCallName === 'book_meeting' - ); - assert.equal(starts.length, 2); - assert.notEqual(starts[0].toolCallId, starts[1].toolCallId); - assert.equal(stats.current.observed.model.httpSeen, 4); - } -); -test( - 'New after pause and completion starts fresh histories without reusing prior authority', - { timeout: 40000 }, - async (t) => { - const { fixture, url } = await setup(t); - await fixture.reset({ scenario: 'batch' }); - const ids = []; - for (const approve of [null, true, false]) { - const session = createSession({ - url: url + '/runtimes/aws-strands/agent', - threadId: crypto.randomUUID(), - }); - try { - assert.equal( - await bounded( - session.submit( - 'Book the fictional Q3 roadmap review on Tuesday at 10:00 after checking availability.' - ) - ), - 'paused' - ); - const d = session.getSnapshot().decision; - ids.push(d.interrupts[0].id); - if (approve !== null) - assert.equal( - await bounded( - session.resume( - d.id, - d.interrupts.map((i) => ({ - interruptId: i.id, - status: 'resolved', - payload: { approved: approve }, - })) - ) - ), - 'success' - ); - } finally { - await bounded(session.dispose(), 4000); - } - } - assert.equal(new Set(ids).size, 3); - const stats = await (await fetch(url + '/__stats')).json(); - assert.equal(stats.current.observed.model.httpSeen, 5); - assert.equal(stats.current.observed.completedReturns.length, 2); - } -); -for (const action of ['stop', 'dispose']) - test( - `native held ${action} closes actual provider peer before owner cleanup`, - { timeout: 30000 }, - async (t) => { - const { fixture, url } = await setup(t); - await fixture.reset({ scenario: 'cancel' }); - const session = createSession({ - url: url + '/runtimes/aws-strands/agent', - threadId: crypto.randomUUID(), - }); - t.after(() => session.dispose()); - const pending = session.submit( - 'Book the fictional Q3 roadmap review on Tuesday at 10:00 after checking availability.' - ); - let seen; - const deadline = Date.now() + 5000; - while (Date.now() < deadline) { - seen = await (await fetch(url + '/__stats')).json(); - if (seen.current.observed.model.heldActive) break; - await pause(25); - } - assert.equal(seen.current.observed.model.heldActive, 1); - await bounded(session[action](), 4000); - assert.equal(await bounded(pending, 4000), 'aborted'); - const peerDeadline = Date.now() + 3000; - while (Date.now() < peerDeadline) { - seen = await (await fetch(url + '/__stats')).json(); - if ( - seen.current.observed.model.records[0].peerClosed && - seen.current.observed.model.active === 0 - ) - break; - await pause(25); - } - const record = seen.current.observed.model.records[0]; - assert.equal(record.peerClosed, true); - assert.equal(record.cleanupReleased, false); - assert.equal(seen.current.observed.entries, 0); - await fixture.close(); - assert.ok( - Object.values(fixture.stats().closed[0].closure).every(Boolean) - ); - } - ); -test( - 'tap guards routes, serializes resets and closes incomplete caller bodies', - { timeout: 30000 }, - async (t) => { - const { fixture, url, module } = await setup(t); - assert.equal((await fetch(url + '/unknown')).status, 404); - assert.equal((await fetch(url + '/__reset')).status, 405); - await Promise.all([fixture.reset(), fixture.reset({ scenario: 'batch' })]); - assert.equal(fixture.stats().closed.length, 1); - assert.equal(fixture.stats().current.scenario, 'batch'); - assert.equal( - ( - await fetch(url + '/runtimes/aws-strands/agent', { - method: 'POST', - body: '{broken', - }) - ).status, - 500 - ); - assert.equal( - Buffer.from( - fixture.stats().current.requests[0].requestBase64, - 'base64' - ).toString(), - '{broken' - ); - const partial = httpRequest(url + '/runtimes/aws-strands/agent', { - method: 'POST', - }); - partial.on('error', () => {}); - partial.write('{partial'); - const deadline = Date.now() + 1000; - while (fixture.stats().current.requests.length < 2 && Date.now() < deadline) - await pause(5); - assert.equal(fixture.stats().current.active, 1); - await Promise.all([fixture.close(), fixture.close()]); - partial.destroy(); - assert.equal(fixture.stats().closed[1].active, 0); - const terminal = - 'data: {"type":"RUN_FINISHED","runId":"r","threadId":"t","outcome":{"type":"success"}}\n\n'; - assert.equal(module.transformFrame(terminal, 'missing-terminal'), ''); - assert.match( - module.transformFrame(terminal, 'wrong-terminal'), - /negative-wrong-run/ - ); - assert.match( - module.transformFrame(terminal, 'child-only-terminal'), - /SUBAGENT_FINISHED/ - ); - assert.equal(module.transformFrame(terminal, 'approval'), terminal); - } -); - -for (const scenario of [ - 'missing-terminal', - 'wrong-terminal', - 'child-only-terminal', -]) - test( - `negative ${scenario} retains genuine upstream terminal and canonical result`, - { timeout: 30000 }, - async (t) => { - const { fixture, url } = await setup(t); - await fixture.reset({ scenario }); - const session = createSession({ - url: url + '/runtimes/aws-strands/agent', - threadId: crypto.randomUUID(), - }); - t.after(() => session.dispose()); - assert.equal( - await bounded( - session.submit( - 'Book the fictional Q3 roadmap review on Tuesday at 10:00 after checking availability.' - ) - ), - 'paused' - ); - const d = session.getSnapshot().decision; - await bounded( - session.resume( - d.id, - d.interrupts.map((i) => ({ - interruptId: i.id, - status: 'resolved', - payload: { approved: true }, - })) - ) - ); - const stats = await (await fetch(url + '/__stats')).json(), - entries = stats.current.requests; - assert.equal(entries.length, 2); - assert.equal(entries[0].upstreamBase64, entries[0].sentBase64); - const entry = entries[1], - native = events(entry); - const upstreamTerminal = native.find((e) => e.type === 'RUN_FINISHED'); - assert.equal(upstreamTerminal.runId, entry.body.runId); - assert.equal(upstreamTerminal.outcome.type, 'success'); - assert.ok( - native.some( - (e) => - e.type === 'TOOL_CALL_RESULT' && - JSON.parse(e.content) === - 'Meeting booked for Tuesday 10:00: Q3 roadmap review' - ) - ); - const sent = events({ ...entry, upstreamBase64: entry.sentBase64 }); - assert.equal( - sent.some( - (e) => e.type === 'RUN_FINISHED' && e.runId === entry.body.runId - ), - false - ); - assert.equal(stats.current.observed.model.httpSeen, 3); - if (scenario === 'child-only-terminal') - assert.ok(sent.some((e) => e.type === 'SUBAGENT_FINISHED')); - } - ); - -test( - 'absolute operation deadline closes a partial caller body before owner shutdown', - { timeout: 30000 }, - async (t) => { - const { fixture, url } = await setup(t, { operationMs: 100 }); - await fixture.reset(); - const partial = httpRequest(url + '/runtimes/aws-strands/agent', { - method: 'POST', - }); - partial.on('error', () => {}); - let callerClosed = false; - partial.once('close', () => { - callerClosed = true; - }); - partial.write('{partial'); - t.after(() => partial.destroy()); - const deadline = Date.now() + 1500; - // The deadline flag precedes asynchronous physical close and active cleanup. - while ( - Date.now() < deadline && - (!fixture.stats().current.requests[0]?.deadlineExceeded || - !callerClosed || - fixture.stats().current.active !== 0) - ) - await pause(10); - const stats = fixture.stats().current; - assert.equal(stats.requests[0]?.deadlineExceeded, true); - assert.equal(callerClosed, true); - assert.equal(partial.socket?.destroyed, true); - assert.equal(stats.active, 0); - assert.ok( - Date.now() <= deadline, - 'Physical close and cleanup must settle within 1500ms' - ); - } -); - -for (const action of ['deadline', 'close']) - test( - `partial reset ${action} settles without backend allocation`, - { timeout: 3000 }, - async (t) => { - const { createNativeStrandsFixture } = await import( - './native-strands-fixture.mjs' - ); - const fixture = createNativeStrandsFixture({ - operationMs: action === 'deadline' ? 30 : 5000, - }); - const request = new PassThrough(); - request.method = 'POST'; - const response = new PassThrough(); - response.writeHead = () => response; - const handled = fixture(request, response, '/__reset'); - t.after(async () => { - request.destroy(); - response.destroy(); - await fixture.close(); - await handled; - }); - request.write('{partial'); - // Let the async iterator retain the received chunk before closing its peer. - await pause(0); - if (action === 'close') await bounded(fixture.close(), 300); - else await pause(150); - assert.equal(request.destroyed, true); - assert.equal(response.destroyed, true); - assert.equal(await bounded(handled, 300), true); - const stats = fixture.stats(); - assert.equal(stats.current, null); - assert.deepEqual(stats.closed, []); - assert.equal(stats.activeControls, 0); - assert.equal(stats.controlRequests.length, 1); - assert.equal( - Buffer.from( - stats.controlRequests[0].requestBase64, - 'base64' - ).toString(), - '{partial' - ); - assert.equal( - stats.controlRequests[0].deadlineExceeded, - action === 'deadline' - ); - assert.equal(response.listenerCount('close'), 0); - assert.equal(existsSync(preparationLock), false); - await fixture.close(); - await assert.rejects(fixture.reset(), /Fixture closed/); - const lateRequest = new PassThrough(); - lateRequest.method = 'POST'; - const lateResponse = new PassThrough(); - let status; - lateResponse.writeHead = (value) => { - status = value; - return lateResponse; - }; - try { - assert.equal( - await bounded(fixture(lateRequest, lateResponse, '/__reset'), 300), - true - ); - assert.equal(status, 500); - assert.equal(fixture.stats().controlRequests.length, 1); - } finally { - lateRequest.destroy(); - lateResponse.destroy(); - } - } - ); diff --git a/scripts/react-cockpit/prepare-native-maf-fixture.mjs b/scripts/react-cockpit/prepare-native-maf-fixture.mjs deleted file mode 100644 index 81718935e..000000000 --- a/scripts/react-cockpit/prepare-native-maf-fixture.mjs +++ /dev/null @@ -1,29 +0,0 @@ -/** Local test dependency owner; no deployment or backend source is modified. */ -import { execFileSync } from 'node:child_process'; -import { mkdirSync, rmSync } from 'node:fs'; -import { resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { root, project, environment, preparationLock, fixtureEnvironment } from '../../fixtures/react-parity/native-maf-react/configuration.mjs'; -export { root, project, environment, preparationLock, fixtureEnvironment } from '../../fixtures/react-parity/native-maf-react/configuration.mjs'; -export function prepareNativeMafFixture({ run = execFileSync } = {}) { - mkdirSync(preparationLock); - try { - run('uv', ['sync', '--frozen', '--python', '3.12', '--project', project], { - cwd: root, - stdio: 'inherit', - timeout: 180000, - env: { - ...fixtureEnvironment(), - UV_PYTHON: '3.12', - UV_PROJECT_ENVIRONMENT: environment, - }, - }); - } finally { - rmSync(preparationLock, { recursive: true }); - } -} -if ( - process.argv[1] && - resolve(process.argv[1]) === fileURLToPath(import.meta.url) -) - prepareNativeMafFixture(); diff --git a/scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs b/scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs deleted file mode 100644 index cc583f4d9..000000000 --- a/scripts/react-cockpit/prepare-native-maf-fixture.spec.mjs +++ /dev/null @@ -1,58 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { existsSync, mkdirSync, rmSync, readFileSync } from 'node:fs'; -import { - prepareNativeMafFixture, - preparationLock, - environment, - project, -} from './prepare-native-maf-fixture.mjs'; -test('fixture-owned modules never import the scripts project', () => { - for (const file of ['server.mjs', 'server.spec.mjs']) { - const source = readFileSync(new URL(`../../fixtures/react-parity/native-maf-react/${file}`, import.meta.url), 'utf8'); - assert.doesNotMatch(source, /from ['"][^'"]*scripts\//); - } -}); -test('preparation uses isolated exact frozen Python 3.12 environment and bounded acquisition', () => { - let invocation; - prepareNativeMafFixture({ - run: (...args) => { - invocation = args; - }, - }); - assert.deepEqual(invocation[1], [ - 'sync', - '--frozen', - '--python', - '3.12', - '--project', - project, - ]); - assert.equal(invocation[2].timeout, 180000); - assert.equal(invocation[2].env.UV_PROJECT_ENVIRONMENT, environment); - assert.equal(invocation[2].env.UV_PYTHON, '3.12'); - assert.equal( - invocation[2].env.THREADPLANE_AG_UI_FIXTURE_ENVIRONMENT, - undefined - ); - assert.equal(existsSync(preparationLock), false); -}); -test('preparation serializes and releases its lock on failure without deleting another owner', () => { - mkdirSync(preparationLock); - try { - assert.throws(() => prepareNativeMafFixture(), /EEXIST/); - assert.ok(existsSync(preparationLock)); - } finally { - rmSync(preparationLock, { recursive: true }); - } - assert.throws( - () => - prepareNativeMafFixture({ - run: () => { - throw Error('intentional'); - }, - }), - /intentional/ - ); - assert.equal(existsSync(preparationLock), false); -}); diff --git a/scripts/react-cockpit/prepare-native-mastra-fixture.mjs b/scripts/react-cockpit/prepare-native-mastra-fixture.mjs deleted file mode 100644 index ba52f2609..000000000 --- a/scripts/react-cockpit/prepare-native-mastra-fixture.mjs +++ /dev/null @@ -1,31 +0,0 @@ -/** Explicit frozen preparation. Run serialized before other Mastra dependency users. */ -import { execFileSync } from 'node:child_process'; -import { mkdirSync, rmSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { createHash } from 'node:crypto'; -import { dirname, resolve, join } from 'node:path'; -import { fileURLToPath } from 'node:url'; -export const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); -export const serviceProject = resolve(root, 'deployments/ag-ui-mastra'); -const lock = join( - tmpdir(), - 'threadplane-mastra-install-' + - createHash('sha256').update(serviceProject).digest('hex').slice(0, 16) -); -export function prepareNativeMastraFixture({ run = execFileSync } = {}) { - mkdirSync(lock); - try { - run('npm', ['ci', '--prefix', serviceProject, '--no-audit', '--no-fund'], { - cwd: root, - stdio: 'inherit', - timeout: 180000, - }); - } finally { - rmSync(lock, { recursive: true }); - } -} -if ( - process.argv[1] && - resolve(process.argv[1]) === fileURLToPath(import.meta.url) -) - prepareNativeMastraFixture(); diff --git a/scripts/react-cockpit/prepare-native-mastra-fixture.spec.mjs b/scripts/react-cockpit/prepare-native-mastra-fixture.spec.mjs deleted file mode 100644 index 979381d8e..000000000 --- a/scripts/react-cockpit/prepare-native-mastra-fixture.spec.mjs +++ /dev/null @@ -1,35 +0,0 @@ -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { - prepareNativeMastraFixture, - serviceProject, -} from './prepare-native-mastra-fixture.mjs'; -test('preparation uses frozen independent service dependencies', () => { - let invocation; - prepareNativeMastraFixture({ - run: (...args) => { - invocation = args; - }, - }); - assert.equal(invocation[0], 'npm'); - assert.deepEqual(invocation[1], [ - 'ci', - '--prefix', - serviceProject, - '--no-audit', - '--no-fund', - ]); - assert.equal(invocation[2].timeout, 180000); -}); -test('failed preparation releases install serialization lock', () => { - assert.throws( - () => - prepareNativeMastraFixture({ - run: () => { - throw new Error('install failed'); - }, - }), - /install failed/ - ); - prepareNativeMastraFixture({ run: () => undefined }); -}); diff --git a/scripts/react-cockpit/prepare-native-strands-fixture.mjs b/scripts/react-cockpit/prepare-native-strands-fixture.mjs deleted file mode 100644 index 0badca56d..000000000 --- a/scripts/react-cockpit/prepare-native-strands-fixture.mjs +++ /dev/null @@ -1,41 +0,0 @@ -/** Local test dependency owner; no deployment or backend source is modified. */ -import { execFileSync } from 'node:child_process'; -import { mkdirSync, rmSync } from 'node:fs'; -import { resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { - root, - project, - environment, - preparationLock, - fixtureEnvironment, -} from '../../fixtures/react-parity/native-strands-react/configuration.mjs'; -export { - root, - project, - environment, - preparationLock, - fixtureEnvironment, -} from '../../fixtures/react-parity/native-strands-react/configuration.mjs'; -export function prepareNativeStrandsFixture({ run = execFileSync } = {}) { - mkdirSync(preparationLock); - try { - run('uv', ['sync', '--frozen', '--python', '3.12', '--project', project], { - cwd: root, - stdio: 'inherit', - timeout: 180000, - env: { - ...fixtureEnvironment(), - UV_PYTHON: '3.12', - UV_PROJECT_ENVIRONMENT: environment, - }, - }); - } finally { - rmSync(preparationLock, { recursive: true }); - } -} -if ( - process.argv[1] && - resolve(process.argv[1]) === fileURLToPath(import.meta.url) -) - prepareNativeStrandsFixture(); diff --git a/scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs b/scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs deleted file mode 100644 index 319f6aa07..000000000 --- a/scripts/react-cockpit/prepare-native-strands-fixture.spec.mjs +++ /dev/null @@ -1,53 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; -import { existsSync, mkdirSync, rmSync, readFileSync } from 'node:fs'; -test('frozen preparation isolates Python 3.12, credentials, sources and shared ownership', async () => { - const module = await import('./prepare-native-strands-fixture.mjs').catch( - () => null - ); - assert.ok(module, 'Native Strands preparation must exist'); - const { prepareNativeStrandsFixture, preparationLock, environment, project } = - module; - const source = readFileSync(project + '/uv.lock'); - process.env.OPENAI_API_KEY = 'must-not-inherit'; - process.env.AWS_SECRET_ACCESS_KEY = 'must-not-inherit'; - let args; - prepareNativeStrandsFixture({ - run: (...value) => { - args = value; - assert.ok(existsSync(preparationLock)); - }, - }); - assert.deepEqual(args[1], [ - 'sync', - '--frozen', - '--python', - '3.12', - '--project', - project, - ]); - assert.equal(args[2].env.UV_PROJECT_ENVIRONMENT, environment); - assert.equal(args[2].env.UV_PYTHON, '3.12'); - assert.equal(args[2].env.OPENAI_API_KEY, undefined); - assert.equal(args[2].env.AWS_SECRET_ACCESS_KEY, undefined); - assert.equal(args[2].timeout, 180000); - assert.equal(existsSync(preparationLock), false); - assert.deepEqual(readFileSync(project + '/uv.lock'), source); - mkdirSync(preparationLock); - try { - assert.throws(() => prepareNativeStrandsFixture(), /EEXIST/); - assert.ok(existsSync(preparationLock)); - } finally { - rmSync(preparationLock, { recursive: true }); - } - assert.throws( - () => - prepareNativeStrandsFixture({ - run: () => { - throw Error('intentional'); - }, - }), - /intentional/ - ); - assert.equal(existsSync(preparationLock), false); -}); diff --git a/scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs b/scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs index ca1335598..2f0aebe9a 100644 --- a/scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs +++ b/scripts/react-cockpit/runtimes-aws-strands-configuration.spec.mjs @@ -12,6 +12,14 @@ test('selects only the explicit native AWS Strands React entry', () => { base: '/runtimes/aws-strands/react/', port: 4643, project: 'cockpit-runtimes-aws-strands-react', + backend: { + kind: 'aimock-ag-ui', + python: 'cockpit/runtimes/aws-strands/python', + fixtures: 'cockpit/runtimes/aws-strands/angular/e2e/fixtures', + backendPort: 5643, + aimockPort: 6643, + agentPath: '/agent', + }, }); assert.ok(Object.isFrozen(entry)); assert.equal( @@ -28,7 +36,7 @@ test('selects only the explicit native AWS Strands React entry', () => { ]) assert.throws(() => reactCockpitConfiguration(key), /Unsupported/); }); -test('uses the installed native build with explicit uncached preparation and browser targets', () => { +test('uses the installed native build and the aimock-backed browser target', () => { const project = JSON.parse( readFileSync( new URL( @@ -49,14 +57,11 @@ test('uses the installed native build with explicit uncached preparation and bro ]); assert.equal(project.targets.e2e.executor, '@nx/playwright:playwright'); assert.equal(project.targets.e2e.cache, false); - assert.deepEqual(project.targets.e2e.dependsOn, ['prepare-fixture', 'build']); + assert.deepEqual(project.targets.e2e.dependsOn, ['build']); + assert.deepEqual(project.targets.e2e.inputs, ['default', 'reactAimockBackend']); + assert.equal(project.targets['prepare-fixture'], undefined); assert.equal( project.targets.e2e.options.config, 'cockpit/runtimes/aws-strands/react/e2e/playwright.config.ts' ); - assert.equal(project.targets['prepare-fixture'].cache, false); - assert.equal( - project.targets['prepare-fixture'].options.command, - 'node scripts/react-cockpit/prepare-native-strands-fixture.mjs' - ); }); diff --git a/scripts/react-cockpit/runtimes-mastra-configuration.spec.mjs b/scripts/react-cockpit/runtimes-mastra-configuration.spec.mjs index d2e31400a..7368b5506 100644 --- a/scripts/react-cockpit/runtimes-mastra-configuration.spec.mjs +++ b/scripts/react-cockpit/runtimes-mastra-configuration.spec.mjs @@ -12,6 +12,16 @@ test('explicit native Mastra entry preserves other authored defaults and rejects base: '/runtimes/mastra/react/', port: 4641, project: 'cockpit-runtimes-mastra-react', + backend: { + kind: 'aimock-mastra', + service: 'deployments/ag-ui-mastra', + fixtures: 'cockpit/runtimes/mastra/angular/e2e/fixtures', + backendPort: 5641, + aimockPort: 6641, + agentPath: '/agent/mastra', + publicAgentPath: '/agent', + headers: { 'x-internal-token': 'dev-local-token' }, + }, }); assert.ok(Object.isFrozen(entry)); assert.equal( @@ -48,4 +58,7 @@ test('Nx uses the isolated installed native build with required package dependen assert.deepEqual(project.targets.build.outputs, [ '{workspaceRoot}/dist/cockpit/runtimes/mastra/react', ]); + assert.deepEqual(project.targets.e2e.dependsOn, ['build']); + assert.deepEqual(project.targets.e2e.inputs, ['default', 'reactAimockBackend']); + assert.equal(project.targets['prepare-fixture'], undefined); }); diff --git a/scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs b/scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs index 744100f8c..5df61f1e6 100644 --- a/scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs +++ b/scripts/react-cockpit/runtimes-microsoft-agent-framework-configuration.spec.mjs @@ -12,6 +12,14 @@ test('selects only the explicit native Microsoft Agent Framework React entry', ( base: '/runtimes/microsoft-agent-framework/react/', port: 4642, project: 'cockpit-runtimes-microsoft-agent-framework-react', + backend: { + kind: 'aimock-ag-ui', + python: 'cockpit/runtimes/microsoft-agent-framework/python', + fixtures: 'cockpit/runtimes/microsoft-agent-framework/angular/e2e/fixtures', + backendPort: 5642, + aimockPort: 6642, + agentPath: '/agent', + }, }); assert.ok(Object.isFrozen(entry)); assert.equal( @@ -28,7 +36,7 @@ test('selects only the explicit native Microsoft Agent Framework React entry', ( ]) assert.throws(() => reactCockpitConfiguration(key), /Unsupported/); }); -test('uses the installed native build with explicit uncached preparation and browser targets', () => { +test('uses the installed native build and the aimock-backed browser target', () => { const project = JSON.parse( readFileSync( new URL( @@ -49,8 +57,8 @@ test('uses the installed native build with explicit uncached preparation and bro ]); assert.equal(project.targets.e2e.executor, '@nx/playwright:playwright'); assert.equal(project.targets.e2e.cache, false); - assert.deepEqual(project.targets.e2e.dependsOn, ['prepare-fixture', 'build']); + assert.deepEqual(project.targets.e2e.dependsOn, ['build']); + assert.deepEqual(project.targets.e2e.inputs, ['default', 'reactAimockBackend']); assert.equal(project.targets.e2e.options.config, 'cockpit/runtimes/microsoft-agent-framework/react/e2e/playwright.config.ts'); - assert.equal(project.targets['prepare-fixture'].cache, false); - assert.equal(project.targets['prepare-fixture'].options.command, 'node scripts/react-cockpit/prepare-native-maf-fixture.mjs'); + assert.equal(project.targets['prepare-fixture'], undefined); }); diff --git a/scripts/react-cockpit/serve.mjs b/scripts/react-cockpit/serve.mjs index 17d9d4dcc..85595064c 100644 --- a/scripts/react-cockpit/serve.mjs +++ b/scripts/react-cockpit/serve.mjs @@ -4,21 +4,15 @@ import { readFileSync } from 'node:fs'; import { dirname, extname, resolve, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; import { reactCockpitConfiguration } from './configuration.mjs'; -import { createNativeMastraFixture } from './native-mastra-fixture.mjs'; -import { createNativeStrandsFixture } from './native-strands-fixture.mjs'; -import { createNativeMafFixture } from './native-maf-fixture.mjs'; import { createLanggraphProxy } from './langgraph-proxy.mjs'; import { createAgUiProxy } from './ag-ui-proxy.mjs'; import { createAimockBackend } from './aimock-backend.mjs'; - const configuration = reactCockpitConfiguration(process.argv[2]); -const nativeStrandsFixture = configuration.library === 'runtimes' && configuration.topic === 'aws-strands' ? createNativeStrandsFixture() : null; -const nativeMafFixture = configuration.library === 'runtimes' && configuration.topic === 'microsoft-agent-framework' ? createNativeMafFixture() : null; -const nativeMastraFixture = configuration.library === 'runtimes' && configuration.topic === 'mastra' ? createNativeMastraFixture() : null; -// aimock-backed examples: the real graph runs under `langgraph dev` or, for -// AG-UI examples, the example's uvicorn server (started by the Playwright -// global setup); this server proxies the API or agent endpoint to it. +// aimock-backed examples: the real graph runs under `langgraph dev`, the +// example's uvicorn AG-UI server, or (Mastra) the deployments/ag-ui-mastra +// service, started by the Playwright global setup; this server proxies the +// API or agent endpoint to it. // `--backend` (Website previews) starts that backend here, lazily on the first // API or control request; cockpit e2e starts it from global setup. const aimockBackend = configuration.backend && process.argv.includes('--backend') @@ -33,11 +27,16 @@ const langgraphProxy = configuration.backend?.kind === 'aimock-langgraph' ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) : null; -const agUiProxy = configuration.backend?.kind === 'aimock-ag-ui' +const agUiProxy = ['aimock-ag-ui', 'aimock-mastra'].includes(configuration.backend?.kind) ? createAgUiProxy({ upstream: `http://127.0.0.1:${configuration.backend.backendPort}`, - agentPaths: [`/ag-ui/${configuration.topic}${configuration.backend.agentPath}`, '/developer-agent'], + agentPaths: [ + // e.g. /ag-ui/interrupts/agent/native, /runtimes/mastra/agent + configuration.base.replace(/\/react\/$/, '') + (configuration.backend.publicAgentPath ?? configuration.backend.agentPath), + '/developer-agent', + ], agentPath: configuration.backend.agentPath, + headers: configuration.backend.headers, modelJournal: `http://127.0.0.1:${configuration.backend.aimockPort}/__aimock/journal`, ready: aimockBackend ? () => aimockBackend.ensure() : undefined, }) @@ -53,9 +52,6 @@ const json = (response, value, status = 200) => { }; const server = createServer(async (request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; - if (nativeStrandsFixture && (await nativeStrandsFixture(request, response, pathname))) return; - if (nativeMafFixture && (await nativeMafFixture(request, response, pathname))) return; - if (nativeMastraFixture && (await nativeMastraFixture(request, response, pathname))) return; if (langgraphProxy && (await langgraphProxy(request, response, pathname))) return; if (agUiProxy && (await agUiProxy(request, response, pathname))) return; if (configuration.adapter === 'none') { @@ -121,41 +117,12 @@ const server = createServer(async (request, response) => { }); const parentServer = !process.argv.includes('--no-parent') ? createServer(server.listeners('request')[0]) : null; -let closing; -let shutdown; -if (nativeStrandsFixture || nativeMafFixture || nativeMastraFixture) { - shutdown = (code = 0) => { - closing ??= (async () => { - await (nativeStrandsFixture ?? nativeMafFixture ?? nativeMastraFixture).close(); - await Promise.all([server, parentServer].filter(Boolean).map((owned) => new Promise((resolve) => { - owned.close(resolve); - owned.closeAllConnections(); - }))); - })(); - void closing.finally(() => process.exit(code)); - }; - process.once('SIGINT', () => shutdown()); - process.once('SIGTERM', () => shutdown()); - server.once('error', () => shutdown(1)); - parentServer?.once('error', () => shutdown(1)); -} -if (aimockBackend && !shutdown) { +if (aimockBackend) { const stop = () => { void aimockBackend.close().finally(() => process.exit(0)); }; process.once('SIGINT', stop); process.once('SIGTERM', stop); } -// Own signals, errors, workers and both listeners before tooling discovery or proof. -// Only dependency tooling is reused; each reset still owns a fresh graph process. -try { - if (!closing) { - server.listen(configuration.port, '127.0.0.1'); - parentServer?.listen(3000, '127.0.0.1'); - } -} catch (error) { - if (!closing) { - console.error('Local browser fixture preparation failed:', error.message); - shutdown?.(1); - } -} +server.listen(configuration.port, '127.0.0.1'); +parentServer?.listen(3000, '127.0.0.1'); From 869db403a71cdd74f8facc62fed8603853df462f Mon Sep 17 00:00:00 2001 From: Brian Love Date: Thu, 19 Nov 2026 11:49:51 -0800 Subject: [PATCH 40/41] test(ci): re-pin the Cockpit proof step hash after rebasing the aimock stack onto main The rebased stack removes only the authored fixture ownership steps (client tools, MAF, Strands, Theming, Debug) from the cockpit-e2e job. Co-Authored-By: Claude Opus 5.5 --- scripts/ci-workflow.spec.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ci-workflow.spec.mjs b/scripts/ci-workflow.spec.mjs index 0984f4d04..de449a2dc 100644 --- a/scripts/ci-workflow.spec.mjs +++ b/scripts/ci-workflow.spec.mjs @@ -59,7 +59,7 @@ it('reuses only same-run dependencies while preserving every isolated Cockpit pr const originalSteps = consumer.steps.filter(step => ![download, restore].includes(step)) .map(step => step === nodeSetup ? { ...step, with: { ...step.with, 'node-version': 22 } } : step); assert.equal(createHash('sha256').update(JSON.stringify(originalSteps)).digest('hex'), - 'f36512856a3f4071b4a4542cab88703cdc3fa4e4958410c2b970e067487b8198', + '3651222de6f8c30595d37f54c28a98c0e84e16ab92d243d5c1245273189646b6', 'all original setup, Python, source, fixture, browser and artifact steps remain identical except the verified Node patch pin'); }); From f3d08b50fc339a6d740d9aec7b3aeb2e24c2766a Mon Sep 17 00:00:00 2001 From: Brian Love Date: Thu, 19 Nov 2026 12:32:21 -0800 Subject: [PATCH 41/41] test(cockpit): subgraphs child fixtures use state after the values rename Co-Authored-By: Claude Opus 5.5 --- cockpit/langgraph/subgraphs/react/src/application.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cockpit/langgraph/subgraphs/react/src/application.spec.ts b/cockpit/langgraph/subgraphs/react/src/application.spec.ts index 01b4dae51..b1001eeab 100644 --- a/cockpit/langgraph/subgraphs/react/src/application.spec.ts +++ b/cockpit/langgraph/subgraphs/react/src/application.spec.ts @@ -247,7 +247,7 @@ it('a child input checkpoint without fields precedes a confirmed nested route', const h = setup(), { pending, selected } = await h.start(); selected.replace({ - subgraphs: [{ ...selected.child(), values: {} }], + subgraphs: [{ ...selected.child(), state: {} }], }); selected.research(); selected.final(); @@ -265,7 +265,7 @@ it('a child with a malformed topic stays unconfirmed', async () => { { pending, selected } = await h.start(); selected.replace({ subgraphs: [ - { ...selected.child(), values: { research_topic: 42, research_brief: '' } }, + { ...selected.child(), state: { research_topic: 42, research_brief: '' } }, ], }); selected.research();