mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
refactor(javascript): bind JavaScript source analysis in composition (#11467)
This commit is contained in:
1 parent
24406aa018
commit
64e6caeca2
22 files changed
+102
-34
No files matched your search
@@ -30,7 +30,7 @@ afterEach(() => {
|
||||
cleanup.denied = false;
|
||||
cleanup.onRemoval = undefined;
|
||||
});
|
||||
import { createJavaScriptSourceAnalysis } from "../../artifacts/javascript/JavaScriptSourceAnalysis.js";
|
||||
import { createJavaScriptSourceAnalysis } from "./JavaScriptSourceAnalysis.js";
|
||||
import { spawnOwnedProviderProcess } from "../../process/ProviderProcess.js";
|
||||
import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js";
|
||||
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
import { DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE } from "../../domain/javascript/javascriptSemanticWorkAllowance.js";
|
||||
import { resolveJavaScriptSemanticPrecision } from "../../domain/javascript/javascriptSemanticPrecision.js";
|
||||
import type {
|
||||
JavaScriptSourceAnalysisLimits,
|
||||
JavaScriptSourceAnalysisPort,
|
||||
} from "../../domain/javascript/javascriptSourceAnalysis.js";
|
||||
import { JavaScriptAnalysisWorker } from "./JavaScriptAnalysisWorker.js";
|
||||
import type { spawnOwnedProviderProcess } from "../../process/ProviderProcess.js";
|
||||
import { WorkerCleanupRegistry } from "../../process/OwnedWorkerRuntime.js";
|
||||
import { ArtifactResourceScope } from "../../resources/index.js";
|
||||
|
||||
interface RetainedJavaScriptSourceAnalysis {
|
||||
readonly worker: JavaScriptAnalysisWorker;
|
||||
readonly limits: JavaScriptSourceAnalysisLimits;
|
||||
busy: boolean;
|
||||
}
|
||||
|
||||
const retained = new WeakMap<
|
||||
ArtifactResourceScope,
|
||||
RetainedJavaScriptSourceAnalysis
|
||||
>();
|
||||
const closeHookRegistered = new WeakSet<ArtifactResourceScope>();
|
||||
const workerCleanups = new WeakMap<
|
||||
ArtifactResourceScope,
|
||||
WorkerCleanupRegistry
|
||||
>();
|
||||
|
||||
const workerCleanupFor = (
|
||||
scope: ArtifactResourceScope,
|
||||
): WorkerCleanupRegistry => {
|
||||
let cleanup = workerCleanups.get(scope);
|
||||
if (cleanup === undefined) {
|
||||
cleanup = new WorkerCleanupRegistry();
|
||||
workerCleanups.set(scope, cleanup);
|
||||
}
|
||||
return cleanup;
|
||||
};
|
||||
|
||||
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,
|
||||
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,
|
||||
workerCleanupFor(scope),
|
||||
);
|
||||
// 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);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether a parked worker was built for the same result-affecting choices. Its
|
||||
* requests carry the limits it was built with, so every choice that changes a
|
||||
* result belongs here, including the member-mutation allowance.
|
||||
*/
|
||||
const sameLimits = (
|
||||
left: JavaScriptSourceAnalysisLimits,
|
||||
right: JavaScriptSourceAnalysisLimits,
|
||||
): boolean => {
|
||||
const leftAllowance =
|
||||
left.memberMutationAllowance ??
|
||||
DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE;
|
||||
const rightAllowance =
|
||||
right.memberMutationAllowance ??
|
||||
DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALLOWANCE;
|
||||
if (
|
||||
leftAllowance.aliasStates !== rightAllowance.aliasStates ||
|
||||
leftAllowance.source !== rightAllowance.source ||
|
||||
left.heapMb !== right.heapMb ||
|
||||
left.timeoutMs !== right.timeoutMs ||
|
||||
left.heapSource !== right.heapSource ||
|
||||
left.timeoutSource !== right.timeoutSource
|
||||
)
|
||||
return false;
|
||||
const leftPrecision = resolveJavaScriptSemanticPrecision(
|
||||
left.semanticPrecision,
|
||||
);
|
||||
const rightPrecision = resolveJavaScriptSemanticPrecision(
|
||||
right.semanticPrecision,
|
||||
);
|
||||
return Object.entries(leftPrecision).every(
|
||||
([key, value]) =>
|
||||
rightPrecision[key as keyof typeof rightPrecision] === value,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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