@fgv/ts-agent-memory 5.1.0-37 → 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.
- package/.rush/temp/{6dfc331ddf74dc2db0d4483d0c96ea971f351c83.tar.log → b82cf6bdece20481260e6bab946179eeec9d7b46.tar.log} +116 -2
- package/.rush/temp/chunked-rush-logs/ts-agent-memory.build.chunks.jsonl +4 -4
- package/.rush/temp/operation/build/all.log +4 -4
- package/.rush/temp/operation/build/log-chunks.jsonl +4 -4
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +222 -221
- package/config/jest.config.json +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/packlets/ingest/cycleGuard.js +111 -0
- package/dist/packlets/ingest/cycleGuard.js.map +1 -0
- package/dist/packlets/ingest/hostStages.js +6 -0
- package/dist/packlets/ingest/hostStages.js.map +1 -0
- package/dist/packlets/ingest/index.js +9 -0
- package/dist/packlets/ingest/index.js.map +1 -0
- package/dist/packlets/ingest/model.js +6 -0
- package/dist/packlets/ingest/model.js.map +1 -0
- package/dist/packlets/ingest/orchestrator.js +438 -0
- package/dist/packlets/ingest/orchestrator.js.map +1 -0
- package/dist/packlets/retrieve/index.js +1 -0
- package/dist/packlets/retrieve/index.js.map +1 -1
- package/dist/packlets/retrieve/temporalRetrievers.js +172 -0
- package/dist/packlets/retrieve/temporalRetrievers.js.map +1 -0
- package/dist/packlets/store/fileTreeMemoryStore.js +323 -41
- package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/dist/packlets/tools/index.js +6 -0
- package/dist/packlets/tools/index.js.map +1 -0
- package/dist/packlets/tools/memoryTools.js +337 -0
- package/dist/packlets/tools/memoryTools.js.map +1 -0
- package/dist/packlets/types/identityCodec.js +115 -0
- package/dist/packlets/types/identityCodec.js.map +1 -1
- package/dist/packlets/types/index.js +1 -0
- package/dist/packlets/types/index.js.map +1 -1
- package/dist/packlets/types/temporal.js +85 -0
- package/dist/packlets/types/temporal.js.map +1 -0
- package/dist/packlets/types/writePolicy.js +96 -0
- package/dist/packlets/types/writePolicy.js.map +1 -1
- package/dist/test/unit/converters/antagonistRoundTrip.test.js +95 -0
- package/dist/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js +273 -0
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
- package/dist/test/unit/ingest/cycleGuard.test.js +54 -0
- package/dist/test/unit/ingest/cycleGuard.test.js.map +1 -0
- package/dist/test/unit/ingest/orchestrator.test.js +913 -0
- package/dist/test/unit/ingest/orchestrator.test.js.map +1 -0
- package/dist/test/unit/retrieve/temporalRetrievers.test.js +182 -0
- package/dist/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
- package/dist/test/unit/store/antagonistTemporalBoundary.test.js +120 -0
- package/dist/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
- package/dist/test/unit/store/fileTreeMemoryStore.test.js +74 -7
- package/dist/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
- package/dist/test/unit/store/temporalStore.test.js +398 -0
- package/dist/test/unit/store/temporalStore.test.js.map +1 -0
- package/dist/test/unit/tools/memoryTools.test.js +572 -0
- package/dist/test/unit/tools/memoryTools.test.js.map +1 -0
- package/dist/test/unit/types/temporalCodec.test.js +203 -0
- package/dist/test/unit/types/temporalCodec.test.js.map +1 -0
- package/dist/test/unit/types/temporalPolicy.test.js +62 -0
- package/dist/test/unit/types/temporalPolicy.test.js.map +1 -0
- package/dist/ts-agent-memory.d.ts +1042 -10
- package/dist/tsdoc-metadata.json +1 -1
- package/etc/ts-agent-memory.api.md +284 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/packlets/ingest/cycleGuard.d.ts +41 -0
- package/lib/packlets/ingest/cycleGuard.d.ts.map +1 -0
- package/lib/packlets/ingest/cycleGuard.js +115 -0
- package/lib/packlets/ingest/cycleGuard.js.map +1 -0
- package/lib/packlets/ingest/hostStages.d.ts +88 -0
- package/lib/packlets/ingest/hostStages.d.ts.map +1 -0
- package/lib/packlets/ingest/hostStages.js +7 -0
- package/lib/packlets/ingest/hostStages.js.map +1 -0
- package/lib/packlets/ingest/index.d.ts +5 -0
- package/lib/packlets/ingest/index.d.ts.map +1 -0
- package/lib/packlets/ingest/index.js +25 -0
- package/lib/packlets/ingest/index.js.map +1 -0
- package/lib/packlets/ingest/model.d.ts +177 -0
- package/lib/packlets/ingest/model.d.ts.map +1 -0
- package/lib/packlets/ingest/model.js +7 -0
- package/lib/packlets/ingest/model.js.map +1 -0
- package/lib/packlets/ingest/orchestrator.d.ts +206 -0
- package/lib/packlets/ingest/orchestrator.d.ts.map +1 -0
- package/lib/packlets/ingest/orchestrator.js +442 -0
- package/lib/packlets/ingest/orchestrator.js.map +1 -0
- package/lib/packlets/retrieve/index.d.ts +1 -0
- package/lib/packlets/retrieve/index.d.ts.map +1 -1
- package/lib/packlets/retrieve/index.js +1 -0
- package/lib/packlets/retrieve/index.js.map +1 -1
- package/lib/packlets/retrieve/temporalRetrievers.d.ts +78 -0
- package/lib/packlets/retrieve/temporalRetrievers.d.ts.map +1 -0
- package/lib/packlets/retrieve/temporalRetrievers.js +178 -0
- package/lib/packlets/retrieve/temporalRetrievers.js.map +1 -0
- package/lib/packlets/store/fileTreeMemoryStore.d.ts +118 -9
- package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.js +322 -40
- package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/lib/packlets/tools/index.d.ts +2 -0
- package/lib/packlets/tools/index.d.ts.map +1 -0
- package/lib/packlets/tools/index.js +22 -0
- package/lib/packlets/tools/index.js.map +1 -0
- package/lib/packlets/tools/memoryTools.d.ts +139 -0
- package/lib/packlets/tools/memoryTools.d.ts.map +1 -0
- package/lib/packlets/tools/memoryTools.js +341 -0
- package/lib/packlets/tools/memoryTools.js.map +1 -0
- package/lib/packlets/types/identityCodec.d.ts +86 -0
- package/lib/packlets/types/identityCodec.d.ts.map +1 -1
- package/lib/packlets/types/identityCodec.js +118 -1
- package/lib/packlets/types/identityCodec.js.map +1 -1
- package/lib/packlets/types/index.d.ts +1 -0
- package/lib/packlets/types/index.d.ts.map +1 -1
- package/lib/packlets/types/index.js +1 -0
- package/lib/packlets/types/index.js.map +1 -1
- package/lib/packlets/types/temporal.d.ts +40 -0
- package/lib/packlets/types/temporal.d.ts.map +1 -0
- package/lib/packlets/types/temporal.js +92 -0
- package/lib/packlets/types/temporal.js.map +1 -0
- package/lib/packlets/types/writePolicy.d.ts +49 -0
- package/lib/packlets/types/writePolicy.d.ts.map +1 -1
- package/lib/packlets/types/writePolicy.js +98 -1
- package/lib/packlets/types/writePolicy.js.map +1 -1
- package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts +10 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts.map +1 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.js +97 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts +9 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js +275 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
- package/lib/test/unit/ingest/cycleGuard.test.d.ts +2 -0
- package/lib/test/unit/ingest/cycleGuard.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/cycleGuard.test.js +56 -0
- package/lib/test/unit/ingest/cycleGuard.test.js.map +1 -0
- package/lib/test/unit/ingest/orchestrator.test.d.ts +2 -0
- package/lib/test/unit/ingest/orchestrator.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/orchestrator.test.js +915 -0
- package/lib/test/unit/ingest/orchestrator.test.js.map +1 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts +2 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts.map +1 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.js +184 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts +9 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts.map +1 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.js +122 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.js +74 -7
- package/lib/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
- package/lib/test/unit/store/temporalStore.test.d.ts +2 -0
- package/lib/test/unit/store/temporalStore.test.d.ts.map +1 -0
- package/lib/test/unit/store/temporalStore.test.js +400 -0
- package/lib/test/unit/store/temporalStore.test.js.map +1 -0
- package/lib/test/unit/tools/memoryTools.test.d.ts +2 -0
- package/lib/test/unit/tools/memoryTools.test.d.ts.map +1 -0
- package/lib/test/unit/tools/memoryTools.test.js +574 -0
- package/lib/test/unit/tools/memoryTools.test.js.map +1 -0
- package/lib/test/unit/types/temporalCodec.test.d.ts +2 -0
- package/lib/test/unit/types/temporalCodec.test.d.ts.map +1 -0
- package/lib/test/unit/types/temporalCodec.test.js +205 -0
- package/lib/test/unit/types/temporalCodec.test.js.map +1 -0
- package/lib/test/unit/types/temporalPolicy.test.d.ts +2 -0
- package/lib/test/unit/types/temporalPolicy.test.d.ts.map +1 -0
- package/lib/test/unit/types/temporalPolicy.test.js +64 -0
- package/lib/test/unit/types/temporalPolicy.test.js.map +1 -0
- package/package.json +7 -7
- package/rush-logs/ts-agent-memory.build.cache.log +1 -1
- package/rush-logs/ts-agent-memory.build.log +4 -4
- package/src/index.ts +2 -0
- package/src/packlets/ingest/cycleGuard.ts +142 -0
- package/src/packlets/ingest/hostStages.ts +111 -0
- package/src/packlets/ingest/index.ts +9 -0
- package/src/packlets/ingest/model.ts +184 -0
- package/src/packlets/ingest/orchestrator.ts +797 -0
- package/src/packlets/retrieve/index.ts +1 -0
- package/src/packlets/retrieve/temporalRetrievers.ts +210 -0
- package/src/packlets/store/fileTreeMemoryStore.ts +460 -66
- package/src/packlets/tools/index.ts +6 -0
- package/src/packlets/tools/memoryTools.ts +579 -0
- package/src/packlets/types/identityCodec.ts +184 -0
- package/src/packlets/types/index.ts +1 -0
- package/src/packlets/types/temporal.ts +96 -0
- package/src/packlets/types/writePolicy.ts +127 -0
- package/src/test/unit/converters/antagonistRoundTrip.test.ts +110 -0
- package/src/test/unit/ingest/antagonistCycleAndParity.test.ts +362 -0
- package/src/test/unit/ingest/cycleGuard.test.ts +68 -0
- package/src/test/unit/ingest/orchestrator.test.ts +1158 -0
- package/src/test/unit/retrieve/temporalRetrievers.test.ts +226 -0
- package/src/test/unit/store/antagonistTemporalBoundary.test.ts +158 -0
- package/src/test/unit/store/fileTreeMemoryStore.test.ts +98 -7
- package/src/test/unit/store/temporalStore.test.ts +469 -0
- package/src/test/unit/tools/memoryTools.test.ts +771 -0
- package/src/test/unit/types/temporalCodec.test.ts +259 -0
- package/src/test/unit/types/temporalPolicy.test.ts +96 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +85 -9
- package/temp/build/typescript/ts_8nwakTlr.json +1 -1
- package/temp/ts-agent-memory.api.json +11984 -6314
- package/temp/ts-agent-memory.api.md +284 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Antagonist torture tests — cycle guard adversarial graphs (target class 6) and
|
|
8
|
+
* enum-branch parity (target class 7), exercised at the ORCHESTRATOR/integration
|
|
9
|
+
* level over a real {@link FileTreeMemoryStore}, complementing the pure-function
|
|
10
|
+
* unit tests in `cycleGuard.test.ts` and the per-verdict tests in
|
|
11
|
+
* `orchestrator.test.ts`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import '@fgv/ts-utils-jest';
|
|
15
|
+
import { Converters, Result, succeed } from '@fgv/ts-utils';
|
|
16
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
17
|
+
import {
|
|
18
|
+
BodyConverterRegistry,
|
|
19
|
+
EntityId,
|
|
20
|
+
FileTreeMemoryStore,
|
|
21
|
+
IBodyConverterRegistry,
|
|
22
|
+
ICandidateEdge,
|
|
23
|
+
ICandidateRecord,
|
|
24
|
+
IEntityResolutionCandidate,
|
|
25
|
+
IEntityResolver,
|
|
26
|
+
IFactExtractor,
|
|
27
|
+
IIdentityCodec,
|
|
28
|
+
IIngestItem,
|
|
29
|
+
IIngestItemResult,
|
|
30
|
+
IMemoryClassification,
|
|
31
|
+
IMemoryClassifier,
|
|
32
|
+
IMemoryEnvelope,
|
|
33
|
+
IMemoryRecord,
|
|
34
|
+
IRelationContext,
|
|
35
|
+
IRelationExtractor,
|
|
36
|
+
InMemoryCosineIndex,
|
|
37
|
+
Kind,
|
|
38
|
+
KnowledgeIdentityCodec,
|
|
39
|
+
MemoryId,
|
|
40
|
+
MemoryIngestOrchestrator,
|
|
41
|
+
ResolutionVerdict,
|
|
42
|
+
Tag
|
|
43
|
+
} from '../../../index';
|
|
44
|
+
|
|
45
|
+
const noteKind: Kind = 'note' as Kind;
|
|
46
|
+
|
|
47
|
+
function mutableRoot(): FileTree.IMutableFileTreeDirectoryItem {
|
|
48
|
+
const tree = FileTree.inMemory([], { mutable: true }).orThrow();
|
|
49
|
+
const root = tree.getDirectory('/').orThrow();
|
|
50
|
+
if (!FileTree.isMutableDirectoryItem(root)) {
|
|
51
|
+
throw new Error('expected a mutable root directory');
|
|
52
|
+
}
|
|
53
|
+
return root;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function registry(): IBodyConverterRegistry {
|
|
57
|
+
const reg = BodyConverterRegistry.create().orThrow();
|
|
58
|
+
reg.register(noteKind, Converters.string);
|
|
59
|
+
return reg;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const codecs: ReadonlyMap<Kind, IIdentityCodec> = new Map<Kind, IIdentityCodec>([
|
|
63
|
+
[noteKind, new KnowledgeIdentityCodec()]
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
function buildStore(): FileTreeMemoryStore {
|
|
67
|
+
return FileTreeMemoryStore.create({ root: mutableRoot(), registry: registry(), codecs }).orThrow();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function putFull(
|
|
71
|
+
store: FileTreeMemoryStore,
|
|
72
|
+
entityId: string,
|
|
73
|
+
body: string,
|
|
74
|
+
links: IMemoryEnvelope['links'] = []
|
|
75
|
+
): Promise<void> {
|
|
76
|
+
const rec: IMemoryRecord<unknown> = {
|
|
77
|
+
envelope: {
|
|
78
|
+
id: entityId as MemoryId,
|
|
79
|
+
entityId: entityId as EntityId,
|
|
80
|
+
kind: noteKind,
|
|
81
|
+
tags: [],
|
|
82
|
+
links,
|
|
83
|
+
created: 0,
|
|
84
|
+
updated: 0,
|
|
85
|
+
seq: 0,
|
|
86
|
+
contentHash: '',
|
|
87
|
+
provenance: { source: 'agent' }
|
|
88
|
+
},
|
|
89
|
+
body
|
|
90
|
+
};
|
|
91
|
+
(await store.put(rec)).orThrow();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function classifier(fn: (item: IIngestItem) => Result<IMemoryClassification>): IMemoryClassifier {
|
|
95
|
+
return { classify: (item) => Promise.resolve(fn(item)) };
|
|
96
|
+
}
|
|
97
|
+
function extractor(
|
|
98
|
+
fn: (item: IIngestItem, c: IMemoryClassification) => Result<ReadonlyArray<ICandidateRecord>>
|
|
99
|
+
): IFactExtractor {
|
|
100
|
+
return { extract: (item, c) => Promise.resolve(fn(item, c)) };
|
|
101
|
+
}
|
|
102
|
+
function relater(fn: (ctx: IRelationContext) => Result<ReadonlyArray<ICandidateEdge>>): IRelationExtractor {
|
|
103
|
+
return { relate: (ctx) => Promise.resolve(fn(ctx)) };
|
|
104
|
+
}
|
|
105
|
+
function resolver(
|
|
106
|
+
fn: (
|
|
107
|
+
candidate: ICandidateRecord,
|
|
108
|
+
similar: ReadonlyArray<IEntityResolutionCandidate>
|
|
109
|
+
) => Result<ResolutionVerdict>
|
|
110
|
+
): IEntityResolver {
|
|
111
|
+
return { resolve: (candidate, similar) => Promise.resolve(fn(candidate, similar)) };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const classifyNote: IMemoryClassifier = classifier(() => succeed({ kind: noteKind }));
|
|
115
|
+
|
|
116
|
+
function candidate(
|
|
117
|
+
entityId: string,
|
|
118
|
+
body: unknown,
|
|
119
|
+
links: ICandidateRecord['envelope']['links'] = []
|
|
120
|
+
): ICandidateRecord {
|
|
121
|
+
return {
|
|
122
|
+
envelope: {
|
|
123
|
+
entityId: entityId as EntityId,
|
|
124
|
+
kind: noteKind,
|
|
125
|
+
tags: [],
|
|
126
|
+
links,
|
|
127
|
+
provenance: { source: 'agent' }
|
|
128
|
+
},
|
|
129
|
+
body
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function candEdge(source: string, type: string, target: string): ICandidateEdge {
|
|
134
|
+
return { source: source as MemoryId, edge: { type: type as never, target: target as MemoryId } };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function buildOrchestrator(params: {
|
|
138
|
+
store: FileTreeMemoryStore;
|
|
139
|
+
extractor: IFactExtractor;
|
|
140
|
+
relationExtractor: IRelationExtractor;
|
|
141
|
+
entityResolver?: IEntityResolver;
|
|
142
|
+
}): MemoryIngestOrchestrator {
|
|
143
|
+
return MemoryIngestOrchestrator.create({
|
|
144
|
+
store: params.store,
|
|
145
|
+
registry: registry(),
|
|
146
|
+
codecs,
|
|
147
|
+
classifier: classifyNote,
|
|
148
|
+
extractor: params.extractor,
|
|
149
|
+
relationExtractor: params.relationExtractor,
|
|
150
|
+
entityResolver: params.entityResolver
|
|
151
|
+
}).orThrow();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
describe('antagonist — cycle guard through EXISTING store edges (integration)', () => {
|
|
155
|
+
// Wrong impl this catches: an orchestrator that only feeds the cycle guard the
|
|
156
|
+
// proposed-edge batch (as its own pure-function unit tests do) and forgets to
|
|
157
|
+
// fold in the store snapshot's existing edges, so a cycle that closes THROUGH
|
|
158
|
+
// an already-persisted edge is silently admitted.
|
|
159
|
+
test('a proposed edge that closes a cycle through a pre-existing on-disk edge is rejected', async () => {
|
|
160
|
+
const store = buildStore();
|
|
161
|
+
// Existing on-disk edge: doc-a -> doc-b (persisted before this ingest runs).
|
|
162
|
+
await putFull(store, 'doc-a', 'anchor-a', [{ type: 'rel' as never, target: 'doc-b' as MemoryId }]);
|
|
163
|
+
await putFull(store, 'doc-b', 'anchor-b');
|
|
164
|
+
|
|
165
|
+
// This ingest re-writes doc-b (a 'new'-verdict same-id update, since no
|
|
166
|
+
// resolver is wired) and proposes doc-b -> doc-a, which — unioned with the
|
|
167
|
+
// EXISTING doc-a -> doc-b edge — closes a 2-cycle that exists ONLY because of
|
|
168
|
+
// the on-disk edge, not anything proposed in this batch.
|
|
169
|
+
const orch = buildOrchestrator({
|
|
170
|
+
store,
|
|
171
|
+
extractor: extractor(() => succeed([candidate('doc-b', 'anchor-b-v2')])),
|
|
172
|
+
relationExtractor: relater(() => succeed([candEdge('doc-b', 'rel', 'doc-a')]))
|
|
173
|
+
});
|
|
174
|
+
expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(/would create a cycle/);
|
|
175
|
+
|
|
176
|
+
// Confirm the guard fired BEFORE any write — doc-b's content must be unchanged.
|
|
177
|
+
expect(await store.get(noteKind, 'doc-b' as EntityId)).toSucceedAndSatisfy(
|
|
178
|
+
(rec: IMemoryRecord<unknown> | undefined) => expect(rec?.body).toBe('anchor-b')
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('a long existing chain plus one proposed edge closing the loop is rejected', async () => {
|
|
183
|
+
const store = buildStore();
|
|
184
|
+
// Existing chain: a -> b -> c -> d (four pre-existing on-disk edges spanning
|
|
185
|
+
// three hops), persisted before this ingest.
|
|
186
|
+
await putFull(store, 'chain-a', 'a', [{ type: 'rel' as never, target: 'chain-b' as MemoryId }]);
|
|
187
|
+
await putFull(store, 'chain-b', 'b', [{ type: 'rel' as never, target: 'chain-c' as MemoryId }]);
|
|
188
|
+
await putFull(store, 'chain-c', 'c', [{ type: 'rel' as never, target: 'chain-d' as MemoryId }]);
|
|
189
|
+
await putFull(store, 'chain-d', 'd');
|
|
190
|
+
|
|
191
|
+
// Proposed: chain-d -> chain-a, closing the whole loop through the existing chain.
|
|
192
|
+
const orch = buildOrchestrator({
|
|
193
|
+
store,
|
|
194
|
+
extractor: extractor(() => succeed([candidate('chain-d', 'd-v2')])),
|
|
195
|
+
relationExtractor: relater(() => succeed([candEdge('chain-d', 'rel', 'chain-a')]))
|
|
196
|
+
});
|
|
197
|
+
expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(/would create a cycle/);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('cycleGuard "off" admits a cycle that closes through an existing edge', async () => {
|
|
201
|
+
const store = buildStore();
|
|
202
|
+
await putFull(store, 'doc-a', 'anchor-a', [{ type: 'rel' as never, target: 'doc-b' as MemoryId }]);
|
|
203
|
+
await putFull(store, 'doc-b', 'anchor-b');
|
|
204
|
+
const orch = MemoryIngestOrchestrator.create({
|
|
205
|
+
store,
|
|
206
|
+
registry: registry(),
|
|
207
|
+
codecs,
|
|
208
|
+
classifier: classifyNote,
|
|
209
|
+
extractor: extractor(() => succeed([candidate('doc-b', 'anchor-b-v2')])),
|
|
210
|
+
relationExtractor: relater(() => succeed([candEdge('doc-b', 'rel', 'doc-a')])),
|
|
211
|
+
cycleGuard: 'off'
|
|
212
|
+
}).orThrow();
|
|
213
|
+
expect(await orch.ingestItem({ id: 'i', content: 'x' })).toSucceedAndSatisfy((r: IIngestItemResult) => {
|
|
214
|
+
expect(r.records[0].record?.envelope.links).toEqual([{ type: 'rel', target: 'doc-a' }]);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('antagonist — resolver target need not be a surfaced `similar` candidate (structural-only gate)', () => {
|
|
220
|
+
// Documents (and locks in) the actual contract: fgv validates a target-bearing
|
|
221
|
+
// verdict STRUCTURALLY (exists in the store + matching kind), uniformly across
|
|
222
|
+
// all three target-bearing verdicts, regardless of whether the resolver's
|
|
223
|
+
// choice was among the `similar` candidates it was shown. Wrong impl this
|
|
224
|
+
// guards against: fgv silently restricting itself to `similar` membership
|
|
225
|
+
// (which would incorrectly reject a host resolver with external knowledge), OR
|
|
226
|
+
// fgv skipping the structural check for one of the three verdicts.
|
|
227
|
+
test.each(['duplicate-of', 'supersede', 'merge-into'] as const)(
|
|
228
|
+
"verdict '%s' targeting a valid existing record OUTSIDE the surfaced similar set is accepted",
|
|
229
|
+
async (verdict) => {
|
|
230
|
+
const store = buildStore();
|
|
231
|
+
await putFull(store, 'doc-a', 'apple pie'); // will be surfaced as `similar`
|
|
232
|
+
await putFull(store, 'doc-q', 'unrelated target'); // exists, but never surfaced
|
|
233
|
+
|
|
234
|
+
const vectorIndex = InMemoryCosineIndex.create().orThrow();
|
|
235
|
+
const embed = (): Promise<Result<Float32Array>> =>
|
|
236
|
+
Promise.resolve(succeed(Float32Array.from([1, 0, 0])));
|
|
237
|
+
// Only doc-a is embedded/indexed; doc-q is never surfaced by similarity —
|
|
238
|
+
// it can only be reached by the resolver's own out-of-band choice.
|
|
239
|
+
(await vectorIndex.add('doc-a' as MemoryId, Float32Array.from([1, 0, 0]))).orThrow();
|
|
240
|
+
|
|
241
|
+
const orch = MemoryIngestOrchestrator.create({
|
|
242
|
+
store,
|
|
243
|
+
registry: registry(),
|
|
244
|
+
codecs,
|
|
245
|
+
classifier: classifyNote,
|
|
246
|
+
extractor: extractor(() => succeed([candidate('doc-b', 'apple tart')])),
|
|
247
|
+
relationExtractor: relater(() => succeed([])),
|
|
248
|
+
vectorIndex,
|
|
249
|
+
embed,
|
|
250
|
+
entityResolver: resolver(() => succeed({ verdict, target: 'doc-q' as MemoryId } as ResolutionVerdict))
|
|
251
|
+
}).orThrow();
|
|
252
|
+
|
|
253
|
+
const result = await orch.ingestItem({ id: 'i', content: 'x' });
|
|
254
|
+
if (verdict === 'duplicate-of') {
|
|
255
|
+
expect(result).toSucceedAndSatisfy((r: IIngestItemResult) => {
|
|
256
|
+
expect(r.records[0].disposition).toBe('deduped');
|
|
257
|
+
expect(r.records[0].id).toBe('doc-q');
|
|
258
|
+
});
|
|
259
|
+
} else if (verdict === 'supersede') {
|
|
260
|
+
expect(result).toSucceedAndSatisfy((r: IIngestItemResult) => {
|
|
261
|
+
expect(r.records[0].disposition).toBe('written');
|
|
262
|
+
expect(r.records[0].resolution).toEqual({ verdict: 'supersede', target: 'doc-q' });
|
|
263
|
+
});
|
|
264
|
+
} else {
|
|
265
|
+
expect(result).toSucceedAndSatisfy((r: IIngestItemResult) => {
|
|
266
|
+
expect(r.records[0].disposition).toBe('merged');
|
|
267
|
+
expect(r.records[0].id).toBe('doc-q');
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe('antagonist — enum-branch parity: uniform structural rejection across target-bearing verdicts', () => {
|
|
275
|
+
// A single table-driven test locking in that EVERY target-bearing verdict arm
|
|
276
|
+
// shares the SAME two structural checks (existence, then kind match) in the
|
|
277
|
+
// SAME order — the exact class of hole ("only merge-into checked") that a
|
|
278
|
+
// prior review caught. Wrong impl this guards against: a future edit that adds
|
|
279
|
+
// a fourth verdict or refactors validation per-branch and re-introduces an
|
|
280
|
+
// asymmetric gap.
|
|
281
|
+
const verdicts = ['duplicate-of', 'supersede', 'merge-into'] as const;
|
|
282
|
+
|
|
283
|
+
test.each(verdicts)(
|
|
284
|
+
"verdict '%s' rejects a nonexistent target with the SAME message shape",
|
|
285
|
+
async (verdict) => {
|
|
286
|
+
const store = buildStore();
|
|
287
|
+
await putFull(store, 'doc-a', 'apple pie');
|
|
288
|
+
const vectorIndex = InMemoryCosineIndex.create().orThrow();
|
|
289
|
+
const embed = (): Promise<Result<Float32Array>> =>
|
|
290
|
+
Promise.resolve(succeed(Float32Array.from([1, 0, 0])));
|
|
291
|
+
const orch = MemoryIngestOrchestrator.create({
|
|
292
|
+
store,
|
|
293
|
+
registry: registry(),
|
|
294
|
+
codecs,
|
|
295
|
+
classifier: classifyNote,
|
|
296
|
+
extractor: extractor(() => succeed([candidate('doc-b', 'apple tart')])),
|
|
297
|
+
relationExtractor: relater(() => succeed([])),
|
|
298
|
+
vectorIndex,
|
|
299
|
+
embed,
|
|
300
|
+
entityResolver: resolver(() => succeed({ verdict, target: 'ghost' as MemoryId } as ResolutionVerdict))
|
|
301
|
+
}).orThrow();
|
|
302
|
+
(await vectorIndex.add('doc-a' as MemoryId, Float32Array.from([1, 0, 0]))).orThrow();
|
|
303
|
+
expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
|
|
304
|
+
`ingest 'i': ${verdict} target 'ghost' does not exist in the store`
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
test.each(verdicts)(
|
|
310
|
+
"verdict '%s' rejects a foreign-kind target with the SAME message shape",
|
|
311
|
+
async (verdict) => {
|
|
312
|
+
const otherKind: Kind = 'other' as Kind;
|
|
313
|
+
const reg = registry();
|
|
314
|
+
reg.register(otherKind, Converters.string);
|
|
315
|
+
const otherCodecs: ReadonlyMap<Kind, IIdentityCodec> = new Map<Kind, IIdentityCodec>([
|
|
316
|
+
...codecs,
|
|
317
|
+
[otherKind, new KnowledgeIdentityCodec()]
|
|
318
|
+
]);
|
|
319
|
+
const store = FileTreeMemoryStore.create({
|
|
320
|
+
root: mutableRoot(),
|
|
321
|
+
registry: reg,
|
|
322
|
+
codecs: otherCodecs
|
|
323
|
+
}).orThrow();
|
|
324
|
+
const foreignRec: IMemoryRecord<unknown> = {
|
|
325
|
+
envelope: {
|
|
326
|
+
id: 'foreign-1' as MemoryId,
|
|
327
|
+
entityId: 'foreign-1' as EntityId,
|
|
328
|
+
kind: otherKind,
|
|
329
|
+
tags: [] as ReadonlyArray<Tag>,
|
|
330
|
+
links: [],
|
|
331
|
+
created: 0,
|
|
332
|
+
updated: 0,
|
|
333
|
+
seq: 0,
|
|
334
|
+
contentHash: '',
|
|
335
|
+
provenance: { source: 'agent' }
|
|
336
|
+
},
|
|
337
|
+
body: 'apple pie'
|
|
338
|
+
};
|
|
339
|
+
(await store.put(foreignRec)).orThrow();
|
|
340
|
+
const vectorIndex = InMemoryCosineIndex.create().orThrow();
|
|
341
|
+
const embed = (): Promise<Result<Float32Array>> =>
|
|
342
|
+
Promise.resolve(succeed(Float32Array.from([1, 0, 0])));
|
|
343
|
+
const orch = MemoryIngestOrchestrator.create({
|
|
344
|
+
store,
|
|
345
|
+
registry: reg,
|
|
346
|
+
codecs: otherCodecs,
|
|
347
|
+
classifier: classifyNote,
|
|
348
|
+
extractor: extractor(() => succeed([candidate('doc-b', 'apple tart')])),
|
|
349
|
+
relationExtractor: relater(() => succeed([])),
|
|
350
|
+
vectorIndex,
|
|
351
|
+
embed,
|
|
352
|
+
entityResolver: resolver(() =>
|
|
353
|
+
succeed({ verdict, target: 'foreign-1' as MemoryId } as ResolutionVerdict)
|
|
354
|
+
)
|
|
355
|
+
}).orThrow();
|
|
356
|
+
(await vectorIndex.add('foreign-1' as MemoryId, Float32Array.from([1, 0, 0]))).orThrow();
|
|
357
|
+
expect(await orch.ingestItem({ id: 'i', content: 'x' })).toFailWith(
|
|
358
|
+
`ingest 'i': ${verdict} target 'foreign-1' is kind 'other' but the candidate is kind 'note'`
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
);
|
|
362
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import '@fgv/ts-utils-jest';
|
|
7
|
+
import { ICycleGuardEdge, LinkType, MemoryId, assertNoCycles, buildCycleKey } from '../../../index';
|
|
8
|
+
|
|
9
|
+
const rel: LinkType = 'rel' as LinkType;
|
|
10
|
+
|
|
11
|
+
function edge(source: string, target: string, type: LinkType = rel): ICycleGuardEdge {
|
|
12
|
+
return { source: source as MemoryId, target: target as MemoryId, type };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('buildCycleKey', () => {
|
|
16
|
+
test('is deterministic for the same edge', () => {
|
|
17
|
+
expect(buildCycleKey(edge('a', 'b'))).toSucceedAndSatisfy((key: string) => {
|
|
18
|
+
expect(buildCycleKey(edge('a', 'b'))).toSucceedWith(key);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('differs by direction and type', () => {
|
|
23
|
+
const forward = buildCycleKey(edge('a', 'b')).orThrow();
|
|
24
|
+
const reverse = buildCycleKey(edge('b', 'a')).orThrow();
|
|
25
|
+
const typed = buildCycleKey(edge('a', 'b', 'other' as LinkType)).orThrow();
|
|
26
|
+
expect(forward).not.toBe(reverse);
|
|
27
|
+
expect(forward).not.toBe(typed);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('assertNoCycles', () => {
|
|
32
|
+
test('accepts an acyclic graph', () => {
|
|
33
|
+
expect(assertNoCycles([edge('a', 'b')], [edge('b', 'c'), edge('c', 'd')])).toSucceed();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('rejects a proposed edge that closes a cycle against existing edges', () => {
|
|
37
|
+
expect(assertNoCycles([edge('a', 'b'), edge('b', 'c')], [edge('c', 'a')])).toFailWith(
|
|
38
|
+
/would create a cycle/
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('rejects a cycle formed entirely within the proposed batch', () => {
|
|
43
|
+
expect(assertNoCycles([], [edge('a', 'b'), edge('b', 'a')])).toFailWith(/would create a cycle/);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('rejects a self-loop', () => {
|
|
47
|
+
expect(assertNoCycles([], [edge('a', 'a')])).toFailWith(/self-loop/);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('tolerates a duplicate proposed edge (deduped by canonical key)', () => {
|
|
51
|
+
expect(assertNoCycles([], [edge('a', 'b'), edge('a', 'b')])).toSucceed();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('tolerates a duplicate existing edge', () => {
|
|
55
|
+
expect(assertNoCycles([edge('a', 'b'), edge('a', 'b')], [edge('b', 'c')])).toSucceed();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('accepts a diamond (shared descendant is not a cycle)', () => {
|
|
59
|
+
expect(assertNoCycles([], [edge('a', 'b'), edge('a', 'c'), edge('b', 'd'), edge('c', 'd')])).toSucceed();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('reachability tolerates a frontier node reached by two paths (revisit guard)', () => {
|
|
63
|
+
// Existing graph: a->c, a->b, b->c. A proposed edge z->a runs reachability
|
|
64
|
+
// from `a`, whose DFS pushes `c` twice before it is visited — exercising the
|
|
65
|
+
// visited-node skip in the traversal.
|
|
66
|
+
expect(assertNoCycles([edge('a', 'c'), edge('a', 'b'), edge('b', 'c')], [edge('z', 'a')])).toSucceed();
|
|
67
|
+
});
|
|
68
|
+
});
|