okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1/** 2* Helper agents on a model the person names (server/agent/model-routes.mjs and plugins/strata-models.mjs). 3* Pure tests always run; the real-Harness tests need STRATA_HARNESS_DIR and skip loudly without it. 4*/ 5import test from 'node:test' ; 6import assert from 'node:assert/strict' ; 7import { mkdtempSync , mkdirSync , rmSync } from 'node:fs' ; 8import { tmpdir } from 'node:os' ; 9import { join } from 'node:path' ; 10import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 11import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 12import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 13import { resolveModel , modelProblem , routeList } from '../server/agent/model-routes.mjs' ; 14import { AGENT_PROMPT } from '../server/agent/preset.mjs' ; 15 16const HARNESS = process . env . STRATA_HARNESS_DIR ; 17const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 18 19const routes = [ 20{ provider :'strata-local' , model :'gyro-gyro-s' , name :'qwen3.8-flash-next-gyro-gyro-s' , connection :'this computer' , local :true }, 21{ provider :'api-aaaa' , model :'deepseek/deepseek-v3.2' , name :'DeepSeek: DeepSeek V3.2' , connection :'OpenRouter' , local :false }, 22{ provider :'api-aaaa' , model :'anthropic/claude-sonnet-4.5' , name :'Anthropic: Claude Sonnet 4.5' , connection :'OpenRouter' , local :false }, 23{ provider :'api-bbbb' , model :'deepseek-chat' , name :'DeepSeek Chat' , connection :'DeepSeek' , local :false }, 24]; 25 26test ( 'nothing named: the helper uses the chat model' , () => { 27assert . equal ( resolveModel ( routes , {}). status , 'inherit' ); 28assert . equal ( modelProblem ( routes , {}), null ); 29}); 30 31test ( 'exact route, exact id, provider/id, connection and name, in any letters' , () => { 32const want = ( request , id ) => { const r = resolveModel ( routes , request ); assert . equal ( r . status , 'exact' , JSON . stringify ( request )); assert . equal ( r . route . model , id ); }; 33want ({ provider :'api-aaaa' , model :'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2' ); 34want ({ model :'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2' ); 35want ({ model :'DeepSeek/DeepSeek-V3.2' }, 'deepseek/deepseek-v3.2' ); 36want ({ provider :'openrouter' , model :'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2' ); 37want ({ model :'openrouter/deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2' ); 38want ({ model :'DeepSeek: DeepSeek V3.2' }, 'deepseek/deepseek-v3.2' ); 39want ({ model :'deepseek chat' }, 'deepseek-chat' ); 40want ({ model :'Local' }, 'gyro-gyro-s' ); 41want ({ provider :'strata-local' , model :'gyro-gyro-s' }, 'gyro-gyro-s' ); 42}); 43 44test ( 'a close guess is checked with the person, several fits are listed, nothing fitting lists the models' , () => { 45const guess = resolveModel ( routes , { model :'claude sonnet' }); 46assert . equal ( guess . status , 'confirm' ); assert . equal ( guess . route . model , 'anthropic/claude-sonnet-4.5' ); 47assert . match ( modelProblem ( routes , { model :'claude sonnet' }), / Did you mean Anthropic: Claude Sonnet 4\.5 / ); 48const several = resolveModel ( routes , { model :'deepseek' }); 49assert . equal ( several . status , 'ambiguous' ); assert . ok ( several . options . length >= 2 ); 50const none = modelProblem ( routes , { model :'llama banana' }); 51assert . match ( none , / No model is called "llama banana" / ); assert . match ( none , / DeepSeek Chat / ); assert . match ( none , / Nothing was started / ); 52assert . match ( modelProblem ([], { model :'x' }), / No other model is set up / ); 53assert . match ( routeList ( routes ), / running on this computer / ); 54}); 55 56test ( 'the assistant is told to use the subagent tool when asked for a subagent or a model' , () => { 57assert . match ( AGENT_PROMPT , / ask a subagent / ); assert . match ( AGENT_PROMPT , / Never look for a model with shell commands / ); 58}); 59 60async function rig ( t ) { 61const local = await startScriptedWorker (); 62const remote = await startScriptedWorker ({ model :'deepseek/deepseek-v3.2' }); 63const dir = mkdtempSync ( join ( tmpdir (), 'strata-sub-' )); 64const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 65const bridge = createAgentBridge ({ 66dataDir :dir , env :process . env , startTimeoutMs :90_000 , 67localInstances :() => [{ id :'inst-1' , label :'Local Scripted' , baseUrl :local . url , modelId :'scripted-model' , context :32768 , slots :2 }], 68connections :() => [{ id :'conn-1' , name :'OpenRouter' , baseUrl :remote . url , protocol :'openai-chat' , apiKey :'k' , models :[{ id :'deepseek/deepseek-v3.2' , name :'DeepSeek V3.2' , contextMax :32768 }] }], 69}); 70t . after ( async () => { await bridge . close (); await local . close (); await remote . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 71const call = async ( chatId , args ) => { 72const frames = []; 73await bridge . runTurn ({ chatId, message :`SCRIPT:call subagent ${ JSON . stringify ( args )} ` , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 74return { frames, text :frames . filter ( f => f . type === 'delta' ). map ( f => f . text ). join ( '' ) }; 75}; 76return { local, remote, bridge, call, ws}; 77} 78 79test ( 'the real Harness: the model sees guidance and the discovery list, and a name starts the helper on that model' , { skip}, async ( t ) => { 80const r = await rig ( t ); 81const first = await r . call ( 'm1' , { description :'Weather' , prompt :'SCRIPT:echo grey' , model :'DeepSeek V3.2' }); 82const tools = JSON . parse ([ ...r . local . requests ]. reverse (). find ( q => q . toolNames . length ). raw ). tools ; 83const sub = tools . find ( x => x . function . name === 'subagent' ). function ; 84assert . match ( sub . description , / ask a subagent / ); assert . match ( sub . description , / do not look for the model yourself / ); 85assert . match ( sub . parameters . properties . model . description , / "local" / ); 86assert . ok ( tools . some ( x => x . function . name === 'list_subagent_models' )); 87// the helper ran on the remote route, not on the chat's model 88for ( let i = 0 ; i < 60 && ! r . remote . requests . length ; i ++ ) await new Promise ( res => setTimeout ( res , 250 )); 89assert . ok ( r . remote . requests . length >= 1 , `the helper reached the OpenRouter mock ( ${ first . text } )` ); 90}); 91 92test ( 'the real Harness: a close guess and an unknown model are answered in plain words and start nothing' , { skip}, async ( t ) => { 93const r = await rig ( t ); 94const guess = await r . call ( 'm2' , { description :'Weather' , prompt :'x' , model :'deepseek v3' }); 95assert . match ( guess . text , / Did you mean DeepSeek V3\.2 / ); 96const none = await r . call ( 'm3' , { description :'Weather' , prompt :'x' , model :'llama banana' }); 97assert . match ( none . text , / No model is called "llama banana" / ); 98const seen = [ ...r . local . requests ]. reverse (). find ( q => q . lastRole === 'tool' ). raw ; 99assert . match ( seen , / DeepSeek V3.2 / , 'the list the assistant read names the models' ); 100await new Promise ( res => setTimeout ( res , 1500 )); 101assert . equal ( r . remote . requests . length , 0 , 'nothing was started' ); 102}); 103 104test ( 'the real Harness: naming the local model, and naming no model, both work' , { skip}, async ( t ) => { 105const r = await rig ( t ); 106const before = r . local . requests . length ; 107await r . call ( 'm4' , { description :'Local' , prompt :'SCRIPT:echo hi' , model :'the local model' }); 108await r . call ( 'm5' , { description :'Plain' , prompt :'SCRIPT:echo hi' }); 109for ( let i = 0 ; i < 40 && r . local . requests . filter ( q => q . directive === 'echo' ). length < 2 ; i ++ ) await new Promise ( res => setTimeout ( res , 250 )); 110assert . ok ( r . local . requests . length > before ); 111assert . equal ( r . remote . requests . length , 0 ); 112}); 113 114test ( 'the real Harness: a helper that finishes while the chat model is still answering does not end the run early ("interrupted before it finished")' , { skip}, async ( t ) => { 115// The chat model answers slowly, the helper's model (a second server) answers at once: the helper reports back while the parent is mid-answer. 116const slow = await startScriptedWorker ({ chunkDelayMs :900 }); 117const fast = await startScriptedWorker ({ model :'deepseek/deepseek-v3.2' , chunkDelayMs :5 }); 118const dir = mkdtempSync ( join ( tmpdir (), 'strata-race-' )); const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 119const bridge = createAgentBridge ({ dataDir :dir , env :process . env , startTimeoutMs :90_000 , 120localInstances :() => [{ id :'inst-1' , label :'Slow chat model' , baseUrl :slow . url , modelId :'scripted-model' , context :32768 , slots :2 }], 121connections :() => [{ id :'conn-1' , name :'OpenRouter' , baseUrl :fast . url , protocol :'openai-chat' , apiKey :'k' , models :[{ id :'deepseek/deepseek-v3.2' , name :'DeepSeek V3.2' , contextMax :32768 }] }] }); 122t . after ( async () => { await bridge . close (); await slow . close (); await fast . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 123const frames = []; 124await bridge . runTurn ({ chatId :'race' , message :`SCRIPT:call subagent ${ JSON . stringify ({ description : 'Quick' , prompt : 'SCRIPT:echo hi' , model : 'DeepSeek V3.2' })} ` , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 125assert . ok ( ! frames . some ( f => f . type === 'error' ), `no error: ${ JSON . stringify ( frames . filter ( f => f . type === 'error' ))} ` ); 126assert . equal ( frames . at ( - 1 ). type , 'done' ); 127assert . match ( frames . filter ( f => f . type === 'delta' ). map ( f => f . text ). join ( '' ), / Tool finished / , 'the chat model got to answer' ); 128});