@remnic/core 9.3.622 → 9.3.623

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.
Files changed (141) hide show
  1. package/dist/access-cli.js +24 -24
  2. package/dist/access-http.js +9 -9
  3. package/dist/access-mcp.js +8 -8
  4. package/dist/access-service.js +7 -7
  5. package/dist/briefing.js +4 -4
  6. package/dist/buffer-surprise.js +3 -3
  7. package/dist/calibration.js +2 -2
  8. package/dist/causal-consolidation.js +8 -8
  9. package/dist/{chunk-UK727RHF.js → chunk-2L54V4ZO.js} +3 -3
  10. package/dist/{chunk-YPNGPHNZ.js → chunk-2UFQYU5F.js} +2 -2
  11. package/dist/{chunk-XAZOWLW4.js → chunk-3VONWEQB.js} +3 -3
  12. package/dist/{chunk-BF7ZRHH2.js → chunk-66SLUXKM.js} +2 -2
  13. package/dist/{chunk-AVHPSLQ2.js → chunk-AYHXQR53.js} +2 -2
  14. package/dist/{chunk-LANHQ7EN.js → chunk-BNW5NJJH.js} +2 -2
  15. package/dist/{chunk-6GMPIJAZ.js → chunk-C3IW2F5Z.js} +2 -2
  16. package/dist/{chunk-VNR3K2R3.js → chunk-C4PZTWTG.js} +14 -14
  17. package/dist/{chunk-3GM7ZY6H.js → chunk-FMGWXIES.js} +4 -4
  18. package/dist/{chunk-LMZ7XQBB.js → chunk-GLWW3EJQ.js} +3 -3
  19. package/dist/{chunk-2GRRN7SZ.js → chunk-GYTVOLNX.js} +2 -2
  20. package/dist/{chunk-IMA6GU4Y.js → chunk-H3PHZLMF.js} +3 -3
  21. package/dist/chunk-H3PHZLMF.js.map +1 -0
  22. package/dist/{chunk-XQUIHXNI.js → chunk-I6UCUHLK.js} +4 -4
  23. package/dist/{chunk-2I2MDQIB.js → chunk-I74SUMNI.js} +2 -2
  24. package/dist/chunk-I74SUMNI.js.map +1 -0
  25. package/dist/{chunk-4H5ZJHEN.js → chunk-J6A3CX5N.js} +8 -3
  26. package/dist/{chunk-4H5ZJHEN.js.map → chunk-J6A3CX5N.js.map} +1 -1
  27. package/dist/{chunk-DEVUWMME.js → chunk-KGIGRNR6.js} +2 -2
  28. package/dist/{chunk-EOLCAPOU.js → chunk-KQFQ3IS5.js} +5 -5
  29. package/dist/{chunk-QSVPYQPG.js → chunk-LDXUBPMO.js} +2 -2
  30. package/dist/chunk-LDXUBPMO.js.map +1 -0
  31. package/dist/{chunk-JFEKNTX7.js → chunk-LN4YGHTM.js} +6 -2
  32. package/dist/chunk-LN4YGHTM.js.map +1 -0
  33. package/dist/{chunk-WB3LYXC5.js → chunk-MON3LMO7.js} +3 -3
  34. package/dist/{chunk-GA3PMY73.js → chunk-O4UNM6OR.js} +2 -2
  35. package/dist/{chunk-4G2RQTAE.js → chunk-OZXVGYGZ.js} +2 -2
  36. package/dist/{chunk-WCYKT2DE.js → chunk-P4BC54KI.js} +23 -14
  37. package/dist/chunk-P4BC54KI.js.map +1 -0
  38. package/dist/{chunk-DXBCNDVD.js → chunk-PJGB7XRR.js} +5 -5
  39. package/dist/chunk-PJGB7XRR.js.map +1 -0
  40. package/dist/{chunk-ZNCDQZIS.js → chunk-QFQQFX2H.js} +3 -3
  41. package/dist/{chunk-ZNCDQZIS.js.map → chunk-QFQQFX2H.js.map} +1 -1
  42. package/dist/{chunk-CCNZM5UM.js → chunk-R3OQGYOU.js} +2 -2
  43. package/dist/{chunk-UZB5KHKX.js → chunk-RGMVMVMF.js} +2 -2
  44. package/dist/chunk-RGMVMVMF.js.map +1 -0
  45. package/dist/{chunk-EDP57PFC.js → chunk-RKW6QR7W.js} +22 -18
  46. package/dist/chunk-RKW6QR7W.js.map +1 -0
  47. package/dist/{chunk-4MHHUPNH.js → chunk-UGEBPVNI.js} +3 -3
  48. package/dist/{chunk-4WMCPJWX.js → chunk-UQ7RN5HK.js} +22 -13
  49. package/dist/chunk-UQ7RN5HK.js.map +1 -0
  50. package/dist/{chunk-ZUNNG6PC.js → chunk-W3BKVM64.js} +2 -2
  51. package/dist/{chunk-K5O2QY6T.js → chunk-YTWNKQ2G.js} +2 -2
  52. package/dist/chunk-YTWNKQ2G.js.map +1 -0
  53. package/dist/{chunk-2SGJY2UY.js → chunk-Z3CCEP6F.js} +3 -3
  54. package/dist/{chunk-4NS2ELXF.js → chunk-ZJSZNTEI.js} +4 -4
  55. package/dist/{chunk-UCGCSZP2.js → chunk-ZZPIJPPD.js} +2 -2
  56. package/dist/chunking.js +1 -1
  57. package/dist/cli.js +19 -19
  58. package/dist/compounding/engine.js +4 -4
  59. package/dist/connectors/codex-materialize-runner.js +5 -5
  60. package/dist/connectors/codex-materialize.js +1 -1
  61. package/dist/connectors/index.js +5 -5
  62. package/dist/contradiction/index.js +2 -2
  63. package/dist/{contradiction-scan-GD7KUFWS.js → contradiction-scan-AZTGFMPY.js} +3 -3
  64. package/dist/entity-retrieval.js +4 -4
  65. package/dist/explicit-capture.js +1 -1
  66. package/dist/extraction-judge.js +3 -3
  67. package/dist/extraction.js +3 -3
  68. package/dist/fallback-llm.js +2 -2
  69. package/dist/identity-continuity.js +1 -1
  70. package/dist/index.js +39 -36
  71. package/dist/index.js.map +1 -1
  72. package/dist/json-extract.js +1 -1
  73. package/dist/maintenance/memory-governance.js +4 -4
  74. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +4 -4
  75. package/dist/maintenance/rebuild-memory-projection.js +5 -5
  76. package/dist/namespaces/migrate.js +5 -5
  77. package/dist/namespaces/storage.js +4 -4
  78. package/dist/operator-toolkit.js +7 -7
  79. package/dist/orchestrator.js +21 -21
  80. package/dist/peers/index.js +1 -1
  81. package/dist/recall-planner-llm.js +2 -2
  82. package/dist/schemas.d.ts +22 -22
  83. package/dist/semantic-chunking.js +2 -2
  84. package/dist/semantic-consolidation.js +6 -6
  85. package/dist/semantic-rule-promotion.js +4 -4
  86. package/dist/semantic-rule-verifier.js +4 -4
  87. package/dist/source-attribution.js +1 -1
  88. package/dist/storage.js +3 -3
  89. package/dist/summarizer.js +3 -3
  90. package/dist/temporal-supersession.js +1 -1
  91. package/dist/transfer/types.d.ts +12 -12
  92. package/dist/verified-recall.js +4 -4
  93. package/package.json +1 -1
  94. package/src/chunking.ts +38 -23
  95. package/src/coding/review-context.ts +7 -1
  96. package/src/connectors/codex-materialize.ts +6 -1
  97. package/src/explicit-capture.ts +7 -2
  98. package/src/identity-continuity.ts +7 -1
  99. package/src/json-extract.ts +4 -1
  100. package/src/orchestrator.ts +5 -1
  101. package/src/peers/profile-reasoner.ts +4 -1
  102. package/src/semantic-chunking.ts +32 -16
  103. package/src/semantic-consolidation.ts +4 -1
  104. package/src/source-attribution.test.ts +21 -0
  105. package/src/source-attribution.ts +17 -2
  106. package/src/storage.ts +11 -2
  107. package/src/temporal-supersession.ts +4 -1
  108. package/dist/chunk-2I2MDQIB.js.map +0 -1
  109. package/dist/chunk-4WMCPJWX.js.map +0 -1
  110. package/dist/chunk-DXBCNDVD.js.map +0 -1
  111. package/dist/chunk-EDP57PFC.js.map +0 -1
  112. package/dist/chunk-IMA6GU4Y.js.map +0 -1
  113. package/dist/chunk-JFEKNTX7.js.map +0 -1
  114. package/dist/chunk-K5O2QY6T.js.map +0 -1
  115. package/dist/chunk-QSVPYQPG.js.map +0 -1
  116. package/dist/chunk-UZB5KHKX.js.map +0 -1
  117. package/dist/chunk-WCYKT2DE.js.map +0 -1
  118. /package/dist/{chunk-UK727RHF.js.map → chunk-2L54V4ZO.js.map} +0 -0
  119. /package/dist/{chunk-YPNGPHNZ.js.map → chunk-2UFQYU5F.js.map} +0 -0
  120. /package/dist/{chunk-XAZOWLW4.js.map → chunk-3VONWEQB.js.map} +0 -0
  121. /package/dist/{chunk-BF7ZRHH2.js.map → chunk-66SLUXKM.js.map} +0 -0
  122. /package/dist/{chunk-AVHPSLQ2.js.map → chunk-AYHXQR53.js.map} +0 -0
  123. /package/dist/{chunk-LANHQ7EN.js.map → chunk-BNW5NJJH.js.map} +0 -0
  124. /package/dist/{chunk-6GMPIJAZ.js.map → chunk-C3IW2F5Z.js.map} +0 -0
  125. /package/dist/{chunk-VNR3K2R3.js.map → chunk-C4PZTWTG.js.map} +0 -0
  126. /package/dist/{chunk-3GM7ZY6H.js.map → chunk-FMGWXIES.js.map} +0 -0
  127. /package/dist/{chunk-LMZ7XQBB.js.map → chunk-GLWW3EJQ.js.map} +0 -0
  128. /package/dist/{chunk-2GRRN7SZ.js.map → chunk-GYTVOLNX.js.map} +0 -0
  129. /package/dist/{chunk-XQUIHXNI.js.map → chunk-I6UCUHLK.js.map} +0 -0
  130. /package/dist/{chunk-DEVUWMME.js.map → chunk-KGIGRNR6.js.map} +0 -0
  131. /package/dist/{chunk-EOLCAPOU.js.map → chunk-KQFQ3IS5.js.map} +0 -0
  132. /package/dist/{chunk-WB3LYXC5.js.map → chunk-MON3LMO7.js.map} +0 -0
  133. /package/dist/{chunk-GA3PMY73.js.map → chunk-O4UNM6OR.js.map} +0 -0
  134. /package/dist/{chunk-4G2RQTAE.js.map → chunk-OZXVGYGZ.js.map} +0 -0
  135. /package/dist/{chunk-CCNZM5UM.js.map → chunk-R3OQGYOU.js.map} +0 -0
  136. /package/dist/{chunk-4MHHUPNH.js.map → chunk-UGEBPVNI.js.map} +0 -0
  137. /package/dist/{chunk-ZUNNG6PC.js.map → chunk-W3BKVM64.js.map} +0 -0
  138. /package/dist/{chunk-2SGJY2UY.js.map → chunk-Z3CCEP6F.js.map} +0 -0
  139. /package/dist/{chunk-4NS2ELXF.js.map → chunk-ZJSZNTEI.js.map} +0 -0
  140. /package/dist/{chunk-UCGCSZP2.js.map → chunk-ZZPIJPPD.js.map} +0 -0
  141. /package/dist/{contradiction-scan-GD7KUFWS.js.map → contradiction-scan-AZTGFMPY.js.map} +0 -0
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  runPostConsolidationMaterialize
3
- } from "./chunk-4MHHUPNH.js";
3
+ } from "./chunk-UGEBPVNI.js";
4
4
  import {
5
5
  discoverMemoryExtensions,
6
6
  renderExtensionsBlock,
@@ -135,7 +135,7 @@ function parseOperatorAwareConsolidationResponse(response, cluster) {
135
135
  };
136
136
  const trimmed = response.trim();
137
137
  if (trimmed.length === 0) return fallback;
138
- const fenced = /^```(?:json)?\s*([\s\S]*?)```\s*$/u.exec(trimmed);
138
+ const fenced = /^```(?:json)?([\s\S]*?)```$/u.exec(trimmed);
139
139
  const payload = fenced ? fenced[1].trim() : trimmed;
140
140
  const parsed = findLastJsonObjectWithOperator(payload);
141
141
  if (parsed === void 0) return fallback;
@@ -217,4 +217,4 @@ export {
217
217
  buildExtensionsBlockForConsolidation,
218
218
  materializeAfterSemanticConsolidation
219
219
  };
220
- //# sourceMappingURL=chunk-ZNCDQZIS.js.map
220
+ //# sourceMappingURL=chunk-QFQQFX2H.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/semantic-consolidation.ts"],"sourcesContent":["/**\n * semantic-consolidation.ts — Semantic Consolidation Engine\n *\n * Finds clusters of semantically similar memories using token overlap,\n * synthesizes canonical versions via LLM, and archives the originals.\n * Reduces memory store bloat while preserving all unique information.\n */\n\nimport type { MemoryFile, PluginConfig } from \"./types.js\";\nimport { normalizeRecallTokens, countRecallTokenOverlap } from \"./recall-tokenization.js\";\nimport { runPostConsolidationMaterialize } from \"./connectors/codex-materialize-runner.js\";\nimport type { MaterializeResult, RolloutSummaryInput } from \"./connectors/codex-materialize.js\";\nimport { discoverMemoryExtensions, renderExtensionsBlock, resolveExtensionsRoot } from \"./memory-extension-host/index.js\";\nimport { log } from \"./logger.js\";\n\n// Re-export resolveExtensionsRoot for backward compatibility — existing\n// consumers that import from semantic-consolidation.ts continue to work.\nexport { resolveExtensionsRoot } from \"./memory-extension-host/index.js\";\n\n// Operator vocabulary (issue #561). The types and validators live in a\n// standalone `consolidation-operator.ts` module so `storage.ts` can import\n// them without creating a `storage → semantic-consolidation →\n// codex-materialize-runner → storage` cycle. Re-exported here so existing\n// consumers that reach for `./semantic-consolidation.js` keep working.\nexport {\n CONSOLIDATION_OPERATORS,\n isConsolidationOperator,\n isSemanticConsolidationLlmOperator,\n isValidDerivedFromEntry,\n type ConsolidationOperator,\n type SemanticConsolidationLlmOperator,\n} from \"./consolidation-operator.js\";\n\nimport {\n CONSOLIDATION_OPERATORS as _CONSOLIDATION_OPERATORS,\n isConsolidationOperator as _isConsolidationOperator,\n isSemanticConsolidationLlmOperator as _isSemanticConsolidationLlmOperator,\n type ConsolidationOperator as _ConsolidationOperator,\n type SemanticConsolidationLlmOperator as _SemanticConsolidationLlmOperator,\n} from \"./consolidation-operator.js\";\n\nexport interface ConsolidationCluster {\n category: string;\n memories: MemoryFile[];\n overlapScore: number;\n canonicalContent?: string;\n}\n\nexport interface SemanticConsolidationResult {\n clustersFound: number;\n memoriesConsolidated: number;\n memoriesArchived: number;\n errors: number;\n clusters: ConsolidationCluster[];\n}\n\n/**\n * Find clusters of semantically similar memories using token overlap.\n */\nexport function findSimilarClusters(\n memories: MemoryFile[],\n config: {\n threshold: number;\n minClusterSize: number;\n excludeCategories: string[];\n maxPerRun: number;\n },\n): ConsolidationCluster[] {\n const excluded = new Set(config.excludeCategories);\n\n // Group by category first\n const byCategory = new Map<string, MemoryFile[]>();\n for (const m of memories) {\n const cat = m.frontmatter.category;\n if (excluded.has(cat)) continue;\n if (m.frontmatter.status && m.frontmatter.status !== \"active\") continue;\n const list = byCategory.get(cat) ?? [];\n list.push(m);\n byCategory.set(cat, list);\n }\n\n const clusters: ConsolidationCluster[] = [];\n let totalCandidates = 0;\n\n for (const [category, mems] of byCategory) {\n if (totalCandidates >= config.maxPerRun) break;\n\n // Token-normalize all memories in this category\n const tokenized = mems.map((m) => ({\n memory: m,\n tokens: new Set(normalizeRecallTokens(m.content, [])),\n }));\n\n // Track which memories are already clustered\n const clustered = new Set<string>();\n\n for (let i = 0; i < tokenized.length && totalCandidates < config.maxPerRun; i++) {\n const remainingBudget = config.maxPerRun - totalCandidates;\n if (remainingBudget < config.minClusterSize) break;\n if (clustered.has(tokenized[i].memory.frontmatter.id)) continue;\n\n const cluster: MemoryFile[] = [tokenized[i].memory];\n let totalOverlap = 0;\n let comparisons = 0;\n\n for (let j = i + 1; j < tokenized.length && cluster.length < remainingBudget; j++) {\n if (clustered.has(tokenized[j].memory.frontmatter.id)) continue;\n\n const aTokens = tokenized[i].tokens;\n const bTokens = tokenized[j].tokens;\n if (aTokens.size === 0 || bTokens.size === 0) continue;\n\n // Bidirectional overlap: what fraction of tokens are shared\n const overlap = countRecallTokenOverlap(aTokens, [...bTokens].join(\" \"));\n const maxTokens = Math.max(aTokens.size, bTokens.size);\n const score = maxTokens > 0 ? overlap / maxTokens : 0;\n\n if (score >= config.threshold) {\n cluster.push(tokenized[j].memory);\n totalOverlap += score;\n comparisons++;\n }\n }\n\n if (cluster.length >= config.minClusterSize) {\n for (const m of cluster) clustered.add(m.frontmatter.id);\n clusters.push({\n category,\n memories: cluster,\n overlapScore: comparisons > 0 ? totalOverlap / comparisons : 0,\n });\n totalCandidates += cluster.length;\n }\n }\n }\n\n return clusters;\n}\n\n/**\n * Build the LLM prompt for synthesizing a canonical memory from a cluster.\n */\nexport function buildConsolidationPrompt(cluster: ConsolidationCluster): string {\n const memoryTexts = cluster.memories\n .map(\n (m, i) =>\n `Memory ${i + 1} (${m.frontmatter.id}, created ${m.frontmatter.created}):\\n${m.content}`,\n )\n .join(\"\\n\\n\");\n\n return `You are a memory consolidation system. The following ${cluster.memories.length} memories in the \"${cluster.category}\" category contain overlapping information.\n\nSynthesize them into ONE canonical memory that:\n1. Preserves ALL unique information from every source memory\n2. Removes redundancy and repetition\n3. Uses clear, concise language\n4. Maintains the same category and tone\n5. Does NOT add information that isn't in the sources\n\n${memoryTexts}\n\nWrite ONLY the consolidated memory content (no metadata, no explanation, no preamble):`;\n}\n\n/**\n * Parse the LLM response to extract the canonical content.\n */\nexport function parseConsolidationResponse(response: string): string {\n return response.trim();\n}\n\n// ─── Operator-aware prompt / parse (issue #561 PR 3) ─────────────────────────\n\n/**\n * Structured result from an operator-aware consolidation LLM call.\n *\n * - `operator` — the consolidation operator the LLM chose for this cluster.\n * Falls back to the heuristic default when the LLM omits or returns an\n * unknown value (the parser never surfaces an invalid operator; see\n * `parseOperatorAwareConsolidationResponse`).\n * - `output` — the canonical content (same format the legacy prompt\n * returns). Callers persist this as the body of the new memory.\n */\nexport interface OperatorAwareConsolidationResult {\n // Restricted to the LLM-allowed subset (Cursor Bugbot, PR #730):\n // `pattern-reinforcement` is reserved for the maintenance job and\n // must never reach this struct from a consolidation LLM response.\n operator: _SemanticConsolidationLlmOperator;\n output: string;\n}\n\n/**\n * Heuristic default operator for a cluster. Used as the fallback when the\n * LLM does not return a parseable operator, and as the \"floor\" decision in\n * `parseOperatorAwareConsolidationResponse`.\n *\n * Current heuristic (kept deliberately conservative — PR 3 only):\n *\n * - Two or more memories being collapsed into one canonical blob is a\n * MERGE by definition. This is the path the current clustering\n * pipeline exercises.\n * - A cluster of size 1 that still reaches consolidation (future path,\n * e.g. supersession of a single older memory by a newer value) is an\n * UPDATE.\n * - SPLIT is never selected by the heuristic because the current write\n * path emits exactly one canonical memory per cluster. The prompt\n * reserves SPLIT for future cluster shapes where the LLM decides one\n * logical source actually encodes several distinct facts — at which\n * point the orchestrator would need to write multiple outputs.\n */\nexport function chooseConsolidationOperator(\n cluster: ConsolidationCluster,\n): _SemanticConsolidationLlmOperator {\n if (cluster.memories.length <= 1) return \"update\";\n return \"merge\";\n}\n\n/**\n * Build the operator-aware LLM prompt. The LLM is asked to return a\n * JSON object `{ \"operator\": <split|merge|update>, \"output\": <content> }`.\n *\n * The prompt is additive: it still asks for a single canonical blob under\n * `output`, so the upstream write path does not change. Future expansions\n * (SPLIT emitting multiple outputs) are explicitly documented as\n * out-of-scope for the parser — `parseOperatorAwareConsolidationResponse`\n * collapses any SPLIT response into a single canonical output for now.\n */\nexport function buildOperatorAwareConsolidationPrompt(\n cluster: ConsolidationCluster,\n): string {\n const memoryTexts = cluster.memories\n .map(\n (m, i) =>\n `Memory ${i + 1} (${m.frontmatter.id}, created ${m.frontmatter.created}):\\n${m.content}`,\n )\n .join(\"\\n\\n\");\n\n return `You are a memory consolidation system. The following ${cluster.memories.length} memories in the \"${cluster.category}\" category contain overlapping information.\n\nPick exactly ONE consolidation operator for this cluster and return a JSON object.\n\nOperator vocabulary:\n - \"merge\" — multiple distinct source memories overlap and should be collapsed into one canonical memory (most common).\n - \"update\" — one source memory carries a stale value that a newer source supersedes within the same logical fact.\n - \"split\" — a single logical source really encodes multiple distinct facts that should be separated (rare; if you pick split, still emit ONE canonical body — the write path will chunk it later).\n\nOutput JSON ONLY, no prose before or after. The \"operator\" key MUST be set to exactly one of the three strings \"merge\", \"update\", or \"split\" — never a pipe-separated placeholder like \"merge|update|split\". Example shape:\n {\n \"operator\": \"merge\",\n \"output\": \"<the canonical memory text>\"\n }\n\nThe \"output\" value must:\n1. Preserve ALL unique information from every source memory\n2. Remove redundancy and repetition\n3. Use clear, concise language\n4. Match the \"${cluster.category}\" category and tone\n5. NOT add information that isn't in the sources\n\n${memoryTexts}\n\nReturn ONLY the JSON object:`;\n}\n\n/**\n * Parse an operator-aware consolidation response.\n *\n * Contract:\n * - Accepts strict JSON `{ \"operator\": \"...\", \"output\": \"...\" }`.\n * - Tolerates a JSON payload wrapped in a fenced code block (```json ...```).\n * - Falls back to the heuristic operator when the JSON is malformed,\n * the `operator` field is missing / unknown, or the raw response is\n * a plain blob with no JSON at all. This keeps PR 3 backwards\n * compatible with older models that ignore the JSON instruction.\n * - Never throws. A missing / empty `output` field falls back to the\n * trimmed raw response so the caller still writes something rather\n * than dropping the cluster.\n */\nexport function parseOperatorAwareConsolidationResponse(\n response: string,\n cluster: ConsolidationCluster,\n): OperatorAwareConsolidationResult {\n const fallback: OperatorAwareConsolidationResult = {\n operator: chooseConsolidationOperator(cluster),\n output: response.trim(),\n };\n\n const trimmed = response.trim();\n if (trimmed.length === 0) return fallback;\n\n // Strip a fenced code block if present.\n const fenced = /^```(?:json)?\\s*([\\s\\S]*?)```\\s*$/u.exec(trimmed);\n const payload = fenced ? fenced[1].trim() : trimmed;\n\n // Find a balanced brace-delimited JSON object that has an `operator`\n // key (PR #632 round-4 review, codex P1). A first/last-brace slice\n // breaks when the model prepends an earlier brace block (e.g.\n // `Example: {\"note\":\"...\"} ... {\"operator\":\"merge\",...}`). Walk the\n // payload tracking nesting + string/escape state and skip past\n // objects that don't look like our target shape.\n const parsed = findLastJsonObjectWithOperator(payload);\n if (parsed === undefined) return fallback;\n if (typeof parsed !== \"object\" || parsed === null) return fallback;\n\n const obj = parsed as Record<string, unknown>;\n const rawOperator = typeof obj.operator === \"string\" ? obj.operator.trim().toLowerCase() : \"\";\n const rawOutput = typeof obj.output === \"string\" ? obj.output : \"\";\n\n // Narrow gate (Cursor Bugbot review on PR #730 head `aa1c2a8`):\n // accept ONLY the legacy split/merge/update LLM vocabulary here.\n // `pattern-reinforcement` joined the broader `ConsolidationOperator`\n // type in #687 PR 2/4 but is reserved for the maintenance job — if\n // an LLM hallucinates that operator we must NOT promote it onto\n // `derived_via`.\n const operator = _isSemanticConsolidationLlmOperator(rawOperator)\n ? rawOperator\n : chooseConsolidationOperator(cluster);\n const output = rawOutput.trim().length > 0 ? rawOutput.trim() : response.trim();\n\n return { operator, output };\n}\n\n/**\n * Walk `text`, find all balanced top-level `{ ... }` blocks whose\n * JSON.parse result is an object with an `operator` key, and return\n * the LAST one (function name reflects this — PR #632 review,\n * cursor Low). Returns `undefined` when nothing matches. Tracks\n * string state + escape sequences so braces inside string values\n * don't throw off the depth counter.\n *\n * Used by `parseOperatorAwareConsolidationResponse` to tolerate models\n * that prepend an explanatory JSON example block before the real\n * payload (PR #632 round-4 + round-5 review, codex P1). We take the\n * LAST candidate so that an instructional example with an `operator`\n * key ahead of the real answer doesn't steal precedence — models\n * typically write the example first and the real answer last.\n */\nfunction findLastJsonObjectWithOperator(text: string): unknown {\n let searchFrom = 0;\n let last: unknown = undefined;\n while (searchFrom < text.length) {\n const start = text.indexOf(\"{\", searchFrom);\n if (start < 0) return last;\n let depth = 0;\n let inString = false;\n let escape = false;\n let closed = false;\n let endIdx = -1;\n for (let i = start; i < text.length; i++) {\n const ch = text[i];\n if (inString) {\n if (escape) {\n escape = false;\n } else if (ch === \"\\\\\") {\n escape = true;\n } else if (ch === '\"') {\n inString = false;\n }\n continue;\n }\n if (ch === '\"') {\n inString = true;\n } else if (ch === \"{\") {\n depth += 1;\n } else if (ch === \"}\") {\n depth -= 1;\n if (depth === 0) {\n closed = true;\n endIdx = i;\n break;\n }\n }\n }\n if (!closed) return last;\n const slice = text.slice(start, endIdx + 1);\n try {\n const parsed = JSON.parse(slice);\n if (\n typeof parsed === \"object\" &&\n parsed !== null &&\n \"operator\" in (parsed as Record<string, unknown>)\n ) {\n last = parsed;\n }\n } catch {\n // Fall through to the next candidate.\n }\n searchFrom = endIdx + 1;\n }\n return last;\n}\n\n// Silence unused-import warnings when tsup tree-shakes: these are used\n// above in chooseConsolidationOperator + parse helpers.\nvoid _CONSOLIDATION_OPERATORS;\n\n/**\n * Discover extensions and build the block to append to a consolidation prompt.\n * Returns \"\" when extensions are disabled or none are found.\n */\nexport async function buildExtensionsBlockForConsolidation(\n config: PluginConfig,\n): Promise<string> {\n if (!config.memoryExtensionsEnabled) return \"\";\n const root = resolveExtensionsRoot(config);\n const extensions = await discoverMemoryExtensions(root, log);\n if (extensions.length === 0) return \"\";\n return renderExtensionsBlock(extensions);\n}\n\n/**\n * Optional post-consolidation hook — materializes the namespace into Codex's\n * native memory layout when the consolidation run finishes. Kept here (rather\n * than in orchestrator.ts) so #378 doesn't conflict with Wave 1 edits.\n *\n * Safe to call regardless of config state: honors `codexMaterializeMemories`\n * and `codexMaterializeOnConsolidation` and silently becomes a no-op when\n * either is disabled.\n */\nexport async function materializeAfterSemanticConsolidation(options: {\n config: PluginConfig;\n namespace?: string;\n memories?: MemoryFile[];\n memoryDir?: string;\n codexHome?: string;\n rolloutSummaries?: RolloutSummaryInput[];\n now?: Date;\n}): Promise<MaterializeResult | null> {\n // Delegates to the shared post-consolidation helper so semantic and causal\n // flows stay in lock-step — any guard/logging change happens in one place.\n return runPostConsolidationMaterialize(\"[semantic-consolidation]\", options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA2DO,SAAS,oBACd,UACA,QAMwB;AACxB,QAAM,WAAW,IAAI,IAAI,OAAO,iBAAiB;AAGjD,QAAM,aAAa,oBAAI,IAA0B;AACjD,aAAW,KAAK,UAAU;AACxB,UAAM,MAAM,EAAE,YAAY;AAC1B,QAAI,SAAS,IAAI,GAAG,EAAG;AACvB,QAAI,EAAE,YAAY,UAAU,EAAE,YAAY,WAAW,SAAU;AAC/D,UAAM,OAAO,WAAW,IAAI,GAAG,KAAK,CAAC;AACrC,SAAK,KAAK,CAAC;AACX,eAAW,IAAI,KAAK,IAAI;AAAA,EAC1B;AAEA,QAAM,WAAmC,CAAC;AAC1C,MAAI,kBAAkB;AAEtB,aAAW,CAAC,UAAU,IAAI,KAAK,YAAY;AACzC,QAAI,mBAAmB,OAAO,UAAW;AAGzC,UAAM,YAAY,KAAK,IAAI,CAAC,OAAO;AAAA,MACjC,QAAQ;AAAA,MACR,QAAQ,IAAI,IAAI,sBAAsB,EAAE,SAAS,CAAC,CAAC,CAAC;AAAA,IACtD,EAAE;AAGF,UAAM,YAAY,oBAAI,IAAY;AAElC,aAAS,IAAI,GAAG,IAAI,UAAU,UAAU,kBAAkB,OAAO,WAAW,KAAK;AAC/E,YAAM,kBAAkB,OAAO,YAAY;AAC3C,UAAI,kBAAkB,OAAO,eAAgB;AAC7C,UAAI,UAAU,IAAI,UAAU,CAAC,EAAE,OAAO,YAAY,EAAE,EAAG;AAEvD,YAAM,UAAwB,CAAC,UAAU,CAAC,EAAE,MAAM;AAClD,UAAI,eAAe;AACnB,UAAI,cAAc;AAElB,eAAS,IAAI,IAAI,GAAG,IAAI,UAAU,UAAU,QAAQ,SAAS,iBAAiB,KAAK;AACjF,YAAI,UAAU,IAAI,UAAU,CAAC,EAAE,OAAO,YAAY,EAAE,EAAG;AAEvD,cAAM,UAAU,UAAU,CAAC,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AQ,KAAK,QAAQ,KAAK,UAAU;AAC1C,QAAI,QAAQ,EAAG,QAAO;AACtB,QAAI,QAAQ;AACZ,QAAI,WAAW;AACf,QAAI,SAAS;AACb,QAAI,SAAS;AACb,QAAI,SAAS;AACb,aAAS,IAAI,OAAO,IAAI,KAAK,QAAQ,KAAK;AACxC,YAAM,KAAK,KAAK,CAAC;AACjB,UAAI,UAAU;AACZ,YAAI,QAAQ;AACV,mBAAS;AAAA,QACX,WAAW,OAAO,MAAM;AACtB,mBAAS;AAAA,QACX,WAAW,OAAO,KAAK;AACrB,qBAAW;AAAA,QACb;AACA;AAAA,MACF;AACA,UAAI,OAAO,KAAK;AACd,mBAAW;AAAA,MACb,WAAW,OAAO,KAAK;AACrB,iBAAS;AAAA,MACX,WAAW,OAAO,KAAK;AACrB,iBAAS;AACT,YAAI,UAAU,GAAG;AACf,mBAAS;AACT,mBAAS;AACT;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,OAAQ,QAAO;AACpB,UAAM,QAAQ,KAAK,MAAM,OAAO,SAAS,CAAC;AAC1C,QAAI;AACF,YAAM,SAAS,KAAK,MAAM,KAAK;AAC/B,UACE,OAAO,WAAW,YAClB,WAAW,QACX,cAAe,QACf;AACA,eAAO;AAAA,MACT;AAAA,IACF,QAAQ;AAAA,IAER;AACA,iBAAa,SAAS;AAAA,EACxB;AACA,SAAO;AACT;AAUA,eAAsB,qCACpB,QACiB;AACjB,MAAI,CAAC,OAAO,wBAAyB,QAAO;AAC5C,QAAM,OAAO,sBAAsB,MAAM;AACzC,QAAM,aAAa,MAAM,yBAAyB,MAAM,GAAG;AAC3D,MAAI,WAAW,WAAW,EAAG,QAAO;AACpC,SAAO,sBAAsB,UAAU;AACzC;AAWA,eAAsB,sCAAsC,SAQtB;AAGpC,SAAO,gCAAgC,4BAA4B,OAAO;AAC5E;","names":[]}
1
+ {"version":3,"sources":["../src/semantic-consolidation.ts"],"sourcesContent":["/**\n * semantic-consolidation.ts — Semantic Consolidation Engine\n *\n * Finds clusters of semantically similar memories using token overlap,\n * synthesizes canonical versions via LLM, and archives the originals.\n * Reduces memory store bloat while preserving all unique information.\n */\n\nimport type { MemoryFile, PluginConfig } from \"./types.js\";\nimport { normalizeRecallTokens, countRecallTokenOverlap } from \"./recall-tokenization.js\";\nimport { runPostConsolidationMaterialize } from \"./connectors/codex-materialize-runner.js\";\nimport type { MaterializeResult, RolloutSummaryInput } from \"./connectors/codex-materialize.js\";\nimport { discoverMemoryExtensions, renderExtensionsBlock, resolveExtensionsRoot } from \"./memory-extension-host/index.js\";\nimport { log } from \"./logger.js\";\n\n// Re-export resolveExtensionsRoot for backward compatibility — existing\n// consumers that import from semantic-consolidation.ts continue to work.\nexport { resolveExtensionsRoot } from \"./memory-extension-host/index.js\";\n\n// Operator vocabulary (issue #561). The types and validators live in a\n// standalone `consolidation-operator.ts` module so `storage.ts` can import\n// them without creating a `storage → semantic-consolidation →\n// codex-materialize-runner → storage` cycle. Re-exported here so existing\n// consumers that reach for `./semantic-consolidation.js` keep working.\nexport {\n CONSOLIDATION_OPERATORS,\n isConsolidationOperator,\n isSemanticConsolidationLlmOperator,\n isValidDerivedFromEntry,\n type ConsolidationOperator,\n type SemanticConsolidationLlmOperator,\n} from \"./consolidation-operator.js\";\n\nimport {\n CONSOLIDATION_OPERATORS as _CONSOLIDATION_OPERATORS,\n isConsolidationOperator as _isConsolidationOperator,\n isSemanticConsolidationLlmOperator as _isSemanticConsolidationLlmOperator,\n type ConsolidationOperator as _ConsolidationOperator,\n type SemanticConsolidationLlmOperator as _SemanticConsolidationLlmOperator,\n} from \"./consolidation-operator.js\";\n\nexport interface ConsolidationCluster {\n category: string;\n memories: MemoryFile[];\n overlapScore: number;\n canonicalContent?: string;\n}\n\nexport interface SemanticConsolidationResult {\n clustersFound: number;\n memoriesConsolidated: number;\n memoriesArchived: number;\n errors: number;\n clusters: ConsolidationCluster[];\n}\n\n/**\n * Find clusters of semantically similar memories using token overlap.\n */\nexport function findSimilarClusters(\n memories: MemoryFile[],\n config: {\n threshold: number;\n minClusterSize: number;\n excludeCategories: string[];\n maxPerRun: number;\n },\n): ConsolidationCluster[] {\n const excluded = new Set(config.excludeCategories);\n\n // Group by category first\n const byCategory = new Map<string, MemoryFile[]>();\n for (const m of memories) {\n const cat = m.frontmatter.category;\n if (excluded.has(cat)) continue;\n if (m.frontmatter.status && m.frontmatter.status !== \"active\") continue;\n const list = byCategory.get(cat) ?? [];\n list.push(m);\n byCategory.set(cat, list);\n }\n\n const clusters: ConsolidationCluster[] = [];\n let totalCandidates = 0;\n\n for (const [category, mems] of byCategory) {\n if (totalCandidates >= config.maxPerRun) break;\n\n // Token-normalize all memories in this category\n const tokenized = mems.map((m) => ({\n memory: m,\n tokens: new Set(normalizeRecallTokens(m.content, [])),\n }));\n\n // Track which memories are already clustered\n const clustered = new Set<string>();\n\n for (let i = 0; i < tokenized.length && totalCandidates < config.maxPerRun; i++) {\n const remainingBudget = config.maxPerRun - totalCandidates;\n if (remainingBudget < config.minClusterSize) break;\n if (clustered.has(tokenized[i].memory.frontmatter.id)) continue;\n\n const cluster: MemoryFile[] = [tokenized[i].memory];\n let totalOverlap = 0;\n let comparisons = 0;\n\n for (let j = i + 1; j < tokenized.length && cluster.length < remainingBudget; j++) {\n if (clustered.has(tokenized[j].memory.frontmatter.id)) continue;\n\n const aTokens = tokenized[i].tokens;\n const bTokens = tokenized[j].tokens;\n if (aTokens.size === 0 || bTokens.size === 0) continue;\n\n // Bidirectional overlap: what fraction of tokens are shared\n const overlap = countRecallTokenOverlap(aTokens, [...bTokens].join(\" \"));\n const maxTokens = Math.max(aTokens.size, bTokens.size);\n const score = maxTokens > 0 ? overlap / maxTokens : 0;\n\n if (score >= config.threshold) {\n cluster.push(tokenized[j].memory);\n totalOverlap += score;\n comparisons++;\n }\n }\n\n if (cluster.length >= config.minClusterSize) {\n for (const m of cluster) clustered.add(m.frontmatter.id);\n clusters.push({\n category,\n memories: cluster,\n overlapScore: comparisons > 0 ? totalOverlap / comparisons : 0,\n });\n totalCandidates += cluster.length;\n }\n }\n }\n\n return clusters;\n}\n\n/**\n * Build the LLM prompt for synthesizing a canonical memory from a cluster.\n */\nexport function buildConsolidationPrompt(cluster: ConsolidationCluster): string {\n const memoryTexts = cluster.memories\n .map(\n (m, i) =>\n `Memory ${i + 1} (${m.frontmatter.id}, created ${m.frontmatter.created}):\\n${m.content}`,\n )\n .join(\"\\n\\n\");\n\n return `You are a memory consolidation system. The following ${cluster.memories.length} memories in the \"${cluster.category}\" category contain overlapping information.\n\nSynthesize them into ONE canonical memory that:\n1. Preserves ALL unique information from every source memory\n2. Removes redundancy and repetition\n3. Uses clear, concise language\n4. Maintains the same category and tone\n5. Does NOT add information that isn't in the sources\n\n${memoryTexts}\n\nWrite ONLY the consolidated memory content (no metadata, no explanation, no preamble):`;\n}\n\n/**\n * Parse the LLM response to extract the canonical content.\n */\nexport function parseConsolidationResponse(response: string): string {\n return response.trim();\n}\n\n// ─── Operator-aware prompt / parse (issue #561 PR 3) ─────────────────────────\n\n/**\n * Structured result from an operator-aware consolidation LLM call.\n *\n * - `operator` — the consolidation operator the LLM chose for this cluster.\n * Falls back to the heuristic default when the LLM omits or returns an\n * unknown value (the parser never surfaces an invalid operator; see\n * `parseOperatorAwareConsolidationResponse`).\n * - `output` — the canonical content (same format the legacy prompt\n * returns). Callers persist this as the body of the new memory.\n */\nexport interface OperatorAwareConsolidationResult {\n // Restricted to the LLM-allowed subset (Cursor Bugbot, PR #730):\n // `pattern-reinforcement` is reserved for the maintenance job and\n // must never reach this struct from a consolidation LLM response.\n operator: _SemanticConsolidationLlmOperator;\n output: string;\n}\n\n/**\n * Heuristic default operator for a cluster. Used as the fallback when the\n * LLM does not return a parseable operator, and as the \"floor\" decision in\n * `parseOperatorAwareConsolidationResponse`.\n *\n * Current heuristic (kept deliberately conservative — PR 3 only):\n *\n * - Two or more memories being collapsed into one canonical blob is a\n * MERGE by definition. This is the path the current clustering\n * pipeline exercises.\n * - A cluster of size 1 that still reaches consolidation (future path,\n * e.g. supersession of a single older memory by a newer value) is an\n * UPDATE.\n * - SPLIT is never selected by the heuristic because the current write\n * path emits exactly one canonical memory per cluster. The prompt\n * reserves SPLIT for future cluster shapes where the LLM decides one\n * logical source actually encodes several distinct facts — at which\n * point the orchestrator would need to write multiple outputs.\n */\nexport function chooseConsolidationOperator(\n cluster: ConsolidationCluster,\n): _SemanticConsolidationLlmOperator {\n if (cluster.memories.length <= 1) return \"update\";\n return \"merge\";\n}\n\n/**\n * Build the operator-aware LLM prompt. The LLM is asked to return a\n * JSON object `{ \"operator\": <split|merge|update>, \"output\": <content> }`.\n *\n * The prompt is additive: it still asks for a single canonical blob under\n * `output`, so the upstream write path does not change. Future expansions\n * (SPLIT emitting multiple outputs) are explicitly documented as\n * out-of-scope for the parser — `parseOperatorAwareConsolidationResponse`\n * collapses any SPLIT response into a single canonical output for now.\n */\nexport function buildOperatorAwareConsolidationPrompt(\n cluster: ConsolidationCluster,\n): string {\n const memoryTexts = cluster.memories\n .map(\n (m, i) =>\n `Memory ${i + 1} (${m.frontmatter.id}, created ${m.frontmatter.created}):\\n${m.content}`,\n )\n .join(\"\\n\\n\");\n\n return `You are a memory consolidation system. The following ${cluster.memories.length} memories in the \"${cluster.category}\" category contain overlapping information.\n\nPick exactly ONE consolidation operator for this cluster and return a JSON object.\n\nOperator vocabulary:\n - \"merge\" — multiple distinct source memories overlap and should be collapsed into one canonical memory (most common).\n - \"update\" — one source memory carries a stale value that a newer source supersedes within the same logical fact.\n - \"split\" — a single logical source really encodes multiple distinct facts that should be separated (rare; if you pick split, still emit ONE canonical body — the write path will chunk it later).\n\nOutput JSON ONLY, no prose before or after. The \"operator\" key MUST be set to exactly one of the three strings \"merge\", \"update\", or \"split\" — never a pipe-separated placeholder like \"merge|update|split\". Example shape:\n {\n \"operator\": \"merge\",\n \"output\": \"<the canonical memory text>\"\n }\n\nThe \"output\" value must:\n1. Preserve ALL unique information from every source memory\n2. Remove redundancy and repetition\n3. Use clear, concise language\n4. Match the \"${cluster.category}\" category and tone\n5. NOT add information that isn't in the sources\n\n${memoryTexts}\n\nReturn ONLY the JSON object:`;\n}\n\n/**\n * Parse an operator-aware consolidation response.\n *\n * Contract:\n * - Accepts strict JSON `{ \"operator\": \"...\", \"output\": \"...\" }`.\n * - Tolerates a JSON payload wrapped in a fenced code block (```json ...```).\n * - Falls back to the heuristic operator when the JSON is malformed,\n * the `operator` field is missing / unknown, or the raw response is\n * a plain blob with no JSON at all. This keeps PR 3 backwards\n * compatible with older models that ignore the JSON instruction.\n * - Never throws. A missing / empty `output` field falls back to the\n * trimmed raw response so the caller still writes something rather\n * than dropping the cluster.\n */\nexport function parseOperatorAwareConsolidationResponse(\n response: string,\n cluster: ConsolidationCluster,\n): OperatorAwareConsolidationResult {\n const fallback: OperatorAwareConsolidationResult = {\n operator: chooseConsolidationOperator(cluster),\n output: response.trim(),\n };\n\n const trimmed = response.trim();\n if (trimmed.length === 0) return fallback;\n\n // Strip a fenced code block if present.\n // Dropped the \\s* groups around the lazy body (they overlapped it and\n // backtracked polynomially — CodeQL js/polynomial-redos). Input is already\n // trimmed and fenced[1] is trimmed below, so matches are identical.\n const fenced = /^```(?:json)?([\\s\\S]*?)```$/u.exec(trimmed);\n const payload = fenced ? fenced[1].trim() : trimmed;\n\n // Find a balanced brace-delimited JSON object that has an `operator`\n // key (PR #632 round-4 review, codex P1). A first/last-brace slice\n // breaks when the model prepends an earlier brace block (e.g.\n // `Example: {\"note\":\"...\"} ... {\"operator\":\"merge\",...}`). Walk the\n // payload tracking nesting + string/escape state and skip past\n // objects that don't look like our target shape.\n const parsed = findLastJsonObjectWithOperator(payload);\n if (parsed === undefined) return fallback;\n if (typeof parsed !== \"object\" || parsed === null) return fallback;\n\n const obj = parsed as Record<string, unknown>;\n const rawOperator = typeof obj.operator === \"string\" ? obj.operator.trim().toLowerCase() : \"\";\n const rawOutput = typeof obj.output === \"string\" ? obj.output : \"\";\n\n // Narrow gate (Cursor Bugbot review on PR #730 head `aa1c2a8`):\n // accept ONLY the legacy split/merge/update LLM vocabulary here.\n // `pattern-reinforcement` joined the broader `ConsolidationOperator`\n // type in #687 PR 2/4 but is reserved for the maintenance job — if\n // an LLM hallucinates that operator we must NOT promote it onto\n // `derived_via`.\n const operator = _isSemanticConsolidationLlmOperator(rawOperator)\n ? rawOperator\n : chooseConsolidationOperator(cluster);\n const output = rawOutput.trim().length > 0 ? rawOutput.trim() : response.trim();\n\n return { operator, output };\n}\n\n/**\n * Walk `text`, find all balanced top-level `{ ... }` blocks whose\n * JSON.parse result is an object with an `operator` key, and return\n * the LAST one (function name reflects this — PR #632 review,\n * cursor Low). Returns `undefined` when nothing matches. Tracks\n * string state + escape sequences so braces inside string values\n * don't throw off the depth counter.\n *\n * Used by `parseOperatorAwareConsolidationResponse` to tolerate models\n * that prepend an explanatory JSON example block before the real\n * payload (PR #632 round-4 + round-5 review, codex P1). We take the\n * LAST candidate so that an instructional example with an `operator`\n * key ahead of the real answer doesn't steal precedence — models\n * typically write the example first and the real answer last.\n */\nfunction findLastJsonObjectWithOperator(text: string): unknown {\n let searchFrom = 0;\n let last: unknown = undefined;\n while (searchFrom < text.length) {\n const start = text.indexOf(\"{\", searchFrom);\n if (start < 0) return last;\n let depth = 0;\n let inString = false;\n let escape = false;\n let closed = false;\n let endIdx = -1;\n for (let i = start; i < text.length; i++) {\n const ch = text[i];\n if (inString) {\n if (escape) {\n escape = false;\n } else if (ch === \"\\\\\") {\n escape = true;\n } else if (ch === '\"') {\n inString = false;\n }\n continue;\n }\n if (ch === '\"') {\n inString = true;\n } else if (ch === \"{\") {\n depth += 1;\n } else if (ch === \"}\") {\n depth -= 1;\n if (depth === 0) {\n closed = true;\n endIdx = i;\n break;\n }\n }\n }\n if (!closed) return last;\n const slice = text.slice(start, endIdx + 1);\n try {\n const parsed = JSON.parse(slice);\n if (\n typeof parsed === \"object\" &&\n parsed !== null &&\n \"operator\" in (parsed as Record<string, unknown>)\n ) {\n last = parsed;\n }\n } catch {\n // Fall through to the next candidate.\n }\n searchFrom = endIdx + 1;\n }\n return last;\n}\n\n// Silence unused-import warnings when tsup tree-shakes: these are used\n// above in chooseConsolidationOperator + parse helpers.\nvoid _CONSOLIDATION_OPERATORS;\n\n/**\n * Discover extensions and build the block to append to a consolidation prompt.\n * Returns \"\" when extensions are disabled or none are found.\n */\nexport async function buildExtensionsBlockForConsolidation(\n config: PluginConfig,\n): Promise<string> {\n if (!config.memoryExtensionsEnabled) return \"\";\n const root = resolveExtensionsRoot(config);\n const extensions = await discoverMemoryExtensions(root, log);\n if (extensions.length === 0) return \"\";\n return renderExtensionsBlock(extensions);\n}\n\n/**\n * Optional post-consolidation hook — materializes the namespace into Codex's\n * native memory layout when the consolidation run finishes. Kept here (rather\n * than in orchestrator.ts) so #378 doesn't conflict with Wave 1 edits.\n *\n * Safe to call regardless of config state: honors `codexMaterializeMemories`\n * and `codexMaterializeOnConsolidation` and silently becomes a no-op when\n * either is disabled.\n */\nexport async function materializeAfterSemanticConsolidation(options: {\n config: PluginConfig;\n namespace?: string;\n memories?: MemoryFile[];\n memoryDir?: string;\n codexHome?: string;\n rolloutSummaries?: RolloutSummaryInput[];\n now?: Date;\n}): Promise<MaterializeResult | null> {\n // Delegates to the shared post-consolidation helper so semantic and causal\n // flows stay in lock-step — any guard/logging change happens in one place.\n return runPostConsolidationMaterialize(\"[semantic-consolidation]\", options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA2DO,SAAS,oBACd,UACA,QAMwB;AACxB,QAAM,WAAW,IAAI,IAAI,OAAO,iBAAiB;AAGjD,QAAM,aAAa,oBAAI,IAA0B;AACjD,aAAW,KAAK,UAAU;AACxB,UAAM,MAAM,EAAE,YAAY;AAC1B,QAAI,SAAS,IAAI,GAAG,EAAG;AACvB,QAAI,EAAE,YAAY,UAAU,EAAE,YAAY,WAAW,SAAU;AAC/D,UAAM,OAAO,WAAW,IAAI,GAAG,KAAK,CAAC;AACrC,SAAK,KAAK,CAAC;A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;AAAA,MACb,WAAW,OAAO,KAAK;AACrB,iBAAS;AAAA,MACX,WAAW,OAAO,KAAK;AACrB,iBAAS;AACT,YAAI,UAAU,GAAG;AACf,mBAAS;AACT,mBAAS;AACT;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,OAAQ,QAAO;AACpB,UAAM,QAAQ,KAAK,MAAM,OAAO,SAAS,CAAC;AAC1C,QAAI;AACF,YAAM,SAAS,KAAK,MAAM,KAAK;AAC/B,UACE,OAAO,WAAW,YAClB,WAAW,QACX,cAAe,QACf;AACA,eAAO;AAAA,MACT;AAAA,IACF,QAAQ;AAAA,IAER;AACA,iBAAa,SAAS;AAAA,EACxB;AACA,SAAO;AACT;AAUA,eAAsB,qCACpB,QACiB;AACjB,MAAI,CAAC,OAAO,wBAAyB,QAAO;AAC5C,QAAM,OAAO,sBAAsB,MAAM;AACzC,QAAM,aAAa,MAAM,yBAAyB,MAAM,GAAG;AAC3D,MAAI,WAAW,WAAW,EAAG,QAAO;AACpC,SAAO,sBAAsB,UAAU;AACzC;AAWA,eAAsB,sCAAsC,SAQtB;AAGpC,SAAO,gCAAgC,4BAA4B,OAAO;AAC5E;","names":[]}
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-HQ6NIBL6.js";
4
4
  import {
5
5
  StorageManager
6
- } from "./chunk-DXBCNDVD.js";
6
+ } from "./chunk-PJGB7XRR.js";
7
7
  import {
8
8
  getCachedEpisodeMap,
9
9
  setCachedEpisodeMap
@@ -105,4 +105,4 @@ async function searchVerifiedEpisodes(options) {
105
105
  export {
106
106
  searchVerifiedEpisodes
107
107
  };
108
- //# sourceMappingURL=chunk-CCNZM5UM.js.map
108
+ //# sourceMappingURL=chunk-R3OQGYOU.js.map
@@ -1,6 +1,6 @@
1
1
  // src/json-extract.ts
2
2
  function stripCodeFences(text) {
3
- return text.replace(/```(?:json)?\s*([\s\S]*?)```/gi, (_m, inner) => String(inner).trim());
3
+ return text.replace(/```(?:json)?([\s\S]*?)```/gi, (_m, inner) => String(inner).trim());
4
4
  }
5
5
  function extractJsonCandidates(text) {
6
6
  const trimmed = text.trim();
@@ -60,4 +60,4 @@ export {
60
60
  stripCodeFences,
61
61
  extractJsonCandidates
62
62
  };
63
- //# sourceMappingURL=chunk-UZB5KHKX.js.map
63
+ //# sourceMappingURL=chunk-RGMVMVMF.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/json-extract.ts"],"sourcesContent":["/**\n * Utilities for extracting JSON payloads from LLM outputs.\n *\n * We see common failure modes:\n * - \"Here's an example: {..}\\nHere's the real answer: {..}\" (multiple JSON blocks)\n * - fenced ```json blocks\n * - leading/trailing prose around JSON\n *\n * These helpers attempt multiple candidates and let callers validate with schemas.\n */\n\nexport function stripCodeFences(text: string): string {\n // Drop the leading \\s* before the lazy body: it overlapped the body and caused\n // polynomial backtracking on unterminated fences (CodeQL js/polynomial-redos).\n // inner is trimmed, so captured content is identical.\n return text.replace(/```(?:json)?([\\s\\S]*?)```/gi, (_m, inner) => String(inner).trim());\n}\n\nexport function extractJsonCandidates(text: string): string[] {\n const trimmed = text.trim();\n const cleaned = stripCodeFences(trimmed);\n const candidates: string[] = [];\n\n if (cleaned.length > 0) candidates.push(cleaned);\n candidates.push(...scanBalancedJsonBlocks(cleaned));\n\n // Legacy regex fallback (single object)\n const objMatch = cleaned.match(/\\{[\\s\\S]*\\}/);\n if (objMatch) candidates.push(objMatch[0]);\n\n const seen = new Set<string>();\n return candidates\n .map((c) => c.trim())\n .filter((c) => c.length > 0)\n .filter((c) => {\n if (seen.has(c)) return false;\n seen.add(c);\n return true;\n });\n}\n\nfunction scanBalancedJsonBlocks(text: string): string[] {\n const out: string[] = [];\n const opens = new Set([\"{\", \"[\"]);\n const closes: Record<string, string> = { \"{\": \"}\", \"[\": \"]\" };\n\n for (let i = 0; i < text.length; i++) {\n const start = text[i];\n if (!opens.has(start)) continue;\n\n const expectedClose = closes[start];\n let depth = 0;\n let inString = false;\n let escape = false;\n\n for (let j = i; j < text.length; j++) {\n const ch = text[j];\n\n if (inString) {\n if (escape) {\n escape = false;\n } else if (ch === \"\\\\\") {\n escape = true;\n } else if (ch === \"\\\"\") {\n inString = false;\n }\n continue;\n }\n\n if (ch === \"\\\"\") {\n inString = true;\n continue;\n }\n\n if (ch === start) depth++;\n if (ch === expectedClose) depth--;\n\n if (depth === 0) {\n out.push(text.slice(i, j + 1).trim());\n i = j;\n break;\n }\n }\n }\n\n return out;\n}\n\n"],"mappings":";AAWO,SAAS,gBAAgB,MAAsB;AAIpD,SAAO,KAAK,QAAQ,+BAA+B,CAAC,IAAI,UAAU,OAAO,KAAK,EAAE,KAAK,CAAC;AACxF;AAEO,SAAS,sBAAsB,MAAwB;AAC5D,QAAM,UAAU,KAAK,KAAK;AAC1B,QAAM,UAAU,gBAAgB,OAAO;AACvC,QAAM,aAAuB,CAAC;AAE9B,MAAI,QAAQ,SAAS,EAAG,YAAW,KAAK,OAAO;AAC/C,aAAW,KAAK,GAAG,uBAAuB,OAAO,CAAC;AAGlD,QAAM,WAAW,QAAQ,MAAM,aAAa;AAC5C,MAAI,SAAU,YAAW,KAAK,SAAS,CAAC,CAAC;AAEzC,QAAM,OAAO,oBAAI,IAAY;AAC7B,SAAO,WACJ,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,EACnB,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,EAC1B,OAAO,CAAC,MAAM;AACb,QAAI,KAAK,IAAI,CAAC,EAAG,QAAO;AACxB,SAAK,IAAI,CAAC;AACV,WAAO;AAAA,EACT,CAAC;AACL;AAEA,SAAS,uBAAuB,MAAwB;AACtD,QAAM,MAAgB,CAAC;AACvB,QAAM,QAAQ,oBAAI,IAAI,CAAC,KAAK,GAAG,CAAC;AAChC,QAAM,SAAiC,EAAE,KAAK,KAAK,KAAK,IAAI;AAE5D,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,UAAM,QAAQ,KAAK,CAAC;AACpB,QAAI,CAAC,MAAM,IAAI,KAAK,EAAG;AAEvB,UAAM,gBAAgB,OAAO,KAAK;AAClC,QAAI,QAAQ;AACZ,QAAI,WAAW;AACf,QAAI,SAAS;AAEb,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,YAAM,KAAK,KAAK,CAAC;AAEjB,UAAI,UAAU;AACZ,YAAI,QAAQ;AACV,mBAAS;AAAA,QACX,WAAW,OAAO,MAAM;AACtB,mBAAS;AAAA,QACX,WAAW,OAAO,KAAM;AACtB,qBAAW;AAAA,QACb;AACA;AAAA,MACF;AAEA,UAAI,OAAO,KAAM;AACf,mBAAW;AACX;AAAA,MACF;AAEA,UAAI,OAAO,MAAO;AAClB,UAAI,OAAO,cAAe;AAE1B,UAAI,UAAU,GAAG;AACf,YAAI,KAAK,KAAK,MAAM,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;AACpC,YAAI;AACJ;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -22,7 +22,7 @@ import {
22
22
  import {
23
23
  CompoundingEngine,
24
24
  defaultTierMigrationCycleBudget
25
- } from "./chunk-UK727RHF.js";
25
+ } from "./chunk-2L54V4ZO.js";
26
26
  import {
27
27
  SharedContextManager
28
28
  } from "./chunk-DRD2Q7HQ.js";
@@ -48,7 +48,7 @@ import {
48
48
  applyTemporalSupersession,
49
49
  normalizeSupersessionKey,
50
50
  shouldFilterSupersededFromRecall
51
- } from "./chunk-K5O2QY6T.js";
51
+ } from "./chunk-YTWNKQ2G.js";
52
52
  import {
53
53
  isValidAsOf
54
54
  } from "./chunk-MDYG7VI7.js";
@@ -57,7 +57,7 @@ import {
57
57
  } from "./chunk-VOUOLGIP.js";
58
58
  import {
59
59
  HourlySummarizer
60
- } from "./chunk-XAZOWLW4.js";
60
+ } from "./chunk-3VONWEQB.js";
61
61
  import {
62
62
  SessionObserverState
63
63
  } from "./chunk-MHQC2WU2.js";
@@ -147,10 +147,10 @@ import {
147
147
  getVerdictKind,
148
148
  judgeFactDurability,
149
149
  validateProcedureExtraction
150
- } from "./chunk-2SGJY2UY.js";
150
+ } from "./chunk-Z3CCEP6F.js";
151
151
  import {
152
152
  ExtractionEngine
153
- } from "./chunk-WB3LYXC5.js";
153
+ } from "./chunk-MON3LMO7.js";
154
154
  import {
155
155
  parseMemoryActionEligibilityContext
156
156
  } from "./chunk-ROZJACKP.js";
