moflo 4.0.4 → 4.1.0

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 (422) hide show
  1. package/.claude/agents/MIGRATION_SUMMARY.md +221 -221
  2. package/.claude/agents/analysis/analyze-code-quality.md +178 -178
  3. package/.claude/agents/analysis/code-analyzer.md +209 -209
  4. package/.claude/agents/analysis/code-review/analyze-code-quality.md +178 -178
  5. package/.claude/agents/architecture/system-design/arch-system-design.md +154 -154
  6. package/.claude/agents/base-template-generator.md +42 -42
  7. package/.claude/agents/consensus/byzantine-coordinator.md +62 -62
  8. package/.claude/agents/consensus/crdt-synchronizer.md +996 -996
  9. package/.claude/agents/consensus/gossip-coordinator.md +62 -62
  10. package/.claude/agents/consensus/performance-benchmarker.md +850 -850
  11. package/.claude/agents/consensus/quorum-manager.md +822 -822
  12. package/.claude/agents/consensus/raft-manager.md +62 -62
  13. package/.claude/agents/consensus/security-manager.md +621 -621
  14. package/.claude/agents/core/coder.md +265 -265
  15. package/.claude/agents/core/planner.md +167 -167
  16. package/.claude/agents/core/researcher.md +189 -189
  17. package/.claude/agents/core/reviewer.md +325 -325
  18. package/.claude/agents/core/tester.md +318 -318
  19. package/.claude/agents/custom/test-long-runner.md +44 -44
  20. package/.claude/agents/data/ml/data-ml-model.md +192 -192
  21. package/.claude/agents/development/backend/dev-backend-api.md +141 -141
  22. package/.claude/agents/development/dev-backend-api.md +344 -344
  23. package/.claude/agents/devops/ci-cd/ops-cicd-github.md +163 -163
  24. package/.claude/agents/documentation/api-docs/docs-api-openapi.md +173 -173
  25. package/.claude/agents/dual-mode/codex-coordinator.md +224 -224
  26. package/.claude/agents/dual-mode/codex-worker.md +211 -211
  27. package/.claude/agents/dual-mode/dual-orchestrator.md +291 -291
  28. package/.claude/agents/flow-nexus/app-store.md +87 -87
  29. package/.claude/agents/flow-nexus/authentication.md +68 -68
  30. package/.claude/agents/flow-nexus/challenges.md +80 -80
  31. package/.claude/agents/flow-nexus/neural-network.md +87 -87
  32. package/.claude/agents/flow-nexus/payments.md +82 -82
  33. package/.claude/agents/flow-nexus/sandbox.md +75 -75
  34. package/.claude/agents/flow-nexus/swarm.md +75 -75
  35. package/.claude/agents/flow-nexus/user-tools.md +95 -95
  36. package/.claude/agents/flow-nexus/workflow.md +83 -83
  37. package/.claude/agents/github/code-review-swarm.md +537 -537
  38. package/.claude/agents/github/github-modes.md +172 -172
  39. package/.claude/agents/github/issue-tracker.md +318 -318
  40. package/.claude/agents/github/multi-repo-swarm.md +552 -552
  41. package/.claude/agents/github/pr-manager.md +190 -190
  42. package/.claude/agents/github/project-board-sync.md +508 -508
  43. package/.claude/agents/github/release-manager.md +366 -366
  44. package/.claude/agents/github/release-swarm.md +582 -582
  45. package/.claude/agents/github/repo-architect.md +397 -397
  46. package/.claude/agents/github/swarm-issue.md +572 -572
  47. package/.claude/agents/github/swarm-pr.md +427 -427
  48. package/.claude/agents/github/sync-coordinator.md +451 -451
  49. package/.claude/agents/github/workflow-automation.md +634 -634
  50. package/.claude/agents/goal/agent.md +815 -815
  51. package/.claude/agents/goal/code-goal-planner.md +445 -445
  52. package/.claude/agents/goal/goal-planner.md +167 -167
  53. package/.claude/agents/hive-mind/collective-intelligence-coordinator.md +129 -129
  54. package/.claude/agents/hive-mind/queen-coordinator.md +202 -202
  55. package/.claude/agents/hive-mind/scout-explorer.md +241 -241
  56. package/.claude/agents/hive-mind/swarm-memory-manager.md +192 -192
  57. package/.claude/agents/hive-mind/worker-specialist.md +216 -216
  58. package/.claude/agents/neural/safla-neural.md +73 -73
  59. package/.claude/agents/optimization/benchmark-suite.md +664 -664
  60. package/.claude/agents/optimization/load-balancer.md +430 -430
  61. package/.claude/agents/optimization/performance-monitor.md +671 -671
  62. package/.claude/agents/optimization/resource-allocator.md +673 -673
  63. package/.claude/agents/optimization/topology-optimizer.md +807 -807
  64. package/.claude/agents/payments/agentic-payments.md +126 -126
  65. package/.claude/agents/reasoning/agent.md +815 -815
  66. package/.claude/agents/reasoning/goal-planner.md +72 -72
  67. package/.claude/agents/sona/sona-learning-optimizer.md +74 -74
  68. package/.claude/agents/sparc/architecture.md +471 -471
  69. package/.claude/agents/sparc/pseudocode.md +317 -317
  70. package/.claude/agents/sparc/refinement.md +524 -524
  71. package/.claude/agents/sparc/specification.md +275 -275
  72. package/.claude/agents/specialized/mobile/spec-mobile-react-native.md +224 -224
  73. package/.claude/agents/sublinear/consensus-coordinator.md +337 -337
  74. package/.claude/agents/sublinear/matrix-optimizer.md +184 -184
  75. package/.claude/agents/sublinear/pagerank-analyzer.md +298 -298
  76. package/.claude/agents/sublinear/performance-optimizer.md +367 -367
  77. package/.claude/agents/sublinear/trading-predictor.md +245 -245
  78. package/.claude/agents/swarm/adaptive-coordinator.md +395 -395
  79. package/.claude/agents/swarm/hierarchical-coordinator.md +326 -326
  80. package/.claude/agents/swarm/mesh-coordinator.md +391 -391
  81. package/.claude/agents/templates/automation-smart-agent.md +204 -204
  82. package/.claude/agents/templates/coordinator-swarm-init.md +104 -104
  83. package/.claude/agents/templates/github-pr-manager.md +176 -176
  84. package/.claude/agents/templates/implementer-sparc-coder.md +258 -258
  85. package/.claude/agents/templates/memory-coordinator.md +186 -186
  86. package/.claude/agents/templates/migration-plan.md +745 -745
  87. package/.claude/agents/templates/orchestrator-task.md +138 -138
  88. package/.claude/agents/templates/performance-analyzer.md +198 -198
  89. package/.claude/agents/templates/sparc-coordinator.md +182 -182
  90. package/.claude/agents/testing/production-validator.md +394 -394
  91. package/.claude/agents/testing/tdd-london-swarm.md +243 -243
  92. package/.claude/agents/testing/unit/tdd-london-swarm.md +243 -243
  93. package/.claude/agents/testing/validation/production-validator.md +394 -394
  94. package/.claude/agents/v3/v3-integration-architect.md +345 -345
  95. package/.claude/agents/v3/v3-memory-specialist.md +317 -317
  96. package/.claude/agents/v3/v3-performance-engineer.md +396 -396
  97. package/.claude/agents/v3/v3-queen-coordinator.md +97 -97
  98. package/.claude/agents/v3/v3-security-architect.md +173 -173
  99. package/.claude/commands/agents/README.md +10 -10
  100. package/.claude/commands/agents/agent-capabilities.md +21 -21
  101. package/.claude/commands/agents/agent-coordination.md +28 -28
  102. package/.claude/commands/agents/agent-spawning.md +28 -28
  103. package/.claude/commands/agents/agent-types.md +26 -26
  104. package/.claude/commands/analysis/COMMAND_COMPLIANCE_REPORT.md +53 -53
  105. package/.claude/commands/analysis/README.md +9 -9
  106. package/.claude/commands/analysis/bottleneck-detect.md +162 -162
  107. package/.claude/commands/analysis/performance-bottlenecks.md +58 -58
  108. package/.claude/commands/analysis/performance-report.md +25 -25
  109. package/.claude/commands/analysis/token-efficiency.md +44 -44
  110. package/.claude/commands/analysis/token-usage.md +25 -25
  111. package/.claude/commands/automation/README.md +9 -9
  112. package/.claude/commands/automation/auto-agent.md +122 -122
  113. package/.claude/commands/automation/self-healing.md +105 -105
  114. package/.claude/commands/automation/session-memory.md +89 -89
  115. package/.claude/commands/automation/smart-agents.md +72 -72
  116. package/.claude/commands/automation/smart-spawn.md +25 -25
  117. package/.claude/commands/automation/workflow-select.md +25 -25
  118. package/.claude/commands/claude-flow-help.md +103 -103
  119. package/.claude/commands/claude-flow-memory.md +107 -107
  120. package/.claude/commands/claude-flow-swarm.md +205 -205
  121. package/.claude/commands/coordination/README.md +9 -9
  122. package/.claude/commands/coordination/agent-spawn.md +25 -25
  123. package/.claude/commands/coordination/init.md +44 -44
  124. package/.claude/commands/coordination/orchestrate.md +43 -43
  125. package/.claude/commands/coordination/spawn.md +45 -45
  126. package/.claude/commands/coordination/swarm-init.md +85 -85
  127. package/.claude/commands/coordination/task-orchestrate.md +25 -25
  128. package/.claude/commands/flow-nexus/app-store.md +123 -123
  129. package/.claude/commands/flow-nexus/challenges.md +119 -119
  130. package/.claude/commands/flow-nexus/login-registration.md +64 -64
  131. package/.claude/commands/flow-nexus/neural-network.md +133 -133
  132. package/.claude/commands/flow-nexus/payments.md +115 -115
  133. package/.claude/commands/flow-nexus/sandbox.md +82 -82
  134. package/.claude/commands/flow-nexus/swarm.md +86 -86
  135. package/.claude/commands/flow-nexus/user-tools.md +151 -151
  136. package/.claude/commands/flow-nexus/workflow.md +114 -114
  137. package/.claude/commands/github/README.md +11 -11
  138. package/.claude/commands/github/code-review-swarm.md +513 -513
  139. package/.claude/commands/github/code-review.md +25 -25
  140. package/.claude/commands/github/github-modes.md +146 -146
  141. package/.claude/commands/github/github-swarm.md +121 -121
  142. package/.claude/commands/github/issue-tracker.md +291 -291
  143. package/.claude/commands/github/issue-triage.md +25 -25
  144. package/.claude/commands/github/multi-repo-swarm.md +518 -518
  145. package/.claude/commands/github/pr-enhance.md +26 -26
  146. package/.claude/commands/github/pr-manager.md +169 -169
  147. package/.claude/commands/github/project-board-sync.md +470 -470
  148. package/.claude/commands/github/release-manager.md +337 -337
  149. package/.claude/commands/github/release-swarm.md +543 -543
  150. package/.claude/commands/github/repo-analyze.md +25 -25
  151. package/.claude/commands/github/repo-architect.md +366 -366
  152. package/.claude/commands/github/swarm-issue.md +481 -481
  153. package/.claude/commands/github/swarm-pr.md +284 -284
  154. package/.claude/commands/github/sync-coordinator.md +300 -300
  155. package/.claude/commands/github/workflow-automation.md +441 -441
  156. package/.claude/commands/hive-mind/README.md +17 -17
  157. package/.claude/commands/hive-mind/hive-mind-consensus.md +8 -8
  158. package/.claude/commands/hive-mind/hive-mind-init.md +18 -18
  159. package/.claude/commands/hive-mind/hive-mind-memory.md +8 -8
  160. package/.claude/commands/hive-mind/hive-mind-metrics.md +8 -8
  161. package/.claude/commands/hive-mind/hive-mind-resume.md +8 -8
  162. package/.claude/commands/hive-mind/hive-mind-sessions.md +8 -8
  163. package/.claude/commands/hive-mind/hive-mind-spawn.md +21 -21
  164. package/.claude/commands/hive-mind/hive-mind-status.md +8 -8
  165. package/.claude/commands/hive-mind/hive-mind-stop.md +8 -8
  166. package/.claude/commands/hive-mind/hive-mind-wizard.md +8 -8
  167. package/.claude/commands/hive-mind/hive-mind.md +27 -27
  168. package/.claude/commands/hooks/README.md +11 -11
  169. package/.claude/commands/hooks/overview.md +57 -57
  170. package/.claude/commands/hooks/post-edit.md +117 -117
  171. package/.claude/commands/hooks/post-task.md +112 -112
  172. package/.claude/commands/hooks/pre-edit.md +113 -113
  173. package/.claude/commands/hooks/pre-task.md +111 -111
  174. package/.claude/commands/hooks/session-end.md +118 -118
  175. package/.claude/commands/hooks/setup.md +102 -102
  176. package/.claude/commands/memory/README.md +9 -9
  177. package/.claude/commands/memory/memory-persist.md +25 -25
  178. package/.claude/commands/memory/memory-search.md +25 -25
  179. package/.claude/commands/memory/memory-usage.md +25 -25
  180. package/.claude/commands/memory/neural.md +47 -47
  181. package/.claude/commands/monitoring/README.md +9 -9
  182. package/.claude/commands/monitoring/agent-metrics.md +25 -25
  183. package/.claude/commands/monitoring/agents.md +44 -44
  184. package/.claude/commands/monitoring/real-time-view.md +25 -25
  185. package/.claude/commands/monitoring/status.md +46 -46
  186. package/.claude/commands/monitoring/swarm-monitor.md +25 -25
  187. package/.claude/commands/optimization/README.md +9 -9
  188. package/.claude/commands/optimization/auto-topology.md +61 -61
  189. package/.claude/commands/optimization/cache-manage.md +25 -25
  190. package/.claude/commands/optimization/parallel-execute.md +25 -25
  191. package/.claude/commands/optimization/parallel-execution.md +49 -49
  192. package/.claude/commands/optimization/topology-optimize.md +25 -25
  193. package/.claude/commands/pair/README.md +260 -260
  194. package/.claude/commands/pair/commands.md +545 -545
  195. package/.claude/commands/pair/config.md +509 -509
  196. package/.claude/commands/pair/examples.md +511 -511
  197. package/.claude/commands/pair/modes.md +347 -347
  198. package/.claude/commands/pair/session.md +406 -406
  199. package/.claude/commands/pair/start.md +208 -208
  200. package/.claude/commands/sparc/analyzer.md +51 -51
  201. package/.claude/commands/sparc/architect.md +53 -53
  202. package/.claude/commands/sparc/ask.md +97 -97
  203. package/.claude/commands/sparc/batch-executor.md +54 -54
  204. package/.claude/commands/sparc/code.md +89 -89
  205. package/.claude/commands/sparc/coder.md +54 -54
  206. package/.claude/commands/sparc/debug.md +83 -83
  207. package/.claude/commands/sparc/debugger.md +54 -54
  208. package/.claude/commands/sparc/designer.md +53 -53
  209. package/.claude/commands/sparc/devops.md +109 -109
  210. package/.claude/commands/sparc/docs-writer.md +80 -80
  211. package/.claude/commands/sparc/documenter.md +54 -54
  212. package/.claude/commands/sparc/innovator.md +54 -54
  213. package/.claude/commands/sparc/integration.md +83 -83
  214. package/.claude/commands/sparc/mcp.md +117 -117
  215. package/.claude/commands/sparc/memory-manager.md +54 -54
  216. package/.claude/commands/sparc/optimizer.md +54 -54
  217. package/.claude/commands/sparc/orchestrator.md +131 -131
  218. package/.claude/commands/sparc/post-deployment-monitoring-mode.md +83 -83
  219. package/.claude/commands/sparc/refinement-optimization-mode.md +83 -83
  220. package/.claude/commands/sparc/researcher.md +54 -54
  221. package/.claude/commands/sparc/reviewer.md +54 -54
  222. package/.claude/commands/sparc/security-review.md +80 -80
  223. package/.claude/commands/sparc/sparc-modes.md +174 -174
  224. package/.claude/commands/sparc/sparc.md +111 -111
  225. package/.claude/commands/sparc/spec-pseudocode.md +80 -80
  226. package/.claude/commands/sparc/supabase-admin.md +348 -348
  227. package/.claude/commands/sparc/swarm-coordinator.md +54 -54
  228. package/.claude/commands/sparc/tdd.md +54 -54
  229. package/.claude/commands/sparc/tester.md +54 -54
  230. package/.claude/commands/sparc/tutorial.md +79 -79
  231. package/.claude/commands/sparc/workflow-manager.md +54 -54
  232. package/.claude/commands/sparc.md +166 -166
  233. package/.claude/commands/stream-chain/pipeline.md +120 -120
  234. package/.claude/commands/stream-chain/run.md +69 -69
  235. package/.claude/commands/swarm/README.md +15 -15
  236. package/.claude/commands/swarm/analysis.md +95 -95
  237. package/.claude/commands/swarm/development.md +96 -96
  238. package/.claude/commands/swarm/examples.md +168 -168
  239. package/.claude/commands/swarm/maintenance.md +102 -102
  240. package/.claude/commands/swarm/optimization.md +117 -117
  241. package/.claude/commands/swarm/research.md +136 -136
  242. package/.claude/commands/swarm/swarm-analysis.md +8 -8
  243. package/.claude/commands/swarm/swarm-background.md +8 -8
  244. package/.claude/commands/swarm/swarm-init.md +19 -19
  245. package/.claude/commands/swarm/swarm-modes.md +8 -8
  246. package/.claude/commands/swarm/swarm-monitor.md +8 -8
  247. package/.claude/commands/swarm/swarm-spawn.md +19 -19
  248. package/.claude/commands/swarm/swarm-status.md +8 -8
  249. package/.claude/commands/swarm/swarm-strategies.md +8 -8
  250. package/.claude/commands/swarm/swarm.md +27 -27
  251. package/.claude/commands/swarm/testing.md +131 -131
  252. package/.claude/commands/training/README.md +9 -9
  253. package/.claude/commands/training/model-update.md +25 -25
  254. package/.claude/commands/training/neural-patterns.md +73 -73
  255. package/.claude/commands/training/neural-train.md +25 -25
  256. package/.claude/commands/training/pattern-learn.md +25 -25
  257. package/.claude/commands/training/specialization.md +62 -62
  258. package/.claude/commands/truth/start.md +142 -142
  259. package/.claude/commands/verify/check.md +49 -49
  260. package/.claude/commands/verify/start.md +127 -127
  261. package/.claude/commands/workflows/README.md +9 -9
  262. package/.claude/commands/workflows/development.md +77 -77
  263. package/.claude/commands/workflows/research.md +62 -62
  264. package/.claude/commands/workflows/workflow-create.md +25 -25
  265. package/.claude/commands/workflows/workflow-execute.md +25 -25
  266. package/.claude/commands/workflows/workflow-export.md +25 -25
  267. package/.claude/config/v3-dependency-optimization.json +265 -265
  268. package/.claude/config/v3-performance-targets.json +250 -250
  269. package/.claude/helpers/README.md +96 -96
  270. package/.claude/helpers/aggressive-microcompact.mjs +36 -36
  271. package/.claude/helpers/auto-memory-hook.mjs +363 -363
  272. package/.claude/helpers/context-persistence-hook.mjs +1979 -1979
  273. package/.claude/helpers/github-safe.js +106 -106
  274. package/.claude/helpers/learning-service.mjs +1144 -1144
  275. package/.claude/helpers/metrics-db.mjs +488 -488
  276. package/.claude/helpers/patch-aggressive-prune.mjs +184 -184
  277. package/.claude/mcp.json +12 -12
  278. package/.claude/settings.json +2 -2
  279. package/.claude/skills/agentdb-advanced/SKILL.md +550 -550
  280. package/.claude/skills/agentdb-learning/SKILL.md +545 -545
  281. package/.claude/skills/agentdb-memory-patterns/SKILL.md +339 -339
  282. package/.claude/skills/agentdb-optimization/SKILL.md +509 -509
  283. package/.claude/skills/agentdb-vector-search/SKILL.md +339 -339
  284. package/.claude/skills/agentic-jujutsu/SKILL.md +645 -645
  285. package/.claude/skills/dual-mode/README.md +71 -71
  286. package/.claude/skills/dual-mode/dual-collect.md +103 -103
  287. package/.claude/skills/dual-mode/dual-coordinate.md +85 -85
  288. package/.claude/skills/dual-mode/dual-spawn.md +81 -81
  289. package/.claude/skills/flow-nexus-neural/SKILL.md +738 -738
  290. package/.claude/skills/flow-nexus-platform/SKILL.md +1157 -1157
  291. package/.claude/skills/flow-nexus-swarm/SKILL.md +610 -610
  292. package/.claude/skills/github-code-review/SKILL.md +1 -1
  293. package/.claude/skills/github-multi-repo/SKILL.md +2 -2
  294. package/.claude/skills/github-project-management/SKILL.md +1 -1
  295. package/.claude/skills/github-release-management/SKILL.md +2 -2
  296. package/.claude/skills/github-workflow-automation/SKILL.md +1 -1
  297. package/.claude/skills/hive-mind-advanced/SKILL.md +4 -4
  298. package/.claude/skills/hooks-automation/SKILL.md +1201 -1201
  299. package/.claude/skills/pair-programming/SKILL.md +1202 -1202
  300. package/.claude/skills/performance-analysis/SKILL.md +563 -563
  301. package/.claude/skills/reasoningbank-agentdb/SKILL.md +446 -446
  302. package/.claude/skills/reasoningbank-intelligence/SKILL.md +201 -201
  303. package/.claude/skills/skill-builder/SKILL.md +910 -910
  304. package/.claude/skills/sparc-methodology/SKILL.md +2 -2
  305. package/.claude/skills/stream-chain/SKILL.md +563 -563
  306. package/.claude/skills/swarm-advanced/SKILL.md +4 -4
  307. package/.claude/skills/swarm-orchestration/SKILL.md +179 -179
  308. package/.claude/skills/v3-cli-modernization/SKILL.md +871 -871
  309. package/.claude/skills/v3-core-implementation/SKILL.md +796 -796
  310. package/.claude/skills/v3-ddd-architecture/SKILL.md +441 -441
  311. package/.claude/skills/v3-integration-deep/SKILL.md +240 -240
  312. package/.claude/skills/v3-mcp-optimization/SKILL.md +776 -776
  313. package/.claude/skills/v3-memory-unification/SKILL.md +173 -173
  314. package/.claude/skills/v3-performance-optimization/SKILL.md +389 -389
  315. package/.claude/skills/v3-security-overhaul/SKILL.md +81 -81
  316. package/.claude/skills/v3-swarm-coordination/SKILL.md +339 -339
  317. package/.claude/skills/verification-quality/SKILL.md +649 -649
  318. package/.claude/skills/worker-benchmarks/skill.md +135 -135
  319. package/.claude/skills/worker-integration/skill.md +154 -154
  320. package/.claude/statusline.mjs +109 -109
  321. package/.claude-plugin/README.md +6 -6
  322. package/.claude-plugin/docs/INSTALLATION.md +4 -4
  323. package/.claude-plugin/docs/PLUGIN_SUMMARY.md +5 -5
  324. package/.claude-plugin/docs/QUICKSTART.md +1 -1
  325. package/.claude-plugin/docs/STRUCTURE.md +128 -128
  326. package/.claude-plugin/hooks/hooks.json +74 -74
  327. package/.claude-plugin/marketplace.json +5 -5
  328. package/.claude-plugin/plugin.json +4 -4
  329. package/README.md +148 -148
  330. package/bin/cli.js +12 -12
  331. package/bin/npx-repair.js +7 -7
  332. package/bin/npx-safe-launch.js +9 -9
  333. package/package.json +115 -114
  334. package/v3/@claude-flow/cli/README.md +5 -5
  335. package/v3/@claude-flow/cli/bin/cli.js +156 -156
  336. package/v3/@claude-flow/cli/bin/mcp-server.js +189 -189
  337. package/v3/@claude-flow/cli/dist/src/commands/analyze.d.ts +1 -1
  338. package/v3/@claude-flow/cli/dist/src/commands/analyze.js +1 -1
  339. package/v3/@claude-flow/cli/dist/src/commands/claims.d.ts +1 -1
  340. package/v3/@claude-flow/cli/dist/src/commands/claims.js +2 -2
  341. package/v3/@claude-flow/cli/dist/src/commands/completions.d.ts +1 -1
  342. package/v3/@claude-flow/cli/dist/src/commands/completions.js +1 -1
  343. package/v3/@claude-flow/cli/dist/src/commands/config.js +36 -2
  344. package/v3/@claude-flow/cli/dist/src/commands/daemon.js +54 -7
  345. package/v3/@claude-flow/cli/dist/src/commands/deployment.d.ts +1 -1
  346. package/v3/@claude-flow/cli/dist/src/commands/deployment.js +2 -2
  347. package/v3/@claude-flow/cli/dist/src/commands/doctor.d.ts +1 -1
  348. package/v3/@claude-flow/cli/dist/src/commands/doctor.js +1 -1
  349. package/v3/@claude-flow/cli/dist/src/commands/embeddings.d.ts +1 -1
  350. package/v3/@claude-flow/cli/dist/src/commands/embeddings.js +2 -2
  351. package/v3/@claude-flow/cli/dist/src/commands/hive-mind.js +90 -90
  352. package/v3/@claude-flow/cli/dist/src/commands/hooks.js +30 -112
  353. package/v3/@claude-flow/cli/dist/src/commands/init.js +6 -1
  354. package/v3/@claude-flow/cli/dist/src/commands/memory.js +30 -30
  355. package/v3/@claude-flow/cli/dist/src/commands/neural.d.ts +1 -1
  356. package/v3/@claude-flow/cli/dist/src/commands/neural.js +2 -2
  357. package/v3/@claude-flow/cli/dist/src/commands/orc.js +1 -0
  358. package/v3/@claude-flow/cli/dist/src/commands/performance.d.ts +1 -1
  359. package/v3/@claude-flow/cli/dist/src/commands/performance.js +2 -2
  360. package/v3/@claude-flow/cli/dist/src/commands/plugins.d.ts +1 -1
  361. package/v3/@claude-flow/cli/dist/src/commands/plugins.js +2 -2
  362. package/v3/@claude-flow/cli/dist/src/commands/providers.d.ts +1 -1
  363. package/v3/@claude-flow/cli/dist/src/commands/providers.js +2 -2
  364. package/v3/@claude-flow/cli/dist/src/commands/route.d.ts +1 -1
  365. package/v3/@claude-flow/cli/dist/src/commands/route.js +1 -1
  366. package/v3/@claude-flow/cli/dist/src/commands/ruvector/backup.js +23 -23
  367. package/v3/@claude-flow/cli/dist/src/commands/ruvector/benchmark.js +29 -29
  368. package/v3/@claude-flow/cli/dist/src/commands/ruvector/import.d.ts +1 -1
  369. package/v3/@claude-flow/cli/dist/src/commands/ruvector/import.js +1 -1
  370. package/v3/@claude-flow/cli/dist/src/commands/ruvector/index.d.ts +1 -1
  371. package/v3/@claude-flow/cli/dist/src/commands/ruvector/index.js +1 -1
  372. package/v3/@claude-flow/cli/dist/src/commands/ruvector/init.js +113 -113
  373. package/v3/@claude-flow/cli/dist/src/commands/ruvector/migrate.js +97 -97
  374. package/v3/@claude-flow/cli/dist/src/commands/ruvector/optimize.js +51 -51
  375. package/v3/@claude-flow/cli/dist/src/commands/ruvector/setup.d.ts +1 -1
  376. package/v3/@claude-flow/cli/dist/src/commands/ruvector/setup.js +2 -2
  377. package/v3/@claude-flow/cli/dist/src/commands/ruvector/status.js +36 -36
  378. package/v3/@claude-flow/cli/dist/src/commands/security.d.ts +1 -1
  379. package/v3/@claude-flow/cli/dist/src/commands/security.js +2 -2
  380. package/v3/@claude-flow/cli/dist/src/config/moflo-config.d.ts +13 -1
  381. package/v3/@claude-flow/cli/dist/src/config/moflo-config.js +93 -41
  382. package/v3/@claude-flow/cli/dist/src/index.d.ts +1 -1
  383. package/v3/@claude-flow/cli/dist/src/index.js +2 -2
  384. package/v3/@claude-flow/cli/dist/src/init/claudemd-generator.js +2 -2
  385. package/v3/@claude-flow/cli/dist/src/init/executor.js +3 -3
  386. package/v3/@claude-flow/cli/dist/src/init/helpers-generator.js +640 -640
  387. package/v3/@claude-flow/cli/dist/src/init/moflo-init.d.ts +10 -0
  388. package/v3/@claude-flow/cli/dist/src/init/moflo-init.js +101 -20
  389. package/v3/@claude-flow/cli/dist/src/init/settings-generator.js +2 -2
  390. package/v3/@claude-flow/cli/dist/src/init/statusline-generator.js +783 -783
  391. package/v3/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +30 -13
  392. package/v3/@claude-flow/cli/dist/src/mcp-tools/security-tools.d.ts +1 -1
  393. package/v3/@claude-flow/cli/dist/src/mcp-tools/security-tools.js +1 -1
  394. package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +61 -61
  395. package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.js +2212 -2263
  396. package/v3/@claude-flow/cli/dist/src/runtime/headless.js +28 -28
  397. package/v3/@claude-flow/cli/dist/src/ruvector/flash-attention.d.ts +1 -1
  398. package/v3/@claude-flow/cli/dist/src/ruvector/flash-attention.js +1 -1
  399. package/v3/@claude-flow/cli/dist/src/ruvector/vector-db.d.ts +1 -1
  400. package/v3/@claude-flow/cli/dist/src/ruvector/vector-db.js +1 -1
  401. package/v3/@claude-flow/cli/dist/src/services/agent-router.d.ts +14 -0
  402. package/v3/@claude-flow/cli/dist/src/services/agent-router.js +86 -7
  403. package/v3/@claude-flow/cli/dist/src/services/headless-worker-executor.js +84 -84
  404. package/v3/@claude-flow/cli/dist/src/services/index.d.ts +1 -3
  405. package/v3/@claude-flow/cli/dist/src/services/index.js +1 -2
  406. package/v3/@claude-flow/cli/dist/src/services/learning-service.js +54 -54
  407. package/v3/@claude-flow/cli/dist/src/services/ruvector-training.d.ts +1 -1
  408. package/v3/@claude-flow/cli/dist/src/services/ruvector-training.js +1 -1
  409. package/v3/@claude-flow/cli/dist/src/services/worker-daemon.d.ts +24 -3
  410. package/v3/@claude-flow/cli/dist/src/services/worker-daemon.js +123 -12
  411. package/v3/@claude-flow/cli/dist/src/services/workflow-gate.d.ts +1 -0
  412. package/v3/@claude-flow/cli/dist/src/services/workflow-gate.js +20 -5
  413. package/v3/@claude-flow/cli/dist/src/suggest.d.ts +1 -1
  414. package/v3/@claude-flow/cli/dist/src/suggest.js +1 -1
  415. package/v3/@claude-flow/cli/dist/src/transfer/deploy-seraphine.js +23 -23
  416. package/v3/@claude-flow/cli/package.json +6 -6
  417. package/v3/@claude-flow/guidance/README.md +6 -6
  418. package/v3/@claude-flow/guidance/package.json +1 -1
  419. package/v3/@claude-flow/memory/README.md +1 -1
  420. package/v3/@claude-flow/shared/README.md +1 -1
  421. package/v3/@claude-flow/shared/package.json +42 -42
  422. package/v3/README.md +3 -3
