feat(process): compare declared concurrent traces
This commit is contained in:
@@ -3,12 +3,15 @@ import { readFile } from "node:fs/promises";
|
||||
import { parseConfig } from "../config.js";
|
||||
import {
|
||||
AnalysisError,
|
||||
AnalysisInputError,
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
projectAnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import { createEvidence, parseEvidence } from "../domain/evidence.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { projectInputIssues } from "../domain/inputIssueProjection.js";
|
||||
import { processTraceSpecificationSchema } from "../domain/processTraceComparison.js";
|
||||
import {
|
||||
compareProcessCaptures,
|
||||
LEGACY_PROCESS_CAPTURE_MESSAGE,
|
||||
@@ -76,24 +79,41 @@ export const captureProcessScenarioFile = async (path: string) => {
|
||||
export const compareProcessEvidenceFiles = async (
|
||||
leftPath: string,
|
||||
rightPath: string,
|
||||
traceSpecPath?: string,
|
||||
) => {
|
||||
try {
|
||||
const left = parseCaptureEvidence(await readJson(leftPath));
|
||||
const right = parseCaptureEvidence(await readJson(rightPath));
|
||||
const comparison = compareProcessCaptures(left.capture, right.capture);
|
||||
const traceSpecification =
|
||||
traceSpecPath === undefined
|
||||
? undefined
|
||||
: parseTraceSpecification(await readJson(traceSpecPath));
|
||||
const comparison =
|
||||
traceSpecification === undefined
|
||||
? compareProcessCaptures(left.capture, right.capture)
|
||||
: compareProcessCaptures(left.capture, right.capture, {
|
||||
traceSpecification,
|
||||
});
|
||||
return createEvidence(undefined, PROCESS_PROVIDER, {
|
||||
predicateType: "rea.process-comparison/v3",
|
||||
predicateType:
|
||||
traceSpecification === undefined
|
||||
? "rea.process-comparison/v3"
|
||||
: "rea.process-comparison/v4",
|
||||
operation: "compare_process_captures",
|
||||
parameters: {
|
||||
left_evidence_id: left.id,
|
||||
right_evidence_id: right.id,
|
||||
left_normalization: left.capture.normalization,
|
||||
right_normalization: right.capture.normalization,
|
||||
...(traceSpecification === undefined
|
||||
? {}
|
||||
: { trace_spec: jsonValueSchema.parse(traceSpecification) }),
|
||||
},
|
||||
result: jsonValueSchema.parse(comparison),
|
||||
confidence: "derived",
|
||||
authority: "analyst-inference",
|
||||
limitations: comparison.limitations,
|
||||
locations: [...left.locations, ...right.locations],
|
||||
evidenceLinks: [left.id, right.id],
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
@@ -128,12 +148,23 @@ const parseCaptureEvidence = (input: unknown) => {
|
||||
return {
|
||||
id: evidence.evidence_id,
|
||||
capture: parseProcessCapture(evidence.normalized_result),
|
||||
locations: evidence.locations,
|
||||
};
|
||||
} catch {
|
||||
throw invalidCaptureEvidence();
|
||||
}
|
||||
};
|
||||
|
||||
const parseTraceSpecification = (input: unknown) => {
|
||||
const parsed = processTraceSpecificationSchema.safeParse(input);
|
||||
if (parsed.success) return parsed.data;
|
||||
throw new AnalysisInputError(
|
||||
"compare-process-captures",
|
||||
{ cause: parsed.error },
|
||||
projectInputIssues(parsed.error.issues, input),
|
||||
);
|
||||
};
|
||||
|
||||
const invalidCaptureEvidence = (): ProcessCliFailure =>
|
||||
new ProcessCliFailure(
|
||||
"invalid_input",
|
||||
|
||||
@@ -34,10 +34,14 @@ export const registerProcessCommands = (
|
||||
args: z.object({
|
||||
left: z.string().describe("Left capture Evidence JSON path"),
|
||||
right: z.string().describe("Right capture Evidence JSON path"),
|
||||
traceSpec: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Optional partial-order or finite-trace specification path"),
|
||||
}),
|
||||
run: ({ args }) =>
|
||||
logCliCommand(logger, "compare-process-captures", () =>
|
||||
compareProcessEvidenceFiles(args.left, args.right),
|
||||
compareProcessEvidenceFiles(args.left, args.right, args.traceSpec),
|
||||
),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ import { callPathInputSchema } from "../domain/callPath.js";
|
||||
import { staticRuntimeCorrelationInputSchema } from "../domain/staticRuntimeCorrelation.js";
|
||||
import { reconstructionVerificationInputSchema } from "../domain/reconstructionVerification.js";
|
||||
import { replayMachineRunInputSchema } from "../domain/replayMachineRun.js";
|
||||
import { processTraceSpecificationSchema } from "../domain/processTraceComparison.js";
|
||||
|
||||
import { enhancedInputSchemas } from "./enhancedInputs.js";
|
||||
import {
|
||||
@@ -385,6 +386,7 @@ export const importEvidenceBundleInputSchema = z.strictObject({
|
||||
export const processComparisonInputSchema = z.strictObject({
|
||||
left_evidence_id: z.string().regex(/^ev_[a-f0-9]{64}$/u),
|
||||
right_evidence_id: z.string().regex(/^ev_[a-f0-9]{64}$/u),
|
||||
trace_spec: processTraceSpecificationSchema.optional(),
|
||||
max_capture_age_ms: z.number().int().nonnegative().optional(),
|
||||
unknown_registry_approved: z
|
||||
.literal(true)
|
||||
@@ -440,7 +442,7 @@ export const SESSION_TOOL_CONTRACTS = [
|
||||
),
|
||||
session(
|
||||
"compare_process_captures",
|
||||
"Compare two compatible Process Capture v4 observations across terminal, interaction, exit, settlement, process, filesystem, command-shim, HTTP, and WebSocket evidence, returning the first bounded divergence. Process Capture v3 is unsupported and must be recaptured with capture_process_scenario. Missing or truncated observations are never treated as equivalent.",
|
||||
"Compare two compatible Process Capture v4 observations across terminal, interaction, lifecycle, process, filesystem, command-shim, HTTP, and WebSocket evidence. Optional trace_spec validates exact events against an explicit partial order or finite trace language; concurrency is never inferred from timestamps or broad sorting. Missing, journal-free, or truncated observations are never treated as equivalent.",
|
||||
processComparisonInputSchema,
|
||||
),
|
||||
session(
|
||||
|
||||
@@ -84,25 +84,30 @@ type RuntimeStatus = z.infer<typeof processCaptureComparisonSchema>["status"];
|
||||
|
||||
const EXPECTED_PROVIDERS = {
|
||||
compare_process_captures: {
|
||||
predicate: "rea.process-comparison/v3",
|
||||
predicates: ["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
id: "rea-process",
|
||||
name: "REA deterministic process harness",
|
||||
version: "3",
|
||||
},
|
||||
compare_artifacts: {
|
||||
predicate: "rea.artifact-comparison/v1",
|
||||
predicates: ["rea.artifact-comparison/v1"],
|
||||
id: "rea-artifact-comparison",
|
||||
name: "REA artifact comparison",
|
||||
version: "1",
|
||||
},
|
||||
compare_functions: {
|
||||
predicate: "rea.function-comparison/v1",
|
||||
predicates: ["rea.function-comparison/v1"],
|
||||
id: "rea-function-comparison",
|
||||
name: "REA function comparison",
|
||||
version: "1",
|
||||
},
|
||||
} as const;
|
||||
|
||||
const acceptsPredicate = (
|
||||
expected: readonly string[],
|
||||
actual: string,
|
||||
): boolean => expected.some((predicate) => predicate === actual);
|
||||
|
||||
/** Find observed runtime changes and separately report static change candidates. */
|
||||
export const findChangedBehavior = (
|
||||
comparisonsInput: unknown,
|
||||
@@ -182,7 +187,7 @@ const parseComparisonEvidence = (input: unknown): Evidence => {
|
||||
.parse(evidence.operation);
|
||||
const expected = EXPECTED_PROVIDERS[operation];
|
||||
if (
|
||||
evidence.predicate_type !== expected.predicate ||
|
||||
!acceptsPredicate(expected.predicates, evidence.predicate_type) ||
|
||||
evidence.provider.id !== expected.id ||
|
||||
evidence.provider.name !== expected.name ||
|
||||
evidence.provider.version !== expected.version ||
|
||||
|
||||
@@ -5,10 +5,14 @@ import { validateProcessCapture } from "./processCapture.js";
|
||||
import { jsonValueSchema } from "./jsonValue.js";
|
||||
import {
|
||||
compareProcessTraces,
|
||||
processTraceOutcomesDiffer,
|
||||
processTraceComparisonResultSchema,
|
||||
type ProcessTraceSource,
|
||||
type ProcessTraceSpecification,
|
||||
} from "./processTraceComparison.js";
|
||||
import {
|
||||
dimensionsForTraceSources,
|
||||
traceCoversObservedDimension,
|
||||
} from "./processTraceDimensionProjection.js";
|
||||
|
||||
/** Comparison classification that never equates incomplete evidence. */
|
||||
export const comparisonStatusSchema = z.enum([
|
||||
@@ -276,82 +280,19 @@ const truncatedComparison = (): ProcessCaptureComparison => ({
|
||||
limitations: ["At least one capture is truncated."],
|
||||
});
|
||||
|
||||
const dimensionsForTraceSources = (
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
): ReadonlySet<(typeof PROCESS_COMPARISON_DIMENSIONS)[number]> => {
|
||||
const dimensions = new Set<(typeof PROCESS_COMPARISON_DIMENSIONS)[number]>();
|
||||
for (const source of sources) {
|
||||
if (source.startsWith("terminal_")) dimensions.add("terminal");
|
||||
else if (source === "interaction") dimensions.add("interaction");
|
||||
else if (source === "lifecycle") dimensions.add("exit");
|
||||
else if (source === "filesystem") dimensions.add("filesystem");
|
||||
else if (
|
||||
source === "http" ||
|
||||
source === "websocket" ||
|
||||
source === "replay_transition"
|
||||
)
|
||||
dimensions.add("protocol");
|
||||
else if (source === "process") dimensions.add("process");
|
||||
else if (source === "shim") dimensions.add("shim");
|
||||
}
|
||||
return dimensions;
|
||||
};
|
||||
|
||||
const traceCoversObservedDimension = (
|
||||
dimension: (typeof PROCESS_COMPARISON_DIMENSIONS)[number],
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
left: ProcessCapture,
|
||||
right: ProcessCapture,
|
||||
): boolean => {
|
||||
const any = <Value>(
|
||||
leftValues: readonly Value[],
|
||||
rightValues: readonly Value[],
|
||||
): boolean => leftValues.length > 0 || rightValues.length > 0;
|
||||
switch (dimension) {
|
||||
case "terminal":
|
||||
return (
|
||||
(!any(left.frames, right.frames) || sources.has("terminal_raw")) &&
|
||||
(!any(left.rendered_frames, right.rendered_frames) ||
|
||||
sources.has("terminal_rendered"))
|
||||
);
|
||||
case "interaction":
|
||||
return sources.has("interaction");
|
||||
case "exit":
|
||||
return sources.has("lifecycle");
|
||||
case "filesystem":
|
||||
return false;
|
||||
case "protocol":
|
||||
return (
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "http",
|
||||
) ||
|
||||
sources.has("http")) &&
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "websocket",
|
||||
) ||
|
||||
sources.has("websocket")) &&
|
||||
(!any(left.replay_transitions, right.replay_transitions) ||
|
||||
sources.has("replay_transition"))
|
||||
);
|
||||
case "process":
|
||||
return sources.has("process");
|
||||
case "shim":
|
||||
return sources.has("shim");
|
||||
}
|
||||
};
|
||||
|
||||
const applyTraceVerdict = (
|
||||
dimensions: ComparisonDimensions,
|
||||
specification: ProcessTraceSpecification,
|
||||
trace: ReturnType<typeof compareProcessTraces>,
|
||||
captures: readonly [left: ProcessCapture, right: ProcessCapture],
|
||||
): ComparisonDimensions => {
|
||||
const traceStatus: ComparisonStatus =
|
||||
trace.verdict === "equivalent"
|
||||
? "unchanged"
|
||||
: trace.verdict === "different"
|
||||
? "changed"
|
||||
: "unknown";
|
||||
const traceShowsDifference = processTraceOutcomesDiffer(trace);
|
||||
const traceStatus =
|
||||
trace.verdict === "unknown"
|
||||
? "unknown"
|
||||
: trace.verdict === "equivalent" || !traceShowsDifference
|
||||
? "unchanged"
|
||||
: "changed";
|
||||
const sources = new Set(specification.events.map(({ source }) => source));
|
||||
const covered = dimensionsForTraceSources(sources);
|
||||
const statusFor = (
|
||||
@@ -359,7 +300,8 @@ const applyTraceVerdict = (
|
||||
): ComparisonStatus => {
|
||||
const exact = dimensions[dimension];
|
||||
if (!covered.has(dimension)) return exact;
|
||||
if (trace.verdict === "different") return "changed";
|
||||
if (trace.verdict === "different")
|
||||
return traceShowsDifference ? "changed" : exact;
|
||||
if (
|
||||
trace.verdict === "unknown" &&
|
||||
["changed", "added", "removed"].includes(exact)
|
||||
|
||||
@@ -51,3 +51,11 @@ export const compareProcessTraces = (
|
||||
diagnostic,
|
||||
});
|
||||
};
|
||||
|
||||
/** Whether trace conformance or raw declared order differs across captures. */
|
||||
export const processTraceOutcomesDiffer = (
|
||||
comparison: ProcessTraceComparisonResult,
|
||||
): boolean =>
|
||||
comparison.left.status !== comparison.right.status ||
|
||||
JSON.stringify(comparison.left.raw_trace.map(({ event_id }) => event_id)) !==
|
||||
JSON.stringify(comparison.right.raw_trace.map(({ event_id }) => event_id));
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { ProcessCapture } from "./processCapture.js";
|
||||
import type { ProcessTraceSource } from "./processTraceSpecification.js";
|
||||
|
||||
export type ProcessComparisonDimension =
|
||||
| "terminal"
|
||||
| "interaction"
|
||||
| "exit"
|
||||
| "filesystem"
|
||||
| "protocol"
|
||||
| "process"
|
||||
| "shim";
|
||||
|
||||
/** Map declared trace sources to legacy process-comparison dimensions. */
|
||||
export const dimensionsForTraceSources = (
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
): ReadonlySet<ProcessComparisonDimension> => {
|
||||
const dimensions = new Set<ProcessComparisonDimension>();
|
||||
for (const source of sources) {
|
||||
if (source.startsWith("terminal_")) dimensions.add("terminal");
|
||||
else if (source === "interaction") dimensions.add("interaction");
|
||||
else if (source === "lifecycle") dimensions.add("exit");
|
||||
else if (source === "filesystem") dimensions.add("filesystem");
|
||||
else if (
|
||||
source === "http" ||
|
||||
source === "websocket" ||
|
||||
source === "replay_transition"
|
||||
)
|
||||
dimensions.add("protocol");
|
||||
else if (source === "process") dimensions.add("process");
|
||||
else if (source === "shim") dimensions.add("shim");
|
||||
}
|
||||
return dimensions;
|
||||
};
|
||||
|
||||
/** Whether a trace specification covers every observed source in a dimension. */
|
||||
export const traceCoversObservedDimension = (
|
||||
dimension: ProcessComparisonDimension,
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
left: ProcessCapture,
|
||||
right: ProcessCapture,
|
||||
): boolean => {
|
||||
const any = <Value>(
|
||||
leftValues: readonly Value[],
|
||||
rightValues: readonly Value[],
|
||||
): boolean => leftValues.length > 0 || rightValues.length > 0;
|
||||
switch (dimension) {
|
||||
case "terminal":
|
||||
return (
|
||||
(!any(left.frames, right.frames) || sources.has("terminal_raw")) &&
|
||||
(!any(left.rendered_frames, right.rendered_frames) ||
|
||||
sources.has("terminal_rendered"))
|
||||
);
|
||||
case "interaction":
|
||||
return sources.has("interaction");
|
||||
case "exit":
|
||||
return sources.has("lifecycle");
|
||||
case "filesystem":
|
||||
return false;
|
||||
case "protocol":
|
||||
return (
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "http",
|
||||
) ||
|
||||
sources.has("http")) &&
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "websocket",
|
||||
) ||
|
||||
sources.has("websocket")) &&
|
||||
(!any(left.replay_transitions, right.replay_transitions) ||
|
||||
sources.has("replay_transition"))
|
||||
);
|
||||
case "process":
|
||||
return sources.has("process");
|
||||
case "shim":
|
||||
return sources.has("shim");
|
||||
}
|
||||
};
|
||||
@@ -43,6 +43,21 @@ const eventDeclarationSchema = z.strictObject({
|
||||
id: identifierSchema,
|
||||
source: processTraceSourceSchema,
|
||||
exact: jsonValueSchema,
|
||||
ignore_fields: z
|
||||
.array(
|
||||
z.enum([
|
||||
"sequence",
|
||||
"at_ms",
|
||||
"scheduled_at_ms",
|
||||
"dispatched_at_ms",
|
||||
"elapsed_ms",
|
||||
]),
|
||||
)
|
||||
.max(5)
|
||||
.refine((values) => new Set(values).size === values.length, {
|
||||
message: "ignored event fields must be unique",
|
||||
})
|
||||
.optional(),
|
||||
cardinality: cardinalitySchema.default({ kind: "required" }),
|
||||
});
|
||||
|
||||
@@ -57,6 +72,15 @@ const partialOrderLanguageSchema = z.strictObject({
|
||||
)
|
||||
.max(4_096)
|
||||
.default([]),
|
||||
not_before: z
|
||||
.array(
|
||||
z.strictObject({
|
||||
event: identifierSchema,
|
||||
anchor: identifierSchema,
|
||||
}),
|
||||
)
|
||||
.max(4_096)
|
||||
.default([]),
|
||||
unordered_groups: z
|
||||
.array(
|
||||
z.strictObject({ events: z.array(identifierSchema).min(2).max(256) }),
|
||||
@@ -106,6 +130,23 @@ export const canonicalTraceJson = (value: unknown): string => {
|
||||
return serialized;
|
||||
};
|
||||
|
||||
/** Remove only explicitly declared schedule metadata before exact matching. */
|
||||
export const comparableTracePayload = (
|
||||
value: unknown,
|
||||
ignoredFields: readonly string[] = [],
|
||||
): unknown => {
|
||||
if (
|
||||
ignoredFields.length === 0 ||
|
||||
typeof value !== "object" ||
|
||||
value === null ||
|
||||
Array.isArray(value)
|
||||
)
|
||||
return value;
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).filter(([name]) => !ignoredFields.includes(name)),
|
||||
);
|
||||
};
|
||||
|
||||
export const processTraceCardinalityBounds = (
|
||||
cardinality: ProcessTraceSpecification["events"][number]["cardinality"],
|
||||
): readonly [number, number] => {
|
||||
@@ -156,6 +197,18 @@ const buildAdjacency = (
|
||||
});
|
||||
adjacency.get(edge.before)?.add(edge.after);
|
||||
}
|
||||
for (const [index, constraint] of language.not_before.entries()) {
|
||||
const path = ["language", "not_before", index];
|
||||
addReferenceIssue(context, known, constraint.event, [...path, "event"]);
|
||||
addReferenceIssue(context, known, constraint.anchor, [...path, "anchor"]);
|
||||
if (constraint.event === constraint.anchor)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "not-before constraints must reference distinct events",
|
||||
path,
|
||||
});
|
||||
adjacency.get(constraint.anchor)?.add(constraint.event);
|
||||
}
|
||||
return adjacency;
|
||||
};
|
||||
|
||||
@@ -274,7 +327,7 @@ const validatePartialOrder = (
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "happens-before graph must be acyclic",
|
||||
path: ["language", "happens_before"],
|
||||
path: ["language"],
|
||||
});
|
||||
const reachable = computeReachability(ids, adjacency);
|
||||
const unorderedPairs = validateUnorderedGroups(
|
||||
@@ -309,6 +362,7 @@ const validateFiniteTraces = (
|
||||
if (specification.language.kind !== "finite_traces") return;
|
||||
const known = new Set(specification.events.map(({ id }) => id));
|
||||
const variantIds = new Set<string>();
|
||||
const variantTraces = new Set<string>();
|
||||
for (const [
|
||||
variantIndex,
|
||||
variant,
|
||||
@@ -321,6 +375,14 @@ const validateFiniteTraces = (
|
||||
path: [...variantPath, "id"],
|
||||
});
|
||||
variantIds.add(variant.id);
|
||||
const canonicalTrace = canonicalTraceJson(variant.trace);
|
||||
if (variantTraces.has(canonicalTrace))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "finite trace variants must not duplicate a trace",
|
||||
path: [...variantPath, "trace"],
|
||||
});
|
||||
variantTraces.add(canonicalTrace);
|
||||
for (const [tokenIndex, token] of variant.trace.entries()) {
|
||||
const values = typeof token === "string" ? [token] : token.unordered;
|
||||
for (const [eventIndex, id] of values.entries())
|
||||
@@ -352,6 +414,7 @@ export const processTraceSpecificationSchema =
|
||||
specificationShapeSchema.superRefine((specification, context) => {
|
||||
const ids = new Set<string>();
|
||||
const predicates = new Set<string>();
|
||||
const ignoredFieldsBySource = new Map<string, string>();
|
||||
for (const [index, event] of specification.events.entries()) {
|
||||
if (ids.has(event.id))
|
||||
context.addIssue({
|
||||
@@ -360,7 +423,45 @@ export const processTraceSpecificationSchema =
|
||||
path: ["events", index, "id"],
|
||||
});
|
||||
ids.add(event.id);
|
||||
const predicate = `${event.source}\0${canonicalTraceJson(event.exact)}`;
|
||||
const ignoredFields = [...(event.ignore_fields ?? [])].sort();
|
||||
if (
|
||||
ignoredFields.length > 0 &&
|
||||
(typeof event.exact !== "object" ||
|
||||
event.exact === null ||
|
||||
Array.isArray(event.exact))
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "ignored schedule fields require an exact object payload",
|
||||
path: ["events", index, "ignore_fields"],
|
||||
});
|
||||
const ignoredKey = canonicalTraceJson(ignoredFields);
|
||||
const priorIgnoredKey = ignoredFieldsBySource.get(event.source);
|
||||
if (priorIgnoredKey !== undefined && priorIgnoredKey !== ignoredKey)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message:
|
||||
"events from one source must use the same ignored schedule fields",
|
||||
path: ["events", index, "ignore_fields"],
|
||||
});
|
||||
ignoredFieldsBySource.set(event.source, ignoredKey);
|
||||
if (
|
||||
ignoredFields.some(
|
||||
(field) =>
|
||||
typeof event.exact === "object" &&
|
||||
event.exact !== null &&
|
||||
!Array.isArray(event.exact) &&
|
||||
field in event.exact,
|
||||
)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "exact payload must omit every ignored schedule field",
|
||||
path: ["events", index, "exact"],
|
||||
});
|
||||
const predicate = `${event.source}\0${canonicalTraceJson(
|
||||
comparableTracePayload(event.exact, ignoredFields),
|
||||
)}`;
|
||||
if (predicates.has(predicate))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
|
||||
@@ -46,24 +46,29 @@ const MAX_CANONICAL_BYTES = 32 * 1024 * 1024;
|
||||
const providers = {
|
||||
behavioral: {
|
||||
operation: "compare_process_captures",
|
||||
predicate: "rea.process-comparison/v3",
|
||||
predicates: ["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
id: "rea-process",
|
||||
name: "REA deterministic process harness",
|
||||
},
|
||||
"structural-function": {
|
||||
operation: "compare_functions",
|
||||
predicate: "rea.function-comparison/v1",
|
||||
predicates: ["rea.function-comparison/v1"],
|
||||
id: "rea-function-comparison",
|
||||
name: "REA function comparison",
|
||||
},
|
||||
"structural-artifact": {
|
||||
operation: "compare_artifacts",
|
||||
predicate: "rea.artifact-comparison/v1",
|
||||
predicates: ["rea.artifact-comparison/v1"],
|
||||
id: "rea-artifact-comparison",
|
||||
name: "REA artifact comparison",
|
||||
},
|
||||
} as const;
|
||||
|
||||
const acceptsPredicate = (
|
||||
expected: readonly string[],
|
||||
actual: string,
|
||||
): boolean => expected.some((predicate) => predicate === actual);
|
||||
|
||||
/** Verify only explicitly declared claims; no global equivalence is inferred. */
|
||||
export const verifyReconstruction = (
|
||||
specificationInput: unknown,
|
||||
@@ -229,7 +234,7 @@ const validateComparisonIdentity = (claim: Claim, evidence: Evidence): void => {
|
||||
const expected = providers[claim.kind];
|
||||
if (
|
||||
evidence.operation !== expected.operation ||
|
||||
evidence.predicate_type !== expected.predicate ||
|
||||
!acceptsPredicate(expected.predicates, evidence.predicate_type) ||
|
||||
evidence.provider.id !== expected.id ||
|
||||
evidence.provider.name !== expected.name ||
|
||||
evidence.provider.version !== (claim.kind === "behavioral" ? "3" : "1") ||
|
||||
|
||||
@@ -160,7 +160,7 @@ const EXPECTED = {
|
||||
"REA function comparison",
|
||||
],
|
||||
compare_process_captures: [
|
||||
"rea.process-comparison/v3",
|
||||
["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
"rea-process",
|
||||
"REA deterministic process harness",
|
||||
],
|
||||
@@ -405,7 +405,9 @@ const assertComparisonIdentity = (evidence: Evidence): void => {
|
||||
const expected = EXPECTED[evidence.operation as keyof typeof EXPECTED];
|
||||
if (
|
||||
expected === undefined ||
|
||||
evidence.predicate_type !== expected[0] ||
|
||||
!(Array.isArray(expected[0])
|
||||
? expected[0].includes(evidence.predicate_type)
|
||||
: evidence.predicate_type === expected[0]) ||
|
||||
evidence.provider.id !== expected[1] ||
|
||||
evidence.provider.name !== expected[2] ||
|
||||
evidence.provider.version !==
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
processComparisonInputSchema,
|
||||
SESSION_TOOL_CONTRACTS,
|
||||
} from "../contracts/toolContracts.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import { createEvidence, type EvidenceLocation } from "../domain/evidence.js";
|
||||
import { EvidenceIntegrityError } from "../domain/errors.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import type { RecordUnknownInput } from "../domain/residualUnknown.js";
|
||||
@@ -22,6 +22,16 @@ import { runDerivedOperation } from "./runDerivedOperation.js";
|
||||
import { safeParseToolInput } from "./toolInputValidation.js";
|
||||
import { resolveSessionEvidenceIds } from "./sessionEvidence.js";
|
||||
|
||||
const PROCESS_CAPTURE_EVIDENCE = {
|
||||
operation: "capture_process_scenario",
|
||||
predicate: "rea.process-capture/v4",
|
||||
} as const;
|
||||
|
||||
const sourceLocations = (
|
||||
left: readonly EvidenceLocation[] | undefined,
|
||||
right: readonly EvidenceLocation[] | undefined,
|
||||
): readonly EvidenceLocation[] => [...(left ?? []), ...(right ?? [])];
|
||||
|
||||
/** Register deterministic process-capture comparison and contradiction tracking. */
|
||||
export const registerProcessComparisonTool = (
|
||||
server: McpServer,
|
||||
@@ -40,20 +50,16 @@ export const registerProcessComparisonTool = (
|
||||
);
|
||||
if (!parsedInput.ok) return toCallToolResult(parsedInput, contract);
|
||||
const parsed = parsedInput.value;
|
||||
const expected = {
|
||||
operation: "capture_process_scenario",
|
||||
predicate: "rea.process-capture/v4",
|
||||
};
|
||||
const leftRecord = resolveSessionEvidenceIds(
|
||||
session,
|
||||
[parsed.left_evidence_id],
|
||||
expected,
|
||||
PROCESS_CAPTURE_EVIDENCE,
|
||||
);
|
||||
if (!leftRecord.ok) return toCallToolResult(leftRecord, contract);
|
||||
const rightRecord = resolveSessionEvidenceIds(
|
||||
session,
|
||||
[parsed.right_evidence_id],
|
||||
expected,
|
||||
PROCESS_CAPTURE_EVIDENCE,
|
||||
);
|
||||
if (!rightRecord.ok) return toCallToolResult(rightRecord, contract);
|
||||
let comparison: ReturnType<typeof compareProcessCaptures>;
|
||||
@@ -71,6 +77,9 @@ export const registerProcessComparisonTool = (
|
||||
...(parsed.max_capture_age_ms === undefined
|
||||
? {}
|
||||
: { maxCaptureAgeMs: parsed.max_capture_age_ms }),
|
||||
...(parsed.trace_spec === undefined
|
||||
? {}
|
||||
: { traceSpecification: parsed.trace_spec }),
|
||||
now,
|
||||
}),
|
||||
);
|
||||
@@ -89,18 +98,28 @@ export const registerProcessComparisonTool = (
|
||||
);
|
||||
}
|
||||
const evidence = createEvidence(undefined, PROCESS_PROVIDER, {
|
||||
predicateType: "rea.process-comparison/v3",
|
||||
predicateType:
|
||||
parsed.trace_spec === undefined
|
||||
? "rea.process-comparison/v3"
|
||||
: "rea.process-comparison/v4",
|
||||
operation: contract.name,
|
||||
parameters: {
|
||||
left_evidence_id: parsed.left_evidence_id,
|
||||
right_evidence_id: parsed.right_evidence_id,
|
||||
left_normalization: leftCapture.normalization,
|
||||
right_normalization: rightCapture.normalization,
|
||||
...(parsed.trace_spec === undefined
|
||||
? {}
|
||||
: { trace_spec: jsonValueSchema.parse(parsed.trace_spec) }),
|
||||
},
|
||||
result: jsonValueSchema.parse(comparison),
|
||||
confidence: "derived",
|
||||
authority: "analyst-inference",
|
||||
limitations: comparison.limitations,
|
||||
locations: sourceLocations(
|
||||
leftRecord.value[0]?.locations,
|
||||
rightRecord.value[0]?.locations,
|
||||
),
|
||||
evidenceLinks: [parsed.left_evidence_id, parsed.right_evidence_id],
|
||||
});
|
||||
return toCallToolResult(
|
||||
|
||||
Reference in New Issue
Block a user