@remnic/core 1.1.0 → 1.1.2
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/dist/access-audit.d.ts +56 -0
- package/dist/access-audit.js +9 -0
- package/dist/access-cli.js +70 -53
- package/dist/access-cli.js.map +1 -1
- package/dist/access-http.d.ts +16 -9
- package/dist/access-http.js +26 -18
- package/dist/access-mcp.d.ts +16 -9
- package/dist/access-mcp.js +30 -8
- package/dist/access-schema.d.ts +124 -33
- package/dist/access-schema.js +5 -1
- package/dist/{access-service-HmO1Trrx.d.ts → access-service-Br8ZydTK.d.ts} +158 -63
- package/dist/access-service.d.ts +13 -6
- package/dist/access-service.js +23 -14
- package/dist/bootstrap.d.ts +6 -3
- package/dist/briefing.d.ts +1 -0
- package/dist/briefing.js +8 -6
- package/dist/buffer-surprise-report.d.ts +70 -0
- package/dist/buffer-surprise-report.js +7 -0
- package/dist/buffer-surprise-report.js.map +1 -0
- package/dist/buffer-surprise.d.ts +98 -0
- package/dist/buffer-surprise.js +11 -0
- package/dist/buffer-surprise.js.map +1 -0
- package/dist/buffer.d.ts +100 -2
- package/dist/buffer.js +1 -1
- package/dist/calibration.js +6 -6
- package/dist/causal-behavior.js +4 -4
- package/dist/causal-chain.js +2 -2
- package/dist/causal-consolidation.js +19 -18
- package/dist/causal-consolidation.js.map +1 -1
- package/dist/causal-retrieval.js +4 -4
- package/dist/causal-trajectory.js +1 -1
- package/dist/{chunk-QNJMBKFK.js → chunk-2LGMW3DJ.js} +3 -2
- package/dist/chunk-2LGMW3DJ.js.map +1 -0
- package/dist/{chunk-QDYXG4CS.js → chunk-3FPTCC3Z.js} +4 -3
- package/dist/chunk-3FPTCC3Z.js.map +1 -0
- package/dist/chunk-3GPTTA4J.js +57 -0
- package/dist/chunk-3GPTTA4J.js.map +1 -0
- package/dist/{chunk-ITRLGI2T.js → chunk-3OGMS3PE.js} +2 -2
- package/dist/{chunk-DEPL3635.js → chunk-3YGHKTBF.js} +1446 -196
- package/dist/chunk-3YGHKTBF.js.map +1 -0
- package/dist/{chunk-BLKTA7MM.js → chunk-4HQS2HPX.js} +54 -21
- package/dist/chunk-4HQS2HPX.js.map +1 -0
- package/dist/chunk-54V4BZWP.js +139 -0
- package/dist/chunk-54V4BZWP.js.map +1 -0
- package/dist/chunk-5JRF2PZA.js +67 -0
- package/dist/chunk-5JRF2PZA.js.map +1 -0
- package/dist/chunk-64NJRYU2.js +332 -0
- package/dist/chunk-64NJRYU2.js.map +1 -0
- package/dist/{chunk-OIT5QGG4.js → chunk-6AUUAZEX.js} +72 -2
- package/dist/chunk-6AUUAZEX.js.map +1 -0
- package/dist/{chunk-3QHL5ABG.js → chunk-6YJHX2DL.js} +191 -10
- package/dist/chunk-6YJHX2DL.js.map +1 -0
- package/dist/chunk-AJU4PJGY.js +126 -0
- package/dist/chunk-AJU4PJGY.js.map +1 -0
- package/dist/chunk-ASAITVLA.js +64 -0
- package/dist/chunk-ASAITVLA.js.map +1 -0
- package/dist/{chunk-44ICJRF3.js → chunk-AYXIPSZO.js} +5 -5
- package/dist/{chunk-MBJHSA7F.js → chunk-BECYBZLX.js} +265 -20
- package/dist/chunk-BECYBZLX.js.map +1 -0
- package/dist/chunk-C4SQJZAF.js +486 -0
- package/dist/chunk-C4SQJZAF.js.map +1 -0
- package/dist/{chunk-6UJ47TVX.js → chunk-CUPFXL3J.js} +2 -2
- package/dist/chunk-DF3RVK3X.js +119 -0
- package/dist/chunk-DF3RVK3X.js.map +1 -0
- package/dist/{chunk-N42IWANG.js → chunk-DG6YMRDC.js} +3 -3
- package/dist/chunk-DGVM5SFL.js +69 -0
- package/dist/chunk-DGVM5SFL.js.map +1 -0
- package/dist/{chunk-3SV6CQHO.js → chunk-DIXB44VE.js} +102 -66
- package/dist/chunk-DIXB44VE.js.map +1 -0
- package/dist/chunk-EIR5VLIH.js +90 -0
- package/dist/chunk-EIR5VLIH.js.map +1 -0
- package/dist/{chunk-GV6NLQ4X.js → chunk-F5VP6YCB.js} +374 -16
- package/dist/chunk-F5VP6YCB.js.map +1 -0
- package/dist/{chunk-6ZH4TU6I.js → chunk-FAAFWE4G.js} +2 -1
- package/dist/chunk-FAAFWE4G.js.map +1 -0
- package/dist/{chunk-7WQ6SLIE.js → chunk-FVA6TGI3.js} +2 -2
- package/dist/{chunk-PAORGQRI.js → chunk-GA5P7RST.js} +37 -23
- package/dist/chunk-GA5P7RST.js.map +1 -0
- package/dist/chunk-GDFS42HT.js +206 -0
- package/dist/chunk-GDFS42HT.js.map +1 -0
- package/dist/chunk-IISBCCWR.js +52 -0
- package/dist/chunk-IISBCCWR.js.map +1 -0
- package/dist/chunk-JBMSGZEQ.js +441 -0
- package/dist/chunk-JBMSGZEQ.js.map +1 -0
- package/dist/{chunk-J4IYOZZ5.js → chunk-JXS5PDQ7.js} +3 -1
- package/dist/chunk-JXS5PDQ7.js.map +1 -0
- package/dist/chunk-KVBLZUKV.js +173 -0
- package/dist/chunk-KVBLZUKV.js.map +1 -0
- package/dist/{chunk-4LACOVZX.js → chunk-L7IXWRYE.js} +10 -5
- package/dist/chunk-L7IXWRYE.js.map +1 -0
- package/dist/chunk-LBLXEFWK.js +51 -0
- package/dist/chunk-LBLXEFWK.js.map +1 -0
- package/dist/{chunk-WBSAYXVI.js → chunk-LOIMBRDE.js} +201 -45
- package/dist/chunk-LOIMBRDE.js.map +1 -0
- package/dist/{chunk-3WHVNEN7.js → chunk-LTCGGW2D.js} +1 -1
- package/dist/chunk-LTCGGW2D.js.map +1 -0
- package/dist/{chunk-ZVBB3T7V.js → chunk-NBVAS5MT.js} +25 -23
- package/dist/chunk-NBVAS5MT.js.map +1 -0
- package/dist/{chunk-UEYA6UC7.js → chunk-NZLQTHS5.js} +25 -2
- package/dist/chunk-NZLQTHS5.js.map +1 -0
- package/dist/{chunk-NQEVYWX6.js → chunk-OC5OXUQ4.js} +211 -7
- package/dist/chunk-OC5OXUQ4.js.map +1 -0
- package/dist/{chunk-LK6SGL53.js → chunk-OR64ZGRZ.js} +3 -2
- package/dist/chunk-OR64ZGRZ.js.map +1 -0
- package/dist/{chunk-SYUK3VLY.js → chunk-PVICZTKG.js} +117 -5
- package/dist/chunk-PVICZTKG.js.map +1 -0
- package/dist/chunk-PVPWZSSI.js +37 -0
- package/dist/chunk-PVPWZSSI.js.map +1 -0
- package/dist/{chunk-JL2PU6AI.js → chunk-R2XRID2N.js} +2 -2
- package/dist/{chunk-4NRAJUDS.js → chunk-RBBWYEFJ.js} +1 -1
- package/dist/chunk-RFYAYKTD.js +146 -0
- package/dist/chunk-RFYAYKTD.js.map +1 -0
- package/dist/chunk-SOBJ6NEY.js +18 -0
- package/dist/chunk-SOBJ6NEY.js.map +1 -0
- package/dist/{chunk-JIU55F3X.js → chunk-SPI27QT6.js} +2 -2
- package/dist/{chunk-MVTHXUBX.js → chunk-STGWEHYR.js} +479 -20
- package/dist/chunk-STGWEHYR.js.map +1 -0
- package/dist/{chunk-6LX5ORAS.js → chunk-TMYO7B5P.js} +4 -4
- package/dist/chunk-TVVEYCNW.js +65 -0
- package/dist/chunk-TVVEYCNW.js.map +1 -0
- package/dist/chunk-ULYOGL6R.js +322 -0
- package/dist/chunk-ULYOGL6R.js.map +1 -0
- package/dist/{chunk-37UIFYWO.js → chunk-UWB5LMWY.js} +108 -9
- package/dist/chunk-UWB5LMWY.js.map +1 -0
- package/dist/{chunk-47UU5PU2.js → chunk-VBVG2M5G.js} +18 -3
- package/dist/chunk-VBVG2M5G.js.map +1 -0
- package/dist/{chunk-7ECD5ATE.js → chunk-VDX363PS.js} +2 -2
- package/dist/{chunk-O5ETUNBT.js → chunk-VTU2B4VF.js} +7 -3
- package/dist/chunk-VTU2B4VF.js.map +1 -0
- package/dist/{chunk-MTLYEMJB.js → chunk-WCLICCGB.js} +18 -3
- package/dist/chunk-WCLICCGB.js.map +1 -0
- package/dist/chunk-X6GF3FX2.js +26 -0
- package/dist/chunk-X6GF3FX2.js.map +1 -0
- package/dist/{chunk-3QFQGRHO.js → chunk-XMHBH5H6.js} +4 -4
- package/dist/{chunk-DHHP2Z4X.js → chunk-XXVWLXSG.js} +2 -2
- package/dist/{chunk-XZ2TIKGC.js → chunk-Y7R2XJ5Q.js} +25 -9
- package/dist/chunk-Y7R2XJ5Q.js.map +1 -0
- package/dist/{chunk-ALXMCZEU.js → chunk-Z2E7VW55.js} +6 -3
- package/dist/chunk-Z2E7VW55.js.map +1 -0
- package/dist/chunk-ZAIM4TUE.js +488 -0
- package/dist/chunk-ZAIM4TUE.js.map +1 -0
- package/dist/chunk-ZZTOURJI.js +91 -0
- package/dist/chunk-ZZTOURJI.js.map +1 -0
- package/dist/{cli-BneVIEvh.d.ts → cli-BkeRaYfk.d.ts} +2 -2
- package/dist/cli.d.ts +13 -6
- package/dist/cli.js +42 -31
- package/dist/config.js +2 -2
- package/dist/consolidation-operator.d.ts +41 -0
- package/dist/consolidation-operator.js +11 -0
- package/dist/consolidation-operator.js.map +1 -0
- package/dist/consolidation-provenance-check.d.ts +68 -0
- package/dist/consolidation-provenance-check.js +9 -0
- package/dist/consolidation-provenance-check.js.map +1 -0
- package/dist/consolidation-undo.d.ts +123 -0
- package/dist/consolidation-undo.js +426 -0
- package/dist/consolidation-undo.js.map +1 -0
- package/dist/{contradiction-scan-GR33PONM.js → contradiction-scan-E3GJTI4F.js} +43 -7
- package/dist/contradiction-scan-E3GJTI4F.js.map +1 -0
- package/dist/cross-namespace-budget.d.ts +133 -0
- package/dist/cross-namespace-budget.js +9 -0
- package/dist/cross-namespace-budget.js.map +1 -0
- package/dist/direct-answer-wiring.js +5 -70
- package/dist/direct-answer-wiring.js.map +1 -1
- package/dist/embedding-fallback.js +2 -1
- package/dist/{engine-5TIQBYZR.js → engine-72LSIWQP.js} +8 -7
- package/dist/engine-72LSIWQP.js.map +1 -0
- package/dist/entity-retrieval.d.ts +1 -0
- package/dist/entity-retrieval.js +7 -6
- package/dist/explicit-capture.d.ts +6 -3
- package/dist/explicit-capture.js +2 -2
- package/dist/extraction-judge-telemetry.d.ts +113 -0
- package/dist/extraction-judge-telemetry.js +14 -0
- package/dist/extraction-judge-telemetry.js.map +1 -0
- package/dist/extraction-judge-training.d.ts +85 -0
- package/dist/extraction-judge-training.js +16 -0
- package/dist/extraction-judge-training.js.map +1 -0
- package/dist/extraction-judge.d.ts +124 -2
- package/dist/extraction-judge.js +11 -1
- package/dist/extraction.js +10 -9
- package/dist/fallback-llm.js +3 -3
- package/dist/graph-recall.d.ts +100 -0
- package/dist/graph-recall.js +8 -0
- package/dist/graph-recall.js.map +1 -0
- package/dist/graph-retrieval.d.ts +271 -0
- package/dist/graph-retrieval.js +21 -0
- package/dist/graph-retrieval.js.map +1 -0
- package/dist/importance.js +1 -1
- package/dist/index.d.ts +585 -20
- package/dist/index.js +542 -344
- package/dist/index.js.map +1 -1
- package/dist/local-llm.js +2 -2
- package/dist/memory-worth-bench.d.ts +51 -0
- package/dist/memory-worth-bench.js +131 -0
- package/dist/memory-worth-bench.js.map +1 -0
- package/dist/memory-worth-filter.d.ts +128 -0
- package/dist/memory-worth-filter.js +10 -0
- package/dist/memory-worth-filter.js.map +1 -0
- package/dist/memory-worth-outcomes.d.ts +118 -0
- package/dist/memory-worth-outcomes.js +9 -0
- package/dist/memory-worth-outcomes.js.map +1 -0
- package/dist/memory-worth.d.ts +102 -0
- package/dist/memory-worth.js +7 -0
- package/dist/memory-worth.js.map +1 -0
- package/dist/operator-toolkit.d.ts +40 -1
- package/dist/operator-toolkit.js +25 -16
- package/dist/{orchestrator-DRYA6_lW.d.ts → orchestrator-CmJ-NTdJ.d.ts} +233 -8
- package/dist/orchestrator.d.ts +6 -3
- package/dist/orchestrator.js +54 -44
- package/dist/page-versioning.d.ts +12 -1
- package/dist/page-versioning.js +5 -3
- package/dist/{port-C1GZFv8h.d.ts → port-BADbLZU5.d.ts} +2 -2
- package/dist/qmd-recall-cache.d.ts +1 -1
- package/dist/qmd.d.ts +5 -3
- package/dist/qmd.js +3 -3
- package/dist/reasoning-trace-recall.d.ts +90 -0
- package/dist/reasoning-trace-recall.js +13 -0
- package/dist/reasoning-trace-recall.js.map +1 -0
- package/dist/reasoning-trace-types.d.ts +54 -0
- package/dist/reasoning-trace-types.js +17 -0
- package/dist/reasoning-trace-types.js.map +1 -0
- package/dist/recall-audit-anomaly.d.ts +112 -0
- package/dist/recall-audit-anomaly.js +11 -0
- package/dist/recall-audit-anomaly.js.map +1 -0
- package/dist/recall-audit.js +5 -44
- package/dist/recall-audit.js.map +1 -1
- package/dist/recall-explain-renderer.d.ts +49 -0
- package/dist/recall-explain-renderer.js +18 -0
- package/dist/recall-explain-renderer.js.map +1 -0
- package/dist/recall-state.d.ts +12 -1
- package/dist/recall-state.js +1 -1
- package/dist/recall-xray-cli.d.ts +40 -0
- package/dist/recall-xray-cli.js +11 -0
- package/dist/recall-xray-cli.js.map +1 -0
- package/dist/recall-xray-renderer.d.ts +44 -0
- package/dist/recall-xray-renderer.js +18 -0
- package/dist/recall-xray-renderer.js.map +1 -0
- package/dist/recall-xray.d.ts +179 -0
- package/dist/recall-xray.js +13 -0
- package/dist/recall-xray.js.map +1 -0
- package/dist/resolve-provider-secret.d.ts +5 -1
- package/dist/resolve-provider-secret.js +3 -1
- package/dist/resume-bundles.js +6 -6
- package/dist/retrieval-agents.d.ts +1 -1
- package/dist/retrieval-tiers.d.ts +17 -0
- package/dist/retrieval-tiers.js +9 -0
- package/dist/retrieval-tiers.js.map +1 -0
- package/dist/schemas.d.ts +309 -53
- package/dist/schemas.js +1 -1
- package/dist/{semantic-consolidation-DrvSYRdB.d.ts → semantic-consolidation-CxJU6MJk.d.ts} +62 -1
- package/dist/semantic-consolidation.d.ts +2 -1
- package/dist/semantic-consolidation.js +22 -7
- package/dist/semantic-rule-promotion.js +7 -6
- package/dist/semantic-rule-verifier.js +7 -6
- package/dist/storage.d.ts +82 -1
- package/dist/storage.js +6 -5
- package/dist/summarizer.js +6 -6
- package/dist/temporal-supersession.d.ts +1 -0
- package/dist/tier-migration.d.ts +2 -1
- package/dist/tokens.js +2 -1
- package/dist/types.d.ts +276 -2
- package/dist/types.js +1 -1
- package/dist/verified-recall.js +7 -6
- package/package.json +1 -1
- package/dist/chunk-37UIFYWO.js.map +0 -1
- package/dist/chunk-3QHL5ABG.js.map +0 -1
- package/dist/chunk-3SV6CQHO.js.map +0 -1
- package/dist/chunk-3WHVNEN7.js.map +0 -1
- package/dist/chunk-47UU5PU2.js.map +0 -1
- package/dist/chunk-4LACOVZX.js.map +0 -1
- package/dist/chunk-6ZH4TU6I.js.map +0 -1
- package/dist/chunk-ALXMCZEU.js.map +0 -1
- package/dist/chunk-BLKTA7MM.js.map +0 -1
- package/dist/chunk-DEPL3635.js.map +0 -1
- package/dist/chunk-GV6NLQ4X.js.map +0 -1
- package/dist/chunk-J4IYOZZ5.js.map +0 -1
- package/dist/chunk-LAYN4LDC.js +0 -267
- package/dist/chunk-LAYN4LDC.js.map +0 -1
- package/dist/chunk-LK6SGL53.js.map +0 -1
- package/dist/chunk-MBJHSA7F.js.map +0 -1
- package/dist/chunk-MTLYEMJB.js.map +0 -1
- package/dist/chunk-MVTHXUBX.js.map +0 -1
- package/dist/chunk-NQEVYWX6.js.map +0 -1
- package/dist/chunk-O5ETUNBT.js.map +0 -1
- package/dist/chunk-OIT5QGG4.js.map +0 -1
- package/dist/chunk-PAORGQRI.js.map +0 -1
- package/dist/chunk-QDYXG4CS.js.map +0 -1
- package/dist/chunk-QNJMBKFK.js.map +0 -1
- package/dist/chunk-SYUK3VLY.js.map +0 -1
- package/dist/chunk-UEYA6UC7.js.map +0 -1
- package/dist/chunk-UVJFDP7P.js +0 -202
- package/dist/chunk-UVJFDP7P.js.map +0 -1
- package/dist/chunk-WBSAYXVI.js.map +0 -1
- package/dist/chunk-XZ2TIKGC.js.map +0 -1
- package/dist/chunk-ZVBB3T7V.js.map +0 -1
- package/dist/contradiction-scan-GR33PONM.js.map +0 -1
- /package/dist/{engine-5TIQBYZR.js.map → access-audit.js.map} +0 -0
- /package/dist/{chunk-ITRLGI2T.js.map → chunk-3OGMS3PE.js.map} +0 -0
- /package/dist/{chunk-44ICJRF3.js.map → chunk-AYXIPSZO.js.map} +0 -0
- /package/dist/{chunk-6UJ47TVX.js.map → chunk-CUPFXL3J.js.map} +0 -0
- /package/dist/{chunk-N42IWANG.js.map → chunk-DG6YMRDC.js.map} +0 -0
- /package/dist/{chunk-7WQ6SLIE.js.map → chunk-FVA6TGI3.js.map} +0 -0
- /package/dist/{chunk-JL2PU6AI.js.map → chunk-R2XRID2N.js.map} +0 -0
- /package/dist/{chunk-4NRAJUDS.js.map → chunk-RBBWYEFJ.js.map} +0 -0
- /package/dist/{chunk-JIU55F3X.js.map → chunk-SPI27QT6.js.map} +0 -0
- /package/dist/{chunk-6LX5ORAS.js.map → chunk-TMYO7B5P.js.map} +0 -0
- /package/dist/{chunk-7ECD5ATE.js.map → chunk-VDX363PS.js.map} +0 -0
- /package/dist/{chunk-3QFQGRHO.js.map → chunk-XMHBH5H6.js.map} +0 -0
- /package/dist/{chunk-DHHP2Z4X.js.map → chunk-XXVWLXSG.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/buffer-surprise.ts"],"sourcesContent":["/**\n * Surprise helper — D-MEM-style novelty score for a turn against recent memories.\n *\n * Used (eventually, in a follow-up PR) by the smart buffer as an additive flush\n * trigger: a turn that is semantically far from everything already in memory is\n * \"surprising\" and worth extracting immediately, even if turn-count and signal\n * heuristics would otherwise keep buffering.\n *\n * This module intentionally contains ONLY the pure scoring function. Wiring\n * into the buffer, configuration flags, telemetry, and benchmark work live in\n * later PRs (see issue #563). Keeping the helper in a sibling file instead of\n * inside `buffer.ts` keeps the buffer module focused and makes the eventual\n * integration a small, reviewable change.\n *\n * # Formula\n *\n * Given an input `turn` and a list of `recentMemories`:\n *\n * 1. Embed the turn and every candidate memory via the caller-provided\n * `embedFn`.\n * 2. For each candidate, compute cosine similarity `cos(turn, candidate)` in\n * `[0, 1]` (negative cosines are clamped to 0 — \"opposite directions\"\n * is treated as maximally diverse, consistent with `recall-mmr.ts`).\n * 3. Keep the top-`k` *highest* similarities (the turn's nearest neighbors).\n * 4. Average those top-`k` similarities → `nearestSim ∈ [0, 1]`.\n * 5. Return `1 − nearestSim` as the surprise score.\n *\n * Intuition: a turn that is close to at least one recent memory has a high\n * `nearestSim` and therefore a *low* surprise (near 0 = redundant). A turn\n * that is far from all of its k nearest neighbors has a low `nearestSim` and\n * therefore a *high* surprise (near 1 = novel).\n *\n * Using the top-k average instead of a single nearest neighbor makes the\n * score less sensitive to one outlier duplicate (e.g. an exact restatement of\n * a stale fact). `k=1` reduces to pure nearest-neighbor distance.\n *\n * # Edge cases\n *\n * - `recentMemories` empty → returns `1.0` (maximally surprising). There is\n * nothing to compare against, so the turn cannot be redundant.\n * - `k` default is `5`. It is clamped to `[1, recentMemories.length]` so that\n * small corpora still produce a meaningful score (e.g. 3 memories with\n * `k=5` behaves the same as `k=3`).\n * - Zero-norm embeddings (all zeros) contribute similarity `0` (they cannot\n * be \"close\" to anything), which is treated as maximally surprising for\n * that pair. The rest of the corpus is scored normally.\n * - Embedding-length mismatches between the turn and a candidate are treated\n * as similarity `0` for that pair. We do not silently truncate to the\n * shorter vector because that would hide a real configuration bug.\n * - `embedFn` rejection is allowed to propagate. The caller decides whether\n * to catch, fall back, or fail the flush decision — this helper has no\n * opinion beyond the pure score.\n *\n * # Purity\n *\n * No I/O, no hidden globals. Given the same inputs and the same `embedFn`,\n * the output is deterministic.\n */\n\nimport { cosineSimilarity } from \"./semantic-chunking.js\";\n\n/** Minimal shape of a recent memory passed to `computeSurprise`. */\nexport interface RecentMemoryLike {\n /** Stable identifier. Only used for debug/logging; not part of the score. */\n readonly id: string;\n /** Text to embed and compare against the incoming turn. */\n readonly content: string;\n}\n\n/** Options accepted by `computeSurprise`. */\nexport interface ComputeSurpriseOptions {\n /**\n * Embedding function. Called once for the turn and once per candidate\n * memory. Callers may wrap a provider client, a local model, or a\n * deterministic hash for tests.\n */\n readonly embedFn: (text: string) => Promise<readonly number[]>;\n /**\n * Number of nearest neighbors to average over. Defaults to 5. Clamped to\n * `[1, recentMemories.length]`.\n */\n readonly k?: number;\n}\n\n/** Default k (top nearest neighbors to average). */\nexport const DEFAULT_SURPRISE_K = 5;\n\n/**\n * Compute a surprise score in `[0, 1]` for `turn` against `recentMemories`.\n *\n * See the module-level docstring for the exact formula, edge cases, and\n * purity guarantees.\n *\n * @param turn The incoming turn text (caller is responsible for\n * stringifying structured turn content — this helper\n * only needs the text to embed).\n * @param recentMemories Candidate memories to compare against.\n * @param options `embedFn` (required) and optional `k` (defaults to 5).\n * @returns A scalar in `[0, 1]`. `1.0` = maximally surprising / novel;\n * `0.0` = redundant with at least one recent memory.\n */\nexport async function computeSurprise(\n turn: string,\n recentMemories: readonly RecentMemoryLike[],\n options: ComputeSurpriseOptions,\n): Promise<number> {\n if (typeof turn !== \"string\") {\n throw new TypeError(\"computeSurprise: `turn` must be a string\");\n }\n if (!options || typeof options.embedFn !== \"function\") {\n throw new TypeError(\n \"computeSurprise: `options.embedFn` is required and must be a function\",\n );\n }\n const candidates = Array.isArray(recentMemories) ? recentMemories : [];\n\n // Empty corpus → maximally surprising. Document decision: we prefer\n // \"novelty by default\" over \"silence\" because the surprise score is meant\n // to *promote* flushing, and an empty buffer of recent memories is the\n // clearest case of \"we have no basis to call this redundant\".\n if (candidates.length === 0) return 1;\n\n const kRaw = typeof options.k === \"number\" ? options.k : DEFAULT_SURPRISE_K;\n // Clamp k to [1, candidates.length]. Non-integer / non-finite values fall\n // back to the default, then get re-clamped below.\n const kClamped = clampK(kRaw, candidates.length);\n\n // Embed the turn and every candidate. Rejections propagate — that's the\n // caller's decision to handle. Run candidate embeddings in parallel so a\n // slow embedder does not serialize the whole pass.\n const turnEmbedding = await options.embedFn(turn);\n const candidateEmbeddings = await Promise.all(\n candidates.map((c) => options.embedFn(c.content)),\n );\n\n const sims: number[] = [];\n for (let i = 0; i < candidates.length; i += 1) {\n const candEmbedding = candidateEmbeddings[i] ?? [];\n sims.push(safeCosine(turnEmbedding, candEmbedding));\n }\n\n // Sort similarities descending and take the top-k (the nearest neighbors).\n sims.sort((a, b) => b - a);\n const top = sims.slice(0, kClamped);\n\n if (top.length === 0) {\n // Defensive — with `candidates.length >= 1` and `kClamped >= 1`, we\n // should always have at least one similarity. If we somehow do not,\n // treat as maximally surprising rather than returning NaN.\n return 1;\n }\n\n let sum = 0;\n for (const s of top) sum += s;\n const nearestSim = sum / top.length;\n\n const surprise = 1 - nearestSim;\n // Numerical safety: clamp the final value. Cosine is clamped to [0, 1]\n // per-pair already, so the mean cannot escape that range under normal\n // arithmetic, but floating-point drift is cheap to guard against.\n if (!Number.isFinite(surprise)) return 1;\n if (surprise < 0) return 0;\n if (surprise > 1) return 1;\n return surprise;\n}\n\n// -----------------------------------------------------------------------------\n// Internals\n// -----------------------------------------------------------------------------\n\nfunction clampK(k: number, ceiling: number): number {\n if (!Number.isFinite(k)) {\n return Math.min(DEFAULT_SURPRISE_K, Math.max(1, ceiling));\n }\n const floored = Math.floor(k);\n if (floored < 1) return 1;\n if (floored > ceiling) return ceiling;\n return floored;\n}\n\n/**\n * Safe wrapper around `cosineSimilarity` that returns a similarity value in\n * `[0, 1]` (negative cosines are clamped to 0, matching the convention used\n * by `recall-mmr.ts`) and tolerates mismatched or empty vectors without\n * throwing. See `semantic-chunking.ts` for the shared cosine implementation.\n *\n * Returns `0` for:\n * - empty vectors,\n * - length mismatches (callers likely have a config bug, but surprise\n * scoring is a soft signal — do not crash the buffer flush decision),\n * - non-finite cosine results (zero-norm inputs, arithmetic drift).\n */\nfunction safeCosine(a: readonly number[], b: readonly number[]): number {\n if (!Array.isArray(a) || !Array.isArray(b)) return 0;\n if (a.length === 0 || b.length === 0) return 0;\n if (a.length !== b.length) return 0;\n const raw = cosineSimilarity(a as number[], b as number[]);\n if (!Number.isFinite(raw)) return 0;\n if (raw < 0) return 0;\n if (raw > 1) return 1;\n return raw;\n}\n\n"],"mappings":";;;;;AA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AO,KAAK,IAAI,oBAAoB,KAAK,IAAI,GAAG,OAAO,CAAC;AAAA,EAC1D;AACA,QAAM,UAAU,KAAK,MAAM,CAAC;AAC5B,MAAI,UAAU,EAAG,QAAO;AACxB,MAAI,UAAU,QAAS,QAAO;AAC9B,SAAO;AACT;AAcA,SAAS,WAAW,GAAsB,GAA8B;AACtE,MAAI,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,EAAG,QAAO;AACnD,MAAI,EAAE,WAAW,KAAK,EAAE,WAAW,EAAG,QAAO;AAC7C,MAAI,EAAE,WAAW,EAAE,OAAQ,QAAO;AAClC,QAAM,MAAM,iBAAiB,GAAe,CAAa;AACzD,MAAI,CAAC,OAAO,SAAS,GAAG,EAAG,QAAO;AAClC,MAAI,MAAM,EAAG,QAAO;AACpB,MAAI,MAAM,EAAG,QAAO;AACpB,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// src/graph-retrieval.ts
|
|
2
|
+
var DEFAULT_PPR_DAMPING = 0.85;
|
|
3
|
+
var DEFAULT_PPR_ITERATIONS = 20;
|
|
4
|
+
var DEFAULT_PPR_TOLERANCE = 1e-6;
|
|
5
|
+
function normalizeSeedWeights(input) {
|
|
6
|
+
const out = /* @__PURE__ */ new Map();
|
|
7
|
+
if (!input) return out;
|
|
8
|
+
const entries = input instanceof Map ? input.entries() : Object.entries(input);
|
|
9
|
+
for (const [k, v] of entries) {
|
|
10
|
+
if (typeof k !== "string" || !k) continue;
|
|
11
|
+
if (typeof v !== "number" || !Number.isFinite(v) || v < 0) continue;
|
|
12
|
+
out.set(k, v);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function buildSeedVector(graph, seedIds, options) {
|
|
17
|
+
const seed = /* @__PURE__ */ new Map();
|
|
18
|
+
const explicitWeights = normalizeSeedWeights(options.seedWeights);
|
|
19
|
+
if (explicitWeights.size > 0) {
|
|
20
|
+
const allowedKeys = seedIds.length > 0 ? new Set(seedIds.filter((id) => typeof id === "string")) : null;
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (const [id, w] of explicitWeights) {
|
|
23
|
+
if (!graph.nodes.has(id)) continue;
|
|
24
|
+
if (allowedKeys !== null && !allowedKeys.has(id)) continue;
|
|
25
|
+
seed.set(id, (seed.get(id) ?? 0) + w);
|
|
26
|
+
total += w;
|
|
27
|
+
}
|
|
28
|
+
if (total > 0) {
|
|
29
|
+
for (const [id, v] of seed) seed.set(id, v / total);
|
|
30
|
+
return seed;
|
|
31
|
+
}
|
|
32
|
+
seed.clear();
|
|
33
|
+
}
|
|
34
|
+
const validSeeds = /* @__PURE__ */ new Set();
|
|
35
|
+
for (const id of seedIds) {
|
|
36
|
+
if (typeof id === "string" && graph.nodes.has(id)) validSeeds.add(id);
|
|
37
|
+
}
|
|
38
|
+
if (validSeeds.size > 0) {
|
|
39
|
+
const share = 1 / validSeeds.size;
|
|
40
|
+
for (const id of validSeeds) seed.set(id, share);
|
|
41
|
+
return seed;
|
|
42
|
+
}
|
|
43
|
+
if (graph.nodes.size > 0) {
|
|
44
|
+
const share = 1 / graph.nodes.size;
|
|
45
|
+
for (const id of graph.nodes.keys()) seed.set(id, share);
|
|
46
|
+
}
|
|
47
|
+
return seed;
|
|
48
|
+
}
|
|
49
|
+
function buildAdjacency(graph) {
|
|
50
|
+
const outgoing = /* @__PURE__ */ new Map();
|
|
51
|
+
const outSum = /* @__PURE__ */ new Map();
|
|
52
|
+
for (const edge of graph.edges) {
|
|
53
|
+
if (!edge || typeof edge.from !== "string" || typeof edge.to !== "string") continue;
|
|
54
|
+
if (!graph.nodes.has(edge.from) || !graph.nodes.has(edge.to)) continue;
|
|
55
|
+
const weight = typeof edge.weight === "number" && Number.isFinite(edge.weight) && edge.weight > 0 ? edge.weight : 1;
|
|
56
|
+
let bucket = outgoing.get(edge.from);
|
|
57
|
+
if (!bucket) {
|
|
58
|
+
bucket = [];
|
|
59
|
+
outgoing.set(edge.from, bucket);
|
|
60
|
+
}
|
|
61
|
+
bucket.push({ to: edge.to, weight });
|
|
62
|
+
outSum.set(edge.from, (outSum.get(edge.from) ?? 0) + weight);
|
|
63
|
+
}
|
|
64
|
+
return { outgoing, outSum };
|
|
65
|
+
}
|
|
66
|
+
function queryGraph(graph, seedIds, options = {}) {
|
|
67
|
+
const n = graph.nodes.size;
|
|
68
|
+
if (n === 0) {
|
|
69
|
+
return { rankedNodes: [], iterations: 0, converged: true };
|
|
70
|
+
}
|
|
71
|
+
let damping = typeof options.damping === "number" ? options.damping : DEFAULT_PPR_DAMPING;
|
|
72
|
+
if (!Number.isFinite(damping) || damping < 0) damping = 0;
|
|
73
|
+
if (damping >= 1) damping = 1 - 1e-9;
|
|
74
|
+
let maxIter = typeof options.iterations === "number" ? Math.floor(options.iterations) : DEFAULT_PPR_ITERATIONS;
|
|
75
|
+
if (!Number.isFinite(maxIter) || maxIter < 0) maxIter = 0;
|
|
76
|
+
let tolerance = typeof options.tolerance === "number" ? options.tolerance : DEFAULT_PPR_TOLERANCE;
|
|
77
|
+
if (!Number.isFinite(tolerance) || tolerance < 0) tolerance = 0;
|
|
78
|
+
const seed = buildSeedVector(graph, seedIds, options);
|
|
79
|
+
const hasExplicitWeights = options.seedWeights !== void 0 && options.seedWeights !== null;
|
|
80
|
+
if (!hasExplicitWeights) {
|
|
81
|
+
let priorTotal = 0;
|
|
82
|
+
for (const [id, s] of seed) {
|
|
83
|
+
const node = graph.nodes.get(id);
|
|
84
|
+
const w = node !== void 0 && typeof node.weight === "number" && Number.isFinite(node.weight) && node.weight > 0 ? node.weight : 1;
|
|
85
|
+
const biased = s * w;
|
|
86
|
+
seed.set(id, biased);
|
|
87
|
+
priorTotal += biased;
|
|
88
|
+
}
|
|
89
|
+
if (priorTotal > 0) {
|
|
90
|
+
for (const [id, s] of seed) seed.set(id, s / priorTotal);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const { outgoing, outSum } = buildAdjacency(graph);
|
|
94
|
+
let rank = /* @__PURE__ */ new Map();
|
|
95
|
+
for (const id of graph.nodes.keys()) rank.set(id, seed.get(id) ?? 0);
|
|
96
|
+
let seedTotal = 0;
|
|
97
|
+
for (const v of seed.values()) seedTotal += v;
|
|
98
|
+
if (seedTotal === 0) {
|
|
99
|
+
return { rankedNodes: [], iterations: 0, converged: true };
|
|
100
|
+
}
|
|
101
|
+
let converged = false;
|
|
102
|
+
let iter = 0;
|
|
103
|
+
for (; iter < maxIter; iter++) {
|
|
104
|
+
const next = /* @__PURE__ */ new Map();
|
|
105
|
+
let danglingMass = 0;
|
|
106
|
+
for (const [id, r] of rank) {
|
|
107
|
+
if ((outSum.get(id) ?? 0) === 0) danglingMass += r;
|
|
108
|
+
}
|
|
109
|
+
const teleportScale = 1 - damping + damping * danglingMass;
|
|
110
|
+
for (const [id, s] of seed) {
|
|
111
|
+
next.set(id, (next.get(id) ?? 0) + teleportScale * s);
|
|
112
|
+
}
|
|
113
|
+
for (const [from, edges] of outgoing) {
|
|
114
|
+
const r = rank.get(from) ?? 0;
|
|
115
|
+
if (r === 0) continue;
|
|
116
|
+
const total = outSum.get(from) ?? 0;
|
|
117
|
+
if (total === 0) continue;
|
|
118
|
+
const share = damping * r / total;
|
|
119
|
+
for (const { to, weight } of edges) {
|
|
120
|
+
next.set(to, (next.get(to) ?? 0) + share * weight);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let delta = 0;
|
|
124
|
+
for (const id of graph.nodes.keys()) {
|
|
125
|
+
delta += Math.abs((next.get(id) ?? 0) - (rank.get(id) ?? 0));
|
|
126
|
+
}
|
|
127
|
+
rank = next;
|
|
128
|
+
if (delta < tolerance) {
|
|
129
|
+
converged = true;
|
|
130
|
+
iter += 1;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const ranked = [];
|
|
135
|
+
for (const [id, score] of rank) {
|
|
136
|
+
if (score > 0) ranked.push({ id, score });
|
|
137
|
+
}
|
|
138
|
+
ranked.sort((a, b) => {
|
|
139
|
+
if (b.score !== a.score) return b.score - a.score;
|
|
140
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
141
|
+
});
|
|
142
|
+
const topK = options.topK;
|
|
143
|
+
let trimmed;
|
|
144
|
+
if (typeof topK === "number") {
|
|
145
|
+
if (topK <= 0) {
|
|
146
|
+
trimmed = [];
|
|
147
|
+
} else if (topK < ranked.length) {
|
|
148
|
+
trimmed = ranked.slice(0, topK);
|
|
149
|
+
} else {
|
|
150
|
+
trimmed = ranked;
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
trimmed = ranked;
|
|
154
|
+
}
|
|
155
|
+
return { rankedNodes: trimmed, iterations: iter, converged };
|
|
156
|
+
}
|
|
157
|
+
var NODE_TYPES = /* @__PURE__ */ new Set([
|
|
158
|
+
"memory",
|
|
159
|
+
"entity",
|
|
160
|
+
"episode",
|
|
161
|
+
"concept",
|
|
162
|
+
"reflection"
|
|
163
|
+
]);
|
|
164
|
+
var EDGE_TYPES = /* @__PURE__ */ new Set([
|
|
165
|
+
"references",
|
|
166
|
+
"supersedes",
|
|
167
|
+
"authored-by",
|
|
168
|
+
"mentions",
|
|
169
|
+
"derived-from",
|
|
170
|
+
"temporal-next",
|
|
171
|
+
"related-to"
|
|
172
|
+
]);
|
|
173
|
+
function isNodeType(value) {
|
|
174
|
+
return typeof value === "string" && NODE_TYPES.has(value);
|
|
175
|
+
}
|
|
176
|
+
function isEdgeType(value) {
|
|
177
|
+
return typeof value === "string" && EDGE_TYPES.has(value);
|
|
178
|
+
}
|
|
179
|
+
var CITATION_REGEX = /\[Source:([^\]\n[]+)\]/gi;
|
|
180
|
+
function parseCitationFields(body) {
|
|
181
|
+
const out = {};
|
|
182
|
+
for (const rawPart of body.split(",")) {
|
|
183
|
+
const part = rawPart.trim();
|
|
184
|
+
if (!part) continue;
|
|
185
|
+
const eq = part.indexOf("=");
|
|
186
|
+
if (eq <= 0) continue;
|
|
187
|
+
const key = part.slice(0, eq).trim().toLowerCase();
|
|
188
|
+
const value = part.slice(eq + 1).trim();
|
|
189
|
+
if (key && value) out[key] = value;
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
function stripDerivedFromVersion(ref) {
|
|
194
|
+
const colon = ref.lastIndexOf(":");
|
|
195
|
+
if (colon < 0) return ref;
|
|
196
|
+
const tail = ref.slice(colon + 1);
|
|
197
|
+
if (tail.length === 0) return ref.slice(0, colon);
|
|
198
|
+
if (/^\d+$/.test(tail)) return ref.slice(0, colon);
|
|
199
|
+
return ref;
|
|
200
|
+
}
|
|
201
|
+
function extractGraphEdges(memories, options = {}) {
|
|
202
|
+
const includeDangling = options.includeDanglingEdges === true;
|
|
203
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
204
|
+
const edges = [];
|
|
205
|
+
const seenEdgeKeys = /* @__PURE__ */ new Set();
|
|
206
|
+
const addNode = (id, type) => {
|
|
207
|
+
if (!nodes.has(id)) nodes.set(id, { id, type });
|
|
208
|
+
};
|
|
209
|
+
const addEdge = (from, to, type) => {
|
|
210
|
+
if (!from || !to || from === to) return;
|
|
211
|
+
const key = `${from}\0${to}\0${type}`;
|
|
212
|
+
if (seenEdgeKeys.has(key)) return;
|
|
213
|
+
seenEdgeKeys.add(key);
|
|
214
|
+
edges.push({ from, to, type });
|
|
215
|
+
};
|
|
216
|
+
for (const memory of memories) {
|
|
217
|
+
if (!memory?.id) continue;
|
|
218
|
+
addNode(memory.id, "memory");
|
|
219
|
+
}
|
|
220
|
+
const entityClaimed = /* @__PURE__ */ new Set();
|
|
221
|
+
for (const memory of memories) {
|
|
222
|
+
if (!memory?.id) continue;
|
|
223
|
+
if (typeof memory.entityRef === "string" && memory.entityRef) {
|
|
224
|
+
if (memory.entityRef !== memory.id) entityClaimed.add(memory.entityRef);
|
|
225
|
+
}
|
|
226
|
+
if (Array.isArray(memory.entityRefs)) {
|
|
227
|
+
for (const ref of memory.entityRefs) {
|
|
228
|
+
if (typeof ref === "string" && ref && ref !== memory.id) {
|
|
229
|
+
entityClaimed.add(ref);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (typeof memory.content === "string" && memory.content.length > 0) {
|
|
234
|
+
CITATION_REGEX.lastIndex = 0;
|
|
235
|
+
let match;
|
|
236
|
+
while ((match = CITATION_REGEX.exec(memory.content)) !== null) {
|
|
237
|
+
const body = match[1];
|
|
238
|
+
if (!body) continue;
|
|
239
|
+
const agent = parseCitationFields(body).agent;
|
|
240
|
+
if (!agent) continue;
|
|
241
|
+
const agentId = `agent:${agent}`;
|
|
242
|
+
if (agentId !== memory.id) entityClaimed.add(agentId);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
for (const memory of memories) {
|
|
247
|
+
if (memory?.id) entityClaimed.delete(memory.id);
|
|
248
|
+
}
|
|
249
|
+
for (const memory of memories) {
|
|
250
|
+
if (!memory?.id) continue;
|
|
251
|
+
const from = memory.id;
|
|
252
|
+
const canTargetMemory = (id) => {
|
|
253
|
+
const existing = nodes.get(id);
|
|
254
|
+
if (existing !== void 0) return existing.type === "memory";
|
|
255
|
+
if (entityClaimed.has(id)) return false;
|
|
256
|
+
return includeDangling;
|
|
257
|
+
};
|
|
258
|
+
if (typeof memory.supersedes === "string" && memory.supersedes) {
|
|
259
|
+
const to = memory.supersedes;
|
|
260
|
+
if (canTargetMemory(to)) {
|
|
261
|
+
if (!nodes.has(to)) addNode(to, "memory");
|
|
262
|
+
addEdge(from, to, "supersedes");
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (Array.isArray(memory.lineage)) {
|
|
266
|
+
for (const parent of memory.lineage) {
|
|
267
|
+
if (typeof parent !== "string" || !parent) continue;
|
|
268
|
+
if (!canTargetMemory(parent)) continue;
|
|
269
|
+
if (!nodes.has(parent)) addNode(parent, "memory");
|
|
270
|
+
addEdge(from, parent, "derived-from");
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (Array.isArray(memory.derived_from)) {
|
|
274
|
+
for (const raw of memory.derived_from) {
|
|
275
|
+
if (typeof raw !== "string" || !raw) continue;
|
|
276
|
+
const to = stripDerivedFromVersion(raw);
|
|
277
|
+
if (!to) continue;
|
|
278
|
+
if (!canTargetMemory(to)) continue;
|
|
279
|
+
if (!nodes.has(to)) addNode(to, "memory");
|
|
280
|
+
addEdge(from, to, "derived-from");
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const entitySet = /* @__PURE__ */ new Set();
|
|
284
|
+
if (typeof memory.entityRef === "string" && memory.entityRef) {
|
|
285
|
+
entitySet.add(memory.entityRef);
|
|
286
|
+
}
|
|
287
|
+
if (Array.isArray(memory.entityRefs)) {
|
|
288
|
+
for (const ref of memory.entityRefs) {
|
|
289
|
+
if (typeof ref === "string" && ref) entitySet.add(ref);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
for (const ref of entitySet) {
|
|
293
|
+
const existing = nodes.get(ref);
|
|
294
|
+
if (existing !== void 0 && existing.type !== "entity") continue;
|
|
295
|
+
if (!existing) addNode(ref, "entity");
|
|
296
|
+
addEdge(from, ref, "mentions");
|
|
297
|
+
}
|
|
298
|
+
if (typeof memory.content === "string" && memory.content.length > 0) {
|
|
299
|
+
CITATION_REGEX.lastIndex = 0;
|
|
300
|
+
let match;
|
|
301
|
+
while ((match = CITATION_REGEX.exec(memory.content)) !== null) {
|
|
302
|
+
const body = match[1];
|
|
303
|
+
if (!body) continue;
|
|
304
|
+
const fields = parseCitationFields(body);
|
|
305
|
+
const agent = fields.agent;
|
|
306
|
+
if (!agent) continue;
|
|
307
|
+
const agentId = `agent:${agent}`;
|
|
308
|
+
const existing = nodes.get(agentId);
|
|
309
|
+
if (existing !== void 0 && existing.type !== "entity") continue;
|
|
310
|
+
if (!existing) addNode(agentId, "entity");
|
|
311
|
+
addEdge(from, agentId, "authored-by");
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return { nodes, edges };
|
|
316
|
+
}
|
|
317
|
+
function buildGraphFromMemories(memories, options = {}) {
|
|
318
|
+
const { nodes, edges } = extractGraphEdges(memories, options);
|
|
319
|
+
return { nodes, edges };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export {
|
|
323
|
+
DEFAULT_PPR_DAMPING,
|
|
324
|
+
DEFAULT_PPR_ITERATIONS,
|
|
325
|
+
DEFAULT_PPR_TOLERANCE,
|
|
326
|
+
queryGraph,
|
|
327
|
+
isNodeType,
|
|
328
|
+
isEdgeType,
|
|
329
|
+
extractGraphEdges,
|
|
330
|
+
buildGraphFromMemories
|
|
331
|
+
};
|
|
332
|
+
//# sourceMappingURL=chunk-64NJRYU2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/graph-retrieval.ts"],"sourcesContent":["/**\n * Graph-based retrieval types (issue #559, PR 1 of 5).\n *\n * This module defines the forward-looking type contract for Remnic's\n * first-class retrieval graph (GAAMA / GAM inspired). It ships types\n * and a no-op `queryGraph()` stub only — no behavior, no I/O, and\n * no importers inside the codebase yet.\n *\n * Subsequent slices will land:\n * - PR 2: Edge extraction from existing relationship facts + cross-memory\n * entity references during indexing (writes `~/.remnic/graph.json`).\n * - PR 3: Pure `personalizedPageRank()` implementation.\n * - PR 4: Feature-flagged wiring into `retrieval.ts` behind\n * `graphRetrievalEnabled` (default `false`).\n * - PR 5: LoCoMo A/B bench harness + default flip decision.\n *\n * Keeping the node/edge type enums complete from PR 1 avoids type churn\n * when later slices add reflection/concept node synthesis or additional\n * edge semantics.\n */\n\n// ---------------------------------------------------------------------------\n// Node & edge type enums\n// ---------------------------------------------------------------------------\n\n/**\n * Kinds of nodes the retrieval graph can hold.\n *\n * - `memory`: a stored memory file (the primary retrieval target).\n * - `entity`: a named entity referenced by one or more memories.\n * - `episode`: a temporally-bounded interaction or session grouping.\n * - `concept`: an abstract topic / idea (forward-looking; synthesis\n * is out of scope for this issue, but the type is\n * defined here to keep later slices additive).\n * - `reflection`: an LLM-generated summary / meta-memory about other\n * nodes (also forward-looking, same rationale).\n */\nexport type NodeType =\n | \"memory\"\n | \"entity\"\n | \"episode\"\n | \"concept\"\n | \"reflection\";\n\n/**\n * Kinds of edges the retrieval graph can hold.\n *\n * Edges are directed. `from` → `to` semantics:\n *\n * - `references`: `from` contains an explicit reference to `to`\n * (e.g., a memory referencing another memory).\n * - `supersedes`: `from` supersedes `to` (newer memory replaces older).\n * - `authored-by`: `from` was authored by the entity in `to`.\n * - `mentions`: `from` mentions the entity/concept in `to` without\n * a stronger relationship claim.\n * - `derived-from`: `from` was derived from `to` (reflections,\n * consolidations, summaries).\n * - `temporal-next`: `from` immediately follows `to` in time (episodes).\n * - `related-to`: generic weak relationship fallback for edges that\n * do not fit a stronger type.\n */\nexport type EdgeType =\n | \"references\"\n | \"supersedes\"\n | \"authored-by\"\n | \"mentions\"\n | \"derived-from\"\n | \"temporal-next\"\n | \"related-to\";\n\n// ---------------------------------------------------------------------------\n// Graph element shapes\n// ---------------------------------------------------------------------------\n\n/**\n * A single node in the retrieval graph.\n *\n * `id` is the caller-controlled stable identifier (typically a memory\n * file path, entity slug, or episode id). `weight` is an optional prior\n * importance score used as a starting bias during Personalized PageRank;\n * it is intentionally optional because most nodes default to uniform\n * priors.\n *\n * Named `RemnicGraphNode` (not `GraphNode`) to avoid colliding with the\n * unrelated `GraphEdge` in `graph.ts`, which models Multi-Graph Memory\n * (MAGMA/SYNAPSE) edges and is an incompatible shape.\n */\nexport interface RemnicGraphNode {\n id: string;\n type: NodeType;\n weight?: number;\n}\n\n/**\n * A directed edge between two nodes.\n *\n * `weight` is optional; when absent PPR implementations should treat\n * the edge as weight `1`. We keep weight optional rather than defaulting\n * at construction so producers can serialize a minimal edge shape.\n *\n * Named `RemnicGraphEdge` (not `GraphEdge`) to avoid colliding with the\n * unrelated `GraphEdge` in `graph.ts` (Multi-Graph Memory).\n */\nexport interface RemnicGraphEdge {\n from: string;\n to: string;\n type: EdgeType;\n weight?: number;\n}\n\n/**\n * The retrieval graph itself.\n *\n * Nodes are held in a `Map<string, RemnicGraphNode>` keyed by `id` so\n * PPR lookups are O(1). Edges are kept as a flat array; adjacency\n * indexing is a PR-3 concern (PPR will likely build a transient\n * outgoing-adjacency map on demand).\n */\nexport interface RemnicGraph {\n nodes: Map<string, RemnicGraphNode>;\n edges: RemnicGraphEdge[];\n}\n\n// ---------------------------------------------------------------------------\n// Query surface (stub)\n// ---------------------------------------------------------------------------\n\n/**\n * Options for `queryGraph()`.\n *\n * Defaults (applied when undefined):\n * - `damping`: 0.85 (standard PageRank value — higher values make the\n * random walk follow edges longer before teleporting back\n * to the seed distribution).\n * - `iterations`: 20 (power-iteration cap).\n * - `tolerance`: 1e-6 (L1 convergence threshold; iteration stops early\n * when the L1 norm of the delta between successive\n * rank vectors falls below this).\n * - `topK`: unbounded — all nodes with positive score are returned,\n * ordered by descending score.\n */\nexport interface QueryGraphOptions {\n /** Number of top-ranked nodes to return. Defaults to unbounded. */\n topK?: number;\n /**\n * PPR damping factor in (0, 1). The probability of following an outgoing\n * edge at each step (vs. teleporting back to the seed distribution).\n * Defaults to 0.85.\n */\n damping?: number;\n /** Maximum PPR iterations before falling back to current rank vector. */\n iterations?: number;\n /** L1 convergence threshold. Defaults to 1e-6. */\n tolerance?: number;\n /**\n * Optional per-seed weights. Keys must appear in `seedIds` or (if empty)\n * in the graph. Values must be non-negative. Weights are normalized so\n * they sum to 1 before seeding. If omitted, seed mass is distributed\n * uniformly across `seedIds`.\n */\n seedWeights?: ReadonlyMap<string, number> | Readonly<Record<string, number>>;\n}\n\n/**\n * A scored node returned by `queryGraph()`.\n */\nexport interface RankedGraphNode {\n id: string;\n score: number;\n}\n\n/**\n * The shape returned by `queryGraph()`.\n */\nexport interface QueryGraphResult {\n rankedNodes: RankedGraphNode[];\n /** Number of power-iteration rounds actually executed. */\n iterations: number;\n /** L1 delta at the last iteration. */\n converged: boolean;\n}\n\n/** PPR damping factor default. */\nexport const DEFAULT_PPR_DAMPING = 0.85;\n/** Power-iteration cap default. */\nexport const DEFAULT_PPR_ITERATIONS = 20;\n/** L1 convergence threshold default. */\nexport const DEFAULT_PPR_TOLERANCE = 1e-6;\n\n/**\n * Normalize a `seedWeights` option (Map or plain object) to a plain Map.\n * Silently drops non-finite / negative / non-numeric values. Returns an\n * empty map if the input is undefined.\n */\nfunction normalizeSeedWeights(\n input: QueryGraphOptions[\"seedWeights\"],\n): Map<string, number> {\n const out = new Map<string, number>();\n if (!input) return out;\n const entries: Iterable<[string, unknown]> =\n input instanceof Map\n ? input.entries()\n : Object.entries(input as Record<string, unknown>);\n for (const [k, v] of entries) {\n if (typeof k !== \"string\" || !k) continue;\n if (typeof v !== \"number\" || !Number.isFinite(v) || v < 0) continue;\n out.set(k, v);\n }\n return out;\n}\n\n/**\n * Build the seed probability vector.\n *\n * Strategy:\n * 1. If the caller supplied `seedWeights`, restrict to keys present in\n * the graph, sum, and normalize.\n * 2. Otherwise, take the subset of `seedIds` present in the graph and\n * assign each an equal 1/n share.\n * 3. If neither produces any in-graph mass, fall back to a uniform\n * distribution over all graph nodes (matches standard PageRank).\n */\nfunction buildSeedVector(\n graph: RemnicGraph,\n seedIds: readonly string[],\n options: QueryGraphOptions,\n): Map<string, number> {\n const seed = new Map<string, number>();\n\n const explicitWeights = normalizeSeedWeights(options.seedWeights);\n if (explicitWeights.size > 0) {\n // Restrict weight keys to the declared seed set (or, if empty, to\n // graph nodes). Documented contract in `QueryGraphOptions.seedWeights`:\n // keys must appear in `seedIds` — unrelated / stale weight entries\n // must not silently override the requested personalization.\n const allowedKeys =\n seedIds.length > 0\n ? new Set(seedIds.filter((id) => typeof id === \"string\"))\n : null;\n let total = 0;\n for (const [id, w] of explicitWeights) {\n if (!graph.nodes.has(id)) continue;\n if (allowedKeys !== null && !allowedKeys.has(id)) continue;\n seed.set(id, (seed.get(id) ?? 0) + w);\n total += w;\n }\n if (total > 0) {\n for (const [id, v] of seed) seed.set(id, v / total);\n return seed;\n }\n seed.clear();\n }\n\n // Deduplicate before computing shares — `[\"a\", \"a\", \"b\"]` must behave\n // identically to `[\"a\", \"b\"]`. Computing `share` against a de-duped set\n // is the only way to guarantee that; renormalizing after the fact is\n // a no-op because the pre-dedup shares already sum to 1.\n const validSeeds = new Set<string>();\n for (const id of seedIds) {\n if (typeof id === \"string\" && graph.nodes.has(id)) validSeeds.add(id);\n }\n if (validSeeds.size > 0) {\n const share = 1 / validSeeds.size;\n for (const id of validSeeds) seed.set(id, share);\n return seed;\n }\n\n // Uniform fallback over all graph nodes.\n if (graph.nodes.size > 0) {\n const share = 1 / graph.nodes.size;\n for (const id of graph.nodes.keys()) seed.set(id, share);\n }\n return seed;\n}\n\n/**\n * Build an out-adjacency index with summed outgoing edge weights per source.\n *\n * Missing `weight` on an edge defaults to `1`. Edges referencing nodes that\n * are not in `graph.nodes` are skipped (dangling edges cannot propagate\n * mass). The returned `outSum` map lets the PPR loop divide once per source\n * instead of re-summing per iteration.\n */\nfunction buildAdjacency(graph: RemnicGraph): {\n outgoing: Map<string, { to: string; weight: number }[]>;\n outSum: Map<string, number>;\n} {\n const outgoing = new Map<string, { to: string; weight: number }[]>();\n const outSum = new Map<string, number>();\n for (const edge of graph.edges) {\n if (!edge || typeof edge.from !== \"string\" || typeof edge.to !== \"string\") continue;\n if (!graph.nodes.has(edge.from) || !graph.nodes.has(edge.to)) continue;\n const weight =\n typeof edge.weight === \"number\" && Number.isFinite(edge.weight) && edge.weight > 0\n ? edge.weight\n : 1;\n let bucket = outgoing.get(edge.from);\n if (!bucket) {\n bucket = [];\n outgoing.set(edge.from, bucket);\n }\n bucket.push({ to: edge.to, weight });\n outSum.set(edge.from, (outSum.get(edge.from) ?? 0) + weight);\n }\n return { outgoing, outSum };\n}\n\n/**\n * Personalized PageRank via power iteration.\n *\n * Pure function — no I/O. Deterministic given the same graph and options.\n *\n * Algorithm:\n *\n * r_{t+1}(v) = (1 - d) * s(v)\n * + d * Σ_{u → v} r_t(u) * w(u,v) / Σ_w w(u,·)\n * + d * (dangling mass) * s(v)\n *\n * where:\n * - `d` is the damping factor (default 0.85).\n * - `s` is the seed vector (normalized personalization distribution).\n * - dangling mass is the total rank on nodes with no outgoing edges,\n * redistributed over the seed vector so probability mass is conserved.\n *\n * The loop stops early when `|r_{t+1} - r_t|_1 < tolerance` or after\n * `iterations` rounds, whichever comes first.\n *\n * Edge cases:\n * - Empty graph → `{ rankedNodes: [], iterations: 0, converged: true }`.\n * - Seed ids that are not in the graph are silently dropped.\n * - If no in-graph seed mass remains (empty seed or all seeds missing),\n * the uniform distribution over graph nodes is used — matching\n * standard PageRank semantics.\n * - `damping` is clamped to `[0, 1)` (a damping of exactly 1 would make\n * the chain non-ergodic; damping of exactly 0 reduces to the seed\n * distribution).\n * - `topK <= 0` returns an empty ranked list (but the `iterations` and\n * `converged` fields still reflect the actual computation).\n */\nexport function queryGraph(\n graph: RemnicGraph,\n seedIds: readonly string[],\n options: QueryGraphOptions = {},\n): QueryGraphResult {\n const n = graph.nodes.size;\n if (n === 0) {\n return { rankedNodes: [], iterations: 0, converged: true };\n }\n\n // Clamp options.\n let damping = typeof options.damping === \"number\" ? options.damping : DEFAULT_PPR_DAMPING;\n if (!Number.isFinite(damping) || damping < 0) damping = 0;\n if (damping >= 1) damping = 1 - 1e-9;\n\n let maxIter = typeof options.iterations === \"number\" ? Math.floor(options.iterations) : DEFAULT_PPR_ITERATIONS;\n if (!Number.isFinite(maxIter) || maxIter < 0) maxIter = 0;\n\n let tolerance = typeof options.tolerance === \"number\" ? options.tolerance : DEFAULT_PPR_TOLERANCE;\n if (!Number.isFinite(tolerance) || tolerance < 0) tolerance = 0;\n\n const seed = buildSeedVector(graph, seedIds, options);\n\n // Apply `RemnicGraphNode.weight` as a starting-rank prior — BUT only\n // when the caller did not supply explicit `seedWeights`. Explicit\n // caller-supplied weights must not be silently re-biased by node\n // priors (would double-count and contradict the documented contract\n // for `seedWeights`). When no explicit weights are given, multiply\n // each seed entry by its node weight (defaulting missing / non-\n // positive / non-finite weights to 1) and renormalize.\n const hasExplicitWeights =\n options.seedWeights !== undefined && options.seedWeights !== null;\n if (!hasExplicitWeights) {\n let priorTotal = 0;\n for (const [id, s] of seed) {\n const node = graph.nodes.get(id);\n const w =\n node !== undefined &&\n typeof node.weight === \"number\" &&\n Number.isFinite(node.weight) &&\n node.weight > 0\n ? node.weight\n : 1;\n const biased = s * w;\n seed.set(id, biased);\n priorTotal += biased;\n }\n if (priorTotal > 0) {\n for (const [id, s] of seed) seed.set(id, s / priorTotal);\n }\n }\n\n const { outgoing, outSum } = buildAdjacency(graph);\n\n // Initialize rank vector = seed.\n let rank = new Map<string, number>();\n for (const id of graph.nodes.keys()) rank.set(id, seed.get(id) ?? 0);\n // If the seed vector is empty (e.g. graph has no nodes matching seeds\n // AND the uniform fallback was somehow empty — shouldn't happen when\n // n > 0, but defensively): return empty ranking.\n let seedTotal = 0;\n for (const v of seed.values()) seedTotal += v;\n if (seedTotal === 0) {\n return { rankedNodes: [], iterations: 0, converged: true };\n }\n\n let converged = false;\n let iter = 0;\n for (; iter < maxIter; iter++) {\n const next = new Map<string, number>();\n\n // Sum of rank on dangling nodes (no outgoing edges) — redistribute\n // proportional to the seed vector to conserve probability mass.\n let danglingMass = 0;\n for (const [id, r] of rank) {\n if ((outSum.get(id) ?? 0) === 0) danglingMass += r;\n }\n\n // Teleport contribution: (1 - d) * s(v) + d * danglingMass * s(v).\n const teleportScale = 1 - damping + damping * danglingMass;\n for (const [id, s] of seed) {\n next.set(id, (next.get(id) ?? 0) + teleportScale * s);\n }\n\n // Edge contribution: d * r(u) * w(u,v) / Σ w(u,·).\n for (const [from, edges] of outgoing) {\n const r = rank.get(from) ?? 0;\n if (r === 0) continue;\n const total = outSum.get(from) ?? 0;\n if (total === 0) continue;\n const share = (damping * r) / total;\n for (const { to, weight } of edges) {\n next.set(to, (next.get(to) ?? 0) + share * weight);\n }\n }\n\n // Compute L1 delta.\n let delta = 0;\n for (const id of graph.nodes.keys()) {\n delta += Math.abs((next.get(id) ?? 0) - (rank.get(id) ?? 0));\n }\n\n rank = next;\n if (delta < tolerance) {\n converged = true;\n iter += 1; // record that this iteration completed\n break;\n }\n }\n\n // Rank and trim.\n const ranked: RankedGraphNode[] = [];\n for (const [id, score] of rank) {\n if (score > 0) ranked.push({ id, score });\n }\n // Sort by descending score, ties broken by id for determinism.\n ranked.sort((a, b) => {\n if (b.score !== a.score) return b.score - a.score;\n return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;\n });\n\n const topK = options.topK;\n let trimmed: RankedGraphNode[];\n if (typeof topK === \"number\") {\n if (topK <= 0) {\n trimmed = [];\n } else if (topK < ranked.length) {\n trimmed = ranked.slice(0, topK);\n } else {\n trimmed = ranked;\n }\n } else {\n trimmed = ranked;\n }\n\n return { rankedNodes: trimmed, iterations: iter, converged };\n}\n\n// ---------------------------------------------------------------------------\n// Type guards (useful for defensive construction from untyped JSON)\n// ---------------------------------------------------------------------------\n\nconst NODE_TYPES: ReadonlySet<NodeType> = new Set<NodeType>([\n \"memory\",\n \"entity\",\n \"episode\",\n \"concept\",\n \"reflection\",\n]);\n\nconst EDGE_TYPES: ReadonlySet<EdgeType> = new Set<EdgeType>([\n \"references\",\n \"supersedes\",\n \"authored-by\",\n \"mentions\",\n \"derived-from\",\n \"temporal-next\",\n \"related-to\",\n]);\n\n/** Returns true iff `value` is a valid `NodeType`. */\nexport function isNodeType(value: unknown): value is NodeType {\n return typeof value === \"string\" && NODE_TYPES.has(value as NodeType);\n}\n\n/** Returns true iff `value` is a valid `EdgeType`. */\nexport function isEdgeType(value: unknown): value is EdgeType {\n return typeof value === \"string\" && EDGE_TYPES.has(value as EdgeType);\n}\n\n// ---------------------------------------------------------------------------\n// Edge extraction (issue #559, PR 2 of 5)\n// ---------------------------------------------------------------------------\n\n/**\n * Minimum fields the edge extractor reads from a memory record. Structural\n * typing is used so callers can pass any subset of `MemoryFrontmatter`\n * (including richer loaded memories) without a cast.\n *\n * All reference fields are optional — memories written before earlier slices\n * landed will simply contribute no edges for those dimensions.\n */\nexport interface MemoryEdgeSource {\n /** Stable identifier for the memory (typically the file path). */\n id: string;\n /** Older memory id this memory supersedes (1:1). */\n supersedes?: string;\n /** Parent memory ids this memory was derived from (lineage). */\n lineage?: string[];\n /**\n * Consolidation provenance — `\"<memory-id>:<version-number>\"` strings.\n * The memory-id portion before the last `:` is used as the edge target.\n */\n derived_from?: string[];\n /** Primary entity reference on the memory (e.g. `person:Jane Doe`). */\n entityRef?: string;\n /** Additional entity references (used by episodes and ledger records). */\n entityRefs?: string[];\n /** Raw memory body — scanned for inline `[Source: ...]` citation blocks. */\n content?: string;\n}\n\n/** Options controlling edge extraction. */\nexport interface ExtractGraphEdgesOptions {\n /**\n * When true, include edges whose `to` endpoint is not present in the\n * provided node index. Defaults to `false` — dangling edges are silently\n * skipped because PPR cannot propagate mass through a missing node.\n */\n includeDanglingEdges?: boolean;\n}\n\n/**\n * Regex that matches the `[Source: ...]` citation block emitted by\n * `source-attribution.ts`. Kept local (rather than importing) because the\n * extractor intentionally has no dependency on the citation module's mutable\n * template configuration — it only recognizes the default shape plus minor\n * whitespace / ordering variants.\n */\n// Non-greedy quantifiers on classes like `[^\\]\\n]+?` can be polynomial on\n// pathological inputs (CodeQL rule js/polynomial-redos). Using a greedy\n// quantifier with a negated character class that also excludes `[` prevents\n// nested-bracket inputs from forcing catastrophic backtracking, and the `\\s*`\n// after `Source:` is bounded by the terminal `]`.\nconst CITATION_REGEX = /\\[Source:([^\\]\\n[]+)\\]/gi;\n\n/**\n * Parse `key=value` pairs out of a citation body. Whitespace-tolerant and\n * case-insensitive: keys are normalized to lowercase so callers can do\n * `fields.agent` without worrying about `[Source: Agent=...]` variants.\n */\nfunction parseCitationFields(body: string): Record<string, string> {\n const out: Record<string, string> = {};\n for (const rawPart of body.split(\",\")) {\n const part = rawPart.trim();\n if (!part) continue;\n const eq = part.indexOf(\"=\");\n if (eq <= 0) continue;\n const key = part.slice(0, eq).trim().toLowerCase();\n const value = part.slice(eq + 1).trim();\n if (key && value) out[key] = value;\n }\n return out;\n}\n\n/**\n * Strip the trailing `:<version>` from a `derived_from` entry. Returns the\n * original string if no colon is present. We intentionally use `lastIndexOf`\n * because memory ids may themselves contain colons (e.g. entity-prefixed\n * ids), but the version suffix — when present — is always the final\n * `:<digits>` segment.\n */\nfunction stripDerivedFromVersion(ref: string): string {\n const colon = ref.lastIndexOf(\":\");\n if (colon < 0) return ref;\n const tail = ref.slice(colon + 1);\n if (tail.length === 0) return ref.slice(0, colon);\n // Only strip when the tail is purely numeric; otherwise keep the full ref.\n if (/^\\d+$/.test(tail)) return ref.slice(0, colon);\n return ref;\n}\n\n/**\n * Extract retrieval-graph edges from a collection of memories.\n *\n * Pure function — no I/O, no config access, no time-based side effects.\n * Given the same inputs, always produces the same edges in the same order\n * so dedup downstream is deterministic.\n *\n * Source → target semantics by edge type:\n *\n * - `supersedes`: memory → older memory (from `supersedes` field).\n * - `derived-from`: memory → each parent in `lineage` OR `derived_from`.\n * - `mentions`: memory → each entity in `entityRef` / `entityRefs`.\n * - `authored-by`: memory → agent id parsed from inline `[Source: ...]`.\n *\n * `temporal-next`, `references`, `related-to`, and `concept` / `reflection`\n * node synthesis are deferred to later slices — they require either episode\n * sequencing or an abstraction synthesis pass that is out of scope for PR 2.\n *\n * @param memories Memories to scan. Order is preserved; duplicates are\n * not deduped (the caller controls the input set).\n * @param options Extraction knobs. See `ExtractGraphEdgesOptions`.\n * @returns A `{ nodes, edges }` pair. `nodes` contains one\n * `memory` node per input memory plus one `entity` node\n * per distinct entity discovered across all mentions.\n * Edges reference ids in the returned node map unless\n * `includeDanglingEdges` is set.\n */\nexport function extractGraphEdges(\n memories: readonly MemoryEdgeSource[],\n options: ExtractGraphEdgesOptions = {},\n): { nodes: Map<string, RemnicGraphNode>; edges: RemnicGraphEdge[] } {\n const includeDangling = options.includeDanglingEdges === true;\n\n const nodes = new Map<string, RemnicGraphNode>();\n const edges: RemnicGraphEdge[] = [];\n // Dedupe within this extraction pass. Key is `${from}\\u0000${to}\\u0000${type}`\n // — using a NUL separator avoids collisions with ids that contain `|`.\n const seenEdgeKeys = new Set<string>();\n\n const addNode = (id: string, type: NodeType) => {\n if (!nodes.has(id)) nodes.set(id, { id, type });\n };\n\n const addEdge = (from: string, to: string, type: EdgeType) => {\n if (!from || !to || from === to) return;\n const key = `${from}\\u0000${to}\\u0000${type}`;\n if (seenEdgeKeys.has(key)) return;\n seenEdgeKeys.add(key);\n edges.push({ from, to, type });\n };\n\n // First pass — register every memory as a node so cross-references can\n // resolve regardless of input ordering.\n for (const memory of memories) {\n if (!memory?.id) continue;\n addNode(memory.id, \"memory\");\n }\n\n // Pre-scan every memory for entity / agent references so the\n // memory-target guard can reject ids that are \"claimed\" by entity\n // mentions in the same batch — regardless of iteration order.\n // Without this, with `includeDanglingEdges: true`, processing\n // `{supersedes: \"shared\"}` before `{entityRef: \"shared\"}` would\n // materialize a memory node for \"shared\" and then the later entity\n // mention would be silently dropped (and vice versa), producing\n // order-dependent output.\n const entityClaimed = new Set<string>();\n for (const memory of memories) {\n if (!memory?.id) continue;\n // Do not mark the memory's own id as entity-claimed just because it\n // carries an entityRef — the memory node takes precedence over its\n // own mention (which is impossible anyway; the edge would be a\n // self-loop and is dropped).\n if (typeof memory.entityRef === \"string\" && memory.entityRef) {\n if (memory.entityRef !== memory.id) entityClaimed.add(memory.entityRef);\n }\n if (Array.isArray(memory.entityRefs)) {\n for (const ref of memory.entityRefs) {\n if (typeof ref === \"string\" && ref && ref !== memory.id) {\n entityClaimed.add(ref);\n }\n }\n }\n if (typeof memory.content === \"string\" && memory.content.length > 0) {\n CITATION_REGEX.lastIndex = 0;\n let match: RegExpExecArray | null;\n while ((match = CITATION_REGEX.exec(memory.content)) !== null) {\n const body = match[1];\n if (!body) continue;\n const agent = parseCitationFields(body).agent;\n if (!agent) continue;\n const agentId = `agent:${agent}`;\n if (agentId !== memory.id) entityClaimed.add(agentId);\n }\n }\n }\n // Ids claimed by both roles (memory + entity) stay memory — an\n // explicit memory node with a given id always wins over an entity\n // mention of the same id in a different memory. Drop any memory ids\n // out of `entityClaimed` so the memory-target guard does not reject\n // cross-memory supersedes / lineage references to them.\n for (const memory of memories) {\n if (memory?.id) entityClaimed.delete(memory.id);\n }\n\n // Second pass — walk each memory's relationship fields.\n for (const memory of memories) {\n if (!memory?.id) continue;\n const from = memory.id;\n\n // `supersedes`, `lineage`, and `derived_from` edges must point at a\n // memory node specifically — never an entity / episode / concept /\n // reflection node that happens to share an id. The guard applies\n // regardless of `includeDanglingEdges`:\n //\n // - If the target is already registered under a non-memory type,\n // reject the edge (type mismatch).\n // - If the target will be claimed by an entity mention in this\n // batch (pre-scan above), reject the edge.\n // - If the target is not yet known and is not entity-claimed,\n // accept only when `includeDanglingEdges` is `true`. A new\n // memory node is registered for the dangling reference.\n // - If the target is already registered as a memory, accept.\n const canTargetMemory = (id: string): boolean => {\n const existing = nodes.get(id);\n if (existing !== undefined) return existing.type === \"memory\";\n if (entityClaimed.has(id)) return false;\n return includeDangling;\n };\n\n // supersedes: memory → older memory\n if (typeof memory.supersedes === \"string\" && memory.supersedes) {\n const to = memory.supersedes;\n if (canTargetMemory(to)) {\n if (!nodes.has(to)) addNode(to, \"memory\");\n addEdge(from, to, \"supersedes\");\n }\n }\n\n // lineage: memory → each parent memory\n if (Array.isArray(memory.lineage)) {\n for (const parent of memory.lineage) {\n if (typeof parent !== \"string\" || !parent) continue;\n if (!canTargetMemory(parent)) continue;\n if (!nodes.has(parent)) addNode(parent, \"memory\");\n addEdge(from, parent, \"derived-from\");\n }\n }\n\n // derived_from: memory → parent memory (strip `:<version>` suffix)\n if (Array.isArray(memory.derived_from)) {\n for (const raw of memory.derived_from) {\n if (typeof raw !== \"string\" || !raw) continue;\n const to = stripDerivedFromVersion(raw);\n if (!to) continue;\n if (!canTargetMemory(to)) continue;\n if (!nodes.has(to)) addNode(to, \"memory\");\n addEdge(from, to, \"derived-from\");\n }\n }\n\n // entityRef / entityRefs: memory → entity. The target must either be\n // absent (we register a new entity node) or already an entity node\n // (no type conflict). If the id is already a memory node, drop the\n // edge rather than mis-typing it — the extractor never mutates an\n // existing node's type.\n const entitySet = new Set<string>();\n if (typeof memory.entityRef === \"string\" && memory.entityRef) {\n entitySet.add(memory.entityRef);\n }\n if (Array.isArray(memory.entityRefs)) {\n for (const ref of memory.entityRefs) {\n if (typeof ref === \"string\" && ref) entitySet.add(ref);\n }\n }\n for (const ref of entitySet) {\n const existing = nodes.get(ref);\n if (existing !== undefined && existing.type !== \"entity\") continue;\n if (!existing) addNode(ref, \"entity\");\n addEdge(from, ref, \"mentions\");\n }\n\n // Inline [Source: agent=..., ...] citations → authored-by edge.\n // Same type-collision guard as the entity block above.\n if (typeof memory.content === \"string\" && memory.content.length > 0) {\n // Reset the regex's lastIndex on each memory since it is a global regex.\n CITATION_REGEX.lastIndex = 0;\n let match: RegExpExecArray | null;\n while ((match = CITATION_REGEX.exec(memory.content)) !== null) {\n const body = match[1];\n if (!body) continue;\n const fields = parseCitationFields(body);\n const agent = fields.agent;\n if (!agent) continue;\n const agentId = `agent:${agent}`;\n const existing = nodes.get(agentId);\n if (existing !== undefined && existing.type !== \"entity\") continue;\n if (!existing) addNode(agentId, \"entity\");\n addEdge(from, agentId, \"authored-by\");\n }\n }\n }\n\n return { nodes, edges };\n}\n\n/**\n * Build a `RemnicGraph` from a collection of memories by delegating to\n * `extractGraphEdges()`. Convenience wrapper so callers do not have to\n * re-wrap the `{ nodes, edges }` pair into the `RemnicGraph` interface.\n *\n * Pure function — no I/O. Persisting the graph (e.g. writing\n * `~/.remnic/graph.json`) is left to the caller; that decision belongs with\n * the maintenance / consolidation pass in PR 4, not the extractor.\n */\nexport function buildGraphFromMemories(\n memories: readonly MemoryEdgeSource[],\n options: ExtractGraphEdgesOptions = {},\n): RemnicGraph {\n const { nodes, edges } = extractGraphEdges(memories, options);\n return { nodes, edges };\n}\n"],"mappings":";AAuLO,IAAM,sBAAsB;AAE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AI,OAAO;AAClC,YAAI,aAAa,UAAa,SAAS,SAAS,SAAU;AAC1D,YAAI,CAAC,SAAU,SAAQ,SAAS,QAAQ;AACxC,gBAAQ,MAAM,SAAS,aAAa;AAAA,MACtC;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,OAAO,MAAM;AACxB;AAWO,SAAS,uBACd,UACA,UAAoC,CAAC,GACxB;AACb,QAAM,EAAE,OAAO,MAAM,IAAI,kBAAkB,UAAU,OAAO;AAC5D,SAAO,EAAE,OAAO,MAAM;AACxB;","names":[]}
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDirectAnswerEligible
|
|
3
|
+
} from "./chunk-Y4FHOFJ2.js";
|
|
4
|
+
import {
|
|
5
|
+
throwIfAborted
|
|
6
|
+
} from "./chunk-PVGDJXVK.js";
|
|
7
|
+
import {
|
|
8
|
+
normalizeRecallTokens
|
|
9
|
+
} from "./chunk-DT5TVLJE.js";
|
|
10
|
+
|
|
1
11
|
// src/taxonomy/resolver.ts
|
|
2
12
|
var DEFAULT_CATEGORY_ID = "facts";
|
|
3
13
|
function resolveCategory(content, memoryCategory, taxonomy) {
|
|
@@ -74,7 +84,67 @@ function computeKeywordScore(contentLower, cat) {
|
|
|
74
84
|
return score;
|
|
75
85
|
}
|
|
76
86
|
|
|
87
|
+
// src/direct-answer-wiring.ts
|
|
88
|
+
async function tryDirectAnswer(input) {
|
|
89
|
+
const { query, namespace, config, sources, queryEntityRefs, abortSignal } = input;
|
|
90
|
+
const eligibilityConfig = {
|
|
91
|
+
enabled: config.recallDirectAnswerEnabled,
|
|
92
|
+
tokenOverlapFloor: config.recallDirectAnswerTokenOverlapFloor,
|
|
93
|
+
importanceFloor: config.recallDirectAnswerImportanceFloor,
|
|
94
|
+
ambiguityMargin: config.recallDirectAnswerAmbiguityMargin,
|
|
95
|
+
eligibleTaxonomyBuckets: config.recallDirectAnswerEligibleTaxonomyBuckets
|
|
96
|
+
};
|
|
97
|
+
if (!eligibilityConfig.enabled) {
|
|
98
|
+
return isDirectAnswerEligible({
|
|
99
|
+
query,
|
|
100
|
+
candidates: [],
|
|
101
|
+
config: eligibilityConfig,
|
|
102
|
+
queryEntityRefs
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (normalizeRecallTokens(query).length === 0) {
|
|
106
|
+
return isDirectAnswerEligible({
|
|
107
|
+
query,
|
|
108
|
+
candidates: [],
|
|
109
|
+
config: eligibilityConfig,
|
|
110
|
+
queryEntityRefs
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
throwIfAborted(abortSignal, "direct-answer wiring aborted");
|
|
114
|
+
const memories = await sources.listCandidateMemories({ namespace, abortSignal });
|
|
115
|
+
throwIfAborted(abortSignal, "direct-answer wiring aborted");
|
|
116
|
+
const candidates = [];
|
|
117
|
+
for (const memory of memories) {
|
|
118
|
+
throwIfAborted(abortSignal, "direct-answer wiring aborted");
|
|
119
|
+
const trustZone = await sources.trustZoneFor(memory.frontmatter.id);
|
|
120
|
+
throwIfAborted(abortSignal, "direct-answer wiring aborted");
|
|
121
|
+
if (trustZone !== "trusted") continue;
|
|
122
|
+
const decision = resolveCategory(
|
|
123
|
+
memory.content,
|
|
124
|
+
memory.frontmatter.category,
|
|
125
|
+
sources.taxonomy
|
|
126
|
+
);
|
|
127
|
+
const taxonomyBucket = decision.categoryId;
|
|
128
|
+
if (!eligibilityConfig.eligibleTaxonomyBuckets.includes(taxonomyBucket)) continue;
|
|
129
|
+
const importanceScore = sources.importanceFor(memory);
|
|
130
|
+
candidates.push({
|
|
131
|
+
memory,
|
|
132
|
+
trustZone,
|
|
133
|
+
taxonomyBucket,
|
|
134
|
+
importanceScore
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
throwIfAborted(abortSignal, "direct-answer wiring aborted");
|
|
138
|
+
return isDirectAnswerEligible({
|
|
139
|
+
query,
|
|
140
|
+
candidates,
|
|
141
|
+
config: eligibilityConfig,
|
|
142
|
+
queryEntityRefs
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
77
146
|
export {
|
|
78
|
-
resolveCategory
|
|
147
|
+
resolveCategory,
|
|
148
|
+
tryDirectAnswer
|
|
79
149
|
};
|
|
80
|
-
//# sourceMappingURL=chunk-
|
|
150
|
+
//# sourceMappingURL=chunk-6AUUAZEX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/taxonomy/resolver.ts","../src/direct-answer-wiring.ts"],"sourcesContent":["/**\n * Resolver decision tree for the MECE taxonomy.\n *\n * Given extracted content and its MemoryCategory, determines which\n * taxonomy category the knowledge should be filed under.\n */\n\nimport type { MemoryCategory } from \"../types.js\";\nimport type { ResolverDecision, Taxonomy, TaxonomyCategory } from \"./types.js\";\n\nconst DEFAULT_CATEGORY_ID = \"facts\";\n\n/**\n * Resolve a piece of content to a taxonomy category.\n *\n * Algorithm:\n * 1. Find all taxonomy categories whose `memoryCategories` include\n * the given `memoryCategory`.\n * 2. If exactly one match, return it with confidence 1.0.\n * 3. If multiple matches, pick the one with the lowest priority\n * number (highest precedence). Apply keyword heuristics from\n * filing rules as a secondary signal.\n * 4. If no match, fall back to the \"facts\" category (or first\n * category if \"facts\" is absent) with low confidence.\n * 5. Always populate `alternatives` with other plausible categories.\n */\nexport function resolveCategory(\n content: string,\n memoryCategory: MemoryCategory,\n taxonomy: Taxonomy,\n): ResolverDecision {\n const contentLower = content.toLowerCase();\n\n // Step 1: find matching categories\n const matches = taxonomy.categories.filter((cat) =>\n cat.memoryCategories.includes(memoryCategory),\n );\n\n if (matches.length === 0) {\n // No taxonomy category accepts this MemoryCategory — fall back\n const fallback =\n taxonomy.categories.find((c) => c.id === DEFAULT_CATEGORY_ID) ??\n taxonomy.categories[0];\n if (!fallback) {\n return {\n categoryId: DEFAULT_CATEGORY_ID,\n confidence: 0,\n reason: \"Taxonomy is empty; using default category\",\n alternatives: [],\n };\n }\n const alternatives = taxonomy.categories\n .filter((c) => c.id !== fallback.id)\n .map((c) => ({\n categoryId: c.id,\n reason: c.description,\n }));\n return {\n categoryId: fallback.id,\n confidence: 0.3,\n reason: `No taxonomy category maps to MemoryCategory \"${memoryCategory}\"; falling back to \"${fallback.name}\"`,\n alternatives,\n };\n }\n\n if (matches.length === 1) {\n const match = matches[0]!;\n const alternatives = taxonomy.categories\n .filter((c) => c.id !== match.id)\n .map((c) => ({\n categoryId: c.id,\n reason: c.description,\n }));\n return {\n categoryId: match.id,\n confidence: 1.0,\n reason: `Unique match: MemoryCategory \"${memoryCategory}\" maps to \"${match.name}\"`,\n alternatives,\n };\n }\n\n // Multiple matches — use filing rule keyword heuristics + priority\n const scored = matches.map((cat) => ({\n cat,\n keywordScore: computeKeywordScore(contentLower, cat),\n }));\n\n // Sort by keyword score descending, then priority ascending (lower wins)\n scored.sort((a, b) => {\n if (b.keywordScore !== a.keywordScore) return b.keywordScore - a.keywordScore;\n return a.cat.priority - b.cat.priority;\n });\n\n const best = scored[0]!;\n const runnerUp = scored[1];\n\n // Confidence is higher when keyword match clearly differentiates\n const confidence =\n best.keywordScore > 0 && (!runnerUp || best.keywordScore > runnerUp.keywordScore)\n ? 0.9\n : 0.7;\n\n const alternatives = taxonomy.categories\n .filter((c) => c.id !== best.cat.id)\n .map((c) => ({\n categoryId: c.id,\n reason: c.description,\n }));\n\n const reason =\n best.keywordScore > 0\n ? `Filing rules for \"${best.cat.name}\" matched content keywords (priority ${best.cat.priority})`\n : `Priority tie-break: \"${best.cat.name}\" has lowest priority number (${best.cat.priority})`;\n\n return {\n categoryId: best.cat.id,\n confidence,\n reason,\n alternatives,\n };\n}\n\n/**\n * Compute a simple keyword overlap score between content and\n * a category's filing rules + description.\n */\nfunction computeKeywordScore(contentLower: string, cat: TaxonomyCategory): number {\n let score = 0;\n const ruleText = [...cat.filingRules, cat.description]\n .join(\" \")\n .toLowerCase();\n\n // Extract meaningful words (3+ chars) from the rule text\n const keywords = ruleText\n .split(/[^a-z0-9]+/)\n .filter((w) => w.length >= 3);\n\n for (const kw of keywords) {\n if (contentLower.includes(kw)) {\n score += 1;\n }\n }\n return score;\n}\n","/**\n * Direct-answer wiring (issue #518 slice 3).\n *\n * Binds the pure eligibility decision (`direct-answer.ts`) to the data\n * sources needed to build candidates: storage, trust-zones, taxonomy,\n * and importance scoring. Kept as a separate module so that:\n *\n * - The eligibility layer stays pure and unit-testable without stores.\n * - Each caller injects its own source accessors. The orchestrator\n * binding is a follow-on slice; tests here use mock sources.\n * - The wiring is safe to ship alone — nothing calls `tryDirectAnswer`\n * yet, so enabling this module's presence does not change recall\n * behavior. The next slice adds exactly one call site before QMD.\n *\n * Short-circuit contract:\n *\n * - When `config.recallDirectAnswerEnabled === false`, the function\n * returns the eligibility verdict with reason `\"disabled\"` without\n * touching any source accessor. This is the documented default.\n * - When enabled, the wiring cheaply drops non-trusted-zone memories\n * and ineligible taxonomy buckets before computing importance, so\n * the eligibility module sees a pre-filtered candidate set. The\n * eligibility module still performs the same checks itself — this\n * module is purely an I/O and prefiltering layer.\n */\n\nimport type { MemoryFile, PluginConfig } from \"./types.js\";\nimport type { TrustZoneName } from \"./trust-zones.js\";\nimport type { Taxonomy } from \"./taxonomy/types.js\";\nimport { resolveCategory } from \"./taxonomy/resolver.js\";\nimport { normalizeRecallTokens } from \"./recall-tokenization.js\";\nimport { throwIfAborted } from \"./abort-error.js\";\nimport {\n isDirectAnswerEligible,\n type DirectAnswerCandidate,\n type DirectAnswerConfig,\n type DirectAnswerResult,\n} from \"./direct-answer.js\";\n\n/**\n * Caller-provided accessors for candidate sourcing. Decouples the\n * wiring from any specific storage / trust-zone / importance backend.\n */\nexport interface DirectAnswerSources {\n /**\n * List memories eligible to be considered for direct-answer.\n * Callers are expected to return only active, non-superseded memories\n * in the requested namespace; the wiring will cheaply re-filter on\n * trust zone and taxonomy bucket and hand the rest to the eligibility\n * module, which applies the full gate ladder.\n */\n listCandidateMemories(options: {\n namespace: string;\n abortSignal?: AbortSignal;\n }): Promise<MemoryFile[]>;\n /**\n * Resolve the trust-zone record for a memory. Returns `null` when\n * the memory has no trust-zone record (treated as not trusted).\n */\n trustZoneFor(memoryId: string): Promise<TrustZoneName | null>;\n /**\n * Resolve a calibrated importance score in [0, 1] for a memory.\n */\n importanceFor(memory: MemoryFile): number;\n /**\n * Taxonomy used to classify memories into direct-answer buckets.\n */\n taxonomy: Taxonomy;\n}\n\nexport interface DirectAnswerWiringInput {\n query: string;\n namespace: string;\n config: Pick<\n PluginConfig,\n | \"recallDirectAnswerEnabled\"\n | \"recallDirectAnswerTokenOverlapFloor\"\n | \"recallDirectAnswerImportanceFloor\"\n | \"recallDirectAnswerAmbiguityMargin\"\n | \"recallDirectAnswerEligibleTaxonomyBuckets\"\n >;\n sources: DirectAnswerSources;\n queryEntityRefs?: string[];\n abortSignal?: AbortSignal;\n}\n\n/**\n * Attempt direct-answer resolution. Returns the eligibility verdict\n * produced by `isDirectAnswerEligible` with candidates materialized\n * from the caller-supplied sources.\n */\nexport async function tryDirectAnswer(\n input: DirectAnswerWiringInput,\n): Promise<DirectAnswerResult> {\n const { query, namespace, config, sources, queryEntityRefs, abortSignal } = input;\n\n const eligibilityConfig: DirectAnswerConfig = {\n enabled: config.recallDirectAnswerEnabled,\n tokenOverlapFloor: config.recallDirectAnswerTokenOverlapFloor,\n importanceFloor: config.recallDirectAnswerImportanceFloor,\n ambiguityMargin: config.recallDirectAnswerAmbiguityMargin,\n eligibleTaxonomyBuckets: config.recallDirectAnswerEligibleTaxonomyBuckets,\n };\n\n // Short-circuit disabled case before touching any I/O.\n if (!eligibilityConfig.enabled) {\n return isDirectAnswerEligible({\n query,\n candidates: [],\n config: eligibilityConfig,\n queryEntityRefs,\n });\n }\n\n // Short-circuit empty-query case before any I/O. isDirectAnswerEligible\n // deterministically returns reason \"empty-query\" when the query\n // normalizes to zero searchable tokens; there's no point materializing\n // candidates just to reach the same verdict, and doing so would\n // surface avoidable upstream errors for requests that should exit\n // immediately.\n if (normalizeRecallTokens(query).length === 0) {\n return isDirectAnswerEligible({\n query,\n candidates: [],\n config: eligibilityConfig,\n queryEntityRefs,\n });\n }\n\n throwIfAborted(abortSignal, \"direct-answer wiring aborted\");\n const memories = await sources.listCandidateMemories({ namespace, abortSignal });\n throwIfAborted(abortSignal, \"direct-answer wiring aborted\");\n const candidates: DirectAnswerCandidate[] = [];\n\n for (const memory of memories) {\n // Throw rather than returning a partial verdict — a mid-loop abort\n // that left competing candidates unprocessed could otherwise surface\n // a spurious \"eligible\" result that the ambiguity gate never got a\n // chance to reject. The check repeats after every await so an\n // abort that lands during the in-flight I/O on the final memory\n // (after which no further iteration would exist) still stops us.\n throwIfAborted(abortSignal, \"direct-answer wiring aborted\");\n\n const trustZone = await sources.trustZoneFor(memory.frontmatter.id);\n throwIfAborted(abortSignal, \"direct-answer wiring aborted\");\n\n // Cheap pre-filter: non-trusted memories can't qualify, so skip\n // taxonomy and importance resolution for them.\n if (trustZone !== \"trusted\") continue;\n\n const decision = resolveCategory(\n memory.content,\n memory.frontmatter.category,\n sources.taxonomy,\n );\n const taxonomyBucket = decision.categoryId;\n if (!eligibilityConfig.eligibleTaxonomyBuckets.includes(taxonomyBucket)) continue;\n\n const importanceScore = sources.importanceFor(memory);\n\n candidates.push({\n memory,\n trustZone,\n taxonomyBucket,\n importanceScore,\n });\n }\n\n // Final check — if abort landed during the trust-zone await for the\n // last memory, the loop condition no longer fires. Guard before we\n // hand candidates to the eligibility gate.\n throwIfAborted(abortSignal, \"direct-answer wiring aborted\");\n\n return isDirectAnswerEligible({\n query,\n candidates,\n config: eligibilityConfig,\n queryEntityRefs,\n });\n}\n"],"mappings":";;;;;;;;;;;AAUA,IAAM,sBAAsB;AAgBrB,SAAS,gBACd,SACA,gBACA,UACkB;AAClB,QAAM,eAAe,QAAQ,YAAY;AAGzC,QAAM,UAAU,SAAS,WAAW;AAAA,IAAO,CAAC,QAC1C,IAAI,iBAAiB,SAAS,cAAc;AAAA,EAC9C;AAEA,MAAI,QAAQ,WAAW,GAAG;AAExB,UAAM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