okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1// Automatic compaction for a LOCAL model: it starts before the window is full, the chat goes on after it, and helpers get it too. 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 { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 8import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 9import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 10import { buildProfile , compactionHeadroom , localOutputBudget } from '../server/agent/profile.mjs' ; 11 12const HARNESS = process . env . STRATA_HARNESS_DIR ; 13const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 14 15test ( 'the reply keeps 30 percent of the window (at most 32K) and compaction gets a headroom that fits, for every window size' , () => { 16assert . equal ( localOutputBudget ( 32768 ), 9830 ); assert . equal ( localOutputBudget ( 262144 ), 32768 ); assert . equal ( localOutputBudget ( 8192 ), 4096 ); assert . equal ( localOutputBudget ( undefined ), 9830 ); 17for ( const w of [ 8192 , 16384 , 32768 , 65536 , 131072 , 262144 , 1_000_000 ]) { 18const out = localOutputBudget ( w ), head = compactionHeadroom ( w , out ); 19const threshold = Math . floor ( Math . min ( w * 0.8 , w - out - head )); 20assert . ok ( threshold > 0 , `window ${ w } : a positive start point ( ${ threshold } )` ); 21assert . ok ( threshold >= ( w - out ) * 0.5 , `window ${ w } : compaction starts after at least half of the room the conversation has ( ${ threshold } )` ); 22assert . ok ( Math . floor (( w - out ) * 0.16 ) < threshold , `window ${ w } : the verbatim tail is smaller than the start point` ); 23} 24const profile = buildProfile ({ local :[{ id :'a' , modelId :'m' , baseUrl :'http://x' , context :32768 }], connections :[] }); 25assert . match ( profile . patch , / "modelPolicies": \[\s * \{\s * "provider": "strata-local",\s * "model": "m",\s * "headroomTokens": 2867 / ); 26}); 27 28test ( 'a long chat on a small window compacts before it overflows, and the conversation goes on' , { skip}, async ( t ) => { 29const worker = await startScriptedWorker ({}); 30const dir = mkdtempSync ( join ( tmpdir (), 'strata-compact-' )); 31const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 32const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => [{ id :'i' , label :'S' , baseUrl :worker . url , modelId :'scripted-model' , context :16384 , slots :2 }], connections :() => [], env :process . env , startTimeoutMs :90_000 }); 33t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 34const big = 'alpha ' . repeat ( 1500 ); 35const replies = []; 36for ( let i = 0 ; i < 6 ; i ++ ) { 37const frames = []; 38await bridge . runTurn ({ chatId :'long' , message :`SCRIPT:echo round ${ i } ${ big } ` , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 39replies . push ( frames . filter ( f => f . type === 'delta' && ! f . noteReason ). map ( f => f . text ). join ( '' ). slice ( 0 , 12 )); 40assert . equal ( frames . at ( - 1 ). type , 'done' , `round ${ i } ended normally` ); 41} 42assert . ok ( replies . every ( r => / ^round\d / . test ( r )), 'every round was answered: ' + replies . join ( ' | ' )); 43const reqs = worker . requests ; 44const summaries = reqs . filter ( r => r . summary ); 45assert . ok ( summaries . length >= 1 , 'the Harness asked for a summary of the old history' ); 46const answers = reqs . filter ( r => ! r . summary && r . toolNames . length ); 47const peak = Math . max ( ...answers . map ( r => r . promptChars )); 48assert . ok ( peak < 16384 * 4 , `no request was bigger than the window (about ${ Math . round ( peak / 4 )} tokens of 16384)` ); 49const lastRoundPrompt = answers . at ( - 1 ). promptChars ; 50assert . ok ( lastRoundPrompt < answers [ Math . min ( 4 , answers . length - 1 )]. promptChars + 1 , 'the prompt stopped growing' ); 51});