okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221scripts: agent-flow waits for visible output before Stop and compares tool cards across reload2425a1870

main
8.7 KiB156 linesraw
1/**
2 * Drive the real app through an agent turn and take screenshots (playwright-core + installed Chrome).
3 *
4 *   STRATA_URL=http://127.0.0.1:8791 SHOTS=./shots node scripts/agent-flow.mjs
5 *
6 * What it does, against a host that has the Harness installed and one API connection saved:
7 *   1. opens the app, picks the API model in the chat picker (the choice is for this chat only);
8 *   2. sends a prompt whose reply is a tool call, waits for the approval card, clicks Approve;
9 *   3. sends a long reply and clicks Stop mid-stream;
10 *   4. reloads the page, reopens the chat and sends one more message (resume, no replayed tool).
11 *
12 * Environment:
13 *   STRATA_URL     host base URL (required)
14 *   SHOTS          output directory for screenshots (default ./agent-shots)
15 *   MODEL          text of the API model in the picker (default "Cloud model")
16 *   PROMPT_TOOL    prompt that makes the model call a tool
17 *   PROMPT_LONG    prompt that makes the model write a long reply
18 *   PROMPT_AFTER   prompt sent after reopening
19 *   CHROME         path to Chrome (default: the usual Windows/macOS/Linux install)
20 *
21 * It prints one line per checkpoint and exits non-zero if a checkpoint fails.
22 */
23import { chromium } from 'playwright-core';
24import { existsSync, mkdirSync } from 'node:fs';
25import { join, resolve } from 'node:path';
26
27const BASE = process.env.STRATA_URL;
28if (!BASE) { console.error('STRATA_URL is required'); process.exit(2); }
29const SHOTS = resolve(process.env.SHOTS ?? 'agent-shots');
30mkdirSync(SHOTS, { recursive: true });
31const MODEL = process.env.MODEL ?? 'Cloud model';
32const PROMPT_TOOL = process.env.PROMPT_TOOL ?? 'SCRIPT:tool notes.txt hello from the agent';
33const PROMPT_LONG = process.env.PROMPT_LONG ?? 'SCRIPT:slow 400';
34const PROMPT_AFTER = process.env.PROMPT_AFTER ?? 'SCRIPT:echo still here after reopening';
35const CHROME = process.env.CHROME ?? [
36  'C:/Program Files/Google/Chrome/Application/chrome.exe',
37  '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
38  '/usr/bin/google-chrome',
39].find(existsSync);
40
41let failed = false;
42const check = (ok, label) => { console.log(`${ok ? 'ok  ' : 'FAIL'} ${label}`); if (!ok) failed = true; };
43const shot = async (page, name) => { await page.screenshot({ path: join(SHOTS, `${name}.png`) }); console.log(`shot ${name}.png`); };
44
45const browser = await chromium.launch({ executablePath: CHROME, headless: true });
46const page = await browser.newPage({ viewport: { width: 1512, height: 1000 } });
47const problems = [];
48page.on('pageerror', e => problems.push(`pageerror: ${e.message}`));
49page.on('console', m => { if (m.type() === 'error') problems.push(`console: ${m.text().slice(0, 200)}`); });
50
51const send = async text => {
52  const box = page.locator('textarea').first();
53  await box.fill(text);
54  await page.getByRole('button', { name: 'Send' }).click();
55};
56const idle = async (ms = 120000) => page.waitForFunction(() => document.documentElement.dataset.streaming !== 'true', null, { timeout: ms });
57
58try {
59  await page.goto(`${BASE}/#/chat`);
60  await page.waitForTimeout(2000);
61  const skip = page.getByText('Go straight to the app');
62  if (await skip.count()) { await skip.click(); await page.waitForTimeout(600); }
63  await page.getByRole('button', { name: 'New chat' }).first().click();
64  await page.waitForTimeout(600);
65  await page.getByRole('button', { name: /Choose model/ }).first().click();
66  await page.waitForTimeout(300);
67  await shot(page, '01-picker');
68  await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();
69  await page.waitForTimeout(300);
70  check((await page.getByRole('button', { name: /Choose model/ }).first().getAttribute('aria-label'))?.includes(MODEL), `the picker shows ${MODEL} for this chat`);
71
72  // 2. a tool call that needs approval
73  await send(PROMPT_TOOL);
74  await page.locator('.toolcard[data-state="proposed"]').first().waitFor({ timeout: 120000 });
75  await page.waitForTimeout(300);
76  await shot(page, '02-approval-card');
77  check(await page.getByRole('button', { name: 'Approve' }).count() === 1, 'an Approve button is on the card');
78  check(await page.getByText('Needs your approval').count() === 1, 'the card says it needs approval, not "Demo approval"');
79  await page.getByRole('button', { name: 'Approve' }).click();
80  await idle();
81  await page.waitForTimeout(500);
82  await shot(page, '03-after-approve');
83  check(await page.locator('.tool-summary').count() >= 1, 'the tool finished and collapsed into its summary');
84  check(await page.locator('.toolcard').count() === 0, 'no card is left waiting');
85
86  // 3. Stop mid-stream
87  const baseline = await page.evaluate(() => document.body.innerText.length);
88  await send(PROMPT_LONG);
89  await page.waitForFunction(() => document.documentElement.dataset.streaming === 'true');
90  // Stop while text is arriving, not during the prompt read: wait until the page has grown by a few lines.
91  await page.waitForFunction(b => document.body.innerText.length > b + 400, baseline, { timeout: 300000 });
92  const stop = page.getByRole('button', { name: /^Stop/ }).first();
93  check(await stop.count() === 1, 'a Stop button is visible while the turn runs');
94  await shot(page, '04-streaming');
95  await stop.click();
96  await idle(60000);
97  await page.waitForTimeout(500);
98  await shot(page, '05-after-stop');
99  const afterStop = await (await fetch(`${BASE}/api/agent/status`)).json();
100  check(afterStop.activeTurns === 0, 'the host reports no active turn after Stop (the turn really ended)');
101  check(await page.getByRole('button', { name: 'Send' }).count() === 1, 'the composer is free again');
102
103  // 4. reload, reopen, continue
104  const toolsBefore = await page.locator('.tool-summary').count();
105  await page.reload();
106  await page.waitForTimeout(2000);
107  await page.locator('a.navsession').first().click();
108  await page.waitForTimeout(800);
109  await shot(page, '06-reopened');
110  check(await page.locator('.tool-summary').count() === toolsBefore && toolsBefore >= 1, `reopening shows the same ${toolsBefore} finished tool card(s) and no waiting card`);
111  await send(PROMPT_AFTER);
112  await idle();
113  await page.waitForTimeout(500);
114  await shot(page, '07-after-reopen');
115  check(await page.locator('.toolcard').count() === 0, 'no tool is waiting after the reopened turn');
116  if (process.env.EXPECT_AFTER) check(await page.getByText(new RegExp(process.env.EXPECT_AFTER)).count() >= 1, `the reopened chat answered (${process.env.EXPECT_AFTER})`);
117  // 5. the model choice belongs to the chat: a new chat starts on the local model again
118  await page.getByRole('button', { name: 'New chat' }).first().click();
119  await page.waitForTimeout(600);
120  const fresh = await page.getByRole('button', { name: /Choose model/ }).first().getAttribute('aria-label');
121  check(!fresh?.includes(MODEL), `a new chat does not inherit the API model (picker says: ${fresh})`);
122  await shot(page, '08-new-chat-model');
123
124  // 6. two chats at once (optional: a real model has one inference slot, so the second just queues)
125  if (process.env.TWO_CHATS === '1') {
126    await page.locator('a.navsession').nth(1).click().catch(() => {});
127    await page.getByRole('button', { name: 'New chat' }).first().click();
128    await page.getByRole('button', { name: /Choose model/ }).first().click();
129    await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();
130    await send(process.env.PROMPT_A ?? 'SCRIPT:slow 150');
131    await page.waitForFunction(() => document.documentElement.dataset.streaming === 'true');
132    await page.waitForTimeout(800);
133    await page.getByRole('button', { name: 'New chat' }).first().click();
134    await page.getByRole('button', { name: /Choose model/ }).first().click();
135    await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();
136    check(await page.getByRole('button', { name: 'Send' }).count() === 1, 'the second chat can send while the first is mid-turn');
137    await send(process.env.PROMPT_B ?? 'SCRIPT:echo the other chat');
138    await page.getByText(process.env.EXPECT_B ?? 'the other chat').first().waitFor({ timeout: 60000 });
139    const stillRunning = await page.evaluate(() => document.documentElement.dataset.streaming);
140    await shot(page, '09-two-chats-second-done');
141    check(stillRunning === 'true', 'the first chat is still running when the second one has answered');
142    await idle();
143    await page.locator('a.navsession').first().click();
144    await page.waitForTimeout(500);
145    await shot(page, '10-two-chats-first-done');
146    check(await page.getByText(/w149/).count() >= 1, 'the first chat finished its own stream');
147  }
148  check(problems.length === 0, `no page errors (${problems.join(' | ') || 'none'})`);
149} catch (error) {
150  failed = true;
151  console.log(`FAIL ${error.message}`);
152  await page.screenshot({ path: join(SHOTS, 'zz-failure.png') }).catch(() => {});
153} finally {
154  await browser.close();
155}
156process.exit(failed ? 1 : 0);