From 1b31aabab327eca7985ca1d471e8d63d1800eb32 Mon Sep 17 00:00:00 2001 From: QuentinCody <33259999+QuentinCody@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:35:19 -0500 Subject: [PATCH] perf(harness): bound native daemon project checks by default --- skills/interlinked-verify/SKILL.md | 2 +- .../references/compiler-batches.md | 4 + .../tool-runners/tsc-daemon-cpu.test.ts | 93 +++++++++++++++++++ src/harness/check-engine/tool-runners/tsc.ts | 39 ++++---- src/harness/project-compiler-gate.ts | 1 + src/harness/project-typecheck-gate.ts | 3 +- src/harness/server.ts | 2 + 7 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 src/harness/check-engine/tool-runners/tsc-daemon-cpu.test.ts diff --git a/skills/interlinked-verify/SKILL.md b/skills/interlinked-verify/SKILL.md index d6a5f102..29268989 100644 --- a/skills/interlinked-verify/SKILL.md +++ b/skills/interlinked-verify/SKILL.md @@ -491,7 +491,7 @@ Read a reference file only when the task needs it. - [references/check-notes.md](references/check-notes.md): `verify --fp-filter`, `html_duplicate_id`, `test_legitimacy` receipts, effect-cleanup and type-discipline advisories, suppression hygiene, checker availability and warning delivery. - [references/test-discrimination.md](references/test-discrimination.md): `tests fail-before-fix` and the test-discrimination advisories. - [references/prove-kill.md](references/prove-kill.md): kill-proof receipt versions, parameterized families and check-evidence stores. -- [references/compiler-batches.md](references/compiler-batches.md): Claude `PostToolBatch` compiler batches. +- See [compiler batches and native TypeScript CPU settings](references/compiler-batches.md) for Claude `PostToolBatch` and daemon project checks. - [references/advisories.md](references/advisories.md): `metrics decisions` warnings and the `repeated_implementation` advisory. - [references/corpus-replay.md](references/corpus-replay.md): `harness corpus` qualification and replay evidence. - [references/interlinked-e2e-lane.md](references/interlinked-e2e-lane.md): the e2e lane for boundary edits in the Interlinked CLI checkout only. diff --git a/skills/interlinked-verify/references/compiler-batches.md b/skills/interlinked-verify/references/compiler-batches.md index b2d5774f..8075352d 100644 --- a/skills/interlinked-verify/references/compiler-batches.md +++ b/skills/interlinked-verify/references/compiler-batches.md @@ -20,6 +20,10 @@ from repetition. Project-wide Gitleaks runs in explicit verify/background recove not the interactive ChangeSet batch. Inline secret checks remain active. A deferred Gitleaks scan is NOT CHECKED, never a passing security verdict. +The daemon runs native TypeScript project checks with `GOMAXPROCS=2` by +default. Set `GOMAXPROCS` in the daemon's launch environment to choose its Go +parallelism for these checks. Foreground CLI checks inherit the caller's setting. + Gitleaks subprocesses use at most two Go execution threads (`GOMAXPROCS`); an existing value of `1` is preserved. Interrupted or truncated asynchronous scans produce unavailable evidence. Recovery retains a failed scan per project for the diff --git a/src/harness/check-engine/tool-runners/tsc-daemon-cpu.test.ts b/src/harness/check-engine/tool-runners/tsc-daemon-cpu.test.ts new file mode 100644 index 00000000..149072d3 --- /dev/null +++ b/src/harness/check-engine/tool-runners/tsc-daemon-cpu.test.ts @@ -0,0 +1,93 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import type { ToolRunnerInput } from "../types.js"; +import type { SpawnSyncStub } from "./test-process-fixtures.js"; + +const { resolvePackage, sync, asyncRun } = vi.hoisted(() => ({ + resolvePackage: vi.fn<() => string>(), + sync: vi.fn(), + asyncRun: vi.fn(), +})); +vi.mock("node:module", () => ({ createRequire: () => ({ resolve: resolvePackage }) })); +vi.mock("node:child_process", async (original) => ({ + ...await original(), spawnSync: sync, +})); +vi.mock("../spawn-async.js", () => ({ runProcessAsync: asyncRun })); + +let root: string; +beforeEach(() => { + vi.resetModules(); + vi.clearAllMocks(); + vi.stubEnv("GOMAXPROCS", undefined); + root = mkdtempSync(join(tmpdir(), "tsgo-daemon-cpu-")); + mkdirSync(join(root, "bin")); + writeFileSync(join(root, "bin", "tsgo.js"), ""); + writeFileSync(join(root, "tsconfig.json"), "{}"); + resolvePackage.mockReturnValue(join(root, "package.json")); + sync.mockImplementation((cmd, args) => ({ + pid: 1, status: cmd === "npx" && args.includes("--version") ? 1 : 0, + stdout: "", stderr: "", signal: null, output: [], + })); + asyncRun.mockResolvedValue({ code: 0, stdout: "", stderr: "", timedOut: false, killed: false }); +}); +afterEach(() => { + vi.unstubAllEnvs(); + rmSync(root, { recursive: true, force: true }); +}); + +async function compile(input: ToolRunnerInput, daemon: boolean) { + const { runTsc, runTscAsync, enableDaemonTsgoCpuLimit } = await import("./tsc.js"); + if (daemon) enableDaemonTsgoCpuLimit(); + expect(runTsc(input)).toEqual([]); + expect(await runTscAsync(input)).toEqual([]); + const syncCall = sync.mock.calls.find(([, args]) => args.includes("--noEmit")); + const asyncCall = asyncRun.mock.calls[0]; + if (!syncCall || !asyncCall) throw new Error("expected both project compilers to spawn"); + expect(syncCall[1]).toEqual(asyncCall[1]); + return { args: syncCall[1], envs: [syncCall[2].env, asyncCall[2]?.env] }; +} + +function project(): ToolRunnerInput { + return { scope: { projectRoot: root, mode: "project" }, timeoutMs: 5000 }; +} + +it("gives native daemon project checks two Go processors while preserving the child environment", async () => { + vi.stubEnv("TSGO_TEST_MARKER", "inherited"); + const { args, envs } = await compile(project(), true); + expect(args).toEqual([join(root, "bin", "tsgo.js"), "--noEmit", "--pretty", "false"]); + for (const env of envs) { + expect(env?.GOMAXPROCS).toBe("2"); + expect(env?.TSGO_TEST_MARKER).toBe("inherited"); + } + expect(process.env.GOMAXPROCS).toBeUndefined(); +}); + +it.each(["1", "8"])("inherits the operator's GOMAXPROCS=%s for both daemon project runners", async value => { + vi.stubEnv("GOMAXPROCS", value); + const { envs } = await compile(project(), true); + for (const env of envs) expect((env ?? process.env).GOMAXPROCS).toBe(value); +}); + +it("leaves ordinary foreground commands and explicit foreground verification unbounded", async () => { + expect((await compile(project(), false)).envs).toEqual([undefined, undefined]); + vi.clearAllMocks(); + expect((await compile({ ...project(), resourcePolicy: "foreground-verify" }, true)).envs) + .toEqual([undefined, undefined]); +}); + +it("leaves regular tsc and standalone native file checks on their inherited environment", async () => { + resolvePackage.mockReturnValue(join(root, "absent", "package.json")); + const tsc = await compile(project(), true); + expect(tsc.args[0]).toBe("tsc"); + expect(tsc.envs).toEqual([undefined, undefined]); + vi.resetModules(); + vi.clearAllMocks(); + resolvePackage.mockReturnValue(join(root, "package.json")); + rmSync(join(root, "tsconfig.json")); + const targetFile = join(root, "single.ts"); + const standalone = await compile({ scope: { projectRoot: root, mode: "file", targetFile }, timeoutMs: 5000 }, true); + expect(standalone.args).toContain(targetFile); + expect(standalone.envs).toEqual([undefined, undefined]); +}); diff --git a/src/harness/check-engine/tool-runners/tsc.ts b/src/harness/check-engine/tool-runners/tsc.ts index 444cc0cc..01900169 100644 --- a/src/harness/check-engine/tool-runners/tsc.ts +++ b/src/harness/check-engine/tool-runners/tsc.ts @@ -11,6 +11,7 @@ import { dirname, relative, resolve } from "node:path"; import { errorMessage } from "../../../lib/error-message.js"; import { nonNull } from "../../../lib/non-null.js"; import { + BACKGROUND_COMPILER_GOMAXPROCS, ProjectCompilerUnavailableError, type ProjectCompilerLeaseOptions, runWithProjectCompilerLease, @@ -47,6 +48,18 @@ let _daemonTsgoResolved: { bin: string; viaNode: boolean } | null | undefined; const COMPILER_OUTPUT_LIMIT_BYTES = 10 * 1024 * 1024; +let daemonTsgoCpuLimit = false; + +/** Called only by the daemon entry point, including on a cold start or handover. */ +export function enableDaemonTsgoCpuLimit(): void { + daemonTsgoCpuLimit = true; +} + +function projectCompilerEnv(useTsgo: boolean, input: ToolRunnerInput): { env?: NodeJS.ProcessEnv } { + if (!daemonTsgoCpuLimit || !useTsgo || input.resourcePolicy === "foreground-verify" || process.env.GOMAXPROCS !== undefined) return {}; + return { env: { ...process.env, GOMAXPROCS: BACKGROUND_COMPILER_GOMAXPROCS } }; +} + function unavailableFinding(file: string, reason: string): CheckResult[] { return [ { @@ -88,12 +101,14 @@ function runCompilerSync(options: { cwd: string; timeoutMs: number; resultFile: string; + env?: NodeJS.ProcessEnv; }): CheckResult[] { const release = tryAcquireProjectCompilerLease(options.cwd); if (!release) return unavailableFinding(options.resultFile, "another compiler owns this project"); try { const result = spawnSync(options.cmd, options.args, { cwd: options.cwd, + env: options.env, timeout: options.timeoutMs, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"], @@ -219,7 +234,7 @@ function tscCommandForAsyncRunner(): { cmd: string; args: string[]; useTsgo: boo export function runTsc(input: ToolRunnerInput): CheckResult[] { const { scope, timeoutMs } = input; const tscRoot = findTsconfig(scope.projectRoot); - const { cmd, args: cmdArgs } = tscCommand(); + const { cmd, args: cmdArgs, useTsgo } = tscCommand(); // If no tsconfig found but we have a specific .ts file, type-check it standalone. // This catches standalone scripts (hooks, configs) outside any tsconfig project. @@ -236,6 +251,7 @@ export function runTsc(input: ToolRunnerInput): CheckResult[] { cwd: tscRoot, timeoutMs, resultFile: compilerResultFile(input, tscRoot), + ...projectCompilerEnv(useTsgo, input), }); if (scope.mode === "file" && scope.targetFile && scope.filterToFile) { @@ -290,7 +306,6 @@ export async function runTscAsync(input: ToolRunnerInput): Promise { + const { cmd, args, useTsgo } = tscCommandForAsyncRunner(); + const resultFile = compilerResultFile(input, tscRoot); try { return await runWithProjectCompilerLease(tscRoot, async () => { const result = await runProcessAsync( cmd, - [...cmdArgs, "--noEmit", "--pretty", "false"], - { cwd: tscRoot, timeout: remainingCompilerTimeout(timeoutMs, foregroundDeadline) }, + [...args, "--noEmit", "--pretty", "false"], + { cwd: tscRoot, timeout: remainingCompilerTimeout(input.timeoutMs, foregroundDeadline), ...projectCompilerEnv(useTsgo, input) }, ); return parseAsyncCompilerResult(result, resultFile); }, compilerAdmissionOptions(foregroundDeadline)); diff --git a/src/harness/project-compiler-gate.ts b/src/harness/project-compiler-gate.ts index 78b33092..6c71414d 100644 --- a/src/harness/project-compiler-gate.ts +++ b/src/harness/project-compiler-gate.ts @@ -14,6 +14,7 @@ import { const DEFAULT_MAX_QUEUED = 8; const DEFAULT_ADMISSION_TIMEOUT_MS = 2_000; +export const BACKGROUND_COMPILER_GOMAXPROCS = "2"; export type ProjectCompilerUnavailableReason = "aborted" | "busy" | "queue_full"; diff --git a/src/harness/project-typecheck-gate.ts b/src/harness/project-typecheck-gate.ts index a0611cc4..69815669 100644 --- a/src/harness/project-typecheck-gate.ts +++ b/src/harness/project-typecheck-gate.ts @@ -36,6 +36,7 @@ import { readResourceBudget } from "./resource-budget.js"; import { tryAcquireForegroundCapacity } from "./test-capacity.js"; import { observeProjectProcess, type ProjectProcessObservation, type ProjectProcessObserver } from "./project-process-observation.js"; import { + BACKGROUND_COMPILER_GOMAXPROCS, runWithProjectCompilerLease, tryAcquireProjectCompilerLease, } from "./project-compiler-gate.js"; @@ -333,7 +334,7 @@ async function runObservedCompiler(cwd: string, command: ResolvedTypecheckComman execStarted = Date.now(); Object.assign(observation, { waitMs: Math.max(0, execStarted - started), resourceBudget, heapLimitMb }); const result = await runProcessAsync(command.bin, command.args, { cwd, timeout, resourceBudget, ...(signal ? { signal } : {}), - env: { NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ""} --max-old-space-size=${heapLimitMb}`.trim(), GOMAXPROCS: "2", UV_THREADPOOL_SIZE: "2" } }); + env: { NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ""} --max-old-space-size=${heapLimitMb}`.trim(), GOMAXPROCS: BACKGROUND_COMPILER_GOMAXPROCS, UV_THREADPOOL_SIZE: "2" } }); if (result.peakRssBytes !== undefined) observation.peakRssBytes = result.peakRssBytes; if (result.resourceReason !== undefined) observation.resourceReason = result.resourceReason; return result; diff --git a/src/harness/server.ts b/src/harness/server.ts index 5c54e070..4cd8ff2b 100644 --- a/src/harness/server.ts +++ b/src/harness/server.ts @@ -18,6 +18,7 @@ import { ensureLocalRuntimePolicy } from "./runtime-policy.js"; ensureLocalRuntimePolicy(); import { registerAllBuiltinVerifyPasses } from "./check-pipeline/builtin-verify-passes.js"; +import { enableDaemonTsgoCpuLimit } from "./check-engine/tool-runners/tsc.js"; import { astComplexityAvailable } from "./checks/cyclomatic-ast.js"; import { recordDaemonEvent, recordDaemonExit } from "./daemon-ledger.js"; import { type FilePriority } from "./file-priority.js"; @@ -51,6 +52,7 @@ import type { GuardRulesConfig, HarnessDecision, HarnessEvent } from "./types.js // =========================================== const cliConfig = readServerCliConfig(); +enableDaemonTsgoCpuLimit(); const { cwd: CWD, interlinkedDir: INTERLINKED_DIR,