@fgv/ts-agent-memory 5.1.0-39 → 5.1.0-41
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/{b82cf6bdece20481260e6bab946179eeec9d7b46.tar.log → cbbdbe09515171b4eba8f2592be2dace1e4e8142.tar.log} +14 -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/converters/envelopeConverter.js +17 -3
- package/dist/packlets/converters/envelopeConverter.js.map +1 -1
- package/dist/packlets/index/memoryIndex.js +58 -10
- package/dist/packlets/index/memoryIndex.js.map +1 -1
- package/dist/packlets/ingest/cycleGuard.js +13 -6
- package/dist/packlets/ingest/cycleGuard.js.map +1 -1
- package/dist/packlets/ingest/hostStages.js.map +1 -1
- package/dist/packlets/ingest/model.js.map +1 -1
- package/dist/packlets/ingest/orchestrator.js +94 -49
- package/dist/packlets/ingest/orchestrator.js.map +1 -1
- package/dist/packlets/retrieve/hybridRetriever.js +13 -2
- package/dist/packlets/retrieve/hybridRetriever.js.map +1 -1
- package/dist/packlets/retrieve/linkTraversalRetriever.js +46 -57
- package/dist/packlets/retrieve/linkTraversalRetriever.js.map +1 -1
- package/dist/packlets/retrieve/recencyRetriever.js +3 -3
- package/dist/packlets/retrieve/recencyRetriever.js.map +1 -1
- package/dist/packlets/retrieve/retriever.js +50 -7
- package/dist/packlets/retrieve/retriever.js.map +1 -1
- package/dist/packlets/retrieve/semanticRetriever.js +9 -3
- package/dist/packlets/retrieve/semanticRetriever.js.map +1 -1
- package/dist/packlets/retrieve/structuredFilterRetriever.js +3 -3
- package/dist/packlets/retrieve/structuredFilterRetriever.js.map +1 -1
- package/dist/packlets/retrieve/tagRetriever.js +3 -3
- package/dist/packlets/retrieve/tagRetriever.js.map +1 -1
- package/dist/packlets/retrieve/temporalRetrievers.js +3 -3
- package/dist/packlets/retrieve/temporalRetrievers.js.map +1 -1
- package/dist/packlets/store/fileTreeMemoryStore.js +72 -16
- package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/dist/packlets/tools/memoryTools.js +104 -21
- package/dist/packlets/tools/memoryTools.js.map +1 -1
- package/dist/packlets/types/envelope.js +13 -1
- package/dist/packlets/types/envelope.js.map +1 -1
- package/dist/packlets/vector/inMemoryCosineIndex.js +22 -17
- package/dist/packlets/vector/inMemoryCosineIndex.js.map +1 -1
- package/dist/packlets/vector/vectorIndex.js.map +1 -1
- package/dist/test/unit/converters/antagonistRoundTrip.test.js +3 -3
- package/dist/test/unit/converters/antagonistRoundTrip.test.js.map +1 -1
- package/dist/test/unit/converters/envelopeConverter.test.js +125 -8
- package/dist/test/unit/converters/envelopeConverter.test.js.map +1 -1
- package/dist/test/unit/index/memoryIndex.test.js +87 -25
- package/dist/test/unit/index/memoryIndex.test.js.map +1 -1
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js +20 -16
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -1
- package/dist/test/unit/ingest/cycleGuard.test.js +28 -1
- package/dist/test/unit/ingest/cycleGuard.test.js.map +1 -1
- package/dist/test/unit/ingest/orchestrator.test.js +187 -45
- package/dist/test/unit/ingest/orchestrator.test.js.map +1 -1
- package/dist/test/unit/retrieve/linkTraversalRetriever.test.js +106 -31
- package/dist/test/unit/retrieve/linkTraversalRetriever.test.js.map +1 -1
- package/dist/test/unit/retrieve/retrievers.test.js +299 -37
- package/dist/test/unit/retrieve/retrievers.test.js.map +1 -1
- package/dist/test/unit/store/embedOnWrite.test.js +69 -12
- package/dist/test/unit/store/embedOnWrite.test.js.map +1 -1
- package/dist/test/unit/store/listScoped.test.js +109 -0
- package/dist/test/unit/store/listScoped.test.js.map +1 -0
- package/dist/test/unit/store/rankAxis.test.js +254 -0
- package/dist/test/unit/store/rankAxis.test.js.map +1 -0
- package/dist/test/unit/tools/memoryTools.test.js +280 -11
- package/dist/test/unit/tools/memoryTools.test.js.map +1 -1
- package/dist/test/unit/types/writePolicy.test.js +9 -2
- package/dist/test/unit/types/writePolicy.test.js.map +1 -1
- package/dist/test/unit/vector/inMemoryCosineIndex.test.js +95 -35
- package/dist/test/unit/vector/inMemoryCosineIndex.test.js.map +1 -1
- package/dist/test/unit/vector/vectorIndex.test.js +24 -15
- package/dist/test/unit/vector/vectorIndex.test.js.map +1 -1
- package/dist/ts-agent-memory.d.ts +384 -97
- package/etc/ts-agent-memory.api.md +64 -22
- package/lib/packlets/converters/envelopeConverter.d.ts +8 -1
- package/lib/packlets/converters/envelopeConverter.d.ts.map +1 -1
- package/lib/packlets/converters/envelopeConverter.js +18 -4
- package/lib/packlets/converters/envelopeConverter.js.map +1 -1
- package/lib/packlets/index/memoryIndex.d.ts +42 -10
- package/lib/packlets/index/memoryIndex.d.ts.map +1 -1
- package/lib/packlets/index/memoryIndex.js +58 -10
- package/lib/packlets/index/memoryIndex.js.map +1 -1
- package/lib/packlets/ingest/cycleGuard.d.ts +5 -5
- package/lib/packlets/ingest/cycleGuard.d.ts.map +1 -1
- package/lib/packlets/ingest/cycleGuard.js +13 -6
- package/lib/packlets/ingest/cycleGuard.js.map +1 -1
- package/lib/packlets/ingest/hostStages.d.ts +3 -3
- package/lib/packlets/ingest/hostStages.d.ts.map +1 -1
- package/lib/packlets/ingest/hostStages.js.map +1 -1
- package/lib/packlets/ingest/model.d.ts +25 -14
- package/lib/packlets/ingest/model.d.ts.map +1 -1
- package/lib/packlets/ingest/model.js.map +1 -1
- package/lib/packlets/ingest/orchestrator.d.ts +18 -4
- package/lib/packlets/ingest/orchestrator.d.ts.map +1 -1
- package/lib/packlets/ingest/orchestrator.js +93 -48
- package/lib/packlets/ingest/orchestrator.js.map +1 -1
- package/lib/packlets/retrieve/hybridRetriever.d.ts.map +1 -1
- package/lib/packlets/retrieve/hybridRetriever.js +12 -1
- package/lib/packlets/retrieve/hybridRetriever.js.map +1 -1
- package/lib/packlets/retrieve/linkTraversalRetriever.d.ts +18 -23
- package/lib/packlets/retrieve/linkTraversalRetriever.d.ts.map +1 -1
- package/lib/packlets/retrieve/linkTraversalRetriever.js +45 -56
- package/lib/packlets/retrieve/linkTraversalRetriever.js.map +1 -1
- package/lib/packlets/retrieve/recencyRetriever.js +2 -2
- package/lib/packlets/retrieve/recencyRetriever.js.map +1 -1
- package/lib/packlets/retrieve/retriever.d.ts +70 -11
- package/lib/packlets/retrieve/retriever.d.ts.map +1 -1
- package/lib/packlets/retrieve/retriever.js +52 -7
- package/lib/packlets/retrieve/retriever.js.map +1 -1
- package/lib/packlets/retrieve/semanticRetriever.d.ts.map +1 -1
- package/lib/packlets/retrieve/semanticRetriever.js +9 -3
- package/lib/packlets/retrieve/semanticRetriever.js.map +1 -1
- package/lib/packlets/retrieve/structuredFilterRetriever.js +2 -2
- package/lib/packlets/retrieve/structuredFilterRetriever.js.map +1 -1
- package/lib/packlets/retrieve/tagRetriever.js +2 -2
- package/lib/packlets/retrieve/tagRetriever.js.map +1 -1
- package/lib/packlets/retrieve/temporalRetrievers.js +3 -3
- package/lib/packlets/retrieve/temporalRetrievers.js.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.d.ts +57 -3
- package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.js +72 -16
- package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/lib/packlets/tools/memoryTools.d.ts +24 -0
- package/lib/packlets/tools/memoryTools.d.ts.map +1 -1
- package/lib/packlets/tools/memoryTools.js +104 -21
- package/lib/packlets/tools/memoryTools.js.map +1 -1
- package/lib/packlets/types/envelope.d.ts +61 -8
- package/lib/packlets/types/envelope.d.ts.map +1 -1
- package/lib/packlets/types/envelope.js +14 -0
- package/lib/packlets/types/envelope.js.map +1 -1
- package/lib/packlets/vector/inMemoryCosineIndex.d.ts +9 -5
- package/lib/packlets/vector/inMemoryCosineIndex.d.ts.map +1 -1
- package/lib/packlets/vector/inMemoryCosineIndex.js +22 -17
- package/lib/packlets/vector/inMemoryCosineIndex.js.map +1 -1
- package/lib/packlets/vector/vectorIndex.d.ts +46 -20
- package/lib/packlets/vector/vectorIndex.d.ts.map +1 -1
- package/lib/packlets/vector/vectorIndex.js.map +1 -1
- package/lib/test/unit/converters/antagonistRoundTrip.test.js +3 -3
- package/lib/test/unit/converters/antagonistRoundTrip.test.js.map +1 -1
- package/lib/test/unit/converters/envelopeConverter.test.js +124 -7
- package/lib/test/unit/converters/envelopeConverter.test.js.map +1 -1
- package/lib/test/unit/index/memoryIndex.test.js +86 -24
- package/lib/test/unit/index/memoryIndex.test.js.map +1 -1
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js +20 -16
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -1
- package/lib/test/unit/ingest/cycleGuard.test.js +28 -1
- package/lib/test/unit/ingest/cycleGuard.test.js.map +1 -1
- package/lib/test/unit/ingest/orchestrator.test.js +186 -44
- package/lib/test/unit/ingest/orchestrator.test.js.map +1 -1
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.js +106 -31
- package/lib/test/unit/retrieve/linkTraversalRetriever.test.js.map +1 -1
- package/lib/test/unit/retrieve/retrievers.test.js +298 -36
- package/lib/test/unit/retrieve/retrievers.test.js.map +1 -1
- package/lib/test/unit/store/embedOnWrite.test.js +68 -11
- package/lib/test/unit/store/embedOnWrite.test.js.map +1 -1
- package/lib/test/unit/store/listScoped.test.d.ts +2 -0
- package/lib/test/unit/store/listScoped.test.d.ts.map +1 -0
- package/lib/test/unit/store/listScoped.test.js +111 -0
- package/lib/test/unit/store/listScoped.test.js.map +1 -0
- package/lib/test/unit/store/rankAxis.test.d.ts +2 -0
- package/lib/test/unit/store/rankAxis.test.d.ts.map +1 -0
- package/lib/test/unit/store/rankAxis.test.js +256 -0
- package/lib/test/unit/store/rankAxis.test.js.map +1 -0
- package/lib/test/unit/tools/memoryTools.test.js +280 -11
- package/lib/test/unit/tools/memoryTools.test.js.map +1 -1
- package/lib/test/unit/types/writePolicy.test.js +9 -2
- package/lib/test/unit/types/writePolicy.test.js.map +1 -1
- package/lib/test/unit/vector/inMemoryCosineIndex.test.js +95 -35
- package/lib/test/unit/vector/inMemoryCosineIndex.test.js.map +1 -1
- package/lib/test/unit/vector/vectorIndex.test.js +24 -15
- package/lib/test/unit/vector/vectorIndex.test.js.map +1 -1
- 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/converters/envelopeConverter.ts +27 -4
- package/src/packlets/index/memoryIndex.ts +86 -22
- package/src/packlets/ingest/cycleGuard.ts +22 -11
- package/src/packlets/ingest/hostStages.ts +3 -3
- package/src/packlets/ingest/model.ts +25 -14
- package/src/packlets/ingest/orchestrator.ts +143 -67
- package/src/packlets/retrieve/hybridRetriever.ts +14 -1
- package/src/packlets/retrieve/linkTraversalRetriever.ts +51 -62
- package/src/packlets/retrieve/recencyRetriever.ts +3 -3
- package/src/packlets/retrieve/retriever.ts +97 -13
- package/src/packlets/retrieve/semanticRetriever.ts +10 -5
- package/src/packlets/retrieve/structuredFilterRetriever.ts +3 -3
- package/src/packlets/retrieve/tagRetriever.ts +3 -3
- package/src/packlets/retrieve/temporalRetrievers.ts +3 -3
- package/src/packlets/store/fileTreeMemoryStore.ts +117 -12
- package/src/packlets/tools/memoryTools.ts +152 -25
- package/src/packlets/types/envelope.ts +66 -8
- package/src/packlets/vector/inMemoryCosineIndex.ts +45 -22
- package/src/packlets/vector/vectorIndex.ts +47 -20
- package/src/test/unit/converters/antagonistRoundTrip.test.ts +3 -3
- package/src/test/unit/converters/envelopeConverter.test.ts +168 -11
- package/src/test/unit/index/memoryIndex.test.ts +99 -14
- package/src/test/unit/ingest/antagonistCycleAndParity.test.ts +23 -18
- package/src/test/unit/ingest/cycleGuard.test.ts +44 -2
- package/src/test/unit/ingest/orchestrator.test.ts +234 -41
- package/src/test/unit/retrieve/linkTraversalRetriever.test.ts +134 -35
- package/src/test/unit/retrieve/retrievers.test.ts +381 -25
- package/src/test/unit/store/embedOnWrite.test.ts +83 -11
- package/src/test/unit/store/listScoped.test.ts +138 -0
- package/src/test/unit/store/rankAxis.test.ts +349 -0
- package/src/test/unit/tools/memoryTools.test.ts +362 -13
- package/src/test/unit/types/writePolicy.test.ts +11 -2
- package/src/test/unit/vector/inMemoryCosineIndex.test.ts +115 -39
- package/src/test/unit/vector/vectorIndex.test.ts +33 -17
- package/temp/build/lint/_eslint-5eVG3S6w.json +41 -33
- package/temp/build/typescript/ts_8nwakTlr.json +1 -1
- package/temp/ts-agent-memory.api.json +1080 -136
- package/temp/ts-agent-memory.api.md +64 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hybridRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/hybridRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAkE;AAElE,2CAOqB;AAkBrB;;;;;;;;;;;GAWG;AACH,MAAa,uBAAuB;IAClC,gBAAuB,CAAC;IAExB,iCAAiC;IAC1B,MAAM,CAAC,MAAM;QAClB,OAAO,IAAA,kBAAO,EAAC,IAAI,uBAAuB,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,yCAAyC;IAClC,KAAK,CACV,UAAgE;QAEhE,MAAM,MAAM,GAA8E,IAAI,GAAG,EAAE,CAAC;QACpG,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;YAC7B,uEAAuE;YACvE,wEAAwE;YACxE,MAAM,SAAS,GAAkB,IAAI,GAAG,EAAY,CAAC;YACrD,KAAK,MAAM,MAAM,IAAI,GAAG,EAAE,CAAC;gBACzB,MAAM,EAAE,GAAa,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACxC,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;oBACtB,SAAS;gBACX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AAU,CAAC,CAAC;iBAChE,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAA,kBAAO,EAAC,IAAA,wBAAY,EAAC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACvE,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;OAWG;IACK,aAAa,CAAC,KAAmB,EAAE,SAA2B;QACpE,MAAM,SAAS,qBAAkE,KAAK,CAAE,CAAC;QACzF,OAAO,SAAS,CAAC,KAAK,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,sBAAsB,EAAE,CAAC;YACnD,OAAO,SAAS,CAAC,QAAQ,CAAC;YAC1B,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,qBAAqB,EAAE,CAAC;YAClD,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,qBAAqB,EAAE,CAAC;YAClD,OAAO,SAAS,CAAC,UAAU,CAAC;YAC5B,OAAO,SAAS,CAAC,QAAQ,CAAC;YAC1B,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AApFD,0CAoFC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport { IMemoryRecord, MemoryId } from '../types';\nimport {\n IMemoryQuery,\n IMemoryRetriever,\n IMemoryRetrieverCapabilities,\n guardRetrieverCapabilities,\n limitRecords,\n recencyCompare\n} from './retriever';\n\n/**\n * Strategy for combining the result sets of the retrievers a\n * {@link HybridRetriever} composes. Injectable so a consumer can weight,\n * re-rank, or intersect instead of the default union.\n * @public\n */\nexport interface IMergeStrategy {\n /**\n * Merge the per-retriever result sets into a single ordered result.\n * @param resultSets - One entry per composed retriever, in composition order.\n */\n merge(\n resultSets: ReadonlyArray<ReadonlyArray<IMemoryRecord<unknown>>>\n ): Result<ReadonlyArray<IMemoryRecord<unknown>>>;\n}\n\n/**\n * The reference {@link IMergeStrategy}: a score-union. Every record is scored by\n * the number of composed result sets it appears in (deduplicated by\n * {@link IMemoryEnvelope.id | id}); the merged result is ordered by descending\n * score, then by recency. Records surfaced by more retrievers rank higher.\n *\n * @remarks\n * Dedup is by `id` alone, which is unambiguous for the flat (one-file-per-entity)\n * kinds B2 ships. Phase-C versioned kinds that reuse a stem across scopes will\n * extend the dedup key — additive, no API change.\n * @public\n */\nexport class ScoreUnionMergeStrategy implements IMergeStrategy {\n private constructor() {}\n\n /** Family-convention factory. */\n public static create(): Result<ScoreUnionMergeStrategy> {\n return succeed(new ScoreUnionMergeStrategy());\n }\n\n /** {@inheritDoc IMergeStrategy.merge} */\n public merge(\n resultSets: ReadonlyArray<ReadonlyArray<IMemoryRecord<unknown>>>\n ): Result<ReadonlyArray<IMemoryRecord<unknown>>> {\n const scored: Map<MemoryId, { readonly record: IMemoryRecord<unknown>; score: number }> = new Map();\n for (const set of resultSets) {\n // Count each id at most once per result set, so the score is \"how many\n // retrievers surfaced this record\" rather than \"how many copies total\".\n const seenInSet: Set<MemoryId> = new Set<MemoryId>();\n for (const record of set) {\n const id: MemoryId = record.envelope.id;\n if (seenInSet.has(id)) {\n continue;\n }\n seenInSet.add(id);\n const existing = scored.get(id);\n if (existing === undefined) {\n scored.set(id, { record, score: 1 });\n } else {\n existing.score += 1;\n }\n }\n }\n const merged: IMemoryRecord<unknown>[] = Array.from(scored.values())\n .sort((a, b) => (b.score !== a.score ? b.score - a.score : recencyCompare(a.record, b.record)))\n .map((entry) => entry.record);\n return succeed(merged);\n }\n}\n\n/**\n * Composes several retrievers, dispatches a query to each, and merges their\n * results via an injectable {@link IMergeStrategy}. Its capabilities are the\n * union of the composed retrievers' capabilities.\n *\n * @remarks\n * The hybrid enforces the loud-degradation contract against its OWN (union)\n * capabilities, then projects the query for each child: a child that does not\n * support the `semantic` (or `asOf`) axis is handed a query with that axis\n * stripped, so it returns its normal results instead of loud-failing on a field\n * a sibling handles. A child that genuinely fails (e.g. a wired semantic backend\n * erroring) propagates — the hybrid never silently drops a failure.\n * @public\n */\nexport class HybridRetriever implements IMemoryRetriever {\n private readonly _retrievers: ReadonlyArray<IMemoryRetriever>;\n private readonly _mergeStrategy: IMergeStrategy;\n private readonly _capabilities: IMemoryRetrieverCapabilities;\n\n private constructor(\n retrievers: ReadonlyArray<IMemoryRetriever>,\n mergeStrategy: IMergeStrategy,\n capabilities: IMemoryRetrieverCapabilities\n ) {\n this._retrievers = retrievers;\n this._mergeStrategy = mergeStrategy;\n this._capabilities = capabilities;\n }\n\n /** {@inheritDoc IMemoryRetriever.capabilities} */\n public get capabilities(): IMemoryRetrieverCapabilities {\n return this._capabilities;\n }\n\n /**\n * Family-convention factory.\n * @param retrievers - The retrievers to compose (at least one).\n * @param mergeStrategy - How to combine their results.\n */\n public static create(\n retrievers: ReadonlyArray<IMemoryRetriever>,\n mergeStrategy: IMergeStrategy\n ): Result<HybridRetriever> {\n if (retrievers.length === 0) {\n return fail('HybridRetriever: at least one retriever is required');\n }\n // Snapshot the caller's array so a later mutation cannot make `retrieve()`\n // and the cached `capabilities` disagree.\n const stableRetrievers: ReadonlyArray<IMemoryRetriever> = [...retrievers];\n const capabilities: IMemoryRetrieverCapabilities = {\n supportsSemanticRecall: stableRetrievers.some((r) => r.capabilities.supportsSemanticRecall),\n supportsTemporalQuery: stableRetrievers.some((r) => r.capabilities.supportsTemporalQuery),\n supportsLinkTraversal: stableRetrievers.some((r) => r.capabilities.supportsLinkTraversal)\n };\n return succeed(new HybridRetriever(stableRetrievers, mergeStrategy, capabilities));\n }\n\n /** {@inheritDoc IMemoryRetriever.retrieve} */\n public async retrieve(query: IMemoryQuery): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> {\n return guardRetrieverCapabilities(query, this._capabilities).thenOnSuccess(async () => {\n const perRetriever: Result<ReadonlyArray<IMemoryRecord<unknown>>>[] = await Promise.all(\n this._retrievers.map((retriever) => retriever.retrieve(this._projectQuery(query, retriever)))\n );\n return mapResults(perRetriever)\n .onSuccess((resultSets) => this._mergeStrategy.merge(resultSets))\n .onSuccess((merged) => succeed(limitRecords(merged, query.limit)));\n });\n }\n\n /**\n * Project the query for one child retriever. Two adjustments:\n *\n * - Strip axes the child does not support (`semantic` / `topK` for a\n * non-semantic child, `asOf` for a non-temporal child, the link axes for a\n * non-link child) so it returns its normal results rather than loud-failing\n * on a field a sibling handles.\n * - Strip `limit` unconditionally: limit is a post-merge concern. A child that\n * pre-truncated its result set would starve the merge strategy of candidates\n * it needs to score correctly (a record both children would surface must\n * reach the merge to score 2). The hybrid applies `limit` once, after merge.\n */\n private _projectQuery(query: IMemoryQuery, retriever: IMemoryRetriever): IMemoryQuery {\n const projected: { -readonly [K in keyof IMemoryQuery]: IMemoryQuery[K] } = { ...query };\n delete projected.limit;\n if (!retriever.capabilities.supportsSemanticRecall) {\n delete projected.semantic;\n delete projected.topK;\n }\n if (!retriever.capabilities.supportsTemporalQuery) {\n delete projected.asOf;\n }\n if (!retriever.capabilities.supportsLinkTraversal) {\n delete projected.linkedFrom;\n delete projected.linkedTo;\n delete projected.hops;\n }\n return projected;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"hybridRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/hybridRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAkE;AAElE,2CAQqB;AAkBrB;;;;;;;;;;;GAWG;AACH,MAAa,uBAAuB;IAClC,gBAAuB,CAAC;IAExB,iCAAiC;IAC1B,MAAM,CAAC,MAAM;QAClB,OAAO,IAAA,kBAAO,EAAC,IAAI,uBAAuB,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,yCAAyC;IAClC,KAAK,CACV,UAAgE;QAEhE,MAAM,MAAM,GAA8E,IAAI,GAAG,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OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,qBAAqB,EAAE,CAAC;YAClD,OAAO,SAAS,CAAC,UAAU,CAAC;YAC5B,OAAO,SAAS,CAAC,QAAQ,CAAC;YAC1B,OAAO,SAAS,CAAC,IAAI,CAAC;QACxB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAhGD,0CAgGC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport { IMemoryRecord, MemoryId } from '../types';\nimport {\n IMemoryQuery,\n IMemoryRetriever,\n IMemoryRetrieverCapabilities,\n guardRetrieverCapabilities,\n limitRecords,\n rankCompare,\n recencyCompare\n} from './retriever';\n\n/**\n * Strategy for combining the result sets of the retrievers a\n * {@link HybridRetriever} composes. Injectable so a consumer can weight,\n * re-rank, or intersect instead of the default union.\n * @public\n */\nexport interface IMergeStrategy {\n /**\n * Merge the per-retriever result sets into a single ordered result.\n * @param resultSets - One entry per composed retriever, in composition order.\n */\n merge(\n resultSets: ReadonlyArray<ReadonlyArray<IMemoryRecord<unknown>>>\n ): Result<ReadonlyArray<IMemoryRecord<unknown>>>;\n}\n\n/**\n * The reference {@link IMergeStrategy}: a score-union. Every record is scored by\n * the number of composed result sets it appears in (deduplicated by\n * {@link IMemoryEnvelope.id | id}); the merged result is ordered by descending\n * score, then by recency. Records surfaced by more retrievers rank higher.\n *\n * @remarks\n * Dedup is by `id` alone, which is unambiguous for the flat (one-file-per-entity)\n * kinds B2 ships. Phase-C versioned kinds that reuse a stem across scopes will\n * extend the dedup key — additive, no API change.\n * @public\n */\nexport class ScoreUnionMergeStrategy implements IMergeStrategy {\n private constructor() {}\n\n /** Family-convention factory. */\n public static create(): Result<ScoreUnionMergeStrategy> {\n return succeed(new ScoreUnionMergeStrategy());\n }\n\n /** {@inheritDoc IMergeStrategy.merge} */\n public merge(\n resultSets: ReadonlyArray<ReadonlyArray<IMemoryRecord<unknown>>>\n ): Result<ReadonlyArray<IMemoryRecord<unknown>>> {\n const scored: Map<MemoryId, { readonly record: IMemoryRecord<unknown>; score: number }> = new Map();\n for (const set of resultSets) {\n // Count each id at most once per result set, so the score is \"how many\n // retrievers surfaced this record\" rather than \"how many copies total\".\n const seenInSet: Set<MemoryId> = new Set<MemoryId>();\n for (const record of set) {\n const id: MemoryId = record.envelope.id;\n if (seenInSet.has(id)) {\n continue;\n }\n seenInSet.add(id);\n const existing = scored.get(id);\n if (existing === undefined) {\n scored.set(id, { record, score: 1 });\n } else {\n existing.score += 1;\n }\n }\n }\n const merged: IMemoryRecord<unknown>[] = Array.from(scored.values())\n .sort((a, b) => (b.score !== a.score ? b.score - a.score : recencyCompare(a.record, b.record)))\n .map((entry) => entry.record);\n return succeed(merged);\n }\n}\n\n/**\n * Composes several retrievers, dispatches a query to each, and merges their\n * results via an injectable {@link IMergeStrategy}. Its capabilities are the\n * union of the composed retrievers' capabilities.\n *\n * @remarks\n * The hybrid enforces the loud-degradation contract against its OWN (union)\n * capabilities, then projects the query for each child: a child that does not\n * support the `semantic` (or `asOf`) axis is handed a query with that axis\n * stripped, so it returns its normal results instead of loud-failing on a field\n * a sibling handles. A child that genuinely fails (e.g. a wired semantic backend\n * erroring) propagates — the hybrid never silently drops a failure.\n * @public\n */\nexport class HybridRetriever implements IMemoryRetriever {\n private readonly _retrievers: ReadonlyArray<IMemoryRetriever>;\n private readonly _mergeStrategy: IMergeStrategy;\n private readonly _capabilities: IMemoryRetrieverCapabilities;\n\n private constructor(\n retrievers: ReadonlyArray<IMemoryRetriever>,\n mergeStrategy: IMergeStrategy,\n capabilities: IMemoryRetrieverCapabilities\n ) {\n this._retrievers = retrievers;\n this._mergeStrategy = mergeStrategy;\n this._capabilities = capabilities;\n }\n\n /** {@inheritDoc IMemoryRetriever.capabilities} */\n public get capabilities(): IMemoryRetrieverCapabilities {\n return this._capabilities;\n }\n\n /**\n * Family-convention factory.\n * @param retrievers - The retrievers to compose (at least one).\n * @param mergeStrategy - How to combine their results.\n */\n public static create(\n retrievers: ReadonlyArray<IMemoryRetriever>,\n mergeStrategy: IMergeStrategy\n ): Result<HybridRetriever> {\n if (retrievers.length === 0) {\n return fail('HybridRetriever: at least one retriever is required');\n }\n // Snapshot the caller's array so a later mutation cannot make `retrieve()`\n // and the cached `capabilities` disagree.\n const stableRetrievers: ReadonlyArray<IMemoryRetriever> = [...retrievers];\n const capabilities: IMemoryRetrieverCapabilities = {\n supportsSemanticRecall: stableRetrievers.some((r) => r.capabilities.supportsSemanticRecall),\n supportsTemporalQuery: stableRetrievers.some((r) => r.capabilities.supportsTemporalQuery),\n supportsLinkTraversal: stableRetrievers.some((r) => r.capabilities.supportsLinkTraversal)\n };\n return succeed(new HybridRetriever(stableRetrievers, mergeStrategy, capabilities));\n }\n\n /** {@inheritDoc IMemoryRetriever.retrieve} */\n public async retrieve(query: IMemoryQuery): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> {\n return guardRetrieverCapabilities(query, this._capabilities).thenOnSuccess(async () => {\n const perRetriever: Result<ReadonlyArray<IMemoryRecord<unknown>>>[] = await Promise.all(\n this._retrievers.map((retriever) => retriever.retrieve(this._projectQuery(query, retriever)))\n );\n return mapResults(perRetriever)\n .onSuccess((resultSets) => this._mergeStrategy.merge(resultSets))\n .onSuccess((merged) => {\n // `orderBy: 'rank'` re-orders the merged set by rank (descending, absent\n // last) before the page window, so a rank-ordered hybrid query yields a\n // rank-ordered page. Absent / `'recency'` preserves the merge strategy's\n // own ordering (byte-identical to the pre-`orderBy` behavior).\n const ordered: ReadonlyArray<IMemoryRecord<unknown>> =\n query.orderBy === 'rank' ? [...merged].sort(rankCompare) : merged;\n return succeed(limitRecords(ordered, query.limit, query.offset));\n });\n });\n }\n\n /**\n * Project the query for one child retriever. Two adjustments:\n *\n * - Strip axes the child does not support (`semantic` / `topK` for a\n * non-semantic child, `asOf` for a non-temporal child, the link axes for a\n * non-link child) so it returns its normal results rather than loud-failing\n * on a field a sibling handles.\n * - Strip `limit` unconditionally: limit is a post-merge concern. A child that\n * pre-truncated its result set would starve the merge strategy of candidates\n * it needs to score correctly (a record both children would surface must\n * reach the merge to score 2). The hybrid applies `limit` once, after merge.\n */\n private _projectQuery(query: IMemoryQuery, retriever: IMemoryRetriever): IMemoryQuery {\n const projected: { -readonly [K in keyof IMemoryQuery]: IMemoryQuery[K] } = { ...query };\n delete projected.limit;\n // Offset, like limit, is a post-merge concern: a child that pre-skipped its\n // own ordered set would drop candidates the merge needs to score correctly.\n // The hybrid applies the `{ offset, limit }` window once, after merge.\n delete projected.offset;\n if (!retriever.capabilities.supportsSemanticRecall) {\n delete projected.semantic;\n delete projected.topK;\n }\n if (!retriever.capabilities.supportsTemporalQuery) {\n delete projected.asOf;\n }\n if (!retriever.capabilities.supportsLinkTraversal) {\n delete projected.linkedFrom;\n delete projected.linkedTo;\n delete projected.hops;\n }\n return projected;\n }\n}\n"]}
|
|
@@ -9,20 +9,23 @@ import { IMemoryQuery, IMemoryRetriever, IMemoryRetrieverCapabilities } from './
|
|
|
9
9
|
*/
|
|
10
10
|
export declare const LINK_TRAVERSAL_NO_SEED_MESSAGE: string;
|
|
11
11
|
/**
|
|
12
|
-
* Breadth-first link-traversal retriever. From a
|
|
13
|
-
* the link graph up to `query.hops` levels and
|
|
14
|
-
* (excluding the seed), recency-ordered and limited.
|
|
12
|
+
* Breadth-first link-traversal retriever. From a scope-qualified
|
|
13
|
+
* {@link IEdgeTarget} seed it walks the link graph up to `query.hops` levels and
|
|
14
|
+
* returns the records reached (excluding the seed), recency-ordered and limited.
|
|
15
15
|
*
|
|
16
16
|
* @remarks
|
|
17
17
|
* - **Direction.** `linkedFrom` walks OUTBOUND edges (each record's
|
|
18
18
|
* `envelope.links[].target`); `linkedTo` walks INBOUND edges (the index's
|
|
19
19
|
* `backlinks`). Exactly one is the seed; `linkedFrom` wins if both are set.
|
|
20
|
+
* - **Scope-qualified nodes.** Every graph node is an {@link IEdgeTarget}
|
|
21
|
+
* `(scope, id)` pair, so following an edge to `turn-3` reaches ONLY the record
|
|
22
|
+
* in the edge's own scope — never a same-stem record in another scope.
|
|
20
23
|
* - **Bound + cycle safety.** Traversal is bounded by `hops` (default `1` — a
|
|
21
|
-
* single hop) and a visited-set guard.
|
|
22
|
-
* string {@link
|
|
23
|
-
* collision-free cycle key — no structural hashing (e.g.
|
|
24
|
-
* needed. A self-loop or any multi-hop cycle terminates
|
|
25
|
-
* is never re-expanded.
|
|
24
|
+
* single hop) and a visited-set guard. Nodes are canonicalized to their
|
|
25
|
+
* `(scope, id)` string via {@link edgeTargetKey}, so a `Set<string>` visited-set
|
|
26
|
+
* is the exact, collision-free cycle key — no structural hashing (e.g.
|
|
27
|
+
* `Crc32Normalizer`) is needed. A self-loop or any multi-hop cycle terminates
|
|
28
|
+
* because a revisited node is never re-expanded.
|
|
26
29
|
* - **Post-filter.** The scope / kind / tag / predicate axes of the query are
|
|
27
30
|
* applied to the reached records (the link axes are the traversal itself).
|
|
28
31
|
* @public
|
|
@@ -39,23 +42,15 @@ export declare class LinkTraversalRetriever implements IMemoryRetriever {
|
|
|
39
42
|
/** Run the bounded, cycle-safe BFS and post-filter the reached records. */
|
|
40
43
|
private _traverse;
|
|
41
44
|
/**
|
|
42
|
-
* Group the index's entries by
|
|
43
|
-
*
|
|
44
|
-
* two
|
|
45
|
-
*
|
|
46
|
-
* @remarks
|
|
47
|
-
* **Design note (links are globally-scoped identifiers in this phase).** An
|
|
48
|
-
* {@link IEdge.target} is a bare `MemoryId`, not a `(scope, id)` pair, so
|
|
49
|
-
* traversal resolves a target across ALL scopes that hold that id. When two
|
|
50
|
-
* scopes reuse a stem, following an edge to it reaches every match. This
|
|
51
|
-
* mirrors the `backlinks` index, which is also keyed by bare id. Scope-
|
|
52
|
-
* qualified link resolution is intentionally out of scope for Phase C and
|
|
53
|
-
* would be an additive change here (and to {@link IEdge} / the index).
|
|
45
|
+
* Group the index's entries by their scope-qualified {@link edgeTargetKey}
|
|
46
|
+
* `(scope, id)` composite. Each composite is the index's primary key, so it maps
|
|
47
|
+
* to exactly one entry — two records that reuse a filename stem across scopes
|
|
48
|
+
* (e.g. `turn-0` in two conversations) get distinct keys and never collide.
|
|
54
49
|
*/
|
|
55
|
-
private
|
|
56
|
-
/** Outbound neighbors: the targets of every edge on the
|
|
50
|
+
private _indexByKey;
|
|
51
|
+
/** Outbound neighbors: the scope-qualified targets of every edge on the record at `node`. */
|
|
57
52
|
private _outbound;
|
|
58
|
-
/** Inbound neighbors: the
|
|
53
|
+
/** Inbound neighbors: the scope-qualified sources whose edges point AT `node` (the backlinks). */
|
|
59
54
|
private _inbound;
|
|
60
55
|
}
|
|
61
56
|
//# sourceMappingURL=linkTraversalRetriever.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"linkTraversalRetriever.d.ts","sourceRoot":"","sources":["../../../src/packlets/retrieve/linkTraversalRetriever.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACtD,OAAO,
|
|
1
|
+
{"version":3,"file":"linkTraversalRetriever.d.ts","sourceRoot":"","sources":["../../../src/packlets/retrieve/linkTraversalRetriever.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACtD,OAAO,EAAe,aAAa,EAAiB,MAAM,UAAU,CAAC;AACrE,OAAO,EAAwB,YAAY,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,4BAA4B,EAK7B,MAAM,aAAa,CAAC;AAYrB;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,EAAE,MACiB,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,sBAAuB,YAAW,gBAAgB;IAC7D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IAEtC,OAAO;IAIP,iCAAiC;WACnB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAAC,sBAAsB,CAAC;IAIzE,kDAAkD;IAClD,IAAW,YAAY,IAAI,4BAA4B,CAEtD;IAED,8CAA8C;IACvC,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAM5F,2EAA2E;IAC3E,OAAO,CAAC,SAAS;IA4CjB;;;;;OAKG;IACH,OAAO,CAAC,WAAW;IAQnB,6FAA6F;IAC7F,OAAO,CAAC,SAAS;IAWjB,kGAAkG;IAClG,OAAO,CAAC,QAAQ;CAGjB"}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.LinkTraversalRetriever = exports.LINK_TRAVERSAL_NO_SEED_MESSAGE = void 0;
|
|
8
8
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
9
|
+
const types_1 = require("../types");
|
|
9
10
|
const retriever_1 = require("./retriever");
|
|
10
11
|
/** The capabilities a link-traversal retriever exposes (link traversal only). */
|
|
11
12
|
const LINK_TRAVERSAL_CAPABILITIES = {
|
|
@@ -22,20 +23,23 @@ const DEFAULT_HOPS = 1;
|
|
|
22
23
|
*/
|
|
23
24
|
exports.LINK_TRAVERSAL_NO_SEED_MESSAGE = 'link traversal requires a seed id (linkedFrom or linkedTo)';
|
|
24
25
|
/**
|
|
25
|
-
* Breadth-first link-traversal retriever. From a
|
|
26
|
-
* the link graph up to `query.hops` levels and
|
|
27
|
-
* (excluding the seed), recency-ordered and limited.
|
|
26
|
+
* Breadth-first link-traversal retriever. From a scope-qualified
|
|
27
|
+
* {@link IEdgeTarget} seed it walks the link graph up to `query.hops` levels and
|
|
28
|
+
* returns the records reached (excluding the seed), recency-ordered and limited.
|
|
28
29
|
*
|
|
29
30
|
* @remarks
|
|
30
31
|
* - **Direction.** `linkedFrom` walks OUTBOUND edges (each record's
|
|
31
32
|
* `envelope.links[].target`); `linkedTo` walks INBOUND edges (the index's
|
|
32
33
|
* `backlinks`). Exactly one is the seed; `linkedFrom` wins if both are set.
|
|
34
|
+
* - **Scope-qualified nodes.** Every graph node is an {@link IEdgeTarget}
|
|
35
|
+
* `(scope, id)` pair, so following an edge to `turn-3` reaches ONLY the record
|
|
36
|
+
* in the edge's own scope — never a same-stem record in another scope.
|
|
33
37
|
* - **Bound + cycle safety.** Traversal is bounded by `hops` (default `1` — a
|
|
34
|
-
* single hop) and a visited-set guard.
|
|
35
|
-
* string {@link
|
|
36
|
-
* collision-free cycle key — no structural hashing (e.g.
|
|
37
|
-
* needed. A self-loop or any multi-hop cycle terminates
|
|
38
|
-
* is never re-expanded.
|
|
38
|
+
* single hop) and a visited-set guard. Nodes are canonicalized to their
|
|
39
|
+
* `(scope, id)` string via {@link edgeTargetKey}, so a `Set<string>` visited-set
|
|
40
|
+
* is the exact, collision-free cycle key — no structural hashing (e.g.
|
|
41
|
+
* `Crc32Normalizer`) is needed. A self-loop or any multi-hop cycle terminates
|
|
42
|
+
* because a revisited node is never re-expanded.
|
|
39
43
|
* - **Post-filter.** The scope / kind / tag / predicate axes of the query are
|
|
40
44
|
* applied to the reached records (the link axes are the traversal itself).
|
|
41
45
|
* @public
|
|
@@ -65,18 +69,20 @@ class LinkTraversalRetriever {
|
|
|
65
69
|
return (0, ts_utils_1.fail)(exports.LINK_TRAVERSAL_NO_SEED_MESSAGE);
|
|
66
70
|
}
|
|
67
71
|
const hops = (_b = query.hops) !== null && _b !== void 0 ? _b : DEFAULT_HOPS;
|
|
68
|
-
const
|
|
69
|
-
// The visited-set IS the cycle guard:
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
+
const byKey = this._indexByKey();
|
|
73
|
+
// The visited-set IS the cycle guard: nodes are canonicalized to their
|
|
74
|
+
// `(scope, id)` string, so set membership is an exact identity check. The
|
|
75
|
+
// seed is pre-marked so it is never re-added.
|
|
76
|
+
const visited = new Set([(0, types_1.edgeTargetKey)(seed)]);
|
|
72
77
|
const reached = [];
|
|
73
78
|
let frontier = [seed];
|
|
74
79
|
for (let hop = 0; hop < hops && frontier.length > 0; hop++) {
|
|
75
80
|
const next = [];
|
|
76
|
-
for (const
|
|
77
|
-
for (const neighbor of outbound ? this._outbound(
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
for (const node of frontier) {
|
|
82
|
+
for (const neighbor of outbound ? this._outbound(node, byKey) : this._inbound(node)) {
|
|
83
|
+
const neighborKey = (0, types_1.edgeTargetKey)(neighbor);
|
|
84
|
+
if (!visited.has(neighborKey)) {
|
|
85
|
+
visited.add(neighborKey);
|
|
80
86
|
reached.push(neighbor);
|
|
81
87
|
next.push(neighbor);
|
|
82
88
|
}
|
|
@@ -85,62 +91,45 @@ class LinkTraversalRetriever {
|
|
|
85
91
|
frontier = next;
|
|
86
92
|
}
|
|
87
93
|
const entries = [];
|
|
88
|
-
for (const
|
|
89
|
-
const
|
|
90
|
-
if (
|
|
91
|
-
entries.push(
|
|
94
|
+
for (const node of reached) {
|
|
95
|
+
const match = byKey.get((0, types_1.edgeTargetKey)(node));
|
|
96
|
+
if (match !== undefined) {
|
|
97
|
+
entries.push(match);
|
|
92
98
|
}
|
|
93
99
|
}
|
|
94
100
|
const ordered = entries
|
|
95
101
|
.filter((entry) => (0, retriever_1.indexedRecordMatchesQuery)(entry, query))
|
|
96
102
|
.map((entry) => entry.record)
|
|
97
|
-
.sort(retriever_1.
|
|
98
|
-
return (0, ts_utils_1.succeed)((0, retriever_1.limitRecords)(ordered, query.limit));
|
|
103
|
+
.sort((0, retriever_1.orderingCompare)(query.orderBy));
|
|
104
|
+
return (0, ts_utils_1.succeed)((0, retriever_1.limitRecords)(ordered, query.limit, query.offset));
|
|
99
105
|
}
|
|
100
106
|
/**
|
|
101
|
-
* Group the index's entries by
|
|
102
|
-
*
|
|
103
|
-
* two
|
|
104
|
-
*
|
|
105
|
-
* @remarks
|
|
106
|
-
* **Design note (links are globally-scoped identifiers in this phase).** An
|
|
107
|
-
* {@link IEdge.target} is a bare `MemoryId`, not a `(scope, id)` pair, so
|
|
108
|
-
* traversal resolves a target across ALL scopes that hold that id. When two
|
|
109
|
-
* scopes reuse a stem, following an edge to it reaches every match. This
|
|
110
|
-
* mirrors the `backlinks` index, which is also keyed by bare id. Scope-
|
|
111
|
-
* qualified link resolution is intentionally out of scope for Phase C and
|
|
112
|
-
* would be an additive change here (and to {@link IEdge} / the index).
|
|
107
|
+
* Group the index's entries by their scope-qualified {@link edgeTargetKey}
|
|
108
|
+
* `(scope, id)` composite. Each composite is the index's primary key, so it maps
|
|
109
|
+
* to exactly one entry — two records that reuse a filename stem across scopes
|
|
110
|
+
* (e.g. `turn-0` in two conversations) get distinct keys and never collide.
|
|
113
111
|
*/
|
|
114
|
-
|
|
115
|
-
const
|
|
112
|
+
_indexByKey() {
|
|
113
|
+
const byKey = new Map();
|
|
116
114
|
for (const entry of this._index.entries()) {
|
|
117
|
-
|
|
118
|
-
const existing = byId.get(id);
|
|
119
|
-
if (existing === undefined) {
|
|
120
|
-
byId.set(id, [entry]);
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
existing.push(entry);
|
|
124
|
-
}
|
|
115
|
+
byKey.set((0, types_1.edgeTargetKey)({ scope: entry.scope, id: entry.record.envelope.id }), entry);
|
|
125
116
|
}
|
|
126
|
-
return
|
|
117
|
+
return byKey;
|
|
127
118
|
}
|
|
128
|
-
/** Outbound neighbors: the targets of every edge on the
|
|
129
|
-
_outbound(
|
|
119
|
+
/** Outbound neighbors: the scope-qualified targets of every edge on the record at `node`. */
|
|
120
|
+
_outbound(node, byKey) {
|
|
130
121
|
const targets = [];
|
|
131
|
-
const
|
|
132
|
-
if (
|
|
133
|
-
for (const
|
|
134
|
-
|
|
135
|
-
targets.push(edge.target);
|
|
136
|
-
}
|
|
122
|
+
const match = byKey.get((0, types_1.edgeTargetKey)(node));
|
|
123
|
+
if (match !== undefined) {
|
|
124
|
+
for (const edge of match.record.envelope.links) {
|
|
125
|
+
targets.push(edge.target);
|
|
137
126
|
}
|
|
138
127
|
}
|
|
139
128
|
return targets;
|
|
140
129
|
}
|
|
141
|
-
/** Inbound neighbors: the
|
|
142
|
-
_inbound(
|
|
143
|
-
return this._index.backlinks(
|
|
130
|
+
/** Inbound neighbors: the scope-qualified sources whose edges point AT `node` (the backlinks). */
|
|
131
|
+
_inbound(node) {
|
|
132
|
+
return this._index.backlinks(node);
|
|
144
133
|
}
|
|
145
134
|
}
|
|
146
135
|
exports.LinkTraversalRetriever = LinkTraversalRetriever;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"linkTraversalRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/linkTraversalRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAsD;AAGtD,2CAQqB;AAErB,iFAAiF;AACjF,MAAM,2BAA2B,GAAiC;IAChE,sBAAsB,EAAE,KAAK;IAC7B,qBAAqB,EAAE,KAAK;IAC5B,qBAAqB,EAAE,IAAI;CAC5B,CAAC;AAEF,+DAA+D;AAC/D,MAAM,YAAY,GAAW,CAAC,CAAC;AAE/B;;;;GAIG;AACU,QAAA,8BAA8B,GACzC,4DAA4D,CAAC;AAE/D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAa,sBAAsB;IAGjC,YAAoB,KAAmB;QACrC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,iCAAiC;IAC1B,MAAM,CAAC,MAAM,CAAC,KAAmB;QACtC,OAAO,IAAA,kBAAO,EAAC,IAAI,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC;IACpD,CAAC;IAED,kDAAkD;IAClD,IAAW,YAAY;QACrB,OAAO,2BAA2B,CAAC;IACrC,CAAC;IAED,8CAA8C;IACvC,QAAQ,CAAC,KAAmB;QACjC,OAAO,OAAO,CAAC,OAAO,CACpB,IAAA,sCAA0B,EA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AC,KAAK,CAAC,CAAC,CAAC;YACxB,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,iFAAiF;IACzE,SAAS,CAAC,EAAY,EAAE,IAAmD;QACjF,MAAM,OAAO,GAAe,EAAE,CAAC;QAC/B,MAAM,OAAO,GAAuC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACjE,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;gBAC5B,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;oBAC/C,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC5B,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,+EAA+E;IACvE,QAAQ,CAAC,EAAY;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IACnC,CAAC;CACF;AAjHD,wDAiHC","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 { IIndexedMemoryRecord, IMemoryIndex } from '../index';\nimport {\n IMemoryQuery,\n IMemoryRetriever,\n IMemoryRetrieverCapabilities,\n guardRetrieverCapabilities,\n indexedRecordMatchesQuery,\n limitRecords,\n recencyCompare\n} from './retriever';\n\n/** The capabilities a link-traversal retriever exposes (link traversal only). */\nconst LINK_TRAVERSAL_CAPABILITIES: IMemoryRetrieverCapabilities = {\n supportsSemanticRecall: false,\n supportsTemporalQuery: false,\n supportsLinkTraversal: true\n};\n\n/** Default BFS hop count when `query.hops` is not supplied. */\nconst DEFAULT_HOPS: number = 1;\n\n/**\n * The loud-degradation message returned when a link-traversal query supplies no\n * seed (`linkedFrom` / `linkedTo`).\n * @public\n */\nexport const LINK_TRAVERSAL_NO_SEED_MESSAGE: string =\n 'link traversal requires a seed id (linkedFrom or linkedTo)';\n\n/**\n * Breadth-first link-traversal retriever. From a seed {@link MemoryId} it walks\n * the link graph up to `query.hops` levels and returns the records reached\n * (excluding the seed), recency-ordered and limited.\n *\n * @remarks\n * - **Direction.** `linkedFrom` walks OUTBOUND edges (each record's\n * `envelope.links[].target`); `linkedTo` walks INBOUND edges (the index's\n * `backlinks`). Exactly one is the seed; `linkedFrom` wins if both are set.\n * - **Bound + cycle safety.** Traversal is bounded by `hops` (default `1` — a\n * single hop) and a visited-set guard. The graph is keyed by bare\n * string {@link MemoryId}s, so a `Set<string>` visited-set is the exact,\n * collision-free cycle key — no structural hashing (e.g. `Crc32Normalizer`) is\n * needed. A self-loop or any multi-hop cycle terminates because a revisited id\n * is never re-expanded.\n * - **Post-filter.** The scope / kind / tag / predicate axes of the query are\n * applied to the reached records (the link axes are the traversal itself).\n * @public\n */\nexport class LinkTraversalRetriever implements IMemoryRetriever {\n private readonly _index: IMemoryIndex;\n\n private constructor(index: IMemoryIndex) {\n this._index = index;\n }\n\n /** Family-convention factory. */\n public static create(index: IMemoryIndex): Result<LinkTraversalRetriever> {\n return succeed(new LinkTraversalRetriever(index));\n }\n\n /** {@inheritDoc IMemoryRetriever.capabilities} */\n public get capabilities(): IMemoryRetrieverCapabilities {\n return LINK_TRAVERSAL_CAPABILITIES;\n }\n\n /** {@inheritDoc IMemoryRetriever.retrieve} */\n public retrieve(query: IMemoryQuery): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> {\n return Promise.resolve(\n guardRetrieverCapabilities(query, this.capabilities).onSuccess(() => this._traverse(query))\n );\n }\n\n /** Run the bounded, cycle-safe BFS and post-filter the reached records. */\n private _traverse(query: IMemoryQuery): Result<ReadonlyArray<IMemoryRecord<unknown>>> {\n const outbound: boolean = query.linkedFrom !== undefined;\n const seed: MemoryId | undefined = query.linkedFrom ?? query.linkedTo;\n if (seed === undefined) {\n return fail(LINK_TRAVERSAL_NO_SEED_MESSAGE);\n }\n const hops: number = query.hops ?? DEFAULT_HOPS;\n const byId: ReadonlyMap<MemoryId, IIndexedMemoryRecord[]> = this._indexById();\n\n // The visited-set IS the cycle guard: ids are strings, so set membership is\n // an exact identity check. The seed is pre-marked so it is never re-added.\n const visited: Set<string> = new Set<string>([seed]);\n const reached: MemoryId[] = [];\n let frontier: MemoryId[] = [seed];\n for (let hop = 0; hop < hops && frontier.length > 0; hop++) {\n const next: MemoryId[] = [];\n for (const id of frontier) {\n for (const neighbor of outbound ? this._outbound(id, byId) : this._inbound(id)) {\n if (!visited.has(neighbor)) {\n visited.add(neighbor);\n reached.push(neighbor);\n next.push(neighbor);\n }\n }\n }\n frontier = next;\n }\n\n const entries: IIndexedMemoryRecord[] = [];\n for (const id of reached) {\n const matches: IIndexedMemoryRecord[] | undefined = byId.get(id);\n if (matches !== undefined) {\n entries.push(...matches);\n }\n }\n const ordered: IMemoryRecord<unknown>[] = entries\n .filter((entry) => indexedRecordMatchesQuery(entry, query))\n .map((entry) => entry.record)\n .sort(recencyCompare);\n return succeed(limitRecords(ordered, query.limit));\n }\n\n /**\n * Group the index's entries by bare {@link MemoryId}. An id can map to more\n * than one entry when distinct scopes reuse a filename stem (e.g. `turn-0` in\n * two conversations), so the value is an array.\n *\n * @remarks\n * **Design note (links are globally-scoped identifiers in this phase).** An\n * {@link IEdge.target} is a bare `MemoryId`, not a `(scope, id)` pair, so\n * traversal resolves a target across ALL scopes that hold that id. When two\n * scopes reuse a stem, following an edge to it reaches every match. This\n * mirrors the `backlinks` index, which is also keyed by bare id. Scope-\n * qualified link resolution is intentionally out of scope for Phase C and\n * would be an additive change here (and to {@link IEdge} / the index).\n */\n private _indexById(): ReadonlyMap<MemoryId, IIndexedMemoryRecord[]> {\n const byId: Map<MemoryId, IIndexedMemoryRecord[]> = new Map<MemoryId, IIndexedMemoryRecord[]>();\n for (const entry of this._index.entries()) {\n const id: MemoryId = entry.record.envelope.id;\n const existing: IIndexedMemoryRecord[] | undefined = byId.get(id);\n if (existing === undefined) {\n byId.set(id, [entry]);\n } else {\n existing.push(entry);\n }\n }\n return byId;\n }\n\n /** Outbound neighbors: the targets of every edge on the records with this id. */\n private _outbound(id: MemoryId, byId: ReadonlyMap<MemoryId, IIndexedMemoryRecord[]>): MemoryId[] {\n const targets: MemoryId[] = [];\n const matches: IIndexedMemoryRecord[] | undefined = byId.get(id);\n if (matches !== undefined) {\n for (const entry of matches) {\n for (const edge of entry.record.envelope.links) {\n targets.push(edge.target);\n }\n }\n }\n return targets;\n }\n\n /** Inbound neighbors: the ids whose edges point AT this id (the backlinks). */\n private _inbound(id: MemoryId): ReadonlyArray<MemoryId> {\n return this._index.backlinks(id);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"linkTraversalRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/linkTraversalRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAsD;AACtD,oCAAqE;AAErE,2CAQqB;AAErB,iFAAiF;AACjF,MAAM,2BAA2B,GAAiC;IAChE,sBAAsB,EAAE,KAAK;IAC7B,qBAAqB,EAAE,KAAK;IAC5B,qBAAqB,EAAE,IAAI;CAC5B,CAAC;AAEF,+DAA+D;AAC/D,MAAM,YAAY,GAAW,CAAC,CAAC;AAE/B;;;;GAIG;AACU,QAAA,8BAA8B,GACzC,4DAA4D,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,sBAAsB;IAGjC,YAAoB,KAAmB;QACrC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,iCAAiC;IAC1B,MAAM,CAAC,MAAM,CAAC,KAAmB;QACtC,OAAO,IAAA,kBAAO,EAAC,IAAI,sBAAsB,CAAC,KAAK,CAAC,CAAC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M,KAAK,GAAsC,IAAI,GAAG,EAAgC,CAAC;QACzF,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;YAC1C,KAAK,CAAC,GAAG,CAAC,IAAA,qBAAa,EAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,6FAA6F;IACrF,SAAS,CAAC,IAAiB,EAAE,KAAgD;QACnF,MAAM,OAAO,GAAkB,EAAE,CAAC;QAClC,MAAM,KAAK,GAAqC,KAAK,CAAC,GAAG,CAAC,IAAA,qBAAa,EAAC,IAAI,CAAC,CAAC,CAAC;QAC/E,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;gBAC/C,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,kGAAkG;IAC1F,QAAQ,CAAC,IAAiB;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;CACF;AAnGD,wDAmGC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, succeed } from '@fgv/ts-utils';\nimport { IEdgeTarget, IMemoryRecord, edgeTargetKey } from '../types';\nimport { IIndexedMemoryRecord, IMemoryIndex } from '../index';\nimport {\n IMemoryQuery,\n IMemoryRetriever,\n IMemoryRetrieverCapabilities,\n guardRetrieverCapabilities,\n indexedRecordMatchesQuery,\n limitRecords,\n orderingCompare\n} from './retriever';\n\n/** The capabilities a link-traversal retriever exposes (link traversal only). */\nconst LINK_TRAVERSAL_CAPABILITIES: IMemoryRetrieverCapabilities = {\n supportsSemanticRecall: false,\n supportsTemporalQuery: false,\n supportsLinkTraversal: true\n};\n\n/** Default BFS hop count when `query.hops` is not supplied. */\nconst DEFAULT_HOPS: number = 1;\n\n/**\n * The loud-degradation message returned when a link-traversal query supplies no\n * seed (`linkedFrom` / `linkedTo`).\n * @public\n */\nexport const LINK_TRAVERSAL_NO_SEED_MESSAGE: string =\n 'link traversal requires a seed id (linkedFrom or linkedTo)';\n\n/**\n * Breadth-first link-traversal retriever. From a scope-qualified\n * {@link IEdgeTarget} seed it walks the link graph up to `query.hops` levels and\n * returns the records reached (excluding the seed), recency-ordered and limited.\n *\n * @remarks\n * - **Direction.** `linkedFrom` walks OUTBOUND edges (each record's\n * `envelope.links[].target`); `linkedTo` walks INBOUND edges (the index's\n * `backlinks`). Exactly one is the seed; `linkedFrom` wins if both are set.\n * - **Scope-qualified nodes.** Every graph node is an {@link IEdgeTarget}\n * `(scope, id)` pair, so following an edge to `turn-3` reaches ONLY the record\n * in the edge's own scope — never a same-stem record in another scope.\n * - **Bound + cycle safety.** Traversal is bounded by `hops` (default `1` — a\n * single hop) and a visited-set guard. Nodes are canonicalized to their\n * `(scope, id)` string via {@link edgeTargetKey}, so a `Set<string>` visited-set\n * is the exact, collision-free cycle key — no structural hashing (e.g.\n * `Crc32Normalizer`) is needed. A self-loop or any multi-hop cycle terminates\n * because a revisited node is never re-expanded.\n * - **Post-filter.** The scope / kind / tag / predicate axes of the query are\n * applied to the reached records (the link axes are the traversal itself).\n * @public\n */\nexport class LinkTraversalRetriever implements IMemoryRetriever {\n private readonly _index: IMemoryIndex;\n\n private constructor(index: IMemoryIndex) {\n this._index = index;\n }\n\n /** Family-convention factory. */\n public static create(index: IMemoryIndex): Result<LinkTraversalRetriever> {\n return succeed(new LinkTraversalRetriever(index));\n }\n\n /** {@inheritDoc IMemoryRetriever.capabilities} */\n public get capabilities(): IMemoryRetrieverCapabilities {\n return LINK_TRAVERSAL_CAPABILITIES;\n }\n\n /** {@inheritDoc IMemoryRetriever.retrieve} */\n public retrieve(query: IMemoryQuery): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> {\n return Promise.resolve(\n guardRetrieverCapabilities(query, this.capabilities).onSuccess(() => this._traverse(query))\n );\n }\n\n /** Run the bounded, cycle-safe BFS and post-filter the reached records. */\n private _traverse(query: IMemoryQuery): Result<ReadonlyArray<IMemoryRecord<unknown>>> {\n const outbound: boolean = query.linkedFrom !== undefined;\n const seed: IEdgeTarget | undefined = query.linkedFrom ?? query.linkedTo;\n if (seed === undefined) {\n return fail(LINK_TRAVERSAL_NO_SEED_MESSAGE);\n }\n const hops: number = query.hops ?? DEFAULT_HOPS;\n const byKey: ReadonlyMap<string, IIndexedMemoryRecord> = this._indexByKey();\n\n // The visited-set IS the cycle guard: nodes are canonicalized to their\n // `(scope, id)` string, so set membership is an exact identity check. The\n // seed is pre-marked so it is never re-added.\n const visited: Set<string> = new Set<string>([edgeTargetKey(seed)]);\n const reached: IEdgeTarget[] = [];\n let frontier: IEdgeTarget[] = [seed];\n for (let hop = 0; hop < hops && frontier.length > 0; hop++) {\n const next: IEdgeTarget[] = [];\n for (const node of frontier) {\n for (const neighbor of outbound ? this._outbound(node, byKey) : this._inbound(node)) {\n const neighborKey: string = edgeTargetKey(neighbor);\n if (!visited.has(neighborKey)) {\n visited.add(neighborKey);\n reached.push(neighbor);\n next.push(neighbor);\n }\n }\n }\n frontier = next;\n }\n\n const entries: IIndexedMemoryRecord[] = [];\n for (const node of reached) {\n const match: IIndexedMemoryRecord | undefined = byKey.get(edgeTargetKey(node));\n if (match !== undefined) {\n entries.push(match);\n }\n }\n const ordered: IMemoryRecord<unknown>[] = entries\n .filter((entry) => indexedRecordMatchesQuery(entry, query))\n .map((entry) => entry.record)\n .sort(orderingCompare(query.orderBy));\n return succeed(limitRecords(ordered, query.limit, query.offset));\n }\n\n /**\n * Group the index's entries by their scope-qualified {@link edgeTargetKey}\n * `(scope, id)` composite. Each composite is the index's primary key, so it maps\n * to exactly one entry — two records that reuse a filename stem across scopes\n * (e.g. `turn-0` in two conversations) get distinct keys and never collide.\n */\n private _indexByKey(): ReadonlyMap<string, IIndexedMemoryRecord> {\n const byKey: Map<string, IIndexedMemoryRecord> = new Map<string, IIndexedMemoryRecord>();\n for (const entry of this._index.entries()) {\n byKey.set(edgeTargetKey({ scope: entry.scope, id: entry.record.envelope.id }), entry);\n }\n return byKey;\n }\n\n /** Outbound neighbors: the scope-qualified targets of every edge on the record at `node`. */\n private _outbound(node: IEdgeTarget, byKey: ReadonlyMap<string, IIndexedMemoryRecord>): IEdgeTarget[] {\n const targets: IEdgeTarget[] = [];\n const match: IIndexedMemoryRecord | undefined = byKey.get(edgeTargetKey(node));\n if (match !== undefined) {\n for (const edge of match.record.envelope.links) {\n targets.push(edge.target);\n }\n }\n return targets;\n }\n\n /** Inbound neighbors: the scope-qualified sources whose edges point AT `node` (the backlinks). */\n private _inbound(node: IEdgeTarget): ReadonlyArray<IEdgeTarget> {\n return this._index.backlinks(node);\n }\n}\n"]}
|
|
@@ -28,8 +28,8 @@ class RecencyRetriever {
|
|
|
28
28
|
/** {@inheritDoc IMemoryRetriever.retrieve} */
|
|
29
29
|
retrieve(query) {
|
|
30
30
|
return Promise.resolve((0, retriever_1.guardRetrieverCapabilities)(query, this.capabilities).onSuccess(() => {
|
|
31
|
-
const ordered = (0, retriever_1.selectByQuery)(this._index.entries(), query).sort(retriever_1.
|
|
32
|
-
return (0, ts_utils_1.succeed)((0, retriever_1.limitRecords)(ordered, query.limit));
|
|
31
|
+
const ordered = (0, retriever_1.selectByQuery)(this._index.entries(), query).sort((0, retriever_1.orderingCompare)(query.orderBy));
|
|
32
|
+
return (0, ts_utils_1.succeed)((0, retriever_1.limitRecords)(ordered, query.limit, query.offset));
|
|
33
33
|
}));
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recencyRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/recencyRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAgD;AAGhD,2CASqB;AAErB;;;;;GAKG;AACH,MAAa,gBAAgB;IAG3B,YAAoB,KAAmB;QACrC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,kDAAkD;IAClD,IAAW,YAAY;QACrB,OAAO,qCAAyB,CAAC;IACnC,CAAC;IAED,iCAAiC;IAC1B,MAAM,CAAC,MAAM,CAAC,KAAmB;QACtC,OAAO,IAAA,kBAAO,EAAC,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9C,CAAC;IAED,8CAA8C;IACvC,QAAQ,CAAC,KAAmB;QACjC,OAAO,OAAO,CAAC,OAAO,CACpB,IAAA,sCAA0B,EAAC,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE;YAClE,MAAM,OAAO,GAA6B,IAAA,yBAAa,EAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,CAAC,IAAI,CACxF,
|
|
1
|
+
{"version":3,"file":"recencyRetriever.js","sourceRoot":"","sources":["../../../src/packlets/retrieve/recencyRetriever.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAgD;AAGhD,2CASqB;AAErB;;;;;GAKG;AACH,MAAa,gBAAgB;IAG3B,YAAoB,KAAmB;QACrC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,kDAAkD;IAClD,IAAW,YAAY;QACrB,OAAO,qCAAyB,CAAC;IACnC,CAAC;IAED,iCAAiC;IAC1B,MAAM,CAAC,MAAM,CAAC,KAAmB;QACtC,OAAO,IAAA,kBAAO,EAAC,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9C,CAAC;IAED,8CAA8C;IACvC,QAAQ,CAAC,KAAmB;QACjC,OAAO,OAAO,CAAC,OAAO,CACpB,IAAA,sCAA0B,EAAC,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE;YAClE,MAAM,OAAO,GAA6B,IAAA,yBAAa,EAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,CAAC,IAAI,CACxF,IAAA,2BAAe,EAAC,KAAK,CAAC,OAAO,CAAC,CAC/B,CAAC;YACF,OAAO,IAAA,kBAAO,EAAC,IAAA,wBAAY,EAAC,OAAO,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;QACnE,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF;AA5BD,4CA4BC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, succeed } from '@fgv/ts-utils';\nimport { IMemoryRecord } from '../types';\nimport { IMemoryIndex } from '../index';\nimport {\n IMemoryQuery,\n IMemoryRetriever,\n IMemoryRetrieverCapabilities,\n NON_SEMANTIC_CAPABILITIES,\n guardRetrieverCapabilities,\n limitRecords,\n orderingCompare,\n selectByQuery\n} from './retriever';\n\n/**\n * Returns records matching the query's scope / kind / tag / predicate filters,\n * ordered most-recently-updated first. The universal v1 retriever — an empty\n * query returns the whole vault in recency order.\n * @public\n */\nexport class RecencyRetriever implements IMemoryRetriever {\n private readonly _index: IMemoryIndex;\n\n private constructor(index: IMemoryIndex) {\n this._index = index;\n }\n\n /** {@inheritDoc IMemoryRetriever.capabilities} */\n public get capabilities(): IMemoryRetrieverCapabilities {\n return NON_SEMANTIC_CAPABILITIES;\n }\n\n /** Family-convention factory. */\n public static create(index: IMemoryIndex): Result<RecencyRetriever> {\n return succeed(new RecencyRetriever(index));\n }\n\n /** {@inheritDoc IMemoryRetriever.retrieve} */\n public retrieve(query: IMemoryQuery): Promise<Result<ReadonlyArray<IMemoryRecord<unknown>>>> {\n return Promise.resolve(\n guardRetrieverCapabilities(query, this.capabilities).onSuccess(() => {\n const ordered: IMemoryRecord<unknown>[] = selectByQuery(this._index.entries(), query).sort(\n orderingCompare(query.orderBy)\n );\n return succeed(limitRecords(ordered, query.limit, query.offset));\n })\n );\n }\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Result } from '@fgv/ts-utils';
|
|
2
|
-
import { IMemoryRecord, Kind,
|
|
2
|
+
import { IEdgeTarget, IMemoryRecord, Kind, MemoryScopeKey, Tag } from '../types';
|
|
3
3
|
import { IIndexedMemoryRecord } from '../index';
|
|
4
4
|
/**
|
|
5
5
|
* The capabilities a retriever exposes. A consumer probes these before
|
|
@@ -27,12 +27,24 @@ export interface IMemoryQuery {
|
|
|
27
27
|
readonly scope?: MemoryScopeKey;
|
|
28
28
|
/** Restrict to records carrying this tag (exact match). */
|
|
29
29
|
readonly tag?: Tag;
|
|
30
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Restrict to records of this kind — the single-kind shorthand for
|
|
32
|
+
* {@link IMemoryQuery.kinds | kinds}. When both are set they compose as AND
|
|
33
|
+
* (the record's kind must satisfy both), so `kind` must itself be a member of
|
|
34
|
+
* `kinds` for anything to match.
|
|
35
|
+
*/
|
|
31
36
|
readonly kind?: Kind;
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Restrict to records in ANY of these kinds — the general (multi-kind) form of
|
|
39
|
+
* {@link IMemoryQuery.kind | kind}. Absent → no kind-set constraint (today's
|
|
40
|
+
* behavior). An explicit empty array `[]` matches NOTHING (mirroring the
|
|
41
|
+
* non-positive-`limit` "explicit empty" convention), never "match all".
|
|
42
|
+
*/
|
|
43
|
+
readonly kinds?: ReadonlyArray<Kind>;
|
|
44
|
+
/** Restrict to records linked FROM this scope-qualified seed (outbound). */
|
|
45
|
+
readonly linkedFrom?: IEdgeTarget;
|
|
46
|
+
/** Restrict to records linked TO this scope-qualified seed (inbound / backlinks). */
|
|
47
|
+
readonly linkedTo?: IEdgeTarget;
|
|
36
48
|
/** BFS hop count for link traversal. Default: 1. */
|
|
37
49
|
readonly hops?: number;
|
|
38
50
|
/**
|
|
@@ -49,8 +61,32 @@ export interface IMemoryQuery {
|
|
|
49
61
|
* `Result.fail` — never a silent empty.
|
|
50
62
|
*/
|
|
51
63
|
readonly asOf?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Ordering for the result set. `'recency'` (the default when absent — today's
|
|
66
|
+
* exact behavior) orders most-recently-updated first; `'rank'` orders by the
|
|
67
|
+
* store-computed {@link IMemoryEnvelope.rank} descending (records with an absent
|
|
68
|
+
* `rank` last), with recency as the tiebreak. Combined with `{ limit, offset }`
|
|
69
|
+
* this yields a bounded top-M rank-ordered page with no full-vault scan.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* `orderBy` governs the ordered non-semantic retrievers (recency / tag /
|
|
73
|
+
* structured-filter / link-traversal) and the {@link HybridRetriever}'s
|
|
74
|
+
* post-merge ordering. The {@link SemanticRetriever} is the sole exception: it
|
|
75
|
+
* preserves its native vector-similarity order regardless of `orderBy` —
|
|
76
|
+
* re-sorting semantic hits by `rank` would discard the similarity ranking that
|
|
77
|
+
* is the whole point of that path; a consumer that wants rank ordering uses a
|
|
78
|
+
* non-semantic query.
|
|
79
|
+
*/
|
|
80
|
+
readonly orderBy?: 'recency' | 'rank';
|
|
52
81
|
/** Maximum records to return. Applied after all other filters. */
|
|
53
82
|
readonly limit?: number;
|
|
83
|
+
/**
|
|
84
|
+
* Records to skip after ordering, before `limit` — so `{ offset, limit }` is a
|
|
85
|
+
* stable page window over the ordered result set. Default 0. A non-positive or
|
|
86
|
+
* absent offset is today's behavior (no skip); an offset past the end yields an
|
|
87
|
+
* empty page, never a throw.
|
|
88
|
+
*/
|
|
89
|
+
readonly offset?: number;
|
|
54
90
|
/** Arbitrary predicate applied after the scope / kind / tag pre-filter. */
|
|
55
91
|
readonly filter?: (record: IMemoryRecord<unknown>) => boolean;
|
|
56
92
|
}
|
|
@@ -109,6 +145,21 @@ export declare function guardRetrieverCapabilities(query: IMemoryQuery, capabili
|
|
|
109
145
|
* @public
|
|
110
146
|
*/
|
|
111
147
|
export declare function recencyCompare(a: IMemoryRecord<unknown>, b: IMemoryRecord<unknown>): number;
|
|
148
|
+
/**
|
|
149
|
+
* Rank comparator: store-computed {@link IMemoryEnvelope.rank} descending, with
|
|
150
|
+
* {@link recencyCompare} as the tiebreak. Records with an absent `rank` sort LAST
|
|
151
|
+
* (after every ranked record), then by recency among themselves. Mirrors the
|
|
152
|
+
* index's rank-view ordering.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
export declare function rankCompare(a: IMemoryRecord<unknown>, b: IMemoryRecord<unknown>): number;
|
|
156
|
+
/**
|
|
157
|
+
* Select the record comparator for a query's {@link IMemoryQuery.orderBy | orderBy}
|
|
158
|
+
* axis: {@link rankCompare} for `'rank'`, {@link recencyCompare} otherwise (the
|
|
159
|
+
* default, byte-identical to the pre-`orderBy` behavior).
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
export declare function orderingCompare(orderBy?: IMemoryQuery['orderBy']): (a: IMemoryRecord<unknown>, b: IMemoryRecord<unknown>) => number;
|
|
112
163
|
/**
|
|
113
164
|
* Whether an indexed entry satisfies a query's scope / kind / tag / predicate
|
|
114
165
|
* pre-filter (the axes shared by every v1 retriever). The `semantic` / `asOf` /
|
|
@@ -123,11 +174,19 @@ export declare function indexedRecordMatchesQuery(entry: IIndexedMemoryRecord, q
|
|
|
123
174
|
*/
|
|
124
175
|
export declare function selectByQuery(entries: ReadonlyArray<IIndexedMemoryRecord>, query: IMemoryQuery): IMemoryRecord<unknown>[];
|
|
125
176
|
/**
|
|
126
|
-
*
|
|
127
|
-
* last, after ordering, so it always takes
|
|
128
|
-
*
|
|
129
|
-
*
|
|
177
|
+
* Apply the `{ offset, limit }` page window to an ordered record set. Applied
|
|
178
|
+
* last, after ordering, so it always takes a stable window of the ordered
|
|
179
|
+
* result. `offset` is applied first (records to skip), then `limit` (top-N of
|
|
180
|
+
* the remainder).
|
|
181
|
+
*
|
|
182
|
+
* @remarks
|
|
183
|
+
* Both bounds are public query input and are guarded against non-positive
|
|
184
|
+
* values slipping into `slice`:
|
|
185
|
+
* - `offset` absent or non-positive → no skip (today's behavior). An offset past
|
|
186
|
+
* the end yields an empty page rather than a throw.
|
|
187
|
+
* - `limit` absent → no truncation; a non-positive `limit` means "no records"
|
|
188
|
+
* and returns an empty array.
|
|
130
189
|
* @public
|
|
131
190
|
*/
|
|
132
|
-
export declare function limitRecords(records: ReadonlyArray<IMemoryRecord<unknown>>, limit?: number): ReadonlyArray<IMemoryRecord<unknown>>;
|
|
191
|
+
export declare function limitRecords(records: ReadonlyArray<IMemoryRecord<unknown>>, limit?: number, offset?: number): ReadonlyArray<IMemoryRecord<unknown>>;
|
|
133
192
|
//# sourceMappingURL=retriever.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retriever.d.ts","sourceRoot":"","sources":["../../../src/packlets/retrieve/retriever.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"retriever.d.ts","sourceRoot":"","sources":["../../../src/packlets/retrieve/retriever.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AACjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,WAAW,4BAA4B;IAC3C,kFAAkF;IAClF,QAAQ,CAAC,sBAAsB,EAAE,OAAO,CAAC;IACzC,4EAA4E;IAC5E,QAAQ,CA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AY,GAClB,aAAa,CAAC,OAAO,CAAC,EAAE,CAE1B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAC9C,KAAK,CAAC,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,GACd,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAUvC"}
|
|
@@ -8,6 +8,8 @@ exports.NON_SEMANTIC_CAPABILITIES = exports.LINK_TRAVERSAL_UNWIRED_MESSAGE = exp
|
|
|
8
8
|
exports.temporalUnwiredMessage = temporalUnwiredMessage;
|
|
9
9
|
exports.guardRetrieverCapabilities = guardRetrieverCapabilities;
|
|
10
10
|
exports.recencyCompare = recencyCompare;
|
|
11
|
+
exports.rankCompare = rankCompare;
|
|
12
|
+
exports.orderingCompare = orderingCompare;
|
|
11
13
|
exports.indexedRecordMatchesQuery = indexedRecordMatchesQuery;
|
|
12
14
|
exports.selectByQuery = selectByQuery;
|
|
13
15
|
exports.limitRecords = limitRecords;
|
|
@@ -71,6 +73,36 @@ function recencyCompare(a, b) {
|
|
|
71
73
|
const byUpdated = b.envelope.updated - a.envelope.updated;
|
|
72
74
|
return byUpdated !== 0 ? byUpdated : b.envelope.seq - a.envelope.seq;
|
|
73
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Rank comparator: store-computed {@link IMemoryEnvelope.rank} descending, with
|
|
78
|
+
* {@link recencyCompare} as the tiebreak. Records with an absent `rank` sort LAST
|
|
79
|
+
* (after every ranked record), then by recency among themselves. Mirrors the
|
|
80
|
+
* index's rank-view ordering.
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
function rankCompare(a, b) {
|
|
84
|
+
const ra = a.envelope.rank;
|
|
85
|
+
const rb = b.envelope.rank;
|
|
86
|
+
if (ra === undefined && rb !== undefined) {
|
|
87
|
+
return 1;
|
|
88
|
+
}
|
|
89
|
+
if (rb === undefined && ra !== undefined) {
|
|
90
|
+
return -1;
|
|
91
|
+
}
|
|
92
|
+
if (ra !== undefined && rb !== undefined && ra !== rb) {
|
|
93
|
+
return rb - ra;
|
|
94
|
+
}
|
|
95
|
+
return recencyCompare(a, b);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Select the record comparator for a query's {@link IMemoryQuery.orderBy | orderBy}
|
|
99
|
+
* axis: {@link rankCompare} for `'rank'`, {@link recencyCompare} otherwise (the
|
|
100
|
+
* default, byte-identical to the pre-`orderBy` behavior).
|
|
101
|
+
* @public
|
|
102
|
+
*/
|
|
103
|
+
function orderingCompare(orderBy) {
|
|
104
|
+
return orderBy === 'rank' ? rankCompare : recencyCompare;
|
|
105
|
+
}
|
|
74
106
|
/**
|
|
75
107
|
* Whether an indexed entry satisfies a query's scope / kind / tag / predicate
|
|
76
108
|
* pre-filter (the axes shared by every v1 retriever). The `semantic` / `asOf` /
|
|
@@ -84,6 +116,9 @@ function indexedRecordMatchesQuery(entry, query) {
|
|
|
84
116
|
if (query.kind !== undefined && entry.record.envelope.kind !== query.kind) {
|
|
85
117
|
return false;
|
|
86
118
|
}
|
|
119
|
+
if (query.kinds !== undefined && !query.kinds.includes(entry.record.envelope.kind)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
87
122
|
if (query.tag !== undefined && !entry.record.envelope.tags.includes(query.tag)) {
|
|
88
123
|
return false;
|
|
89
124
|
}
|
|
@@ -101,19 +136,29 @@ function selectByQuery(entries, query) {
|
|
|
101
136
|
return entries.filter((entry) => indexedRecordMatchesQuery(entry, query)).map((entry) => entry.record);
|
|
102
137
|
}
|
|
103
138
|
/**
|
|
104
|
-
*
|
|
105
|
-
* last, after ordering, so it always takes
|
|
106
|
-
*
|
|
107
|
-
*
|
|
139
|
+
* Apply the `{ offset, limit }` page window to an ordered record set. Applied
|
|
140
|
+
* last, after ordering, so it always takes a stable window of the ordered
|
|
141
|
+
* result. `offset` is applied first (records to skip), then `limit` (top-N of
|
|
142
|
+
* the remainder).
|
|
143
|
+
*
|
|
144
|
+
* @remarks
|
|
145
|
+
* Both bounds are public query input and are guarded against non-positive
|
|
146
|
+
* values slipping into `slice`:
|
|
147
|
+
* - `offset` absent or non-positive → no skip (today's behavior). An offset past
|
|
148
|
+
* the end yields an empty page rather than a throw.
|
|
149
|
+
* - `limit` absent → no truncation; a non-positive `limit` means "no records"
|
|
150
|
+
* and returns an empty array.
|
|
108
151
|
* @public
|
|
109
152
|
*/
|
|
110
|
-
function limitRecords(records, limit) {
|
|
153
|
+
function limitRecords(records, limit, offset) {
|
|
154
|
+
const skip = offset !== undefined && offset > 0 ? offset : 0;
|
|
155
|
+
const windowed = skip > 0 ? records.slice(skip) : records;
|
|
111
156
|
if (limit === undefined) {
|
|
112
|
-
return
|
|
157
|
+
return windowed;
|
|
113
158
|
}
|
|
114
159
|
if (limit <= 0) {
|
|
115
160
|
return [];
|
|
116
161
|
}
|
|
117
|
-
return
|
|
162
|
+
return windowed.length > limit ? windowed.slice(0, limit) : windowed;
|
|
118
163
|
}
|
|
119
164
|
//# sourceMappingURL=retriever.js.map
|