{"version":3,"file":"memory-diff.d.ts","sourceRoot":"","sources":["../../src/core/memory-diff.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAc,MAAM,aAAa,CAAC;AAErE,yDAAyD;AACzD,MAAM,WAAW,SAAS;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACd;AAED,4DAA4D;AAC5D,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACd;AAED,iEAAiE;AACjE,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;CACrB;AAED,+CAA+C;AAC/C,MAAM,WAAW,kBAAkB;IAClC,mDAAmD;IACnD,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,oDAAoD;IACpD,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,kEAAkE;IAClE,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,kFAAkF;IAClF,iBAAiB,EAAE,OAAO,CAAC;CAC3B;AAaD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,yBAAyB,CACxC,QAAQ,EAAE,qBAAqB,GAAG,SAAS,EAC3C,OAAO,EAAE,qBAAqB,GAC5B,kBAAkB,CAqCpB","sourcesContent":["/**\n * Shared snapshot diff computation for session memory history.\n *\n * Compare two MemoryHistorySnapshot states to derive what changed between them.\n * Because persistence is snapshot-based (not event-sourced), each snapshot\n * represents the complete memory state at that point in time.\n *\n * This diff is derived from adjacent real persisted snapshots and is NOT\n * a true operation log — it cannot tell you which specific add/update/delete\n * operation produced a given change.\n */\n\nimport type { MemoryHistorySnapshot, MemoryItem } from \"./memory.js\";\n\n/** A key present only in the current snapshot (added) */\nexport interface DiffAdded {\n\ttype: \"added\";\n\tkey: string;\n\tvalue: string;\n}\n\n/** A key present only in the previous snapshot (removed) */\nexport interface DiffRemoved {\n\ttype: \"removed\";\n\tkey: string;\n\tvalue: string;\n}\n\n/** A key present in both snapshots, but with different values */\nexport interface DiffChanged {\n\ttype: \"changed\";\n\tkey: string;\n\tpreviousValue: string;\n\tcurrentValue: string;\n}\n\n/** Result of comparing two memory snapshots */\nexport interface MemorySnapshotDiff {\n\t/** Keys that exist only in the current snapshot */\n\tadded: DiffAdded[];\n\t/** Keys that exist only in the previous snapshot */\n\tremoved: DiffRemoved[];\n\t/** Keys that exist in both snapshots but have different values */\n\tchanged: DiffChanged[];\n\t/** True when previous was undefined (current is the initial/earliest snapshot) */\n\tisInitialSnapshot: boolean;\n}\n\n/**\n * Build a lookup map from key → value string for efficient diffing.\n */\nfunction buildKeyMap(items: MemoryItem[]): Map<string, string> {\n\tconst map = new Map<string, string>();\n\tfor (const item of items) {\n\t\tmap.set(item.key, item.value);\n\t}\n\treturn map;\n}\n\n/**\n * Compute the diff between two memory snapshots.\n *\n * Semantics:\n * - `added`: key only in current (not in previous)\n * - `removed`: key only in previous (not in current)\n * - `changed`: key in both, value strings differ\n * - Unchanged keys are omitted from the result\n *\n * If `previous` is undefined (initial snapshot / nothing to diff against),\n * all current keys are treated as added and `isInitialSnapshot` is set to true.\n *\n * @param previous - The earlier snapshot (or undefined for initial state)\n * @param current  - The later snapshot\n */\nexport function computeMemorySnapshotDiff(\n\tprevious: MemoryHistorySnapshot | undefined,\n\tcurrent: MemoryHistorySnapshot,\n): MemorySnapshotDiff {\n\tconst added: DiffAdded[] = [];\n\tconst removed: DiffRemoved[] = [];\n\tconst changed: DiffChanged[] = [];\n\n\tconst prevMap = buildKeyMap(previous?.items ?? []);\n\tconst currMap = buildKeyMap(current.items);\n\n\t// Find added and changed keys (present in current)\n\tfor (const [key, currentValue] of currMap) {\n\t\tconst previousValue = prevMap.get(key);\n\t\tif (previousValue === undefined) {\n\t\t\t// Key only in current → added\n\t\t\tadded.push({ type: \"added\", key, value: currentValue });\n\t\t} else if (previousValue !== currentValue) {\n\t\t\t// Key in both but value differs → changed\n\t\t\tchanged.push({ type: \"changed\", key, previousValue, currentValue });\n\t\t}\n\t\t// else: key in both with same value → unchanged, omitted\n\t}\n\n\t// Find removed keys (present only in previous)\n\tif (previous !== undefined) {\n\t\tfor (const [key, previousValue] of prevMap) {\n\t\t\tif (!currMap.has(key)) {\n\t\t\t\t// Key only in previous → removed\n\t\t\t\tremoved.push({ type: \"removed\", key, value: previousValue });\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tadded,\n\t\tremoved,\n\t\tchanged,\n\t\tisInitialSnapshot: previous === undefined,\n\t};\n}\n"]}