/*
RFC-0656: JSON stable normalizer — removes non-deterministic timestamp fields and sorts keys before hashing.
- Do not remove fields other than known timestamp keys (createdAt, buildTimestamp, generatedAt).
- RFC-0656: initial JSON stable normalizer.
*/
import { stableJsonHash } from "@warpgogol/werkstatt-shared/fingerprint";
const NON_DETERMINISTIC_KEYS = new Set(["createdAt", "buildTimestamp", "generatedAt"]);
function stripTimestamps(value: unknown): unknown {
if (Array.isArray(value)) return value.map(stripTimestamps);
if (value && typeof value === "object") {
return Object.fromEntries(
Object.entries(value as Record)
.filter(([key]) => !NON_DETERMINISTIC_KEYS.has(key))
.map(([key, nested]) => [key, stripTimestamps(nested)]),
);
}
return value;
}
export function normalizeJsonStable(content: string): string {
const parsed = JSON.parse(content);
return stableJsonHash(stripTimestamps(parsed));
}