@@ -168,7 +168,7 @@ import {
168
168
  buildEntityRecallSection,
169
169
  entityRecentTranscriptLookbackHours,
170
170
  readRecentEntityTranscriptEntries
171
- } from "./chunk-LANHQ7EN.js";
171
+ } from "./chunk-BNW5NJJH.js";
172
172
  import {
173
173
  buildEventOrderRecallSection,
174
174
  shouldRecallEventOrderEvidence
@@ -203,7 +203,7 @@ import {
203
203
  materializeAfterSemanticConsolidation,
204
204
  parseConsolidationResponse,
205
205
  parseOperatorAwareConsolidationResponse
206
- } from "./chunk-ZNCDQZIS.js";
206
+ } from "./chunk-QFQQFX2H.js";
207
207
  import {
208
208
  normalizeReplaySessionKey
209
209
  } from "./chunk-2PRQG7PV.js";
@@ -212,13 +212,13 @@ import {
212
212
  } from "./chunk-X6IRLNOO.js";
213
213
  import {
214
214
  searchVerifiedEpisodes
215
- } from "./chunk-CCNZM5UM.js";
215
+ } from "./chunk-R3OQGYOU.js";
216
216
  import {
217
217
  ThreadingManager
218
218
  } from "./chunk-W4RVMTHR.js";
