Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(process): expose Darwin swiftc compile deadline via env (#2240)

BeqaKse4186af31be8
diff --git a/docs/installation.md b/docs/installation.md
index a39471585..3ad6b7071 100644
--- a/docs/installation.md
+++ b/docs/installation.md
@@ -764,6 +764,9 @@ errors expose Error names, messages, and available codes under
truncation markers. Darwin ownership-helper compilation failures also retain
the compiler outcome, exit code or signal, and bounded stderr; a timed-out
compiler remains distinguishable from a compiler that exited with an error.
+The helper compile deadline defaults to 120,000 ms; set
+`REA_SWIFTC_COMPILE_TIMEOUT_MS` to an integer between 1 and 2,147,483,647 when
+a busy host needs longer.
Other rejection values retain their primitive value or an explicit type.
Shutdown warnings include the failure
kind, message, and diagnostics, while process and temporary-project cleanup
diff --git a/docs/mcp-contracts.md b/docs/mcp-contracts.md
index 876a30f6d..a2708511c 100644
--- a/docs/mcp-contracts.md
+++ b/docs/mcp-contracts.md
@@ -474,6 +474,7 @@ These deadlines have different owners:
| MCP initialize | The client bounds transport/REA connection startup, before any target query. |
| Hopper startup | REA allows 120,000 ms by default; `REA_HOPPER_STARTUP_TIMEOUT_MS` accepts decimal integers from 1–2,147,483,647 ms. Invalid values fail configuration validation. Startup failure is reported by the first provider query. |
| Ghidra startup | REA allows 330,000 ms by default; `REA_GHIDRA_STARTUP_TIMEOUT_MS` accepts decimal integers from 1–2,147,483,647 ms. Invalid supplied values fail configuration validation. Startup failure is reported by the first provider query. |
+| Darwin swiftc helper | REA allows 120,000 ms by default for ownership-helper compilation; `REA_SWIFTC_COMPILE_TIMEOUT_MS` accepts decimal integers from 1–2,147,483,647 ms. Invalid values fail configuration validation. |
| Individual tool call | The client bounds its wait, including cold engine startup. The pinned client SDK 2.3.1 defaults to 60,000 ms and can cancel earlier than REA's startup deadline. |
For an already connected client using the pinned SDK, request options are the
diff --git a/src/config.test.ts b/src/config.test.ts
index 67db7e111..efa1b48fc 100644
--- a/src/config.test.ts
+++ b/src/config.test.ts
@@ -249,6 +249,40 @@ describe("configuration failure diagnostics", () => {
},
);
+ it.each(["1", "90000", "2147483647"])(
+ "accepts Swift compile deadline %s without changing Hopper's deadline",
+ (value) => {
+ expect(
+ parseConfig({ REA_SWIFTC_COMPILE_TIMEOUT_MS: value }),
+ ).toMatchObject({
+ ok: true,
+ value: {
+ hopperStartupTimeoutMs: 120_000,
+ },
+ });
+ },
+ );
+
+ it.each(["", "0", "-1", "1.5", "1e3", "invalid", "2147483648"])(
+ "rejects malformed Swift compile deadline %j with its setting",
+ (value) => {
+ const result = parseConfig({ REA_SWIFTC_COMPILE_TIMEOUT_MS: value });
+ if (result.ok) throw new Error("Expected invalid Swift compile deadline");
+ expect(projectAnalysisError(result.error)).toMatchObject({
+ code: "configuration_invalid",
+ category: "invalid_input",
+ details: {
+ settings: [
+ {
+ setting: "REA_SWIFTC_COMPILE_TIMEOUT_MS",
+ constraint: expect.any(String),
+ },
+ ],
+ },
+ });
+ },
+ );
+
it.each([
["GHIDRA_INSTALL_DIR", "./fixture"],
["JAVA_HOME", "./jdk"],
diff --git a/src/config/environment.ts b/src/config/environment.ts
index 7dcd955d0..121d46614 100644
--- a/src/config/environment.ts
+++ b/src/config/environment.ts
@@ -12,6 +12,7 @@ import { ghidraDisabledAnalyzersSchema } from "./ghidraDisabledAnalyzers.js";
import {
DEFAULT_GHIDRA_STARTUP_TIMEOUT_MS,
DEFAULT_HOPPER_STARTUP_TIMEOUT_MS,
+ DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS,
providerStartupTimeoutSchema,
} from "./providerStartupTimeout.js";
import { mcpInputSchemaProfileSchema } from "./mcpInputSchemaProfile.js";
@@ -47,6 +48,9 @@ const environmentSchema = z.object({
REA_HOPPER_STARTUP_TIMEOUT_MS: providerStartupTimeoutSchema.default(
DEFAULT_HOPPER_STARTUP_TIMEOUT_MS,
),
+ REA_SWIFTC_COMPILE_TIMEOUT_MS: providerStartupTimeoutSchema.default(
+ DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS,
+ ),
REA_GHIDRA_LANGUAGE_ID: ghidraLanguageIdSchema.optional(),
REA_GHIDRA_COMPILER_SPEC_ID: ghidraCompilerSpecIdSchema.optional(),
REA_GHIDRA_DISABLED_ANALYZERS: ghidraDisabledAnalyzersSchema.optional(),
diff --git a/src/config/providerStartupTimeout.ts b/src/config/providerStartupTimeout.ts
index 27c0116de..eb3b9d52f 100644
--- a/src/config/providerStartupTimeout.ts
+++ b/src/config/providerStartupTimeout.ts
@@ -6,6 +6,10 @@ export const DEFAULT_GHIDRA_STARTUP_TIMEOUT_MS = 330_000;
/** Default absolute deadline for Hopper launch, analysis, bridge, and readiness. */
export const DEFAULT_HOPPER_STARTUP_TIMEOUT_MS = 120_000;
+/** Default deadline for Darwin ownership-helper `swiftc` compilation. */
+/** Fresh Swift SDK cache took ~88s on the supported Intel macOS VM. */
+export const DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS = 120_000;
+
// Node schedules delays above this signed 32-bit boundary as a 1 ms timer.
const MAX_TIMER_DELAY_MS = 2_147_483_647;
diff --git a/src/process/DarwinProcessRunTokenReader.timeout.test.ts b/src/process/DarwinProcessRunTokenReader.timeout.test.ts
new file mode 100644
index 000000000..224b49600
--- /dev/null
+++ b/src/process/DarwinProcessRunTokenReader.timeout.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from "vitest";
+
+import { resolveSwiftcCompileTimeoutMs } from "./DarwinProcessRunTokenReader.js";
+
+describe("resolveSwiftcCompileTimeoutMs", () => {
+ it("keeps the historical default when unset", () => {
+ expect(resolveSwiftcCompileTimeoutMs({ environment: {} })).toBe(120_000);
+ });
+
+ it("prefers an explicit compileTimeoutMs over the environment", () => {
+ expect(
+ resolveSwiftcCompileTimeoutMs({
+ compileTimeoutMs: 12_000,
+ environment: { REA_SWIFTC_COMPILE_TIMEOUT_MS: "300000" },
+ }),
+ ).toBe(12_000);
+ });
+
+ it("reads a valid REA_SWIFTC_COMPILE_TIMEOUT_MS value", () => {
+ expect(
+ resolveSwiftcCompileTimeoutMs({
+ environment: { REA_SWIFTC_COMPILE_TIMEOUT_MS: "300000" },
+ }),
+ ).toBe(300_000);
+ });
+
+ it("falls back to the default for a malformed environment value", () => {
+ expect(
+ resolveSwiftcCompileTimeoutMs({
+ environment: { REA_SWIFTC_COMPILE_TIMEOUT_MS: "loud" },
+ }),
+ ).toBe(120_000);
+ });
+});
diff --git a/src/process/DarwinProcessRunTokenReader.ts b/src/process/DarwinProcessRunTokenReader.ts
index 335e72fbe..79e9d9c11 100644
--- a/src/process/DarwinProcessRunTokenReader.ts
+++ b/src/process/DarwinProcessRunTokenReader.ts
@@ -5,6 +5,10 @@ import { isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { z } from "zod";
+import {
+ DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS,
+ providerStartupTimeoutSchema,
+} from "../config/providerStartupTimeout.js";
import { safeParseJson } from "../domain/safeJson.js";
import { boundedUtf8Text } from "./boundedUtf8Text.js";
import { execFileOutput, execFileOutputFailure } from "./ExecFileOutput.js";
@@ -73,10 +77,27 @@ const sourceFiles = [
*/
export const SWIFTC_INTERRUPT = "SIGINT";
-/** Deadline for the one-shot helper compilation. */
-// A fresh Swift SDK cache took 88 seconds on the supported Intel macOS VM.
-// Keep compilation bounded independently of short provider version probes.
-const COMPILER_TIMEOUT_MS = 120_000;
+/**
+ * Resolve the Darwin ownership-helper compile deadline from an explicit option
+ * or `REA_SWIFTC_COMPILE_TIMEOUT_MS`. The default matches the measured fresh
+ * Swift SDK cache on the supported Intel macOS VM (about 88s), so compilation
+ * stays bounded independently of short provider version probes. Invalid ambient
+ * values fall back to the default; `parseConfig` still rejects them at CLI/MCP
+ * admission.
+ */
+export const resolveSwiftcCompileTimeoutMs = (
+ options: {
+ readonly compileTimeoutMs?: number;
+ readonly environment?: Readonly<Record<string, string | undefined>>;
+ } = {},
+): number => {
+ if (options.compileTimeoutMs !== undefined) return options.compileTimeoutMs;
+ const raw = (options.environment ?? process.env)
+ .REA_SWIFTC_COMPILE_TIMEOUT_MS;
+ if (raw === undefined) return DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS;
+ const parsed = providerStartupTimeoutSchema.safeParse(raw);
+ return parsed.success ? parsed.data : DEFAULT_SWIFTC_COMPILE_TIMEOUT_MS;
+};
/** Bound on compiler diagnostics embedded in the inspection failure text. */
const STDERR_EXCERPT_LIMIT = 512;
@@ -257,6 +278,8 @@ export const createDarwinProcessRunTokenReader = (
readonly compiler?: DarwinProcessReaderCompiler;
/** Deadline for helper compilation, independent of provider execution. */
readonly compileTimeoutMs?: number;
+ /** Environment consulted when `compileTimeoutMs` is omitted. */
+ readonly environment?: Readonly<Record<string, string | undefined>>;
/**
* Caller-owned directory holding the compiler's module cache. Unset keeps
* Swift's reusable default cache; a supplied directory outlives this
@@ -269,7 +292,7 @@ export const createDarwinProcessRunTokenReader = (
kind: "xcrun",
executable: "/usr/bin/xcrun",
};
- const compilerTimeoutMs = options.compileTimeoutMs ?? COMPILER_TIMEOUT_MS;
+ const compilerTimeoutMs = resolveSwiftcCompileTimeoutMs(options);
const moduleCachePath = options.moduleCachePath ?? "";
let root: string | undefined;
let executable: string | undefined;
diff --git a/src/process/ProcessOwnershipObservation.ts b/src/process/ProcessOwnershipObservation.ts
index 834335d57..5747cded8 100644
--- a/src/process/ProcessOwnershipObservation.ts
+++ b/src/process/ProcessOwnershipObservation.ts
@@ -225,6 +225,8 @@ export const createSystemProcessOwnershipHost = (
readonly darwinCompiler?: DarwinProcessReaderCompiler;
/** Override the caller-owned module cache used compiling that helper. */
readonly darwinModuleCachePath?: string;
+ /** Deadline for Darwin ownership-helper compilation. */
+ readonly compileTimeoutMs?: number;
} = {},
): ProcessOwnershipHost => {
const darwinModuleCachePath =
@@ -247,6 +249,9 @@ export const createSystemProcessOwnershipHost = (
...(darwinModuleCachePath === undefined
? {}
: { moduleCachePath: darwinModuleCachePath }),
+ ...(options.compileTimeoutMs === undefined
+ ? { environment: hostEnvironment }
+ : { compileTimeoutMs: options.compileTimeoutMs }),
})
: undefined;
const listProcesses = async (signal?: AbortSignal) => {