2026-07-12 21:12:18 +00:00
|
|
|
import { z } from "zod";
|
2026-10-14 03:01:41 +13:00
|
|
|
import { overallStatus } from "./functionComparisonResults.js";
|
2026-10-20 17:48:52 +08:00
|
|
|
import { evidenceInputSchema } from "./evidenceReference.js";
|
2026-10-05 17:29:20 +08:00
|
|
|
import { digestSchema } from "./../domain/digests.js";
|
|
|
|
|
import { prefixedDigestSchema } from "./../domain/digests.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-10-09 20:03:47 +08:00
|
|
|
const comparisonStatusSchema = z.enum(["unchanged", "changed", "unknown"]);
|
2026-10-13 04:12:34 +08:00
|
|
|
export const functionComparisonDimensionNameSchema = z.enum([
|
2026-07-12 21:12:18 +00:00
|
|
|
"identity",
|
|
|
|
|
"pseudocode",
|
|
|
|
|
"assembly",
|
|
|
|
|
"comments",
|
|
|
|
|
"calls",
|
|
|
|
|
"references",
|
|
|
|
|
"strings_names",
|
|
|
|
|
"cfg",
|
|
|
|
|
]);
|
2026-10-05 17:29:20 +08:00
|
|
|
const evidenceIdSchema = prefixedDigestSchema("ev");
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-09-29 07:08:46 +08:00
|
|
|
/** Inputs for explicit function-to-function comparison. */
|
2026-07-16 18:49:35 +08:00
|
|
|
export const functionComparisonInputSchema = z.strictObject({
|
2026-10-20 17:48:52 +08:00
|
|
|
left: evidenceInputSchema,
|
|
|
|
|
right: evidenceInputSchema,
|
2026-07-12 21:12:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const textDeltaSchema = z.object({
|
|
|
|
|
added_lines: z.number().int().min(0),
|
|
|
|
|
removed_lines: z.number().int().min(0),
|
|
|
|
|
hunks: z.number().int().min(0),
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-11 18:04:36 +08:00
|
|
|
const functionDimensionContextShape = {
|
2026-10-13 04:12:34 +08:00
|
|
|
dimension: functionComparisonDimensionNameSchema,
|
2026-07-12 21:12:18 +00:00
|
|
|
left_count: z.number().int().min(0).nullable(),
|
|
|
|
|
right_count: z.number().int().min(0).nullable(),
|
2026-09-29 14:53:50 +08:00
|
|
|
evidence_links: z.array(evidenceIdSchema).min(2),
|
2026-07-12 21:12:18 +00:00
|
|
|
limitations: z.array(z.string()),
|
2026-08-11 18:04:36 +08:00
|
|
|
};
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-08-11 18:04:36 +08:00
|
|
|
const functionDimensionSchema = z.union([
|
|
|
|
|
z.object({
|
|
|
|
|
...functionDimensionContextShape,
|
|
|
|
|
status: z.literal("unchanged"),
|
|
|
|
|
left_digest: digestSchema,
|
|
|
|
|
right_digest: digestSchema,
|
|
|
|
|
text_delta: textDeltaSchema.nullable(),
|
|
|
|
|
conclusion_kind: z.literal("derived_relationship"),
|
|
|
|
|
}),
|
|
|
|
|
z.object({
|
|
|
|
|
...functionDimensionContextShape,
|
|
|
|
|
status: z.literal("changed"),
|
|
|
|
|
left_digest: digestSchema,
|
|
|
|
|
right_digest: digestSchema,
|
|
|
|
|
text_delta: textDeltaSchema.nullable(),
|
|
|
|
|
conclusion_kind: z.enum(["derived_relationship", "contradiction"]),
|
|
|
|
|
}),
|
|
|
|
|
z.object({
|
|
|
|
|
...functionDimensionContextShape,
|
2026-10-09 20:03:47 +08:00
|
|
|
status: z.literal("unknown"),
|
2026-08-11 18:04:36 +08:00
|
|
|
left_digest: z.null(),
|
|
|
|
|
right_digest: z.null(),
|
|
|
|
|
text_delta: z.null(),
|
|
|
|
|
conclusion_kind: z.literal("unresolved_branch"),
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const functionMatchSchema = z.union([
|
|
|
|
|
z.object({
|
|
|
|
|
status: z.literal("matched"),
|
|
|
|
|
method: z.literal("symbol"),
|
2026-07-12 21:12:18 +00:00
|
|
|
left_name: z.string(),
|
|
|
|
|
right_name: z.string(),
|
|
|
|
|
}),
|
2026-08-11 18:04:36 +08:00
|
|
|
z.object({
|
|
|
|
|
status: z.enum(["mismatched", "ambiguous"]),
|
|
|
|
|
method: z.literal("explicit"),
|
|
|
|
|
left_name: z.string(),
|
|
|
|
|
right_name: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
/** Deterministic dimension-classified function comparison Evidence payload. */
|
2026-10-14 03:01:41 +13:00
|
|
|
export const functionComparisonResultSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
status: comparisonStatusSchema,
|
|
|
|
|
function_match: functionMatchSchema,
|
|
|
|
|
left_subject_sha256: digestSchema,
|
|
|
|
|
right_subject_sha256: digestSchema,
|
|
|
|
|
summary: z.object({
|
|
|
|
|
unchanged: z.number().int().min(0),
|
|
|
|
|
changed: z.number().int().min(0),
|
|
|
|
|
unknown: z.number().int().min(0),
|
|
|
|
|
}),
|
|
|
|
|
dimensions: z.array(functionDimensionSchema).length(8),
|
|
|
|
|
changes: z.array(functionDimensionSchema),
|
|
|
|
|
limitations: z.array(z.string()),
|
|
|
|
|
})
|
|
|
|
|
.superRefine((result, context) => {
|
|
|
|
|
if (
|
|
|
|
|
result.status !==
|
|
|
|
|
overallStatus(result.dimensions, result.function_match.status)
|
|
|
|
|
)
|
|
|
|
|
context.addIssue({
|
|
|
|
|
code: "custom",
|
|
|
|
|
path: ["status"],
|
|
|
|
|
message:
|
|
|
|
|
"Function comparison status contradicts its dimensions or function match.",
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-12 21:12:18 +00:00
|
|
|
|
|
|
|
|
export type FunctionComparisonResult = z.infer<
|
|
|
|
|
typeof functionComparisonResultSchema
|
|
|
|
|
>;
|
|
|
|
|
export type FunctionDimension = z.infer<typeof functionDimensionSchema>;
|
2026-10-13 04:12:34 +08:00
|
|
|
export type DimensionName = z.infer<
|
|
|
|
|
typeof functionComparisonDimensionNameSchema
|
|
|
|
|
>;
|