From 6d2cde88db5b5f4a687a8815ed9d4d5ff3ebe900 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 6 Oct 2026 17:15:34 -0400 Subject: [PATCH] fix(npc-dialogue): content-only flow hashing for NEW badges Rearranging nodes on the canvas (positions, edge ids, styling) no longer changes the flow hash, so tidying a conversation in the editor does not re-badge it as NEW for every player. The hash now covers node identity, type, data, and edge source/target with order-independent serialization; real text or branching edits still re-flag. --- src/lib/npc-dialogue/seen.ts | 35 +++++++++++++++++-- .../int/npc-dialogue-interpolate.int.spec.ts | 30 ++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/lib/npc-dialogue/seen.ts b/src/lib/npc-dialogue/seen.ts index d65ea268..6d6a8427 100644 --- a/src/lib/npc-dialogue/seen.ts +++ b/src/lib/npc-dialogue/seen.ts @@ -20,9 +20,13 @@ export interface SeenEntry { export type SeenMap = Record; -/** Deterministic djb2 hash of the serialized flowchart (base36). */ +/** Deterministic djb2 hash of the flowchart's CONTENT (base36). Node + * positions and edge plumbing (ids, handles, styling) are excluded on + * purpose: rearranging the canvas is not a story change and must not + * re-badge conversations as NEW. Object keys are sorted so an equivalent + * document serialized with a different key order hashes the same. */ export function flowHash(flowData: unknown): string { - const json = JSON.stringify(flowData ?? null); + const json = stableStringify(contentOnly(flowData)); let h = 5381; for (let i = 0; i < json.length; i += 1) { h = ((h << 5) + h + json.charCodeAt(i)) | 0; @@ -30,6 +34,33 @@ export function flowHash(flowData: unknown): string { return (h >>> 0).toString(36); } +/** Strips layout from a flowchart, keeping node identity/branching/text. */ +function contentOnly(flowData: unknown): unknown { + const doc = (flowData ?? {}) as { nodes?: unknown; edges?: unknown }; + const nodes = Array.isArray(doc.nodes) ? doc.nodes : []; + const edges = Array.isArray(doc.edges) ? doc.edges : []; + return { + nodes: nodes.map((node) => { + const n = (node ?? {}) as { id?: unknown; type?: unknown; data?: unknown }; + return { id: n.id, type: n.type, data: n.data }; + }), + edges: edges.map((edge) => { + const e = (edge ?? {}) as { source?: unknown; target?: unknown }; + return { source: e.source, target: e.target }; + }), + }; +} + +/** JSON.stringify with sorted object keys for order-independent hashing. */ +function stableStringify(value: unknown): string { + if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null"; + if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`; + const entries = Object.entries(value as Record) + .filter(([, v]) => v !== undefined) + .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); + return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(",")}}`; +} + /** Reads the seen map; corrupt or absent storage reads empty. */ export function loadSeenMap(): SeenMap { if (typeof window === "undefined") return {}; diff --git a/tests/int/npc-dialogue-interpolate.int.spec.ts b/tests/int/npc-dialogue-interpolate.int.spec.ts index bad001f7..91c60597 100644 --- a/tests/int/npc-dialogue-interpolate.int.spec.ts +++ b/tests/int/npc-dialogue-interpolate.int.spec.ts @@ -457,6 +457,36 @@ describe("seen tracking (NEW badges)", () => { expect(flowHash({ nodes: [{ id: "a" }] })).not.toBe(flowHash({ nodes: [{ id: "b" }] })); }); + it("ignores layout (positions, edge ids/styling) and key order", () => { + const base = { + nodes: [{ id: "a", type: "dialogue", position: { x: 0, y: 0 }, data: { body: "hi" } }], + edges: [{ id: "e1", source: "a", target: "b", style: {} }], + }; + const rearranged = { + nodes: [{ id: "a", type: "dialogue", position: { x: 900, y: 400 }, data: { body: "hi" } }], + edges: [{ id: "zzz", source: "a", target: "b", style: { stroke: "red" } }], + }; + expect(flowHash(rearranged)).toBe(flowHash(base)); + const reordered = { + nodes: [{ data: { body: "hi" }, type: "dialogue", id: "a" }], + edges: [{ target: "b", source: "a" }], + }; + expect(flowHash(reordered)).toBe(flowHash(base)); + }); + + it("re-flags when node content changes", () => { + const base = { nodes: [{ id: "a", type: "dialogue", data: { body: "hi" } }] }; + const edited = { nodes: [{ id: "a", type: "dialogue", data: { body: "changed" } }] }; + expect(flowHash(edited)).not.toBe(flowHash(base)); + const addedChoice = { + nodes: [ + { id: "a", type: "dialogue", data: { body: "hi" } }, + { id: "b", type: "choice", data: { choices: [{ label: "New path", target: "a" }] } }, + ], + }; + expect(flowHash(addedChoice)).not.toBe(flowHash(base)); + }); + it("treats a hash mismatch as everything unseen", () => { expect(unseenNodeIds({ hash: "old", nodes: ["a", "b"] }, "new", ["a", "b"])).toEqual([ "a",