1
0
Fork 0

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.
This commit is contained in:
Jason Fraley 2026-10-06 17:15:34 -04:00
parent f595828c99
commit 6d2cde88db
2 changed files with 63 additions and 2 deletions

View file

@ -20,9 +20,13 @@ export interface SeenEntry {
export type SeenMap = Record<string, SeenEntry>;
/** 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<string, unknown>)
.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 {};

View file

@ -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",