mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
When the retained-Evidence cache could not keep an over-budget result, the CLI delivered its summary with rerun arguments. Rerunning a non-idempotent operation does not reproduce it: an extraction that created its output root refuses the same root. Each CLI delivery now knows whether its contract is idempotent, and a non-idempotent result the cache does not keep is delivered complete. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
150 lines
4.9 KiB
TypeScript
150 lines
4.9 KiB
TypeScript
import type { Logger } from "pino";
|
|
|
|
import { captureBrowserScenario } from "../application/BrowserScenarioCaptureService.js";
|
|
import type { BrowserScenarioCapturePort } from "../ports/BrowserScenarioCapturePort.js";
|
|
import { publishCliAnalysisEvidence } from "./analysisEvidenceDelivery.js";
|
|
import { contractNamed } from "./contractCliDescription.js";
|
|
import { registerContractDocumentCommand } from "./contractDocumentCommand.js";
|
|
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
|
import type { CliInstance } from "./types.js";
|
|
import { CLI_COMMANDS } from "../contracts/cliCommandNames.js";
|
|
import { BROWSER_SCENARIO_TOOL_CONTRACTS } from "../contracts/browserScenarioToolContracts.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import {
|
|
browserScenarioSchema,
|
|
type BrowserScenario,
|
|
} from "../domain/browserScenario.js";
|
|
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
|
|
import type { JsonValue } from "../domain/jsonValue.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
|
|
const contract = contractNamed(
|
|
BROWSER_SCENARIO_TOOL_CONTRACTS,
|
|
"capture_browser_scenario",
|
|
);
|
|
const PROVIDER_ID = "rea-playwright-browser-scenario";
|
|
const document =
|
|
"Inline browser scenario JSON, JSON file path, or '-' for stdin";
|
|
const callerPaths = [["browser", "executable_path"]] as const;
|
|
|
|
/** Register controlled browser execution and offline validation commands. */
|
|
export const registerBrowserScenarioCommands = (
|
|
cli: CliInstance,
|
|
logger: Logger,
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
options: {
|
|
readonly createProvider?: (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
) => BrowserScenarioCapturePort;
|
|
} = {},
|
|
): void => {
|
|
registerContractDocumentCommand(
|
|
cli,
|
|
logger,
|
|
contract,
|
|
{
|
|
description:
|
|
"Run one bounded Playwright browser scenario and return Evidence",
|
|
document,
|
|
callerPaths,
|
|
evidenceDelivery: true,
|
|
},
|
|
async (input, { signal, operation, delivery, spelled }) => {
|
|
if (delivery === undefined)
|
|
throw new Error(`${operation} declares evidenceDelivery`);
|
|
const scenario = parseBrowserScenario(
|
|
input,
|
|
spelled,
|
|
"Browser scenario capture failed",
|
|
);
|
|
if (!scenario.ok) return scenario.error;
|
|
// Load Playwright only for this command, not during CLI startup.
|
|
const provider =
|
|
options.createProvider === undefined
|
|
? await import("../composition/browserScenario.js").then(
|
|
({ createBrowserScenarioProvider }) =>
|
|
createBrowserScenarioProvider(environment),
|
|
)
|
|
: options.createProvider(environment);
|
|
const result = await finishAnalysisCommand(
|
|
() => captureBrowserScenario(provider, scenario.value, { signal }),
|
|
{
|
|
close: async () => {
|
|
await provider?.close?.();
|
|
},
|
|
},
|
|
PROVIDER_ID,
|
|
operation,
|
|
signal,
|
|
undefined,
|
|
{ failureProjection: "initial" },
|
|
);
|
|
if (!result.ok)
|
|
return { error: "Browser scenario capture failed", ...result.error };
|
|
return publishCliAnalysisEvidence(result, {
|
|
operation,
|
|
signal,
|
|
detail: delivery.detail,
|
|
evidenceOutput: delivery.evidenceOutput,
|
|
rerunRecoversResult: delivery.rerunRecoversResult,
|
|
});
|
|
},
|
|
);
|
|
// Validation admits the capture document without provider or environment
|
|
// access.
|
|
registerContractDocumentCommand(
|
|
cli,
|
|
logger,
|
|
contract,
|
|
{
|
|
command: CLI_COMMANDS.validateBrowserScenario,
|
|
description:
|
|
"Validate one browser scenario without launching or attaching to a browser",
|
|
document,
|
|
callerPaths,
|
|
omit: {
|
|
detail:
|
|
"validation runs no browser and delivers no Evidence; pass --detail to capture-browser-scenario",
|
|
},
|
|
},
|
|
async (input, { spelled }) => {
|
|
const scenario = parseBrowserScenario(
|
|
input,
|
|
spelled,
|
|
"Browser scenario validation failed",
|
|
);
|
|
if (!scenario.ok) return scenario.error;
|
|
return {
|
|
valid: true,
|
|
actions: scenario.value.actions.length,
|
|
secrets: scenario.value.secrets.length,
|
|
};
|
|
},
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Both commands report the canonical scenario parser's field diagnostics,
|
|
* citing the document as the caller spelled it.
|
|
*/
|
|
const parseBrowserScenario = (
|
|
input: unknown,
|
|
spelled: unknown,
|
|
headline: string,
|
|
): Result<BrowserScenario, JsonValue> => {
|
|
const scenario = browserScenarioSchema.safeParse(input);
|
|
return scenario.success
|
|
? ok(scenario.data)
|
|
: err({
|
|
error: headline,
|
|
...projectAnalysisError(
|
|
analysisInputErrorFromIssues(
|
|
contract.name,
|
|
scenario.error.issues,
|
|
spelled,
|
|
{ cause: scenario.error },
|
|
),
|
|
),
|
|
});
|
|
};
|