@fgv/ts-agent-memory 5.1.0-36 → 5.1.0-39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/.rush/temp/{f6a88bfdd66517ccb98c2c7ae1be6e6fe9e15d38.tar.log → b82cf6bdece20481260e6bab946179eeec9d7b46.tar.log} +116 -2
  2. package/.rush/temp/chunked-rush-logs/ts-agent-memory.build.chunks.jsonl +4 -4
  3. package/.rush/temp/operation/build/all.log +4 -4
  4. package/.rush/temp/operation/build/log-chunks.jsonl +4 -4
  5. package/.rush/temp/operation/build/state.json +1 -1
  6. package/.rush/temp/shrinkwrap-deps.json +222 -221
  7. package/config/jest.config.json +1 -1
  8. package/dist/index.js +2 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/packlets/ingest/cycleGuard.js +111 -0
  11. package/dist/packlets/ingest/cycleGuard.js.map +1 -0
  12. package/dist/packlets/ingest/hostStages.js +6 -0
  13. package/dist/packlets/ingest/hostStages.js.map +1 -0
  14. package/dist/packlets/ingest/index.js +9 -0
  15. package/dist/packlets/ingest/index.js.map +1 -0
  16. package/dist/packlets/ingest/model.js +6 -0
  17. package/dist/packlets/ingest/model.js.map +1 -0
  18. package/dist/packlets/ingest/orchestrator.js +438 -0
  19. package/dist/packlets/ingest/orchestrator.js.map +1 -0
  20. package/dist/packlets/retrieve/index.js +1 -0
  21. package/dist/packlets/retrieve/index.js.map +1 -1
  22. package/dist/packlets/retrieve/temporalRetrievers.js +172 -0
  23. package/dist/packlets/retrieve/temporalRetrievers.js.map +1 -0
  24. package/dist/packlets/store/fileTreeMemoryStore.js +323 -41
  25. package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -1
  26. package/dist/packlets/tools/index.js +6 -0
  27. package/dist/packlets/tools/index.js.map +1 -0
  28. package/dist/packlets/tools/memoryTools.js +337 -0
  29. package/dist/packlets/tools/memoryTools.js.map +1 -0
  30. package/dist/packlets/types/identityCodec.js +115 -0
  31. package/dist/packlets/types/identityCodec.js.map +1 -1
  32. package/dist/packlets/types/index.js +1 -0
  33. package/dist/packlets/types/index.js.map +1 -1
  34. package/dist/packlets/types/temporal.js +85 -0
  35. package/dist/packlets/types/temporal.js.map +1 -0
  36. package/dist/packlets/types/writePolicy.js +96 -0
  37. package/dist/packlets/types/writePolicy.js.map +1 -1
  38. package/dist/test/unit/converters/antagonistRoundTrip.test.js +95 -0
  39. package/dist/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
  40. package/dist/test/unit/ingest/antagonistCycleAndParity.test.js +273 -0
  41. package/dist/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
  42. package/dist/test/unit/ingest/cycleGuard.test.js +54 -0
  43. package/dist/test/unit/ingest/cycleGuard.test.js.map +1 -0
  44. package/dist/test/unit/ingest/orchestrator.test.js +913 -0
  45. package/dist/test/unit/ingest/orchestrator.test.js.map +1 -0
  46. package/dist/test/unit/retrieve/temporalRetrievers.test.js +182 -0
  47. package/dist/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
  48. package/dist/test/unit/store/antagonistTemporalBoundary.test.js +120 -0
  49. package/dist/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
  50. package/dist/test/unit/store/fileTreeMemoryStore.test.js +74 -7
  51. package/dist/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
  52. package/dist/test/unit/store/temporalStore.test.js +398 -0
  53. package/dist/test/unit/store/temporalStore.test.js.map +1 -0
  54. package/dist/test/unit/tools/memoryTools.test.js +572 -0
  55. package/dist/test/unit/tools/memoryTools.test.js.map +1 -0
  56. package/dist/test/unit/types/temporalCodec.test.js +203 -0
  57. package/dist/test/unit/types/temporalCodec.test.js.map +1 -0
  58. package/dist/test/unit/types/temporalPolicy.test.js +62 -0
  59. package/dist/test/unit/types/temporalPolicy.test.js.map +1 -0
  60. package/dist/ts-agent-memory.d.ts +1042 -10
  61. package/dist/tsdoc-metadata.json +1 -1
  62. package/etc/ts-agent-memory.api.md +284 -0
  63. package/lib/index.d.ts +2 -0
  64. package/lib/index.d.ts.map +1 -1
  65. package/lib/index.js +2 -0
  66. package/lib/index.js.map +1 -1
  67. package/lib/packlets/ingest/cycleGuard.d.ts +41 -0
  68. package/lib/packlets/ingest/cycleGuard.d.ts.map +1 -0
  69. package/lib/packlets/ingest/cycleGuard.js +115 -0
  70. package/lib/packlets/ingest/cycleGuard.js.map +1 -0
  71. package/lib/packlets/ingest/hostStages.d.ts +88 -0
  72. package/lib/packlets/ingest/hostStages.d.ts.map +1 -0
  73. package/lib/packlets/ingest/hostStages.js +7 -0
  74. package/lib/packlets/ingest/hostStages.js.map +1 -0
  75. package/lib/packlets/ingest/index.d.ts +5 -0
  76. package/lib/packlets/ingest/index.d.ts.map +1 -0
  77. package/lib/packlets/ingest/index.js +25 -0
  78. package/lib/packlets/ingest/index.js.map +1 -0
  79. package/lib/packlets/ingest/model.d.ts +177 -0
  80. package/lib/packlets/ingest/model.d.ts.map +1 -0
  81. package/lib/packlets/ingest/model.js +7 -0
  82. package/lib/packlets/ingest/model.js.map +1 -0
  83. package/lib/packlets/ingest/orchestrator.d.ts +206 -0
  84. package/lib/packlets/ingest/orchestrator.d.ts.map +1 -0
  85. package/lib/packlets/ingest/orchestrator.js +442 -0
  86. package/lib/packlets/ingest/orchestrator.js.map +1 -0
  87. package/lib/packlets/retrieve/index.d.ts +1 -0
  88. package/lib/packlets/retrieve/index.d.ts.map +1 -1
  89. package/lib/packlets/retrieve/index.js +1 -0
  90. package/lib/packlets/retrieve/index.js.map +1 -1
  91. package/lib/packlets/retrieve/temporalRetrievers.d.ts +78 -0
  92. package/lib/packlets/retrieve/temporalRetrievers.d.ts.map +1 -0
  93. package/lib/packlets/retrieve/temporalRetrievers.js +178 -0
  94. package/lib/packlets/retrieve/temporalRetrievers.js.map +1 -0
  95. package/lib/packlets/store/fileTreeMemoryStore.d.ts +118 -9
  96. package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -1
  97. package/lib/packlets/store/fileTreeMemoryStore.js +322 -40
  98. package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -1
  99. package/lib/packlets/tools/index.d.ts +2 -0
  100. package/lib/packlets/tools/index.d.ts.map +1 -0
  101. package/lib/packlets/tools/index.js +22 -0
  102. package/lib/packlets/tools/index.js.map +1 -0
  103. package/lib/packlets/tools/memoryTools.d.ts +139 -0
  104. package/lib/packlets/tools/memoryTools.d.ts.map +1 -0
  105. package/lib/packlets/tools/memoryTools.js +341 -0
  106. package/lib/packlets/tools/memoryTools.js.map +1 -0
  107. package/lib/packlets/types/identityCodec.d.ts +86 -0
  108. package/lib/packlets/types/identityCodec.d.ts.map +1 -1
  109. package/lib/packlets/types/identityCodec.js +118 -1
  110. package/lib/packlets/types/identityCodec.js.map +1 -1
  111. package/lib/packlets/types/index.d.ts +1 -0
  112. package/lib/packlets/types/index.d.ts.map +1 -1
  113. package/lib/packlets/types/index.js +1 -0
  114. package/lib/packlets/types/index.js.map +1 -1
  115. package/lib/packlets/types/temporal.d.ts +40 -0
  116. package/lib/packlets/types/temporal.d.ts.map +1 -0
  117. package/lib/packlets/types/temporal.js +92 -0
  118. package/lib/packlets/types/temporal.js.map +1 -0
  119. package/lib/packlets/types/writePolicy.d.ts +49 -0
  120. package/lib/packlets/types/writePolicy.d.ts.map +1 -1
  121. package/lib/packlets/types/writePolicy.js +98 -1
  122. package/lib/packlets/types/writePolicy.js.map +1 -1
  123. package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts +10 -0
  124. package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts.map +1 -0
  125. package/lib/test/unit/converters/antagonistRoundTrip.test.js +97 -0
  126. package/lib/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
  127. package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts +9 -0
  128. package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts.map +1 -0
  129. package/lib/test/unit/ingest/antagonistCycleAndParity.test.js +275 -0
  130. package/lib/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
  131. package/lib/test/unit/ingest/cycleGuard.test.d.ts +2 -0
  132. package/lib/test/unit/ingest/cycleGuard.test.d.ts.map +1 -0
  133. package/lib/test/unit/ingest/cycleGuard.test.js +56 -0
  134. package/lib/test/unit/ingest/cycleGuard.test.js.map +1 -0
  135. package/lib/test/unit/ingest/orchestrator.test.d.ts +2 -0
  136. package/lib/test/unit/ingest/orchestrator.test.d.ts.map +1 -0
  137. package/lib/test/unit/ingest/orchestrator.test.js +915 -0
  138. package/lib/test/unit/ingest/orchestrator.test.js.map +1 -0
  139. package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts +2 -0
  140. package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts.map +1 -0
  141. package/lib/test/unit/retrieve/temporalRetrievers.test.js +184 -0
  142. package/lib/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
  143. package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts +9 -0
  144. package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts.map +1 -0
  145. package/lib/test/unit/store/antagonistTemporalBoundary.test.js +122 -0
  146. package/lib/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
  147. package/lib/test/unit/store/fileTreeMemoryStore.test.js +74 -7
  148. package/lib/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
  149. package/lib/test/unit/store/temporalStore.test.d.ts +2 -0
  150. package/lib/test/unit/store/temporalStore.test.d.ts.map +1 -0
  151. package/lib/test/unit/store/temporalStore.test.js +400 -0
  152. package/lib/test/unit/store/temporalStore.test.js.map +1 -0
  153. package/lib/test/unit/tools/memoryTools.test.d.ts +2 -0
  154. package/lib/test/unit/tools/memoryTools.test.d.ts.map +1 -0
  155. package/lib/test/unit/tools/memoryTools.test.js +574 -0
  156. package/lib/test/unit/tools/memoryTools.test.js.map +1 -0
  157. package/lib/test/unit/types/temporalCodec.test.d.ts +2 -0
  158. package/lib/test/unit/types/temporalCodec.test.d.ts.map +1 -0
  159. package/lib/test/unit/types/temporalCodec.test.js +205 -0
  160. package/lib/test/unit/types/temporalCodec.test.js.map +1 -0
  161. package/lib/test/unit/types/temporalPolicy.test.d.ts +2 -0
  162. package/lib/test/unit/types/temporalPolicy.test.d.ts.map +1 -0
  163. package/lib/test/unit/types/temporalPolicy.test.js +64 -0
  164. package/lib/test/unit/types/temporalPolicy.test.js.map +1 -0
  165. package/package.json +19 -19
  166. package/rush-logs/ts-agent-memory.build.cache.log +1 -1
  167. package/rush-logs/ts-agent-memory.build.log +4 -4
  168. package/src/index.ts +2 -0
  169. package/src/packlets/ingest/cycleGuard.ts +142 -0
  170. package/src/packlets/ingest/hostStages.ts +111 -0
  171. package/src/packlets/ingest/index.ts +9 -0
  172. package/src/packlets/ingest/model.ts +184 -0
  173. package/src/packlets/ingest/orchestrator.ts +797 -0
  174. package/src/packlets/retrieve/index.ts +1 -0
  175. package/src/packlets/retrieve/temporalRetrievers.ts +210 -0
  176. package/src/packlets/store/fileTreeMemoryStore.ts +460 -66
  177. package/src/packlets/tools/index.ts +6 -0
  178. package/src/packlets/tools/memoryTools.ts +579 -0
  179. package/src/packlets/types/identityCodec.ts +184 -0
  180. package/src/packlets/types/index.ts +1 -0
  181. package/src/packlets/types/temporal.ts +96 -0
  182. package/src/packlets/types/writePolicy.ts +127 -0
  183. package/src/test/unit/converters/antagonistRoundTrip.test.ts +110 -0
  184. package/src/test/unit/ingest/antagonistCycleAndParity.test.ts +362 -0
  185. package/src/test/unit/ingest/cycleGuard.test.ts +68 -0
  186. package/src/test/unit/ingest/orchestrator.test.ts +1158 -0
  187. package/src/test/unit/retrieve/temporalRetrievers.test.ts +226 -0
  188. package/src/test/unit/store/antagonistTemporalBoundary.test.ts +158 -0
  189. package/src/test/unit/store/fileTreeMemoryStore.test.ts +98 -7
  190. package/src/test/unit/store/temporalStore.test.ts +469 -0
  191. package/src/test/unit/tools/memoryTools.test.ts +771 -0
  192. package/src/test/unit/types/temporalCodec.test.ts +259 -0
  193. package/src/test/unit/types/temporalPolicy.test.ts +96 -0
  194. package/temp/build/lint/_eslint-5eVG3S6w.json +122 -46
  195. package/temp/build/typescript/ts_8nwakTlr.json +1 -1
  196. package/temp/ts-agent-memory.api.json +11984 -6314
  197. package/temp/ts-agent-memory.api.md +284 -0
