Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion skills/interlinked-verify/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions skills/interlinked-verify/references/compiler-batches.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions src/harness/check-engine/tool-runners/tsc-daemon-cpu.test.ts
Original file line number Diff line number Diff line change
@@ -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<SpawnSyncStub>(),
asyncRun: vi.fn<typeof import("../spawn-async.js").runProcessAsync>(),
}));
vi.mock("node:module", () => ({ createRequire: () => ({ resolve: resolvePackage }) }));
vi.mock("node:child_process", async (original) => ({
...await original<typeof import("node:child_process")>(), 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]);
});
39 changes: 23 additions & 16 deletions src/harness/check-engine/tool-runners/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 [
{
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -290,7 +306,6 @@ export async function runTscAsync(input: ToolRunnerInput): Promise<CheckResult[]
const { scope, timeoutMs } = input;
const foregroundDeadline = input.resourcePolicy === "foreground-verify" ? (timeoutMs === 0 ? Infinity : Date.now() + timeoutMs) : undefined;
const tscRoot = findTsconfig(scope.projectRoot);
const { cmd, args: cmdArgs } = tscCommandForAsyncRunner();

if (!tscRoot) {
if (scope.mode === "file" && scope.targetFile?.match(/\.tsx?$/)) {
Expand All @@ -299,14 +314,7 @@ export async function runTscAsync(input: ToolRunnerInput): Promise<CheckResult[]
return [];
}

const parsed = await runProjectCheck(
tscRoot,
timeoutMs,
cmd,
cmdArgs,
compilerResultFile(input, tscRoot),
foregroundDeadline,
);
const parsed = await runProjectCheck(tscRoot, input, foregroundDeadline);

if (scope.mode === "file" && scope.targetFile && scope.filterToFile) {
if (compilerWasUnavailable(parsed)) return parsed;
Expand Down Expand Up @@ -338,18 +346,17 @@ function remainingCompilerTimeout(timeoutMs: number, deadline: number | undefine

async function runProjectCheck(
tscRoot: string,
timeoutMs: number,
cmd: string,
cmdArgs: string[],
resultFile: string,
input: ToolRunnerInput,
foregroundDeadline?: number,
): Promise<CheckResult[]> {
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));
Expand Down
1 change: 1 addition & 0 deletions src/harness/project-compiler-gate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
3 changes: 2 additions & 1 deletion src/harness/project-typecheck-gate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/harness/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -51,6 +52,7 @@ import type { GuardRulesConfig, HarnessDecision, HarnessEvent } from "./types.js
// ===========================================

const cliConfig = readServerCliConfig();
enableDaemonTsgoCpuLimit();
const {
cwd: CWD,
interlinkedDir: INTERLINKED_DIR,
Expand Down
Loading