okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1/** "Used 2 skills: Weekly report, Brand voice" under an answer: the skills a turn loaded, with the name the person gave and a one-line description. */ 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 { readFile } from 'node:fs/promises' ; 8import { transform } from 'esbuild' ; 9import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 10import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 11import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 12import { materializeSkills } from '../server/skill-files.mjs' ; 13import { SkillsUsed , skillInfoFrom , skillLabel } from '../server/agent/skills-used.mjs' ; 14 15const skip = ! process . env . STRATA_HARNESS_DIR || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 16const cleanup = dir => { try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }; 17 18test ( 'a skill is reported once, when it has worked, with the whole list so far' , () => { 19const frames = []; 20const used = new SkillsUsed ({ emit :f => frames . push ( f ) }); 21used . card ({ name :'skill' , state :'running' , args :{ name :'weekly-report' } }); 22used . card ({ name :'read' , state :'done' , args :{ name :'x' } }); 23assert . equal ( frames . length , 0 , 'not while running, not for another tool' ); 24used . card ({ name :'skill' , state :'done' , args :{ name :'weekly-report' } }); 25used . card ({ name :'skill' , state :'done' , args :{ name :'weekly-report' } }); 26used . card ({ name :'skill' , state :'error' , args :{ name :'brand-voice' } }); 27used . card ({ name :'skill' , state :'done' , args :{ name :'brand-voice' } }); 28assert . equal ( frames . length , 2 ); 29assert . deepEqual ( frames [ 1 ], { type :'skills-used' , skills :[{ name :'Weekly report' , description :'' }, { name :'Brand voice' , description :'' }] }); 30}); 31 32test ( 'the name and description come from the skill file the app wrote; a folder name is made readable' , () => { 33const dir = mkdtempSync ( join ( tmpdir (), 'strata-su-' )); 34try { 35materializeSkills ( dir , [{ id :'my-weekly-report' , name :'Weekly report' , description :'Turns notes into a short status update' , body :'Be short.' , enabled :true }]); 36const info = skillInfoFrom ( dir ); 37assert . deepEqual ( info ( 'weekly-report' ), { name :'Weekly report' , description :'Turns notes into a short status update' }); 38assert . deepEqual ( info ( 'brand-voice' ), { name :'Brand voice' , description :'' }); 39assert . deepEqual ( info ( '../../etc/passwd' ), { name :skillLabel ( '../../etc/passwd' ), description :'' }, 'a path is never read' ); 40} finally { cleanup ( dir ); } 41}); 42 43test ( 'the page keeps the list on the reply (readSkills) and tolerates a bad frame' , async () => { 44const source = await readFile ( new URL ( '../src/lib/deliverables.ts' , import . meta. url ), 'utf8' ); 45const { code} = await transform ( source . replace ( / ^import . * $ / gm , '' ), { loader :'ts' , format :'esm' }); 46const { readSkills} = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 47assert . deepEqual ( readSkills ({ skills :[{ name :'A' , description :'d' }, { name :'' }, null , { name :'B' }] }), [{ name :'A' , description :'d' }, { name :'B' , description :'' }]); 48assert . deepEqual ( readSkills ({}), []); assert . deepEqual ( readSkills ( null ), []); 49}); 50 51test ( 'real Harness: loading two saved skills in a turn sends one skills-used frame per skill, then both are listed' , { skip}, async ( t ) => { 52const worker = await startScriptedWorker ({}); 53const dir = mkdtempSync ( join ( tmpdir (), 'strata-su-run-' )); const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 54const live = join ( dir , 'skills-live' ); 55materializeSkills ( live , [ 56{ id :'my-weekly-report' , name :'Weekly report' , description :'Turns notes into a short status update' , body :'Be short.' , enabled :true }, 57{ id :'my-brand-voice' , name :'Brand voice' , description :'Plain, friendly, no jargon' , body :'Write plainly.' , enabled :true }, 58]); 59const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => [{ id :'i' , label :'Scripted' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots :2 }], connections :() => [], skillsDir :() => live , env :process . env , startTimeoutMs :90_000 }); 60t . after ( async () => { await bridge . close (); await worker . close (); cleanup ( dir ); }); 61const turn = async ( chatId , args ) => { const frames = []; await bridge . runTurn ({ chatId, message :`SCRIPT:call skill ${ JSON . stringify ( args )} ` , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); return frames . filter ( f => f . type === 'skills-used' ); }; 62const first = await turn ( 'su' , { name :'weekly-report' }); 63assert . equal ( first . length , 1 ); 64assert . deepEqual ( first [ 0 ]. skills , [{ name :'Weekly report' , description :'Turns notes into a short status update' }]); 65const none = await turn ( 'su2' , { name :'does-not-exist' }); 66assert . equal ( none . length , 0 , 'a skill that could not be loaded is not listed' ); 67}); 68 69test ( 'real Harness: two skills in one answer are listed together, in the order they were loaded' , { skip}, async ( t ) => { 70const worker = await startScriptedWorker ({}); 71const dir = mkdtempSync ( join ( tmpdir (), 'strata-su-two-' )); const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 72const live = join ( dir , 'skills-live' ); 73materializeSkills ( live , [ 74{ id :'my-weekly-report' , name :'Weekly report' , description :'Turns notes into a short status update' , body :'Be short.' , enabled :true }, 75{ id :'my-brand-voice' , name :'Brand voice' , description :'Plain, friendly, no jargon' , body :'Write plainly.' , enabled :true }, 76]); 77const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => [{ id :'i' , label :'Scripted' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots :2 }], connections :() => [], skillsDir :() => live , env :process . env , startTimeoutMs :90_000 }); 78t . after ( async () => { await bridge . close (); await worker . close (); cleanup ( dir ); }); 79const frames = []; 80await bridge . runTurn ({ chatId :'two' , message :`SCRIPT:multi ${ JSON . stringify ({ calls : [[ 'skill' , { name : 'weekly-report' }], [ 'skill' , { name : 'brand-voice' }]], say : 'Here is the note.' })} ` , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 81const last = frames . filter ( f => f . type === 'skills-used' ). at ( - 1 ); 82assert . deepEqual ( last . skills . map ( s => s . name ). sort (), [ 'Brand voice' , 'Weekly report' ]); 83assert . match ( frames . filter ( f => f . type === 'delta' ). map ( f => f . text ). join ( '' ), / Here is the note\. / ); 84assert . ok ( frames . findIndex ( f => f . type === 'skills-used' ) < frames . findIndex ( f => f . type === 'done' ), 'the list arrives before the answer is done' ); 85});