okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { linkSync , mkdirSync , mkdtempSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 4import { createHash } from 'node:crypto' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { runInNewContext } from 'node:vm' ; 8import { HARNESS_BUDGET_PATCH , patchStagedHarnessBudget } from '../../desktop/scripts/harness-budget.mjs' ; 9 10// Exact MIT-licensed pi-ai 1.1.0 module; arithmetic fixture, not native tokenization. 11const original = readFileSync ( new URL ( './fixtures/pi-ai-1.1.0-simple-options.js.fixture' , import . meta. url ), 'utf8' ). replaceAll ( '\r\n' , '\n' ); 12const sha256 = bytes => createHash ( 'sha256' ). update ( bytes ). digest ( 'hex' ); 13function staged ( t , { source= original , manifest= { name :'@earendil-works/pi-ai' , version :'1.1.0' } } = {}) { 14const root = mkdtempSync ( join ( tmpdir (), 'strata-harness-budget-' )); 15t . after (() => rmSync ( root , { recursive :true , force :true })); 16const file = join ( root , HARNESS_BUDGET_PATCH . file ), packageFile = join ( root , 'node_modules' , '@earendil-works' , 'pi-ai' , 'package.json' ); 17mkdirSync ( join ( file , '..' ), { recursive :true }); 18writeFileSync ( file , source ); writeFileSync ( packageFile , JSON . stringify ( manifest )); 19return { root, file, packageFile}; 20} 21function patched ( t ) { 22const fixture = staged ( t ), provenance = patchStagedHarnessBudget ( fixture . root ); 23return { ...fixture , provenance, clamp :clampOf ( readFileSync ( fixture . file , 'utf8' )) }; 24} 25function clampOf ( source ) { 26const code = source . replace ( / ^import . * ;\n / gm , '' ). replace ( / ^export / gm , '' ); 27return runInNewContext ( ` ${ code } \nclampMaxTokensToContext;` , { 28estimateContextTokens :context => ({ tokens :context . tokens }), 29clampThinkingLevel :( _model , level ) => level , 30}); 31} 32 33test ( 'staged pi-ai leaves output room at 8K Context' , t => { 34assert . equal ( clampOf ( original )({ contextWindow :8192 }, { tokens :6545 }, 4096 ), 1 , 'The Original Regression' ); 35const { clamp} = patched ( t ); 36assert . equal ( clamp ({ contextWindow :8192 }, { tokens :6545 }, 4096 ), 1135 ); 37}); 38 39test ( 'small-window safety leaves answer room after a tool result' , t => { 40const { clamp} = patched ( t ); 41assert . equal ( clamp ({ contextWindow :8192 }, { tokens :7750 }, 4096 ), 221 ); 42assert . equal ( clamp ({ contextWindow :8192 }, { tokens :8000 }, 4096 ), 96 ); 43assert . equal ( clamp ({ contextWindow :8192 }, { tokens :8000 }, 32 ), 32 ); 44}); 45 46test ( 'the staged correction retains caller caps and the no-room floor' , t => { 47const { clamp} = patched ( t ); 48for ( const cap of [ 1 , 128 ]) assert . equal ( clamp ({ contextWindow :8192 }, { tokens :6545 }, cap ), cap ); 49for ( const tokens of [ 8191 , 8192 , 9000 ]) assert . equal ( clamp ({ contextWindow :8192 }, { tokens}, 4096 ), 1 ); 50}); 51 52test ( '32K scales safety to 2048; 64K and larger keep the original calculation' , t => { 53const { clamp} = patched ( t ), old = clampOf ( original ); 54assert . equal ( clamp ({ contextWindow :32768 }, { tokens :6545 }, 32768 ), 24175 ); 55assert . equal ( clamp ({ contextWindow :65536 }, { tokens :6545 }, 65536 ), 54895 ); 56assert . equal ( clamp ({ contextWindow :131072 }, { tokens :6545 }, 131072 ), 120431 ); 57for ( const window of [ 65536 , 131072 , 262144 ]) { 58for ( const tokens of [ 0 , 6545 , window - 4096 , window ]) { 59for ( const cap of [ 1 , 128 , 32768 , window ]) 60assert . equal ( clamp ({ contextWindow :window }, { tokens}, cap ), old ({ contextWindow :window }, { tokens}, cap )); 61} 62} 63}); 64 65test ( 'unknown and nonpositive context paths retain their previous behavior' , t => { 66const { clamp} = patched ( t ), old = clampOf ( original ); 67for ( const window of [ 0 , - 1 , null , undefined , NaN ]) { 68for ( const cap of [ 0 , 1 , 128 , 4096 ]) 69assert . equal ( clamp ({ contextWindow :window }, { tokens :6545 }, cap ), old ({ contextWindow :window }, { tokens :6545 }, cap )); 70} 71}); 72 73test ( 'patch provenance records its identity, exact dependency and before/after hashes' , t => { 74const { file, packageFile, provenance} = patched ( t ); 75assert . equal ( sha256 ( original ), HARNESS_BUDGET_PATCH . beforeSha256 ); 76assert . deepEqual ( provenance , { id :'strata-pi-ai-context-safety-v2' , package :'@earendil-works/pi-ai' , version :'1.1.0' , 77file :HARNESS_BUDGET_PATCH . file , beforeSha256 :sha256 ( original ), afterSha256 :sha256 ( readFileSync ( file )) }); 78assert . notEqual ( provenance . beforeSha256 , provenance . afterSha256 ); 79assert . deepEqual ( JSON . parse ( readFileSync ( packageFile , 'utf8' )), { name :'@earendil-works/pi-ai' , version :'1.1.0' }); 80const changed = original . replace ( 'const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;' , 81`const remaining = model.contextWindow - estimateContextTokens(context).tokens; 82const safety = model.contextWindow >= 65536 ? CONTEXT_SAFETY_TOKENS 83: Math.min(CONTEXT_SAFETY_TOKENS, Math.floor(model.contextWindow / 16), Math.max(0, Math.floor(remaining / 2))); 84const available = remaining - safety;` ); 85assert . equal ( readFileSync ( file , 'utf8' ), changed , 'Only The Known Clamp Changes' ); 86}); 87 88test ( 'malformed or unexpected dependencies fail before writing' , t => { 89const cases = [ 90{ manifest :{ name :'@earendil-works/pi-ai' , version :'1.2.0' } }, 91{ manifest :{ name :'another-package' , version :'1.1.0' } }, 92{ manifest :null }, 93{ source :original + '\n' }, 94{ source :original . replace ( 'CONTEXT_SAFETY_TOKENS = 4096' , 'CONTEXT_SAFETY_TOKENS = 1024' ) }, 95]; 96for ( const value of cases ) { 97const f = staged ( t , value ), before = readFileSync ( f . file ), manifest = readFileSync ( f . packageFile ); 98assert . throws (() => patchStagedHarnessBudget ( f . root ), / Harness output-budget patch: / ); 99assert . deepEqual ( readFileSync ( f . file ), before ); assert . deepEqual ( readFileSync ( f . packageFile ), manifest ); 100} 101const f = staged ( t ); writeFileSync ( f . packageFile , '{bad json' ); 102assert . throws (() => patchStagedHarnessBudget ( f . root ), / cannot read a standalone staged pi-ai dependency / ); 103assert . equal ( readFileSync ( f . file , 'utf8' ), original ); assert . equal ( readFileSync ( f . packageFile , 'utf8' ), '{bad json' ); 104}); 105 106test ( 'a repeated patch rejects already-changed bytes without writing again' , t => { 107const f = patched ( t ), before = readFileSync ( f . file ); 108assert . throws (() => patchStagedHarnessBudget ( f . root ), / unexpected simple-options.js SHA-256 / ); 109assert . deepEqual ( readFileSync ( f . file ), before ); 110}); 111 112test ( 'staging refuses a module hardlinked to another copy' , t => { 113const f = staged ( t ), other = join ( f . root , 'source-module.js' ); linkSync ( f . file , other ); 114assert . throws (() => patchStagedHarnessBudget ( f . root ), / expected an unlinked staged module / ); 115assert . equal ( readFileSync ( other , 'utf8' ), original ); assert . equal ( readFileSync ( f . file , 'utf8' ), original ); 116}); 117 118test ( 'patching a disposable copy leaves the installed original hash unchanged' , { 119skip :! process . env . STRATA_TEST_PI_AI_DIR && 'STRATA_TEST_PI_AI_DIR is not set (the Harness AI package folder)' , 120}, t => { 121const installed = join ( process . env . STRATA_TEST_PI_AI_DIR , 'dist' , 'api' , 'simple-options.js' ); 122const before = sha256 ( readFileSync ( installed )); 123assert . equal ( before , HARNESS_BUDGET_PATCH . beforeSha256 ); 124const f = staged ( t , { source :readFileSync ( installed ) }); patchStagedHarnessBudget ( f . root ); 125assert . equal ( sha256 ( readFileSync ( installed )), before ); 126});