@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,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
export * from './ids';
|
|
6
|
+
export * from './envelope';
|
|
7
|
+
export * from './filenameSafety';
|
|
8
|
+
export * from './identityCodec';
|
|
9
|
+
export * from './writePolicy';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nexport * from './ids';\nexport * from './envelope';\nexport * from './filenameSafety';\nexport * from './identityCodec';\nexport * from './writePolicy';\n"]}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
import { fail, succeed } from '@fgv/ts-utils';
|
|
6
|
+
import { JsonEditor } from '@fgv/ts-json';
|
|
7
|
+
/**
|
|
8
|
+
* The default {@link DedupScope} applied when a policy does not declare one.
|
|
9
|
+
* Entity-scoped dedup is the safe default — it never silently collapses two
|
|
10
|
+
* distinct entities with coincidentally-identical content.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_DEDUP_SCOPE = 'entity';
|
|
14
|
+
/**
|
|
15
|
+
* RFC-7386-compliant merge options for {@link IWritePolicy.applyUpdate}.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Phase-A verification (design-lock §5.1) confirmed `@fgv/ts-json`'s
|
|
19
|
+
* `JsonEditor` diverges from RFC-7386 under its defaults on two axes —
|
|
20
|
+
* `nullAsDelete` defaults `false` and `arrayMergeBehavior` defaults
|
|
21
|
+
* `'append'`. Both are corrected here via the existing option surface, so the
|
|
22
|
+
* primitive is composed (not extended, not hand-rolled).
|
|
23
|
+
*/
|
|
24
|
+
const MERGE_PATCH_OPTIONS = {
|
|
25
|
+
merge: {
|
|
26
|
+
nullAsDelete: true,
|
|
27
|
+
arrayMergeBehavior: 'replace'
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Last-write-wins write policy for the knowledge kind family. Admission
|
|
32
|
+
* always accepts (no cap, no cull); updates apply an RFC-7386 merge patch
|
|
33
|
+
* restricted to the knowledge mutable surface.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* **Merge-surface pin (resolves design-lock §5.3's body-vs-envelope muddle).**
|
|
37
|
+
* The declared `mutableFields` span both axes of a record: `body` is the
|
|
38
|
+
* per-kind body, while `tags` / `links` / `provenance` / `embeddingRef` live
|
|
39
|
+
* on the {@link IMemoryEnvelope}. `applyUpdate` projects exactly those fields
|
|
40
|
+
* — each read from its canonical location — into a single record-level JSON
|
|
41
|
+
* view, runs the merge over that view, then rebuilds a coherent record. The
|
|
42
|
+
* identity and transaction-time envelope fields (`id`, `entityId`, `kind`,
|
|
43
|
+
* `created`, `updated`, `seq`, `contentHash`) are NOT mutable and are
|
|
44
|
+
* preserved verbatim; the store stamps `updated` / `seq` on write.
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export class KnowledgeLwwPolicy {
|
|
48
|
+
constructor(cloneEditor, mergeEditor) {
|
|
49
|
+
/**
|
|
50
|
+
* The knowledge mutable surface: the body plus the envelope metadata a
|
|
51
|
+
* consumer may revise without minting a new entity.
|
|
52
|
+
*/
|
|
53
|
+
this.mutableFields = [
|
|
54
|
+
'body',
|
|
55
|
+
'tags',
|
|
56
|
+
'links',
|
|
57
|
+
'provenance',
|
|
58
|
+
'embeddingRef'
|
|
59
|
+
];
|
|
60
|
+
/**
|
|
61
|
+
* Knowledge dedups scope-wide: an identical `{ kind, body, links }` triple
|
|
62
|
+
* anywhere in the `knowledge` scope — even under a different `docId` — is a
|
|
63
|
+
* no-op. Declared explicitly so the B1 content-dedup behavior (and its tests)
|
|
64
|
+
* are unchanged by the {@link DedupScope} amendment.
|
|
65
|
+
*/
|
|
66
|
+
this.dedupScope = 'content';
|
|
67
|
+
this._cloneEditor = cloneEditor;
|
|
68
|
+
this._mergeEditor = mergeEditor;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Family-convention factory. Constructs the shared `JsonEditor` instances
|
|
72
|
+
* (one for cloning, one for the RFC-7386 merge), both with the template /
|
|
73
|
+
* conditional / multivalue / reference rules disabled (empty rules array).
|
|
74
|
+
*/
|
|
75
|
+
static create() {
|
|
76
|
+
return JsonEditor.create({}, []).onSuccess((cloneEditor) => JsonEditor.create(MERGE_PATCH_OPTIONS, []).onSuccess((mergeEditor) => succeed(new KnowledgeLwwPolicy(cloneEditor, mergeEditor))));
|
|
77
|
+
}
|
|
78
|
+
/** {@inheritDoc IWritePolicy.admit} */
|
|
79
|
+
admit(__incoming, __existing) {
|
|
80
|
+
// Last-write-wins: always accept. No cap, no cull.
|
|
81
|
+
return succeed({ decision: 'accept' });
|
|
82
|
+
}
|
|
83
|
+
/** {@inheritDoc IWritePolicy.applyUpdate} */
|
|
84
|
+
applyUpdate(existing, patch) {
|
|
85
|
+
// Project the mutable fields into a single record-level view, each sourced
|
|
86
|
+
// from its canonical location. `embeddingRef` is omitted when `undefined`
|
|
87
|
+
// (the editor rejects undefined property values).
|
|
88
|
+
const view = {
|
|
89
|
+
body: existing.body,
|
|
90
|
+
tags: existing.envelope.tags,
|
|
91
|
+
links: existing.envelope.links,
|
|
92
|
+
provenance: existing.envelope.provenance
|
|
93
|
+
};
|
|
94
|
+
if (existing.envelope.embeddingRef !== undefined) {
|
|
95
|
+
view.embeddingRef = existing.envelope.embeddingRef;
|
|
96
|
+
}
|
|
97
|
+
// Restrict the incoming patch to the declared mutable fields; out-of-scope
|
|
98
|
+
// keys are never applied (the store enforces the same constraint).
|
|
99
|
+
const scopedPatch = {};
|
|
100
|
+
for (const field of this.mutableFields) {
|
|
101
|
+
if (field in patch) {
|
|
102
|
+
scopedPatch[field] = patch[field];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Clone the view (no null-deletion), then apply the RFC-7386 merge patch
|
|
106
|
+
// onto the clone so the persisted record is never mutated in place.
|
|
107
|
+
return this._cloneEditor
|
|
108
|
+
.mergeObjectInPlace({}, view)
|
|
109
|
+
.onSuccess((clone) => this._mergeEditor.mergeObjectInPlace(clone, scopedPatch))
|
|
110
|
+
.onSuccess((merged) => this._rebuild(existing, merged));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Reassemble a record from the merged mutable view. `body` / `tags` /
|
|
114
|
+
* `links` / `provenance` are required and may not be deleted by a patch.
|
|
115
|
+
* `embeddingRef` is optional: when it is absent from the merged view —
|
|
116
|
+
* because the existing record never carried it OR a `null` patch deleted it
|
|
117
|
+
* (RFC-7386) — it is restored as `undefined` (absent), NOT `null`. This keeps
|
|
118
|
+
* an originally-absent `embeddingRef` from silently flipping to `null` on an
|
|
119
|
+
* unrelated update, so the field round-trips hash-stably through the store's
|
|
120
|
+
* content-hash recomputation.
|
|
121
|
+
*/
|
|
122
|
+
_rebuild(existing, merged) {
|
|
123
|
+
const required = ['body', 'tags', 'links', 'provenance'];
|
|
124
|
+
const missing = required.filter((field) => !(field in merged));
|
|
125
|
+
if (missing.length > 0) {
|
|
126
|
+
return fail(`knowledge LWW: merge patch may not delete required field(s): ${missing.join(', ')}`);
|
|
127
|
+
}
|
|
128
|
+
// The merged values are JSON projections of the already-validated typed
|
|
129
|
+
// record; restore the domain types. (The types packlet cannot import the
|
|
130
|
+
// converters packlet without a cycle, so these are structural restorations
|
|
131
|
+
// of fields the merge preserved, not fresh untrusted input.)
|
|
132
|
+
const envelope = Object.assign(Object.assign({}, existing.envelope), { tags: merged.tags, links: merged.links, provenance: merged.provenance, embeddingRef: 'embeddingRef' in merged ? merged.embeddingRef : undefined });
|
|
133
|
+
return succeed({ envelope, body: merged.body });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The record-level fields a {@link MemoryCapCullPolicy} merge-patch may project,
|
|
138
|
+
* mapped to their canonical location on a record. Mirrors the store's
|
|
139
|
+
* mutable-field accessor vocabulary; a declared mutable field outside this set
|
|
140
|
+
* is inert (the store cannot project it either).
|
|
141
|
+
*/
|
|
142
|
+
const CAP_CULL_FIELD_READERS = new Map([
|
|
143
|
+
['body', (r) => r.body],
|
|
144
|
+
['tags', (r) => r.envelope.tags],
|
|
145
|
+
['links', (r) => r.envelope.links],
|
|
146
|
+
['provenance', (r) => r.envelope.provenance],
|
|
147
|
+
['embeddingRef', (r) => r.envelope.embeddingRef]
|
|
148
|
+
]);
|
|
149
|
+
/** The record-level mutable fields that may never be deleted by a merge patch. */
|
|
150
|
+
const CAP_CULL_REQUIRED_FIELDS = new Set([
|
|
151
|
+
'body',
|
|
152
|
+
'tags',
|
|
153
|
+
'links',
|
|
154
|
+
'provenance'
|
|
155
|
+
]);
|
|
156
|
+
/**
|
|
157
|
+
* Bounded-ring write policy for the experience (memory) kind families.
|
|
158
|
+
* Admission accepts until `maxRecords` is reached, then evicts the oldest
|
|
159
|
+
* record(s) by `created` ascending (design-lock §5.3); updates apply the same
|
|
160
|
+
* RFC-7386 merge patch as {@link KnowledgeLwwPolicy}, restricted to the declared
|
|
161
|
+
* {@link IMemoryCapCullPolicyParams.mutableFields | mutableFields}.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* - **Dedup scope.** Declares `dedupScope: 'entity'` — two distinct memory
|
|
165
|
+
* entities (e.g. `turn-5` / `turn-9`) with identical `{ kind, body, links }`
|
|
166
|
+
* never collapse; only an identical re-put of the SAME entity is a no-op.
|
|
167
|
+
* - **Eviction boundary.** `admit` only DECIDES (returns the `MemoryId`s to
|
|
168
|
+
* evict); the store executes the file deletions and index patches. The
|
|
169
|
+
* `existing` cohort the cap counts against is whatever the store supplies to
|
|
170
|
+
* `admit`.
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
export class MemoryCapCullPolicy {
|
|
174
|
+
constructor(params, cloneEditor, mergeEditor) {
|
|
175
|
+
/** Experience kinds dedup per-entity (see the class remarks). */
|
|
176
|
+
this.dedupScope = 'entity';
|
|
177
|
+
this.mutableFields = params.mutableFields;
|
|
178
|
+
this._maxRecords = params.maxRecords;
|
|
179
|
+
this._cloneEditor = cloneEditor;
|
|
180
|
+
this._mergeEditor = mergeEditor;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Family-convention factory. Constructs the shared `JsonEditor` instances
|
|
184
|
+
* (one for cloning, one for the RFC-7386 merge) with the same merge config as
|
|
185
|
+
* {@link KnowledgeLwwPolicy} (`nullAsDelete` true, `arrayMergeBehavior`
|
|
186
|
+
* `'replace'`, rules disabled).
|
|
187
|
+
*/
|
|
188
|
+
static create(params) {
|
|
189
|
+
return JsonEditor.create({}, []).onSuccess((cloneEditor) => JsonEditor.create(MERGE_PATCH_OPTIONS, []).onSuccess((mergeEditor) => succeed(new MemoryCapCullPolicy(params, cloneEditor, mergeEditor))));
|
|
190
|
+
}
|
|
191
|
+
/** {@inheritDoc IWritePolicy.admit} */
|
|
192
|
+
admit(__incoming, existing) {
|
|
193
|
+
if (this._maxRecords === undefined || existing.length < this._maxRecords) {
|
|
194
|
+
return succeed({ decision: 'accept' });
|
|
195
|
+
}
|
|
196
|
+
// Cap reached: evict the oldest by `created` ascending so the post-write
|
|
197
|
+
// count is exactly `maxRecords` (existing.length - maxRecords + 1 victims).
|
|
198
|
+
const evict = [...existing]
|
|
199
|
+
.sort((a, b) => a.envelope.created - b.envelope.created)
|
|
200
|
+
.slice(0, existing.length - this._maxRecords + 1)
|
|
201
|
+
.map((record) => record.envelope.id);
|
|
202
|
+
return succeed({ decision: 'cull-oldest', evict });
|
|
203
|
+
}
|
|
204
|
+
/** {@inheritDoc IWritePolicy.applyUpdate} */
|
|
205
|
+
applyUpdate(existing, patch) {
|
|
206
|
+
// Project the declared mutable fields (restricted to the known record-level
|
|
207
|
+
// vocabulary) into a single record-level view from their canonical
|
|
208
|
+
// locations. An `undefined` value is omitted (the editor rejects undefined
|
|
209
|
+
// property values).
|
|
210
|
+
const view = {};
|
|
211
|
+
for (const field of this.mutableFields) {
|
|
212
|
+
const reader = CAP_CULL_FIELD_READERS.get(field);
|
|
213
|
+
if (reader !== undefined) {
|
|
214
|
+
const value = reader(existing);
|
|
215
|
+
if (value !== undefined) {
|
|
216
|
+
view[field] = value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Restrict the incoming patch to the declared mutable fields.
|
|
221
|
+
const scopedPatch = {};
|
|
222
|
+
for (const field of this.mutableFields) {
|
|
223
|
+
if (field in patch) {
|
|
224
|
+
scopedPatch[field] = patch[field];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Clone the view (no null-deletion), then apply the RFC-7386 merge patch
|
|
228
|
+
// onto the clone so the persisted record is never mutated in place.
|
|
229
|
+
return this._cloneEditor
|
|
230
|
+
.mergeObjectInPlace({}, view)
|
|
231
|
+
.onSuccess((clone) => this._mergeEditor.mergeObjectInPlace(clone, scopedPatch))
|
|
232
|
+
.onSuccess((merged) => this._rebuild(existing, merged));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Reassemble a record from the merged mutable view. Only the declared mutable
|
|
236
|
+
* fields are taken from the merge; undeclared fields are preserved verbatim
|
|
237
|
+
* from `existing`. A `null` patch that deletes a *declared mutable* required
|
|
238
|
+
* field (`body` / `tags` / `links` / `provenance`) is an error — a required
|
|
239
|
+
* field that is NOT declared mutable simply falls through to its `existing.*`
|
|
240
|
+
* value and is never at risk. `embeddingRef`, when mutable, is restored as
|
|
241
|
+
* `undefined` (absent) if the merge dropped it — same hash-stable semantics as
|
|
242
|
+
* {@link KnowledgeLwwPolicy}.
|
|
243
|
+
*/
|
|
244
|
+
_rebuild(existing, merged) {
|
|
245
|
+
const deleted = this.mutableFields.filter((field) => CAP_CULL_REQUIRED_FIELDS.has(field) && !(field in merged));
|
|
246
|
+
if (deleted.length > 0) {
|
|
247
|
+
return fail(`memory cap-cull: merge patch may not delete required field(s): ${deleted.join(', ')}`);
|
|
248
|
+
}
|
|
249
|
+
// The merged values are JSON projections of the already-validated typed
|
|
250
|
+
// record; restore the domain types. (The types packlet cannot import the
|
|
251
|
+
// converters packlet without a cycle, so these are structural restorations
|
|
252
|
+
// of fields the merge preserved, not fresh untrusted input — mirrors
|
|
253
|
+
// KnowledgeLwwPolicy._rebuild.)
|
|
254
|
+
const embeddingRefMutable = this.mutableFields.includes('embeddingRef');
|
|
255
|
+
const envelope = Object.assign(Object.assign({}, existing.envelope), { tags: 'tags' in merged ? merged.tags : existing.envelope.tags, links: 'links' in merged ? merged.links : existing.envelope.links, provenance: 'provenance' in merged ? merged.provenance : existing.envelope.provenance, embeddingRef: embeddingRefMutable
|
|
256
|
+
? 'embeddingRef' in merged
|
|
257
|
+
? merged.embeddingRef
|
|
258
|
+
: undefined
|
|
259
|
+
: existing.envelope.embeddingRef });
|
|
260
|
+
return succeed({ envelope, body: 'body' in merged ? merged.body : existing.body });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=writePolicy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"writePolicy.js","sourceRoot":"","sources":["../../../src/packlets/types/writePolicy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAU,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EAAsB,UAAU,EAAE,MAAM,cAAc,CAAC;AAoB9D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAe,QAAQ,CAAC;AAuExD;;;;;;;;;GASG;AACH,MAAM,mBAAmB,GAAgC;IACvD,KAAK,EAAE;QACL,YAAY,EAAE,IAAI;QAClB,kBAAkB,EAAE,SAAS;KAC9B;CACF,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,kBAAkB;IA0B7B,YAAoB,WAAuB,EAAE,WAAuB;QAzBpE;;;WAGG;QACa,kBAAa,GAA0B;YACrD,MAAM;YACN,MAAM;YACN,OAAO;YACP,YAAY;YACZ,cAAc;SACf,CAAC;QAEF;;;;;WAKG;QACa,eAAU,GAAe,SAAS,CAAC;QAQjD,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM;QAClB,OAAO,UAAU,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,EAAE,CACzD,UAAU,CAAC,MAAM,CAAC,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I,EAChG,KAAK,EAAE,OAAO,IAAI,MAAM,CAAC,CAAC,CAAE,MAAM,CAAC,KAAyC,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,EACtG,UAAU,EACR,YAAY,IAAI,MAAM,CAAC,CAAC,CAAE,MAAM,CAAC,UAAqC,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,EACvG,YAAY,EAAE,mBAAmB;gBAC/B,CAAC,CAAC,cAAc,IAAI,MAAM;oBACxB,CAAC,CAAE,MAAM,CAAC,YAA8B;oBACxC,CAAC,CAAC,SAAS;gBACb,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,YAAY,GACnC,CAAC;QACF,OAAO,OAAO,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACrF,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, succeed } from '@fgv/ts-utils';\nimport { JsonObject } from '@fgv/ts-json-base';\nimport { IJsonEditorOptions, JsonEditor } from '@fgv/ts-json';\nimport { IEdge, IMemoryEnvelope, IMemoryRecord, IProvenance } from './envelope';\nimport { MemoryId, Tag } from './ids';\n\n/**\n * The granularity at which the store deduplicates an incoming write against the\n * existing vault, declared per kind via {@link IWritePolicy.dedupScope}.\n *\n * - `'content'`: scope-wide cross-id content dedup. An identical\n * `{ kind, body, links }` triple ANYWHERE in the scope — even under a\n * different id — is a no-op that returns the existing record. The knowledge\n * kind family uses this.\n * - `'entity'`: same-id content dedup only. An identical re-put of the SAME\n * entity is a no-op, but two DISTINCT entities with identical content never\n * collapse. The experience (memory) kind families use this so that, e.g.,\n * two turns whose summaries happen to be byte-identical both persist.\n * @public\n */\nexport type DedupScope = 'content' | 'entity';\n\n/**\n * The default {@link DedupScope} applied when a policy does not declare one.\n * Entity-scoped dedup is the safe default — it never silently collapses two\n * distinct entities with coincidentally-identical content.\n * @public\n */\nexport const DEFAULT_DEDUP_SCOPE: DedupScope = 'entity';\n\n/**\n * The admission decision returned by {@link IWritePolicy.admit}.\n * @public\n */\nexport type AdmissionDecision =\n | { readonly decision: 'accept' }\n | { readonly decision: 'reject'; readonly reason: string }\n | {\n readonly decision: 'cull-oldest';\n /** {@link MemoryId}s to evict before writing the incoming record. */\n readonly evict: ReadonlyArray<MemoryId>;\n };\n\n/**\n * A per-kind write policy, injected at store construction. Invoked AFTER\n * content-hash dedup (dedup is always pre-policy). The policy decides\n * admission and, on update, applies a JSON Merge Patch (RFC-7386) restricted\n * to the declared mutable fields.\n * @public\n */\nexport interface IWritePolicy {\n /**\n * Names of the fields a merge-patch update may touch. Fields outside this\n * list are immutable; a change to one constitutes a new entity (its\n * `contentHash` differs). Used by {@link IWritePolicy.applyUpdate} to\n * restrict which fields the patch applies.\n */\n readonly mutableFields: ReadonlyArray<string>;\n\n /**\n * The granularity at which the store deduplicates an incoming write for this\n * kind. Optional; when absent the store applies {@link DEFAULT_DEDUP_SCOPE}\n * (`'entity'`). See {@link DedupScope}.\n */\n readonly dedupScope?: DedupScope;\n\n /**\n * Determine whether the incoming record is admitted.\n * @param incoming - The record about to be written.\n * @param existing - The admission cohort the kind's cap applies to: the\n * records in the same scope of the same kind, EXCLUDING the record at\n * `incoming`'s target id. Empty on a first write into an empty cohort.\n * Excluding the target id makes the post-write count uniform\n * (`existing.length + 1`) across first-writes and same-id updates. A\n * last-write-wins policy that has no cap ignores this argument.\n * @returns A {@link AdmissionDecision}.\n */\n admit(\n incoming: IMemoryRecord<unknown>,\n existing: ReadonlyArray<IMemoryRecord<unknown>>\n ): Result<AdmissionDecision>;\n\n /**\n * Apply a JSON Merge Patch (RFC-7386) to the mutable fields of an existing\n * record. Called when admission is `accept` AND a record with the same\n * `entityId` already exists (an update, not a first write).\n *\n * @param existing - The current persisted record.\n * @param patch - A partial JSON object in Merge Patch format. `null`\n * deletes the corresponding key; arrays replace wholesale; nested objects\n * deep-merge. Only keys in {@link IWritePolicy.mutableFields} are applied.\n * @returns The updated record (envelope + body).\n */\n applyUpdate(\n existing: IMemoryRecord<unknown>,\n patch: Record<string, unknown>\n ): Result<IMemoryRecord<unknown>>;\n}\n\n/**\n * RFC-7386-compliant merge options for {@link IWritePolicy.applyUpdate}.\n *\n * @remarks\n * Phase-A verification (design-lock §5.1) confirmed `@fgv/ts-json`'s\n * `JsonEditor` diverges from RFC-7386 under its defaults on two axes —\n * `nullAsDelete` defaults `false` and `arrayMergeBehavior` defaults\n * `'append'`. Both are corrected here via the existing option surface, so the\n * primitive is composed (not extended, not hand-rolled).\n */\nconst MERGE_PATCH_OPTIONS: Partial<IJsonEditorOptions> = {\n merge: {\n nullAsDelete: true,\n arrayMergeBehavior: 'replace'\n }\n};\n\n/**\n * Last-write-wins write policy for the knowledge kind family. Admission\n * always accepts (no cap, no cull); updates apply an RFC-7386 merge patch\n * restricted to the knowledge mutable surface.\n *\n * @remarks\n * **Merge-surface pin (resolves design-lock §5.3's body-vs-envelope muddle).**\n * The declared `mutableFields` span both axes of a record: `body` is the\n * per-kind body, while `tags` / `links` / `provenance` / `embeddingRef` live\n * on the {@link IMemoryEnvelope}. `applyUpdate` projects exactly those fields\n * — each read from its canonical location — into a single record-level JSON\n * view, runs the merge over that view, then rebuilds a coherent record. The\n * identity and transaction-time envelope fields (`id`, `entityId`, `kind`,\n * `created`, `updated`, `seq`, `contentHash`) are NOT mutable and are\n * preserved verbatim; the store stamps `updated` / `seq` on write.\n * @public\n */\nexport class KnowledgeLwwPolicy implements IWritePolicy {\n /**\n * The knowledge mutable surface: the body plus the envelope metadata a\n * consumer may revise without minting a new entity.\n */\n public readonly mutableFields: ReadonlyArray<string> = [\n 'body',\n 'tags',\n 'links',\n 'provenance',\n 'embeddingRef'\n ];\n\n /**\n * Knowledge dedups scope-wide: an identical `{ kind, body, links }` triple\n * anywhere in the `knowledge` scope — even under a different `docId` — is a\n * no-op. Declared explicitly so the B1 content-dedup behavior (and its tests)\n * are unchanged by the {@link DedupScope} amendment.\n */\n public readonly dedupScope: DedupScope = 'content';\n\n /** Deep-clones the mutable view without RFC-7386 null-deletion semantics. */\n private readonly _cloneEditor: JsonEditor;\n /** Applies the RFC-7386 merge patch. */\n private readonly _mergeEditor: JsonEditor;\n\n private constructor(cloneEditor: JsonEditor, mergeEditor: JsonEditor) {\n this._cloneEditor = cloneEditor;\n this._mergeEditor = mergeEditor;\n }\n\n /**\n * Family-convention factory. Constructs the shared `JsonEditor` instances\n * (one for cloning, one for the RFC-7386 merge), both with the template /\n * conditional / multivalue / reference rules disabled (empty rules array).\n */\n public static create(): Result<KnowledgeLwwPolicy> {\n return JsonEditor.create({}, []).onSuccess((cloneEditor) =>\n JsonEditor.create(MERGE_PATCH_OPTIONS, []).onSuccess((mergeEditor) =>\n succeed(new KnowledgeLwwPolicy(cloneEditor, mergeEditor))\n )\n );\n }\n\n /** {@inheritDoc IWritePolicy.admit} */\n public admit(\n __incoming: IMemoryRecord<unknown>,\n __existing: ReadonlyArray<IMemoryRecord<unknown>>\n ): Result<AdmissionDecision> {\n // Last-write-wins: always accept. No cap, no cull.\n return succeed({ decision: 'accept' });\n }\n\n /** {@inheritDoc IWritePolicy.applyUpdate} */\n public applyUpdate(\n existing: IMemoryRecord<unknown>,\n patch: Record<string, unknown>\n ): Result<IMemoryRecord<unknown>> {\n // Project the mutable fields into a single record-level view, each sourced\n // from its canonical location. `embeddingRef` is omitted when `undefined`\n // (the editor rejects undefined property values).\n const view: Record<string, unknown> = {\n body: existing.body,\n tags: existing.envelope.tags,\n links: existing.envelope.links,\n provenance: existing.envelope.provenance\n };\n if (existing.envelope.embeddingRef !== undefined) {\n view.embeddingRef = existing.envelope.embeddingRef;\n }\n\n // Restrict the incoming patch to the declared mutable fields; out-of-scope\n // keys are never applied (the store enforces the same constraint).\n const scopedPatch: Record<string, unknown> = {};\n for (const field of this.mutableFields) {\n if (field in patch) {\n scopedPatch[field] = patch[field];\n }\n }\n\n // Clone the view (no null-deletion), then apply the RFC-7386 merge patch\n // onto the clone so the persisted record is never mutated in place.\n return this._cloneEditor\n .mergeObjectInPlace({}, view as JsonObject)\n .onSuccess((clone) => this._mergeEditor.mergeObjectInPlace(clone, scopedPatch as JsonObject))\n .onSuccess((merged) => this._rebuild(existing, merged));\n }\n\n /**\n * Reassemble a record from the merged mutable view. `body` / `tags` /\n * `links` / `provenance` are required and may not be deleted by a patch.\n * `embeddingRef` is optional: when it is absent from the merged view —\n * because the existing record never carried it OR a `null` patch deleted it\n * (RFC-7386) — it is restored as `undefined` (absent), NOT `null`. This keeps\n * an originally-absent `embeddingRef` from silently flipping to `null` on an\n * unrelated update, so the field round-trips hash-stably through the store's\n * content-hash recomputation.\n */\n private _rebuild(existing: IMemoryRecord<unknown>, merged: JsonObject): Result<IMemoryRecord<unknown>> {\n const required: ReadonlyArray<string> = ['body', 'tags', 'links', 'provenance'];\n const missing: ReadonlyArray<string> = required.filter((field) => !(field in merged));\n if (missing.length > 0) {\n return fail(`knowledge LWW: merge patch may not delete required field(s): ${missing.join(', ')}`);\n }\n\n // The merged values are JSON projections of the already-validated typed\n // record; restore the domain types. (The types packlet cannot import the\n // converters packlet without a cycle, so these are structural restorations\n // of fields the merge preserved, not fresh untrusted input.)\n const envelope: IMemoryEnvelope = {\n ...existing.envelope,\n tags: merged.tags as unknown as ReadonlyArray<Tag>,\n links: merged.links as unknown as ReadonlyArray<IEdge>,\n provenance: merged.provenance as unknown as IProvenance,\n embeddingRef: 'embeddingRef' in merged ? (merged.embeddingRef as string | null) : undefined\n };\n return succeed({ envelope, body: merged.body });\n }\n}\n\n/**\n * Parameters for {@link MemoryCapCullPolicy.create}.\n * @public\n */\nexport interface IMemoryCapCullPolicyParams {\n /**\n * Maximum number of records the policy admits before culling the oldest.\n * Counted over the `existing` cohort passed to {@link IWritePolicy.admit}.\n * Absent = no cap (admission always accepts).\n */\n readonly maxRecords?: number;\n /**\n * The fields a merge-patch update may touch (drawn from the record-level\n * mutable vocabulary: `body` / `tags` / `links` / `provenance` /\n * `embeddingRef`). Fields outside this list are immutable.\n */\n readonly mutableFields: ReadonlyArray<string>;\n}\n\n/**\n * The record-level fields a {@link MemoryCapCullPolicy} merge-patch may project,\n * mapped to their canonical location on a record. Mirrors the store's\n * mutable-field accessor vocabulary; a declared mutable field outside this set\n * is inert (the store cannot project it either).\n */\nconst CAP_CULL_FIELD_READERS: ReadonlyMap<string, (record: IMemoryRecord<unknown>) => unknown> = new Map<\n string,\n (record: IMemoryRecord<unknown>) => unknown\n>([\n ['body', (r) => r.body],\n ['tags', (r) => r.envelope.tags],\n ['links', (r) => r.envelope.links],\n ['provenance', (r) => r.envelope.provenance],\n ['embeddingRef', (r) => r.envelope.embeddingRef]\n]);\n\n/** The record-level mutable fields that may never be deleted by a merge patch. */\nconst CAP_CULL_REQUIRED_FIELDS: ReadonlySet<string> = new Set<string>([\n 'body',\n 'tags',\n 'links',\n 'provenance'\n]);\n\n/**\n * Bounded-ring write policy for the experience (memory) kind families.\n * Admission accepts until `maxRecords` is reached, then evicts the oldest\n * record(s) by `created` ascending (design-lock §5.3); updates apply the same\n * RFC-7386 merge patch as {@link KnowledgeLwwPolicy}, restricted to the declared\n * {@link IMemoryCapCullPolicyParams.mutableFields | mutableFields}.\n *\n * @remarks\n * - **Dedup scope.** Declares `dedupScope: 'entity'` — two distinct memory\n * entities (e.g. `turn-5` / `turn-9`) with identical `{ kind, body, links }`\n * never collapse; only an identical re-put of the SAME entity is a no-op.\n * - **Eviction boundary.** `admit` only DECIDES (returns the `MemoryId`s to\n * evict); the store executes the file deletions and index patches. The\n * `existing` cohort the cap counts against is whatever the store supplies to\n * `admit`.\n * @public\n */\nexport class MemoryCapCullPolicy implements IWritePolicy {\n /** {@inheritDoc IWritePolicy.mutableFields} */\n public readonly mutableFields: ReadonlyArray<string>;\n\n /** Experience kinds dedup per-entity (see the class remarks). */\n public readonly dedupScope: DedupScope = 'entity';\n\n /** The admission cap; `undefined` = no cap. */\n private readonly _maxRecords: number | undefined;\n /** Deep-clones the mutable view without RFC-7386 null-deletion semantics. */\n private readonly _cloneEditor: JsonEditor;\n /** Applies the RFC-7386 merge patch. */\n private readonly _mergeEditor: JsonEditor;\n\n private constructor(params: IMemoryCapCullPolicyParams, cloneEditor: JsonEditor, mergeEditor: JsonEditor) {\n this.mutableFields = params.mutableFields;\n this._maxRecords = params.maxRecords;\n this._cloneEditor = cloneEditor;\n this._mergeEditor = mergeEditor;\n }\n\n /**\n * Family-convention factory. Constructs the shared `JsonEditor` instances\n * (one for cloning, one for the RFC-7386 merge) with the same merge config as\n * {@link KnowledgeLwwPolicy} (`nullAsDelete` true, `arrayMergeBehavior`\n * `'replace'`, rules disabled).\n */\n public static create(params: IMemoryCapCullPolicyParams): Result<MemoryCapCullPolicy> {\n return JsonEditor.create({}, []).onSuccess((cloneEditor) =>\n JsonEditor.create(MERGE_PATCH_OPTIONS, []).onSuccess((mergeEditor) =>\n succeed(new MemoryCapCullPolicy(params, cloneEditor, mergeEditor))\n )\n );\n }\n\n /** {@inheritDoc IWritePolicy.admit} */\n public admit(\n __incoming: IMemoryRecord<unknown>,\n existing: ReadonlyArray<IMemoryRecord<unknown>>\n ): Result<AdmissionDecision> {\n if (this._maxRecords === undefined || existing.length < this._maxRecords) {\n return succeed({ decision: 'accept' });\n }\n // Cap reached: evict the oldest by `created` ascending so the post-write\n // count is exactly `maxRecords` (existing.length - maxRecords + 1 victims).\n const evict: ReadonlyArray<MemoryId> = [...existing]\n .sort((a, b) => a.envelope.created - b.envelope.created)\n .slice(0, existing.length - this._maxRecords + 1)\n .map((record) => record.envelope.id);\n return succeed({ decision: 'cull-oldest', evict });\n }\n\n /** {@inheritDoc IWritePolicy.applyUpdate} */\n public applyUpdate(\n existing: IMemoryRecord<unknown>,\n patch: Record<string, unknown>\n ): Result<IMemoryRecord<unknown>> {\n // Project the declared mutable fields (restricted to the known record-level\n // vocabulary) into a single record-level view from their canonical\n // locations. An `undefined` value is omitted (the editor rejects undefined\n // property values).\n const view: Record<string, unknown> = {};\n for (const field of this.mutableFields) {\n const reader: ((record: IMemoryRecord<unknown>) => unknown) | undefined =\n CAP_CULL_FIELD_READERS.get(field);\n if (reader !== undefined) {\n const value: unknown = reader(existing);\n if (value !== undefined) {\n view[field] = value;\n }\n }\n }\n\n // Restrict the incoming patch to the declared mutable fields.\n const scopedPatch: Record<string, unknown> = {};\n for (const field of this.mutableFields) {\n if (field in patch) {\n scopedPatch[field] = patch[field];\n }\n }\n\n // Clone the view (no null-deletion), then apply the RFC-7386 merge patch\n // onto the clone so the persisted record is never mutated in place.\n return this._cloneEditor\n .mergeObjectInPlace({}, view as JsonObject)\n .onSuccess((clone) => this._mergeEditor.mergeObjectInPlace(clone, scopedPatch as JsonObject))\n .onSuccess((merged) => this._rebuild(existing, merged));\n }\n\n /**\n * Reassemble a record from the merged mutable view. Only the declared mutable\n * fields are taken from the merge; undeclared fields are preserved verbatim\n * from `existing`. A `null` patch that deletes a *declared mutable* required\n * field (`body` / `tags` / `links` / `provenance`) is an error — a required\n * field that is NOT declared mutable simply falls through to its `existing.*`\n * value and is never at risk. `embeddingRef`, when mutable, is restored as\n * `undefined` (absent) if the merge dropped it — same hash-stable semantics as\n * {@link KnowledgeLwwPolicy}.\n */\n private _rebuild(existing: IMemoryRecord<unknown>, merged: JsonObject): Result<IMemoryRecord<unknown>> {\n const deleted: ReadonlyArray<string> = this.mutableFields.filter(\n (field) => CAP_CULL_REQUIRED_FIELDS.has(field) && !(field in merged)\n );\n if (deleted.length > 0) {\n return fail(`memory cap-cull: merge patch may not delete required field(s): ${deleted.join(', ')}`);\n }\n\n // The merged values are JSON projections of the already-validated typed\n // record; restore the domain types. (The types packlet cannot import the\n // converters packlet without a cycle, so these are structural restorations\n // of fields the merge preserved, not fresh untrusted input — mirrors\n // KnowledgeLwwPolicy._rebuild.)\n const embeddingRefMutable: boolean = this.mutableFields.includes('embeddingRef');\n const envelope: IMemoryEnvelope = {\n ...existing.envelope,\n tags: 'tags' in merged ? (merged.tags as unknown as ReadonlyArray<Tag>) : existing.envelope.tags,\n links: 'links' in merged ? (merged.links as unknown as ReadonlyArray<IEdge>) : existing.envelope.links,\n provenance:\n 'provenance' in merged ? (merged.provenance as unknown as IProvenance) : existing.envelope.provenance,\n embeddingRef: embeddingRefMutable\n ? 'embeddingRef' in merged\n ? (merged.embeddingRef as string | null)\n : undefined\n : existing.envelope.embeddingRef\n };\n return succeed({ envelope, body: 'body' in merged ? merged.body : existing.body });\n }\n}\n"]}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
import { fail, succeed } from '@fgv/ts-utils';
|
|
6
|
+
/**
|
|
7
|
+
* The brute-force, in-memory cosine {@link IVectorIndex}. Stores one
|
|
8
|
+
* `Float32Array` per record and answers a query by computing cosine similarity
|
|
9
|
+
* against every stored vector, returning the top-k by descending score.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* This is the **complete** vector implementation for the fgv regime — large-N is
|
|
13
|
+
* explicitly out of scope (the seam stays open for a consumer to swap an external
|
|
14
|
+
* ANN backend once N grows beyond "thousands of records"). No external dependency
|
|
15
|
+
* and no ANN structure: a linear scan over a few thousand vectors is well within
|
|
16
|
+
* an interactive budget.
|
|
17
|
+
*
|
|
18
|
+
* The index has a single dimension established by the first vector added; every
|
|
19
|
+
* subsequent `add` and every `query` vector must match that dimension or fail
|
|
20
|
+
* loudly — a mismatched dimension is an embedder-wiring bug, never a silent
|
|
21
|
+
* zero-similarity result. {@link InMemoryCosineIndex.rebuild | rebuild} clears
|
|
22
|
+
* the index, so a re-embed with a different model (hence dimension) is supported.
|
|
23
|
+
*
|
|
24
|
+
* Persistence (a JSON sidecar) is deliberately out of scope for this layer — the
|
|
25
|
+
* index is in-memory and rebuilt from the store via `rebuild`; a sidecar is a
|
|
26
|
+
* future nicety.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export class InMemoryCosineIndex {
|
|
30
|
+
constructor() {
|
|
31
|
+
this._vectors = new Map();
|
|
32
|
+
this._dimension = undefined;
|
|
33
|
+
}
|
|
34
|
+
/** The number of vectors currently held. */
|
|
35
|
+
get size() {
|
|
36
|
+
return this._vectors.size;
|
|
37
|
+
}
|
|
38
|
+
/** Family-convention factory. */
|
|
39
|
+
static create() {
|
|
40
|
+
return succeed(new InMemoryCosineIndex());
|
|
41
|
+
}
|
|
42
|
+
/** {@inheritDoc IVectorIndex.add} */
|
|
43
|
+
add(id, vector) {
|
|
44
|
+
if (vector.length === 0) {
|
|
45
|
+
return Promise.resolve(fail(`vector index: cannot add '${id}': empty vector`));
|
|
46
|
+
}
|
|
47
|
+
if (this._dimension === undefined) {
|
|
48
|
+
this._dimension = vector.length;
|
|
49
|
+
}
|
|
50
|
+
else if (vector.length !== this._dimension) {
|
|
51
|
+
return Promise.resolve(fail(`vector index: cannot add '${id}': dimension ${vector.length} does not match index dimension ${this._dimension}`));
|
|
52
|
+
}
|
|
53
|
+
// Defensive copy: the caller may reuse or mutate the buffer after `add`, and
|
|
54
|
+
// the index must keep serving the embedding it was given.
|
|
55
|
+
this._vectors.set(id, Float32Array.from(vector));
|
|
56
|
+
// The in-memory index keys entries by id, so the id IS the entry reference.
|
|
57
|
+
return Promise.resolve(succeed(id));
|
|
58
|
+
}
|
|
59
|
+
/** {@inheritDoc IVectorIndex.remove} */
|
|
60
|
+
remove(id) {
|
|
61
|
+
this._vectors.delete(id);
|
|
62
|
+
return Promise.resolve(succeed(id));
|
|
63
|
+
}
|
|
64
|
+
/** {@inheritDoc IVectorIndex.query} */
|
|
65
|
+
query(vector, topK) {
|
|
66
|
+
if (topK <= 0 || this._vectors.size === 0) {
|
|
67
|
+
return Promise.resolve(succeed([]));
|
|
68
|
+
}
|
|
69
|
+
if (vector.length !== this._dimension) {
|
|
70
|
+
return Promise.resolve(fail(`vector index: query dimension ${vector.length} does not match index dimension ${this._dimension}`));
|
|
71
|
+
}
|
|
72
|
+
const queryMagnitude = InMemoryCosineIndex._magnitude(vector);
|
|
73
|
+
const hits = [];
|
|
74
|
+
for (const [id, stored] of this._vectors) {
|
|
75
|
+
hits.push({ id, score: InMemoryCosineIndex._cosine(vector, queryMagnitude, stored) });
|
|
76
|
+
}
|
|
77
|
+
// Descending by score; a `seq`-free tiebreak is unnecessary here because the
|
|
78
|
+
// caller (SemanticRetriever) re-resolves hits against the record index.
|
|
79
|
+
hits.sort((a, b) => b.score - a.score);
|
|
80
|
+
return Promise.resolve(succeed(hits.length > topK ? hits.slice(0, topK) : hits));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Re-embed every record from `source` and rebuild the index from scratch.
|
|
84
|
+
* Clears the current contents (and the established dimension) first, so a
|
|
85
|
+
* re-embed with a different model is supported. Returns the number of vectors
|
|
86
|
+
* indexed.
|
|
87
|
+
*
|
|
88
|
+
* On any failure (list, embed, or add) the index is rolled back to empty
|
|
89
|
+
* rather than left in a partially-rebuilt state — a caller that retries a query
|
|
90
|
+
* after a failed rebuild sees a clean empty index, never a half-populated one.
|
|
91
|
+
*
|
|
92
|
+
* @param source - The record source to re-embed (an {@link IMemoryStore}
|
|
93
|
+
* satisfies this structurally).
|
|
94
|
+
* @param embed - The embedder applied to each record.
|
|
95
|
+
*/
|
|
96
|
+
async rebuild(source, embed) {
|
|
97
|
+
// Reset up front so the "any failure leaves the index empty" contract holds
|
|
98
|
+
// even when the listing itself fails (no stale vectors survive a failed
|
|
99
|
+
// rebuild).
|
|
100
|
+
this._reset();
|
|
101
|
+
const listed = await source.list();
|
|
102
|
+
if (listed.isFailure()) {
|
|
103
|
+
return fail(`vector index rebuild: failed to list records: ${listed.message}`);
|
|
104
|
+
}
|
|
105
|
+
for (const record of listed.value) {
|
|
106
|
+
const embedded = await embed(record);
|
|
107
|
+
if (embedded.isFailure()) {
|
|
108
|
+
this._reset();
|
|
109
|
+
return fail(`vector index rebuild: embedding '${record.envelope.id}' failed: ${embedded.message}`);
|
|
110
|
+
}
|
|
111
|
+
const added = await this.add(record.envelope.id, embedded.value);
|
|
112
|
+
if (added.isFailure()) {
|
|
113
|
+
this._reset();
|
|
114
|
+
return fail(`vector index rebuild: ${added.message}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return succeed(this._vectors.size);
|
|
118
|
+
}
|
|
119
|
+
/** Empty the index and forget the established dimension. */
|
|
120
|
+
_reset() {
|
|
121
|
+
this._vectors.clear();
|
|
122
|
+
this._dimension = undefined;
|
|
123
|
+
}
|
|
124
|
+
/** The Euclidean magnitude (L2 norm) of a vector. */
|
|
125
|
+
static _magnitude(vector) {
|
|
126
|
+
let sum = 0;
|
|
127
|
+
for (let i = 0; i < vector.length; i++) {
|
|
128
|
+
sum += vector[i] * vector[i];
|
|
129
|
+
}
|
|
130
|
+
return Math.sqrt(sum);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Cosine similarity between the query (whose magnitude is precomputed once and
|
|
134
|
+
* reused across the scan) and a stored vector. A zero-magnitude vector on
|
|
135
|
+
* either side yields `0` rather than `NaN` — a degenerate vector is simply
|
|
136
|
+
* maximally dissimilar, not an error.
|
|
137
|
+
*/
|
|
138
|
+
static _cosine(query, queryMagnitude, stored) {
|
|
139
|
+
const storedMagnitude = InMemoryCosineIndex._magnitude(stored);
|
|
140
|
+
if (queryMagnitude === 0 || storedMagnitude === 0) {
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
let dot = 0;
|
|
144
|
+
for (let i = 0; i < query.length; i++) {
|
|
145
|
+
dot += query[i] * stored[i];
|
|
146
|
+
}
|
|
147
|
+
return dot / (queryMagnitude * storedMagnitude);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=inMemoryCosineIndex.js.map
|
|
@@ -0,0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rC,CAAC;IAED,4DAA4D;IACpD,MAAM;QACZ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED,qDAAqD;IAC7C,MAAM,CAAC,UAAU,CAAC,MAAoB;QAC5C,IAAI,GAAG,GAAW,CAAC,CAAC;QACpB,KAAK,IAAI,CAAC,GAAW,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/C,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,OAAO,CAAC,KAAmB,EAAE,cAAsB,EAAE,MAAoB;QACtF,MAAM,eAAe,GAAW,mBAAmB,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACvE,IAAI,cAAc,KAAK,CAAC,IAAI,eAAe,KAAK,CAAC,EAAE,CAAC;YAClD,OAAO,CAAC,CAAC;QACX,CAAC;QACD,IAAI,GAAG,GAAW,CAAC,CAAC;QACpB,KAAK,IAAI,CAAC,GAAW,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9C,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QAC9B,CAAC;QACD,OAAO,GAAG,GAAG,CAAC,cAAc,GAAG,eAAe,CAAC,CAAC;IAClD,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, succeed } from '@fgv/ts-utils';\nimport { IMemoryRecord, MemoryId } from '../types';\nimport { IMemoryRecordSource, IVectorIndex, IVectorQueryHit, MemoryEmbedder } from './vectorIndex';\n\n/**\n * The brute-force, in-memory cosine {@link IVectorIndex}. Stores one\n * `Float32Array` per record and answers a query by computing cosine similarity\n * against every stored vector, returning the top-k by descending score.\n *\n * @remarks\n * This is the **complete** vector implementation for the fgv regime — large-N is\n * explicitly out of scope (the seam stays open for a consumer to swap an external\n * ANN backend once N grows beyond \"thousands of records\"). No external dependency\n * and no ANN structure: a linear scan over a few thousand vectors is well within\n * an interactive budget.\n *\n * The index has a single dimension established by the first vector added; every\n * subsequent `add` and every `query` vector must match that dimension or fail\n * loudly — a mismatched dimension is an embedder-wiring bug, never a silent\n * zero-similarity result. {@link InMemoryCosineIndex.rebuild | rebuild} clears\n * the index, so a re-embed with a different model (hence dimension) is supported.\n *\n * Persistence (a JSON sidecar) is deliberately out of scope for this layer — the\n * index is in-memory and rebuilt from the store via `rebuild`; a sidecar is a\n * future nicety.\n * @public\n */\nexport class InMemoryCosineIndex implements IVectorIndex {\n private readonly _vectors: Map<MemoryId, Float32Array>;\n /** The dimension of every stored vector; `undefined` until the first `add`. */\n private _dimension: number | undefined;\n\n private constructor() {\n this._vectors = new Map<MemoryId, Float32Array>();\n this._dimension = undefined;\n }\n\n /** The number of vectors currently held. */\n public get size(): number {\n return this._vectors.size;\n }\n\n /** Family-convention factory. */\n public static create(): Result<InMemoryCosineIndex> {\n return succeed(new InMemoryCosineIndex());\n }\n\n /** {@inheritDoc IVectorIndex.add} */\n public add(id: MemoryId, vector: Float32Array): Promise<Result<string>> {\n if (vector.length === 0) {\n return Promise.resolve(fail(`vector index: cannot add '${id}': empty vector`));\n }\n if (this._dimension === undefined) {\n this._dimension = vector.length;\n } else if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: cannot add '${id}': dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n // Defensive copy: the caller may reuse or mutate the buffer after `add`, and\n // the index must keep serving the embedding it was given.\n this._vectors.set(id, Float32Array.from(vector));\n // The in-memory index keys entries by id, so the id IS the entry reference.\n return Promise.resolve(succeed(id as string));\n }\n\n /** {@inheritDoc IVectorIndex.remove} */\n public remove(id: MemoryId): Promise<Result<MemoryId>> {\n this._vectors.delete(id);\n return Promise.resolve(succeed(id));\n }\n\n /** {@inheritDoc IVectorIndex.query} */\n public query(vector: Float32Array, topK: number): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {\n if (topK <= 0 || this._vectors.size === 0) {\n return Promise.resolve(succeed([]));\n }\n if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: query dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n const queryMagnitude: number = InMemoryCosineIndex._magnitude(vector);\n const hits: IVectorQueryHit[] = [];\n for (const [id, stored] of this._vectors) {\n hits.push({ id, score: InMemoryCosineIndex._cosine(vector, queryMagnitude, stored) });\n }\n // Descending by score; a `seq`-free tiebreak is unnecessary here because the\n // caller (SemanticRetriever) re-resolves hits against the record index.\n hits.sort((a, b) => b.score - a.score);\n return Promise.resolve(succeed(hits.length > topK ? hits.slice(0, topK) : hits));\n }\n\n /**\n * Re-embed every record from `source` and rebuild the index from scratch.\n * Clears the current contents (and the established dimension) first, so a\n * re-embed with a different model is supported. Returns the number of vectors\n * indexed.\n *\n * On any failure (list, embed, or add) the index is rolled back to empty\n * rather than left in a partially-rebuilt state — a caller that retries a query\n * after a failed rebuild sees a clean empty index, never a half-populated one.\n *\n * @param source - The record source to re-embed (an {@link IMemoryStore}\n * satisfies this structurally).\n * @param embed - The embedder applied to each record.\n */\n public async rebuild(source: IMemoryRecordSource, embed: MemoryEmbedder): Promise<Result<number>> {\n // Reset up front so the \"any failure leaves the index empty\" contract holds\n // even when the listing itself fails (no stale vectors survive a failed\n // rebuild).\n this._reset();\n const listed: Result<ReadonlyArray<IMemoryRecord<unknown>>> = await source.list();\n if (listed.isFailure()) {\n return fail(`vector index rebuild: failed to list records: ${listed.message}`);\n }\n for (const record of listed.value) {\n const embedded: Result<Float32Array> = await embed(record);\n if (embedded.isFailure()) {\n this._reset();\n return fail(`vector index rebuild: embedding '${record.envelope.id}' failed: ${embedded.message}`);\n }\n const added: Result<string> = await this.add(record.envelope.id, embedded.value);\n if (added.isFailure()) {\n this._reset();\n return fail(`vector index rebuild: ${added.message}`);\n }\n }\n return succeed(this._vectors.size);\n }\n\n /** Empty the index and forget the established dimension. */\n private _reset(): void {\n this._vectors.clear();\n this._dimension = undefined;\n }\n\n /** The Euclidean magnitude (L2 norm) of a vector. */\n private static _magnitude(vector: Float32Array): number {\n let sum: number = 0;\n for (let i: number = 0; i < vector.length; i++) {\n sum += vector[i] * vector[i];\n }\n return Math.sqrt(sum);\n }\n\n /**\n * Cosine similarity between the query (whose magnitude is precomputed once and\n * reused across the scan) and a stored vector. A zero-magnitude vector on\n * either side yields `0` rather than `NaN` — a degenerate vector is simply\n * maximally dissimilar, not an error.\n */\n private static _cosine(query: Float32Array, queryMagnitude: number, stored: Float32Array): number {\n const storedMagnitude: number = InMemoryCosineIndex._magnitude(stored);\n if (queryMagnitude === 0 || storedMagnitude === 0) {\n return 0;\n }\n let dot: number = 0;\n for (let i: number = 0; i < query.length; i++) {\n dot += query[i] * stored[i];\n }\n return dot / (queryMagnitude * storedMagnitude);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/vector/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nexport * from './vectorIndex';\nexport * from './inMemoryCosineIndex';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vectorIndex.js","sourceRoot":"","sources":["../../../src/packlets/vector/vectorIndex.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result } from '@fgv/ts-utils';\nimport { IMemoryRecord, MemoryId } from '../types';\n\n/**\n * A single hit returned by {@link IVectorIndex.query}: the matched record id and\n * the backend's similarity score (higher = more similar; the exact scale is\n * backend-defined). Hits are returned in descending score order.\n * @public\n */\nexport interface IVectorQueryHit {\n /** The id of the matched record. */\n readonly id: MemoryId;\n /** Backend similarity score; higher is more similar. */\n readonly score: number;\n}\n\n/**\n * The vector-index seam an embedding backend implements to make\n * {@link SemanticRetriever | semantic recall} operational.\n *\n * @remarks\n * Vectors cross this seam as `Float32Array` (the in-memory representation an\n * embedding model produces); `number[]` is reserved for the JSON-wire edges\n * (e.g. a provider's embedding response). The in-package brute-force cosine\n * implementation is {@link InMemoryCosineIndex}; a consumer can swap an external\n * ANN backend behind the same seam once N grows beyond the in-memory regime.\n *\n * Every operation returns a `Result` (async, since a real backend does I/O) so\n * failure is explicit and never throws across the seam.\n * @public\n */\nexport interface IVectorIndex {\n /**\n * Add (or replace) the embedding for `id`. Returns the opaque\n * {@link IMemoryEnvelope.embeddingRef | embeddingRef} the store stamps onto\n * the envelope so a later read knows the record is embedded.\n */\n add(id: MemoryId, vector: Float32Array): Promise<Result<string>>;\n\n /**\n * Remove the embedding for `id`. Returns the removed id. Idempotent — removing\n * an id with no embedding still succeeds (returns the id).\n */\n remove(id: MemoryId): Promise<Result<MemoryId>>;\n\n /**\n * Return the `topK` nearest records to `vector`, in descending score order.\n */\n query(vector: Float32Array, topK: number): Promise<Result<ReadonlyArray<IVectorQueryHit>>>;\n}\n\n/**\n * Embeds a complete record into a vector for the store's embed-on-write hook.\n * Async and `Result`-returning, since a real embedder does a network call (cloud\n * provider) or in-process model inference. The consumer wires this — the core\n * package never calls an embedding provider directly, staying embedder-agnostic.\n * @public\n */\nexport type MemoryEmbedder = (record: IMemoryRecord<unknown>) => Promise<Result<Float32Array>>;\n\n/**\n * The minimal record-source surface {@link InMemoryCosineIndex.rebuild} reads to\n * re-embed an entire vault. {@link IMemoryStore} satisfies it structurally (its\n * `list` accepts an optional filter, which is assignable to this no-argument\n * shape), so a consumer passes the store directly — without the vector packlet\n * taking a dependency on the store packlet (which depends on the vector packlet\n * for {@link IVectorIndex}, so the reverse import would be a cycle).\n * @public\n */\nexport interface IMemoryRecordSource {\n /** List every record in the vault. */\n list(): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>>;\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
import '@fgv/ts-utils-jest';
|
|
6
|
+
import { Converters } from '@fgv/ts-utils';
|
|
7
|
+
import { JsonSchema } from '@fgv/ts-json-base';
|
|
8
|
+
import { BodyConverterRegistry } from '../../../packlets/converters';
|
|
9
|
+
const knowledgeKind = 'knowledge';
|
|
10
|
+
const profileKind = 'profile';
|
|
11
|
+
describe('BodyConverterRegistry', () => {
|
|
12
|
+
let registry;
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
registry = BodyConverterRegistry.create().orThrow();
|
|
15
|
+
});
|
|
16
|
+
test('create succeeds', () => {
|
|
17
|
+
expect(BodyConverterRegistry.create()).toSucceed();
|
|
18
|
+
});
|
|
19
|
+
describe('register / has / convert', () => {
|
|
20
|
+
test('registers a Converter and converts a matching body', () => {
|
|
21
|
+
registry.register(knowledgeKind, Converters.string);
|
|
22
|
+
expect(registry.has(knowledgeKind)).toBe(true);
|
|
23
|
+
expect(registry.convert(knowledgeKind, 'a knowledge doc')).toSucceedWith('a knowledge doc');
|
|
24
|
+
});
|
|
25
|
+
test('a registered Converter rejects an invalid body', () => {
|
|
26
|
+
registry.register(knowledgeKind, Converters.string);
|
|
27
|
+
expect(registry.convert(knowledgeKind, 42)).toFail();
|
|
28
|
+
});
|
|
29
|
+
test('has returns false for an unregistered kind', () => {
|
|
30
|
+
expect(registry.has(knowledgeKind)).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
test('convert fails for an unregistered kind', () => {
|
|
33
|
+
expect(registry.convert(knowledgeKind, 'x')).toFailWith(/no converter registered for kind 'knowledge'/i);
|
|
34
|
+
});
|
|
35
|
+
test('register replaces a prior registration for the same kind', () => {
|
|
36
|
+
registry.register(knowledgeKind, Converters.string);
|
|
37
|
+
registry.register(knowledgeKind, Converters.number);
|
|
38
|
+
expect(registry.convert(knowledgeKind, 7)).toSucceedWith(7);
|
|
39
|
+
expect(registry.convert(knowledgeKind, 'x')).toFail();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe('getConverter', () => {
|
|
43
|
+
test('returns the registered converter', () => {
|
|
44
|
+
registry.register(knowledgeKind, Converters.string);
|
|
45
|
+
expect(registry.getConverter(knowledgeKind)).toSucceedAndSatisfy((converter) => {
|
|
46
|
+
expect(converter.convert('hello')).toSucceedWith('hello');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
test('fails for an unregistered kind', () => {
|
|
50
|
+
expect(registry.getConverter(knowledgeKind)).toFailWith(/no converter registered for kind/i);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('registerSchema', () => {
|
|
54
|
+
const profileSchema = JsonSchema.object({
|
|
55
|
+
name: JsonSchema.string(),
|
|
56
|
+
age: JsonSchema.integer()
|
|
57
|
+
});
|
|
58
|
+
test('registers a JsonSchema validator and validates a matching body', () => {
|
|
59
|
+
registry.registerSchema(profileKind, profileSchema);
|
|
60
|
+
expect(registry.has(profileKind)).toBe(true);
|
|
61
|
+
expect(registry.convert(profileKind, { name: 'Ada', age: 36 })).toSucceedWith({
|
|
62
|
+
name: 'Ada',
|
|
63
|
+
age: 36
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
test('a registered schema rejects an invalid body', () => {
|
|
67
|
+
registry.registerSchema(profileKind, profileSchema);
|
|
68
|
+
expect(registry.convert(profileKind, { name: 'Ada' })).toFail();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=bodyConverterRegistry.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bodyConverterRegistry.test.js","sourceRoot":"","sources":["../../../../src/test/unit/converters/bodyConverterRegistry.test.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAGrE,MAAM,aAAa,GAAS,WAAmB,CAAC;AAChD,MAAM,WAAW,GAAS,SAAiB,CAAC;AAE5C,QAAQ,CAAC,uBAAuB,EAAE,GAAG,EAAE;IACrC,IAAI,QAA+B,CAAC;IAEpC,UAAU,CAAC,GAAG,EAAE;QACd,QAAQ,GAAG,qBAAqB,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC;IACtD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,iBAAiB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC,qBAAqB,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC;IACrD,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;QACxC,IAAI,CAAC,oDAAoD,EAAE,GAAG,EAAE;YAC9D,QAAQ,CAAC,QAAQ,CAAC,aAAa,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;YACpD,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC/C,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,aAAa,EAAE,iBAAiB,CAAC,CAAC,CAAC,aAAa,CAAC,iBAAiB,CAAC,CAAC;QAC9F,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,gDAAgD,EAAE,GAAG,EAAE;YA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AClE,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport '@fgv/ts-utils-jest';\nimport { Converters } from '@fgv/ts-utils';\nimport { JsonSchema } from '@fgv/ts-json-base';\nimport { BodyConverterRegistry } from '../../../packlets/converters';\nimport { Kind } from '../../../packlets/types';\n\nconst knowledgeKind: Kind = 'knowledge' as Kind;\nconst profileKind: Kind = 'profile' as Kind;\n\ndescribe('BodyConverterRegistry', () => {\n let registry: BodyConverterRegistry;\n\n beforeEach(() => {\n registry = BodyConverterRegistry.create().orThrow();\n });\n\n test('create succeeds', () => {\n expect(BodyConverterRegistry.create()).toSucceed();\n });\n\n describe('register / has / convert', () => {\n test('registers a Converter and converts a matching body', () => {\n registry.register(knowledgeKind, Converters.string);\n expect(registry.has(knowledgeKind)).toBe(true);\n expect(registry.convert(knowledgeKind, 'a knowledge doc')).toSucceedWith('a knowledge doc');\n });\n\n test('a registered Converter rejects an invalid body', () => {\n registry.register(knowledgeKind, Converters.string);\n expect(registry.convert(knowledgeKind, 42)).toFail();\n });\n\n test('has returns false for an unregistered kind', () => {\n expect(registry.has(knowledgeKind)).toBe(false);\n });\n\n test('convert fails for an unregistered kind', () => {\n expect(registry.convert(knowledgeKind, 'x')).toFailWith(\n /no converter registered for kind 'knowledge'/i\n );\n });\n\n test('register replaces a prior registration for the same kind', () => {\n registry.register(knowledgeKind, Converters.string);\n registry.register(knowledgeKind, Converters.number);\n expect(registry.convert(knowledgeKind, 7)).toSucceedWith(7);\n expect(registry.convert(knowledgeKind, 'x')).toFail();\n });\n });\n\n describe('getConverter', () => {\n test('returns the registered converter', () => {\n registry.register(knowledgeKind, Converters.string);\n expect(registry.getConverter(knowledgeKind)).toSucceedAndSatisfy((converter) => {\n expect(converter.convert('hello')).toSucceedWith('hello');\n });\n });\n\n test('fails for an unregistered kind', () => {\n expect(registry.getConverter(knowledgeKind)).toFailWith(/no converter registered for kind/i);\n });\n });\n\n describe('registerSchema', () => {\n const profileSchema = JsonSchema.object({\n name: JsonSchema.string(),\n age: JsonSchema.integer()\n });\n\n test('registers a JsonSchema validator and validates a matching body', () => {\n registry.registerSchema(profileKind, profileSchema);\n expect(registry.has(profileKind)).toBe(true);\n expect(registry.convert(profileKind, { name: 'Ada', age: 36 })).toSucceedWith({\n name: 'Ada',\n age: 36\n });\n });\n\n test('a registered schema rejects an invalid body', () => {\n registry.registerSchema(profileKind, profileSchema);\n expect(registry.convert(profileKind, { name: 'Ada' })).toFail();\n });\n });\n});\n"]}
|