okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
52ba0b781
main
1// The page side of the context fixes (2026-10-22 hands-on test): what the error card offers, what the context meter reads while a turn runs, 2// and that every sentence the host or the page says about the context is in the catalog in all nine languages. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { readFileSync } from 'node:fs' ; 6import { build } from 'esbuild' ; 7import { fileURLToPath } from 'node:url' ; 8import { FIT_TEXT } from '../server/agent/context-fit.mjs' ; 9 10const here = rel => fileURLToPath ( new URL ( rel , import . meta. url )); 11const read = rel => readFileSync ( here ( rel ), 'utf8' ). replace ( / \r\n / g , '\n' ); 12globalThis . window ??= globalThis ; globalThis . localStorage ??= { getItem :() => null , setItem :() => {} }; 13globalThis . navigator ??= { language :'en' }; 14 15async function load ( file , stubs = {}) { 16const result = await build ({ 17entryPoints :[ here ( `../src/lib/ ${ file } ` )], bundle :true , format :'esm' , platform :'node' , write :false , logLevel :'silent' , loader :{ '.json' :'json' }, 18plugins :[{ name :'stubs' , setup ( b ) { b . onResolve ({ filter :/ ^\.\/ ( store ) $ / }, () => ({ path :'store' , namespace :'stub' })); b . onLoad ({ filter :/ . */ , namespace :'stub' }, () => ({ contents :stubs . store , loader :'js' })); } }], 19}); 20return import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 21} 22 23const calls = []; 24globalThis . __calls = calls ; 25const recovery = await load ( 'context-recovery.ts' , { 26store :`export const sendOversized = (mode) => globalThis.__calls.push(['oversized', mode]); 27export const summarizeAndContinue = () => globalThis.__calls.push(['summarize']); 28export const startNewChat = () => globalThis.__calls.push(['new-chat']);` , 29}); 30const { chatMetrics} = await load ( 'chat-metrics.ts' ); 31 32test ( 'a conversation that no longer fits offers "Summarize and continue" and "Start a new chat", never "try again"' , () => { 33const r = recovery . contextRecovery ( 'CONTEXT_WINDOW_EXCEEDED' ); 34assert . equal ( r . action , 'Summarize and continue' ); 35assert . equal ( r . secondary . label , 'Start a new chat' ); 36assert . match ( r . next , / Summarize it now and continue, or start a new chat\. / ); 37assert . doesNotMatch ( r . next , / again / i ); 38calls . length = 0 ; 39r . onRetry (); r . secondary . onClick (); 40assert . deepEqual ( calls , [[ 'summarize' ], [ 'new-chat' ]]); 41}); 42 43test ( 'a message that is too large offers the two ways that fit: a file the assistant reads in parts, or a shortened preview' , () => { 44const r = recovery . contextRecovery ( 'CONTEXT_MESSAGE_TOO_LARGE' ); 45assert . equal ( r . action , 'Attach as a file' ); 46assert . equal ( r . secondary . label , 'Send a shortened preview' ); 47calls . length = 0 ; 48r . onRetry (); r . secondary . onClick (); 49assert . deepEqual ( calls , [[ 'oversized' , 'file' ], [ 'oversized' , 'preview' ]]); 50}); 51 52test ( 'any other error keeps the card as it was: one sentence, no buttons of ours' , () => { 53for ( const code of [ undefined , 'UNKNOWN' , 'AGENT_STOPPED' ]) { 54const r = recovery . contextRecovery ( code ); 55assert . equal ( r . next , 'Try sending the message again.' ); 56assert . equal ( r . onRetry , undefined ); 57assert . equal ( r . secondary , undefined ); 58} 59}); 60 61test ( 'the chat shows the error card with Details and the recovery buttons, and the card can hold a second button' , () => { 62const chat = read ( '../src/screens/Chat.tsx' ); 63assert . match ( chat , / <ErrorNotice message=\{m\.error\} detail=\{m\.errorDetail\} \{\.\.\.contextRecovery\(m\.errorCode\)\}\/> / ); 64const ui = read ( '../src/components/ui.tsx' ); 65assert . match ( ui , / secondary\?: \{ label: string; onClick: \(\) => void \} / ); 66assert . match ( ui , / <Btn size="sm" variant="ghost" onClick=\{secondary\.onClick\}> / ); 67}); 68 69test ( 'the store keeps the code and the engine\'s text of an error, shows a sentence the host wrote in the person\'s language, and follows the context live' , () => { 70const store = read ( '../src/lib/store.ts' ); 71assert . match ( store , / typeof frame\.key === 'string' \? tr\(frame\.key, / ); 72assert . match ( store , / errorCode: frame\.code / ); 73assert . match ( store , / errorDetail: frame\.details / ); 74assert . match ( store , / type === 'context' / ); 75const summarize = store . slice ( store . indexOf ( 'export async function summarizeAndContinue' ), store . indexOf ( 'export async function sendOversized' )); 76assert . ok ( summarize . indexOf ( "sendMessage('/compact')" ) > 0 && summarize . indexOf ( "sendMessage('Please answer my last message now.')" ) > summarize . indexOf ( "sendMessage('/compact')" ), 'summarise first, then ask for the answer' ); 77assert . doesNotMatch ( summarize , / failed.prompt | dropFailedExchange / , 'the refused question is not sent a second time (it is already in the conversation, and a large one twice would not fit)' ); 78const oversized = store . slice ( store . indexOf ( 'export async function sendOversized' ), store . indexOf ( 'export function startNewChat' )); 79assert . match ( oversized , / '\/api\/chat\/oversize' / ); 80assert . ok ( oversized . indexOf ( 'dropFailedExchange' ) > oversized . indexOf ( "'/api/chat/oversize'" ), 'the failed message stays until the shorter one is ready' ); 81}); 82 83const reply = ( extra ) => ({ id :'r' , role :'assistant' , content :'' , at :'2026-10-22T12:00:00Z' , ...extra }); 84const session = messages => ({ id :'s' , title :'t' , createdAt :'' , updatedAt :'' , messages, tokensIn :0 , tokensOut :0 , statsAvailable :true }); 85 86test ( 'the context meter follows the turn that is running: a tool step does not read as 1%' , () => { 87// The 2026-10-22 chat: an earlier reply left the meter at 1% (about 600 tokens), then a turn with tools ran on a 65,536 window. 88const earlier = reply ({ id :'a' , inputTokens :590 , tokens :20 , contextUsed :610 , contextMax :65536 }); 89const running = reply ({ id :'b' , streaming :true , contextMax :65536 }); 90const before = chatMetrics ( session ([ earlier , running ]), null , 65536 ); 91assert . equal ( Math . round ( before . pct ), 1 , 'with nothing reported yet for the turn, the earlier reply is all there is' ); 92const stepped = reply ({ id :'b' , streaming :true , contextMax :65536 , contextUsed :43000 }); 93const during = chatMetrics ( session ([ earlier , stepped ]), null , 65536 ); 94assert . equal ( during . used , 43000 ); 95assert . equal ( Math . round ( during . pct ), 66 ); 96const monitor = { phase :'idle' , context :{ sessionId :'s' , usedTokens :600 , maxTokens :65536 } }; 97assert . equal ( chatMetrics ( session ([ earlier , stepped ]), monitor , 65536 ). used , 43000 , 'the engine\'s figure for the request in flight does not override the turn\'s own' ); 98const done = reply ({ id :'b' , inputTokens :43000 , tokens :90 , contextUsed :43090 , contextMax :65536 }); 99assert . equal ( chatMetrics ( session ([ earlier , done ]), null , 65536 ). used , 43090 , 'when the turn has ended its final figure stands' ); 100}); 101 102test ( 'every sentence about the context is in the catalog in all nine languages, with the same slots' , () => { 103const langs = [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]; 104const catalogs = Object . fromEntries ( langs . map ( l => [ l , JSON . parse ( read ( `../src/locales/ ${ l } .json` ))])); 105const idOf = text => Object . entries ( catalogs . en ). find (([, v ]) => v === text )?.[ 0 ]; 106const sentences = [ ...Object . values ( FIT_TEXT ), 'Attach as a file' , 'Send a shortened preview' , 'Summarize and continue' , 'Start a new chat' , 107'The message could not be prepared. Try a shorter message.' , 'The last Strata session is still closing down.' , 'Wait a moment and try again. If it keeps happening, restart the computer.' ]; 108const slots = t => ( t . match ( / \{\d + \} / g ) ?? []). sort (). join (); 109for ( const text of sentences ) { 110const id = idOf ( text ); 111assert . ok ( id , `" ${ text . slice ( 0 , 60 )} " is in the English catalog` ); 112for ( const l of langs ) { 113const t = catalogs [ l ][ id ]; 114assert . ok ( typeof t === 'string' && t . trim (), ` ${ id } in ${ l } ` ); 115assert . equal ( slots ( t ), slots ( text ), ` ${ id } in ${ l } has the same slots` ); 116if ( l !== 'en' && text . length > 25 ) assert . notEqual ( t , text , ` ${ id } is translated in ${ l } ` ); 117} 118} 119});