Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

backlinks RSS test: pin BUN_JSC_forceRAMSize in the child so the 256 MB bound measures retention, not machine RAM; rss_peak_mb → rss_after_scan_mb (#6494)

garrytan008f934463f2
diff --git a/test/backlinks-memory.slow.test.ts b/test/backlinks-memory.slow.test.ts
index 7ef91427b7..dc66419a31 100644
--- a/test/backlinks-memory.slow.test.ts
+++ b/test/backlinks-memory.slow.test.ts
@@ -10,6 +10,18 @@
* child process so the parent's heap and other tests cannot skew it.
* Regression: a map that retains bodies again. No production seam: the
* child reports process.memoryUsage().rss.
+ *
+ * The RSS delta is mostly garbage JSC has not collected yet, and JSC sizes
+ * that allowance from the machine's RAM (#6494): the same scan that reads
+ * +91..152 MB on a 4-core 16 GB builder read +258 MB on a 16-vCPU 64 GB CI
+ * VM with nothing retained. The child therefore runs with
+ * `BUN_JSC_forceRAMSize` pinned to 512 MiB so every runner sizes the heap the
+ * same way, and the 256 MB bound is unchanged. Probes behind that choice (384
+ * MiB corpus, this file's child, 4-core 16 GB builder; rerun with
+ * `BUN_JSC_forceRAMSize=<bytes> bun test/helpers/backlinks-rss-child.ts <dir> 384`):
+ * 64 GiB (68719476736, the CI VM's RAM) → rss +102..283 MB over 17 runs, 2 of them past 256 (272, 283): the CI failure, reproduced.
+ * 512 MiB (536870912, pinned here) → rss +78..137 MB over 5 runs.
+ * a pass1 that keeps every body (the pre-#6438 shape) under the pin → rss +609 MB and heap 385 MB: still fails the RSS, heap and slope bounds.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import { mkdtempSync, rmSync } from 'node:fs';
@@ -21,10 +33,13 @@ const SIZES_MIB = [48, 192, 384];
const roots: string[] = [];
afterAll(() => { while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true }); });
-function measure(mib: number): { gaps: number; delta_peak_mb: number; heap_retained_mb: number } {
+/** JSC sizes its heap growth from machine RAM; pin it so the RSS reading means the same thing on every runner (#6494). */
+const JSC_RAM_PIN_BYTES = 512 * 1024 * 1024;
+
+function measure(mib: number): { gaps: number; rss_delta_mb: number; heap_retained_mb: number } {
const root = mkdtempSync(join(tmpdir(), `gbrain-backlinks-rss-${mib}-`));
roots.push(root);
- const r = Bun.spawnSync(['bun', CHILD, root, String(mib)], { stdout: 'pipe', stderr: 'pipe', timeout: 600_000 });
+ const r = Bun.spawnSync(['bun', CHILD, root, String(mib)], { stdout: 'pipe', stderr: 'pipe', timeout: 600_000, env: { ...process.env, BUN_JSC_forceRAMSize: String(JSC_RAM_PIN_BYTES) } });
if (r.exitCode !== 0) throw new Error(`child failed (${r.exitCode}): ${r.stderr.toString()}`);
return JSON.parse(r.stdout.toString().trim().split('\n').at(-1)!);
}
@@ -32,13 +47,13 @@ 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 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('; ')}`);
+ console.error(`[backlinks-rss] ${runs.map(r => `${r.mib} MiB: rss +${r.rss_delta_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.
// 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.rss_delta_mb).toBeLessThan(256);
expect(r.heap_retained_mb).toBeLessThan(48);
}
// The slope is measured on the heap after a forced GC, which is what the scan kept alive; the RSS peak above
diff --git a/test/helpers/backlinks-rss-child.ts b/test/helpers/backlinks-rss-child.ts
index b48cdb581c..2bdd150a85 100644
--- a/test/helpers/backlinks-rss-child.ts
+++ b/test/helpers/backlinks-rss-child.ts
@@ -3,10 +3,12 @@
* argv: <corpusRoot> <nonTargetMiB>. Builds the corpus if the root is empty
* (50 fixed `people/`/`companies/` targets, non-target pages of ~1 MiB each
* with one sparse ref apiece), runs findBacklinkGaps, prints one JSON line:
- * { gaps, rss_before_mb, rss_peak_mb, delta_peak_mb, heap_retained_mb, largest_file_mb }:
- * the RSS peak is read right after the scan (what the OOM killer sees; RSS
- * never shrinks back), the heap delta after a forced GC is what the scan
- * actually kept alive.
+ * { gaps, rss_before_mb, rss_after_scan_mb, rss_delta_mb, heap_retained_mb, largest_file_mb }:
+ * the RSS is read right after the scan (what the OOM killer sees; RSS never
+ * shrinks back, but this is a point reading, not a true peak), the heap delta
+ * after a forced GC is what the scan actually kept alive. The RSS reading is
+ * mostly garbage JSC has not collected yet, and JSC sizes that allowance from
+ * the machine's RAM, so the parent pins `BUN_JSC_forceRAMSize` (#6494).
*/
import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
@@ -37,11 +39,11 @@ if (!existsSync(join(root, 'people')) || readdirSync(join(root, 'records')).leng
Bun.gc(true);
const before = process.memoryUsage();
const gaps = findBacklinkGaps(root);
-const peak = process.memoryUsage().rss;
+const afterScan = process.memoryUsage().rss;
Bun.gc(true);
const after = process.memoryUsage();
const mb = (n: number) => Math.round(n / (1024 * 1024));
console.log(JSON.stringify({
- gaps: gaps.length, rss_before_mb: mb(before.rss), rss_peak_mb: mb(peak),
- delta_peak_mb: mb(peak - before.rss), heap_retained_mb: mb(after.heapUsed - before.heapUsed), largest_file_mb: FILE_MIB,
+ gaps: gaps.length, rss_before_mb: mb(before.rss), rss_after_scan_mb: mb(afterScan),
+ rss_delta_mb: mb(afterScan - before.rss), heap_retained_mb: mb(after.heapUsed - before.heapUsed), largest_file_mb: FILE_MIB,
}));