219
219
  import {
220
220
  searchVerifiedSemanticRules
221
- } from "./chunk-YPNGPHNZ.js";
221
+ } from "./chunk-2UFQYU5F.js";
222
222
  import {
223
223
  searchWorkProductLedgerEntries
224
224
  } from "./chunk-ZRWB5D4H.js";
@@ -237,7 +237,7 @@ import {
237
237
  } from "./chunk-CYEPCZN5.js";
238
238
  import {
239
239
  NamespaceStorageRouter
240
- } from "./chunk-ZUNNG6PC.js";
240
+ } from "./chunk-W3BKVM64.js";
241
241
  import {
242
242
  namespaceIdentityFromToken
243
243
  } from "./chunk-ZFXCQPNO.js";
@@ -274,17 +274,17 @@ import {
274
274
  } from "./chunk-LMDRGRJ2.js";
275
275
  import {
276
276
  semanticChunkContent
277
- } from "./chunk-WCYKT2DE.js";
277
+ } from "./chunk-P4BC54KI.js";
278
278
  import {
279
279
  chunkContent
280
- } from "./chunk-4WMCPJWX.js";
280
+ } from "./chunk-UQ7RN5HK.js";
281
281
  import {
282
282
  SmartBuffer
283
283
  } from "./chunk-IBTZEBUD.js";
