okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e5476b0e1
main
1// Deliverable events and compact activity groups. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , rmSync , writeFileSync , readFileSync , utimesSync } from 'node:fs' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { DeliverableTracker , ActivityGroups , fileType , groupLine , lineDelta } from '../server/agent/deliverables.mjs' ; 8import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 9import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 10import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 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 NL = String . fromCharCode ( 10 ), MINUS = String . fromCharCode ( 0x2212 ); 14 15test ( 'file types, line deltas and the compact line' , () => { 16assert . deepEqual ([ 'a.html' , 'b.MD' , 'c.pdf' , 'd.csv' , 'e.PNG' , 'f.py' , 'g.txt' , 'h.zip' ]. map ( fileType ), [ 'html' , 'markdown' , 'pdf' , 'csv' , 'image' , 'code' , 'text' , 'other' ]); 17assert . deepEqual ( lineDelta ([ 'a' , 'b' , 'c' ], [ 'a' , 'c' , 'd' , 'e' ]), { added :2 , removed :1 }); 18assert . deepEqual ( lineDelta ([], [ 'x' ]), { added :1 , removed :0 }); 19assert . equal ( groupLine ({ command :2 , read :1 }, 225 , 0 ), 'Ran 2 commands, read a file +225 ' + MINUS + '0' ); 20assert . equal ( groupLine ({ read :3 }, 0 , 0 ), 'Read 3 files' ); 21assert . equal ( groupLine ({ edit :1 , search :2 }, 5 , 2 ), 'Edited a file, searched 2 searches +5 ' + MINUS + '2' ); 22}); 23 24test ( 'a created file, a changed file and a script-made file are reported once each, with sizes and line changes' , () => { 25const dir = mkdtempSync ( join ( tmpdir (), 'strata-dl-' )); 26try { 27writeFileSync ( join ( dir , 'notes.md' ), [ 'one' , 'two' , 'three' , '' ]. join ( NL )); 28const t = new DeliverableTracker ( dir ); t . snapshot (); 29assert . equal ( t . check ( 'notes.md' ), null , 'unchanged: nothing' ); 30writeFileSync ( join ( dir , 'page.html' ), [ '<h1>x</h1>' , '<p>y</p>' , '' ]. join ( NL )); 31const made = t . check ( 'page.html' ); 32assert . deepEqual ([ made . type , made . status , made . kind , made . lines , made . added , made . removed , made . name ], [ 'deliverable' , 'created' , 'html' , 2 , 2 , 0 , 'page.html' ]); 33assert . equal ( made . size , 20 ); assert . equal ( t . check ( 'page.html' ), null , 'the same state is not reported twice' ); 34writeFileSync ( join ( dir , 'notes.md' ), [ 'one' , 'TWO' , 'three' , 'four' , '' ]. join ( NL )); 35const ch = t . check ( 'notes.md' ); 36assert . deepEqual ([ ch . status , ch . added , ch . removed , ch . lines ], [ 'changed' , 2 , 1 , 4 ]); 37mkdirSync ( join ( dir , 'out' )); writeFileSync ( join ( dir , 'out' , 'data.csv' ), [ 'a,b' , '1,2' , '' ]. join ( NL )); writeFileSync ( join ( dir , 'pic.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 , 1 ])); 38mkdirSync ( join ( dir , '.git' )); writeFileSync ( join ( dir , '.git' , 'x' ), 'x' ); mkdirSync ( join ( dir , 'node_modules' )); writeFileSync ( join ( dir , 'node_modules' , 'y.js' ), 'y' ); 39const swept = t . sweep (); 40assert . deepEqual ( swept . map ( d => d . path ). sort (), [ 'out/data.csv' , 'pic.png' ], 'hidden and dependency folders are not deliverables' ); 41assert . equal ( swept . find ( d => d . path === 'pic.png' ). lines , null ); assert . equal ( swept . find ( d => d . path === 'pic.png' ). kind , 'image' ); 42assert . equal ( t . check ( '../outside.txt' ), null ); assert . equal ( t . check ( 'missing.txt' ), null ); 43} finally { rmSync ( dir , { recursive :true , force :true }); } 44}); 45 46test ( 'a snapshot cut short by its time budget never turns the rest of the folder into "new files" (QA report: Edited 308 files)' , () => { 47const dir = mkdtempSync ( join ( tmpdir (), 'strata-dl3-' )); 48try { 49// a big, old tree (the CUDA headers of the report) that was there long before the turn 50const old = new Date ( Date . now () - 3600_000 ); 51for ( const d of [ 'algorithm' , 'atomic' , 'barrier' ]) { 52mkdirSync ( join ( dir , d )); 53for ( let i = 0 ; i < 30 ; i ++ ) { const p = join ( dir , d , 'f' + i + '.h' ); writeFileSync ( p , 'line' + NL ); utimesSync ( p , old , old ); } 54} 55// the first look runs out of time after a few files; the look at the end of the turn has all the time it wants 56let calls = 0 ; 57const t = new DeliverableTracker ( dir , { now :() => ( ++ calls > 12 ?1e9 :0 ) }); 58t . snapshot (); 59assert . equal ( t . truncated , true , 'the setup: the snapshot was cut short' ); 60t . now = () => 0 ; 61assert . deepEqual ( t . sweep (), [], 'nothing the turn did not touch is reported' ); 62// a file the turn really makes is still found, and an old file the turn really changes 63writeFileSync ( join ( dir , 'barrier' , 'made.txt' ), 'x' + NL ); 64assert . deepEqual ( t . sweep (). map ( d => d . path ), [ 'barrier/made.txt' ]); 65} finally { rmSync ( dir , { recursive :true , force :true }); } 66}); 67 68test ( 'the tracker never looks inside the app, engine, component, AppData or temp folders, nor when the chat folder is the app itself' , () => { 69const dir = mkdtempSync ( join ( tmpdir (), 'strata-dl4-' )); 70try { 71const ws = join ( dir , 'work' ), engine = join ( ws , 'engine' ), temp = join ( ws , 'Temp' ), appData = join ( ws , 'AppData' ), mine = join ( ws , 'mine' ); 72for ( const d of [ ws , engine , temp , appData , mine ]) mkdirSync ( d ); 73const t = new DeliverableTracker ( ws , { forbid :[ engine ], exclude :[ temp , appData , join ( dir , 'elsewhere' )] }); 74t . snapshot (); 75for ( const d of [ engine , temp , appData , mine ]) writeFileSync ( join ( d , 'new.h' ), 'a' + NL ); 76assert . deepEqual ( t . sweep (). map ( d => d . path ), [ 'mine/new.h' ]); 77assert . equal ( t . check ( 'engine/new.h' ), null ); assert . equal ( t . check ( 'Temp/new.h' ), null ); assert . equal ( t . check ( 'AppData/new.h' ), null ); 78// a chat whose folder IS the engine folder (or inside it) tracks nothing 79const inside = new DeliverableTracker ( engine , { forbid :[ engine ] }); inside . snapshot (); 80writeFileSync ( join ( engine , 'another.h' ), 'b' + NL ); 81assert . deepEqual ( inside . sweep (), []); assert . equal ( inside . check ( 'another.h' ), null ); 82// the temp folder as the chat's own folder is fine (the exclusion is for folders inside the chat folder) 83const own = new DeliverableTracker ( temp , { exclude :[ temp ] }); own . snapshot (); 84writeFileSync ( join ( temp , 'ok.txt' ), 'c' + NL ); 85assert . deepEqual ( own . sweep (). map ( d => d . path ), [ 'ok.txt' ]); 86} finally { rmSync ( dir , { recursive :true , force :true }); } 87}); 88 89test ( 'activity groups: consecutive tools are one growing group closed by text, with counts, files and the diff stat' , () => { 90const out = []; 91const g = new ActivityGroups ({ emit :f => out . push ( f ) }); 92g . card ({ id :'1' , name :'pwsh' , state :'running' }); 93assert . equal ( out . length , 0 , 'a running card is not counted yet' ); 94g . card ({ id :'1' , name :'pwsh' , state :'done' }); g . card ({ id :'2' , name :'bash' , state :'done' }); g . card ({ id :'3' , name :'read' , state :'done' }); 95g . card ({ id :'4' , name :'write' , state :'done' }, { path :'a.html' , name :'a.html' , kind :'html' , status :'created' , added :225 , removed :0 }); 96g . card ({ id :'4' , name :'write' , state :'done' }); 97g . text (); 98const last = out . at ( - 1 ); 99assert . equal ( last . closed , true ); assert . deepEqual ( last . counts , { command :2 , read :1 , edit :1 }); assert . equal ( last . added , 225 ); 100assert . equal ( last . line , 'Ran 2 commands, read a file, edited a file +225 ' + MINUS + '0' ); assert . deepEqual ( last . files . map ( f => f . path ), [ 'a.html' ]); 101assert . equal ( new Set ( out . map ( f => f . id )). size , 1 , 'one id while it grows' ); 102g . card ({ id :'5' , name :'grep' , state :'done' }); g . text (); 103assert . notEqual ( out . at ( - 1 ). id , last . id , 'text starts a new group' ); 104const n = out . length ; g . card ({ id :'6' , name :'write' , state :'denied' }); g . text (); 105assert . equal ( out . length , n , 'a refused tool is not an activity' ); 106}); 107 108test ( 'a turn sends deliverable and activity-group frames in order, before the end' , { skip}, async ( t ) => { 109const worker = await startScriptedWorker ({}); 110const dir = mkdtempSync ( join ( tmpdir (), 'strata-dl2-' )); const ws = join ( dir , 'ws' ); mkdirSync ( ws ); 111const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => [{ id :'i' , label :'S' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots :2 }], connections :() => [], env :process . env , startTimeoutMs :90_000 }); 112t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 113const fwd = p => p . split ( String . fromCharCode ( 92 )). join ( '/' ); 114const out = []; 115const content = [ '<h1>a</h1>' , '<p>b</p>' , '' ]. join ( NL ); 116await bridge . runTurn ({ chatId :'d1' , message :'SCRIPT:call write ' + JSON . stringify ({ file_path :fwd ( join ( ws , 'page.html' )), content}), workspacePath :ws , mode :'bypass' , onFrame :f => out . push ( f ) }); 117const dl = out . find ( f => f . type === 'deliverable' ); 118assert . deepEqual ([ dl . path , dl . kind , dl . status , dl . lines , dl . added ], [ 'page.html' , 'html' , 'created' , 2 , 2 ]); 119const groups = out . filter ( f => f . type === 'activity-group' ); 120assert . equal ( groups . at ( - 1 ). closed , true ); assert . deepEqual ( groups . at ( - 1 ). counts , { edit :1 }); assert . equal ( groups . at ( - 1 ). line , 'Edited a file +2 ' + MINUS + '0' ); 121assert . ok ( out . indexOf ( dl ) < out . findIndex ( f => f . type === 'done' )); 122assert . equal ( readFileSync ( join ( ws , 'page.html' ), 'utf8' ). length , dl . size ); 123});