@remnic/core 1.0.1 → 1.0.3
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/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/access-cli.d.ts +13 -3
- package/dist/access-cli.js +90 -75
- package/dist/access-cli.js.map +1 -1
- package/dist/access-http.d.ts +10 -3
- package/dist/access-http.js +25 -18
- package/dist/access-mcp.d.ts +30 -3
- package/dist/access-mcp.js +16 -1
- package/dist/access-schema.d.ts +46 -46
- package/dist/access-schema.js +1 -1
- package/dist/access-service.d.ts +65 -4
- package/dist/access-service.js +21 -15
- package/dist/active-memory-bridge.d.ts +66 -0
- package/dist/active-memory-bridge.js +11 -0
- package/dist/active-recall.d.ts +96 -0
- package/dist/active-recall.js +308 -0
- package/dist/active-recall.js.map +1 -0
- package/dist/behavior-learner.js +1 -1
- package/dist/bootstrap.d.ts +6 -3
- package/dist/bootstrap.js +2 -2
- package/dist/boxes.js +2 -2
- package/dist/briefing.d.ts +169 -0
- package/dist/briefing.js +52 -0
- package/dist/briefing.js.map +1 -0
- package/dist/buffer.d.ts +19 -5
- package/dist/buffer.js +2 -2
- package/dist/calibration.js +6 -6
- package/dist/causal-behavior.js +5 -5
- package/dist/causal-chain.js +3 -3
- package/dist/causal-consolidation.d.ts +22 -2
- package/dist/causal-consolidation.js +36 -9
- package/dist/causal-consolidation.js.map +1 -1
- package/dist/causal-retrieval.js +6 -6
- package/dist/causal-trajectory-graph.js +1 -1
- package/dist/causal-trajectory.d.ts +14 -1
- package/dist/causal-trajectory.js +5 -1
- package/dist/{chunk-KWBU5S5U.js → chunk-2ODBA7MQ.js} +9 -3
- package/dist/chunk-2ODBA7MQ.js.map +1 -0
- package/dist/{chunk-6UJQNRIO.js → chunk-2VFW5K5U.js} +93 -36
- package/dist/chunk-2VFW5K5U.js.map +1 -0
- package/dist/chunk-3PG3H5TD.js +7 -0
- package/dist/chunk-3PG3H5TD.js.map +1 -0
- package/dist/{chunk-NTTLPF7F.js → chunk-3QFQGRHO.js} +5 -5
- package/dist/chunk-4DJQYKMN.js +187 -0
- package/dist/chunk-4DJQYKMN.js.map +1 -0
- package/dist/chunk-4KAN3GZ3.js +225 -0
- package/dist/chunk-4KAN3GZ3.js.map +1 -0
- package/dist/{chunk-ORZMT74A.js → chunk-4NRAJUDS.js} +11 -1
- package/dist/chunk-4NRAJUDS.js.map +1 -0
- package/dist/{chunk-B7LOFDVE.js → chunk-4WMCPJWX.js} +8 -3
- package/dist/chunk-4WMCPJWX.js.map +1 -0
- package/dist/{chunk-G3AG3KZN.js → chunk-5IZL4DCV.js} +2 -2
- package/dist/{chunk-BRK4ODMI.js → chunk-5NPGSAVB.js} +2 -2
- package/dist/chunk-6MKAMLQL.js +16 -0
- package/dist/chunk-6MKAMLQL.js.map +1 -0
- package/dist/{chunk-ESSMF2FR.js → chunk-6PFRXT4K.js} +15 -6
- package/dist/chunk-6PFRXT4K.js.map +1 -0
- package/dist/chunk-6ZH4TU6I.js +245 -0
- package/dist/chunk-6ZH4TU6I.js.map +1 -0
- package/dist/{chunk-V4YC4LUK.js → chunk-74JR4N5J.js} +175 -63
- package/dist/chunk-74JR4N5J.js.map +1 -0
- package/dist/{chunk-L5RPWGFK.js → chunk-7DHTMOND.js} +2 -2
- package/dist/{chunk-TVVVQQAK.js → chunk-7PA4OZEU.js} +53 -11
- package/dist/chunk-7PA4OZEU.js.map +1 -0
- package/dist/{chunk-Q6FETXJA.js → chunk-7SEAZFFB.js} +2 -2
- package/dist/chunk-ALXMCZEU.js +332 -0
- package/dist/chunk-ALXMCZEU.js.map +1 -0
- package/dist/{chunk-QANCTXQF.js → chunk-AYPYCLR7.js} +3 -3
- package/dist/{chunk-WWIQTB2Y.js → chunk-BKQJBXXX.js} +9 -2
- package/dist/chunk-BKQJBXXX.js.map +1 -0
- package/dist/{chunk-LP47L3ZX.js → chunk-BTY5RRRF.js} +7 -7
- package/dist/{chunk-SCHEKPYH.js → chunk-C2EFFULQ.js} +1 -1
- package/dist/{chunk-GJR6D6KC.js → chunk-D654IBA6.js} +2 -2
- package/dist/{chunk-UV2FO7J4.js → chunk-E6K4NIEU.js} +2 -2
- package/dist/{chunk-T4WRIV2C.js → chunk-EABGC2TL.js} +2 -2
- package/dist/chunk-ECKDIK5F.js +813 -0
- package/dist/chunk-ECKDIK5F.js.map +1 -0
- package/dist/chunk-EJI5XIBB.js +232 -0
- package/dist/chunk-EJI5XIBB.js.map +1 -0
- package/dist/{chunk-ONRU4L2N.js → chunk-FEMOX5AD.js} +2 -2
- package/dist/{chunk-IFFFR3MR.js → chunk-FSFEQI74.js} +3 -3
- package/dist/chunk-G4SK7DSQ.js +121 -0
- package/dist/chunk-G4SK7DSQ.js.map +1 -0
- package/dist/{chunk-UIYZ5T3I.js → chunk-GJQPH5G3.js} +8 -8
- package/dist/{chunk-2PO5ZRKV.js → chunk-GZCUW5IC.js} +16 -3
- package/dist/chunk-GZCUW5IC.js.map +1 -0
- package/dist/{chunk-IZME7KW2.js → chunk-HITJFT7E.js} +24 -10
- package/dist/{chunk-IZME7KW2.js.map → chunk-HITJFT7E.js.map} +1 -1
- package/dist/chunk-IQT3XTKW.js +121 -0
- package/dist/chunk-IQT3XTKW.js.map +1 -0
- package/dist/{chunk-BDFZXRSO.js → chunk-J4IYOZZ5.js} +15 -2
- package/dist/chunk-J4IYOZZ5.js.map +1 -0
- package/dist/{chunk-ZKYI7UVO.js → chunk-JR4ZC3G4.js} +2 -2
- package/dist/{chunk-UCYSTFZR.js → chunk-JRNQ3RNA.js} +2 -2
- package/dist/{chunk-UYSKNO6E.js → chunk-JROGC36Y.js} +15 -4
- package/dist/chunk-JROGC36Y.js.map +1 -0
- package/dist/{chunk-GPGBSNKM.js → chunk-K4FLSOR5.js} +2 -2
- package/dist/{chunk-M5ZBBBJI.js → chunk-KEG4GNGI.js} +2 -2
- package/dist/chunk-KVE7R4CG.js +320 -0
- package/dist/chunk-KVE7R4CG.js.map +1 -0
- package/dist/{chunk-L7WO3MZ4.js → chunk-KWP7T3DP.js} +2 -2
- package/dist/chunk-LAYN4LDC.js +267 -0
- package/dist/chunk-LAYN4LDC.js.map +1 -0
- package/dist/{chunk-PGK3VUHN.js → chunk-MTLYEMJB.js} +3 -2
- package/dist/chunk-MTLYEMJB.js.map +1 -0
- package/dist/{chunk-J47FNDR7.js → chunk-MYQWXITD.js} +7 -7
- package/dist/{chunk-YNI4S5WT.js → chunk-N53K2EXC.js} +2 -2
- package/dist/{chunk-763GUIOU.js → chunk-NBNN5GOB.js} +2 -2
- package/dist/{chunk-CXWFUJR2.js → chunk-NSB3WSYS.js} +125 -6
- package/dist/chunk-NSB3WSYS.js.map +1 -0
- package/dist/{chunk-KL4CP4SB.js → chunk-O5ETUNBT.js} +17 -5
- package/dist/chunk-O5ETUNBT.js.map +1 -0
- package/dist/{chunk-OOSWAUYB.js → chunk-ODWDQNRE.js} +2 -2
- package/dist/{chunk-ISY75RLM.js → chunk-OJFGVJS6.js} +288 -7
- package/dist/chunk-OJFGVJS6.js.map +1 -0
- package/dist/{chunk-HLBYLYRD.js → chunk-PAORGQRI.js} +70 -13
- package/dist/chunk-PAORGQRI.js.map +1 -0
- package/dist/{chunk-ZJLY4QSU.js → chunk-PMB3WGDL.js} +69 -6
- package/dist/chunk-PMB3WGDL.js.map +1 -0
- package/dist/{chunk-J3BT33K7.js → chunk-POBPGDWI.js} +5 -5
- package/dist/{chunk-QWUUMMIK.js → chunk-POMSFKTB.js} +1351 -76
- package/dist/chunk-POMSFKTB.js.map +1 -0
- package/dist/{chunk-OTAVQCSF.js → chunk-PYXS46O7.js} +2 -2
- package/dist/chunk-QDW3E4RD.js +108 -0
- package/dist/chunk-QDW3E4RD.js.map +1 -0
- package/dist/{chunk-YNCQ7E4M.js → chunk-QDYXG4CS.js} +4 -3
- package/dist/chunk-QDYXG4CS.js.map +1 -0
- package/dist/{chunk-XUHI52HK.js → chunk-QKAH5B6E.js} +4 -4
- package/dist/{chunk-HLXVTBF3.js → chunk-QNJMBKFK.js} +3 -2
- package/dist/chunk-QNJMBKFK.js.map +1 -0
- package/dist/chunk-RCICHSHL.js +789 -0
- package/dist/chunk-RCICHSHL.js.map +1 -0
- package/dist/{chunk-HG2NKWR2.js → chunk-S4LX5EBI.js} +2 -2
- package/dist/{chunk-4A24LIM2.js → chunk-S75M5ZRK.js} +2 -2
- package/dist/{chunk-QCCCQT3O.js → chunk-TBBDFYXW.js} +2 -2
- package/dist/chunk-TBBDFYXW.js.map +1 -0
- package/dist/{chunk-U4PV25RD.js → chunk-U2IQTSBY.js} +1 -1
- package/dist/chunk-U2IQTSBY.js.map +1 -0
- package/dist/chunk-U66YHYC7.js +31 -0
- package/dist/chunk-U66YHYC7.js.map +1 -0
- package/dist/{chunk-MWGVGUIS.js → chunk-UEYA6UC7.js} +36 -4
- package/dist/chunk-UEYA6UC7.js.map +1 -0
- package/dist/{chunk-MDDAA2AO.js → chunk-UPMD5XND.js} +2 -2
- package/dist/{chunk-M5KEYE5E.js → chunk-URB2WSKZ.js} +2 -2
- package/dist/chunk-UVJFDP7P.js +202 -0
- package/dist/chunk-UVJFDP7P.js.map +1 -0
- package/dist/{chunk-QY2BHY5O.js → chunk-V7XCAHIB.js} +265 -25
- package/dist/chunk-V7XCAHIB.js.map +1 -0
- package/dist/chunk-W6SL7OFG.js +180 -0
- package/dist/chunk-W6SL7OFG.js.map +1 -0
- package/dist/{chunk-QDOSNLB4.js → chunk-X4WESCKA.js} +17 -15
- package/dist/chunk-X4WESCKA.js.map +1 -0
- package/dist/{chunk-OTFNI3OO.js → chunk-XMGSSBFX.js} +1738 -383
- package/dist/chunk-XMGSSBFX.js.map +1 -0
- package/dist/chunk-YDBIWGNI.js +298 -0
- package/dist/chunk-YDBIWGNI.js.map +1 -0
- package/dist/chunk-YFYL2SIJ.js +7857 -0
- package/dist/chunk-YFYL2SIJ.js.map +1 -0
- package/dist/chunking.js +1 -1
- package/dist/citations.d.ts +67 -0
- package/dist/citations.js +13 -0
- package/dist/citations.js.map +1 -0
- package/dist/cli-DwIBnp2g.d.ts +1240 -0
- package/dist/cli.d.ts +31 -1147
- package/dist/cli.js +149 -7092
- package/dist/cli.js.map +1 -1
- package/dist/codex-materialize-CQlLTzke.d.ts +139 -0
- package/dist/codex-thread-key.d.ts +3 -0
- package/dist/codex-thread-key.js +7 -0
- package/dist/codex-thread-key.js.map +1 -0
- package/dist/config.js +3 -2
- package/dist/connectors/codex/instructions.md +160 -0
- package/dist/connectors/codex/resources/namespace-cheatsheet.md +48 -0
- package/dist/day-summary.d.ts +7 -2
- package/dist/day-summary.js +5 -2
- package/dist/embedding-fallback.d.ts +96 -2
- package/dist/embedding-fallback.js +6 -4
- package/dist/{engine-2A6J4XEX.js → engine-X7X3AAG3.js} +10 -7
- package/dist/engine-X7X3AAG3.js.map +1 -0
- package/dist/entity-retrieval.d.ts +3 -2
- package/dist/entity-retrieval.js +10 -7
- package/dist/entity-schema.d.ts +11 -0
- package/dist/entity-schema.js +19 -0
- package/dist/entity-schema.js.map +1 -0
- package/dist/explicit-capture.d.ts +6 -3
- package/dist/explicit-capture.js +2 -2
- package/dist/extraction-judge.d.ts +66 -0
- package/dist/extraction-judge.js +18 -0
- package/dist/extraction-judge.js.map +1 -0
- package/dist/extraction.d.ts +1 -0
- package/dist/extraction.js +12 -10
- package/dist/fallback-llm.js +4 -4
- package/dist/graph.js +1 -1
- package/dist/importance.d.ts +11 -1
- package/dist/importance.js +3 -1
- package/dist/index.d.ts +1140 -8
- package/dist/index.js +3350 -333
- package/dist/index.js.map +1 -1
- package/dist/intent.d.ts +2 -1
- package/dist/intent.js +3 -1
- package/dist/lifecycle.js +1 -1
- package/dist/local-llm.js +2 -2
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +1 -1
- package/dist/memory-cache.d.ts +2 -2
- package/dist/memory-cache.js +1 -1
- package/dist/{memory-projection-store-NxMkbocT.d.ts → memory-projection-store-DeSXPh1j.d.ts} +1 -1
- package/dist/memory-projection-store.d.ts +1 -1
- package/dist/model-registry.js +2 -2
- package/dist/models-json.js +2 -2
- package/dist/native-knowledge.js +2 -2
- package/dist/negative.js +2 -2
- package/dist/operator-toolkit.js +20 -16
- package/dist/{orchestrator-CIvLFHx3.d.ts → orchestrator-B9kwlCep.d.ts} +254 -9
- package/dist/orchestrator.d.ts +6 -3
- package/dist/orchestrator.js +70 -58
- package/dist/page-versioning.d.ts +77 -0
- package/dist/page-versioning.js +15 -0
- package/dist/page-versioning.js.map +1 -0
- package/dist/plugin-id.d.ts +37 -0
- package/dist/plugin-id.js +11 -0
- package/dist/plugin-id.js.map +1 -0
- package/dist/policy-runtime.js +2 -2
- package/dist/profiling.js +2 -2
- package/dist/qmd.d.ts +5 -2
- package/dist/qmd.js +3 -3
- package/dist/recall-audit.d.ts +20 -0
- package/dist/recall-audit.js +50 -0
- package/dist/recall-audit.js.map +1 -0
- package/dist/recall-mmr.d.ts +152 -0
- package/dist/recall-mmr.js +17 -0
- package/dist/recall-mmr.js.map +1 -0
- package/dist/recall-qos.js +2 -2
- package/dist/recall-state.js +2 -2
- package/dist/relevance.js +2 -2
- package/dist/resolve-provider-secret.js +2 -2
- package/dist/resume-bundles.js +5 -4
- package/dist/retrieval-agents.js +2 -2
- package/dist/retrieval.js +2 -2
- package/dist/schemas.d.ts +422 -64
- package/dist/schemas.js +3 -1
- package/dist/sdk-compat.d.ts +2 -0
- package/dist/sdk-compat.js +6 -3
- package/dist/sdk-compat.js.map +1 -1
- package/dist/semantic-chunking.d.ts +87 -0
- package/dist/semantic-chunking.js +20 -0
- package/dist/semantic-chunking.js.map +1 -0
- package/dist/semantic-consolidation-DrvSYRdB.d.ts +119 -0
- package/dist/semantic-consolidation.d.ts +4 -42
- package/dist/semantic-consolidation.js +23 -2
- package/dist/semantic-rule-promotion.js +9 -6
- package/dist/semantic-rule-verifier.js +10 -7
- package/dist/session-observer-state.js +2 -2
- package/dist/session-toggles.d.ts +22 -0
- package/dist/session-toggles.js +116 -0
- package/dist/session-toggles.js.map +1 -0
- package/dist/skills-registry.d.ts +47 -0
- package/dist/skills-registry.js +48 -0
- package/dist/skills-registry.js.map +1 -0
- package/dist/source-attribution.d.ts +169 -0
- package/dist/source-attribution.js +27 -0
- package/dist/source-attribution.js.map +1 -0
- package/dist/storage.d.ts +171 -10
- package/dist/storage.js +16 -5
- package/dist/summarizer.js +7 -7
- package/dist/temporal-supersession.d.ts +127 -0
- package/dist/temporal-supersession.js +20 -0
- package/dist/temporal-supersession.js.map +1 -0
- package/dist/threading.js +2 -2
- package/dist/tier-migration.d.ts +2 -1
- package/dist/tier-routing.js +2 -2
- package/dist/tokens.d.ts +21 -1
- package/dist/tokens.js +5 -1
- package/dist/transcript.js +2 -2
- package/dist/types.d.ts +497 -3
- package/dist/types.js +1 -1
- package/dist/utility-learner.js +2 -2
- package/dist/utility-runtime.js +3 -3
- package/dist/verified-recall.js +11 -8
- package/dist/whitespace.d.ts +4 -0
- package/dist/whitespace.js +9 -0
- package/dist/whitespace.js.map +1 -0
- package/package.json +14 -8
- package/dist/chunk-2CJCWDMR.js +0 -87
- package/dist/chunk-2CJCWDMR.js.map +0 -1
- package/dist/chunk-2PO5ZRKV.js.map +0 -1
- package/dist/chunk-6UJQNRIO.js.map +0 -1
- package/dist/chunk-B7LOFDVE.js.map +0 -1
- package/dist/chunk-BDFZXRSO.js.map +0 -1
- package/dist/chunk-CXWFUJR2.js.map +0 -1
- package/dist/chunk-DORBM6OB.js +0 -81
- package/dist/chunk-DORBM6OB.js.map +0 -1
- package/dist/chunk-ESSMF2FR.js.map +0 -1
- package/dist/chunk-HLBYLYRD.js.map +0 -1
- package/dist/chunk-HLXVTBF3.js.map +0 -1
- package/dist/chunk-ISY75RLM.js.map +0 -1
- package/dist/chunk-KL4CP4SB.js.map +0 -1
- package/dist/chunk-KWBU5S5U.js.map +0 -1
- package/dist/chunk-MWGVGUIS.js.map +0 -1
- package/dist/chunk-ORZMT74A.js.map +0 -1
- package/dist/chunk-OTFNI3OO.js.map +0 -1
- package/dist/chunk-PGK3VUHN.js.map +0 -1
- package/dist/chunk-QCCCQT3O.js.map +0 -1
- package/dist/chunk-QDOSNLB4.js.map +0 -1
- package/dist/chunk-QPKFPHOO.js +0 -178
- package/dist/chunk-QPKFPHOO.js.map +0 -1
- package/dist/chunk-QWUUMMIK.js.map +0 -1
- package/dist/chunk-QY2BHY5O.js.map +0 -1
- package/dist/chunk-TVVVQQAK.js.map +0 -1
- package/dist/chunk-U4PV25RD.js.map +0 -1
- package/dist/chunk-UYSKNO6E.js.map +0 -1
- package/dist/chunk-V4YC4LUK.js.map +0 -1
- package/dist/chunk-WWIQTB2Y.js.map +0 -1
- package/dist/chunk-YNCQ7E4M.js.map +0 -1
- package/dist/chunk-ZJLY4QSU.js.map +0 -1
- /package/dist/{engine-2A6J4XEX.js.map → active-memory-bridge.js.map} +0 -0
- /package/dist/{chunk-NTTLPF7F.js.map → chunk-3QFQGRHO.js.map} +0 -0
- /package/dist/{chunk-G3AG3KZN.js.map → chunk-5IZL4DCV.js.map} +0 -0
- /package/dist/{chunk-BRK4ODMI.js.map → chunk-5NPGSAVB.js.map} +0 -0
- /package/dist/{chunk-L5RPWGFK.js.map → chunk-7DHTMOND.js.map} +0 -0
- /package/dist/{chunk-Q6FETXJA.js.map → chunk-7SEAZFFB.js.map} +0 -0
- /package/dist/{chunk-QANCTXQF.js.map → chunk-AYPYCLR7.js.map} +0 -0
- /package/dist/{chunk-LP47L3ZX.js.map → chunk-BTY5RRRF.js.map} +0 -0
- /package/dist/{chunk-SCHEKPYH.js.map → chunk-C2EFFULQ.js.map} +0 -0
- /package/dist/{chunk-GJR6D6KC.js.map → chunk-D654IBA6.js.map} +0 -0
- /package/dist/{chunk-UV2FO7J4.js.map → chunk-E6K4NIEU.js.map} +0 -0
- /package/dist/{chunk-T4WRIV2C.js.map → chunk-EABGC2TL.js.map} +0 -0
- /package/dist/{chunk-ONRU4L2N.js.map → chunk-FEMOX5AD.js.map} +0 -0
- /package/dist/{chunk-IFFFR3MR.js.map → chunk-FSFEQI74.js.map} +0 -0
- /package/dist/{chunk-UIYZ5T3I.js.map → chunk-GJQPH5G3.js.map} +0 -0
- /package/dist/{chunk-ZKYI7UVO.js.map → chunk-JR4ZC3G4.js.map} +0 -0
- /package/dist/{chunk-UCYSTFZR.js.map → chunk-JRNQ3RNA.js.map} +0 -0
- /package/dist/{chunk-GPGBSNKM.js.map → chunk-K4FLSOR5.js.map} +0 -0
- /package/dist/{chunk-M5ZBBBJI.js.map → chunk-KEG4GNGI.js.map} +0 -0
- /package/dist/{chunk-L7WO3MZ4.js.map → chunk-KWP7T3DP.js.map} +0 -0
- /package/dist/{chunk-J47FNDR7.js.map → chunk-MYQWXITD.js.map} +0 -0
- /package/dist/{chunk-YNI4S5WT.js.map → chunk-N53K2EXC.js.map} +0 -0
- /package/dist/{chunk-763GUIOU.js.map → chunk-NBNN5GOB.js.map} +0 -0
- /package/dist/{chunk-OOSWAUYB.js.map → chunk-ODWDQNRE.js.map} +0 -0
- /package/dist/{chunk-J3BT33K7.js.map → chunk-POBPGDWI.js.map} +0 -0
- /package/dist/{chunk-OTAVQCSF.js.map → chunk-PYXS46O7.js.map} +0 -0
- /package/dist/{chunk-XUHI52HK.js.map → chunk-QKAH5B6E.js.map} +0 -0
- /package/dist/{chunk-HG2NKWR2.js.map → chunk-S4LX5EBI.js.map} +0 -0
- /package/dist/{chunk-4A24LIM2.js.map → chunk-S75M5ZRK.js.map} +0 -0
- /package/dist/{chunk-MDDAA2AO.js.map → chunk-UPMD5XND.js.map} +0 -0
- /package/dist/{chunk-M5KEYE5E.js.map → chunk-URB2WSKZ.js.map} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/entity-schema.ts"],"sourcesContent":["import type {\n EntitySchemaDefinition,\n EntitySchemaSectionDefinition,\n EntityStructuredSection,\n} from \"./types.js\";\n\nconst DEFAULT_ENTITY_SCHEMAS: Record<string, EntitySchemaDefinition> = {\n person: {\n sections: [\n {\n key: \"beliefs\",\n title: \"Beliefs\",\n description: \"\",\n aliases: [\"belief\", \"beliefs\", \"believe\", \"believes\"],\n },\n {\n key: \"communication_style\",\n title: \"Communication Style\",\n description: \"\",\n aliases: [\"communication\", \"communication style\", \"communicate\", \"writes\", \"writing style\"],\n },\n {\n key: \"building\",\n title: \"Building / Working On\",\n description: \"\",\n aliases: [\"building\", \"working on\", \"work on\", \"projects\"],\n },\n ],\n },\n project: {\n sections: [\n { key: \"status\", title: \"Status\", description: \"\" },\n {\n key: \"building\",\n title: \"Building / Working On\",\n description: \"\",\n aliases: [\"building\", \"working on\", \"work on\"],\n },\n { key: \"risks\", title: \"Risks\", description: \"\" },\n { key: \"notes\", title: \"Notes\", description: \"\" },\n ],\n },\n};\n\nexport function normalizeEntityText(value: string): string {\n return value\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \" \")\n .trim();\n}\n\nfunction toSnakeCase(value: string): string {\n return normalizeEntityText(value).replace(/\\s+/g, \"_\");\n}\n\nfunction titleFromKey(key: string): string {\n return key\n .split(\"_\")\n .filter(Boolean)\n .map((token) => token.charAt(0).toUpperCase() + token.slice(1))\n .join(\" \");\n}\n\nfunction tokenizeNormalized(value: string): string[] {\n return normalizeEntityText(value).split(/\\s+/).filter(Boolean);\n}\n\nfunction normalizeSectionDefinition(raw: unknown): EntitySchemaSectionDefinition | null {\n if (!raw || typeof raw !== \"object\" || Array.isArray(raw)) return null;\n const value = raw as Record<string, unknown>;\n const keySource = typeof value.key === \"string\" ? value.key : typeof value.title === \"string\" ? value.title : \"\";\n const titleSource = typeof value.title === \"string\" ? value.title : typeof value.key === \"string\" ? value.key : \"\";\n const key = toSnakeCase(keySource);\n const title = titleSource.trim() || titleFromKey(key);\n if (!key || !title) return null;\n const description = typeof value.description === \"string\" ? value.description : \"\";\n const aliases = Array.isArray(value.aliases)\n ? value.aliases\n .filter((alias): alias is string => typeof alias === \"string\")\n .map((alias) => alias.trim())\n .filter((alias) => alias.length > 0)\n : [];\n return aliases.length > 0\n ? { key, title, description, aliases }\n : { key, title, description };\n}\n\nexport function normalizeEntitySchemas(raw: unknown): Record<string, EntitySchemaDefinition> | undefined {\n if (!raw || typeof raw !== \"object\" || Array.isArray(raw)) return undefined;\n const result: Record<string, EntitySchemaDefinition> = {};\n for (const [entityType, schema] of Object.entries(raw as Record<string, unknown>)) {\n if (!schema || typeof schema !== \"object\" || Array.isArray(schema)) continue;\n const rawSections = (schema as Record<string, unknown>).sections;\n if (!Array.isArray(rawSections)) continue;\n const sections = rawSections\n .map((section) => normalizeSectionDefinition(section))\n .filter((section): section is EntitySchemaSectionDefinition => section !== null);\n if (sections.length === 0) continue;\n result[toSnakeCase(entityType)] = { sections };\n }\n return Object.keys(result).length > 0 ? result : undefined;\n}\n\nfunction mergeEntitySchemaDefinitions(\n defaults: EntitySchemaDefinition,\n overrides: EntitySchemaDefinition,\n): EntitySchemaDefinition {\n const overrideByKey = new Map(overrides.sections.map((section) => [section.key, section]));\n const mergedSections: EntitySchemaSectionDefinition[] = [];\n const seen = new Set<string>();\n\n for (const section of defaults.sections) {\n const override = overrideByKey.get(section.key);\n const mergedAliases = Array.from(new Set([...(section.aliases ?? []), ...(override?.aliases ?? [])]));\n const nextSection = override\n ? {\n ...section,\n ...override,\n ...(mergedAliases.length > 0 ? { aliases: mergedAliases } : {}),\n }\n : section;\n mergedSections.push(nextSection);\n seen.add(nextSection.key);\n }\n\n for (const section of overrides.sections) {\n if (seen.has(section.key)) continue;\n mergedSections.push(section);\n seen.add(section.key);\n }\n\n return { sections: mergedSections };\n}\n\nexport function getEntitySchema(\n entityType: string,\n entitySchemas?: Record<string, EntitySchemaDefinition>,\n): EntitySchemaDefinition | undefined {\n const normalizedType = toSnakeCase(entityType);\n const defaults = DEFAULT_ENTITY_SCHEMAS[normalizedType];\n const overrides = entitySchemas?.[normalizedType];\n if (!defaults) return overrides;\n if (!overrides) return defaults;\n return mergeEntitySchemaDefinitions(defaults, overrides);\n}\n\nexport function matchEntitySchemaSection(\n entityType: string,\n title: string,\n entitySchemas?: Record<string, EntitySchemaDefinition>,\n): EntitySchemaSectionDefinition | null {\n const normalizedTitle = normalizeEntityText(title);\n if (!normalizedTitle) return null;\n const schema = getEntitySchema(entityType, entitySchemas);\n if (!schema) return null;\n for (const section of schema.sections) {\n const aliases = [section.title, section.key, ...(section.aliases ?? [])];\n if (aliases.some((alias) => normalizeEntityText(alias) === normalizedTitle)) {\n return section;\n }\n }\n return null;\n}\n\nexport function normalizeEntityStructuredSection(\n entityType: string,\n section: Pick<EntityStructuredSection, \"key\" | \"title\">,\n entitySchemas?: Record<string, EntitySchemaDefinition>,\n): Pick<EntityStructuredSection, \"key\" | \"title\"> {\n const matchedSection = matchEntitySchemaSection(entityType, section.title, entitySchemas)\n ?? matchEntitySchemaSection(entityType, section.key, entitySchemas);\n if (matchedSection) {\n return {\n key: matchedSection.key,\n title: matchedSection.title,\n };\n }\n const key = toSnakeCase(section.key || section.title);\n return {\n key,\n title: section.title.trim() || titleFromKey(key),\n };\n}\n\nfunction queryMentionsAlias(query: string, alias: string): boolean {\n const queryTokens = tokenizeNormalized(query);\n const aliasTokens = tokenizeNormalized(alias);\n if (queryTokens.length === 0 || aliasTokens.length === 0) return false;\n if (aliasTokens.length > queryTokens.length) return false;\n for (let index = 0; index <= queryTokens.length - aliasTokens.length; index += 1) {\n let matched = true;\n for (let offset = 0; offset < aliasTokens.length; offset += 1) {\n if (queryTokens[index + offset] !== aliasTokens[offset]) {\n matched = false;\n break;\n }\n }\n if (matched) return true;\n }\n return false;\n}\n\nexport function resolveRequestedEntitySectionKeys(\n query: string,\n entityType: string,\n availableSections: EntityStructuredSection[],\n entitySchemas?: Record<string, EntitySchemaDefinition>,\n): string[] {\n if (availableSections.length === 0) return [];\n const availableKeys = new Set(availableSections.map((section) => toSnakeCase(section.key)));\n const schema = getEntitySchema(entityType, entitySchemas);\n if (!schema) return [];\n const matches: string[] = [];\n for (const section of schema.sections) {\n const key = toSnakeCase(section.key);\n if (!availableKeys.has(key)) continue;\n const aliases = [section.title, section.key, ...(section.aliases ?? [])];\n if (aliases.some((alias) => queryMentionsAlias(query, alias))) {\n matches.push(key);\n }\n }\n return matches;\n}\n\nexport function sortStructuredSectionsBySchema(\n entityType: string,\n sections: EntityStructuredSection[],\n entitySchemas?: Record<string, EntitySchemaDefinition>,\n): EntityStructuredSection[] {\n const schema = getEntitySchema(entityType, entitySchemas);\n if (!schema || sections.length <= 1) return sections;\n const order = new Map(schema.sections.map((section, index) => [toSnakeCase(section.key), index]));\n return [...sections].sort((left, right) => {\n const leftRank = order.get(toSnakeCase(left.key)) ?? Number.MAX_SAFE_INTEGER;\n const rightRank = order.get(toSnakeCase(right.key)) ?? Number.MAX_SAFE_INTEGER;\n if (leftRank !== rightRank) return leftRank - rightRank;\n return left.title.localeCompare(right.title);\n });\n}\n"],"mappings":";AAMA,IAAM,yBAAiE;AAAA,EACrE,QAAQ;AAAA,IACN,UAAU;AAAA,MACR;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,SAAS,CAAC,UAAU,WAAW,WAAW,UAAU;AAAA,MACtD;AAAA,MACA;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,SAAS,CAAC,iBAAiB,uBAAuB,eAAe,UAAU,eAAe;AAAA,MAC5F;AAAA,MACA;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,SAAS,CAAC,YAAY,cAAc,WAAW,UAAU;AAAA,MAC3D;AAAA,IACF;AAAA,EACF;AAAA,EACA,SAAS;AAAA,IACP,UAAU;AAAA,MACR,EAAE,KAAK,UAAU,OAAO,UAAU,aAAa,GAAG;AAAA,MAClD;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,aAAa;AAAA,QACb,SAAS,CAAC,YAAY,cAAc,SAAS;AAAA,MAC/C;AAAA,MACA,EAAE,KAAK,SAAS,OAAO,SAAS,aAAa,GAAG;AAAA,MAChD,EAAE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C,SAAS,UAAU,CAAC,YAAY,QAAQ,GAAG,GAAG,KAAK,CAAC,CAAC;AAChG,SAAO,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,MAAM,UAAU;AACzC,UAAM,WAAW,MAAM,IAAI,YAAY,KAAK,GAAG,CAAC,KAAK,OAAO;AAC5D,UAAM,YAAY,MAAM,IAAI,YAAY,MAAM,GAAG,CAAC,KAAK,OAAO;AAC9D,QAAI,aAAa,UAAW,QAAO,WAAW;AAC9C,WAAO,KAAK,MAAM,cAAc,MAAM,KAAK;AAAA,EAC7C,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// src/source-attribution.ts
|
|
2
|
+
var DEFAULT_CITATION_FORMAT = "[Source: agent={agent}, session={sessionId}, ts={ts}]";
|
|
3
|
+
var CITATION_UNKNOWN = "unknown";
|
|
4
|
+
function defaultCitationMatcher() {
|
|
5
|
+
return /\[Source:\s*([^\]\n]+?)\]/gi;
|
|
6
|
+
}
|
|
7
|
+
function deriveSessionId(session) {
|
|
8
|
+
if (!session) return void 0;
|
|
9
|
+
const trimmed = session.trim();
|
|
10
|
+
if (trimmed.length === 0) return void 0;
|
|
11
|
+
const parts = trimmed.split(":").filter((p) => p.length > 0);
|
|
12
|
+
if (parts.length === 0) return trimmed;
|
|
13
|
+
return parts[parts.length - 1];
|
|
14
|
+
}
|
|
15
|
+
function formatCitation(ctx, template = DEFAULT_CITATION_FORMAT) {
|
|
16
|
+
const session = ctx.session ?? "";
|
|
17
|
+
const sessionId = ctx.sessionId ?? deriveSessionId(session) ?? CITATION_UNKNOWN;
|
|
18
|
+
const ts = ctx.ts ?? CITATION_UNKNOWN;
|
|
19
|
+
const agent = ctx.agent && ctx.agent.trim().length > 0 ? ctx.agent : CITATION_UNKNOWN;
|
|
20
|
+
const date = ts && ts !== CITATION_UNKNOWN ? ts.slice(0, 10) : CITATION_UNKNOWN;
|
|
21
|
+
const sessionForTemplate = session.trim().length > 0 ? session : CITATION_UNKNOWN;
|
|
22
|
+
const values = {
|
|
23
|
+
agent,
|
|
24
|
+
session: sessionForTemplate,
|
|
25
|
+
sessionId,
|
|
26
|
+
ts,
|
|
27
|
+
date
|
|
28
|
+
};
|
|
29
|
+
return template.replace(/\{([a-zA-Z_][\w]*)\}/g, (match, name) => {
|
|
30
|
+
return Object.prototype.hasOwnProperty.call(values, name) ? values[name] : match;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function hasCitation(text) {
|
|
34
|
+
if (typeof text !== "string" || text.length === 0) return false;
|
|
35
|
+
return defaultCitationMatcher().test(text);
|
|
36
|
+
}
|
|
37
|
+
function escapeRegExp(s) {
|
|
38
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
39
|
+
}
|
|
40
|
+
function escapeRegExpCharClass(ch) {
|
|
41
|
+
if (ch === "]") return "\\]";
|
|
42
|
+
if (ch === "\\") return "\\\\";
|
|
43
|
+
if (ch === "^") return "\\^";
|
|
44
|
+
if (ch === "-") return "\\-";
|
|
45
|
+
return escapeRegExp(ch);
|
|
46
|
+
}
|
|
47
|
+
function buildTokenPattern(nonWordSepChars) {
|
|
48
|
+
const base = "\\n\\s";
|
|
49
|
+
if (nonWordSepChars.size === 0) {
|
|
50
|
+
return `[^\\n]+?`;
|
|
51
|
+
}
|
|
52
|
+
const escaped = [...nonWordSepChars].map(escapeRegExpCharClass).join("");
|
|
53
|
+
return `[^${base}${escaped}]+?`;
|
|
54
|
+
}
|
|
55
|
+
var PLACEHOLDER_REGEX = /\{[a-zA-Z_][\w]*\}/g;
|
|
56
|
+
function templateMatcher(template) {
|
|
57
|
+
const parts = template.split(PLACEHOLDER_REGEX);
|
|
58
|
+
if (parts.length <= 1) return null;
|
|
59
|
+
const prefix = parts[0] ?? "";
|
|
60
|
+
const suffix = parts[parts.length - 1] ?? "";
|
|
61
|
+
if (prefix.length > 0 || suffix.length > 0) {
|
|
62
|
+
const escapedPrefix = escapeRegExp(prefix);
|
|
63
|
+
const escapedSuffix = escapeRegExp(suffix);
|
|
64
|
+
const innerParts = parts.slice(1, -1);
|
|
65
|
+
const nonWordSepChars = /* @__PURE__ */ new Set();
|
|
66
|
+
for (const sep of innerParts) {
|
|
67
|
+
for (const ch of sep) {
|
|
68
|
+
if (!/\w/.test(ch)) {
|
|
69
|
+
nonWordSepChars.add(ch);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const interToken = buildTokenPattern(nonWordSepChars);
|
|
74
|
+
const lastToken = buildTokenPattern(/* @__PURE__ */ new Set());
|
|
75
|
+
const middle = innerParts.length === 0 ? lastToken : interToken + innerParts.slice(0, -1).map((sep) => escapeRegExp(sep) + interToken).join("") + escapeRegExp(innerParts[innerParts.length - 1]) + lastToken;
|
|
76
|
+
const pattern = escapedPrefix + middle + escapedSuffix;
|
|
77
|
+
return new RegExp(pattern, "i");
|
|
78
|
+
}
|
|
79
|
+
const middleLiterals = parts.slice(1, -1);
|
|
80
|
+
const hasNonEmptyMiddle = middleLiterals.some((p) => p.length > 0);
|
|
81
|
+
if (!hasNonEmptyMiddle) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const idToken = "[\\w.:-]+";
|
|
85
|
+
const body = idToken + middleLiterals.map((lit) => escapeRegExp(lit) + idToken).join("");
|
|
86
|
+
const separatorText = middleLiterals.join("");
|
|
87
|
+
if (/\s/.test(separatorText)) {
|
|
88
|
+
const opener = "[\\[\\(\\<]";
|
|
89
|
+
const closer = "[\\]\\)\\>]";
|
|
90
|
+
return new RegExp(opener + body + closer, "i");
|
|
91
|
+
}
|
|
92
|
+
const leadAnchor = "(?:(?<=[\\[\\(\\<])|(?<!\\S))";
|
|
93
|
+
const trailAnchor = "(?:(?=[\\]\\)\\>])|(?=\\s*$))";
|
|
94
|
+
return new RegExp(leadAnchor + body + trailAnchor, "i");
|
|
95
|
+
}
|
|
96
|
+
function hasCitationForTemplate(text, template) {
|
|
97
|
+
if (typeof text !== "string" || text.length === 0) return false;
|
|
98
|
+
if (hasCitation(text)) return true;
|
|
99
|
+
if (template === DEFAULT_CITATION_FORMAT) return false;
|
|
100
|
+
if (!PLACEHOLDER_REGEX.test(template)) {
|
|
101
|
+
PLACEHOLDER_REGEX.lastIndex = 0;
|
|
102
|
+
return text.includes(template);
|
|
103
|
+
}
|
|
104
|
+
PLACEHOLDER_REGEX.lastIndex = 0;
|
|
105
|
+
const matcher = templateMatcher(template);
|
|
106
|
+
if (!matcher) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return matcher.test(text);
|
|
110
|
+
}
|
|
111
|
+
function attachCitation(text, ctx, template = DEFAULT_CITATION_FORMAT) {
|
|
112
|
+
if (typeof text !== "string") return text;
|
|
113
|
+
if (hasCitationForTemplate(text, template)) return text;
|
|
114
|
+
const trimmedEnd = text.replace(/\s+$/u, "");
|
|
115
|
+
if (trimmedEnd.length === 0) return text;
|
|
116
|
+
const citation = formatCitation(ctx, template);
|
|
117
|
+
const trailing = text.slice(trimmedEnd.length);
|
|
118
|
+
return `${trimmedEnd} ${citation}${trailing}`;
|
|
119
|
+
}
|
|
120
|
+
function parseCitation(text) {
|
|
121
|
+
if (typeof text !== "string" || text.length === 0) return null;
|
|
122
|
+
const matcher = defaultCitationMatcher();
|
|
123
|
+
const match = matcher.exec(text);
|
|
124
|
+
if (!match) return null;
|
|
125
|
+
const body = match[1] ?? "";
|
|
126
|
+
const raw = match[0] ?? "";
|
|
127
|
+
const parsed = { raw };
|
|
128
|
+
const fields = body.split(",").map((segment) => segment.trim()).filter((segment) => segment.length > 0);
|
|
129
|
+
for (const field of fields) {
|
|
130
|
+
const eqIdx = field.indexOf("=");
|
|
131
|
+
if (eqIdx <= 0) continue;
|
|
132
|
+
const key = field.slice(0, eqIdx).trim().toLowerCase();
|
|
133
|
+
const value = field.slice(eqIdx + 1).trim();
|
|
134
|
+
if (value.length === 0) continue;
|
|
135
|
+
switch (key) {
|
|
136
|
+
case "agent":
|
|
137
|
+
parsed.agent = value;
|
|
138
|
+
break;
|
|
139
|
+
case "session":
|
|
140
|
+
case "sessionid":
|
|
141
|
+
parsed.session = value;
|
|
142
|
+
break;
|
|
143
|
+
case "ts":
|
|
144
|
+
case "timestamp":
|
|
145
|
+
parsed.ts = value;
|
|
146
|
+
break;
|
|
147
|
+
default:
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return parsed;
|
|
152
|
+
}
|
|
153
|
+
function parseAllCitations(text) {
|
|
154
|
+
if (typeof text !== "string" || text.length === 0) return [];
|
|
155
|
+
const matcher = defaultCitationMatcher();
|
|
156
|
+
const results = [];
|
|
157
|
+
let match;
|
|
158
|
+
while ((match = matcher.exec(text)) !== null) {
|
|
159
|
+
const parsed = parseCitation(match[0]);
|
|
160
|
+
if (parsed) results.push(parsed);
|
|
161
|
+
}
|
|
162
|
+
return results;
|
|
163
|
+
}
|
|
164
|
+
function stripCitation(text) {
|
|
165
|
+
if (typeof text !== "string" || text.length === 0) return text;
|
|
166
|
+
if (!hasCitation(text)) return text;
|
|
167
|
+
const matcher = defaultCitationMatcher();
|
|
168
|
+
let result = "";
|
|
169
|
+
let lastIndex = 0;
|
|
170
|
+
let match;
|
|
171
|
+
while ((match = matcher.exec(text)) !== null) {
|
|
172
|
+
const before = text.slice(lastIndex, match.index).replace(/[ \t]+$/, "");
|
|
173
|
+
result += before;
|
|
174
|
+
lastIndex = match.index + match[0].length;
|
|
175
|
+
}
|
|
176
|
+
const after = text.slice(lastIndex).replace(/^[ \t]+/, "");
|
|
177
|
+
if (after.length > 0) {
|
|
178
|
+
if (result.length > 0) result += " ";
|
|
179
|
+
result += after;
|
|
180
|
+
}
|
|
181
|
+
return result.trimEnd();
|
|
182
|
+
}
|
|
183
|
+
function stripCitationForTemplate(text, template) {
|
|
184
|
+
if (typeof text !== "string" || text.length === 0) return text;
|
|
185
|
+
if (hasCitation(text)) return stripCitation(text);
|
|
186
|
+
if (!hasCitationForTemplate(text, template)) return text;
|
|
187
|
+
const matcher = templateMatcher(template);
|
|
188
|
+
if (!matcher) return stripCitation(text);
|
|
189
|
+
const globalMatcher = new RegExp(
|
|
190
|
+
matcher.source,
|
|
191
|
+
matcher.flags.includes("g") ? matcher.flags : matcher.flags + "g"
|
|
192
|
+
);
|
|
193
|
+
let result = "";
|
|
194
|
+
let lastIndex = 0;
|
|
195
|
+
let match;
|
|
196
|
+
while ((match = globalMatcher.exec(text)) !== null) {
|
|
197
|
+
const before = text.slice(lastIndex, match.index).replace(/[ \t]+$/, "");
|
|
198
|
+
result += before;
|
|
199
|
+
lastIndex = match.index + match[0].length;
|
|
200
|
+
if (match[0].length === 0) {
|
|
201
|
+
globalMatcher.lastIndex++;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const after = text.slice(lastIndex).replace(/^[ \t]+/, "");
|
|
205
|
+
if (after.length > 0) {
|
|
206
|
+
if (result.length > 0) result += " ";
|
|
207
|
+
result += after;
|
|
208
|
+
}
|
|
209
|
+
return result.trimEnd();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export {
|
|
213
|
+
DEFAULT_CITATION_FORMAT,
|
|
214
|
+
CITATION_UNKNOWN,
|
|
215
|
+
deriveSessionId,
|
|
216
|
+
formatCitation,
|
|
217
|
+
hasCitation,
|
|
218
|
+
hasCitationForTemplate,
|
|
219
|
+
attachCitation,
|
|
220
|
+
parseCitation,
|
|
221
|
+
parseAllCitations,
|
|
222
|
+
stripCitation,
|
|
223
|
+
stripCitationForTemplate
|
|
224
|
+
};
|
|
225
|
+
//# sourceMappingURL=chunk-4KAN3GZ3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/source-attribution.ts"],"sourcesContent":["/**\n * Inline Source Attribution Protocol (issue #369)\n *\n * Extracted facts carry provenance inline inside the fact body, so the\n * citation survives prompt injection, copy/paste, and LLM quoting. This\n * complements — never replaces — the YAML frontmatter provenance stored on\n * disk.\n *\n * Default format (matches issue #369 proposal):\n *\n * The foo service uses Redis for rate limiting. [Source: agent=planner, session=abc123, ts=2026-04-10T14:25:07Z]\n *\n * Key properties:\n * - Inline (part of the body, not metadata).\n * - Compact (typically <80 chars of overhead per fact).\n * - Machine-parseable by a single regex.\n * - Opt-in via `inlineSourceAttributionEnabled` config flag (default off\n * for backwards compatibility with existing downstream consumers).\n * - Legacy facts without a citation remain fully readable.\n *\n * The format template is configurable via `inlineSourceAttributionFormat`\n * with supported placeholders:\n *\n * {agent} — principal / agent identifier\n * {session} — full session key (colon-delimited)\n * {sessionId} — short stable session id (trailing component)\n * {ts} — extraction timestamp (ISO 8601)\n * {date} — extraction date (YYYY-MM-DD)\n *\n * Any privacy-sensitive identifiers should be normalized before being passed\n * to `formatCitation` — the helper treats them as opaque strings.\n */\n\n/** Default citation format template (matches issue #369). */\nexport const DEFAULT_CITATION_FORMAT =\n \"[Source: agent={agent}, session={sessionId}, ts={ts}]\";\n\n/** Sentinel value used when a provenance field is missing. */\nexport const CITATION_UNKNOWN = \"unknown\";\n\nexport interface CitationContext {\n /** Principal / agent identifier (e.g. resolved via resolvePrincipal). */\n agent?: string;\n /** Full session key (e.g. \"agent:planner:main\"). */\n session?: string;\n /**\n * Opaque short session id. Derived from the trailing component of the\n * session key when not provided explicitly. Use this for compact formats\n * that do not need the full colon-delimited session key.\n */\n sessionId?: string;\n /** Extraction timestamp as an ISO 8601 string. */\n ts?: string;\n}\n\nexport interface ParsedCitation {\n /** Agent identifier parsed from the citation (never crashes on malformed input). */\n agent?: string;\n /** Session identifier parsed from the citation. */\n session?: string;\n /** Extraction timestamp parsed from the citation. */\n ts?: string;\n /** The full matched citation substring. */\n raw: string;\n}\n\n/**\n * Regex that matches the default `[Source: agent=X, session=Y, ts=Z]` shape\n * as well as human-edited variants (extra whitespace, reordered fields,\n * subset of fields). Matches non-greedily so it can be anchored anywhere in\n * the text. Kept as a getter factory so callers do not share regex state.\n */\nfunction defaultCitationMatcher(): RegExp {\n return /\\[Source:\\s*([^\\]\\n]+?)\\]/gi;\n}\n\n/**\n * Derive a short session id from a full session key.\n * Falls back to the raw session string if no colon is present.\n */\nexport function deriveSessionId(session: string | undefined): string | undefined {\n if (!session) return undefined;\n const trimmed = session.trim();\n if (trimmed.length === 0) return undefined;\n const parts = trimmed.split(\":\").filter((p) => p.length > 0);\n if (parts.length === 0) return trimmed;\n return parts[parts.length - 1];\n}\n\n/**\n * Format an inline citation tag using the provided template.\n *\n * Missing context fields fall back to {@link CITATION_UNKNOWN} — the caller\n * should always get a non-empty, parseable tag.\n *\n * Uses a single-pass substitution so that values which themselves contain\n * placeholder syntax (e.g. an agent literally named `\"{ts}\"`) cannot be\n * re-interpreted by subsequent replacement steps. Each placeholder slot\n * receives exactly one lookup and the substituted value is treated as\n * terminal text, not template source.\n */\nexport function formatCitation(\n ctx: CitationContext,\n template: string = DEFAULT_CITATION_FORMAT,\n): string {\n const session = ctx.session ?? \"\";\n const sessionId = ctx.sessionId ?? deriveSessionId(session) ?? CITATION_UNKNOWN;\n const ts = ctx.ts ?? CITATION_UNKNOWN;\n const agent = ctx.agent && ctx.agent.trim().length > 0 ? ctx.agent : CITATION_UNKNOWN;\n const date = ts && ts !== CITATION_UNKNOWN ? ts.slice(0, 10) : CITATION_UNKNOWN;\n const sessionForTemplate = session.trim().length > 0 ? session : CITATION_UNKNOWN;\n\n // Map from recognised placeholder names to their resolved value. Unknown\n // placeholder names are left intact (returning the original `{name}`).\n const values: Record<string, string> = {\n agent,\n session: sessionForTemplate,\n sessionId,\n ts,\n date,\n };\n\n // Single-pass scan: replace every recognised `{name}` in one sweep so that\n // substituted values cannot themselves be treated as template source on a\n // subsequent pass. The replacer-function form also guarantees that `$` /\n // `$&` / `$1` sequences inside values are emitted literally.\n return template.replace(/\\{([a-zA-Z_][\\w]*)\\}/g, (match, name: string) => {\n return Object.prototype.hasOwnProperty.call(values, name)\n ? values[name]!\n : match;\n });\n}\n\n/**\n * Returns true if the text already carries at least one citation marker.\n * Safe to call on any string — never throws.\n */\nexport function hasCitation(text: string): boolean {\n if (typeof text !== \"string\" || text.length === 0) return false;\n return defaultCitationMatcher().test(text);\n}\n\n/**\n * Escape a string for use as a regex literal.\n */\nfunction escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\n/**\n * Escape a single character for use INSIDE a character class `[...]`.\n * Special chars inside character classes: `]`, `\\`, `^`, `-`.\n */\nfunction escapeRegExpCharClass(ch: string): string {\n if (ch === \"]\") return \"\\\\]\";\n if (ch === \"\\\\\") return \"\\\\\\\\\";\n if (ch === \"^\") return \"\\\\^\";\n if (ch === \"-\") return \"\\\\-\";\n // Other regex meta chars are NOT special inside [...] but escaping them is safe.\n return escapeRegExp(ch);\n}\n\n/**\n * Build a per-placeholder token pattern that excludes newlines, whitespace,\n * and any punctuation characters used as inner separators in the template.\n *\n * This prevents a placeholder span from consuming separator bytes and\n * matching strings that cross separator boundaries in user-supplied content.\n *\n * @param nonWordSepChars - Set of non-word (non-alphanumeric, non-`_`) chars\n * extracted from the inner separator literals of the template.\n */\nfunction buildTokenPattern(nonWordSepChars: Set<string>): string {\n // Always exclude newlines and whitespace.\n const base = \"\\\\n\\\\s\";\n if (nonWordSepChars.size === 0) {\n // No inner separator punctuation — the placeholder spans the full space\n // between prefix and suffix. Fall back to a generous no-newline match.\n return `[^\\\\n]+?`;\n }\n const escaped = [...nonWordSepChars].map(escapeRegExpCharClass).join(\"\");\n return `[^${base}${escaped}]+?`;\n}\n\n/** Regex that matches a `{placeholder}` token inside a template string. */\nconst PLACEHOLDER_REGEX = /\\{[a-zA-Z_][\\w]*\\}/g;\n\n/**\n * Build a regex that matches a citation produced by the given template.\n *\n * The approach depends on the shape of the template:\n *\n * - **Normal case (non-empty literal prefix or suffix).** Anchor the match\n * on the outer literal frame and reconstruct the interior as\n * `interToken + sep + interToken + sep + ... + sep + lastToken`.\n * All **intermediate** per-placeholder tokens exclude the combined set of\n * non-word separator characters used between any two adjacent placeholders,\n * preventing a value from consuming a separator and crossing placeholder\n * boundaries. The **last** token is only required to avoid newlines because\n * it is terminated by the literal suffix anchor — this lets placeholder\n * values that legitimately contain a separator character be recognised (e.g.\n * an ISO-8601 timestamp `2026-04-10T14:25:07Z` in `[src:{agent}:{ts}]`\n * where `:` is the inter-placeholder separator). A template like\n * `[src:{agent}/{sessionId}@{date}]` emits\n * `\\[src:[^\\n\\s/@]+?\\/[^\\n\\s/@]+?@[^\\n]+?\\]` so that `[src:foo/bar]`\n * is NOT matched (wrong separator count), `[src:foo/bar/extra@2026]`\n * is NOT matched (intermediate token crosses a `/` boundary), and\n * `[src:planner/main@2026-04-10]` IS matched correctly.\n *\n * - **Placeholder-bounded with whitespace separator.** Both prefix and\n * suffix are empty and the separator literal(s) between placeholders\n * contain at least one whitespace character (e.g. `{source}: {content}`,\n * `{agent} {sessionId}`). A whitespace-containing separator produces\n * output that is visually indistinguishable from ordinary prose, so the\n * safe strategy is to require a **hard bracket/paren/angle delimiter** on\n * both sides of the reconstructed match. Prose almost never places\n * `[...]` / `(...)` / `<...>` around a phrase, so this yields clean\n * false-positive rejection.\n *\n * - **Placeholder-bounded with compact (non-whitespace) separator.** Both\n * prefix and suffix are empty and the separator literal(s) contain NO\n * whitespace (e.g. `{agent}:{sessionId}`, `{agent}/{sessionId}`).\n * `formatCitation` emits a compact token like `planner:main` with no\n * surrounding delimiters, so the bracket strategy cannot detect it.\n * Instead, the pattern requires that the entire token is bordered by\n * whitespace or a bracket/paren/angle on each side:\n *\n * `(?:(?<=[\\[\\(\\<])|(?<!\\S))[\\w.-]+<sep>[\\w.-]+(?:(?=[\\]\\)\\>])|(?!\\S))`\n *\n * This accepts `planner:main` when it appears standalone or inside a\n * bracket-wrapped token, and rejects `host:80` embedded inside a URL like\n * `http://host:80` because `host` is immediately preceded by `/`\n * (non-whitespace, non-bracket).\n *\n * - **All-placeholder case (no literals between placeholders either).** No\n * reliable regex can be built — a template like `{agent}{sessionId}`\n * contains no anchor characters. Returns `null`; {@link\n * hasCitationForTemplate} treats this as \"cannot detect\" and returns\n * false, falling back on explicit sentinel/format detection only for the\n * default `[Source: ...]` shape.\n *\n * Returns `null` when the template has no placeholders (fully-literal\n * citation, handled by the string-equality fast path in {@link\n * hasCitationForTemplate}) **or** when the template is entirely placeholder-\n * only with no literal content whatsoever.\n */\nfunction templateMatcher(template: string): RegExp | null {\n // Split around all {placeholder} tokens.\n const parts = template.split(PLACEHOLDER_REGEX);\n if (parts.length <= 1) return null;\n\n const prefix = parts[0] ?? \"\";\n const suffix = parts[parts.length - 1] ?? \"\";\n\n // Normal case: at least one literal frame on the outside.\n // Tighten the per-placeholder token so it cannot consume separator\n // characters and match strings that cross separator boundaries\n // (Finding 3 — Uru3).\n if (prefix.length > 0 || suffix.length > 0) {\n const escapedPrefix = escapeRegExp(prefix);\n const escapedSuffix = escapeRegExp(suffix);\n\n // Inner parts: literal separators that sit between adjacent placeholders.\n // For `[src:{agent}/{sessionId}@{date}]`, parts = [\"[src:\", \"/\", \"@\", \"]\"]\n // so innerParts = [\"/\", \"@\"].\n const innerParts = parts.slice(1, -1);\n\n // Collect only the non-word (punctuation/symbol) characters from each\n // inner separator so alphabetic separator text (unlikely but valid) does\n // not exclude letters from the per-placeholder token pattern.\n const nonWordSepChars = new Set<string>();\n for (const sep of innerParts) {\n for (const ch of sep) {\n if (!/\\w/.test(ch)) {\n nonWordSepChars.add(ch);\n }\n }\n }\n\n // All intermediate tokens (every placeholder except the last) use the\n // combined exclusion so they cannot cross placeholder boundaries.\n //\n // The LAST token is different: it is terminated by the literal suffix anchor\n // (e.g. `\\]`), so it does not need to exclude inner-separator characters.\n // Dropping that restriction lets placeholder values that legitimately contain\n // a separator character (e.g. an ISO-8601 timestamp `2026-04-10T14:25:07Z`\n // in template `[src:{agent}:{ts}]`) be recognised correctly instead of\n // producing false-negative misses that trigger duplicate citation injection.\n //\n // Only the LAST token is relaxed. Intermediate tokens keep the combined\n // exclusion so that cross-boundary false positives are still rejected\n // (e.g. `[src:foo/bar/extra@2026-04-11]` for `[src:{a}/{b}@{c}]`).\n const interToken = buildTokenPattern(nonWordSepChars);\n // Last token: terminated by suffix anchor — exclude only newlines.\n const lastToken = buildTokenPattern(new Set<string>());\n\n // Reconstruct the interior: interToken sep interToken sep ... sep lastToken\n // (or just lastToken when there are no inner separators at all).\n const middle =\n innerParts.length === 0\n ? lastToken\n : interToken +\n innerParts\n .slice(0, -1)\n .map((sep) => escapeRegExp(sep) + interToken)\n .join(\"\") +\n escapeRegExp(innerParts[innerParts.length - 1]!) +\n lastToken;\n\n const pattern = escapedPrefix + middle + escapedSuffix;\n return new RegExp(pattern, \"i\");\n }\n\n // Placeholder-bounded case: prefix and suffix are both empty.\n const middleLiterals = parts.slice(1, -1);\n const hasNonEmptyMiddle = middleLiterals.some((p) => p.length > 0);\n if (!hasNonEmptyMiddle) {\n // All-placeholder template with no literal content. Impossible to anchor\n // reliably without sentinel markers; signal the caller.\n return null;\n }\n\n // Identifier token: one or more word chars, dots, dashes, or colons.\n // Colons are included to allow timestamp values like \"10:30\" or session\n // keys like \"agent:planner:main\" inside compact placeholder-bounded\n // templates. URL-like fragments (`http://host:80`) are still rejected\n // because the lead anchor requires whitespace or a bracket immediately\n // before the first id-token group (`http` is preceded by `/`).\n const idToken = \"[\\\\w.:-]+\";\n const body =\n idToken +\n middleLiterals.map((lit) => escapeRegExp(lit) + idToken).join(\"\");\n\n const separatorText = middleLiterals.join(\"\");\n if (/\\s/.test(separatorText)) {\n // Separator contains whitespace: the emitted citation looks like ordinary\n // prose (e.g. `planner main`). Require a hard bracket/paren/angle\n // delimiter on both sides to prevent false matches on English text.\n const opener = \"[\\\\[\\\\(\\\\<]\";\n const closer = \"[\\\\]\\\\)\\\\>]\";\n return new RegExp(opener + body + closer, \"i\");\n }\n\n // Separator is compact (no whitespace): `formatCitation` emits a token like\n // `planner:main` without surrounding delimiters. The challenge is that the\n // same token shape also matches ordinary hyphenated or slashed prose words\n // (e.g. `long-term`, `docs/setup`), causing `hasCitationForTemplate` to\n // return true on uncited fact bodies and silently suppress citation injection\n // from `attachCitation`.\n //\n // Fix (Finding 1): tighten the trail anchor so a bare compact token is only\n // accepted when it sits at the very end of the string (possibly followed by\n // optional trailing whitespace or a newline). Since `attachCitation` always\n // appends the citation at the trimmed end of the fact body, a real citation\n // token will always appear at the tail. Prose like `\"long-term solution\"`\n // has `long-term` in the middle of the string (followed by ` solution`), so\n // the end-of-string anchor rejects it — no false positive, no silent drop.\n //\n // The lead anchor still accepts either a bracket opener or a whitespace\n // boundary (or start of string), so `\"Fact. planner:main\"` and standalone\n // `\"planner:main\"` are both detected after the first attachment pass.\n //\n // Bracket-wrapped form (e.g. `[planner:main]`) is also accepted via the\n // opener/closer pair — bracket still takes precedence over end-of-string.\n //\n // Example — why `http://host:80` does NOT match:\n // Trying to match `host:80`: the char before `h` is `/` (non-whitespace,\n // non-bracket), so `(?<=[\\[\\(\\<])` and `(?<!\\S)` both fail ⟹ no match.\n // Trying to match `http:...`: after `http:` the next chars are `//` which\n // are not `[\\w.-]+`, so the second id-token group fails ⟹ no match.\n const leadAnchor = \"(?:(?<=[\\\\[\\\\(\\\\<])|(?<!\\\\S))\";\n // Trail: either a bracket closer (for `[token]` shape) or end-of-string\n // optionally preceded by whitespace. The `(?!\\S)` is deliberately removed\n // so that a compact token in the MIDDLE of a sentence does not match.\n const trailAnchor = \"(?:(?=[\\\\]\\\\)\\\\>])|(?=\\\\s*$))\";\n return new RegExp(leadAnchor + body + trailAnchor, \"i\");\n}\n\n/**\n * Returns true if `text` already carries a citation produced by `template`\n * **or** by the default `[Source: ...]` format (for facts that were tagged\n * before a config change).\n *\n * Use this instead of {@link hasCitation} whenever the caller has access to\n * the configured `inlineSourceAttributionFormat`.\n *\n * All-placeholder templates such as `{agent}{sessionId}` have no literal\n * content to anchor on and therefore cannot be reliably detected without\n * dedicated sentinel markers. In that case the function returns `false` —\n * callers that need idempotent dedup for such templates should either adopt\n * a template with literal delimiters (recommended) or rely on the default\n * `[Source: ...]` marker detection which is always available via\n * {@link hasCitation}.\n */\nexport function hasCitationForTemplate(text: string, template: string): boolean {\n if (typeof text !== \"string\" || text.length === 0) return false;\n // Always accept the default format as a fallback so facts tagged before a\n // configuration change are not double-tagged on reprocessing.\n if (hasCitation(text)) return true;\n // If the configured template matches the default, we're done.\n //\n // Known limitation (Thread 2 — Codex P2): this fast path exits without\n // checking whether the content carries a citation from a DIFFERENT custom\n // template that was active before the config was changed back to the default.\n // Such a fact would be detected by `hasCitation` above only if the prior\n // custom template happened to match the default `[Source: ...]` pattern.\n // In practice, template changes mid-stream are rare, and the false-negative\n // (missing an old custom citation) produces a benign duplicate citation rather\n // than data loss. A full fix would require storing the citation template used\n // at write time in the frontmatter and checking that here.\n if (template === DEFAULT_CITATION_FORMAT) return false;\n\n // Fully-literal template (no placeholders): exact inclusion check.\n if (!PLACEHOLDER_REGEX.test(template)) {\n // Reset lastIndex because PLACEHOLDER_REGEX is declared with /g.\n PLACEHOLDER_REGEX.lastIndex = 0;\n return text.includes(template);\n }\n // Reset lastIndex after the .test() probe above.\n PLACEHOLDER_REGEX.lastIndex = 0;\n\n const matcher = templateMatcher(template);\n if (!matcher) {\n // All-placeholder template: cannot build a reliable matcher. See the\n // docstring — callers should not rely on dedup for this shape.\n return false;\n }\n return matcher.test(text);\n}\n\n/**\n * Attach an inline citation to fact text.\n *\n * If the text already has a citation — either the default `[Source: ...]`\n * marker or one produced by the configured template — it is returned unchanged.\n * Existing provenance is respected and never overwritten. Otherwise the\n * citation is appended to the trimmed text with a single space separator,\n * which keeps the marker visually adjacent to the fact body.\n */\nexport function attachCitation(\n text: string,\n ctx: CitationContext,\n template: string = DEFAULT_CITATION_FORMAT,\n): string {\n if (typeof text !== \"string\") return text as unknown as string;\n if (hasCitationForTemplate(text, template)) return text;\n const trimmedEnd = text.replace(/\\s+$/u, \"\");\n if (trimmedEnd.length === 0) return text;\n const citation = formatCitation(ctx, template);\n // Preserve any trailing newline that callers rely on for markdown rendering.\n const trailing = text.slice(trimmedEnd.length);\n return `${trimmedEnd} ${citation}${trailing}`;\n}\n\n/**\n * Parse a single inline citation from a piece of text. Returns the first\n * citation encountered or `null` when none is present. Malformed citations\n * do not throw — fields that cannot be parsed simply remain `undefined`.\n */\nexport function parseCitation(text: string): ParsedCitation | null {\n if (typeof text !== \"string\" || text.length === 0) return null;\n const matcher = defaultCitationMatcher();\n const match = matcher.exec(text);\n if (!match) return null;\n\n const body = match[1] ?? \"\";\n const raw = match[0] ?? \"\";\n const parsed: ParsedCitation = { raw };\n\n const fields = body\n .split(\",\")\n .map((segment) => segment.trim())\n .filter((segment) => segment.length > 0);\n\n for (const field of fields) {\n const eqIdx = field.indexOf(\"=\");\n if (eqIdx <= 0) continue;\n const key = field.slice(0, eqIdx).trim().toLowerCase();\n const value = field.slice(eqIdx + 1).trim();\n if (value.length === 0) continue;\n switch (key) {\n case \"agent\":\n parsed.agent = value;\n break;\n case \"session\":\n case \"sessionid\":\n parsed.session = value;\n break;\n case \"ts\":\n case \"timestamp\":\n parsed.ts = value;\n break;\n default:\n // Unknown fields are ignored defensively so human edits never crash.\n break;\n }\n }\n\n return parsed;\n}\n\n/**\n * Parse every citation embedded in the text. Always returns an array; empty\n * when none are present.\n */\nexport function parseAllCitations(text: string): ParsedCitation[] {\n if (typeof text !== \"string\" || text.length === 0) return [];\n const matcher = defaultCitationMatcher();\n const results: ParsedCitation[] = [];\n let match: RegExpExecArray | null;\n while ((match = matcher.exec(text)) !== null) {\n const parsed = parseCitation(match[0]);\n if (parsed) results.push(parsed);\n }\n return results;\n}\n\n/**\n * Remove all inline citations from a piece of text.\n *\n * Callers that want the raw fact body (for dedup hashing, display, or\n * comparison) should use this helper instead of hand-rolled regexes so the\n * whole codebase agrees on the citation syntax.\n *\n * Finding 2 fix: when the input contains no citation marker, the input is\n * returned byte-for-byte unchanged. When a citation IS removed, whitespace\n * normalization is applied only at each join seam (the single space between\n * the preceding text and where the citation was), rather than across the\n * entire string. This preserves markdown hard-break spacing, aligned text,\n * and code-like snippets in fact bodies that happen to carry a citation.\n *\n * Implementation: each citation match is replaced by its \"seam fix\" — the\n * content before the match has its trailing whitespace trimmed and then a\n * single space is appended if any text remains, collapsing only the gap\n * left by the removed marker. Whitespace elsewhere in the body is untouched.\n */\nexport function stripCitation(text: string): string {\n if (typeof text !== \"string\" || text.length === 0) return text;\n // Early exit: no citation marker present — return the input unchanged so\n // that callers never lose formatting fidelity on uncited strings.\n if (!hasCitation(text)) return text;\n\n // Walk through all citations and slice them out one by one so that we can\n // normalise ONLY the whitespace at each seam rather than the entire string.\n const matcher = defaultCitationMatcher();\n let result = \"\";\n let lastIndex = 0;\n\n let match: RegExpExecArray | null;\n while ((match = matcher.exec(text)) !== null) {\n // Text before this citation. Trim trailing spaces/tabs at the seam only.\n const before = text.slice(lastIndex, match.index).replace(/[ \\t]+$/, \"\");\n result += before;\n lastIndex = match.index + match[0].length;\n }\n\n // Append any trailing text after the last citation. Trim leading\n // spaces/tabs and trailing whitespace at the join seam.\n const after = text.slice(lastIndex).replace(/^[ \\t]+/, \"\");\n if (after.length > 0) {\n if (result.length > 0) result += \" \";\n result += after;\n }\n\n return result.trimEnd();\n}\n\n/**\n * Strip an inline citation from text using a specific template regex.\n *\n * This is the template-aware counterpart to {@link stripCitation}. When the\n * caller holds the configured `inlineSourceAttributionFormat`, use this\n * function to strip citations produced by that template — including custom\n * templates that differ from the default `[Source: ...]` pattern.\n *\n * Behaviour:\n * - If the text has a **default-format** citation, delegates to\n * {@link stripCitation} (always safe).\n * - If the text has a **custom-template** citation detected by\n * `hasCitationForTemplate`, builds the template regex and removes every\n * occurrence (citations are appended at the end of the fact body by\n * {@link attachCitation}).\n * - All-placeholder templates (no literal prefix/suffix/separator) cannot\n * produce a reliable matcher. `hasCitationForTemplate` already returns\n * `false` for such templates, so this function never attempts to strip an\n * undetectable citation. The text is returned unchanged when no citation\n * is detected.\n * - If no citation is detected for the given template, returns the text\n * unchanged.\n *\n * @returns The stripped text (or the original text when no citation is found).\n */\nexport function stripCitationForTemplate(\n text: string,\n template: string,\n): string {\n if (typeof text !== \"string\" || text.length === 0) return text;\n\n // Fast path: default-format citation — delegate to the existing stripper.\n if (hasCitation(text)) return stripCitation(text);\n\n // No default citation; check whether the custom template produced one.\n // hasCitationForTemplate returns false for all-placeholder templates (no\n // reliable matcher), so those pass through unchanged below.\n if (!hasCitationForTemplate(text, template)) return text;\n\n // Build the template matcher. hasCitationForTemplate already returned true,\n // which means templateMatcher produced a non-null result. The null branch\n // here is a defensive fallback only — delegate to stripCitation.\n const matcher = templateMatcher(template);\n if (!matcher) return stripCitation(text);\n\n // The matcher regex was built without the global flag; add it for exec loop.\n const globalMatcher = new RegExp(\n matcher.source,\n matcher.flags.includes(\"g\") ? matcher.flags : matcher.flags + \"g\",\n );\n let result = \"\";\n let lastIndex = 0;\n let match: RegExpExecArray | null;\n\n while ((match = globalMatcher.exec(text)) !== null) {\n const before = text.slice(lastIndex, match.index).replace(/[ \\t]+$/, \"\");\n result += before;\n lastIndex = match.index + match[0].length;\n // Guard against zero-width matches causing an infinite loop.\n if (match[0].length === 0) {\n globalMatcher.lastIndex++;\n }\n }\n\n const after = text.slice(lastIndex).replace(/^[ \\t]+/, \"\");\n if (after.length > 0) {\n if (result.length > 0) result += \" \";\n result += after;\n }\n\n return result.trimEnd();\n}\n"],"mappings":";AAkCO,IAAM,0BACX;AAGK,IAAM,mBAAmB;AAkChC,SAAS,yBAAiC;AACxC,SAAO;AACT;AAMO,SAAS,gBAAgB,SAAiD;AAC/E,MAAI,CAAC,QAAS,QAAO;AACrB,QAAM,UAAU,QAAQ,KAAK;AAC7B,MAAI,QAAQ,WAAW,EAAG,QAAO;AACjC,QAAM,QAAQ,QAAQ,MAAM,GAAG,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC;AAC3D,MAAI,MAAM,WAAW,EAAG,QAAO;AAC/B,SAAO,MAAM,MAAM,SAAS,CAAC;AAC/B;AAcO,SAAS,eACd,KACA,WAAmB,yBACX;A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ACrC;AAIA,QAAM,QAAQ,KAAK,MAAM,SAAS,EAAE,QAAQ,WAAW,EAAE;AACzD,MAAI,MAAM,SAAS,GAAG;AACpB,QAAI,OAAO,SAAS,EAAG,WAAU;AACjC,cAAU;AAAA,EACZ;AAEA,SAAO,OAAO,QAAQ;AACxB;AA2BO,SAAS,yBACd,MACA,UACQ;AACR,MAAI,OAAO,SAAS,YAAY,KAAK,WAAW,EAAG,QAAO;AAG1D,MAAI,YAAY,IAAI,EAAG,QAAO,cAAc,IAAI;AAKhD,MAAI,CAAC,uBAAuB,MAAM,QAAQ,EAAG,QAAO;AAKpD,QAAM,UAAU,gBAAgB,QAAQ;AACxC,MAAI,CAAC,QAAS,QAAO,cAAc,IAAI;AAGvC,QAAM,gBAAgB,IAAI;AAAA,IACxB,QAAQ;AAAA,IACR,QAAQ,MAAM,SAAS,GAAG,IAAI,QAAQ,QAAQ,QAAQ,QAAQ;AAAA,EAChE;AACA,MAAI,SAAS;AACb,MAAI,YAAY;AAChB,MAAI;AAEJ,UAAQ,QAAQ,cAAc,KAAK,IAAI,OAAO,MAAM;AAClD,UAAM,SAAS,KAAK,MAAM,WAAW,MAAM,KAAK,EAAE,QAAQ,WAAW,EAAE;AACvE,cAAU;AACV,gBAAY,MAAM,QAAQ,MAAM,CAAC,EAAE;AAEnC,QAAI,MAAM,CAAC,EAAE,WAAW,GAAG;AACzB,oBAAc;AAAA,IAChB;AAAA,EACF;AAEA,QAAM,QAAQ,KAAK,MAAM,SAAS,EAAE,QAAQ,WAAW,EAAE;AACzD,MAAI,MAAM,SAAS,GAAG;AACpB,QAAI,OAAO,SAAS,EAAG,WAAU;AACjC,cAAU;AAAA,EACZ;AAEA,SAAO,OAAO,QAAQ;AACxB;","names":[]}
|
|
@@ -114,6 +114,14 @@ async function readCausalTrajectoryRecords(options) {
|
|
|
114
114
|
}
|
|
115
115
|
return { files, trajectories, invalidTrajectories };
|
|
116
116
|
}
|
|
117
|
+
function filterTrajectoriesByLookbackDays(trajectories, lookbackDays, nowMs = Date.now()) {
|
|
118
|
+
const days = Math.max(1, Math.floor(lookbackDays));
|
|
119
|
+
const cutoff = nowMs - days * 864e5;
|
|
120
|
+
return trajectories.filter((t) => {
|
|
121
|
+
const ms = Date.parse(t.recordedAt);
|
|
122
|
+
return Number.isFinite(ms) && ms >= cutoff;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
117
125
|
async function getCausalTrajectoryStoreStatus(options) {
|
|
118
126
|
const rootDir = resolveCausalTrajectoryStoreDir(options.memoryDir, options.causalTrajectoryStoreDir);
|
|
119
127
|
const trajectoriesDir = path.join(rootDir, "trajectories");
|
|
@@ -203,7 +211,9 @@ export {
|
|
|
203
211
|
resolveCausalTrajectoryStoreDir,
|
|
204
212
|
validateCausalTrajectoryRecord,
|
|
205
213
|
recordCausalTrajectory,
|
|
214
|
+
readCausalTrajectoryRecords,
|
|
215
|
+
filterTrajectoriesByLookbackDays,
|
|
206
216
|
getCausalTrajectoryStoreStatus,
|
|
207
217
|
searchCausalTrajectories
|
|
208
218
|
};
|
|
209
|
-
//# sourceMappingURL=chunk-
|
|
219
|
+
//# sourceMappingURL=chunk-4NRAJUDS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/causal-trajectory.ts"],"sourcesContent":["import path from \"node:path\";\nimport { mkdir, writeFile } from \"node:fs/promises\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport type { ObjectiveStateOutcome } from \"./objective-state.js\";\nimport { countRecallTokenOverlap, normalizeRecallTokens } from \"./recall-tokenization.js\";\nimport {\n assertIsoRecordedAt,\n assertSafePathSegment,\n assertString,\n isRecord,\n optionalString,\n optionalStringArray,\n recordStoreDay,\n validateStringRecord,\n} from \"./store-contract.js\";\n\nexport interface CausalTrajectoryRecord {\n schemaVersion: 1;\n trajectoryId: string;\n recordedAt: string;\n sessionKey: string;\n goal: string;\n actionSummary: string;\n observationSummary: string;\n outcomeKind: ObjectiveStateOutcome;\n outcomeSummary: string;\n followUpSummary?: string;\n objectiveStateSnapshotRefs?: string[];\n entityRefs?: string[];\n tags?: string[];\n metadata?: Record<string, string>;\n}\n\nexport interface CausalTrajectoryStoreStatus {\n enabled: boolean;\n rootDir: string;\n trajectoriesDir: string;\n trajectories: {\n total: number;\n valid: number;\n invalid: number;\n byOutcome: Partial<Record<ObjectiveStateOutcome, number>>;\n latestTrajectoryId?: string;\n latestRecordedAt?: string;\n latestSessionKey?: string;\n };\n latestTrajectory?: CausalTrajectoryRecord;\n invalidTrajectories: Array<{\n path: string;\n error: string;\n }>;\n}\n\nexport interface CausalTrajectorySearchResult {\n record: CausalTrajectoryRecord;\n score: number;\n matchedFields: string[];\n}\n\nfunction validateMetadata(raw: unknown): Record<string, string> | undefined {\n return validateStringRecord(raw, \"metadata\");\n}\n\nexport function resolveCausalTrajectoryStoreDir(memoryDir: string, overrideDir?: string): string {\n if (typeof overrideDir === \"string\" && overrideDir.trim().length > 0) {\n return overrideDir.trim();\n }\n return path.join(memoryDir, \"state\", \"causal-trajectories\");\n}\n\nexport function validateCausalTrajectoryRecord(raw: unknown): CausalTrajectoryRecord {\n if (!isRecord(raw)) throw new Error(\"causal trajectory record must be an object\");\n if (raw.schemaVersion !== 1) throw new Error(\"schemaVersion must be 1\");\n\n const outcomeKind = assertString(raw.outcomeKind, \"outcomeKind\");\n if (![\"success\", \"failure\", \"partial\", \"unknown\"].includes(outcomeKind)) {\n throw new Error(\"outcomeKind must be one of success|failure|partial|unknown\");\n }\n\n return {\n schemaVersion: 1,\n trajectoryId: assertSafePathSegment(assertString(raw.trajectoryId, \"trajectoryId\"), \"trajectoryId\"),\n recordedAt: assertIsoRecordedAt(assertString(raw.recordedAt, \"recordedAt\")),\n sessionKey: assertString(raw.sessionKey, \"sessionKey\"),\n goal: assertString(raw.goal, \"goal\"),\n actionSummary: assertString(raw.actionSummary, \"actionSummary\"),\n observationSummary: assertString(raw.observationSummary, \"observationSummary\"),\n outcomeKind: outcomeKind as ObjectiveStateOutcome,\n outcomeSummary: assertString(raw.outcomeSummary, \"outcomeSummary\"),\n followUpSummary: optionalString(raw.followUpSummary),\n objectiveStateSnapshotRefs: optionalStringArray(raw.objectiveStateSnapshotRefs, \"objectiveStateSnapshotRefs\"),\n entityRefs: optionalStringArray(raw.entityRefs, \"entityRefs\"),\n tags: optionalStringArray(raw.tags, \"tags\"),\n metadata: validateMetadata(raw.metadata),\n };\n}\n\nexport async function recordCausalTrajectory(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n actionGraphRecallEnabled?: boolean;\n cmcEnabled?: boolean;\n cmcStitchLookbackDays?: number;\n cmcStitchMinScore?: number;\n cmcStitchMaxEdgesPerTrajectory?: number;\n record: CausalTrajectoryRecord;\n}): Promise<string> {\n const rootDir = resolveCausalTrajectoryStoreDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const validated = validateCausalTrajectoryRecord(options.record);\n const day = recordStoreDay(validated.recordedAt);\n const trajectoriesDir = path.join(rootDir, \"trajectories\", day);\n const filePath = path.join(trajectoriesDir, `${validated.trajectoryId}.json`);\n await mkdir(trajectoriesDir, { recursive: true });\n await writeFile(filePath, JSON.stringify(validated, null, 2), \"utf8\");\n if (options.actionGraphRecallEnabled === true) {\n try {\n const { appendCausalTrajectoryGraphEdges } = await import(\"./causal-trajectory-graph.js\");\n await appendCausalTrajectoryGraphEdges({\n memoryDir: options.memoryDir,\n record: validated,\n });\n } catch (error) {\n const { log } = await import(\"./logger.js\");\n log.warn(\n `[causal-trajectory] action-conditioned graph write failed for ${validated.trajectoryId}: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n if (options.cmcEnabled === true) {\n try {\n const { stitchCausalChain } = await import(\"./causal-chain.js\");\n await stitchCausalChain({\n memoryDir: options.memoryDir,\n causalTrajectoryStoreDir: options.causalTrajectoryStoreDir,\n newTrajectory: validated,\n config: {\n lookbackDays: options.cmcStitchLookbackDays ?? 7,\n minScore: options.cmcStitchMinScore ?? 2.5,\n maxEdgesPerTrajectory: options.cmcStitchMaxEdgesPerTrajectory ?? 3,\n },\n });\n } catch (error) {\n const { log } = await import(\"./logger.js\");\n log.warn(\n `[cmc] causal chain stitching failed for ${validated.trajectoryId}: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n return filePath;\n}\n\nexport async function readCausalTrajectoryRecords(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n}): Promise<{\n files: string[];\n trajectories: CausalTrajectoryRecord[];\n invalidTrajectories: Array<{ path: string; error: string }>;\n}> {\n const rootDir = resolveCausalTrajectoryStoreDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const files = await listJsonFiles(path.join(rootDir, \"trajectories\"));\n const trajectories: CausalTrajectoryRecord[] = [];\n const invalidTrajectories: Array<{ path: string; error: string }> = [];\n for (const filePath of files) {\n try {\n trajectories.push(validateCausalTrajectoryRecord(await readJsonFile(filePath)));\n } catch (error) {\n invalidTrajectories.push({\n path: filePath,\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n return { files, trajectories, invalidTrajectories };\n}\n\n/** Keep trajectories whose recordedAt is within the last `lookbackDays` (issue #519 miner). */\nexport function filterTrajectoriesByLookbackDays(\n trajectories: CausalTrajectoryRecord[],\n lookbackDays: number,\n nowMs: number = Date.now(),\n): CausalTrajectoryRecord[] {\n const days = Math.max(1, Math.floor(lookbackDays));\n const cutoff = nowMs - days * 86_400_000;\n return trajectories.filter((t) => {\n const ms = Date.parse(t.recordedAt);\n return Number.isFinite(ms) && ms >= cutoff;\n });\n}\n\nexport async function getCausalTrajectoryStoreStatus(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n enabled: boolean;\n}): Promise<CausalTrajectoryStoreStatus> {\n const rootDir = resolveCausalTrajectoryStoreDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const trajectoriesDir = path.join(rootDir, \"trajectories\");\n const { files, trajectories, invalidTrajectories } = await readCausalTrajectoryRecords(options);\n\n trajectories.sort((a, b) => b.recordedAt.localeCompare(a.recordedAt));\n const byOutcome: Partial<Record<ObjectiveStateOutcome, number>> = {};\n for (const trajectory of trajectories) {\n byOutcome[trajectory.outcomeKind] = (byOutcome[trajectory.outcomeKind] ?? 0) + 1;\n }\n\n return {\n enabled: options.enabled,\n rootDir,\n trajectoriesDir,\n trajectories: {\n total: files.length,\n valid: trajectories.length,\n invalid: invalidTrajectories.length,\n byOutcome,\n latestTrajectoryId: trajectories[0]?.trajectoryId,\n latestRecordedAt: trajectories[0]?.recordedAt,\n latestSessionKey: trajectories[0]?.sessionKey,\n },\n latestTrajectory: trajectories[0],\n invalidTrajectories,\n };\n}\n\nfunction lexicalScoreCausalTrajectoryRecord(\n record: CausalTrajectoryRecord,\n queryTokens: Set<string>,\n): { score: number; matchedFields: string[] } {\n const weightedFields: Array<[field: string, value: string | undefined, weight: number]> = [\n [\"goal\", record.goal, 4],\n [\"action\", record.actionSummary, 3],\n [\"observation\", record.observationSummary, 3],\n [\"outcome\", record.outcomeSummary, 3],\n [\"follow_up\", record.followUpSummary, 2],\n [\"outcome_kind\", record.outcomeKind, 1],\n [\"tags\", record.tags?.join(\" \"), 2],\n [\"entity_refs\", record.entityRefs?.join(\" \"), 2],\n [\"objective_state_refs\", record.objectiveStateSnapshotRefs?.join(\" \"), 1],\n ];\n\n let score = 0;\n const matchedFields: string[] = [];\n for (const [field, value, weight] of weightedFields) {\n const matches = countRecallTokenOverlap(queryTokens, value, [\"make\"]);\n if (matches > 0) matchedFields.push(field);\n score += matches * weight;\n }\n return { score, matchedFields };\n}\n\nfunction scoreCausalTrajectoryRecord(\n record: CausalTrajectoryRecord,\n lexicalScore: number,\n sessionKey?: string,\n): number {\n let score = lexicalScore;\n if (sessionKey && record.sessionKey === sessionKey) score += 1.5;\n\n const recordedAtMs = Date.parse(record.recordedAt);\n if (Number.isFinite(recordedAtMs)) {\n const ageHours = Math.max(0, (Date.now() - recordedAtMs) / 3_600_000);\n score += 1 / (1 + ageHours);\n }\n return score;\n}\n\nexport async function searchCausalTrajectories(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n query: string;\n maxResults: number;\n sessionKey?: string;\n}): Promise<CausalTrajectorySearchResult[]> {\n const maxResults = Math.max(0, Math.floor(options.maxResults));\n if (maxResults === 0) return [];\n\n const { trajectories } = await readCausalTrajectoryRecords(options);\n if (trajectories.length === 0) return [];\n\n const queryTokens = new Set(normalizeRecallTokens(options.query, [\"make\"]));\n if (queryTokens.size === 0) return [];\n const scored = trajectories.map((record) => {\n const lexical = lexicalScoreCausalTrajectoryRecord(record, queryTokens);\n return {\n record,\n matchedFields: lexical.matchedFields,\n lexicalScore: lexical.score,\n score: scoreCausalTrajectoryRecord(record, lexical.score, options.sessionKey),\n };\n });\n\n const filtered = scored.filter((result) => result.lexicalScore > 0);\n\n filtered.sort((left, right) => {\n if (right.score !== left.score) return right.score - left.score;\n return right.record.recordedAt.localeCompare(left.record.recordedAt);\n });\n\n return filtered.slice(0, maxResults).map(({ record, score, matchedFields }) => ({\n record,\n score,\n matchedFields,\n }));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,UAAU;AACjB,SAAS,OAAO,iBAAiB;AA0DjC,SAAS,iBAAiB,KAAkD;AAC1E,SAAO,qBAAqB,KAAK,UAAU;AAC7C;AAEO,SAAS,gCAAgC,WAAmB,aAA8B;AAC/F,MAAI,OAAO,gBAAgB,YAAY,YAAY,KAAK,EAAE,SAAS,GAAG;AACpE,WAAO,YAAY,KAAK;AAAA,EAC1B;AACA,SAAO,KAAK,KAAK,WAAW,SAAS,qBAAqB;AAC5D;AAEO,SAAS,+BAA+B,KAAsC;AACnF,MAAI,CAAC,SAAS,GAAG,EAAG,OAAM,IAAI,MAAM,4CAA4C;AAChF,MAAI,IAAI,kBAAkB,EAAG,OAAM,IAAI,MAAM,yBAAyB;AAEtE,QAAM,cAAc,aAAa,IAAI,aAAa,aAAa;AAC/D,MAAI,CAAC,CAAC,WAAW,WAAW,WAAW,SAAS,EAAE,SAAS,WAAW,GAA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G,QAAO,CAAC;AACpC,QAAM,SAAS,aAAa,IAAI,CAAC,WAAW;AAC1C,UAAM,UAAU,mCAAmC,QAAQ,WAAW;AACtE,WAAO;AAAA,MACL;AAAA,MACA,eAAe,QAAQ;AAAA,MACvB,cAAc,QAAQ;AAAA,MACtB,OAAO,4BAA4B,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAAA,IAC9E;AAAA,EACF,CAAC;AAED,QAAM,WAAW,OAAO,OAAO,CAAC,WAAW,OAAO,eAAe,CAAC;AAElE,WAAS,KAAK,CAAC,MAAM,UAAU;AAC7B,QAAI,MAAM,UAAU,KAAK,MAAO,QAAO,MAAM,QAAQ,KAAK;AAC1D,WAAO,MAAM,OAAO,WAAW,cAAc,KAAK,OAAO,UAAU;AAAA,EACrE,CAAC;AAED,SAAO,SAAS,MAAM,GAAG,UAAU,EAAE,IAAI,CAAC,EAAE,QAAQ,OAAO,cAAc,OAAO;AAAA,IAC9E;AAAA,IACA;AAAA,IACA;AAAA,EACF,EAAE;AACJ;","names":[]}
|
|
@@ -68,8 +68,13 @@ function chunkContent(content, config = DEFAULT_CHUNKING_CONFIG) {
|
|
|
68
68
|
chunkIndex++;
|
|
69
69
|
if (!isLastSentence) {
|
|
70
70
|
const overlapCount = Math.min(config.overlapSentences, currentChunkSentences.length);
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if (overlapCount <= 0) {
|
|
72
|
+
currentChunkSentences = [];
|
|
73
|
+
currentTokens = 0;
|
|
74
|
+
} else {
|
|
75
|
+
currentChunkSentences = currentChunkSentences.slice(-overlapCount);
|
|
76
|
+
currentTokens = currentChunkSentences.reduce((sum, s) => sum + estimateTokens(s), 0);
|
|
77
|
+
}
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
80
|
}
|
|
@@ -109,4 +114,4 @@ export {
|
|
|
109
114
|
chunkContent,
|
|
110
115
|
reassembleChunks
|
|
111
116
|
};
|
|
112
|
-
//# sourceMappingURL=chunk-
|
|
117
|
+
//# sourceMappingURL=chunk-4WMCPJWX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/chunking.ts"],"sourcesContent":["/**\n * Automatic Chunking with Overlap (Phase 2A)\n *\n * Sentence-boundary chunking for long memories.\n * Preserves coherent thoughts by never splitting mid-sentence.\n */\n\nexport interface ChunkingConfig {\n /** Target tokens per chunk (default 200) */\n targetTokens: number;\n /** Minimum tokens to trigger chunking (default 150) */\n minTokens: number;\n /** Number of sentences to overlap between chunks (default 2) */\n overlapSentences: number;\n}\n\nexport interface Chunk {\n /** Chunk content */\n content: string;\n /** 0-based index */\n index: number;\n /** Approximate token count */\n tokenCount: number;\n}\n\nexport interface ChunkResult {\n /** Whether content was chunked */\n chunked: boolean;\n /** Array of chunks (length 1 if not chunked) */\n chunks: Chunk[];\n}\n\n/** Default chunking configuration */\nexport const DEFAULT_CHUNKING_CONFIG: ChunkingConfig = {\n targetTokens: 200,\n minTokens: 150,\n overlapSentences: 2,\n};\n\n/**\n * Estimate token count for text.\n * Rough approximation: ~4 characters per token for English.\n */\nfunction estimateTokens(text: string): number {\n return Math.ceil(text.length / 4);\n}\n\n/**\n * Split text into sentences.\n * Handles common abbreviations and edge cases.\n */\nfunction splitSentences(text: string): string[] {\n // Split on sentence-ending punctuation followed by whitespace or end of string\n // Preserve the punctuation with the sentence\n const sentences: string[] = [];\n\n // Regex to match sentence boundaries\n // Match: period/exclamation/question followed by space or end, but not abbreviations\n const sentenceRegex = /[^.!?]*[.!?]+(?:\\s+|$)/g;\n\n let match: RegExpExecArray | null;\n let lastIndex = 0;\n\n while ((match = sentenceRegex.exec(text)) !== null) {\n sentences.push(match[0].trim());\n lastIndex = sentenceRegex.lastIndex;\n }\n\n // Handle remaining text without sentence-ending punctuation\n if (lastIndex < text.length) {\n const remaining = text.slice(lastIndex).trim();\n if (remaining) {\n sentences.push(remaining);\n }\n }\n\n // Filter out empty sentences\n return sentences.filter((s) => s.length > 0);\n}\n\n/**\n * Chunk content into overlapping segments at sentence boundaries.\n *\n * @param content - The text content to chunk\n * @param config - Chunking configuration\n * @returns ChunkResult with chunks array\n */\nexport function chunkContent(\n content: string,\n config: ChunkingConfig = DEFAULT_CHUNKING_CONFIG,\n): ChunkResult {\n const totalTokens = estimateTokens(content);\n\n // Don't chunk if below minimum threshold\n if (totalTokens < config.minTokens) {\n return {\n chunked: false,\n chunks: [{\n content,\n index: 0,\n tokenCount: totalTokens,\n }],\n };\n }\n\n const sentences = splitSentences(content);\n\n // If we couldn't split into multiple sentences, don't chunk\n if (sentences.length <= 1) {\n return {\n chunked: false,\n chunks: [{\n content,\n index: 0,\n tokenCount: totalTokens,\n }],\n };\n }\n\n const chunks: Chunk[] = [];\n let currentChunkSentences: string[] = [];\n let currentTokens = 0;\n let chunkIndex = 0;\n\n for (let i = 0; i < sentences.length; i++) {\n const sentence = sentences[i];\n const sentenceTokens = estimateTokens(sentence);\n\n // Add sentence to current chunk\n currentChunkSentences.push(sentence);\n currentTokens += sentenceTokens;\n\n // Check if we've reached target size (with some flexibility)\n // Allow going over by up to 50% to avoid tiny final chunks\n const atTarget = currentTokens >= config.targetTokens;\n const isLastSentence = i === sentences.length - 1;\n\n if (atTarget || isLastSentence) {\n // Create chunk from accumulated sentences\n const chunkContent = currentChunkSentences.join(\" \");\n chunks.push({\n content: chunkContent,\n index: chunkIndex,\n tokenCount: estimateTokens(chunkContent),\n });\n chunkIndex++;\n\n // Start new chunk with overlap (if not at end)\n if (!isLastSentence) {\n // Keep last N sentences for overlap.\n // Guard: slice(-0) === slice(0), which returns the ENTIRE array\n // (CLAUDE.md gotcha #27). When overlapSentences is 0, clear fully.\n const overlapCount = Math.min(config.overlapSentences, currentChunkSentences.length);\n if (overlapCount <= 0) {\n currentChunkSentences = [];\n currentTokens = 0;\n } else {\n currentChunkSentences = currentChunkSentences.slice(-overlapCount);\n currentTokens = currentChunkSentences.reduce((sum, s) => sum + estimateTokens(s), 0);\n }\n }\n }\n }\n\n // Only consider it \"chunked\" if we got multiple chunks\n return {\n chunked: chunks.length > 1,\n chunks,\n };\n}\n\n/**\n * Get parent content by reassembling chunks.\n * Useful for displaying full context when a chunk is retrieved.\n *\n * @param chunks - Array of chunk contents in order\n * @returns Reassembled parent content (with overlap removed)\n */\nexport function reassembleChunks(chunks: string[]): string {\n if (chunks.length === 0) return \"\";\n if (chunks.length === 1) return chunks[0];\n\n // For overlapping chunks, we need to deduplicate\n // Simple approach: use full first chunk, then non-overlapping parts of subsequent chunks\n // This is imperfect but handles most cases\n const result: string[] = [chunks[0]];\n\n for (let i = 1; i < chunks.length; i++) {\n const prevChunk = chunks[i - 1];\n const currChunk = chunks[i];\n\n // Find overlap by looking for common suffix/prefix\n // Try to find where the previous chunk ends in the current chunk\n const prevSentences = splitSentences(prevChunk);\n const currSentences = splitSentences(currChunk);\n\n // Find how many sentences from prev are at the start of curr\n let overlapCount = 0;\n for (let j = 0; j < Math.min(prevSentences.length, currSentences.length); j++) {\n // Check if last N sentences of prev match first N sentences of curr\n const prevEnd = prevSentences.slice(-(j + 1));\n const currStart = currSentences.slice(0, j + 1);\n\n if (prevEnd.join(\" \") === currStart.join(\" \")) {\n overlapCount = j + 1;\n }\n }\n\n // Add non-overlapping portion\n if (overlapCount > 0 && overlapCount < currSentences.length) {\n result.push(currSentences.slice(overlapCount).join(\" \"));\n } else if (overlapCount === 0) {\n // No detected overlap, add full chunk\n result.push(currChunk);\n }\n // If overlapCount === currSentences.length, skip (fully contained)\n }\n\n return result.join(\" \");\n}\n"],"mappings":";AAiCO,IAAM,0BAA0C;AAAA,EACrD,cAAc;AAAA,EACd,WAAW;AAAA,EACX,kBAAkB;AACpB;AAMA,SAAS,eAAe,MAAsB;AAC5C,SAAO,KAAK,KAAK,KAAK,SAAS,CAAC;AAClC;AAMA,SAAS,eAAe,MAAwB;AAG9C,QAAM,YAAsB,CAAC;AAI7B,QAAM,gBAAgB;AAEtB,MAAI;AACJ,MAAI,YAAY;AAEhB,UAAQ,QAAQ,cAAc,KAAK,IAAI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,OAAO,KAAK,GAAG;AACxB;","names":["chunkContent"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
clamp01
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-TBBDFYXW.js";
|
|
4
4
|
|
|
5
5
|
// src/retrieval.ts
|
|
6
6
|
var DEFAULT_STOPWORDS = /* @__PURE__ */ new Set([
|
|
@@ -75,4 +75,4 @@ export {
|
|
|
75
75
|
applyRuntimeRetrievalPolicy,
|
|
76
76
|
expandQuery
|
|
77
77
|
};
|
|
78
|
-
//# sourceMappingURL=chunk-
|
|
78
|
+
//# sourceMappingURL=chunk-5IZL4DCV.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
log
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-2ODBA7MQ.js";
|
|
4
4
|
|
|
5
5
|
// src/relevance.ts
|
|
6
6
|
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
@@ -57,4 +57,4 @@ var RelevanceStore = class {
|
|
|
57
57
|
export {
|
|
58
58
|
RelevanceStore
|
|
59
59
|
};
|
|
60
|
-
//# sourceMappingURL=chunk-
|
|
60
|
+
//# sourceMappingURL=chunk-5NPGSAVB.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/whitespace.ts
|
|
2
|
+
function collapseWhitespace(value) {
|
|
3
|
+
return value.replace(/\s+/g, " ").trim();
|
|
4
|
+
}
|
|
5
|
+
function truncateCodePointSafe(value, maxChars) {
|
|
6
|
+
const glyphs = Array.from(value);
|
|
7
|
+
if (maxChars <= 0) return "";
|
|
8
|
+
if (glyphs.length <= maxChars) return value;
|
|
9
|
+
return glyphs.slice(0, Math.max(1, maxChars)).join("").trimEnd();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
collapseWhitespace,
|
|
14
|
+
truncateCodePointSafe
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=chunk-6MKAMLQL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/whitespace.ts"],"sourcesContent":["export function collapseWhitespace(value: string): string {\n return value.replace(/\\s+/g, \" \").trim();\n}\n\nexport function truncateCodePointSafe(value: string, maxChars: number): string {\n const glyphs = Array.from(value);\n if (maxChars <= 0) return \"\";\n if (glyphs.length <= maxChars) return value;\n return glyphs.slice(0, Math.max(1, maxChars)).join(\"\").trimEnd();\n}\n"],"mappings":";AAAO,SAAS,mBAAmB,OAAuB;AACxD,SAAO,MAAM,QAAQ,QAAQ,GAAG,EAAE,KAAK;AACzC;AAEO,SAAS,sBAAsB,OAAe,UAA0B;AAC7E,QAAM,SAAS,MAAM,KAAK,KAAK;AAC/B,MAAI,YAAY,EAAG,QAAO;AAC1B,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,SAAO,OAAO,MAAM,GAAG,KAAK,IAAI,GAAG,QAAQ,CAAC,EAAE,KAAK,EAAE,EAAE,QAAQ;AACjE;","names":[]}
|
|
@@ -32,14 +32,21 @@ function setCachedArchivedMemories(baseDir, memories, version) {
|
|
|
32
32
|
archiveCacheByDir.set(baseDir, { memories: map, version, loadedAt: Date.now() });
|
|
33
33
|
}
|
|
34
34
|
var entityCacheByDir = /* @__PURE__ */ new Map();
|
|
35
|
-
function
|
|
35
|
+
function buildEntityCacheKey(baseDir, schemaKey = "") {
|
|
36
|
+
return `${baseDir}\0${schemaKey}`;
|
|
37
|
+
}
|
|
38
|
+
function getCachedEntities(baseDir, currentVersion, schemaKey = "") {
|
|
36
39
|
if (currentVersion === 0) return null;
|
|
37
|
-
const entry = entityCacheByDir.get(baseDir);
|
|
40
|
+
const entry = entityCacheByDir.get(buildEntityCacheKey(baseDir, schemaKey));
|
|
38
41
|
if (!entry || entry.version !== currentVersion) return null;
|
|
39
42
|
return entry.entities;
|
|
40
43
|
}
|
|
41
|
-
function setCachedEntities(baseDir, entities, version) {
|
|
42
|
-
entityCacheByDir.set(baseDir,
|
|
44
|
+
function setCachedEntities(baseDir, entities, version, schemaKey = "") {
|
|
45
|
+
entityCacheByDir.set(buildEntityCacheKey(baseDir, schemaKey), {
|
|
46
|
+
entities,
|
|
47
|
+
version,
|
|
48
|
+
loadedAt: Date.now()
|
|
49
|
+
});
|
|
43
50
|
}
|
|
44
51
|
var episodeMapByDir = /* @__PURE__ */ new Map();
|
|
45
52
|
var ruleMemoriesByDir = /* @__PURE__ */ new Map();
|
|
@@ -102,7 +109,9 @@ function clearMemoryCache(baseDir) {
|
|
|
102
109
|
if (baseDir) {
|
|
103
110
|
hotCacheByDir.delete(baseDir);
|
|
104
111
|
archiveCacheByDir.delete(baseDir);
|
|
105
|
-
|
|
112
|
+
const entityPrefix = `${baseDir}\0`;
|
|
113
|
+
const entityKeysToDelete = [...entityCacheByDir.keys()].filter((key) => key.startsWith(entityPrefix));
|
|
114
|
+
for (const key of entityKeysToDelete) entityCacheByDir.delete(key);
|
|
106
115
|
episodeMapByDir.delete(baseDir);
|
|
107
116
|
ruleMemoriesByDir.delete(baseDir);
|
|
108
117
|
} else {
|
|
@@ -143,4 +152,4 @@ export {
|
|
|
143
152
|
clearMemoryCache,
|
|
144
153
|
getMemoryCacheStats
|
|
145
154
|
};
|
|
146
|
-
//# sourceMappingURL=chunk-
|
|
155
|
+
//# sourceMappingURL=chunk-6PFRXT4K.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/memory-cache.ts"],"sourcesContent":["import type { EntityFile, MemoryFile } from \"./types.js\";\n\ninterface CacheEntry {\n memories: Map<string, MemoryFile>; // keyed by file path\n version: number;\n loadedAt: number;\n}\n\n// Module-level singleton — shared across all StorageManager instances and sessions\nconst hotCacheByDir = new Map<string, CacheEntry>();\nconst archiveCacheByDir = new Map<string, CacheEntry>();\n\nexport function getCachedMemories(baseDir: string, currentVersion: number): MemoryFile[] | null {\n // Don't serve from cache when version tracking is unavailable (version=0).\n // This ensures tests and fresh installs without a version file always read disk.\n if (currentVersion === 0) return null;\n const entry = hotCacheByDir.get(baseDir);\n if (!entry || entry.version !== currentVersion) return null;\n return [...entry.memories.values()];\n}\n\nexport function setCachedMemories(baseDir: string, memories: MemoryFile[], version: number): void {\n const map = new Map<string, MemoryFile>();\n for (const m of memories) map.set(m.path, m);\n hotCacheByDir.set(baseDir, { memories: map, version, loadedAt: Date.now() });\n}\n\nexport function updateCacheOnWrite(baseDir: string, memory: MemoryFile): void {\n const entry = hotCacheByDir.get(baseDir);\n if (entry) entry.memories.set(memory.path, memory);\n}\n\nexport function updateCacheOnDelete(baseDir: string, filePath: string): void {\n const entry = hotCacheByDir.get(baseDir);\n if (entry) entry.memories.delete(filePath);\n}\n\n// Archive cache — same pattern, separate store\nexport function getCachedArchivedMemories(baseDir: string, currentVersion: number): MemoryFile[] | null {\n if (currentVersion === 0) return null;\n const entry = archiveCacheByDir.get(baseDir);\n if (!entry || entry.version !== currentVersion) return null;\n return [...entry.memories.values()];\n}\n\nexport function setCachedArchivedMemories(baseDir: string, memories: MemoryFile[], version: number): void {\n const map = new Map<string, MemoryFile>();\n for (const m of memories) map.set(m.path, m);\n archiveCacheByDir.set(baseDir, { memories: map, version, loadedAt: Date.now() });\n}\n\n// Entity cache — same pattern as memory cache, but keyed by schema-aware parse inputs.\nconst entityCacheByDir = new Map<string, { entities: EntityFile[]; version: number; loadedAt: number }>();\n\nfunction buildEntityCacheKey(baseDir: string, schemaKey: string = \"\"): string {\n return `${baseDir}\\u0000${schemaKey}`;\n}\n\nexport function getCachedEntities(\n baseDir: string,\n currentVersion: number,\n schemaKey: string = \"\",\n): EntityFile[] | null {\n if (currentVersion === 0) return null;\n const entry = entityCacheByDir.get(buildEntityCacheKey(baseDir, schemaKey));\n if (!entry || entry.version !== currentVersion) return null;\n return entry.entities;\n}\n\nexport function setCachedEntities(\n baseDir: string,\n entities: EntityFile[],\n version: number,\n schemaKey: string = \"\",\n): void {\n entityCacheByDir.set(buildEntityCacheKey(baseDir, schemaKey), {\n entities,\n version,\n loadedAt: Date.now(),\n });\n}\n\n// Derived caches — pre-filtered views invalidated alongside the main cache.\n// These avoid O(146K) filter+map on every verified recall/rules call.\ninterface DerivedCacheEntry<T> {\n data: T;\n sourceVersion: number; // matches the hot cache version it was derived from\n}\n\nconst episodeMapByDir = new Map<string, DerivedCacheEntry<Map<string, MemoryFile>>>();\nconst ruleMemoriesByDir = new Map<string, DerivedCacheEntry<{ all: MemoryFile[]; byId: Map<string, MemoryFile> }>>();\n\n/** Get a pre-filtered Map of episode memories (keyed by ID). Derived from hot cache. */\nexport function getCachedEpisodeMap(baseDir: string, currentVersion: number): Map<string, MemoryFile> | null {\n if (currentVersion === 0) return null;\n const entry = episodeMapByDir.get(baseDir);\n if (!entry || entry.sourceVersion !== currentVersion) return null;\n return entry.data;\n}\n\n/** Build and cache the episode memory map from the full memory list. */\nexport function setCachedEpisodeMap(baseDir: string, memories: MemoryFile[], version: number): Map<string, MemoryFile> {\n const map = new Map<string, MemoryFile>();\n for (const m of memories) {\n if (m.frontmatter.status === \"archived\") continue;\n if (m.frontmatter.memoryKind !== \"episode\") continue;\n map.set(m.frontmatter.id, m);\n }\n episodeMapByDir.set(baseDir, { data: map, sourceVersion: version });\n return map;\n}\n\n/** Get pre-filtered rule memories. Derived from hot cache. */\nexport function getCachedRuleMemories(baseDir: string, currentVersion: number): { all: MemoryFile[]; byId: Map<string, MemoryFile> } | null {\n if (currentVersion === 0) return null;\n const entry = ruleMemoriesByDir.get(baseDir);\n if (!entry || entry.sourceVersion !== currentVersion) return null;\n return entry.data;\n}\n\n/** Build and cache the rule memories from the full memory list. */\nexport function setCachedRuleMemories(baseDir: string, memories: MemoryFile[], version: number): { all: MemoryFile[]; byId: Map<string, MemoryFile> } {\n const byId = new Map<string, MemoryFile>();\n const all: MemoryFile[] = [];\n for (const m of memories) {\n byId.set(m.frontmatter.id, m);\n if (m.frontmatter.category === \"rule\" && m.frontmatter.status !== \"archived\") {\n all.push(m);\n }\n }\n const result = { all, byId };\n ruleMemoriesByDir.set(baseDir, { data: result, sourceVersion: version });\n return result;\n}\n\n// QMD search result cache — short-lived (60s TTL) to avoid stale results\n// while reducing redundant daemon calls for repeated/similar queries.\ninterface QmdCacheEntry {\n results: unknown[];\n cachedAt: number;\n}\nconst QMD_CACHE_TTL_MS = 60_000;\nconst qmdSearchCache = new Map<string, QmdCacheEntry>();\n\nexport function getCachedQmdSearch(cacheKey: string): unknown[] | null {\n const entry = qmdSearchCache.get(cacheKey);\n if (!entry) return null;\n if (Date.now() - entry.cachedAt > QMD_CACHE_TTL_MS) {\n qmdSearchCache.delete(cacheKey);\n return null;\n }\n return entry.results;\n}\n\nexport function setCachedQmdSearch(cacheKey: string, results: unknown[]): void {\n qmdSearchCache.set(cacheKey, { results, cachedAt: Date.now() });\n // Evict old entries to prevent unbounded growth\n if (qmdSearchCache.size > 200) {\n const now = Date.now();\n for (const [key, entry] of qmdSearchCache) {\n if (now - entry.cachedAt > QMD_CACHE_TTL_MS) qmdSearchCache.delete(key);\n }\n }\n}\n\nexport function clearMemoryCache(baseDir?: string): void {\n if (baseDir) {\n hotCacheByDir.delete(baseDir);\n archiveCacheByDir.delete(baseDir);\n const entityPrefix = `${baseDir}\\u0000`;\n const entityKeysToDelete = [...entityCacheByDir.keys()].filter((key) => key.startsWith(entityPrefix));\n for (const key of entityKeysToDelete) entityCacheByDir.delete(key);\n episodeMapByDir.delete(baseDir);\n ruleMemoriesByDir.delete(baseDir);\n } else {\n hotCacheByDir.clear();\n archiveCacheByDir.clear();\n entityCacheByDir.clear();\n episodeMapByDir.clear();\n ruleMemoriesByDir.clear();\n qmdSearchCache.clear();\n }\n}\n\nexport function getMemoryCacheStats(baseDir: string): {\n hotSize: number;\n archiveSize: number;\n hotVersion: number | null;\n archiveVersion: number | null;\n} {\n const hot = hotCacheByDir.get(baseDir);\n const archive = archiveCacheByDir.get(baseDir);\n return {\n hotSize: hot?.memories.size ?? 0,\n archiveSize: archive?.memories.size ?? 0,\n hotVersion: hot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