mirror of
https://github.com/morluto/rea
synced 2026-10-27 22:45:19 -04:00
perf(javascript): reuse the source-analysis worker within a process (#5819)
* perf(javascript): reuse the source-analysis worker within a process Each analyze-javascript-application call started a new Node worker. That startup, not fixture IO or the shared CLI session, dominated the slow loader-review and factory-completion files. Park one healthy worker on the artifact resource scope and on the CLI process. Stop it when the scope, session, or one-shot process ends. Do not reuse after failure, cancellation, or a different heap or timeout. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(cli): close the JavaScript worker when dispatch throws Minimal dispatcher runtimes now export a no-op closeCliJavaScriptResources, and CLI dispatch plus output rendering always await that cleanup. A dispatch failure and a cleanup failure are both reported. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(artifacts): report close-hook failures with retained cleanup When retained cleanup and the scope close hook both fail, the close error keeps both causes and both resource labels. The pending owners and the hook stay registered so the next close retries each path. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(javascript): stop a replaced analysis worker before the next one starts A heap or timeout change now closes the parked worker before constructing its replacement. The scope retains that process only when the close fails. Co-authored-by: morluto <morluto@users.noreply.github.com> * style: format worker-reuse close and launch helpers Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(cli): report a failed JavaScript worker shutdown before exit Share one in-flight close and drop the CLI scope only after it succeeds, so a later caller retries a worker that is still owned. The one-shot exit callback writes that failure and exits non-zero, while a closed stdout pipe still exits 141. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(javascript): keep a worker parked during an overlapping limit change Claim the scope slot only when it is still empty after the replaced worker closes. A concurrent analysis that parked another healthy worker keeps that process, and the extra worker is closed through its own port. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(artifacts): run later close hooks when an earlier hook fails Run every registered close hook on each close. One failure is returned unchanged, and several failures are aggregated so a later hook is not skipped. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(javascript): fail a limit change when the previous worker stays alive A failed close used to be retained and then ignored, so MCP could return a successful analysis while the old process and its replacement were both owned. The request now fails before the replacement is constructed, and the still-live worker stays owned for a later close. 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>
This commit is contained in:
@@ -3,10 +3,153 @@ import type {
|
||||
JavaScriptSourceAnalysisPort,
|
||||
} from "../../domain/javascript/javascriptSourceAnalysis.js";
|
||||
import { JavaScriptAnalysisWorker } from "../../javascript/analysis/JavaScriptAnalysisWorker.js";
|
||||
import type { spawnOwnedProviderProcess } from "../../process/ProviderProcess.js";
|
||||
import { ArtifactResourceScope } from "../ArtifactResourceScope.js";
|
||||
|
||||
interface RetainedJavaScriptSourceAnalysis {
|
||||
readonly worker: JavaScriptAnalysisWorker;
|
||||
readonly limits: JavaScriptSourceAnalysisLimits;
|
||||
busy: boolean;
|
||||
}
|
||||
|
||||
const retained = new WeakMap<
|
||||
ArtifactResourceScope,
|
||||
RetainedJavaScriptSourceAnalysis
|
||||
>();
|
||||
const closeHookRegistered = new WeakSet<ArtifactResourceScope>();
|
||||
|
||||
export interface JavaScriptSourceAnalysisAcquisition {
|
||||
/** Keep a healthy worker until this scope closes. */
|
||||
readonly scope?: ArtifactResourceScope;
|
||||
/** Test seam. Reuse keeps the process already launched for the scope. */
|
||||
readonly launcher?: typeof spawnOwnedProviderProcess;
|
||||
}
|
||||
|
||||
/** Construct the inert artifact analyzer; resources are acquired on the first source. */
|
||||
export const createJavaScriptSourceAnalysis = (
|
||||
limits: JavaScriptSourceAnalysisLimits,
|
||||
sourceCount: number,
|
||||
): JavaScriptSourceAnalysisPort =>
|
||||
new JavaScriptAnalysisWorker(limits, undefined, sourceCount);
|
||||
acquisition: JavaScriptSourceAnalysisAcquisition = {},
|
||||
): Promise<JavaScriptSourceAnalysisPort> => {
|
||||
if (acquisition.scope === undefined)
|
||||
return Promise.resolve(
|
||||
new JavaScriptAnalysisWorker(limits, acquisition.launcher, sourceCount),
|
||||
);
|
||||
return checkoutJavaScriptSourceAnalysis(
|
||||
acquisition.scope,
|
||||
limits,
|
||||
sourceCount,
|
||||
acquisition.launcher,
|
||||
);
|
||||
};
|
||||
|
||||
const checkoutJavaScriptSourceAnalysis = async (
|
||||
scope: ArtifactResourceScope,
|
||||
limits: JavaScriptSourceAnalysisLimits,
|
||||
sourceCount: number,
|
||||
launcher: typeof spawnOwnedProviderProcess | undefined,
|
||||
): Promise<JavaScriptSourceAnalysisPort> => {
|
||||
registerJavaScriptSourceAnalysisClose(scope);
|
||||
const current = retained.get(scope);
|
||||
if (current !== undefined && !current.busy) {
|
||||
if (
|
||||
sameLimits(current.limits, limits) &&
|
||||
current.worker.prepareForAnotherAnalysis(sourceCount)
|
||||
) {
|
||||
current.busy = true;
|
||||
return new ScopeJavaScriptSourceAnalysis(scope, current.worker);
|
||||
}
|
||||
retained.delete(scope);
|
||||
try {
|
||||
await current.worker.close();
|
||||
} catch (cause) {
|
||||
// The old process is still alive. Keep it owned and fail this request
|
||||
// before another worker is constructed, including on MCP where the
|
||||
// analysis result is returned without a later cleanup retry.
|
||||
if (retained.get(scope) === undefined) {
|
||||
retained.set(scope, {
|
||||
worker: current.worker,
|
||||
limits: current.limits,
|
||||
busy: false,
|
||||
});
|
||||
} else {
|
||||
scope.retain({
|
||||
kind: "lease",
|
||||
resource: "replaced JavaScript source analyzer",
|
||||
lease: { release: () => current.worker.close() },
|
||||
});
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
const worker = new JavaScriptAnalysisWorker(limits, launcher, sourceCount);
|
||||
// A concurrent analysis may have parked another healthy worker while the
|
||||
// replaced process was closing. Claim the slot only when it is still empty.
|
||||
// This extra process is closed through the port returned below.
|
||||
if (retained.get(scope) === undefined)
|
||||
retained.set(scope, { worker, limits, busy: true });
|
||||
return new ScopeJavaScriptSourceAnalysis(scope, worker);
|
||||
};
|
||||
|
||||
const registerJavaScriptSourceAnalysisClose = (
|
||||
scope: ArtifactResourceScope,
|
||||
): void => {
|
||||
if (closeHookRegistered.has(scope)) return;
|
||||
closeHookRegistered.add(scope);
|
||||
scope.addCloseHook(async () => {
|
||||
const current = retained.get(scope);
|
||||
if (current === undefined) return;
|
||||
await current.worker.close();
|
||||
if (retained.get(scope) === current) retained.delete(scope);
|
||||
});
|
||||
};
|
||||
|
||||
const sameLimits = (
|
||||
left: JavaScriptSourceAnalysisLimits,
|
||||
right: JavaScriptSourceAnalysisLimits,
|
||||
): boolean =>
|
||||
left.heapMb === right.heapMb && left.timeoutMs === right.timeoutMs;
|
||||
|
||||
/**
|
||||
* Park a healthy worker on its scope. The next checkout with the same limits
|
||||
* reuses that process; scope close is what actually stops it.
|
||||
*/
|
||||
class ScopeJavaScriptSourceAnalysis implements JavaScriptSourceAnalysisPort {
|
||||
#failed = false;
|
||||
#closed = false;
|
||||
|
||||
constructor(
|
||||
private readonly scope: ArtifactResourceScope,
|
||||
private readonly worker: JavaScriptAnalysisWorker,
|
||||
) {}
|
||||
|
||||
analyze(
|
||||
...args: Parameters<JavaScriptSourceAnalysisPort["analyze"]>
|
||||
): ReturnType<JavaScriptSourceAnalysisPort["analyze"]> {
|
||||
return this.worker.analyze(...args).then((result) => {
|
||||
if (!result.ok) this.#failed = true;
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.#closed) return;
|
||||
this.#closed = true;
|
||||
try {
|
||||
const current = retained.get(this.scope);
|
||||
if (
|
||||
current?.worker === this.worker &&
|
||||
!this.#failed &&
|
||||
this.worker.canReuse()
|
||||
) {
|
||||
current.busy = false;
|
||||
return;
|
||||
}
|
||||
if (current?.worker === this.worker) retained.delete(this.scope);
|
||||
await this.worker.close();
|
||||
} catch (cause) {
|
||||
this.#closed = false;
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user