okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** Strata's output-budget correction for the staged pi-ai copy. */ 2import { createHash } from 'node:crypto' ; 3import { lstatSync , readFileSync , realpathSync , writeFileSync } from 'node:fs' ; 4import { join , resolve } from 'node:path' ; 5 6export const HARNESS_BUDGET_PATCH = Object . freeze ({ 7id :'strata-pi-ai-context-safety-v2' , 8package :'@earendil-works/pi-ai' , 9version :'1.1.0' , 10file :'node_modules/@earendil-works/pi-ai/dist/api/simple-options.js' , 11beforeSha256 :'9b3cc0ae6daa86fdc050bc0cb0f3f986401e64ccddbde6757183c9f8ad411be2' , 12}); 13 14const sha256 = bytes => createHash ( 'sha256' ). update ( bytes ). digest ( 'hex' ); 15const oldClamp = 'const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;' ; 16// Small windows also need room after tool results; a fixed margin must not 17// consume all remaining estimated space. Keep the old 64K+ calculation. 18const newClamp = `const remaining = model.contextWindow - estimateContextTokens(context).tokens; 19const safety = model.contextWindow >= 65536 ? CONTEXT_SAFETY_TOKENS 20: Math.min(CONTEXT_SAFETY_TOKENS, Math.floor(model.contextWindow / 16), Math.max(0, Math.floor(remaining / 2))); 21const available = remaining - safety;` ; 22 23/** 24* Patch only a flattened, disposable staged Harness. Returns the BUNDLE.json 25* provenance entry; the caller records it when writing the bundle manifest. 26* A repeat application or any unknown package/source fails before writing. 27*/ 28export function patchStagedHarnessBudget ( stagedRoot ) { 29const fail = message => { throw new Error ( `Harness output-budget patch: ${ message } ` ); }; 30const root = realpathSync ( resolve ( stagedRoot )); 31const packageDir = join ( root , 'node_modules' , '@earendil-works' , 'pi-ai' ); 32const target = join ( root , HARNESS_BUDGET_PATCH . file ); 33let manifest , before ; 34try { 35manifest = JSON . parse ( readFileSync ( join ( packageDir , 'package.json' ), 'utf8' )); 36// Flattening must have removed links; never write through a shared source file. 37if ( realpathSync ( target ) !== target || ! lstatSync ( target ). isFile () || lstatSync ( target ). nlink !== 1 ) 38fail ( 'expected an unlinked staged module' ); 39before = readFileSync ( target ); 40} catch ( error ) { 41fail ( `cannot read a standalone staged pi-ai dependency ( ${ error . message } )` ); 42} 43if ( manifest ?. name !== HARNESS_BUDGET_PATCH . package || manifest ?. version !== HARNESS_BUDGET_PATCH . version ) 44fail ( 'expected @earendil-works/pi-ai 1.1.0; review the patch before staging a different dependency' ); 45if ( sha256 ( before ) !== HARNESS_BUDGET_PATCH . beforeSha256 ) 46fail ( 'unexpected simple-options.js SHA-256; review the dependency source before staging' ); 47const source = before . toString ( 'utf8' ); 48if ( source . split ( oldClamp ). length !== 2 ) fail ( 'expected exactly one known context clamp' ); 49const after = Buffer . from ( source . replace ( oldClamp , newClamp )); 50writeFileSync ( target , after ); 51return { ...HARNESS_BUDGET_PATCH , afterSha256 :sha256 ( after ) }; 52}