Files
reverse-engineering-anything/src/application/ProcessPairedExperiment.ts
T
morlutoandDevin e91c8c39c2 refactor(test): colocate pure-domain tests and reduce test debt (#471)
* refactor(test): colocate pure-domain tests and reduce test debt

Move focused immutable domain/contracts/adapter tests from
tests/composition/** beside their production owners under src/** as
colocated *.test.ts/*.fixture.ts, and relocate persistence-shaped
composition tests under tests/boundary/filesystem/**. Update import
paths, the Vitest project topology (shared worker context for pure
domain/contracts and recording-port service projects), the test
migration ledger, testing docs, and AGENTS.md to match.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: remove test migration ledger

The cutover is complete and the ledger is no longer referenced anywhere
in source, scripts, or the docs gate. Drop the standalone artifact.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs: clean up stale migration artifacts

Remove empty directories left by the test migration and replace the
"final cutover" language in docs/testing.md with standing PR guidance
that no longer references the completed migration.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: repair CI failures from schema, knip, and stale generated artifacts

Three root causes fixed:

1. catalog-schemas.mjs: valuesAt only traversed anyOf from Zod's
   toJSONSchema, not oneOf. The webScreenshotDiffSchema change to a
   discriminated union produces oneOf, so the product catalog generator
   could not derive the web_screenshot_diff schema version. Add oneOf
   traversal.

2. conformancePackage.ts: the test rewrite removed the only imports of
   ScenarioManifest, ReplayPlan, ShimPlan, and ExpectedEvidence, making
   them unused exports that knip flags. Remove the dead type aliases.

3. Regenerate stale artifacts (product catalog, MCP tool catalog, skill
   metadata, conformance manifest) that drifted from the source changes.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-08-10 14:57:44 +08:00

153 lines
5.2 KiB
TypeScript

import {
processCaptureCancelled,
type ProcessCaptureError,
} from "./ProcessCaptureError.js";
import { captureProcessScenario } from "./ProcessHarness.js";
import type {
ProcessCapture,
ProcessExecutionPolicy,
ProcessScenario,
} from "../domain/processCapture.js";
import { compareProcessCaptures } from "../domain/processComparison.js";
import { PROCESS_COMPARISON_DIMENSIONS } from "../domain/processComparison.js";
import {
analyzeProcessRepeatability,
bindProcessScenario,
type ProcessPairedExperiment,
} from "../domain/processPairedExperiment.js";
import { err, ok, type Result } from "../domain/result.js";
/** Capture seam used by paired process experiments. */
export interface ProcessCapturePort {
capture(
scenario: ProcessScenario,
policy: ProcessExecutionPolicy,
signal?: AbortSignal,
): Promise<Result<ProcessCapture, ProcessCaptureError>>;
}
interface PairedProcessExperimentEvidence {
readonly authority_runs: readonly ProcessCapture[];
readonly candidate_runs: readonly ProcessCapture[];
readonly authority_repeatability: ReturnType<
typeof analyzeProcessRepeatability
>;
readonly candidate_repeatability: ReturnType<
typeof analyzeProcessRepeatability
>;
}
/** Repeatability evidence and either a stable comparison or its blocking reason. */
export type PairedProcessExperimentResult = PairedProcessExperimentEvidence &
(
| {
readonly cross_side: ReturnType<typeof compareProcessCaptures>;
readonly cross_side_blocked_reason: null;
}
| {
readonly cross_side: null;
readonly cross_side_blocked_reason: string;
}
);
const productionCapturePort: ProcessCapturePort = {
capture: captureProcessScenario,
};
const captureRepeats = async (input: {
readonly scenario: ProcessScenario;
readonly repeatCount: number;
readonly policy: ProcessExecutionPolicy;
readonly port: ProcessCapturePort;
readonly signal?: AbortSignal;
}): Promise<Result<readonly ProcessCapture[], ProcessCaptureError>> => {
const captures: ProcessCapture[] = [];
for (let index = 0; index < input.repeatCount; index += 1) {
if (input.signal?.aborted === true) return err(processCaptureCancelled());
const capture = await input.port.capture(
input.scenario,
input.policy,
input.signal,
);
if (!capture.ok) return err(capture.error);
captures.push(capture.value);
}
return ok(captures);
};
/** Execute repeatability-first authority/candidate captures under one contract. */
export const runPairedProcessExperiment = async (
experiment: ProcessPairedExperiment,
policy: ProcessExecutionPolicy,
options: {
readonly signal?: AbortSignal;
readonly capturePort?: ProcessCapturePort;
readonly now?: () => number;
} = {},
): Promise<Result<PairedProcessExperimentResult, ProcessCaptureError>> => {
const port = options.capturePort ?? productionCapturePort;
const authorityScenario = bindProcessScenario(
experiment.shared_scenario,
experiment.authority,
);
const candidateScenario = bindProcessScenario(
experiment.shared_scenario,
experiment.candidate,
);
const authorityRuns = await captureRepeats({
scenario: authorityScenario,
repeatCount: experiment.repeat_count,
policy,
port,
...(options.signal === undefined ? {} : { signal: options.signal }),
});
if (!authorityRuns.ok) return authorityRuns;
const candidateRuns = await captureRepeats({
scenario: candidateScenario,
repeatCount: experiment.repeat_count,
policy,
port,
...(options.signal === undefined ? {} : { signal: options.signal }),
});
if (!candidateRuns.ok) return candidateRuns;
const authorityRepeatability = analyzeProcessRepeatability(
authorityRuns.value,
experiment.required_dimensions,
);
const candidateRepeatability = analyzeProcessRepeatability(
candidateRuns.value,
experiment.required_dimensions,
);
const stable = authorityRepeatability.stable && candidateRepeatability.stable;
const allDimensionsRequired = PROCESS_COMPARISON_DIMENSIONS.every(
(dimension) => experiment.required_dimensions.includes(dimension),
);
const authorityLatest = authorityRuns.value.at(-1);
const candidateLatest = candidateRuns.value.at(-1);
const crossSideOutcome =
stable &&
allDimensionsRequired &&
authorityLatest !== undefined &&
candidateLatest !== undefined
? {
cross_side: compareProcessCaptures(authorityLatest, candidateLatest, {
maxCaptureAgeMs: experiment.freshness_policy.max_capture_age_ms,
...(options.now === undefined ? {} : { now: options.now }),
}),
cross_side_blocked_reason: null,
}
: {
cross_side: null,
cross_side_blocked_reason: !allDimensionsRequired
? "Cross-side comparison is blocked until every returned dimension is required and repeatable."
: "Cross-side comparison is blocked because a required same-side dimension is unstable.",
};
return ok({
authority_runs: authorityRuns.value,
candidate_runs: candidateRuns.value,
authority_repeatability: authorityRepeatability,
candidate_repeatability: candidateRepeatability,
...crossSideOutcome,
});
};