okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2425a1870
main
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 => { 52const box = page . locator ( 'textarea' ). first (); 53await box . fill ( text ); 54await page . getByRole ( 'button' , { name :'Send' }). click (); 55}; 56const idle = async ( ms = 120000 ) => page . waitForFunction (() => document . documentElement . dataset . streaming !== 'true' , null , { timeout :ms }); 57 58try { 59await page . goto ( ` ${ BASE } /#/chat` ); 60await page . waitForTimeout ( 2000 ); 61const skip = page . getByText ( 'Go straight to the app' ); 62if ( await skip . count ()) { await skip . click (); await page . waitForTimeout ( 600 ); } 63await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 64await page . waitForTimeout ( 600 ); 65await page . getByRole ( 'button' , { name :/ Choose model / }). first (). click (); 66await page . waitForTimeout ( 300 ); 67await shot ( page , '01-picker' ); 68await page . getByRole ( 'menuitemradio' , { name :new RegExp ( MODEL ) }). first (). click (); 69await page . waitForTimeout ( 300 ); 70check (( 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 73await send ( PROMPT_TOOL ); 74await page . locator ( '.toolcard[data-state="proposed"]' ). first (). waitFor ({ timeout :120000 }); 75await page . waitForTimeout ( 300 ); 76await shot ( page , '02-approval-card' ); 77check ( await page . getByRole ( 'button' , { name :'Approve' }). count () === 1 , 'an Approve button is on the card' ); 78check ( await page . getByText ( 'Needs your approval' ). count () === 1 , 'the card says it needs approval, not "Demo approval"' ); 79await page . getByRole ( 'button' , { name :'Approve' }). click (); 80await idle (); 81await page . waitForTimeout ( 500 ); 82await shot ( page , '03-after-approve' ); 83check ( await page . locator ( '.tool-summary' ). count () >= 1 , 'the tool finished and collapsed into its summary' ); 84check ( await page . locator ( '.toolcard' ). count () === 0 , 'no card is left waiting' ); 85 86// 3. Stop mid-stream 87const baseline = await page . evaluate (() => document . body . innerText . length ); 88await send ( PROMPT_LONG ); 89await 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. 91await page . waitForFunction ( b => document . body . innerText . length > b + 400 , baseline , { timeout :300000 }); 92const stop = page . getByRole ( 'button' , { name :/ ^Stop / }). first (); 93check ( await stop . count () === 1 , 'a Stop button is visible while the turn runs' ); 94await shot ( page , '04-streaming' ); 95await stop . click (); 96await idle ( 60000 ); 97await page . waitForTimeout ( 500 ); 98await shot ( page , '05-after-stop' ); 99const afterStop = await ( await fetch ( ` ${ BASE } /api/agent/status` )). json (); 100check ( afterStop . activeTurns === 0 , 'the host reports no active turn after Stop (the turn really ended)' ); 101check ( await page . getByRole ( 'button' , { name :'Send' }). count () === 1 , 'the composer is free again' ); 102 103// 4. reload, reopen, continue 104const toolsBefore = await page . locator ( '.tool-summary' ). count (); 105await page . reload (); 106await page . waitForTimeout ( 2000 ); 107await page . locator ( 'a.navsession' ). first (). click (); 108await page . waitForTimeout ( 800 ); 109await shot ( page , '06-reopened' ); 110check ( await page . locator ( '.tool-summary' ). count () === toolsBefore && toolsBefore >= 1 , `reopening shows the same ${ toolsBefore } finished tool card(s) and no waiting card` ); 111await send ( PROMPT_AFTER ); 112await idle (); 113await page . waitForTimeout ( 500 ); 114await shot ( page , '07-after-reopen' ); 115check ( await page . locator ( '.toolcard' ). count () === 0 , 'no tool is waiting after the reopened turn' ); 116if ( 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 118await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 119await page . waitForTimeout ( 600 ); 120const fresh = await page . getByRole ( 'button' , { name :/ Choose model / }). first (). getAttribute ( 'aria-label' ); 121check ( ! fresh ?. includes ( MODEL ), `a new chat does not inherit the API model (picker says: ${ fresh } )` ); 122await 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) 125if ( process . env . TWO_CHATS === '1' ) { 126await page . locator ( 'a.navsession' ). nth ( 1 ). click (). catch (() => {}); 127await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 128await page . getByRole ( 'button' , { name :/ Choose model / }). first (). click (); 129await page . getByRole ( 'menuitemradio' , { name :new RegExp ( MODEL ) }). first (). click (); 130await send ( process . env . PROMPT_A ?? 'SCRIPT:slow 150' ); 131await page . waitForFunction (() => document . documentElement . dataset . streaming === 'true' ); 132await page . waitForTimeout ( 800 ); 133await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 134await page . getByRole ( 'button' , { name :/ Choose model / }). first (). click (); 135await page . getByRole ( 'menuitemradio' , { name :new RegExp ( MODEL ) }). first (). click (); 136check ( await page . getByRole ( 'button' , { name :'Send' }). count () === 1 , 'the second chat can send while the first is mid-turn' ); 137await send ( process . env . PROMPT_B ?? 'SCRIPT:echo the other chat' ); 138await page . getByText ( process . env . EXPECT_B ?? 'the other chat' ). first (). waitFor ({ timeout :60000 }); 139const stillRunning = await page . evaluate (() => document . documentElement . dataset . streaming ); 140await shot ( page , '09-two-chats-second-done' ); 141check ( stillRunning === 'true' , 'the first chat is still running when the second one has answered' ); 142await idle (); 143await page . locator ( 'a.navsession' ). first (). click (); 144await page . waitForTimeout ( 500 ); 145await shot ( page , '10-two-chats-first-done' ); 146check ( await page . getByText ( / w149 / ). count () >= 1 , 'the first chat finished its own stream' ); 147} 148check ( problems . length === 0 , `no page errors ( ${ problems . join ( ' | ' ) || 'none' } )` ); 149} catch ( error ) { 150failed = true ; 151console . log ( `FAIL ${ error . message } ` ); 152await page . screenshot ({ path :join ( SHOTS , 'zz-failure.png' ) }). catch (() => {}); 153} finally { 154await browser . close (); 155} 156process . exit ( failed ?1 :0 );