@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,226 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import '@fgv/ts-utils-jest';
|
|
7
|
+
import {
|
|
8
|
+
AsOfRetriever,
|
|
9
|
+
CurrentValidRetriever,
|
|
10
|
+
HistoryRetriever,
|
|
11
|
+
HybridRetriever,
|
|
12
|
+
IIndexedMemoryRecord,
|
|
13
|
+
IMemoryRecord,
|
|
14
|
+
Kind,
|
|
15
|
+
MemoryIndex,
|
|
16
|
+
MemoryScopeKey,
|
|
17
|
+
RecencyRetriever,
|
|
18
|
+
ScoreUnionMergeStrategy,
|
|
19
|
+
envelopeConverter
|
|
20
|
+
} from '../../../index';
|
|
21
|
+
|
|
22
|
+
const factKind: Kind = 'fact' as Kind;
|
|
23
|
+
|
|
24
|
+
interface IVersionSpec {
|
|
25
|
+
readonly entityId: string;
|
|
26
|
+
readonly seq: number;
|
|
27
|
+
readonly validAt: number;
|
|
28
|
+
// eslint-disable-next-line @rushstack/no-new-null -- invalid_at null is the meaningful still-valid sentinel
|
|
29
|
+
readonly invalidAt?: number | null;
|
|
30
|
+
readonly kind?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function versionEntry(spec: IVersionSpec): IIndexedMemoryRecord {
|
|
34
|
+
const temporal: Record<string, unknown> = { valid_at: spec.validAt };
|
|
35
|
+
if (spec.invalidAt !== undefined) {
|
|
36
|
+
temporal.invalid_at = spec.invalidAt;
|
|
37
|
+
}
|
|
38
|
+
const record: IMemoryRecord<unknown> = {
|
|
39
|
+
envelope: envelopeConverter
|
|
40
|
+
.convert({
|
|
41
|
+
id: `${spec.entityId}-v${spec.seq}`,
|
|
42
|
+
entityId: spec.entityId,
|
|
43
|
+
kind: spec.kind ?? 'fact',
|
|
44
|
+
tags: [],
|
|
45
|
+
links: [],
|
|
46
|
+
created: spec.validAt,
|
|
47
|
+
updated: spec.validAt,
|
|
48
|
+
seq: spec.seq,
|
|
49
|
+
contentHash: `h-${spec.entityId}-${spec.seq}`,
|
|
50
|
+
provenance: { source: 'agent' },
|
|
51
|
+
temporal
|
|
52
|
+
})
|
|
53
|
+
.orThrow(),
|
|
54
|
+
body: `${spec.entityId} v${spec.seq}`
|
|
55
|
+
};
|
|
56
|
+
return { scope: `facts/entities/${spec.entityId}` as MemoryScopeKey, record };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A flat (non-temporal) entry, to prove the temporal retrievers ignore it. */
|
|
60
|
+
function flatEntry(id: string): IIndexedMemoryRecord {
|
|
61
|
+
const record: IMemoryRecord<unknown> = {
|
|
62
|
+
envelope: envelopeConverter
|
|
63
|
+
.convert({
|
|
64
|
+
id,
|
|
65
|
+
entityId: id,
|
|
66
|
+
kind: 'knowledge',
|
|
67
|
+
tags: [],
|
|
68
|
+
links: [],
|
|
69
|
+
created: 0,
|
|
70
|
+
updated: 0,
|
|
71
|
+
seq: 0,
|
|
72
|
+
contentHash: `h-${id}`,
|
|
73
|
+
provenance: { source: 'agent' }
|
|
74
|
+
})
|
|
75
|
+
.orThrow(),
|
|
76
|
+
body: id
|
|
77
|
+
};
|
|
78
|
+
return { scope: 'knowledge' as MemoryScopeKey, record };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function ids(records: ReadonlyArray<IMemoryRecord<unknown>>): string[] {
|
|
82
|
+
return records.map((r) => r.envelope.id as string);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// fact-1: v1 [100,200), v2 [200,∞) current. fact-2: v1 [150,∞) current. Plus a flat doc.
|
|
86
|
+
function buildIndex(): MemoryIndex {
|
|
87
|
+
const index = MemoryIndex.create().orThrow();
|
|
88
|
+
index
|
|
89
|
+
.rebuild([
|
|
90
|
+
versionEntry({ entityId: 'fact-1', seq: 1, validAt: 100, invalidAt: 200 }),
|
|
91
|
+
versionEntry({ entityId: 'fact-1', seq: 2, validAt: 200 }),
|
|
92
|
+
versionEntry({ entityId: 'fact-2', seq: 3, validAt: 150 }),
|
|
93
|
+
flatEntry('doc-a')
|
|
94
|
+
])
|
|
95
|
+
.orThrow();
|
|
96
|
+
return index;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
describe('temporal retrievers', () => {
|
|
100
|
+
const index: MemoryIndex = buildIndex();
|
|
101
|
+
|
|
102
|
+
describe('CurrentValidRetriever', () => {
|
|
103
|
+
const retriever = CurrentValidRetriever.create(index).orThrow();
|
|
104
|
+
|
|
105
|
+
test('declares supportsTemporalQuery', () => {
|
|
106
|
+
expect(retriever.capabilities.supportsTemporalQuery).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('returns the current version per temporal entity, ignoring flat records', async () => {
|
|
110
|
+
expect(await retriever.retrieve({})).toSucceedAndSatisfy((records) => {
|
|
111
|
+
expect(ids(records).sort()).toEqual(['fact-1-v2', 'fact-2-v3']);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('honors scope / kind pre-filter and limit', async () => {
|
|
116
|
+
expect(await retriever.retrieve({ kind: factKind, limit: 1 })).toSucceedAndSatisfy((records) => {
|
|
117
|
+
expect(records).toHaveLength(1);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('AsOfRetriever', () => {
|
|
123
|
+
const retriever = AsOfRetriever.create(index).orThrow();
|
|
124
|
+
|
|
125
|
+
test('empty when no asOf axis is supplied', async () => {
|
|
126
|
+
expect(await retriever.retrieve({})).toSucceedWith([]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('returns the version valid at asOf per entity', async () => {
|
|
130
|
+
expect(await retriever.retrieve({ asOf: 150 })).toSucceedAndSatisfy((records) => {
|
|
131
|
+
// fact-1 v1 valid at 150; fact-2 v1 valid at 150.
|
|
132
|
+
expect(ids(records).sort()).toEqual(['fact-1-v1', 'fact-2-v3']);
|
|
133
|
+
});
|
|
134
|
+
expect(await retriever.retrieve({ asOf: 250 })).toSucceedAndSatisfy((records) => {
|
|
135
|
+
expect(ids(records).sort()).toEqual(['fact-1-v2', 'fact-2-v3']);
|
|
136
|
+
});
|
|
137
|
+
expect(await retriever.retrieve({ asOf: 50 })).toSucceedWith([]);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('HistoryRetriever', () => {
|
|
142
|
+
const retriever = HistoryRetriever.create(index).orThrow();
|
|
143
|
+
|
|
144
|
+
test('returns all versions ascending by valid_at', async () => {
|
|
145
|
+
expect(await retriever.retrieve({ kind: factKind })).toSucceedAndSatisfy((records) => {
|
|
146
|
+
expect(ids(records)).toEqual(['fact-1-v1', 'fact-2-v3', 'fact-1-v2']);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('applies the limit after ordering', async () => {
|
|
151
|
+
expect(await retriever.retrieve({ kind: factKind, limit: 2 })).toSucceedAndSatisfy((records) => {
|
|
152
|
+
expect(ids(records)).toEqual(['fact-1-v1', 'fact-2-v3']);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('orders by created when a version has no valid_at', async () => {
|
|
157
|
+
const idx = MemoryIndex.create().orThrow();
|
|
158
|
+
const noValidAt: IIndexedMemoryRecord = {
|
|
159
|
+
scope: 'facts/entities/nv' as MemoryScopeKey,
|
|
160
|
+
record: {
|
|
161
|
+
envelope: envelopeConverter
|
|
162
|
+
.convert({
|
|
163
|
+
id: 'nv-v1',
|
|
164
|
+
entityId: 'nv',
|
|
165
|
+
kind: 'fact',
|
|
166
|
+
tags: [],
|
|
167
|
+
links: [],
|
|
168
|
+
created: 700,
|
|
169
|
+
updated: 700,
|
|
170
|
+
seq: 1,
|
|
171
|
+
contentHash: 'h',
|
|
172
|
+
provenance: { source: 'agent' },
|
|
173
|
+
temporal: {}
|
|
174
|
+
})
|
|
175
|
+
.orThrow(),
|
|
176
|
+
body: 'nv'
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
idx.rebuild([noValidAt, versionEntry({ entityId: 'early', seq: 2, validAt: 100 })]).orThrow();
|
|
180
|
+
const r = HistoryRetriever.create(idx).orThrow();
|
|
181
|
+
// `nv` has no valid_at → its start defaults to created (700), after early (100).
|
|
182
|
+
expect(await r.retrieve({})).toSucceedAndSatisfy((records) => {
|
|
183
|
+
expect(ids(records)).toEqual(['early-v2', 'nv-v1']);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test('breaks an equal-valid_at tie by ascending seq', async () => {
|
|
188
|
+
// Two versions of one entity sharing a valid_at — the seq tiebreak orders them.
|
|
189
|
+
const tieIndex = MemoryIndex.create().orThrow();
|
|
190
|
+
tieIndex
|
|
191
|
+
.rebuild([
|
|
192
|
+
versionEntry({ entityId: 'tie', seq: 5, validAt: 500, invalidAt: 500 }),
|
|
193
|
+
versionEntry({ entityId: 'tie', seq: 4, validAt: 500 })
|
|
194
|
+
])
|
|
195
|
+
.orThrow();
|
|
196
|
+
const tieRetriever = HistoryRetriever.create(tieIndex).orThrow();
|
|
197
|
+
expect(await tieRetriever.retrieve({})).toSucceedAndSatisfy((records) => {
|
|
198
|
+
expect(ids(records)).toEqual(['tie-v4', 'tie-v5']);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('loud-degrade + Hybrid composite', () => {
|
|
204
|
+
test('a non-temporal retriever loud-degrades on an asOf query', async () => {
|
|
205
|
+
const recency = RecencyRetriever.create(index).orThrow();
|
|
206
|
+
expect(await recency.retrieve({ asOf: 150 })).toFailWith(/temporal query requires temporal index/i);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('a Hybrid of [Recency, AsOf] lights up temporal recall via the temporal child', async () => {
|
|
210
|
+
const recency = RecencyRetriever.create(index).orThrow();
|
|
211
|
+
const asOf = AsOfRetriever.create(index).orThrow();
|
|
212
|
+
const hybrid = HybridRetriever.create(
|
|
213
|
+
[recency, asOf],
|
|
214
|
+
ScoreUnionMergeStrategy.create().orThrow()
|
|
215
|
+
).orThrow();
|
|
216
|
+
// The union supports temporal query, so the guard passes; Recency gets asOf
|
|
217
|
+
// stripped (returns everything), AsOf returns the valid-at versions.
|
|
218
|
+
expect(hybrid.capabilities.supportsTemporalQuery).toBe(true);
|
|
219
|
+
expect(await hybrid.retrieve({ asOf: 250, kind: factKind })).toSucceedAndSatisfy((records) => {
|
|
220
|
+
// fact-1-v2 is surfaced by BOTH (recency all + asOf), so it ranks first.
|
|
221
|
+
expect(ids(records)[0]).toBe('fact-1-v2');
|
|
222
|
+
expect(ids(records)).toContain('fact-2-v3');
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Antagonist torture tests — bi-temporal / two-clock boundary math (target class
|
|
8
|
+
* 2), complementing `temporalCodec.test.ts` (which already covers the
|
|
9
|
+
* `invalid_at`-exclusive boundary and the future-dated-supersede no-gap case).
|
|
10
|
+
* This file targets two remaining gaps: the `valid_at`-inclusive boundary, and
|
|
11
|
+
* an out-of-order sequence of `valid_at` writes.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import '@fgv/ts-utils-jest';
|
|
15
|
+
import { Converters } from '@fgv/ts-utils';
|
|
16
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
17
|
+
import {
|
|
18
|
+
BodyConverterRegistry,
|
|
19
|
+
EntityId,
|
|
20
|
+
FileTreeMemoryStore,
|
|
21
|
+
IBodyConverterRegistry,
|
|
22
|
+
IIdentityCodec,
|
|
23
|
+
IMemoryRecord,
|
|
24
|
+
IWritePolicy,
|
|
25
|
+
Kind,
|
|
26
|
+
MemoryScopeKey,
|
|
27
|
+
TemporalIdentityCodec,
|
|
28
|
+
TemporalVersionedPolicy,
|
|
29
|
+
envelopeConverter,
|
|
30
|
+
isVersionValidAt
|
|
31
|
+
} from '../../../index';
|
|
32
|
+
|
|
33
|
+
const factKind: Kind = 'fact' as Kind;
|
|
34
|
+
const factScope: MemoryScopeKey = 'facts/entities/fact-1' as MemoryScopeKey;
|
|
35
|
+
|
|
36
|
+
function mutableRoot(): FileTree.IMutableFileTreeDirectoryItem {
|
|
37
|
+
const tree = FileTree.inMemory([], { mutable: true }).orThrow();
|
|
38
|
+
const root = tree.getDirectory('/').orThrow();
|
|
39
|
+
if (!FileTree.isMutableDirectoryItem(root)) {
|
|
40
|
+
throw new Error('expected a mutable root directory');
|
|
41
|
+
}
|
|
42
|
+
return root;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function registry(): IBodyConverterRegistry {
|
|
46
|
+
const reg = BodyConverterRegistry.create().orThrow();
|
|
47
|
+
reg.register(factKind, Converters.string);
|
|
48
|
+
return reg;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const codecs: ReadonlyMap<Kind, IIdentityCodec> = new Map<Kind, IIdentityCodec>([
|
|
52
|
+
[factKind, TemporalIdentityCodec.create('facts').orThrow()]
|
|
53
|
+
]);
|
|
54
|
+
const policies: ReadonlyMap<Kind, IWritePolicy> = new Map<Kind, IWritePolicy>([
|
|
55
|
+
[factKind, TemporalVersionedPolicy.create().orThrow()]
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
function factRecordAt(entityId: string, body: string, validAt?: number): IMemoryRecord<unknown> {
|
|
59
|
+
return {
|
|
60
|
+
envelope: {
|
|
61
|
+
id: entityId as unknown as IMemoryRecord<unknown>['envelope']['id'],
|
|
62
|
+
entityId: entityId as EntityId,
|
|
63
|
+
kind: factKind,
|
|
64
|
+
tags: [],
|
|
65
|
+
links: [],
|
|
66
|
+
created: 0,
|
|
67
|
+
updated: 0,
|
|
68
|
+
seq: 0,
|
|
69
|
+
contentHash: '',
|
|
70
|
+
provenance: { source: 'agent' },
|
|
71
|
+
...(validAt !== undefined ? { temporal: { valid_at: validAt } } : {})
|
|
72
|
+
},
|
|
73
|
+
body
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
describe('antagonist — isVersionValidAt: valid_at is INCLUSIVE (start-boundary parity with the exclusive end)', () => {
|
|
78
|
+
// Wrong impl this catches: a `>` (strict) comparison for the start boundary
|
|
79
|
+
// instead of `<=`, which would incorrectly exclude the instant a version FIRST
|
|
80
|
+
// became valid — an asymmetry with the already-tested exclusive end boundary.
|
|
81
|
+
test('a version is valid AT its own valid_at instant, not just strictly after it', () => {
|
|
82
|
+
const v1 = {
|
|
83
|
+
envelope: envelopeConverter
|
|
84
|
+
.convert({
|
|
85
|
+
id: 'fact-1-v1',
|
|
86
|
+
entityId: 'fact-1',
|
|
87
|
+
kind: 'fact',
|
|
88
|
+
tags: [],
|
|
89
|
+
links: [],
|
|
90
|
+
created: 100,
|
|
91
|
+
updated: 100,
|
|
92
|
+
seq: 1,
|
|
93
|
+
contentHash: 'h1',
|
|
94
|
+
provenance: { source: 'agent' },
|
|
95
|
+
temporal: { valid_at: 100, invalid_at: 200 }
|
|
96
|
+
})
|
|
97
|
+
.orThrow(),
|
|
98
|
+
body: 'v1'
|
|
99
|
+
};
|
|
100
|
+
expect(isVersionValidAt(v1, 99)).toBe(false); // strictly before
|
|
101
|
+
expect(isVersionValidAt(v1, 100)).toBe(true); // AT valid_at — inclusive start
|
|
102
|
+
expect(isVersionValidAt(v1, 199)).toBe(true); // just before invalid_at
|
|
103
|
+
expect(isVersionValidAt(v1, 200)).toBe(false); // AT invalid_at — exclusive end
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('antagonist — out-of-order valid_at sequence (no gap/overlap regardless of write order)', () => {
|
|
108
|
+
let clockValue: number;
|
|
109
|
+
const clock = (): number => clockValue;
|
|
110
|
+
|
|
111
|
+
function createStore(): FileTreeMemoryStore {
|
|
112
|
+
return FileTreeMemoryStore.create({
|
|
113
|
+
root: mutableRoot(),
|
|
114
|
+
registry: registry(),
|
|
115
|
+
codecs,
|
|
116
|
+
writePolicies: policies,
|
|
117
|
+
clock
|
|
118
|
+
}).orThrow();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Wrong impl this catches: an implementation that assumes valid_at is
|
|
122
|
+
// monotonically increasing with seq (e.g. sorts by valid_at instead of seq for
|
|
123
|
+
// "current" resolution), which breaks the moment a caller backdates a SECOND
|
|
124
|
+
// time after an earlier future-dated write.
|
|
125
|
+
test('two consecutive backdated writes still close each prior interval at the NEW valid_at, not now', async () => {
|
|
126
|
+
const store = createStore();
|
|
127
|
+
clockValue = 1000;
|
|
128
|
+
// v1: valid_at defaults to transaction time (1000).
|
|
129
|
+
expect(await store.put(factRecordAt('fact-1', 'blue'))).toSucceed();
|
|
130
|
+
|
|
131
|
+
clockValue = 2000;
|
|
132
|
+
// v2: backdated BEHIND v1's own valid_at (500 < 1000) — a correction asserting
|
|
133
|
+
// "actually, this was already true starting at t=500".
|
|
134
|
+
expect(await store.put(factRecordAt('fact-1', 'grey', 500))).toSucceedAndSatisfy((put) => {
|
|
135
|
+
expect(put.envelope.temporal).toEqual({ valid_at: 500 });
|
|
136
|
+
});
|
|
137
|
+
// v1's interval must close at v2's valid_at (500), not the transaction time (2000).
|
|
138
|
+
expect(await store.getById(factScope, 'fact-1-v1' as never)).toSucceedAndSatisfy((v1) => {
|
|
139
|
+
expect(v1?.envelope.temporal).toEqual({ valid_at: 1000, invalid_at: 500 });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
clockValue = 3000;
|
|
143
|
+
// v3: backdated again, even further behind (100 < 500) — a SECOND correction.
|
|
144
|
+
expect(await store.put(factRecordAt('fact-1', 'black', 100))).toSucceedAndSatisfy((put) => {
|
|
145
|
+
expect(put.envelope.temporal).toEqual({ valid_at: 100 });
|
|
146
|
+
});
|
|
147
|
+
// v2's interval must close at v3's valid_at (100), not the transaction time (3000).
|
|
148
|
+
expect(await store.getById(factScope, 'fact-1-v2' as never)).toSucceedAndSatisfy((v2) => {
|
|
149
|
+
expect(v2?.envelope.temporal).toEqual({ valid_at: 500, invalid_at: 100 });
|
|
150
|
+
});
|
|
151
|
+
// `selectCurrentVersion` still resolves the newest write by `seq` (v3),
|
|
152
|
+
// regardless of `valid_at` no longer being seq-monotonic.
|
|
153
|
+
expect(await store.get(factKind, 'fact-1' as EntityId)).toSucceedAndSatisfy((cur) => {
|
|
154
|
+
expect(cur?.envelope.id).toBe('fact-1-v3');
|
|
155
|
+
expect(cur?.body).toBe('black');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -165,6 +165,20 @@ describe('FileTreeMemoryStore', () => {
|
|
|
165
165
|
const root = mutableRoot([{ path: 'elsewhere/doc.md', contents: knowledgeFile('doc', 'body') }]);
|
|
166
166
|
expect(createStore({ root })).toFailWith(/does not match expected scope/i);
|
|
167
167
|
});
|
|
168
|
+
|
|
169
|
+
test('fails loudly when a seeded file envelope entityId disagrees with its subtree scope', () => {
|
|
170
|
+
// The id matches the filename stem and the scope round-trips, but the
|
|
171
|
+
// envelope's own entityId is foreign — a tamper/corruption the codec
|
|
172
|
+
// round-trip alone (which derives entityId from the scope path, never
|
|
173
|
+
// reading the envelope field) would not catch. entityId is trusted
|
|
174
|
+
// verbatim downstream, so the load must reject it.
|
|
175
|
+
const root = mutableRoot([
|
|
176
|
+
{ path: 'knowledge/doc-a.md', contents: knowledgeFile('doc-a', 'body', { entityId: 'doc-999' }) }
|
|
177
|
+
]);
|
|
178
|
+
expect(createStore({ root })).toFailWith(
|
|
179
|
+
/entityId 'doc-999' does not match scope-derived entityId 'doc-a'/i
|
|
180
|
+
);
|
|
181
|
+
});
|
|
168
182
|
});
|
|
169
183
|
|
|
170
184
|
describe('factory defaults', () => {
|
|
@@ -297,6 +311,56 @@ describe('FileTreeMemoryStore', () => {
|
|
|
297
311
|
// doc-b was never written.
|
|
298
312
|
expect(await store.get(knowledgeKind, 'doc-b' as EntityId)).toSucceedWith(undefined);
|
|
299
313
|
});
|
|
314
|
+
|
|
315
|
+
test('a same-id re-put with unchanged body but revised tags applies the update (content dedup must not swallow it)', async () => {
|
|
316
|
+
// Content-scoped dedup collapses identical content across DIFFERENT ids,
|
|
317
|
+
// but a same-id re-put whose body/links are unchanged while tags change is
|
|
318
|
+
// a real LWW update: tags are outside the content hash yet inside the
|
|
319
|
+
// policy's mutableFields, so the metadata revision must reach applyUpdate.
|
|
320
|
+
const store = createStore().orThrow();
|
|
321
|
+
let firstSeq: number = -1;
|
|
322
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'same', tags: ['orig'] }))).toSucceedAndSatisfy(
|
|
323
|
+
(first: IMemoryRecord<unknown>) => {
|
|
324
|
+
firstSeq = first.envelope.seq;
|
|
325
|
+
expect(first.envelope.tags).toEqual(['orig']);
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
clockValue = 2000;
|
|
329
|
+
expect(
|
|
330
|
+
await store.put(makeRecord({ id: 'doc', body: 'same', tags: ['orig', 'added'] }))
|
|
331
|
+
).toSucceedAndSatisfy((second: IMemoryRecord<unknown>) => {
|
|
332
|
+
expect(second.envelope.tags).toEqual(['orig', 'added']);
|
|
333
|
+
expect(second.envelope.seq).toBe(firstSeq + 1);
|
|
334
|
+
expect(second.envelope.updated).toBe(2000);
|
|
335
|
+
});
|
|
336
|
+
// The persisted record reflects the metadata update.
|
|
337
|
+
expect(await store.get(knowledgeKind, 'doc' as EntityId)).toSucceedAndSatisfy(
|
|
338
|
+
(r: IMemoryRecord<unknown> | undefined) => {
|
|
339
|
+
expect(r?.envelope.tags).toEqual(['orig', 'added']);
|
|
340
|
+
}
|
|
341
|
+
);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test('a same-id re-put with unchanged body AND unchanged metadata stays a no-op', async () => {
|
|
345
|
+
// The idempotence guard: identical content AND identical mutable metadata
|
|
346
|
+
// must not bump seq/updated (regression guard alongside the fix above).
|
|
347
|
+
const store = createStore().orThrow();
|
|
348
|
+
let firstSeq: number = -1;
|
|
349
|
+
let firstUpdated: number = -1;
|
|
350
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'same', tags: ['a'] }))).toSucceedAndSatisfy(
|
|
351
|
+
(first: IMemoryRecord<unknown>) => {
|
|
352
|
+
firstSeq = first.envelope.seq;
|
|
353
|
+
firstUpdated = first.envelope.updated;
|
|
354
|
+
}
|
|
355
|
+
);
|
|
356
|
+
clockValue = 2000;
|
|
357
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'same', tags: ['a'] }))).toSucceedAndSatisfy(
|
|
358
|
+
(second: IMemoryRecord<unknown>) => {
|
|
359
|
+
expect(second.envelope.seq).toBe(firstSeq);
|
|
360
|
+
expect(second.envelope.updated).toBe(firstUpdated);
|
|
361
|
+
}
|
|
362
|
+
);
|
|
363
|
+
});
|
|
300
364
|
});
|
|
301
365
|
|
|
302
366
|
describe('last-write-wins update', () => {
|
|
@@ -546,8 +610,11 @@ describe('FileTreeMemoryStore', () => {
|
|
|
546
610
|
});
|
|
547
611
|
});
|
|
548
612
|
|
|
549
|
-
describe('versioned codec
|
|
613
|
+
describe('versioned codec misconfiguration', () => {
|
|
550
614
|
const versionedKind: Kind = 'versioned' as Kind;
|
|
615
|
+
// A codec that reports isVersioned:true but does NOT implement the temporal
|
|
616
|
+
// codec interface (no encodeVersion/decodeVersion) — a misconfiguration the
|
|
617
|
+
// store must reject loudly rather than mis-handle.
|
|
551
618
|
const versionedCodec: IIdentityCodec = {
|
|
552
619
|
encode: (entityId: EntityId): Result<IIdentityCodecResult> =>
|
|
553
620
|
succeed({ scope: 'versioned' as MemoryScopeKey, idStem: entityId as string, isVersioned: true }),
|
|
@@ -566,21 +633,21 @@ describe('FileTreeMemoryStore', () => {
|
|
|
566
633
|
}).orThrow();
|
|
567
634
|
}
|
|
568
635
|
|
|
569
|
-
test('put fails loudly
|
|
636
|
+
test('put fails loudly when a versioned codec omits the temporal interface', async () => {
|
|
570
637
|
expect(await versionedStore().put(makeRecord({ id: 'v', kind: 'versioned', body: 'x' }))).toFailWith(
|
|
571
|
-
/
|
|
638
|
+
/does not implement the temporal codec interface/i
|
|
572
639
|
);
|
|
573
640
|
});
|
|
574
641
|
|
|
575
|
-
test('get fails loudly
|
|
642
|
+
test('get fails loudly when a versioned codec omits the temporal interface', async () => {
|
|
576
643
|
expect(await versionedStore().get(versionedKind, 'v' as EntityId)).toFailWith(
|
|
577
|
-
/
|
|
644
|
+
/does not implement the temporal codec interface/i
|
|
578
645
|
);
|
|
579
646
|
});
|
|
580
647
|
|
|
581
|
-
test('delete fails loudly
|
|
648
|
+
test('delete fails loudly when a versioned codec omits the temporal interface', async () => {
|
|
582
649
|
expect(await versionedStore().delete(versionedKind, 'v' as EntityId)).toFailWith(
|
|
583
|
-
/
|
|
650
|
+
/does not implement the temporal codec interface/i
|
|
584
651
|
);
|
|
585
652
|
});
|
|
586
653
|
});
|
|
@@ -718,6 +785,30 @@ describe('FileTreeMemoryStore', () => {
|
|
|
718
785
|
});
|
|
719
786
|
});
|
|
720
787
|
|
|
788
|
+
test('entity-scoped dedup: a same-id re-put with unchanged body but revised tags applies the update', async () => {
|
|
789
|
+
// The metadata-unchanged gate on the same-id no-op is shared across BOTH
|
|
790
|
+
// dedup scopes; this asserts it on the entity (experience) family too, so a
|
|
791
|
+
// future split of the two branches cannot silently regress this side.
|
|
792
|
+
const store = memoryStore();
|
|
793
|
+
let firstSeq: number = -1;
|
|
794
|
+
expect(
|
|
795
|
+
await store.put(
|
|
796
|
+
makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'same', tags: ['a'] })
|
|
797
|
+
)
|
|
798
|
+
).toSucceedAndSatisfy((r) => {
|
|
799
|
+
firstSeq = r.envelope.seq;
|
|
800
|
+
expect(r.envelope.tags).toEqual(['a']);
|
|
801
|
+
});
|
|
802
|
+
expect(
|
|
803
|
+
await store.put(
|
|
804
|
+
makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'same', tags: ['a', 'b'] })
|
|
805
|
+
)
|
|
806
|
+
).toSucceedAndSatisfy((r) => {
|
|
807
|
+
expect(r.envelope.tags).toEqual(['a', 'b']);
|
|
808
|
+
expect(r.envelope.seq).toBe(firstSeq + 1);
|
|
809
|
+
});
|
|
810
|
+
});
|
|
811
|
+
|
|
721
812
|
test('merge-patch updates the body of an existing memory entity (distinct content)', async () => {
|
|
722
813
|
const store = memoryStore();
|
|
723
814
|
let firstCreated: number = -1;
|