Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(schema-pack): a pack bundled into a compiled binary loads again; second-gate fixes

garrytanf048ef4e7e95
diff --git a/src/core/schema-pack/loader.ts b/src/core/schema-pack/loader.ts
index ec914ae906..de1c4abf9b 100644
--- a/src/core/schema-pack/loader.ts
+++ b/src/core/schema-pack/loader.ts
@@ -16,7 +16,7 @@
// when available. Empty file → INVALID_SHAPE. Unknown extension → falls
// through to JSON.parse attempt.
-import { closeSync, fstatSync, openSync, readSync } from 'node:fs';
+import { closeSync, fstatSync, openSync, readFileSync, readSync } from 'node:fs';
import { extname } from 'node:path';
import { parseSchemaPackManifest, type SchemaPackManifest } from './manifest-v1.ts';
@@ -69,15 +69,19 @@ export function loadPackFromFile(path: string, opts: { maxBytes?: number } = {})
let fd = -1;
let content: string;
try {
- fd = openSync(path, 'r');
- const size = fstatSync(fd).size;
- if (size > maxBytes) {
- throw new SchemaPackLoaderError(
- 'PACK_TOO_LARGE',
- `pack file ${path} is ${formatPackBytes(size)}, above the ${formatPackBytes(maxBytes)} bound; it was not read. A pack this large is usually a quoted scalar (a link-type regex) whose backslashes an older release doubled on every mutation: open the file in an editor, restore the affected lines by hand, and re-run. Runbook: ${OVERSIZED_PACK_RUNBOOK}.`,
- path,
- );
+ try {
+ fd = openSync(path, 'r');
+ } catch (e) {
+ // A pack bundled into a compiled binary lives on Bun's virtual
+ // filesystem (`/$bunfs/...`), which serves readFileSync but has no
+ // descriptors; the bound is checked on the bytes read instead.
+ if (!path.startsWith('/$bunfs/')) throw e;
+ const bundled = readFileSync(path);
+ if (bundled.byteLength > maxBytes) throw tooLarge(path, bundled.byteLength, maxBytes);
+ return loadPackFromString(bundled.toString('utf-8'), path);
}
+ const size = fstatSync(fd).size;
+ if (size > maxBytes) throw tooLarge(path, size, maxBytes);
const buf = Buffer.allocUnsafe(size);
let read = 0;
while (read < size) {
@@ -95,6 +99,14 @@ export function loadPackFromFile(path: string, opts: { maxBytes?: number } = {})
return loadPackFromString(content, path);
}
+function tooLarge(path: string, size: number, maxBytes: number): SchemaPackLoaderError {
+ return new SchemaPackLoaderError(
+ 'PACK_TOO_LARGE',
+ `pack file ${path} is ${formatPackBytes(size)}, above the ${formatPackBytes(maxBytes)} bound; it was not read. A pack this large is usually a quoted scalar (a link-type regex) whose backslashes an older release doubled on every mutation: open the file in an editor, restore the affected lines by hand, and re-run. Runbook: ${OVERSIZED_PACK_RUNBOOK}.`,
+ path,
+ );
+}
+
/**
* Parse a manifest from a raw string. Extension-driven; `.json` uses
* JSON.parse, anything else uses the YAML mini-parser. Test seam.
diff --git a/test/backlinks-memory.slow.test.ts b/test/backlinks-memory.slow.test.ts
index 746410a45b..b859b6f0fe 100644
--- a/test/backlinks-memory.slow.test.ts
+++ b/test/backlinks-memory.slow.test.ts
@@ -30,12 +30,15 @@ function measure(mib: number): { gaps: number; delta_peak_mb: number; heap_retai
}
describe('findBacklinkGaps RSS slope', () => {
- test('up to 384 MiB of non-target markdown the RSS delta stays under 150 MB, the heap keeps under 48 MB, and the delta does not follow the corpus', () => {
+ test('up to 384 MiB of non-target markdown the RSS delta stays under 256 MB, the heap keeps under 48 MB, and the delta does not follow the corpus', () => {
const runs = SIZES_MIB.map(mib => ({ mib, ...measure(mib) }));
+ console.error(`[backlinks-rss] ${runs.map(r => `${r.mib} MiB: rss +${r.delta_peak_mb} MB, heap ${r.heap_retained_mb} MB`).join('; ')}`);
for (const r of runs) {
expect(r.gaps).toBe(r.mib);
// Before the fix the RSS delta was ~3x the corpus (48 MiB → +193 MB, 384 MiB → +1470 MB) and the heap kept every body.
- expect(r.delta_peak_mb).toBeLessThan(150);
+ // After: +88 MB at 384 MiB on a 4-core builder, +168 MB on a 16-vCPU CI VM (JSC sizes its nursery by the machine), so
+ // the absolute bound leaves room for that while staying 5x under the pre-fix number; the slope check below is the property.
+ expect(r.delta_peak_mb).toBeLessThan(256);
expect(r.heap_retained_mb).toBeLessThan(48);
}
const largest = runs.at(-1)!;
diff --git a/test/helpers/recreate-root.ts b/test/helpers/recreate-root.ts
index a26ca1f2de..399803b6cc 100644
--- a/test/helpers/recreate-root.ts
+++ b/test/helpers/recreate-root.ts
@@ -8,11 +8,18 @@ export function recreateRoot(root: string, opts: { keepStamp: boolean }): void {
const stamp = readFileSync(join(root, PHYSICAL_ROOT_MARKER));
const before = statSync(root, { bigint: true });
rmSync(root, { recursive: true });
- mkdirSync(join(root, '..', `decoy-${randomUUID().slice(0, 8)}`));
- mkdirSync(root);
+ // ext4 and XFS hand a freed inode straight back to the next mkdir, so take
+ // decoys until the recreated root gets a different one (the tests compare
+ // the reservation's recorded inode with the live directory).
+ for (let attempt = 0; attempt < 64; attempt++) {
+ mkdirSync(join(root, '..', `decoy-${randomUUID().slice(0, 8)}`));
+ mkdirSync(root);
+ if (statSync(root, { bigint: true }).ino !== before.ino) break;
+ rmSync(root, { recursive: true });
+ }
writeFileSync(join(root, 'note.md'), 'Canonical example');
if (opts.keepStamp) writeFileSync(join(root, PHYSICAL_ROOT_MARKER), stamp, { mode: 0o600 });
const after = statSync(root, { bigint: true });
- if (after.ino === before.ino && after.birthtimeNs === before.birthtimeNs) throw new Error('fixture: the recreated directory kept its identity');
+ if (after.ino === before.ino) throw new Error('fixture: the recreated directory kept its inode after 64 decoys');
}
diff --git a/test/trust-write-gate-drift.test.ts b/test/trust-write-gate-drift.test.ts
index ccf5401fec..ac28c1aa03 100644
--- a/test/trust-write-gate-drift.test.ts
+++ b/test/trust-write-gate-drift.test.ts
@@ -19,7 +19,7 @@ const EXEMPT: Record<string, string> = {
'src/commands/extract-timeline-db.ts': 're-derives timeline rows from stored page text the page gate already assessed',
'src/commands/migrate-engine.ts': 'copies rows verbatim between engines; tiers and receipts travel with them',
'src/commands/sync/holds.ts': 'held-file bookkeeping; writes no content',
- 'src/commands/sync/renames.ts': 'renames import through importFile, which passes the owner gate input (import-file.ts)',
+ 'src/commands/sync/rename-collision.ts': 'moves a page between slugs (updateSlug, tombstone re-key, source_path repair) and writes no content; the renamed body still imports through importFile, which passes the owner gate input (import-file.ts)',
'src/core/calibration/undo-wave.ts': 'restores prior take lifecycle fields; no new text',
'src/core/company-brain/profile.ts': 'company profile publication of an owner-approved company source (operator_curated, never gated)',
'src/core/cycle/dream-provenance.ts': 'stamps provenance frontmatter; no new text',