okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3c3d2881b
main
1// /plan, plan goals, goal points and queued messages through the real Harness with a scripted model. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , rmSync } from 'node:fs' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 8import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 9import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 10 11const HARNESS = process . env . STRATA_HARNESS_DIR ; 12const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 13const wait = ms => new Promise ( r => setTimeout ( r , ms )); 14const words = frames => frames . filter ( f => f . type === 'delta' ). map ( f => f . text ). join ( '' ); 15 16async function rig ( t ) { 17const worker = await startScriptedWorker ({}); 18const dir = mkdtempSync ( join ( tmpdir (), 'strata-planrun-' )); 19const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 20const events = { plan :[], queue :[], goal :[] }; 21const local = [{ id :'inst-1' , label :'Scripted' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots :2 }]; 22const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => local , connections :() => [], env :process . env , startTimeoutMs :90_000 , 23onPlan :( chatId , event , plan ) => events . plan . push ({ chatId, event, plan}), onQueue :( chatId , queue ) => events . queue . push ({ chatId, queue}), onGoal :( chatId , goal ) => events . goal . push ({ chatId, goal}) }); 24t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 25const base = ( chatId , onFrame = () => {}) => ({ chatId, workspacePath :ws , mode :'bypass' , onFrame}); 26return { worker, bridge, ws, events, base}; 27} 28 29test ( '/plan: the assistant looks (Plan mode), the host sends the plan, and the person can edit, approve or reject it' , { skip}, async ( t ) => { 30const r = await rig ( t ); 31const out = []; 32await r . bridge . planCommand ({ ...r . base ( 'p1' , f => out . push ( f )), mode :'manual' , line :'SCRIPT:planreply' }); 33const start = out . find ( f => f . type === 'start' ); 34assert . equal ( start . mode , 'plan' , 'a plan turn is read-only whatever mode the chat is in' ); assert . equal ( start . planning , true ); 35const plan = out . find ( f => f . type === 'plan' ); 36assert . equal ( plan . event , 'proposed' ); assert . equal ( plan . plan . state , 'proposed' ); assert . equal ( plan . plan . steps . length , 3 ); 37assert . ok ( out . findIndex ( f => f . type === 'plan' ) < out . findIndex ( f => f . type === 'done' ), 'the plan frame comes before the end of the turn' ); 38assert . equal ( r . events . plan . at ( - 1 ). event , 'proposed' ); 39assert . equal ( r . bridge . planState ( 'p1' ). id , plan . plan . id ); 40const edited = r . bridge . planControl ({ chatId :'p1' , planId :plan . plan . id , action :'edit' , steps :[ 'Only step SCRIPT:goalstep' , { text :'Second SCRIPT:goalstep' , detail :'why' }], title :'My plan' }); 41assert . equal ( edited . plan . steps . length , 2 ); assert . equal ( edited . plan . state , 'proposed' ); assert . equal ( edited . plan . title , 'My plan' ); 42const approved = r . bridge . planControl ({ chatId :'p1' , planId :plan . plan . id , action :'approve' }); 43assert . equal ( approved . plan . state , 'approved' ); assert . match ( approved . prompt , / 1\. Only step / ); 44assert . throws (() => r . bridge . planControl ({ chatId :'p1' , planId :'other' , action :'approve' }), / no plan waiting / ); 45const rej = r . bridge . planControl ({ chatId :'p1' , planId :plan . plan . id , action :'reject' }); 46assert . equal ( rej . plan . state , 'rejected' ); 47assert . throws (() => r . bridge . planControl ({ chatId :'p1' , planId :plan . plan . id , action :'edit' , steps :[ 'x' ] }), / already been used / ); 48assert . deepEqual ( r . events . plan . map ( e => e . event ), [ 'proposed' , 'edited' , 'approved' , 'rejected' ]); 49}); 50 51test ( 'Start as goal: each plan step is a goal step, reported one by one, queued messages are delivered between steps, and the plan ends done' , { skip}, async ( t ) => { 52const r = await rig ( t ); 53await r . bridge . planCommand ({ ...r . base ( 'p2' ), line :'SCRIPT:planreply' }); 54const plan = r . bridge . planState ( 'p2' ); 55assert . equal ( plan . steps . length , 3 ); 56const out = []; 57let queued = null ; 58await r . bridge . planGoal ({ ...r . base ( 'p2' , f => { 59out . push ( f ); 60if ( f . type === 'plan-step' && f . index === 1 && f . state === 'running' && ! queued ) { try { queued = r . bridge . queueMessage ({ chatId :'p2' , text :'Also add a moon SCRIPT:echo noted' }); } catch ( e ) { out . push ({ type :'test-error' , message :e . message }); } } 61}), planId :plan . id }); 62assert . deepEqual ( out . filter ( f => f . type === 'test-error' ), []); 63const stepFrames = out . filter ( f => f . type === 'plan-step' ). map ( f => ` ${ f . index } : ${ f . state } ` ); 64assert . deepEqual ( stepFrames , [ '1:running' , '1:done' , '2:running' , '2:done' , '3:running' , '3:done' ]); 65const lines = words ( out ); 66assert . match ( lines , / First thing SCRIPT:goalstep — done / ); assert . match ( lines , / Third thing SCRIPT:goalstep — done / ); 67assert . ok ( out . find ( f => f . type === 'plan-step' && f . state === 'done' ). summary , 'a done step carries a short summary from its closing words' ); 68const goals = out . filter ( f => f . type === 'goal' && f . goal ); 69assert . ok ( goals . some ( f => f . goal . step ?. index === 2 && f . goal . step . total === 3 && f . goal . objective === plan . title ), 'the goal bar knows the step and shows the plan title' ); 70assert . equal ( r . bridge . planState ( 'p2' ). state , 'done' ); assert . ok ( r . bridge . planState ( 'p2' ). steps . every ( s => s . state === 'done' )); 71assert . equal ( out . filter ( f => f . type === 'plan' ). at ( - 1 ). event , 'done' ); 72assert . equal ( queued . message . deliverAt , 'step' ); 73assert . deepEqual ( out . filter ( f => f . type === 'queued-message' ). map ( f => f . state ), [ 'queued' , 'delivered' ]); 74assert . equal ( out . find ( f => f . type === 'queued-message' && f . state === 'delivered' ). afterStep , 1 ); 75assert . equal ( out . filter ( f => f . type === 'queued-release' ). length , 0 ); 76assert . match ( words ( out ), / noted / , 'the assistant answered the delivered message' ); 77assert . equal ( out . at ( - 1 ). type , 'done' ); 78assert . deepEqual ( r . bridge . takeQueue ( 'p2' ), { queue :[], released :[] }); 79}); 80 81test ( 'a goal with numbered points runs point by point and reports each one' , { skip}, async ( t ) => { 82const r = await rig ( t ); 83const out = []; 84await r . bridge . goalCommand ({ ...r . base ( 'p3' , f => out . push ( f )), line :'Make the page: 1. draw the base SCRIPT:goalstep 2. add the roof SCRIPT:goalstep' }); 85const plan = r . bridge . planState ( 'p3' ); 86assert . equal ( plan . source , 'goal' ); assert . equal ( plan . title , 'Make the page' ); assert . equal ( plan . state , 'done' ); 87assert . match ( words ( out ), / draw the base SCRIPT:goalstep — done / ); assert . match ( words ( out ), / add the roof SCRIPT:goalstep — done / ); 88}); 89 90test ( 'a message typed while a goal without points runs is queued, can be taken back, and is handed back when the goal is stopped' , { skip}, async ( t ) => { 91const r = await rig ( t ); 92assert . throws (() => r . bridge . queueMessage ({ chatId :'q1' , text :'hello' }), / No goal is running / ); 93const out = []; 94const run = r . bridge . runTurn ({ ...r . base ( 'q1' , f => out . push ( f )), goal :{ action :'create' , objective :'keep going' , maxRounds :50 } }); 95// the goal turn may take a while to start on a busy machine: queue as soon as it runs 96let a = null ; 97for ( let i = 0 ; i < 150 && ! a ; i ++ ) { try { a = r . bridge . queueMessage ({ chatId :'q1' , text :'first thing' }); } catch { await wait ( 100 ); } } 98assert . ok ( a , 'the goal was running' ); 99await wait ( 500 ); 100const b = r . bridge . queueMessage ({ chatId :'q1' , text :'second thing' }); 101assert . equal ( a . message . deliverAt , 'end' ); assert . deepEqual ( b . queue . map ( m => m . text ), [ 'first thing' , 'second thing' ]); 102const back = r . bridge . cancelQueued ({ chatId :'q1' , id :b . message . id }); 103assert . deepEqual ( back . queue . map ( m => m . text ), [ 'first thing' ]); 104assert . throws (() => r . bridge . cancelQueued ({ chatId :'q1' , id :b . message . id }), / already sent or removed / ); 105// Stop may answer 'still stopping' on a busy machine: ask again until the run has ended 106for ( let i = 0 ; i < 5 ; i ++ ) { const st = await r . bridge . stop ( 'q1' ); if ( st . stopped || ! r . bridge . isRunning ( 'q1' )) break ; } 107await run ; 108const rel = out . find ( f => f . type === 'queued-release' ); 109assert . deepEqual ( rel . messages . map ( m => m . text ), [ 'first thing' ]); 110assert . ok ( out . findIndex ( f => f . type === 'queued-release' ) < out . findIndex ( f => f . type === 'done' )); 111assert . deepEqual ( out . filter ( f => f . type === 'queued-message' ). map ( f => f . state ), [ 'queued' , 'queued' , 'cancelled' ]); 112assert . deepEqual ( r . bridge . takeQueue ( 'q1' ), { queue :[], released :[] }); 113}); 114 115test ( 'Stop in the middle of a plan pauses it with the finished steps kept; Resume goes on from the unfinished step' , { skip}, async ( t ) => { 116const r = await rig ( t ); 117await r . bridge . planCommand ({ ...r . base ( 'p4' ), line :'SCRIPT:planreply' }); 118const plan = r . bridge . planState ( 'p4' ); 119r . bridge . planControl ({ chatId :'p4' , planId :plan . id , action :'edit' , steps :[ 'Quick one SCRIPT:goalstep' , 'Slow one SCRIPT:slow 400' , 'Last one SCRIPT:goalstep' ] }); 120const out = []; 121const run = r . bridge . planGoal ({ ...r . base ( 'p4' , f => out . push ( f )), planId :plan . id }); 122for ( let i = 0 ; i < 100 && ! out . some ( f => f . type === 'plan-step' && f . index === 2 && f . state === 'running' ); i ++ ) await wait ( 100 ); 123await wait ( 1500 ); 124assert . equal (( await r . bridge . stop ( 'p4' )). stopped , true ); 125await run ; 126const paused = r . bridge . planState ( 'p4' ); 127assert . equal ( paused . state , 'paused' ); assert . deepEqual ( paused . steps . map ( s => s . state ), [ 'done' , 'pending' , 'pending' ]); 128assert . equal ( out . filter ( f => f . type === 'plan' ). at ( - 1 ). event , 'paused' ); 129const goal = await r . bridge . goalState ( 'p4' ); 130assert . equal ( goal . phase , 'paused' ); assert . equal ( goal . step . index , 2 ); 131const again = []; 132const second = r . bridge . goalCommand ({ ...r . base ( 'p4' , f => again . push ( f )), line :'resume' }); 133for ( let i = 0 ; i < 100 && ! again . some ( f => f . type === 'plan' && f . event === 'step' ); i ++ ) await wait ( 100 ); 134assert . equal ( r . bridge . planState ( 'p4' ). state , 'running' ); assert . equal ( r . bridge . planState ( 'p4' ). steps [ 1 ]. state , 'running' ); 135await wait ( 1000 ); 136await r . bridge . stop ( 'p4' ); await second ; 137assert . equal ( r . bridge . planState ( 'p4' ). state , 'paused' ); 138}); 139 140test ( '/research: gathering is not shown, a run that opened no pages is sent back once, and the answer is written in a second step with its sources listed' , { skip}, async ( t ) => { 141const r = await rig ( t ); 142const out = []; 143await r . bridge . researchCommand ({ ...r . base ( 'rs1' , f => out . push ( f )), line :'SCRIPT:echo Fact one [1]. https://a.example/x' }); 144const shown = words ( out ); 145assert . equal ( shown . split ( 'Fact one' ). length - 1 , 1 , 'only the final answer is shown, not the notes of the first two replies: ' + JSON . stringify ( shown )); 146const asked = r . worker . requests . filter ( q => q . raw . includes ( 'Research task:' ) || q . raw . includes ( 'You have not opened enough pages' ) || q . raw . includes ( 'Now write the final answer' )); 147assert . ok ( asked . length >= 3 , 'gather, nudge (no pages were opened), write' ); 148const last = r . worker . requests . at ( - 1 ). raw ; 149assert . ok ( last . includes ( 'Now write the final answer to the research task' ) && last . includes ( '## Summary' ), 'the last request carries the writing prompt' ); 150const research = out . find ( f => f . type === 'research' ); 151assert . deepEqual ( research . sources , [{ n :1 , title :'a.example' , url :'https://a.example/x' }]); 152assert . ok ( out . findIndex ( f => f . type === 'research' ) < out . findIndex ( f => f . type === 'done' )); 153assert . equal ( out . at ( - 1 ). type , 'done' ); 154// without a topic, or with search turned off, nothing runs 155const help = []; 156await r . bridge . researchCommand ({ ...r . base ( 'rs2' , f => help . push ( f )), line :'' }); 157assert . match ( words ( help ), / Type \/research followed by / ); 158});