@remnic/core 9.3.630 → 9.3.632

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 (214) hide show
  1. package/dist/access-cli.js +15 -13
  2. package/dist/access-cli.js.map +1 -1
  3. package/dist/access-http.d.ts +4 -4
  4. package/dist/access-http.js +7 -6
  5. package/dist/access-mcp.d.ts +4 -4
  6. package/dist/access-mcp.js +6 -5
  7. package/dist/{access-service-C4v-eFjB.d.ts → access-service-C9_EpVHd.d.ts} +2 -2
  8. package/dist/access-service.d.ts +4 -4
  9. package/dist/access-service.js +5 -4
  10. package/dist/action-confidence.d.ts +1 -1
  11. package/dist/active-memory-bridge.d.ts +1 -1
  12. package/dist/active-recall.d.ts +1 -1
  13. package/dist/active-recall.js +1 -1
  14. package/dist/auto-sync-RFADEHIQ.js +75 -0
  15. package/dist/auto-sync-RFADEHIQ.js.map +1 -0
  16. package/dist/behavior-learner.d.ts +1 -1
  17. package/dist/behavior-signals.d.ts +1 -1
  18. package/dist/bootstrap.d.ts +3 -3
  19. package/dist/briefing.d.ts +1 -1
  20. package/dist/briefing.js +3 -2
  21. package/dist/buffer-surprise-report.d.ts +1 -1
  22. package/dist/buffer.d.ts +1 -1
  23. package/dist/calibration.d.ts +1 -1
  24. package/dist/causal-behavior.d.ts +1 -1
  25. package/dist/causal-consolidation.d.ts +1 -1
  26. package/dist/causal-consolidation.js +4 -3
  27. package/dist/causal-consolidation.js.map +1 -1
  28. package/dist/{chunk-BWK5EEKS.js → chunk-242XFZ36.js} +2 -2
  29. package/dist/{chunk-K3BTOW7N.js → chunk-32U3N7H5.js} +3 -3
  30. package/dist/{chunk-5S6IREG3.js → chunk-3RDYU3JS.js} +3 -3
  31. package/dist/{chunk-SACS6KE6.js → chunk-4S3N6HFG.js} +2 -2
  32. package/dist/{chunk-VUTPRX7K.js → chunk-5PT5I6JQ.js} +14 -14
  33. package/dist/{chunk-2VJ7AJFX.js → chunk-7A2QKUUA.js} +2 -2
  34. package/dist/{chunk-EORL2IDM.js → chunk-7H5WCPBS.js} +58 -5
  35. package/dist/{chunk-EORL2IDM.js.map → chunk-7H5WCPBS.js.map} +1 -1
  36. package/dist/{chunk-NRQJBK36.js → chunk-C4KKM62E.js} +2 -2
  37. package/dist/{chunk-GXWFZYSR.js → chunk-CMN5AWAZ.js} +2 -2
  38. package/dist/{chunk-F6USGHMO.js → chunk-DOBJH4I6.js} +4 -4
  39. package/dist/{chunk-ADOD7PJC.js → chunk-IFVFQRZ2.js} +5 -5
  40. package/dist/{chunk-6LBQL5US.js → chunk-JCLECECB.js} +2 -2
  41. package/dist/chunk-KVDUDYEN.js +1164 -0
  42. package/dist/chunk-KVDUDYEN.js.map +1 -0
  43. package/dist/{chunk-S5W37FPX.js → chunk-LEG7XWS2.js} +2 -2
  44. package/dist/chunk-M7XQSUBB.js +280 -0
  45. package/dist/chunk-M7XQSUBB.js.map +1 -0
  46. package/dist/{chunk-MQ24KOOR.js → chunk-PUEAEQSN.js} +2 -2
  47. package/dist/{chunk-RSKUUEBA.js → chunk-QYGIQ5NM.js} +140 -417
  48. package/dist/chunk-QYGIQ5NM.js.map +1 -0
  49. package/dist/{chunk-UE57H4MA.js → chunk-UXFOGILU.js} +2 -2
  50. package/dist/{chunk-OQMR2SDZ.js → chunk-VTR3MNYF.js} +2 -2
  51. package/dist/{chunk-4QEUKASL.js → chunk-W25I7G6U.js} +2 -2
  52. package/dist/{chunk-YJOWWRRS.js → chunk-WLZBVYC6.js} +125 -1206
  53. package/dist/chunk-WLZBVYC6.js.map +1 -0
  54. package/dist/{chunk-OOFBE62K.js → chunk-X7EJF46S.js} +2 -2
  55. package/dist/{chunk-BL33LBTN.js → chunk-XG4NAWAV.js} +3 -3
  56. package/dist/{chunk-ZZSXUZF3.js → chunk-YROCXMCK.js} +2 -2
  57. package/dist/{cli-B_6EMiQc.d.ts → cli-CuVEQWKr.d.ts} +3 -3
  58. package/dist/cli.d.ts +5 -5
  59. package/dist/cli.js +18 -17
  60. package/dist/compounding/engine.d.ts +1 -1
  61. package/dist/compounding/engine.js +3 -2
  62. package/dist/compounding/preference-consolidator.d.ts +1 -1
  63. package/dist/compression-optimizer.d.ts +1 -1
  64. package/dist/config.d.ts +1 -1
  65. package/dist/config.js +1 -1
  66. package/dist/connectors/codex-materialize-runner.d.ts +1 -1
  67. package/dist/connectors/codex-materialize-runner.js +3 -2
  68. package/dist/connectors/codex-materialize.d.ts +1 -1
  69. package/dist/connectors/index.d.ts +1 -1
  70. package/dist/connectors/index.js +3 -2
  71. package/dist/consolidation-provenance-check.d.ts +1 -1
  72. package/dist/consolidation-undo.d.ts +1 -1
  73. package/dist/contradiction/index.d.ts +1 -1
  74. package/dist/contradiction/index.js +4 -4
  75. package/dist/conversation-index/backend.d.ts +1 -1
  76. package/dist/conversation-index/chunker.d.ts +1 -1
  77. package/dist/conversation-index/faiss-adapter.d.ts +1 -1
  78. package/dist/conversation-index/indexer.d.ts +1 -1
  79. package/dist/conversation-index/search.d.ts +1 -1
  80. package/dist/day-summary.d.ts +1 -1
  81. package/dist/delinearize.d.ts +1 -1
  82. package/dist/direct-answer-wiring.d.ts +1 -1
  83. package/dist/direct-answer.d.ts +1 -1
  84. package/dist/embedding-fallback.d.ts +1 -1
  85. package/dist/enrichment/index.d.ts +1 -1
  86. package/dist/entity-retrieval.d.ts +1 -1
  87. package/dist/entity-retrieval.js +3 -2
  88. package/dist/entity-schema.d.ts +1 -1
  89. package/dist/explicit-capture.d.ts +3 -3
  90. package/dist/extraction-judge-telemetry.d.ts +1 -1
  91. package/dist/extraction-judge-training.d.ts +1 -1
  92. package/dist/extraction-judge.d.ts +1 -1
  93. package/dist/extraction.d.ts +1 -1
  94. package/dist/fallback-llm.d.ts +1 -1
  95. package/dist/identity-continuity.d.ts +1 -1
  96. package/dist/importance.d.ts +1 -1
  97. package/dist/index.d.ts +8 -8
  98. package/dist/index.js +49 -45
  99. package/dist/index.js.map +1 -1
  100. package/dist/intent.d.ts +1 -1
  101. package/dist/lcm/engine.d.ts +1 -1
  102. package/dist/lcm/index.d.ts +1 -1
  103. package/dist/lcm/tools.d.ts +1 -1
  104. package/dist/lifecycle.d.ts +1 -1
  105. package/dist/live-connectors-runner.d.ts +1 -1
  106. package/dist/local-llm.d.ts +1 -1
  107. package/dist/maintenance/memory-governance.d.ts +1 -1
  108. package/dist/maintenance/memory-governance.js +3 -2
  109. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +3 -2
  110. package/dist/maintenance/rebuild-memory-projection.js +4 -3
  111. package/dist/mcp-memory-inspector-app.d.ts +4 -4
  112. package/dist/memory-action-policy.d.ts +1 -1
  113. package/dist/memory-cache.d.ts +1 -1
  114. package/dist/memory-lifecycle-ledger-utils.d.ts +1 -1
  115. package/dist/memory-projection-store.d.ts +1 -1
  116. package/dist/memory-provenance.d.ts +1 -1
  117. package/dist/memory-worth-outcomes.d.ts +1 -1
  118. package/dist/models-json.d.ts +1 -1
  119. package/dist/namespaces/migrate.d.ts +1 -1
  120. package/dist/namespaces/migrate.js +4 -3
  121. package/dist/namespaces/principal.d.ts +1 -1
  122. package/dist/namespaces/search.d.ts +1 -1
  123. package/dist/namespaces/storage.d.ts +1 -1
  124. package/dist/namespaces/storage.js +3 -2
  125. package/dist/native-knowledge.d.ts +1 -1
  126. package/dist/operator-toolkit.d.ts +1 -1
  127. package/dist/operator-toolkit.js +7 -6
  128. package/dist/{orchestrator-Dlw3ae4B.d.ts → orchestrator-CoqytbK_.d.ts} +3 -2
  129. package/dist/orchestrator.d.ts +3 -3
  130. package/dist/orchestrator.js +12 -10
  131. package/dist/patterns-cli.d.ts +1 -1
  132. package/dist/policy-runtime.d.ts +1 -1
  133. package/dist/qmd-recall-cache.d.ts +1 -1
  134. package/dist/qmd.d.ts +1 -1
  135. package/dist/recall-disclosure-escalation.d.ts +1 -1
  136. package/dist/recall-explain-renderer.d.ts +1 -1
  137. package/dist/recall-planner-llm.d.ts +1 -1
  138. package/dist/recall-state.d.ts +1 -1
  139. package/dist/recall-tag-filter.d.ts +1 -1
  140. package/dist/recall-xray-cli.d.ts +1 -1
  141. package/dist/recall-xray-renderer.d.ts +1 -1
  142. package/dist/recall-xray.d.ts +1 -1
  143. package/dist/resolve-auth-token.d.ts +1 -1
  144. package/dist/resume-bundles.js +2 -2
  145. package/dist/retrieval-agents.d.ts +1 -1
  146. package/dist/retrieval-tiers.d.ts +1 -1
  147. package/dist/routing/engine.d.ts +1 -1
  148. package/dist/routing/store.d.ts +1 -1
  149. package/dist/schemas.d.ts +22 -22
  150. package/dist/search/embed-helper.d.ts +1 -1
  151. package/dist/search/factory.d.ts +1 -1
  152. package/dist/search/index.d.ts +1 -1
  153. package/dist/search/lancedb-backend.d.ts +1 -1
  154. package/dist/search/meilisearch-backend.d.ts +1 -1
  155. package/dist/search/noop-backend.d.ts +1 -1
  156. package/dist/search/orama-backend.d.ts +1 -1
  157. package/dist/search/port.d.ts +1 -1
  158. package/dist/search/remote-backend.d.ts +1 -1
  159. package/dist/{semantic-consolidation-C4sefXEI.d.ts → semantic-consolidation-BPs6BURk.d.ts} +1 -1
  160. package/dist/semantic-consolidation.d.ts +2 -2
  161. package/dist/semantic-consolidation.js +4 -3
  162. package/dist/semantic-rule-promotion.js +3 -2
  163. package/dist/semantic-rule-verifier.d.ts +1 -1
  164. package/dist/semantic-rule-verifier.js +3 -2
  165. package/dist/session-observer-bands.d.ts +1 -1
  166. package/dist/session-observer-state.d.ts +1 -1
  167. package/dist/shared-context/manager.d.ts +1 -1
  168. package/dist/signal.d.ts +1 -1
  169. package/dist/storage.d.ts +1 -1
  170. package/dist/storage.js +2 -1
  171. package/dist/summarizer.d.ts +1 -1
  172. package/dist/summary-snapshot.d.ts +1 -1
  173. package/dist/temporal-supersession.d.ts +1 -1
  174. package/dist/temporal-validity.d.ts +1 -1
  175. package/dist/threading.d.ts +1 -1
  176. package/dist/tier-migration.d.ts +1 -1
  177. package/dist/tier-routing.d.ts +1 -1
  178. package/dist/topics.d.ts +1 -1
  179. package/dist/transcript.d.ts +1 -1
  180. package/dist/transfer/types.d.ts +12 -12
  181. package/dist/{types-2vqxmO0j.d.ts → types-CpMPD8xl.d.ts} +20 -1
  182. package/dist/types.d.ts +1 -1
  183. package/dist/utility-runtime.d.ts +1 -1
  184. package/dist/verified-recall.js +3 -2
  185. package/package.json +1 -1
  186. package/src/orchestrator.ts +58 -0
  187. package/src/wearables/auto-sync.test.ts +181 -0
  188. package/src/wearables/auto-sync.ts +129 -0
  189. package/src/wearables/config.test.ts +58 -8
  190. package/src/wearables/config.ts +75 -5
  191. package/src/wearables/pipeline.test.ts +87 -4
  192. package/src/wearables/pipeline.ts +43 -13
  193. package/src/wearables/types.ts +20 -1
  194. package/dist/chunk-RSKUUEBA.js.map +0 -1
  195. package/dist/chunk-YJOWWRRS.js.map +0 -1
  196. /package/dist/{chunk-BWK5EEKS.js.map → chunk-242XFZ36.js.map} +0 -0
  197. /package/dist/{chunk-K3BTOW7N.js.map → chunk-32U3N7H5.js.map} +0 -0
  198. /package/dist/{chunk-5S6IREG3.js.map → chunk-3RDYU3JS.js.map} +0 -0
  199. /package/dist/{chunk-SACS6KE6.js.map → chunk-4S3N6HFG.js.map} +0 -0
  200. /package/dist/{chunk-VUTPRX7K.js.map → chunk-5PT5I6JQ.js.map} +0 -0
  201. /package/dist/{chunk-2VJ7AJFX.js.map → chunk-7A2QKUUA.js.map} +0 -0
  202. /package/dist/{chunk-NRQJBK36.js.map → chunk-C4KKM62E.js.map} +0 -0
  203. /package/dist/{chunk-GXWFZYSR.js.map → chunk-CMN5AWAZ.js.map} +0 -0
  204. /package/dist/{chunk-F6USGHMO.js.map → chunk-DOBJH4I6.js.map} +0 -0
  205. /package/dist/{chunk-ADOD7PJC.js.map → chunk-IFVFQRZ2.js.map} +0 -0
  206. /package/dist/{chunk-6LBQL5US.js.map → chunk-JCLECECB.js.map} +0 -0
  207. /package/dist/{chunk-S5W37FPX.js.map → chunk-LEG7XWS2.js.map} +0 -0
  208. /package/dist/{chunk-MQ24KOOR.js.map → chunk-PUEAEQSN.js.map} +0 -0
  209. /package/dist/{chunk-UE57H4MA.js.map → chunk-UXFOGILU.js.map} +0 -0
  210. /package/dist/{chunk-OQMR2SDZ.js.map → chunk-VTR3MNYF.js.map} +0 -0
  211. /package/dist/{chunk-4QEUKASL.js.map → chunk-W25I7G6U.js.map} +0 -0
  212. /package/dist/{chunk-OOFBE62K.js.map → chunk-X7EJF46S.js.map} +0 -0
  213. /package/dist/{chunk-BL33LBTN.js.map → chunk-XG4NAWAV.js.map} +0 -0
  214. /package/dist/{chunk-ZZSXUZF3.js.map → chunk-YROCXMCK.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/wearables/trust.ts","../src/wearables/memory-gen.ts","../src/wearables/cleanup.ts","../src/wearables/sync-state.ts","../src/wearables/pipeline.ts"],"sourcesContent":["/**\n * Wearable memory trust scoring — the \"smart\" memoryMode engine.\n *\n * Grounded in how production memory systems handle noisy ingest:\n *\n * - **LLM-as-judge gating** (Remnic's own extraction judge,\n * `judgeFactDurability`): an LLM verdict on durability decides\n * accept / defer / reject before anything persists.\n * - **Provenance priors**: each source carries a configurable trust\n * prior (`sourceTrust`) — a noisy ASR channel contributes less\n * confidence than a clean one (provenance-weighted fusion).\n * - **Corroboration**: independent agreement raises trust. A fact\n * supported by a second wearable that recorded the same day, or by\n * an existing active memory, is far less likely to be an ASR\n * artifact (ensemble agreement / self-consistency).\n *\n * The combined score maps to a three-way decision:\n *\n * trust >= autoApproveTrust -> written ACTIVE\n * trust >= reviewTrust -> written pending_review\n * below -> dropped\n *\n * with the judge able to short-circuit (reject -> drop,\n * defer -> pending_review regardless of score). The score, verdict,\n * and corroboration evidence persist on the memory (confidence +\n * structuredAttributes + verificationState), so Remnic's existing\n * trust machinery — memory-worth outcome counters, pattern\n * reinforcement, temporal supersession, contradiction scans — keeps\n * calibrating these memories after they land.\n */\n\nimport {\n countRecallTokenOverlap,\n normalizeRecallTokens,\n} from \"../recall-tokenization.js\";\n\n/** Judge adjustments and corroboration boosts (documented, fixed). */\nexport const TRUST_JUDGE_ACCEPT_BOOST = 0.15;\nexport const TRUST_CROSS_SOURCE_BOOST = 0.15;\nexport const TRUST_SUPPORTING_MEMORY_BOOST = 0.1;\n\n/** Minimum distinct tokens before similarity is meaningful. */\nconst MIN_FACT_TOKENS = 4;\n/** Fact-token coverage required to call a day-text corroborating. */\nconst CROSS_SOURCE_COVERAGE = 0.6;\n/** Fact-token coverage required to call an existing memory supporting. */\nconst MEMORY_SUPPORT_COVERAGE = 0.7;\n/** Bound on existing memories scanned per fact batch. */\nconst MAX_MEMORIES_SCANNED = 5_000;\n\nexport interface TrustEvidence {\n /** Other-source ids whose same-day content corroborates the fact. */\n corroboratedBySources: string[];\n /** Id of an existing active memory whose content supports the fact. */\n supportingMemoryId?: string;\n}\n\nexport interface TrustScoreInput {\n /** Extraction confidence for the fact (defaults to 0.7 when absent). */\n extractionConfidence: number | undefined;\n /** Per-source trust prior from config (0..1). */\n sourceTrust: number;\n /** Judge verdict kind when a judge ran; undefined when unavailable. */\n judgeVerdict?: \"accept\" | \"reject\" | \"defer\";\n evidence: TrustEvidence;\n}\n\nexport function computeTrustScore(input: TrustScoreInput): number {\n const confidence =\n typeof input.extractionConfidence === \"number\" &&\n Number.isFinite(input.extractionConfidence)\n ? Math.min(1, Math.max(0, input.extractionConfidence))\n : 0.7;\n let trust = confidence * Math.min(1, Math.max(0, input.sourceTrust));\n if (input.judgeVerdict === \"accept\") trust += TRUST_JUDGE_ACCEPT_BOOST;\n if (input.evidence.corroboratedBySources.length > 0) {\n trust += TRUST_CROSS_SOURCE_BOOST;\n }\n if (input.evidence.supportingMemoryId !== undefined) {\n trust += TRUST_SUPPORTING_MEMORY_BOOST;\n }\n return Math.min(1, Math.max(0, trust));\n}\n\nexport interface CorroborationContext {\n /**\n * Same-day transcript bodies from OTHER sources, keyed by source id.\n * Pre-tokenized once per sync (day bodies are large).\n */\n otherSourceDayTokens: Map<string, Set<string>>;\n /** Existing active memories: id + content. */\n existingMemories: Array<{ id: string; content: string }>;\n}\n\n/** Tokenize a day body once for repeated per-fact coverage checks. */\nexport function tokenizeDayBody(body: string): Set<string> {\n return new Set(normalizeRecallTokens(body));\n}\n\n/**\n * Find corroborating evidence for one fact. Deterministic and local:\n * token-coverage similarity via the shared recall tokenizer — no LLM\n * cost on the corroboration path.\n */\nexport function findCorroboration(\n factText: string,\n context: CorroborationContext,\n): TrustEvidence {\n const factTokens = normalizeRecallTokens(factText);\n const evidence: TrustEvidence = { corroboratedBySources: [] };\n if (factTokens.length < MIN_FACT_TOKENS) return evidence;\n const factTokenSet = new Set(factTokens);\n\n for (const [sourceId, dayTokens] of context.otherSourceDayTokens) {\n let matches = 0;\n for (const token of factTokenSet) {\n if (dayTokens.has(token)) matches += 1;\n }\n if (matches / factTokenSet.size >= CROSS_SOURCE_COVERAGE) {\n evidence.corroboratedBySources.push(sourceId);\n }\n }\n evidence.corroboratedBySources.sort();\n\n let scanned = 0;\n for (const memory of context.existingMemories) {\n if (scanned >= MAX_MEMORIES_SCANNED) break;\n scanned += 1;\n const matches = countRecallTokenOverlap(factTokenSet, memory.content);\n if (matches / factTokenSet.size >= MEMORY_SUPPORT_COVERAGE) {\n evidence.supportingMemoryId = memory.id;\n break;\n }\n }\n return evidence;\n}\n\nexport interface SmartDecision {\n outcome: \"active\" | \"review\" | \"drop\";\n reason:\n | \"judge-rejected\"\n | \"judge-deferred\"\n | \"auto-approved\"\n | \"queued-for-review\"\n | \"below-trust\";\n trust: number;\n}\n\n/** Map judge verdict + trust score to the smart-mode decision. */\nexport function decideSmart(\n trust: number,\n judgeVerdict: \"accept\" | \"reject\" | \"defer\" | undefined,\n thresholds: { autoApproveTrust: number; reviewTrust: number },\n): SmartDecision {\n if (judgeVerdict === \"reject\") {\n return { outcome: \"drop\", reason: \"judge-rejected\", trust };\n }\n if (judgeVerdict === \"defer\") {\n return { outcome: \"review\", reason: \"judge-deferred\", trust };\n }\n if (trust >= thresholds.autoApproveTrust) {\n return { outcome: \"active\", reason: \"auto-approved\", trust };\n }\n if (trust >= thresholds.reviewTrust) {\n return { outcome: \"review\", reason: \"queued-for-review\", trust };\n }\n return { outcome: \"drop\", reason: \"below-trust\", trust };\n}\n","/**\n * Wearable memory generation — trust-gated extraction from cleaned day\n * transcripts.\n *\n * Wearable ASR quality varies wildly between providers and rooms, so\n * unlike live-session extraction this path is gated per source:\n *\n * - memoryMode \"off\" -> never runs\n * - memoryMode \"review\" -> candidates land with status\n * \"pending_review\" (operator approves via\n * the existing review-queue surfaces)\n * - memoryMode \"auto\" -> candidates that pass every gate land\n * active\n *\n * Deterministic gates (applied in order, all modes except \"off\"):\n * 1. category gate — procedure / reasoning_trace candidates are\n * skipped (they need richer persistence than\n * this path provides)\n * 2. confidence floor — `minConfidence`\n * 3. importance floor — local `scoreImportance` >= `minImportance`\n * 4. dedup — storage content-hash index + intra-run set\n * 5. day cap — top `maxMemoriesPerDay` by importance score\n * (0 disables the cap)\n *\n * The extraction engine itself is injected so this module stays free of\n * LLM-client construction; callers hand in `orchestrator`-owned or\n * standalone engines alike.\n */\n\nimport { scoreImportance } from \"../importance.js\";\nimport type { JudgeBatchResult, JudgeCandidate } from \"../extraction-judge.js\";\nimport { getVerdictKind } from \"../extraction-judge.js\";\nimport { describeErrorForOperator } from \"./errors.js\";\nimport {\n computeTrustScore,\n decideSmart,\n findCorroboration,\n type CorroborationContext,\n type TrustEvidence,\n} from \"./trust.js\";\nimport type {\n BufferTurn,\n ExtractedFact,\n ExtractionResult,\n ImportanceLevel,\n ImportanceScore,\n MemoryCategory,\n MemoryStatus,\n} from \"../types.js\";\nimport { resolveSpeaker, type SpeakerRegistry } from \"./speakers.js\";\nimport type {\n WearableConversation,\n WearableMemoryMode,\n WearableNativeMemory,\n WearableSourceSettings,\n} from \"./types.js\";\n\n/**\n * Memory status used for a given memory mode. Least-privilege default:\n * anything that is not explicitly \"auto\" lands in the review queue\n * rather than active recall.\n */\nexport function memoryStatusForMode(\n mode: WearableMemoryMode,\n): Extract<MemoryStatus, \"active\" | \"pending_review\"> {\n return mode === \"auto\" || mode === \"smart\" ? \"active\" : \"pending_review\";\n}\n\n/** Narrow writer interface satisfied by `StorageManager`. */\nexport interface WearableMemoryWriter {\n writeMemory(\n category: MemoryCategory,\n content: string,\n options: {\n confidence?: number;\n tags?: string[];\n source?: string;\n importance?: ImportanceScore;\n validAt?: string;\n structuredAttributes?: Record<string, string>;\n contentHashSource?: string;\n status?: MemoryStatus;\n memoryKind?: \"episode\" | \"note\" | \"box\" | \"dream\" | \"procedural\";\n },\n ): Promise<string>;\n hasFactContentHash(content: string): Promise<boolean>;\n /**\n * Locate an earlier wearable write of the same content (any status).\n * Optional: enables in-place promotion when re-scoring with stronger\n * evidence.\n */\n findWearableMemoryByContent?(\n content: string,\n ): Promise<{ id: string; status: MemoryStatus | undefined } | null>;\n /**\n * Promote a pending_review wearable memory to active, merging trust\n * evidence. Returns false when missing or no longer pending.\n */\n promoteWearableMemory?(\n id: string,\n attributeUpdates: Record<string, string>,\n confidence?: number,\n ): Promise<boolean>;\n /**\n * Demote a pending_review wearable memory to rejected on an explicit\n * judge-reject re-verdict. Returns false when missing or no longer\n * pending. Active rows are never auto-demoted.\n */\n demoteWearableMemory?(\n id: string,\n attributeUpdates: Record<string, string>,\n ): Promise<boolean>;\n}\n\nexport interface WearableMemoryGenDeps {\n extract(turns: BufferTurn[]): Promise<ExtractionResult>;\n writer: WearableMemoryWriter;\n /**\n * LLM-as-judge batch evaluation (the existing extraction judge).\n * Absent when no judge is wired (degraded smart mode: trust scoring\n * runs on confidence x sourceTrust + corroboration alone).\n */\n judgeFacts?(candidates: JudgeCandidate[]): Promise<JudgeBatchResult>;\n /**\n * Corroboration evidence for smart mode: other sources' same-day\n * transcript tokens + existing active memories. Absent disables\n * corroboration boosts.\n */\n corroboration?: CorroborationContext;\n}\n\nexport interface WearableMemoryGenResult {\n created: number;\n /** Earlier borderline writes promoted to active by new evidence. */\n promoted: number;\n /** Earlier pending writes retired by a fresh judge-reject verdict. */\n demoted: number;\n skipped: number;\n skippedByReason: Record<string, number>;\n /** Non-fatal problems (e.g. the extraction engine erroring). */\n warnings: string[];\n /**\n * True when every conversation was extracted (the pass may still\n * carry degraded-mode warnings, e.g. judge unavailable). False only\n * when extraction itself aborted mid-day — the signal callers use to\n * re-run the pass on the next sync. A degraded-but-complete pass\n * must NOT re-run forever: its facts are already written and dedup\n * would suppress improvements anyway.\n */\n completed: boolean;\n}\n\nexport const WEARABLE_SOURCE_PREFIX = \"wearable\";\n\nexport function wearableSourceLabel(sourceId: string): string {\n return `${WEARABLE_SOURCE_PREFIX}:${sourceId}`;\n}\n\nexport function wearableDayTag(date: string): string {\n return `wearable-day:${date}`;\n}\n\nconst IMPORTANCE_RANK: Record<ImportanceLevel, number> = {\n trivial: 0,\n low: 1,\n normal: 2,\n high: 3,\n critical: 4,\n};\n\n/** Max characters of transcript per extraction turn. */\nconst MAX_EXTRACTION_CHUNK_CHARS = 6_000;\n/** Skip extraction for conversations with less substance than this. */\nconst MIN_CONVERSATION_CHARS = 80;\n\n/**\n * Render a cleaned conversation into extraction-ready turns. Each turn\n * carries a labeled multi-speaker transcript block; the wearer is\n * marked \"(you)\" so first-person facts attribute correctly.\n */\nexport function buildExtractionTurns(\n sourceId: string,\n date: string,\n conversation: WearableConversation,\n registry: SpeakerRegistry,\n): BufferTurn[] {\n const headerParts = [\n `Wearable transcript (${sourceId}) — ${date}`,\n conversation.title ? `\"${conversation.title}\"` : undefined,\n conversation.location ? `at ${conversation.location}` : undefined,\n ].filter((part): part is string => typeof part === \"string\");\n const header = `[${headerParts.join(\" — \")}]`;\n\n const lines: string[] = [];\n for (const segment of conversation.segments) {\n const { label } = resolveSpeaker(sourceId, segment, registry);\n lines.push(`${label}: ${segment.text}`);\n }\n const transcript = lines.join(\"\\n\");\n if (transcript.trim().length < MIN_CONVERSATION_CHARS) return [];\n\n const sessionKey = `wearables:${sourceId}:${date}:${conversation.id}`;\n const timestamp = conversation.startIso;\n const turns: BufferTurn[] = [];\n let chunkLines: string[] = [];\n let chunkChars = 0;\n const flush = () => {\n if (chunkLines.length === 0) return;\n turns.push({\n role: \"user\",\n content: `${header}\\n${chunkLines.join(\"\\n\")}`,\n timestamp,\n sourceValidAt: timestamp,\n sessionKey,\n });\n chunkLines = [];\n chunkChars = 0;\n };\n for (const line of transcript.split(\"\\n\")) {\n if (chunkChars + line.length + 1 > MAX_EXTRACTION_CHUNK_CHARS) flush();\n chunkLines.push(line);\n chunkChars += line.length + 1;\n }\n flush();\n return turns;\n}\n\ninterface GatedCandidate {\n fact: ExtractedFact;\n importance: ImportanceScore;\n conversation: WearableConversation;\n}\n\ninterface ScoredCandidate {\n trust: number;\n verdict?: \"accept\" | \"reject\" | \"defer\";\n evidence: TrustEvidence;\n}\n\n/**\n * Smart-mode scoring: one judge batch call for the whole day, then\n * per-fact trust = confidence x sourceTrust + judge/corroboration\n * boosts. A judge failure degrades gracefully (warned once; scoring\n * continues on confidence + corroboration alone).\n */\nasync function scoreCandidates(\n novel: GatedCandidate[],\n settings: WearableSourceSettings,\n deps: WearableMemoryGenDeps,\n result: WearableMemoryGenResult,\n): Promise<Map<number, ScoredCandidate>> {\n const scored = new Map<number, ScoredCandidate>();\n if (novel.length === 0) return scored;\n\n let verdicts: Map<number, \"accept\" | \"reject\" | \"defer\"> | undefined;\n if (deps.judgeFacts) {\n const judgeCandidates: JudgeCandidate[] = novel.map((candidate) => ({\n text: candidate.fact.content,\n category: candidate.fact.category,\n confidence:\n typeof candidate.fact.confidence === \"number\"\n ? candidate.fact.confidence\n : 0.7,\n tags: candidate.fact.tags ?? [],\n importanceLevel: candidate.importance.level,\n }));\n try {\n const judgeResult = await deps.judgeFacts(judgeCandidates);\n verdicts = new Map();\n for (const [index, verdict] of judgeResult.verdicts) {\n verdicts.set(index, getVerdictKind(verdict));\n }\n } catch (err) {\n result.warnings.push(\n `extraction judge unavailable for this pass: ${describeErrorForOperator(err)} — trust scoring continued without judge verdicts`,\n );\n }\n }\n\n const corroboration: CorroborationContext = deps.corroboration ?? {\n otherSourceDayTokens: new Map(),\n existingMemories: [],\n };\n novel.forEach((candidate, index) => {\n const evidence = findCorroboration(candidate.fact.content, corroboration);\n const verdict = verdicts?.get(index);\n const trust = computeTrustScore({\n extractionConfidence: candidate.fact.confidence,\n sourceTrust: settings.sourceTrust,\n judgeVerdict: verdict,\n evidence,\n });\n scored.set(index, { trust, ...(verdict !== undefined ? { verdict } : {}), evidence });\n });\n return scored;\n}\n\n/**\n * Run extraction + gates over a day's conversations and persist the\n * survivors. Returns counts for the sync summary.\n */\nexport async function generateWearableMemories(\n sourceId: string,\n date: string,\n conversations: WearableConversation[],\n settings: WearableSourceSettings,\n registry: SpeakerRegistry,\n deps: WearableMemoryGenDeps,\n): Promise<WearableMemoryGenResult> {\n const result: WearableMemoryGenResult = {\n created: 0,\n promoted: 0,\n demoted: 0,\n skipped: 0,\n skippedByReason: {},\n warnings: [],\n completed: true,\n };\n if (settings.memoryMode === \"off\") return result;\n\n const skip = (reason: string, count = 1): void => {\n result.skipped += count;\n result.skippedByReason[reason] =\n (result.skippedByReason[reason] ?? 0) + count;\n };\n\n const candidates: GatedCandidate[] = [];\n const seenContent = new Set<string>();\n\n for (const conversation of conversations) {\n const turns = buildExtractionTurns(sourceId, date, conversation, registry);\n if (turns.length === 0) continue;\n let extraction: ExtractionResult;\n try {\n extraction = await deps.extract(turns);\n } catch (err) {\n // One failing extraction call almost always means every call will\n // fail (missing key, provider outage) — stop hammering the engine\n // and surface a single actionable warning instead of one per\n // conversation. Candidates gathered before the failure still\n // persist below.\n result.warnings.push(\n `extraction failed for ${sourceId}/${date} (conversation ${conversation.id}): ${describeErrorForOperator(err)} — the memory pass for this day retries on the next sync`,\n );\n result.completed = false;\n break;\n }\n for (const fact of extraction.facts) {\n const content = fact.content?.trim();\n if (!content) {\n skip(\"empty\");\n continue;\n }\n if (fact.category === \"procedure\" || fact.category === \"reasoning_trace\") {\n skip(\"unsupported-category\");\n continue;\n }\n // In smart mode the trust bands subsume the hard confidence\n // floor — a borderline fact belongs in the review band, not on\n // the floor. The pre-filter applies to review/auto modes only.\n if (\n settings.memoryMode !== \"smart\" &&\n typeof fact.confidence === \"number\" &&\n fact.confidence < settings.minConfidence\n ) {\n skip(\"below-confidence\");\n continue;\n }\n const importance = scoreImportance(content, fact.category, fact.tags ?? []);\n if (\n IMPORTANCE_RANK[importance.level] <\n IMPORTANCE_RANK[settings.minImportance]\n ) {\n skip(\"below-importance\");\n continue;\n }\n const dedupKey = content.toLowerCase();\n if (seenContent.has(dedupKey)) {\n skip(\"duplicate-in-run\");\n continue;\n }\n seenContent.add(dedupKey);\n candidates.push({ fact: { ...fact, content }, importance, conversation });\n }\n }\n\n // Drop candidates that already exist in storage BEFORE applying the\n // day cap so duplicates never consume cap slots that novel,\n // lower-scoring candidates should get (Codex P2 on PR #1458). In\n // smart mode a duplicate of a PENDING_REVIEW write is kept aside as\n // a promotion candidate — corroboration that arrives after the\n // original borderline write (another device syncing the same day)\n // must be able to promote it in place (Cursor review on PR #1462).\n const novel: GatedCandidate[] = [];\n const promotable: GatedCandidate[] = [];\n for (const candidate of candidates) {\n if (await deps.writer.hasFactContentHash(candidate.fact.content)) {\n if (\n settings.memoryMode === \"smart\" &&\n deps.writer.findWearableMemoryByContent !== undefined &&\n deps.writer.promoteWearableMemory !== undefined\n ) {\n promotable.push(candidate);\n } else {\n skip(\"duplicate-existing\");\n }\n continue;\n }\n novel.push(candidate);\n }\n\n // Re-score promotion candidates with TODAY'S evidence; promote the\n // ones that now clear the auto threshold. Never consumes day-cap\n // slots (no new memory is written).\n if (promotable.length > 0) {\n const promoteScores = await scoreCandidates(promotable, settings, deps, result);\n for (const [index, candidate] of promotable.entries()) {\n const scored = promoteScores.get(index);\n const decision = scored\n ? decideSmart(scored.trust, scored.verdict, settings)\n : undefined;\n if (!scored || !decision) {\n skip(\"duplicate-existing\");\n continue;\n }\n // A fresh judge-REJECT retires the stored row — but only a\n // pending_review one. Active rows are never auto-demoted: an\n // operator approval or accrued recall signals must not be\n // overturned by one later LLM verdict; contradiction scans and\n // supersession own active-row retirement (Cursor review on PR\n // #1462, round 7).\n if (scored.verdict === \"reject\") {\n if (deps.writer.demoteWearableMemory !== undefined) {\n const existingForDemote = await deps.writer.findWearableMemoryByContent!(\n candidate.fact.content,\n );\n if (\n existingForDemote &&\n existingForDemote.status === \"pending_review\" &&\n (await deps.writer.demoteWearableMemory(existingForDemote.id, {\n trustScore: scored.trust.toFixed(3),\n trustDecision: \"demoted-by-rejection\",\n judgeVerdict: \"reject\",\n }))\n ) {\n result.demoted += 1;\n continue;\n }\n }\n skip(\"duplicate-existing\");\n continue;\n }\n if (decision.outcome !== \"active\") {\n skip(\"duplicate-existing\");\n continue;\n }\n const existing = await deps.writer.findWearableMemoryByContent!(\n candidate.fact.content,\n );\n if (!existing || existing.status !== \"pending_review\") {\n skip(\"duplicate-existing\");\n continue;\n }\n const promoted = await deps.writer.promoteWearableMemory!(\n existing.id,\n {\n trustScore: scored.trust.toFixed(3),\n trustDecision: \"promoted-by-corroboration\",\n ...(scored.verdict !== undefined ? { judgeVerdict: scored.verdict } : {}),\n ...(scored.evidence.corroboratedBySources.length > 0\n ? { corroboratedBySources: scored.evidence.corroboratedBySources.join(\",\") }\n : {}),\n ...(scored.evidence.supportingMemoryId !== undefined\n ? { supportingMemoryId: scored.evidence.supportingMemoryId }\n : {}),\n },\n scored.trust,\n );\n if (promoted) {\n result.promoted += 1;\n } else {\n skip(\"duplicate-existing\");\n }\n }\n }\n\n // Smart mode: judge + trust scoring decide active/review/drop per\n // fact. The judge runs ONE batch call for the whole day; trust\n // combines extraction confidence x sourceTrust with corroboration\n // boosts (cross-device agreement, existing-memory support).\n let trustById = new Map<number, ScoredCandidate>();\n if (settings.memoryMode === \"smart\") {\n trustById = await scoreCandidates(novel, settings, deps, result);\n }\n\n // Smart decisions run BEFORE the day cap so dropped facts (judge\n // rejections, below-trust) never consume cap slots that surviving\n // candidates ranked past position N should get (Cursor review on PR\n // #1462).\n interface Writable {\n candidate: GatedCandidate;\n index: number;\n status: MemoryStatus;\n trustAttributes: Record<string, string>;\n }\n const modeStatus = memoryStatusForMode(settings.memoryMode);\n const writable: Writable[] = [];\n novel.forEach((candidate, index) => {\n if (settings.memoryMode !== \"smart\") {\n writable.push({ candidate, index, status: modeStatus, trustAttributes: {} });\n return;\n }\n const scored = trustById.get(index);\n if (!scored) return;\n const decision = decideSmart(scored.trust, scored.verdict, settings);\n if (decision.outcome === \"drop\") {\n skip(decision.reason);\n return;\n }\n writable.push({\n candidate,\n index,\n status: decision.outcome === \"active\" ? \"active\" : \"pending_review\",\n trustAttributes: {\n trustScore: scored.trust.toFixed(3),\n trustDecision: decision.reason,\n ...(scored.verdict !== undefined ? { judgeVerdict: scored.verdict } : {}),\n ...(scored.evidence.corroboratedBySources.length > 0\n ? { corroboratedBySources: scored.evidence.corroboratedBySources.join(\",\") }\n : {}),\n ...(scored.evidence.supportingMemoryId !== undefined\n ? { supportingMemoryId: scored.evidence.supportingMemoryId }\n : {}),\n },\n });\n });\n\n // Day cap over the SURVIVORS: strongest by trust in smart mode, by\n // importance otherwise. Stable ordering with a content tiebreak.\n const strength = (entry: Writable): number =>\n settings.memoryMode === \"smart\"\n ? trustById.get(entry.index)?.trust ?? 0\n : entry.candidate.importance.score;\n writable.sort((a, b) => {\n const sa = strength(a);\n const sb = strength(b);\n if (sa > sb) return -1;\n if (sa < sb) return 1;\n if (a.candidate.fact.content < b.candidate.fact.content) return -1;\n if (a.candidate.fact.content > b.candidate.fact.content) return 1;\n return 0;\n });\n const cap = settings.maxMemoriesPerDay;\n const kept = cap > 0 ? writable.slice(0, cap) : writable;\n if (writable.length > kept.length) {\n skip(\"over-day-cap\", writable.length - kept.length);\n }\n\n for (const { candidate, index, status, trustAttributes } of kept) {\n const tags = [\n ...new Set([\n ...(candidate.fact.tags ?? []),\n WEARABLE_SOURCE_PREFIX,\n wearableSourceLabel(sourceId),\n wearableDayTag(date),\n ]),\n ];\n await deps.writer.writeMemory(candidate.fact.category, candidate.fact.content, {\n confidence:\n settings.memoryMode === \"smart\"\n ? trustById.get(index)?.trust\n : candidate.fact.confidence,\n tags,\n source: wearableSourceLabel(sourceId),\n importance: candidate.importance,\n validAt: candidate.conversation.startIso,\n structuredAttributes: {\n ...(candidate.fact.structuredAttributes ?? {}),\n wearableSource: sourceId,\n wearableDate: date,\n wearableConversationId: candidate.conversation.id,\n ...trustAttributes,\n },\n contentHashSource: candidate.fact.content,\n status,\n });\n result.created += 1;\n }\n return result;\n}\n\n/**\n * Write one compact daily-digest memory summarizing the day's recorded\n * conversations. Deterministic (no LLM): titles, time ranges, speaker\n * counts. Gated by `wearables.digestEnabled`.\n */\nexport async function writeDailyDigestMemory(\n sourceId: string,\n date: string,\n conversations: WearableConversation[],\n settings: WearableSourceSettings,\n registry: SpeakerRegistry,\n writer: WearableMemoryWriter,\n): Promise<boolean> {\n if (settings.memoryMode === \"off\") return false;\n if (conversations.length === 0) return false;\n const lines = conversations.map((conversation) => {\n const title = conversation.title?.trim() || \"Untitled conversation\";\n const speakers = new Set(\n conversation.segments.map(\n (segment) => resolveSpeaker(sourceId, segment, registry).label,\n ),\n );\n return `- ${title} (${speakers.size} speaker${speakers.size === 1 ? \"\" : \"s\"})`;\n });\n const content =\n `Wearable day digest — ${sourceId}, ${date}: ` +\n `${conversations.length} recorded conversation${conversations.length === 1 ? \"\" : \"s\"}.\\n` +\n lines.join(\"\\n\");\n if (await writer.hasFactContentHash(content)) return false;\n await writer.writeMemory(\"moment\", content, {\n confidence: 0.9,\n tags: [\n WEARABLE_SOURCE_PREFIX,\n wearableSourceLabel(sourceId),\n wearableDayTag(date),\n \"daily-digest\",\n ],\n source: wearableSourceLabel(sourceId),\n importance: scoreImportance(content, \"moment\", [\"daily-digest\"]),\n validAt: `${date}T00:00:00.000Z`,\n structuredAttributes: {\n wearableSource: sourceId,\n wearableDate: date,\n },\n contentHashSource: content,\n status: memoryStatusForMode(settings.memoryMode),\n memoryKind: \"episode\",\n });\n return true;\n}\n\n/**\n * Import provider-extracted memories (Bee facts, Omi memories) into the\n * review queue. Always `pending_review` regardless of memoryMode — the\n * provider's extraction quality is outside Remnic's control.\n */\n/** Native-source trust prior reduction (provider extraction quality). */\nconst NATIVE_TRUST_FACTOR = 0.9;\n\nexport async function importNativeMemories(\n sourceId: string,\n memories: WearableNativeMemory[],\n alreadyImportedIds: ReadonlySet<string>,\n settings: WearableSourceSettings,\n deps: WearableMemoryGenDeps,\n): Promise<{ imported: number; importedIds: string[]; warnings: string[] }> {\n let imported = 0;\n const importedIds: string[] = [];\n const warnings: string[] = [];\n const seenContent = new Set<string>();\n const smart = settings.importNativeMemories === \"smart\";\n\n // Smart path: one judge batch over the novel items, like transcript\n // facts, with a reduced source prior — provider extraction quality is\n // outside Remnic's control.\n const novel: WearableNativeMemory[] = [];\n for (const memory of memories) {\n const content = memory.content?.trim();\n if (!content) continue;\n if (alreadyImportedIds.has(memory.id)) continue;\n // Intra-run + cross-run dedup: the storage hash index only learns a\n // fact after its write lands, so same-content items within one page\n // batch need the local set.\n if (seenContent.has(content) || (await deps.writer.hasFactContentHash(content))) {\n importedIds.push(memory.id);\n continue;\n }\n seenContent.add(content);\n novel.push({ ...memory, content });\n }\n\n let verdicts: Map<number, \"accept\" | \"reject\" | \"defer\"> | undefined;\n if (smart && deps.judgeFacts && novel.length > 0) {\n try {\n const judgeResult = await deps.judgeFacts(\n novel.map((memory) => ({\n text: memory.content,\n category: \"fact\",\n confidence: 0.7,\n tags: memory.tags ?? [],\n })),\n );\n verdicts = new Map();\n for (const [index, verdict] of judgeResult.verdicts) {\n verdicts.set(index, getVerdictKind(verdict));\n }\n } catch (err) {\n warnings.push(\n `extraction judge unavailable for native import: ${describeErrorForOperator(err)} — trust scoring continued without judge verdicts`,\n );\n }\n }\n const corroboration: CorroborationContext = deps.corroboration ?? {\n otherSourceDayTokens: new Map(),\n existingMemories: [],\n };\n\n for (const [index, memory] of novel.entries()) {\n const content = memory.content;\n let status: MemoryStatus = \"pending_review\";\n let trustAttributes: Record<string, string> = {};\n let confidence = 0.6;\n if (smart) {\n const evidence = findCorroboration(content, corroboration);\n const verdict = verdicts?.get(index);\n const trust = computeTrustScore({\n extractionConfidence: undefined,\n sourceTrust: settings.sourceTrust * NATIVE_TRUST_FACTOR,\n judgeVerdict: verdict,\n evidence,\n });\n const decision = decideSmart(trust, verdict, settings);\n if (decision.outcome === \"drop\") {\n // Deliberately NOT recorded in importedIds: a dropped native\n // fact re-fetches and re-scores on later syncs, so corpus or\n // corroboration support that arrives later can still admit it.\n // The judge verdict cache keeps repeated rejections cheap\n // (Cursor review on PR #1462).\n continue;\n }\n status = decision.outcome === \"active\" ? \"active\" : \"pending_review\";\n confidence = trust;\n trustAttributes = {\n trustScore: trust.toFixed(3),\n trustDecision: decision.reason,\n ...(verdict !== undefined ? { judgeVerdict: verdict } : {}),\n ...(evidence.corroboratedBySources.length > 0\n ? { corroboratedBySources: evidence.corroboratedBySources.join(\",\") }\n : {}),\n ...(evidence.supportingMemoryId !== undefined\n ? { supportingMemoryId: evidence.supportingMemoryId }\n : {}),\n };\n }\n await deps.writer.writeMemory(\"fact\", content, {\n confidence,\n tags: [\n ...new Set([\n ...(memory.tags ?? []),\n WEARABLE_SOURCE_PREFIX,\n wearableSourceLabel(sourceId),\n \"native-import\",\n ]),\n ],\n source: `${wearableSourceLabel(sourceId)}:native`,\n importance: scoreImportance(content, \"fact\", memory.tags ?? []),\n validAt: memory.createdIso,\n structuredAttributes: {\n wearableSource: sourceId,\n wearableNativeId: memory.id,\n ...trustAttributes,\n },\n contentHashSource: content,\n status,\n });\n imported += 1;\n importedIds.push(memory.id);\n }\n return { imported, importedIds, warnings };\n}\n","/**\n * Wearable transcript cleanup — deterministic, zero-LLM normalization.\n *\n * ASR output from always-on wearables is noisy: fragmented utterances,\n * filler tokens, stuttered repeats, and occasional pure garbage. This\n * module cleans a conversation in place-order without changing meaning:\n * everything here is conservative and reversible by re-syncing.\n */\n\nimport type {\n WearableCleanupSettings,\n WearableConversation,\n WearableTranscriptSegment,\n} from \"./types.js\";\n\nexport interface CleanupResult {\n conversation: WearableConversation;\n /** Segments removed by the low-quality heuristic. */\n droppedSegments: number;\n /** Segments merged into a predecessor. */\n mergedSegments: number;\n}\n\n/** Merge consecutive same-speaker segments when gaps are below this. */\nconst MERGE_GAP_MS = 30_000;\n\n/**\n * Standalone filler tokens stripped when `stripFillers` is on. Matched\n * case-insensitively on word boundaries, only as whole tokens — \"um\"\n * inside \"umbrella\" is never touched. Deliberately short, low-risk\n * list; meaning-bearing hedges (\"like\", \"well\") are NOT stripped.\n */\nconst FILLER_TOKENS = [\"um\", \"uh\", \"uhm\", \"umm\", \"uhh\", \"erm\", \"hmm\", \"mhm\"];\n\nconst FILLER_PATTERN = new RegExp(\n // Leading/trailing punctuation around the filler collapses with it so\n // \"Um, so we should\" -> \"so we should\" rather than \", so we should\".\n `(?:^|\\\\s)(?:${FILLER_TOKENS.join(\"|\")})[,.]?(?=\\\\s|$)`,\n \"gi\",\n);\n\n/** Apply configured cleanup passes to one conversation. */\nexport function cleanConversation(\n conversation: WearableConversation,\n settings: WearableCleanupSettings,\n): CleanupResult {\n let segments = conversation.segments.map((segment) => ({ ...segment }));\n let droppedSegments = 0;\n let mergedSegments = 0;\n\n if (settings.stripFillers) {\n for (const segment of segments) {\n segment.text = stripFillerTokens(segment.text);\n }\n }\n\n if (settings.collapseRepeats) {\n for (const segment of segments) {\n segment.text = collapseImmediateRepeats(segment.text);\n }\n }\n\n for (const segment of segments) {\n segment.text = normalizeWhitespace(segment.text);\n }\n\n if (settings.dropLowQuality) {\n const kept: WearableTranscriptSegment[] = [];\n for (const segment of segments) {\n if (isLowQualitySegment(segment.text)) {\n droppedSegments += 1;\n } else {\n kept.push(segment);\n }\n }\n segments = kept;\n } else {\n // Even without the quality heuristic, segments whose text became\n // empty after filler stripping carry no information.\n const kept = segments.filter((segment) => segment.text.length > 0);\n droppedSegments += segments.length - kept.length;\n segments = kept;\n }\n\n if (settings.mergeSameSpeaker) {\n const merged: WearableTranscriptSegment[] = [];\n for (const segment of segments) {\n const previous = merged[merged.length - 1];\n if (previous && canMerge(previous, segment)) {\n previous.text = `${previous.text} ${segment.text}`.trim();\n if (segment.endIso) previous.endIso = segment.endIso;\n mergedSegments += 1;\n } else {\n merged.push(segment);\n }\n }\n segments = merged;\n }\n\n return {\n conversation: { ...conversation, segments },\n droppedSegments,\n mergedSegments,\n };\n}\n\nfunction canMerge(\n previous: WearableTranscriptSegment,\n next: WearableTranscriptSegment,\n): boolean {\n if (previous.speakerKey !== next.speakerKey) return false;\n const previousEnd = previous.endIso ? Date.parse(previous.endIso) : NaN;\n const nextStart = next.startIso ? Date.parse(next.startIso) : NaN;\n // Without timestamps, adjacency is the only signal — still merge.\n if (Number.isNaN(previousEnd) || Number.isNaN(nextStart)) return true;\n return nextStart - previousEnd <= MERGE_GAP_MS;\n}\n\nexport function stripFillerTokens(text: string): string {\n return normalizeWhitespace(text.replace(FILLER_PATTERN, \" \"));\n}\n\n/**\n * Collapse immediate word/phrase stutters: \"I I I think\" -> \"I think\",\n * \"we should we should go\" -> \"we should go\". Only collapses *adjacent*\n * repeats (up to 4-word phrases) so intentional repetition across a\n * sentence is preserved.\n */\nexport function collapseImmediateRepeats(text: string): string {\n const words = text.split(/\\s+/).filter((word) => word.length > 0);\n if (words.length < 2) return text.trim();\n const out: string[] = [];\n let index = 0;\n while (index < words.length) {\n out.push(words[index]);\n index += 1;\n // Greedily consume every adjacent repeat of the phrase that just\n // ended at the output tail (largest phrase first, then re-check so\n // \"I I I think\" fully collapses to \"I think\").\n let matched = true;\n while (matched) {\n matched = false;\n for (let size = 4; size >= 1; size--) {\n if (out.length < size || index + size > words.length) continue;\n const tail = out.slice(-size).join(\" \").toLowerCase();\n // Spoken digit sequences legitimately repeat (\"555 555 1234\");\n // never collapse a phrase that carries no letters.\n if (!/\\p{L}/u.test(tail)) continue;\n const ahead = words.slice(index, index + size).join(\" \").toLowerCase();\n if (tail === ahead) {\n index += size;\n matched = true;\n break;\n }\n }\n }\n }\n return out.join(\" \");\n}\n\n/**\n * Heuristic ASR-garbage detector. Intentionally conservative: it only\n * drops segments that carry no plausible information.\n */\nexport function isLowQualitySegment(text: string): boolean {\n const trimmed = text.trim();\n if (trimmed.length === 0) return true;\n // Single repeated character runs (\"aaaaaa\", \"######\").\n if (/^(.)\\1{4,}$/.test(trimmed)) return true;\n // Mostly non-letter content with no digits (timestamps/amounts are\n // information; \"%$#@!\" is not).\n const letters = trimmed.replace(/[^\\p{L}\\p{N}]/gu, \"\");\n if (letters.length === 0) return true;\n if (trimmed.length >= 12 && letters.length / trimmed.length < 0.3) {\n return true;\n }\n // One identical token repeated many times (\"yeah yeah yeah yeah yeah\").\n const words = trimmed.toLowerCase().split(/\\s+/);\n if (words.length >= 5) {\n const unique = new Set(words);\n if (unique.size === 1) return true;\n }\n return false;\n}\n\nexport function normalizeWhitespace(text: string): string {\n return text.replace(/\\s+/g, \" \").trim();\n}\n","/**\n * Wearable sync-state ledger — per-source bookkeeping for incremental\n * syncs, stored at `state/wearables/sync.json`.\n *\n * State is written only AFTER a source's transcripts (and any memories)\n * were persisted successfully, so a failed sync never advances the\n * watermark past data that didn't land.\n */\n\nimport { promises as fsPromises } from \"node:fs\";\nimport * as path from \"node:path\";\n\nexport interface WearableSourceSyncState {\n /** ISO timestamp of the last successful sync run. */\n lastSyncAt: string;\n /** Most recent day (YYYY-MM-DD) that was synced. */\n lastDateSynced: string;\n /** Body hashes of the last-written day files, keyed by date. */\n dayHashes: Record<string, string>;\n /**\n * Body hashes of days whose memory-extraction pass completed without\n * warnings, keyed by date. A day whose entry is missing or stale\n * re-extracts on the next sync even when its transcript is unchanged\n * — this is how a memory pass that failed mid-run (transcript stored,\n * memories incomplete) self-heals. Absent on records written before\n * this field existed.\n */\n memoryDayHashes?: Record<string, string>;\n /** Native-memory ids already imported (bounded, newest kept). */\n importedNativeMemoryIds: string[];\n}\n\nexport interface WearableSyncStateFile {\n version: 1;\n sources: Record<string, WearableSourceSyncState>;\n}\n\n/** Cap on remembered native-memory ids per source. */\nconst MAX_TRACKED_NATIVE_IDS = 5_000;\n/** Cap on remembered day hashes per source (~2 years of days). */\nconst MAX_TRACKED_DAY_HASHES = 800;\n\nexport function syncStateFilePath(memoryDir: string): string {\n return path.join(memoryDir, \"state\", \"wearables\", \"sync.json\");\n}\n\nexport function emptySyncState(): WearableSyncStateFile {\n return { version: 1, sources: {} };\n}\n\nexport async function loadSyncState(\n memoryDir: string,\n): Promise<WearableSyncStateFile> {\n const filePath = syncStateFilePath(memoryDir);\n let raw: string;\n try {\n raw = await fsPromises.readFile(filePath, \"utf-8\");\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n return emptySyncState();\n }\n throw err;\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n // A corrupt state file should not brick syncing forever; treat it\n // as a cold start (the worst case is re-syncing days we already\n // have, which the day-hash skip makes cheap and idempotent).\n return emptySyncState();\n }\n if (\n typeof parsed !== \"object\" ||\n parsed === null ||\n Array.isArray(parsed) ||\n typeof (parsed as WearableSyncStateFile).sources !== \"object\" ||\n (parsed as WearableSyncStateFile).sources === null\n ) {\n return emptySyncState();\n }\n return { version: 1, sources: (parsed as WearableSyncStateFile).sources };\n}\n\nexport async function saveSyncState(\n memoryDir: string,\n state: WearableSyncStateFile,\n): Promise<void> {\n const filePath = syncStateFilePath(memoryDir);\n await fsPromises.mkdir(path.dirname(filePath), { recursive: true });\n const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now().toString(36)}`;\n await fsPromises.writeFile(\n tmpPath,\n `${JSON.stringify(state, null, 2)}\\n`,\n \"utf-8\",\n );\n try {\n await fsPromises.rename(tmpPath, filePath);\n } catch (err) {\n await fsPromises.unlink(tmpPath).catch(() => undefined);\n throw err;\n }\n}\n\n/** Merge a completed source sync into the state file shape. */\nexport function updateSourceSyncState(\n state: WearableSyncStateFile,\n sourceId: string,\n update: {\n syncedAt: string;\n days: string[];\n dayHashes: Record<string, string>;\n /** Days whose memory pass completed cleanly this run. */\n memoryDayHashes?: Record<string, string>;\n /**\n * Days whose memory pass ran this sync but did NOT complete\n * cleanly. Their previous completion records are removed so a\n * stale hash from an earlier clean pass can never mask the\n * failure on the next sync.\n */\n clearMemoryDays?: string[];\n importedNativeMemoryIds?: string[];\n },\n): WearableSyncStateFile {\n const previous = state.sources[sourceId];\n const mergedHashes: Record<string, string> = {\n ...(previous?.dayHashes ?? {}),\n ...update.dayHashes,\n };\n // Bound the hash map: keep the lexicographically-largest (most\n // recent) dates, which sort naturally for YYYY-MM-DD keys.\n const hashKeys = Object.keys(mergedHashes).sort();\n while (hashKeys.length > MAX_TRACKED_DAY_HASHES) {\n const oldest = hashKeys.shift();\n if (oldest === undefined) break;\n delete mergedHashes[oldest];\n }\n\n const mergedMemoryHashes: Record<string, string> = {\n ...(previous?.memoryDayHashes ?? {}),\n ...(update.memoryDayHashes ?? {}),\n };\n for (const day of update.clearMemoryDays ?? []) {\n if (!(day in (update.memoryDayHashes ?? {}))) {\n delete mergedMemoryHashes[day];\n }\n }\n const memoryHashKeys = Object.keys(mergedMemoryHashes).sort();\n while (memoryHashKeys.length > MAX_TRACKED_DAY_HASHES) {\n const oldest = memoryHashKeys.shift();\n if (oldest === undefined) break;\n delete mergedMemoryHashes[oldest];\n }\n\n const mergedNativeIds = [\n ...(previous?.importedNativeMemoryIds ?? []),\n ...(update.importedNativeMemoryIds ?? []),\n ];\n const dedupedNativeIds = [...new Set(mergedNativeIds)];\n const boundedNativeIds =\n dedupedNativeIds.length > MAX_TRACKED_NATIVE_IDS\n ? dedupedNativeIds.slice(dedupedNativeIds.length - MAX_TRACKED_NATIVE_IDS)\n : dedupedNativeIds;\n\n const sortedDays = [...update.days].sort();\n const latestDay = sortedDays[sortedDays.length - 1];\n const lastDateSynced =\n latestDay !== undefined &&\n (!previous || previous.lastDateSynced < latestDay)\n ? latestDay\n : previous?.lastDateSynced ?? latestDay ?? \"\";\n\n return {\n version: 1,\n sources: {\n ...state.sources,\n [sourceId]: {\n lastSyncAt: update.syncedAt,\n lastDateSynced,\n dayHashes: mergedHashes,\n memoryDayHashes: mergedMemoryHashes,\n importedNativeMemoryIds: boundedNativeIds,\n },\n },\n };\n}\n","/**\n * Wearable sync pipeline — pull → clean → label → correct → store →\n * (optionally) remember, for one source.\n *\n * Stage order per conversation is deliberate:\n * 1. off-the-record elision (before anything can persist the span)\n * 2. cleanup (merging first lets redaction see numbers\n * that ASR split across segments)\n * 3. redaction (built-in + user patterns)\n * 4. corrections (user-specific word fixes)\n *\n * Day files are rebuilt idempotently; the per-day body hash recorded in\n * sync state lets unchanged days skip both the rewrite and the\n * (expensive) memory extraction. Sync state advances only after every\n * write for the run has succeeded.\n */\n\nimport { cleanConversation } from \"./cleanup.js\";\nimport { describeErrorForOperator, WearablesInputError } from \"./errors.js\";\nimport {\n applyCorrections,\n compileCorrectionRules,\n loadCorrectionsFile,\n type CompiledCorrectionRule,\n} from \"./corrections.js\";\nimport {\n composeDayTranscriptBody,\n composeDayTranscriptMeta,\n hashTranscriptBody,\n isValidTranscriptDate,\n serializeDayTranscript,\n} from \"./day-store.js\";\nimport {\n generateWearableMemories,\n importNativeMemories,\n writeDailyDigestMemory,\n type WearableMemoryGenDeps,\n} from \"./memory-gen.js\";\nimport { tokenizeDayBody, type CorroborationContext } from \"./trust.js\";\nimport { applyOffTheRecord, compileRedactionPatterns, redactText } from \"./redaction.js\";\nimport { loadSpeakerRegistry } from \"./speakers.js\";\nimport {\n loadSyncState,\n saveSyncState,\n updateSourceSyncState,\n} from \"./sync-state.js\";\nimport type {\n WearableConversation,\n WearableSourceConnector,\n WearableSourceSettings,\n WearableSyncSummary,\n WearablesConfig,\n} from \"./types.js\";\n\n/**\n * Pathological-provider backstop on pagination loops. Day fetches are\n * intentionally UNLIMITED for real data — a full day must never be\n * truncated — so runaway protection comes from cursor-cycle detection\n * plus this far-out-of-band ceiling, not from a content-sized cap.\n */\nconst PAGE_SAFETY_CEILING = 10_000;\n/** Default lookback window (today + yesterday) for unscoped syncs. */\nconst DEFAULT_SYNC_DAYS = 2;\nconst MAX_SYNC_DAYS = 90;\n\nexport interface WearableSyncOptions {\n /** Sync exactly this day (YYYY-MM-DD). Overrides `days`. */\n date?: string;\n /** Lookback window in days ending today (default 2, max 90). */\n days?: number;\n /** Re-run memory extraction even for unchanged days. */\n forceMemories?: boolean;\n signal?: AbortSignal;\n}\n\nexport interface WearableSyncDeps {\n /** Memory dir for state files (speakers, corrections, sync ledger). */\n memoryDir: string;\n /** Read the stored content hash for a day file, if present. */\n readDayContentHash(sourceId: string, date: string): Promise<string | null>;\n /** Persist a serialized day-transcript file (atomic). */\n writeDayTranscript(\n sourceId: string,\n date: string,\n serialized: string,\n ): Promise<void>;\n /**\n * Optional hook fired once after the sync wrote ANYTHING the search\n * index should see: day transcripts, created memories, in-place\n * promotions, or native imports. (A cross-source invalidation can\n * promote memories on a run with zero transcript writes — the index\n * must still refresh.)\n */\n afterWrites?(): Promise<void>;\n /**\n * Memory-generation dependencies, or null when no extraction engine\n * is available in this context (transcripts still sync; memory\n * creation is skipped with a warning when the mode wanted it).\n */\n memoryGen: WearableMemoryGenDeps | null;\n /**\n * Same-day transcript bodies from OTHER sources, for cross-device\n * corroboration in smart mode. Absent disables the boost.\n */\n readOtherSourceDayBodies?(\n date: string,\n excludeSource: string,\n ): Promise<Map<string, string>>;\n /**\n * Existing memories usable as support evidence: status \"active\" or\n * \"pending_review\" (explicit allow-list — a borderline fact observed\n * again on a later day is repetition signal, and the +0.10 boost is\n * how it earns promotion). Rejected/quarantined/superseded/archived/\n * forgotten rows are never support evidence.\n */\n listSupportMemories?(): Promise<Array<{ id: string; content: string }>>;\n /** Clock injection for tests. */\n now?: () => Date;\n}\n\n/** Format a Date as YYYY-MM-DD in the given IANA timezone. */\nexport function dateInTimezone(date: Date, timezone: string): string {\n try {\n const parts = new Intl.DateTimeFormat(\"en-CA\", {\n timeZone: timezone,\n year: \"numeric\",\n month: \"2-digit\",\n day: \"2-digit\",\n }).formatToParts(date);\n const get = (type: string) =>\n parts.find((part) => part.type === type)?.value ?? \"\";\n return `${get(\"year\")}-${get(\"month\")}-${get(\"day\")}`;\n } catch {\n return date.toISOString().slice(0, 10);\n }\n}\n\n/** Resolve the list of days to sync, oldest first. */\nexport function resolveSyncDates(\n options: WearableSyncOptions,\n timezone: string,\n now: Date,\n): string[] {\n if (options.date !== undefined) {\n if (!isValidTranscriptDate(options.date)) {\n throw new WearablesInputError(\n `wearables sync: invalid date '${options.date}' — expected YYYY-MM-DD`,\n );\n }\n return [options.date];\n }\n let days = DEFAULT_SYNC_DAYS;\n if (options.days !== undefined) {\n if (\n !Number.isFinite(options.days) ||\n !Number.isInteger(options.days) ||\n options.days < 1 ||\n options.days > MAX_SYNC_DAYS\n ) {\n throw new WearablesInputError(\n `wearables sync: invalid days '${options.days}' — expected an integer between 1 and ${MAX_SYNC_DAYS}`,\n );\n }\n days = options.days;\n }\n // Walk back by CALENDAR days from today's local date — subtracting\n // fixed 24h intervals from the wall clock can skip a local day\n // around DST transitions (Codex P2 on PR #1458).\n const dates: string[] = [];\n let cursor = dateInTimezone(now, timezone);\n for (let count = 0; count < days; count++) {\n dates.unshift(cursor);\n cursor = previousIsoDate(cursor);\n }\n return dates;\n}\n\n/** Previous calendar date in pure date arithmetic (no DST exposure). */\nfunction previousIsoDate(date: string): string {\n const parsed = new Date(`${date}T00:00:00Z`);\n parsed.setUTCDate(parsed.getUTCDate() - 1);\n return parsed.toISOString().slice(0, 10);\n}\n\nasync function fetchAllConversationsForDate(\n connector: WearableSourceConnector,\n date: string,\n timezone: string,\n signal: AbortSignal | undefined,\n warnings: string[],\n): Promise<{ conversations: WearableConversation[]; partial: boolean }> {\n // Keyed by conversation id: a looping or overlapping provider can\n // re-serve rows it already returned, and appending blindly would\n // store the same conversation twice in the day file (Cursor review\n // on PR #1464). Map keeps first-seen order; a re-served id replaces\n // its entry in place, so the provider's LATEST version of a\n // conversation wins — exactly what the current-day refresh wants.\n const byId = new Map<string, WearableConversation>();\n let cursor: string | null | undefined = undefined;\n const seenCursors = new Set<string>();\n const collect = () => [...byId.values()];\n for (let page = 0; page < PAGE_SAFETY_CEILING; page++) {\n const result = await connector.fetchConversations({\n date,\n timezone,\n cursor,\n signal,\n });\n for (const conversation of result.conversations) {\n byId.set(conversation.id, conversation);\n }\n if (!result.nextCursor) return { conversations: collect(), partial: false };\n // A repeated cursor means the provider's pagination is looping —\n // following it again would refetch the same page forever.\n if (seenCursors.has(result.nextCursor)) {\n warnings.push(\n `${connector.id}: provider pagination repeated cursor on ${date} — stopped to avoid an infinite loop; day may be partially synced (every sync refetches and re-warns while the provider misbehaves)`,\n );\n return { conversations: collect(), partial: true };\n }\n seenCursors.add(result.nextCursor);\n cursor = result.nextCursor;\n }\n warnings.push(\n `${connector.id}: stopped paginating ${date} after the ${PAGE_SAFETY_CEILING}-page safety ceiling — day may be partially synced (every sync refetches and re-warns while this persists)`,\n );\n return { conversations: collect(), partial: true };\n}\n\n/** Visible marker appended to day files whose fetch hit the page cap. */\nconst PARTIAL_DAY_MARKER =\n \"\\n*Note: pagination safety cap reached during sync — this day may be incomplete.*\\n\";\n\n/**\n * Explicit replacement body for a day whose provider data exists but\n * whose every segment was elided or dropped (all off-the-record, all\n * ASR garbage). Written so previously-stored content for the day stops\n * being searchable instead of lingering as a stale transcript.\n */\nfunction emptyDayBody(sourceId: string, date: string): string {\n return (\n `# ${sourceId} transcript — ${date}\\n\\n` +\n \"_No storable conversation content for this day (all segments were elided or dropped)._\\n\"\n );\n}\n\ninterface CleanedDay {\n conversations: WearableConversation[];\n segmentsKept: number;\n segmentsDropped: number;\n redactions: number;\n correctionsApplied: number;\n}\n\nfunction cleanDay(\n raw: WearableConversation[],\n sourceId: string,\n settings: WearableSourceSettings,\n config: WearablesConfig,\n userRedaction: RegExp[],\n correctionRules: CompiledCorrectionRule[],\n): CleanedDay {\n const out: CleanedDay = {\n conversations: [],\n segmentsKept: 0,\n segmentsDropped: 0,\n redactions: 0,\n correctionsApplied: 0,\n };\n for (const conversation of raw) {\n let current = conversation;\n if (config.offTheRecordEnabled) {\n const otr = applyOffTheRecord(current);\n current = otr.conversation;\n out.segmentsDropped += otr.droppedSegments;\n }\n const cleaned = cleanConversation(current, settings.cleanup);\n current = cleaned.conversation;\n out.segmentsDropped += cleaned.droppedSegments;\n\n const segments = current.segments.map((segment) => {\n let text = segment.text;\n if (config.redactionEnabled) {\n const redacted = redactText(text, userRedaction);\n text = redacted.text;\n out.redactions += redacted.redactions;\n }\n const corrected = applyCorrections(text, correctionRules, sourceId);\n out.correctionsApplied += corrected.applied;\n return { ...segment, text: corrected.text };\n });\n current = { ...current, segments };\n\n if (current.segments.length > 0) {\n out.conversations.push(current);\n out.segmentsKept += current.segments.length;\n }\n }\n return out;\n}\n\n/** Sync one source. */\nexport async function syncWearableSource(\n connector: WearableSourceConnector,\n settings: WearableSourceSettings,\n config: WearablesConfig,\n options: WearableSyncOptions,\n deps: WearableSyncDeps,\n): Promise<WearableSyncSummary> {\n const now = deps.now ? deps.now() : new Date();\n const timezone = config.timezone ?? defaultTimezone();\n const dates = resolveSyncDates(options, timezone, now);\n\n const summary: WearableSyncSummary = {\n source: connector.id,\n days: dates,\n conversations: 0,\n segmentsKept: 0,\n segmentsDropped: 0,\n redactions: 0,\n correctionsApplied: 0,\n transcriptsWritten: [],\n memoriesCreated: 0,\n memoriesPromoted: 0,\n memoriesDemoted: 0,\n memoriesSkipped: 0,\n nativeMemoriesImported: 0,\n warnings: [],\n };\n\n const registry = await loadSpeakerRegistry(deps.memoryDir);\n const stateRules = await loadCorrectionsFile(deps.memoryDir);\n const correctionRules = [\n ...compileCorrectionRules(config.corrections, \"wearables.corrections\"),\n ...compileCorrectionRules(stateRules, \"state corrections\"),\n ];\n const userRedaction = compileRedactionPatterns(config.redactionPatterns);\n\n let syncState = await loadSyncState(deps.memoryDir);\n const previousState = syncState.sources[connector.id];\n const dayHashes: Record<string, string> = {};\n const memoryDayHashes: Record<string, string> = {};\n const failedMemoryDays: string[] = [];\n const importedNativeIds: string[] = [];\n\n for (const date of dates) {\n const fetched = await fetchAllConversationsForDate(\n connector,\n date,\n timezone,\n options.signal,\n summary.warnings,\n );\n const cleaned = cleanDay(\n fetched.conversations,\n connector.id,\n settings,\n config,\n userRedaction,\n correctionRules,\n );\n summary.conversations += cleaned.conversations.length;\n summary.segmentsKept += cleaned.segmentsKept;\n summary.segmentsDropped += cleaned.segmentsDropped;\n summary.redactions += cleaned.redactions;\n summary.correctionsApplied += cleaned.correctionsApplied;\n\n if (fetched.conversations.length === 0) {\n // No provider data at all. A transient provider hiccup can\n // legitimately produce an empty result, so an existing stored\n // transcript is never auto-deleted here — surface it instead.\n const existing = await deps.readDayContentHash(connector.id, date);\n if (existing !== null) {\n summary.warnings.push(\n `${connector.id}: provider returned no conversations for ${date} but a stored transcript exists — leaving it in place; delete the day file manually if the recordings were intentionally removed upstream`,\n );\n }\n continue;\n }\n\n // Provider data exists but cleanup/off-the-record elided all of it:\n // replace any stored transcript with an explicit empty-day file so\n // elided content stops being stored and searchable (Codex P2 on PR\n // #1458).\n const allElided = cleaned.conversations.length === 0;\n let body = allElided\n ? emptyDayBody(connector.id, date)\n : composeDayTranscriptBody(\n connector.id,\n date,\n timezone,\n cleaned.conversations,\n registry,\n );\n if (fetched.partial && !allElided) {\n body += PARTIAL_DAY_MARKER;\n }\n const bodyHash = hashTranscriptBody(body);\n // The on-disk file is the authority for the skip decision — a hash\n // remembered in sync state must never suppress recreating a day\n // file that was deleted or lost (Cursor review on PR #1458). The\n // state's dayHashes remain as bookkeeping only.\n const existingHash = await deps.readDayContentHash(connector.id, date);\n const changed = existingHash !== bodyHash;\n // An all-elided day only writes a replacement over an existing\n // file; it never creates an empty-day file from nothing.\n const shouldWrite = changed && (!allElided || existingHash !== null);\n\n if (shouldWrite) {\n const meta = composeDayTranscriptMeta(\n connector.id,\n date,\n timezone,\n cleaned.conversations,\n registry,\n body,\n now.toISOString(),\n );\n await deps.writeDayTranscript(\n connector.id,\n date,\n serializeDayTranscript(meta, body),\n );\n summary.transcriptsWritten.push(date);\n }\n dayHashes[date] = bodyHash;\n\n if (allElided) continue;\n\n const needsSmartContext =\n settings.memoryMode === \"smart\" || settings.importNativeMemories === \"smart\";\n\n // The memory pass runs when the day changed, when forced, or when\n // the last pass for this exact content didn't complete cleanly —\n // a sync that stored the transcript but failed mid-memory-write\n // self-heals on the next run instead of being frozen out by the\n // unchanged-day skip (Cursor review on PR #1458).\n const memoryPassComplete =\n previousState?.memoryDayHashes?.[date] === bodyHash;\n if (\n settings.memoryMode !== \"off\" &&\n (changed || options.forceMemories === true || !memoryPassComplete)\n ) {\n if (!deps.memoryGen) {\n summary.warnings.push(\n `${connector.id}: memoryMode is '${settings.memoryMode}' but no extraction engine is available in this context — transcripts synced, memories skipped`,\n );\n } else {\n // The whole memory pass (extraction, fact writes, digest) is\n // warn-and-retry rather than abort: the transcript is already\n // stored, and aborting here would leave any stale completion\n // record from an earlier clean run in place to mask the\n // failure (Kilo review on PR #1458 for the digest case; fact\n // writes share the same failure class). A clean pass records\n // completion; anything else clears it so the next sync\n // re-runs the day.\n let passClean = false;\n try {\n const corroboration = needsSmartContext\n ? await buildCorroborationContext(connector.id, date, deps)\n : undefined;\n const dayMemoryGen: WearableMemoryGenDeps = {\n ...deps.memoryGen,\n ...(corroboration !== undefined ? { corroboration } : {}),\n };\n const generated = await generateWearableMemories(\n connector.id,\n date,\n cleaned.conversations,\n settings,\n registry,\n dayMemoryGen,\n );\n summary.memoriesCreated += generated.created;\n summary.memoriesPromoted += generated.promoted;\n summary.memoriesDemoted += generated.demoted;\n summary.memoriesSkipped += generated.skipped;\n summary.warnings.push(...generated.warnings);\n // Degraded-but-complete passes (e.g. judge unavailable) still\n // record completion — only an aborted extraction should force\n // the day to re-run on the next sync (Cursor review on PR\n // #1462).\n passClean = generated.completed;\n if (config.digestEnabled) {\n const wrote = await writeDailyDigestMemory(\n connector.id,\n date,\n cleaned.conversations,\n settings,\n registry,\n deps.memoryGen.writer,\n );\n if (wrote) summary.memoriesCreated += 1;\n }\n } catch (err) {\n passClean = false;\n summary.warnings.push(\n `${connector.id}: memory pass failed for ${date}: ${describeErrorForOperator(err)} — retries on the next sync`,\n );\n }\n if (passClean) {\n memoryDayHashes[date] = bodyHash;\n } else {\n failedMemoryDays.push(date);\n }\n }\n } else if (settings.memoryMode !== \"off\" && memoryPassComplete) {\n // Carry the completion record forward for unchanged days.\n memoryDayHashes[date] = bodyHash;\n }\n }\n\n if (\n settings.importNativeMemories !== \"off\" &&\n typeof connector.fetchNativeMemories === \"function\"\n ) {\n if (!deps.memoryGen) {\n summary.warnings.push(\n `${connector.id}: importNativeMemories is enabled but no memory writer is available in this context`,\n );\n } else {\n const alreadyImported = new Set(\n previousState?.importedNativeMemoryIds ?? [],\n );\n // Native memories carry no day, so same-day cross-device\n // corroboration does not apply to them — scoring a provider fact\n // against an arbitrary day's tokens would be wrong-day evidence\n // (Cursor review on PR #1462). They keep only the day-independent\n // existing-memory support boost.\n const nativeCorroboration =\n settings.importNativeMemories === \"smart\"\n ? {\n otherSourceDayTokens: new Map<string, Set<string>>(),\n existingMemories: deps.listSupportMemories\n ? await deps.listSupportMemories()\n : [],\n }\n : undefined;\n const nativeMemoryGen: WearableMemoryGenDeps = {\n ...deps.memoryGen,\n ...(nativeCorroboration !== undefined\n ? { corroboration: nativeCorroboration }\n : {}),\n };\n let cursor: string | null | undefined = undefined;\n const seenNativeCursors = new Set<string>();\n for (let page = 0; page < PAGE_SAFETY_CEILING; page++) {\n const result = await connector.fetchNativeMemories({\n cursor,\n signal: options.signal,\n });\n const imported = await importNativeMemories(\n connector.id,\n result.memories,\n alreadyImported,\n settings,\n nativeMemoryGen,\n );\n summary.warnings.push(...imported.warnings);\n summary.nativeMemoriesImported += imported.imported;\n importedNativeIds.push(...imported.importedIds);\n for (const id of imported.importedIds) alreadyImported.add(id);\n if (!result.nextCursor) break;\n if (seenNativeCursors.has(result.nextCursor)) {\n summary.warnings.push(\n `${connector.id}: provider pagination repeated cursor during native-memory import — stopped to avoid an infinite loop; remaining items import on the next sync`,\n );\n break;\n }\n seenNativeCursors.add(result.nextCursor);\n cursor = result.nextCursor;\n if (page === PAGE_SAFETY_CEILING - 1) {\n summary.warnings.push(\n `${connector.id}: stopped native-memory import at the ${PAGE_SAFETY_CEILING}-page safety ceiling — remaining items import on the next sync`,\n );\n }\n }\n }\n }\n\n const wroteAnything =\n summary.transcriptsWritten.length > 0 ||\n summary.memoriesCreated > 0 ||\n summary.memoriesPromoted > 0 ||\n summary.memoriesDemoted > 0 ||\n summary.nativeMemoriesImported > 0;\n if (wroteAnything && deps.afterWrites) {\n try {\n await deps.afterWrites();\n } catch (err) {\n summary.warnings.push(\n `search reindex failed (writes are stored and will index on the next update): ${describeErrorForOperator(err)}`,\n );\n }\n }\n\n // Watermark advances only now — after transcript writes, memory\n // writes, and native imports all succeeded.\n syncState = updateSourceSyncState(syncState, connector.id, {\n syncedAt: now.toISOString(),\n days: dates,\n dayHashes,\n memoryDayHashes,\n clearMemoryDays: failedMemoryDays,\n importedNativeMemoryIds: importedNativeIds,\n });\n // New same-day evidence invalidates OTHER sources' memory-pass\n // completion for the days this source just (re)wrote: their next\n // sync re-scores with this transcript available, and the promotion\n // path upgrades earlier borderline writes in place (Cursor review on\n // PR #1462).\n if (summary.transcriptsWritten.length > 0) {\n const cleared: typeof syncState.sources = {};\n for (const [otherId, otherState] of Object.entries(syncState.sources)) {\n if (otherId === connector.id || otherState.memoryDayHashes === undefined) {\n cleared[otherId] = otherState;\n continue;\n }\n const memoryDays = { ...otherState.memoryDayHashes };\n let touched = false;\n for (const date of summary.transcriptsWritten) {\n if (date in memoryDays) {\n delete memoryDays[date];\n touched = true;\n }\n }\n cleared[otherId] = touched\n ? { ...otherState, memoryDayHashes: memoryDays }\n : otherState;\n }\n syncState = { version: 1, sources: cleared };\n }\n await saveSyncState(deps.memoryDir, syncState);\n\n return summary;\n}\n\n/**\n * Assemble smart-mode corroboration evidence: other sources' same-day\n * transcript tokens + existing active memories. The memory list loads\n * fresh per day (not per run) so facts written on earlier days of a\n * multi-day backfill are visible as support evidence on later days —\n * the underlying readAllMemories is cached in storage and invalidated\n * by writes, so the per-day refresh is cheap (Cursor review on PR\n * #1462).\n */\nasync function buildCorroborationContext(\n sourceId: string,\n date: string,\n deps: WearableSyncDeps,\n): Promise<CorroborationContext> {\n const otherSourceDayTokens = new Map<string, Set<string>>();\n if (deps.readOtherSourceDayBodies) {\n const bodies = await deps.readOtherSourceDayBodies(date, sourceId);\n for (const [otherSource, body] of bodies) {\n otherSourceDayTokens.set(otherSource, tokenizeDayBody(body));\n }\n }\n const existingMemories = deps.listSupportMemories\n ? await deps.listSupportMemories()\n : [];\n return { otherSourceDayTokens, existingMemories };\n}\n\nexport function defaultTimezone(): string {\n try {\n return Intl.DateTimeFormat().resolvedOptions().timeZone || \"UTC\";\n } catch {\n return \"UTC\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCO,IAAM,2BAA2B;AACjC,IAAM,2BAA2B;AACjC,IAAM,gCAAgC;AAG7C,IAAM,kBAAkB;AAExB,IAAM,wBAAwB;AAE9B,IAAM,0BAA0B;AAEhC,IAAM,uBAAuB;AAmBtB,SAAS,kBAAkB,OAAgC;AAChE,QAAM,aACJ,OAAO,MAAM,yBAAyB,YACtC,OAAO,SAAS,MAAM,oBAAoB,IACtC,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,oBAAoB,CAAC,IACnD;AACN,MAAI,QAAQ,aAAa,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,WAAW,CAAC;AACnE,MAAI,MAAM,iBAAiB,SAAU,UAAS;AAC9C,MAAI,MAAM,SAAS,sBAAsB,SAAS,GAAG;AACnD,aAAS;AAAA,EACX;AACA,MAAI,MAAM,SAAS,uBAAuB,QAAW;AACn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;AACrE,UAAI,YAAY,UAAU,MAAM,WAAW,oBAAoB,QAAW;AACxE,gBAAQ,OAAO,IAAI;AACnB;AAAA,MACF;AACA,YAAM,aAAa,EAAE,GAAG,WAAW,gBAAgB;AACnD,UAAI,UAAU;AACd,iBAAW,QAAQ,QAAQ,oBAAoB;AAC7C,YAAI,QAAQ,YAAY;AACtB,iBAAO,WAAW,IAAI;AACtB,oBAAU;AAAA,QACZ;AAAA,MACF;AACA,cAAQ,OAAO,IAAI,UACf,EAAE,GAAG,YAAY,iBAAiB,WAAW,IAC7C;AAAA,IACN;AACA,gBAAY,EAAE,SAAS,GAAG,SAAS,QAAQ;AAAA,EAC7C;AACA,QAAM,cAAc,KAAK,WAAW,SAAS;AAE7C,SAAO;AACT;AAWA,eAAe,0BACb,UACA,MACA,MAC+B;AAC/B,QAAM,uBAAuB,oBAAI,IAAyB;AAC1D,MAAI,KAAK,0BAA0B;AACjC,UAAM,SAAS,MAAM,KAAK,yBAAyB,MAAM,QAAQ;AACjE,eAAW,CAAC,aAAa,IAAI,KAAK,QAAQ;AACxC,2BAAqB,IAAI,aAAa,gBAAgB,IAAI,CAAC;AAAA,IAC7D;AAAA,EACF;AACA,QAAM,mBAAmB,KAAK,sBAC1B,MAAM,KAAK,oBAAoB,IAC/B,CAAC;AACL,SAAO,EAAE,sBAAsB,iBAAiB;AAClD;AAEO,SAAS,kBAA0B;AACxC,MAAI;AACF,WAAO,KAAK,eAAe,EAAE,gBAAgB,EAAE,YAAY;AAAA,EAC7D,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-ZFXCQPNO.js";
5
5
  import {
6
6
  StorageManager
7
- } from "./chunk-RSKUUEBA.js";
7
+ } from "./chunk-QYGIQ5NM.js";
8
8
  import {
9
9
  isSafeRouteNamespace
10
10
  } from "./chunk-U3PN77QT.js";
