@fgv/ts-agent-memory 5.1.0-36
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/chunked-rush-logs/ts-agent-memory.build.chunks.jsonl +9 -0
- package/.rush/temp/f6a88bfdd66517ccb98c2c7ae1be6e6fe9e15d38.tar.log +282 -0
- package/.rush/temp/operation/build/all.log +9 -0
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +688 -0
- package/LICENSE +21 -0
- package/README.md +45 -0
- package/config/api-extractor.json +343 -0
- package/config/jest.config.json +14 -0
- package/config/rig.json +4 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/packlets/converters/bodyConverterRegistry.js +51 -0
- package/dist/packlets/converters/bodyConverterRegistry.js.map +1 -0
- package/dist/packlets/converters/envelopeConverter.js +159 -0
- package/dist/packlets/converters/envelopeConverter.js.map +1 -0
- package/dist/packlets/converters/index.js +7 -0
- package/dist/packlets/converters/index.js.map +1 -0
- package/dist/packlets/index/index.js +6 -0
- package/dist/packlets/index/index.js.map +1 -0
- package/dist/packlets/index/memoryIndex.js +170 -0
- package/dist/packlets/index/memoryIndex.js.map +1 -0
- package/dist/packlets/observe/index.js +7 -0
- package/dist/packlets/observe/index.js.map +1 -0
- package/dist/packlets/observe/memoryObservationStore.js +119 -0
- package/dist/packlets/observe/memoryObservationStore.js.map +1 -0
- package/dist/packlets/observe/observer.js +6 -0
- package/dist/packlets/observe/observer.js.map +1 -0
- package/dist/packlets/retrieve/hybridRetriever.js +135 -0
- package/dist/packlets/retrieve/hybridRetriever.js.map +1 -0
- package/dist/packlets/retrieve/index.js +12 -0
- package/dist/packlets/retrieve/index.js.map +1 -0
- package/dist/packlets/retrieve/linkTraversalRetriever.js +143 -0
- package/dist/packlets/retrieve/linkTraversalRetriever.js.map +1 -0
- package/dist/packlets/retrieve/recencyRetriever.js +33 -0
- package/dist/packlets/retrieve/recencyRetriever.js.map +1 -0
- package/dist/packlets/retrieve/retriever.js +110 -0
- package/dist/packlets/retrieve/retriever.js.map +1 -0
- package/dist/packlets/retrieve/semanticRetriever.js +86 -0
- package/dist/packlets/retrieve/semanticRetriever.js.map +1 -0
- package/dist/packlets/retrieve/structuredFilterRetriever.js +37 -0
- package/dist/packlets/retrieve/structuredFilterRetriever.js.map +1 -0
- package/dist/packlets/retrieve/tagRetriever.js +37 -0
- package/dist/packlets/retrieve/tagRetriever.js.map +1 -0
- package/dist/packlets/store/fileTreeMemoryStore.js +698 -0
- package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -0
- package/dist/packlets/store/index.js +7 -0
- package/dist/packlets/store/index.js.map +1 -0
- package/dist/packlets/store/scopeEncoding.js +31 -0
- package/dist/packlets/store/scopeEncoding.js.map +1 -0
- package/dist/packlets/types/envelope.js +6 -0
- package/dist/packlets/types/envelope.js.map +1 -0
- package/dist/packlets/types/filenameSafety.js +52 -0
- package/dist/packlets/types/filenameSafety.js.map +1 -0
- package/dist/packlets/types/identityCodec.js +184 -0
- package/dist/packlets/types/identityCodec.js.map +1 -0
- package/dist/packlets/types/ids.js +67 -0
- package/dist/packlets/types/ids.js.map +1 -0
- package/dist/packlets/types/index.js +10 -0
- package/dist/packlets/types/index.js.map +1 -0
- package/dist/packlets/types/writePolicy.js +263 -0
- package/dist/packlets/types/writePolicy.js.map +1 -0
- package/dist/packlets/vector/inMemoryCosineIndex.js +150 -0
- package/dist/packlets/vector/inMemoryCosineIndex.js.map +1 -0
- package/dist/packlets/vector/index.js +7 -0
- package/dist/packlets/vector/index.js.map +1 -0
- package/dist/packlets/vector/vectorIndex.js +6 -0
- package/dist/packlets/vector/vectorIndex.js.map +1 -0
- package/dist/test/unit/converters/bodyConverterRegistry.test.js +72 -0
- package/dist/test/unit/converters/bodyConverterRegistry.test.js.map +1 -0
- package/dist/test/unit/converters/envelopeConverter.test.js +196 -0
- package/dist/test/unit/converters/envelopeConverter.test.js.map +1 -0
- package/dist/test/unit/index/memoryIndex.test.js +152 -0
- package/dist/test/unit/index/memoryIndex.test.js.map +1 -0
- package/dist/test/unit/observe/memoryObservationStore.test.js +118 -0
- package/dist/test/unit/observe/memoryObservationStore.test.js.map +1 -0
- package/dist/test/unit/retrieve/linkTraversalRetriever.test.js +182 -0
- package/dist/test/unit/retrieve/linkTraversalRetriever.test.js.map +1 -0
- package/dist/test/unit/retrieve/retrievers.test.js +506 -0
- package/dist/test/unit/retrieve/retrievers.test.js.map +1 -0
- package/dist/test/unit/store/embedOnWrite.test.js +260 -0
- package/dist/test/unit/store/embedOnWrite.test.js.map +1 -0
- package/dist/test/unit/store/fileTreeMemoryStore.test.js +647 -0
- package/dist/test/unit/store/fileTreeMemoryStore.test.js.map +1 -0
- package/dist/test/unit/store/observations.test.js +239 -0
- package/dist/test/unit/store/observations.test.js.map +1 -0
- package/dist/test/unit/store/scopeEncoding.test.js +24 -0
- package/dist/test/unit/store/scopeEncoding.test.js.map +1 -0
- package/dist/test/unit/types/identityCodec.test.js +187 -0
- package/dist/test/unit/types/identityCodec.test.js.map +1 -0
- package/dist/test/unit/types/ids.test.js +84 -0
- package/dist/test/unit/types/ids.test.js.map +1 -0
- package/dist/test/unit/types/writePolicy.test.js +241 -0
- package/dist/test/unit/types/writePolicy.test.js.map +1 -0
- package/dist/test/unit/vector/inMemoryCosineIndex.test.js +192 -0
- package/dist/test/unit/vector/inMemoryCosineIndex.test.js.map +1 -0
- package/dist/test/unit/vector/vectorIndex.test.js +42 -0
- package/dist/test/unit/vector/vectorIndex.test.js.map +1 -0
- package/dist/ts-agent-memory.d.ts +1901 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/eslint.config.js +15 -0
- package/etc/ts-agent-memory.api.md +525 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +28 -0
- package/lib/index.js.map +1 -0
- package/lib/packlets/converters/bodyConverterRegistry.d.ts +64 -0
- package/lib/packlets/converters/bodyConverterRegistry.d.ts.map +1 -0
- package/lib/packlets/converters/bodyConverterRegistry.js +55 -0
- package/lib/packlets/converters/bodyConverterRegistry.js.map +1 -0
- package/lib/packlets/converters/envelopeConverter.d.ts +70 -0
- package/lib/packlets/converters/envelopeConverter.d.ts.map +1 -0
- package/lib/packlets/converters/envelopeConverter.js +166 -0
- package/lib/packlets/converters/envelopeConverter.js.map +1 -0
- package/lib/packlets/converters/index.d.ts +3 -0
- package/lib/packlets/converters/index.d.ts.map +1 -0
- package/lib/packlets/converters/index.js +23 -0
- package/lib/packlets/converters/index.js.map +1 -0
- package/lib/packlets/index/index.d.ts +2 -0
- package/lib/packlets/index/index.d.ts.map +1 -0
- package/lib/packlets/index/index.js +22 -0
- package/lib/packlets/index/index.js.map +1 -0
- package/lib/packlets/index/memoryIndex.d.ts +127 -0
- package/lib/packlets/index/memoryIndex.d.ts.map +1 -0
- package/lib/packlets/index/memoryIndex.js +174 -0
- package/lib/packlets/index/memoryIndex.js.map +1 -0
- package/lib/packlets/observe/index.d.ts +3 -0
- package/lib/packlets/observe/index.d.ts.map +1 -0
- package/lib/packlets/observe/index.js +23 -0
- package/lib/packlets/observe/index.js.map +1 -0
- package/lib/packlets/observe/memoryObservationStore.d.ts +91 -0
- package/lib/packlets/observe/memoryObservationStore.d.ts.map +1 -0
- package/lib/packlets/observe/memoryObservationStore.js +123 -0
- package/lib/packlets/observe/memoryObservationStore.js.map +1 -0
- package/lib/packlets/observe/observer.d.ts +110 -0
- package/lib/packlets/observe/observer.d.ts.map +1 -0
- package/lib/packlets/observe/observer.js +7 -0
- package/lib/packlets/observe/observer.js.map +1 -0
- package/lib/packlets/retrieve/hybridRetriever.d.ts +79 -0
- package/lib/packlets/retrieve/hybridRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/hybridRetriever.js +140 -0
- package/lib/packlets/retrieve/hybridRetriever.js.map +1 -0
- package/lib/packlets/retrieve/index.d.ts +8 -0
- package/lib/packlets/retrieve/index.d.ts.map +1 -0
- package/lib/packlets/retrieve/index.js +28 -0
- package/lib/packlets/retrieve/index.js.map +1 -0
- package/lib/packlets/retrieve/linkTraversalRetriever.d.ts +61 -0
- package/lib/packlets/retrieve/linkTraversalRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/linkTraversalRetriever.js +147 -0
- package/lib/packlets/retrieve/linkTraversalRetriever.js.map +1 -0
- package/lib/packlets/retrieve/recencyRetriever.d.ts +21 -0
- package/lib/packlets/retrieve/recencyRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/recencyRetriever.js +37 -0
- package/lib/packlets/retrieve/recencyRetriever.js.map +1 -0
- package/lib/packlets/retrieve/retriever.d.ts +133 -0
- package/lib/packlets/retrieve/retriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/retriever.js +119 -0
- package/lib/packlets/retrieve/retriever.js.map +1 -0
- package/lib/packlets/retrieve/semanticRetriever.d.ts +69 -0
- package/lib/packlets/retrieve/semanticRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/semanticRetriever.js +90 -0
- package/lib/packlets/retrieve/semanticRetriever.js.map +1 -0
- package/lib/packlets/retrieve/structuredFilterRetriever.d.ts +22 -0
- package/lib/packlets/retrieve/structuredFilterRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/structuredFilterRetriever.js +41 -0
- package/lib/packlets/retrieve/structuredFilterRetriever.js.map +1 -0
- package/lib/packlets/retrieve/tagRetriever.d.ts +22 -0
- package/lib/packlets/retrieve/tagRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/tagRetriever.js +41 -0
- package/lib/packlets/retrieve/tagRetriever.js.map +1 -0
- package/lib/packlets/store/fileTreeMemoryStore.d.ts +327 -0
- package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -0
- package/lib/packlets/store/fileTreeMemoryStore.js +702 -0
- package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -0
- package/lib/packlets/store/index.d.ts +3 -0
- package/lib/packlets/store/index.d.ts.map +1 -0
- package/lib/packlets/store/index.js +23 -0
- package/lib/packlets/store/index.js.map +1 -0
- package/lib/packlets/store/scopeEncoding.d.ts +19 -0
- package/lib/packlets/store/scopeEncoding.d.ts.map +1 -0
- package/lib/packlets/store/scopeEncoding.js +34 -0
- package/lib/packlets/store/scopeEncoding.js.map +1 -0
- package/lib/packlets/types/envelope.d.ts +119 -0
- package/lib/packlets/types/envelope.d.ts.map +1 -0
- package/lib/packlets/types/envelope.js +7 -0
- package/lib/packlets/types/envelope.js.map +1 -0
- package/lib/packlets/types/filenameSafety.d.ts +16 -0
- package/lib/packlets/types/filenameSafety.d.ts.map +1 -0
- package/lib/packlets/types/filenameSafety.js +55 -0
- package/lib/packlets/types/filenameSafety.js.map +1 -0
- package/lib/packlets/types/identityCodec.d.ts +136 -0
- package/lib/packlets/types/identityCodec.d.ts.map +1 -0
- package/lib/packlets/types/identityCodec.js +190 -0
- package/lib/packlets/types/identityCodec.js.map +1 -0
- package/lib/packlets/types/ids.d.ts +55 -0
- package/lib/packlets/types/ids.d.ts.map +1 -0
- package/lib/packlets/types/ids.js +70 -0
- package/lib/packlets/types/ids.js.map +1 -0
- package/lib/packlets/types/index.d.ts +6 -0
- package/lib/packlets/types/index.d.ts.map +1 -0
- package/lib/packlets/types/index.js +26 -0
- package/lib/packlets/types/index.js.map +1 -0
- package/lib/packlets/types/writePolicy.d.ts +213 -0
- package/lib/packlets/types/writePolicy.d.ts.map +1 -0
- package/lib/packlets/types/writePolicy.js +268 -0
- package/lib/packlets/types/writePolicy.js.map +1 -0
- package/lib/packlets/vector/inMemoryCosineIndex.d.ts +69 -0
- package/lib/packlets/vector/inMemoryCosineIndex.d.ts.map +1 -0
- package/lib/packlets/vector/inMemoryCosineIndex.js +154 -0
- package/lib/packlets/vector/inMemoryCosineIndex.js.map +1 -0
- package/lib/packlets/vector/index.d.ts +3 -0
- package/lib/packlets/vector/index.d.ts.map +1 -0
- package/lib/packlets/vector/index.js +23 -0
- package/lib/packlets/vector/index.js.map +1 -0
- package/lib/packlets/vector/vectorIndex.d.ts +68 -0
- package/lib/packlets/vector/vectorIndex.d.ts.map +1 -0
- package/lib/packlets/vector/vectorIndex.js +7 -0
- package/lib/packlets/vector/vectorIndex.js.map +1 -0
- package/lib/test/unit/converters/bodyConverterRegistry.test.d.ts +2 -0
- package/lib/test/unit/converters/bodyConverterRegistry.test.d.ts.map +1 -0
- package/lib/test/unit/converters/bodyConverterRegistry.test.js +74 -0
- package/lib/test/unit/converters/bodyConverterRegistry.test.js.map +1 -0
- package/lib/test/unit/converters/envelopeConverter.test.d.ts +2 -0
- package/lib/test/unit/converters/envelopeConverter.test.d.ts.map +1 -0
- package/lib/test/unit/converters/envelopeConverter.test.js +198 -0
- package/lib/test/unit/converters/envelopeConverter.test.js.map +1 -0
- package/lib/test/unit/index/memoryIndex.test.d.ts +2 -0
- package/lib/test/unit/index/memoryIndex.test.d.ts.map +1 -0
- package/lib/test/unit/index/memoryIndex.test.js +154 -0
- package/lib/test/unit/index/memoryIndex.test.js.map +1 -0
- package/lib/test/unit/observe/memoryObservationStore.test.d.ts +2 -0
- package/lib/test/unit/observe/memoryObservationStore.test.d.ts.map +1 -0
- package/lib/test/unit/observe/memoryObservationStore.test.js +120 -0
- package/lib/test/unit/observe/memoryObservationStore.test.js.map +1 -0
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.d.ts +2 -0
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.d.ts.map +1 -0
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.js +184 -0
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.js.map +1 -0
- package/lib/test/unit/retrieve/retrievers.test.d.ts +2 -0
- package/lib/test/unit/retrieve/retrievers.test.d.ts.map +1 -0
- package/lib/test/unit/retrieve/retrievers.test.js +508 -0
- package/lib/test/unit/retrieve/retrievers.test.js.map +1 -0
- package/lib/test/unit/store/embedOnWrite.test.d.ts +2 -0
- package/lib/test/unit/store/embedOnWrite.test.d.ts.map +1 -0
- package/lib/test/unit/store/embedOnWrite.test.js +262 -0
- package/lib/test/unit/store/embedOnWrite.test.js.map +1 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.d.ts +2 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.d.ts.map +1 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.js +649 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.js.map +1 -0
- package/lib/test/unit/store/observations.test.d.ts +2 -0
- package/lib/test/unit/store/observations.test.d.ts.map +1 -0
- package/lib/test/unit/store/observations.test.js +241 -0
- package/lib/test/unit/store/observations.test.js.map +1 -0
- package/lib/test/unit/store/scopeEncoding.test.d.ts +2 -0
- package/lib/test/unit/store/scopeEncoding.test.d.ts.map +1 -0
- package/lib/test/unit/store/scopeEncoding.test.js +26 -0
- package/lib/test/unit/store/scopeEncoding.test.js.map +1 -0
- package/lib/test/unit/types/identityCodec.test.d.ts +2 -0
- package/lib/test/unit/types/identityCodec.test.d.ts.map +1 -0
- package/lib/test/unit/types/identityCodec.test.js +189 -0
- package/lib/test/unit/types/identityCodec.test.js.map +1 -0
- package/lib/test/unit/types/ids.test.d.ts +2 -0
- package/lib/test/unit/types/ids.test.d.ts.map +1 -0
- package/lib/test/unit/types/ids.test.js +86 -0
- package/lib/test/unit/types/ids.test.js.map +1 -0
- package/lib/test/unit/types/writePolicy.test.d.ts +2 -0
- package/lib/test/unit/types/writePolicy.test.d.ts.map +1 -0
- package/lib/test/unit/types/writePolicy.test.js +243 -0
- package/lib/test/unit/types/writePolicy.test.js.map +1 -0
- package/lib/test/unit/vector/inMemoryCosineIndex.test.d.ts +2 -0
- package/lib/test/unit/vector/inMemoryCosineIndex.test.d.ts.map +1 -0
- package/lib/test/unit/vector/inMemoryCosineIndex.test.js +194 -0
- package/lib/test/unit/vector/inMemoryCosineIndex.test.js.map +1 -0
- package/lib/test/unit/vector/vectorIndex.test.d.ts +2 -0
- package/lib/test/unit/vector/vectorIndex.test.d.ts.map +1 -0
- package/lib/test/unit/vector/vectorIndex.test.js +44 -0
- package/lib/test/unit/vector/vectorIndex.test.js.map +1 -0
- package/package.json +81 -0
- package/rush-logs/ts-agent-memory.build.cache.log +3 -0
- package/rush-logs/ts-agent-memory.build.log +9 -0
- package/src/index.ts +12 -0
- package/src/packlets/converters/bodyConverterRegistry.ts +105 -0
- package/src/packlets/converters/envelopeConverter.ts +210 -0
- package/src/packlets/converters/index.ts +7 -0
- package/src/packlets/index/index.ts +6 -0
- package/src/packlets/index/memoryIndex.ts +268 -0
- package/src/packlets/observe/index.ts +7 -0
- package/src/packlets/observe/memoryObservationStore.ts +153 -0
- package/src/packlets/observe/observer.ts +119 -0
- package/src/packlets/retrieve/hybridRetriever.ts +181 -0
- package/src/packlets/retrieve/index.ts +12 -0
- package/src/packlets/retrieve/linkTraversalRetriever.ts +169 -0
- package/src/packlets/retrieve/recencyRetriever.ts +54 -0
- package/src/packlets/retrieve/retriever.ts +207 -0
- package/src/packlets/retrieve/semanticRetriever.ts +147 -0
- package/src/packlets/retrieve/structuredFilterRetriever.ts +58 -0
- package/src/packlets/retrieve/tagRetriever.ts +58 -0
- package/src/packlets/store/fileTreeMemoryStore.ts +1073 -0
- package/src/packlets/store/index.ts +7 -0
- package/src/packlets/store/scopeEncoding.ts +36 -0
- package/src/packlets/types/envelope.ts +138 -0
- package/src/packlets/types/filenameSafety.ts +57 -0
- package/src/packlets/types/identityCodec.ts +263 -0
- package/src/packlets/types/ids.ts +124 -0
- package/src/packlets/types/index.ts +10 -0
- package/src/packlets/types/writePolicy.ts +447 -0
- package/src/packlets/vector/inMemoryCosineIndex.ts +173 -0
- package/src/packlets/vector/index.ts +7 -0
- package/src/packlets/vector/vectorIndex.ts +78 -0
- package/src/test/unit/converters/bodyConverterRegistry.test.ts +89 -0
- package/src/test/unit/converters/envelopeConverter.test.ts +261 -0
- package/src/test/unit/index/memoryIndex.test.ts +187 -0
- package/src/test/unit/observe/memoryObservationStore.test.ts +158 -0
- package/src/test/unit/retrieve/linkTraversalRetriever.test.ts +230 -0
- package/src/test/unit/retrieve/retrievers.test.ts +662 -0
- package/src/test/unit/store/embedOnWrite.test.ts +346 -0
- package/src/test/unit/store/fileTreeMemoryStore.test.ts +875 -0
- package/src/test/unit/store/observations.test.ts +290 -0
- package/src/test/unit/store/scopeEncoding.test.ts +37 -0
- package/src/test/unit/types/identityCodec.test.ts +266 -0
- package/src/test/unit/types/ids.test.ts +94 -0
- package/src/test/unit/types/writePolicy.test.ts +325 -0
- package/src/test/unit/vector/inMemoryCosineIndex.test.ts +242 -0
- package/src/test/unit/vector/vectorIndex.test.ts +48 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +182 -0
- package/temp/build/typescript/ts_8nwakTlr.json +1 -0
- package/temp/ts-agent-memory.api.json +12438 -0
- package/temp/ts-agent-memory.api.md +525 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
import '@fgv/ts-utils-jest';
|
|
6
|
+
import { Converters, Logging, fail, succeed } from '@fgv/ts-utils';
|
|
7
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
8
|
+
import { BodyConverterRegistry, Convert, FileTreeMemoryStore, KnowledgeIdentityCodec, LtmIdentityCodec, MemoryCapCullPolicy, MtmIdentityCodec, envelopeConverter, joinFrontmatter } from '../../../index';
|
|
9
|
+
const knowledgeKind = 'knowledge';
|
|
10
|
+
/** Build a record whose envelope fields are validated through the B0 converter. */
|
|
11
|
+
function makeRecord(spec) {
|
|
12
|
+
var _a, _b, _c, _d, _e;
|
|
13
|
+
return {
|
|
14
|
+
envelope: envelopeConverter
|
|
15
|
+
.convert({
|
|
16
|
+
id: spec.id,
|
|
17
|
+
entityId: (_a = spec.entityId) !== null && _a !== void 0 ? _a : spec.id,
|
|
18
|
+
kind: (_b = spec.kind) !== null && _b !== void 0 ? _b : 'knowledge',
|
|
19
|
+
tags: (_c = spec.tags) !== null && _c !== void 0 ? _c : [],
|
|
20
|
+
links: (_d = spec.links) !== null && _d !== void 0 ? _d : [],
|
|
21
|
+
created: 0,
|
|
22
|
+
updated: 0,
|
|
23
|
+
seq: 0,
|
|
24
|
+
contentHash: '',
|
|
25
|
+
provenance: { source: 'agent' }
|
|
26
|
+
})
|
|
27
|
+
.orThrow(),
|
|
28
|
+
body: (_e = spec.body) !== null && _e !== void 0 ? _e : `body for ${spec.id}`
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** A mutable in-memory root, optionally seeded with raw files. */
|
|
32
|
+
function mutableRoot(files = []) {
|
|
33
|
+
const tree = FileTree.inMemory([...files], { mutable: true }).orThrow();
|
|
34
|
+
const root = tree.getDirectory('/').orThrow();
|
|
35
|
+
if (!FileTree.isMutableDirectoryItem(root)) {
|
|
36
|
+
throw new Error('expected a mutable root directory');
|
|
37
|
+
}
|
|
38
|
+
return root;
|
|
39
|
+
}
|
|
40
|
+
function knowledgeRegistry(bodyConverter = Converters.string) {
|
|
41
|
+
const registry = BodyConverterRegistry.create().orThrow();
|
|
42
|
+
registry.register(knowledgeKind, bodyConverter);
|
|
43
|
+
return registry;
|
|
44
|
+
}
|
|
45
|
+
const knowledgeCodecs = new Map([
|
|
46
|
+
[knowledgeKind, new KnowledgeIdentityCodec()]
|
|
47
|
+
]);
|
|
48
|
+
/** A serialized knowledge file for seeding a vault. */
|
|
49
|
+
function knowledgeFile(id, body, overrides = {}) {
|
|
50
|
+
const envelope = Object.assign({ id, entityId: id, kind: 'knowledge', tags: [], links: [], created: 1, updated: 1, seq: 5, contentHash: 'seed', provenance: { source: 'agent' } }, overrides);
|
|
51
|
+
const frontmatter = Object.entries(envelope)
|
|
52
|
+
.map(([k, v]) => `${k}: ${JSON.stringify(v)}`)
|
|
53
|
+
.join('\n');
|
|
54
|
+
return joinFrontmatter(frontmatter, body);
|
|
55
|
+
}
|
|
56
|
+
describe('FileTreeMemoryStore', () => {
|
|
57
|
+
let clockValue;
|
|
58
|
+
const clock = () => clockValue;
|
|
59
|
+
function createStore(params = {}) {
|
|
60
|
+
var _a, _b, _c;
|
|
61
|
+
return FileTreeMemoryStore.create({
|
|
62
|
+
root: (_a = params.root) !== null && _a !== void 0 ? _a : mutableRoot(),
|
|
63
|
+
registry: (_b = params.registry) !== null && _b !== void 0 ? _b : knowledgeRegistry(),
|
|
64
|
+
codecs: (_c = params.codecs) !== null && _c !== void 0 ? _c : knowledgeCodecs,
|
|
65
|
+
defaultCodec: params.defaultCodec,
|
|
66
|
+
writePolicies: params.writePolicies,
|
|
67
|
+
clock
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
clockValue = 1000;
|
|
72
|
+
});
|
|
73
|
+
describe('create', () => {
|
|
74
|
+
test('starts from an empty vault', async () => {
|
|
75
|
+
const store = createStore().orThrow();
|
|
76
|
+
expect(await store.list()).toSucceedWith([]);
|
|
77
|
+
});
|
|
78
|
+
test('indexes a pre-existing vault and resumes seq past the highest persisted seq', async () => {
|
|
79
|
+
const root = mutableRoot([
|
|
80
|
+
{ path: 'knowledge/doc-a.md', contents: knowledgeFile('doc-a', 'A', { seq: 7 }) },
|
|
81
|
+
{ path: 'knowledge/doc-b.md', contents: knowledgeFile('doc-b', 'B', { seq: 3 }) },
|
|
82
|
+
{ path: 'knowledge/notes.txt', contents: 'not a record' },
|
|
83
|
+
{ path: 'top-level.md', contents: 'ignored at root' }
|
|
84
|
+
]);
|
|
85
|
+
const store = createStore({ root }).orThrow();
|
|
86
|
+
expect(await store.list()).toSucceedAndSatisfy((listed) => {
|
|
87
|
+
expect(listed.map((r) => r.envelope.id).sort()).toEqual(['doc-a', 'doc-b']);
|
|
88
|
+
});
|
|
89
|
+
// The next put must get seq 8 (highest persisted was 7).
|
|
90
|
+
expect(await store.put(makeRecord({ id: 'doc-c' }))).toSucceedAndSatisfy((put) => {
|
|
91
|
+
expect(put.envelope.seq).toBe(8);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
test('fails loudly when a seeded file id does not match its filename', () => {
|
|
95
|
+
const root = mutableRoot([{ path: 'knowledge/real.md', contents: knowledgeFile('wrong', 'body') }]);
|
|
96
|
+
expect(createStore({ root })).toFailWith(/does not match filename stem/i);
|
|
97
|
+
});
|
|
98
|
+
test('fails loudly when a seeded file violates the codec round-trip (wrong scope)', () => {
|
|
99
|
+
const root = mutableRoot([{ path: 'elsewhere/doc.md', contents: knowledgeFile('doc', 'body') }]);
|
|
100
|
+
expect(createStore({ root })).toFailWith(/does not match expected scope/i);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('factory defaults', () => {
|
|
104
|
+
test('falls back to default codecs/policies/scope-encoding/clock when omitted', async () => {
|
|
105
|
+
const store = FileTreeMemoryStore.create({
|
|
106
|
+
root: mutableRoot(),
|
|
107
|
+
registry: knowledgeRegistry(),
|
|
108
|
+
defaultCodec: new KnowledgeIdentityCodec()
|
|
109
|
+
}).orThrow();
|
|
110
|
+
expect(await store.put(makeRecord({ id: 'x', body: 'y' }))).toSucceedAndSatisfy((record) => {
|
|
111
|
+
expect(record.envelope.id).toBe('x');
|
|
112
|
+
// The default clock (Date.now) stamps a real epoch.
|
|
113
|
+
expect(record.envelope.created).toBeGreaterThan(0);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
test('uses a custom scope encoding when provided', async () => {
|
|
117
|
+
const store = FileTreeMemoryStore.create({
|
|
118
|
+
root: mutableRoot(),
|
|
119
|
+
registry: knowledgeRegistry(),
|
|
120
|
+
codecs: knowledgeCodecs,
|
|
121
|
+
scopeEncoding: (scope) => succeed(scope),
|
|
122
|
+
clock
|
|
123
|
+
}).orThrow();
|
|
124
|
+
await store.put(makeRecord({ id: 'x', body: 'y' }));
|
|
125
|
+
expect(await store.get(knowledgeKind, 'x')).toSucceedAndSatisfy((record) => {
|
|
126
|
+
expect(record === null || record === void 0 ? void 0 : record.body).toBe('y');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe('put / get round-trip', () => {
|
|
131
|
+
test('first write stamps created/updated/seq/contentHash and is retrievable', async () => {
|
|
132
|
+
const store = createStore().orThrow();
|
|
133
|
+
expect(await store.put(makeRecord({ id: 'intro', body: 'hello' }))).toSucceedAndSatisfy((put) => {
|
|
134
|
+
expect(put.envelope.created).toBe(1000);
|
|
135
|
+
expect(put.envelope.updated).toBe(1000);
|
|
136
|
+
expect(put.envelope.seq).toBe(1);
|
|
137
|
+
expect(put.envelope.contentHash).not.toBe('');
|
|
138
|
+
});
|
|
139
|
+
expect(await store.get(knowledgeKind, 'intro')).toSucceedAndSatisfy((record) => {
|
|
140
|
+
expect(record === null || record === void 0 ? void 0 : record.body).toBe('hello');
|
|
141
|
+
expect(record === null || record === void 0 ? void 0 : record.envelope.id).toBe('intro');
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
test('get returns undefined for a missing entity', async () => {
|
|
145
|
+
const store = createStore().orThrow();
|
|
146
|
+
expect(await store.get(knowledgeKind, 'absent')).toSucceedWith(undefined);
|
|
147
|
+
});
|
|
148
|
+
test('getById reads a record directly by scope and id', async () => {
|
|
149
|
+
const store = createStore().orThrow();
|
|
150
|
+
await store.put(makeRecord({ id: 'direct', body: 'x' }));
|
|
151
|
+
expect(await store.getById('knowledge', 'direct')).toSucceedAndSatisfy((record) => {
|
|
152
|
+
expect(record === null || record === void 0 ? void 0 : record.body).toBe('x');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
test('rejects a non-string body', async () => {
|
|
156
|
+
const store = createStore().orThrow();
|
|
157
|
+
expect(await store.put(makeRecord({ id: 'obj', body: { a: 1 } }))).toFailWith(/only string .* bodies are supported/i);
|
|
158
|
+
});
|
|
159
|
+
test('rejects a body the registered converter refuses', async () => {
|
|
160
|
+
const strict = Converters.string.map((s) => s === 'ok' ? succeed(s) : fail('must be ok'));
|
|
161
|
+
const store = createStore({ registry: knowledgeRegistry(strict) }).orThrow();
|
|
162
|
+
expect(await store.put(makeRecord({ id: 'bad', body: 'nope' }))).toFailWith(/invalid body.*must be ok/i);
|
|
163
|
+
});
|
|
164
|
+
test('fails when no codec is registered for the kind', async () => {
|
|
165
|
+
const registry = BodyConverterRegistry.create().orThrow();
|
|
166
|
+
registry.register('other', Converters.string);
|
|
167
|
+
const store = createStore({ registry }).orThrow();
|
|
168
|
+
expect(await store.put(makeRecord({ id: 'x', kind: 'other' }))).toFailWith(/no identity codec registered/i);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
describe('content-hash dedup', () => {
|
|
172
|
+
test('an identical re-put is a no-op that returns the existing record', async () => {
|
|
173
|
+
const store = createStore().orThrow();
|
|
174
|
+
let firstSeq = -1;
|
|
175
|
+
let firstUpdated = -1;
|
|
176
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'same' }))).toSucceedAndSatisfy((first) => {
|
|
177
|
+
firstSeq = first.envelope.seq;
|
|
178
|
+
firstUpdated = first.envelope.updated;
|
|
179
|
+
});
|
|
180
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'same' }))).toSucceedAndSatisfy((second) => {
|
|
181
|
+
expect(second.envelope.seq).toBe(firstSeq);
|
|
182
|
+
expect(second.envelope.updated).toBe(firstUpdated);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
test('dedup is scope-wide: a different entity with identical content collapses', async () => {
|
|
186
|
+
const store = createStore().orThrow();
|
|
187
|
+
let firstId = '';
|
|
188
|
+
expect(await store.put(makeRecord({ id: 'doc-a', body: 'identical' }))).toSucceedAndSatisfy((first) => {
|
|
189
|
+
firstId = first.envelope.id;
|
|
190
|
+
});
|
|
191
|
+
expect(await store.put(makeRecord({ id: 'doc-b', body: 'identical' }))).toSucceedAndSatisfy((second) => {
|
|
192
|
+
expect(second.envelope.id).toBe(firstId);
|
|
193
|
+
});
|
|
194
|
+
// doc-b was never written.
|
|
195
|
+
expect(await store.get(knowledgeKind, 'doc-b')).toSucceedWith(undefined);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
describe('last-write-wins update', () => {
|
|
199
|
+
test('updates mutable fields via applyUpdate, preserving created and bumping seq/updated', async () => {
|
|
200
|
+
const store = createStore().orThrow();
|
|
201
|
+
let firstCreated = -1;
|
|
202
|
+
let firstSeq = -1;
|
|
203
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'v1', tags: ['a'] }))).toSucceedAndSatisfy((first) => {
|
|
204
|
+
firstCreated = first.envelope.created;
|
|
205
|
+
firstSeq = first.envelope.seq;
|
|
206
|
+
});
|
|
207
|
+
clockValue = 2000;
|
|
208
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'v2', tags: ['b', 'c'] }))).toSucceedAndSatisfy((second) => {
|
|
209
|
+
expect(second.envelope.created).toBe(firstCreated);
|
|
210
|
+
expect(second.envelope.updated).toBe(2000);
|
|
211
|
+
expect(second.envelope.seq).toBe(firstSeq + 1);
|
|
212
|
+
expect(second.body).toBe('v2');
|
|
213
|
+
expect(second.envelope.tags).toEqual(['b', 'c']);
|
|
214
|
+
});
|
|
215
|
+
expect(await store.list({ tag: 'a' })).toSucceedWith([]);
|
|
216
|
+
});
|
|
217
|
+
test('surfaces a policy applyUpdate failure with context', async () => {
|
|
218
|
+
const failingPolicy = {
|
|
219
|
+
mutableFields: ['body'],
|
|
220
|
+
admit: () => succeed({ decision: 'accept' }),
|
|
221
|
+
applyUpdate: () => fail('patch blew up')
|
|
222
|
+
};
|
|
223
|
+
const store = createStore({
|
|
224
|
+
writePolicies: new Map([[knowledgeKind, failingPolicy]])
|
|
225
|
+
}).orThrow();
|
|
226
|
+
await store.put(makeRecord({ id: 'doc', body: 'v1' }));
|
|
227
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'v2' }))).toFailWith(/update failed: patch blew up/i);
|
|
228
|
+
});
|
|
229
|
+
test('persists the policy-updated body and fails loudly if it is not a string', async () => {
|
|
230
|
+
// A policy whose applyUpdate swaps in a non-string body must be rejected at
|
|
231
|
+
// serialization time, not silently persist the incoming string.
|
|
232
|
+
const nonStringBodyPolicy = {
|
|
233
|
+
mutableFields: ['body'],
|
|
234
|
+
admit: () => succeed({ decision: 'accept' }),
|
|
235
|
+
applyUpdate: (existing) => succeed({ envelope: existing.envelope, body: { not: 'a string' } })
|
|
236
|
+
};
|
|
237
|
+
const store = createStore({
|
|
238
|
+
writePolicies: new Map([[knowledgeKind, nonStringBodyPolicy]])
|
|
239
|
+
}).orThrow();
|
|
240
|
+
await store.put(makeRecord({ id: 'doc', body: 'v1' }));
|
|
241
|
+
expect(await store.put(makeRecord({ id: 'doc', body: 'v2' }))).toFailWith(/policy returned a non-string body/i);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
describe('admission policy', () => {
|
|
245
|
+
test("a 'reject' decision fails the put with the reason", async () => {
|
|
246
|
+
const rejectPolicy = {
|
|
247
|
+
mutableFields: [],
|
|
248
|
+
admit: () => succeed({ decision: 'reject', reason: 'over quota' }),
|
|
249
|
+
applyUpdate: (r) => succeed(r)
|
|
250
|
+
};
|
|
251
|
+
const store = createStore({
|
|
252
|
+
writePolicies: new Map([[knowledgeKind, rejectPolicy]])
|
|
253
|
+
}).orThrow();
|
|
254
|
+
expect(await store.put(makeRecord({ id: 'doc' }))).toFailWith(/rejected by policy: over quota/i);
|
|
255
|
+
});
|
|
256
|
+
test("a 'cull-oldest' decision evicts the named records before writing", async () => {
|
|
257
|
+
let putCount = 0;
|
|
258
|
+
const cullPolicy = {
|
|
259
|
+
mutableFields: ['body'],
|
|
260
|
+
admit: () => {
|
|
261
|
+
putCount += 1;
|
|
262
|
+
// First put admits; the second evicts the first.
|
|
263
|
+
return putCount === 1
|
|
264
|
+
? succeed({ decision: 'accept' })
|
|
265
|
+
: succeed({ decision: 'cull-oldest', evict: ['old'] });
|
|
266
|
+
},
|
|
267
|
+
applyUpdate: (r) => succeed(r)
|
|
268
|
+
};
|
|
269
|
+
const store = createStore({
|
|
270
|
+
writePolicies: new Map([[knowledgeKind, cullPolicy]])
|
|
271
|
+
}).orThrow();
|
|
272
|
+
await store.put(makeRecord({ id: 'old', body: 'old' }));
|
|
273
|
+
expect(await store.put(makeRecord({ id: 'new', body: 'new' }))).toSucceedAndSatisfy((put) => {
|
|
274
|
+
expect(put.envelope.id).toBe('new');
|
|
275
|
+
});
|
|
276
|
+
expect(await store.get(knowledgeKind, 'old')).toSucceedWith(undefined);
|
|
277
|
+
});
|
|
278
|
+
test("a 'cull-oldest' eviction of a missing record is best-effort: the committed put still succeeds and the failure is logged", async () => {
|
|
279
|
+
const cullPolicy = {
|
|
280
|
+
mutableFields: ['body'],
|
|
281
|
+
admit: () => succeed({ decision: 'cull-oldest', evict: ['ghost'] }),
|
|
282
|
+
applyUpdate: (r) => succeed(r)
|
|
283
|
+
};
|
|
284
|
+
const logger = new Logging.InMemoryLogger();
|
|
285
|
+
const store = FileTreeMemoryStore.create({
|
|
286
|
+
root: mutableRoot(),
|
|
287
|
+
registry: knowledgeRegistry(),
|
|
288
|
+
codecs: knowledgeCodecs,
|
|
289
|
+
writePolicies: new Map([[knowledgeKind, cullPolicy]]),
|
|
290
|
+
clock,
|
|
291
|
+
logger
|
|
292
|
+
}).orThrow();
|
|
293
|
+
// The new record is the authoritative commit; a failed best-effort eviction
|
|
294
|
+
// must NOT fail the put — it is logged and the cap self-heals next write.
|
|
295
|
+
expect(await store.put(makeRecord({ id: 'new', body: 'new' }))).toSucceedAndSatisfy((put) => {
|
|
296
|
+
expect(put.envelope.id).toBe('new');
|
|
297
|
+
expect(put.envelope.embeddingRef).toBeUndefined();
|
|
298
|
+
});
|
|
299
|
+
expect(await store.get(knowledgeKind, 'new')).toSucceedAndSatisfy((record) => {
|
|
300
|
+
expect(record === null || record === void 0 ? void 0 : record.envelope.id).toBe('new');
|
|
301
|
+
});
|
|
302
|
+
expect(logger.logged.some((m) => /best-effort eviction of 'ghost' failed.*not found/i.test(m))).toBe(true);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
describe('list', () => {
|
|
306
|
+
test('filters by scope, kind, and tag', async () => {
|
|
307
|
+
const store = createStore().orThrow();
|
|
308
|
+
await store.put(makeRecord({ id: 'a', body: 'a', tags: ['t1'] }));
|
|
309
|
+
await store.put(makeRecord({ id: 'b', body: 'b', tags: ['t2'] }));
|
|
310
|
+
expect(await store.list()).toSucceedAndSatisfy((r) => expect(r).toHaveLength(2));
|
|
311
|
+
expect(await store.list({ scope: 'knowledge' })).toSucceedAndSatisfy((r) => expect(r).toHaveLength(2));
|
|
312
|
+
expect(await store.list({ scope: 'other' })).toSucceedAndSatisfy((r) => expect(r).toHaveLength(0));
|
|
313
|
+
expect(await store.list({ kind: knowledgeKind })).toSucceedAndSatisfy((r) => expect(r).toHaveLength(2));
|
|
314
|
+
expect(await store.list({ kind: 'missing' })).toSucceedAndSatisfy((r) => expect(r).toHaveLength(0));
|
|
315
|
+
expect(await store.list({ tag: 't1' })).toSucceedAndSatisfy((byTag) => {
|
|
316
|
+
expect(byTag).toHaveLength(1);
|
|
317
|
+
expect(byTag[0].envelope.id).toBe('a');
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
describe('delete', () => {
|
|
322
|
+
test('removes a record and returns its id', async () => {
|
|
323
|
+
const store = createStore().orThrow();
|
|
324
|
+
await store.put(makeRecord({ id: 'doomed', body: 'x' }));
|
|
325
|
+
expect(await store.delete(knowledgeKind, 'doomed')).toSucceedWith('doomed');
|
|
326
|
+
expect(await store.get(knowledgeKind, 'doomed')).toSucceedWith(undefined);
|
|
327
|
+
expect(await store.list()).toSucceedAndSatisfy((r) => expect(r).toHaveLength(0));
|
|
328
|
+
});
|
|
329
|
+
test('fails when the record does not exist', async () => {
|
|
330
|
+
const store = createStore().orThrow();
|
|
331
|
+
expect(await store.delete(knowledgeKind, 'nope')).toFailWith(/no record found/i);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
describe('write-lock serialization', () => {
|
|
335
|
+
test('concurrent puts all land with monotonic seq', async () => {
|
|
336
|
+
const store = createStore().orThrow();
|
|
337
|
+
const results = await Promise.all([
|
|
338
|
+
store.put(makeRecord({ id: 'p1', body: '1' })),
|
|
339
|
+
store.put(makeRecord({ id: 'p2', body: '2' })),
|
|
340
|
+
store.put(makeRecord({ id: 'p3', body: '3' }))
|
|
341
|
+
]);
|
|
342
|
+
const seqs = results.map((r) => {
|
|
343
|
+
let seq = -1;
|
|
344
|
+
expect(r).toSucceedAndSatisfy((rec) => {
|
|
345
|
+
seq = rec.envelope.seq;
|
|
346
|
+
});
|
|
347
|
+
return seq;
|
|
348
|
+
});
|
|
349
|
+
expect([...seqs].sort((a, b) => a - b)).toEqual([1, 2, 3]);
|
|
350
|
+
expect(await store.list()).toSucceedAndSatisfy((r) => expect(r).toHaveLength(3));
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
describe('multi-segment scope codec', () => {
|
|
354
|
+
const turnKind = 'turn';
|
|
355
|
+
/** Maps `<conv>:<turn>` ⇄ scope `conversations/<conv>`, stem `turn-<n>`. */
|
|
356
|
+
const turnCodec = {
|
|
357
|
+
encode: (entityId) => {
|
|
358
|
+
const [conv, turn] = entityId.split(':');
|
|
359
|
+
return succeed({
|
|
360
|
+
scope: `conversations/${conv}`,
|
|
361
|
+
idStem: `turn-${turn}`,
|
|
362
|
+
isVersioned: false
|
|
363
|
+
});
|
|
364
|
+
},
|
|
365
|
+
decode: (scope, stem) => {
|
|
366
|
+
const conv = scope.split('/')[1];
|
|
367
|
+
const turn = stem.replace('turn-', '');
|
|
368
|
+
return Convert.entityId.convert(`${conv}:${turn}`);
|
|
369
|
+
},
|
|
370
|
+
verifyRoundTrip: () => succeed(true)
|
|
371
|
+
};
|
|
372
|
+
function turnStore() {
|
|
373
|
+
const registry = BodyConverterRegistry.create().orThrow();
|
|
374
|
+
registry.register(turnKind, Converters.string);
|
|
375
|
+
return FileTreeMemoryStore.create({
|
|
376
|
+
root: mutableRoot(),
|
|
377
|
+
registry,
|
|
378
|
+
codecs: new Map([[turnKind, turnCodec]]),
|
|
379
|
+
clock
|
|
380
|
+
}).orThrow();
|
|
381
|
+
}
|
|
382
|
+
test('writes and reads through a nested scope directory', async () => {
|
|
383
|
+
const store = turnStore();
|
|
384
|
+
await store.put(makeRecord({ id: 'turn-0', entityId: 'c1:0', kind: 'turn', body: 'first turn' }));
|
|
385
|
+
expect(await store.get(turnKind, 'c1:0')).toSucceedAndSatisfy((record) => {
|
|
386
|
+
expect(record === null || record === void 0 ? void 0 : record.body).toBe('first turn');
|
|
387
|
+
expect(record === null || record === void 0 ? void 0 : record.envelope.id).toBe('turn-0');
|
|
388
|
+
});
|
|
389
|
+
expect((await store.list({ scope: 'conversations/c1' })).orThrow()).toHaveLength(1);
|
|
390
|
+
});
|
|
391
|
+
test('fails when the envelope id does not match the codec-derived stem', async () => {
|
|
392
|
+
const store = turnStore();
|
|
393
|
+
// id 'turn-9' but codec maps c1:0 → stem 'turn-0'.
|
|
394
|
+
expect(await store.put(makeRecord({ id: 'turn-9', entityId: 'c1:0', kind: 'turn', body: 'x' }))).toFailWith(/does not match codec-derived stem/i);
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
describe('versioned codec (loud degradation)', () => {
|
|
398
|
+
const versionedKind = 'versioned';
|
|
399
|
+
const versionedCodec = {
|
|
400
|
+
encode: (entityId) => succeed({ scope: 'versioned', idStem: entityId, isVersioned: true }),
|
|
401
|
+
decode: (__scope, stem) => Convert.entityId.convert(stem),
|
|
402
|
+
verifyRoundTrip: () => succeed(true)
|
|
403
|
+
};
|
|
404
|
+
function versionedStore() {
|
|
405
|
+
const registry = BodyConverterRegistry.create().orThrow();
|
|
406
|
+
registry.register(versionedKind, Converters.string);
|
|
407
|
+
return FileTreeMemoryStore.create({
|
|
408
|
+
root: mutableRoot(),
|
|
409
|
+
registry,
|
|
410
|
+
codecs: new Map([[versionedKind, versionedCodec]]),
|
|
411
|
+
clock
|
|
412
|
+
}).orThrow();
|
|
413
|
+
}
|
|
414
|
+
test('put fails loudly on a versioned layout', async () => {
|
|
415
|
+
expect(await versionedStore().put(makeRecord({ id: 'v', kind: 'versioned', body: 'x' }))).toFailWith(/versioned\/temporal layout not yet supported/i);
|
|
416
|
+
});
|
|
417
|
+
test('get fails loudly on a versioned layout', async () => {
|
|
418
|
+
expect(await versionedStore().get(versionedKind, 'v')).toFailWith(/versioned\/temporal layout not yet supported/i);
|
|
419
|
+
});
|
|
420
|
+
test('delete fails loudly on a versioned layout', async () => {
|
|
421
|
+
expect(await versionedStore().delete(versionedKind, 'v')).toFailWith(/versioned\/temporal layout not yet supported/i);
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
describe('default codec', () => {
|
|
425
|
+
test('a kind without an explicit codec entry uses the default codec', async () => {
|
|
426
|
+
const store = FileTreeMemoryStore.create({
|
|
427
|
+
root: mutableRoot(),
|
|
428
|
+
registry: knowledgeRegistry(),
|
|
429
|
+
codecs: new Map(),
|
|
430
|
+
defaultCodec: new KnowledgeIdentityCodec(),
|
|
431
|
+
clock
|
|
432
|
+
}).orThrow();
|
|
433
|
+
const put = (await store.put(makeRecord({ id: 'fallback', body: 'x' }))).orThrow();
|
|
434
|
+
expect(put.envelope.id).toBe('fallback');
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
describe('experience (memory) kinds — Phase C', () => {
|
|
438
|
+
const ltmKind = 'ltm';
|
|
439
|
+
const mtmKind = 'mtm';
|
|
440
|
+
/** A registry that gates the knowledge + LTM + MTM kind bodies (all strings here). */
|
|
441
|
+
function multiKindRegistry() {
|
|
442
|
+
const registry = BodyConverterRegistry.create().orThrow();
|
|
443
|
+
registry.register(knowledgeKind, Converters.string);
|
|
444
|
+
registry.register(ltmKind, Converters.string);
|
|
445
|
+
registry.register(mtmKind, Converters.string);
|
|
446
|
+
return registry;
|
|
447
|
+
}
|
|
448
|
+
const multiKindCodecs = new Map([
|
|
449
|
+
[knowledgeKind, new KnowledgeIdentityCodec()],
|
|
450
|
+
[ltmKind, new LtmIdentityCodec()],
|
|
451
|
+
[mtmKind, new MtmIdentityCodec()]
|
|
452
|
+
]);
|
|
453
|
+
/** Cap-cull policy for the LTM/MTM kinds (entity-scoped dedup). */
|
|
454
|
+
function memoryPolicies(maxRecords = 5) {
|
|
455
|
+
const capCull = MemoryCapCullPolicy.create({
|
|
456
|
+
maxRecords,
|
|
457
|
+
mutableFields: ['body', 'tags', 'links', 'provenance', 'embeddingRef']
|
|
458
|
+
}).orThrow();
|
|
459
|
+
return new Map([
|
|
460
|
+
[ltmKind, capCull],
|
|
461
|
+
[mtmKind, capCull]
|
|
462
|
+
]);
|
|
463
|
+
}
|
|
464
|
+
function memoryStore(root, maxRecords = 5) {
|
|
465
|
+
return FileTreeMemoryStore.create({
|
|
466
|
+
root: root !== null && root !== void 0 ? root : mutableRoot(),
|
|
467
|
+
registry: multiKindRegistry(),
|
|
468
|
+
codecs: multiKindCodecs,
|
|
469
|
+
writePolicies: memoryPolicies(maxRecords),
|
|
470
|
+
clock
|
|
471
|
+
}).orThrow();
|
|
472
|
+
}
|
|
473
|
+
test('registers all three kind families from one FileTree vault', async () => {
|
|
474
|
+
const store = memoryStore();
|
|
475
|
+
(await store.put(makeRecord({ id: 'intro', kind: 'knowledge', body: 'k' }))).orThrow();
|
|
476
|
+
(await store.put(makeRecord({ id: 'conv-1', entityId: 'conv-1', kind: 'ltm', body: 'l' }))).orThrow();
|
|
477
|
+
(await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'm' }))).orThrow();
|
|
478
|
+
expect(await store.get(knowledgeKind, 'intro')).toSucceedAndSatisfy((r) => {
|
|
479
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.kind).toBe('knowledge');
|
|
480
|
+
});
|
|
481
|
+
expect(await store.get(ltmKind, 'conv-1')).toSucceedAndSatisfy((r) => {
|
|
482
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.kind).toBe('ltm');
|
|
483
|
+
});
|
|
484
|
+
expect(await store.get(mtmKind, 'conv-1:5')).toSucceedAndSatisfy((r) => {
|
|
485
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-5');
|
|
486
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.entityId).toBe('conv-1:5');
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
test('MTM composite key persists at conversations/<id>/turn-<n> and round-trips a reload', async () => {
|
|
490
|
+
const root = mutableRoot();
|
|
491
|
+
const store = memoryStore(root);
|
|
492
|
+
(await store.put(makeRecord({ id: 'turn-0', entityId: 'conv-x:0', kind: 'mtm', body: 'm0' }))).orThrow();
|
|
493
|
+
// A fresh store over the same root must re-index the MTM record (verifyRoundTrip
|
|
494
|
+
// exercises the multi-segment scope on load).
|
|
495
|
+
const reopened = memoryStore(root);
|
|
496
|
+
expect(await reopened.get(mtmKind, 'conv-x:0')).toSucceedAndSatisfy((r) => {
|
|
497
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-0');
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
test('entity-scoped dedup: two distinct entities with identical content both persist', async () => {
|
|
501
|
+
// Regression for the flagged collapse bug: turn-5 and turn-9 share the same
|
|
502
|
+
// { kind, body, links } but are distinct entities and must NOT collapse.
|
|
503
|
+
const store = memoryStore();
|
|
504
|
+
const five = (await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'identical' }))).orThrow();
|
|
505
|
+
const nine = (await store.put(makeRecord({ id: 'turn-9', entityId: 'conv-1:9', kind: 'mtm', body: 'identical' }))).orThrow();
|
|
506
|
+
expect(five.envelope.id).toBe('turn-5');
|
|
507
|
+
expect(nine.envelope.id).toBe('turn-9');
|
|
508
|
+
expect(nine.envelope.id).not.toBe(five.envelope.id);
|
|
509
|
+
// Both are independently retrievable — neither collapsed into the other.
|
|
510
|
+
expect(await store.get(mtmKind, 'conv-1:5')).toSucceedAndSatisfy((r) => {
|
|
511
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-5');
|
|
512
|
+
});
|
|
513
|
+
expect(await store.get(mtmKind, 'conv-1:9')).toSucceedAndSatisfy((r) => {
|
|
514
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-9');
|
|
515
|
+
});
|
|
516
|
+
expect(await store.list({ kind: mtmKind })).toSucceedAndSatisfy((records) => {
|
|
517
|
+
expect(records).toHaveLength(2);
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
test('entity-scoped dedup: an identical re-put of the SAME entity is a no-op', async () => {
|
|
521
|
+
const store = memoryStore();
|
|
522
|
+
let firstSeq = -1;
|
|
523
|
+
expect(await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'same' }))).toSucceedAndSatisfy((r) => {
|
|
524
|
+
firstSeq = r.envelope.seq;
|
|
525
|
+
});
|
|
526
|
+
expect(await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'same' }))).toSucceedAndSatisfy((r) => {
|
|
527
|
+
expect(r.envelope.seq).toBe(firstSeq);
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
test('merge-patch updates the body of an existing memory entity (distinct content)', async () => {
|
|
531
|
+
const store = memoryStore();
|
|
532
|
+
let firstCreated = -1;
|
|
533
|
+
let firstSeq = -1;
|
|
534
|
+
expect(await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'v1' }))).toSucceedAndSatisfy((r) => {
|
|
535
|
+
firstCreated = r.envelope.created;
|
|
536
|
+
firstSeq = r.envelope.seq;
|
|
537
|
+
});
|
|
538
|
+
clockValue = 2000;
|
|
539
|
+
expect(await store.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'v2' }))).toSucceedAndSatisfy((r) => {
|
|
540
|
+
expect(r.body).toBe('v2');
|
|
541
|
+
expect(r.envelope.created).toBe(firstCreated);
|
|
542
|
+
expect(r.envelope.updated).toBe(2000);
|
|
543
|
+
expect(r.envelope.seq).toBe(firstSeq + 1);
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
test('cap-cull fires end-to-end: writing past maxRecords evicts the oldest in the scope', async () => {
|
|
547
|
+
// maxRecords: 3 over the shared conversations/conv-1 MTM scope. Four
|
|
548
|
+
// distinct turns are written with strictly increasing `created`; the
|
|
549
|
+
// store builds the per-scope/per-kind admission cohort, so the fourth
|
|
550
|
+
// write culls the oldest (turn-0) and leaves the cap at exactly 3.
|
|
551
|
+
const store = memoryStore(undefined, 3);
|
|
552
|
+
for (let turn = 0; turn < 4; turn++) {
|
|
553
|
+
clockValue = 1000 + turn; // distinct `created` so "oldest" is unambiguous.
|
|
554
|
+
(await store.put(makeRecord({
|
|
555
|
+
id: `turn-${turn}`,
|
|
556
|
+
entityId: `conv-1:${turn}`,
|
|
557
|
+
kind: 'mtm',
|
|
558
|
+
body: `turn ${turn}`
|
|
559
|
+
}))).orThrow();
|
|
560
|
+
}
|
|
561
|
+
// turn-0 (oldest) was evicted; turn-1..turn-3 remain → exactly maxRecords.
|
|
562
|
+
expect(await store.get(mtmKind, 'conv-1:0')).toSucceedWith(undefined);
|
|
563
|
+
expect(await store.get(mtmKind, 'conv-1:1')).toSucceedAndSatisfy((r) => {
|
|
564
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-1');
|
|
565
|
+
});
|
|
566
|
+
expect(await store.get(mtmKind, 'conv-1:3')).toSucceedAndSatisfy((r) => {
|
|
567
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-3');
|
|
568
|
+
});
|
|
569
|
+
expect(await store.list({ kind: mtmKind })).toSucceedAndSatisfy((records) => {
|
|
570
|
+
expect(records).toHaveLength(3);
|
|
571
|
+
expect(records.map((r) => r.envelope.id).sort()).toEqual(['turn-1', 'turn-2', 'turn-3']);
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
test('cap-cull does not evict on a same-entity update (replace, not grow)', async () => {
|
|
575
|
+
// With the cohort excluding the target id, updating an existing turn while
|
|
576
|
+
// at the cap is a replace: the cohort is maxRecords-1, so no cull fires.
|
|
577
|
+
const store = memoryStore(undefined, 3);
|
|
578
|
+
for (let turn = 0; turn < 3; turn++) {
|
|
579
|
+
clockValue = 1000 + turn;
|
|
580
|
+
(await store.put(makeRecord({ id: `turn-${turn}`, entityId: `conv-1:${turn}`, kind: 'mtm', body: `v1-${turn}` }))).orThrow();
|
|
581
|
+
}
|
|
582
|
+
clockValue = 2000;
|
|
583
|
+
(await store.put(makeRecord({ id: 'turn-0', entityId: 'conv-1:0', kind: 'mtm', body: 'v2-0' }))).orThrow();
|
|
584
|
+
// All three still present (turn-0 updated, not culled).
|
|
585
|
+
expect(await store.list({ kind: mtmKind })).toSucceedAndSatisfy((records) => {
|
|
586
|
+
expect(records).toHaveLength(3);
|
|
587
|
+
});
|
|
588
|
+
expect(await store.get(mtmKind, 'conv-1:0')).toSucceedAndSatisfy((r) => {
|
|
589
|
+
expect(r === null || r === void 0 ? void 0 : r.body).toBe('v2-0');
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
test('cap-cull evicts multiple victims in one write when the cohort starts over cap', async () => {
|
|
593
|
+
// Seed five turns under cap 5 (no eviction), then reopen the same vault
|
|
594
|
+
// with cap 2. The next write's cohort is the five seeded records, so the
|
|
595
|
+
// single admit returns FOUR victims (oldest by `created`) and the post-
|
|
596
|
+
// write count settles to exactly the new cap.
|
|
597
|
+
const root = mutableRoot();
|
|
598
|
+
const seeded = memoryStore(root, 5);
|
|
599
|
+
for (let turn = 0; turn < 5; turn++) {
|
|
600
|
+
clockValue = 1000 + turn;
|
|
601
|
+
(await seeded.put(makeRecord({ id: `turn-${turn}`, entityId: `conv-1:${turn}`, kind: 'mtm', body: `turn ${turn}` }))).orThrow();
|
|
602
|
+
}
|
|
603
|
+
const reopened = memoryStore(root, 2);
|
|
604
|
+
clockValue = 2000;
|
|
605
|
+
(await reopened.put(makeRecord({ id: 'turn-5', entityId: 'conv-1:5', kind: 'mtm', body: 'turn 5' }))).orThrow();
|
|
606
|
+
// turn-0..turn-3 (the four oldest) evicted; turn-4 + turn-5 remain.
|
|
607
|
+
for (const culled of [0, 1, 2, 3]) {
|
|
608
|
+
expect(await reopened.get(mtmKind, `conv-1:${culled}`)).toSucceedWith(undefined);
|
|
609
|
+
}
|
|
610
|
+
expect(await reopened.list({ kind: mtmKind })).toSucceedAndSatisfy((records) => {
|
|
611
|
+
expect(records.map((r) => r.envelope.id).sort()).toEqual(['turn-4', 'turn-5']);
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
test('cap-cull is scoped to (scope, kind): other scopes and kinds are not evicted', async () => {
|
|
615
|
+
// cap 2 over MTM. A second conversation scope and a knowledge record share
|
|
616
|
+
// the vault; filling conv-1 past its cap must leave both untouched.
|
|
617
|
+
const store = memoryStore(undefined, 2);
|
|
618
|
+
clockValue = 500;
|
|
619
|
+
(await store.put(makeRecord({ id: 'turn-0', entityId: 'conv-2:0', kind: 'mtm', body: 'other convo' }))).orThrow();
|
|
620
|
+
clockValue = 600;
|
|
621
|
+
(await store.put(makeRecord({ id: 'intro', kind: 'knowledge', body: 'knowledge doc' }))).orThrow();
|
|
622
|
+
for (let turn = 0; turn < 3; turn++) {
|
|
623
|
+
clockValue = 1000 + turn;
|
|
624
|
+
(await store.put(makeRecord({
|
|
625
|
+
id: `turn-${turn}`,
|
|
626
|
+
entityId: `conv-1:${turn}`,
|
|
627
|
+
kind: 'mtm',
|
|
628
|
+
body: `c1 turn ${turn}`
|
|
629
|
+
}))).orThrow();
|
|
630
|
+
}
|
|
631
|
+
// conv-1 culled its oldest (turn-0) down to exactly cap 2.
|
|
632
|
+
expect(await store.get(mtmKind, 'conv-1:0')).toSucceedWith(undefined);
|
|
633
|
+
expect(await store.list({ scope: 'conversations/conv-1' })).toSucceedAndSatisfy((records) => {
|
|
634
|
+
expect(records.map((r) => r.envelope.id).sort()).toEqual(['turn-1', 'turn-2']);
|
|
635
|
+
});
|
|
636
|
+
// The other conversation scope and the knowledge kind are untouched.
|
|
637
|
+
expect(await store.get(mtmKind, 'conv-2:0')).toSucceedAndSatisfy((r) => {
|
|
638
|
+
expect(r === null || r === void 0 ? void 0 : r.envelope.id).toBe('turn-0');
|
|
639
|
+
expect(r === null || r === void 0 ? void 0 : r.body).toBe('other convo');
|
|
640
|
+
});
|
|
641
|
+
expect(await store.get(knowledgeKind, 'intro')).toSucceedAndSatisfy((r) => {
|
|
642
|
+
expect(r === null || r === void 0 ? void 0 : r.body).toBe('knowledge doc');
|
|
643
|
+
});
|
|
644
|
+
});
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
//# sourceMappingURL=fileTreeMemoryStore.test.js.map
|