* test: prefer structured assertions over recovery prose Lock regex, IDA, Hopper/Kuna, MCP budget, capture-heap, and pcap failures on error codes and reported limits instead of full recovery sentences. Co-authored-by: morluto <[email protected]> * test: keep decoder heap negatives off the Windows reserve "512 MiB" also appears in the Bun Windows reserve sentence. The negative check stays on "512 MiB (source setting)", which is the other adapter's heap. Co-authored-by: morluto <[email protected]> * test: keep the one-retry cap and deadline recovery tokens The lost-connection guidance must say once only, so an unlimited "whenever it is safe" rewrite fails. Deadline recovery still has to name set, restart, the floor, and the maximum constant. Co-authored-by: morluto <[email protected]> * test: keep heap, MCP, pcap, and preload recovery tokens Codex still wanted the set/restart/floor/maximum heap guidance, MCP default/floor/maximum bounds, Wireshark install guidance, and the dropped-preload attribution limit as separate tokens. Co-authored-by: morluto <[email protected]> * test: keep shared-context and transport recovery tokens Shared-context capture must still exclude requests without a known root or descendant frame. Shutdown must end the REA process before starting again, and a failed HTTP listen must offer another port or stopping the process that holds it. Co-authored-by: morluto <[email protected]> * test: keep handoff, input-check, and budget format tokens Skill and server guidance must still tell the user to restart the MCP client. Process failures must still say to check the input files. Invalid MCP phase budgets must still say positive decimal integer and ms. Co-authored-by: morluto <[email protected]> * test: bind budget floors and active deadline sources The phase-budget minimum is the start of "from N to M ms", so a digit inside the maximum no longer satisfies it. Capability text must contain the active "N ms (source ...)" pair, not the default number and a heap source mentioned apart. Co-authored-by: morluto <[email protected]> * test: bind heap MiB sources and pcap host tokens A configured 256 MiB request must appear as "256 MiB (source setting)", so a byte-sized 256 no longer passes. Unsupported pcap hosts must still name Linux, util-linux, Windows, and owned Job Objects as separate tokens. Co-authored-by: morluto <[email protected]> * test: bind the selected decoder heap to MiB and source The two-adapter limitation must contain "256 MiB (source setting)". The exhaustion message binds that capacity as "256 MiB V8 old-generation heap (source setting)", so a byte-sized 256 no longer passes. Co-authored-by: morluto <[email protected]> * test: bind doctor client seconds and prlimit absolute guidance Doctor recovery must name startup_timeout_sec of the policy's seconds, so a hardcoded 60 no longer passes while the policy stays 30. A relative prlimit must say the value must be absolute and tell the user to check REA_TSHARK_PRLIMIT_COMMAND. Co-authored-by: morluto <[email protected]> * test: assert integrity codes for rehashed evidence and coverage Independently rehashed process Evidence and a tampered digest project evidence_integrity_mismatch. An unsupported comparison operation stays invalid_request. Unsupported inventory coverage status is an integrity failure whose reason carries the coverage.status schema issue. Co-authored-by: morluto <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: morluto <[email protected]>
633 lines
21 KiB
TypeScript
633 lines
21 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
import { expect, it } from "vitest";
|
|
|
|
import {
|
|
DEFAULT_REGEX_MATCHING_DEADLINE_MS,
|
|
MAXIMUM_REGEX_MATCHING_DEADLINE_MS,
|
|
REGEX_MATCHING_DEADLINE_SETTING,
|
|
} from "../../../src/config/regexMatchingDeadline.js";
|
|
import { MINIMUM_REGEX_WORKER_HEAP_REQUEST_MIB } from "../../../src/config/regexWorkerHeapRequest.js";
|
|
import { ok } from "../../../src/domain/result.js";
|
|
import { HopperRegexSearch } from "../../../src/hopper/HopperRegexSearch.js";
|
|
import { OFFICIAL_TOOL_CONTRACTS } from "../../../src/contracts/officialToolContracts.js";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import {
|
|
matchRegexValues,
|
|
REGEX_MATCHING_LIMITATION,
|
|
regexMatchingDeadlineError,
|
|
regexMatchingLimitation,
|
|
regexWorkerHeapError,
|
|
execArgvFromCommandLine,
|
|
resolveLaunchRegexWorkerHeap,
|
|
resolveRegexWorkerHeap,
|
|
readLaunchExecArgv,
|
|
readLaunchNodeOptions,
|
|
} from "../../../src/process/RegexMatcher.js";
|
|
|
|
const workerHeapProbe = `
|
|
import { Worker } from "node:worker_threads";
|
|
const worker = new Worker(
|
|
\`import { parentPort, resourceLimits } from "node:worker_threads";
|
|
import v8 from "node:v8";
|
|
parentPort.postMessage({
|
|
resourceOld: resourceLimits.maxOldGenerationSizeMb,
|
|
heap: v8.getHeapStatistics().heap_size_limit,
|
|
});\`,
|
|
{ eval: true, resourceLimits: { maxOldGenerationSizeMb: 128 } },
|
|
);
|
|
worker.once("message", (message) => {
|
|
process.stdout.write(JSON.stringify(message));
|
|
void worker.terminate();
|
|
});
|
|
worker.once("error", (error) => {
|
|
process.stderr.write(error.message);
|
|
process.exitCode = 1;
|
|
});
|
|
`;
|
|
|
|
const probeWorkerHeap = (
|
|
args: readonly string[],
|
|
nodeOptions: string | undefined,
|
|
cwd?: string,
|
|
): { readonly resourceOld: number; readonly heap: number } => {
|
|
const env = { ...process.env };
|
|
if (nodeOptions === undefined) delete env.NODE_OPTIONS;
|
|
else env.NODE_OPTIONS = nodeOptions;
|
|
const result = spawnSync(process.execPath, [...args, "-e", workerHeapProbe], {
|
|
cwd,
|
|
env,
|
|
encoding: "utf8",
|
|
});
|
|
expect(result.status).toBe(0);
|
|
return JSON.parse(result.stdout ?? "") as {
|
|
resourceOld: number;
|
|
heap: number;
|
|
};
|
|
};
|
|
|
|
it("names the configurable regex deadline and the effective worker heap", () => {
|
|
const heap = resolveLaunchRegexWorkerHeap(
|
|
readLaunchExecArgv(),
|
|
readLaunchNodeOptions(),
|
|
);
|
|
expect(REGEX_MATCHING_LIMITATION).toBe(regexMatchingLimitation(heap));
|
|
expect(REGEX_MATCHING_LIMITATION).toContain(REGEX_MATCHING_DEADLINE_SETTING);
|
|
expect(REGEX_MATCHING_LIMITATION).toContain(
|
|
`${String(DEFAULT_REGEX_MATCHING_DEADLINE_MS)} ms (source default`,
|
|
);
|
|
expect(REGEX_MATCHING_LIMITATION).toContain(
|
|
"REA_REGEX_WORKER_HEAP_REQUEST_MIB",
|
|
);
|
|
expect(REGEX_MATCHING_LIMITATION).not.toContain("no runtime override");
|
|
if (heap.kind === "unknown") {
|
|
expect(REGEX_MATCHING_LIMITATION).toContain("was not established");
|
|
} else {
|
|
expect(REGEX_MATCHING_LIMITATION).toContain(
|
|
`effective ${String(heap.mib)} MiB`,
|
|
);
|
|
}
|
|
const deadline = projectAnalysisError(
|
|
regexMatchingDeadlineError("search_procedures"),
|
|
);
|
|
expect(deadline).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
resource: "cpu",
|
|
reported_limits: {
|
|
matching_deadline_ms: DEFAULT_REGEX_MATCHING_DEADLINE_MS,
|
|
matching_deadline_source: "default",
|
|
},
|
|
},
|
|
});
|
|
expect(deadline.message).toContain(REGEX_MATCHING_DEADLINE_SETTING);
|
|
expect(deadline.remediation.action).toContain(
|
|
REGEX_MATCHING_DEADLINE_SETTING,
|
|
);
|
|
expect(deadline.remediation.action).not.toContain("no runtime override");
|
|
const failure = projectAnalysisError(
|
|
regexWorkerHeapError("search_procedures", new Error("heap")),
|
|
);
|
|
expect(failure.remediation.action).not.toContain("no runtime override");
|
|
expect(failure.remediation.action).toContain("--max-old-space-size");
|
|
expect(failure.remediation.action).toContain(
|
|
"REA_REGEX_WORKER_HEAP_REQUEST_MIB",
|
|
);
|
|
if (heap.kind === "effective") {
|
|
expect(failure.message).toContain(`${String(heap.mib)} MiB`);
|
|
expect(failure.details).toMatchObject({
|
|
reported_limits: {
|
|
worker_heap_mib: heap.mib,
|
|
requested_worker_heap_mib: 128,
|
|
requested_worker_heap_source: "default",
|
|
},
|
|
});
|
|
} else {
|
|
expect(failure.message).toContain("was not established");
|
|
expect(failure.details).toMatchObject({
|
|
reported_limits: {
|
|
requested_worker_heap_mib: 128,
|
|
requested_worker_heap_source: "default",
|
|
},
|
|
});
|
|
}
|
|
for (const name of ["search_procedures", "search_strings"] as const) {
|
|
const description = OFFICIAL_TOOL_CONTRACTS.find(
|
|
(tool) => tool.name === name,
|
|
)?.description;
|
|
expect(description).toContain(REGEX_MATCHING_DEADLINE_SETTING);
|
|
expect(description).toContain(
|
|
`default ${String(DEFAULT_REGEX_MATCHING_DEADLINE_MS)}`,
|
|
);
|
|
expect(description).toContain(String(MAXIMUM_REGEX_MATCHING_DEADLINE_MS));
|
|
expect(description).toContain("source (default or setting)");
|
|
expect(description).toContain("REA_REGEX_WORKER_HEAP_REQUEST_MIB");
|
|
expect(description).toContain("default 128");
|
|
expect(description).toContain("from 7 to 16384");
|
|
expect(description).toContain("floor");
|
|
expect(description).toContain("maximum");
|
|
expect(description).not.toContain("no runtime override");
|
|
}
|
|
});
|
|
|
|
const relativeHeapGap = (left: number, right: number): number =>
|
|
Math.abs(left - right) / Math.max(left, right);
|
|
|
|
it("leaves a heap percentage unestablished without reading host memory", () => {
|
|
const percentage = "--max-old-space-size-percentage=50";
|
|
const absolute = "--max-old-space-size=64";
|
|
const observed = probeWorkerHeap([percentage, absolute], undefined);
|
|
expect(observed.resourceOld).toBe(128);
|
|
for (const execArgv of [
|
|
[percentage],
|
|
[percentage, absolute],
|
|
[absolute, percentage],
|
|
])
|
|
expect(resolveRegexWorkerHeap(execArgv, undefined)).toEqual({
|
|
kind: "unknown",
|
|
});
|
|
});
|
|
|
|
it("does not report the 128 MiB request for NODE_OPTIONS supplied by an env file", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-env-file-"));
|
|
const file = join(directory, "heap.env");
|
|
writeFileSync(file, "NODE_OPTIONS=--max-old-space-size=256\n");
|
|
try {
|
|
const fromFile = probeWorkerHeap([`--env-file=${file}`], undefined);
|
|
const ifExists = probeWorkerHeap(
|
|
[`--env-file-if-exists=${file}`],
|
|
undefined,
|
|
);
|
|
const small = probeWorkerHeap(["--max-old-space-size=64"], undefined);
|
|
const large = probeWorkerHeap(["--max-old-space-size=256"], undefined);
|
|
expect(fromFile.resourceOld).toBe(128);
|
|
expect(ifExists.resourceOld).toBe(128);
|
|
expect(small.resourceOld).toBe(128);
|
|
expect(large.resourceOld).toBe(128);
|
|
expect(relativeHeapGap(fromFile.heap, large.heap)).toBeLessThan(0.05);
|
|
expect(relativeHeapGap(ifExists.heap, large.heap)).toBeLessThan(0.05);
|
|
expect(fromFile.heap).toBeGreaterThan(small.heap);
|
|
expect(ifExists.heap).toBeGreaterThan(small.heap);
|
|
for (const execArgv of [
|
|
[`--env-file=${file}`],
|
|
[`--env-file-if-exists=${file}`],
|
|
[`--env-file=${file}`, "--max-old-space-size=64"],
|
|
])
|
|
expect(resolveRegexWorkerHeap(execArgv, undefined)).toEqual({
|
|
kind: "unknown",
|
|
});
|
|
expect(
|
|
resolveRegexWorkerHeap([`--env-file=${file}`], "--max-old-space-size=64"),
|
|
).toEqual({ kind: "effective", mib: 64, overridden: true });
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("does not report the 128 MiB request for a heap percentage in a Node config file", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-config-file-"));
|
|
const file = join(directory, "heap.config.json");
|
|
const config = '{"nodeOptions":{"max-old-space-size-percentage":"1"}}\n';
|
|
writeFileSync(file, config);
|
|
writeFileSync(join(directory, "node.config.json"), config);
|
|
try {
|
|
const fromFile = probeWorkerHeap(
|
|
[`--experimental-config-file=${file}`],
|
|
undefined,
|
|
);
|
|
const split = probeWorkerHeap(
|
|
["--experimental-config-file", file],
|
|
undefined,
|
|
);
|
|
const withAbsolute = probeWorkerHeap(
|
|
[`--experimental-config-file=${file}`, "--max-old-space-size=256"],
|
|
undefined,
|
|
);
|
|
const fromDefault = probeWorkerHeap(
|
|
["--experimental-default-config-file"],
|
|
undefined,
|
|
directory,
|
|
);
|
|
expect(fromFile.resourceOld).toBe(128);
|
|
expect(split.resourceOld).toBe(128);
|
|
expect(withAbsolute.resourceOld).toBe(128);
|
|
expect(fromDefault.resourceOld).toBe(128);
|
|
for (const execArgv of [
|
|
[`--experimental-config-file=${file}`],
|
|
["--experimental-config-file", file],
|
|
[`--experimental-config-file=${file}`, "--max-old-space-size=64"],
|
|
["--experimental-default-config-file"],
|
|
])
|
|
expect(resolveRegexWorkerHeap(execArgv, undefined)).toEqual({
|
|
kind: "unknown",
|
|
});
|
|
expect(
|
|
resolveRegexWorkerHeap(
|
|
["--experimental-config-file=node.config.json"],
|
|
"--max-old-space-size=64",
|
|
),
|
|
).toEqual({ kind: "unknown" });
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("does not report the 128 MiB request when --max-heap-size lowers the worker heap", () => {
|
|
const heap64 = probeWorkerHeap(["--max-heap-size=64"], undefined);
|
|
const underscore = probeWorkerHeap(["--max_heap_size=64"], undefined);
|
|
const old256 = probeWorkerHeap(["--max-old-space-size=256"], undefined);
|
|
expect(heap64.resourceOld).toBe(128);
|
|
expect(underscore.resourceOld).toBe(128);
|
|
expect(old256.resourceOld).toBe(128);
|
|
expect(heap64.heap).toBeLessThan(old256.heap);
|
|
expect(relativeHeapGap(underscore.heap, heap64.heap)).toBeLessThan(0.05);
|
|
expect(resolveRegexWorkerHeap(["--max-heap-size=64"], undefined)).toEqual({
|
|
kind: "unknown",
|
|
});
|
|
expect(resolveRegexWorkerHeap([], "--max-heap-size=64")).toEqual({
|
|
kind: "unknown",
|
|
});
|
|
expect(
|
|
resolveRegexWorkerHeap(
|
|
["--max-heap-size=64", "--max-old-space-size=256"],
|
|
undefined,
|
|
),
|
|
).toEqual({ kind: "unknown" });
|
|
});
|
|
|
|
it("lets a launch --max-old-space-size override the 128 MiB worker request", () => {
|
|
const smaller = probeWorkerHeap(["--max-old-space-size=64"], undefined);
|
|
const larger = probeWorkerHeap([], "--max-old-space-size=256");
|
|
expect(smaller.resourceOld).toBe(128);
|
|
expect(larger.resourceOld).toBe(128);
|
|
expect(smaller.heap).toBeLessThan(larger.heap);
|
|
});
|
|
|
|
const assertUnrecoverableOffLinux = (): boolean => {
|
|
if (process.platform === "linux") return false;
|
|
const launchExecArgv = readLaunchExecArgv();
|
|
expect(launchExecArgv).toEqual({ kind: "unrecoverable" });
|
|
expect(
|
|
resolveLaunchRegexWorkerHeap(launchExecArgv, {
|
|
kind: "recovered",
|
|
value: "--max-old-space-size=64",
|
|
}),
|
|
).toEqual({ kind: "unknown" });
|
|
return true;
|
|
};
|
|
|
|
const spawnCommandLineReport = (
|
|
directory: string,
|
|
args: readonly string[],
|
|
preloadBody: string | undefined,
|
|
): {
|
|
execLen: number;
|
|
cmdline: string;
|
|
heap: number;
|
|
resourceOld: number;
|
|
} => {
|
|
const script = join(directory, "report.cjs");
|
|
const launchArgs = [...args];
|
|
if (preloadBody !== undefined) {
|
|
const preload = join(directory, "preload.cjs");
|
|
writeFileSync(preload, preloadBody);
|
|
launchArgs.push(`--require=${preload}`);
|
|
}
|
|
writeFileSync(
|
|
script,
|
|
`const { Worker } = require("node:worker_threads");
|
|
const fs = require("node:fs");
|
|
const worker = new Worker(
|
|
\`const { parentPort, resourceLimits } = require("node:worker_threads");
|
|
const v8 = require("node:v8");
|
|
parentPort.postMessage({
|
|
resourceOld: resourceLimits.maxOldGenerationSizeMb,
|
|
heap: v8.getHeapStatistics().heap_size_limit,
|
|
});\`,
|
|
{ eval: true, resourceLimits: { maxOldGenerationSizeMb: 128 } },
|
|
);
|
|
worker.once("message", (message) => {
|
|
process.stdout.write(JSON.stringify({
|
|
execLen: process.execArgv.length,
|
|
cmdline: fs.readFileSync("/proc/self/cmdline").toString("base64"),
|
|
heap: message.heap,
|
|
resourceOld: message.resourceOld,
|
|
}));
|
|
});
|
|
worker.once("error", (error) => {
|
|
process.stderr.write(error.message);
|
|
process.exitCode = 1;
|
|
});
|
|
`,
|
|
);
|
|
launchArgs.push(script);
|
|
const result = spawnSync(process.execPath, launchArgs, { encoding: "utf8" });
|
|
expect(result.status).toBe(0);
|
|
return JSON.parse(result.stdout ?? "") as {
|
|
execLen: number;
|
|
cmdline: string;
|
|
heap: number;
|
|
resourceOld: number;
|
|
};
|
|
};
|
|
|
|
it("does not report the 128 MiB request after a preload clears process.execArgv", () => {
|
|
if (assertUnrecoverableOffLinux()) return;
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-exec-argv-"));
|
|
try {
|
|
const reported = spawnCommandLineReport(
|
|
directory,
|
|
["--max-old-space-size=64"],
|
|
"process.execArgv.length = 0;\n",
|
|
);
|
|
const preload = join(directory, "preload.cjs");
|
|
const pinned = probeWorkerHeap(["--max-old-space-size=64"], undefined);
|
|
const larger = probeWorkerHeap(["--max-old-space-size=256"], undefined);
|
|
expect(reported.execLen).toBe(0);
|
|
expect(reported.resourceOld).toBe(128);
|
|
expect(relativeHeapGap(reported.heap, pinned.heap)).toBeLessThan(0.05);
|
|
expect(reported.heap).toBeLessThan(larger.heap);
|
|
const recovered = execArgvFromCommandLine(
|
|
Buffer.from(reported.cmdline, "base64"),
|
|
);
|
|
expect(recovered).toEqual({
|
|
kind: "recovered",
|
|
value: ["--max-old-space-size=64", `--require=${preload}`],
|
|
});
|
|
expect(
|
|
resolveLaunchRegexWorkerHeap(recovered, {
|
|
kind: "recovered",
|
|
value: undefined,
|
|
}),
|
|
).toEqual({ kind: "effective", mib: 64, overridden: true });
|
|
expect(resolveRegexWorkerHeap([], undefined)).toEqual({
|
|
kind: "effective",
|
|
mib: 128,
|
|
overridden: false,
|
|
});
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("leaves the heap unknown when a preload title overwrites the command line", () => {
|
|
if (assertUnrecoverableOffLinux()) return;
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-title-"));
|
|
try {
|
|
const reported = spawnCommandLineReport(
|
|
directory,
|
|
["--max-old-space-size=64"],
|
|
'process.title = "rea";\n',
|
|
);
|
|
const pinned = probeWorkerHeap(["--max-old-space-size=64"], undefined);
|
|
expect(reported.resourceOld).toBe(128);
|
|
expect(relativeHeapGap(reported.heap, pinned.heap)).toBeLessThan(0.05);
|
|
const recovered = execArgvFromCommandLine(
|
|
Buffer.from(reported.cmdline, "base64"),
|
|
);
|
|
expect(recovered).toEqual({ kind: "unrecoverable" });
|
|
expect(
|
|
resolveLaunchRegexWorkerHeap(recovered, {
|
|
kind: "recovered",
|
|
value: undefined,
|
|
}),
|
|
).toEqual({ kind: "unknown" });
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("leaves the heap unknown when --title overwrites the command line", () => {
|
|
if (assertUnrecoverableOffLinux()) return;
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-title-flag-"));
|
|
try {
|
|
const reported = spawnCommandLineReport(
|
|
directory,
|
|
["--title=rea", "--max-old-space-size=64"],
|
|
undefined,
|
|
);
|
|
const pinned = probeWorkerHeap(["--max-old-space-size=64"], undefined);
|
|
expect(reported.resourceOld).toBe(128);
|
|
expect(relativeHeapGap(reported.heap, pinned.heap)).toBeLessThan(0.05);
|
|
const recovered = execArgvFromCommandLine(
|
|
Buffer.from(reported.cmdline, "base64"),
|
|
);
|
|
expect(recovered).toEqual({ kind: "unrecoverable" });
|
|
expect(
|
|
resolveLaunchRegexWorkerHeap(recovered, {
|
|
kind: "recovered",
|
|
value: undefined,
|
|
}),
|
|
).toEqual({ kind: "unknown" });
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("arms the worker timer with an overridden matching deadline", async () => {
|
|
const delays: number[] = [];
|
|
const original = global.setTimeout;
|
|
global.setTimeout = ((handler, timeout, ...args) => {
|
|
if (typeof timeout === "number") delays.push(timeout);
|
|
return original(handler, timeout, ...args);
|
|
}) as typeof setTimeout;
|
|
try {
|
|
expect(
|
|
await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: "^a",
|
|
caseSensitive: true,
|
|
values: ["a"],
|
|
deadline: { deadlineMs: 10_000, source: "setting" },
|
|
}),
|
|
).toEqual(ok([0]));
|
|
expect(delays).toContain(10_000);
|
|
expect(delays).not.toContain(5_000);
|
|
} finally {
|
|
global.setTimeout = original;
|
|
}
|
|
});
|
|
|
|
it("stops a slow match at the configured deadline and names the setting", async () => {
|
|
const started = performance.now();
|
|
const matched = await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: "^(a+)+$",
|
|
caseSensitive: true,
|
|
values: [`${"a".repeat(30)}!`],
|
|
deadline: { deadlineMs: 100, source: "setting" },
|
|
});
|
|
expect(performance.now() - started).toBeLessThan(4_000);
|
|
expect(matched.ok).toBe(false);
|
|
if (matched.ok) return;
|
|
const projected = projectAnalysisError(matched.error);
|
|
expect(projected).toMatchObject({
|
|
code: "resource_constraint",
|
|
details: {
|
|
resource: "cpu",
|
|
reported_limits: {
|
|
matching_deadline_ms: 100,
|
|
matching_deadline_source: "setting",
|
|
},
|
|
},
|
|
});
|
|
expect(projected.message).toContain(REGEX_MATCHING_DEADLINE_SETTING);
|
|
expect(projected.remediation.action).toContain(
|
|
REGEX_MATCHING_DEADLINE_SETTING,
|
|
);
|
|
expect(projected.remediation.action).not.toContain("no runtime override");
|
|
expect(matched.error).toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
resource: "cpu",
|
|
reportedLimits: {
|
|
matching_deadline_ms: 100,
|
|
matching_deadline_source: "setting",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("matches a trivial pattern in a real worker at the heap floor", () => {
|
|
const directory = mkdtempSync(join(tmpdir(), "rea-regex-heap-floor-"));
|
|
const probe = join(directory, "probe.mjs");
|
|
const repository = fileURLToPath(new URL("../../../", import.meta.url));
|
|
const tsxApi = pathToFileURL(
|
|
join(repository, "node_modules/tsx/dist/esm/api/index.mjs"),
|
|
).href;
|
|
const matcher = pathToFileURL(
|
|
join(repository, "src/process/RegexMatcher.ts"),
|
|
).href;
|
|
// Plain Node, with NODE_OPTIONS removed. Do not put `--import tsx` on this
|
|
// process: workers inherit execArgv, and that flag exhausts a floor-sized heap.
|
|
writeFileSync(
|
|
probe,
|
|
`import { tsImport } from ${JSON.stringify(tsxApi)};
|
|
const { matchRegexValues } = await tsImport(${JSON.stringify(matcher)}, import.meta.url);
|
|
const result = await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: "^a",
|
|
caseSensitive: true,
|
|
values: ["a"],
|
|
heapRequest: {
|
|
heapMiB: ${String(MINIMUM_REGEX_WORKER_HEAP_REQUEST_MIB)},
|
|
source: "setting",
|
|
},
|
|
});
|
|
process.stdout.write(JSON.stringify(result.ok
|
|
? { ok: true, indices: result.value }
|
|
: { ok: false, message: String(result.error?.message ?? "") }));
|
|
if (!result.ok) process.exitCode = 1;
|
|
`,
|
|
);
|
|
const env = { ...process.env };
|
|
delete env.NODE_OPTIONS;
|
|
try {
|
|
const result = spawnSync(process.execPath, [probe], {
|
|
env,
|
|
encoding: "utf8",
|
|
timeout: 30_000,
|
|
});
|
|
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
|
|
expect(JSON.parse(result.stdout)).toEqual({ ok: true, indices: [0] });
|
|
} finally {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("preserves Unicode matching order and refuses a pre-cancelled match", async () => {
|
|
expect(
|
|
await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: "^δ",
|
|
caseSensitive: false,
|
|
values: ["Δelta", "delta", "δίκη"],
|
|
}),
|
|
).toEqual(ok([0, 2]));
|
|
expect(
|
|
await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: ".",
|
|
caseSensitive: true,
|
|
values: ["value"],
|
|
signal: AbortSignal.abort(),
|
|
}),
|
|
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
|
|
});
|
|
|
|
it("names the rejected pattern field for a default invalid-pattern refusal", async () => {
|
|
const matched = await matchRegexValues({
|
|
operation: "search_procedures",
|
|
pattern: "[",
|
|
caseSensitive: false,
|
|
values: ["value"],
|
|
});
|
|
expect(matched.ok).toBe(false);
|
|
if (matched.ok) return;
|
|
expect(projectAnalysisError(matched.error)).toMatchObject({
|
|
code: "invalid_request",
|
|
category: "invalid_input",
|
|
recovery: {
|
|
kind: "inspect_input_contract",
|
|
operation: "search_procedures",
|
|
},
|
|
details: {
|
|
operation: "search_procedures",
|
|
issues: [
|
|
{
|
|
path: ["pattern"],
|
|
reason: "invalid_format",
|
|
message: "Supply a valid regular expression.",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps Hopper's result projection and invalid-pattern contract with the shared worker", async () => {
|
|
const search = new HopperRegexSearch({
|
|
callTool: async () =>
|
|
ok([
|
|
{ address: "0x10", value: "Δelta" },
|
|
{ address: "0x20", value: "other" },
|
|
]),
|
|
});
|
|
expect(
|
|
await search.execute("search_strings", { pattern: "^δ", mode: "regex" }),
|
|
).toEqual(ok([{ address: "0x10", value: "Δelta" }]));
|
|
expect(
|
|
await search.execute("search_strings", { pattern: "[", mode: "regex" }),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "HopperRemoteError",
|
|
code: -32000,
|
|
diagnosticType: "invalid_request",
|
|
operation: "search_strings",
|
|
},
|
|
});
|
|
await search.close();
|
|
});
|