284
284
  import {
285
285
  FallbackLlmClient,
286
286
  fallbackLlmRuntimeContextFromConfig
287
- } from "./chunk-DEVUWMME.js";
287
+ } from "./chunk-KGIGRNR6.js";
288
288
  import {
289
289
  buildRecallQueryPolicy
290
290
  } from "./chunk-LBJBNWS2.js";
@@ -330,7 +330,7 @@ import {
330
330
  } from "./chunk-QDW3E4RD.js";
331
331
  import {
332
332
  shouldSkipImplicitExtraction
333
- } from "./chunk-IMA6GU4Y.js";
333
+ } from "./chunk-H3PHZLMF.js";
334
334
  import {
335
335
  GraphIndex
336
336
  } from "./chunk-XL7FK7PJ.js";
@@ -342,12 +342,12 @@ import {
342
342
  normalizeAttributePairs,
343
343
  normalizeEntityName,
344
344
  parseEntityFile
345
- } from "./chunk-DXBCNDVD.js";
345
+ } from "./chunk-PJGB7XRR.js";
346
346
  import {
347
347
  attachCitation,
348
348
  hasCitationForTemplate,
349
349
  stripCitationForTemplate
350
- } from "./chunk-4H5ZJHEN.js";
350
+ } from "./chunk-J6A3CX5N.js";
351
351
  import {
352
352
  confidenceTier
353
353
  } from "./chunk-4R4KTDIE.js";
