mixdog 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (404) hide show
  1. package/.claude-plugin/marketplace.json +31 -0
  2. package/.claude-plugin/plugin.json +20 -0
  3. package/.gitattributes +34 -0
  4. package/.mcp.json +14 -0
  5. package/ARCHITECTURE.md +77 -0
  6. package/CHANGELOG.md +7 -0
  7. package/CONTRIBUTING.md +45 -0
  8. package/DATA-FLOW.md +79 -0
  9. package/LICENSE +21 -0
  10. package/README.md +389 -0
  11. package/SECURITY.md +138 -0
  12. package/UNINSTALL.md +112 -0
  13. package/agents/maintenance.md +5 -0
  14. package/agents/memory-classification.md +30 -0
  15. package/agents/scheduler-task.md +18 -0
  16. package/agents/webhook-handler.md +27 -0
  17. package/agents/worker.md +24 -0
  18. package/bin/bridge +133 -0
  19. package/bin/statusline-launcher.mjs +78 -0
  20. package/bin/statusline-lib.mjs +550 -0
  21. package/bin/statusline.mjs +607 -0
  22. package/bun.lock +802 -0
  23. package/commands/config.md +16 -0
  24. package/commands/doctor.md +13 -0
  25. package/commands/setup.md +17 -0
  26. package/defaults/cycle3-review-prompt.md +90 -0
  27. package/defaults/hidden-roles.json +65 -0
  28. package/defaults/memory-chunk-prompt.md +63 -0
  29. package/defaults/memory-promote-prompt.md +135 -0
  30. package/defaults/mixdog-config.template.json +27 -0
  31. package/defaults/user-workflow.json +8 -0
  32. package/defaults/user-workflow.md +12 -0
  33. package/hooks/hooks.json +73 -0
  34. package/hooks/lib/active-instance.cjs +77 -0
  35. package/hooks/lib/permission-evaluator.cjs +411 -0
  36. package/hooks/lib/permission-route.cjs +63 -0
  37. package/hooks/lib/permission-rules.cjs +170 -0
  38. package/hooks/lib/settings-loader.cjs +116 -0
  39. package/hooks/post-tool-use.cjs +84 -0
  40. package/hooks/pre-mcp-sandbox.cjs +158 -0
  41. package/hooks/pre-tool-subagent.cjs +253 -0
  42. package/hooks/session-start.cjs +1372 -0
  43. package/hooks/turn-timer.cjs +82 -0
  44. package/lib/claude-md-writer.cjs +386 -0
  45. package/lib/config-cjs.cjs +61 -0
  46. package/lib/hook-pipe-path.cjs +10 -0
  47. package/lib/keychain-cjs.cjs +263 -0
  48. package/lib/plugin-paths.cjs +61 -0
  49. package/lib/rules-builder.cjs +241 -0
  50. package/lib/text-utils.cjs +61 -0
  51. package/native/README.md +117 -0
  52. package/native/prebuilt/linux-aarch64/mixdog-shim +0 -0
  53. package/native/prebuilt/linux-x86_64/mixdog-shim +0 -0
  54. package/native/prebuilt/macos-aarch64/mixdog-shim +0 -0
  55. package/native/prebuilt/macos-x86_64/mixdog-shim +0 -0
  56. package/native/prebuilt/windows-x86_64/mixdog-shim.exe +0 -0
  57. package/package.json +107 -0
  58. package/prompts/code-review.txt +16 -0
  59. package/prompts/security-audit.txt +17 -0
  60. package/rules/bridge/00-common.md +39 -0
  61. package/rules/bridge/20-skip-protocol.md +18 -0
  62. package/rules/bridge/30-explorer.md +33 -0
  63. package/rules/bridge/40-cycle1-agent.md +52 -0
  64. package/rules/bridge/41-cycle2-agent.md +62 -0
  65. package/rules/bridge/42-cycle3-agent.md +44 -0
  66. package/rules/lead/00-tool-lead.md +61 -0
  67. package/rules/lead/01-general.md +23 -0
  68. package/rules/lead/02-channels.md +49 -0
  69. package/rules/lead/03-team.md +27 -0
  70. package/rules/lead/04-workflow.md +20 -0
  71. package/rules/shared/00-language.md +14 -0
  72. package/rules/shared/01-tool.md +138 -0
  73. package/scripts/bootstrap.mjs +184 -0
  74. package/scripts/bridge-unify-smoke.mjs +308 -0
  75. package/scripts/build-runtime-linux.sh +348 -0
  76. package/scripts/build-runtime-macos.sh +217 -0
  77. package/scripts/build-runtime-windows.ps1 +242 -0
  78. package/scripts/builtin-utils-smoke.mjs +392 -0
  79. package/scripts/check-json.mjs +45 -0
  80. package/scripts/check-syntax-changed.mjs +102 -0
  81. package/scripts/check-syntax.mjs +58 -0
  82. package/scripts/code-graph-batch.test.mjs +33 -0
  83. package/scripts/config-preserve-smoke.mjs +180 -0
  84. package/scripts/doctor.mjs +484 -0
  85. package/scripts/edit-normalize-fuzz.mjs +130 -0
  86. package/scripts/edit-normalize-smoke.mjs +401 -0
  87. package/scripts/edit-operation-smoke.mjs +369 -0
  88. package/scripts/edit2-smoke.mjs +63 -0
  89. package/scripts/fuzzy-e2e.mjs +28 -0
  90. package/scripts/fuzzy-smoke.mjs +26 -0
  91. package/scripts/generate-runtime-manifest.mjs +166 -0
  92. package/scripts/guard-smoke.mjs +66 -0
  93. package/scripts/hidden-role-schema-smoke.mjs +162 -0
  94. package/scripts/hook-routing-smoke.mjs +29 -0
  95. package/scripts/inject-input.ps1 +204 -0
  96. package/scripts/io-complex-smoke.mjs +667 -0
  97. package/scripts/io-explore-bench.mjs +424 -0
  98. package/scripts/io-guardrails-smoke.mjs +205 -0
  99. package/scripts/io-mini-bench-baseline.json +11 -0
  100. package/scripts/io-mini-bench.mjs +216 -0
  101. package/scripts/io-route-harness.mjs +933 -0
  102. package/scripts/io-telemetry-report.mjs +691 -0
  103. package/scripts/mutation-bench.mjs +564 -0
  104. package/scripts/mutation-io-smoke.mjs +1081 -0
  105. package/scripts/native-patch-bridge-smoke.mjs +288 -0
  106. package/scripts/native-patch-smoke.mjs +304 -0
  107. package/scripts/patch-interior-context-smoke.mjs +49 -0
  108. package/scripts/patch-newline-utf8-smoke.mjs +157 -0
  109. package/scripts/perf-hook-smoke.mjs +71 -0
  110. package/scripts/permission-eval-smoke.mjs +426 -0
  111. package/scripts/prep-patch.mjs +53 -0
  112. package/scripts/prep-shim.mjs +96 -0
  113. package/scripts/provider-cache-smoke.mjs +687 -0
  114. package/scripts/report-runtime-health.mjs +132 -0
  115. package/scripts/run-mcp.mjs +1547 -0
  116. package/scripts/salvage-v4a-shatter.test.mjs +58 -0
  117. package/scripts/scoped-cache-io-smoke.mjs +103 -0
  118. package/scripts/shell-policy-round3-smoke.mjs +46 -0
  119. package/scripts/smoke-runtime-negative.ps1 +100 -0
  120. package/scripts/smoke-runtime-negative.sh +95 -0
  121. package/scripts/stall-policy-smoke.mjs +50 -0
  122. package/scripts/start-memory-worker.mjs +23 -0
  123. package/scripts/statusline-launcher-smoke.mjs +82 -0
  124. package/scripts/stress-atomic-write.mjs +1028 -0
  125. package/scripts/test-config-rmw-restore.mjs +122 -0
  126. package/scripts/test-fault-inject.mjs +164 -0
  127. package/scripts/test-large-file.mjs +174 -0
  128. package/scripts/tool-edge-smoke.mjs +209 -0
  129. package/scripts/uninstall.mjs +201 -0
  130. package/scripts/webhook-selfheal-smoke.mjs +29 -0
  131. package/scripts/write-overwrite-guard-smoke.mjs +56 -0
  132. package/server-main.mjs +3055 -0
  133. package/server.mjs +468 -0
  134. package/setup/config-merge.mjs +254 -0
  135. package/setup/install.mjs +120 -0
  136. package/setup/launch-core.mjs +507 -0
  137. package/setup/launch.mjs +101 -0
  138. package/setup/setup-server.mjs +3206 -0
  139. package/setup/setup.html +3693 -0
  140. package/skills/retro-skill-proposer/SKILL.md +92 -0
  141. package/skills/schedule-add/SKILL.md +77 -0
  142. package/skills/setup/SKILL.md +346 -0
  143. package/skills/webhook-add/SKILL.md +81 -0
  144. package/src/agent/bridge-stall-watchdog.mjs +337 -0
  145. package/src/agent/index.mjs +2138 -0
  146. package/src/agent/orchestrator/activity-bus.mjs +38 -0
  147. package/src/agent/orchestrator/ai-wrapped-dispatch.mjs +1010 -0
  148. package/src/agent/orchestrator/bridge-retry.mjs +220 -0
  149. package/src/agent/orchestrator/bridge-trace.mjs +583 -0
  150. package/src/agent/orchestrator/cache-mtime.mjs +58 -0
  151. package/src/agent/orchestrator/config.mjs +358 -0
  152. package/src/agent/orchestrator/context/collect.mjs +651 -0
  153. package/src/agent/orchestrator/dispatch-persist.mjs +549 -0
  154. package/src/agent/orchestrator/drain-registry.mjs +50 -0
  155. package/src/agent/orchestrator/explore-validator.mjs +8 -0
  156. package/src/agent/orchestrator/internal-roles.mjs +118 -0
  157. package/src/agent/orchestrator/internal-tools.mjs +88 -0
  158. package/src/agent/orchestrator/jobs.mjs +116 -0
  159. package/src/agent/orchestrator/mcp/client.mjs +364 -0
  160. package/src/agent/orchestrator/providers/anthropic-betas.mjs +21 -0
  161. package/src/agent/orchestrator/providers/anthropic-oauth.mjs +1745 -0
  162. package/src/agent/orchestrator/providers/anthropic.mjs +437 -0
  163. package/src/agent/orchestrator/providers/gemini.mjs +1175 -0
  164. package/src/agent/orchestrator/providers/grok-oauth.mjs +782 -0
  165. package/src/agent/orchestrator/providers/model-catalog.mjs +241 -0
  166. package/src/agent/orchestrator/providers/openai-compat.mjs +1467 -0
  167. package/src/agent/orchestrator/providers/openai-oauth-ws.mjs +1890 -0
  168. package/src/agent/orchestrator/providers/openai-oauth.mjs +1307 -0
  169. package/src/agent/orchestrator/providers/openai-ws.mjs +104 -0
  170. package/src/agent/orchestrator/providers/registry.mjs +192 -0
  171. package/src/agent/orchestrator/providers/retry-classifier.mjs +325 -0
  172. package/src/agent/orchestrator/session/abort-lookup.mjs +13 -0
  173. package/src/agent/orchestrator/session/cache/post-edit-marks.mjs +42 -0
  174. package/src/agent/orchestrator/session/cache/prefetch-cache.mjs +142 -0
  175. package/src/agent/orchestrator/session/cache/read-cache.mjs +319 -0
  176. package/src/agent/orchestrator/session/cache/scoped-cache-outcome.mjs +11 -0
  177. package/src/agent/orchestrator/session/cache/scoped-cache.mjs +361 -0
  178. package/src/agent/orchestrator/session/cache/util.mjs +49 -0
  179. package/src/agent/orchestrator/session/loop.mjs +1478 -0
  180. package/src/agent/orchestrator/session/manager.mjs +1975 -0
  181. package/src/agent/orchestrator/session/read-dedup.mjs +6 -0
  182. package/src/agent/orchestrator/session/result-classification.mjs +65 -0
  183. package/src/agent/orchestrator/session/save-session-worker.mjs +18 -0
  184. package/src/agent/orchestrator/session/store.mjs +624 -0
  185. package/src/agent/orchestrator/session/stream-watchdog.mjs +130 -0
  186. package/src/agent/orchestrator/session/tool-result-offload.mjs +166 -0
  187. package/src/agent/orchestrator/session/trim.mjs +491 -0
  188. package/src/agent/orchestrator/smart-bridge/CACHE-SHARD.md +115 -0
  189. package/src/agent/orchestrator/smart-bridge/bridge-llm.mjs +327 -0
  190. package/src/agent/orchestrator/smart-bridge/cache-obs.mjs +150 -0
  191. package/src/agent/orchestrator/smart-bridge/cache-strategy.mjs +228 -0
  192. package/src/agent/orchestrator/smart-bridge/index.mjs +215 -0
  193. package/src/agent/orchestrator/smart-bridge/profiles.mjs +37 -0
  194. package/src/agent/orchestrator/smart-bridge/registry.mjs +348 -0
  195. package/src/agent/orchestrator/smart-bridge/session-builder.mjs +116 -0
  196. package/src/agent/orchestrator/stall-policy.mjs +195 -0
  197. package/src/agent/orchestrator/tool-loop-guard.mjs +75 -0
  198. package/src/agent/orchestrator/tools/bash-policy-scan.mjs +77 -0
  199. package/src/agent/orchestrator/tools/bash-session.mjs +721 -0
  200. package/src/agent/orchestrator/tools/builtin/advisory-lock.mjs +171 -0
  201. package/src/agent/orchestrator/tools/builtin/arg-guard.mjs +455 -0
  202. package/src/agent/orchestrator/tools/builtin/atomic-write.mjs +236 -0
  203. package/src/agent/orchestrator/tools/builtin/bash-tool.mjs +480 -0
  204. package/src/agent/orchestrator/tools/builtin/binary-file.mjs +76 -0
  205. package/src/agent/orchestrator/tools/builtin/builtin-tools.mjs +256 -0
  206. package/src/agent/orchestrator/tools/builtin/cache-layers.mjs +386 -0
  207. package/src/agent/orchestrator/tools/builtin/cwd-utils.mjs +37 -0
  208. package/src/agent/orchestrator/tools/builtin/device-paths.mjs +154 -0
  209. package/src/agent/orchestrator/tools/builtin/diagnostics-tool.mjs +292 -0
  210. package/src/agent/orchestrator/tools/builtin/diff-utils.mjs +109 -0
  211. package/src/agent/orchestrator/tools/builtin/edit-base-guard.mjs +58 -0
  212. package/src/agent/orchestrator/tools/builtin/edit-byte-plan.mjs +240 -0
  213. package/src/agent/orchestrator/tools/builtin/edit-byte-utils.mjs +113 -0
  214. package/src/agent/orchestrator/tools/builtin/edit-commit.mjs +74 -0
  215. package/src/agent/orchestrator/tools/builtin/edit-context-utils.mjs +242 -0
  216. package/src/agent/orchestrator/tools/builtin/edit-diagnostics.mjs +211 -0
  217. package/src/agent/orchestrator/tools/builtin/edit-engine.mjs +1364 -0
  218. package/src/agent/orchestrator/tools/builtin/edit-failure-context.mjs +126 -0
  219. package/src/agent/orchestrator/tools/builtin/edit-hint.mjs +141 -0
  220. package/src/agent/orchestrator/tools/builtin/edit-match-utils.mjs +194 -0
  221. package/src/agent/orchestrator/tools/builtin/edit-partial-write.mjs +60 -0
  222. package/src/agent/orchestrator/tools/builtin/edit-stale-refresh.mjs +168 -0
  223. package/src/agent/orchestrator/tools/builtin/edit-tool.mjs +173 -0
  224. package/src/agent/orchestrator/tools/builtin/edit-utf8-guard.mjs +48 -0
  225. package/src/agent/orchestrator/tools/builtin/fs-reachability.mjs +48 -0
  226. package/src/agent/orchestrator/tools/builtin/fuzzy-match.mjs +99 -0
  227. package/src/agent/orchestrator/tools/builtin/glob-walk.mjs +170 -0
  228. package/src/agent/orchestrator/tools/builtin/grep-formatting.mjs +113 -0
  229. package/src/agent/orchestrator/tools/builtin/hash-utils.mjs +6 -0
  230. package/src/agent/orchestrator/tools/builtin/list-formatting.mjs +7 -0
  231. package/src/agent/orchestrator/tools/builtin/list-tool.mjs +593 -0
  232. package/src/agent/orchestrator/tools/builtin/native-edit-runner.mjs +89 -0
  233. package/src/agent/orchestrator/tools/builtin/notebook-edit-tool.mjs +300 -0
  234. package/src/agent/orchestrator/tools/builtin/open-config-tool.mjs +26 -0
  235. package/src/agent/orchestrator/tools/builtin/path-diagnostics.mjs +152 -0
  236. package/src/agent/orchestrator/tools/builtin/path-locks.mjs +35 -0
  237. package/src/agent/orchestrator/tools/builtin/path-utils.mjs +201 -0
  238. package/src/agent/orchestrator/tools/builtin/read-args.mjs +103 -0
  239. package/src/agent/orchestrator/tools/builtin/read-batch.mjs +172 -0
  240. package/src/agent/orchestrator/tools/builtin/read-constants.mjs +40 -0
  241. package/src/agent/orchestrator/tools/builtin/read-formatting.mjs +118 -0
  242. package/src/agent/orchestrator/tools/builtin/read-image-resize.mjs +189 -0
  243. package/src/agent/orchestrator/tools/builtin/read-image.mjs +88 -0
  244. package/src/agent/orchestrator/tools/builtin/read-lines.mjs +12 -0
  245. package/src/agent/orchestrator/tools/builtin/read-mode-tool.mjs +455 -0
  246. package/src/agent/orchestrator/tools/builtin/read-open.mjs +190 -0
  247. package/src/agent/orchestrator/tools/builtin/read-range-index.mjs +271 -0
  248. package/src/agent/orchestrator/tools/builtin/read-ranges.mjs +26 -0
  249. package/src/agent/orchestrator/tools/builtin/read-single-tool.mjs +728 -0
  250. package/src/agent/orchestrator/tools/builtin/read-snapshot-runtime.mjs +173 -0
  251. package/src/agent/orchestrator/tools/builtin/read-special-files.mjs +268 -0
  252. package/src/agent/orchestrator/tools/builtin/read-streaming.mjs +602 -0
  253. package/src/agent/orchestrator/tools/builtin/read-tool.mjs +530 -0
  254. package/src/agent/orchestrator/tools/builtin/read-windows.mjs +107 -0
  255. package/src/agent/orchestrator/tools/builtin/rename-tool.mjs +196 -0
  256. package/src/agent/orchestrator/tools/builtin/rg-runner.mjs +422 -0
  257. package/src/agent/orchestrator/tools/builtin/search-builders.mjs +158 -0
  258. package/src/agent/orchestrator/tools/builtin/search-tool.mjs +869 -0
  259. package/src/agent/orchestrator/tools/builtin/shell-analysis.mjs +653 -0
  260. package/src/agent/orchestrator/tools/builtin/shell-jobs.mjs +936 -0
  261. package/src/agent/orchestrator/tools/builtin/shell-output.mjs +36 -0
  262. package/src/agent/orchestrator/tools/builtin/shell-runtime.mjs +214 -0
  263. package/src/agent/orchestrator/tools/builtin/snapshot-helpers.mjs +143 -0
  264. package/src/agent/orchestrator/tools/builtin/snapshot-store.mjs +206 -0
  265. package/src/agent/orchestrator/tools/builtin/snapshot-validation.mjs +98 -0
  266. package/src/agent/orchestrator/tools/builtin/text-stats.mjs +69 -0
  267. package/src/agent/orchestrator/tools/builtin/windows-roots.mjs +23 -0
  268. package/src/agent/orchestrator/tools/builtin/write-tool.mjs +401 -0
  269. package/src/agent/orchestrator/tools/builtin.mjs +500 -0
  270. package/src/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +39 -0
  271. package/src/agent/orchestrator/tools/code-graph-tool-defs.mjs +24 -0
  272. package/src/agent/orchestrator/tools/code-graph.mjs +4095 -0
  273. package/src/agent/orchestrator/tools/cwd-tool.mjs +298 -0
  274. package/src/agent/orchestrator/tools/destructive-warning.mjs +323 -0
  275. package/src/agent/orchestrator/tools/edit-normalize.mjs +603 -0
  276. package/src/agent/orchestrator/tools/env-scrub.mjs +100 -0
  277. package/src/agent/orchestrator/tools/graph-binary-fetcher.mjs +144 -0
  278. package/src/agent/orchestrator/tools/graph-manifest.json +26 -0
  279. package/src/agent/orchestrator/tools/host-input.mjs +204 -0
  280. package/src/agent/orchestrator/tools/mutation-content-cache.mjs +67 -0
  281. package/src/agent/orchestrator/tools/mutation-planner.mjs +75 -0
  282. package/src/agent/orchestrator/tools/next-call-utils.mjs +48 -0
  283. package/src/agent/orchestrator/tools/patch-binary-fetcher.mjs +133 -0
  284. package/src/agent/orchestrator/tools/patch-manifest.json +26 -0
  285. package/src/agent/orchestrator/tools/patch-tool-defs.mjs +20 -0
  286. package/src/agent/orchestrator/tools/patch.mjs +2754 -0
  287. package/src/agent/orchestrator/tools/progress-message.mjs +118 -0
  288. package/src/agent/orchestrator/tools/result-compression.mjs +279 -0
  289. package/src/agent/orchestrator/tools/shell-command.mjs +865 -0
  290. package/src/agent/orchestrator/tools/shell-exec-policy.mjs +89 -0
  291. package/src/agent/orchestrator/tools/shell-policy-danger-target.mjs +27 -0
  292. package/src/agent/orchestrator/tools/shell-policy-imports.mjs +7 -0
  293. package/src/agent/orchestrator/tools/shell-policy.mjs +345 -0
  294. package/src/agent/orchestrator/tools/shell-snapshot.mjs +313 -0
  295. package/src/agent/orchestrator/workflow-store.mjs +93 -0
  296. package/src/agent/tool-defs.mjs +103 -0
  297. package/src/channels/backends/discord.mjs +784 -0
  298. package/src/channels/data/voice-runtime-manifest.json +138 -0
  299. package/src/channels/index.mjs +3229 -0
  300. package/src/channels/lib/cli-worker-host.mjs +12 -0
  301. package/src/channels/lib/config-lock.mjs +13 -0
  302. package/src/channels/lib/config.mjs +292 -0
  303. package/src/channels/lib/drop-trace.mjs +71 -0
  304. package/src/channels/lib/event-pipeline.mjs +81 -0
  305. package/src/channels/lib/event-queue.mjs +345 -0
  306. package/src/channels/lib/executor.mjs +168 -0
  307. package/src/channels/lib/format.mjs +188 -0
  308. package/src/channels/lib/holidays.mjs +138 -0
  309. package/src/channels/lib/hook-pipe-server.mjs +802 -0
  310. package/src/channels/lib/interaction-workflows.mjs +184 -0
  311. package/src/channels/lib/memory-client.mjs +149 -0
  312. package/src/channels/lib/output-forwarder.mjs +765 -0
  313. package/src/channels/lib/runtime-paths.mjs +479 -0
  314. package/src/channels/lib/scheduler.mjs +723 -0
  315. package/src/channels/lib/session-control.mjs +36 -0
  316. package/src/channels/lib/session-discovery.mjs +103 -0
  317. package/src/channels/lib/settings.mjs +11 -0
  318. package/src/channels/lib/state-file.mjs +68 -0
  319. package/src/channels/lib/status-snapshot.mjs +219 -0
  320. package/src/channels/lib/tool-format.mjs +140 -0
  321. package/src/channels/lib/transcript-discovery.mjs +195 -0
  322. package/src/channels/lib/voice-runtime-fetcher.mjs +734 -0
  323. package/src/channels/lib/webhook.mjs +1179 -0
  324. package/src/channels/lib/whisper-server.mjs +477 -0
  325. package/src/channels/tool-defs.mjs +170 -0
  326. package/src/daemon/host.mjs +118 -0
  327. package/src/daemon/mcp-transport.mjs +47 -0
  328. package/src/daemon/session.mjs +100 -0
  329. package/src/daemon/thin-client.mjs +71 -0
  330. package/src/daemon/transport.mjs +163 -0
  331. package/src/memory/data/runtime-manifest.json +40 -0
  332. package/src/memory/index.mjs +3305 -0
  333. package/src/memory/lib/agent-ipc.mjs +93 -0
  334. package/src/memory/lib/bridge-trace-queries.mjs +120 -0
  335. package/src/memory/lib/core-memory-store.mjs +330 -0
  336. package/src/memory/lib/embedding-provider.mjs +269 -0
  337. package/src/memory/lib/embedding-worker.mjs +323 -0
  338. package/src/memory/lib/llm-worker-host.mjs +17 -0
  339. package/src/memory/lib/memory-cycle.mjs +11 -0
  340. package/src/memory/lib/memory-cycle1.mjs +641 -0
  341. package/src/memory/lib/memory-cycle2.mjs +1284 -0
  342. package/src/memory/lib/memory-cycle3.mjs +540 -0
  343. package/src/memory/lib/memory-embed.mjs +299 -0
  344. package/src/memory/lib/memory-extraction.mjs +5 -0
  345. package/src/memory/lib/memory-maintenance-store.mjs +32 -0
  346. package/src/memory/lib/memory-ops-policy.mjs +190 -0
  347. package/src/memory/lib/memory-recall-id-patch.mjs +15 -0
  348. package/src/memory/lib/memory-recall-read-query.mjs +7 -0
  349. package/src/memory/lib/memory-recall-scope-filter.mjs +63 -0
  350. package/src/memory/lib/memory-recall-store.mjs +621 -0
  351. package/src/memory/lib/memory-retrievers.mjs +112 -0
  352. package/src/memory/lib/memory-score.mjs +71 -0
  353. package/src/memory/lib/memory-text-utils.mjs +58 -0
  354. package/src/memory/lib/memory.mjs +412 -0
  355. package/src/memory/lib/model-profile.mjs +85 -0
  356. package/src/memory/lib/pg/adapter.mjs +308 -0
  357. package/src/memory/lib/pg/process.mjs +360 -0
  358. package/src/memory/lib/pg/supervisor.mjs +396 -0
  359. package/src/memory/lib/project-id-resolver.mjs +86 -0
  360. package/src/memory/lib/runtime-fetcher.mjs +442 -0
  361. package/src/memory/lib/trace-store.mjs +728 -0
  362. package/src/memory/tool-defs.mjs +79 -0
  363. package/src/search/index.mjs +1173 -0
  364. package/src/search/lib/backends/anthropic-oauth.mjs +98 -0
  365. package/src/search/lib/backends/exa.mjs +50 -0
  366. package/src/search/lib/backends/firecrawl.mjs +61 -0
  367. package/src/search/lib/backends/gemini-api.mjs +83 -0
  368. package/src/search/lib/backends/grok-oauth.mjs +86 -0
  369. package/src/search/lib/backends/index.mjs +150 -0
  370. package/src/search/lib/backends/openai-api.mjs +144 -0
  371. package/src/search/lib/backends/openai-oauth.mjs +98 -0
  372. package/src/search/lib/backends/openai-web-search.mjs +76 -0
  373. package/src/search/lib/backends/tavily.mjs +55 -0
  374. package/src/search/lib/backends/xai-api.mjs +113 -0
  375. package/src/search/lib/cache.mjs +131 -0
  376. package/src/search/lib/config.mjs +192 -0
  377. package/src/search/lib/formatter.mjs +115 -0
  378. package/src/search/lib/provider-usage.mjs +67 -0
  379. package/src/search/lib/providers.mjs +47 -0
  380. package/src/search/lib/search-intent.mjs +109 -0
  381. package/src/search/lib/setup-handler.mjs +261 -0
  382. package/src/search/lib/state.mjs +201 -0
  383. package/src/search/lib/web-tools.mjs +1207 -0
  384. package/src/search/tool-defs.mjs +83 -0
  385. package/src/setup/defender-exclusion.mjs +183 -0
  386. package/src/shared/abort-controller.mjs +15 -0
  387. package/src/shared/atomic-file.mjs +420 -0
  388. package/src/shared/config.mjs +350 -0
  389. package/src/shared/daemon-recycle.mjs +108 -0
  390. package/src/shared/disable-claude-builtins.mjs +88 -0
  391. package/src/shared/err-text.mjs +12 -0
  392. package/src/shared/llm/cost.mjs +66 -0
  393. package/src/shared/llm/http-agent.mjs +123 -0
  394. package/src/shared/llm/index.mjs +41 -0
  395. package/src/shared/llm/pid-cleanup.mjs +27 -0
  396. package/src/shared/llm/usage-log.mjs +47 -0
  397. package/src/shared/plugin-paths.mjs +58 -0
  398. package/src/shared/schedules-store.mjs +70 -0
  399. package/src/shared/seed.mjs +119 -0
  400. package/src/shared/user-cwd.mjs +213 -0
  401. package/src/shared/user-data-guard.mjs +238 -0
  402. package/src/status/aggregator.mjs +584 -0
  403. package/src/status/server.mjs +413 -0
  404. package/tools.json +1653 -0
