@graphorin/memory 0.5.0 → 0.6.1

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 (236) hide show
  1. package/CHANGELOG.md +102 -1
  2. package/README.md +54 -37
  3. package/dist/conflict/index.d.ts +3 -1
  4. package/dist/conflict/index.js +1 -1
  5. package/dist/conflict/locale-packs/en.d.ts +13 -0
  6. package/dist/conflict/locale-packs/en.d.ts.map +1 -0
  7. package/dist/conflict/locale-packs/en.js +1 -1
  8. package/dist/conflict/locale-packs/en.js.map +1 -1
  9. package/dist/conflict/pipeline.d.ts +2 -2
  10. package/dist/conflict/pipeline.js +3 -3
  11. package/dist/conflict/pipeline.js.map +1 -1
  12. package/dist/conflict/stages/helpers.js +1 -1
  13. package/dist/conflict/stages/helpers.js.map +1 -1
  14. package/dist/conflict/stages/stage1-exact-dedup.js.map +1 -1
  15. package/dist/conflict/stages/stage2-embedding-three-zone.js.map +1 -1
  16. package/dist/conflict/stages/stage3-heuristic-regex.js +1 -1
  17. package/dist/conflict/stages/stage3-heuristic-regex.js.map +1 -1
  18. package/dist/conflict/stages/stage4-subject-predicate.js.map +1 -1
  19. package/dist/conflict/stages/stage5-defer-to-deep.js.map +1 -1
  20. package/dist/conflict/types.d.ts +7 -7
  21. package/dist/conflict/types.js +1 -1
  22. package/dist/conflict/types.js.map +1 -1
  23. package/dist/consolidator/budget.js +16 -3
  24. package/dist/consolidator/budget.js.map +1 -1
  25. package/dist/consolidator/decay.d.ts +13 -2
  26. package/dist/consolidator/decay.js +17 -5
  27. package/dist/consolidator/decay.js.map +1 -1
  28. package/dist/consolidator/dlq.js +1 -1
  29. package/dist/consolidator/dlq.js.map +1 -1
  30. package/dist/consolidator/errors.js +2 -2
  31. package/dist/consolidator/errors.js.map +1 -1
  32. package/dist/consolidator/index.js +8 -6
  33. package/dist/consolidator/index.js.map +1 -1
  34. package/dist/consolidator/lock.js +1 -1
  35. package/dist/consolidator/lock.js.map +1 -1
  36. package/dist/consolidator/noise-filter.js.map +1 -1
  37. package/dist/consolidator/phases/deep.js +6 -2
  38. package/dist/consolidator/phases/deep.js.map +1 -1
  39. package/dist/consolidator/phases/induce.d.ts +10 -10
  40. package/dist/consolidator/phases/induce.js +6 -6
  41. package/dist/consolidator/phases/induce.js.map +1 -1
  42. package/dist/consolidator/phases/learned-context.js +129 -0
  43. package/dist/consolidator/phases/learned-context.js.map +1 -0
  44. package/dist/consolidator/phases/light.js +2 -1
  45. package/dist/consolidator/phases/light.js.map +1 -1
  46. package/dist/consolidator/phases/reflect.js +5 -4
  47. package/dist/consolidator/phases/reflect.js.map +1 -1
  48. package/dist/consolidator/phases/standard.js +37 -8
  49. package/dist/consolidator/phases/standard.js.map +1 -1
  50. package/dist/consolidator/reconcile.js +1 -1
  51. package/dist/consolidator/reconcile.js.map +1 -1
  52. package/dist/consolidator/runtime.d.ts +4 -4
  53. package/dist/consolidator/runtime.d.ts.map +1 -1
  54. package/dist/consolidator/runtime.js +73 -16
  55. package/dist/consolidator/runtime.js.map +1 -1
  56. package/dist/consolidator/scheduler.d.ts +5 -5
  57. package/dist/consolidator/scheduler.js +1 -1
  58. package/dist/consolidator/scheduler.js.map +1 -1
  59. package/dist/consolidator/triggers.js +7 -7
  60. package/dist/consolidator/triggers.js.map +1 -1
  61. package/dist/consolidator/types.d.ts +68 -20
  62. package/dist/consolidator/types.d.ts.map +1 -1
  63. package/dist/consolidator/types.js +16 -6
  64. package/dist/consolidator/types.js.map +1 -1
  65. package/dist/context-engine/annotations.d.ts +9 -9
  66. package/dist/context-engine/annotations.js +3 -3
  67. package/dist/context-engine/annotations.js.map +1 -1
  68. package/dist/context-engine/auto-recall.js.map +1 -1
  69. package/dist/context-engine/compaction/clear-tool-results.d.ts +18 -3
  70. package/dist/context-engine/compaction/clear-tool-results.d.ts.map +1 -1
  71. package/dist/context-engine/compaction/clear-tool-results.js +26 -5
  72. package/dist/context-engine/compaction/clear-tool-results.js.map +1 -1
  73. package/dist/context-engine/compaction/compactor.js +19 -6
  74. package/dist/context-engine/compaction/compactor.js.map +1 -1
  75. package/dist/context-engine/compaction/hooks/reanchor-persona-block.js +4 -0
  76. package/dist/context-engine/compaction/hooks/reanchor-persona-block.js.map +1 -1
  77. package/dist/context-engine/compaction/hooks/reanchor-pinned-facts.js +1 -0
  78. package/dist/context-engine/compaction/hooks/reanchor-pinned-facts.js.map +1 -1
  79. package/dist/context-engine/compaction/hooks/reanchor-project-rules.js +2 -1
  80. package/dist/context-engine/compaction/hooks/reanchor-project-rules.js.map +1 -1
  81. package/dist/context-engine/compaction/hooks/reanchor-recent-results.d.ts +28 -0
  82. package/dist/context-engine/compaction/hooks/reanchor-recent-results.d.ts.map +1 -0
  83. package/dist/context-engine/compaction/hooks/reanchor-recent-results.js +71 -0
  84. package/dist/context-engine/compaction/hooks/reanchor-recent-results.js.map +1 -0
  85. package/dist/context-engine/compaction/hooks/types.d.ts +15 -3
  86. package/dist/context-engine/compaction/hooks/types.d.ts.map +1 -1
  87. package/dist/context-engine/compaction/index.d.ts +1 -0
  88. package/dist/context-engine/compaction/index.js +2 -1
  89. package/dist/context-engine/compaction/templates/summary-9-section.d.ts +10 -3
  90. package/dist/context-engine/compaction/templates/summary-9-section.d.ts.map +1 -1
  91. package/dist/context-engine/compaction/templates/summary-9-section.js +42 -5
  92. package/dist/context-engine/compaction/templates/summary-9-section.js.map +1 -1
  93. package/dist/context-engine/compaction/thresholds.js.map +1 -1
  94. package/dist/context-engine/compaction/types.d.ts +47 -7
  95. package/dist/context-engine/compaction/types.d.ts.map +1 -1
  96. package/dist/context-engine/engine.d.ts +23 -4
  97. package/dist/context-engine/engine.d.ts.map +1 -1
  98. package/dist/context-engine/engine.js +69 -15
  99. package/dist/context-engine/engine.js.map +1 -1
  100. package/dist/context-engine/index.d.ts +1 -0
  101. package/dist/context-engine/index.d.ts.map +1 -1
  102. package/dist/context-engine/index.js +2 -1
  103. package/dist/context-engine/locale-packs/en.js +14 -13
  104. package/dist/context-engine/locale-packs/en.js.map +1 -1
  105. package/dist/context-engine/locale-packs/types.d.ts +10 -10
  106. package/dist/context-engine/locale-packs/types.d.ts.map +1 -1
  107. package/dist/context-engine/locale-packs/types.js.map +1 -1
  108. package/dist/context-engine/metadata.d.ts +1 -1
  109. package/dist/context-engine/metadata.js +2 -2
  110. package/dist/context-engine/metadata.js.map +1 -1
  111. package/dist/context-engine/preambles/inbound-en.d.ts +1 -1
  112. package/dist/context-engine/preambles/inbound-en.js +1 -1
  113. package/dist/context-engine/preambles/inbound-en.js.map +1 -1
  114. package/dist/context-engine/privacy-filter.d.ts +2 -2
  115. package/dist/context-engine/privacy-filter.js +1 -1
  116. package/dist/context-engine/privacy-filter.js.map +1 -1
  117. package/dist/context-engine/templates/base-en.d.ts +1 -1
  118. package/dist/context-engine/templates/base-en.js +1 -1
  119. package/dist/context-engine/templates/base-en.js.map +1 -1
  120. package/dist/context-engine/templates/composer.d.ts +1 -1
  121. package/dist/context-engine/templates/composer.js.map +1 -1
  122. package/dist/context-engine/token-budget.d.ts +1 -1
  123. package/dist/context-engine/token-budget.js +1 -1
  124. package/dist/context-engine/token-budget.js.map +1 -1
  125. package/dist/context-engine/token-counter.d.ts +16 -7
  126. package/dist/context-engine/token-counter.d.ts.map +1 -1
  127. package/dist/context-engine/token-counter.js +31 -9
  128. package/dist/context-engine/token-counter.js.map +1 -1
  129. package/dist/errors/index.d.ts +2 -2
  130. package/dist/errors/index.js +4 -4
  131. package/dist/errors/index.js.map +1 -1
  132. package/dist/facade.d.ts +44 -15
  133. package/dist/facade.d.ts.map +1 -1
  134. package/dist/facade.js +16 -8
  135. package/dist/facade.js.map +1 -1
  136. package/dist/graph/entity-resolver.d.ts +2 -2
  137. package/dist/graph/entity-resolver.js +2 -2
  138. package/dist/graph/entity-resolver.js.map +1 -1
  139. package/dist/index.d.ts +9 -6
  140. package/dist/index.d.ts.map +1 -1
  141. package/dist/index.js +11 -5
  142. package/dist/index.js.map +1 -1
  143. package/dist/internal/contextualize.js +2 -2
  144. package/dist/internal/contextualize.js.map +1 -1
  145. package/dist/internal/embedder-binding.js +1 -1
  146. package/dist/internal/embedder-binding.js.map +1 -1
  147. package/dist/internal/id.js +1 -1
  148. package/dist/internal/id.js.map +1 -1
  149. package/dist/internal/injection-heuristics.js +25 -2
  150. package/dist/internal/injection-heuristics.js.map +1 -1
  151. package/dist/internal/spans.js +1 -1
  152. package/dist/internal/spans.js.map +1 -1
  153. package/dist/internal/storage-adapter.d.ts +71 -22
  154. package/dist/internal/storage-adapter.d.ts.map +1 -1
  155. package/dist/migration/embedder-migration.d.ts +7 -7
  156. package/dist/migration/embedder-migration.js.map +1 -1
  157. package/dist/package.js +6 -0
  158. package/dist/package.js.map +1 -0
  159. package/dist/search/explain.d.ts +7 -4
  160. package/dist/search/explain.d.ts.map +1 -1
  161. package/dist/search/explain.js +1 -1
  162. package/dist/search/explain.js.map +1 -1
  163. package/dist/search/fit-weights.d.ts +48 -0
  164. package/dist/search/fit-weights.d.ts.map +1 -0
  165. package/dist/search/fit-weights.js +70 -0
  166. package/dist/search/fit-weights.js.map +1 -0
  167. package/dist/search/graph-ppr.d.ts +36 -0
  168. package/dist/search/graph-ppr.d.ts.map +1 -0
  169. package/dist/search/graph-ppr.js +38 -0
  170. package/dist/search/graph-ppr.js.map +1 -0
  171. package/dist/search/index.d.ts +4 -1
  172. package/dist/search/index.js +4 -1
  173. package/dist/search/iterative.d.ts +15 -8
  174. package/dist/search/iterative.d.ts.map +1 -1
  175. package/dist/search/iterative.js +10 -9
  176. package/dist/search/iterative.js.map +1 -1
  177. package/dist/search/query-transform.d.ts +6 -6
  178. package/dist/search/query-transform.js +6 -6
  179. package/dist/search/query-transform.js.map +1 -1
  180. package/dist/search/rrf.d.ts +4 -4
  181. package/dist/search/rrf.js +4 -4
  182. package/dist/search/rrf.js.map +1 -1
  183. package/dist/search/trust.d.ts +30 -0
  184. package/dist/search/trust.d.ts.map +1 -0
  185. package/dist/search/trust.js +50 -0
  186. package/dist/search/trust.js.map +1 -0
  187. package/dist/search/types.d.ts +1 -1
  188. package/dist/tiers/episodic-memory.d.ts +6 -4
  189. package/dist/tiers/episodic-memory.d.ts.map +1 -1
  190. package/dist/tiers/episodic-memory.js +3 -2
  191. package/dist/tiers/episodic-memory.js.map +1 -1
  192. package/dist/tiers/index.d.ts +1 -1
  193. package/dist/tiers/index.js +1 -1
  194. package/dist/tiers/insight-memory.d.ts +4 -4
  195. package/dist/tiers/insight-memory.js +3 -3
  196. package/dist/tiers/insight-memory.js.map +1 -1
  197. package/dist/tiers/procedural-memory.d.ts +29 -7
  198. package/dist/tiers/procedural-memory.d.ts.map +1 -1
  199. package/dist/tiers/procedural-memory.js +66 -6
  200. package/dist/tiers/procedural-memory.js.map +1 -1
  201. package/dist/tiers/semantic-memory.d.ts +111 -30
  202. package/dist/tiers/semantic-memory.d.ts.map +1 -1
  203. package/dist/tiers/semantic-memory.js +120 -27
  204. package/dist/tiers/semantic-memory.js.map +1 -1
  205. package/dist/tiers/session-memory.d.ts +7 -7
  206. package/dist/tiers/session-memory.js +7 -7
  207. package/dist/tiers/session-memory.js.map +1 -1
  208. package/dist/tiers/shared-memory.d.ts +1 -1
  209. package/dist/tiers/shared-memory.js +1 -1
  210. package/dist/tiers/shared-memory.js.map +1 -1
  211. package/dist/tiers/working-memory.d.ts +3 -3
  212. package/dist/tiers/working-memory.js +2 -2
  213. package/dist/tiers/working-memory.js.map +1 -1
  214. package/dist/tools/block-tools.d.ts +3 -3
  215. package/dist/tools/block-tools.js +3 -3
  216. package/dist/tools/block-tools.js.map +1 -1
  217. package/dist/tools/fact-tools.d.ts +48 -31
  218. package/dist/tools/fact-tools.d.ts.map +1 -1
  219. package/dist/tools/fact-tools.js +30 -18
  220. package/dist/tools/fact-tools.js.map +1 -1
  221. package/dist/tools/index.d.ts +9 -2
  222. package/dist/tools/index.d.ts.map +1 -1
  223. package/dist/tools/index.js +4 -2
  224. package/dist/tools/index.js.map +1 -1
  225. package/dist/tools/recall-tools.d.ts +40 -20
  226. package/dist/tools/recall-tools.d.ts.map +1 -1
  227. package/dist/tools/recall-tools.js +29 -10
  228. package/dist/tools/recall-tools.js.map +1 -1
  229. package/dist/tools/runbook-tools.d.ts +76 -0
  230. package/dist/tools/runbook-tools.d.ts.map +1 -0
  231. package/dist/tools/runbook-tools.js +53 -0
  232. package/dist/tools/runbook-tools.js.map +1 -0
  233. package/dist/tools/taint.js +33 -0
  234. package/dist/tools/taint.js.map +1 -0
  235. package/dist/tools/types.d.ts +1 -1
  236. package/package.json +8 -8
