okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3c3d2881b
main
1// Time-skip v2: plan, lever, A all the way, B forked at the lever with an improvement plan, compare, keep. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , rmSync , writeFileSync , readFileSync , existsSync } 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' ; 10import { isViewStep , stepObjective } from '../server/agent/plan.mjs' ; 11import { UNATTENDED , createForkTimeskip , leverPrompt , parseLever , defaultLever , improvementPrompt , copyProject } from '../server/timeskip-fork.mjs' ; 12 13const HARNESS = process . env . STRATA_HARNESS_DIR ; 14const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 15 16test ( 'the lever answer is read strictly; a bad answer gets a fixed rule with its reason said plainly' , () => { 17assert . deepEqual ( parseLever ( 'Sure! {"step": 3, "why": "The shape is set here."} done' , 5 ), { step :3 , why :'The shape is set here.' }); 18assert . equal ( parseLever ( '{"step": 1, "why": "x"}' , 5 ), null , 'never step 1' ); 19assert . equal ( parseLever ( '{"step": 9, "why": "x"}' , 5 ), null ); 20assert . equal ( parseLever ( 'no json' , 5 ), null ); 21assert . equal ( parseLever ( '{"step": 2}' , 5 ). why . length > 5 , true ); 22const steps = [ 'Look around' , 'Write the page' , 'Add controls' , 'Check it' ]. map ( text => ({ text})); 23const d = defaultLever ( steps ); 24assert . deepEqual ([ d . step , d . defaulted ], [ 3 , true ]); assert . match ( d . why , / default rule / ); 25assert . ok ( defaultLever ([{ text :'a' }, { text :'b' }, { text :'c' }]). step >= 2 ); 26assert . match ( leverPrompt ( 'make x' , steps ), / ONE biggest lever / ); assert . match ( leverPrompt ( 'make x' , steps ), / "step" / ); 27const ip = improvementPrompt ({ request :'make x' , steps, lever :{ step :3 , why :'shape' }, doneSummaries :[ '1. Look around: done.' ] }); 28assert . match ( ip , / IMPROVEMENT PLAN / ); assert . match ( ip , / Steps 1 to 2 are done / ); assert . match ( ip , / shape / ); assert . match ( ip , / \.timeskip\/context\.md / ); 29}); 30 31test ( 'copyProject leaves out dependency and VCS folders' , () => { 32const dir = mkdtempSync ( join ( tmpdir (), 'strata-cp-' )); 33try { 34const from = join ( dir , 'p' ); mkdirSync ( join ( from , 'node_modules' ), { recursive :true }); mkdirSync ( join ( from , 'src' ), { recursive :true }); mkdirSync ( join ( from , '.git' )); 35writeFileSync ( join ( from , 'node_modules' , 'x.js' ), 'x' ); writeFileSync ( join ( from , 'src' , 'a.txt' ), 'a' ); writeFileSync ( join ( from , '.git' , 'h' ), 'h' ); 36copyProject ( from , join ( dir , 'c' )); 37assert . ok ( existsSync ( join ( dir , 'c' , 'src' , 'a.txt' ))); assert . ok ( ! existsSync ( join ( dir , 'c' , 'node_modules' ))); assert . ok ( ! existsSync ( join ( dir , 'c' , '.git' ))); 38} finally { rmSync ( dir , { recursive :true , force :true }); } 39}); 40 41async function rig ( t , slots , opts = {}) { 42const worker = await startScriptedWorker ({}); 43const dir = mkdtempSync ( join ( tmpdir (), 'strata-tsf-' )); 44const project = join ( dir , 'project' ); mkdirSync ( project ); writeFileSync ( join ( project , 'keep.txt' ), 'original' ); 45const bridge = createAgentBridge ({ dataDir :join ( dir , 'data' ), localInstances :() => [{ id :'i' , label :'S' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots}], connections :() => [], env :process . env , startTimeoutMs :90_000 }); 46const events = []; 47const ts = createForkTimeskip ({ agent :bridge , dir :join ( dir , 'ts' ), notify :( type , data ) => events . push ( data ), ...opts }); 48t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 49return { ts, events, project, dir, worker}; 50} 51const until = async ( fn , ms = 90_000 ) => { const t0 = Date . now (); while ( Date . now () - t0 < ms ) { const v = fn (); if ( v ) return v ; await new Promise ( r => setTimeout ( r , 100 )); } throw new Error ( 'timed out' ); }; 52 53test ( 'plan -> lever (with its why) -> A runs every step -> B forks after step 1 with an improvement plan -> A and B are compared, and Keep B changes the folder' , { skip}, async ( t ) => { 54const r = await rig ( t , 2 ); 55const run = r . ts . start ({ prompt :'SCRIPT:planreply' , workspace :r . project , choice :{ instanceId :'i' }, slots :2 }); 56assert . ok ([ 'queued' , 'checkpointing' ]. includes ( run . phase ), 'a run starts at once and does not wait for the work' ); 57const done = await until (() => { const s = r . ts . status ( run . id ); return [ 'completed' , 'failed' ]. includes ( s . phase ) ?s :null ; }); 58assert . equal ( done . phase , 'completed' , done . error ); 59assert . equal ( done . plan . steps . length , 3 ); 60assert . deepEqual ([ done . lever . step , done . lever . total , done . lever . defaulted ], [ 2 , 3 , false ]); assert . match ( done . lever . why , / layout / ); 61assert . deepEqual ([ done . a . state , done . a . step , done . a . total ], [ 'done' , 3 , 3 ]); 62assert . equal ( done . b . state , 'done' ); assert . equal ( done . b . plan . length , 2 ); 63assert . equal ( done . needsUserReview , true ); 64assert . match ( done . activity . text , / ^Both versions are ready / ); assert . equal ( done . b . note ?? null , null , 'no stale warning when B finished' ); 65assert . deepEqual ( done . labels , { original :'A' , updated :'B' }); 66// events told the story in order 67const phases = r . events . map ( e => e . phase ); 68assert . ok ( phases . indexOf ( 'checkpointing' ) < phases . indexOf ( 'generating' ) && phases . indexOf ( 'generating' ) < phases . indexOf ( 'completed' )); 69assert . ok ( r . events . some ( e => e . lever && e . lever . step === 2 ), 'the lever and its reason were sent' ); 70assert . ok ( r . events . some ( e => e . a ?. line && / — / . test ( e . a . line )) && r . events . some ( e => e . b ?. line && / — / . test ( e . b . line )), "A's and B's progress were sent" ); 71assert . ok ( r . events . some ( e => / ^B, / . test ( e . activity ?. text ?? '' )), 'the activity line names B' ); 72// B was copied from A at the fork and got the context attachment; the project is untouched 73assert . ok ( existsSync ( join ( r . dir , 'ts' , run . id , 'B' , '.timeskip' , 'context.md' ))); 74assert . match ( readFileSync ( join ( r . dir , 'ts' , run . id , 'B' , '.timeskip' , 'context.md' ), 'utf8' ), / Why this step: The layout / ); 75assert . equal ( readFileSync ( join ( r . project , 'keep.txt' ), 'utf8' ), 'original' ); 76// the compare is A against B 77const found = r . ts . files ( run . id ); 78assert . ok ( Array . isArray ( found . files )); 79// keep B, then put the original back 80const kept = r . ts . apply ({ id :run . id , which :'b' }); 81assert . equal ( kept . applied , 'b' ); 82assert . throws (() => r . ts . apply ({ id :run . id , which :'a' }), / already in your files / ); 83assert . deepEqual ( r . ts . apply ({ id :run . id , restore :true }), { restored :true }); 84assert . equal ( readFileSync ( join ( r . project , 'keep.txt' ), 'utf8' ), 'original' ); 85}); 86 87test ( 'with one model slot B waits for A, says so, and still finishes' , { skip}, async ( t ) => { 88const r = await rig ( t , 1 ); 89const run = r . ts . start ({ prompt :'SCRIPT:planreply' , workspace :r . project , choice :{ instanceId :'i' }, slots :1 }); 90const done = await until (() => { const s = r . ts . status ( run . id ); return [ 'completed' , 'failed' ]. includes ( s . phase ) ?s :null ; }); 91assert . equal ( done . phase , 'completed' , done . error ); 92assert . ok ( r . events . some ( e => e . b ?. waiting === true ), 'B was shown as waiting' ); 93assert . ok ( r . events . some ( e => / B waited for A / . test ( e . activity ?. text ?? '' ))); 94assert . equal ( done . b . state , 'done' ); 95}); 96 97test ( 'Stop ends both agents, removes the copies and leaves the folder alone' , { skip}, async ( t ) => { 98const r = await rig ( t , 2 ); 99const run = r . ts . start ({ prompt :'SCRIPT:planreply' , workspace :r . project , choice :{ instanceId :'i' }, slots :2 }); 100await until (() => r . ts . status ( run . id ). a . state === 'running' ); 101assert . equal ( await r . ts . stop ( run . id ), true ); 102assert . equal ( r . ts . status ( run . id ). phase , 'cancelled' ); 103assert . ok ( ! existsSync ( join ( r . dir , 'ts' , run . id ))); 104assert . equal ( readFileSync ( join ( r . project , 'keep.txt' ), 'utf8' ), 'original' ); 105assert . equal ( r . ts . files ( run . id ), null ); 106}); 107 108test ( 'a step about looking at the page in a browser is recognised, carries the no-hang rule, and the unattended note forbids windows' , () => { 109assert . ok ( isViewStep ( 'Open goku.html in a browser to confirm rendering' )); assert . ok ( isViewStep ( 'Preview the page in a window' )); assert . ok ( ! isViewStep ( 'Write the HTML file' )); assert . ok ( ! isViewStep ( 'Count the voxels' )); 110const plan = { title :'T' , steps :[{ text :'Write it' }, { text :'Open it in a browser to check' , detail :'' }] }; 111assert . match ( stepObjective ( plan , 1 ), / Never wait for a program to close / ); assert . match ( stepObjective ( plan , 1 ), / Start-Process / ); assert . ok ( ! /Start-Process / . test ( stepObjective ( plan , 0 ))); 112assert . match ( UNATTENDED , / Never open a visible window / ); 113}); 114 115test ( 'a step that never finishes is stopped after the time limit and left as it is with a plain note; the version still completes' , { skip}, async ( t ) => { 116const r = await rig ( t , 2 , { stepTimeoutMs :4000 }); 117const run = r . ts . start ({ prompt :'SCRIPT:planslow' , workspace :r . project , choice :{ instanceId :'i' }, slots :2 }); 118const done = await until (() => { const s = r . ts . status ( run . id ); return [ 'completed' , 'failed' ]. includes ( s . phase ) ?s :null ; }, 150_000 ); 119assert . equal ( done . phase , 'completed' , done . error ); 120assert . ok ( r . events . some ( e => / step 2 took too long and was left as it is / . test ( e . activity ?. text ?? '' )), 'the person was told which step was left' ); 121assert . equal ( done . a . state , 'done' ); 122});