okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1#!/usr/bin/env node 2/** 3* The context fixes against a REAL engine (not the scripted worker): Gyro-S at 64K on this PC, the agent bridge and the real Harness. 4* 5* gpu + heavy locks held first (python phaseA-tests\lock.py acquire gpu "023-agent: context e2e", then heavy); one engine; nothing of the 6* installed app is used or touched: its model files are read, its Harness is copied (STRATA_HARNESS_DIR), data goes to a scratch folder. 7* 8* node scripts/context-e2e.mjs (needs STRATA_HARNESS_DIR; see the E2E_* names below) 9* 10* It starts serve/server.py as ITS OWN child (foreground: this script waits for it and stops it by PID at the end or on a memory alarm), 11* runs four scenarios and prints one table. Exit code 0 when every expectation held. 12* 13* A a message that can never fit (about 60K tokens in a 64K window) is refused in seconds, before the engine reads it 14* B a chat of dense log messages grows past the window several times over: automatic compaction runs, nothing is refused 15* C /compact at a high fill: summarises, says the figures; and a second /compact is answered in plain words 16* D the needle: a fact from the first message is asked about at the end (reported, not asserted: it is the summary's quality) 17*/ 18import { spawn , spawnSync } from 'node:child_process' ; 19import { existsSync , mkdtempSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 20import { tmpdir } from 'node:os' ; 21import { join } from 'node:path' ; 22import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 23import { countTokens } from '../server/agent/context-fit.mjs' ; 24 25const REPO = join ( import . meta. dirname , '..' , '..' , '..' ); 26const env = ( name , fallback ) => process . env [ name ] ?? fallback ; 27const DOCS = env ( 'E2E_DOCS' , 'C:\\Users\\AI-Server\\Documents\\Strata' ); 28const ENGINE = env ( 'E2E_ENGINE' , 'C:\\Users\\AI-Server\\Desktop\\Strata\\GitHub\\wt-023-int\\build-cuda\\strata.exe' ); 29const PROFILE = env ( 'E2E_EXPERT_PROFILE' , 'C:\\Users\\AI-Server\\AppData\\Local\\Programs\\Strata\\control\\data\\expert-profile.bin' ); 30const PYTHON = env ( 'E2E_PYTHON' , 'python' ); 31const PORT = Number ( env ( 'E2E_PORT' , '18590' )); 32const MIN_FREE_GB = Number ( env ( 'E2E_MIN_FREE_GB' , '7' )); 33const GGUF = join ( DOCS , 'models' , 'gyro-Gyro-S' , 'Qwen3.8-Flash-Next-Gyro-S-TQ1_0.gguf' ); 34const CUDA_BIN = env ( 'E2E_CUDA_BIN' , 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v13.0\\bin' ); 35const WINDOW = 65536 ; 36/** A model server that is already running (the scripted worker, to try this script without a GPU): nothing is started or stopped. */ 37const EXISTING = process . env . E2E_BASE_URL ?? '' ; 38 39const sleep = ms => new Promise ( r => setTimeout ( r , ms )); 40const freeGb = () => { 41const out = spawnSync ( 'powershell' , [ '-NoProfile' , '-Command' , '[math]::Round((Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory/1MB,1)' ], { encoding :'utf8' }). stdout . trim (); 42return Number ( out ); 43}; 44const rows = []; 45const note = ( id , ok , detail ) => { rows . push ({ id, ok, detail}); console . log ( ` ${ ok ? 'PASS' : 'FAIL' } ${ id } ${ detail } ` ); }; 46 47if ( ! process . env . STRATA_HARNESS_DIR ) { console . error ( 'STRATA_HARNESS_DIR is not set' ); process . exit ( 2 ); } 48if ( ! EXISTING ) for ( const f of [ ENGINE , GGUF ]) if ( ! existsSync ( f )) { console . error ( `missing: ${ f } ` ); process . exit ( 2 ); } 49if ( ! EXISTING && freeGb () < 20 ) { console . error ( `only ${ freeGb ()} GB of RAM are free; wait for the other streams (lock.py status) and run again` ); process . exit ( 2 ); } 50 51const scratch = mkdtempSync ( join ( tmpdir (), 'strata-ctx-e2e-' )); 52const configFile = join ( scratch , 'server.json' ); 53const logFile = join ( scratch , 'server.log' ); 54const readyFile = join ( scratch , 'ready.json' ); 55writeFileSync ( configFile , JSON . stringify ({ 56exe :ENGINE , 57args :[ '--pack' , join ( DOCS , 'packs' , 'gyro-gyro-s' ), '--native' , GGUF , '--ple-gguf' , GGUF , ...( existsSync ( PROFILE ) ?[ '--expert-profile' , PROFILE ] :[]), 58'--prefill' , 'auto' , '--spec-min-p' , '0.5' , '--mtp' , join ( DOCS , 'mtp' , 'rt' ), '--max-context' , String ( WINDOW ), '--kv' , 'int8' , '--pool-workers' , '5' , 59'--expert-cache' , 'auto' , '--kv-resident' , '32768' , '--spec' , '4' , '--vram-reserve-mib' , '1536' ], 60cwd :REPO , tokenizer :join ( DOCS , 'packs' , 'gyro-gyro-s' , 'tokenizer' ), model_name :'qwen3.8-flash-next-gyro-gyro-s' , log :join ( scratch , 'engine.log' ), lib_dirs :[], port :PORT , gpu :0 , 61}, null , 2 )); 62 63let server = null , bridge = null , alarm = null ; 64const stopServer = () => { 65if ( server ?. pid ) spawnSync ( 'taskkill' , [ '/T' , '/F' , '/PID' , String ( server . pid )], { stdio :'ignore' }); 66server = null ; 67}; 68const finish = async ( code ) => { 69clearInterval ( alarm ); 70try { await bridge ?. close (); } catch { /* going away */ } 71stopServer (); 72await sleep ( 1500 ); 73console . log ( `\nscratch folder: ${ scratch } ` ); 74process . exit ( code ); 75}; 76process . on ( 'SIGINT' , () => finish ( 130 )); 77 78if ( ! EXISTING ) server = spawn ( PYTHON , [ 'serve/server.py' , '--engine' , 'strata' , '--config' , configFile , '--port' , String ( PORT ), '--no-api-key' , '--ready-file' , readyFile ], { 79cwd :REPO , env :{ ...process . env , PATH :` ${ CUDA_BIN } ; ${ process . env . PATH } ` , STRATA_DATA_DIR :join ( scratch , 'data' ) }, stdio :[ 'ignore' , 'pipe' , 'pipe' ], windowsHide :true , 80}); 81const sink = []; 82if ( server ) { server . stdout . on ( 'data' , d => sink . push ( String ( d ))); server . stderr . on ( 'data' , d => sink . push ( String ( d ))); server . on ( 'exit' , code => { sink . push ( `[server exited ${ code } ]` ); server = null ; }); } 83// Memory alarm: this run must never be what freezes the PC. Under the floor the engine is stopped (by PID) and the run ends. 84if ( ! EXISTING ) alarm = setInterval (() => { const free = freeGb (); if ( free < MIN_FREE_GB ) { console . error ( `ALARM: ${ free } GB of RAM free, stopping the engine` ); finish ( 3 ); } }, 3000 ); 85alarm ?. unref ?.(); 86 87const base = EXISTING || `http://127.0.0.1: ${ PORT } ` ; 88let ready = false ; 89for ( let i = 0 ; i < 120 && ! ready ; i ++ ) { 90if ( ! EXISTING && ! server ) break ; 91try { ready = ( await fetch ( ` ${ base } /health` , { signal :AbortSignal . timeout ( 2000 ) })). ok ; } catch { /* still loading */ } 92if ( ! ready ) await sleep ( 2000 ); 93} 94writeFileSync ( logFile , sink . join ( '' )); 95if ( ! ready ) { console . error ( 'the engine did not become ready:\n' + sink . join ( '' ). slice ( - 1500 )); await finish ( 4 ); } 96const model = ( await ( await fetch ( ` ${ base } /v1/models` )). json ()). data [ 0 ]. id ; 97console . log ( `engine ready: ${ model } , free RAM ${ freeGb ()} GB` ); 98 99// ---------------------------------------------------------------------------------------------------------------- the pieces 100 101const line = i => `2026-10-22 13: ${ String ( Math . floor ( i / 60 ) % 60 ). padStart ( 2 , '0' )} : ${ String ( i % 60 ). padStart ( 2 , '0' )} INFO svc- ${ i % 17 } req= ${( i * 2654435761 >>> 0 ). toString ( 16 )} status= ${ 200 + ( i % 7 ) * 3 } ms= ${( i * 37 ) % 900 } path=/api/v1/items/ ${( i * 7919 ) % 100000 } ` ; 102const logText = ( chars , from = 0 , needle = '' ) => { 103const out = []; 104let n = 0 , i = from ; 105while ( n < chars ) { const l = line ( i ++ ); out . push ( l ); n += l . length + 1 ; } 106if ( needle ) out . splice ( Math . floor ( out . length / 2 ), 0 , needle ); 107return out . join ( '\n' ); 108}; 109const count = text => countTokens ({ baseUrl :` ${ base } /v1` , model, text}); 110 111const turnsOf = async ( message ) => { 112const frames = []; 113const t0 = Date . now (); 114await bridge . runTurn ({ chatId :'e2e' , message, workspacePath :scratch , mode :'bypass' , thinking :'Off' , onFrame :f => frames . push ( f ) }); 115return { frames, seconds :( Date . now () - t0 ) / 1000 , last :frames . at ( - 1 ) }; 116}; 117const textOf = frames => frames . filter ( f => f . type === 'delta' && ! f . noteReason ). map ( f => f . text ). join ( '' ). trim (); 118const noteOf = frames => frames . find ( f => f . type === 'delta' && f . noteReason === 'compacted' )?. text ?. trim () ?? null ; 119 120bridge = createAgentBridge ({ 121dataDir :join ( scratch , 'agent' ), env :process . env , startTimeoutMs :120_000 , log :l => console . log ( ' bridge:' , l ), 122localInstances :() => [{ id :'e2e' , label :'Gyro-S' , baseUrl :` ${ base } /v1` , modelId :model , context :WINDOW , slots :1 , key :'' }], connections :() => [], 123}); 124mkdirSync ( join ( scratch , 'agent' ), { recursive :true }); 125 126// ---------------------------------------------------------------------------------------------------------------- A 127{ 128// Grow a log until the engine itself counts about 60K tokens. 129let chars = 150_000 , tokens = await count ( logText ( chars )); 130while ( tokens && tokens < 58_000 && chars < 400_000 ) { chars = Math . round ( chars * ( 60_000 / tokens )); tokens = await count ( logText ( chars )); } 131const t = await turnsOf ( logText ( chars ) + '\n\nWhat is the most common status?' ); 132const refused = t . last . type === 'error' && t . last . code === 'CONTEXT_MESSAGE_TOO_LARGE' ; 133note ( 'A too large is refused before it is read' , refused && t . seconds < 15 , ` ${ chars } characters = ${ tokens } tokens by the engine; answered in ${ t . seconds . toFixed ( 1 )} s; frame ${ t . last . type } / ${ t . last . code ?? '-' } : ${ String ( t . last . message ?? '' ). slice ( 0 , 110 )} ` ); 134} 135 136// ---------------------------------------------------------------------------------------------------------------- B 137const needle = 'NOTE FROM OPS: the vault code is COBALT-27. Do not lose it.' ; 138const timeline = []; 139let notes = 0 , refusedAny = false , peak = 0 ; 140{ 141for ( let k = 0 ; k < 5 ; k ++ ) { 142const body = logText ( 34_000 , k * 1000 , k === 0 ?needle :'' ); 143const t = await turnsOf ( ` ${ body } \n\nReply with the single word OK.` ); 144const note = noteOf ( t . frames ); 145if ( note ) notes ++ ; 146if ( t . last . type === 'error' ) refusedAny = true ; 147const used = t . last . contextTokens ?? 0 ; 148peak = Math . max ( peak , ...t . frames . filter ( f => f . type === 'context' ). map ( f => f . contextTokens ), used ); 149timeline . push ( `msg ${ k + 1 } : ${ t . last . type } ${ t . last . code ? '/' + t . last . code : '' } ${ t . seconds . toFixed ( 0 )} s context ${ used } ( ${ Math . round ( 100 * used / WINDOW )} %) ${ note ? ' [' + note . slice ( 0 , 80 ) + ']' : '' } ` ); 150console . log ( ' ' + timeline . at ( - 1 )); 151if ( t . last . type === 'error' ) break ; 152} 153note ( 'B five dense messages (about 14K tokens each) in a 64K window: no error' , ! refusedAny , `peak context ${ peak } of ${ WINDOW } ; ${ notes } compaction note(s) in the chat` ); 154note ( 'B compaction ran and was announced' , notes >= 1 , timeline . join ( ' | ' )); 155} 156 157// ---------------------------------------------------------------------------------------------------------------- C 158{ 159const run = async ( line ) => { const frames = []; await bridge . compactCommand ({ chatId :'e2e' , message :line , workspacePath :scratch , mode :'bypass' , choice :null , onFrame :f => frames . push ( f ) }); return frames ; }; 160await turnsOf ( ` ${ logText ( 30_000 , 90_000 )} \n\nReply with the single word OK.` ); 161const t0 = Date . now (); 162const first = await run ( '/compact' ); 163const said = first . find ( f => f . type === 'delta' ); 164const ok = said ?. key === 'Older messages were summarized to make room. The chat was {0}% full and is now {1}% full.' && said . values [ 0 ] > said . values [ 1 ]; 165note ( 'C /compact summarises and states the figures' , ok , ` ${(( Date . now () - t0 ) / 1000 ). toFixed ( 0 )} s: ${ said ?. text } ` ); 166const second = await run ( '/compact' ); 167const again = second . find ( f => f . type === 'delta' )?. text ?? '' ; 168note ( 'C a second /compact is answered in plain words' , / summarized | nothing | could not / i . test ( again ) && ! /\{ | 400 | invalid_request / . test ( again ), again ); 169} 170 171// ---------------------------------------------------------------------------------------------------------------- D 172{ 173const t = await turnsOf ( 'What was the vault code in the very first message? Answer with the code only.' ); 174const answer = textOf ( t . frames ); 175console . log ( `D needle after compaction: " ${ answer . slice ( 0 , 80 )} " ( ${ t . last . type } )` ); 176rows . push ({ id :'D the needle' , ok :/ COBALT-27 / . test ( answer ), detail :`reported only: answer " ${ answer . slice ( 0 , 80 )} "` }); 177} 178 179console . log ( '\n' + rows . map ( r => ` ${ r . ok ? 'PASS' : r . id . startsWith ( 'D' ) ? 'INFO' : 'FAIL' } ${ r . id } ${ r . id . startsWith ( 'D' ) ? ` ${ r . detail } ` : '' } ` ). join ( '\n' )); 180const failed = rows . filter ( r => ! r . ok && ! r . id . startsWith ( 'D' )). length ; 181writeFileSync ( join ( scratch , 'result.json' ), JSON . stringify ( rows , null , 2 )); 182await finish ( failed ?1 :0 );