@@ -1,1979 +1,1979 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Context Persistence Hook (ADR-051)
4
- *
5
- * Intercepts Claude Code's PreCompact, SessionStart, and UserPromptSubmit
6
- * lifecycle events to persist conversation history in SQLite (primary),
7
- * RuVector PostgreSQL (optional), or JSON (fallback), enabling "infinite
8
- * context" across compaction boundaries.
9
- *
10
- * Backend priority:
11
- * 1. better-sqlite3 (native, WAL mode, indexed queries, ACID transactions)
12
- * 2. RuVector PostgreSQL (if RUVECTOR_* env vars set - TB-scale, GNN search)
13
- * 3. AgentDB from @claude-flow/memory (HNSW vector search)
14
- * 4. JsonFileBackend (zero dependencies, always works)
15
- *
16
- * Proactive archiving:
17
- * - UserPromptSubmit hook archives on every prompt, BEFORE context fills up
18
- * - PreCompact hook is a safety net that catches any remaining unarchived turns
19
- * - SessionStart hook restores context after compaction
20
- * - Together, compaction becomes invisible — no information is ever lost
21
- *
22
- * Usage:
23
- * node context-persistence-hook.mjs pre-compact # PreCompact: archive transcript
24
- * node context-persistence-hook.mjs session-start # SessionStart: restore context
25
- * node context-persistence-hook.mjs user-prompt-submit # UserPromptSubmit: proactive archive
26
- * node context-persistence-hook.mjs status # Show archive stats
27
- */
28
-
29
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
30
- import { createHash } from 'crypto';
31
- import { join, dirname } from 'path';
32
- import { fileURLToPath } from 'url';
33
- import { createRequire } from 'module';
34
-
35
- const __filename = fileURLToPath(import.meta.url);
36
- const __dirname = dirname(__filename);
37
- const PROJECT_ROOT = join(__dirname, '../..');
38
- const DATA_DIR = join(PROJECT_ROOT, '.claude-flow', 'data');
39
- const ARCHIVE_JSON_PATH = join(DATA_DIR, 'transcript-archive.json');
40
- const ARCHIVE_DB_PATH = join(DATA_DIR, 'transcript-archive.db');
41
-
42
- const NAMESPACE = 'transcript-archive';
43
- const RESTORE_BUDGET = parseInt(process.env.CLAUDE_FLOW_COMPACT_RESTORE_BUDGET || '4000', 10);
44
- const MAX_MESSAGES = 500;
45
- const BLOCK_COMPACTION = process.env.CLAUDE_FLOW_BLOCK_COMPACTION === 'true';
46
- const COMPACT_INSTRUCTION_BUDGET = parseInt(process.env.CLAUDE_FLOW_COMPACT_INSTRUCTION_BUDGET || '2000', 10);
47
- const RETENTION_DAYS = parseInt(process.env.CLAUDE_FLOW_RETENTION_DAYS || '30', 10);
48
- const AUTO_OPTIMIZE = process.env.CLAUDE_FLOW_AUTO_OPTIMIZE !== 'false'; // on by default
49
-
50
- // ============================================================================
51
- // Context Autopilot — prevent compaction by managing context size in real-time
52
- // ============================================================================
53
- const AUTOPILOT_ENABLED = process.env.CLAUDE_FLOW_CONTEXT_AUTOPILOT !== 'false'; // on by default
54
- const CONTEXT_WINDOW_TOKENS = parseInt(process.env.CLAUDE_FLOW_CONTEXT_WINDOW || '200000', 10);
55
- const AUTOPILOT_WARN_PCT = parseFloat(process.env.CLAUDE_FLOW_AUTOPILOT_WARN || '0.70');
56
- const AUTOPILOT_PRUNE_PCT = parseFloat(process.env.CLAUDE_FLOW_AUTOPILOT_PRUNE || '0.85');
57
- const AUTOPILOT_STATE_PATH = join(DATA_DIR, 'autopilot-state.json');
58
-
59
- // Approximate tokens per character (Claude averages ~3.5 chars per token)
60
- const CHARS_PER_TOKEN = 3.5;
61
-
62
- // Ensure data dir
63
- if (!existsSync(DATA_DIR)) mkdirSync(DATA_DIR, { recursive: true });
64
-
65
- // ============================================================================
66
- // SQLite Backend (better-sqlite3 — synchronous, fast, WAL mode)
67
- // ============================================================================
68
-
69
- class SQLiteBackend {
70
- constructor(dbPath) {
71
- this.dbPath = dbPath;
72
- this.db = null;
73
- }
74
-
75
- async initialize() {
76
- const require = createRequire(import.meta.url);
77
- const Database = require('better-sqlite3');
78
- this.db = new Database(this.dbPath);
79
-
80
- // Performance optimizations
81
- this.db.pragma('journal_mode = WAL');
82
- this.db.pragma('synchronous = NORMAL');
83
- this.db.pragma('cache_size = 5000');
84
- this.db.pragma('temp_store = MEMORY');
85
-
86
- // Create schema
87
- this.db.exec(`
88
- CREATE TABLE IF NOT EXISTS transcript_entries (
89
- id TEXT PRIMARY KEY,
90
- key TEXT NOT NULL,
91
- content TEXT NOT NULL,
92
- type TEXT NOT NULL DEFAULT 'episodic',
93
- namespace TEXT NOT NULL DEFAULT 'transcript-archive',
94
- tags TEXT NOT NULL DEFAULT '[]',
95
- metadata TEXT NOT NULL DEFAULT '{}',
96
- access_level TEXT NOT NULL DEFAULT 'private',
97
- created_at INTEGER NOT NULL,
98
- updated_at INTEGER NOT NULL,
99
- version INTEGER NOT NULL DEFAULT 1,
100
- access_count INTEGER NOT NULL DEFAULT 0,
101
- last_accessed_at INTEGER NOT NULL,
102
- content_hash TEXT,
103
- session_id TEXT,
104
- chunk_index INTEGER,
105
- summary TEXT
106
- );
107
-
108
- CREATE INDEX IF NOT EXISTS idx_te_namespace ON transcript_entries(namespace);
109
- CREATE INDEX IF NOT EXISTS idx_te_session ON transcript_entries(session_id);
110
- CREATE INDEX IF NOT EXISTS idx_te_hash ON transcript_entries(content_hash);
111
- CREATE INDEX IF NOT EXISTS idx_te_chunk ON transcript_entries(session_id, chunk_index);
112
- CREATE INDEX IF NOT EXISTS idx_te_created ON transcript_entries(created_at);
113
- `);
114
-
115
- // Schema migration: add confidence + embedding columns (self-learning support)
116
- try {
117
- this.db.exec(`ALTER TABLE transcript_entries ADD COLUMN confidence REAL NOT NULL DEFAULT 0.8`);
118
- } catch { /* column already exists */ }
119
- try {
120
- this.db.exec(`ALTER TABLE transcript_entries ADD COLUMN embedding BLOB`);
121
- } catch { /* column already exists */ }
122
- try {
123
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_te_confidence ON transcript_entries(confidence)`);
124
- } catch { /* index already exists */ }
125
-
126
- // Prepare statements for reuse
127
- this._stmts = {
128
- insert: this.db.prepare(`
129
- INSERT OR IGNORE INTO transcript_entries
130
- (id, key, content, type, namespace, tags, metadata, access_level,
131
- created_at, updated_at, version, access_count, last_accessed_at,
132
- content_hash, session_id, chunk_index, summary)
133
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
134
- `),
135
- queryByNamespace: this.db.prepare(
136
- 'SELECT * FROM transcript_entries WHERE namespace = ? ORDER BY created_at DESC'
137
- ),
138
- queryBySession: this.db.prepare(
139
- 'SELECT * FROM transcript_entries WHERE namespace = ? AND session_id = ? ORDER BY chunk_index DESC'
140
- ),
141
- countAll: this.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries'),
142
- countByNamespace: this.db.prepare(
143
- 'SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = ?'
144
- ),
145
- hashExists: this.db.prepare(
146
- 'SELECT 1 FROM transcript_entries WHERE content_hash = ? LIMIT 1'
147
- ),
148
- listNamespaces: this.db.prepare(
149
- 'SELECT DISTINCT namespace FROM transcript_entries'
150
- ),
151
- listSessions: this.db.prepare(
152
- 'SELECT session_id, COUNT(*) as cnt FROM transcript_entries WHERE namespace = ? GROUP BY session_id ORDER BY MAX(created_at) DESC'
153
- ),
154
- };
155
-
156
- this._bulkInsert = this.db.transaction((entries) => {
157
- for (const e of entries) {
158
- this._stmts.insert.run(
159
- e.id, e.key, e.content, e.type, e.namespace,
160
- JSON.stringify(e.tags), JSON.stringify(e.metadata), e.accessLevel,
161
- e.createdAt, e.updatedAt, e.version, e.accessCount, e.lastAccessedAt,
162
- e.metadata?.contentHash || null,
163
- e.metadata?.sessionId || null,
164
- e.metadata?.chunkIndex ?? null,
165
- e.metadata?.summary || null
166
- );
167
- }
168
- });
169
-
170
- // Optimization statements
171
- this._stmts.markAccessed = this.db.prepare(
172
- 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = ? WHERE id = ?'
173
- );
174
- this._stmts.pruneStale = this.db.prepare(
175
- 'DELETE FROM transcript_entries WHERE namespace = ? AND access_count = 0 AND created_at < ?'
176
- );
177
- this._stmts.queryByImportance = this.db.prepare(`
178
- SELECT *, (
179
- (CAST(access_count AS REAL) + 1) *
180
- (1.0 / (1.0 + (? - created_at) / 86400000.0)) *
181
- (CASE WHEN json_array_length(json_extract(metadata, '$.toolNames')) > 0 THEN 1.5 ELSE 1.0 END) *
182
- (CASE WHEN json_array_length(json_extract(metadata, '$.filePaths')) > 0 THEN 1.3 ELSE 1.0 END)
183
- ) AS importance_score
184
- FROM transcript_entries
185
- WHERE namespace = ? AND session_id = ?
186
- ORDER BY importance_score DESC
187
- `);
188
- this._stmts.allForSync = this.db.prepare(
189
- 'SELECT * FROM transcript_entries WHERE namespace = ? ORDER BY created_at ASC'
190
- );
191
- }
192
-
193
- async store(entry) {
194
- this._stmts.insert.run(
195
- entry.id, entry.key, entry.content, entry.type, entry.namespace,
196
- JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
197
- entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
198
- entry.metadata?.contentHash || null,
199
- entry.metadata?.sessionId || null,
200
- entry.metadata?.chunkIndex ?? null,
201
- entry.metadata?.summary || null
202
- );
203
- }
204
-
205
- async bulkInsert(entries) {
206
- this._bulkInsert(entries);
207
- }
208
-
209
- async query(opts) {
210
- let rows;
211
- if (opts?.namespace && opts?.sessionId) {
212
- rows = this._stmts.queryBySession.all(opts.namespace, opts.sessionId);
213
- } else if (opts?.namespace) {
214
- rows = this._stmts.queryByNamespace.all(opts.namespace);
215
- } else {
216
- rows = this.db.prepare('SELECT * FROM transcript_entries ORDER BY created_at DESC').all();
217
- }
218
- return rows.map(r => this._rowToEntry(r));
219
- }
220
-
221
- async queryBySession(namespace, sessionId) {
222
- const rows = this._stmts.queryBySession.all(namespace, sessionId);
223
- return rows.map(r => this._rowToEntry(r));
224
- }
225
-
226
- hashExists(hash) {
227
- return !!this._stmts.hashExists.get(hash);
228
- }
229
-
230
- async count(namespace) {
231
- if (namespace) {
232
- return this._stmts.countByNamespace.get(namespace).cnt;
233
- }
234
- return this._stmts.countAll.get().cnt;
235
- }
236
-
237
- async listNamespaces() {
238
- return this._stmts.listNamespaces.all().map(r => r.namespace);
239
- }
240
-
241
- async listSessions(namespace) {
242
- return this._stmts.listSessions.all(namespace || NAMESPACE);
243
- }
244
-
245
- markAccessed(ids) {
246
- const now = Date.now();
247
- const boostStmt = this.db.prepare(
248
- 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = ?, confidence = MIN(1.0, confidence + 0.03) WHERE id = ?'
249
- );
250
- for (const id of ids) {
251
- boostStmt.run(now, id);
252
- }
253
- }
254
-
255
- /**
256
- * Confidence decay: reduce confidence for entries not accessed recently.
257
- * Decay rate: 0.5% per hour (matches LearningBridge default).
258
- * Entries with confidence below 0.1 are floor-clamped.
259
- */
260
- decayConfidence(namespace, hoursElapsed = 1) {
261
- const decayRate = 0.005 * hoursElapsed;
262
- const result = this.db.prepare(
263
- 'UPDATE transcript_entries SET confidence = MAX(0.1, confidence - ?) WHERE namespace = ? AND confidence > 0.1'
264
- ).run(decayRate, namespace || NAMESPACE);
265
- return result.changes;
266
- }
267
-
268
- /**
269
- * Store embedding blob for an entry (768-dim Float32Array → Buffer).
270
- */
271
- storeEmbedding(id, embedding) {
272
- const buf = Buffer.from(embedding.buffer, embedding.byteOffset, embedding.byteLength);
273
- this.db.prepare('UPDATE transcript_entries SET embedding = ? WHERE id = ?').run(buf, id);
274
- }
275
-
276
- /**
277
- * Cosine similarity search across all entries with embeddings.
278
- * Handles both 384-dim (ONNX) and 768-dim (legacy hash) embeddings.
279
- * Returns top-k entries ranked by similarity to the query embedding.
280
- */
281
- semanticSearch(queryEmbedding, k = 10, namespace) {
282
- const rows = this.db.prepare(
283
- 'SELECT id, embedding, summary, session_id, chunk_index, confidence, access_count FROM transcript_entries WHERE namespace = ? AND embedding IS NOT NULL'
284
- ).all(namespace || NAMESPACE);
285
-
286
- const queryDim = queryEmbedding.length;
287
- const scored = [];
288
- for (const row of rows) {
289
- if (!row.embedding) continue;
290
- const stored = new Float32Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 4);
291
- // Only compare if dimensions match
292
- if (stored.length !== queryDim) continue;
293
- let dot = 0;
294
- for (let i = 0; i < queryDim; i++) {
295
- dot += queryEmbedding[i] * stored[i];
296
- }
297
- // Boost by confidence (self-learning signal)
298
- const score = dot * (row.confidence || 0.8);
299
- scored.push({ id: row.id, score, summary: row.summary, sessionId: row.session_id, chunkIndex: row.chunk_index, confidence: row.confidence, accessCount: row.access_count });
300
- }
301
-
302
- scored.sort((a, b) => b.score - a.score);
303
- return scored.slice(0, k);
304
- }
305
-
306
- /**
307
- * Smart pruning: prune by confidence instead of just age.
308
- * Removes entries with confidence <= threshold AND access_count = 0.
309
- */
310
- pruneByConfidence(namespace, threshold = 0.2) {
311
- const result = this.db.prepare(
312
- 'DELETE FROM transcript_entries WHERE namespace = ? AND confidence <= ? AND access_count = 0'
313
- ).run(namespace || NAMESPACE, threshold);
314
- return result.changes;
315
- }
316
-
317
- pruneStale(namespace, maxAgeDays) {
318
- const cutoff = Date.now() - (maxAgeDays * 24 * 60 * 60 * 1000);
319
- const result = this._stmts.pruneStale.run(namespace || NAMESPACE, cutoff);
320
- return result.changes;
321
- }
322
-
323
- queryByImportance(namespace, sessionId) {
324
- const now = Date.now();
325
- const rows = this._stmts.queryByImportance.all(now, namespace, sessionId);
326
- return rows.map(r => ({ ...this._rowToEntry(r), importanceScore: r.importance_score }));
327
- }
328
-
329
- allForSync(namespace) {
330
- const rows = this._stmts.allForSync.all(namespace || NAMESPACE);
331
- return rows.map(r => this._rowToEntry(r));
332
- }
333
-
334
- async shutdown() {
335
- if (this.db) {
336
- this.db.pragma('optimize');
337
- this.db.close();
338
- this.db = null;
339
- }
340
- }
341
-
342
- _rowToEntry(row) {
343
- return {
344
- id: row.id,
345
- key: row.key,
346
- content: row.content,
347
- type: row.type,
348
- namespace: row.namespace,
349
- tags: JSON.parse(row.tags),
350
- metadata: JSON.parse(row.metadata),
351
- accessLevel: row.access_level,
352
- createdAt: row.created_at,
353
- updatedAt: row.updated_at,
354
- version: row.version,
355
- accessCount: row.access_count,
356
- lastAccessedAt: row.last_accessed_at,
357
- references: [],
358
- };
359
- }
360
- }
361
-
362
- // ============================================================================
363
- // JSON File Backend (fallback when better-sqlite3 unavailable)
364
- // ============================================================================
365
-
366
- class JsonFileBackend {
367
- constructor(filePath) {
368
- this.filePath = filePath;
369
- this.entries = new Map();
370
- }
371
-
372
- async initialize() {
373
- if (existsSync(this.filePath)) {
374
- try {
375
- const data = JSON.parse(readFileSync(this.filePath, 'utf-8'));
376
- if (Array.isArray(data)) {
377
- for (const entry of data) this.entries.set(entry.id, entry);
378
- }
379
- } catch { /* start fresh */ }
380
- }
381
- }
382
-
383
- async store(entry) { this.entries.set(entry.id, entry); this._persist(); }
384
-
385
- async bulkInsert(entries) {
386
- for (const e of entries) this.entries.set(e.id, e);
387
- this._persist();
388
- }
389
-
390
- async query(opts) {
391
- let results = [...this.entries.values()];
392
- if (opts?.namespace) results = results.filter(e => e.namespace === opts.namespace);
393
- if (opts?.type) results = results.filter(e => e.type === opts.type);
394
- if (opts?.limit) results = results.slice(0, opts.limit);
395
- return results;
396
- }
397
-
398
- async queryBySession(namespace, sessionId) {
399
- return [...this.entries.values()]
400
- .filter(e => e.namespace === namespace && e.metadata?.sessionId === sessionId)
401
- .sort((a, b) => (b.metadata?.chunkIndex ?? 0) - (a.metadata?.chunkIndex ?? 0));
402
- }
403
-
404
- hashExists(hash) {
405
- for (const e of this.entries.values()) {
406
- if (e.metadata?.contentHash === hash) return true;
407
- }
408
- return false;
409
- }
410
-
411
- async count(namespace) {
412
- if (!namespace) return this.entries.size;
413
- let n = 0;
414
- for (const e of this.entries.values()) {
415
- if (e.namespace === namespace) n++;
416
- }
417
- return n;
418
- }
419
-
420
- async listNamespaces() {
421
- const ns = new Set();
422
- for (const e of this.entries.values()) ns.add(e.namespace || 'default');
423
- return [...ns];
424
- }
425
-
426
- async listSessions(namespace) {
427
- const sessions = new Map();
428
- for (const e of this.entries.values()) {
429
- if (e.namespace === (namespace || NAMESPACE) && e.metadata?.sessionId) {
430
- sessions.set(e.metadata.sessionId, (sessions.get(e.metadata.sessionId) || 0) + 1);
431
- }
432
- }
433
- return [...sessions.entries()].map(([session_id, cnt]) => ({ session_id, cnt }));
434
- }
435
-
436
- async shutdown() { this._persist(); }
437
-
438
- _persist() {
439
- try {
440
- writeFileSync(this.filePath, JSON.stringify([...this.entries.values()], null, 2), 'utf-8');
441
- } catch { /* best effort */ }
442
- }
443
- }
444
-
445
- // ============================================================================
446
- // RuVector PostgreSQL Backend (optional, TB-scale, GNN-enhanced)
447
- // ============================================================================
448
-
449
- class RuVectorBackend {
450
- constructor(config) {
451
- this.config = config;
452
- this.pool = null;
453
- }
454
-
455
- async initialize() {
456
- const pg = await import('pg');
457
- const Pool = pg.default?.Pool || pg.Pool;
458
- this.pool = new Pool({
459
- host: this.config.host,
460
- port: this.config.port || 5432,
461
- database: this.config.database,
462
- user: this.config.user,
463
- password: this.config.password,
464
- ssl: this.config.ssl || false,
465
- max: 3,
466
- idleTimeoutMillis: 10000,
467
- connectionTimeoutMillis: 3000,
468
- application_name: 'claude-flow-context-persistence',
469
- });
470
-
471
- // Test connection and create schema
472
- const client = await this.pool.connect();
473
- try {
474
- await client.query(`
475
- CREATE TABLE IF NOT EXISTS transcript_entries (
476
- id TEXT PRIMARY KEY,
477
- key TEXT NOT NULL,
478
- content TEXT NOT NULL,
479
- type TEXT NOT NULL DEFAULT 'episodic',
480
- namespace TEXT NOT NULL DEFAULT 'transcript-archive',
481
- tags JSONB NOT NULL DEFAULT '[]',
482
- metadata JSONB NOT NULL DEFAULT '{}',
483
- access_level TEXT NOT NULL DEFAULT 'private',
484
- created_at BIGINT NOT NULL,
485
- updated_at BIGINT NOT NULL,
486
- version INTEGER NOT NULL DEFAULT 1,
487
- access_count INTEGER NOT NULL DEFAULT 0,
488
- last_accessed_at BIGINT NOT NULL,
489
- content_hash TEXT,
490
- session_id TEXT,
491
- chunk_index INTEGER,
492
- summary TEXT,
493
- embedding vector(768)
494
- );
495
-
496
- CREATE INDEX IF NOT EXISTS idx_te_namespace ON transcript_entries(namespace);
497
- CREATE INDEX IF NOT EXISTS idx_te_session ON transcript_entries(session_id);
498
- CREATE INDEX IF NOT EXISTS idx_te_hash ON transcript_entries(content_hash);
499
- CREATE INDEX IF NOT EXISTS idx_te_chunk ON transcript_entries(session_id, chunk_index);
500
- CREATE INDEX IF NOT EXISTS idx_te_created ON transcript_entries(created_at);
501
- `);
502
- } finally {
503
- client.release();
504
- }
505
- }
506
-
507
- async store(entry) {
508
- const embeddingArr = entry._embedding
509
- ? `[${Array.from(entry._embedding).join(',')}]`
510
- : null;
511
- await this.pool.query(
512
- `INSERT INTO transcript_entries
513
- (id, key, content, type, namespace, tags, metadata, access_level,
514
- created_at, updated_at, version, access_count, last_accessed_at,
515
- content_hash, session_id, chunk_index, summary, embedding)
516
- VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)
517
- ON CONFLICT (id) DO NOTHING`,
518
- [
519
- entry.id, entry.key, entry.content, entry.type, entry.namespace,
520
- JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
521
- entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
522
- entry.metadata?.contentHash || null,
523
- entry.metadata?.sessionId || null,
524
- entry.metadata?.chunkIndex ?? null,
525
- entry.metadata?.summary || null,
526
- embeddingArr,
527
- ]
528
- );
529
- }
530
-
531
- async bulkInsert(entries) {
532
- const client = await this.pool.connect();
533
- try {
534
- await client.query('BEGIN');
535
- for (const entry of entries) {
536
- const embeddingArr = entry._embedding
537
- ? `[${Array.from(entry._embedding).join(',')}]`
538
- : null;
539
- await client.query(
540
- `INSERT INTO transcript_entries
541
- (id, key, content, type, namespace, tags, metadata, access_level,
542
- created_at, updated_at, version, access_count, last_accessed_at,
543
- content_hash, session_id, chunk_index, summary, embedding)
544
- VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)
545
- ON CONFLICT (id) DO NOTHING`,
546
- [
547
- entry.id, entry.key, entry.content, entry.type, entry.namespace,
548
- JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
549
- entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
550
- entry.metadata?.contentHash || null,
551
- entry.metadata?.sessionId || null,
552
- entry.metadata?.chunkIndex ?? null,
553
- entry.metadata?.summary || null,
554
- embeddingArr,
555
- ]
556
- );
557
- }
558
- await client.query('COMMIT');
559
- } catch (err) {
560
- await client.query('ROLLBACK');
561
- throw err;
562
- } finally {
563
- client.release();
564
- }
565
- }
566
-
567
- async query(opts) {
568
- let sql = 'SELECT * FROM transcript_entries';
569
- const params = [];
570
- const clauses = [];
571
- if (opts?.namespace) { params.push(opts.namespace); clauses.push(`namespace = $${params.length}`); }
572
- if (clauses.length) sql += ' WHERE ' + clauses.join(' AND ');
573
- sql += ' ORDER BY created_at DESC';
574
- if (opts?.limit) { params.push(opts.limit); sql += ` LIMIT $${params.length}`; }
575
- const { rows } = await this.pool.query(sql, params);
576
- return rows.map(r => this._rowToEntry(r));
577
- }
578
-
579
- async queryBySession(namespace, sessionId) {
580
- const { rows } = await this.pool.query(
581
- 'SELECT * FROM transcript_entries WHERE namespace = $1 AND session_id = $2 ORDER BY chunk_index DESC',
582
- [namespace, sessionId]
583
- );
584
- return rows.map(r => this._rowToEntry(r));
585
- }
586
-
587
- hashExists(hash) {
588
- // Synchronous check not possible with pg — use a cached check
589
- // The bulkInsert uses ON CONFLICT DO NOTHING for dedup at DB level
590
- return false;
591
- }
592
-
593
- async hashExistsAsync(hash) {
594
- const { rows } = await this.pool.query(
595
- 'SELECT 1 FROM transcript_entries WHERE content_hash = $1 LIMIT 1',
596
- [hash]
597
- );
598
- return rows.length > 0;
599
- }
600
-
601
- async count(namespace) {
602
- const sql = namespace
603
- ? 'SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = $1'
604
- : 'SELECT COUNT(*) as cnt FROM transcript_entries';
605
- const params = namespace ? [namespace] : [];
606
- const { rows } = await this.pool.query(sql, params);
607
- return parseInt(rows[0].cnt, 10);
608
- }
609
-
610
- async listNamespaces() {
611
- const { rows } = await this.pool.query('SELECT DISTINCT namespace FROM transcript_entries');
612
- return rows.map(r => r.namespace);
613
- }
614
-
615
- async listSessions(namespace) {
616
- const { rows } = await this.pool.query(
617
- `SELECT session_id, COUNT(*) as cnt FROM transcript_entries
618
- WHERE namespace = $1 GROUP BY session_id ORDER BY MAX(created_at) DESC`,
619
- [namespace || NAMESPACE]
620
- );
621
- return rows.map(r => ({ session_id: r.session_id, cnt: parseInt(r.cnt, 10) }));
622
- }
623
-
624
- async markAccessed(ids) {
625
- const now = Date.now();
626
- for (const id of ids) {
627
- await this.pool.query(
628
- 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = $1 WHERE id = $2',
629
- [now, id]
630
- );
631
- }
632
- }
633
-
634
- async pruneStale(namespace, maxAgeDays) {
635
- const cutoff = Date.now() - (maxAgeDays * 24 * 60 * 60 * 1000);
636
- const { rowCount } = await this.pool.query(
637
- 'DELETE FROM transcript_entries WHERE namespace = $1 AND access_count = 0 AND created_at < $2',
638
- [namespace || NAMESPACE, cutoff]
639
- );
640
- return rowCount;
641
- }
642
-
643
- async queryByImportance(namespace, sessionId) {
644
- const now = Date.now();
645
- const { rows } = await this.pool.query(`
646
- SELECT *, (
647
- (CAST(access_count AS REAL) + 1) *
648
- (1.0 / (1.0 + ($1 - created_at) / 86400000.0)) *
649
- (CASE WHEN jsonb_array_length(metadata->'toolNames') > 0 THEN 1.5 ELSE 1.0 END) *
650
- (CASE WHEN jsonb_array_length(metadata->'filePaths') > 0 THEN 1.3 ELSE 1.0 END)
651
- ) AS importance_score
652
- FROM transcript_entries
653
- WHERE namespace = $2 AND session_id = $3
654
- ORDER BY importance_score DESC
655
- `, [now, namespace, sessionId]);
656
- return rows.map(r => ({ ...this._rowToEntry(r), importanceScore: r.importance_score }));
657
- }
658
-
659
- async shutdown() {
660
- if (this.pool) {
661
- await this.pool.end();
662
- this.pool = null;
663
- }
664
- }
665
-
666
- _rowToEntry(row) {
667
- return {
668
- id: row.id,
669
- key: row.key,
670
- content: row.content,
671
- type: row.type,
672
- namespace: row.namespace,
673
- tags: typeof row.tags === 'string' ? JSON.parse(row.tags) : row.tags,
674
- metadata: typeof row.metadata === 'string' ? JSON.parse(row.metadata) : row.metadata,
675
- accessLevel: row.access_level,
676
- createdAt: parseInt(row.created_at, 10),
677
- updatedAt: parseInt(row.updated_at, 10),
678
- version: row.version,
679
- accessCount: row.access_count,
680
- lastAccessedAt: parseInt(row.last_accessed_at, 10),
681
- references: [],
682
- };
683
- }
684
- }
685
-
686
- /**
687
- * Parse RuVector config from environment variables.
688
- * Returns null if required vars are not set.
689
- */
690
- function getRuVectorConfig() {
691
- const host = process.env.RUVECTOR_HOST || process.env.PGHOST;
692
- const database = process.env.RUVECTOR_DATABASE || process.env.PGDATABASE;
693
- const user = process.env.RUVECTOR_USER || process.env.PGUSER;
694
- const password = process.env.RUVECTOR_PASSWORD || process.env.PGPASSWORD;
695
-
696
- if (!host || !database || !user) return null;
697
-
698
- return {
699
- host,
700
- port: parseInt(process.env.RUVECTOR_PORT || process.env.PGPORT || '5432', 10),
701
- database,
702
- user,
703
- password: password || '',
704
- ssl: process.env.RUVECTOR_SSL === 'true',
705
- };
706
- }
707
-
708
- // ============================================================================
709
- // Backend resolution: SQLite > RuVector PostgreSQL > AgentDB > JSON
710
- // ============================================================================
711
-
712
- async function resolveBackend() {
713
- // Tier 1: better-sqlite3 (native, fastest, local)
714
- try {
715
- const backend = new SQLiteBackend(ARCHIVE_DB_PATH);
716
- await backend.initialize();
717
- return { backend, type: 'sqlite' };
718
- } catch { /* fall through */ }
719
-
720
- // Tier 2: RuVector PostgreSQL (TB-scale, vector search, GNN)
721
- try {
722
- const rvConfig = getRuVectorConfig();
723
- if (rvConfig) {
724
- const backend = new RuVectorBackend(rvConfig);
725
- await backend.initialize();
726
- return { backend, type: 'ruvector' };
727
- }
728
- } catch { /* fall through */ }
729
-
730
- // Tier 3: AgentDB from @claude-flow/memory (HNSW)
731
- try {
732
- const localDist = join(PROJECT_ROOT, 'v3/@claude-flow/memory/dist/index.js');
733
- let memPkg = null;
734
- if (existsSync(localDist)) {
735
- memPkg = await import(`file://${localDist}`);
736
- } else {
737
- memPkg = await import('@claude-flow/memory');
738
- }
739
- if (memPkg?.AgentDBBackend) {
740
- const backend = new memPkg.AgentDBBackend();
741
- await backend.initialize();
742
- return { backend, type: 'agentdb' };
743
- }
744
- } catch { /* fall through */ }
745
-
746
- // Tier 4: JSON file (always works)
747
- const backend = new JsonFileBackend(ARCHIVE_JSON_PATH);
748
- await backend.initialize();
749
- return { backend, type: 'json' };
750
- }
751
-
752
- // ============================================================================
753
- // ONNX Embedding (384-dim, all-MiniLM-L6-v2 via @xenova/transformers)
754
- // ============================================================================
755
-
756
- const EMBEDDING_DIM = 384; // ONNX all-MiniLM-L6-v2 output dimension
757
- let _onnxPipeline = null;
758
- let _onnxFailed = false;
759
-
760
- /**
761
- * Initialize ONNX embedding pipeline (lazy, cached).
762
- * Returns null if @xenova/transformers is not available.
763
- */
764
- async function getOnnxPipeline() {
765
- if (_onnxFailed) return null;
766
- if (_onnxPipeline) return _onnxPipeline;
767
- try {
768
- const { pipeline } = await import('@xenova/transformers');
769
- _onnxPipeline = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
770
- return _onnxPipeline;
771
- } catch {
772
- _onnxFailed = true;
773
- return null;
774
- }
775
- }
776
-
777
- /**
778
- * Generate ONNX embedding (384-dim, high quality semantic vectors).
779
- * Falls back to hash embedding if ONNX is unavailable.
780
- */
781
- async function createEmbedding(text) {
782
- // Try ONNX first (384-dim, real semantic understanding)
783
- const pipe = await getOnnxPipeline();
784
- if (pipe) {
785
- try {
786
- const truncated = text.slice(0, 512); // MiniLM max ~512 tokens
787
- const output = await pipe(truncated, { pooling: 'mean', normalize: true });
788
- return { embedding: new Float32Array(output.data), dim: 384, method: 'onnx' };
789
- } catch { /* fall through to hash */ }
790
- }
791
- // Fallback: hash embedding (384-dim to match ONNX dimension)
792
- return { embedding: createHashEmbedding(text, 384), dim: 384, method: 'hash' };
793
- }
794
-
795
- // ============================================================================
796
- // Hash embedding fallback (deterministic, sub-millisecond)
797
- // ============================================================================
798
-
799
- function createHashEmbedding(text, dimensions = 384) {
800
- const embedding = new Float32Array(dimensions);
801
- const normalized = text.toLowerCase().trim();
802
- for (let i = 0; i < dimensions; i++) {
803
- let hash = 0;
804
- for (let j = 0; j < normalized.length; j++) {
805
- hash = ((hash << 5) - hash + normalized.charCodeAt(j) * (i + 1)) | 0;
806
- }
807
- embedding[i] = (Math.sin(hash) + 1) / 2;
808
- }
809
- let norm = 0;
810
- for (let i = 0; i < dimensions; i++) norm += embedding[i] * embedding[i];
811
- norm = Math.sqrt(norm);
812
- if (norm > 0) for (let i = 0; i < dimensions; i++) embedding[i] /= norm;
813
- return embedding;
814
- }
815
-
816
- // ============================================================================
817
- // Content hash for dedup
818
- // ============================================================================
819
-
820
- function hashContent(content) {
821
- return createHash('sha256').update(content).digest('hex');
822
- }
823
-
824
- // ============================================================================
825
- // Read stdin with timeout (hooks receive JSON input on stdin)
826
- // ============================================================================
827
-
828
- function readStdin(timeoutMs = 100) {
829
- return new Promise((resolve) => {
830
- let data = '';
831
- const timer = setTimeout(() => {
832
- process.stdin.removeAllListeners();
833
- resolve(data ? JSON.parse(data) : null);
834
- }, timeoutMs);
835
-
836
- if (process.stdin.isTTY) {
837
- clearTimeout(timer);
838
- resolve(null);
839
- return;
840
- }
841
-
842
- process.stdin.setEncoding('utf-8');
843
- process.stdin.on('data', (chunk) => { data += chunk; });
844
- process.stdin.on('end', () => {
845
- clearTimeout(timer);
846
- try { resolve(data ? JSON.parse(data) : null); }
847
- catch { resolve(null); }
848
- });
849
- process.stdin.on('error', () => {
850
- clearTimeout(timer);
851
- resolve(null);
852
- });
853
- process.stdin.resume();
854
- });
855
- }
856
-
857
- // ============================================================================
858
- // Transcript parsing
859
- // ============================================================================
860
-
861
- function parseTranscript(transcriptPath) {
862
- if (!existsSync(transcriptPath)) return [];
863
- const content = readFileSync(transcriptPath, 'utf-8');
864
- const lines = content.split('\n').filter(Boolean);
865
- const messages = [];
866
- for (const line of lines) {
867
- try {
868
- const parsed = JSON.parse(line);
869
- // SDK transcript wraps messages: { type: "user"|"A", message: { role, content } }
870
- // Unwrap to get the inner API message with role/content
871
- if (parsed.message && parsed.message.role) {
872
- messages.push(parsed.message);
873
- } else if (parsed.role) {
874
- // Already in API message format (e.g. from tests)
875
- messages.push(parsed);
876
- }
877
- // Skip non-message entries (progress, file-history-snapshot, queue-operation)
878
- } catch { /* skip malformed lines */ }
879
- }
880
- return messages;
881
- }
882
-
883
- // ============================================================================
884
- // Extract text content from message content blocks
885
- // ============================================================================
886
-
887
- function extractTextContent(message) {
888
- if (!message) return '';
889
- if (typeof message.content === 'string') return message.content;
890
- if (Array.isArray(message.content)) {
891
- return message.content
892
- .filter(b => b.type === 'text')
893
- .map(b => b.text || '')
894
- .join('\n');
895
- }
896
- if (typeof message.text === 'string') return message.text;
897
- return '';
898
- }
899
-
900
- // ============================================================================
901
- // Extract tool calls from assistant message
902
- // ============================================================================
903
-
904
- function extractToolCalls(message) {
905
- if (!message || !Array.isArray(message.content)) return [];
906
- return message.content
907
- .filter(b => b.type === 'tool_use')
908
- .map(b => ({
909
- name: b.name || 'unknown',
910
- input: b.input || {},
911
- }));
912
- }
913
-
914
- // ============================================================================
915
- // Extract file paths from tool calls
916
- // ============================================================================
917
-
918
- function extractFilePaths(toolCalls) {
919
- const paths = new Set();
920
- for (const tc of toolCalls) {
921
- if (tc.input?.file_path) paths.add(tc.input.file_path);
922
- if (tc.input?.path) paths.add(tc.input.path);
923
- if (tc.input?.notebook_path) paths.add(tc.input.notebook_path);
924
- }
925
- return [...paths];
926
- }
927
-
928
- // ============================================================================
929
- // Chunk transcript into conversation turns
930
- // ============================================================================
931
-
932
- function chunkTranscript(messages) {
933
- const relevant = messages.filter(
934
- m => m.role === 'user' || m.role === 'assistant'
935
- );
936
- const capped = relevant.slice(-MAX_MESSAGES);
937
-
938
- const chunks = [];
939
- let currentChunk = null;
940
-
941
- for (const msg of capped) {
942
- if (msg.role === 'user') {
943
- const isSynthetic = Array.isArray(msg.content) &&
944
- msg.content.every(b => b.type === 'tool_result');
945
- if (isSynthetic && currentChunk) continue;
946
- if (currentChunk) chunks.push(currentChunk);
947
- currentChunk = {
948
- userMessage: msg,
949
- assistantMessage: null,
950
- toolCalls: [],
951
- turnIndex: chunks.length,
952
- };
953
- } else if (msg.role === 'assistant' && currentChunk) {
954
- currentChunk.assistantMessage = msg;
955
- currentChunk.toolCalls = extractToolCalls(msg);
956
- }
957
- }
958
-
959
- if (currentChunk) chunks.push(currentChunk);
960
- return chunks;
961
- }
962
-
963
- // ============================================================================
964
- // Extract summary from chunk (no LLM, extractive only)
965
- // ============================================================================
966
-
967
- function extractSummary(chunk) {
968
- const parts = [];
969
-
970
- const userText = extractTextContent(chunk.userMessage);
971
- const firstUserLine = userText.split('\n').find(l => l.trim()) || '';
972
- if (firstUserLine) parts.push(firstUserLine.slice(0, 100));
973
-
974
- const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
975
- if (toolNames.length) parts.push('Tools: ' + toolNames.join(', '));
976
-
977
- const filePaths = extractFilePaths(chunk.toolCalls);
978
- if (filePaths.length) {
979
- const shortPaths = filePaths.slice(0, 5).map(p => {
980
- const segs = p.split('/');
981
- return segs.length > 2 ? '.../' + segs.slice(-2).join('/') : p;
982
- });
983
- parts.push('Files: ' + shortPaths.join(', '));
984
- }
985
-
986
- const assistantText = extractTextContent(chunk.assistantMessage);
987
- const assistantLines = assistantText.split('\n').filter(l => l.trim()).slice(0, 2);
988
- if (assistantLines.length) parts.push(assistantLines.join(' ').slice(0, 120));
989
-
990
- return parts.join(' | ').slice(0, 300);
991
- }
992
-
993
- // ============================================================================
994
- // Generate unique ID
995
- // ============================================================================
996
-
997
- let idCounter = 0;
998
- function generateId() {
999
- return `ctx-${Date.now()}-${++idCounter}-${Math.random().toString(36).slice(2, 8)}`;
1000
- }
1001
-
1002
- // ============================================================================
1003
- // Build MemoryEntry from chunk
1004
- // ============================================================================
1005
-
1006
- function buildEntry(chunk, sessionId, trigger, timestamp) {
1007
- const userText = extractTextContent(chunk.userMessage);
1008
- const assistantText = extractTextContent(chunk.assistantMessage);
1009
- const fullContent = `User: ${userText}\n\nAssistant: ${assistantText}`;
1010
- const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1011
- const filePaths = extractFilePaths(chunk.toolCalls);
1012
- const summary = extractSummary(chunk);
1013
- const contentHash = hashContent(fullContent);
1014
-
1015
- const now = Date.now();
1016
- return {
1017
- id: generateId(),
1018
- key: `transcript:${sessionId}:${chunk.turnIndex}:${timestamp}`,
1019
- content: fullContent,
1020
- type: 'episodic',
1021
- namespace: NAMESPACE,
1022
- tags: ['transcript', 'compaction', sessionId, ...toolNames],
1023
- metadata: {
1024
- sessionId,
1025
- chunkIndex: chunk.turnIndex,
1026
- trigger,
1027
- timestamp,
1028
- toolNames,
1029
- filePaths,
1030
- summary,
1031
- contentHash,
1032
- turnRange: [chunk.turnIndex, chunk.turnIndex],
1033
- },
1034
- accessLevel: 'private',
1035
- createdAt: now,
1036
- updatedAt: now,
1037
- version: 1,
1038
- references: [],
1039
- accessCount: 0,
1040
- lastAccessedAt: now,
1041
- };
1042
- }
1043
-
1044
- // ============================================================================
1045
- // Store chunks with dedup (uses indexed hash lookup for SQLite)
1046
- // ============================================================================
1047
-
1048
- async function storeChunks(backend, chunks, sessionId, trigger) {
1049
- const timestamp = new Date().toISOString();
1050
-
1051
- const entries = [];
1052
- for (const chunk of chunks) {
1053
- const entry = buildEntry(chunk, sessionId, trigger, timestamp);
1054
- // Fast hash-based dedup (indexed lookup in SQLite, scan in JSON)
1055
- if (!backend.hashExists(entry.metadata.contentHash)) {
1056
- entries.push(entry);
1057
- }
1058
- }
1059
-
1060
- if (entries.length > 0) {
1061
- await backend.bulkInsert(entries);
1062
- }
1063
-
1064
- return { stored: entries.length, deduped: chunks.length - entries.length };
1065
- }
1066
-
1067
- // ============================================================================
1068
- // Retrieve context for restoration (uses indexed session query for SQLite)
1069
- // ============================================================================
1070
-
1071
- async function retrieveContext(backend, sessionId, budget) {
1072
- // Use optimized session query if available, otherwise filter manually
1073
- const sessionEntries = backend.queryBySession
1074
- ? await backend.queryBySession(NAMESPACE, sessionId)
1075
- : (await backend.query({ namespace: NAMESPACE }))
1076
- .filter(e => e.metadata?.sessionId === sessionId)
1077
- .sort((a, b) => (b.metadata?.chunkIndex ?? 0) - (a.metadata?.chunkIndex ?? 0));
1078
-
1079
- if (sessionEntries.length === 0) return '';
1080
-
1081
- const lines = [];
1082
- let charCount = 0;
1083
- const header = `## Restored Context (from pre-compaction archive)\n\nPrevious conversation included ${sessionEntries.length} archived turns:\n\n`;
1084
- charCount += header.length;
1085
-
1086
- for (const entry of sessionEntries) {
1087
- const meta = entry.metadata || {};
1088
- const toolStr = meta.toolNames?.length ? ` Tools: ${meta.toolNames.join(', ')}.` : '';
1089
- const fileStr = meta.filePaths?.length ? ` Files: ${meta.filePaths.slice(0, 3).join(', ')}.` : '';
1090
- const line = `- [Turn ${meta.chunkIndex ?? '?'}] ${meta.summary || '(no summary)'}${toolStr}${fileStr}`;
1091
-
1092
- if (charCount + line.length + 1 > budget) break;
1093
- lines.push(line);
1094
- charCount += line.length + 1;
1095
- }
1096
-
1097
- if (lines.length === 0) return '';
1098
-
1099
- const footer = `\n\nFull archive: ${NAMESPACE} namespace in AgentDB (query with session ID: ${sessionId})`;
1100
- return header + lines.join('\n') + footer;
1101
- }
1102
-
1103
- // ============================================================================
1104
- // Build custom compact instructions (exit code 0 stdout)
1105
- // Guides Claude on what to preserve during compaction summary
1106
- // ============================================================================
1107
-
1108
- function buildCompactInstructions(chunks, sessionId, archiveResult) {
1109
- const parts = [];
1110
-
1111
- parts.push('COMPACTION GUIDANCE (from context-persistence-hook):');
1112
- parts.push('');
1113
- parts.push(`All ${chunks.length} conversation turns have been archived to the transcript-archive database.`);
1114
- parts.push(`Session: ${sessionId} | Stored: ${archiveResult.stored} new, ${archiveResult.deduped} deduped.`);
1115
- parts.push('After compaction, archived context will be automatically restored via SessionStart hook.');
1116
- parts.push('');
1117
-
1118
- // Collect unique tools and files across all chunks for preservation hints
1119
- const allTools = new Set();
1120
- const allFiles = new Set();
1121
- const decisions = [];
1122
-
1123
- for (const chunk of chunks) {
1124
- const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1125
- for (const t of toolNames) allTools.add(t);
1126
- const filePaths = extractFilePaths(chunk.toolCalls);
1127
- for (const f of filePaths) allFiles.add(f);
1128
-
1129
- // Look for decision indicators in assistant text
1130
- const assistantText = extractTextContent(chunk.assistantMessage);
1131
- if (assistantText) {
1132
- const lower = assistantText.toLowerCase();
1133
- if (lower.includes('decided') || lower.includes('choosing') || lower.includes('approach')
1134
- || lower.includes('instead of') || lower.includes('rather than')) {
1135
- const firstLine = assistantText.split('\n').find(l => l.trim()) || '';
1136
- if (firstLine.length > 10) decisions.push(firstLine.slice(0, 120));
1137
- }
1138
- }
1139
- }
1140
-
1141
- parts.push('PRESERVE in compaction summary:');
1142
-
1143
- if (allFiles.size > 0) {
1144
- const fileList = [...allFiles].slice(0, 15).map(f => {
1145
- const segs = f.split('/');
1146
- return segs.length > 3 ? '.../' + segs.slice(-3).join('/') : f;
1147
- });
1148
- parts.push(`- Files modified/read: ${fileList.join(', ')}`);
1149
- }
1150
-
1151
- if (allTools.size > 0) {
1152
- parts.push(`- Tools used: ${[...allTools].join(', ')}`);
1153
- }
1154
-
1155
- if (decisions.length > 0) {
1156
- parts.push('- Key decisions:');
1157
- for (const d of decisions.slice(0, 5)) {
1158
- parts.push(` * ${d}`);
1159
- }
1160
- }
1161
-
1162
- // Recent turns summary (most important context)
1163
- const recentChunks = chunks.slice(-5);
1164
- if (recentChunks.length > 0) {
1165
- parts.push('');
1166
- parts.push('MOST RECENT TURNS (prioritize preserving):');
1167
- for (const chunk of recentChunks) {
1168
- const userText = extractTextContent(chunk.userMessage);
1169
- const firstLine = userText.split('\n').find(l => l.trim()) || '';
1170
- const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1171
- parts.push(`- [Turn ${chunk.turnIndex}] ${firstLine.slice(0, 80)}${toolNames.length ? ` (${toolNames.join(', ')})` : ''}`);
1172
- }
1173
- }
1174
-
1175
- // Cap at budget
1176
- let result = parts.join('\n');
1177
- if (result.length > COMPACT_INSTRUCTION_BUDGET) {
1178
- result = result.slice(0, COMPACT_INSTRUCTION_BUDGET - 3) + '...';
1179
- }
1180
- return result;
1181
- }
1182
-
1183
- // ============================================================================
1184
- // Importance scoring for retrieval ranking
1185
- // ============================================================================
1186
-
1187
- function computeImportance(entry, now) {
1188
- const meta = entry.metadata || {};
1189
- const accessCount = entry.accessCount || 0;
1190
- const createdAt = entry.createdAt || now;
1191
- const ageMs = Math.max(1, now - createdAt);
1192
- const ageDays = ageMs / 86400000;
1193
-
1194
- // Recency: exponential decay, half-life of 7 days
1195
- const recency = Math.exp(-0.693 * ageDays / 7);
1196
-
1197
- // Frequency: log-scaled access count
1198
- const frequency = Math.log2(accessCount + 1) + 1;
1199
-
1200
- // Richness: tool calls and file paths indicate actionable context
1201
- const toolCount = meta.toolNames?.length || 0;
1202
- const fileCount = meta.filePaths?.length || 0;
1203
- const richness = 1.0 + (toolCount > 0 ? 0.5 : 0) + (fileCount > 0 ? 0.3 : 0);
1204
-
1205
- return recency * frequency * richness;
1206
- }
1207
-
1208
- // ============================================================================
1209
- // Smart retrieval: importance-ranked instead of just recency
1210
- // ============================================================================
1211
-
1212
- async function retrieveContextSmart(backend, sessionId, budget) {
1213
- let sessionEntries;
1214
-
1215
- // Use importance-ranked query if backend supports it
1216
- if (backend.queryByImportance) {
1217
- try {
1218
- sessionEntries = backend.queryByImportance(NAMESPACE, sessionId);
1219
- } catch {
1220
- // Fall back to standard query
1221
- sessionEntries = null;
1222
- }
1223
- }
1224
-
1225
- if (!sessionEntries) {
1226
- // Fall back: fetch all, compute importance in JS
1227
- const raw = backend.queryBySession
1228
- ? await backend.queryBySession(NAMESPACE, sessionId)
1229
- : (await backend.query({ namespace: NAMESPACE }))
1230
- .filter(e => e.metadata?.sessionId === sessionId);
1231
-
1232
- const now = Date.now();
1233
- sessionEntries = raw
1234
- .map(e => ({ ...e, importanceScore: computeImportance(e, now) }))
1235
- .sort((a, b) => b.importanceScore - a.importanceScore);
1236
- }
1237
-
1238
- if (sessionEntries.length === 0) return { text: '', accessedIds: [] };
1239
-
1240
- const lines = [];
1241
- const accessedIds = [];
1242
- let charCount = 0;
1243
- const header = `## Restored Context (importance-ranked from archive)\n\nPrevious conversation: ${sessionEntries.length} archived turns, ranked by importance:\n\n`;
1244
- charCount += header.length;
1245
-
1246
- for (const entry of sessionEntries) {
1247
- const meta = entry.metadata || {};
1248
- const score = entry.importanceScore?.toFixed(2) || '?';
1249
- const toolStr = meta.toolNames?.length ? ` Tools: ${meta.toolNames.join(', ')}.` : '';
1250
- const fileStr = meta.filePaths?.length ? ` Files: ${meta.filePaths.slice(0, 3).join(', ')}.` : '';
1251
- const line = `- [Turn ${meta.chunkIndex ?? '?'}, score:${score}] ${meta.summary || '(no summary)'}${toolStr}${fileStr}`;
1252
-
1253
- if (charCount + line.length + 1 > budget) break;
1254
- lines.push(line);
1255
- accessedIds.push(entry.id);
1256
- charCount += line.length + 1;
1257
- }
1258
-
1259
- if (lines.length === 0) return { text: '', accessedIds: [] };
1260
-
1261
- // Cross-session semantic search: find related context from previous sessions
1262
- let crossSessionText = '';
1263
- if (backend.semanticSearch && sessionEntries.length > 0) {
1264
- try {
1265
- // Use the most recent turn's summary as the search query
1266
- const recentSummary = sessionEntries[0]?.metadata?.summary || '';
1267
- if (recentSummary) {
1268
- const crossResults = await crossSessionSearch(backend, recentSummary, sessionId, 3);
1269
- if (crossResults.length > 0) {
1270
- const crossLines = crossResults.map(r =>
1271
- `- [Session ${r.sessionId?.slice(0, 8)}..., turn ${r.chunkIndex ?? '?'}, conf:${(r.confidence || 0).toFixed(2)}] ${r.summary || '(no summary)'}`
1272
- );
1273
- crossSessionText = `\n\nRelated context from previous sessions:\n${crossLines.join('\n')}`;
1274
- }
1275
- }
1276
- } catch { /* cross-session search is best-effort */ }
1277
- }
1278
-
1279
- const footer = `\n\nFull archive: ${NAMESPACE} namespace (session: ${sessionId}). ${sessionEntries.length - lines.length} additional turns available.`;
1280
- return { text: header + lines.join('\n') + crossSessionText + footer, accessedIds };
1281
- }
1282
-
1283
- // ============================================================================
1284
- // Auto-optimize: prune stale entries, run after archiving
1285
- // ============================================================================
1286
-
1287
- async function autoOptimize(backend, backendType) {
1288
- if (!AUTO_OPTIMIZE) return { pruned: 0, synced: 0, decayed: 0, embedded: 0 };
1289
-
1290
- let pruned = 0;
1291
- let decayed = 0;
1292
- let embedded = 0;
1293
-
1294
- // Step 1: Confidence decay — reduce confidence for unaccessed entries
1295
- if (backend.decayConfidence) {
1296
- try {
1297
- decayed = backend.decayConfidence(NAMESPACE, 1); // 1 hour worth of decay per optimize cycle
1298
- } catch { /* non-critical */ }
1299
- }
1300
-
1301
- // Step 2: Smart pruning — remove low-confidence entries first
1302
- if (backend.pruneByConfidence) {
1303
- try {
1304
- pruned += backend.pruneByConfidence(NAMESPACE, 0.15);
1305
- } catch { /* non-critical */ }
1306
- }
1307
-
1308
- // Step 3: Age-based pruning as fallback
1309
- if (backend.pruneStale) {
1310
- try {
1311
- pruned += backend.pruneStale(NAMESPACE, RETENTION_DAYS);
1312
- } catch { /* non-critical */ }
1313
- }
1314
-
1315
- // Step 4: Generate ONNX embeddings (384-dim) for entries missing them
1316
- if (backend.storeEmbedding) {
1317
- try {
1318
- const rows = backend.db?.prepare?.(
1319
- 'SELECT id, content FROM transcript_entries WHERE namespace = ? AND embedding IS NULL LIMIT 20'
1320
- )?.all(NAMESPACE);
1321
- if (rows) {
1322
- for (const row of rows) {
1323
- const { embedding } = await createEmbedding(row.content);
1324
- backend.storeEmbedding(row.id, embedding);
1325
- embedded++;
1326
- }
1327
- }
1328
- } catch { /* non-critical */ }
1329
- }
1330
-
1331
- // Step 5: Auto-sync to RuVector if available
1332
- let synced = 0;
1333
- if (backendType === 'sqlite' && backend.allForSync) {
1334
- try {
1335
- const rvConfig = getRuVectorConfig();
1336
- if (rvConfig) {
1337
- const rvBackend = new RuVectorBackend(rvConfig);
1338
- await rvBackend.initialize();
1339
-
1340
- const allEntries = backend.allForSync(NAMESPACE);
1341
- if (allEntries.length > 0) {
1342
- // Add hash embeddings for vector search in RuVector
1343
- const entriesToSync = allEntries.map(e => ({
1344
- ...e,
1345
- _embedding: createHashEmbedding(e.content),
1346
- }));
1347
- await rvBackend.bulkInsert(entriesToSync);
1348
- synced = entriesToSync.length;
1349
- }
1350
-
1351
- await rvBackend.shutdown();
1352
- }
1353
- } catch { /* RuVector sync is best-effort */ }
1354
- }
1355
-
1356
- return { pruned, synced, decayed, embedded };
1357
- }
1358
-
1359
- // ============================================================================
1360
- // Cross-session semantic retrieval
1361
- // ============================================================================
1362
-
1363
- /**
1364
- * Find relevant context from OTHER sessions using semantic similarity.
1365
- * This enables "What did we discuss about auth?" across sessions.
1366
- */
1367
- async function crossSessionSearch(backend, queryText, currentSessionId, k = 5) {
1368
- if (!backend.semanticSearch) return [];
1369
- try {
1370
- const { embedding: queryEmb } = await createEmbedding(queryText);
1371
- const results = backend.semanticSearch(queryEmb, k * 2, NAMESPACE);
1372
- // Filter out current session entries (we already have those)
1373
- return results
1374
- .filter(r => r.sessionId !== currentSessionId)
1375
- .slice(0, k);
1376
- } catch { return []; }
1377
- }
1378
-
1379
- // ============================================================================
1380
- // Context Autopilot Engine
1381
- // ============================================================================
1382
-
1383
- /**
1384
- * Estimate context token usage from transcript JSONL.
1385
- *
1386
- * Primary method: Read the most recent assistant message's `usage` field which
1387
- * contains `input_tokens` + `cache_read_input_tokens` — this is the ACTUAL
1388
- * context size as reported by the Claude API. This includes system prompt,
1389
- * CLAUDE.md, tool definitions, all messages, and everything Claude sees.
1390
- *
1391
- * Fallback: Sum character lengths and divide by CHARS_PER_TOKEN.
1392
- */
1393
- function estimateContextTokens(transcriptPath) {
1394
- if (!existsSync(transcriptPath)) return { tokens: 0, turns: 0, method: 'none' };
1395
-
1396
- const content = readFileSync(transcriptPath, 'utf-8');
1397
- const lines = content.split('\n').filter(Boolean);
1398
-
1399
- // Track the most recent usage data (from the last assistant message)
1400
- let lastInputTokens = 0;
1401
- let lastCacheRead = 0;
1402
- let lastCacheCreate = 0;
1403
- let turns = 0;
1404
- let lastPreTokens = 0;
1405
- let totalChars = 0;
1406
-
1407
- for (let i = 0; i < lines.length; i++) {
1408
- try {
1409
- const parsed = JSON.parse(lines[i]);
1410
-
1411
- // Check for compact_boundary
1412
- if (parsed.type === 'system' && parsed.subtype === 'compact_boundary') {
1413
- lastPreTokens = parsed.compactMetadata?.preTokens
1414
- || parsed.compact_metadata?.pre_tokens || 0;
1415
- // Reset after compaction — new context starts here
1416
- totalChars = 0;
1417
- turns = 0;
1418
- lastInputTokens = 0;
1419
- lastCacheRead = 0;
1420
- lastCacheCreate = 0;
1421
- continue;
1422
- }
1423
-
1424
- // Extract ACTUAL token usage from assistant messages
1425
- // The SDK transcript stores: { message: { role, content, usage: { input_tokens, cache_read_input_tokens, ... } } }
1426
- const msg = parsed.message || parsed;
1427
- const usage = msg.usage;
1428
- if (usage && (msg.role === 'assistant' || parsed.type === 'assistant')) {
1429
- const inputTokens = usage.input_tokens || 0;
1430
- const cacheRead = usage.cache_read_input_tokens || 0;
1431
- const cacheCreate = usage.cache_creation_input_tokens || 0;
1432
-
1433
- // The total context sent to Claude = input_tokens + cache_read + cache_create
1434
- // input_tokens: non-cached tokens actually processed
1435
- // cache_read: tokens served from cache (still in context)
1436
- // cache_create: tokens newly cached (still in context)
1437
- const totalContext = inputTokens + cacheRead + cacheCreate;
1438
-
1439
- if (totalContext > 0) {
1440
- lastInputTokens = inputTokens;
1441
- lastCacheRead = cacheRead;
1442
- lastCacheCreate = cacheCreate;
1443
- }
1444
- }
1445
-
1446
- // Count turns for display
1447
- const role = msg.role || parsed.type;
1448
- if (role === 'user') turns++;
1449
-
1450
- // Char fallback accumulation
1451
- if (role === 'user' || role === 'assistant') {
1452
- const c = msg.content;
1453
- if (typeof c === 'string') totalChars += c.length;
1454
- else if (Array.isArray(c)) {
1455
- for (const block of c) {
1456
- if (block.text) totalChars += block.text.length;
1457
- else if (block.input) totalChars += JSON.stringify(block.input).length;
1458
- }
1459
- }
1460
- }
1461
- } catch { /* skip */ }
1462
- }
1463
-
1464
- // Primary: use actual API usage data
1465
- const actualTotal = lastInputTokens + lastCacheRead + lastCacheCreate;
1466
- if (actualTotal > 0) {
1467
- return {
1468
- tokens: actualTotal,
1469
- turns,
1470
- method: 'api-usage',
1471
- lastPreTokens,
1472
- breakdown: {
1473
- input: lastInputTokens,
1474
- cacheRead: lastCacheRead,
1475
- cacheCreate: lastCacheCreate,
1476
- },
1477
- };
1478
- }
1479
-
1480
- // Fallback: char-based estimate
1481
- const estimatedTokens = Math.ceil(totalChars / CHARS_PER_TOKEN);
1482
- if (lastPreTokens > 0) {
1483
- const compactSummaryTokens = 3000;
1484
- return {
1485
- tokens: compactSummaryTokens + estimatedTokens,
1486
- turns,
1487
- method: 'post-compact-char-estimate',
1488
- lastPreTokens,
1489
- };
1490
- }
1491
-
1492
- return { tokens: estimatedTokens, turns, method: 'char-estimate' };
1493
- }
1494
-
1495
- /**
1496
- * Load autopilot state (persisted across hook invocations).
1497
- */
1498
- function loadAutopilotState() {
1499
- try {
1500
- if (existsSync(AUTOPILOT_STATE_PATH)) {
1501
- return JSON.parse(readFileSync(AUTOPILOT_STATE_PATH, 'utf-8'));
1502
- }
1503
- } catch { /* fresh state */ }
1504
- return {
1505
- sessionId: null,
1506
- lastTokenEstimate: 0,
1507
- lastPercentage: 0,
1508
- pruneCount: 0,
1509
- warningIssued: false,
1510
- lastCheck: 0,
1511
- history: [], // Track token growth over time
1512
- };
1513
- }
1514
-
1515
- /**
1516
- * Save autopilot state.
1517
- */
1518
- function saveAutopilotState(state) {
1519
- try {
1520
- writeFileSync(AUTOPILOT_STATE_PATH, JSON.stringify(state, null, 2), 'utf-8');
1521
- } catch { /* best effort */ }
1522
- }
1523
-
1524
- /**
1525
- * Build a context optimization report for additionalContext injection.
1526
- */
1527
- function buildAutopilotReport(percentage, tokens, windowSize, turns, state) {
1528
- const bar = buildProgressBar(percentage);
1529
- const status = percentage >= AUTOPILOT_PRUNE_PCT
1530
- ? 'OPTIMIZING'
1531
- : percentage >= AUTOPILOT_WARN_PCT
1532
- ? 'WARNING'
1533
- : 'OK';
1534
-
1535
- const parts = [
1536
- `[ContextAutopilot] ${bar} ${(percentage * 100).toFixed(1)}% context used`,
1537
- `(~${formatTokens(tokens)}/${formatTokens(windowSize)} tokens, ${turns} turns)`,
1538
- `Status: ${status}`,
1539
- ];
1540
-
1541
- if (state.pruneCount > 0) {
1542
- parts.push(`| Optimizations: ${state.pruneCount} prune cycles`);
1543
- }
1544
-
1545
- // Add trend if we have history
1546
- if (state.history.length >= 2) {
1547
- const recent = state.history.slice(-3);
1548
- const avgGrowth = recent.reduce((sum, h, i) => {
1549
- if (i === 0) return 0;
1550
- return sum + (h.pct - recent[i - 1].pct);
1551
- }, 0) / (recent.length - 1);
1552
-
1553
- if (avgGrowth > 0) {
1554
- const turnsUntilFull = Math.ceil((1.0 - percentage) / avgGrowth);
1555
- parts.push(`| ~${turnsUntilFull} turns until optimization needed`);
1556
- }
1557
- }
1558
-
1559
- return parts.join(' ');
1560
- }
1561
-
1562
- /**
1563
- * Visual progress bar for context usage.
1564
- */
1565
- function buildProgressBar(percentage) {
1566
- const width = 20;
1567
- const filled = Math.round(percentage * width);
1568
- const empty = width - filled;
1569
- const fillChar = percentage >= AUTOPILOT_PRUNE_PCT ? '!' : percentage >= AUTOPILOT_WARN_PCT ? '#' : '=';
1570
- return `[${fillChar.repeat(filled)}${'-'.repeat(empty)}]`;
1571
- }
1572
-
1573
- /**
1574
- * Format token count for display.
1575
- */
1576
- function formatTokens(n) {
1577
- if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M';
1578
- if (n >= 1000) return (n / 1000).toFixed(1) + 'K';
1579
- return String(n);
1580
- }
1581
-
1582
- /**
1583
- * Context Autopilot: run on every UserPromptSubmit.
1584
- * Returns { additionalContext, shouldBlock } for the hook output.
1585
- */
1586
- async function runAutopilot(transcriptPath, sessionId, backend, backendType) {
1587
- const state = loadAutopilotState();
1588
-
1589
- // Reset state if session changed
1590
- if (state.sessionId !== sessionId) {
1591
- state.sessionId = sessionId;
1592
- state.lastTokenEstimate = 0;
1593
- state.lastPercentage = 0;
1594
- state.pruneCount = 0;
1595
- state.warningIssued = false;
1596
- state.history = [];
1597
- }
1598
-
1599
- // Estimate current context usage
1600
- const { tokens, turns, method, lastPreTokens } = estimateContextTokens(transcriptPath);
1601
- const percentage = Math.min(tokens / CONTEXT_WINDOW_TOKENS, 1.0);
1602
-
1603
- // Track history (keep last 50 data points)
1604
- state.history.push({ ts: Date.now(), tokens, pct: percentage, turns });
1605
- if (state.history.length > 50) state.history.shift();
1606
-
1607
- state.lastTokenEstimate = tokens;
1608
- state.lastPercentage = percentage;
1609
- state.lastCheck = Date.now();
1610
-
1611
- let optimizationMessage = '';
1612
-
1613
- // Phase 1: Warning zone (70-85%) — advise concise responses
1614
- if (percentage >= AUTOPILOT_WARN_PCT && percentage < AUTOPILOT_PRUNE_PCT) {
1615
- if (!state.warningIssued) {
1616
- state.warningIssued = true;
1617
- optimizationMessage = ` | Context at ${(percentage * 100).toFixed(0)}%. Keep responses concise to extend session.`;
1618
- }
1619
- }
1620
-
1621
- // Phase 2: Critical zone (85%+) — session rotation needed
1622
- if (percentage >= AUTOPILOT_PRUNE_PCT) {
1623
- state.pruneCount++;
1624
-
1625
- // Prune stale entries from archive to free up storage
1626
- if (backend.pruneStale) {
1627
- try {
1628
- const pruned = backend.pruneStale(NAMESPACE, Math.min(RETENTION_DAYS, 7));
1629
- if (pruned > 0) {
1630
- optimizationMessage += ` | Pruned ${pruned} stale archive entries.`;
1631
- }
1632
- } catch { /* non-critical */ }
1633
- }
1634
-
1635
- const turnsLeft = Math.max(0, Math.ceil((1.0 - percentage) / 0.03));
1636
- optimizationMessage += ` | CRITICAL: ${(percentage * 100).toFixed(0)}% context used (~${turnsLeft} turns left). All ${turns} turns archived. Start a new session with /clear — context will be fully restored via SessionStart hook.`;
1637
- }
1638
-
1639
- const report = buildAutopilotReport(percentage, tokens, CONTEXT_WINDOW_TOKENS, turns, state);
1640
- saveAutopilotState(state);
1641
-
1642
- return {
1643
- additionalContext: report + optimizationMessage,
1644
- percentage,
1645
- tokens,
1646
- turns,
1647
- method,
1648
- state,
1649
- };
1650
- }
1651
-
1652
- // ============================================================================
1653
- // Commands
1654
- // ============================================================================
1655
-
1656
- async function doPreCompact() {
1657
- const input = await readStdin(200);
1658
- if (!input) return;
1659
-
1660
- const { session_id: sessionId, transcript_path: transcriptPath, trigger } = input;
1661
- if (!transcriptPath || !sessionId) return;
1662
-
1663
- const messages = parseTranscript(transcriptPath);
1664
- if (messages.length === 0) return;
1665
-
1666
- const chunks = chunkTranscript(messages);
1667
- if (chunks.length === 0) return;
1668
-
1669
- const { backend, type } = await resolveBackend();
1670
-
1671
- const archiveResult = await storeChunks(backend, chunks, sessionId, trigger || 'auto');
1672
-
1673
- // Auto-optimize: prune stale entries + sync to RuVector if available
1674
- const optimizeResult = await autoOptimize(backend, type);
1675
-
1676
- const total = await backend.count(NAMESPACE);
1677
- await backend.shutdown();
1678
-
1679
- const optParts = [];
1680
- if (optimizeResult.pruned > 0) optParts.push(`${optimizeResult.pruned} pruned`);
1681
- if (optimizeResult.decayed > 0) optParts.push(`${optimizeResult.decayed} decayed`);
1682
- if (optimizeResult.embedded > 0) optParts.push(`${optimizeResult.embedded} embedded`);
1683
- if (optimizeResult.synced > 0) optParts.push(`${optimizeResult.synced} synced`);
1684
- const optimizeMsg = optParts.length > 0 ? ` Optimized: ${optParts.join(', ')}.` : '';
1685
- process.stderr.write(
1686
- `[ContextPersistence] Archived ${archiveResult.stored} turns (${archiveResult.deduped} deduped) via ${type}. Total: ${total}.${optimizeMsg}\n`
1687
- );
1688
-
1689
- // Exit code 0: stdout is appended as custom compact instructions
1690
- // This guides Claude on what to preserve in the compaction summary
1691
- const instructions = buildCompactInstructions(chunks, sessionId, archiveResult);
1692
- process.stdout.write(instructions);
1693
-
1694
- // Context Autopilot: track state and log archival status
1695
- // NOTE: Claude Code 2.0.76 executePreCompactHooks uses executeHooksOutsideREPL
1696
- // which does NOT support exit code 2 blocking. Compaction always proceeds.
1697
- // Our "infinite context" comes from archive + restore, not blocking.
1698
- if (AUTOPILOT_ENABLED) {
1699
- const state = loadAutopilotState();
1700
- const pct = state.lastPercentage || 0;
1701
- const bar = buildProgressBar(pct);
1702
-
1703
- process.stderr.write(
1704
- `[ContextAutopilot] ${bar} ${(pct * 100).toFixed(1)}% | ${trigger} compact — ${chunks.length} turns archived. Context will be restored after compaction.\n`
1705
- );
1706
-
1707
- // Reset autopilot state for post-compaction fresh start
1708
- state.lastTokenEstimate = 0;
1709
- state.lastPercentage = 0;
1710
- state.warningIssued = false;
1711
- saveAutopilotState(state);
1712
- }
1713
- }
1714
-
1715
- async function doSessionStart() {
1716
- const input = await readStdin(200);
1717
-
1718
- // Restore context after compaction OR after /clear (session rotation)
1719
- // With DISABLE_COMPACT, /clear is the primary way to free context
1720
- if (!input || (input.source !== 'compact' && input.source !== 'clear')) return;
1721
-
1722
- const sessionId = input.session_id;
1723
- if (!sessionId) return;
1724
-
1725
- const { backend, type } = await resolveBackend();
1726
-
1727
- // Use smart retrieval (importance-ranked) when auto-optimize is on
1728
- let additionalContext;
1729
- if (AUTO_OPTIMIZE) {
1730
- const { text, accessedIds } = await retrieveContextSmart(backend, sessionId, RESTORE_BUDGET);
1731
- additionalContext = text;
1732
-
1733
- // Track which entries were actually restored (access pattern learning)
1734
- if (accessedIds.length > 0 && backend.markAccessed) {
1735
- try { backend.markAccessed(accessedIds); } catch { /* non-critical */ }
1736
- }
1737
-
1738
- if (accessedIds.length > 0) {
1739
- process.stderr.write(
1740
- `[ContextPersistence] Smart restore: ${accessedIds.length} turns (importance-ranked) via ${type}\n`
1741
- );
1742
- }
1743
- } else {
1744
- additionalContext = await retrieveContext(backend, sessionId, RESTORE_BUDGET);
1745
- }
1746
-
1747
- await backend.shutdown();
1748
-
1749
- if (!additionalContext) return;
1750
-
1751
- const output = {
1752
- hookSpecificOutput: {
1753
- hookEventName: 'SessionStart',
1754
- additionalContext,
1755
- },
1756
- };
1757
- process.stdout.write(JSON.stringify(output));
1758
- }
1759
-
1760
- // ============================================================================
1761
- // Proactive archiving on every user prompt (prevents context cliff)
1762
- // ============================================================================
1763
-
1764
- async function doUserPromptSubmit() {
1765
- const input = await readStdin(200);
1766
- if (!input) return;
1767
-
1768
- const { session_id: sessionId, transcript_path: transcriptPath } = input;
1769
- if (!transcriptPath || !sessionId) return;
1770
-
1771
- const messages = parseTranscript(transcriptPath);
1772
- if (messages.length === 0) return;
1773
-
1774
- const chunks = chunkTranscript(messages);
1775
- if (chunks.length === 0) return;
1776
-
1777
- const { backend, type } = await resolveBackend();
1778
-
1779
- // Only archive new turns (dedup handles the rest, but we can skip early
1780
- // by only processing the last N chunks since the previous archive)
1781
- const existingCount = backend.queryBySession
1782
- ? (await backend.queryBySession(NAMESPACE, sessionId)).length
1783
- : 0;
1784
-
1785
- // Skip if we've already archived most turns (within 2 turns tolerance)
1786
- const skipArchive = existingCount > 0 && chunks.length - existingCount <= 2;
1787
-
1788
- let archiveMsg = '';
1789
- if (!skipArchive) {
1790
- const result = await storeChunks(backend, chunks, sessionId, 'proactive');
1791
- if (result.stored > 0) {
1792
- const total = await backend.count(NAMESPACE);
1793
- archiveMsg = `[ContextPersistence] Proactively archived ${result.stored} turns (total: ${total}).`;
1794
- process.stderr.write(
1795
- `[ContextPersistence] Proactive archive: ${result.stored} new, ${result.deduped} deduped via ${type}. Total: ${total}\n`
1796
- );
1797
- }
1798
- }
1799
-
1800
- // Context Autopilot: estimate usage and report percentage
1801
- let autopilotMsg = '';
1802
- if (AUTOPILOT_ENABLED && transcriptPath) {
1803
- try {
1804
- const autopilot = await runAutopilot(transcriptPath, sessionId, backend, type);
1805
- autopilotMsg = autopilot.additionalContext;
1806
-
1807
- process.stderr.write(
1808
- `[ContextAutopilot] ${(autopilot.percentage * 100).toFixed(1)}% context used (~${formatTokens(autopilot.tokens)} tokens, ${autopilot.turns} turns, ${autopilot.method})\n`
1809
- );
1810
- } catch (err) {
1811
- process.stderr.write(`[ContextAutopilot] Error: ${err.message}\n`);
1812
- }
1813
- }
1814
-
1815
- await backend.shutdown();
1816
-
1817
- // Combine archive message and autopilot report
1818
- const additionalContext = [archiveMsg, autopilotMsg].filter(Boolean).join(' ');
1819
-
1820
- if (additionalContext) {
1821
- const output = {
1822
- hookSpecificOutput: {
1823
- hookEventName: 'UserPromptSubmit',
1824
- additionalContext,
1825
- },
1826
- };
1827
- process.stdout.write(JSON.stringify(output));
1828
- }
1829
- }
1830
-
1831
- async function doStatus() {
1832
- const { backend, type } = await resolveBackend();
1833
-
1834
- const total = await backend.count();
1835
- const archiveCount = await backend.count(NAMESPACE);
1836
- const namespaces = await backend.listNamespaces();
1837
- const sessions = await backend.listSessions(NAMESPACE);
1838
-
1839
- console.log('\n=== Context Persistence Archive Status ===\n');
1840
- const backendLabel = {
1841
- sqlite: ARCHIVE_DB_PATH,
1842
- ruvector: `${process.env.RUVECTOR_HOST || 'N/A'}:${process.env.RUVECTOR_PORT || '5432'}`,
1843
- agentdb: 'in-memory HNSW',
1844
- json: ARCHIVE_JSON_PATH,
1845
- };
1846
- console.log(` Backend: ${type} (${backendLabel[type] || type})`);
1847
- console.log(` Total: ${total} entries`);
1848
- console.log(` Transcripts: ${archiveCount} entries`);
1849
- console.log(` Namespaces: ${namespaces.join(', ') || 'none'}`);
1850
- console.log(` Budget: ${RESTORE_BUDGET} chars`);
1851
- console.log(` Sessions: ${sessions.length}`);
1852
- console.log(` Proactive: enabled (UserPromptSubmit hook)`);
1853
- console.log(` Auto-opt: ${AUTO_OPTIMIZE ? 'enabled' : 'disabled'} (importance ranking, pruning, sync)`);
1854
- console.log(` Retention: ${RETENTION_DAYS} days (prune never-accessed entries)`);
1855
- const rvConfig = getRuVectorConfig();
1856
- console.log(` RuVector: ${rvConfig ? `${rvConfig.host}:${rvConfig.port}/${rvConfig.database} (auto-sync enabled)` : 'not configured'}`);
1857
-
1858
- // Self-learning stats
1859
- if (type === 'sqlite' && backend.db) {
1860
- try {
1861
- const embCount = backend.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries WHERE embedding IS NOT NULL').get().cnt;
1862
- const avgConf = backend.db.prepare('SELECT AVG(confidence) as avg FROM transcript_entries WHERE namespace = ?').get(NAMESPACE)?.avg || 0;
1863
- const lowConf = backend.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = ? AND confidence < 0.3').get(NAMESPACE).cnt;
1864
- console.log('');
1865
- console.log(' --- Self-Learning ---');
1866
- console.log(` Embeddings: ${embCount}/${archiveCount} entries have vector embeddings`);
1867
- console.log(` Avg conf: ${(avgConf * 100).toFixed(1)}% (decay: -0.5%/hr, boost: +3%/access)`);
1868
- console.log(` Low conf: ${lowConf} entries below 30% (pruned at 15%)`);
1869
- console.log(` Semantic: ${embCount > 0 ? 'enabled (cross-session search)' : 'pending (embeddings generating)'}`);
1870
- } catch { /* stats are non-critical */ }
1871
- }
1872
-
1873
- // Autopilot status
1874
- console.log('');
1875
- console.log(' --- Context Autopilot ---');
1876
- console.log(` Enabled: ${AUTOPILOT_ENABLED}`);
1877
- console.log(` Window: ${formatTokens(CONTEXT_WINDOW_TOKENS)} tokens`);
1878
- console.log(` Warn at: ${(AUTOPILOT_WARN_PCT * 100).toFixed(0)}%`);
1879
- console.log(` Prune at: ${(AUTOPILOT_PRUNE_PCT * 100).toFixed(0)}%`);
1880
- console.log(` Compaction: LOSSLESS (archive before, restore after)`);
1881
-
1882
- const apState = loadAutopilotState();
1883
- if (apState.sessionId) {
1884
- const pct = apState.lastPercentage || 0;
1885
- const bar = buildProgressBar(pct);
1886
- console.log(` Current: ${bar} ${(pct * 100).toFixed(1)}% (~${formatTokens(apState.lastTokenEstimate)} tokens)`);
1887
- console.log(` Prune cycles: ${apState.pruneCount}`);
1888
- if (apState.history.length >= 2) {
1889
- const first = apState.history[0];
1890
- const last = apState.history[apState.history.length - 1];
1891
- const growthRate = (last.pct - first.pct) / apState.history.length;
1892
- if (growthRate > 0) {
1893
- const turnsLeft = Math.ceil((1.0 - pct) / growthRate);
1894
- console.log(` Est. runway: ~${turnsLeft} turns until prune threshold`);
1895
- }
1896
- }
1897
- }
1898
-
1899
- if (sessions.length > 0) {
1900
- console.log('\n Recent sessions:');
1901
- for (const s of sessions.slice(0, 10)) {
1902
- console.log(` - ${s.session_id}: ${s.cnt} turns`);
1903
- }
1904
- }
1905
-
1906
- console.log('');
1907
- await backend.shutdown();
1908
- }
1909
-
1910
- // ============================================================================
1911
- // Exports for testing
1912
- // ============================================================================
1913
-
1914
- export {
1915
- SQLiteBackend,
1916
- RuVectorBackend,
1917
- JsonFileBackend,
1918
- resolveBackend,
1919
- getRuVectorConfig,
1920
- createEmbedding,
1921
- createHashEmbedding,
1922
- getOnnxPipeline,
1923
- EMBEDDING_DIM,
1924
- hashContent,
1925
- parseTranscript,
1926
- extractTextContent,
1927
- extractToolCalls,
1928
- extractFilePaths,
1929
- chunkTranscript,
1930
- extractSummary,
1931
- buildEntry,
1932
- buildCompactInstructions,
1933
- computeImportance,
1934
- retrieveContextSmart,
1935
- autoOptimize,
1936
- crossSessionSearch,
1937
- storeChunks,
1938
- retrieveContext,
1939
- readStdin,
1940
- // Autopilot
1941
- estimateContextTokens,
1942
- loadAutopilotState,
1943
- saveAutopilotState,
1944
- runAutopilot,
1945
- buildProgressBar,
1946
- formatTokens,
1947
- buildAutopilotReport,
1948
- NAMESPACE,
1949
- ARCHIVE_DB_PATH,
1950
- ARCHIVE_JSON_PATH,
1951
- COMPACT_INSTRUCTION_BUDGET,
1952
- RETENTION_DAYS,
1953
- AUTO_OPTIMIZE,
1954
- AUTOPILOT_ENABLED,
1955
- CONTEXT_WINDOW_TOKENS,
1956
- AUTOPILOT_WARN_PCT,
1957
- AUTOPILOT_PRUNE_PCT,
1958
- };
1959
-
1960
- // ============================================================================
1961
- // Main
1962
- // ============================================================================
1963
-
1964
- const command = process.argv[2] || 'status';
1965
-
1966
- try {
1967
- switch (command) {
1968
- case 'pre-compact': await doPreCompact(); break;
1969
- case 'session-start': await doSessionStart(); break;
1970
- case 'user-prompt-submit': await doUserPromptSubmit(); break;
1971
- case 'status': await doStatus(); break;
1972
- default:
1973
- console.log('Usage: context-persistence-hook.mjs <pre-compact|session-start|user-prompt-submit|status>');
1974
- process.exit(1);
1975
- }
1976
- } catch (err) {
1977
- // Hooks must never crash Claude Code - fail silently
1978
- process.stderr.write(`[ContextPersistence] Error (non-critical): ${err.message}\n`);
1979
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Context Persistence Hook (ADR-051)
4
+ *
5
+ * Intercepts Claude Code's PreCompact, SessionStart, and UserPromptSubmit
6
+ * lifecycle events to persist conversation history in SQLite (primary),
7
+ * RuVector PostgreSQL (optional), or JSON (fallback), enabling "infinite
8
+ * context" across compaction boundaries.
9
+ *
10
+ * Backend priority:
11
+ * 1. better-sqlite3 (native, WAL mode, indexed queries, ACID transactions)
12
+ * 2. RuVector PostgreSQL (if RUVECTOR_* env vars set - TB-scale, GNN search)
13
+ * 3. AgentDB from @claude-flow/memory (HNSW vector search)
14
+ * 4. JsonFileBackend (zero dependencies, always works)
15
+ *
16
+ * Proactive archiving:
17
+ * - UserPromptSubmit hook archives on every prompt, BEFORE context fills up
18
+ * - PreCompact hook is a safety net that catches any remaining unarchived turns
19
+ * - SessionStart hook restores context after compaction
20
+ * - Together, compaction becomes invisible — no information is ever lost
21
+ *
22
+ * Usage:
23
+ * node context-persistence-hook.mjs pre-compact # PreCompact: archive transcript
24
+ * node context-persistence-hook.mjs session-start # SessionStart: restore context
25
+ * node context-persistence-hook.mjs user-prompt-submit # UserPromptSubmit: proactive archive
26
+ * node context-persistence-hook.mjs status # Show archive stats
27
+ */
28
+
29
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
30
+ import { createHash } from 'crypto';
31
+ import { join, dirname } from 'path';
32
+ import { fileURLToPath } from 'url';
33
+ import { createRequire } from 'module';
34
+
35
+ const __filename = fileURLToPath(import.meta.url);
36
+ const __dirname = dirname(__filename);
37
+ const PROJECT_ROOT = join(__dirname, '../..');
38
+ const DATA_DIR = join(PROJECT_ROOT, '.claude-flow', 'data');
39
+ const ARCHIVE_JSON_PATH = join(DATA_DIR, 'transcript-archive.json');
40
+ const ARCHIVE_DB_PATH = join(DATA_DIR, 'transcript-archive.db');
41
+
42
+ const NAMESPACE = 'transcript-archive';
43
+ const RESTORE_BUDGET = parseInt(process.env.CLAUDE_FLOW_COMPACT_RESTORE_BUDGET || '4000', 10);
44
+ const MAX_MESSAGES = 500;
45
+ const BLOCK_COMPACTION = process.env.CLAUDE_FLOW_BLOCK_COMPACTION === 'true';
46
+ const COMPACT_INSTRUCTION_BUDGET = parseInt(process.env.CLAUDE_FLOW_COMPACT_INSTRUCTION_BUDGET || '2000', 10);
47
+ const RETENTION_DAYS = parseInt(process.env.CLAUDE_FLOW_RETENTION_DAYS || '30', 10);
48
+ const AUTO_OPTIMIZE = process.env.CLAUDE_FLOW_AUTO_OPTIMIZE !== 'false'; // on by default
49
+
50
+ // ============================================================================
51
+ // Context Autopilot — prevent compaction by managing context size in real-time
52
+ // ============================================================================
53
+ const AUTOPILOT_ENABLED = process.env.CLAUDE_FLOW_CONTEXT_AUTOPILOT !== 'false'; // on by default
54
+ const CONTEXT_WINDOW_TOKENS = parseInt(process.env.CLAUDE_FLOW_CONTEXT_WINDOW || '200000', 10);
55
+ const AUTOPILOT_WARN_PCT = parseFloat(process.env.CLAUDE_FLOW_AUTOPILOT_WARN || '0.70');
56
+ const AUTOPILOT_PRUNE_PCT = parseFloat(process.env.CLAUDE_FLOW_AUTOPILOT_PRUNE || '0.85');
57
+ const AUTOPILOT_STATE_PATH = join(DATA_DIR, 'autopilot-state.json');
58
+
59
+ // Approximate tokens per character (Claude averages ~3.5 chars per token)
60
+ const CHARS_PER_TOKEN = 3.5;
61
+
62
+ // Ensure data dir
63
+ if (!existsSync(DATA_DIR)) mkdirSync(DATA_DIR, { recursive: true });
64
+
65
+ // ============================================================================
66
+ // SQLite Backend (better-sqlite3 — synchronous, fast, WAL mode)
67
+ // ============================================================================
68
+
69
+ class SQLiteBackend {
70
+ constructor(dbPath) {
71
+ this.dbPath = dbPath;
72
+ this.db = null;
73
+ }
74
+
75
+ async initialize() {
76
+ const require = createRequire(import.meta.url);
77
+ const Database = require('better-sqlite3');
78
+ this.db = new Database(this.dbPath);
79
+
80
+ // Performance optimizations
81
+ this.db.pragma('journal_mode = WAL');
82
+ this.db.pragma('synchronous = NORMAL');
83
+ this.db.pragma('cache_size = 5000');
84
+ this.db.pragma('temp_store = MEMORY');
85
+
86
+ // Create schema
87
+ this.db.exec(`
88
+ CREATE TABLE IF NOT EXISTS transcript_entries (
89
+ id TEXT PRIMARY KEY,
90
+ key TEXT NOT NULL,
91
+ content TEXT NOT NULL,
92
+ type TEXT NOT NULL DEFAULT 'episodic',
93
+ namespace TEXT NOT NULL DEFAULT 'transcript-archive',
94
+ tags TEXT NOT NULL DEFAULT '[]',
95
+ metadata TEXT NOT NULL DEFAULT '{}',
96
+ access_level TEXT NOT NULL DEFAULT 'private',
97
+ created_at INTEGER NOT NULL,
98
+ updated_at INTEGER NOT NULL,
99
+ version INTEGER NOT NULL DEFAULT 1,
100
+ access_count INTEGER NOT NULL DEFAULT 0,
101
+ last_accessed_at INTEGER NOT NULL,
102
+ content_hash TEXT,
103
+ session_id TEXT,
104
+ chunk_index INTEGER,
105
+ summary TEXT
106
+ );
107
+
108
+ CREATE INDEX IF NOT EXISTS idx_te_namespace ON transcript_entries(namespace);
109
+ CREATE INDEX IF NOT EXISTS idx_te_session ON transcript_entries(session_id);
110
+ CREATE INDEX IF NOT EXISTS idx_te_hash ON transcript_entries(content_hash);
111
+ CREATE INDEX IF NOT EXISTS idx_te_chunk ON transcript_entries(session_id, chunk_index);
112
+ CREATE INDEX IF NOT EXISTS idx_te_created ON transcript_entries(created_at);
113
+ `);
114
+
115
+ // Schema migration: add confidence + embedding columns (self-learning support)
116
+ try {
117
+ this.db.exec(`ALTER TABLE transcript_entries ADD COLUMN confidence REAL NOT NULL DEFAULT 0.8`);
118
+ } catch { /* column already exists */ }
119
+ try {
120
+ this.db.exec(`ALTER TABLE transcript_entries ADD COLUMN embedding BLOB`);
121
+ } catch { /* column already exists */ }
122
+ try {
123
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_te_confidence ON transcript_entries(confidence)`);
124
+ } catch { /* index already exists */ }
125
+
126
+ // Prepare statements for reuse
127
+ this._stmts = {
128
+ insert: this.db.prepare(`
129
+ INSERT OR IGNORE INTO transcript_entries
130
+ (id, key, content, type, namespace, tags, metadata, access_level,
131
+ created_at, updated_at, version, access_count, last_accessed_at,
132
+ content_hash, session_id, chunk_index, summary)
133
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
134
+ `),
135
+ queryByNamespace: this.db.prepare(
136
+ 'SELECT * FROM transcript_entries WHERE namespace = ? ORDER BY created_at DESC'
137
+ ),
138
+ queryBySession: this.db.prepare(
139
+ 'SELECT * FROM transcript_entries WHERE namespace = ? AND session_id = ? ORDER BY chunk_index DESC'
140
+ ),
141
+ countAll: this.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries'),
142
+ countByNamespace: this.db.prepare(
143
+ 'SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = ?'
144
+ ),
145
+ hashExists: this.db.prepare(
146
+ 'SELECT 1 FROM transcript_entries WHERE content_hash = ? LIMIT 1'
147
+ ),
148
+ listNamespaces: this.db.prepare(
149
+ 'SELECT DISTINCT namespace FROM transcript_entries'
150
+ ),
151
+ listSessions: this.db.prepare(
152
+ 'SELECT session_id, COUNT(*) as cnt FROM transcript_entries WHERE namespace = ? GROUP BY session_id ORDER BY MAX(created_at) DESC'
153
+ ),
154
+ };
155
+
156
+ this._bulkInsert = this.db.transaction((entries) => {
157
+ for (const e of entries) {
158
+ this._stmts.insert.run(
159
+ e.id, e.key, e.content, e.type, e.namespace,
160
+ JSON.stringify(e.tags), JSON.stringify(e.metadata), e.accessLevel,
161
+ e.createdAt, e.updatedAt, e.version, e.accessCount, e.lastAccessedAt,
162
+ e.metadata?.contentHash || null,
163
+ e.metadata?.sessionId || null,
164
+ e.metadata?.chunkIndex ?? null,
165
+ e.metadata?.summary || null
166
+ );
167
+ }
168
+ });
169
+
170
+ // Optimization statements
171
+ this._stmts.markAccessed = this.db.prepare(
172
+ 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = ? WHERE id = ?'
173
+ );
174
+ this._stmts.pruneStale = this.db.prepare(
175
+ 'DELETE FROM transcript_entries WHERE namespace = ? AND access_count = 0 AND created_at < ?'
176
+ );
177
+ this._stmts.queryByImportance = this.db.prepare(`
178
+ SELECT *, (
179
+ (CAST(access_count AS REAL) + 1) *
180
+ (1.0 / (1.0 + (? - created_at) / 86400000.0)) *
181
+ (CASE WHEN json_array_length(json_extract(metadata, '$.toolNames')) > 0 THEN 1.5 ELSE 1.0 END) *
182
+ (CASE WHEN json_array_length(json_extract(metadata, '$.filePaths')) > 0 THEN 1.3 ELSE 1.0 END)
183
+ ) AS importance_score
184
+ FROM transcript_entries
185
+ WHERE namespace = ? AND session_id = ?
186
+ ORDER BY importance_score DESC
187
+ `);
188
+ this._stmts.allForSync = this.db.prepare(
189
+ 'SELECT * FROM transcript_entries WHERE namespace = ? ORDER BY created_at ASC'
190
+ );
191
+ }
192
+
193
+ async store(entry) {
194
+ this._stmts.insert.run(
195
+ entry.id, entry.key, entry.content, entry.type, entry.namespace,
196
+ JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
197
+ entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
198
+ entry.metadata?.contentHash || null,
199
+ entry.metadata?.sessionId || null,
200
+ entry.metadata?.chunkIndex ?? null,
201
+ entry.metadata?.summary || null
202
+ );
203
+ }
204
+
205
+ async bulkInsert(entries) {
206
+ this._bulkInsert(entries);
207
+ }
208
+
209
+ async query(opts) {
210
+ let rows;
211
+ if (opts?.namespace && opts?.sessionId) {
212
+ rows = this._stmts.queryBySession.all(opts.namespace, opts.sessionId);
213
+ } else if (opts?.namespace) {
214
+ rows = this._stmts.queryByNamespace.all(opts.namespace);
215
+ } else {
216
+ rows = this.db.prepare('SELECT * FROM transcript_entries ORDER BY created_at DESC').all();
217
+ }
218
+ return rows.map(r => this._rowToEntry(r));
219
+ }
220
+
221
+ async queryBySession(namespace, sessionId) {
222
+ const rows = this._stmts.queryBySession.all(namespace, sessionId);
223
+ return rows.map(r => this._rowToEntry(r));
224
+ }
225
+
226
+ hashExists(hash) {
227
+ return !!this._stmts.hashExists.get(hash);
228
+ }
229
+
230
+ async count(namespace) {
231
+ if (namespace) {
232
+ return this._stmts.countByNamespace.get(namespace).cnt;
233
+ }
234
+ return this._stmts.countAll.get().cnt;
235
+ }
236
+
237
+ async listNamespaces() {
238
+ return this._stmts.listNamespaces.all().map(r => r.namespace);
239
+ }
240
+
241
+ async listSessions(namespace) {
242
+ return this._stmts.listSessions.all(namespace || NAMESPACE);
243
+ }
244
+
245
+ markAccessed(ids) {
246
+ const now = Date.now();
247
+ const boostStmt = this.db.prepare(
248
+ 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = ?, confidence = MIN(1.0, confidence + 0.03) WHERE id = ?'
249
+ );
250
+ for (const id of ids) {
251
+ boostStmt.run(now, id);
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Confidence decay: reduce confidence for entries not accessed recently.
257
+ * Decay rate: 0.5% per hour (matches LearningBridge default).
258
+ * Entries with confidence below 0.1 are floor-clamped.
259
+ */
260
+ decayConfidence(namespace, hoursElapsed = 1) {
261
+ const decayRate = 0.005 * hoursElapsed;
262
+ const result = this.db.prepare(
263
+ 'UPDATE transcript_entries SET confidence = MAX(0.1, confidence - ?) WHERE namespace = ? AND confidence > 0.1'
264
+ ).run(decayRate, namespace || NAMESPACE);
265
+ return result.changes;
266
+ }
267
+
268
+ /**
269
+ * Store embedding blob for an entry (768-dim Float32Array → Buffer).
270
+ */
271
+ storeEmbedding(id, embedding) {
272
+ const buf = Buffer.from(embedding.buffer, embedding.byteOffset, embedding.byteLength);
273
+ this.db.prepare('UPDATE transcript_entries SET embedding = ? WHERE id = ?').run(buf, id);
274
+ }
275
+
276
+ /**
277
+ * Cosine similarity search across all entries with embeddings.
278
+ * Handles both 384-dim (ONNX) and 768-dim (legacy hash) embeddings.
279
+ * Returns top-k entries ranked by similarity to the query embedding.
280
+ */
281
+ semanticSearch(queryEmbedding, k = 10, namespace) {
282
+ const rows = this.db.prepare(
283
+ 'SELECT id, embedding, summary, session_id, chunk_index, confidence, access_count FROM transcript_entries WHERE namespace = ? AND embedding IS NOT NULL'
284
+ ).all(namespace || NAMESPACE);
285
+
286
+ const queryDim = queryEmbedding.length;
287
+ const scored = [];
288
+ for (const row of rows) {
289
+ if (!row.embedding) continue;
290
+ const stored = new Float32Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 4);
291
+ // Only compare if dimensions match
292
+ if (stored.length !== queryDim) continue;
293
+ let dot = 0;
294
+ for (let i = 0; i < queryDim; i++) {
295
+ dot += queryEmbedding[i] * stored[i];
296
+ }
297
+ // Boost by confidence (self-learning signal)
298
+ const score = dot * (row.confidence || 0.8);
299
+ scored.push({ id: row.id, score, summary: row.summary, sessionId: row.session_id, chunkIndex: row.chunk_index, confidence: row.confidence, accessCount: row.access_count });
300
+ }
301
+
302
+ scored.sort((a, b) => b.score - a.score);
303
+ return scored.slice(0, k);
304
+ }
305
+
306
+ /**
307
+ * Smart pruning: prune by confidence instead of just age.
308
+ * Removes entries with confidence <= threshold AND access_count = 0.
309
+ */
310
+ pruneByConfidence(namespace, threshold = 0.2) {
311
+ const result = this.db.prepare(
312
+ 'DELETE FROM transcript_entries WHERE namespace = ? AND confidence <= ? AND access_count = 0'
313
+ ).run(namespace || NAMESPACE, threshold);
314
+ return result.changes;
315
+ }
316
+
317
+ pruneStale(namespace, maxAgeDays) {
318
+ const cutoff = Date.now() - (maxAgeDays * 24 * 60 * 60 * 1000);
319
+ const result = this._stmts.pruneStale.run(namespace || NAMESPACE, cutoff);
320
+ return result.changes;
321
+ }
322
+
323
+ queryByImportance(namespace, sessionId) {
324
+ const now = Date.now();
325
+ const rows = this._stmts.queryByImportance.all(now, namespace, sessionId);
326
+ return rows.map(r => ({ ...this._rowToEntry(r), importanceScore: r.importance_score }));
327
+ }
328
+
329
+ allForSync(namespace) {
330
+ const rows = this._stmts.allForSync.all(namespace || NAMESPACE);
331
+ return rows.map(r => this._rowToEntry(r));
332
+ }
333
+
334
+ async shutdown() {
335
+ if (this.db) {
336
+ this.db.pragma('optimize');
337
+ this.db.close();
338
+ this.db = null;
339
+ }
340
+ }
341
+
342
+ _rowToEntry(row) {
343
+ return {
344
+ id: row.id,
345
+ key: row.key,
346
+ content: row.content,
347
+ type: row.type,
348
+ namespace: row.namespace,
349
+ tags: JSON.parse(row.tags),
350
+ metadata: JSON.parse(row.metadata),
351
+ accessLevel: row.access_level,
352
+ createdAt: row.created_at,
353
+ updatedAt: row.updated_at,
354
+ version: row.version,
355
+ accessCount: row.access_count,
356
+ lastAccessedAt: row.last_accessed_at,
357
+ references: [],
358
+ };
359
+ }
360
+ }
361
+
362
+ // ============================================================================
363
+ // JSON File Backend (fallback when better-sqlite3 unavailable)
364
+ // ============================================================================
365
+
366
+ class JsonFileBackend {
367
+ constructor(filePath) {
368
+ this.filePath = filePath;
369
+ this.entries = new Map();
370
+ }
371
+
372
+ async initialize() {
373
+ if (existsSync(this.filePath)) {
374
+ try {
375
+ const data = JSON.parse(readFileSync(this.filePath, 'utf-8'));
376
+ if (Array.isArray(data)) {
377
+ for (const entry of data) this.entries.set(entry.id, entry);
378
+ }
379
+ } catch { /* start fresh */ }
380
+ }
381
+ }
382
+
383
+ async store(entry) { this.entries.set(entry.id, entry); this._persist(); }
384
+
385
+ async bulkInsert(entries) {
386
+ for (const e of entries) this.entries.set(e.id, e);
387
+ this._persist();
388
+ }
389
+
390
+ async query(opts) {
391
+ let results = [...this.entries.values()];
392
+ if (opts?.namespace) results = results.filter(e => e.namespace === opts.namespace);
393
+ if (opts?.type) results = results.filter(e => e.type === opts.type);
394
+ if (opts?.limit) results = results.slice(0, opts.limit);
395
+ return results;
396
+ }
397
+
398
+ async queryBySession(namespace, sessionId) {
399
+ return [...this.entries.values()]
400
+ .filter(e => e.namespace === namespace && e.metadata?.sessionId === sessionId)
401
+ .sort((a, b) => (b.metadata?.chunkIndex ?? 0) - (a.metadata?.chunkIndex ?? 0));
402
+ }
403
+
404
+ hashExists(hash) {
405
+ for (const e of this.entries.values()) {
406
+ if (e.metadata?.contentHash === hash) return true;
407
+ }
408
+ return false;
409
+ }
410
+
411
+ async count(namespace) {
412
+ if (!namespace) return this.entries.size;
413
+ let n = 0;
414
+ for (const e of this.entries.values()) {
415
+ if (e.namespace === namespace) n++;
416
+ }
417
+ return n;
418
+ }
419
+
420
+ async listNamespaces() {
421
+ const ns = new Set();
422
+ for (const e of this.entries.values()) ns.add(e.namespace || 'default');
423
+ return [...ns];
424
+ }
425
+
426
+ async listSessions(namespace) {
427
+ const sessions = new Map();
428
+ for (const e of this.entries.values()) {
429
+ if (e.namespace === (namespace || NAMESPACE) && e.metadata?.sessionId) {
430
+ sessions.set(e.metadata.sessionId, (sessions.get(e.metadata.sessionId) || 0) + 1);
431
+ }
432
+ }
433
+ return [...sessions.entries()].map(([session_id, cnt]) => ({ session_id, cnt }));
434
+ }
435
+
436
+ async shutdown() { this._persist(); }
437
+
438
+ _persist() {
439
+ try {
440
+ writeFileSync(this.filePath, JSON.stringify([...this.entries.values()], null, 2), 'utf-8');
441
+ } catch { /* best effort */ }
442
+ }
443
+ }
444
+
445
+ // ============================================================================
446
+ // RuVector PostgreSQL Backend (optional, TB-scale, GNN-enhanced)
447
+ // ============================================================================
448
+
449
+ class RuVectorBackend {
450
+ constructor(config) {
451
+ this.config = config;
452
+ this.pool = null;
453
+ }
454
+
455
+ async initialize() {
456
+ const pg = await import('pg');
457
+ const Pool = pg.default?.Pool || pg.Pool;
458
+ this.pool = new Pool({
459
+ host: this.config.host,
460
+ port: this.config.port || 5432,
461
+ database: this.config.database,
462
+ user: this.config.user,
463
+ password: this.config.password,
464
+ ssl: this.config.ssl || false,
465
+ max: 3,
466
+ idleTimeoutMillis: 10000,
467
+ connectionTimeoutMillis: 3000,
468
+ application_name: 'claude-flow-context-persistence',
469
+ });
470
+
471
+ // Test connection and create schema
472
+ const client = await this.pool.connect();
473
+ try {
474
+ await client.query(`
475
+ CREATE TABLE IF NOT EXISTS transcript_entries (
476
+ id TEXT PRIMARY KEY,
477
+ key TEXT NOT NULL,
478
+ content TEXT NOT NULL,
479
+ type TEXT NOT NULL DEFAULT 'episodic',
480
+ namespace TEXT NOT NULL DEFAULT 'transcript-archive',
481
+ tags JSONB NOT NULL DEFAULT '[]',
482
+ metadata JSONB NOT NULL DEFAULT '{}',
483
+ access_level TEXT NOT NULL DEFAULT 'private',
484
+ created_at BIGINT NOT NULL,
485
+ updated_at BIGINT NOT NULL,
486
+ version INTEGER NOT NULL DEFAULT 1,
487
+ access_count INTEGER NOT NULL DEFAULT 0,
488
+ last_accessed_at BIGINT NOT NULL,
489
+ content_hash TEXT,
490
+ session_id TEXT,
491
+ chunk_index INTEGER,
492
+ summary TEXT,
493
+ embedding vector(768)
494
+ );
495
+
496
+ CREATE INDEX IF NOT EXISTS idx_te_namespace ON transcript_entries(namespace);
497
+ CREATE INDEX IF NOT EXISTS idx_te_session ON transcript_entries(session_id);
498
+ CREATE INDEX IF NOT EXISTS idx_te_hash ON transcript_entries(content_hash);
499
+ CREATE INDEX IF NOT EXISTS idx_te_chunk ON transcript_entries(session_id, chunk_index);
500
+ CREATE INDEX IF NOT EXISTS idx_te_created ON transcript_entries(created_at);
501
+ `);
502
+ } finally {
503
+ client.release();
504
+ }
505
+ }
506
+
507
+ async store(entry) {
508
+ const embeddingArr = entry._embedding
509
+ ? `[${Array.from(entry._embedding).join(',')}]`
510
+ : null;
511
+ await this.pool.query(
512
+ `INSERT INTO transcript_entries
513
+ (id, key, content, type, namespace, tags, metadata, access_level,
514
+ created_at, updated_at, version, access_count, last_accessed_at,
515
+ content_hash, session_id, chunk_index, summary, embedding)
516
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)
517
+ ON CONFLICT (id) DO NOTHING`,
518
+ [
519
+ entry.id, entry.key, entry.content, entry.type, entry.namespace,
520
+ JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
521
+ entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
522
+ entry.metadata?.contentHash || null,
523
+ entry.metadata?.sessionId || null,
524
+ entry.metadata?.chunkIndex ?? null,
525
+ entry.metadata?.summary || null,
526
+ embeddingArr,
527
+ ]
528
+ );
529
+ }
530
+
531
+ async bulkInsert(entries) {
532
+ const client = await this.pool.connect();
533
+ try {
534
+ await client.query('BEGIN');
535
+ for (const entry of entries) {
536
+ const embeddingArr = entry._embedding
537
+ ? `[${Array.from(entry._embedding).join(',')}]`
538
+ : null;
539
+ await client.query(
540
+ `INSERT INTO transcript_entries
541
+ (id, key, content, type, namespace, tags, metadata, access_level,
542
+ created_at, updated_at, version, access_count, last_accessed_at,
543
+ content_hash, session_id, chunk_index, summary, embedding)
544
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)
545
+ ON CONFLICT (id) DO NOTHING`,
546
+ [
547
+ entry.id, entry.key, entry.content, entry.type, entry.namespace,
548
+ JSON.stringify(entry.tags), JSON.stringify(entry.metadata), entry.accessLevel,
549
+ entry.createdAt, entry.updatedAt, entry.version, entry.accessCount, entry.lastAccessedAt,
550
+ entry.metadata?.contentHash || null,
551
+ entry.metadata?.sessionId || null,
552
+ entry.metadata?.chunkIndex ?? null,
553
+ entry.metadata?.summary || null,
554
+ embeddingArr,
555
+ ]
556
+ );
557
+ }
558
+ await client.query('COMMIT');
559
+ } catch (err) {
560
+ await client.query('ROLLBACK');
561
+ throw err;
562
+ } finally {
563
+ client.release();
564
+ }
565
+ }
566
+
567
+ async query(opts) {
568
+ let sql = 'SELECT * FROM transcript_entries';
569
+ const params = [];
570
+ const clauses = [];
571
+ if (opts?.namespace) { params.push(opts.namespace); clauses.push(`namespace = $${params.length}`); }
572
+ if (clauses.length) sql += ' WHERE ' + clauses.join(' AND ');
573
+ sql += ' ORDER BY created_at DESC';
574
+ if (opts?.limit) { params.push(opts.limit); sql += ` LIMIT $${params.length}`; }
575
+ const { rows } = await this.pool.query(sql, params);
576
+ return rows.map(r => this._rowToEntry(r));
577
+ }
578
+
579
+ async queryBySession(namespace, sessionId) {
580
+ const { rows } = await this.pool.query(
581
+ 'SELECT * FROM transcript_entries WHERE namespace = $1 AND session_id = $2 ORDER BY chunk_index DESC',
582
+ [namespace, sessionId]
583
+ );
584
+ return rows.map(r => this._rowToEntry(r));
585
+ }
586
+
587
+ hashExists(hash) {
588
+ // Synchronous check not possible with pg — use a cached check
589
+ // The bulkInsert uses ON CONFLICT DO NOTHING for dedup at DB level
590
+ return false;
591
+ }
592
+
593
+ async hashExistsAsync(hash) {
594
+ const { rows } = await this.pool.query(
595
+ 'SELECT 1 FROM transcript_entries WHERE content_hash = $1 LIMIT 1',
596
+ [hash]
597
+ );
598
+ return rows.length > 0;
599
+ }
600
+
601
+ async count(namespace) {
602
+ const sql = namespace
603
+ ? 'SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = $1'
604
+ : 'SELECT COUNT(*) as cnt FROM transcript_entries';
605
+ const params = namespace ? [namespace] : [];
606
+ const { rows } = await this.pool.query(sql, params);
607
+ return parseInt(rows[0].cnt, 10);
608
+ }
609
+
610
+ async listNamespaces() {
611
+ const { rows } = await this.pool.query('SELECT DISTINCT namespace FROM transcript_entries');
612
+ return rows.map(r => r.namespace);
613
+ }
614
+
615
+ async listSessions(namespace) {
616
+ const { rows } = await this.pool.query(
617
+ `SELECT session_id, COUNT(*) as cnt FROM transcript_entries
618
+ WHERE namespace = $1 GROUP BY session_id ORDER BY MAX(created_at) DESC`,
619
+ [namespace || NAMESPACE]
620
+ );
621
+ return rows.map(r => ({ session_id: r.session_id, cnt: parseInt(r.cnt, 10) }));
622
+ }
623
+
624
+ async markAccessed(ids) {
625
+ const now = Date.now();
626
+ for (const id of ids) {
627
+ await this.pool.query(
628
+ 'UPDATE transcript_entries SET access_count = access_count + 1, last_accessed_at = $1 WHERE id = $2',
629
+ [now, id]
630
+ );
631
+ }
632
+ }
633
+
634
+ async pruneStale(namespace, maxAgeDays) {
635
+ const cutoff = Date.now() - (maxAgeDays * 24 * 60 * 60 * 1000);
636
+ const { rowCount } = await this.pool.query(
637
+ 'DELETE FROM transcript_entries WHERE namespace = $1 AND access_count = 0 AND created_at < $2',
638
+ [namespace || NAMESPACE, cutoff]
639
+ );
640
+ return rowCount;
641
+ }
642
+
643
+ async queryByImportance(namespace, sessionId) {
644
+ const now = Date.now();
645
+ const { rows } = await this.pool.query(`
646
+ SELECT *, (
647
+ (CAST(access_count AS REAL) + 1) *
648
+ (1.0 / (1.0 + ($1 - created_at) / 86400000.0)) *
649
+ (CASE WHEN jsonb_array_length(metadata->'toolNames') > 0 THEN 1.5 ELSE 1.0 END) *
650
+ (CASE WHEN jsonb_array_length(metadata->'filePaths') > 0 THEN 1.3 ELSE 1.0 END)
651
+ ) AS importance_score
652
+ FROM transcript_entries
653
+ WHERE namespace = $2 AND session_id = $3
654
+ ORDER BY importance_score DESC
655
+ `, [now, namespace, sessionId]);
656
+ return rows.map(r => ({ ...this._rowToEntry(r), importanceScore: r.importance_score }));
657
+ }
658
+
659
+ async shutdown() {
660
+ if (this.pool) {
661
+ await this.pool.end();
662
+ this.pool = null;
663
+ }
664
+ }
665
+
666
+ _rowToEntry(row) {
667
+ return {
668
+ id: row.id,
669
+ key: row.key,
670
+ content: row.content,
671
+ type: row.type,
672
+ namespace: row.namespace,
673
+ tags: typeof row.tags === 'string' ? JSON.parse(row.tags) : row.tags,
674
+ metadata: typeof row.metadata === 'string' ? JSON.parse(row.metadata) : row.metadata,
675
+ accessLevel: row.access_level,
676
+ createdAt: parseInt(row.created_at, 10),
677
+ updatedAt: parseInt(row.updated_at, 10),
678
+ version: row.version,
679
+ accessCount: row.access_count,
680
+ lastAccessedAt: parseInt(row.last_accessed_at, 10),
681
+ references: [],
682
+ };
683
+ }
684
+ }
685
+
686
+ /**
687
+ * Parse RuVector config from environment variables.
688
+ * Returns null if required vars are not set.
689
+ */
690
+ function getRuVectorConfig() {
691
+ const host = process.env.RUVECTOR_HOST || process.env.PGHOST;
692
+ const database = process.env.RUVECTOR_DATABASE || process.env.PGDATABASE;
693
+ const user = process.env.RUVECTOR_USER || process.env.PGUSER;
694
+ const password = process.env.RUVECTOR_PASSWORD || process.env.PGPASSWORD;
695
+
696
+ if (!host || !database || !user) return null;
697
+
698
+ return {
699
+ host,
700
+ port: parseInt(process.env.RUVECTOR_PORT || process.env.PGPORT || '5432', 10),
701
+ database,
702
+ user,
703
+ password: password || '',
704
+ ssl: process.env.RUVECTOR_SSL === 'true',
705
+ };
706
+ }
707
+
708
+ // ============================================================================
709
+ // Backend resolution: SQLite > RuVector PostgreSQL > AgentDB > JSON
710
+ // ============================================================================
711
+
712
+ async function resolveBackend() {
713
+ // Tier 1: better-sqlite3 (native, fastest, local)
714
+ try {
715
+ const backend = new SQLiteBackend(ARCHIVE_DB_PATH);
716
+ await backend.initialize();
717
+ return { backend, type: 'sqlite' };
718
+ } catch { /* fall through */ }
719
+
720
+ // Tier 2: RuVector PostgreSQL (TB-scale, vector search, GNN)
721
+ try {
722
+ const rvConfig = getRuVectorConfig();
723
+ if (rvConfig) {
724
+ const backend = new RuVectorBackend(rvConfig);
725
+ await backend.initialize();
726
+ return { backend, type: 'ruvector' };
727
+ }
728
+ } catch { /* fall through */ }
729
+
730
+ // Tier 3: AgentDB from @claude-flow/memory (HNSW)
731
+ try {
732
+ const localDist = join(PROJECT_ROOT, 'v3/@claude-flow/memory/dist/index.js');
733
+ let memPkg = null;
734
+ if (existsSync(localDist)) {
735
+ memPkg = await import(`file://${localDist}`);
736
+ } else {
737
+ memPkg = await import('@claude-flow/memory');
738
+ }
739
+ if (memPkg?.AgentDBBackend) {
740
+ const backend = new memPkg.AgentDBBackend();
741
+ await backend.initialize();
742
+ return { backend, type: 'agentdb' };
743
+ }
744
+ } catch { /* fall through */ }
745
+
746
+ // Tier 4: JSON file (always works)
747
+ const backend = new JsonFileBackend(ARCHIVE_JSON_PATH);
748
+ await backend.initialize();
749
+ return { backend, type: 'json' };
750
+ }
751
+
752
+ // ============================================================================
753
+ // ONNX Embedding (384-dim, all-MiniLM-L6-v2 via @xenova/transformers)
754
+ // ============================================================================
755
+
756
+ const EMBEDDING_DIM = 384; // ONNX all-MiniLM-L6-v2 output dimension
757
+ let _onnxPipeline = null;
758
+ let _onnxFailed = false;
759
+
760
+ /**
761
+ * Initialize ONNX embedding pipeline (lazy, cached).
762
+ * Returns null if @xenova/transformers is not available.
763
+ */
764
+ async function getOnnxPipeline() {
765
+ if (_onnxFailed) return null;
766
+ if (_onnxPipeline) return _onnxPipeline;
767
+ try {
768
+ const { pipeline } = await import('@xenova/transformers');
769
+ _onnxPipeline = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
770
+ return _onnxPipeline;
771
+ } catch {
772
+ _onnxFailed = true;
773
+ return null;
774
+ }
775
+ }
776
+
777
+ /**
778
+ * Generate ONNX embedding (384-dim, high quality semantic vectors).
779
+ * Falls back to hash embedding if ONNX is unavailable.
780
+ */
781
+ async function createEmbedding(text) {
782
+ // Try ONNX first (384-dim, real semantic understanding)
783
+ const pipe = await getOnnxPipeline();
784
+ if (pipe) {
785
+ try {
786
+ const truncated = text.slice(0, 512); // MiniLM max ~512 tokens
787
+ const output = await pipe(truncated, { pooling: 'mean', normalize: true });
788
+ return { embedding: new Float32Array(output.data), dim: 384, method: 'onnx' };
789
+ } catch { /* fall through to hash */ }
790
+ }
791
+ // Fallback: hash embedding (384-dim to match ONNX dimension)
792
+ return { embedding: createHashEmbedding(text, 384), dim: 384, method: 'hash' };
793
+ }
794
+
795
+ // ============================================================================
796
+ // Hash embedding fallback (deterministic, sub-millisecond)
797
+ // ============================================================================
798
+
799
+ function createHashEmbedding(text, dimensions = 384) {
800
+ const embedding = new Float32Array(dimensions);
801
+ const normalized = text.toLowerCase().trim();
802
+ for (let i = 0; i < dimensions; i++) {
803
+ let hash = 0;
804
+ for (let j = 0; j < normalized.length; j++) {
805
+ hash = ((hash << 5) - hash + normalized.charCodeAt(j) * (i + 1)) | 0;
806
+ }
807
+ embedding[i] = (Math.sin(hash) + 1) / 2;
808
+ }
809
+ let norm = 0;
810
+ for (let i = 0; i < dimensions; i++) norm += embedding[i] * embedding[i];
811
+ norm = Math.sqrt(norm);
812
+ if (norm > 0) for (let i = 0; i < dimensions; i++) embedding[i] /= norm;
813
+ return embedding;
814
+ }
815
+
816
+ // ============================================================================
817
+ // Content hash for dedup
818
+ // ============================================================================
819
+
820
+ function hashContent(content) {
821
+ return createHash('sha256').update(content).digest('hex');
822
+ }
823
+
824
+ // ============================================================================
825
+ // Read stdin with timeout (hooks receive JSON input on stdin)
826
+ // ============================================================================
827
+
828
+ function readStdin(timeoutMs = 100) {
829
+ return new Promise((resolve) => {
830
+ let data = '';
831
+ const timer = setTimeout(() => {
832
+ process.stdin.removeAllListeners();
833
+ resolve(data ? JSON.parse(data) : null);
834
+ }, timeoutMs);
835
+
836
+ if (process.stdin.isTTY) {
837
+ clearTimeout(timer);
838
+ resolve(null);
839
+ return;
840
+ }
841
+
842
+ process.stdin.setEncoding('utf-8');
843
+ process.stdin.on('data', (chunk) => { data += chunk; });
844
+ process.stdin.on('end', () => {
845
+ clearTimeout(timer);
846
+ try { resolve(data ? JSON.parse(data) : null); }
847
+ catch { resolve(null); }
848
+ });
849
+ process.stdin.on('error', () => {
850
+ clearTimeout(timer);
851
+ resolve(null);
852
+ });
853
+ process.stdin.resume();
854
+ });
855
+ }
856
+
857
+ // ============================================================================
858
+ // Transcript parsing
859
+ // ============================================================================
860
+
861
+ function parseTranscript(transcriptPath) {
862
+ if (!existsSync(transcriptPath)) return [];
863
+ const content = readFileSync(transcriptPath, 'utf-8');
864
+ const lines = content.split('\n').filter(Boolean);
865
+ const messages = [];
866
+ for (const line of lines) {
867
+ try {
868
+ const parsed = JSON.parse(line);
869
+ // SDK transcript wraps messages: { type: "user"|"A", message: { role, content } }
870
+ // Unwrap to get the inner API message with role/content
871
+ if (parsed.message && parsed.message.role) {
872
+ messages.push(parsed.message);
873
+ } else if (parsed.role) {
874
+ // Already in API message format (e.g. from tests)
875
+ messages.push(parsed);
876
+ }
877
+ // Skip non-message entries (progress, file-history-snapshot, queue-operation)
878
+ } catch { /* skip malformed lines */ }
879
+ }
880
+ return messages;
881
+ }
882
+
883
+ // ============================================================================
884
+ // Extract text content from message content blocks
885
+ // ============================================================================
886
+
887
+ function extractTextContent(message) {
888
+ if (!message) return '';
889
+ if (typeof message.content === 'string') return message.content;
890
+ if (Array.isArray(message.content)) {
891
+ return message.content
892
+ .filter(b => b.type === 'text')
893
+ .map(b => b.text || '')
894
+ .join('\n');
895
+ }
896
+ if (typeof message.text === 'string') return message.text;
897
+ return '';
898
+ }
899
+
900
+ // ============================================================================
901
+ // Extract tool calls from assistant message
902
+ // ============================================================================
903
+
904
+ function extractToolCalls(message) {
905
+ if (!message || !Array.isArray(message.content)) return [];
906
+ return message.content
907
+ .filter(b => b.type === 'tool_use')
908
+ .map(b => ({
909
+ name: b.name || 'unknown',
910
+ input: b.input || {},
911
+ }));
912
+ }
913
+
914
+ // ============================================================================
915
+ // Extract file paths from tool calls
916
+ // ============================================================================
917
+
918
+ function extractFilePaths(toolCalls) {
919
+ const paths = new Set();
920
+ for (const tc of toolCalls) {
921
+ if (tc.input?.file_path) paths.add(tc.input.file_path);
922
+ if (tc.input?.path) paths.add(tc.input.path);
923
+ if (tc.input?.notebook_path) paths.add(tc.input.notebook_path);
924
+ }
925
+ return [...paths];
926
+ }
927
+
928
+ // ============================================================================
929
+ // Chunk transcript into conversation turns
930
+ // ============================================================================
931
+
932
+ function chunkTranscript(messages) {
933
+ const relevant = messages.filter(
934
+ m => m.role === 'user' || m.role === 'assistant'
935
+ );
936
+ const capped = relevant.slice(-MAX_MESSAGES);
937
+
938
+ const chunks = [];
939
+ let currentChunk = null;
940
+
941
+ for (const msg of capped) {
942
+ if (msg.role === 'user') {
943
+ const isSynthetic = Array.isArray(msg.content) &&
944
+ msg.content.every(b => b.type === 'tool_result');
945
+ if (isSynthetic && currentChunk) continue;
946
+ if (currentChunk) chunks.push(currentChunk);
947
+ currentChunk = {
948
+ userMessage: msg,
949
+ assistantMessage: null,
950
+ toolCalls: [],
951
+ turnIndex: chunks.length,
952
+ };
953
+ } else if (msg.role === 'assistant' && currentChunk) {
954
+ currentChunk.assistantMessage = msg;
955
+ currentChunk.toolCalls = extractToolCalls(msg);
956
+ }
957
+ }
958
+
959
+ if (currentChunk) chunks.push(currentChunk);
960
+ return chunks;
961
+ }
962
+
963
+ // ============================================================================
964
+ // Extract summary from chunk (no LLM, extractive only)
965
+ // ============================================================================
966
+
967
+ function extractSummary(chunk) {
968
+ const parts = [];
969
+
970
+ const userText = extractTextContent(chunk.userMessage);
971
+ const firstUserLine = userText.split('\n').find(l => l.trim()) || '';
972
+ if (firstUserLine) parts.push(firstUserLine.slice(0, 100));
973
+
974
+ const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
975
+ if (toolNames.length) parts.push('Tools: ' + toolNames.join(', '));
976
+
977
+ const filePaths = extractFilePaths(chunk.toolCalls);
978
+ if (filePaths.length) {
979
+ const shortPaths = filePaths.slice(0, 5).map(p => {
980
+ const segs = p.split('/');
981
+ return segs.length > 2 ? '.../' + segs.slice(-2).join('/') : p;
982
+ });
983
+ parts.push('Files: ' + shortPaths.join(', '));
984
+ }
985
+
986
+ const assistantText = extractTextContent(chunk.assistantMessage);
987
+ const assistantLines = assistantText.split('\n').filter(l => l.trim()).slice(0, 2);
988
+ if (assistantLines.length) parts.push(assistantLines.join(' ').slice(0, 120));
989
+
990
+ return parts.join(' | ').slice(0, 300);
991
+ }
992
+
993
+ // ============================================================================
994
+ // Generate unique ID
995
+ // ============================================================================
996
+
997
+ let idCounter = 0;
998
+ function generateId() {
999
+ return `ctx-${Date.now()}-${++idCounter}-${Math.random().toString(36).slice(2, 8)}`;
1000
+ }
1001
+
1002
+ // ============================================================================
1003
+ // Build MemoryEntry from chunk
1004
+ // ============================================================================
1005
+
1006
+ function buildEntry(chunk, sessionId, trigger, timestamp) {
1007
+ const userText = extractTextContent(chunk.userMessage);
1008
+ const assistantText = extractTextContent(chunk.assistantMessage);
1009
+ const fullContent = `User: ${userText}\n\nAssistant: ${assistantText}`;
1010
+ const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1011
+ const filePaths = extractFilePaths(chunk.toolCalls);
1012
+ const summary = extractSummary(chunk);
1013
+ const contentHash = hashContent(fullContent);
1014
+
1015
+ const now = Date.now();
1016
+ return {
1017
+ id: generateId(),
1018
+ key: `transcript:${sessionId}:${chunk.turnIndex}:${timestamp}`,
1019
+ content: fullContent,
1020
+ type: 'episodic',
1021
+ namespace: NAMESPACE,
1022
+ tags: ['transcript', 'compaction', sessionId, ...toolNames],
1023
+ metadata: {
1024
+ sessionId,
1025
+ chunkIndex: chunk.turnIndex,
1026
+ trigger,
1027
+ timestamp,
1028
+ toolNames,
1029
+ filePaths,
1030
+ summary,
1031
+ contentHash,
1032
+ turnRange: [chunk.turnIndex, chunk.turnIndex],
1033
+ },
1034
+ accessLevel: 'private',
1035
+ createdAt: now,
1036
+ updatedAt: now,
1037
+ version: 1,
1038
+ references: [],
1039
+ accessCount: 0,
1040
+ lastAccessedAt: now,
1041
+ };
1042
+ }
1043
+
1044
+ // ============================================================================
1045
+ // Store chunks with dedup (uses indexed hash lookup for SQLite)
1046
+ // ============================================================================
1047
+
1048
+ async function storeChunks(backend, chunks, sessionId, trigger) {
1049
+ const timestamp = new Date().toISOString();
1050
+
1051
+ const entries = [];
1052
+ for (const chunk of chunks) {
1053
+ const entry = buildEntry(chunk, sessionId, trigger, timestamp);
1054
+ // Fast hash-based dedup (indexed lookup in SQLite, scan in JSON)
1055
+ if (!backend.hashExists(entry.metadata.contentHash)) {
1056
+ entries.push(entry);
1057
+ }
1058
+ }
1059
+
1060
+ if (entries.length > 0) {
1061
+ await backend.bulkInsert(entries);
1062
+ }
1063
+
1064
+ return { stored: entries.length, deduped: chunks.length - entries.length };
1065
+ }
1066
+
1067
+ // ============================================================================
1068
+ // Retrieve context for restoration (uses indexed session query for SQLite)
1069
+ // ============================================================================
1070
+
1071
+ async function retrieveContext(backend, sessionId, budget) {
1072
+ // Use optimized session query if available, otherwise filter manually
1073
+ const sessionEntries = backend.queryBySession
1074
+ ? await backend.queryBySession(NAMESPACE, sessionId)
1075
+ : (await backend.query({ namespace: NAMESPACE }))
1076
+ .filter(e => e.metadata?.sessionId === sessionId)
1077
+ .sort((a, b) => (b.metadata?.chunkIndex ?? 0) - (a.metadata?.chunkIndex ?? 0));
1078
+
1079
+ if (sessionEntries.length === 0) return '';
1080
+
1081
+ const lines = [];
1082
+ let charCount = 0;
1083
+ const header = `## Restored Context (from pre-compaction archive)\n\nPrevious conversation included ${sessionEntries.length} archived turns:\n\n`;
1084
+ charCount += header.length;
1085
+
1086
+ for (const entry of sessionEntries) {
1087
+ const meta = entry.metadata || {};
1088
+ const toolStr = meta.toolNames?.length ? ` Tools: ${meta.toolNames.join(', ')}.` : '';
1089
+ const fileStr = meta.filePaths?.length ? ` Files: ${meta.filePaths.slice(0, 3).join(', ')}.` : '';
1090
+ const line = `- [Turn ${meta.chunkIndex ?? '?'}] ${meta.summary || '(no summary)'}${toolStr}${fileStr}`;
1091
+
1092
+ if (charCount + line.length + 1 > budget) break;
1093
+ lines.push(line);
1094
+ charCount += line.length + 1;
1095
+ }
1096
+
1097
+ if (lines.length === 0) return '';
1098
+
1099
+ const footer = `\n\nFull archive: ${NAMESPACE} namespace in AgentDB (query with session ID: ${sessionId})`;
1100
+ return header + lines.join('\n') + footer;
1101
+ }
1102
+
1103
+ // ============================================================================
1104
+ // Build custom compact instructions (exit code 0 stdout)
1105
+ // Guides Claude on what to preserve during compaction summary
1106
+ // ============================================================================
1107
+
1108
+ function buildCompactInstructions(chunks, sessionId, archiveResult) {
1109
+ const parts = [];
1110
+
1111
+ parts.push('COMPACTION GUIDANCE (from context-persistence-hook):');
1112
+ parts.push('');
1113
+ parts.push(`All ${chunks.length} conversation turns have been archived to the transcript-archive database.`);
1114
+ parts.push(`Session: ${sessionId} | Stored: ${archiveResult.stored} new, ${archiveResult.deduped} deduped.`);
1115
+ parts.push('After compaction, archived context will be automatically restored via SessionStart hook.');
1116
+ parts.push('');
1117
+
1118
+ // Collect unique tools and files across all chunks for preservation hints
1119
+ const allTools = new Set();
1120
+ const allFiles = new Set();
1121
+ const decisions = [];
1122
+
1123
+ for (const chunk of chunks) {
1124
+ const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1125
+ for (const t of toolNames) allTools.add(t);
1126
+ const filePaths = extractFilePaths(chunk.toolCalls);
1127
+ for (const f of filePaths) allFiles.add(f);
1128
+
1129
+ // Look for decision indicators in assistant text
1130
+ const assistantText = extractTextContent(chunk.assistantMessage);
1131
+ if (assistantText) {
1132
+ const lower = assistantText.toLowerCase();
1133
+ if (lower.includes('decided') || lower.includes('choosing') || lower.includes('approach')
1134
+ || lower.includes('instead of') || lower.includes('rather than')) {
1135
+ const firstLine = assistantText.split('\n').find(l => l.trim()) || '';
1136
+ if (firstLine.length > 10) decisions.push(firstLine.slice(0, 120));
1137
+ }
1138
+ }
1139
+ }
1140
+
1141
+ parts.push('PRESERVE in compaction summary:');
1142
+
1143
+ if (allFiles.size > 0) {
1144
+ const fileList = [...allFiles].slice(0, 15).map(f => {
1145
+ const segs = f.split('/');
1146
+ return segs.length > 3 ? '.../' + segs.slice(-3).join('/') : f;
1147
+ });
1148
+ parts.push(`- Files modified/read: ${fileList.join(', ')}`);
1149
+ }
1150
+
1151
+ if (allTools.size > 0) {
1152
+ parts.push(`- Tools used: ${[...allTools].join(', ')}`);
1153
+ }
1154
+
1155
+ if (decisions.length > 0) {
1156
+ parts.push('- Key decisions:');
1157
+ for (const d of decisions.slice(0, 5)) {
1158
+ parts.push(` * ${d}`);
1159
+ }
1160
+ }
1161
+
1162
+ // Recent turns summary (most important context)
1163
+ const recentChunks = chunks.slice(-5);
1164
+ if (recentChunks.length > 0) {
1165
+ parts.push('');
1166
+ parts.push('MOST RECENT TURNS (prioritize preserving):');
1167
+ for (const chunk of recentChunks) {
1168
+ const userText = extractTextContent(chunk.userMessage);
1169
+ const firstLine = userText.split('\n').find(l => l.trim()) || '';
1170
+ const toolNames = [...new Set(chunk.toolCalls.map(tc => tc.name))];
1171
+ parts.push(`- [Turn ${chunk.turnIndex}] ${firstLine.slice(0, 80)}${toolNames.length ? ` (${toolNames.join(', ')})` : ''}`);
1172
+ }
1173
+ }
1174
+
1175
+ // Cap at budget
1176
+ let result = parts.join('\n');
1177
+ if (result.length > COMPACT_INSTRUCTION_BUDGET) {
1178
+ result = result.slice(0, COMPACT_INSTRUCTION_BUDGET - 3) + '...';
1179
+ }
1180
+ return result;
1181
+ }
1182
+
1183
+ // ============================================================================
1184
+ // Importance scoring for retrieval ranking
1185
+ // ============================================================================
1186
+
1187
+ function computeImportance(entry, now) {
1188
+ const meta = entry.metadata || {};
1189
+ const accessCount = entry.accessCount || 0;
1190
+ const createdAt = entry.createdAt || now;
1191
+ const ageMs = Math.max(1, now - createdAt);
1192
+ const ageDays = ageMs / 86400000;
1193
+
1194
+ // Recency: exponential decay, half-life of 7 days
1195
+ const recency = Math.exp(-0.693 * ageDays / 7);
1196
+
1197
+ // Frequency: log-scaled access count
1198
+ const frequency = Math.log2(accessCount + 1) + 1;
1199
+
1200
+ // Richness: tool calls and file paths indicate actionable context
1201
+ const toolCount = meta.toolNames?.length || 0;
1202
+ const fileCount = meta.filePaths?.length || 0;
1203
+ const richness = 1.0 + (toolCount > 0 ? 0.5 : 0) + (fileCount > 0 ? 0.3 : 0);
1204
+
1205
+ return recency * frequency * richness;
1206
+ }
1207
+
1208
+ // ============================================================================
1209
+ // Smart retrieval: importance-ranked instead of just recency
1210
+ // ============================================================================
1211
+
1212
+ async function retrieveContextSmart(backend, sessionId, budget) {
1213
+ let sessionEntries;
1214
+
1215
+ // Use importance-ranked query if backend supports it
1216
+ if (backend.queryByImportance) {
1217
+ try {
1218
+ sessionEntries = backend.queryByImportance(NAMESPACE, sessionId);
1219
+ } catch {
1220
+ // Fall back to standard query
1221
+ sessionEntries = null;
1222
+ }
1223
+ }
1224
+
1225
+ if (!sessionEntries) {
1226
+ // Fall back: fetch all, compute importance in JS
1227
+ const raw = backend.queryBySession
1228
+ ? await backend.queryBySession(NAMESPACE, sessionId)
1229
+ : (await backend.query({ namespace: NAMESPACE }))
1230
+ .filter(e => e.metadata?.sessionId === sessionId);
1231
+
1232
+ const now = Date.now();
1233
+ sessionEntries = raw
1234
+ .map(e => ({ ...e, importanceScore: computeImportance(e, now) }))
1235
+ .sort((a, b) => b.importanceScore - a.importanceScore);
1236
+ }
1237
+
1238
+ if (sessionEntries.length === 0) return { text: '', accessedIds: [] };
1239
+
1240
+ const lines = [];
1241
+ const accessedIds = [];
1242
+ let charCount = 0;
1243
+ const header = `## Restored Context (importance-ranked from archive)\n\nPrevious conversation: ${sessionEntries.length} archived turns, ranked by importance:\n\n`;
1244
+ charCount += header.length;
1245
+
1246
+ for (const entry of sessionEntries) {
1247
+ const meta = entry.metadata || {};
1248
+ const score = entry.importanceScore?.toFixed(2) || '?';
1249
+ const toolStr = meta.toolNames?.length ? ` Tools: ${meta.toolNames.join(', ')}.` : '';
1250
+ const fileStr = meta.filePaths?.length ? ` Files: ${meta.filePaths.slice(0, 3).join(', ')}.` : '';
1251
+ const line = `- [Turn ${meta.chunkIndex ?? '?'}, score:${score}] ${meta.summary || '(no summary)'}${toolStr}${fileStr}`;
1252
+
1253
+ if (charCount + line.length + 1 > budget) break;
1254
+ lines.push(line);
1255
+ accessedIds.push(entry.id);
1256
+ charCount += line.length + 1;
1257
+ }
1258
+
1259
+ if (lines.length === 0) return { text: '', accessedIds: [] };
1260
+
1261
+ // Cross-session semantic search: find related context from previous sessions
1262
+ let crossSessionText = '';
1263
+ if (backend.semanticSearch && sessionEntries.length > 0) {
1264
+ try {
1265
+ // Use the most recent turn's summary as the search query
1266
+ const recentSummary = sessionEntries[0]?.metadata?.summary || '';
1267
+ if (recentSummary) {
1268
+ const crossResults = await crossSessionSearch(backend, recentSummary, sessionId, 3);
1269
+ if (crossResults.length > 0) {
1270
+ const crossLines = crossResults.map(r =>
1271
+ `- [Session ${r.sessionId?.slice(0, 8)}..., turn ${r.chunkIndex ?? '?'}, conf:${(r.confidence || 0).toFixed(2)}] ${r.summary || '(no summary)'}`
1272
+ );
1273
+ crossSessionText = `\n\nRelated context from previous sessions:\n${crossLines.join('\n')}`;
1274
+ }
1275
+ }
1276
+ } catch { /* cross-session search is best-effort */ }
1277
+ }
1278
+
1279
+ const footer = `\n\nFull archive: ${NAMESPACE} namespace (session: ${sessionId}). ${sessionEntries.length - lines.length} additional turns available.`;
1280
+ return { text: header + lines.join('\n') + crossSessionText + footer, accessedIds };
1281
+ }
1282
+
1283
+ // ============================================================================
1284
+ // Auto-optimize: prune stale entries, run after archiving
1285
+ // ============================================================================
1286
+
1287
+ async function autoOptimize(backend, backendType) {
1288
+ if (!AUTO_OPTIMIZE) return { pruned: 0, synced: 0, decayed: 0, embedded: 0 };
1289
+
1290
+ let pruned = 0;
1291
+ let decayed = 0;
1292
+ let embedded = 0;
1293
+
1294
+ // Step 1: Confidence decay — reduce confidence for unaccessed entries
1295
+ if (backend.decayConfidence) {
1296
+ try {
1297
+ decayed = backend.decayConfidence(NAMESPACE, 1); // 1 hour worth of decay per optimize cycle
1298
+ } catch { /* non-critical */ }
1299
+ }
1300
+
1301
+ // Step 2: Smart pruning — remove low-confidence entries first
1302
+ if (backend.pruneByConfidence) {
1303
+ try {
1304
+ pruned += backend.pruneByConfidence(NAMESPACE, 0.15);
1305
+ } catch { /* non-critical */ }
1306
+ }
1307
+
1308
+ // Step 3: Age-based pruning as fallback
1309
+ if (backend.pruneStale) {
1310
+ try {
1311
+ pruned += backend.pruneStale(NAMESPACE, RETENTION_DAYS);
1312
+ } catch { /* non-critical */ }
1313
+ }
1314
+
1315
+ // Step 4: Generate ONNX embeddings (384-dim) for entries missing them
1316
+ if (backend.storeEmbedding) {
1317
+ try {
1318
+ const rows = backend.db?.prepare?.(
1319
+ 'SELECT id, content FROM transcript_entries WHERE namespace = ? AND embedding IS NULL LIMIT 20'
1320
+ )?.all(NAMESPACE);
1321
+ if (rows) {
1322
+ for (const row of rows) {
1323
+ const { embedding } = await createEmbedding(row.content);
1324
+ backend.storeEmbedding(row.id, embedding);
1325
+ embedded++;
1326
+ }
1327
+ }
1328
+ } catch { /* non-critical */ }
1329
+ }
1330
+
1331
+ // Step 5: Auto-sync to RuVector if available
1332
+ let synced = 0;
1333
+ if (backendType === 'sqlite' && backend.allForSync) {
1334
+ try {
1335
+ const rvConfig = getRuVectorConfig();
1336
+ if (rvConfig) {
1337
+ const rvBackend = new RuVectorBackend(rvConfig);
1338
+ await rvBackend.initialize();
1339
+
1340
+ const allEntries = backend.allForSync(NAMESPACE);
1341
+ if (allEntries.length > 0) {
1342
+ // Add hash embeddings for vector search in RuVector
1343
+ const entriesToSync = allEntries.map(e => ({
1344
+ ...e,
1345
+ _embedding: createHashEmbedding(e.content),
1346
+ }));
1347
+ await rvBackend.bulkInsert(entriesToSync);
1348
+ synced = entriesToSync.length;
1349
+ }
1350
+
1351
+ await rvBackend.shutdown();
1352
+ }
1353
+ } catch { /* RuVector sync is best-effort */ }
1354
+ }
1355
+
1356
+ return { pruned, synced, decayed, embedded };
1357
+ }
1358
+
1359
+ // ============================================================================
1360
+ // Cross-session semantic retrieval
1361
+ // ============================================================================
1362
+
1363
+ /**
1364
+ * Find relevant context from OTHER sessions using semantic similarity.
1365
+ * This enables "What did we discuss about auth?" across sessions.
1366
+ */
1367
+ async function crossSessionSearch(backend, queryText, currentSessionId, k = 5) {
1368
+ if (!backend.semanticSearch) return [];
1369
+ try {
1370
+ const { embedding: queryEmb } = await createEmbedding(queryText);
1371
+ const results = backend.semanticSearch(queryEmb, k * 2, NAMESPACE);
1372
+ // Filter out current session entries (we already have those)
1373
+ return results
1374
+ .filter(r => r.sessionId !== currentSessionId)
1375
+ .slice(0, k);
1376
+ } catch { return []; }
1377
+ }
1378
+
1379
+ // ============================================================================
1380
+ // Context Autopilot Engine
1381
+ // ============================================================================
1382
+
1383
+ /**
1384
+ * Estimate context token usage from transcript JSONL.
1385
+ *
1386
+ * Primary method: Read the most recent assistant message's `usage` field which
1387
+ * contains `input_tokens` + `cache_read_input_tokens` — this is the ACTUAL
1388
+ * context size as reported by the Claude API. This includes system prompt,
1389
+ * CLAUDE.md, tool definitions, all messages, and everything Claude sees.
1390
+ *
1391
+ * Fallback: Sum character lengths and divide by CHARS_PER_TOKEN.
1392
+ */
1393
+ function estimateContextTokens(transcriptPath) {
1394
+ if (!existsSync(transcriptPath)) return { tokens: 0, turns: 0, method: 'none' };
1395
+
1396
+ const content = readFileSync(transcriptPath, 'utf-8');
1397
+ const lines = content.split('\n').filter(Boolean);
1398
+
1399
+ // Track the most recent usage data (from the last assistant message)
1400
+ let lastInputTokens = 0;
1401
+ let lastCacheRead = 0;
1402
+ let lastCacheCreate = 0;
1403
+ let turns = 0;
1404
+ let lastPreTokens = 0;
1405
+ let totalChars = 0;
1406
+
1407
+ for (let i = 0; i < lines.length; i++) {
1408
+ try {
1409
+ const parsed = JSON.parse(lines[i]);
1410
+
1411
+ // Check for compact_boundary
1412
+ if (parsed.type === 'system' && parsed.subtype === 'compact_boundary') {
1413
+ lastPreTokens = parsed.compactMetadata?.preTokens
1414
+ || parsed.compact_metadata?.pre_tokens || 0;
1415
+ // Reset after compaction — new context starts here
1416
+ totalChars = 0;
1417
+ turns = 0;
1418
+ lastInputTokens = 0;
1419
+ lastCacheRead = 0;
1420
+ lastCacheCreate = 0;
1421
+ continue;
1422
+ }
1423
+
1424
+ // Extract ACTUAL token usage from assistant messages
1425
+ // The SDK transcript stores: { message: { role, content, usage: { input_tokens, cache_read_input_tokens, ... } } }
1426
+ const msg = parsed.message || parsed;
1427
+ const usage = msg.usage;
1428
+ if (usage && (msg.role === 'assistant' || parsed.type === 'assistant')) {
1429
+ const inputTokens = usage.input_tokens || 0;
1430
+ const cacheRead = usage.cache_read_input_tokens || 0;
1431
+ const cacheCreate = usage.cache_creation_input_tokens || 0;
1432
+
1433
+ // The total context sent to Claude = input_tokens + cache_read + cache_create
1434
+ // input_tokens: non-cached tokens actually processed
1435
+ // cache_read: tokens served from cache (still in context)
1436
+ // cache_create: tokens newly cached (still in context)
1437
+ const totalContext = inputTokens + cacheRead + cacheCreate;
1438
+
1439
+ if (totalContext > 0) {
1440
+ lastInputTokens = inputTokens;
1441
+ lastCacheRead = cacheRead;
1442
+ lastCacheCreate = cacheCreate;
1443
+ }
1444
+ }
1445
+
1446
+ // Count turns for display
1447
+ const role = msg.role || parsed.type;
1448
+ if (role === 'user') turns++;
1449
+
1450
+ // Char fallback accumulation
1451
+ if (role === 'user' || role === 'assistant') {
1452
+ const c = msg.content;
1453
+ if (typeof c === 'string') totalChars += c.length;
1454
+ else if (Array.isArray(c)) {
1455
+ for (const block of c) {
1456
+ if (block.text) totalChars += block.text.length;
1457
+ else if (block.input) totalChars += JSON.stringify(block.input).length;
1458
+ }
1459
+ }
1460
+ }
1461
+ } catch { /* skip */ }
1462
+ }
1463
+
1464
+ // Primary: use actual API usage data
1465
+ const actualTotal = lastInputTokens + lastCacheRead + lastCacheCreate;
1466
+ if (actualTotal > 0) {
1467
+ return {
1468
+ tokens: actualTotal,
1469
+ turns,
1470
+ method: 'api-usage',
1471
+ lastPreTokens,
1472
+ breakdown: {
1473
+ input: lastInputTokens,
1474
+ cacheRead: lastCacheRead,
1475
+ cacheCreate: lastCacheCreate,
1476
+ },
1477
+ };
1478
+ }
1479
+
1480
+ // Fallback: char-based estimate
1481
+ const estimatedTokens = Math.ceil(totalChars / CHARS_PER_TOKEN);
1482
+ if (lastPreTokens > 0) {
1483
+ const compactSummaryTokens = 3000;
1484
+ return {
1485
+ tokens: compactSummaryTokens + estimatedTokens,
1486
+ turns,
1487
+ method: 'post-compact-char-estimate',
1488
+ lastPreTokens,
1489
+ };
1490
+ }
1491
+
1492
+ return { tokens: estimatedTokens, turns, method: 'char-estimate' };
1493
+ }
1494
+
1495
+ /**
1496
+ * Load autopilot state (persisted across hook invocations).
1497
+ */
1498
+ function loadAutopilotState() {
1499
+ try {
1500
+ if (existsSync(AUTOPILOT_STATE_PATH)) {
1501
+ return JSON.parse(readFileSync(AUTOPILOT_STATE_PATH, 'utf-8'));
1502
+ }
1503
+ } catch { /* fresh state */ }
1504
+ return {
1505
+ sessionId: null,
1506
+ lastTokenEstimate: 0,
1507
+ lastPercentage: 0,
1508
+ pruneCount: 0,
1509
+ warningIssued: false,
1510
+ lastCheck: 0,
1511
+ history: [], // Track token growth over time
1512
+ };
1513
+ }
1514
+
1515
+ /**
1516
+ * Save autopilot state.
1517
+ */
1518
+ function saveAutopilotState(state) {
1519
+ try {
1520
+ writeFileSync(AUTOPILOT_STATE_PATH, JSON.stringify(state, null, 2), 'utf-8');
1521
+ } catch { /* best effort */ }
1522
+ }
1523
+
1524
+ /**
1525
+ * Build a context optimization report for additionalContext injection.
1526
+ */
1527
+ function buildAutopilotReport(percentage, tokens, windowSize, turns, state) {
1528
+ const bar = buildProgressBar(percentage);
1529
+ const status = percentage >= AUTOPILOT_PRUNE_PCT
1530
+ ? 'OPTIMIZING'
1531
+ : percentage >= AUTOPILOT_WARN_PCT
1532
+ ? 'WARNING'
1533
+ : 'OK';
1534
+
1535
+ const parts = [
1536
+ `[ContextAutopilot] ${bar} ${(percentage * 100).toFixed(1)}% context used`,
1537
+ `(~${formatTokens(tokens)}/${formatTokens(windowSize)} tokens, ${turns} turns)`,
1538
+ `Status: ${status}`,
1539
+ ];
1540
+
1541
+ if (state.pruneCount > 0) {
1542
+ parts.push(`| Optimizations: ${state.pruneCount} prune cycles`);
1543
+ }
1544
+
1545
+ // Add trend if we have history
1546
+ if (state.history.length >= 2) {
1547
+ const recent = state.history.slice(-3);
1548
+ const avgGrowth = recent.reduce((sum, h, i) => {
1549
+ if (i === 0) return 0;
1550
+ return sum + (h.pct - recent[i - 1].pct);
1551
+ }, 0) / (recent.length - 1);
1552
+
1553
+ if (avgGrowth > 0) {
1554
+ const turnsUntilFull = Math.ceil((1.0 - percentage) / avgGrowth);
1555
+ parts.push(`| ~${turnsUntilFull} turns until optimization needed`);
1556
+ }
1557
+ }
1558
+
1559
+ return parts.join(' ');
1560
+ }
1561
+
1562
+ /**
1563
+ * Visual progress bar for context usage.
1564
+ */
1565
+ function buildProgressBar(percentage) {
1566
+ const width = 20;
1567
+ const filled = Math.round(percentage * width);
1568
+ const empty = width - filled;
1569
+ const fillChar = percentage >= AUTOPILOT_PRUNE_PCT ? '!' : percentage >= AUTOPILOT_WARN_PCT ? '#' : '=';
1570
+ return `[${fillChar.repeat(filled)}${'-'.repeat(empty)}]`;
1571
+ }
1572
+
1573
+ /**
1574
+ * Format token count for display.
1575
+ */
1576
+ function formatTokens(n) {
1577
+ if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M';
1578
+ if (n >= 1000) return (n / 1000).toFixed(1) + 'K';
1579
+ return String(n);
1580
+ }
1581
+
1582
+ /**
1583
+ * Context Autopilot: run on every UserPromptSubmit.
1584
+ * Returns { additionalContext, shouldBlock } for the hook output.
1585
+ */
1586
+ async function runAutopilot(transcriptPath, sessionId, backend, backendType) {
1587
+ const state = loadAutopilotState();
1588
+
1589
+ // Reset state if session changed
1590
+ if (state.sessionId !== sessionId) {
1591
+ state.sessionId = sessionId;
1592
+ state.lastTokenEstimate = 0;
1593
+ state.lastPercentage = 0;
1594
+ state.pruneCount = 0;
1595
+ state.warningIssued = false;
1596
+ state.history = [];
1597
+ }
1598
+
1599
+ // Estimate current context usage
1600
+ const { tokens, turns, method, lastPreTokens } = estimateContextTokens(transcriptPath);
1601
+ const percentage = Math.min(tokens / CONTEXT_WINDOW_TOKENS, 1.0);
1602
+
1603
+ // Track history (keep last 50 data points)
1604
+ state.history.push({ ts: Date.now(), tokens, pct: percentage, turns });
1605
+ if (state.history.length > 50) state.history.shift();
1606
+
1607
+ state.lastTokenEstimate = tokens;
1608
+ state.lastPercentage = percentage;
1609
+ state.lastCheck = Date.now();
1610
+
1611
+ let optimizationMessage = '';
1612
+
1613
+ // Phase 1: Warning zone (70-85%) — advise concise responses
1614
+ if (percentage >= AUTOPILOT_WARN_PCT && percentage < AUTOPILOT_PRUNE_PCT) {
1615
+ if (!state.warningIssued) {
1616
+ state.warningIssued = true;
1617
+ optimizationMessage = ` | Context at ${(percentage * 100).toFixed(0)}%. Keep responses concise to extend session.`;
1618
+ }
1619
+ }
1620
+
1621
+ // Phase 2: Critical zone (85%+) — session rotation needed
1622
+ if (percentage >= AUTOPILOT_PRUNE_PCT) {
1623
+ state.pruneCount++;
1624
+
1625
+ // Prune stale entries from archive to free up storage
1626
+ if (backend.pruneStale) {
1627
+ try {
1628
+ const pruned = backend.pruneStale(NAMESPACE, Math.min(RETENTION_DAYS, 7));
1629
+ if (pruned > 0) {
1630
+ optimizationMessage += ` | Pruned ${pruned} stale archive entries.`;
1631
+ }
1632
+ } catch { /* non-critical */ }
1633
+ }
1634
+
1635
+ const turnsLeft = Math.max(0, Math.ceil((1.0 - percentage) / 0.03));
1636
+ optimizationMessage += ` | CRITICAL: ${(percentage * 100).toFixed(0)}% context used (~${turnsLeft} turns left). All ${turns} turns archived. Start a new session with /clear — context will be fully restored via SessionStart hook.`;
1637
+ }
1638
+
1639
+ const report = buildAutopilotReport(percentage, tokens, CONTEXT_WINDOW_TOKENS, turns, state);
1640
+ saveAutopilotState(state);
1641
+
1642
+ return {
1643
+ additionalContext: report + optimizationMessage,
1644
+ percentage,
1645
+ tokens,
1646
+ turns,
1647
+ method,
1648
+ state,
1649
+ };
1650
+ }
1651
+
1652
+ // ============================================================================
1653
+ // Commands
1654
+ // ============================================================================
1655
+
1656
+ async function doPreCompact() {
1657
+ const input = await readStdin(200);
1658
+ if (!input) return;
1659
+
1660
+ const { session_id: sessionId, transcript_path: transcriptPath, trigger } = input;
1661
+ if (!transcriptPath || !sessionId) return;
1662
+
1663
+ const messages = parseTranscript(transcriptPath);
1664
+ if (messages.length === 0) return;
1665
+
1666
+ const chunks = chunkTranscript(messages);
1667
+ if (chunks.length === 0) return;
1668
+
1669
+ const { backend, type } = await resolveBackend();
1670
+
1671
+ const archiveResult = await storeChunks(backend, chunks, sessionId, trigger || 'auto');
1672
+
1673
+ // Auto-optimize: prune stale entries + sync to RuVector if available
1674
+ const optimizeResult = await autoOptimize(backend, type);
1675
+
1676
+ const total = await backend.count(NAMESPACE);
1677
+ await backend.shutdown();
1678
+
1679
+ const optParts = [];
1680
+ if (optimizeResult.pruned > 0) optParts.push(`${optimizeResult.pruned} pruned`);
1681
+ if (optimizeResult.decayed > 0) optParts.push(`${optimizeResult.decayed} decayed`);
1682
+ if (optimizeResult.embedded > 0) optParts.push(`${optimizeResult.embedded} embedded`);
1683
+ if (optimizeResult.synced > 0) optParts.push(`${optimizeResult.synced} synced`);
1684
+ const optimizeMsg = optParts.length > 0 ? ` Optimized: ${optParts.join(', ')}.` : '';
1685
+ process.stderr.write(
1686
+ `[ContextPersistence] Archived ${archiveResult.stored} turns (${archiveResult.deduped} deduped) via ${type}. Total: ${total}.${optimizeMsg}\n`
1687
+ );
1688
+
1689
+ // Exit code 0: stdout is appended as custom compact instructions
1690
+ // This guides Claude on what to preserve in the compaction summary
1691
+ const instructions = buildCompactInstructions(chunks, sessionId, archiveResult);
1692
+ process.stdout.write(instructions);
1693
+
1694
+ // Context Autopilot: track state and log archival status
1695
+ // NOTE: Claude Code 2.0.76 executePreCompactHooks uses executeHooksOutsideREPL
1696
+ // which does NOT support exit code 2 blocking. Compaction always proceeds.
1697
+ // Our "infinite context" comes from archive + restore, not blocking.
1698
+ if (AUTOPILOT_ENABLED) {
1699
+ const state = loadAutopilotState();
1700
+ const pct = state.lastPercentage || 0;
1701
+ const bar = buildProgressBar(pct);
1702
+
1703
+ process.stderr.write(
1704
+ `[ContextAutopilot] ${bar} ${(pct * 100).toFixed(1)}% | ${trigger} compact — ${chunks.length} turns archived. Context will be restored after compaction.\n`
1705
+ );
1706
+
1707
+ // Reset autopilot state for post-compaction fresh start
1708
+ state.lastTokenEstimate = 0;
1709
+ state.lastPercentage = 0;
1710
+ state.warningIssued = false;
1711
+ saveAutopilotState(state);
1712
+ }
1713
+ }
1714
+
1715
+ async function doSessionStart() {
1716
+ const input = await readStdin(200);
1717
+
1718
+ // Restore context after compaction OR after /clear (session rotation)
1719
+ // With DISABLE_COMPACT, /clear is the primary way to free context
1720
+ if (!input || (input.source !== 'compact' && input.source !== 'clear')) return;
1721
+
1722
+ const sessionId = input.session_id;
1723
+ if (!sessionId) return;
1724
+
1725
+ const { backend, type } = await resolveBackend();
1726
+
1727
+ // Use smart retrieval (importance-ranked) when auto-optimize is on
1728
+ let additionalContext;
1729
+ if (AUTO_OPTIMIZE) {
1730
+ const { text, accessedIds } = await retrieveContextSmart(backend, sessionId, RESTORE_BUDGET);
1731
+ additionalContext = text;
1732
+
1733
+ // Track which entries were actually restored (access pattern learning)
1734
+ if (accessedIds.length > 0 && backend.markAccessed) {
1735
+ try { backend.markAccessed(accessedIds); } catch { /* non-critical */ }
1736
+ }
1737
+
1738
+ if (accessedIds.length > 0) {
1739
+ process.stderr.write(
1740
+ `[ContextPersistence] Smart restore: ${accessedIds.length} turns (importance-ranked) via ${type}\n`
1741
+ );
1742
+ }
1743
+ } else {
1744
+ additionalContext = await retrieveContext(backend, sessionId, RESTORE_BUDGET);
1745
+ }
1746
+
1747
+ await backend.shutdown();
1748
+
1749
+ if (!additionalContext) return;
1750
+
1751
+ const output = {
1752
+ hookSpecificOutput: {
1753
+ hookEventName: 'SessionStart',
1754
+ additionalContext,
1755
+ },
1756
+ };
1757
+ process.stdout.write(JSON.stringify(output));
1758
+ }
1759
+
1760
+ // ============================================================================
1761
+ // Proactive archiving on every user prompt (prevents context cliff)
1762
+ // ============================================================================
1763
+
1764
+ async function doUserPromptSubmit() {
1765
+ const input = await readStdin(200);
1766
+ if (!input) return;
1767
+
1768
+ const { session_id: sessionId, transcript_path: transcriptPath } = input;
1769
+ if (!transcriptPath || !sessionId) return;
1770
+
1771
+ const messages = parseTranscript(transcriptPath);
1772
+ if (messages.length === 0) return;
1773
+
1774
+ const chunks = chunkTranscript(messages);
1775
+ if (chunks.length === 0) return;
1776
+
1777
+ const { backend, type } = await resolveBackend();
1778
+
1779
+ // Only archive new turns (dedup handles the rest, but we can skip early
1780
+ // by only processing the last N chunks since the previous archive)
1781
+ const existingCount = backend.queryBySession
1782
+ ? (await backend.queryBySession(NAMESPACE, sessionId)).length
1783
+ : 0;
1784
+
1785
+ // Skip if we've already archived most turns (within 2 turns tolerance)
1786
+ const skipArchive = existingCount > 0 && chunks.length - existingCount <= 2;
1787
+
1788
+ let archiveMsg = '';
1789
+ if (!skipArchive) {
1790
+ const result = await storeChunks(backend, chunks, sessionId, 'proactive');
1791
+ if (result.stored > 0) {
1792
+ const total = await backend.count(NAMESPACE);
1793
+ archiveMsg = `[ContextPersistence] Proactively archived ${result.stored} turns (total: ${total}).`;
1794
+ process.stderr.write(
1795
+ `[ContextPersistence] Proactive archive: ${result.stored} new, ${result.deduped} deduped via ${type}. Total: ${total}\n`
1796
+ );
1797
+ }
1798
+ }
1799
+
1800
+ // Context Autopilot: estimate usage and report percentage
1801
+ let autopilotMsg = '';
1802
+ if (AUTOPILOT_ENABLED && transcriptPath) {
1803
+ try {
1804
+ const autopilot = await runAutopilot(transcriptPath, sessionId, backend, type);
1805
+ autopilotMsg = autopilot.additionalContext;
1806
+
1807
+ process.stderr.write(
1808
+ `[ContextAutopilot] ${(autopilot.percentage * 100).toFixed(1)}% context used (~${formatTokens(autopilot.tokens)} tokens, ${autopilot.turns} turns, ${autopilot.method})\n`
1809
+ );
1810
+ } catch (err) {
1811
+ process.stderr.write(`[ContextAutopilot] Error: ${err.message}\n`);
1812
+ }
1813
+ }
1814
+
1815
+ await backend.shutdown();
1816
+
1817
+ // Combine archive message and autopilot report
1818
+ const additionalContext = [archiveMsg, autopilotMsg].filter(Boolean).join(' ');
1819
+
1820
+ if (additionalContext) {
1821
+ const output = {
1822
+ hookSpecificOutput: {
1823
+ hookEventName: 'UserPromptSubmit',
1824
+ additionalContext,
1825
+ },
1826
+ };
1827
+ process.stdout.write(JSON.stringify(output));
1828
+ }
1829
+ }
1830
+
1831
+ async function doStatus() {
1832
+ const { backend, type } = await resolveBackend();
1833
+
1834
+ const total = await backend.count();
1835
+ const archiveCount = await backend.count(NAMESPACE);
1836
+ const namespaces = await backend.listNamespaces();
1837
+ const sessions = await backend.listSessions(NAMESPACE);
1838
+
1839
+ console.log('\n=== Context Persistence Archive Status ===\n');
1840
+ const backendLabel = {
1841
+ sqlite: ARCHIVE_DB_PATH,
1842
+ ruvector: `${process.env.RUVECTOR_HOST || 'N/A'}:${process.env.RUVECTOR_PORT || '5432'}`,
1843
+ agentdb: 'in-memory HNSW',
1844
+ json: ARCHIVE_JSON_PATH,
1845
+ };
1846
+ console.log(` Backend: ${type} (${backendLabel[type] || type})`);
1847
+ console.log(` Total: ${total} entries`);
1848
+ console.log(` Transcripts: ${archiveCount} entries`);
1849
+ console.log(` Namespaces: ${namespaces.join(', ') || 'none'}`);
1850
+ console.log(` Budget: ${RESTORE_BUDGET} chars`);
1851
+ console.log(` Sessions: ${sessions.length}`);
1852
+ console.log(` Proactive: enabled (UserPromptSubmit hook)`);
1853
+ console.log(` Auto-opt: ${AUTO_OPTIMIZE ? 'enabled' : 'disabled'} (importance ranking, pruning, sync)`);
1854
+ console.log(` Retention: ${RETENTION_DAYS} days (prune never-accessed entries)`);
1855
+ const rvConfig = getRuVectorConfig();
1856
+ console.log(` RuVector: ${rvConfig ? `${rvConfig.host}:${rvConfig.port}/${rvConfig.database} (auto-sync enabled)` : 'not configured'}`);
1857
+
1858
+ // Self-learning stats
1859
+ if (type === 'sqlite' && backend.db) {
1860
+ try {
1861
+ const embCount = backend.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries WHERE embedding IS NOT NULL').get().cnt;
1862
+ const avgConf = backend.db.prepare('SELECT AVG(confidence) as avg FROM transcript_entries WHERE namespace = ?').get(NAMESPACE)?.avg || 0;
1863
+ const lowConf = backend.db.prepare('SELECT COUNT(*) as cnt FROM transcript_entries WHERE namespace = ? AND confidence < 0.3').get(NAMESPACE).cnt;
1864
+ console.log('');
1865
+ console.log(' --- Self-Learning ---');
1866
+ console.log(` Embeddings: ${embCount}/${archiveCount} entries have vector embeddings`);
1867
+ console.log(` Avg conf: ${(avgConf * 100).toFixed(1)}% (decay: -0.5%/hr, boost: +3%/access)`);
1868
+ console.log(` Low conf: ${lowConf} entries below 30% (pruned at 15%)`);
1869
+ console.log(` Semantic: ${embCount > 0 ? 'enabled (cross-session search)' : 'pending (embeddings generating)'}`);
1870
+ } catch { /* stats are non-critical */ }
1871
+ }
1872
+
1873
+ // Autopilot status
1874
+ console.log('');
1875
+ console.log(' --- Context Autopilot ---');
1876
+ console.log(` Enabled: ${AUTOPILOT_ENABLED}`);
1877
+ console.log(` Window: ${formatTokens(CONTEXT_WINDOW_TOKENS)} tokens`);
1878
+ console.log(` Warn at: ${(AUTOPILOT_WARN_PCT * 100).toFixed(0)}%`);
1879
+ console.log(` Prune at: ${(AUTOPILOT_PRUNE_PCT * 100).toFixed(0)}%`);
1880
+ console.log(` Compaction: LOSSLESS (archive before, restore after)`);
1881
+
1882
+ const apState = loadAutopilotState();
1883
+ if (apState.sessionId) {
1884
+ const pct = apState.lastPercentage || 0;
1885
+ const bar = buildProgressBar(pct);
1886
+ console.log(` Current: ${bar} ${(pct * 100).toFixed(1)}% (~${formatTokens(apState.lastTokenEstimate)} tokens)`);
1887
+ console.log(` Prune cycles: ${apState.pruneCount}`);
1888
+ if (apState.history.length >= 2) {
1889
+ const first = apState.history[0];
1890
+ const last = apState.history[apState.history.length - 1];
1891
+ const growthRate = (last.pct - first.pct) / apState.history.length;
1892
+ if (growthRate > 0) {
1893
+ const turnsLeft = Math.ceil((1.0 - pct) / growthRate);
1894
+ console.log(` Est. runway: ~${turnsLeft} turns until prune threshold`);
1895
+ }
1896
+ }
1897
+ }
1898
+
1899
+ if (sessions.length > 0) {
1900
+ console.log('\n Recent sessions:');
1901
+ for (const s of sessions.slice(0, 10)) {
1902
+ console.log(` - ${s.session_id}: ${s.cnt} turns`);
1903
+ }
1904
+ }
1905
+
1906
+ console.log('');
1907
+ await backend.shutdown();
1908
+ }
1909
+
1910
+ // ============================================================================
1911
+ // Exports for testing
1912
+ // ============================================================================
1913
+
1914
+ export {
1915
+ SQLiteBackend,
1916
+ RuVectorBackend,
1917
+ JsonFileBackend,
1918
+ resolveBackend,
1919
+ getRuVectorConfig,
1920
+ createEmbedding,
1921
+ createHashEmbedding,
1922
+ getOnnxPipeline,
1923
+ EMBEDDING_DIM,
1924
+ hashContent,
1925
+ parseTranscript,
1926
+ extractTextContent,
1927
+ extractToolCalls,
1928
+ extractFilePaths,
1929
+ chunkTranscript,
1930
+ extractSummary,
1931
+ buildEntry,
1932
+ buildCompactInstructions,
1933
+ computeImportance,
1934
+ retrieveContextSmart,
1935
+ autoOptimize,
1936
+ crossSessionSearch,
1937
+ storeChunks,
1938
+ retrieveContext,
1939
+ readStdin,
1940
+ // Autopilot
1941
+ estimateContextTokens,
1942
+ loadAutopilotState,
1943
+ saveAutopilotState,
1944
+ runAutopilot,
1945
+ buildProgressBar,
1946
+ formatTokens,
1947
+ buildAutopilotReport,
1948
+ NAMESPACE,
1949
+ ARCHIVE_DB_PATH,
1950
+ ARCHIVE_JSON_PATH,
1951
+ COMPACT_INSTRUCTION_BUDGET,
1952
+ RETENTION_DAYS,
1953
+ AUTO_OPTIMIZE,
1954
+ AUTOPILOT_ENABLED,
1955
+ CONTEXT_WINDOW_TOKENS,
1956
+ AUTOPILOT_WARN_PCT,
1957
+ AUTOPILOT_PRUNE_PCT,
1958
+ };
1959
+
1960
+ // ============================================================================
1961
+ // Main
1962
+ // ============================================================================
1963
+
1964
+ const command = process.argv[2] || 'status';
1965
+
1966
+ try {
1967
+ switch (command) {
1968
+ case 'pre-compact': await doPreCompact(); break;
1969
+ case 'session-start': await doSessionStart(); break;
1970
+ case 'user-prompt-submit': await doUserPromptSubmit(); break;
1971
+ case 'status': await doStatus(); break;
1972
+ default:
1973
+ console.log('Usage: context-persistence-hook.mjs <pre-compact|session-start|user-prompt-submit|status>');
1974
+ process.exit(1);
1975
+ }
1976
+ } catch (err) {
1977
+ // Hooks must never crash Claude Code - fail silently
1978
+ process.stderr.write(`[ContextPersistence] Error (non-critical): ${err.message}\n`);
1979
+ }