@@ -161,4 +161,4 @@ export {
161
161
  getCategoryDir,
162
162
  NamespaceStorageRouter
163
163
  };
164
- //# sourceMappingURL=chunk-S5W37FPX.js.map
164
+ //# sourceMappingURL=chunk-LEG7XWS2.js.map
@@ -0,0 +1,280 @@
1
+ // src/wearables/speakers.ts
2
+ import { promises as fsPromises } from "fs";
3
+ import * as path from "path";
4
+ var DEFAULT_SELF_NAME = "Me";
5
+ function emptySpeakerRegistry() {
6
+ return { version: 1, selfName: DEFAULT_SELF_NAME, speakers: {} };
7
+ }
8
+ function speakersFilePath(memoryDir) {
9
+ return path.join(memoryDir, "state", "wearables", "speakers.json");
10
+ }
11
+ async function loadSpeakerRegistry(memoryDir) {
12
+ const filePath = speakersFilePath(memoryDir);
13
+ let raw;
14
+ try {
15
+ raw = await fsPromises.readFile(filePath, "utf-8");
16
+ } catch (err) {
17
+ if (err.code === "ENOENT") {
18
+ return emptySpeakerRegistry();
19
+ }
20
+ throw err;
21
+ }
22
+ let parsed;
23
+ try {
24
+ parsed = JSON.parse(raw);
25
+ } catch (err) {
26
+ throw new Error(
27
+ `wearables speakers file is not valid JSON (state/wearables/speakers.json): ${err instanceof Error ? err.message : String(err)}`
28
+ );
29
+ }
30
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed) || typeof parsed.speakers !== "object" || parsed.speakers === null) {
31
+ throw new Error(
32
+ 'wearables speakers file has an unexpected shape (state/wearables/speakers.json); expected {"version":1,"selfName":"...","speakers":{}}'
33
+ );
34
+ }
35
+ const registry = parsed;
36
+ return {
37
+ version: 1,
38
+ selfName: typeof registry.selfName === "string" && registry.selfName.trim().length > 0 ? registry.selfName.trim() : DEFAULT_SELF_NAME,
39
+ speakers: registry.speakers
40
+ };
41
+ }
42
+ async function saveSpeakerRegistry(memoryDir, registry) {
43
+ const filePath = speakersFilePath(memoryDir);
44
+ await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
45
+ const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now().toString(36)}`;
46
+ await fsPromises.writeFile(
47
+ tmpPath,
48
+ `${JSON.stringify(registry, null, 2)}
49
+ `,
50
+ "utf-8"
51
+ );
52
+ try {
53
+ await fsPromises.rename(tmpPath, filePath);
54
+ } catch (err) {
55
+ await fsPromises.unlink(tmpPath).catch(() => void 0);
56
+ throw err;
57
+ }
58
+ }
59
+ function speakerRegistryKey(sourceId, speakerKey) {
60
+ return `${sourceId}:${speakerKey}`;
61
+ }
62
+ function resolveSpeaker(sourceId, segment, registry) {
63
+ const override = registry.speakers[speakerRegistryKey(sourceId, segment.speakerKey)];
64
+ if (override) {
65
+ const isSelf = override.isSelf === true;
66
+ return {
67
+ label: isSelf ? `${override.name} (you)` : override.name,
68
+ isSelf
69
+ };
70
+ }
71
+ if (segment.isWearer === true) {
72
+ return { label: `${registry.selfName} (you)`, isSelf: true };
73
+ }
74
+ if (typeof segment.speakerName === "string" && segment.speakerName.trim().length > 0) {
75
+ return { label: segment.speakerName.trim(), isSelf: false };
76
+ }
77
+ const key = segment.speakerKey.trim();
78
+ if (/^\d+$/.test(key)) {
79
+ return { label: `Speaker ${key}`, isSelf: false };
80
+ }
81
+ return { label: key.length > 0 ? key : "Unknown speaker", isSelf: false };
82
+ }
83
+ function distinctSpeakerLabels(sourceId, segments, registry) {
84
+ const labels = [];
85
+ const seen = /* @__PURE__ */ new Set();
86
+ for (const segment of segments) {
87
+ const { label } = resolveSpeaker(sourceId, segment, registry);
88
+ if (!seen.has(label)) {
89
+ seen.add(label);
90
+ labels.push(label);
91
+ }
92
+ }
93
+ return labels;
94
+ }
95
+
96
+ // src/wearables/day-store.ts
97
+ import { createHash } from "crypto";
98
+ var WEARABLES_DIR_NAME = "wearables";
99
+ var DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
100
+ function isValidTranscriptDate(date) {
101
+ if (!DATE_PATTERN.test(date)) return false;
102
+ const parsed = /* @__PURE__ */ new Date(`${date}T00:00:00Z`);
103
+ return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === date;
104
+ }
105
+ function hashTranscriptBody(body) {
106
+ return createHash("sha256").update(body, "utf-8").digest("hex");
107
+ }
108
+ function formatClockTime(iso, timezone) {
109
+ if (!iso) return "--:--";
110
+ const ms = Date.parse(iso);
111
+ if (Number.isNaN(ms)) return "--:--";
112
+ try {
113
+ return new Intl.DateTimeFormat("en-US", {
114
+ timeZone: timezone,
115
+ hour12: false,
116
+ hour: "2-digit",
117
+ minute: "2-digit"
118
+ }).format(new Date(ms));
119
+ } catch {
120
+ return new Date(ms).toISOString().slice(11, 16);
121
+ }
122
+ }
123
+ function conversationDurationMinutes(conversation) {
124
+ const start = Date.parse(conversation.startIso);
125
+ const end = conversation.endIso ? Date.parse(conversation.endIso) : NaN;
126
+ if (Number.isNaN(start) || Number.isNaN(end) || end <= start) return 0;
127
+ return (end - start) / 6e4;
128
+ }
129
+ function composeDayTranscriptBody(sourceId, date, timezone, conversations, registry) {
130
+ const lines = [];
131
+ lines.push(`# ${sourceId} transcript \u2014 ${date}`);
132
+ lines.push("");
133
+ const ordered = [...conversations].sort((a, b) => {
134
+ const aMs = Date.parse(a.startIso);
135
+ const bMs = Date.parse(b.startIso);
136
+ if (aMs < bMs) return -1;
137
+ if (aMs > bMs) return 1;
138
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
139
+ });
140
+ for (const conversation of ordered) {
141
+ const start = formatClockTime(conversation.startIso, timezone);
142
+ const end = formatClockTime(conversation.endIso, timezone);
143
+ const title = conversation.title?.trim();
144
+ const heading = title && title.length > 0 ? ` \xB7 ${title}` : "";
145
+ lines.push(`## ${start}\u2013${end}${heading} (conversation ${conversation.id})`);
146
+ if (conversation.location) {
147
+ lines.push(`*Location: ${conversation.location}*`);
148
+ }
149
+ lines.push("");
150
+ for (const segment of conversation.segments) {
151
+ const { label } = resolveSpeaker(sourceId, segment, registry);
152
+ const at = formatClockTime(segment.startIso, timezone);
153
+ lines.push(`**${label}** [${at}]: ${segment.text}`);
154
+ }
155
+ lines.push("");
156
+ }
157
+ return `${lines.join("\n").trimEnd()}
158
+ `;
159
+ }
160
+ function composeDayTranscriptMeta(sourceId, date, timezone, conversations, registry, body, syncedAt) {
161
+ const allSegments = conversations.flatMap((c) => c.segments);
162
+ const durationMinutes = Math.round(
163
+ conversations.reduce((sum, c) => sum + conversationDurationMinutes(c), 0)
164
+ );
165
+ return {
166
+ kind: "wearable-transcript",
167
+ source: sourceId,
168
+ date,
169
+ timezone,
170
+ conversationCount: conversations.length,
171
+ segmentCount: allSegments.length,
172
+ speakers: distinctSpeakerLabels(sourceId, allSegments, registry),
173
+ durationMinutes,
174
+ contentHash: hashTranscriptBody(body),
175
+ syncedAt
176
+ };
177
+ }
178
+ function serializeDayTranscript(meta, body) {
179
+ const lines = ["---"];
180
+ lines.push(`kind: ${meta.kind}`);
181
+ lines.push(`source: ${JSON.stringify(meta.source)}`);
182
+ lines.push(`date: ${JSON.stringify(meta.date)}`);
183
+ lines.push(`timezone: ${JSON.stringify(meta.timezone)}`);
184
+ lines.push(`conversationCount: ${meta.conversationCount}`);
185
+ lines.push(`segmentCount: ${meta.segmentCount}`);
186
+ if (meta.speakers.length === 0) {
187
+ lines.push("speakers: []");
188
+ } else {
189
+ lines.push("speakers:");
190
+ for (const speaker of meta.speakers) {
191
+ lines.push(` - ${JSON.stringify(speaker)}`);
192
+ }
193
+ }
194
+ lines.push(`durationMinutes: ${meta.durationMinutes}`);
195
+ lines.push(`contentHash: ${JSON.stringify(meta.contentHash)}`);
196
+ lines.push(`syncedAt: ${JSON.stringify(meta.syncedAt)}`);
197
+ lines.push("---");
198
+ lines.push("");
199
+ return `${lines.join("\n")}${body}`;
200
+ }
201
+ function parseDayTranscript(raw) {
202
+ if (!raw.startsWith("---\n")) return null;
203
+ const closeIndex = raw.indexOf("\n---\n", 4);
204
+ if (closeIndex === -1) return null;
205
+ const header = raw.slice(4, closeIndex);
206
+ const body = raw.slice(closeIndex + 5).replace(/^\n/, "");
207
+ const scalars = /* @__PURE__ */ new Map();
208
+ const speakers = [];
209
+ let inSpeakers = false;
210
+ for (const line of header.split("\n")) {
211
+ if (inSpeakers) {
212
+ const item = line.match(/^ {2}- (.*)$/);
213
+ if (item) {
214
+ speakers.push(parseYamlScalar(item[1]));
215
+ continue;
216
+ }
217
+ inSpeakers = false;
218
+ }
219
+ if (line === "speakers:") {
220
+ inSpeakers = true;
221
+ continue;
222
+ }
223
+ if (line === "speakers: []") continue;
224
+ const match = line.match(/^([A-Za-z][A-Za-z0-9]*): (.*)$/);
225
+ if (match) scalars.set(match[1], parseYamlScalar(match[2]));
226
+ }
227
+ if (scalars.get("kind") !== "wearable-transcript") return null;
228
+ const source = scalars.get("source");
229
+ const date = scalars.get("date");
230
+ if (!source || !date) return null;
231
+ const meta = {
232
+ kind: "wearable-transcript",
233
+ source,
234
+ date,
235
+ timezone: scalars.get("timezone") ?? "UTC",
236
+ conversationCount: parseNonNegativeInt(scalars.get("conversationCount")),
237
+ segmentCount: parseNonNegativeInt(scalars.get("segmentCount")),
238
+ speakers,
239
+ durationMinutes: parseNonNegativeInt(scalars.get("durationMinutes")),
240
+ contentHash: scalars.get("contentHash") ?? "",
241
+ syncedAt: scalars.get("syncedAt") ?? ""
242
+ };
243
+ return { meta, body };
244
+ }
245
+ function parseYamlScalar(value) {
246
+ const trimmed = value.trim();
247
+ if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2) {
248
+ try {
249
+ const parsed = JSON.parse(trimmed);
250
+ if (typeof parsed === "string") return parsed;
251
+ } catch {
252
+ }
253
+ }
254
+ return trimmed;
255
+ }
256
+ function parseNonNegativeInt(value) {
257
+ if (value === void 0) return 0;
258
+ const parsed = Number(value);
259
+ if (!Number.isFinite(parsed) || parsed < 0) return 0;
260
+ return Math.floor(parsed);
261
+ }
262
+
263
+ export {
264
+ DEFAULT_SELF_NAME,
265
+ emptySpeakerRegistry,
266
+ speakersFilePath,
267
+ loadSpeakerRegistry,
268
+ saveSpeakerRegistry,
269
+ speakerRegistryKey,
270
+ resolveSpeaker,
271
+ distinctSpeakerLabels,
272
+ WEARABLES_DIR_NAME,
273
+ isValidTranscriptDate,
274
+ hashTranscriptBody,
275
+ composeDayTranscriptBody,
276
+ composeDayTranscriptMeta,
277
+ serializeDayTranscript,
278
+ parseDayTranscript
279
+ };
280
+ //# sourceMappingURL=chunk-M7XQSUBB.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/wearables/speakers.ts","../src/wearables/day-store.ts"],"sourcesContent":["/**\n * Wearable speaker registry — maps provider diarization labels to\n * human names, persistently, per source.\n *\n * Providers expose speakers differently (Limitless: names + a \"user\"\n * marker; Bee: opaque labels like \"0\"/\"1\"; Omi: \"SPEAKER_00\" + is_user\n * + optional person ids). The registry stores operator-confirmed\n * mappings keyed `<sourceId>:<speakerKey>` in\n * `state/wearables/speakers.json`, plus the wearer's display name.\n *\n * Resolution precedence (most-authoritative first):\n * 1. registry override for `<sourceId>:<speakerKey>`\n * 2. provider-identified wearer -> selfName\n * 3. provider-supplied speaker name\n * 4. the raw speaker key, prefixed \"Speaker\" when it is bare digits\n */\n\nimport { promises as fsPromises } from \"node:fs\";\nimport * as path from \"node:path\";\n\nexport interface SpeakerOverride {\n name: string;\n /** Mark this speaker as the wearer (their words become \"you\"). */\n isSelf?: boolean;\n updatedAt: string;\n}\n\nexport interface SpeakerRegistry {\n version: 1;\n /** Display name used for the wearer across all sources. */\n selfName: string;\n /** Overrides keyed `<sourceId>:<speakerKey>`. */\n speakers: Record<string, SpeakerOverride>;\n}\n\nexport const DEFAULT_SELF_NAME = \"Me\";\n\nexport function emptySpeakerRegistry(): SpeakerRegistry {\n return { version: 1, selfName: DEFAULT_SELF_NAME, speakers: {} };\n}\n\nexport function speakersFilePath(memoryDir: string): string {\n return path.join(memoryDir, \"state\", \"wearables\", \"speakers.json\");\n}\n\nexport async function loadSpeakerRegistry(\n memoryDir: string,\n): Promise<SpeakerRegistry> {\n const filePath = speakersFilePath(memoryDir);\n let raw: string;\n try {\n raw = await fsPromises.readFile(filePath, \"utf-8\");\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n return emptySpeakerRegistry();\n }\n throw err;\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (err) {\n throw new Error(\n `wearables speakers file is not valid JSON (state/wearables/speakers.json): ${\n err instanceof Error ? err.message : String(err)\n }`,\n );\n }\n if (\n typeof parsed !== \"object\" ||\n parsed === null ||\n Array.isArray(parsed) ||\n typeof (parsed as SpeakerRegistry).speakers !== \"object\" ||\n (parsed as SpeakerRegistry).speakers === null\n ) {\n throw new Error(\n 'wearables speakers file has an unexpected shape (state/wearables/speakers.json); expected {\"version\":1,\"selfName\":\"...\",\"speakers\":{}}',\n );\n }\n const registry = parsed as SpeakerRegistry;\n return {\n version: 1,\n selfName:\n typeof registry.selfName === \"string\" && registry.selfName.trim().length > 0\n ? registry.selfName.trim()\n : DEFAULT_SELF_NAME,\n speakers: registry.speakers,\n };\n}\n\nexport async function saveSpeakerRegistry(\n memoryDir: string,\n registry: SpeakerRegistry,\n): Promise<void> {\n const filePath = speakersFilePath(memoryDir);\n await fsPromises.mkdir(path.dirname(filePath), { recursive: true });\n const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now().toString(36)}`;\n await fsPromises.writeFile(\n tmpPath,\n `${JSON.stringify(registry, null, 2)}\\n`,\n \"utf-8\",\n );\n try {\n await fsPromises.rename(tmpPath, filePath);\n } catch (err) {\n await fsPromises.unlink(tmpPath).catch(() => undefined);\n throw err;\n }\n}\n\nexport function speakerRegistryKey(\n sourceId: string,\n speakerKey: string,\n): string {\n return `${sourceId}:${speakerKey}`;\n}\n\nexport interface ResolvedSpeaker {\n /** Display label used in transcripts, e.g. \"Jane\" or \"Me (you)\". */\n label: string;\n /** Whether this speaker is the wearer. */\n isSelf: boolean;\n}\n\nexport function resolveSpeaker(\n sourceId: string,\n segment: { speakerKey: string; speakerName?: string; isWearer?: boolean },\n registry: SpeakerRegistry,\n): ResolvedSpeaker {\n const override = registry.speakers[speakerRegistryKey(sourceId, segment.speakerKey)];\n if (override) {\n const isSelf = override.isSelf === true;\n return {\n label: isSelf ? `${override.name} (you)` : override.name,\n isSelf,\n };\n }\n if (segment.isWearer === true) {\n return { label: `${registry.selfName} (you)`, isSelf: true };\n }\n if (\n typeof segment.speakerName === \"string\" &&\n segment.speakerName.trim().length > 0\n ) {\n return { label: segment.speakerName.trim(), isSelf: false };\n }\n const key = segment.speakerKey.trim();\n // Bare diarization indexes read better with a prefix.\n if (/^\\d+$/.test(key)) {\n return { label: `Speaker ${key}`, isSelf: false };\n }\n return { label: key.length > 0 ? key : \"Unknown speaker\", isSelf: false };\n}\n\n/**\n * Distinct speaker labels for a set of segments, in first-appearance\n * order — used for day-transcript frontmatter.\n */\nexport function distinctSpeakerLabels(\n sourceId: string,\n segments: Array<{ speakerKey: string; speakerName?: string; isWearer?: boolean }>,\n registry: SpeakerRegistry,\n): string[] {\n const labels: string[] = [];\n const seen = new Set<string>();\n for (const segment of segments) {\n const { label } = resolveSpeaker(sourceId, segment, registry);\n if (!seen.has(label)) {\n seen.add(label);\n labels.push(label);\n }\n }\n return labels;\n}\n","/**\n * Wearable day-transcript composition and parsing.\n *\n * One markdown file per source per day, stored under\n * `<memoryDir>/wearables/<source>/<YYYY-MM-DD>.md` with YAML\n * frontmatter. The location is deliberate:\n *\n * - it is OUTSIDE the memory scan roots (facts/, procedures/,\n * reasoning-traces/, corrections/), so transcripts never appear as\n * memories in recall or governance passes;\n * - it is INSIDE the QMD collection root (the memory dir), so day\n * transcripts are full-text searchable after the next index update.\n *\n * Files are rebuilt idempotently from provider data on every sync; the\n * body hash in frontmatter lets the pipeline skip rewriting (and\n * re-extracting) unchanged days.\n *\n * This module is pure composition/parsing — file IO lives in\n * `StorageManager` so encrypted-at-rest deployments and atomic write\n * semantics are inherited from the same code paths memories use.\n */\n\nimport { createHash } from \"node:crypto\";\n\nimport type { SpeakerRegistry } from \"./speakers.js\";\nimport { distinctSpeakerLabels, resolveSpeaker } from \"./speakers.js\";\nimport type {\n WearableConversation,\n WearableDayTranscript,\n WearableDayTranscriptMeta,\n} from \"./types.js\";\n\nexport const WEARABLES_DIR_NAME = \"wearables\";\n\nconst DATE_PATTERN = /^\\d{4}-\\d{2}-\\d{2}$/;\n\nexport function isValidTranscriptDate(date: string): boolean {\n if (!DATE_PATTERN.test(date)) return false;\n const parsed = new Date(`${date}T00:00:00Z`);\n return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === date;\n}\n\nexport function hashTranscriptBody(body: string): string {\n return createHash(\"sha256\").update(body, \"utf-8\").digest(\"hex\");\n}\n\nfunction formatClockTime(iso: string | undefined, timezone: string): string {\n if (!iso) return \"--:--\";\n const ms = Date.parse(iso);\n if (Number.isNaN(ms)) return \"--:--\";\n try {\n return new Intl.DateTimeFormat(\"en-US\", {\n timeZone: timezone,\n hour12: false,\n hour: \"2-digit\",\n minute: \"2-digit\",\n }).format(new Date(ms));\n } catch {\n // Unknown timezone identifiers fall back to UTC rather than\n // crashing a sync that already fetched data.\n return new Date(ms).toISOString().slice(11, 16);\n }\n}\n\nfunction conversationDurationMinutes(conversation: WearableConversation): number {\n const start = Date.parse(conversation.startIso);\n const end = conversation.endIso ? Date.parse(conversation.endIso) : NaN;\n if (Number.isNaN(start) || Number.isNaN(end) || end <= start) return 0;\n return (end - start) / 60_000;\n}\n\n/** Compose the markdown body (no frontmatter) for one source/day. */\nexport function composeDayTranscriptBody(\n sourceId: string,\n date: string,\n timezone: string,\n conversations: WearableConversation[],\n registry: SpeakerRegistry,\n): string {\n const lines: string[] = [];\n lines.push(`# ${sourceId} transcript — ${date}`);\n lines.push(\"\");\n const ordered = [...conversations].sort((a, b) => {\n const aMs = Date.parse(a.startIso);\n const bMs = Date.parse(b.startIso);\n if (aMs < bMs) return -1;\n if (aMs > bMs) return 1;\n // Stable secondary key so equal start times order deterministically.\n return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;\n });\n for (const conversation of ordered) {\n const start = formatClockTime(conversation.startIso, timezone);\n const end = formatClockTime(conversation.endIso, timezone);\n const title = conversation.title?.trim();\n const heading = title && title.length > 0 ? ` · ${title}` : \"\";\n lines.push(`## ${start}–${end}${heading} (conversation ${conversation.id})`);\n if (conversation.location) {\n lines.push(`*Location: ${conversation.location}*`);\n }\n lines.push(\"\");\n for (const segment of conversation.segments) {\n const { label } = resolveSpeaker(sourceId, segment, registry);\n const at = formatClockTime(segment.startIso, timezone);\n lines.push(`**${label}** [${at}]: ${segment.text}`);\n }\n lines.push(\"\");\n }\n return `${lines.join(\"\\n\").trimEnd()}\\n`;\n}\n\nexport function composeDayTranscriptMeta(\n sourceId: string,\n date: string,\n timezone: string,\n conversations: WearableConversation[],\n registry: SpeakerRegistry,\n body: string,\n syncedAt: string,\n): WearableDayTranscriptMeta {\n const allSegments = conversations.flatMap((c) => c.segments);\n const durationMinutes = Math.round(\n conversations.reduce((sum, c) => sum + conversationDurationMinutes(c), 0),\n );\n return {\n kind: \"wearable-transcript\",\n source: sourceId,\n date,\n timezone,\n conversationCount: conversations.length,\n segmentCount: allSegments.length,\n speakers: distinctSpeakerLabels(sourceId, allSegments, registry),\n durationMinutes,\n contentHash: hashTranscriptBody(body),\n syncedAt,\n };\n}\n\n/** Serialize meta + body into the persisted file format. */\nexport function serializeDayTranscript(\n meta: WearableDayTranscriptMeta,\n body: string,\n): string {\n const lines: string[] = [\"---\"];\n lines.push(`kind: ${meta.kind}`);\n lines.push(`source: ${JSON.stringify(meta.source)}`);\n lines.push(`date: ${JSON.stringify(meta.date)}`);\n lines.push(`timezone: ${JSON.stringify(meta.timezone)}`);\n lines.push(`conversationCount: ${meta.conversationCount}`);\n lines.push(`segmentCount: ${meta.segmentCount}`);\n if (meta.speakers.length === 0) {\n lines.push(\"speakers: []\");\n } else {\n lines.push(\"speakers:\");\n for (const speaker of meta.speakers) {\n lines.push(` - ${JSON.stringify(speaker)}`);\n }\n }\n lines.push(`durationMinutes: ${meta.durationMinutes}`);\n lines.push(`contentHash: ${JSON.stringify(meta.contentHash)}`);\n lines.push(`syncedAt: ${JSON.stringify(meta.syncedAt)}`);\n lines.push(\"---\");\n lines.push(\"\");\n return `${lines.join(\"\\n\")}${body}`;\n}\n\n/**\n * Parse a persisted day-transcript file. Returns null when the content\n * does not look like a wearable transcript (wrong kind, missing\n * frontmatter) so callers can distinguish \"not a transcript\" from a\n * read error.\n */\nexport function parseDayTranscript(raw: string): WearableDayTranscript | null {\n if (!raw.startsWith(\"---\\n\")) return null;\n const closeIndex = raw.indexOf(\"\\n---\\n\", 4);\n if (closeIndex === -1) return null;\n const header = raw.slice(4, closeIndex);\n const body = raw.slice(closeIndex + 5).replace(/^\\n/, \"\");\n\n const scalars = new Map<string, string>();\n const speakers: string[] = [];\n let inSpeakers = false;\n for (const line of header.split(\"\\n\")) {\n if (inSpeakers) {\n const item = line.match(/^ {2}- (.*)$/);\n if (item) {\n speakers.push(parseYamlScalar(item[1]));\n continue;\n }\n inSpeakers = false;\n }\n if (line === \"speakers:\") {\n inSpeakers = true;\n continue;\n }\n if (line === \"speakers: []\") continue;\n const match = line.match(/^([A-Za-z][A-Za-z0-9]*): (.*)$/);\n if (match) scalars.set(match[1], parseYamlScalar(match[2]));\n }\n\n if (scalars.get(\"kind\") !== \"wearable-transcript\") return null;\n const source = scalars.get(\"source\");\n const date = scalars.get(\"date\");\n if (!source || !date) return null;\n\n const meta: WearableDayTranscriptMeta = {\n kind: \"wearable-transcript\",\n source,\n date,\n timezone: scalars.get(\"timezone\") ?? \"UTC\",\n conversationCount: parseNonNegativeInt(scalars.get(\"conversationCount\")),\n segmentCount: parseNonNegativeInt(scalars.get(\"segmentCount\")),\n speakers,\n durationMinutes: parseNonNegativeInt(scalars.get(\"durationMinutes\")),\n contentHash: scalars.get(\"contentHash\") ?? \"\",\n syncedAt: scalars.get(\"syncedAt\") ?? \"\",\n };\n return { meta, body };\n}\n\nfunction parseYamlScalar(value: string): string {\n const trimmed = value.trim();\n if (trimmed.startsWith('\"') && trimmed.endsWith('\"') && trimmed.length >= 2) {\n try {\n const parsed = JSON.parse(trimmed);\n if (typeof parsed === \"string\") return parsed;\n } catch {\n // Fall through to the raw value below.\n }\n }\n return trimmed;\n}\n\nfunction parseNonNegativeInt(value: string | undefined): number {\n if (value === undefined) return 0;\n const parsed = Number(value);\n if (!Number.isFinite(parsed) || parsed < 0) return 0;\n return Math.floor(parsed);\n}\n"],"mappings":";AAiBA,SAAS,YAAY,kBAAkB;AACvC,YAAY,UAAU;AAiBf,IAAM,oBAAoB;AAE1B,SAAS,uBAAwC;AACtD,SAAO,EAAE,SAAS,GAAG,UAAU,mBAAmB,UAAU,CAAC,EAAE;AACjE;AAEO,SAAS,iBAAiB,WAA2B;AAC1D,SAAY,UAAK,WAAW,SAAS,aAAa,eAAe;AACnE;AAEA,eAAsB,oBACpB,WAC0B;AAC1B,QAAM,WAAW,iBAAiB,SAAS;AAC3C,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,WAAW,SAAS,UAAU,OAAO;AAAA,EACnD,SAAS,KAAK;AACZ,QAAK,IAA8B,SAAS,UAAU;AACpD,aAAO,qBAAqB;AAAA,IAC9B;AACA,UAAM;AAAA,EACR;AACA,MAAI;AACJ,MAAI;AACF,aAAS,KAAK,MAAM,GAAG;AAAA,EACzB,SAAS,KAAK;AACZ,UAAM,IAAI;AAAA,MACR,8EACE,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CACjD;AAAA,IACF;AAAA,EACF;AACA,MACE,OAAO,WAAW,YAClB,WAAW,QACX,MAAM,QAAQ,MAAM,KACpB,OAAQ,OAA2B,aAAa,YAC/C,OAA2B,aAAa,MACzC;AACA,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,QAAM,WAAW;AACjB,SAAO;AAAA,IACL,SAAS;AAAA,IACT,UACE,OAAO,SAAS,aAAa,YAAY,SAAS,SAAS,KAAK,EAAE,SAAS,IACvE,SAAS,SAAS,KAAK,IACvB;AAAA,IACN,UAAU,SAAS;AAAA,EACrB;AACF;AAEA,eAAsB,oBACpB,WACA,UAC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B,QAAQ,IAAI,mBAAmB,CAAC;AAAA,IACvE,cAAc,oBAAoB,QAAQ,IAAI,cAAc,CAAC;AAAA,IAC7D;AAAA,IACA,iBAAiB,oBAAoB,QAAQ,IAAI,iBAAiB,CAAC;AAAA,IACnE,aAAa,QAAQ,IAAI,aAAa,KAAK;AAAA,IAC3C,UAAU,QAAQ,IAAI,UAAU,KAAK;AAAA,EACvC;AACA,SAAO,EAAE,MAAM,KAAK;AACtB;AAEA,SAAS,gBAAgB,OAAuB;AAC9C,QAAM,UAAU,MAAM,KAAK;AAC3B,MAAI,QAAQ,WAAW,GAAG,KAAK,QAAQ,SAAS,GAAG,KAAK,QAAQ,UAAU,GAAG;AAC3E,QAAI;AACF,YAAM,SAAS,KAAK,MAAM,OAAO;AACjC,UAAI,OAAO,WAAW,SAAU,QAAO;AAAA,IACzC,QAAQ;AAAA,IAER;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,oBAAoB,OAAmC;AAC9D,MAAI,UAAU,OAAW,QAAO;AAChC,QAAM,SAAS,OAAO,KAAK;AAC3B,MAAI,CAAC,OAAO,SAAS,MAAM,KAAK,SAAS,EAAG,QAAO;AACnD,SAAO,KAAK,MAAM,MAAM;AAC1B;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  StorageManager
3
- } from "./chunk-RSKUUEBA.js";
3
+ } from "./chunk-QYGIQ5NM.js";
4
4
 
5
5
  // src/semantic-rule-promotion.ts
6
6
  import { createHash, randomUUID } from "crypto";
@@ -529,4 +529,4 @@ export {
529
529
  setSemanticRulePromotionTestHooks,
530
530
  promoteSemanticRuleFromMemory
531
531
  };
532
- //# sourceMappingURL=chunk-MQ24KOOR.js.map
532
+ //# sourceMappingURL=chunk-PUEAEQSN.js.map