@@ -0,0 +1,129 @@
1
+ import { withMemorySpan } from "../../internal/spans.js";
2
+ import { defineBlock } from "../../tiers/working-memory.js";
3
+
4
+ //#region src/consolidator/phases/learned-context.ts
5
+ /** Reserved working-block label maintained by the pass. */
6
+ const LEARNED_CONTEXT_BLOCK_LABEL = "learned_context";
7
+ /** How many recent episode summaries feed one rewrite. */
8
+ const EPISODE_CONTEXT_LIMIT = 15;
9
+ /** How many active insights / procedures feed one rewrite. */
10
+ const INSIGHT_CONTEXT_LIMIT = 10;
11
+ const PROCEDURE_CONTEXT_LIMIT = 8;
12
+ const LEARNED_CONTEXT_SYSTEM_PROMPT = [
13
+ "You maintain the learned-context block of a long-running personal-assistant memory:",
14
+ "a compact standing digest of what is durably known about the user and their active",
15
+ "work (profile, preferences, ongoing projects, validated insights, working procedures).",
16
+ "Rewrite the digest by folding the new evidence into the previous digest: keep what is",
17
+ "still true, drop what is stale or superseded, and never invent anything the evidence",
18
+ "does not support. Write plain prose or short dashed lines - no markdown headings, no",
19
+ "code fences, no preamble. Stay strictly under the character budget."
20
+ ].join(" ");
21
+ /**
22
+ * Strip an accidental leading code fence from the model's digest and
23
+ * clamp it to the character budget. Returns `null` for an effectively
24
+ * empty rewrite (the previous digest is then left untouched).
25
+ */
26
+ function normalizeLearnedContext(raw, maxChars) {
27
+ let text = raw.trim();
28
+ const fence = /^```[^\n]*\n([\s\S]*?)\n?```\s*$/.exec(text);
29
+ if (fence?.[1] !== void 0) text = fence[1].trim();
30
+ if (text.length === 0) return null;
31
+ return text.length > maxChars ? text.slice(0, maxChars).trimEnd() : text;
32
+ }
33
+ /** Build the single rewrite request (pure - testable offline). */
34
+ function buildLearnedContextRequest(args) {
35
+ const sections = [`Character budget: ${args.maxChars}.`, args.previous.length > 0 ? `Previous digest:\n${args.previous}` : "Previous digest: (none yet - write the first one)"];
36
+ if (args.episodes.length > 0) sections.push(`Recent episodes:\n${args.episodes.map((s) => `- ${s}`).join("\n")}`);
37
+ if (args.insights.length > 0) sections.push(`Validated insights:\n${args.insights.map((s) => `- ${s}`).join("\n")}`);
38
+ if (args.procedures.length > 0) sections.push(`Active procedures:\n${args.procedures.map((s) => `- ${s}`).join("\n")}`);
39
+ sections.push("Return ONLY the rewritten digest text.");
40
+ return {
41
+ messages: [{
42
+ role: "user",
43
+ content: sections.join("\n\n")
44
+ }],
45
+ systemMessage: LEARNED_CONTEXT_SYSTEM_PROMPT,
46
+ temperature: 0,
47
+ maxTokens: Math.max(256, Math.ceil(args.maxChars / 2))
48
+ };
49
+ }
50
+ /**
51
+ * Run the learned-context rewrite. Resilient by construction: a
52
+ * provider failure or empty rewrite leaves the existing block untouched
53
+ * and never throws; only storage errors propagate.
54
+ */
55
+ async function runLearnedContextPass(deps) {
56
+ return withMemorySpan(deps.tracer, "memory.consolidate.learned-context", deps.scope, { "consolidator.phase": "learned-context" }, async (span) => {
57
+ const skip = (reason, chars = 0) => {
58
+ span.setAttributes({ "consolidator.learned_context.skipped": reason });
59
+ return {
60
+ updated: false,
61
+ chars,
62
+ tokens: 0,
63
+ costUsd: 0,
64
+ skippedReason: reason
65
+ };
66
+ };
67
+ if (deps.budget.snapshot().paused) return skip("budget");
68
+ if (deps.working.definitionFor(LEARNED_CONTEXT_BLOCK_LABEL) === void 0) deps.working.define(defineBlock({
69
+ label: LEARNED_CONTEXT_BLOCK_LABEL,
70
+ description: "Standing digest of durable knowledge about the user and active work, maintained by the deep-phase learned-context pass.",
71
+ charLimit: deps.maxChars
72
+ }));
73
+ const previous = await deps.working.read(deps.scope, LEARNED_CONTEXT_BLOCK_LABEL) ?? "";
74
+ const episodes = deps.episodic === null ? [] : (await deps.episodic.listRecent(deps.scope, EPISODE_CONTEXT_LIMIT, { includeQuarantined: true })).map((episode) => episode.summary);
75
+ const insights = deps.store.insights === void 0 ? [] : (await deps.store.insights.list(deps.scope, { limit: INSIGHT_CONTEXT_LIMIT })).map((insight) => insight.text);
76
+ const procedures = (await deps.store.procedural.list(deps.scope)).filter((rule) => rule.status !== "quarantined").slice(0, PROCEDURE_CONTEXT_LIMIT).map((rule) => rule.text.split("\n")[0] ?? rule.text);
77
+ const evidenceCount = episodes.length + insights.length + procedures.length;
78
+ span.setAttributes({ "consolidator.learned_context.evidence": evidenceCount });
79
+ if (evidenceCount === 0) return skip("no-evidence", previous.length);
80
+ let tokens = 0;
81
+ let costUsd = 0;
82
+ try {
83
+ const res = await deps.provider.generate(buildLearnedContextRequest({
84
+ previous,
85
+ episodes,
86
+ insights,
87
+ procedures,
88
+ maxChars: deps.maxChars
89
+ }));
90
+ tokens = (res.usage.promptTokens ?? 0) + (res.usage.completionTokens ?? 0) + (res.usage.reasoningTokens ?? 0);
91
+ costUsd = deps.priceUsage?.({
92
+ promptTokens: res.usage.promptTokens ?? 0,
93
+ completionTokens: res.usage.completionTokens ?? 0
94
+ }) ?? 0;
95
+ deps.budget.record({
96
+ phase: "deep",
97
+ tokens,
98
+ costUsd
99
+ });
100
+ const next = normalizeLearnedContext(res.text ?? "", deps.maxChars);
101
+ if (next === null) return {
102
+ ...skip("empty-rewrite", previous.length),
103
+ tokens,
104
+ costUsd
105
+ };
106
+ await deps.working.write(deps.scope, LEARNED_CONTEXT_BLOCK_LABEL, next);
107
+ span.setAttributes({
108
+ "consolidator.learned_context.updated": true,
109
+ "consolidator.learned_context.chars": next.length
110
+ });
111
+ return {
112
+ updated: true,
113
+ chars: next.length,
114
+ tokens,
115
+ costUsd
116
+ };
117
+ } catch {
118
+ return {
119
+ ...skip("provider-error", previous.length),
120
+ tokens,
121
+ costUsd
122
+ };
123
+ }
124
+ });
125
+ }
126
+
127
+ //#endregion
128
+ export { runLearnedContextPass };
129
+ //# sourceMappingURL=learned-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"learned-context.js","names":["sections: string[]"],"sources":["../../../src/consolidator/phases/learned-context.ts"],"sourcesContent":["/**\n * Learned-context pass (D3) - Letta-style sleep-time maintenance of a\n * persistent, size-bounded, agent-editable digest block. The deep phase\n * rewrites one reserved working-memory block (`learned_context`) from\n * the previous digest + fresh evidence (recent episodes, active\n * insights, active procedures), so every subsequent step's system\n * prompt carries a compact \"what I have learned about this user /\n * project\" summary inside the stable KV-cache prefix - without\n * retrieval latency and without unbounded growth.\n *\n * The block itself is an ordinary working block: it is spliced into\n * layer 3 of the assembled context automatically, re-anchored after\n * compaction by the persona-block hook pattern, and remains editable by\n * the agent through the existing `block_*` tools (the pass rewrites it\n * on its own cadence, folding any agent edits into the next rewrite).\n *\n * Runs after the deep-phase reflection pass, reusing the same budget /\n * lock / run-audit envelope. Gated off by default at every tier\n * (`learnedContext: false`) - Wave-D trial discipline.\n *\n * @packageDocumentation\n */\n\nimport type { Provider, ProviderRequest, SessionScope, Tracer } from '@graphorin/core';\nimport { withMemorySpan } from '../../internal/spans.js';\nimport type { MemoryStoreAdapter } from '../../internal/storage-adapter.js';\nimport type { EpisodicMemory } from '../../tiers/episodic-memory.js';\nimport { defineBlock, type WorkingMemory } from '../../tiers/working-memory.js';\nimport type { BudgetTracker } from '../budget.js';\n\n/** Reserved working-block label maintained by the pass. */\nexport const LEARNED_CONTEXT_BLOCK_LABEL = 'learned_context';\n\n/** Default character bound for the digest block. */\nexport const DEFAULT_LEARNED_CONTEXT_MAX_CHARS = 1200;\n\n/** How many recent episode summaries feed one rewrite. */\nconst EPISODE_CONTEXT_LIMIT = 15;\n\n/** How many active insights / procedures feed one rewrite. */\nconst INSIGHT_CONTEXT_LIMIT = 10;\nconst PROCEDURE_CONTEXT_LIMIT = 8;\n\nconst LEARNED_CONTEXT_SYSTEM_PROMPT = [\n 'You maintain the learned-context block of a long-running personal-assistant memory:',\n 'a compact standing digest of what is durably known about the user and their active',\n 'work (profile, preferences, ongoing projects, validated insights, working procedures).',\n 'Rewrite the digest by folding the new evidence into the previous digest: keep what is',\n 'still true, drop what is stale or superseded, and never invent anything the evidence',\n 'does not support. Write plain prose or short dashed lines - no markdown headings, no',\n 'code fences, no preamble. Stay strictly under the character budget.',\n].join(' ');\n\n/** Inputs accepted by {@link runLearnedContextPass}. */\nexport interface LearnedContextDeps {\n readonly provider: Provider;\n readonly tracer: Tracer;\n readonly scope: SessionScope;\n /** Working tier that owns the digest block. */\n readonly working: WorkingMemory;\n /** Recent-episode evidence source. `null` ⇒ episodes are skipped. */\n readonly episodic: EpisodicMemory | null;\n /** Storage adapter - supplies active insights + procedures as evidence. */\n readonly store: MemoryStoreAdapter;\n readonly budget: BudgetTracker;\n /** Character bound enforced on the stored digest. */\n readonly maxChars: number;\n readonly priceUsage?: (usage: { promptTokens: number; completionTokens: number }) => number;\n /** Override the wall clock - used by tests. */\n readonly now?: () => number;\n}\n\n/** Summary returned by {@link runLearnedContextPass}. */\nexport interface LearnedContextOutcome {\n /** True when the block was rewritten this pass. */\n readonly updated: boolean;\n /** Stored digest length after the pass (0 when skipped and absent). */\n readonly chars: number;\n readonly tokens: number;\n readonly costUsd: number;\n /** Why the pass made no LLM call, when it did not. */\n readonly skippedReason?: 'budget' | 'no-evidence' | 'empty-rewrite' | 'provider-error';\n}\n\n/**\n * Strip an accidental leading code fence from the model's digest and\n * clamp it to the character budget. Returns `null` for an effectively\n * empty rewrite (the previous digest is then left untouched).\n */\nexport function normalizeLearnedContext(raw: string, maxChars: number): string | null {\n let text = raw.trim();\n const fence = /^```[^\\n]*\\n([\\s\\S]*?)\\n?```\\s*$/.exec(text);\n if (fence?.[1] !== undefined) text = fence[1].trim();\n if (text.length === 0) return null;\n return text.length > maxChars ? text.slice(0, maxChars).trimEnd() : text;\n}\n\n/** Build the single rewrite request (pure - testable offline). */\nexport function buildLearnedContextRequest(args: {\n readonly previous: string;\n readonly episodes: ReadonlyArray<string>;\n readonly insights: ReadonlyArray<string>;\n readonly procedures: ReadonlyArray<string>;\n readonly maxChars: number;\n}): ProviderRequest {\n const sections: string[] = [\n `Character budget: ${args.maxChars}.`,\n args.previous.length > 0\n ? `Previous digest:\\n${args.previous}`\n : 'Previous digest: (none yet - write the first one)',\n ];\n if (args.episodes.length > 0) {\n sections.push(`Recent episodes:\\n${args.episodes.map((s) => `- ${s}`).join('\\n')}`);\n }\n if (args.insights.length > 0) {\n sections.push(`Validated insights:\\n${args.insights.map((s) => `- ${s}`).join('\\n')}`);\n }\n if (args.procedures.length > 0) {\n sections.push(`Active procedures:\\n${args.procedures.map((s) => `- ${s}`).join('\\n')}`);\n }\n sections.push('Return ONLY the rewritten digest text.');\n return {\n messages: [{ role: 'user', content: sections.join('\\n\\n') }],\n systemMessage: LEARNED_CONTEXT_SYSTEM_PROMPT,\n temperature: 0,\n maxTokens: Math.max(256, Math.ceil(args.maxChars / 2)),\n };\n}\n\n/**\n * Run the learned-context rewrite. Resilient by construction: a\n * provider failure or empty rewrite leaves the existing block untouched\n * and never throws; only storage errors propagate.\n */\nexport async function runLearnedContextPass(\n deps: LearnedContextDeps,\n): Promise<LearnedContextOutcome> {\n return withMemorySpan(\n deps.tracer,\n 'memory.consolidate.learned-context',\n deps.scope,\n { 'consolidator.phase': 'learned-context' },\n async (span) => {\n const skip = (\n reason: NonNullable<LearnedContextOutcome['skippedReason']>,\n chars = 0,\n ): LearnedContextOutcome => {\n span.setAttributes({ 'consolidator.learned_context.skipped': reason });\n return { updated: false, chars, tokens: 0, costUsd: 0, skippedReason: reason };\n };\n if (deps.budget.snapshot().paused) return skip('budget');\n\n // Ensure the reserved block is registered so write() can create it.\n if (deps.working.definitionFor(LEARNED_CONTEXT_BLOCK_LABEL) === undefined) {\n deps.working.define(\n defineBlock({\n label: LEARNED_CONTEXT_BLOCK_LABEL,\n description:\n 'Standing digest of durable knowledge about the user and active work, ' +\n 'maintained by the deep-phase learned-context pass.',\n charLimit: deps.maxChars,\n }),\n );\n }\n const previous = (await deps.working.read(deps.scope, LEARNED_CONTEXT_BLOCK_LABEL)) ?? '';\n\n // Evidence: recent episodes (incl. quarantined auto-formed ones -\n // they carry the importance signal), ACTIVE insights only (the\n // digest drives behaviour, so quarantined synthesis stays out),\n // and active procedures.\n const episodes =\n deps.episodic === null\n ? []\n : (\n await deps.episodic.listRecent(deps.scope, EPISODE_CONTEXT_LIMIT, {\n includeQuarantined: true,\n })\n ).map((episode) => episode.summary);\n const insights =\n deps.store.insights === undefined\n ? []\n : (await deps.store.insights.list(deps.scope, { limit: INSIGHT_CONTEXT_LIMIT })).map(\n (insight) => insight.text,\n );\n const procedures = (await deps.store.procedural.list(deps.scope))\n .filter((rule) => rule.status !== 'quarantined')\n .slice(0, PROCEDURE_CONTEXT_LIMIT)\n .map((rule) => rule.text.split('\\n')[0] ?? rule.text);\n\n const evidenceCount = episodes.length + insights.length + procedures.length;\n span.setAttributes({ 'consolidator.learned_context.evidence': evidenceCount });\n // Nothing new to fold and nothing written yet ⇒ no paid call.\n if (evidenceCount === 0) return skip('no-evidence', previous.length);\n\n let tokens = 0;\n let costUsd = 0;\n try {\n const res = await deps.provider.generate(\n buildLearnedContextRequest({\n previous,\n episodes,\n insights,\n procedures,\n maxChars: deps.maxChars,\n }),\n );\n tokens =\n (res.usage.promptTokens ?? 0) +\n (res.usage.completionTokens ?? 0) +\n (res.usage.reasoningTokens ?? 0);\n costUsd =\n deps.priceUsage?.({\n promptTokens: res.usage.promptTokens ?? 0,\n completionTokens: res.usage.completionTokens ?? 0,\n }) ?? 0;\n deps.budget.record({ phase: 'deep', tokens, costUsd });\n const next = normalizeLearnedContext(res.text ?? '', deps.maxChars);\n if (next === null) {\n return { ...skip('empty-rewrite', previous.length), tokens, costUsd };\n }\n await deps.working.write(deps.scope, LEARNED_CONTEXT_BLOCK_LABEL, next);\n span.setAttributes({\n 'consolidator.learned_context.updated': true,\n 'consolidator.learned_context.chars': next.length,\n });\n return { updated: true, chars: next.length, tokens, costUsd };\n } catch {\n // Resilient: a provider failure must never fail the deep phase.\n return { ...skip('provider-error', previous.length), tokens, costUsd };\n }\n },\n );\n}\n"],"mappings":";;;;;AA+BA,MAAa,8BAA8B;;AAM3C,MAAM,wBAAwB;;AAG9B,MAAM,wBAAwB;AAC9B,MAAM,0BAA0B;AAEhC,MAAM,gCAAgC;CACpC;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC,KAAK,IAAI;;;;;;AAsCX,SAAgB,wBAAwB,KAAa,UAAiC;CACpF,IAAI,OAAO,IAAI,MAAM;CACrB,MAAM,QAAQ,mCAAmC,KAAK,KAAK;AAC3D,KAAI,QAAQ,OAAO,OAAW,QAAO,MAAM,GAAG,MAAM;AACpD,KAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,QAAO,KAAK,SAAS,WAAW,KAAK,MAAM,GAAG,SAAS,CAAC,SAAS,GAAG;;;AAItE,SAAgB,2BAA2B,MAMvB;CAClB,MAAMA,WAAqB,CACzB,qBAAqB,KAAK,SAAS,IACnC,KAAK,SAAS,SAAS,IACnB,qBAAqB,KAAK,aAC1B,oDACL;AACD,KAAI,KAAK,SAAS,SAAS,EACzB,UAAS,KAAK,qBAAqB,KAAK,SAAS,KAAK,MAAM,KAAK,IAAI,CAAC,KAAK,KAAK,GAAG;AAErF,KAAI,KAAK,SAAS,SAAS,EACzB,UAAS,KAAK,wBAAwB,KAAK,SAAS,KAAK,MAAM,KAAK,IAAI,CAAC,KAAK,KAAK,GAAG;AAExF,KAAI,KAAK,WAAW,SAAS,EAC3B,UAAS,KAAK,uBAAuB,KAAK,WAAW,KAAK,MAAM,KAAK,IAAI,CAAC,KAAK,KAAK,GAAG;AAEzF,UAAS,KAAK,yCAAyC;AACvD,QAAO;EACL,UAAU,CAAC;GAAE,MAAM;GAAQ,S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,cAAc,EAAE,yCAAyC,eAAe,CAAC;AAE9E,MAAI,kBAAkB,EAAG,QAAO,KAAK,eAAe,SAAS,OAAO;EAEpE,IAAI,SAAS;EACb,IAAI,UAAU;AACd,MAAI;GACF,MAAM,MAAM,MAAM,KAAK,SAAS,SAC9B,2BAA2B;IACzB;IACA;IACA;IACA;IACA,UAAU,KAAK;IAChB,CAAC,CACH;AACD,aACG,IAAI,MAAM,gBAAgB,MAC1B,IAAI,MAAM,oBAAoB,MAC9B,IAAI,MAAM,mBAAmB;AAChC,aACE,KAAK,aAAa;IAChB,cAAc,IAAI,MAAM,gBAAgB;IACxC,kBAAkB,IAAI,MAAM,oBAAoB;IACjD,CAAC,IAAI;AACR,QAAK,OAAO,OAAO;IAAE,OAAO;IAAQ;IAAQ;IAAS,CAAC;GACtD,MAAM,OAAO,wBAAwB,IAAI,QAAQ,IAAI,KAAK,SAAS;AACnE,OAAI,SAAS,KACX,QAAO;IAAE,GAAG,KAAK,iBAAiB,SAAS,OAAO;IAAE;IAAQ;IAAS;AAEvE,SAAM,KAAK,QAAQ,MAAM,KAAK,OAAO,6BAA6B,KAAK;AACvE,QAAK,cAAc;IACjB,wCAAwC;IACxC,sCAAsC,KAAK;IAC5C,CAAC;AACF,UAAO;IAAE,SAAS;IAAM,OAAO,KAAK;IAAQ;IAAQ;IAAS;UACvD;AAEN,UAAO;IAAE,GAAG,KAAK,kBAAkB,SAAS,OAAO;IAAE;IAAQ;IAAS;;GAG3E"}
@@ -34,6 +34,7 @@ async function runLightPhase(deps) {
34
34
  importance: row.importance,
35
35
  quarantined: row.status === "quarantined",
36
36
  foreignProvenance: isForeignProvenance(row.provenance),
37
+ accessCount: row.accessCount ?? 0,
37
38
  weights: deps.salienceWeights
38
39
  });
