Files
reverse-engineering-anything/src/process/RegexMatcherReply.test.ts
T
dcf7e25d09 perf(process): pool regex workers across match calls (#14114)
* perf(process): pool regex workers across match calls

matchRegexValues spawned a fresh worker_threads Worker per call (~10.5 ms measured, vs ~0.9 ms pooled for a trivial match on 1-5k candidates). Workers are now pooled by heap class (max 4, 10 s idle reap, unref'd while idle): the worker protocol is message-driven, a worker is reused only after a clean matched/invalid-pattern reply, and deadline, abort, error, exit or malformed replies retire it so a stuck pattern can never poison the pool. Queueing for a busy pool counts toward the caller's deadline. Results, heap and deadline errors, and cancellation semantics are unchanged.

Closes #14110. Verified: RegexMatcher + RegexMatcherReply + boundary regexMatcher + HopperRegexSearch + IdaOperationRunner tests (91+15), check:fast, and a 15-call benchmark (10.5 ms -> 0.9 ms per call).

* test(process): use the admitted deadline source spelling in the pool test

* fix(process): enforce queue deadlines, reclaim idle regex workers, and await retirement

Address pooled-worker review findings: the matching deadline now starts before pool acquisition (queue waiters expire and expired jobs are never posted), idle workers of another heap class are reclaimed instead of blocking a changed heap setting, queued worker startup failures reject into the normal Cannot-start path, and non-clean outcomes await worker termination before returning so close() cannot finish while matching work lives. Add RegexMatcherPool.test.ts with a construction-counting fake worker proving reuse across clean calls and termination-before-return plus replacement after a deadline.

* fix(process): allocate regex workers through one capacity path

Acquisition retired an idle worker and spawned directly, bypassing waiter order: retirement served an older waiter into the freed slot and acquisition then spawned a fifth worker. Acquisition now only takes an idle match or enqueues, and serveRegexWaiters owns every spawn/reclaim decision in waiter order. Add a mock-pool regression proving a fifth queued search deadlines at four constructions.

* fix(process): reclaim incompatible regex workers on one path

Keep a stopping worker counted until terminate settles, and let release
ask the shared allocator so a different heap does not wait out the idle
reap. A failed stop stays owned and is recorded beside the match error.

* fix(process): mark background regex-worker stops and release test gates

Typed lint rejects an unmarked beginStopRegexWorker promise, so the
allocator's fire-and-forget stops are explicit. Pool tests release held
workers in finally, including when an assertion fails first.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-23 18:19:56 +00:00

57 lines
1.7 KiB
TypeScript

import { EventEmitter } from "node:events";
import { expect, it, vi } from "vitest";
import { matchRegexValues } from "./RegexMatcher.js";
const { reply } = vi.hoisted(() => ({
reply: { value: undefined as unknown },
}));
vi.mock("node:worker_threads", () => ({
Worker: class extends EventEmitter {
postMessage(): void {
queueMicrotask(() => this.emit("message", reply.value));
}
ref(): void {}
unref(): void {}
terminate(): Promise<number> {
return Promise.resolve(0);
}
},
}));
it.each([
[{ status: "invalid_pattern", reason: 42 }, "reply is malformed"],
[
{ status: "invalid_pattern", reason: "invalid", extra: true },
"reply is malformed",
],
[{ status: "matched", wrong: [] }, "reply is malformed"],
[{ status: "matched", indices: [0], extra: true }, "reply is malformed"],
[{ status: "matched", indices: [-1] }, "reply is malformed"],
[{ status: "matched", indices: [0.5] }, "reply is malformed"],
[{ status: "matched", indices: [2, "0"] }, "reply is malformed"],
[{ status: "matched", indices: [2] }, "invalid inventory indices"],
[{ status: "matched", indices: [0, 0] }, "invalid inventory indices"],
[{ status: "matched", indices: [1, 0] }, "invalid inventory indices"],
] as const)(
"rejects a worker reply %j at the matching boundary",
async (message, reason) => {
reply.value = message;
await expect(
matchRegexValues({
operation: "search_procedures",
pattern: "main",
caseSensitive: true,
values: ["main", "domain"],
}),
).resolves.toMatchObject({
ok: false,
error: {
_tag: "AnalysisOutputError",
message: expect.stringContaining(reason),
},
});
},
);