{"version":3,"file":"memory-snapshot-contract.d.ts","sourceRoot":"","sources":["../../src/core/memory-snapshot-contract.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAmC,KAAK,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAE3G,MAAM,WAAW,wBAAwB;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,kCAAkC;IAClD,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,mCAAmC;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,+BAA+B;IAC/C,KAAK,EAAE,UAAU,GAAG,QAAQ,CAAC;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,WAAW,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IAChD,UAAU,EAAE,mCAAmC,EAAE,CAAC;CAClD;AAED,MAAM,WAAW,2BAA2B;IAC3C,WAAW,EAAE,SAAS,CAAC;IACvB,YAAY,EAAE,UAAU,CAAC;IACzB,SAAS,EAAE,wBAAwB,EAAE,CAAC;CACtC;AAED,MAAM,WAAW,uCAAuC;IACvD,WAAW,EAAE,SAAS,CAAC;IACvB,YAAY,EAAE,UAAU,CAAC;IACzB,MAAM,EAAE,eAAe,CA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AkB,CAAC,GAC9D,2BAA2B,CAO7B;AAED,wBAAgB,gCAAgC,CAC/C,OAAO,EAAE,6BAA6B,EACtC,SAAS,CAAC,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC9C,2BAA2B,CA6E7B","sourcesContent":["import type { MemoryHistorySnapshot } from \"./memory.js\";\nimport { computeMemorySnapshotDiff } from \"./memory-diff.js\";\nimport { MEMORY_SNAPSHOT_SHORT_ID_LENGTH, type SnapshotResolution } from \"./snapshot-selector-resolver.js\";\n\nexport interface StructuredMemorySnapshot {\n\tentryId: string;\n\tshortId: string;\n\tparentId: string | null;\n\trecordedAt: string;\n\titems: MemoryHistorySnapshot[\"items\"];\n\titemCount: number;\n\tisCurrent: boolean;\n}\n\nexport interface StructuredResolvedSnapshotSelector {\n\tinput: string;\n\tmatchedInput: string;\n\tresolvedId: string;\n}\n\nexport interface StructuredSnapshotSelectorCandidate {\n\tentryId: string;\n\tshortId: string;\n}\n\nexport interface StructuredSnapshotSelectorIssue {\n\tlabel: \"baseline\" | \"target\";\n\tinput: string;\n\tmatchedInput: string;\n\terror: NonNullable<SnapshotResolution[\"error\"]>;\n\tcandidates: StructuredSnapshotSelectorCandidate[];\n}\n\nexport interface StructuredMemoryHistoryData {\n\tbranchScope: \"current\";\n\thistoryModel: \"snapshot\";\n\tsnapshots: StructuredMemorySnapshot[];\n}\n\nexport interface StructuredMemoryCompareEmptyHistoryData {\n\tbranchScope: \"current\";\n\thistoryModel: \"snapshot\";\n\tstatus: \"empty_history\";\n\tsnapshotCount: 0;\n}\n\nexport interface StructuredMemoryCompareInitialSnapshotData {\n\tbranchScope: \"current\";\n\thistoryModel: \"snapshot\";\n\tstatus: \"initial_snapshot\";\n\tcompareMode: \"adjacent\";\n\ttarget: StructuredMemorySnapshot;\n\tdiff: ReturnType<typeof computeMemorySnapshotDiff>;\n}\n\nexport interface StructuredMemoryCompareSelectorResolutionFailedData {\n\tbranchScope: \"current\";\n\thistoryModel: \"snapshot\";\n\tstatus: \"selector_resolution_failed\";\n\tcompareMode: \"explicit\";\n\tsnapshotCount: number;\n\tissues: StructuredSnapshotSelectorIssue[];\n}\n\nexport interface StructuredMemoryCompareOkData {\n\tbranchScope: \"current\";\n\thistoryModel: \"snapshot\";\n\tstatus: \"ok\";\n\tcompareMode: \"adjacent\" | \"explicit\";\n\tbaseline: StructuredMemorySnapshot;\n\ttarget: StructuredMemorySnapshot;\n\tdiff: ReturnType<typeof computeMemorySnapshotDiff>;\n\tsameSnapshot: boolean;\n\tselectors?: {\n\t\tbaseline: StructuredResolvedSnapshotSelector;\n\t\ttarget: StructuredResolvedSnapshotSelector;\n\t};\n}\n\nexport type StructuredMemoryCompareData =\n\t| StructuredMemoryCompareEmptyHistoryData\n\t| StructuredMemoryCompareInitialSnapshotData\n\t| StructuredMemoryCompareSelectorResolutionFailedData\n\t| StructuredMemoryCompareOkData;\n\nexport interface MemorySnapshotContractContext {\n\tgetMemoryHistory(): readonly MemoryHistorySnapshot[];\n\tresolveMemorySnapshotSelector(input: string): SnapshotResolution;\n}\n\nconst MEMORY_HISTORY_BRANCH_SCOPE = \"current\" as const;\nconst MEMORY_HISTORY_MODEL = \"snapshot\" as const;\n\nfunction toShortId(entryId: string): string {\n\treturn entryId.slice(0, MEMORY_SNAPSHOT_SHORT_ID_LENGTH);\n}\n\nfunction toStructuredMemorySnapshot(snapshot: MemoryHistorySnapshot): StructuredMemorySnapshot {\n\treturn {\n\t\tentryId: snapshot.entryId,\n\t\tshortId: toShortId(snapshot.entryId),\n\t\tparentId: snapshot.parentId,\n\t\trecordedAt: snapshot.recordedAt,\n\t\titems: [...snapshot.items],\n\t\titemCount: snapshot.items.length,\n\t\tisCurrent: snapshot.isCurrent,\n\t};\n}\n\nfunction toResolvedSelector(input: string, resolution: SnapshotResolution): StructuredResolvedSnapshotSelector {\n\treturn {\n\t\tinput,\n\t\tmatchedInput: resolution.matchedInput,\n\t\tresolvedId: resolution.resolvedId!,\n\t};\n}\n\nfunction toSelectorIssue(\n\tlabel: \"baseline\" | \"target\",\n\tinput: string,\n\tresolution: SnapshotResolution,\n): StructuredSnapshotSelectorIssue {\n\treturn {\n\t\tlabel,\n\t\tinput,\n\t\tmatchedInput: resolution.matchedInput,\n\t\terror: resolution.error!,\n\t\tcandidates: resolution.candidates.map((entryId) => ({\n\t\t\tentryId,\n\t\t\tshortId: toShortId(entryId),\n\t\t})),\n\t};\n}\n\nexport function buildStructuredMemoryHistoryData(\n\tcontext: Pick<MemorySnapshotContractContext, \"getMemoryHistory\">,\n): StructuredMemoryHistoryData {\n\tconst snapshots = context.getMemoryHistory();\n\treturn {\n\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\tsnapshots: snapshots.map(toStructuredMemorySnapshot),\n\t};\n}\n\nexport function buildStructuredMemoryCompareData(\n\tcontext: MemorySnapshotContractContext,\n\tselectors?: { baseline: string; target: string },\n): StructuredMemoryCompareData {\n\tconst snapshots = context.getMemoryHistory();\n\tif (snapshots.length === 0) {\n\t\treturn {\n\t\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\t\tstatus: \"empty_history\",\n\t\t\tsnapshotCount: 0,\n\t\t};\n\t}\n\n\tif (selectors) {\n\t\tconst baselineResolution = context.resolveMemorySnapshotSelector(selectors.baseline);\n\t\tconst targetResolution = context.resolveMemorySnapshotSelector(selectors.target);\n\t\tconst issues: StructuredSnapshotSelectorIssue[] = [];\n\n\t\tif (baselineResolution.error !== undefined) {\n\t\t\tissues.push(toSelectorIssue(\"baseline\", selectors.baseline, baselineResolution));\n\t\t}\n\t\tif (targetResolution.error !== undefined) {\n\t\t\tissues.push(toSelectorIssue(\"target\", selectors.target, targetResolution));\n\t\t}\n\n\t\tif (issues.length > 0) {\n\t\t\treturn {\n\t\t\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\t\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\t\t\tstatus: \"selector_resolution_failed\",\n\t\t\t\tcompareMode: \"explicit\",\n\t\t\t\tsnapshotCount: snapshots.length,\n\t\t\t\tissues,\n\t\t\t};\n\t\t}\n\n\t\tconst baselineSnapshot = baselineResolution.snapshot!;\n\t\tconst targetSnapshot = targetResolution.snapshot!;\n\t\treturn {\n\t\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\t\tstatus: \"ok\",\n\t\t\tcompareMode: \"explicit\",\n\t\t\tbaseline: toStructuredMemorySnapshot(baselineSnapshot),\n\t\t\ttarget: toStructuredMemorySnapshot(targetSnapshot),\n\t\t\tdiff: computeMemorySnapshotDiff(baselineSnapshot, targetSnapshot),\n\t\t\tsameSnapshot: baselineSnapshot.entryId === targetSnapshot.entryId,\n\t\t\tselectors: {\n\t\t\t\tbaseline: toResolvedSelector(selectors.baseline, baselineResolution),\n\t\t\t\ttarget: toResolvedSelector(selectors.target, targetResolution),\n\t\t\t},\n\t\t};\n\t}\n\n\tconst targetSnapshot = snapshots[snapshots.length - 1]!;\n\tconst baselineSnapshot = snapshots.length >= 2 ? snapshots[snapshots.length - 2]! : undefined;\n\tconst diff = computeMemorySnapshotDiff(baselineSnapshot, targetSnapshot);\n\n\tif (!baselineSnapshot) {\n\t\treturn {\n\t\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\t\tstatus: \"initial_snapshot\",\n\t\t\tcompareMode: \"adjacent\",\n\t\t\ttarget: toStructuredMemorySnapshot(targetSnapshot),\n\t\t\tdiff,\n\t\t};\n\t}\n\n\treturn {\n\t\tbranchScope: MEMORY_HISTORY_BRANCH_SCOPE,\n\t\thistoryModel: MEMORY_HISTORY_MODEL,\n\t\tstatus: \"ok\",\n\t\tcompareMode: \"adjacent\",\n\t\tbaseline: toStructuredMemorySnapshot(baselineSnapshot),\n\t\ttarget: toStructuredMemorySnapshot(targetSnapshot),\n\t\tdiff,\n\t\tsameSnapshot: baselineSnapshot.entryId === targetSnapshot.entryId,\n\t};\n}\n"]}