@fgv/ts-agent-memory 5.1.0-41 → 5.1.0-43
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/{cbbdbe09515171b4eba8f2592be2dace1e4e8142.tar.log → 285f03271c27ef724d49e730c0db58d9e4ac1a44.tar.log} +38 -2
- package/.rush/temp/chunked-rush-logs/ts-agent-memory.build.chunks.jsonl +2 -2
- package/.rush/temp/operation/build/all.log +2 -2
- package/.rush/temp/operation/build/log-chunks.jsonl +2 -2
- package/.rush/temp/operation/build/state.json +1 -1
- package/dist/packlets/retrieve/fragmentSemanticRetriever.js +78 -0
- package/dist/packlets/retrieve/fragmentSemanticRetriever.js.map +1 -0
- package/dist/packlets/retrieve/index.js +1 -0
- package/dist/packlets/retrieve/index.js.map +1 -1
- package/dist/packlets/store/fileTreeMemoryStore.js +105 -14
- package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/dist/packlets/vector/inMemoryFragmentCosineIndex.js +200 -0
- package/dist/packlets/vector/inMemoryFragmentCosineIndex.js.map +1 -0
- package/dist/packlets/vector/index.js +1 -0
- package/dist/packlets/vector/index.js.map +1 -1
- package/dist/packlets/vector/vectorIndex.js.map +1 -1
- package/dist/test/unit/retrieve/fragmentSemanticRetriever.test.js +116 -0
- package/dist/test/unit/retrieve/fragmentSemanticRetriever.test.js.map +1 -0
- package/dist/test/unit/store/fragmentEmbedOnWrite.test.js +255 -0
- package/dist/test/unit/store/fragmentEmbedOnWrite.test.js.map +1 -0
- package/dist/test/unit/store/lenientOpen.test.js +248 -0
- package/dist/test/unit/store/lenientOpen.test.js.map +1 -0
- package/dist/test/unit/vector/inMemoryFragmentCosineIndex.test.js +297 -0
- package/dist/test/unit/vector/inMemoryFragmentCosineIndex.test.js.map +1 -0
- package/dist/ts-agent-memory.d.ts +367 -4
- package/etc/ts-agent-memory.api.md +78 -0
- package/lib/packlets/retrieve/fragmentSemanticRetriever.d.ts +90 -0
- package/lib/packlets/retrieve/fragmentSemanticRetriever.d.ts.map +1 -0
- package/lib/packlets/retrieve/fragmentSemanticRetriever.js +82 -0
- package/lib/packlets/retrieve/fragmentSemanticRetriever.js.map +1 -0
- package/lib/packlets/retrieve/index.d.ts +1 -0
- package/lib/packlets/retrieve/index.d.ts.map +1 -1
- package/lib/packlets/retrieve/index.js +1 -0
- package/lib/packlets/retrieve/index.js.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.d.ts +115 -1
- package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.js +104 -13
- package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/lib/packlets/vector/inMemoryFragmentCosineIndex.d.ts +74 -0
- package/lib/packlets/vector/inMemoryFragmentCosineIndex.d.ts.map +1 -0
- package/lib/packlets/vector/inMemoryFragmentCosineIndex.js +204 -0
- package/lib/packlets/vector/inMemoryFragmentCosineIndex.js.map +1 -0
- package/lib/packlets/vector/index.d.ts +1 -0
- package/lib/packlets/vector/index.d.ts.map +1 -1
- package/lib/packlets/vector/index.js +1 -0
- package/lib/packlets/vector/index.js.map +1 -1
- package/lib/packlets/vector/vectorIndex.d.ts +85 -4
- package/lib/packlets/vector/vectorIndex.d.ts.map +1 -1
- package/lib/packlets/vector/vectorIndex.js.map +1 -1
- package/lib/test/unit/retrieve/fragmentSemanticRetriever.test.d.ts +2 -0
- package/lib/test/unit/retrieve/fragmentSemanticRetriever.test.d.ts.map +1 -0
- package/lib/test/unit/retrieve/fragmentSemanticRetriever.test.js +118 -0
- package/lib/test/unit/retrieve/fragmentSemanticRetriever.test.js.map +1 -0
- package/lib/test/unit/store/fragmentEmbedOnWrite.test.d.ts +2 -0
- package/lib/test/unit/store/fragmentEmbedOnWrite.test.d.ts.map +1 -0
- package/lib/test/unit/store/fragmentEmbedOnWrite.test.js +257 -0
- package/lib/test/unit/store/fragmentEmbedOnWrite.test.js.map +1 -0
- package/lib/test/unit/store/lenientOpen.test.d.ts +2 -0
- package/lib/test/unit/store/lenientOpen.test.d.ts.map +1 -0
- package/lib/test/unit/store/lenientOpen.test.js +250 -0
- package/lib/test/unit/store/lenientOpen.test.js.map +1 -0
- package/lib/test/unit/vector/inMemoryFragmentCosineIndex.test.d.ts +2 -0
- package/lib/test/unit/vector/inMemoryFragmentCosineIndex.test.d.ts.map +1 -0
- package/lib/test/unit/vector/inMemoryFragmentCosineIndex.test.js +299 -0
- package/lib/test/unit/vector/inMemoryFragmentCosineIndex.test.js.map +1 -0
- package/package.json +7 -7
- package/rush-logs/ts-agent-memory.build.cache.log +1 -1
- package/rush-logs/ts-agent-memory.build.log +2 -2
- package/src/packlets/retrieve/fragmentSemanticRetriever.ts +135 -0
- package/src/packlets/retrieve/index.ts +1 -0
- package/src/packlets/store/fileTreeMemoryStore.ts +208 -16
- package/src/packlets/vector/inMemoryFragmentCosineIndex.ts +262 -0
- package/src/packlets/vector/index.ts +1 -0
- package/src/packlets/vector/vectorIndex.ts +97 -4
- package/src/test/unit/retrieve/fragmentSemanticRetriever.test.ts +163 -0
- package/src/test/unit/store/fragmentEmbedOnWrite.test.ts +349 -0
- package/src/test/unit/store/lenientOpen.test.ts +292 -0
- package/src/test/unit/vector/inMemoryFragmentCosineIndex.test.ts +389 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +29 -5
- package/temp/build/typescript/ts_8nwakTlr.json +1 -1
- package/temp/ts-agent-memory.api.json +4584 -2859
- package/temp/ts-agent-memory.api.md +78 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lenientOpen.test.js","sourceRoot":"","sources":["../../../../src/test/unit/store/lenientOpen.test.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAEH,8BAA4B;AAC5B,4CAAsF;AACtF,oDAA6C;AAC7C,0CAYwB;AAExB;;;;;;;;GAQG;AAEH,MAAM,QAAQ,GAAS,MAAc,CAAC;AACtC,MAAM,MAAM,GAAW,QAAQ,CAAC;AAEhC;;;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mB,CAAC,MAAM,CAAC;YACvC,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC;YACxB,QAAQ,EAAE,QAAQ,CAAC,qBAAU,CAAC,MAAM,CAAC;YACrC,MAAM;YACN,aAAa,EAAE,MAAM;SACtB,CAAC,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,mBAAmB,CAAC,CAAC,MAA6C,EAAE,EAAE;YAC/F,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;QAC7E,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 { Converter, Converters, Logging, Result, fail, succeed } from '@fgv/ts-utils';\nimport { FileTree } from '@fgv/ts-json-base';\nimport {\n BodyConverterRegistry,\n EntityId,\n FileTreeMemoryStore,\n IBodyConverterRegistry,\n IIdentityCodec,\n IIdentityCodecResult,\n IMemoryRecord,\n ISkippedRecord,\n Kind,\n MemoryScopeKey,\n joinFrontmatter\n} from '../../../index';\n\n/**\n * The incident: one vault holds every kind (mtm, ltm, conversation, knowledge,\n * claim) in ONE store. A required field was added to the `claim` body; every\n * pre-existing `claim` failed validation; and because the initial walk collapsed\n * per-record results with `mapResults` (fail-the-whole-array on any element),\n * every OTHER valid record became unreadable behind the failed open. These tests\n * reproduce that shape (a poison-rejecting body converter standing in for the\n * migrated converter) and pin the lenient (`onRecordError: 'skip'`) behavior.\n */\n\nconst noteKind: Kind = 'note' as Kind;\nconst POISON: string = 'POISON';\n\n/**\n * A permissive identity codec that treats the full `<scope>/<stem>` path as the\n * entity id, so records can live at any nesting depth (the shipped codecs pin a\n * fixed scope shape; this test needs arbitrary-depth scopes to prove the\n * recursion threads skips out of nested subtrees).\n */\nclass PathIdentityCodec implements IIdentityCodec {\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n const value: string = String(entityId);\n const cut: number = value.lastIndexOf('/');\n const scope: MemoryScopeKey = (cut >= 0 ? value.slice(0, cut) : '') as MemoryScopeKey;\n const idStem: string = cut >= 0 ? value.slice(cut + 1) : value;\n return succeed({ scope, idStem, isVersioned: false });\n }\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n return succeed(`${scope}/${encodedStem}` as EntityId);\n }\n public verifyRoundTrip(): Result<true> {\n return succeed(true);\n }\n}\n\n/** A body converter that rejects any body containing the poison marker. */\nconst poisonRejectingConverter: Converter<string> = Converters.string.map((body) =>\n body.includes(POISON) ? fail(`note body rejected: contains ${POISON}`) : succeed(body)\n);\n\nfunction registry(bodyConverter: Converter<string>): IBodyConverterRegistry {\n const created = BodyConverterRegistry.create().orThrow();\n created.register(noteKind, bodyConverter);\n return created;\n}\n\nconst codecs: ReadonlyMap<Kind, IIdentityCodec> = new Map<Kind, IIdentityCodec>([\n [noteKind, new PathIdentityCodec()]\n]);\n\n/** Serialize a note record at `<scope>/<stem>.md` with the given body. */\nfunction noteFile(scope: string, stem: string, body: string): { path: string; contents: string } {\n const envelope = {\n id: stem,\n entityId: `${scope}/${stem}`,\n kind: 'note',\n tags: [],\n links: [],\n created: 1,\n updated: 1,\n seq: 4,\n contentHash: 'seed',\n provenance: { source: 'agent' }\n };\n const frontmatter = Object.entries(envelope)\n .map(([k, v]) => `${k}: ${JSON.stringify(v)}`)\n .join('\\n');\n return { path: `${scope}/${stem}.md`, contents: joinFrontmatter(frontmatter, body) };\n}\n\nfunction mutableRoot(\n files: ReadonlyArray<{ path: string; contents: string }>\n): FileTree.IMutableFileTreeDirectoryItem {\n const tree = FileTree.inMemory([...files], { mutable: true }).orThrow();\n const root = tree.getDirectory('/').orThrow();\n if (!FileTree.isMutableDirectoryItem(root)) {\n throw new Error('expected a mutable root directory');\n }\n return root;\n}\n\ndescribe('FileTreeMemoryStore lenient open (onRecordError)', () => {\n test('incident: one bad record does not make the whole store unopenable in skip mode', async () => {\n const files = [\n noteFile('mtm', 'turn-1', 'valid mtm body'),\n noteFile('ltm', 'summary', 'valid ltm body'),\n noteFile('knowledge', 'doc-a', 'valid knowledge body'),\n // The migrated `claim` converter now rejects the pre-existing record.\n noteFile('claim', 'claim-1', `${POISON} missing required field`)\n ];\n\n // Default (== 'fail') still fails the whole open, exactly as it did before.\n expect(\n FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs\n })\n ).toFailWith(/POISON/);\n\n // 'skip' mode opens: every valid record loads; the bad one is quarantined.\n const store = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n\n expect(await store.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id).sort()).toEqual(['doc-a', 'summary', 'turn-1']);\n });\n expect(store.skippedRecords).toHaveLength(1);\n const skip: ISkippedRecord = store.skippedRecords[0];\n expect(skip.path).toBe('claim/claim-1.md');\n expect(skip.scope).toBe('claim');\n expect(skip.error).toContain('claim/claim-1.md');\n expect(skip.error).toContain(POISON);\n });\n\n test('default mode is byte-identical to explicit fail mode', () => {\n const files = [noteFile('note', 'good', 'ok'), noteFile('note', 'bad', POISON)];\n const asDefault = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs\n });\n const asFail = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'fail'\n });\n expect(asDefault).toFail();\n expect(asFail).toFail();\n // Same exact failure message — the load path is untouched in fail mode.\n expect(asDefault.isFailure() && asFail.isFailure() && asDefault.message === asFail.message).toBe(true);\n expect(asDefault).toFailWith(/POISON/);\n });\n\n test('a bad record in a nested scope is skipped while valid siblings load', async () => {\n const files = [\n noteFile('alpha/beta', 'ok-1', 'valid'),\n noteFile('alpha/beta', 'bad-1', POISON),\n noteFile('alpha/beta/gamma', 'ok-2', 'valid deep')\n ];\n const store = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n\n expect(await store.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id).sort()).toEqual(['ok-1', 'ok-2']);\n });\n expect(store.skippedRecords.map((s) => s.path)).toEqual(['alpha/beta/bad-1.md']);\n expect(store.skippedRecords[0].scope).toBe('alpha/beta');\n });\n\n test('multiple skips across different scopes all surface; all valid records load', async () => {\n const files = [\n noteFile('scope-a', 'good-a', 'a-ok'),\n noteFile('scope-a', 'bad-a', POISON),\n noteFile('scope-b', 'good-b', 'b-ok'),\n noteFile('scope-b', 'bad-b', POISON)\n ];\n const store = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n\n expect(await store.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id).sort()).toEqual(['good-a', 'good-b']);\n });\n expect(store.skippedRecords.map((s) => s.path).sort()).toEqual(['scope-a/bad-a.md', 'scope-b/bad-b.md']);\n });\n\n test('an all-invalid subtree opens with everything skipped (mapSuccess orDefault edge)', async () => {\n const files = [\n noteFile('bad-scope', 'x', POISON),\n noteFile('bad-scope', 'y', POISON),\n noteFile('good-scope', 'z', 'fine')\n ];\n const store = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n\n expect(await store.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id)).toEqual(['z']);\n });\n expect(store.skippedRecords.map((s) => s.path).sort()).toEqual(['bad-scope/x.md', 'bad-scope/y.md']);\n });\n\n test('an empty vault opens with no skips in either mode', async () => {\n const skip = FileTreeMemoryStore.create({\n root: mutableRoot([]),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n expect(await skip.list()).toSucceedWith([]);\n expect(skip.skippedRecords).toEqual([]);\n\n const strict = FileTreeMemoryStore.create({\n root: mutableRoot([]),\n registry: registry(poisonRejectingConverter),\n codecs\n }).orThrow();\n expect(strict.skippedRecords).toEqual([]);\n });\n\n test('each skip is logged at warn; with the default NoOpLogger skippedRecords is still populated', async () => {\n const files = [noteFile('note', 'good', 'ok'), noteFile('note', 'bad', POISON)];\n const logger = new Logging.InMemoryLogger('warning');\n const withLogger = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip',\n logger\n }).orThrow();\n expect(withLogger.skippedRecords).toHaveLength(1);\n expect(logger.logged.some((m) => m.includes('note/bad.md') && m.includes(POISON))).toBe(true);\n\n // No logger wired (default NoOpLogger): create still succeeds and the\n // structured collection is still populated (observable without a logger).\n const silent = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n expect(silent.skippedRecords.map((s) => s.path)).toEqual(['note/bad.md']);\n });\n\n test('re-open after the converter is fixed re-indexes the quarantined record (non-destructive)', async () => {\n const files = [noteFile('note', 'good', 'ok'), noteFile('note', 'later', POISON)];\n const root = mutableRoot(files);\n\n // First open with the rejecting converter: the record is quarantined.\n const before = FileTreeMemoryStore.create({\n root,\n registry: registry(poisonRejectingConverter),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n expect(before.skippedRecords).toHaveLength(1);\n expect(await before.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id)).toEqual(['good']);\n });\n\n // The file was never deleted or mutated, so a fresh open with the fixed\n // (accept-all) converter re-indexes it — quarantine is non-destructive.\n const after = FileTreeMemoryStore.create({\n root: mutableRoot(files),\n registry: registry(Converters.string),\n codecs,\n onRecordError: 'skip'\n }).orThrow();\n expect(after.skippedRecords).toEqual([]);\n expect(await after.list()).toSucceedAndSatisfy((listed: ReadonlyArray<IMemoryRecord<unknown>>) => {\n expect(listed.map((r) => r.envelope.id).sort()).toEqual(['good', 'later']);\n });\n });\n});\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryFragmentCosineIndex.test.d.ts","sourceRoot":"","sources":["../../../../src/test/unit/vector/inMemoryFragmentCosineIndex.test.ts"],"names":[],"mappings":"AAKA,OAAO,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2026 Erik Fortune
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
require("@fgv/ts-utils-jest");
|
|
8
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
9
|
+
const index_1 = require("../../../index");
|
|
10
|
+
/** A scope-qualified target from a `(scope, id)` pair. */
|
|
11
|
+
function target(scope, id) {
|
|
12
|
+
return { scope: scope, id: id };
|
|
13
|
+
}
|
|
14
|
+
/** A locator over an arbitrary `[start, end)` span (opaque to the index). */
|
|
15
|
+
function loc(start, end) {
|
|
16
|
+
return { start, end };
|
|
17
|
+
}
|
|
18
|
+
/** An embedded fragment from a locator + a raw vector. */
|
|
19
|
+
function frag(start, end, vector) {
|
|
20
|
+
return { locator: loc(start, end), vector: Float32Array.from(vector) };
|
|
21
|
+
}
|
|
22
|
+
/** A trivial record carrying just the id + a marker body, for rebuild tests. */
|
|
23
|
+
function record(id, body = `body-${id}`) {
|
|
24
|
+
return {
|
|
25
|
+
envelope: { id: id },
|
|
26
|
+
body
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** A scope-qualified record entry for a rebuild source. */
|
|
30
|
+
function scoped(scope, id, body) {
|
|
31
|
+
return { target: target(scope, id), record: record(id, body) };
|
|
32
|
+
}
|
|
33
|
+
/** A scripted record source for `rebuild`. */
|
|
34
|
+
class FakeSource {
|
|
35
|
+
constructor(result) {
|
|
36
|
+
this._result = result;
|
|
37
|
+
}
|
|
38
|
+
list() {
|
|
39
|
+
return Promise.resolve(this._result);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
describe('InMemoryFragmentCosineIndex', () => {
|
|
43
|
+
describe('addFragments', () => {
|
|
44
|
+
test('stores every fragment and reports the count; tracks record/fragment counts', async () => {
|
|
45
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
46
|
+
expect(index.recordCount).toBe(0);
|
|
47
|
+
expect(index.fragmentCount).toBe(0);
|
|
48
|
+
const t = target('knowledge', 'doc-1');
|
|
49
|
+
expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])).toSucceedWith(2);
|
|
50
|
+
expect(index.recordCount).toBe(1);
|
|
51
|
+
expect(index.fragmentCount).toBe(2);
|
|
52
|
+
});
|
|
53
|
+
test('whole-record replace — a second addFragments drops the prior fragments', async () => {
|
|
54
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
55
|
+
const t = target('knowledge', 'doc-1');
|
|
56
|
+
expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])).toSucceedWith(2);
|
|
57
|
+
// Re-author with a single fragment: the old two must be gone.
|
|
58
|
+
expect(await index.addFragments(t, [frag(0, 3, [1, 1])])).toSucceedWith(1);
|
|
59
|
+
expect(index.recordCount).toBe(1);
|
|
60
|
+
expect(index.fragmentCount).toBe(1);
|
|
61
|
+
expect(await index.query(Float32Array.from([1, 1]), 5)).toSucceedAndSatisfy((hits) => {
|
|
62
|
+
expect(hits).toHaveLength(1);
|
|
63
|
+
expect(hits[0].locator).toEqual(loc(0, 3));
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
test('an empty fragments array drops the record entirely', async () => {
|
|
67
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
68
|
+
const t = target('knowledge', 'doc-1');
|
|
69
|
+
expect(await index.addFragments(t, [frag(0, 5, [1, 0])])).toSucceedWith(1);
|
|
70
|
+
expect(await index.addFragments(t, [])).toSucceedWith(0);
|
|
71
|
+
expect(index.recordCount).toBe(0);
|
|
72
|
+
expect(index.fragmentCount).toBe(0);
|
|
73
|
+
});
|
|
74
|
+
test('same stem in different scopes are distinct entries', async () => {
|
|
75
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
76
|
+
const a = target('conv-a', 'turn-3');
|
|
77
|
+
const b = target('conv-b', 'turn-3');
|
|
78
|
+
expect(await index.addFragments(a, [frag(0, 5, [1, 0])])).toSucceedWith(1);
|
|
79
|
+
expect(await index.addFragments(b, [frag(0, 5, [0, 1])])).toSucceedWith(1);
|
|
80
|
+
expect(index.recordCount).toBe(2);
|
|
81
|
+
expect(await index.query(Float32Array.from([1, 1]), 5)).toSucceedAndSatisfy((hits) => {
|
|
82
|
+
expect(hits).toHaveLength(2);
|
|
83
|
+
expect(hits.map((h) => h.target)).toEqual(expect.arrayContaining([a, b]));
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
test('fails loudly on an empty fragment vector — and does not partially store', async () => {
|
|
87
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
88
|
+
const t = target('knowledge', 'doc-1');
|
|
89
|
+
// First fragment is fine, second is empty: whole-record-replace must be all-or-nothing.
|
|
90
|
+
expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [])])).toFailWith(/empty fragment vector/i);
|
|
91
|
+
expect(index.recordCount).toBe(0);
|
|
92
|
+
expect(index.fragmentCount).toBe(0);
|
|
93
|
+
});
|
|
94
|
+
test('fails loudly on a fragment-dimension mismatch against the established dimension', async () => {
|
|
95
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
96
|
+
expect(await index.addFragments(target('knowledge', 'a'), [frag(0, 5, [1, 0])])).toSucceed();
|
|
97
|
+
expect(await index.addFragments(target('knowledge', 'b'), [frag(0, 5, [1, 0, 0])])).toFailWith(/fragment dimension 3 does not match index dimension 2/i);
|
|
98
|
+
});
|
|
99
|
+
test('a failed multi-fragment add on a fresh index does not establish a dimension (all-or-nothing)', async () => {
|
|
100
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
101
|
+
// Batch: first fragment (dim 2) would set the dimension, second (dim 3) fails
|
|
102
|
+
// the check. The failed add must leave the index wholly dimensionless — not
|
|
103
|
+
// half-committed to dim 2 — so a later legitimate dim-3 add still succeeds.
|
|
104
|
+
expect(await index.addFragments(target('knowledge', 'doc-1'), [frag(0, 5, [1, 0]), frag(5, 10, [1, 0, 0])])).toFailWith(/fragment dimension 3 does not match index dimension 2/i);
|
|
105
|
+
expect(index.recordCount).toBe(0);
|
|
106
|
+
expect(index.fragmentCount).toBe(0);
|
|
107
|
+
// The dimension was never committed: a fresh dim-3 record indexes cleanly.
|
|
108
|
+
expect(await index.addFragments(target('knowledge', 'doc-2'), [frag(0, 5, [1, 0, 0])])).toSucceedWith(1);
|
|
109
|
+
expect(await index.query(Float32Array.from([1, 0, 0]), 1)).toSucceedAndSatisfy((hits) => {
|
|
110
|
+
expect(hits[0].target.id).toBe('doc-2');
|
|
111
|
+
expect(hits[0].score).toBeCloseTo(1);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
test('stores a defensive copy — mutating the caller buffer after add does not corrupt the index', async () => {
|
|
115
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
116
|
+
const buffer = Float32Array.from([1, 0]);
|
|
117
|
+
(await index.addFragments(target('knowledge', 'doc-1'), [{ locator: loc(0, 5), vector: buffer }])).orThrow();
|
|
118
|
+
buffer[0] = 0;
|
|
119
|
+
buffer[1] = 1;
|
|
120
|
+
expect(await index.query(Float32Array.from([1, 0]), 1)).toSucceedAndSatisfy((hits) => {
|
|
121
|
+
expect(hits[0].score).toBeCloseTo(1);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
describe('query', () => {
|
|
126
|
+
async function seeded() {
|
|
127
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
128
|
+
(await index.addFragments(target('knowledge', 'doc-a'), [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])).orThrow();
|
|
129
|
+
(await index.addFragments(target('knowledge', 'doc-b'), [frag(0, 5, [1, 1])])).orThrow();
|
|
130
|
+
return index;
|
|
131
|
+
}
|
|
132
|
+
test('returns fragment hits in descending cosine-similarity order, each carrying its locator', async () => {
|
|
133
|
+
const index = await seeded();
|
|
134
|
+
expect(await index.query(Float32Array.from([1, 0]), 3)).toSucceedAndSatisfy((hits) => {
|
|
135
|
+
// doc-a[0,5] matches [1,0] best (score 1), then doc-b (1/sqrt2), then doc-a[5,10] (0).
|
|
136
|
+
expect(hits[0].target.id).toBe('doc-a');
|
|
137
|
+
expect(hits[0].locator).toEqual(loc(0, 5));
|
|
138
|
+
expect(hits[0].score).toBeCloseTo(1);
|
|
139
|
+
expect(hits[1].target.id).toBe('doc-b');
|
|
140
|
+
expect(hits[1].score).toBeCloseTo(1 / Math.sqrt(2));
|
|
141
|
+
expect(hits[2].target.id).toBe('doc-a');
|
|
142
|
+
expect(hits[2].locator).toEqual(loc(5, 10));
|
|
143
|
+
expect(hits[2].score).toBeCloseTo(0);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
test('truncates to topK', async () => {
|
|
147
|
+
const index = await seeded();
|
|
148
|
+
expect(await index.query(Float32Array.from([1, 0]), 2)).toSucceedAndSatisfy((hits) => {
|
|
149
|
+
expect(hits).toHaveLength(2);
|
|
150
|
+
expect(hits.map((h) => h.locator)).toEqual([loc(0, 5), loc(0, 5)]);
|
|
151
|
+
expect(hits[0].target.id).toBe('doc-a');
|
|
152
|
+
expect(hits[1].target.id).toBe('doc-b');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
test('maxPerRecord caps fragments per record during selection (before the topK cut)', async () => {
|
|
156
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
157
|
+
// doc-a has three fragments that all beat doc-b's single fragment.
|
|
158
|
+
(await index.addFragments(target('knowledge', 'doc-a'), [
|
|
159
|
+
frag(0, 5, [1, 0]),
|
|
160
|
+
frag(5, 10, [0.9, 0.1]),
|
|
161
|
+
frag(10, 15, [0.8, 0.2])
|
|
162
|
+
])).orThrow();
|
|
163
|
+
(await index.addFragments(target('knowledge', 'doc-b'), [frag(0, 5, [0.7, 0.3])])).orThrow();
|
|
164
|
+
// Without a cap, top-2 would be doc-a twice. With maxPerRecord=1, doc-b surfaces.
|
|
165
|
+
expect(await index.query(Float32Array.from([1, 0]), 2, 1)).toSucceedAndSatisfy((hits) => {
|
|
166
|
+
expect(hits).toHaveLength(2);
|
|
167
|
+
expect(hits.map((h) => h.target.id)).toEqual(['doc-a', 'doc-b']);
|
|
168
|
+
expect(hits[0].locator).toEqual(loc(0, 5));
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
test('maxPerRecord=0 yields no hits', async () => {
|
|
172
|
+
const index = await seeded();
|
|
173
|
+
expect(await index.query(Float32Array.from([1, 0]), 5, 0)).toSucceedWith([]);
|
|
174
|
+
});
|
|
175
|
+
test('maxPerRecord larger than any record leaves the ranking unchanged', async () => {
|
|
176
|
+
const index = await seeded();
|
|
177
|
+
expect(await index.query(Float32Array.from([1, 0]), 5, 10)).toSucceedAndSatisfy((hits) => {
|
|
178
|
+
expect(hits).toHaveLength(3);
|
|
179
|
+
expect(hits[0].target.id).toBe('doc-a');
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
test('returns empty for a non-positive topK', async () => {
|
|
183
|
+
const index = await seeded();
|
|
184
|
+
expect(await index.query(Float32Array.from([1, 0]), 0)).toSucceedWith([]);
|
|
185
|
+
expect(await index.query(Float32Array.from([1, 0]), -1)).toSucceedWith([]);
|
|
186
|
+
});
|
|
187
|
+
test('returns empty when the index is empty (no dimension check)', async () => {
|
|
188
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
189
|
+
expect(await index.query(Float32Array.from([1, 2, 3, 4]), 5)).toSucceedWith([]);
|
|
190
|
+
});
|
|
191
|
+
test('fails loudly on a query-dimension mismatch', async () => {
|
|
192
|
+
const index = await seeded();
|
|
193
|
+
expect(await index.query(Float32Array.from([1, 0, 0]), 3)).toFailWith(/query dimension 3 does not match index dimension 2/i);
|
|
194
|
+
});
|
|
195
|
+
test('scores a degenerate (zero-magnitude) stored fragment as 0 rather than NaN', async () => {
|
|
196
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
197
|
+
(await index.addFragments(target('knowledge', 'doc'), [frag(0, 5, [0, 0]), frag(5, 10, [1, 0])])).orThrow();
|
|
198
|
+
expect(await index.query(Float32Array.from([1, 0]), 2)).toSucceedAndSatisfy((hits) => {
|
|
199
|
+
expect(hits.map((h) => h.locator)).toEqual([loc(5, 10), loc(0, 5)]);
|
|
200
|
+
expect(hits[1].score).toBe(0);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
test('scores against a degenerate (zero-magnitude) query as 0 rather than NaN', async () => {
|
|
204
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
205
|
+
(await index.addFragments(target('knowledge', 'doc'), [frag(0, 5, [1, 0])])).orThrow();
|
|
206
|
+
expect(await index.query(Float32Array.from([0, 0]), 1)).toSucceedAndSatisfy((hits) => {
|
|
207
|
+
expect(hits[0].score).toBe(0);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
describe('remove', () => {
|
|
212
|
+
test('removes every fragment of a record and is reflected in subsequent queries', async () => {
|
|
213
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
214
|
+
const a = target('knowledge', 'doc-a');
|
|
215
|
+
const b = target('knowledge', 'doc-b');
|
|
216
|
+
(await index.addFragments(a, [frag(0, 5, [1, 0]), frag(5, 10, [1, 0])])).orThrow();
|
|
217
|
+
(await index.addFragments(b, [frag(0, 5, [0, 1])])).orThrow();
|
|
218
|
+
expect(await index.remove(a)).toSucceedWith(a);
|
|
219
|
+
expect(index.recordCount).toBe(1);
|
|
220
|
+
expect(index.fragmentCount).toBe(1);
|
|
221
|
+
expect(await index.query(Float32Array.from([1, 0]), 5)).toSucceedAndSatisfy((hits) => {
|
|
222
|
+
expect(hits.map((h) => h.target.id)).toEqual(['doc-b']);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
test('removes only the scoped target — a same-stem record in another scope is left intact', async () => {
|
|
226
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
227
|
+
const a = target('conv-a', 'turn-3');
|
|
228
|
+
const b = target('conv-b', 'turn-3');
|
|
229
|
+
(await index.addFragments(a, [frag(0, 5, [1, 0])])).orThrow();
|
|
230
|
+
(await index.addFragments(b, [frag(0, 5, [0, 1])])).orThrow();
|
|
231
|
+
expect(await index.remove(a)).toSucceedWith(a);
|
|
232
|
+
expect(index.recordCount).toBe(1);
|
|
233
|
+
expect(await index.query(Float32Array.from([0, 1]), 5)).toSucceedAndSatisfy((hits) => {
|
|
234
|
+
expect(hits).toHaveLength(1);
|
|
235
|
+
expect(hits[0].target).toEqual(b);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
test('is idempotent — removing an absent target still succeeds', async () => {
|
|
239
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
240
|
+
const t = target('knowledge', 'missing');
|
|
241
|
+
expect(await index.remove(t)).toSucceedWith(t);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
describe('rebuild', () => {
|
|
245
|
+
// Deterministic: each record yields two fragments encoding the id's first char code.
|
|
246
|
+
const embed = (r) => {
|
|
247
|
+
const code = r.envelope.id.charCodeAt(0);
|
|
248
|
+
return Promise.resolve((0, ts_utils_1.succeed)([frag(0, 5, [code, 1]), frag(5, 10, [1, code])]));
|
|
249
|
+
};
|
|
250
|
+
test('re-embeds every scoped record and reports the total fragment count', async () => {
|
|
251
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
252
|
+
const source = new FakeSource((0, ts_utils_1.succeed)([scoped('knowledge', 'a'), scoped('knowledge', 'b')]));
|
|
253
|
+
expect(await index.rebuild(source, embed)).toSucceedWith(4);
|
|
254
|
+
expect(index.recordCount).toBe(2);
|
|
255
|
+
expect(index.fragmentCount).toBe(4);
|
|
256
|
+
});
|
|
257
|
+
test('keeps same-stem records under different scopes distinct across a rebuild', async () => {
|
|
258
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
259
|
+
const source = new FakeSource((0, ts_utils_1.succeed)([scoped('conv-a', 'turn-3'), scoped('conv-b', 'turn-3')]));
|
|
260
|
+
expect(await index.rebuild(source, embed)).toSucceedWith(4);
|
|
261
|
+
expect(index.recordCount).toBe(2);
|
|
262
|
+
});
|
|
263
|
+
test('clears prior contents and re-establishes the dimension', async () => {
|
|
264
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
265
|
+
(await index.addFragments(target('knowledge', 'old'), [frag(0, 5, [1, 2, 3])])).orThrow();
|
|
266
|
+
const source = new FakeSource((0, ts_utils_1.succeed)([scoped('knowledge', 'a')]));
|
|
267
|
+
expect(await index.rebuild(source, embed)).toSucceedWith(2);
|
|
268
|
+
expect(index.recordCount).toBe(1);
|
|
269
|
+
expect(await index.query(Float32Array.from([97, 1]), 1)).toSucceed();
|
|
270
|
+
});
|
|
271
|
+
test('fails loudly when the source list fails and rolls back to empty', async () => {
|
|
272
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
273
|
+
(await index.addFragments(target('knowledge', 'seed'), [frag(0, 5, [1, 1])])).orThrow();
|
|
274
|
+
const source = new FakeSource((0, ts_utils_1.fail)('disk gone'));
|
|
275
|
+
expect(await index.rebuild(source, embed)).toFailWith(/failed to list records: disk gone/i);
|
|
276
|
+
expect(index.recordCount).toBe(0);
|
|
277
|
+
});
|
|
278
|
+
test('fails loudly and rolls back to empty when an embedding fails mid-rebuild', async () => {
|
|
279
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
280
|
+
(await index.addFragments(target('knowledge', 'seed'), [frag(0, 5, [1, 1])])).orThrow();
|
|
281
|
+
let calls = 0;
|
|
282
|
+
const flakyEmbed = () => {
|
|
283
|
+
calls += 1;
|
|
284
|
+
return Promise.resolve(calls === 1 ? (0, ts_utils_1.succeed)([frag(0, 5, [1, 1])]) : (0, ts_utils_1.fail)('no model'));
|
|
285
|
+
};
|
|
286
|
+
const source = new FakeSource((0, ts_utils_1.succeed)([scoped('conv-a', 'turn-1'), scoped('conv-b', 'turn-1')]));
|
|
287
|
+
expect(await index.rebuild(source, flakyEmbed)).toFailWith(/embedding 'conv-b\0turn-1' failed: no model/i);
|
|
288
|
+
expect(index.recordCount).toBe(0);
|
|
289
|
+
});
|
|
290
|
+
test('fails loudly and rolls back to empty when adding an embedded fragment fails', async () => {
|
|
291
|
+
const index = index_1.InMemoryFragmentCosineIndex.create().orThrow();
|
|
292
|
+
const source = new FakeSource((0, ts_utils_1.succeed)([scoped('knowledge', 'a')]));
|
|
293
|
+
const emptyEmbed = () => Promise.resolve((0, ts_utils_1.succeed)([frag(0, 5, [])]));
|
|
294
|
+
expect(await index.rebuild(source, emptyEmbed)).toFailWith(/empty fragment vector/i);
|
|
295
|
+
expect(index.recordCount).toBe(0);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
//# sourceMappingURL=inMemoryFragmentCosineIndex.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryFragmentCosineIndex.test.js","sourceRoot":"","sources":["../../../../src/test/unit/vector/inMemoryFragmentCosineIndex.test.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAEH,8BAA4B;AAC5B,4CAAsD;AACtD,0CAWwB;AAExB,0DAA0D;AAC1D,SAAS,MAAM,CAAC,KAAa,EAAE,EAAU;IACvC,OAAO,EAAE,KAAK,EAAE,KAAuB,EAAE,EAAE,EAAE,EAAc,EAAE,CAAC;AAChE,CAAC;AAED,6EAA6E;AAC7E,SAAS,GAAG,CAAC,KAAa,EAAE,GAAW;IACrC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,0DAA0D;AAC1D,SAAS,IAAI,CAAC,KAAa,EAAE,GAAW,EAAE,MAAgB;IACxD,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;AACzE,CAAC;AAED,gFAAgF;AAChF,SAAS,MAAM,CAAC,EAAU,EAAE,OAAe,QAAQ,EAAE,EAAE;IACrD,OAAO;QACL,QAAQ,EAAE,EAAE,EAAE,EAAE,EAAc,EAAwC;QACtE,IAAI;KACL,CAAC;AACJ,CAAC;AAED,2DAA2D;AAC3D,SAAS,MAAM,CAAC,KAAa,EAAE,EAAU,EAAE,IAAa;IACtD,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC;AACjE,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU;IA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,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,UAAU,CACxD,8CAA8C,CAC/C,CAAC;YACF,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,6EAA6E,EAAE,KAAK,IAAI,EAAE;YAC7F,MAAM,KAAK,GAAG,mCAA2B,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC;YAC7D,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAA,kBAAO,EAAC,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YACnE,MAAM,UAAU,GAAG,GAAsD,EAAE,CACzE,OAAO,CAAC,OAAO,CAAC,IAAA,kBAAO,EAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YAC7C,MAAM,CAAC,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAC;YACrF,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpC,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 { Result, fail, succeed } from '@fgv/ts-utils';\nimport {\n IEdgeTarget,\n IEmbeddedFragment,\n IFragmentLocator,\n IMemoryRecord,\n IMemoryRecordSource,\n IScopedMemoryRecord,\n IVectorQueryHit,\n InMemoryFragmentCosineIndex,\n MemoryId,\n MemoryScopeKey\n} from '../../../index';\n\n/** A scope-qualified target from a `(scope, id)` pair. */\nfunction target(scope: string, id: string): IEdgeTarget {\n return { scope: scope as MemoryScopeKey, id: id as MemoryId };\n}\n\n/** A locator over an arbitrary `[start, end)` span (opaque to the index). */\nfunction loc(start: number, end: number): IFragmentLocator {\n return { start, end };\n}\n\n/** An embedded fragment from a locator + a raw vector. */\nfunction frag(start: number, end: number, vector: number[]): IEmbeddedFragment {\n return { locator: loc(start, end), vector: Float32Array.from(vector) };\n}\n\n/** A trivial record carrying just the id + a marker body, for rebuild tests. */\nfunction record(id: string, body: string = `body-${id}`): IMemoryRecord<unknown> {\n return {\n envelope: { id: id as MemoryId } as IMemoryRecord<unknown>['envelope'],\n body\n };\n}\n\n/** A scope-qualified record entry for a rebuild source. */\nfunction scoped(scope: string, id: string, body?: string): IScopedMemoryRecord {\n return { target: target(scope, id), record: record(id, body) };\n}\n\n/** A scripted record source for `rebuild`. */\nclass FakeSource implements IMemoryRecordSource {\n private readonly _result: Result<ReadonlyArray<IScopedMemoryRecord>>;\n public constructor(result: Result<ReadonlyArray<IScopedMemoryRecord>>) {\n this._result = result;\n }\n public list(): Promise<Result<ReadonlyArray<IScopedMemoryRecord>>> {\n return Promise.resolve(this._result);\n }\n}\n\ndescribe('InMemoryFragmentCosineIndex', () => {\n describe('addFragments', () => {\n test('stores every fragment and reports the count; tracks record/fragment counts', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n expect(index.recordCount).toBe(0);\n expect(index.fragmentCount).toBe(0);\n const t = target('knowledge', 'doc-1');\n expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])).toSucceedWith(2);\n expect(index.recordCount).toBe(1);\n expect(index.fragmentCount).toBe(2);\n });\n\n test('whole-record replace — a second addFragments drops the prior fragments', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const t = target('knowledge', 'doc-1');\n expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])).toSucceedWith(2);\n // Re-author with a single fragment: the old two must be gone.\n expect(await index.addFragments(t, [frag(0, 3, [1, 1])])).toSucceedWith(1);\n expect(index.recordCount).toBe(1);\n expect(index.fragmentCount).toBe(1);\n expect(await index.query(Float32Array.from([1, 1]), 5)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(1);\n expect(hits[0].locator).toEqual(loc(0, 3));\n }\n );\n });\n\n test('an empty fragments array drops the record entirely', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const t = target('knowledge', 'doc-1');\n expect(await index.addFragments(t, [frag(0, 5, [1, 0])])).toSucceedWith(1);\n expect(await index.addFragments(t, [])).toSucceedWith(0);\n expect(index.recordCount).toBe(0);\n expect(index.fragmentCount).toBe(0);\n });\n\n test('same stem in different scopes are distinct entries', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const a = target('conv-a', 'turn-3');\n const b = target('conv-b', 'turn-3');\n expect(await index.addFragments(a, [frag(0, 5, [1, 0])])).toSucceedWith(1);\n expect(await index.addFragments(b, [frag(0, 5, [0, 1])])).toSucceedWith(1);\n expect(index.recordCount).toBe(2);\n expect(await index.query(Float32Array.from([1, 1]), 5)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(2);\n expect(hits.map((h) => h.target)).toEqual(expect.arrayContaining([a, b]));\n }\n );\n });\n\n test('fails loudly on an empty fragment vector — and does not partially store', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const t = target('knowledge', 'doc-1');\n // First fragment is fine, second is empty: whole-record-replace must be all-or-nothing.\n expect(await index.addFragments(t, [frag(0, 5, [1, 0]), frag(5, 10, [])])).toFailWith(\n /empty fragment vector/i\n );\n expect(index.recordCount).toBe(0);\n expect(index.fragmentCount).toBe(0);\n });\n\n test('fails loudly on a fragment-dimension mismatch against the established dimension', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n expect(await index.addFragments(target('knowledge', 'a'), [frag(0, 5, [1, 0])])).toSucceed();\n expect(await index.addFragments(target('knowledge', 'b'), [frag(0, 5, [1, 0, 0])])).toFailWith(\n /fragment dimension 3 does not match index dimension 2/i\n );\n });\n\n test('a failed multi-fragment add on a fresh index does not establish a dimension (all-or-nothing)', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n // Batch: first fragment (dim 2) would set the dimension, second (dim 3) fails\n // the check. The failed add must leave the index wholly dimensionless — not\n // half-committed to dim 2 — so a later legitimate dim-3 add still succeeds.\n expect(\n await index.addFragments(target('knowledge', 'doc-1'), [frag(0, 5, [1, 0]), frag(5, 10, [1, 0, 0])])\n ).toFailWith(/fragment dimension 3 does not match index dimension 2/i);\n expect(index.recordCount).toBe(0);\n expect(index.fragmentCount).toBe(0);\n // The dimension was never committed: a fresh dim-3 record indexes cleanly.\n expect(await index.addFragments(target('knowledge', 'doc-2'), [frag(0, 5, [1, 0, 0])])).toSucceedWith(\n 1\n );\n expect(await index.query(Float32Array.from([1, 0, 0]), 1)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits[0].target.id).toBe('doc-2');\n expect(hits[0].score).toBeCloseTo(1);\n }\n );\n });\n\n test('stores a defensive copy — mutating the caller buffer after add does not corrupt the index', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const buffer = Float32Array.from([1, 0]);\n (\n await index.addFragments(target('knowledge', 'doc-1'), [{ locator: loc(0, 5), vector: buffer }])\n ).orThrow();\n buffer[0] = 0;\n buffer[1] = 1;\n expect(await index.query(Float32Array.from([1, 0]), 1)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits[0].score).toBeCloseTo(1);\n }\n );\n });\n });\n\n describe('query', () => {\n async function seeded(): Promise<InMemoryFragmentCosineIndex> {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (\n await index.addFragments(target('knowledge', 'doc-a'), [frag(0, 5, [1, 0]), frag(5, 10, [0, 1])])\n ).orThrow();\n (await index.addFragments(target('knowledge', 'doc-b'), [frag(0, 5, [1, 1])])).orThrow();\n return index;\n }\n\n test('returns fragment hits in descending cosine-similarity order, each carrying its locator', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0]), 3)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n // doc-a[0,5] matches [1,0] best (score 1), then doc-b (1/sqrt2), then doc-a[5,10] (0).\n expect(hits[0].target.id).toBe('doc-a');\n expect(hits[0].locator).toEqual(loc(0, 5));\n expect(hits[0].score).toBeCloseTo(1);\n expect(hits[1].target.id).toBe('doc-b');\n expect(hits[1].score).toBeCloseTo(1 / Math.sqrt(2));\n expect(hits[2].target.id).toBe('doc-a');\n expect(hits[2].locator).toEqual(loc(5, 10));\n expect(hits[2].score).toBeCloseTo(0);\n }\n );\n });\n\n test('truncates to topK', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0]), 2)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(2);\n expect(hits.map((h) => h.locator)).toEqual([loc(0, 5), loc(0, 5)]);\n expect(hits[0].target.id).toBe('doc-a');\n expect(hits[1].target.id).toBe('doc-b');\n }\n );\n });\n\n test('maxPerRecord caps fragments per record during selection (before the topK cut)', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n // doc-a has three fragments that all beat doc-b's single fragment.\n (\n await index.addFragments(target('knowledge', 'doc-a'), [\n frag(0, 5, [1, 0]),\n frag(5, 10, [0.9, 0.1]),\n frag(10, 15, [0.8, 0.2])\n ])\n ).orThrow();\n (await index.addFragments(target('knowledge', 'doc-b'), [frag(0, 5, [0.7, 0.3])])).orThrow();\n // Without a cap, top-2 would be doc-a twice. With maxPerRecord=1, doc-b surfaces.\n expect(await index.query(Float32Array.from([1, 0]), 2, 1)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(2);\n expect(hits.map((h) => h.target.id)).toEqual(['doc-a', 'doc-b']);\n expect(hits[0].locator).toEqual(loc(0, 5));\n }\n );\n });\n\n test('maxPerRecord=0 yields no hits', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0]), 5, 0)).toSucceedWith([]);\n });\n\n test('maxPerRecord larger than any record leaves the ranking unchanged', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0]), 5, 10)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(3);\n expect(hits[0].target.id).toBe('doc-a');\n }\n );\n });\n\n test('returns empty for a non-positive topK', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0]), 0)).toSucceedWith([]);\n expect(await index.query(Float32Array.from([1, 0]), -1)).toSucceedWith([]);\n });\n\n test('returns empty when the index is empty (no dimension check)', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n expect(await index.query(Float32Array.from([1, 2, 3, 4]), 5)).toSucceedWith([]);\n });\n\n test('fails loudly on a query-dimension mismatch', async () => {\n const index = await seeded();\n expect(await index.query(Float32Array.from([1, 0, 0]), 3)).toFailWith(\n /query dimension 3 does not match index dimension 2/i\n );\n });\n\n test('scores a degenerate (zero-magnitude) stored fragment as 0 rather than NaN', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (\n await index.addFragments(target('knowledge', 'doc'), [frag(0, 5, [0, 0]), frag(5, 10, [1, 0])])\n ).orThrow();\n expect(await index.query(Float32Array.from([1, 0]), 2)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits.map((h) => h.locator)).toEqual([loc(5, 10), loc(0, 5)]);\n expect(hits[1].score).toBe(0);\n }\n );\n });\n\n test('scores against a degenerate (zero-magnitude) query as 0 rather than NaN', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (await index.addFragments(target('knowledge', 'doc'), [frag(0, 5, [1, 0])])).orThrow();\n expect(await index.query(Float32Array.from([0, 0]), 1)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits[0].score).toBe(0);\n }\n );\n });\n });\n\n describe('remove', () => {\n test('removes every fragment of a record and is reflected in subsequent queries', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const a = target('knowledge', 'doc-a');\n const b = target('knowledge', 'doc-b');\n (await index.addFragments(a, [frag(0, 5, [1, 0]), frag(5, 10, [1, 0])])).orThrow();\n (await index.addFragments(b, [frag(0, 5, [0, 1])])).orThrow();\n expect(await index.remove(a)).toSucceedWith(a);\n expect(index.recordCount).toBe(1);\n expect(index.fragmentCount).toBe(1);\n expect(await index.query(Float32Array.from([1, 0]), 5)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits.map((h) => h.target.id)).toEqual(['doc-b']);\n }\n );\n });\n\n test('removes only the scoped target — a same-stem record in another scope is left intact', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const a = target('conv-a', 'turn-3');\n const b = target('conv-b', 'turn-3');\n (await index.addFragments(a, [frag(0, 5, [1, 0])])).orThrow();\n (await index.addFragments(b, [frag(0, 5, [0, 1])])).orThrow();\n expect(await index.remove(a)).toSucceedWith(a);\n expect(index.recordCount).toBe(1);\n expect(await index.query(Float32Array.from([0, 1]), 5)).toSucceedAndSatisfy(\n (hits: ReadonlyArray<IVectorQueryHit>) => {\n expect(hits).toHaveLength(1);\n expect(hits[0].target).toEqual(b);\n }\n );\n });\n\n test('is idempotent — removing an absent target still succeeds', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const t = target('knowledge', 'missing');\n expect(await index.remove(t)).toSucceedWith(t);\n });\n });\n\n describe('rebuild', () => {\n // Deterministic: each record yields two fragments encoding the id's first char code.\n const embed = (r: IMemoryRecord<unknown>): Promise<Result<ReadonlyArray<IEmbeddedFragment>>> => {\n const code: number = (r.envelope.id as string).charCodeAt(0);\n return Promise.resolve(succeed([frag(0, 5, [code, 1]), frag(5, 10, [1, code])]));\n };\n\n test('re-embeds every scoped record and reports the total fragment count', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const source = new FakeSource(succeed([scoped('knowledge', 'a'), scoped('knowledge', 'b')]));\n expect(await index.rebuild(source, embed)).toSucceedWith(4);\n expect(index.recordCount).toBe(2);\n expect(index.fragmentCount).toBe(4);\n });\n\n test('keeps same-stem records under different scopes distinct across a rebuild', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const source = new FakeSource(succeed([scoped('conv-a', 'turn-3'), scoped('conv-b', 'turn-3')]));\n expect(await index.rebuild(source, embed)).toSucceedWith(4);\n expect(index.recordCount).toBe(2);\n });\n\n test('clears prior contents and re-establishes the dimension', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (await index.addFragments(target('knowledge', 'old'), [frag(0, 5, [1, 2, 3])])).orThrow();\n const source = new FakeSource(succeed([scoped('knowledge', 'a')]));\n expect(await index.rebuild(source, embed)).toSucceedWith(2);\n expect(index.recordCount).toBe(1);\n expect(await index.query(Float32Array.from([97, 1]), 1)).toSucceed();\n });\n\n test('fails loudly when the source list fails and rolls back to empty', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (await index.addFragments(target('knowledge', 'seed'), [frag(0, 5, [1, 1])])).orThrow();\n const source = new FakeSource(fail('disk gone'));\n expect(await index.rebuild(source, embed)).toFailWith(/failed to list records: disk gone/i);\n expect(index.recordCount).toBe(0);\n });\n\n test('fails loudly and rolls back to empty when an embedding fails mid-rebuild', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n (await index.addFragments(target('knowledge', 'seed'), [frag(0, 5, [1, 1])])).orThrow();\n let calls: number = 0;\n const flakyEmbed = (): Promise<Result<ReadonlyArray<IEmbeddedFragment>>> => {\n calls += 1;\n return Promise.resolve(calls === 1 ? succeed([frag(0, 5, [1, 1])]) : fail('no model'));\n };\n const source = new FakeSource(succeed([scoped('conv-a', 'turn-1'), scoped('conv-b', 'turn-1')]));\n expect(await index.rebuild(source, flakyEmbed)).toFailWith(\n /embedding 'conv-b\\0turn-1' failed: no model/i\n );\n expect(index.recordCount).toBe(0);\n });\n\n test('fails loudly and rolls back to empty when adding an embedded fragment fails', async () => {\n const index = InMemoryFragmentCosineIndex.create().orThrow();\n const source = new FakeSource(succeed([scoped('knowledge', 'a')]));\n const emptyEmbed = (): Promise<Result<ReadonlyArray<IEmbeddedFragment>>> =>\n Promise.resolve(succeed([frag(0, 5, [])]));\n expect(await index.rebuild(source, emptyEmbed)).toFailWith(/empty fragment vector/i);\n expect(index.recordCount).toBe(0);\n });\n });\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-agent-memory",
|
|
3
|
-
"version": "5.1.0-
|
|
3
|
+
"version": "5.1.0-43",
|
|
4
4
|
"description": "FileTree-backed storage and retrieval substrate for agent memory and knowledge",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "dist/ts-agent-memory.d.ts",
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"tslib": "^2.8.1",
|
|
38
|
-
"@fgv/ts-extras": "5.1.0-
|
|
39
|
-
"@fgv/ts-
|
|
40
|
-
"@fgv/ts-json-base": "5.1.0-
|
|
41
|
-
"@fgv/ts-
|
|
38
|
+
"@fgv/ts-extras": "5.1.0-43",
|
|
39
|
+
"@fgv/ts-utils": "5.1.0-43",
|
|
40
|
+
"@fgv/ts-json-base": "5.1.0-43",
|
|
41
|
+
"@fgv/ts-json": "5.1.0-43"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@microsoft/api-extractor": "^7.55.2",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
"@rushstack/heft-node-rig": "2.11.27",
|
|
66
66
|
"@types/heft-jest": "1.0.6",
|
|
67
67
|
"eslint-plugin-tsdoc": "~0.5.2",
|
|
68
|
-
"@fgv/
|
|
69
|
-
"@fgv/
|
|
68
|
+
"@fgv/heft-dual-rig": "5.1.0-43",
|
|
69
|
+
"@fgv/ts-utils-jest": "5.1.0-43"
|
|
70
70
|
},
|
|
71
71
|
"scripts": {
|
|
72
72
|
"build": "heft build --clean",
|
|
@@ -5,5 +5,5 @@ Invoking: heft build --clean
|
|
|
5
5
|
[build:lint] Using ESLint version 9.39.5
|
|
6
6
|
[build:api-extractor] Using API Extractor version 7.58.9
|
|
7
7
|
[build:api-extractor] Analysis will use the bundled TypeScript version 5.9.3
|
|
8
|
-
---- build finished (26.
|
|
9
|
-
-------------------- Finished (26.
|
|
8
|
+
---- build finished (26.514s) ----
|
|
9
|
+
-------------------- Finished (26.523s) --------------------
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Result, fail, succeed } from '@fgv/ts-utils';
|
|
7
|
+
import { IFragmentVectorIndex, IVectorQueryHit } from '../vector';
|
|
8
|
+
import { QueryEmbedder } from './semanticRetriever';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The loud-degradation message a {@link FragmentSemanticRetriever} returns when a
|
|
12
|
+
* fragment query is issued but no {@link IFragmentSemanticBackend | backend} is
|
|
13
|
+
* wired — the discovery surface NEVER answers a fragment query with a silent empty.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export const FRAGMENT_SEMANTIC_UNWIRED_MESSAGE: string =
|
|
17
|
+
'fragment recall: no fragment index is wired; wire an IFragmentSemanticBackend to enable sub-document search';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The fragment backend wired into a {@link FragmentSemanticRetriever}: the fragment
|
|
21
|
+
* index to query and the embedder that turns the query text into a vector. Both are
|
|
22
|
+
* required together — a fragment index is useless without a way to embed the query.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export interface IFragmentSemanticBackend {
|
|
26
|
+
/** The fragment-granular vector index to query. */
|
|
27
|
+
readonly fragmentIndex: IFragmentVectorIndex;
|
|
28
|
+
/** Turns the query text into a vector. */
|
|
29
|
+
readonly embedQuery: QueryEmbedder;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A sub-document semantic-search request: the natural-language `semantic` text to
|
|
34
|
+
* match, an optional `topK` result cap (default 10), and an optional
|
|
35
|
+
* `maxPerRecord` cap that keeps one long document from monopolizing the result.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export interface IFragmentQuery {
|
|
39
|
+
/** The natural-language text to embed and match against stored fragments. */
|
|
40
|
+
readonly semantic: string;
|
|
41
|
+
/** Maximum number of fragment hits to return. Defaults to 10. */
|
|
42
|
+
readonly topK?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Maximum number of fragments any single record may contribute to the result.
|
|
45
|
+
* Applied during selection (before the `topK` cut). Omit for uncapped.
|
|
46
|
+
*/
|
|
47
|
+
readonly maxPerRecord?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What a {@link FragmentSemanticRetriever} can do given its wiring.
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export interface IFragmentRetrieverCapabilities {
|
|
55
|
+
/** `true` when a fragment backend is wired and fragment recall is operational. */
|
|
56
|
+
readonly supportsFragmentRecall: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The sub-document semantic-search retriever — the "discovery" half of a
|
|
61
|
+
* search-then-read contract. It embeds a fragment query, queries the
|
|
62
|
+
* {@link IFragmentVectorIndex}, and returns the raw per-fragment
|
|
63
|
+
* {@link IVectorQueryHit | hits} (each carrying a record `target` AND the matched
|
|
64
|
+
* `locator`), NOT resolved records: the consumer re-reads each record and slices it
|
|
65
|
+
* by the locator on its own read side.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* Deliberately NOT an {@link IMemoryRetriever}: memory recall is record-granular and
|
|
69
|
+
* returns records; fragment discovery is span-granular and returns locators. Keeping
|
|
70
|
+
* it a distinct surface matches the consumer contract (memory stays record-granular;
|
|
71
|
+
* sub-document knowledge uses a separate fragment index) and avoids overloading the
|
|
72
|
+
* record retriever's return type with a locator that only makes sense here.
|
|
73
|
+
*
|
|
74
|
+
* When no backend is wired, `supportsFragmentRecall` is `false` and any fragment
|
|
75
|
+
* query degrades loudly ({@link FRAGMENT_SEMANTIC_UNWIRED_MESSAGE}) — it NEVER
|
|
76
|
+
* returns a silent empty. A consumer-supplied backend that rejects (throws) is
|
|
77
|
+
* normalized into a `Failure`.
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export class FragmentSemanticRetriever {
|
|
81
|
+
private readonly _backend: IFragmentSemanticBackend | undefined;
|
|
82
|
+
|
|
83
|
+
private constructor(backend: IFragmentSemanticBackend | undefined) {
|
|
84
|
+
this._backend = backend;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** What this retriever can do given its wiring. */
|
|
88
|
+
public get capabilities(): IFragmentRetrieverCapabilities {
|
|
89
|
+
return { supportsFragmentRecall: this._backend !== undefined };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Family-convention factory. */
|
|
93
|
+
public static create(params: {
|
|
94
|
+
readonly backend?: IFragmentSemanticBackend;
|
|
95
|
+
}): Result<FragmentSemanticRetriever> {
|
|
96
|
+
return succeed(new FragmentSemanticRetriever(params.backend));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Embed `query.semantic`, query the fragment index, and return the per-fragment
|
|
101
|
+
* hits in descending score order. Fails loudly when no backend is wired.
|
|
102
|
+
*/
|
|
103
|
+
public async retrieve(query: IFragmentQuery): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {
|
|
104
|
+
if (this._backend === undefined) {
|
|
105
|
+
return fail(FRAGMENT_SEMANTIC_UNWIRED_MESSAGE);
|
|
106
|
+
}
|
|
107
|
+
const backend: IFragmentSemanticBackend = this._backend;
|
|
108
|
+
// Consumer-supplied hooks may throw; normalize both a returned `fail` and a
|
|
109
|
+
// rejection into a single `fragment recall: <label> failed` Failure so
|
|
110
|
+
// `retrieve` always honors its `Promise<Result<...>>` contract.
|
|
111
|
+
const embedded: Result<Float32Array> = await FragmentSemanticRetriever._callBackend(
|
|
112
|
+
'query embedding',
|
|
113
|
+
() => backend.embedQuery(query.semantic)
|
|
114
|
+
);
|
|
115
|
+
if (embedded.isFailure()) {
|
|
116
|
+
return fail(embedded.message);
|
|
117
|
+
}
|
|
118
|
+
return FragmentSemanticRetriever._callBackend('fragment query', () =>
|
|
119
|
+
backend.fragmentIndex.query(embedded.value, query.topK ?? 10, query.maxPerRecord)
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Invoke a consumer-supplied backend hook, normalizing both a returned `fail`
|
|
125
|
+
* and a thrown/rejected promise into a single `fragment recall: <label> failed`
|
|
126
|
+
* `Failure`.
|
|
127
|
+
*/
|
|
128
|
+
private static async _callBackend<T>(label: string, op: () => Promise<Result<T>>): Promise<Result<T>> {
|
|
129
|
+
try {
|
|
130
|
+
return (await op()).withErrorFormat((msg) => `fragment recall: ${label} failed: ${msg}`);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
return fail(`fragment recall: ${label} failed: ${String(err)}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|