okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1/** 2* Fixes from the real-engine functional test of the integrated build (notes-split-uitest.md, "Functional (real engine)"): 3* tok/s and context in the agent path, plain-language tool rows, a launch that reports success, and the size and wording of 4* what the agent sends to the model. The model is scripted; the Harness, the bridge and the projector are real. 5*/ 6import test from 'node:test' ; 7import assert from 'node:assert/strict' ; 8import { mkdirSync , mkdtempSync , rmSync } from 'node:fs' ; 9import { tmpdir } from 'node:os' ; 10import { join } from 'node:path' ; 11import { TurnProjector , outcomeText , toolVerb } from '../server/agent/events.mjs' ; 12import { AGENT_PROMPT } from '../server/agent/preset.mjs' ; 13import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 14import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 15import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 16 17const HARNESS = process . env . STRATA_HARNESS_DIR ; 18const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 19const cleanup = dir => { try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }; 20const ev = ( type , data , seq = 1 , time = 1000 ) => ({ type :'event' , event :{ type, seq, time, data} }); 21const chunk = ( c , attemptId = 'a:1' ) => ({ type :'assistant-stream' , frame :{ type :'chunk' , attemptId, chunk :c } }); 22 23/** Run `fn` with a clock the test moves by hand. */ 24function withClock ( fn ) { 25const real = Date . now ; let now = 1_000_000 ; 26Date . now = () => now ; 27try { return fn ( ms => { now += ms ; }); } finally { Date . now = real ; } 28} 29 30test ( 'item 1: the agent path reports live tok/s while it streams and a final tok/s, not null' , () => withClock ( tick => { 31const p = new TurnProjector ({ startedAt :Date . now () }); 32const frames = []; 33// 60 chunks of one token each, 25 ms apart: 40 tokens per second. 34for ( let i = 0 ; i < 60 ; i ++ ) { tick ( 25 ); frames . push ( ...p . consume ( chunk ({ type :'text-delta' , index :0 , text :'ab' }))); } 35const stats = frames . filter ( f => f . type === 'stats' ); 36assert . ok ( stats . length >= 2 , 'live speed frames while the answer streams' ); 37const last = stats . at ( - 1 ); 38assert . equal ( last . source , 'stream' ); 39assert . ok ( last . tps > 30 && last . tps < 50 , `live tps ${ last . tps } ` ); 40p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :900 , outputTokens :60 } })); 41p . consume ( ev ( 'turn/end' , { turn :1 , reason :{ kind :'completed' } })); 42const done = p . finish ({ model :'M' , contextMax :65536 }). find ( f => f . type === 'done' ); 43assert . ok ( done . tps > 30 && done . tps < 50 , `final tps ${ done . tps } ` ); 44assert . equal ( done . tokensOut , 60 ); 45})); 46 47test ( 'item 1: reasoning tokens count towards the speed too (a thinking model streams reasoning first)' , () => withClock ( tick => { 48const p = new TurnProjector ({ startedAt :Date . now () }); 49for ( let i = 0 ; i < 40 ; i ++ ) { tick ( 20 ); p . consume ( chunk ({ type :'reasoning-delta' , index :0 , text :'x' })); } 50p . consume ( ev ( 'turn/end' , { turn :1 , reason :{ kind :'completed' } })); 51const done = p . finish ({}). find ( f => f . type === 'done' ); 52assert . ok ( done . tps > 40 && done . tps < 60 , `tps ${ done . tps } ` ); 53})); 54 55test ( 'item 2: the context is the LAST request (prompt + reply), not the sum of every step' , () => { 56const p = new TurnProjector ({ startedAt :0 }); 57p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :5000 , outputTokens :100 } }, 'a:1' )); 58p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :5200 , outputTokens :300 } }, 'a:2' )); 59p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :5600 , outputTokens :50 } }, 'a:3' )); 60p . consume ( ev ( 'turn/end' , { turn :1 , reason :{ kind :'completed' } })); 61const done = p . finish ({ model :'M' , contextMax :65536 }). find ( f => f . type === 'done' ); 62assert . equal ( done . contextTokens , 5650 , 'what the context holds after the turn' ); 63assert . equal ( done . tokensIn , 15800 , 'the billed sum is still reported separately' ); 64}); 65 66test ( 'item 3: a prompt the server already held counts towards the context (the Harness reports it apart from the input)' , () => { 67const p = new TurnProjector ({ startedAt :0 }); 68// The shape of a real Swift 64K run: the last request had a prompt of 8,042 tokens of which 7,317 came from the conversation cache. 69p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :5800 , outputTokens :40 , cacheReadTokens :0 } }, 'a:1' )); 70p . consume ( chunk ({ type :'usage' , usage :{ inputTokens :725 , outputTokens :110 , cacheReadTokens :7317 } }, 'a:2' )); 71p . consume ( ev ( 'turn/end' , { turn :1 , reason :{ kind :'completed' } })); 72const done = p . finish ({ model :'M' , contextMax :65536 }). find ( f => f . type === 'done' ); 73assert . equal ( done . contextTokens , 8152 , 'prompt (input + cache) + reply of the last request, which is what the engine held' ); 74assert . equal ( done . tokensIn , 5800 + 725 + 7317 ); 75}); 76 77test ( 'item 7: tool rows say what happened in plain words, with what it was about' , () => { 78const p = new TurnProjector ({ startedAt :0 }); 79const call = p . consume ( ev ( 'tool/call' , { callId :'w1' , name :'write' , arguments :JSON . stringify ({ file_path :'C:/work/notes.md' , content :'x' }) }))[ 0 ]. tool ; 80assert . equal ( call . verb , 'Writing a file' ); assert . equal ( call . target , 'C:/work/notes.md' ); 81const asked = p . consume ( ev ( 'approval/asked' , { id :'a1' , callId :'w1' , reason :'Create or replace the file C:/work/notes.md' }))[ 0 ]. tool ; 82assert . equal ( asked . state , 'proposed' ); assert . equal ( asked . target , 'C:/work/notes.md' , 'the approval card shows the file' ); 83const ok = p . consume ( ev ( 'tool/result' , { message :{ toolCallId :'w1' , content :[{ type :'text' , text :'written' }] } }))[ 0 ]. tool ; 84assert . equal ( ok . verb , 'Wrote a file' ); 85p . consume ( ev ( 'tool/call' , { callId :'w2' , name :'write' , arguments :JSON . stringify ({ file_path :'D:/x.txt' }) })); 86const bad = p . consume ( ev ( 'tool/result' , { message :{ toolCallId :'w2' , isError :true , content :[{ type :'text' , text :'denied' }] } }))[ 0 ]. tool ; 87assert . equal ( bad . state , 'error' ); assert . equal ( bad . verb , "Couldn't write the file" ); 88const sh = p . consume ( ev ( 'tool/call' , { callId :'s1' , name :'pwsh' , arguments :JSON . stringify ({ command :'Get-ChildItem C:/work' }) }))[ 0 ]. tool ; 89assert . equal ( sh . verb , 'Running a command' ); assert . equal ( sh . target , 'Get-ChildItem C:/work' ); 90const g = p . consume ( ev ( 'tool/call' , { callId :'g1' , name :'glob' , arguments :JSON . stringify ({ pattern :'**/*.md' }) }))[ 0 ]. tool ; 91assert . equal ( g . verb , 'Searching for files' ); assert . equal ( g . target , '**/*.md' ); 92assert . equal ( toolVerb ( 'glob' , 'done' ), 'Searched for files' ); 93assert . equal ( toolVerb ( 'some_connected_tool' , 'running' ), 'Using a tool' ); 94for ( const name of [ 'write' , 'edit' , 'read' , 'glob' , 'grep' , 'pwsh' , 'bash' , 'web_fetch' , 'todo_write' ]) { 95for ( const state of [ 'running' , 'done' , 'error' ]) assert . ok ( ! /^ ( write | edit | read | glob | grep | pwsh | bash | web_fetch | todo_write ) \b / . test ( toolVerb ( name , state )), ` ${ name } ${ state } : no raw tool name` ); 96} 97}); 98 99test ( 'item 8 / T9: starting a program that prints nothing is reported as started, never as opened' , () => { 100for ( const command of [ 'Start-Process explorer.exe C:\\work' , 'explorer.exe C:\\work' , 'start notepad' , 'Invoke-Item C:\\work\\a.pdf' , 'ii .' , 'xdg-open .' , 'open .' ]) { 101const said = outcomeText ( 'pwsh' , '(no output)' , { command}); 102assert . match ( said , / ^The command finished without an error, so the program was started\. / , command ); 103assert . match ( said , / cannot confirm that a window or page actually opened / , command ); 104assert . ok ( ! /was opened | is open / i . test ( said ), command ); 105} 106assert . match ( outcomeText ( 'pwsh' , '(no output)' , { command :'New-Item x.txt | Out-Null' }), / finished without any error / , 'other silent commands keep their wording' ); 107assert . equal ( outcomeText ( 'pwsh' , 'hello' , { command :'echo hello' }), 'hello' , 'a command that printed something shows it' ); 108const p = new TurnProjector ({ startedAt :0 }); 109p . consume ( ev ( 'tool/call' , { callId :'e1' , name :'pwsh' , arguments :JSON . stringify ({ command :'Start-Process explorer.exe C:\\work' }) })); 110const res = p . consume ( ev ( 'tool/result' , { message :{ toolCallId :'e1' , content :[{ type :'text' , text :'(no output)' }] } }))[ 0 ]. tool ; 111assert . match ( res . result , / cannot confirm that a window or page actually opened / ); assert . equal ( res . verb , 'Ran a command' ); 112}); 113 114test ( 'item 7/10: our prompt has no internal terms and is short' , () => { 115assert . ok ( AGENT_PROMPT . length < 1600 , ` ${ AGENT_PROMPT . length } chars` ); 116assert . doesNotMatch ( AGENT_PROMPT , / DSH | DeepSeek | sandbox | isolated session | file policy | Harness / i ); 117// O6 / #3576: the prompt points at the present tool for local files and never tells the model to claim a window opened 118assert . match ( AGENT_PROMPT , / use the present tool / ); 119assert . match ( AGENT_PROMPT , / Never say a window opened, a page rendered / ); 120assert . doesNotMatch ( AGENT_PROMPT , / say plainly that it is open | do not say you could not confirm / i ); 121}); 122 123test ( 'items 3, 7, 10: what the agent sends the model is small, plain, and carries Thinking Off' , { skip}, async t => { 124const worker = await startScriptedWorker ({}); 125const dir = mkdtempSync ( join ( tmpdir (), 'strata-fx-' )); 126const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 127const local = [{ id :'i' , baseUrl :worker . url , modelId :'scripted-model' , context :65536 }]; 128const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => local , connections :() => [], env :process . env , startTimeoutMs :90_000 }); 129t . after ( async () => { await bridge . close (); await worker . close (); cleanup ( dir ); }); 130worker . requests . length = 0 ; 131await bridge . runTurn ({ chatId :'fx-1' , message :'SCRIPT:echo hi' , workspacePath :ws , mode :'plan' , thinking :'Off' , onFrame :() => {} }); 132const req = worker . requests . find ( r => r . toolNames . length ); 133assert . ok ( req , 'a request with tools' ); 134assert . equal ( req . params . chat_template_kwargs . enable_thinking , false , 'Thinking Off reaches the model request' ); 135for ( const kept of [ 'pwsh' , 'read' , 'write' , 'edit' , 'glob' , 'grep' , 'workflow' , 'create_goal' , 'update_goal' , 'get_goal' , 'skill' , 'subagent_fork' ]) assert . ok ( req . toolNames . includes ( kept ), ` ${ kept } is offered (owner decision: the goal, skill, workflow and fork tools stay)` ); 136}); 137 138// The Reasoning menu in the agent: every level reaches the model server as chat_template_kwargs, and an unset menu is Medium (owner decision). 139for ( const [ level , thinking , effort ] of [[ 'Off' , 'Off' , undefined ], [ 'Low' , 'Low' , 'low' ], [ 'Medium' , 'Medium' , 'medium' ], [ 'High' , 'High' , 'high' ], [ 'unset (Medium)' , undefined , 'medium' ]]) { 140test ( `agent Reasoning ${ level } : the request carries chat_template_kwargs ${ effort ? `enable_thinking true, reasoning_effort ${ effort } ` : 'enable_thinking false and no effort' } ` , { skip}, async t => { 141const worker = await startScriptedWorker ({}); 142const dir = mkdtempSync ( join ( tmpdir (), 'strata-eff-' )); 143const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 144const local = [{ id :'i' , baseUrl :worker . url , modelId :'scripted-model' , context :65536 }]; 145const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => local , connections :() => [], env :process . env , startTimeoutMs :90_000 }); 146t . after ( async () => { await bridge . close (); await worker . close (); cleanup ( dir ); }); 147worker . requests . length = 0 ; 148await bridge . runTurn ({ chatId :`eff- ${ level } ` , message :'SCRIPT:echo hi' , workspacePath :ws , mode :'plan' , ...( thinking ?{ thinking} :{}), onFrame :() => {} }); 149const req = worker . requests . find ( r => r . toolNames . length ); 150assert . ok ( req , 'a request with tools' ); 151const kwargs = req . params . chat_template_kwargs ; 152assert . equal ( kwargs . enable_thinking , effort !== undefined ); 153assert . equal ( kwargs . reasoning_effort , effort ); 154}); 155}