39
40
  if (score < deps.decayArchiveThreshold) {
@@ -84,7 +85,7 @@ async function runLightPhase(deps) {
84
85
  });
85
86
  }
86
87
  /**
87
- * `true` for provenance that did not originate first-party (P1-4) used
88
+ * `true` for provenance that did not originate first-party (P1-4) - used
88
89
  * to apply the mild salience penalty in capacity eviction. First-party
89
90
  * is `null` (legacy / direct write), `'user'`, and `'extraction'` (the
90
91
  * consolidator distilling the user's own session); `'tool'`,
@@ -1 +1 @@
1
- {"version":3,"file":"light.js","names":["survivors: Array<{ id: string; salience: number }>"],"sources":["../../../src/consolidator/phases/light.ts"],"sourcesContent":["/**\n * Light phase no LLM. Decays every fact's retention curve and\n * archives facts whose salience has fallen below the configured\n * threshold (plus the X-1 capacity pass). Noise filtering happens in\n * the standard phase where the batch is actually consumed the old\n * advisory re-count here read the same unconsumed messages on every\n * pass for a counter nothing acted on (MCON-17).\n *\n * @packageDocumentation\n */\n\nimport type { SessionScope, Tracer } from '@graphorin/core';\nimport { withMemorySpan } from '../../internal/spans.js';\nimport type {\n ConsolidatorMemoryStoreExt,\n MemoryStoreAdapter,\n} from '../../internal/storage-adapter.js';\nimport { type SalienceWeights, salience, selectForCapacityEviction } from '../decay.js';\nimport type { NoiseFilterPreset } from '../noise-filter.js';\nimport type { PhaseOutcome } from '../types.js';\n\n/** Inputs accepted by {@link runLightPhase}. */\nexport interface LightPhaseDeps {\n readonly store: MemoryStoreAdapter;\n readonly consolidatorStore: ConsolidatorMemoryStoreExt | null;\n readonly tracer: Tracer;\n readonly scope: SessionScope;\n readonly now: () => number;\n readonly decayTauDays: number;\n readonly decayArchiveThreshold: number;\n /**\n * Capacity-bounded eviction (X-1). When non-null, the light phase\n * archives the lowest-salience live facts in the decay window down to\n * this many, in addition to the threshold archiving. `null` (the\n * default) leaves storage unbounded behaviour identical to pre-X-1.\n */\n readonly decayCapacity: number | null;\n /** Weights for the multi-signal salience score (X-1). */\n readonly salienceWeights: SalienceWeights;\n readonly noiseFilters: ReadonlyArray<NoiseFilterPreset>;\n readonly maxBatchSize: number;\n readonly lastProcessedMessageId: string | null;\n /** The active consolidator tier surfaced on the AISpan attribute. */\n readonly tier?: 'free' | 'cheap' | 'standard' | 'full' | 'custom';\n}\n\n/**\n * Run the light phase against the supplied scope.\n *\n * @stable\n */\nexport async function runLightPhase(deps: LightPhaseDeps): Promise<PhaseOutcome> {\n return withMemorySpan(\n deps.tracer,\n 'memory.consolidate.light',\n deps.scope,\n {\n 'consolidator.phase': 'light',\n 'consolidator.tier': deps.tier ?? 'free',\n },\n async (span) => {\n const startedAt = deps.now();\n let facts = 0;\n let archived = 0;\n let capacityEvicted = 0;\n if (typeof deps.store.semantic.listForDecay === 'function') {\n // When a capacity bound is set we widen the decay window past the\n // batch size so there is always headroom to trim the overflow; the\n // window is the LRU head (ordered by last-accessed ASC), so the\n // bound converges over repeated passes.\n const decayLimit =\n deps.decayCapacity !== null\n ? Math.max(deps.maxBatchSize, deps.decayCapacity + deps.maxBatchSize)\n : deps.maxBatchSize;\n const rows = await deps.store.semantic.listForDecay(deps.scope, decayLimit);\n facts = rows.length;\n const now = deps.now();\n // Bind once so the optional method stays narrowed across `await`\n // (and keeps its `this`); `undefined` ⇒ the store can't archive.\n const archiveFact = deps.store.semantic.archiveFact?.bind(deps.store.semantic);\n // Pass 1 multi-signal threshold archiving. Salience folds the\n // Ebbinghaus retention curve together with the P1-2 importance hint\n // and the P1-4 security-risk negative term, so a stale, low-value,\n // or quarantined fact crosses the threshold sooner. Neutral inputs\n // collapse salience to plain retention (pre-X-1 behaviour).\n const survivors: Array<{ id: string; salience: number }> = [];\n for (const row of rows) {\n if (row.archived) continue;\n const score = salience({\n now,\n lastAccessedAt: row.lastAccessedAt,\n createdAt: row.createdAt,\n strength: row.strength,\n tauDays: deps.decayTauDays,\n importance: row.importance,\n quarantined: row.status === 'quarantined',\n foreignProvenance: isForeignProvenance(row.provenance),\n weights: deps.salienceWeights,\n });\n if (score < deps.decayArchiveThreshold) {\n if (archiveFact !== undefined) {\n await archiveFact(row.id, 'low_salience');\n archived += 1;\n }\n continue;\n }\n survivors.push({ id: row.id, salience: score });\n }\n // Pass 2 capacity-bounded eviction. Archive the lowest-salience\n // survivors (security-flagged first) until the window fits.\n if (deps.decayCapacity !== null && archiveFact !== undefined) {\n const evictIds = selectForCapacityEviction(survivors, deps.decayCapacity);\n for (const id of evictIds) {\n await archiveFact(id, 'capacity_exceeded');\n archived += 1;\n capacityEvicted += 1;\n }\n }\n }\n\n // MCON-17: the light phase no longer re-reads the unconsumed batch\n // just to produce an advisory dropped-count the standard phase\n // filters where extraction actually consumes the messages.\n const noiseFilteredCount = 0;\n\n span.setAttributes({\n 'consolidator.duration_ms': Math.max(0, deps.now() - startedAt),\n 'consolidator.facts_extracted': 0,\n 'consolidator.budget_used_usd': 0,\n 'consolidator.light.facts_seen': facts,\n 'consolidator.light.facts_archived': archived,\n 'consolidator.light.capacity_evicted': capacityEvicted,\n 'consolidator.light.noise_filtered': noiseFilteredCount,\n });\n\n return {\n phase: 'light',\n status: 'completed',\n factsCreated: 0,\n factsUpdated: archived,\n conflictsResolved: 0,\n episodesFormed: 0,\n insightsCreated: 0,\n noiseFilteredCount,\n emptyExtractions: 0,\n llmTokensUsed: 0,\n llmCostUsd: null,\n errorMessage: null,\n };\n },\n );\n}\n\n/**\n * `true` for provenance that did not originate first-party (P1-4) used\n * to apply the mild salience penalty in capacity eviction. First-party\n * is `null` (legacy / direct write), `'user'`, and `'extraction'` (the\n * consolidator distilling the user's own session); `'tool'`,\n * `'imported'`, and `'reflection'` are treated as foreign.\n */\nfunction isForeignProvenance(provenance: string | null): boolean {\n return provenance !== null && provenance !== 'user' && provenance !== 'extraction';\n}\n"],"mappings":";;;;;;;;;AAmDA,eAAsB,cAAc,MAA6C;AAC/E,QAAO,eACL,KAAK,QACL,4BACA,KAAK,OACL;EACE,sBAAsB;EACtB,qBAAqB,KAAK,QAAQ;EACnC,EACD,OAAO,SAAS;EACd,MAAM,YAAY,KAAK,KAAK;EAC5B,IAAI,QAAQ;EACZ,IAAI,WAAW;EACf,IAAI,kBAAkB;AACtB,MAAI,OAAO,KAAK,MAAM,SAAS,iBAAiB,YAAY;GAK1D,MAAM,aACJ,KAAK,kBAAkB,OACnB,KAAK,IAAI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GACZ,cAAc;GACf;GAEJ;;;;;;;;;AAUH,SAAS,oBAAoB,YAAoC;AAC/D,QAAO,eAAe,QAAQ,eAAe,UAAU,eAAe"}
1
+ {"version":3,"file":"light.js","names":["survivors: Array<{ id: string; salience: number }>"],"sources":["../../../src/consolidator/phases/light.ts"],"sourcesContent":["/**\n * Light phase - no LLM. Decays every fact's retention curve and\n * archives facts whose salience has fallen below the configured\n * threshold (plus the X-1 capacity pass). Noise filtering happens in\n * the standard phase where the batch is actually consumed - the old\n * advisory re-count here read the same unconsumed messages on every\n * pass for a counter nothing acted on (MCON-17).\n *\n * @packageDocumentation\n */\n\nimport type { SessionScope, Tracer } from '@graphorin/core';\nimport { withMemorySpan } from '../../internal/spans.js';\nimport type {\n ConsolidatorMemoryStoreExt,\n MemoryStoreAdapter,\n} from '../../internal/storage-adapter.js';\nimport { type SalienceWeights, salience, selectForCapacityEviction } from '../decay.js';\nimport type { NoiseFilterPreset } from '../noise-filter.js';\nimport type { PhaseOutcome } from '../types.js';\n\n/** Inputs accepted by {@link runLightPhase}. */\nexport interface LightPhaseDeps {\n readonly store: MemoryStoreAdapter;\n readonly consolidatorStore: ConsolidatorMemoryStoreExt | null;\n readonly tracer: Tracer;\n readonly scope: SessionScope;\n readonly now: () => number;\n readonly decayTauDays: number;\n readonly decayArchiveThreshold: number;\n /**\n * Capacity-bounded eviction (X-1). When non-null, the light phase\n * archives the lowest-salience live facts in the decay window down to\n * this many, in addition to the threshold archiving. `null` (the\n * default) leaves storage unbounded - behaviour identical to pre-X-1.\n */\n readonly decayCapacity: number | null;\n /** Weights for the multi-signal salience score (X-1). */\n readonly salienceWeights: SalienceWeights;\n readonly noiseFilters: ReadonlyArray<NoiseFilterPreset>;\n readonly maxBatchSize: number;\n readonly lastProcessedMessageId: string | null;\n /** The active consolidator tier - surfaced on the AISpan attribute. */\n readonly tier?: 'free' | 'cheap' | 'standard' | 'full' | 'custom';\n}\n\n/**\n * Run the light phase against the supplied scope.\n *\n * @stable\n */\nexport async function runLightPhase(deps: LightPhaseDeps): Promise<PhaseOutcome> {\n return withMemorySpan(\n deps.tracer,\n 'memory.consolidate.light',\n deps.scope,\n {\n 'consolidator.phase': 'light',\n 'consolidator.tier': deps.tier ?? 'free',\n },\n async (span) => {\n const startedAt = deps.now();\n let facts = 0;\n let archived = 0;\n let capacityEvicted = 0;\n if (typeof deps.store.semantic.listForDecay === 'function') {\n // When a capacity bound is set we widen the decay window past the\n // batch size so there is always headroom to trim the overflow; the\n // window is the LRU head (ordered by last-accessed ASC), so the\n // bound converges over repeated passes.\n const decayLimit =\n deps.decayCapacity !== null\n ? Math.max(deps.maxBatchSize, deps.decayCapacity + deps.maxBatchSize)\n : deps.maxBatchSize;\n const rows = await deps.store.semantic.listForDecay(deps.scope, decayLimit);\n facts = rows.length;\n const now = deps.now();\n // Bind once so the optional method stays narrowed across `await`\n // (and keeps its `this`); `undefined` ⇒ the store can't archive.\n const archiveFact = deps.store.semantic.archiveFact?.bind(deps.store.semantic);\n // Pass 1 - multi-signal threshold archiving. Salience folds the\n // Ebbinghaus retention curve together with the P1-2 importance hint\n // and the P1-4 security-risk negative term, so a stale, low-value,\n // or quarantined fact crosses the threshold sooner. Neutral inputs\n // collapse salience to plain retention (pre-X-1 behaviour).\n const survivors: Array<{ id: string; salience: number }> = [];\n for (const row of rows) {\n if (row.archived) continue;\n const score = salience({\n now,\n lastAccessedAt: row.lastAccessedAt,\n createdAt: row.createdAt,\n strength: row.strength,\n tauDays: deps.decayTauDays,\n importance: row.importance,\n quarantined: row.status === 'quarantined',\n foreignProvenance: isForeignProvenance(row.provenance),\n // D3: retrieval-frequency reinforcement - inert at the\n // default weight 0; adapters without the column report 0.\n accessCount: row.accessCount ?? 0,\n weights: deps.salienceWeights,\n });\n if (score < deps.decayArchiveThreshold) {\n if (archiveFact !== undefined) {\n await archiveFact(row.id, 'low_salience');\n archived += 1;\n }\n continue;\n }\n survivors.push({ id: row.id, salience: score });\n }\n // Pass 2 - capacity-bounded eviction. Archive the lowest-salience\n // survivors (security-flagged first) until the window fits.\n if (deps.decayCapacity !== null && archiveFact !== undefined) {\n const evictIds = selectForCapacityEviction(survivors, deps.decayCapacity);\n for (const id of evictIds) {\n await archiveFact(id, 'capacity_exceeded');\n archived += 1;\n capacityEvicted += 1;\n }\n }\n }\n\n // MCON-17: the light phase no longer re-reads the unconsumed batch\n // just to produce an advisory dropped-count - the standard phase\n // filters where extraction actually consumes the messages.\n const noiseFilteredCount = 0;\n\n span.setAttributes({\n 'consolidator.duration_ms': Math.max(0, deps.now() - startedAt),\n 'consolidator.facts_extracted': 0,\n 'consolidator.budget_used_usd': 0,\n 'consolidator.light.facts_seen': facts,\n 'consolidator.light.facts_archived': archived,\n 'consolidator.light.capacity_evicted': capacityEvicted,\n 'consolidator.light.noise_filtered': noiseFilteredCount,\n });\n\n return {\n phase: 'light',\n status: 'completed',\n factsCreated: 0,\n factsUpdated: archived,\n conflictsResolved: 0,\n episodesFormed: 0,\n insightsCreated: 0,\n noiseFilteredCount,\n emptyExtractions: 0,\n llmTokensUsed: 0,\n llmCostUsd: null,\n errorMessage: null,\n };\n },\n );\n}\n\n/**\n * `true` for provenance that did not originate first-party (P1-4) - used\n * to apply the mild salience penalty in capacity eviction. First-party\n * is `null` (legacy / direct write), `'user'`, and `'extraction'` (the\n * consolidator distilling the user's own session); `'tool'`,\n * `'imported'`, and `'reflection'` are treated as foreign.\n */\nfunction isForeignProvenance(provenance: string | null): boolean {\n return provenance !== null && provenance !== 'user' && provenance !== 'extraction';\n}\n"],"mappings":";;;;;;;;;AAmDA,eAAsB,cAAc,MAA6C;AAC/E,QAAO,eACL,KAAK,QACL,4BACA,KAAK,OACL;EACE,sBAAsB;EACtB,qBAAqB,KAAK,QAAQ;EACnC,EACD,OAAO,SAAS;EACd,MAAM,YAAY,KAAK,KAAK;EAC5B,IAAI,QAAQ;EACZ,IAAI,WAAW;EACf,IAAI,kBAAkB;AACtB,MAAI,OAAO,KAAK,MAAM,SAAS,iBAAiB,Y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,mBAAmB;GACnB,gBAAgB;GAChB,iBAAiB;GACjB;GACA,kBAAkB;GAClB,eAAe;GACf,YAAY;GACZ,cAAc;GACf;GAEJ;;;;;;;;;AAUH,SAAS,oBAAoB,YAAoC;AAC/D,QAAO,eAAe,QAAQ,eAAe,UAAU,eAAe"}
@@ -20,7 +20,7 @@ const INSIGHT_SYSTEM_PROMPT = [
20
20
  "JSON: { \"insight\": string }."
21
21
  ].join(" ");
22
22
  /**
23
- * Run the reflection pass. Idempotent w.r.t. failures a parse miss or
23
+ * Run the reflection pass. Idempotent w.r.t. failures - a parse miss or
24
24
  * empty synthesis simply yields no insight. Never throws on model
25
25
  * output; only storage errors propagate.
26
26
  */
@@ -121,6 +121,7 @@ async function runReflectionPass(deps) {
121
121
  salience: STARTING_SALIENCE,
122
122
  provenance: "reflection",
123
123
  status: "quarantined",
124
+ owner: "agent",
124
125
  sensitivity: "internal",
125
126
  createdAt: iso,
126
127
  updatedAt: iso
@@ -155,7 +156,7 @@ async function runReflectionPass(deps) {
155
156
  });
156
157
  }
157
158
  /**
158
- * Normalise an insight for exact-dedup comparison lowercased, punctuation
159
+ * Normalise an insight for exact-dedup comparison - lowercased, punctuation
159
160
  * stripped, whitespace collapsed. Two insights that differ only in casing or
160
161
  * trailing punctuation collapse to the same key.
161
162
  *
@@ -219,7 +220,7 @@ function dedupeEvidence(items) {
219
220
  return out;
220
221
  }
221
222
  /**
222
- * Parse the salient-questions response accepts `{ questions: [...] }`
223
+ * Parse the salient-questions response - accepts `{ questions: [...] }`
223
224
  * or a bare `[...]`, tolerating chatty / fenced output. Returns the
224
225
  * trimmed, non-empty string questions (possibly `[]`).
225
226
  *
@@ -234,7 +235,7 @@ function parseQuestions(text) {
234
235
  return arr.filter((q) => typeof q === "string" && q.trim().length > 0).map((q) => q.trim());
235
236
  }
236
237
  /**
237
- * Parse the insight-synthesis response accepts `{ insight: string }`
238
+ * Parse the insight-synthesis response - accepts `{ insight: string }`
238
239
  * (or `{ text: string }`), tolerating chatty / fenced output. Returns
239
240
  * the trimmed insight text, or `null` when absent / empty (insufficient
240
241
  * evidence ⇒ no insight).
@@ -1 +1 @@
1
- {"version":3,"file":"reflect.js","names":["created: string[]","out: Evidence[]"],"sources":["../../../src/consolidator/phases/reflect.ts"],"sourcesContent":["/**\n * Reflection pass (P1-1) — the deep phase's higher-order synthesis\n * step, following the Generative-Agents recipe. When the accumulated\n * importance of recent episodes crosses a threshold, ask the model for\n * the few most salient questions, retrieve evidence for each, and\n * synthesize an **insight** the evidence supports — recorded\n * quarantined, `provenance: 'reflection'`, and **citing the retrieved\n * memory ids** (citations are set by the framework from the actual\n * evidence, never chosen by the model, so they can never be\n * hallucinated). Insights start at ExpeL salience `2`; the pass prunes\n * any that have decayed to `0`.\n *\n * Runs after the deep-phase conflict drain (wired in the runtime\n * dispatch), reusing the same budget / lock / run-audit envelope. The\n * conflict-resolution logic in `deep.ts` is untouched.\n *\n * @packageDocumentation\n */\n\nimport type { Provider, ProviderRequest, SessionScope, Tracer } from '@graphorin/core';\nimport { newMemoryId } from '../../internal/id.js';\nimport { withMemorySpan } from '../../internal/spans.js';\nimport type { InsightMemoryStoreExt } from '../../internal/storage-adapter.js';\nimport type { EpisodicMemory } from '../../tiers/episodic-memory.js';\nimport type { SemanticMemory } from '../../tiers/semantic-memory.js';\nimport type { BudgetTracker } from '../budget.js';\n\n/** ExpeL starting salience for a freshly-synthesized insight. */\nconst STARTING_SALIENCE = 2;\n\n/** How many recent episode summaries to show the salient-questions prompt. */\nconst RECENT_CONTEXT_LIMIT = 20;\n\n/** Inputs accepted by {@link runReflectionPass}. */\nexport interface ReflectionDeps {\n readonly provider: Provider;\n readonly tracer: Tracer;\n readonly scope: SessionScope;\n /** Tier used to retrieve fact evidence for each salient question. */\n readonly semantic: SemanticMemory;\n /** Importance source + episode-evidence retrieval. `null` ⇒ pass is a no-op. */\n readonly episodic: EpisodicMemory | null;\n /** Insight write + salience surface. */\n readonly insights: InsightMemoryStoreExt;\n readonly budget: BudgetTracker;\n /** Reflection fires only when accumulated episode importance ≥ this. */\n readonly importanceThreshold: number;\n /**\n * `ended_at` (epoch ms) of the newest episode a prior pass already reflected\n * on (MCON-13). The gate accumulates importance only from strictly-newer\n * episodes, so a deep run with no new episodes never re-fires. `null` /\n * absent ⇒ nothing reflected yet (accumulate over all recent episodes).\n */\n readonly reflectionWatermark?: number | null;\n /** Upper bound on salient questions asked per pass. */\n readonly maxQuestions: number;\n readonly priceUsage?: (usage: { promptTokens: number; completionTokens: number }) => number;\n /** Override the wall clock — used by tests. */\n readonly now?: () => number;\n}\n\n/** Summary returned by {@link runReflectionPass}. */\nexport interface ReflectionOutcome {\n /** True when accumulated importance crossed the threshold and the pass ran. */\n readonly triggered: boolean;\n readonly accumulatedImportance: number;\n readonly questionsAsked: number;\n readonly insightsCreated: number;\n readonly tokens: number;\n readonly costUsd: number;\n /**\n * Watermark to persist for the next pass (MCON-13): the `ended_at` (epoch\n * ms) of the newest episode this pass reflected on, or the incoming\n * watermark unchanged when the pass did not fire. The runtime persists it\n * to `consolidator_state`.\n */\n readonly nextWatermark: number | null;\n}\n\nconst QUESTIONS_SYSTEM_PROMPT = [\n 'You are the reflection step (salient-questions) of a long-running personal-assistant memory.',\n 'Given a list of recent memories, name the few most salient, high-level questions whose',\n 'answers would most improve understanding of the user. Do NOT answer them; ask only what the',\n 'memories collectively raise. Return strictly JSON: { \"questions\": string[] }.',\n].join(' ');\n\nconst INSIGHT_SYSTEM_PROMPT = [\n 'You are the reflection step (insight-synthesis) of a long-running personal-assistant memory.',\n 'Given a salient question and the supporting evidence, synthesize ONE concise, higher-order',\n 'insight that the evidence collectively supports. Ground it ONLY in the supplied evidence; if',\n 'the evidence is insufficient, return an empty string. Do NOT invent facts. Return strictly',\n 'JSON: { \"insight\": string }.',\n].join(' ');\n\ninterface Evidence {\n readonly id: string;\n readonly text: string;\n}\n\n/**\n * Run the reflection pass. Idempotent w.r.t. failures — a parse miss or\n * empty synthesis simply yields no insight. Never throws on model\n * output; only storage errors propagate.\n */\nexport async function runReflectionPass(deps: ReflectionDeps): Promise<ReflectionOutcome> {\n return withMemorySpan(\n deps.tracer,\n 'memory.consolidate.reflect',\n deps.scope,\n { 'consolidator.phase': 'reflect' },\n async (span) => {\n const empty = (over: Partial<ReflectionOutcome> = {}): ReflectionOutcome => ({\n triggered: false,\n accumulatedImportance: 0,\n questionsAsked: 0,\n insightsCreated: 0,\n tokens: 0,\n costUsd: 0,\n nextWatermark: deps.reflectionWatermark ?? null,\n ...over,\n });\n\n if (deps.episodic === null) {\n span.setAttributes({ 'consolidator.reflect.skipped': 'no-episodic' });\n return empty();\n }\n if (deps.budget.snapshot().paused) {\n span.setAttributes({ 'consolidator.reflect.skipped': 'budget' });\n return empty();\n }\n\n // 1. Accumulated-importance gate. Recency, not relevance: list the most\n // recent episodes (MCON-1 — the old `'*'` FTS probe matched zero rows\n // on real SQLite). Auto-formed episodes (P1-2) land\n // quarantined, so importance accrues there — include them. Only count\n // episodes newer than the watermark so a pass with no new episodes\n // never re-fires the paid synthesis (MCON-13).\n const recentEpisodes = await deps.episodic.listRecent(deps.scope, 50, {\n includeQuarantined: true,\n });\n const watermark = deps.reflectionWatermark ?? null;\n const fresh =\n watermark === null\n ? recentEpisodes\n : recentEpisodes.filter((e) => Date.parse(e.endedAt) > watermark);\n const accumulated = fresh.reduce((sum, episode) => sum + (episode.importance ?? 0), 0);\n span.setAttributes({\n 'consolidator.reflect.accumulated_importance': accumulated,\n 'consolidator.reflect.threshold': deps.importanceThreshold,\n });\n if (accumulated < deps.importanceThreshold) {\n span.setAttributes({ 'consolidator.reflect.triggered': 0 });\n return empty({ accumulatedImportance: accumulated });\n }\n // The newest episode this pass will reflect on — persisted as the next\n // watermark so subsequent passes skip everything up to here.\n const advancedWatermark = fresh.reduce(\n (max, episode) => Math.max(max, Date.parse(episode.endedAt)),\n watermark ?? Number.NEGATIVE_INFINITY,\n );\n\n let tokens = 0;\n let costUsd = 0;\n const record = (usage: {\n promptTokens?: number;\n completionTokens?: number;\n reasoningTokens?: number;\n }): void => {\n const t =\n (usage.promptTokens ?? 0) + (usage.completionTokens ?? 0) + (usage.reasoningTokens ?? 0);\n const c =\n deps.priceUsage?.({\n promptTokens: usage.promptTokens ?? 0,\n completionTokens: usage.completionTokens ?? 0,\n }) ?? 0;\n tokens += t;\n costUsd += c;\n deps.budget.record({ phase: 'deep', tokens: t, costUsd: c });\n };\n\n // 2. Salient questions over the new (post-watermark) memories.\n const recentSummaries = fresh\n .slice(0, RECENT_CONTEXT_LIMIT)\n .map((episode) => `- ${episode.summary}`)\n .join('\\n');\n const questionsRes = await deps.provider.generate(\n buildQuestionsRequest(deps, recentSummaries),\n );\n record(questionsRes.usage);\n const questions = parseQuestions(questionsRes.text).slice(0, Math.max(0, deps.maxQuestions));\n span.setAttributes({ 'consolidator.reflect.questions': questions.length });\n\n // Pre-filter new insights against already-stored ones so a re-fire over\n // overlapping evidence does not write near-duplicates (MCON-13). Offline\n // and embedding-free: normalised exact match, the insight analogue of\n // the conflict pipeline's stage-1 dedup.\n const seenInsights = new Set(\n (await deps.insights.list(deps.scope, { includeQuarantined: true, limit: 200 })).map((i) =>\n normalizeInsightText(i.text),\n ),\n );\n\n const nowFn = typeof deps.now === 'function' ? deps.now : Date.now;\n let insightsCreated = 0;\n const created: string[] = [];\n for (const question of questions) {\n if (deps.budget.snapshot().paused) break;\n\n // 3. Retrieve evidence. Facts are trustworthy (active only);\n // episodes include the quarantined auto-formed ones.\n const factHits = await deps.semantic.search(deps.scope, question, { topK: 5 });\n const epHits = await deps.episodic.search(deps.scope, question, {\n topK: 5,\n includeQuarantined: true,\n });\n const evidence = dedupeEvidence([\n ...factHits.map((h) => ({ id: h.record.id, text: h.record.text })),\n ...epHits.map((h) => ({ id: h.record.id, text: h.record.summary })),\n ]);\n // Citations are mandatory — no evidence ⇒ no insight.\n if (evidence.length === 0) continue;\n\n // 4. Synthesize an insight grounded in that evidence.\n if (deps.budget.snapshot().paused) break;\n const insightRes = await deps.provider.generate(\n buildInsightRequest(deps, question, evidence),\n );\n record(insightRes.usage);\n const text = parseInsight(insightRes.text);\n if (text === null) continue;\n\n // Skip near-duplicates of an already-stored insight (and of one just\n // written this pass).\n const normalized = normalizeInsightText(text);\n if (seenInsights.has(normalized)) continue;\n seenInsights.add(normalized);\n\n const iso = new Date(nowFn()).toISOString();\n const insightId = newMemoryId('ins');\n await deps.insights.insert({\n id: insightId,\n kind: 'insight',\n userId: deps.scope.userId,\n ...(deps.scope.sessionId !== undefined ? { sessionId: deps.scope.sessionId } : {}),\n ...(deps.scope.agentId !== undefined ? { agentId: deps.scope.agentId } : {}),\n text,\n // cites = the actual retrieved evidence ids (framework-set).\n cites: evidence.map((e) => e.id),\n salience: STARTING_SALIENCE,\n provenance: 'reflection',\n status: 'quarantined',\n sensitivity: 'internal',\n createdAt: iso,\n updatedAt: iso,\n });\n created.push(insightId);\n insightsCreated += 1;\n }\n\n // 5. ExpeL forgetting (MCON-16): decay every existing insight by 1\n // per reflection pass. Retrieval bumps +1 (InsightMemory.search),\n // so an insight recalled since the last pass nets level-or-better\n // while an unused one slides toward the prune floor — starting\n // salience 2 ⇒ pruned after two idle passes. Fresh insights from\n // THIS pass are exempt (they were inserted above at full salience\n // and have had no retrieval window yet).\n const freshIds = new Set(created);\n for (const existing of await deps.insights.list(deps.scope, {\n includeQuarantined: true,\n limit: 500,\n })) {\n if (freshIds.has(existing.id)) continue;\n await deps.insights.bumpSalience(existing.id, -1, 'reflection-pass-decay');\n }\n await deps.insights.prune(deps.scope);\n\n span.setAttributes({\n 'consolidator.reflect.triggered': 1,\n 'consolidator.reflect.insights_created': insightsCreated,\n 'consolidator.reflect.tokens': tokens,\n 'consolidator.reflect.cost.usd': costUsd,\n });\n return {\n triggered: true,\n accumulatedImportance: accumulated,\n questionsAsked: questions.length,\n insightsCreated,\n tokens,\n costUsd,\n // Advance past everything reflected on this pass. `advancedWatermark`\n // is finite here (the gate fired ⇒ `fresh` is non-empty).\n nextWatermark: Number.isFinite(advancedWatermark) ? advancedWatermark : watermark,\n };\n },\n );\n}\n\n/**\n * Normalise an insight for exact-dedup comparison — lowercased, punctuation\n * stripped, whitespace collapsed. Two insights that differ only in casing or\n * trailing punctuation collapse to the same key.\n *\n * @internal\n */\nexport function normalizeInsightText(text: string): string {\n return text\n .toLowerCase()\n .replace(/[^\\p{L}\\p{N}\\s]/gu, ' ')\n .replace(/\\s+/gu, ' ')\n .trim();\n}\n\nfunction buildQuestionsRequest(deps: ReflectionDeps, recentSummaries: string): ProviderRequest {\n const userBlock = [\n 'Recent memories:',\n recentSummaries.length > 0 ? recentSummaries : '(none)',\n '',\n `Name up to ${deps.maxQuestions} salient questions.`,\n ].join('\\n');\n return {\n messages: [{ role: 'user', content: userBlock }],\n systemMessage: QUESTIONS_SYSTEM_PROMPT,\n temperature: 0,\n // MCON-14: per-call output cap — a short question list.\n maxTokens: 512,\n metadata: scopeMetadata(deps.scope),\n outputType: { kind: 'structured' },\n };\n}\n\nfunction buildInsightRequest(\n deps: ReflectionDeps,\n question: string,\n evidence: ReadonlyArray<Evidence>,\n): ProviderRequest {\n const evidenceBlock = evidence.map((e, i) => `[${i + 1}] (id=${e.id}) ${e.text}`).join('\\n');\n const userBlock = [`Question: ${question}`, '', 'Evidence:', evidenceBlock].join('\\n');\n return {\n messages: [{ role: 'user', content: userBlock }],\n systemMessage: INSIGHT_SYSTEM_PROMPT,\n temperature: 0,\n // MCON-14: per-call output cap — one insight object.\n maxTokens: 512,\n metadata: scopeMetadata(deps.scope),\n outputType: { kind: 'structured' },\n };\n}\n\nfunction scopeMetadata(scope: SessionScope): Record<string, string> {\n return {\n userId: scope.userId,\n ...(scope.sessionId !== undefined ? { sessionId: scope.sessionId } : {}),\n ...(scope.agentId !== undefined ? { agentId: scope.agentId } : {}),\n };\n}\n\nfunction dedupeEvidence(items: ReadonlyArray<Evidence>): Evidence[] {\n const seen = new Set<string>();\n const out: Evidence[] = [];\n for (const e of items) {\n if (e.id.length === 0 || seen.has(e.id)) continue;\n seen.add(e.id);\n out.push(e);\n }\n return out;\n}\n\n/**\n * Parse the salient-questions response — accepts `{ questions: [...] }`\n * or a bare `[...]`, tolerating chatty / fenced output. Returns the\n * trimmed, non-empty string questions (possibly `[]`).\n *\n * @internal\n */\nexport function parseQuestions(text: string | undefined): string[] {\n if (text === undefined || text.length === 0) return [];\n const candidate = stripFence(text).trim();\n const parsed = tryParse(candidate);\n if (parsed === undefined) return [];\n const arr = Array.isArray(parsed)\n ? parsed\n : isRecord(parsed) && Array.isArray(parsed.questions)\n ? parsed.questions\n : null;\n if (arr === null) return [];\n return arr\n .filter((q): q is string => typeof q === 'string' && q.trim().length > 0)\n .map((q) => q.trim());\n}\n\n/**\n * Parse the insight-synthesis response — accepts `{ insight: string }`\n * (or `{ text: string }`), tolerating chatty / fenced output. Returns\n * the trimmed insight text, or `null` when absent / empty (insufficient\n * evidence ⇒ no insight).\n *\n * @internal\n */\nexport function parseInsight(text: string | undefined): string | null {\n if (text === undefined || text.length === 0) return null;\n const candidate = stripFence(text).trim();\n const parsed = tryParse(candidate);\n if (!isRecord(parsed)) return null;\n const raw = typeof parsed.insight === 'string' ? parsed.insight : null;\n const fallback = typeof parsed.text === 'string' ? parsed.text : null;\n const value = raw ?? fallback;\n if (value === null || value.trim().length === 0) return null;\n return value.trim();\n}\n\nfunction tryParse(candidate: string): unknown {\n if (candidate.length === 0) return undefined;\n try {\n return JSON.parse(candidate);\n } catch {\n const obj = sliceBetween(candidate, '{', '}');\n if (obj !== null) {\n try {\n return JSON.parse(obj);\n } catch {\n // fall through to array attempt\n }\n }\n const arr = sliceBetween(candidate, '[', ']');\n if (arr !== null) {\n try {\n return JSON.parse(arr);\n } catch {\n return undefined;\n }\n }\n return undefined;\n }\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\n\nfunction stripFence(text: string): string {\n const match = /^```[^\\n]*\\n([\\s\\S]*?)\\n```/u.exec(text.trim());\n return match?.[1] ?? text;\n}\n\nfunction sliceBetween(text: string, open: string, close: string): string | null {\n const start = text.indexOf(open);\n const end = text.lastIndexOf(close);\n if (start < 0 || end < start) return null;\n return text.slice(start, end + 1);\n}\n"],"mappings":";;;;;AA4BA,MAAM,oBAAoB;;AAG1B,MAAM,uBAAuB;AAgD7B,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACD,CAAC,KAAK,IAAI;AAEX,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;CACA;CACD,CAAC,KAAK,IAAI;;;;;;AAYX,eAAsB,kBAAkB,MAAkD;AACxF,QAAO,eACL,KAAK,QACL,8BACA,KAAK,OACL,EAAE,sBAAsB,WAAW,EACnC,OAAO,SAAS;EACd,MAAM,SAAS,OAAmC,EAAE,MAAyB;GAC3E,WAAW;GACX,uBAAuB;GACvB,gBAAgB;GAChB,iBAAiB;GACjB,QAAQ;GACR,SAAS;GACT,eAAe,KAAK,uBAAuB;GAC3C,GAAG;GACJ;AAED,MAAI,KAAK,aAAa,MAAM;AAC1B,QAAK,cAAc,EAAE,gCAAgC,eAAe,CAAC;AACrE,UAAO,OAAO;;AAEhB,MAAI,KAAK,OAAO,UAAU,CAAC,QAAQ;AACjC,QAAK,cAAc,EAAE,gCAAgC,UAAU,CAAC;AAChE,UAAO,OAAO;;EAShB,MAAM,iBAAiB,MAAM,KAAK,SAAS,WAAW,KAAK,OAAO,IAAI,EACpE,oBAAoB,MACrB,CAAC;EACF,MAAM,YAAY,KAAK,uBAAuB;EAC9C,MAAM,QACJ,cAAc,OACV,iBACA,eAAe,QAAQ,MAAM,KAAK,MAAM,EAAE,QAAQ,GAAG,UAAU;EACrE,MAAM,cAAc,MAAM,QAAQ,KAAK,YAAY,OAAO,QAAQ,cAAc,IAAI,EAAE;AACtF,OAAK,cAAc;GACjB,+CAA+C;GAC/C,kCAAkC,KAAK;GACxC,CAAC;AACF,MAAI,cAAc,KAAK,qBAAqB;AAC1C,QAAK,cAAc,EAAE,kCAAkC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ACrB,KAAI,UAAU,QAAQ,MAAM,MAAM,CAAC,WAAW,EAAG,QAAO;AACxD,QAAO,MAAM,MAAM;;AAGrB,SAAS,SAAS,WAA4B;AAC5C,KAAI,UAAU,WAAW,EAAG,QAAO;AACnC,KAAI;AACF,SAAO,KAAK,MAAM,UAAU;SACtB;EACN,MAAM,MAAM,aAAa,WAAW,KAAK,IAAI;AAC7C,MAAI,QAAQ,KACV,KAAI;AACF,UAAO,KAAK,MAAM,IAAI;UAChB;EAIV,MAAM,MAAM,aAAa,WAAW,KAAK,IAAI;AAC7C,MAAI,QAAQ,KACV,KAAI;AACF,UAAO,KAAK,MAAM,IAAI;UAChB;AACN;;AAGJ;;;AAIJ,SAAS,SAAS,OAAkD;AAClE,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM;;AAG7E,SAAS,WAAW,MAAsB;AAExC,QADc,+BAA+B,KAAK,KAAK,MAAM,CAAC,GAC/C,MAAM;;AAGvB,SAAS,aAAa,MAAc,MAAc,OAA8B;CAC9E,MAAM,QAAQ,KAAK,QAAQ,KAAK;CAChC,MAAM,MAAM,KAAK,YAAY,MAAM;AACnC,KAAI,QAAQ,KAAK,MAAM,MAAO,QAAO;AACrC,QAAO,KAAK,MAAM,OAAO,MAAM,EAAE"}
1
+ {"version":3,"file":"reflect.js","names":["created: string[]","out: Evidence[]"],"sources":["../../../src/consolidator/phases/reflect.ts"],"sourcesContent":["/**\n * Reflection pass (P1-1) - the deep phase's higher-order synthesis\n * step, following the Generative-Agents recipe. When the accumulated\n * importance of recent episodes crosses a threshold, ask the model for\n * the few most salient questions, retrieve evidence for each, and\n * synthesize an **insight** the evidence supports - recorded\n * quarantined, `provenance: 'reflection'`, and **citing the retrieved\n * memory ids** (citations are set by the framework from the actual\n * evidence, never chosen by the model, so they can never be\n * hallucinated). Insights start at ExpeL salience `2`; the pass prunes\n * any that have decayed to `0`.\n *\n * Runs after the deep-phase conflict drain (wired in the runtime\n * dispatch), reusing the same budget / lock / run-audit envelope. The\n * conflict-resolution logic in `deep.ts` is untouched.\n *\n * @packageDocumentation\n */\n\nimport type { Provider, ProviderRequest, SessionScope, Tracer } from '@graphorin/core';\nimport { newMemoryId } from '../../internal/id.js';\nimport { withMemorySpan } from '../../internal/spans.js';\nimport type { InsightMemoryStoreExt } from '../../internal/storage-adapter.js';\nimport type { EpisodicMemory } from '../../tiers/episodic-memory.js';\nimport type { SemanticMemory } from '../../tiers/semantic-memory.js';\nimport type { BudgetTracker } from '../budget.js';\n\n/** ExpeL starting salience for a freshly-synthesized insight. */\nconst STARTING_SALIENCE = 2;\n\n/** How many recent episode summaries to show the salient-questions prompt. */\nconst RECENT_CONTEXT_LIMIT = 20;\n\n/** Inputs accepted by {@link runReflectionPass}. */\nexport interface ReflectionDeps {\n readonly provider: Provider;\n readonly tracer: Tracer;\n readonly scope: SessionScope;\n /** Tier used to retrieve fact evidence for each salient question. */\n readonly semantic: SemanticMemory;\n /** Importance source + episode-evidence retrieval. `null` ⇒ pass is a no-op. */\n readonly episodic: EpisodicMemory | null;\n /** Insight write + salience surface. */\n readonly insights: InsightMemoryStoreExt;\n readonly budget: BudgetTracker;\n /** Reflection fires only when accumulated episode importance ≥ this. */\n readonly importanceThreshold: number;\n /**\n * `ended_at` (epoch ms) of the newest episode a prior pass already reflected\n * on (MCON-13). The gate accumulates importance only from strictly-newer\n * episodes, so a deep run with no new episodes never re-fires. `null` /\n * absent ⇒ nothing reflected yet (accumulate over all recent episodes).\n */\n readonly reflectionWatermark?: number | null;\n /** Upper bound on salient questions asked per pass. */\n readonly maxQuestions: number;\n readonly priceUsage?: (usage: { promptTokens: number; completionTokens: number }) => number;\n /** Override the wall clock - used by tests. */\n readonly now?: () => number;\n}\n\n/** Summary returned by {@link runReflectionPass}. */\nexport interface ReflectionOutcome {\n /** True when accumulated importance crossed the threshold and the pass ran. */\n readonly triggered: boolean;\n readonly accumulatedImportance: number;\n readonly questionsAsked: number;\n readonly insightsCreated: number;\n readonly tokens: number;\n readonly costUsd: number;\n /**\n * Watermark to persist for the next pass (MCON-13): the `ended_at` (epoch\n * ms) of the newest episode this pass reflected on, or the incoming\n * watermark unchanged when the pass did not fire. The runtime persists it\n * to `consolidator_state`.\n */\n readonly nextWatermark: number | null;\n}\n\nconst QUESTIONS_SYSTEM_PROMPT = [\n 'You are the reflection step (salient-questions) of a long-running personal-assistant memory.',\n 'Given a list of recent memories, name the few most salient, high-level questions whose',\n 'answers would most improve understanding of the user. Do NOT answer them; ask only what the',\n 'memories collectively raise. Return strictly JSON: { \"questions\": string[] }.',\n].join(' ');\n\nconst INSIGHT_SYSTEM_PROMPT = [\n 'You are the reflection step (insight-synthesis) of a long-running personal-assistant memory.',\n 'Given a salient question and the supporting evidence, synthesize ONE concise, higher-order',\n 'insight that the evidence collectively supports. Ground it ONLY in the supplied evidence; if',\n 'the evidence is insufficient, return an empty string. Do NOT invent facts. Return strictly',\n 'JSON: { \"insight\": string }.',\n].join(' ');\n\ninterface Evidence {\n readonly id: string;\n readonly text: string;\n}\n\n/**\n * Run the reflection pass. Idempotent w.r.t. failures - a parse miss or\n * empty synthesis simply yields no insight. Never throws on model\n * output; only storage errors propagate.\n */\nexport async function runReflectionPass(deps: ReflectionDeps): Promise<ReflectionOutcome> {\n return withMemorySpan(\n deps.tracer,\n 'memory.consolidate.reflect',\n deps.scope,\n { 'consolidator.phase': 'reflect' },\n async (span) => {\n const empty = (over: Partial<ReflectionOutcome> = {}): ReflectionOutcome => ({\n triggered: false,\n accumulatedImportance: 0,\n questionsAsked: 0,\n insightsCreated: 0,\n tokens: 0,\n costUsd: 0,\n nextWatermark: deps.reflectionWatermark ?? null,\n ...over,\n });\n\n if (deps.episodic === null) {\n span.setAttributes({ 'consolidator.reflect.skipped': 'no-episodic' });\n return empty();\n }\n if (deps.budget.snapshot().paused) {\n span.setAttributes({ 'consolidator.reflect.skipped': 'budget' });\n return empty();\n }\n\n // 1. Accumulated-importance gate. Recency, not relevance: list the most\n // recent episodes (MCON-1 - the old `'*'` FTS probe matched zero rows\n // on real SQLite). Auto-formed episodes (P1-2) land\n // quarantined, so importance accrues there - include them. Only count\n // episodes newer than the watermark so a pass with no new episodes\n // never re-fires the paid synthesis (MCON-13).\n const recentEpisodes = await deps.episodic.listRecent(deps.scope, 50, {\n includeQuarantined: true,\n });\n const watermark = deps.reflectionWatermark ?? null;\n const fresh =\n watermark === null\n ? recentEpisodes\n : recentEpisodes.filter((e) => Date.parse(e.endedAt) > watermark);\n const accumulated = fresh.reduce((sum, episode) => sum + (episode.importance ?? 0), 0);\n span.setAttributes({\n 'consolidator.reflect.accumulated_importance': accumulated,\n 'consolidator.reflect.threshold': deps.importanceThreshold,\n });\n if (accumulated < deps.importanceThreshold) {\n span.setAttributes({ 'consolidator.reflect.triggered': 0 });\n return empty({ accumulatedImportance: accumulated });\n }\n // The newest episode this pass will reflect on - persisted as the next\n // watermark so subsequent passes skip everything up to here.\n const advancedWatermark = fresh.reduce(\n (max, episode) => Math.max(max, Date.parse(episode.endedAt)),\n watermark ?? Number.NEGATIVE_INFINITY,\n );\n\n let tokens = 0;\n let costUsd = 0;\n const record = (usage: {\n promptTokens?: number;\n completionTokens?: number;\n reasoningTokens?: number;\n }): void => {\n const t =\n (usage.promptTokens ?? 0) + (usage.completionTokens ?? 0) + (usage.reasoningTokens ?? 0);\n const c =\n deps.priceUsage?.({\n promptTokens: usage.promptTokens ?? 0,\n completionTokens: usage.completionTokens ?? 0,\n }) ?? 0;\n tokens += t;\n costUsd += c;\n deps.budget.record({ phase: 'deep', tokens: t, costUsd: c });\n };\n\n // 2. Salient questions over the new (post-watermark) memories.\n const recentSummaries = fresh\n .slice(0, RECENT_CONTEXT_LIMIT)\n .map((episode) => `- ${episode.summary}`)\n .join('\\n');\n const questionsRes = await deps.provider.generate(\n buildQuestionsRequest(deps, recentSummaries),\n );\n record(questionsRes.usage);\n const questions = parseQuestions(questionsRes.text).slice(0, Math.max(0, deps.maxQuestions));\n span.setAttributes({ 'consolidator.reflect.questions': questions.length });\n\n // Pre-filter new insights against already-stored ones so a re-fire over\n // overlapping evidence does not write near-duplicates (MCON-13). Offline\n // and embedding-free: normalised exact match, the insight analogue of\n // the conflict pipeline's stage-1 dedup.\n const seenInsights = new Set(\n (await deps.insights.list(deps.scope, { includeQuarantined: true, limit: 200 })).map((i) =>\n normalizeInsightText(i.text),\n ),\n );\n\n const nowFn = typeof deps.now === 'function' ? deps.now : Date.now;\n let insightsCreated = 0;\n const created: string[] = [];\n for (const question of questions) {\n if (deps.budget.snapshot().paused) break;\n\n // 3. Retrieve evidence. Facts are trustworthy (active only);\n // episodes include the quarantined auto-formed ones.\n const factHits = await deps.semantic.search(deps.scope, question, { topK: 5 });\n const epHits = await deps.episodic.search(deps.scope, question, {\n topK: 5,\n includeQuarantined: true,\n });\n const evidence = dedupeEvidence([\n ...factHits.map((h) => ({ id: h.record.id, text: h.record.text })),\n ...epHits.map((h) => ({ id: h.record.id, text: h.record.summary })),\n ]);\n // Citations are mandatory - no evidence ⇒ no insight.\n if (evidence.length === 0) continue;\n\n // 4. Synthesize an insight grounded in that evidence.\n if (deps.budget.snapshot().paused) break;\n const insightRes = await deps.provider.generate(\n buildInsightRequest(deps, question, evidence),\n );\n record(insightRes.usage);\n const text = parseInsight(insightRes.text);\n if (text === null) continue;\n\n // Skip near-duplicates of an already-stored insight (and of one just\n // written this pass).\n const normalized = normalizeInsightText(text);\n if (seenInsights.has(normalized)) continue;\n seenInsights.add(normalized);\n\n const iso = new Date(nowFn()).toISOString();\n const insightId = newMemoryId('ins');\n await deps.insights.insert({\n id: insightId,\n kind: 'insight',\n userId: deps.scope.userId,\n ...(deps.scope.sessionId !== undefined ? { sessionId: deps.scope.sessionId } : {}),\n ...(deps.scope.agentId !== undefined ? { agentId: deps.scope.agentId } : {}),\n text,\n // cites = the actual retrieved evidence ids (framework-set).\n cites: evidence.map((e) => e.id),\n salience: STARTING_SALIENCE,\n provenance: 'reflection',\n status: 'quarantined',\n // D3: a synthesized insight is the agent's own inference.\n owner: 'agent',\n sensitivity: 'internal',\n createdAt: iso,\n updatedAt: iso,\n });\n created.push(insightId);\n insightsCreated += 1;\n }\n\n // 5. ExpeL forgetting (MCON-16): decay every existing insight by 1\n // per reflection pass. Retrieval bumps +1 (InsightMemory.search),\n // so an insight recalled since the last pass nets level-or-better\n // while an unused one slides toward the prune floor - starting\n // salience 2 ⇒ pruned after two idle passes. Fresh insights from\n // THIS pass are exempt (they were inserted above at full salience\n // and have had no retrieval window yet).\n const freshIds = new Set(created);\n for (const existing of await deps.insights.list(deps.scope, {\n includeQuarantined: true,\n limit: 500,\n })) {\n if (freshIds.has(existing.id)) continue;\n await deps.insights.bumpSalience(existing.id, -1, 'reflection-pass-decay');\n }\n await deps.insights.prune(deps.scope);\n\n span.setAttributes({\n 'consolidator.reflect.triggered': 1,\n 'consolidator.reflect.insights_created': insightsCreated,\n 'consolidator.reflect.tokens': tokens,\n 'consolidator.reflect.cost.usd': costUsd,\n });\n return {\n triggered: true,\n accumulatedImportance: accumulated,\n questionsAsked: questions.length,\n insightsCreated,\n tokens,\n costUsd,\n // Advance past everything reflected on this pass. `advancedWatermark`\n // is finite here (the gate fired ⇒ `fresh` is non-empty).\n nextWatermark: Number.isFinite(advancedWatermark) ? advancedWatermark : watermark,\n };\n },\n );\n}\n\n/**\n * Normalise an insight for exact-dedup comparison - lowercased, punctuation\n * stripped, whitespace collapsed. Two insights that differ only in casing or\n * trailing punctuation collapse to the same key.\n *\n * @internal\n */\nexport function normalizeInsightText(text: string): string {\n return text\n .toLowerCase()\n .replace(/[^\\p{L}\\p{N}\\s]/gu, ' ')\n .replace(/\\s+/gu, ' ')\n .trim();\n}\n\nfunction buildQuestionsRequest(deps: ReflectionDeps, recentSummaries: string): ProviderRequest {\n const userBlock = [\n 'Recent memories:',\n recentSummaries.length > 0 ? recentSummaries : '(none)',\n '',\n `Name up to ${deps.maxQuestions} salient questions.`,\n ].join('\\n');\n return {\n messages: [{ role: 'user', content: userBlock }],\n systemMessage: QUESTIONS_SYSTEM_PROMPT,\n temperature: 0,\n // MCON-14: per-call output cap - a short question list.\n maxTokens: 512,\n metadata: scopeMetadata(deps.scope),\n outputType: { kind: 'structured' },\n };\n}\n\nfunction buildInsightRequest(\n deps: ReflectionDeps,\n question: string,\n evidence: ReadonlyArray<Evidence>,\n): ProviderRequest {\n const evidenceBlock = evidence.map((e, i) => `[${i + 1}] (id=${e.id}) ${e.text}`).join('\\n');\n const userBlock = [`Question: ${question}`, '', 'Evidence:', evidenceBlock].join('\\n');\n return {\n messages: [{ role: 'user', content: userBlock }],\n systemMessage: INSIGHT_SYSTEM_PROMPT,\n temperature: 0,\n // MCON-14: per-call output cap - one insight object.\n maxTokens: 512,\n metadata: scopeMetadata(deps.scope),\n outputType: { kind: 'structured' },\n };\n}\n\nfunction scopeMetadata(scope: SessionScope): Record<string, string> {\n return {\n userId: scope.userId,\n ...(scope.sessionId !== undefined ? { sessionId: scope.sessionId } : {}),\n ...(scope.agentId !== undefined ? { agentId: scope.agentId } : {}),\n };\n}\n\nfunction dedupeEvidence(items: ReadonlyArray<Evidence>): Evidence[] {\n const seen = new Set<string>();\n const out: Evidence[] = [];\n for (const e of items) {\n if (e.id.length === 0 || seen.has(e.id)) continue;\n seen.add(e.id);\n out.push(e);\n }\n return out;\n}\n\n/**\n * Parse the salient-questions response - accepts `{ questions: [...] }`\n * or a bare `[...]`, tolerating chatty / fenced output. Returns the\n * trimmed, non-empty string questions (possibly `[]`).\n *\n * @internal\n */\nexport function parseQuestions(text: string | undefined): string[] {\n if (text === undefined || text.length === 0) return [];\n const candidate = stripFence(text).trim();\n const parsed = tryParse(candidate);\n if (parsed === undefined) return [];\n const arr = Array.isArray(parsed)\n ? parsed\n : isRecord(parsed) && Array.isArray(parsed.questions)\n ? parsed.questions\n : null;\n if (arr === null) return [];\n return arr\n .filter((q): q is string => typeof q === 'string' && q.trim().length > 0)\n .map((q) => q.trim());\n}\n\n/**\n * Parse the insight-synthesis response - accepts `{ insight: string }`\n * (or `{ text: string }`), tolerating chatty / fenced output. Returns\n * the trimmed insight text, or `null` when absent / empty (insufficient\n * evidence ⇒ no insight).\n *\n * @internal\n */\nexport function parseInsight(text: string | undefined): string | null {\n if (text === undefined || text.length === 0) return null;\n const candidate = stripFence(text).trim();\n const parsed = tryParse(candidate);\n if (!isRecord(parsed)) return null;\n const raw = typeof parsed.insight === 'string' ? parsed.insight : null;\n const fallback = typeof parsed.text === 'string' ? parsed.text : null;\n const value = raw ?? fallback;\n if (value === null || value.trim().length === 0) return null;\n return value.trim();\n}\n\nfunction tryParse(candidate: string): unknown {\n if (candidate.length === 0) return undefined;\n try {\n return JSON.parse(candidate);\n } catch {\n const obj = sliceBetween(candidate, '{', '}');\n if (obj !== null) {\n try {\n return JSON.parse(obj);\n } catch {\n // fall through to array attempt\n }\n }\n const arr = sliceBetween(candidate, '[', ']');\n if (arr !== null) {\n try {\n return JSON.parse(arr);\n } catch {\n return undefined;\n }\n }\n return undefined;\n }\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\n\nfunction stripFence(text: string): string {\n const match = /^```[^\\n]*\\n([\\s\\S]*?)\\n```/u.exec(text.trim());\n return match?.[1] ?? text;\n}\n\nfunction sliceBetween(text: string, open: string, close: string): string | null {\n const start = text.indexOf(open);\n const end = text.lastIndexOf(close);\n if (start < 0 || end < start) return null;\n return text.slice(start, end + 1);\n}\n"],"mappings":";;;;;AA4BA,MAAM,oBAAoB;;AAG1B,MAAM,uBAAuB;AAgD7B,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACD,CAAC,KAAK,IAAI;AAEX,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;CACA;CACD,CAAC,KAAK,IAAI;;;;;;AAYX,eAAsB,kBAAkB,MAAkD;AACxF,QAAO,eACL,KAAK,QACL,8BACA,KAAK,OACL,EAAE,sBAAsB,WAAW,EACnC,OAAO,SAAS;EACd,MAAM,SAAS,OAAmC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O,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM;;AAG7E,SAAS,WAAW,MAAsB;AAExC,QADc,+BAA+B,KAAK,KAAK,MAAM,CAAC,GAC/C,MAAM;;AAGvB,SAAS,aAAa,MAAc,MAAc,OAA8B;CAC9E,MAAM,QAAQ,KAAK,QAAQ,KAAK;CAChC,MAAM,MAAM,KAAK,YAAY,MAAM;AACnC,KAAI,QAAQ,KAAK,MAAM,MAAO,QAAO;AACrC,QAAO,KAAK,MAAM,OAAO,MAAM,EAAE"}
@@ -13,6 +13,7 @@ const EXTRACTION_SYSTEM_PROMPT = [
13
13
  "You are a memory-extraction assistant for a long-running personal-assistant runtime.",
14
14
  "Read the supplied conversation slice and return the durable facts it asserts about the user, the world, or stable preferences.",
15
15
  "Skip greetings, banter, transient state, and anything the assistant produced as boilerplate.",
16
+ "Each fact text MUST be a self-contained proposition understandable with no surrounding context: resolve pronouns and ellipses to the named person or thing (\"she\" -> \"Maria\"), inline the concrete entities, and never write a fact that needs a neighbouring fact to make sense.",
16
17
  "For each fact also rate how important it is for remembering the user, on an integer scale from 1 (incidental detail) to 10 (identity-defining).",
17
18
  "Return a single JSON object: { \"facts\": [{ \"text\": string, \"subject\"?: string, \"predicate\"?: string, \"object\"?: string, \"confidence\"?: number, \"importance\"?: number }] }.",
18
19
  "If the slice contains no durable facts, return { \"facts\": [] }."
@@ -28,12 +29,12 @@ const EXTRACTION_MAX_TOKENS = 1024;
28
29
  const EPISODE_MAX_TOKENS = 512;
29
30
  const EPISODE_SUMMARY_SYSTEM_PROMPT = [
30
31
  "You are an episode-summarization assistant for a long-running personal-assistant runtime.",
31
- "Read the supplied conversation slice and write one concise third-person summary of what happened the episode.",
32
+ "Read the supplied conversation slice and write one concise third-person summary of what happened - the episode.",
32
33
  "Return a single JSON object: { \"summary\": string }."
33
34
  ].join(" ");
34
35
  const EPISODE_SUMMARY_IMPORTANCE_SYSTEM_PROMPT = [
35
36
  "You are an episode-summarization assistant for a long-running personal-assistant runtime.",
36
- "Read the supplied conversation slice and write one concise third-person summary of what happened the episode.",
37
+ "Read the supplied conversation slice and write one concise third-person summary of what happened - the episode.",
37
38
  "Also rate how important / poignant this episode is for remembering the user, on an integer scale from 1 (mundane) to 10 (deeply significant).",
38
39
  "Return a single JSON object: { \"summary\": string, \"importance\": number }."
39
40
  ].join(" ");
@@ -109,7 +110,15 @@ async function runStandardPhase(deps) {
109
110
  if (fact.text.trim().length === 0) continue;
110
111
  const input = buildFactInput(fact);
111
112
  const neighbors = await deps.semantic.neighbors(deps.scope, fact.text, { topK: RECONCILE_TOP_K });
112
- const route = await preFilterCandidate(fact.text, neighbors);
113
+ let route = await preFilterCandidate(fact.text, neighbors);
114
+ if (route.route !== "noop" && neighbors.length === 0) {
115
+ const exactId = await findExactTextDuplicate(deps.semantic, deps.scope, fact.text);
116
+ if (exactId !== null) route = {
117
+ route: "noop",
118
+ targetId: exactId,
119
+ reason: "exact-text-fts"
120
+ };
121
+ }
113
122
  let decision;
114
123
  let audited;
115
124
  if (route.route === "reconcile" && !deps.budget.snapshot().paused) {
@@ -154,7 +163,7 @@ async function runStandardPhase(deps) {
154
163
  };
155
164
  audited = {
156
165
  kind: "dedup",
157
- stage: route.reason === "exact-hash-match" ? "exact-dedup" : "embedding-three-zone",
166
+ stage: route.reason === "exact-hash-match" || route.reason === "exact-text-fts" ? "exact-dedup" : "embedding-three-zone",
158
167
  existingId: route.targetId,
159
168
  ...route.similarity !== void 0 ? { similarity: route.similarity } : {},
160
169
  reason: route.reason
@@ -250,7 +259,8 @@ async function runStandardPhase(deps) {
250
259
  endedAt,
251
260
  ...importance !== void 0 ? { importance } : {},
252
261
  provenance: "extraction",
253
- status: "quarantined"
262
+ status: "quarantined",
263
+ owner: "agent"
254
264
  });
255
265
  episodesFormed = 1;
256
266
  }
@@ -293,12 +303,13 @@ async function runStandardPhase(deps) {
293
303
  });
294
304
  }
295
305
  function buildRequest(deps, transcript) {
306
+ const today = new Date(deps.now?.() ?? Date.now()).toISOString().slice(0, 10);
296
307
  return {
297
308
  messages: [{
298
309
  role: "user",
299
310
  content: `Conversation slice:\n${transcript}`
300
311
  }],
301
- systemMessage: EXTRACTION_SYSTEM_PROMPT,
312
+ systemMessage: `${EXTRACTION_SYSTEM_PROMPT} Today is ${today}. Each transcript line carries its timestamp in parentheses; resolve relative dates ("next Friday", "last month") into absolute ISO dates in the extracted fact text.`,
302
313
  temperature: 0,
303
314
  maxTokens: EXTRACTION_MAX_TOKENS,
304
315
  metadata: {
@@ -336,7 +347,8 @@ function renderTranscript(messages) {
336
347
  return messages.map((m) => {
337
348
  const role = m.message.role;
338
349
  const text = renderText(m.message);
339
- return `[${m.sequence}] ${role}: ${text}`;
350
+ const stamp = typeof m.createdAt === "string" && m.createdAt.length > 0 ? m.createdAt : "";
351
+ return stamp.length > 0 ? `[${m.sequence}] (${stamp}) ${role}: ${text}` : `[${m.sequence}] ${role}: ${text}`;
340
352
  }).join("\n");
341
353
  }
342
354
  /**
@@ -442,15 +454,32 @@ function sliceJsonObject(text) {
442
454
  }
443
455
  /**
444
456
  * Build the {@link FactInput} for an extracted candidate. P1-4:
445
- * distilled-from-transcript facts are synthesized memory tagged
457
+ * distilled-from-transcript facts are synthesized memory - tagged
446
458
  * `extraction` so they land quarantined (excluded from action-driving
447
459
  * recall until validated).
448
460
  */
461
+ /**
462
+ * Embedder-independent exact-duplicate lookup
463
+ * (memory-consolidation-07): FTS top hits + strict string equality.
464
+ * Returns the existing fact's id or `null`. Failures are swallowed -
465
+ * the guard is an optimization, never a reason to fail the slice.
466
+ */
467
+ async function findExactTextDuplicate(semantic, scope, text) {
468
+ try {
469
+ return (await semantic.search(scope, text, {
470
+ topK: 5,
471
+ includeQuarantined: true
472
+ })).find((h) => h.record.text === text)?.record.id ?? null;
473
+ } catch {
474
+ return null;
475
+ }
476
+ }
449
477
  function buildFactInput(fact) {
450
478
  const importance = normalizeImportance(fact.importance);
451
479
  return {
452
480
  text: fact.text,
453
481
  provenance: "extraction",
482
+ owner: "agent",
454
483
  ...fact.subject !== void 0 ? { subject: fact.subject } : {},
455
484
  ...fact.predicate !== void 0 ? { predicate: fact.predicate } : {},
456
485
  ...fact.object !== void 0 ? { object: fact.object } : {},