From 44bad6f965ce75dfbcae4f33de4532d33c63e111 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 24 Nov 2026 08:20:52 -0600 Subject: [PATCH 1/2] test(pm-stack): isolate health-priming server from machine state (#14167) The health-priming test spawned the real aggregator with the caller env and its cwd in the package directory. Quota-current, the quota heartbeat, the meta-renewal state and the gatherer (QUOTA_CURRENT_PATH) all defaulted into the package dir, the freshness IPC port collided with a live aggregator on 4871, and background quota gather ran against the real machine. Point every writable path at the test tmp dir, run the server from there, and add a guard that fails if the run leaves files in the package dir. --- .../aggregator/test/health-priming.test.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pm-stack/aggregator/test/health-priming.test.ts b/pm-stack/aggregator/test/health-priming.test.ts index 45b514eac9..ebdf30c513 100644 --- a/pm-stack/aggregator/test/health-priming.test.ts +++ b/pm-stack/aggregator/test/health-priming.test.ts @@ -1,6 +1,6 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test"; import { spawn, type Subprocess } from "bun"; -import { mkdtemp, writeFile } from "node:fs/promises"; +import { mkdtemp, readdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -19,6 +19,8 @@ interface HealthBody { let proc: Subprocess | undefined; let firstHealth: HealthBody | undefined; +let packageDirBefore: string[] = []; +const packageDir = join(import.meta.dir, ".."); // #10467: starting the aggregator subprocess can take longer than bun's // default 5 s hook budget on a loaded machine; give the hook room to @@ -70,14 +72,19 @@ beforeAll(async () => { const feedFile = join(dir, "fleet-feed.json"); await writeFile(feedFile, JSON.stringify(feed)); const port = 5100 + Math.floor(Math.random() * 800); + packageDirBefore = (await readdir(packageDir)).sort(); proc = spawn({ - cmd: ["bun", "run", "src/server.ts"], - cwd: join(import.meta.dir, ".."), + cmd: ["bun", "run", join(packageDir, "src", "server.ts")], + cwd: dir, env: { ...Bun.env, PM_AGG_PORT: String(port), + PM_AGG_FRESHNESS_PORT: String(port + 1000), PM_AGG_SOURCE: "live", PM_AGG_FEED_FILE: feedFile, + PM_AGG_QUOTA_CURRENT_FILE: join(dir, "quota-current.json"), + QUOTA_CURRENT_PATH: join(dir, "quota-current.json"), + META_RENEWAL_STATE_PATH: join(dir, "meta-renewal.json"), PM_AGG_REGISTRY_FILE: join(dir, "registry.json"), PM_AGG_COMMENTS_FILE: join(dir, "comments.json"), PM_AGG_SNAPSHOTS_DIR: join(dir, "snapshots"), @@ -123,4 +130,9 @@ describe("/api/health provenance priming (#1267)", () => { expect(firstHealth?.sections?.orchestration.state).toBe("live"); expect(firstHealth?.sections?.orchestration.runId).toBe("orch-health-test"); }); + + test("the run leaves no files in the package directory", async () => { + await Bun.sleep(500); + expect((await readdir(packageDir)).sort()).toEqual(packageDirBefore); + }); }); From d134e9cfdf632f0caa9447c3df67bcbfdcc7b68d Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Tue, 24 Nov 2026 09:39:42 -0600 Subject: [PATCH 2/2] test(pm-stack): fully isolate health-priming server from machine state (#14167) Extends #14414: redirect HOME, freshness port, quota heartbeat/state/ snapshots/forecast paths, fixture QUOTA_SUBCOMMAND, disable Meta/OpenDesign renewal keepers and Turso sink, audit path, registry/comments/snapshots, projector off, blank Kaneo/Grist creds. Server writes only under tmp dir; no real quota gather during test. --- .../aggregator/test/health-priming.test.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pm-stack/aggregator/test/health-priming.test.ts b/pm-stack/aggregator/test/health-priming.test.ts index ebdf30c513..b5acdfd35b 100644 --- a/pm-stack/aggregator/test/health-priming.test.ts +++ b/pm-stack/aggregator/test/health-priming.test.ts @@ -71,6 +71,28 @@ beforeAll(async () => { }; const feedFile = join(dir, "fleet-feed.json"); await writeFile(feedFile, JSON.stringify(feed)); + // #14167: the spawned server must not touch the real machine — its + // background jobs (the pm-core quota/fleet-feed schedulers, the Meta + // renewal keeper and the snapshot/hot-list consumers) run real + // collection unless their seams are disconnected. The fixture + // subcommand answers with empty provider readings so the gatherer's + // files stay in the tmp dir; the renewal keeper is disabled; the Turso + // sink is off; the audit logs and snapshots land beside the feed. + const subcommandPath = join(dir, "quota-subcommand.mjs"); + const providerResult = { + providerId: "openai", + providerName: "OpenAI", + ok: true, + configured: true, + fetchedAt: Date.parse(now), + failureClass: "no-data", + lastSuccessAt: Date.parse(now), + usage: { windows: { weekly: { usedPercent: null } } }, + }; + // Fixture subcommand: answers with one valid provider row so the + // gatherer exercises its write paths (heartbeat, current, anomaly + // state) without touching real provider credentials or the network. + await writeFile(subcommandPath, `console.log(${JSON.stringify(JSON.stringify([providerResult]))});\n`, "utf8"); const port = 5100 + Math.floor(Math.random() * 800); packageDirBefore = (await readdir(packageDir)).sort(); proc = spawn({ @@ -78,13 +100,24 @@ beforeAll(async () => { cwd: dir, env: { ...Bun.env, + HOME: dir, PM_AGG_PORT: String(port), PM_AGG_FRESHNESS_PORT: String(port + 1000), PM_AGG_SOURCE: "live", PM_AGG_FEED_FILE: feedFile, PM_AGG_QUOTA_CURRENT_FILE: join(dir, "quota-current.json"), QUOTA_CURRENT_PATH: join(dir, "quota-current.json"), + QUOTA_HEARTBEAT_PATH: join(dir, "quota-gatherer-heartbeat.json"), + QUOTA_GATHERER_STATE_PATH: join(dir, "quota-gatherer-state.json"), + QUOTA_SNAPSHOTS_DIR: join(dir, "snapshots"), + QUOTA_FORECAST_PATH: join(dir, "quota-forecast.json"), + QUOTA_SUBCOMMAND: `${process.execPath} ${subcommandPath}`, + QUOTA_SUBCOMMAND_TIMEOUT_MS: "5000", + META_RENEWAL_DISABLED: "1", + OPENDESIGN_RENEWAL_DISABLED: "1", + TURSO_QUOTA_SINK: "0", META_RENEWAL_STATE_PATH: join(dir, "meta-renewal.json"), + PM_CORE_JOBS_AUDIT_PATH: join(dir, "pm-core-jobs.jsonl"), PM_AGG_REGISTRY_FILE: join(dir, "registry.json"), PM_AGG_COMMENTS_FILE: join(dir, "comments.json"), PM_AGG_SNAPSHOTS_DIR: join(dir, "snapshots"),