okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1631a9fde
main
1/** "Continue in a new chat" on a task: the first message of the new chat carries what the task found, did, and the files it made. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { build } from 'esbuild' ; 5import { createAgentTaskRunner } from '../server/agent/task-run.mjs' ; 6import { createTaskService } from '../server/tasks.mjs' ; 7 8const compiled = await build ({ stdin :{ contents :"export * from './src/lib/task-handover'; export * from './src/lib/i18n';" , resolveDir :process . cwd () }, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' } }); 9const { splitTaskOutput, taskHandoverMessage, taskHandoverTitle, setTranslationLanguage, loadCatalog} = await import ( `data:text/javascript;base64, ${ Buffer . from ( compiled . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 10for ( const l of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) await loadCatalog ( l ); // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr() 11 12const OUTPUT = 'Two things need your attention.\n\n- Disk space is low.\n- The card ran hot twice.\n\n---\n\n**What it did**\n\n- Read the temperature log\n- Checked free space on C: and D:\n\n> This task wanted to change something and needed a yes. Nothing was changed.' ; 13const task = { id :'t' , title :'Hardware check' , prompt :'Check it' , frequency :'daily' , at :'08:00' , weekday :1 , timeZone :'UTC' , enabled :true , lastRunAt :'2026-10-18T06:00:00.000Z' , output :OUTPUT , 14files :[{ name :'report.md' , path :'C:\\Users\\you\\Documents\\Strata\\Workspace\\report.md' }] }; 15 16test ( 'the saved answer is split into what it found, what it did, and the note that needed a yes' , () => { 17const parts = splitTaskOutput ( OUTPUT ); 18assert . equal ( parts . found , 'Two things need your attention.\n\n- Disk space is low.\n- The card ran hot twice.' ); 19assert . deepEqual ( parts . did , [ 'Read the temperature log' , 'Checked free space on C: and D:' ]); 20assert . match ( parts . attention , / needed a yes / ); 21assert . deepEqual ( splitTaskOutput ( 'Just an answer.' ), { found :'Just an answer.' , did :[], attention :'' }); 22assert . deepEqual ( splitTaskOutput ( '' ), { found :'' , did :[], attention :'' }); 23}); 24 25test ( 'the first message names the task and carries the findings, the steps, the files and a next step' , () => { 26setTranslationLanguage ( 'en' ); 27const text = taskHandoverMessage ( task ); 28assert . match ( text , / ^\*\*Continuing from the task “Hardware check”\*\* \(ran . * , finished\)\. / ); 29for ( const part of [ '**What it found**' , 'Disk space is low.' , '**Needs your attention**' , 'needed a yes' , '**What it did**' , '- Read the temperature log' , '**Files**' , '- C:\\Users\\you\\Documents\\Strata\\Workspace\\report.md' , 'Please help me take it from here.' ]) assert . ok ( text . includes ( part ), part ); 30assert . ok ( text . indexOf ( 'What it found' ) < text . indexOf ( 'What it did' ) && text . indexOf ( 'What it did' ) < text . indexOf ( 'Files' ), 'in reading order' ); 31assert . equal ( taskHandoverTitle ( task ), 'Continuing from the task “Hardware check”' ); 32}); 33 34test ( 'a task that did not finish says what went wrong; a task with no files has no Files part' , () => { 35setTranslationLanguage ( 'en' ); 36const failed = taskHandoverMessage ({ ...task , error :'The model is not reachable.' , output :'' , files :[] }); 37assert . match ( failed , / did not finish / ); assert . match ( failed , / \*\*What went wrong\*\*\n\nThe model is not reachable\. / ); 38assert . ok ( ! failed . includes ( '**Files**' ) && ! failed . includes ( '**What it found**' )); 39}); 40 41test ( 'the labels follow the language of the app' , () => { 42setTranslationLanguage ( 'de' ); 43const text = taskHandoverMessage ( task ); 44assert . match ( text , / Weiter mit der Aufgabe „Hardware check“ / ); assert . match ( text , / \*\*Was sie gefunden hat\*\* / ); assert . match ( text , / Dateien / ); 45setTranslationLanguage ( 'en' ); 46}); 47 48test ( 'a scheduled run keeps the files the agent made, and a later failed run does not keep the old ones' , async () => { 49const frames = [{ type :'delta' , text :'Done.' }, { type :'deliverable' , abs :'/w/report.md' , name :'report.md' , path :'report.md' }, { type :'deliverable' , abs :'/w/report.md' , name :'report.md' , path :'report.md' }]; 50const agent = { runTurn :async ({ onFrame}) => { for ( const f of frames ) onFrame ( f ); }, stop :async () => {}, forget () {}, approve :async () => {} }; 51const run = createAgentTaskRunner ({ agent, choice :() => ({ instanceId :'i' }), workspace :'/w' }); 52let store = []; 53const service = createTaskService ({ read :() => store , write :v => { store = v ; }, run}); 54const saved = service . put ({ title :'T' , prompt :'p' , frequency :'daily' , at :'08:00' , timeZone :'UTC' }); 55await service . execute ( saved . id ); 56assert . deepEqual ( service . list ()[ 0 ]. files , [{ name :'report.md' , path :'/w/report.md' }]); 57frames . length = 0 ; frames . push ({ type :'error' , message :'boom' }); 58await service . execute ( saved . id ); 59assert . equal ( service . list ()[ 0 ]. status , 'error' ); assert . deepEqual ( service . list ()[ 0 ]. files , []); 60}); 61 62test ( 'a long run keeps the first steps, shortened, and says how many more' , () => { 63setTranslationLanguage ( 'en' ); 64const steps = Array . from ({ length :14 }, ( _ , i ) => `Step ${ i + 1 } ${ 'x' . repeat ( 300 )} ` ); 65const text = taskHandoverMessage ({ ...task , output :`Done.\n\n---\n\n**What it did**\n\n ${ steps . map ( s => `- ${ s } ` ). join ( '\n' )} ` , files :[] }); 66const lines = text . split ( '\n' ). filter ( l => l . startsWith ( '- ' )); 67assert . equal ( lines . length , 11 ); assert . ok ( lines . slice ( 0 , 10 ). every ( l => l . length <= 162 && l . endsWith ( '…' ))); 68assert . equal ( lines [ 10 ], '- …and 4 more steps' ); 69});