okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
2.9 KiB52 linesraw
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({
7  id: 'strata-pi-ai-context-safety-v2',
8  package: '@earendil-works/pi-ai',
9  version: '1.1.0',
10  file: 'node_modules/@earendil-works/pi-ai/dist/api/simple-options.js',
11  beforeSha256: '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;
19    const 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)));
21    const 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) {
29  const fail = message => { throw new Error(`Harness output-budget patch: ${message}`); };
30  const root = realpathSync(resolve(stagedRoot));
31  const packageDir = join(root, 'node_modules', '@earendil-works', 'pi-ai');
32  const target = join(root, HARNESS_BUDGET_PATCH.file);
33  let manifest, before;
34  try {
35    manifest = JSON.parse(readFileSync(join(packageDir, 'package.json'), 'utf8'));
36    // Flattening must have removed links; never write through a shared source file.
37    if (realpathSync(target) !== target || !lstatSync(target).isFile() || lstatSync(target).nlink !== 1)
38      fail('expected an unlinked staged module');
39    before = readFileSync(target);
40  } catch (error) {
41    fail(`cannot read a standalone staged pi-ai dependency (${error.message})`);
42  }
43  if (manifest?.name !== HARNESS_BUDGET_PATCH.package || manifest?.version !== HARNESS_BUDGET_PATCH.version)
44    fail('expected @earendil-works/pi-ai 1.1.0; review the patch before staging a different dependency');
45  if (sha256(before) !== HARNESS_BUDGET_PATCH.beforeSha256)
46    fail('unexpected simple-options.js SHA-256; review the dependency source before staging');
47  const source = before.toString('utf8');
48  if (source.split(oldClamp).length !== 2) fail('expected exactly one known context clamp');
49  const after = Buffer.from(source.replace(oldClamp, newClamp));
50  writeFileSync(target, after);
51  return { ...HARNESS_BUDGET_PATCH, afterSha256: sha256(after) };
52}