@@ -0,0 +1,641 @@
1
+ import { cleanMemoryText } from './memory.mjs'
2
+ import { resolveMaintenancePreset } from '../../shared/llm/index.mjs'
3
+ import { callBridgeLlm } from './agent-ipc.mjs'
4
+ import {
5
+ flushEmbeddingDirty, inferChunkProjectId,
6
+ } from './memory-embed.mjs'
7
+
8
+ const VALID_CATEGORIES = new Set([
9
+ 'rule', 'constraint', 'decision', 'fact', 'goal', 'preference', 'task', 'issue',
10
+ ])
11
+ const CYCLE1_OMITTED_RETRY_LIMIT = 2
12
+ const CYCLE1_OMITTED_COOLDOWN_MS = 60 * 60 * 1000
13
+
14
+ // Structural validation only. Conceptual omission belongs to the LLM; code
15
+ // should not encode language-specific phrases such as acknowledgements.
16
+ function _isStructurallyInvalidSummary(text) {
17
+ if (!text || typeof text !== 'string') return true
18
+ const t = text.trim()
19
+ if (!t) return true
20
+ if (!/[\p{L}\p{N}]/u.test(t)) return true
21
+ return false
22
+ }
23
+
24
+ function _isStructurallyUnchunkableInput(row) {
25
+ const raw = cleanMemoryText(String(row?.content ?? '')).trim()
26
+ if (!raw) return true
27
+ return !/[\p{L}\p{N}]/u.test(raw)
28
+ }
29
+
30
+ async function markTerminalRows(db, rowIds, label = 'terminal') {
31
+ const ids = [...new Set((Array.isArray(rowIds) ? rowIds : [])
32
+ .map(id => Number(id))
33
+ .filter(id => Number.isFinite(id) && id > 0))]
34
+ if (ids.length === 0) return { attempted: 0, marked: 0, failed: 0 }
35
+ try {
36
+ const result = await db.query(
37
+ `UPDATE entries
38
+ SET chunk_root = id,
39
+ is_root = 0,
40
+ status = 'archived',
41
+ reviewed_at = COALESCE(reviewed_at, $2)
42
+ WHERE id = ANY($1::bigint[])
43
+ AND chunk_root IS NULL
44
+ AND is_root = 0`,
45
+ [ids, Date.now()],
46
+ )
47
+ const marked = Number(result?.rowCount ?? 0)
48
+ return { attempted: ids.length, marked, failed: Math.max(0, ids.length - marked) }
49
+ } catch (err) {
50
+ process.stderr.write(`[cycle1] ${label} sentinel update failed: ${err.message}\n`)
51
+ return { attempted: ids.length, marked: 0, failed: ids.length }
52
+ }
53
+ }
54
+
55
+ async function markOmittedRows(db, rowIds) {
56
+ const ids = [...new Set((Array.isArray(rowIds) ? rowIds : [])
57
+ .map(id => Number(id))
58
+ .filter(id => Number.isFinite(id) && id > 0))]
59
+ if (ids.length === 0) return { attempted: 0, deferred: 0, marked: 0, failed: 0 }
60
+ try {
61
+ const result = await db.query(
62
+ `WITH candidates AS (
63
+ SELECT id, COALESCE(error_count, 0) + 1 AS next_error_count
64
+ FROM entries
65
+ WHERE id = ANY($1::bigint[])
66
+ AND chunk_root IS NULL
67
+ AND is_root = 0
68
+ )
69
+ UPDATE entries e
70
+ SET reviewed_at = $2,
71
+ error_count = c.next_error_count,
72
+ chunk_root = CASE WHEN c.next_error_count >= $3 THEN e.id ELSE e.chunk_root END,
73
+ status = CASE WHEN c.next_error_count >= $3 THEN 'archived'::entry_status ELSE e.status END
74
+ FROM candidates c
75
+ WHERE e.id = c.id
76
+ RETURNING e.id, (c.next_error_count >= $3) AS marked_terminal`,
77
+ [ids, Date.now(), CYCLE1_OMITTED_RETRY_LIMIT],
78
+ )
79
+ const rows = Array.isArray(result?.rows) ? result.rows : []
80
+ const marked = rows.filter(r => r.marked_terminal === true).length
81
+ const deferred = rows.length - marked
82
+ return { attempted: ids.length, deferred, marked, failed: Math.max(0, ids.length - rows.length) }
83
+ } catch (err) {
84
+ process.stderr.write(`[cycle1] omitted retry update failed: ${err.message}\n`)
85
+ return { attempted: ids.length, deferred: 0, marked: 0, failed: ids.length }
86
+ }
87
+ }
88
+
89
+ function selectRootId(members) {
90
+ let rootId = null
91
+ let rootTs = null
92
+ for (const m of members) {
93
+ const ts = Number(m.ts)
94
+ const id = Number(m.id)
95
+ if (!Number.isFinite(ts) || !Number.isFinite(id)) continue
96
+ if (rootId === null || ts < rootTs || (ts === rootTs && id < rootId)) {
97
+ rootId = id
98
+ rootTs = ts
99
+ }
100
+ }
101
+ return rootId
102
+ }
103
+
104
+ export function buildEntriesText(entries) {
105
+ // @N is a 1-based prompt-local index; cycle1-agent answers with @N indexes.
106
+ return entries.map((e, i) => {
107
+ const content = cleanMemoryText(String(e.content ?? '')).slice(0, 400)
108
+ const sess = e.session_id ? String(e.session_id).slice(0, 8) : 'null----'
109
+ return `@${i + 1} ts:${e.ts} role:${e.role} [sess:${sess}] content:${content}`
110
+ }).join('\n')
111
+ }
112
+
113
+ // Balanced quality rules: the model decides conceptual memory value; code only
114
+ // validates line grammar and membership.
115
+ const DEFAULT_CYCLE1_RULES = [
116
+ `Chunk these entries. Emit one chunk per line, NO JSON, NO tool calls, NO prose.`,
117
+ `Format: idx_csv|element|category|summary.`,
118
+ `Every substantive @N should appear in exactly one chunk; omit entries with no standalone memory value.`,
119
+ `Group by coherent topic, keep cause and resolution together, and never merge across [sess:] markers.`,
120
+ `Category must be one of rule / constraint / decision / fact / goal / preference / task / issue; choose the one that best preserves future recall intent.`,
121
+ `Keep summary compact and source-grounded; preserve decisive identifiers, constraints, causes, and outcomes when present.`,
122
+ `First character of your response must be a digit. Use bare @N indexes without @ in output.`,
123
+ ]
124
+
125
+ export function buildCycle1ChunkPrompt(rows, customRules = null) {
126
+ const rules = Array.isArray(customRules) && customRules.length > 0
127
+ ? customRules
128
+ : DEFAULT_CYCLE1_RULES
129
+ return [...rules, '', buildEntriesText(rows)].join('\n')
130
+ }
131
+
132
+ export function parseCycle1LineFormat(raw) {
133
+ if (raw == null) return null
134
+ const text = String(raw).trim()
135
+ if (!text) return null
136
+ const lines = text.split('\n')
137
+ const chunks = []
138
+ for (const rawLine of lines) {
139
+ const line = rawLine.trim()
140
+ if (!line) continue
141
+ if (line.startsWith('//') || line.startsWith('#')) continue
142
+ if (line.startsWith('```')) continue
143
+ const parts = line.split('|')
144
+ if (parts.length < 4) continue
145
+ const idxField = parts[0].trim()
146
+ const idxList = idxField.split(',')
147
+ .map(s => Number(String(s).replace(/^@/, '').trim()))
148
+ .filter(n => Number.isFinite(n) && n > 0)
149
+ if (idxList.length === 0) continue
150
+ chunks.push({
151
+ _idxList: idxList,
152
+ element: parts[1].trim(),
153
+ category: parts[2].trim(),
154
+ summary: parts.slice(3).join('|').trim(),
155
+ })
156
+ }
157
+ return chunks.length > 0 ? { chunks } : null
158
+ }
159
+
160
+ // Partition by session_id; MIN_BATCH also gates per-session windows, SESSION_CAP bounds per-tick fan-out.
161
+ const CYCLE1_MIN_BATCH = 3
162
+ const CYCLE1_SESSION_CAP = 10
163
+
164
+ // Per-db SKIP gate — concurrent calls drop, scheduler retries.
165
+ const _runCycle1InFlight = new WeakMap()
166
+ const _lastCycle1LogAt = new Map()
167
+
168
+ export function getInFlightCycle1(db) {
169
+ return _runCycle1InFlight.get(db) || null
170
+ }
171
+
172
+ function throwIfAborted(signal) {
173
+ if (signal?.aborted) throw signal.reason ?? new Error('aborted')
174
+ }
175
+
176
+ function logCycle1Throttled(key, message, intervalMs = 60_000) {
177
+ const now = Date.now()
178
+ const last = _lastCycle1LogAt.get(key) || 0
179
+ if (now - last < intervalMs) return
180
+ _lastCycle1LogAt.set(key, now)
181
+ process.stderr.write(message)
182
+ }
183
+
184
+ // Tiny inline semaphore — bounds cycle1 window fan-out.
185
+ function createSemaphore(limit) {
186
+ const cap = Math.max(1, Number(limit) || 1)
187
+ let active = 0
188
+ const queue = []
189
+ const release = () => {
190
+ active -= 1
191
+ const next = queue.shift()
192
+ if (next) next()
193
+ }
194
+ return async (fn) => {
195
+ if (active >= cap) await new Promise(res => queue.push(res))
196
+ active += 1
197
+ try { return await fn() }
198
+ finally { release() }
199
+ }
200
+ }
201
+
202
+ async function countPendingRows(db) {
203
+ try {
204
+ const result = await db.query(
205
+ `SELECT COUNT(*) AS c
206
+ FROM entries
207
+ WHERE chunk_root IS NULL
208
+ AND session_id IS NOT NULL
209
+ AND (reviewed_at IS NULL OR reviewed_at < $1)`,
210
+ [Date.now() - CYCLE1_OMITTED_COOLDOWN_MS],
211
+ )
212
+ return Number(result.rows[0]?.c ?? 0)
213
+ } catch {
214
+ return null
215
+ }
216
+ }
217
+
218
+ export async function runCycle1(db, config = {}, options = {}, dataDir = null) {
219
+ const signal = options?.signal
220
+ throwIfAborted(signal)
221
+ if (_runCycle1InFlight.has(db)) {
222
+ logCycle1Throttled('in-flight', '[cycle1] skipped: already in flight for this db\n')
223
+ return {
224
+ processed: 0, chunks: 0, skipped: 0, sessions: 0,
225
+ skippedInFlight: true,
226
+ pendingRows: await countPendingRows(db),
227
+ }
228
+ }
229
+ const client = await db._pool.connect()
230
+ let gotLock = false
231
+ try {
232
+ throwIfAborted(signal)
233
+ const r = await client.query(`SELECT pg_try_advisory_lock(hashtext($1)) AS got`, ['mixdog.cycle1'])
234
+ gotLock = r.rows[0]?.got === true
235
+ } catch (err) {
236
+ client.release()
237
+ if (signal?.aborted) throw signal.reason ?? err
238
+ process.stderr.write(`[cycle1] advisory lock query failed: ${err.message}\n`)
239
+ return { processed: 0, chunks: 0, skipped: 0, sessions: 0, skippedInFlight: true, pendingRows: await countPendingRows(db) }
240
+ }
241
+ if (!gotLock) {
242
+ client.release()
243
+ logCycle1Throttled('advisory-lock', '[cycle1] skipped: advisory lock held by another worker\n')
244
+ return { processed: 0, chunks: 0, skipped: 0, sessions: 0, skippedInFlight: true, pendingRows: await countPendingRows(db) }
245
+ }
246
+ const p = (async () => {
247
+ try {
248
+ return await _runCycle1Impl(db, config, options, dataDir)
249
+ } finally {
250
+ try { await client.query(`SELECT pg_advisory_unlock(hashtext($1))`, ['mixdog.cycle1']) } catch {}
251
+ client.release()
252
+ }
253
+ })()
254
+ _runCycle1InFlight.set(db, p)
255
+ try {
256
+ return await p
257
+ } finally {
258
+ _runCycle1InFlight.delete(db)
259
+ }
260
+ }
261
+
262
+ async function _runCycle1Impl(db, config = {}, options = {}, dataDir = null) {
263
+ const signal = options?.signal
264
+ throwIfAborted(signal)
265
+ const pendingRowsAtStart = await countPendingRows(db)
266
+ throwIfAborted(signal)
267
+ const batchSize = Math.max(1, Number(config.batch_size ?? 100))
268
+ // Fallback chain handles flat config + nested cycle1 wrap shapes.
269
+ const minBatch = Math.max(1, Number(config?.min_batch ?? config?.cycle1?.min_batch ?? CYCLE1_MIN_BATCH))
270
+ const sessionCap = Math.max(1, Number(config?.session_cap ?? config?.cycle1?.session_cap ?? CYCLE1_SESSION_CAP))
271
+ const preset = options.preset || resolveMaintenancePreset('cycle1')
272
+ // Inner LLM timeout aligns to caller deadline -1s so the channel side can ack gracefully.
273
+ const callerDeadlineMs = Number(options.callerDeadlineMs ?? 0)
274
+ const baseTimeout = Number(config?.timeout ?? config?.cycle1?.timeout ?? 180000)
275
+ const timeout = callerDeadlineMs > 0
276
+ ? Math.min(baseTimeout, Math.max(5000, callerDeadlineMs - 1000))
277
+ : baseTimeout
278
+ // Time-ordered fetch; windows are split purely by total row count below.
279
+ const fetchLimit = sessionCap * batchSize
280
+ const fetchResult = await db.query(
281
+ `SELECT id, ts, role, content, session_id, source_ref, project_id
282
+ FROM entries
283
+ WHERE chunk_root IS NULL
284
+ AND session_id IS NOT NULL
285
+ AND (reviewed_at IS NULL OR reviewed_at < $2)
286
+ ORDER BY ts DESC, id DESC
287
+ LIMIT $1`,
288
+ [fetchLimit, Date.now() - CYCLE1_OMITTED_COOLDOWN_MS],
289
+ )
290
+ throwIfAborted(signal)
291
+ const rowsDesc = fetchResult.rows
292
+
293
+ if (rowsDesc.length < minBatch) {
294
+ throwIfAborted(signal)
295
+ flushEmbeddingDirty(db, { signal }).catch((err) =>
296
+ process.stderr.write(`[cycle1] quick-exit embedding flush failed: ${err.message}\n`)
297
+ )
298
+ return {
299
+ processed: 0, chunks: 0, skipped: 0, sessions: 0,
300
+ skippedInFlight: false,
301
+ pendingRows: pendingRowsAtStart,
302
+ failed_row_ids: [], omitted_row_ids: [], invalid_chunks: [],
303
+ quality: {
304
+ rows_considered: 0,
305
+ committed_members: 0,
306
+ skipped_chunks: 0,
307
+ omitted_rows: 0,
308
+ failed_rows: 0,
309
+ invalid_chunks: 0,
310
+ },
311
+ embedding_dirty: { deferred: true, attempted: 0, succeeded: 0, failed: 0, failed_ids: [] },
312
+ }
313
+ }
314
+
315
+ // Window purely by total fetched row count: ceil(rows / batchSize) windows,
316
+ // evenly sized, in chronological order. Session boundaries are NOT used to
317
+ // split windows — each entry carries a [sess:] marker in the prompt and
318
+ // DEFAULT_CYCLE1_RULES forbids merging chunks across those markers, so a
319
+ // single window may safely span sessions. fetchLimit caps total rows, so
320
+ // windowCount stays bounded (<= session_cap) and every window runs at once.
321
+ const rowsAsc = rowsDesc.slice().reverse()
322
+ const windows = []
323
+ const windowCount = Math.max(1, Math.ceil(rowsAsc.length / batchSize))
324
+ const baseSize = Math.floor(rowsAsc.length / windowCount)
325
+ const remainder = rowsAsc.length % windowCount
326
+ let _offset = 0
327
+ for (let i = 0; i < windowCount; i++) {
328
+ throwIfAborted(signal)
329
+ const size = baseSize + (i < remainder ? 1 : 0)
330
+ windows.push(rowsAsc.slice(_offset, _offset + size))
331
+ _offset += size
332
+ }
333
+
334
+ async function processWindow(rows, windowIdx) {
335
+ throwIfAborted(signal)
336
+ if (rows.length === 0) {
337
+ return {
338
+ committedChunks: 0, committedMembers: 0, skippedChunks: 0, rowsConsidered: 0,
339
+ invalidChunks: [], failedRowIds: [], omittedRowIds: [],
340
+ }
341
+ }
342
+
343
+ const originalRows = rows
344
+ const prefilteredRowIds = []
345
+ let prefilterMarked = 0
346
+ let prefilterMarkFailed = 0
347
+ rows = originalRows.filter((row) => {
348
+ if (!_isStructurallyUnchunkableInput(row)) return true
349
+ prefilteredRowIds.push(Number(row.id))
350
+ return false
351
+ })
352
+ if (prefilteredRowIds.length > 0) {
353
+ const mark = await markTerminalRows(db, prefilteredRowIds, 'prefilter')
354
+ prefilterMarked = mark.marked
355
+ prefilterMarkFailed = mark.failed
356
+ }
357
+ if (rows.length === 0) {
358
+ return {
359
+ committedChunks: 0, committedMembers: 0, skippedChunks: 0, rowsConsidered: originalRows.length,
360
+ invalidChunks: [], failedRowIds: [], omittedRowIds: prefilteredRowIds,
361
+ prefilteredRowIds, prefilterMarked, prefilterMarkFailed,
362
+ }
363
+ }
364
+
365
+ const userMessage = buildCycle1ChunkPrompt(rows)
366
+ const llmCall = typeof options?.callLlm === 'function' ? options.callLlm : callBridgeLlm
367
+
368
+ let raw
369
+ const _tLlm = Date.now()
370
+ try {
371
+ raw = await llmCall({
372
+ role: 'cycle1-agent',
373
+ taskType: 'maintenance',
374
+ mode: 'cycle1',
375
+ preset,
376
+ timeout,
377
+ // Pin cwd to null so every memory cycle call hits the same bridge cache shard.
378
+ cwd: null,
379
+ }, userMessage)
380
+ } catch (err) {
381
+ if (signal?.aborted) throw signal.reason ?? err
382
+ process.stderr.write(`[cycle1] LLM error (window=${windowIdx}): ${err.message}\n`)
383
+ return {
384
+ committedChunks: 0, committedMembers: 0, skippedChunks: rows.length, rowsConsidered: originalRows.length,
385
+ invalidChunks: [{ reason: 'llm_error', member_ids: rows.map(r => Number(r.id)) }],
386
+ failedRowIds: rows.map(r => Number(r.id)),
387
+ omittedRowIds: prefilteredRowIds,
388
+ prefilteredRowIds,
389
+ prefilterMarked,
390
+ prefilterMarkFailed,
391
+ }
392
+ }
393
+ throwIfAborted(signal)
394
+ process.stderr.write(`[cycle1-time] window=${windowIdx} llmMs=${Date.now() - _tLlm}\n`)
395
+
396
+ const parsed = parseCycle1LineFormat(raw)
397
+ const chunkList = Array.isArray(parsed?.chunks) ? parsed.chunks : null
398
+ if (!chunkList) {
399
+ process.stderr.write(`[cycle1] unparseable response (window=${windowIdx}) (${String(raw).slice(0, 200)})\n`)
400
+ return {
401
+ committedChunks: 0, committedMembers: 0, skippedChunks: rows.length, rowsConsidered: originalRows.length,
402
+ invalidChunks: [{ reason: 'unparseable_response', member_ids: rows.map(r => Number(r.id)) }],
403
+ failedRowIds: rows.map(r => Number(r.id)),
404
+ omittedRowIds: prefilteredRowIds,
405
+ prefilteredRowIds,
406
+ prefilterMarked,
407
+ prefilterMarkFailed,
408
+ }
409
+ }
410
+
411
+ const entryByIdx = new Map(rows.map((r, i) => [i + 1, r]))
412
+ const entryById = new Map(rows.map(r => [Number(r.id), r]))
413
+ const usedIds = new Set()
414
+ const committedRowIds = new Set()
415
+ let committedChunks = 0
416
+ let committedMembers = 0
417
+ let skippedChunks = 0
418
+ const invalidChunks = []
419
+ const failedRowIds = []
420
+ const referencedRowIds = new Set()
421
+
422
+ for (const chunk of chunkList) {
423
+ throwIfAborted(signal)
424
+ // Out-of-range @N from the LLM = corrupt grouping. Reject the whole
425
+ // chunk rather than silently committing the survivors; otherwise a
426
+ // line like `1,999|...` would commit only @1 and drop @999.
427
+ const idxList = chunk._idxList.map(n => Number(n))
428
+ const outOfRange = idxList.filter(n => !entryByIdx.has(n))
429
+ if (outOfRange.length > 0) {
430
+ invalidChunks.push({ reason: 'out_of_range_idx', idx_list: idxList })
431
+ skippedChunks += 1
432
+ process.stderr.write(
433
+ `[cycle1] chunk rejected: out_of_range_idx idx_list=${JSON.stringify(idxList)}\n`,
434
+ )
435
+ continue
436
+ }
437
+ const rawIds = idxList.map(n => Number(entryByIdx.get(n).id))
438
+ for (const id of rawIds) {
439
+ if (Number.isFinite(id)) referencedRowIds.add(id)
440
+ }
441
+ const dupeWithin = rawIds.length !== new Set(rawIds).size
442
+ const externalIds = rawIds.filter(n => !Number.isFinite(n) || !entryById.has(n))
443
+ const reusedIds = rawIds.filter(n => usedIds.has(n))
444
+ const memberIds = rawIds.filter(n => Number.isFinite(n) && entryById.has(n) && !usedIds.has(n))
445
+ const element = String(chunk?.element ?? '').trim()
446
+ const category = String(chunk?.category ?? '').trim().toLowerCase()
447
+ const summary = String(chunk?.summary ?? '').trim()
448
+
449
+ if (dupeWithin || externalIds.length > 0 || reusedIds.length > 0) {
450
+ const reason = dupeWithin ? 'duplicate_member_ids'
451
+ : externalIds.length > 0 ? 'external_member_ids'
452
+ : 'reused_member_ids'
453
+ invalidChunks.push({ reason, member_ids: rawIds })
454
+ skippedChunks += 1
455
+ process.stderr.write(
456
+ `[cycle1] chunk rejected: ${reason} member_ids=${JSON.stringify(rawIds)}\n`,
457
+ )
458
+ continue
459
+ }
460
+
461
+ if (memberIds.length === 0 || !element || !summary || !VALID_CATEGORIES.has(category)) {
462
+ invalidChunks.push({ reason: 'incomplete_fields', member_ids: rawIds })
463
+ skippedChunks += 1
464
+ continue
465
+ }
466
+
467
+ if (_isStructurallyInvalidSummary(summary)) {
468
+ process.stderr.write(`[cycle1] noise filtered: ${summary.slice(0, 60)}\n`)
469
+ invalidChunks.push({ reason: 'noise_filtered', member_ids: rawIds })
470
+ skippedChunks += 1
471
+ continue
472
+ }
473
+
474
+ const members = memberIds.map(id => entryById.get(id))
475
+ const rootId = selectRootId(members)
476
+ if (rootId === null) {
477
+ invalidChunks.push({ reason: 'no_root_id', member_ids: memberIds })
478
+ skippedChunks += 1
479
+ continue
480
+ }
481
+
482
+ const projectId = inferChunkProjectId(members)
483
+
484
+ try {
485
+ // A chunk commit is one DB transaction; do not split it with an
486
+ // abort checkpoint. Cancellation is honored before the next chunk.
487
+ await db.transaction(async (tx) => {
488
+ // category on root only; recall filters member leaves via parent root.
489
+ await tx.query(
490
+ `UPDATE entries
491
+ SET chunk_root = $1, is_root = 1, element = $2, category = $3, summary = $4,
492
+ status = 'pending', project_id = $5,
493
+ last_seen_at = $7
494
+ WHERE id = $6`,
495
+ [rootId, element, category, summary, projectId, rootId, Date.now()],
496
+ )
497
+ const nonRootIds = memberIds.filter(mid => mid !== rootId)
498
+ if (nonRootIds.length > 0) {
499
+ await tx.query(
500
+ `UPDATE entries SET chunk_root = $1, project_id = $2 WHERE id = ANY($3::bigint[])`,
501
+ [rootId, projectId, nonRootIds],
502
+ )
503
+ }
504
+ })
505
+ committedChunks += 1
506
+ committedMembers += memberIds.length
507
+ for (const mid of memberIds) {
508
+ usedIds.add(mid)
509
+ committedRowIds.add(mid)
510
+ }
511
+ } catch (err) {
512
+ process.stderr.write(`[cycle1] chunk commit failed (root=${rootId}): ${err.message}\n`)
513
+ skippedChunks += 1
514
+ for (const mid of memberIds) failedRowIds.push(mid)
515
+ }
516
+ }
517
+
518
+ throwIfAborted(signal)
519
+
520
+ const llmOmittedRowIds = rows
521
+ .map(r => Number(r.id))
522
+ .filter(id => !committedRowIds.has(id) && !failedRowIds.includes(id) && !referencedRowIds.has(id))
523
+ const omittedMark = await markOmittedRows(db, llmOmittedRowIds)
524
+ const omittedRowIds = llmOmittedRowIds.concat(prefilteredRowIds)
525
+
526
+ process.stderr.write(
527
+ `[cycle1] window=${windowIdx} entries=${originalRows.length} prompt_entries=${rows.length} chunks=${committedChunks}` +
528
+ ` members=${committedMembers} skipped_chunks=${skippedChunks}` +
529
+ ` omitted=${omittedRowIds.length} prefiltered=${prefilteredRowIds.length}` +
530
+ ` prefilter_marked=${prefilterMarked} prefilter_mark_failed=${prefilterMarkFailed}` +
531
+ ` omitted_deferred=${omittedMark.deferred} omitted_marked=${omittedMark.marked}` +
532
+ ` omitted_mark_failed=${omittedMark.failed}` +
533
+ ` failed_rows=${failedRowIds.length}` +
534
+ ` invalid_chunks=${invalidChunks.length}\n`,
535
+ )
536
+
537
+ return {
538
+ committedChunks, committedMembers, skippedChunks,
539
+ rowsConsidered: originalRows.length,
540
+ invalidChunks, failedRowIds, omittedRowIds, prefilteredRowIds,
541
+ prefilterMarked, prefilterMarkFailed,
542
+ omittedMarked: omittedMark.marked,
543
+ omittedDeferred: omittedMark.deferred,
544
+ omittedMarkFailed: omittedMark.failed,
545
+ }
546
+ }
547
+
548
+ // Fire every window at once: windowCount is bounded by fetchLimit / batchSize
549
+ // (<= session_cap), so there is no need to throttle the fan-out.
550
+ const sem = createSemaphore(Math.max(1, windows.length))
551
+ const settled = await Promise.allSettled(
552
+ windows.map((rows, idx) => sem(() => {
553
+ throwIfAborted(signal)
554
+ return processWindow(rows, idx)
555
+ })),
556
+ )
557
+ const rejected = settled.find(r => r.status === 'rejected')
558
+ if (rejected) throw rejected.reason
559
+ const results = settled.map(r => r.value)
560
+ throwIfAborted(signal)
561
+
562
+ let totalChunks = 0
563
+ let totalMembers = 0
564
+ let totalSkipped = 0
565
+ let totalRowsConsidered = 0
566
+ const allInvalidChunks = []
567
+ const allFailedRowIds = []
568
+ const allOmittedRowIds = []
569
+ const allPrefilteredRowIds = []
570
+ let totalPrefilterMarked = 0
571
+ let totalPrefilterMarkFailed = 0
572
+ let totalOmittedMarked = 0
573
+ let totalOmittedDeferred = 0
574
+ let totalOmittedMarkFailed = 0
575
+ for (const r of results) {
576
+ totalChunks += r.committedChunks
577
+ totalMembers += r.committedMembers
578
+ totalSkipped += r.skippedChunks
579
+ totalRowsConsidered += r.rowsConsidered
580
+ if (Array.isArray(r.invalidChunks)) allInvalidChunks.push(...r.invalidChunks)
581
+ if (Array.isArray(r.failedRowIds)) allFailedRowIds.push(...r.failedRowIds)
582
+ if (Array.isArray(r.omittedRowIds)) allOmittedRowIds.push(...r.omittedRowIds)
583
+ if (Array.isArray(r.prefilteredRowIds)) allPrefilteredRowIds.push(...r.prefilteredRowIds)
584
+ totalPrefilterMarked += Number(r.prefilterMarked || 0)
585
+ totalPrefilterMarkFailed += Number(r.prefilterMarkFailed || 0)
586
+ totalOmittedMarked += Number(r.omittedMarked || 0)
587
+ totalOmittedDeferred += Number(r.omittedDeferred || 0)
588
+ totalOmittedMarkFailed += Number(r.omittedMarkFailed || 0)
589
+ }
590
+
591
+ process.stderr.write(
592
+ `[cycle1] windows=${windows.length} rows=${totalRowsConsidered} chunks=${totalChunks}` +
593
+ ` members=${totalMembers} skipped_chunks=${totalSkipped}` +
594
+ ` omitted=${allOmittedRowIds.length} prefiltered=${allPrefilteredRowIds.length}` +
595
+ ` prefilter_marked=${totalPrefilterMarked} prefilter_mark_failed=${totalPrefilterMarkFailed}` +
596
+ ` omitted_deferred=${totalOmittedDeferred} omitted_marked=${totalOmittedMarked}` +
597
+ ` omitted_mark_failed=${totalOmittedMarkFailed}` +
598
+ ` failed_rows=${allFailedRowIds.length}` +
599
+ ` invalid_chunks=${allInvalidChunks.length}\n`,
600
+ )
601
+
602
+ // Embedding is fire-and-forget; sidecar persist does not guarantee embedding completion.
603
+ throwIfAborted(signal)
604
+ flushEmbeddingDirty(db, { signal })
605
+ .then((d) => {
606
+ if (d.attempted > 0) {
607
+ process.stderr.write(
608
+ `[cycle1] embedding flush attempted=${d.attempted} ok=${d.succeeded} failed=${d.failed.length}\n`,
609
+ )
610
+ }
611
+ })
612
+ .catch((err) => process.stderr.write(`[cycle1] embedding flush failed: ${err.message}\n`))
613
+
614
+ return {
615
+ processed: totalMembers,
616
+ chunks: totalChunks,
617
+ skipped: totalSkipped,
618
+ sessions: windows.length,
619
+ skippedInFlight: false,
620
+ pendingRows: pendingRowsAtStart,
621
+ failed_row_ids: allFailedRowIds,
622
+ omitted_row_ids: allOmittedRowIds,
623
+ prefiltered_row_ids: allPrefilteredRowIds,
624
+ invalid_chunks: allInvalidChunks,
625
+ quality: {
626
+ rows_considered: totalRowsConsidered,
627
+ committed_members: totalMembers,
628
+ skipped_chunks: totalSkipped,
629
+ omitted_rows: allOmittedRowIds.length,
630
+ prefiltered_rows: allPrefilteredRowIds.length,
631
+ prefilter_marked_rows: totalPrefilterMarked,
632
+ prefilter_mark_failed_rows: totalPrefilterMarkFailed,
633
+ omitted_deferred_rows: totalOmittedDeferred,
634
+ omitted_marked_rows: totalOmittedMarked,
635
+ omitted_mark_failed_rows: totalOmittedMarkFailed,
636
+ failed_rows: allFailedRowIds.length,
637
+ invalid_chunks: allInvalidChunks.length,
638
+ },
639
+ embedding_dirty: { deferred: true, attempted: 0, succeeded: 0, failed: 0, failed_ids: [] },
640
+ }
641
+ }