mirror of
https://github.com/morluto/rea
synced 2026-10-27 06:33:32 -04:00
0ecf3f2438
* fix(build): emit no runtime imports for type-only bindings
Under verbatimModuleSyntax, an import whose specifiers all carry inline
`type` modifiers compiles to `import {} from "..."`, a side-effect import
that still resolves and evaluates the module. 78 compiled modules carried
one, including `import {} from "playwright-core"` in the browser scenario
session, which loaded Playwright's module graph at MCP startup.
Use top-level `import type` for these bindings and enable oxlint's
typescript/no-import-type-side-effects rule so the pattern cannot return.
Refs #1685
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* perf(mcp): load Playwright when a scenario launches, not at startup
The browser scenario and Electron active providers are constructed before
the stdio server serves, and both bound Playwright's launchers at module
load. Resolve `chromium` and `_electron` from playwright-core when a
capture opens instead, as the web module resolver already does.
MCP startup now resolves 1,312 modules (4,902 resolutions) instead of
1,598 (6,076) before reading the first request, and no longer loads
playwright-core. Provider construction, the tool catalog, and capability
inventory are unchanged.
A boundary test spawns the stdio server, initializes, lists tools, and
asserts playwright-core and isomorphic-git were not resolved; the CLI
probe now exercises the real launch path to prove detection.
Closes #1685
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
5.2 KiB
TypeScript
151 lines
5.2 KiB
TypeScript
import type { AnalysisProfileCommitment } from "../../domain/analysisProfile.js";
|
|
import type { BinaryTarget } from "../../domain/binaryTargetTypes.js";
|
|
import { AnalysisCapabilityUnavailableError } from "../../domain/analysisErrorCore.js";
|
|
import { NoBinaryOpenError } from "../../domain/configurationErrors.js";
|
|
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
|
|
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
|
import type { JsonValue } from "../../domain/jsonValue.js";
|
|
import type { EvidenceSubjectTarget } from "../../domain/evidence.js";
|
|
import { err, ok, type Result } from "../../domain/result.js";
|
|
import type {
|
|
AnalysisClient,
|
|
AnalysisExecution,
|
|
AnalysisOperation,
|
|
CapabilityDescriptor,
|
|
} from "../AnalysisProvider.js";
|
|
import { isSnapshotCacheable } from "./AnalysisSnapshotCache.js";
|
|
import type { SessionProviderRoute } from "./SessionProviderRouter.js";
|
|
|
|
interface ActiveExecutionBinding {
|
|
readonly target: BinaryTarget;
|
|
readonly client: AnalysisClient;
|
|
readonly profile: AnalysisProfileCommitment | null;
|
|
readonly route: SessionProviderRoute;
|
|
}
|
|
|
|
export interface PreparedSessionExecution {
|
|
readonly active: ActiveExecutionBinding;
|
|
readonly capability: CapabilityDescriptor;
|
|
readonly profile: AnalysisProfileCommitment | undefined;
|
|
readonly cacheable: boolean;
|
|
readonly cached: AnalysisExecution | undefined;
|
|
}
|
|
|
|
interface PrepareSessionExecutionInput {
|
|
readonly active: ActiveExecutionBinding | undefined;
|
|
readonly operation: AnalysisOperation;
|
|
readonly parameters: Readonly<Record<string, JsonValue>>;
|
|
readonly unboundOperationError: (
|
|
operation: AnalysisOperation,
|
|
route: SessionProviderRoute,
|
|
) => AnalysisError | undefined;
|
|
readonly lookupSnapshot: (
|
|
target: BinaryTarget,
|
|
profile: AnalysisProfileCommitment,
|
|
operation: AnalysisOperation,
|
|
parameters: Readonly<Record<string, JsonValue>>,
|
|
) => AnalysisExecution | undefined;
|
|
}
|
|
|
|
/** Resolve capability, profile, and snapshot state before invoking a provider. */
|
|
export const prepareSessionExecution = (
|
|
input: PrepareSessionExecutionInput,
|
|
): Result<PreparedSessionExecution, AnalysisError> => {
|
|
const {
|
|
active,
|
|
operation,
|
|
parameters,
|
|
unboundOperationError,
|
|
lookupSnapshot,
|
|
} = input;
|
|
if (active === undefined) return err(new NoBinaryOpenError());
|
|
const capability = active.route.capabilities.get(operation);
|
|
if (capability?.available !== true) {
|
|
const selectionError = unboundOperationError(operation, active.route);
|
|
if (selectionError !== undefined) return err(selectionError);
|
|
return err(
|
|
new AnalysisCapabilityUnavailableError(
|
|
active.route.binding?.identity.id ?? active.route.identity.id,
|
|
operation,
|
|
capability?.reason ?? "operation is not declared by this provider",
|
|
),
|
|
);
|
|
}
|
|
const profile =
|
|
active.profile !== null &&
|
|
capability.provider.id === active.profile.provider.id
|
|
? active.profile
|
|
: undefined;
|
|
const cacheable =
|
|
profile !== undefined &&
|
|
isSnapshotCacheable(operation, capability, parameters);
|
|
const cached = cacheable
|
|
? lookupSnapshot(active.target, profile, operation, parameters)
|
|
: undefined;
|
|
return ok({ active, capability, profile, cacheable, cached });
|
|
};
|
|
|
|
/** Attach the selected profile only when the provider identity agrees. */
|
|
export const commitExecutionProfile = (
|
|
operation: AnalysisOperation,
|
|
result: Result<AnalysisExecution, AnalysisError>,
|
|
profile: AnalysisProfileCommitment | undefined,
|
|
): Result<AnalysisExecution, AnalysisError> => {
|
|
if (!result.ok || profile === undefined) return result;
|
|
const provider = result.value.provider;
|
|
if (
|
|
provider.id !== profile.provider.id ||
|
|
provider.name !== profile.provider.name ||
|
|
provider.version !== profile.provider.version
|
|
)
|
|
return err(
|
|
new ProviderAdapterError(profile.provider.id, `${operation}:profile`),
|
|
);
|
|
return ok({
|
|
...result.value,
|
|
analysisProfile: structuredClone(profile),
|
|
});
|
|
};
|
|
|
|
/** Bind an execution to the selected target, preserving valid artifact subjects. */
|
|
export const bindExecutionTarget = (
|
|
result: Result<AnalysisExecution, AnalysisError>,
|
|
operation: AnalysisOperation,
|
|
target: BinaryTarget,
|
|
): Result<AnalysisExecution, AnalysisError> => {
|
|
if (!result.ok) return result;
|
|
const subject: EvidenceSubjectTarget = {
|
|
path: target.path,
|
|
sha256: target.sha256,
|
|
format:
|
|
target.format === "analysis-database"
|
|
? "analysis-database"
|
|
: target.format,
|
|
...(target.architecture === undefined
|
|
? {}
|
|
: { architecture: target.architecture }),
|
|
};
|
|
if (
|
|
result.value.subject !== null &&
|
|
result.value.subject.sha256 !== target.sha256 &&
|
|
!isArtifactBundleSubject(operation, result.value.subject, target)
|
|
)
|
|
return err(
|
|
new ProviderAdapterError(
|
|
result.value.provider.id,
|
|
`${operation}:subject`,
|
|
),
|
|
);
|
|
return ok({ ...result.value, subject: result.value.subject ?? subject });
|
|
};
|
|
|
|
const isArtifactBundleSubject = (
|
|
operation: AnalysisOperation,
|
|
subject: EvidenceSubjectTarget,
|
|
target: BinaryTarget,
|
|
): boolean =>
|
|
(operation === "inventory_artifact" ||
|
|
operation === "inspect_artifact" ||
|
|
operation === "extract_artifact") &&
|
|
subject.path === (target.sourcePath ?? target.path);
|