@@ -0,0 +1,1158 @@
1
+ /*
2
+ * Copyright (c) 2026 Erik Fortune
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import '@fgv/ts-utils-jest';
7
+ import { Converters, Logging, Result, fail, succeed } from '@fgv/ts-utils';
8
+ import { FileTree } from '@fgv/ts-json-base';
9
+ import {
10
+ BodyConverterRegistry,
11
+ CONTRADICTS_LINK_TYPE,
12
+ DEFAULT_SIMILARITY_THRESHOLD,
13
+ DEFAULT_SIMILARITY_TOP_K,
14
+ EntityId,
15
+ FileTreeMemoryStore,
16
+ HOST_INGEST_PROVENANCE_SOURCE,
17
+ IBodyConverterRegistry,
18
+ ICandidateEdge,
19
+ ICandidateRecord,
20
+ IEntityResolutionCandidate,
21
+ IEntityResolver,
22
+ IFactExtractor,
23
+ IIdentityCodec,
24
+ IIngestItem,
25
+ IIngestItemResult,
26
+ InMemoryCosineIndex,
27
+ IMemoryClassification,
28
+ IMemoryClassifier,
29
+ IMemoryEnvelope,
30
+ IMemoryRecord,
31
+ IMemoryStore,
32
+ IProvenance,
33
+ IRelationContext,
34
+ IRelationExtractor,
35
+ IVectorIndex,
36
+ IVectorQueryHit,
37
+ IWritePolicy,
38
+ Kind,
39
+ KnowledgeIdentityCodec,
40
+ LinkType,
41
+ LtmIdentityCodec,
42
+ MemoryEmbedder,
43
+ MemoryId,
44
+ MemoryIngestOrchestrator,
45
+ MemoryScopeKey,
46
+ ResolutionVerdict,
47
+ Tag,
48
+ TemporalIdentityCodec,
49
+ TemporalVersionedPolicy,
50
+ serializeMemoryFile
51
+ } from '../../../index';
52
+
53
+ // --- kinds --------------------------------------------------------------------
54
+ const noteKind: Kind = 'note' as Kind;
55
+ const factKind: Kind = 'fact' as Kind;
56
+ const numKind: Kind = 'num' as Kind;
57
+ const orphanKind: Kind = 'orphan' as Kind;
58
+ const ltmKind: Kind = 'ltm' as Kind;
59
+
60
+ // --- shared fixtures ----------------------------------------------------------
61
+ let clockValue: number;
62
+ const clock = (): number => clockValue;
63
+
64
+ function mutableRoot(
65
+ files: ReadonlyArray<{ path: string; contents: string }> = []
66
+ ): FileTree.IMutableFileTreeDirectoryItem {
67
+ const tree = FileTree.inMemory([...files], { mutable: true }).orThrow();
68
+ const root = tree.getDirectory('/').orThrow();
69
+ if (!FileTree.isMutableDirectoryItem(root)) {
70
+ throw new Error('expected a mutable root directory');
71
+ }
72
+ return root;
73
+ }
74
+
75
+ function registry(): IBodyConverterRegistry {
76
+ const reg = BodyConverterRegistry.create().orThrow();
77
+ reg.register(noteKind, Converters.string);
78
+ reg.register(factKind, Converters.string);
79
+ reg.register(numKind, Converters.number);
80
+ reg.register(orphanKind, Converters.string);
81
+ reg.register(ltmKind, Converters.string);
82
+ return reg;
83
+ }
84
+
85
+ const codecs: ReadonlyMap<Kind, IIdentityCodec> = new Map<Kind, IIdentityCodec>([
86
+ [noteKind, new KnowledgeIdentityCodec()],
87
+ [numKind, new KnowledgeIdentityCodec()],
88
+ [ltmKind, new LtmIdentityCodec()],
89
+ [factKind, TemporalIdentityCodec.create('facts').orThrow()]
90
+ ]);
91
+
92
+ const policies: ReadonlyMap<Kind, IWritePolicy> = new Map<Kind, IWritePolicy>([
93
+ [factKind, TemporalVersionedPolicy.create().orThrow()]
94
+ ]);
95
+
96
+ // A deterministic embedder keyed by body text (three-dimensional vectors so
97
+ // cosine relationships are controllable). Unknown bodies map to a distinct axis.
98
+ const VECTORS: ReadonlyMap<string, ReadonlyArray<number>> = new Map<string, ReadonlyArray<number>>([
99
+ ['apple pie', [1, 0, 0]],
100
+ ['apple tart', [0.99, 0.14, 0]],
101
+ ['banana bread', [0, 1, 0]]
102
+ ]);
103
+ const embed: MemoryEmbedder = (record) => {
104
+ if (record.body === 'boom') {
105
+ return Promise.resolve(fail('embed boom'));
106
+ }
107
+ const vec: ReadonlyArray<number> = VECTORS.get(record.body as string) ?? [0, 0, 1];
108
+ return Promise.resolve(succeed(Float32Array.from(vec)));
109
+ };
110
+
111
+ // --- mock host stages ---------------------------------------------------------
112
+ function classifier(fn: (item: IIngestItem) => Result<IMemoryClassification>): IMemoryClassifier {
113
+ return { classify: (item) => Promise.resolve(fn(item)) };
114
+ }
115
+ function throwingClassifier(): IMemoryClassifier {
116
+ return {
117
+ classify: () => {
118
+ throw new Error('classifier boom');
119
+ }
120
+ };
121
+ }
122
+ function extractor(
123
+ fn: (item: IIngestItem, c: IMemoryClassification) => Result<ReadonlyArray<ICandidateRecord>>
124
+ ): IFactExtractor {
125
+ return { extract: (item, c) => Promise.resolve(fn(item, c)) };
126
+ }
127
+ function resolver(
128
+ fn: (
129
+ candidate: ICandidateRecord,
130
+ similar: ReadonlyArray<IEntityResolutionCandidate>
131
+ ) => Result<ResolutionVerdict>
132
+ ): IEntityResolver {
133
+ return { resolve: (candidate, similar) => Promise.resolve(fn(candidate, similar)) };
134
+ }
135
+ function relater(fn: (ctx: IRelationContext) => Result<ReadonlyArray<ICandidateEdge>>): IRelationExtractor {
136
+ return { relate: (ctx) => Promise.resolve(fn(ctx)) };
137
+ }
138
+
139
+ const noEdges: IRelationExtractor = relater(() => succeed([]));
140
+ const classifyNote: IMemoryClassifier = classifier(() => succeed({ kind: noteKind, confidence: 0.9 }));
141
+
142
+ /** Extractor that emits one note candidate per item, body = item.content, entityId = item.id. */
143
+ function oneNoteExtractor(): IFactExtractor {
144
+ return extractor((item) => succeed([candidate(noteKind, item.id, item.content as string)]));
145
+ }
146
+
147
+ function candidate(
148
+ kind: Kind,
149
+ entityId: string,
150
+ body: unknown,
151
+ opts: {
152
+ readonly tags?: ReadonlyArray<string>;
153
+ readonly links?: ICandidateRecord['envelope']['links'];
154
+ readonly provenance?: IProvenance;
155
+ readonly temporal?: IMemoryEnvelope['temporal'];
156
+ } = {}
157
+ ): ICandidateRecord {
158
+ return {
159
+ envelope: {
160
+ entityId: entityId as EntityId,
161
+ kind,
162
+ tags: (opts.tags ?? []) as ReadonlyArray<Tag>,
163
+ links: opts.links ?? [],
164
+ provenance: opts.provenance ?? { source: 'agent' },
165
+ ...(opts.temporal !== undefined ? { temporal: opts.temporal } : {})
166
+ },
167
+ body
168
+ };
169
+ }
170
+
171
+ function candEdge(source: string, type: string, target: string, confidence?: number): ICandidateEdge {
172
+ return {
173
+ source: source as MemoryId,
174
+ edge: {
175
+ type: type as LinkType,
176
+ target: target as MemoryId,
177
+ ...(confidence !== undefined ? { confidence } : {})
178
+ }
179
+ };
180
+ }
181
+
182
+ interface IBuildParams {
183
+ readonly files?: ReadonlyArray<{ path: string; contents: string }>;
184
+ readonly registry?: IBodyConverterRegistry;
185
+ readonly vectorIndex?: IVectorIndex;
186
+ readonly embed?: MemoryEmbedder;
187
+ }
188
+
189
+ function buildStore(params: IBuildParams = {}): FileTreeMemoryStore {
190
+ return FileTreeMemoryStore.create({
191
+ root: mutableRoot(params.files),
192
+ registry: params.registry ?? registry(),
193
+ codecs,
194
+ writePolicies: policies,
195
+ clock,
196
+ vectorIndex: params.vectorIndex,
197
+ embed: params.embed
198
+ }).orThrow();
199
+ }
200
+
201
+ interface IOrchestratorParams {
202
+ readonly store: IMemoryStore;
203
+ readonly registry?: IBodyConverterRegistry;
204
+ readonly classifier?: IMemoryClassifier;
205
+ readonly extractor?: IFactExtractor;
206
+ readonly relationExtractor?: IRelationExtractor;
207
+ readonly entityResolver?: IEntityResolver;
208
+ readonly vectorIndex?: IVectorIndex;
209
+ readonly embed?: MemoryEmbedder;
210
+ readonly similarityThreshold?: number;
211
+ readonly cycleGuard?: 'reject' | 'off';
212
+ }
213
+
214
+ function buildOrchestrator(params: IOrchestratorParams): MemoryIngestOrchestrator {
215
+ return MemoryIngestOrchestrator.create({
216
+ store: params.store,
217
+ registry: params.registry ?? registry(),
218
+ codecs,
219
+ classifier: params.classifier ?? classifyNote,
220
+ extractor: params.extractor ?? oneNoteExtractor(),
221
+ relationExtractor: params.relationExtractor ?? noEdges,
222
+ entityResolver: params.entityResolver,
223
+ vectorIndex: params.vectorIndex,
224
+ embed: params.embed,
225
+ similarityThreshold: params.similarityThreshold,
226
+ cycleGuard: params.cycleGuard
227
+ }).orThrow();
228
+ }
229
+
230
+ /** A seeded note file with only `source` provenance (the additive-provenance baseline). */
231
+ function seedNote(
232
+ id: string,
233
+ body: string,
234
+ provenance: Record<string, unknown> = { source: 'agent' }
235
+ ): {
236
+ path: string;
237
+ contents: string;
238
+ } {
239
+ const envelope: IMemoryEnvelope = {
240
+ id: id as MemoryId,
241
+ entityId: id as EntityId,
242
+ kind: noteKind,
243
+ tags: [],
244
+ links: [],
245
+ created: 1,
246
+ updated: 1,
247
+ seq: 4,
248
+ contentHash: 'seed',
249
+ provenance: provenance as unknown as IProvenance
250
+ };
251
+ return { path: `knowledge/${id}.md`, contents: serializeMemoryFile(envelope, body).orThrow() };
252
+ }
253
+
254
+ beforeEach(() => {
255
+ clockValue = 1000;
256
+ });
257
+
258
+ describe('MemoryIngestOrchestrator', () => {
259
+ describe('create', () => {
260
+ test('constructs via the factory', () => {
261
+ expect(
262
+ MemoryIngestOrchestrator.create({
263
+ store: buildStore(),
264
+ registry: registry(),
265
+ codecs,
266
+ classifier: classifyNote,
267
+ extractor: oneNoteExtractor(),
268
+ relationExtractor: noEdges
269
+ })
270
+ ).toSucceed();
271
+ });
272
+
273
+ test('exposes the documented similarity defaults', () => {
274
+ expect(DEFAULT_SIMILARITY_THRESHOLD).toBe(0.85);
275
+ expect(DEFAULT_SIMILARITY_TOP_K).toBe(5);
276
+ });
277
+ });
278
+
279
+ describe('single-item ingest (first-class path)', () => {
280
+ test('classifies, extracts, and writes one record end-to-end with host-ingest provenance + derivedFrom', async () => {
281
+ const store = buildStore();
282
+ const orch = buildOrchestrator({ store });
283
+ const sourceId: MemoryId = 'turn-3' as MemoryId;
284
+ const result = await orch.ingestItem({ id: 'doc-1', content: 'the sky is blue', sourceId });
285
+ expect(result).toSucceedAndSatisfy((r: IIngestItemResult) => {
286
+ expect(r.item.id).toBe('doc-1');
287
+ expect(r.records).toHaveLength(1);
288
+ const rec = r.records[0];
289
+ expect(rec.disposition).toBe('written');
290
+ expect(rec.resolution.verdict).toBe('new');
291
+ expect(rec.id).toBe('doc-1');
292
+ expect(rec.record?.envelope.provenance.source).toBe(HOST_INGEST_PROVENANCE_SOURCE);
293
+ expect(rec.record?.envelope.provenance.derivedFrom).toBe('turn-3');
294
+ expect(rec.record?.body).toBe('the sky is blue');
295
+ expect(rec.interlock).toBeUndefined();
296
+ });
297
+ expect(await store.get(noteKind, 'doc-1' as EntityId)).toSucceedAndSatisfy(
298
+ (rec: IMemoryRecord<unknown> | undefined) => {
299
+ expect(rec?.body).toBe('the sky is blue');
300
+ }
301
+ );
302
+ });
303
+
304
+ test('omits derivedFrom when the item carries no sourceId', async () => {
305
+ const store = buildStore();
306
+ const orch = buildOrchestrator({ store });
307
+ const result = await orch.ingestItem({ id: 'doc-1', content: 'a fact' });
308
+ expect(result).toSucceedAndSatisfy((r: IIngestItemResult) => {
309
+ expect(r.records[0].record?.envelope.provenance.derivedFrom).toBeUndefined();
310
+ expect(r.records[0].record?.envelope.provenance.source).toBe(HOST_INGEST_PROVENANCE_SOURCE);
311
+ });
312
+ });
313
+
314
+ test('yields an empty record set when the extractor finds nothing memorable', async () => {
315
+ const store = buildStore();
316
+ const orch = buildOrchestrator({ store, extractor: extractor(() => succeed([])) });
317
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'noise' })).toSucceedAndSatisfy(
318
+ (r: IIngestItemResult) => {
319
+ expect(r.records).toEqual([]);
320
+ }
321
+ );
322
+ });
323
+ });
324
+
325
+ describe('host-stage failures abort loudly', () => {
326
+ test('classifier failure', async () => {
327
+ const orch = buildOrchestrator({
328
+ store: buildStore(),
329
+ classifier: classifier(() => fail('cannot classify'))
330
+ });
331
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(/cannot classify/);
332
+ });
333
+
334
+ test('classifier throw is captured as a failure', async () => {
335
+ const orch = buildOrchestrator({ store: buildStore(), classifier: throwingClassifier() });
336
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(
337
+ /classify threw.*classifier boom/
338
+ );
339
+ });
340
+
341
+ test('extractor failure', async () => {
342
+ const orch = buildOrchestrator({
343
+ store: buildStore(),
344
+ extractor: extractor(() => fail('extract error'))
345
+ });
346
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(/extract error/);
347
+ });
348
+
349
+ test('relation-extractor failure', async () => {
350
+ const orch = buildOrchestrator({
351
+ store: buildStore(),
352
+ relationExtractor: relater(() => fail('relate error'))
353
+ });
354
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(/relate error/);
355
+ });
356
+ });
357
+
358
+ describe('stage 3b — typed validation boundary', () => {
359
+ test('rejects a candidate whose body fails the kind converter', async () => {
360
+ const orch = buildOrchestrator({
361
+ store: buildStore(),
362
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-1', 42)]))
363
+ });
364
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(
365
+ /candidate body for kind 'note' is invalid/
366
+ );
367
+ });
368
+
369
+ test('rejects a non-string body even when the converter accepts it', async () => {
370
+ const orch = buildOrchestrator({
371
+ store: buildStore(),
372
+ extractor: extractor(() => succeed([candidate(numKind, 'n-1', 42)]))
373
+ });
374
+ expect(await orch.ingestItem({ id: 'n-1', content: 'x' })).toFailWith(/must be a string/);
375
+ });
376
+
377
+ test('fails when no identity codec is registered for the candidate kind', async () => {
378
+ const orch = buildOrchestrator({
379
+ store: buildStore(),
380
+ extractor: extractor(() => succeed([candidate(orphanKind, 'o-1', 'body')]))
381
+ });
382
+ expect(await orch.ingestItem({ id: 'o-1', content: 'x' })).toFailWith(/no identity codec/);
383
+ });
384
+ });
385
+
386
+ describe('stage 4 — exact dedup (layer 1)', () => {
387
+ test('an exact {kind,body} match in scope is duplicate-of (no resolver)', async () => {
388
+ const store = buildStore({ files: [seedNote('doc-a', 'hello')] });
389
+ const orch = buildOrchestrator({
390
+ store,
391
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'hello')]))
392
+ });
393
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
394
+ expect(r.records[0].disposition).toBe('deduped');
395
+ expect(r.records[0].resolution).toEqual({ verdict: 'duplicate-of', target: 'doc-a' });
396
+ expect(r.records[0].id).toBe('doc-a');
397
+ });
398
+ // doc-b was never written.
399
+ expect(await store.get(noteKind, 'doc-b' as EntityId)).toSucceedWith(undefined);
400
+ });
401
+
402
+ test('exact dedup takes precedence over the resolver (resolver never called)', async () => {
403
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
404
+ const store = buildStore({ files: [seedNote('doc-a', 'hello')] });
405
+ const orch = buildOrchestrator({
406
+ store,
407
+ entityResolver: resolver(() => fail('resolver must not be called')),
408
+ vectorIndex,
409
+ embed,
410
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'hello')]))
411
+ });
412
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
413
+ expect(r.records[0].disposition).toBe('deduped');
414
+ });
415
+ });
416
+
417
+ test('a distinct body is a fresh write (no resolver → exact-only fall-back)', async () => {
418
+ const store = buildStore({ files: [seedNote('doc-a', 'hello')] });
419
+ const orch = buildOrchestrator({
420
+ store,
421
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'goodbye')]))
422
+ });
423
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
424
+ expect(r.records[0].disposition).toBe('written');
425
+ expect(r.records[0].resolution.verdict).toBe('new');
426
+ });
427
+ });
428
+ });
429
+ });
430
+
431
+ // --- similarity + resolver fixtures ------------------------------------------
432
+ const classifyFact: IMemoryClassifier = classifier(() => succeed({ kind: factKind }));
433
+ function oneFactExtractor(): IFactExtractor {
434
+ return extractor((item) => succeed([candidate(factKind, item.id, item.content as string)]));
435
+ }
436
+
437
+ async function putNote(store: FileTreeMemoryStore, id: string, body: string): Promise<void> {
438
+ const rec: IMemoryRecord<unknown> = {
439
+ envelope: {
440
+ id: id as MemoryId,
441
+ entityId: id as EntityId,
442
+ kind: noteKind,
443
+ tags: [],
444
+ links: [],
445
+ created: 0,
446
+ updated: 0,
447
+ seq: 0,
448
+ contentHash: '',
449
+ provenance: { source: 'agent' }
450
+ },
451
+ body
452
+ };
453
+ (await store.put(rec)).orThrow();
454
+ }
455
+
456
+ function mockStore(overrides: Partial<IMemoryStore>): IMemoryStore {
457
+ return {
458
+ get:
459
+ overrides.get ??
460
+ ((): Promise<Result<IMemoryRecord<unknown> | undefined>> => Promise.resolve(succeed(undefined))),
461
+ getById:
462
+ overrides.getById ??
463
+ ((): Promise<Result<IMemoryRecord<unknown> | undefined>> => Promise.resolve(succeed(undefined))),
464
+ list:
465
+ overrides.list ??
466
+ ((): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> => Promise.resolve(succeed([]))),
467
+ put: overrides.put ?? ((r): Promise<Result<IMemoryRecord<unknown>>> => Promise.resolve(succeed(r))),
468
+ delete: overrides.delete ?? ((): Promise<Result<MemoryId>> => Promise.resolve(fail('n/a')))
469
+ };
470
+ }
471
+
472
+ describe('MemoryIngestOrchestrator — stage 4 similarity (layer 2)', () => {
473
+ test('resolver verdict new writes the candidate despite a near-duplicate', async () => {
474
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
475
+ const store = buildStore({ vectorIndex, embed });
476
+ await putNote(store, 'doc-a', 'apple pie');
477
+ const orch = buildOrchestrator({
478
+ store,
479
+ vectorIndex,
480
+ embed,
481
+ entityResolver: resolver(() => succeed({ verdict: 'new' })),
482
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
483
+ });
484
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
485
+ expect(r.records[0].disposition).toBe('written');
486
+ expect(r.records[0].resolution.verdict).toBe('new');
487
+ });
488
+ });
489
+
490
+ test('resolver verdict duplicate-of dedups against the surfaced neighbor', async () => {
491
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
492
+ const store = buildStore({ vectorIndex, embed });
493
+ await putNote(store, 'doc-a', 'apple pie');
494
+ const orch = buildOrchestrator({
495
+ store,
496
+ vectorIndex,
497
+ embed,
498
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'duplicate-of', target: similar[0].id })),
499
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
500
+ });
501
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
502
+ expect(r.records[0].disposition).toBe('deduped');
503
+ expect(r.records[0].id).toBe('doc-a');
504
+ });
505
+ expect(await store.get(noteKind, 'doc-b' as EntityId)).toSucceedWith(undefined);
506
+ });
507
+
508
+ test('resolver verdict supersede writes the candidate and records the superseded target', async () => {
509
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
510
+ const store = buildStore({ vectorIndex, embed });
511
+ await putNote(store, 'doc-a', 'apple pie');
512
+ const orch = buildOrchestrator({
513
+ store,
514
+ vectorIndex,
515
+ embed,
516
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'supersede', target: similar[0].id })),
517
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
518
+ });
519
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
520
+ expect(r.records[0].disposition).toBe('written');
521
+ expect(r.records[0].resolution).toEqual({ verdict: 'supersede', target: 'doc-a' });
522
+ expect(r.records[0].id).toBe('doc-b');
523
+ });
524
+ expect(await store.get(noteKind, 'doc-b' as EntityId)).toSucceedAndSatisfy(
525
+ (rec: IMemoryRecord<unknown> | undefined) => expect(rec?.body).toBe('apple tart')
526
+ );
527
+ });
528
+
529
+ test('resolver verdict merge-into re-addresses the write to the target entity', async () => {
530
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
531
+ const store = buildStore({ vectorIndex, embed });
532
+ await putNote(store, 'doc-a', 'apple pie');
533
+ const orch = buildOrchestrator({
534
+ store,
535
+ vectorIndex,
536
+ embed,
537
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'merge-into', target: similar[0].id })),
538
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
539
+ });
540
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
541
+ expect(r.records[0].disposition).toBe('merged');
542
+ expect(r.records[0].id).toBe('doc-a');
543
+ });
544
+ // The merge updated doc-a's body (LWW merge patch) and never minted doc-b.
545
+ expect(await store.get(noteKind, 'doc-a' as EntityId)).toSucceedAndSatisfy(
546
+ (rec: IMemoryRecord<unknown> | undefined) => expect(rec?.body).toBe('apple tart')
547
+ );
548
+ expect(await store.get(noteKind, 'doc-b' as EntityId)).toSucceedWith(undefined);
549
+ });
550
+
551
+ test('merge-into with a non-existent target fails loudly', async () => {
552
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
553
+ const store = buildStore({ vectorIndex, embed });
554
+ await putNote(store, 'doc-a', 'apple pie');
555
+ const orch = buildOrchestrator({
556
+ store,
557
+ vectorIndex,
558
+ embed,
559
+ entityResolver: resolver(() => succeed({ verdict: 'merge-into', target: 'ghost' as MemoryId })),
560
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
561
+ });
562
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
563
+ /merge-into target 'ghost' does not exist/
564
+ );
565
+ });
566
+
567
+ test('a below-threshold neighbor is not surfaced (resolver not called)', async () => {
568
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
569
+ const store = buildStore({ vectorIndex, embed });
570
+ await putNote(store, 'doc-a', 'apple pie');
571
+ const orch = buildOrchestrator({
572
+ store,
573
+ vectorIndex,
574
+ embed,
575
+ entityResolver: resolver(() => fail('resolver must not be called')),
576
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'banana bread')]))
577
+ });
578
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
579
+ expect(r.records[0].resolution.verdict).toBe('new');
580
+ });
581
+ });
582
+
583
+ test('the candidate self-hit is excluded from surfaced neighbors', async () => {
584
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
585
+ const store = buildStore({ vectorIndex, embed });
586
+ await putNote(store, 'doc-a', 'apple pie');
587
+ const orch = buildOrchestrator({
588
+ store,
589
+ vectorIndex,
590
+ embed,
591
+ entityResolver: resolver(() => fail('resolver must not be called')),
592
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-a', 'apple tart')]))
593
+ });
594
+ // Re-ingesting the SAME entity: its own prior vector is the only over-threshold
595
+ // hit, and it is skipped as the self-hit, so no resolver dispatch occurs.
596
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
597
+ expect(r.records[0].resolution.verdict).toBe('new');
598
+ expect(r.records[0].disposition).toBe('written');
599
+ });
600
+ });
601
+
602
+ test('a surfaced hit with no backing record is skipped', async () => {
603
+ const ghostIndex: IVectorIndex = {
604
+ add: (id) => Promise.resolve(succeed(id as string)),
605
+ remove: (id) => Promise.resolve(succeed(id)),
606
+ query: (): Promise<Result<ReadonlyArray<IVectorQueryHit>>> =>
607
+ Promise.resolve(succeed([{ id: 'ghost' as MemoryId, score: 0.99 }]))
608
+ };
609
+ const orch = buildOrchestrator({
610
+ store: buildStore(),
611
+ vectorIndex: ghostIndex,
612
+ embed,
613
+ entityResolver: resolver(() => fail('resolver must not be called')),
614
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
615
+ });
616
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
617
+ expect(r.records[0].resolution.verdict).toBe('new');
618
+ });
619
+ });
620
+
621
+ test('an embedder failure surfaces loudly', async () => {
622
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
623
+ const orch = buildOrchestrator({
624
+ store: buildStore(),
625
+ vectorIndex,
626
+ embed,
627
+ entityResolver: resolver(() => succeed({ verdict: 'new' })),
628
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'boom')]))
629
+ });
630
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(/embed boom/);
631
+ });
632
+
633
+ test('a similarity-query failure surfaces loudly', async () => {
634
+ const failingIndex: IVectorIndex = {
635
+ add: (id) => Promise.resolve(succeed(id as string)),
636
+ remove: (id) => Promise.resolve(succeed(id)),
637
+ query: (): Promise<Result<ReadonlyArray<IVectorQueryHit>>> => Promise.resolve(fail('query kaput'))
638
+ };
639
+ const orch = buildOrchestrator({
640
+ store: buildStore(),
641
+ vectorIndex: failingIndex,
642
+ embed,
643
+ entityResolver: resolver(() => succeed({ verdict: 'new' })),
644
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
645
+ });
646
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(/query kaput/);
647
+ });
648
+
649
+ test('a custom similarityThreshold gates the resolver dispatch', async () => {
650
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
651
+ const store = buildStore({ vectorIndex, embed });
652
+ await putNote(store, 'doc-a', 'apple pie');
653
+ const orch = buildOrchestrator({
654
+ store,
655
+ vectorIndex,
656
+ embed,
657
+ similarityThreshold: 0.999, // 0.99 neighbor now falls below threshold
658
+ entityResolver: resolver(() => fail('resolver must not be called')),
659
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
660
+ });
661
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
662
+ expect(r.records[0].resolution.verdict).toBe('new');
663
+ });
664
+ });
665
+ });
666
+
667
+ describe('MemoryIngestOrchestrator — layer-2 wiring gates (exact-only fall-back)', () => {
668
+ const failResolver: IEntityResolver = resolver(() => fail('resolver must not be called'));
669
+ const almostSimilar = extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]));
670
+
671
+ test('no resolver → exact-only', async () => {
672
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
673
+ const store = buildStore({ vectorIndex, embed });
674
+ await putNote(store, 'doc-a', 'apple pie');
675
+ const orch = buildOrchestrator({ store, vectorIndex, embed, extractor: almostSimilar });
676
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) =>
677
+ expect(r.records[0].resolution.verdict).toBe('new')
678
+ );
679
+ });
680
+
681
+ test('resolver + embed but no vector index → exact-only', async () => {
682
+ const orch = buildOrchestrator({
683
+ store: buildStore(),
684
+ embed,
685
+ entityResolver: failResolver,
686
+ extractor: almostSimilar
687
+ });
688
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) =>
689
+ expect(r.records[0].resolution.verdict).toBe('new')
690
+ );
691
+ });
692
+
693
+ test('resolver + vector index but no embedder → exact-only', async () => {
694
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
695
+ const orch = buildOrchestrator({
696
+ store: buildStore(),
697
+ vectorIndex,
698
+ entityResolver: failResolver,
699
+ extractor: almostSimilar
700
+ });
701
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) =>
702
+ expect(r.records[0].resolution.verdict).toBe('new')
703
+ );
704
+ });
705
+ });
706
+
707
+ describe('MemoryIngestOrchestrator — stage 5 relate + cycle guard', () => {
708
+ test('attaches a valid edge to an existing record', async () => {
709
+ const store = buildStore();
710
+ await putNote(store, 'doc-a', 'anchor');
711
+ const orch = buildOrchestrator({
712
+ store,
713
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'goodbye')])),
714
+ relationExtractor: relater(() => succeed([candEdge('doc-b', 'mentions', 'doc-a', 0.7)]))
715
+ });
716
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
717
+ expect(r.records[0].edges).toHaveLength(1);
718
+ expect(r.records[0].record?.envelope.links).toEqual([
719
+ { type: 'mentions', target: 'doc-a', confidence: 0.7 }
720
+ ]);
721
+ });
722
+ });
723
+
724
+ test('attaches an edge between two sibling candidates', async () => {
725
+ const orch = buildOrchestrator({
726
+ store: buildStore(),
727
+ extractor: extractor(() =>
728
+ succeed([candidate(noteKind, 'doc-b', 'first'), candidate(noteKind, 'doc-c', 'second')])
729
+ ),
730
+ relationExtractor: relater(() => succeed([candEdge('doc-b', 'rel', 'doc-c')]))
731
+ });
732
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
733
+ expect(r.records).toHaveLength(2);
734
+ expect(r.records[0].record?.envelope.links).toEqual([{ type: 'rel', target: 'doc-c' }]);
735
+ expect(r.records[1].edges).toEqual([]);
736
+ });
737
+ });
738
+
739
+ test('rejects an edge whose source is not a candidate being written', async () => {
740
+ const orch = buildOrchestrator({
741
+ store: buildStore(),
742
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'x')])),
743
+ relationExtractor: relater(() => succeed([candEdge('ghost', 'rel', 'doc-b')]))
744
+ });
745
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
746
+ /edge source 'ghost' is not a candidate being written/
747
+ );
748
+ });
749
+
750
+ test('rejects an edge whose target resolves to nothing', async () => {
751
+ const orch = buildOrchestrator({
752
+ store: buildStore(),
753
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'x')])),
754
+ relationExtractor: relater(() => succeed([candEdge('doc-b', 'rel', 'ghost')]))
755
+ });
756
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
757
+ /edge target 'ghost' resolves to neither/
758
+ );
759
+ });
760
+
761
+ test('rejects a cycle-inducing edge (default guard)', async () => {
762
+ const orch = buildOrchestrator({
763
+ store: buildStore(),
764
+ extractor: extractor(() =>
765
+ succeed([candidate(noteKind, 'doc-b', 'first'), candidate(noteKind, 'doc-c', 'second')])
766
+ ),
767
+ relationExtractor: relater(() =>
768
+ succeed([candEdge('doc-b', 'rel', 'doc-c'), candEdge('doc-c', 'rel', 'doc-b')])
769
+ )
770
+ });
771
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(/would create a cycle/);
772
+ });
773
+
774
+ test('cycleGuard "off" admits an otherwise-cyclic edge set', async () => {
775
+ const orch = buildOrchestrator({
776
+ store: buildStore(),
777
+ cycleGuard: 'off',
778
+ extractor: extractor(() =>
779
+ succeed([candidate(noteKind, 'doc-b', 'first'), candidate(noteKind, 'doc-c', 'second')])
780
+ ),
781
+ relationExtractor: relater(() =>
782
+ succeed([candEdge('doc-b', 'rel', 'doc-c'), candEdge('doc-c', 'rel', 'doc-b')])
783
+ )
784
+ });
785
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
786
+ expect(r.records[0].record?.envelope.links).toEqual([{ type: 'rel', target: 'doc-c' }]);
787
+ expect(r.records[1].record?.envelope.links).toEqual([{ type: 'rel', target: 'doc-b' }]);
788
+ });
789
+ });
790
+
791
+ test('a contradicts edge on a NON-temporal kind persists as a plain link (no interlock)', async () => {
792
+ const store = buildStore();
793
+ await putNote(store, 'doc-a', 'anchor');
794
+ const orch = buildOrchestrator({
795
+ store,
796
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'goodbye')])),
797
+ relationExtractor: relater(() => succeed([candEdge('doc-b', 'contradicts', 'doc-a')]))
798
+ });
799
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
800
+ expect(r.records[0].interlock).toBeUndefined();
801
+ expect(r.records[0].record?.envelope.links).toEqual([{ type: 'contradicts', target: 'doc-a' }]);
802
+ });
803
+ });
804
+ });
805
+
806
+ describe('MemoryIngestOrchestrator — contradicts→temporal interlock', () => {
807
+ test('a contradicts edge on a temporal kind invalidates the prior version and writes a new one', async () => {
808
+ const store = buildStore();
809
+ const firstOrch = buildOrchestrator({
810
+ store,
811
+ classifier: classifyFact,
812
+ extractor: oneFactExtractor()
813
+ });
814
+ clockValue = 1000;
815
+ const first = await firstOrch.ingestItem({ id: 'fact-1', content: 'the sky is blue' });
816
+ const v1Id: MemoryId = first.orThrow().records[0].id;
817
+ expect(v1Id).toBe('fact-1-v1');
818
+
819
+ clockValue = 2000;
820
+ const secondOrch = buildOrchestrator({
821
+ store,
822
+ classifier: classifyFact,
823
+ extractor: extractor(() => succeed([candidate(factKind, 'fact-1', 'the sky is grey')])),
824
+ relationExtractor: relater(() => succeed([candEdge('fact-1', 'contradicts', v1Id)]))
825
+ });
826
+ expect(await secondOrch.ingestItem({ id: 'fact-1', content: 'the sky is grey' })).toSucceedAndSatisfy(
827
+ (r: IIngestItemResult) => {
828
+ expect(r.records[0].interlock).toBe('temporal-versioned');
829
+ expect(r.records[0].disposition).toBe('written');
830
+ expect(r.records[0].record?.envelope.links).toEqual([{ type: CONTRADICTS_LINK_TYPE, target: v1Id }]);
831
+ }
832
+ );
833
+
834
+ // The current version is the new (contradicting) one.
835
+ expect(await store.get(factKind, 'fact-1' as EntityId)).toSucceedAndSatisfy(
836
+ (rec: IMemoryRecord<unknown> | undefined) => expect(rec?.body).toBe('the sky is grey')
837
+ );
838
+ // The prior version is invalidated (invalidate-don't-delete).
839
+ expect(await store.list()).toSucceedAndSatisfy((all: ReadonlyArray<IMemoryRecord<unknown>>) => {
840
+ const v1 = all.find((rec) => rec.envelope.id === v1Id);
841
+ expect(v1?.envelope.temporal?.invalid_at).toBe(2000);
842
+ });
843
+ });
844
+
845
+ test('an invalidated version does not exact-dedup a later candidate that repeats its body', async () => {
846
+ const store = buildStore();
847
+ clockValue = 1000;
848
+ const o1 = buildOrchestrator({ store, classifier: classifyFact, extractor: oneFactExtractor() });
849
+ const first = await o1.ingestItem({ id: 'fact-1', content: 'blue' });
850
+ const v1Id: MemoryId = first.orThrow().records[0].id;
851
+
852
+ clockValue = 2000;
853
+ const o2 = buildOrchestrator({
854
+ store,
855
+ classifier: classifyFact,
856
+ extractor: extractor(() => succeed([candidate(factKind, 'fact-1', 'grey')])),
857
+ relationExtractor: relater(() => succeed([candEdge('fact-1', 'contradicts', v1Id)]))
858
+ });
859
+ (await o2.ingestItem({ id: 'fact-1', content: 'grey' })).orThrow();
860
+
861
+ // Re-introduce the ORIGINAL body: the invalidated v1 must be skipped by exact
862
+ // dedup, so this is a NEW version, not a duplicate-of.
863
+ clockValue = 3000;
864
+ const o3 = buildOrchestrator({
865
+ store,
866
+ classifier: classifyFact,
867
+ extractor: extractor(() => succeed([candidate(factKind, 'fact-1', 'blue')]))
868
+ });
869
+ expect(await o3.ingestItem({ id: 'fact-1', content: 'blue' })).toSucceedAndSatisfy(
870
+ (r: IIngestItemResult) => {
871
+ expect(r.records[0].resolution.verdict).toBe('new');
872
+ expect(r.records[0].disposition).toBe('written');
873
+ }
874
+ );
875
+ });
876
+ });
877
+
878
+ describe('MemoryIngestOrchestrator — additive-provenance non-regression', () => {
879
+ test('a pre-existing record with only source provenance still loads and validates unchanged', async () => {
880
+ const store = buildStore({ files: [seedNote('legacy', 'old note', { source: 'human' })] });
881
+ // Loads at construction time.
882
+ expect(await store.getById('knowledge' as MemoryScopeKey, 'legacy' as MemoryId)).toSucceedAndSatisfy(
883
+ (rec: IMemoryRecord<unknown> | undefined) => {
884
+ expect(rec?.envelope.provenance).toEqual({ source: 'human' });
885
+ expect(rec?.envelope.provenance.by).toBeUndefined();
886
+ expect(rec?.envelope.provenance.derivedFrom).toBeUndefined();
887
+ }
888
+ );
889
+ // An unrelated ingest does not disturb it.
890
+ const orch = buildOrchestrator({ store });
891
+ (await orch.ingestItem({ id: 'fresh', content: 'new content' })).orThrow();
892
+ expect(await store.getById('knowledge' as MemoryScopeKey, 'legacy' as MemoryId)).toSucceedAndSatisfy(
893
+ (rec: IMemoryRecord<unknown> | undefined) => {
894
+ expect(rec?.envelope.provenance).toEqual({ source: 'human' });
895
+ }
896
+ );
897
+ });
898
+ });
899
+
900
+ describe('MemoryIngestOrchestrator — ingestBatch', () => {
901
+ test('ingests a batch in order', async () => {
902
+ const store = buildStore();
903
+ const orch = buildOrchestrator({ store });
904
+ const result = await orch.ingestBatch([
905
+ { id: 'doc-1', content: 'one' },
906
+ { id: 'doc-2', content: 'two' }
907
+ ]);
908
+ expect(result).toSucceedAndSatisfy((results: ReadonlyArray<IIngestItemResult>) => {
909
+ expect(results).toHaveLength(2);
910
+ expect(results.map((r) => r.item.id)).toEqual(['doc-1', 'doc-2']);
911
+ expect(results.every((r) => r.records[0].disposition === 'written')).toBe(true);
912
+ });
913
+ });
914
+
915
+ test('aborts the batch on the first item failure', async () => {
916
+ const orch = buildOrchestrator({
917
+ store: buildStore(),
918
+ classifier: classifier((item) =>
919
+ item.id === 'bad' ? fail('classify bad') : succeed({ kind: noteKind })
920
+ )
921
+ });
922
+ expect(
923
+ await orch.ingestBatch([
924
+ { id: 'bad', content: 'one' },
925
+ { id: 'doc-2', content: 'two' }
926
+ ])
927
+ ).toFailWith(/classify bad/);
928
+ });
929
+ });
930
+
931
+ describe('MemoryIngestOrchestrator — store failure paths', () => {
932
+ test('a store snapshot (list) failure aborts the item', async () => {
933
+ const store = mockStore({ list: () => Promise.resolve(fail('list down')) });
934
+ const orch = buildOrchestrator({ store });
935
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(
936
+ /failed to snapshot store.*list down/
937
+ );
938
+ });
939
+
940
+ test('a store put failure surfaces as a write failure', async () => {
941
+ const store = mockStore({ put: () => Promise.resolve(fail('put kaput')) });
942
+ const orch = buildOrchestrator({ store });
943
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(/write failed.*put kaput/);
944
+ });
945
+ });
946
+
947
+ describe('MemoryIngestOrchestrator — optional constructor params', () => {
948
+ test('falls back to defaultCodec when no per-kind codec map is supplied', async () => {
949
+ const store = buildStore();
950
+ const orch = MemoryIngestOrchestrator.create({
951
+ store,
952
+ registry: registry(),
953
+ defaultCodec: new KnowledgeIdentityCodec(),
954
+ classifier: classifyNote,
955
+ extractor: oneNoteExtractor(),
956
+ relationExtractor: noEdges
957
+ }).orThrow();
958
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'hello' })).toSucceedAndSatisfy(
959
+ (r: IIngestItemResult) => expect(r.records[0].disposition).toBe('written')
960
+ );
961
+ });
962
+
963
+ test('honors an explicit similarityTopK and logs a captured host throw to the supplied logger', async () => {
964
+ const logger = new Logging.InMemoryLogger('detail');
965
+ const orch = MemoryIngestOrchestrator.create({
966
+ store: buildStore(),
967
+ registry: registry(),
968
+ codecs,
969
+ classifier: throwingClassifier(),
970
+ extractor: oneNoteExtractor(),
971
+ relationExtractor: noEdges,
972
+ similarityTopK: 3,
973
+ logger
974
+ }).orThrow();
975
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'x' })).toFailWith(/classifier boom/);
976
+ expect(logger.logged.some((m) => /classifier boom/.test(m))).toBe(true);
977
+ });
978
+ });
979
+
980
+ describe('MemoryIngestOrchestrator — review-punch-list fixes', () => {
981
+ test('a stored record with a non-string body fails the exact-dedup pass loudly', async () => {
982
+ const weird: IMemoryRecord<unknown> = {
983
+ envelope: {
984
+ id: 'weird' as MemoryId,
985
+ entityId: 'weird' as EntityId,
986
+ kind: noteKind,
987
+ tags: [],
988
+ links: [],
989
+ created: 0,
990
+ updated: 0,
991
+ seq: 0,
992
+ contentHash: '',
993
+ provenance: { source: 'agent' }
994
+ },
995
+ body: 42
996
+ };
997
+ const store = mockStore({ list: () => Promise.resolve(succeed([weird])) });
998
+ const orch = buildOrchestrator({ store });
999
+ expect(await orch.ingestItem({ id: 'doc-1', content: 'hello' })).toFailWith(
1000
+ /stored record 'weird' has a non-string body/
1001
+ );
1002
+ });
1003
+
1004
+ test('duplicate stage-5 edges are deduped on the written record', async () => {
1005
+ const store = buildStore();
1006
+ await putNote(store, 'doc-a', 'anchor');
1007
+ const orch = buildOrchestrator({
1008
+ store,
1009
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'goodbye')])),
1010
+ relationExtractor: relater(() =>
1011
+ succeed([candEdge('doc-b', 'rel', 'doc-a'), candEdge('doc-b', 'rel', 'doc-a')])
1012
+ )
1013
+ });
1014
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
1015
+ expect(r.records[0].record?.envelope.links).toEqual([{ type: 'rel', target: 'doc-a' }]);
1016
+ });
1017
+ });
1018
+
1019
+ test('two candidates merging into the same target do not duplicate a shared edge', async () => {
1020
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
1021
+ const store = buildStore({ vectorIndex, embed });
1022
+ await putNote(store, 'doc-a', 'apple pie');
1023
+ await putNote(store, 'doc-x', 'target');
1024
+ const orch = buildOrchestrator({
1025
+ store,
1026
+ vectorIndex,
1027
+ embed,
1028
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'merge-into', target: similar[0].id })),
1029
+ extractor: extractor(() =>
1030
+ succeed([candidate(noteKind, 'doc-b', 'apple tart'), candidate(noteKind, 'doc-c', 'apple tart')])
1031
+ ),
1032
+ relationExtractor: relater(() => succeed([candEdge('doc-a', 'rel', 'doc-x')]))
1033
+ });
1034
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceed();
1035
+ expect(await store.get(noteKind, 'doc-a' as EntityId)).toSucceedAndSatisfy(
1036
+ (rec: IMemoryRecord<unknown> | undefined) => {
1037
+ expect(rec?.envelope.links).toEqual([{ type: 'rel', target: 'doc-x' }]);
1038
+ }
1039
+ );
1040
+ });
1041
+ });
1042
+
1043
+ describe('MemoryIngestOrchestrator — merge-into safety (second review)', () => {
1044
+ // Seed a record with pre-existing tags + links under a flat codec (id === entityId).
1045
+ async function putFull(
1046
+ store: FileTreeMemoryStore,
1047
+ kind: Kind,
1048
+ entityId: string,
1049
+ body: string,
1050
+ opts: { tags?: ReadonlyArray<string>; links?: IMemoryEnvelope['links'] } = {}
1051
+ ): Promise<void> {
1052
+ const rec: IMemoryRecord<unknown> = {
1053
+ envelope: {
1054
+ id: entityId as MemoryId,
1055
+ entityId: entityId as EntityId,
1056
+ kind,
1057
+ tags: (opts.tags ?? []) as ReadonlyArray<Tag>,
1058
+ links: opts.links ?? [],
1059
+ created: 0,
1060
+ updated: 0,
1061
+ seq: 0,
1062
+ contentHash: '',
1063
+ provenance: { source: 'agent' }
1064
+ },
1065
+ body
1066
+ };
1067
+ (await store.put(rec)).orThrow();
1068
+ }
1069
+
1070
+ test('merge-into UNIONs the target’s existing tags + links (never overwrites them)', async () => {
1071
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
1072
+ const store = buildStore({ vectorIndex, embed });
1073
+ await putFull(store, noteKind, 'doc-a', 'apple pie', {
1074
+ tags: ['old-tag'],
1075
+ links: [{ type: 'existing' as LinkType, target: 'doc-z' as MemoryId }]
1076
+ });
1077
+ await putFull(store, noteKind, 'doc-x', 'target of stage-5');
1078
+ const orch = buildOrchestrator({
1079
+ store,
1080
+ vectorIndex,
1081
+ embed,
1082
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'merge-into', target: similar[0].id })),
1083
+ extractor: extractor(() =>
1084
+ succeed([
1085
+ candidate(noteKind, 'doc-b', 'apple tart', {
1086
+ tags: ['new-tag'],
1087
+ links: [{ type: 'candlink' as LinkType, target: 'doc-z' as MemoryId }]
1088
+ })
1089
+ ])
1090
+ ),
1091
+ relationExtractor: relater(() => succeed([candEdge('doc-a', 'stage5', 'doc-x')]))
1092
+ });
1093
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
1094
+ expect(r.records[0].disposition).toBe('merged');
1095
+ });
1096
+ expect(await store.get(noteKind, 'doc-a' as EntityId)).toSucceedAndSatisfy(
1097
+ (rec: IMemoryRecord<unknown> | undefined) => {
1098
+ // Original tag preserved AND the candidate's tag gained, each once.
1099
+ expect([...(rec?.envelope.tags ?? [])].sort()).toEqual(['new-tag', 'old-tag']);
1100
+ // Original link + candidate link + stage-5 edge, each exactly once.
1101
+ expect(rec?.envelope.links).toEqual([
1102
+ { type: 'existing', target: 'doc-z' },
1103
+ { type: 'candlink', target: 'doc-z' },
1104
+ { type: 'stage5', target: 'doc-x' }
1105
+ ]);
1106
+ }
1107
+ );
1108
+ });
1109
+
1110
+ test('merge-into a different-kind target fails loudly', async () => {
1111
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
1112
+ const store = buildStore({ vectorIndex, embed });
1113
+ // A DIFFERENT-kind (ltm) record embedded so it surfaces as a neighbor of a note candidate.
1114
+ await putFull(store, ltmKind, 'conv-1', 'apple pie');
1115
+ const orch = buildOrchestrator({
1116
+ store,
1117
+ vectorIndex,
1118
+ embed,
1119
+ entityResolver: resolver((__c, similar) => succeed({ verdict: 'merge-into', target: similar[0].id })),
1120
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
1121
+ });
1122
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
1123
+ /merge-into target 'conv-1' is kind 'ltm' but the candidate is kind 'note'/
1124
+ );
1125
+ });
1126
+
1127
+ test('supersede with a non-existent target fails loudly', async () => {
1128
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
1129
+ const store = buildStore({ vectorIndex, embed });
1130
+ await putFull(store, noteKind, 'doc-a', 'apple pie');
1131
+ const orch = buildOrchestrator({
1132
+ store,
1133
+ vectorIndex,
1134
+ embed,
1135
+ entityResolver: resolver(() => succeed({ verdict: 'supersede', target: 'ghost' as MemoryId })),
1136
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
1137
+ });
1138
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
1139
+ /supersede target 'ghost' does not exist/
1140
+ );
1141
+ });
1142
+
1143
+ test('duplicate-of with a non-existent target fails loudly', async () => {
1144
+ const vectorIndex = InMemoryCosineIndex.create().orThrow();
1145
+ const store = buildStore({ vectorIndex, embed });
1146
+ await putFull(store, noteKind, 'doc-a', 'apple pie');
1147
+ const orch = buildOrchestrator({
1148
+ store,
1149
+ vectorIndex,
1150
+ embed,
1151
+ entityResolver: resolver(() => succeed({ verdict: 'duplicate-of', target: 'ghost' as MemoryId })),
1152
+ extractor: extractor(() => succeed([candidate(noteKind, 'doc-b', 'apple tart')]))
1153
+ });
1154
+ expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
1155
+ /duplicate-of target 'ghost' does not exist/
1156
+ );
1157
+ });
1158
+ });