@@ -1200,7 +1200,11 @@ function buildCompressionGuidelinesMarkdown2(events, generatedAtIso = (/* @__PUR
1200
1200
  function formatCompressionGuidelinesForRecall(raw, maxLines = 5) {
1201
1201
  if (typeof raw !== "string" || raw.trim().length === 0) return null;
1202
1202
  const sectionMatch = raw.match(
1203
- /## Suggested Guidelines\s*\n([\s\S]*?)(?:\n##\s+|\s*$)/i
1203
+ // End the section at `\n## ` or end-of-string. Plain $ (not \s*$): the
1204
+ // \s* branch overlapped the lazy body and backtracked polynomially
1205
+ // (CodeQL js/polynomial-redos). Captured lines are trimmed/filtered below,
1206
+ // so trailing whitespace handling is unchanged.
1207
+ /## Suggested Guidelines\s*\n([\s\S]*?)(?:\n##\s+|$)/i
1204
1208
  );
1205
1209
  if (!sectionMatch) return null;
1206
1210
  const lines = sectionMatch[1].split("\n").map((line) => line.trim()).filter((line) => line.startsWith("- ")).slice(0, Math.max(1, Math.floor(maxLines)));
@@ -12498,4 +12502,4 @@ export {
12498
12502
  resolvePersistedMemoryRelativePath,
12499
12503
  Orchestrator
12500
12504
  };
12501
- //# sourceMappingURL=chunk-EDP57PFC.js.map
12505
+ //# sourceMappingURL=chunk-RKW6QR7W.js.map