@adhdev/daemon-core 0.9.82-rc.46 → 0.9.82-rc.461

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 (440) hide show
  1. package/dist/boot/daemon-lifecycle.d.ts +9 -0
  2. package/dist/boot/process-hardening.d.ts +50 -0
  3. package/dist/build-info.d.ts +37 -0
  4. package/dist/chat/source-machine.d.ts +166 -0
  5. package/dist/chat/source-resolver.d.ts +104 -0
  6. package/dist/chat/subscription-updates.d.ts +1 -0
  7. package/dist/cli-adapter-types.d.ts +68 -2
  8. package/dist/cli-adapters/cli-script-runner.d.ts +117 -0
  9. package/dist/cli-adapters/cli-state-engine.d.ts +200 -0
  10. package/dist/cli-adapters/provider-cli-adapter.d.ts +126 -64
  11. package/dist/cli-adapters/provider-cli-parse.d.ts +5 -0
  12. package/dist/cli-adapters/provider-cli-runtime.d.ts +34 -0
  13. package/dist/cli-adapters/provider-cli-shared.d.ts +74 -0
  14. package/dist/cli-adapters/pty-write-chunking.d.ts +34 -0
  15. package/dist/cli-adapters/raw-terminal-io.d.ts +37 -0
  16. package/dist/cli-adapters/resolve-executable.d.ts +69 -0
  17. package/dist/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +6 -3
  18. package/dist/cli-adapters/terminal-backends/types.d.ts +10 -2
  19. package/dist/cli-adapters/terminal-screen.d.ts +4 -11
  20. package/dist/commands/chat-commands-debug-bundle.d.ts +14 -0
  21. package/dist/commands/chat-commands-read.d.ts +7 -0
  22. package/dist/commands/chat-commands-scope.d.ts +39 -0
  23. package/dist/commands/chat-commands-shared.d.ts +33 -0
  24. package/dist/commands/chat-commands-write.d.ts +14 -0
  25. package/dist/commands/chat-commands.d.ts +10 -23
  26. package/dist/commands/cli-manager.d.ts +25 -1
  27. package/dist/commands/handler.d.ts +125 -0
  28. package/dist/commands/high-family/index.d.ts +3 -0
  29. package/dist/commands/high-family/mesh-coordinator-launch.d.ts +2 -0
  30. package/dist/commands/high-family/mesh-events.d.ts +2 -0
  31. package/dist/commands/high-family/mesh-status.d.ts +2 -0
  32. package/dist/commands/high-family/types.d.ts +64 -0
  33. package/dist/commands/low-family/coordinator-prompt.d.ts +9 -0
  34. package/dist/commands/low-family/daemon-lifecycle.d.ts +2 -0
  35. package/dist/commands/low-family/diagnostics.d.ts +2 -0
  36. package/dist/commands/low-family/index.d.ts +3 -0
  37. package/dist/commands/low-family/mesh-ledger.d.ts +10 -0
  38. package/dist/commands/low-family/mesh-node-logs.d.ts +2 -0
  39. package/dist/commands/low-family/notification.d.ts +2 -0
  40. package/dist/commands/low-family/refine-config.d.ts +2 -0
  41. package/dist/commands/low-family/session-host.d.ts +2 -0
  42. package/dist/commands/low-family/spec-providerdev.d.ts +11 -0
  43. package/dist/commands/low-family/status-meta.d.ts +2 -0
  44. package/dist/commands/low-family/types.d.ts +33 -0
  45. package/dist/commands/med-family/cli-agent.d.ts +2 -0
  46. package/dist/commands/med-family/fast-forward.d.ts +2 -0
  47. package/dist/commands/med-family/ide.d.ts +10 -0
  48. package/dist/commands/med-family/index.d.ts +3 -0
  49. package/dist/commands/med-family/mesh-crud.d.ts +48 -0
  50. package/dist/commands/med-family/mesh-host-pairing.d.ts +2 -0
  51. package/dist/commands/med-family/mesh-queue.d.ts +2 -0
  52. package/dist/commands/med-family/mesh-restart.d.ts +2 -0
  53. package/dist/commands/med-family/types.d.ts +152 -0
  54. package/dist/commands/mesh-coordinator.d.ts +85 -1
  55. package/dist/commands/router-refine.d.ts +137 -0
  56. package/dist/commands/router-worktree-cleanup.d.ts +133 -0
  57. package/dist/commands/router.d.ts +241 -12
  58. package/dist/commands/upgrade-helper.d.ts +41 -1
  59. package/dist/config/chat-history.d.ts +9 -0
  60. package/dist/config/config.d.ts +19 -0
  61. package/dist/config/mesh-config.d.ts +136 -1
  62. package/dist/config/mesh-json-config.d.ts +199 -0
  63. package/dist/config/registry-resolver.d.ts +54 -0
  64. package/dist/config/repo-settings.d.ts +77 -0
  65. package/dist/daemon/dev-server.d.ts +0 -2
  66. package/dist/detection/cli-detector.d.ts +17 -0
  67. package/dist/detection/ide-detector.d.ts +13 -0
  68. package/dist/detection/win32-ide-version.d.ts +37 -0
  69. package/dist/git/change-impact-config.d.ts +159 -0
  70. package/dist/git/git-commands.d.ts +26 -2
  71. package/dist/git/git-diff.d.ts +6 -0
  72. package/dist/git/git-executor.d.ts +11 -0
  73. package/dist/git/git-status.d.ts +57 -0
  74. package/dist/git/git-types.d.ts +2 -50
  75. package/dist/git/git-worktree.d.ts +71 -1
  76. package/dist/git/index.d.ts +3 -1
  77. package/dist/index.d.ts +67 -15
  78. package/dist/index.js +58583 -25541
  79. package/dist/index.js.map +1 -1
  80. package/dist/index.mjs +58207 -25332
  81. package/dist/index.mjs.map +1 -1
  82. package/dist/installer.d.ts +1 -4
  83. package/dist/ipc/local-ipc-server.d.ts +91 -0
  84. package/dist/launch.d.ts +1 -1
  85. package/dist/logging/async-batch-writer.d.ts +10 -0
  86. package/dist/logging/debug-config.d.ts +16 -0
  87. package/dist/logging/log-redactor.d.ts +24 -0
  88. package/dist/logging/log-tail-reader.d.ts +81 -0
  89. package/dist/logging/logger.d.ts +1 -1
  90. package/dist/mesh/contracts.d.ts +230 -0
  91. package/dist/mesh/coordinator-prompt.d.ts +76 -0
  92. package/dist/mesh/coordinator-registry.d.ts +59 -0
  93. package/dist/mesh/mesh-active-work.d.ts +174 -0
  94. package/dist/mesh/mesh-clone-grace.d.ts +8 -0
  95. package/dist/mesh/mesh-coordinator-config.d.ts +21 -0
  96. package/dist/mesh/mesh-delivery-policy.d.ts +131 -0
  97. package/dist/mesh/mesh-event-classify.d.ts +7 -0
  98. package/dist/mesh/mesh-event-forwarding.d.ts +41 -0
  99. package/dist/mesh/mesh-event-trace.d.ts +21 -0
  100. package/dist/mesh/mesh-events-coordinator.d.ts +4 -0
  101. package/dist/mesh/mesh-events-pending.d.ts +155 -0
  102. package/dist/mesh/mesh-events-stale.d.ts +61 -0
  103. package/dist/mesh/mesh-events-utils.d.ts +75 -0
  104. package/dist/mesh/mesh-events.d.ts +6 -49
  105. package/dist/mesh/mesh-fast-forward.d.ts +81 -0
  106. package/dist/mesh/mesh-host-ownership.d.ts +29 -0
  107. package/dist/mesh/mesh-init.d.ts +128 -0
  108. package/dist/mesh/mesh-ledger-reconciliation.d.ts +23 -4
  109. package/dist/mesh/mesh-ledger.d.ts +133 -1
  110. package/dist/mesh/mesh-magi-status.d.ts +63 -0
  111. package/dist/mesh/mesh-missions.d.ts +189 -0
  112. package/dist/mesh/mesh-node-identity.d.ts +310 -0
  113. package/dist/mesh/mesh-queue-assignment.d.ts +158 -0
  114. package/dist/mesh/mesh-reconcile-loop.d.ts +80 -0
  115. package/dist/mesh/mesh-refine-batch.d.ts +68 -0
  116. package/dist/mesh/mesh-refine-gates.d.ts +428 -0
  117. package/dist/mesh/mesh-refine-status.d.ts +62 -0
  118. package/dist/mesh/mesh-review-inbox.d.ts +90 -0
  119. package/dist/mesh/mesh-routing.d.ts +70 -0
  120. package/dist/mesh/mesh-runtime-store.d.ts +493 -0
  121. package/dist/mesh/mesh-scheduling-runtime.d.ts +78 -0
  122. package/dist/mesh/mesh-task-inflight.d.ts +46 -0
  123. package/dist/mesh/mesh-task-stats.d.ts +49 -0
  124. package/dist/mesh/mesh-unresolved-forward-outbox.d.ts +30 -0
  125. package/dist/mesh/mesh-warmup-deadline.d.ts +68 -0
  126. package/dist/mesh/mesh-work-queue.d.ts +318 -5
  127. package/dist/mesh/preview-freshness.d.ts +28 -0
  128. package/dist/mesh/refine-config.d.ts +216 -0
  129. package/dist/mesh/worktree-bootstrap-config.d.ts +219 -0
  130. package/dist/providers/acp-provider-instance.d.ts +5 -0
  131. package/dist/providers/approval-utils.d.ts +45 -0
  132. package/dist/providers/chat-message-normalization.d.ts +58 -1
  133. package/dist/providers/cli-provider-instance.d.ts +290 -3
  134. package/dist/providers/contracts.d.ts +149 -6
  135. package/dist/providers/external-sources.d.ts +71 -0
  136. package/dist/providers/manual-attendance.d.ts +79 -0
  137. package/dist/providers/native-history/antigravity-claim-registry.d.ts +28 -0
  138. package/dist/providers/native-history/antigravity-cli-transcript.d.ts +137 -0
  139. package/dist/providers/native-history/claude-cli-transcript.d.ts +70 -0
  140. package/dist/providers/native-history/codex-cli-transcript.d.ts +73 -0
  141. package/dist/providers/native-history/constants.d.ts +12 -0
  142. package/dist/providers/native-history/dispatcher.d.ts +31 -0
  143. package/dist/providers/native-history/hermes-cli-transcript.d.ts +30 -0
  144. package/dist/providers/native-history/index.d.ts +13 -0
  145. package/dist/providers/provider-instance-manager.d.ts +29 -0
  146. package/dist/providers/provider-instance.d.ts +30 -1
  147. package/dist/providers/provider-loader.d.ts +42 -5
  148. package/dist/providers/provider-trust.d.ts +31 -0
  149. package/dist/providers/read-chat-contract.d.ts +29 -0
  150. package/dist/providers/sdk/v1/builders/acp/detect-status.d.ts +68 -0
  151. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +86 -0
  152. package/dist/providers/sdk/v1/builders/cli/parse-approval-squash.d.ts +59 -0
  153. package/dist/providers/sdk/v1/builders/cli/parse-approval.d.ts +65 -0
  154. package/dist/providers/sdk/v1/builders/cli/parse-session.d.ts +91 -0
  155. package/dist/providers/sdk/v1/builders/cli/visible-region.d.ts +42 -0
  156. package/dist/providers/sdk/v1/fixture-tooling/format.d.ts +126 -0
  157. package/dist/providers/sdk/v1/fixture-tooling/index.d.ts +8 -0
  158. package/dist/providers/sdk/v1/fixture-tooling/replay.d.ts +38 -0
  159. package/dist/providers/sdk/v1/index.d.ts +30 -0
  160. package/dist/providers/sdk/v1/sandbox/README-design.d.ts +193 -0
  161. package/dist/providers/sdk/v1/sandbox/require-whitelist.d.ts +74 -0
  162. package/dist/providers/sdk/v1/sandbox/script-runner.d.ts +98 -0
  163. package/dist/providers/sdk/v1/types/cli/index.d.ts +277 -0
  164. package/dist/providers/sdk/v1/types/common/index.d.ts +203 -0
  165. package/dist/providers/sdk/v1/validators/index.d.ts +5 -0
  166. package/dist/providers/sdk/v1/validators/manifest.d.ts +40 -0
  167. package/dist/providers/sdk/v1/validators/taint.d.ts +52 -0
  168. package/dist/providers/spec/adapter.d.ts +98 -0
  169. package/dist/providers/spec/cli-adapter.d.ts +223 -0
  170. package/dist/providers/spec/evaluator.d.ts +45 -0
  171. package/dist/providers/spec/fsm-driver.d.ts +449 -0
  172. package/dist/providers/spec/fsm-evaluator.d.ts +93 -0
  173. package/dist/providers/spec/fsm-loader.d.ts +14 -0
  174. package/dist/providers/spec/fsm-types.d.ts +202 -0
  175. package/dist/providers/spec/native-history-executor.d.ts +41 -0
  176. package/dist/providers/spec/pre-launch-trust.d.ts +16 -0
  177. package/dist/providers/spec/route.d.ts +4 -0
  178. package/dist/providers/spec/types.d.ts +222 -0
  179. package/dist/providers/status-monitor.d.ts +7 -7
  180. package/dist/providers/transcript-v2.d.ts +176 -0
  181. package/dist/providers/types/interactive-prompt.d.ts +84 -0
  182. package/dist/providers/working-dir.d.ts +17 -0
  183. package/dist/repo-mesh-types.d.ts +598 -12
  184. package/dist/runtime-defaults.d.ts +2 -0
  185. package/dist/session-host/managed-host.d.ts +64 -0
  186. package/dist/session-host/runtime-surface.d.ts +10 -16
  187. package/dist/sessions/registry.d.ts +6 -0
  188. package/dist/shared-types-extra.d.ts +2 -4
  189. package/dist/shared-types.d.ts +86 -55
  190. package/dist/status/normalize.d.ts +1 -1
  191. package/dist/status/normalize.js +1 -0
  192. package/dist/status/normalize.js.map +1 -1
  193. package/dist/status/normalize.mjs +1 -0
  194. package/dist/status/normalize.mjs.map +1 -1
  195. package/dist/status/reporter.d.ts +2 -0
  196. package/dist/status/snapshot.d.ts +26 -0
  197. package/dist/system/hash.d.ts +8 -0
  198. package/dist/system/load-better-sqlite3.d.ts +21 -0
  199. package/dist/types.d.ts +5 -0
  200. package/package.json +11 -4
  201. package/src/agent-stream/poller.ts +2 -3
  202. package/src/agent-stream/provider-adapter.ts +1 -1
  203. package/src/boot/daemon-lifecycle.ts +80 -14
  204. package/src/boot/process-hardening.ts +89 -0
  205. package/src/build-info.ts +73 -0
  206. package/src/chat/source-machine.ts +534 -0
  207. package/src/chat/source-resolver.ts +0 -0
  208. package/src/chat/subscription-updates.ts +20 -1
  209. package/src/cli-adapter-types.d.ts +3 -1
  210. package/src/cli-adapter-types.ts +68 -2
  211. package/src/cli-adapters/cli-script-runner.ts +421 -0
  212. package/src/cli-adapters/cli-state-engine.ts +1246 -0
  213. package/src/cli-adapters/provider-cli-adapter.d.ts +1 -1
  214. package/src/cli-adapters/provider-cli-adapter.ts +914 -1144
  215. package/src/cli-adapters/provider-cli-parse.d.ts +2 -0
  216. package/src/cli-adapters/provider-cli-parse.ts +19 -0
  217. package/src/cli-adapters/provider-cli-runtime.ts +56 -7
  218. package/src/cli-adapters/provider-cli-shared.d.ts +4 -0
  219. package/src/cli-adapters/provider-cli-shared.ts +121 -11
  220. package/src/cli-adapters/pty-transport.ts +2 -1
  221. package/src/cli-adapters/pty-write-chunking.ts +106 -0
  222. package/src/cli-adapters/raw-terminal-io.ts +252 -0
  223. package/src/cli-adapters/resolve-executable.ts +204 -0
  224. package/src/cli-adapters/session-host-transport.ts +2 -1
  225. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.ts +63 -29
  226. package/src/cli-adapters/terminal-backends/types.ts +10 -3
  227. package/src/cli-adapters/terminal-screen.ts +16 -81
  228. package/src/commands/chat-commands-debug-bundle.ts +398 -0
  229. package/src/commands/chat-commands-read.ts +2488 -0
  230. package/src/commands/chat-commands-scope.ts +54 -0
  231. package/src/commands/chat-commands-shared.ts +114 -0
  232. package/src/commands/chat-commands-write.ts +891 -0
  233. package/src/commands/chat-commands.ts +19 -1841
  234. package/src/commands/cli-manager.ts +596 -27
  235. package/src/commands/handler.ts +845 -2
  236. package/src/commands/high-family/index.ts +28 -0
  237. package/src/commands/high-family/mesh-coordinator-launch.ts +680 -0
  238. package/src/commands/high-family/mesh-events.ts +89 -0
  239. package/src/commands/high-family/mesh-status.ts +832 -0
  240. package/src/commands/high-family/types.ts +80 -0
  241. package/src/commands/low-family/coordinator-prompt.ts +72 -0
  242. package/src/commands/low-family/daemon-lifecycle.ts +120 -0
  243. package/src/commands/low-family/diagnostics.ts +57 -0
  244. package/src/commands/low-family/index.ts +37 -0
  245. package/src/commands/low-family/mesh-ledger.ts +62 -0
  246. package/src/commands/low-family/mesh-node-logs.ts +87 -0
  247. package/src/commands/low-family/notification.ts +116 -0
  248. package/src/commands/low-family/refine-config.ts +106 -0
  249. package/src/commands/low-family/session-host.ts +274 -0
  250. package/src/commands/low-family/spec-providerdev.ts +217 -0
  251. package/src/commands/low-family/status-meta.ts +112 -0
  252. package/src/commands/low-family/types.ts +39 -0
  253. package/src/commands/med-family/cli-agent.ts +241 -0
  254. package/src/commands/med-family/fast-forward.ts +230 -0
  255. package/src/commands/med-family/ide.ts +163 -0
  256. package/src/commands/med-family/index.ts +37 -0
  257. package/src/commands/med-family/mesh-crud.ts +1287 -0
  258. package/src/commands/med-family/mesh-host-pairing.ts +234 -0
  259. package/src/commands/med-family/mesh-queue.ts +167 -0
  260. package/src/commands/med-family/mesh-restart.ts +92 -0
  261. package/src/commands/med-family/types.ts +153 -0
  262. package/src/commands/mesh-coordinator.ts +334 -124
  263. package/src/commands/router-refine.ts +1711 -0
  264. package/src/commands/router-worktree-cleanup.ts +870 -0
  265. package/src/commands/router.ts +1025 -4070
  266. package/src/commands/stream-commands.ts +8 -0
  267. package/src/commands/upgrade-helper.ts +310 -45
  268. package/src/config/chat-history.ts +493 -24
  269. package/src/config/config.ts +30 -0
  270. package/src/config/mesh-config.ts +632 -23
  271. package/src/config/mesh-json-config.ts +376 -0
  272. package/src/config/recent-activity.ts +8 -2
  273. package/src/config/registry-resolver.ts +100 -0
  274. package/src/config/repo-settings.ts +111 -0
  275. package/src/daemon/dev-auto-implement.ts +3 -2
  276. package/src/daemon/dev-cli-debug.ts +10 -1
  277. package/src/daemon/dev-server.ts +0 -541
  278. package/src/detection/cli-detector.ts +94 -9
  279. package/src/detection/ide-detector.ts +55 -16
  280. package/src/detection/win32-ide-version.ts +106 -0
  281. package/src/git/change-impact-config.ts +354 -0
  282. package/src/git/git-commands.ts +112 -16
  283. package/src/git/git-diff.ts +81 -11
  284. package/src/git/git-executor.ts +12 -0
  285. package/src/git/git-status.ts +767 -48
  286. package/src/git/git-types.ts +14 -62
  287. package/src/git/git-worktree.ts +261 -4
  288. package/src/git/index.ts +17 -0
  289. package/src/index.ts +200 -12
  290. package/src/installer.d.ts +1 -1
  291. package/src/installer.ts +8 -6
  292. package/src/ipc/local-ipc-server.ts +278 -0
  293. package/src/launch.d.ts +1 -1
  294. package/src/launch.ts +37 -28
  295. package/src/logging/async-batch-writer.ts +55 -0
  296. package/src/logging/command-log.ts +7 -5
  297. package/src/logging/debug-config.ts +25 -0
  298. package/src/logging/debug-trace.ts +7 -2
  299. package/src/logging/log-redactor.ts +100 -0
  300. package/src/logging/log-tail-reader.ts +341 -0
  301. package/src/logging/logger.ts +14 -7
  302. package/src/mesh/contracts.ts +469 -0
  303. package/src/mesh/coordinator-prompt.ts +554 -34
  304. package/src/mesh/coordinator-registry.ts +121 -0
  305. package/src/mesh/mesh-active-work.ts +656 -0
  306. package/src/mesh/mesh-clone-grace.ts +68 -0
  307. package/src/mesh/mesh-coordinator-config.ts +97 -0
  308. package/src/mesh/mesh-delivery-policy.ts +315 -0
  309. package/src/mesh/mesh-event-classify.ts +73 -0
  310. package/src/mesh/mesh-event-forwarding.ts +1950 -0
  311. package/src/mesh/mesh-event-trace.ts +67 -0
  312. package/src/mesh/mesh-events-coordinator.ts +32 -0
  313. package/src/mesh/mesh-events-pending.ts +1132 -0
  314. package/src/mesh/mesh-events-stale.ts +440 -0
  315. package/src/mesh/mesh-events-utils.ts +443 -0
  316. package/src/mesh/mesh-events.ts +35 -1035
  317. package/src/mesh/mesh-fast-forward.ts +856 -0
  318. package/src/mesh/mesh-host-ownership.ts +111 -0
  319. package/src/mesh/mesh-init.ts +350 -0
  320. package/src/mesh/mesh-ledger-reconciliation.ts +12 -7
  321. package/src/mesh/mesh-ledger.ts +873 -102
  322. package/src/mesh/mesh-magi-status.ts +223 -0
  323. package/src/mesh/mesh-missions.ts +449 -0
  324. package/src/mesh/mesh-node-identity.ts +2065 -0
  325. package/src/mesh/mesh-queue-assignment.ts +2233 -0
  326. package/src/mesh/mesh-reconcile-loop.ts +2397 -0
  327. package/src/mesh/mesh-refine-batch.ts +205 -0
  328. package/src/mesh/mesh-refine-gates.ts +1686 -0
  329. package/src/mesh/mesh-refine-status.ts +231 -0
  330. package/src/mesh/mesh-review-inbox.ts +308 -0
  331. package/src/mesh/mesh-routing.ts +291 -0
  332. package/src/mesh/mesh-runtime-store.ts +2056 -0
  333. package/src/mesh/mesh-scheduling-runtime.ts +198 -0
  334. package/src/mesh/mesh-task-inflight.ts +70 -0
  335. package/src/mesh/mesh-task-stats.ts +161 -0
  336. package/src/mesh/mesh-unresolved-forward-outbox.ts +185 -0
  337. package/src/mesh/mesh-warmup-deadline.ts +152 -0
  338. package/src/mesh/mesh-work-queue.ts +1281 -141
  339. package/src/mesh/preview-freshness.ts +163 -0
  340. package/src/mesh/refine-config.ts +423 -0
  341. package/src/mesh/worktree-bootstrap-config.ts +599 -0
  342. package/src/providers/acp-provider-instance.ts +43 -10
  343. package/src/providers/approval-utils.d.ts +5 -0
  344. package/src/providers/approval-utils.ts +99 -5
  345. package/src/providers/chat-message-normalization.ts +126 -4
  346. package/src/providers/cli-provider-instance.ts +2456 -107
  347. package/src/providers/contracts.d.ts +55 -0
  348. package/src/providers/contracts.ts +160 -6
  349. package/src/providers/extension-provider-instance.ts +12 -7
  350. package/src/providers/external-sources.ts +218 -0
  351. package/src/providers/ide-provider-instance.ts +35 -12
  352. package/src/providers/manual-attendance.ts +105 -0
  353. package/src/providers/native-history/antigravity-claim-registry.ts +131 -0
  354. package/src/providers/native-history/antigravity-cli-transcript.ts +1193 -0
  355. package/src/providers/native-history/claude-cli-transcript.ts +396 -0
  356. package/src/providers/native-history/codex-cli-transcript.ts +479 -0
  357. package/src/providers/native-history/constants.ts +19 -0
  358. package/src/providers/native-history/dispatcher.ts +503 -0
  359. package/src/providers/native-history/hermes-cli-transcript.ts +260 -0
  360. package/src/providers/native-history/index.ts +30 -0
  361. package/src/providers/provider-instance-manager.ts +71 -0
  362. package/src/providers/provider-instance.ts +24 -1
  363. package/src/providers/provider-loader.ts +700 -55
  364. package/src/providers/provider-schema.ts +87 -14
  365. package/src/providers/provider-trust.ts +114 -0
  366. package/src/providers/read-chat-contract.ts +76 -16
  367. package/src/providers/sdk/README.md +49 -0
  368. package/src/providers/sdk/v1/builders/acp/detect-status.ts +144 -0
  369. package/src/providers/sdk/v1/builders/cli/detect-status.ts +313 -0
  370. package/src/providers/sdk/v1/builders/cli/parse-approval-squash.ts +158 -0
  371. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +261 -0
  372. package/src/providers/sdk/v1/builders/cli/parse-session.ts +276 -0
  373. package/src/providers/sdk/v1/builders/cli/visible-region.ts +143 -0
  374. package/src/providers/sdk/v1/fixture-tooling/format.ts +130 -0
  375. package/src/providers/sdk/v1/fixture-tooling/index.ts +22 -0
  376. package/src/providers/sdk/v1/fixture-tooling/replay.ts +352 -0
  377. package/src/providers/sdk/v1/index.ts +152 -0
  378. package/src/providers/sdk/v1/sandbox/README-design.ts +195 -0
  379. package/src/providers/sdk/v1/sandbox/require-whitelist.ts +472 -0
  380. package/src/providers/sdk/v1/sandbox/script-runner.ts +150 -0
  381. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +509 -0
  382. package/src/providers/sdk/v1/schemas/primitives/acp-session-protocol-v1.json +131 -0
  383. package/src/providers/sdk/v1/schemas/primitives/native-history-codex-rollout-v1.json +66 -0
  384. package/src/providers/sdk/v1/schemas/primitives/tui-approval-squash-v1.json +91 -0
  385. package/src/providers/sdk/v1/schemas/primitives/tui-assistant-block-v1.json +91 -0
  386. package/src/providers/sdk/v1/schemas/primitives/tui-cue-ordering-v1.json +47 -0
  387. package/src/providers/sdk/v1/schemas/primitives/tui-dispatch-order-v1.json +32 -0
  388. package/src/providers/sdk/v1/schemas/primitives/tui-footer-chrome-v1.json +42 -0
  389. package/src/providers/sdk/v1/schemas/primitives/tui-index-finder-v1.json +27 -0
  390. package/src/providers/sdk/v1/schemas/primitives/tui-modal-v1.json +125 -0
  391. package/src/providers/sdk/v1/schemas/primitives/tui-prompt-marker-v1.json +45 -0
  392. package/src/providers/sdk/v1/schemas/primitives/tui-session-id-extraction-v1.json +46 -0
  393. package/src/providers/sdk/v1/schemas/primitives/tui-settled-prompt-v1.json +71 -0
  394. package/src/providers/sdk/v1/schemas/primitives/tui-spinner-v1.json +83 -0
  395. package/src/providers/sdk/v1/schemas/primitives/tui-transcript-pty-v1.json +83 -0
  396. package/src/providers/sdk/v1/schemas/primitives/tui-visible-region-v1.json +57 -0
  397. package/src/providers/sdk/v1/schemas/primitives/tui-welcome-screen-v1.json +35 -0
  398. package/src/providers/sdk/v1/types/cli/index.ts +365 -0
  399. package/src/providers/sdk/v1/types/common/index.ts +229 -0
  400. package/src/providers/sdk/v1/validators/index.ts +19 -0
  401. package/src/providers/sdk/v1/validators/manifest.ts +110 -0
  402. package/src/providers/sdk/v1/validators/taint.ts +309 -0
  403. package/src/providers/spec/adapter.ts +246 -0
  404. package/src/providers/spec/cli-adapter.ts +1064 -0
  405. package/src/providers/spec/evaluator.ts +407 -0
  406. package/src/providers/spec/fsm-driver.ts +1528 -0
  407. package/src/providers/spec/fsm-evaluator.ts +290 -0
  408. package/src/providers/spec/fsm-loader.ts +128 -0
  409. package/src/providers/spec/fsm-types.ts +301 -0
  410. package/src/providers/spec/native-history-executor.ts +1174 -0
  411. package/src/providers/spec/pre-launch-trust.ts +104 -0
  412. package/src/providers/spec/route.ts +51 -0
  413. package/src/providers/spec/types.ts +262 -0
  414. package/src/providers/status-monitor.d.ts +7 -7
  415. package/src/providers/status-monitor.ts +37 -22
  416. package/src/providers/transcript-v2.ts +567 -0
  417. package/src/providers/types/interactive-prompt.ts +536 -0
  418. package/src/providers/version-archive.ts +64 -24
  419. package/src/providers/working-dir.ts +23 -0
  420. package/src/repo-mesh-types.ts +836 -13
  421. package/src/runtime-defaults.ts +39 -0
  422. package/src/session-host/managed-host.ts +218 -0
  423. package/src/session-host/runtime-surface.ts +20 -80
  424. package/src/sessions/registry.ts +6 -0
  425. package/src/shared-types-extra.ts +2 -4
  426. package/src/shared-types.d.ts +8 -0
  427. package/src/shared-types.ts +124 -55
  428. package/src/status/builders.ts +26 -6
  429. package/src/status/normalize.ts +2 -0
  430. package/src/status/reporter.ts +19 -1
  431. package/src/status/snapshot.ts +95 -26
  432. package/src/system/hash.ts +23 -0
  433. package/src/system/host-memory.ts +29 -12
  434. package/src/system/load-better-sqlite3.ts +68 -0
  435. package/src/types.ts +5 -0
  436. package/dist/cli-adapters/terminal-backends/xterm-backend.d.ts +0 -17
  437. package/dist/mesh/mesh-sync.d.ts +0 -53
  438. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +0 -16
  439. package/src/cli-adapters/terminal-backends/xterm-backend.ts +0 -97
  440. package/src/mesh/mesh-sync.ts +0 -111
@@ -0,0 +1,1132 @@
1
+ import { appendFileSync, existsSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'fs';
2
+ import { join } from 'path';
3
+ import { randomUUID } from 'crypto';
4
+ import { LOG } from '../logging/logger.js';
5
+ import { getLedgerDir, readLedgerEntries, appendLedgerEntry } from './mesh-ledger.js';
6
+ import { MeshRuntimeStore } from './mesh-runtime-store.js';
7
+ import { buildMeshSystemMessage, readNonEmptyString, readRecord, resolveEventSessionId, readMeshCompletionSummary, isWeakCompletionMetadata } from './mesh-events-utils.js';
8
+ import { daemonIdsEquivalent, expandDaemonIdForms } from '@adhdev/mesh-shared';
9
+ import {
10
+ assertPendingMeshCoordinatorEventV2,
11
+ buildPendingEventEmitStamp,
12
+ coordinatorIdentityEquals,
13
+ coordinatorIdentityFromEmitFields,
14
+ coordinatorIdentityKey,
15
+ isMeshEventScope,
16
+ MESH_PROTOCOL_VERSION_V2,
17
+ shouldDeliverPendingEventToCoordinator,
18
+ type CoordinatorIdentity,
19
+ type MeshEventScope,
20
+ type PendingMeshCoordinatorEventV2,
21
+ } from './contracts.js';
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // MCP coordinator pending-event queue — FILE-BASED PERSISTENCE
25
+ // ---------------------------------------------------------------------------
26
+ // When a mesh event fires but no CLI coordinator session is registered (e.g.
27
+ // the coordinator is Claude Code running via MCP), we persist the event to a
28
+ // per-mesh JSONL file so it survives daemon restarts. The 50-entry hard cap
29
+ // is removed; the file is drained atomically on each get_pending_mesh_events
30
+ // call and limited to 100 KB to prevent runaway growth.
31
+ //
32
+ // File: <ledgerDir>/<meshId>.pending-events.jsonl
33
+ // ---------------------------------------------------------------------------
34
+
35
+ export interface PendingMeshCoordinatorEvent {
36
+ event: string;
37
+ meshId: string;
38
+ nodeLabel: string;
39
+ nodeId?: string;
40
+ workspace?: string;
41
+ metadataEvent: Record<string, unknown>;
42
+ coordinatorMessage?: string;
43
+ queuedAt: number;
44
+ /**
45
+ * When set, this event is intended for a specific coordinator daemon.
46
+ * Coordinators on other daemons should ignore it during drain.
47
+ * Absent on legacy events — treated as broadcast to any coordinator.
48
+ */
49
+ targetCoordinatorDaemonId?: string;
50
+ /**
51
+ * When set, this event is intended for a specific coordinator SESSION on the
52
+ * target daemon (the session that originally dispatched the work). PHASE 2 inject
53
+ * strict-matches the live coordinator by this session id so a sibling coordinator
54
+ * session on the same daemon does not receive another coordinator's completion.
55
+ * Absent on legacy / version-skewed events → daemon-level broadcast (no regression).
56
+ * Rides inside the event payload, so it survives the SQLite payload round-trip and
57
+ * the JSONL file without a dedicated column; it is NOT a drain-scoping key.
58
+ */
59
+ targetCoordinatorSessionId?: string;
60
+
61
+ // ─── v2 protocol envelope (B2a) — additive, populated at emit time ────────
62
+ // Stamped by queuePendingMeshCoordinatorEvent from the fields above plus an
63
+ // optional emit hint. A v1 reader that ignores these is unaffected (the v2
64
+ // shape is a strict superset). Absent → the event is a v1 event, treated as
65
+ // broadcast during rollout.
66
+ /** '2.0' once stamped. Absent on v1 events. */
67
+ protocolVersion?: typeof MESH_PROTOCOL_VERSION_V2;
68
+ /** Idempotency key (UUID). The receiver's authoritative dedup key (B3). */
69
+ eventId?: string;
70
+ /** Routing scope. Defaulted from the event name unless the emit hint overrides. */
71
+ scope?: MeshEventScope;
72
+ /** Identity of the coordinator that dispatched the work this event reports. */
73
+ dispatchedBy?: CoordinatorIdentity;
74
+ /** Present only for unicast scope: the coordinator this event is addressed to. */
75
+ intendedFor?: CoordinatorIdentity;
76
+ }
77
+
78
+ /**
79
+ * Optional emit-time hint passed to queuePendingMeshCoordinatorEvent so a call
80
+ * site can override the name-defaulted scope or supply richer coordinator
81
+ * identity (e.g. a coordinatorRunId the base event fields don't carry). Every
82
+ * field is optional; when omitted the stamp is derived entirely from the
83
+ * event's own targetCoordinatorDaemonId / targetCoordinatorSessionId. Kept
84
+ * separate from the event so existing single-arg callers are untouched.
85
+ */
86
+ export interface PendingEventEmitHint {
87
+ scope?: MeshEventScope;
88
+ /** Overrides the coordinator identity derived from the event's target fields. */
89
+ dispatchedBy?: CoordinatorIdentity;
90
+ /** Overrides the unicast target derived from the event's target fields. */
91
+ intendedFor?: CoordinatorIdentity;
92
+ /** coordinatorRunId to fold into the derived identity when the event lacks one. */
93
+ coordinatorRunId?: string;
94
+ }
95
+
96
+ const REFINE_TERMINAL_EVENTS = new Set(['refine:completed', 'refine:failed']);
97
+
98
+ /** Normalise a coordinator-daemon-id argument (single id, list, or undefined) into a
99
+ * de-duplicated list of non-empty strings, EXPANDED to every equivalent daemon-id
100
+ * form (bare `mach_X` ≡ `daemon_mach_X` ≡ `standalone_mach_X`).
101
+ *
102
+ * A coordinator resolves its own id through one path (status instanceId, the config-
103
+ * form node daemonId, or the bare machineId) but a worker stamps a completion's
104
+ * `coordinator_daemon_id` through another, so the two are routinely in DIFFERENT
105
+ * forms of the SAME machine. The scope filter is an exact-string match, so without
106
+ * expansion a `daemon_mach_X`-scoped completion is silently skipped by a coordinator
107
+ * that only knows itself as bare `mach_X` (the base-node completion-surface bug).
108
+ * Expanding here fixes every drain/peek/surface caller uniformly. The first ORIGINAL
109
+ * id stays at [0] so per-daemon JSONL file naming keeps its primary; expansion stays
110
+ * within one machine core so a different coordinator's events are never claimed. */
111
+ function normalizeCoordinatorDaemonIds(
112
+ coordinatorDaemonId?: string | null | ReadonlyArray<string>,
113
+ ): string[] {
114
+ return expandDaemonIdForms(coordinatorDaemonId);
115
+ }
116
+
117
+ // ─── B3a: drain-side v2 routing (accept-and-warn) ────────────────────────────
118
+ //
119
+ // Stage-1 of the v2 receive path. The drain scope is still primarily gated by the
120
+ // SQLite/JSONL `coordinator_daemon_id` filter (v1 mechanism, untouched here), so
121
+ // this layer runs on top of an already daemon-scoped candidate set and adds:
122
+ //
123
+ // 1. v2 unicast routing — an event whose intendedFor addresses a DIFFERENT
124
+ // coordinator on THIS daemon (a sibling CLI/MCP session) is skipped, so a
125
+ // completion doesn't cross-surface. Broadcast/system-as-broadcast pass.
126
+ // 2. eventId idempotency — a v2 event whose eventId was already drained is
127
+ // skipped (durable, via MeshRuntimeStore.hasDrainedEventId) plus a per-drain
128
+ // batch guard against same-batch duplicates.
129
+ // 3. accept-and-warn — a v2 event that FAILS validation, or has NO version, is
130
+ // NOT dropped: it passes through (no v1 regression) with a one-shot WARN and
131
+ // a counter bump. Hard rejection is T6 (enforce mode), not here.
132
+ // 4. re-attribution fallback — a unicast event whose intendedFor does not match
133
+ // the drainer by identity is NOT dropped when its intendedFor.daemonId is the
134
+ // SAME machine as the drainer (a coordinatorRunId change from a restart
135
+ // orphaned it): it is delivered to the current coordinator on that daemon.
136
+
137
+ /** Observability counters for the accept-and-warn rollout. Read by tests and (later,
138
+ * B4) surfaced in mesh_status. Process-lifetime totals — never reset in production. */
139
+ const meshV2DrainCounters = {
140
+ /** v2 events that passed validation and unicast/broadcast routing → delivered. */
141
+ v2Delivered: 0,
142
+ /** v2 unicast events skipped because intendedFor addressed another coordinator. */
143
+ v2RoutedAway: 0,
144
+ /** v2 events skipped because their eventId was already drained (idempotency). */
145
+ v2DedupSkipped: 0,
146
+ /** v2 events that failed assertPendingMeshCoordinatorEventV2 but were PASSED
147
+ * THROUGH (accept mode). Non-zero here is the rollout signal that a producer
148
+ * emits a malformed envelope. */
149
+ v2ValidationFailedAccepted: 0,
150
+ /** unicast events re-attributed to the drainer via daemon-core match (a
151
+ * coordinatorRunId change orphaned them). */
152
+ v2ReattributedToDrainer: 0,
153
+ /** v1 (unversioned) events passed through as broadcast (rollout baseline). */
154
+ v1BroadcastAccepted: 0,
155
+ };
156
+
157
+ /** Test/observability accessor for the v2 drain counters (snapshot copy). */
158
+ export function getMeshV2DrainCounters(): Readonly<typeof meshV2DrainCounters> {
159
+ return { ...meshV2DrainCounters };
160
+ }
161
+
162
+ /** Test helper: zero the v2 drain counters so a test starts from a clean slate. */
163
+ export function __resetMeshV2DrainCountersForTests(): void {
164
+ for (const k of Object.keys(meshV2DrainCounters) as Array<keyof typeof meshV2DrainCounters>) {
165
+ meshV2DrainCounters[k] = 0;
166
+ }
167
+ }
168
+
169
+ // One-shot WARN dedup: an accept-mode warning is logged once per (meshId, eventId)
170
+ // so a re-polled malformed event doesn't spam the log every 4s reconcile tick.
171
+ const warnedV2Violations = new Set<string>();
172
+ function warnV2Once(key: string, message: string): void {
173
+ if (warnedV2Violations.has(key)) return;
174
+ warnedV2Violations.add(key);
175
+ // Bound the set so a long-lived daemon churning many distinct eventIds can't leak.
176
+ if (warnedV2Violations.size > 2000) {
177
+ const first = warnedV2Violations.values().next().value;
178
+ if (first !== undefined) warnedV2Violations.delete(first);
179
+ }
180
+ LOG.warn('MeshEventsV2', message);
181
+ }
182
+
183
+ /** Test helper: clear the one-shot WARN dedup set. */
184
+ export function __resetMeshV2WarnDedupForTests(): void {
185
+ warnedV2Violations.clear();
186
+ }
187
+
188
+ /**
189
+ * Resolve the drainer's CoordinatorIdentity for v2 routing from the daemon-id
190
+ * argument the (untouchable) reconcile-loop already passes. The daemon ids are the
191
+ * dual/expanded self-identity forms from resolveCoordinatorDaemonIds; the FIRST is
192
+ * the primary. coordinatorRunId is not threaded through the drain call yet, so it
193
+ * falls back to the daemonId exactly as the emit side does
194
+ * (coordinatorIdentityFromEmitFields) — this keeps drain-side identity CONSISTENT
195
+ * with how v1→v2 events were stamped, and unicast equality then reduces to the
196
+ * daemon-core match, which is the correct rollout-window granularity. A caller that
197
+ * knows the full identity (with a real coordinatorRunId) may pass it explicitly to
198
+ * override. Returns undefined when no daemon id is known (→ v2 routing is a no-op,
199
+ * everything passes as-is).
200
+ */
201
+ function resolveDrainerIdentity(
202
+ daemonIds: ReadonlyArray<string>,
203
+ explicit?: CoordinatorIdentity,
204
+ ): CoordinatorIdentity | undefined {
205
+ if (explicit) return explicit;
206
+ return coordinatorIdentityFromEmitFields({ daemonId: daemonIds[0] });
207
+ }
208
+
209
+ /** A v2 event carries a '2.0' protocolVersion. Everything else is a v1 event. */
210
+ function isV2Event(event: PendingMeshCoordinatorEvent): boolean {
211
+ return event.protocolVersion === MESH_PROTOCOL_VERSION_V2;
212
+ }
213
+
214
+ /**
215
+ * True when an identity's coordinatorRunId is merely its own daemonId form (the
216
+ * B2a fallback in coordinatorIdentityFromEmitFields — no real coordinatorRunId was
217
+ * threaded through the emit/drain site yet). For such an identity the runId carries
218
+ * NO information beyond the daemon, so two different daemon-id FORMS of the same
219
+ * machine (mach_ vs daemon_mach_) must not be treated as different coordinators.
220
+ */
221
+ function runIdIsDaemonFormFallback(identity: CoordinatorIdentity): boolean {
222
+ return daemonIdsEquivalent(identity.coordinatorRunId, identity.daemonId);
223
+ }
224
+
225
+ /**
226
+ * Delivery equality for the rollout window. When BOTH sides carry only a
227
+ * daemon-form-fallback runId (no real coordinatorRunId wired yet), a match reduces
228
+ * to same-machine — so a completion stamped `daemon_mach_X` is delivered to a
229
+ * coordinator that knows itself as bare `mach_X` (the canon-identity heterogeneous-
230
+ * form case). The session is compared ONLY when BOTH sides carry one: a session-less
231
+ * drainer is a daemon-level drain (what the reconcile loop passes) that accepts any
232
+ * session's events on that machine — targetCoordinatorSessionId is a PHASE-2 inject
233
+ * key, not a drain-scoping key (see the v1 field comment). When only the drainer AND
234
+ * the event both name a session do we require them to match, so a session-specific
235
+ * coordinator does not receive a sibling session's unicast event.
236
+ *
237
+ * When EITHER side has a real (non-daemon-form) runId, fall back to strict
238
+ * coordinatorIdentityEquals so two genuinely distinct coordinators on the same
239
+ * daemon (different real runIds) stay separated.
240
+ */
241
+ function identityDeliversTo(intendedFor: CoordinatorIdentity, drainer: CoordinatorIdentity): boolean {
242
+ if (runIdIsDaemonFormFallback(intendedFor) && runIdIsDaemonFormFallback(drainer)) {
243
+ if (!daemonIdsEquivalent(intendedFor.daemonId, drainer.daemonId)) return false;
244
+ // Session filter applies only when the drainer itself is session-specific.
245
+ if (intendedFor.sessionId && drainer.sessionId) {
246
+ return intendedFor.sessionId === drainer.sessionId;
247
+ }
248
+ return true;
249
+ }
250
+ return coordinatorIdentityEquals(intendedFor, drainer);
251
+ }
252
+
253
+ /**
254
+ * Apply v2 receive-side routing + idempotency to a merged, already daemon-scoped
255
+ * candidate list (accept-and-warn — never drops on validation failure).
256
+ *
257
+ * - `drainer` undefined → routing is skipped, list returned unchanged (safety).
258
+ * - Marks each surviving v2 event's eventId in `batchSeen` so a same-batch dup is
259
+ * skipped; a caller that persists drains uses `alreadyDrained` for the durable
260
+ * check (getPending peek passes a no-op so a peek never dedups against itself).
261
+ */
262
+ function routeV2EventsForDrainer(
263
+ events: PendingMeshCoordinatorEvent[],
264
+ drainer: CoordinatorIdentity | undefined,
265
+ ctx: {
266
+ alreadyDrained: (eventId: string) => boolean;
267
+ batchSeen: Set<string>;
268
+ /** false for a non-destructive peek so the frequent status-poll path does
269
+ * not inflate the delivery counters (only the real drain counts). */
270
+ countMetrics: boolean;
271
+ },
272
+ ): PendingMeshCoordinatorEvent[] {
273
+ if (!drainer) return events;
274
+ const bump = (k: keyof typeof meshV2DrainCounters) => { if (ctx.countMetrics) meshV2DrainCounters[k]++; };
275
+ const kept: PendingMeshCoordinatorEvent[] = [];
276
+ for (const event of events) {
277
+ if (!isV2Event(event)) {
278
+ // v1 / unversioned event → broadcast during rollout (existing policy).
279
+ bump('v1BroadcastAccepted');
280
+ kept.push(event);
281
+ continue;
282
+ }
283
+
284
+ // Validate the v2 envelope. ACCEPT MODE: a validation failure does NOT drop
285
+ // the event — it passes through with a one-shot WARN + counter. (T6 enforce
286
+ // mode is where this becomes a quarantine.)
287
+ let validated: PendingMeshCoordinatorEventV2;
288
+ try {
289
+ validated = assertPendingMeshCoordinatorEventV2(event);
290
+ } catch (e: any) {
291
+ bump('v2ValidationFailedAccepted');
292
+ warnV2Once(
293
+ `${event.meshId}::${event.eventId ?? event.event}::invalid`,
294
+ `v2 envelope validation failed for ${event.event} on mesh ${event.meshId} — PASSED THROUGH (accept mode): ${e?.message || e}`,
295
+ );
296
+ kept.push(event);
297
+ continue;
298
+ }
299
+
300
+ // eventId idempotency: skip if already drained (durable) or already seen in
301
+ // this same batch (guards the SQLite+JSONL dual-store merge duplicate).
302
+ const eventId = validated.eventId;
303
+ if (ctx.batchSeen.has(eventId) || ctx.alreadyDrained(eventId)) {
304
+ bump('v2DedupSkipped');
305
+ continue;
306
+ }
307
+
308
+ // Broadcast → any coordinator; system → daemon handler only (never a
309
+ // coordinator). Delegates to the contract helper for those two scopes.
310
+ if (validated.scope !== 'unicast') {
311
+ if (shouldDeliverPendingEventToCoordinator(validated, drainer)) {
312
+ ctx.batchSeen.add(eventId);
313
+ bump('v2Delivered');
314
+ kept.push(event);
315
+ } else {
316
+ // system scope → not for any coordinator.
317
+ bump('v2RoutedAway');
318
+ }
319
+ continue;
320
+ }
321
+
322
+ // Unicast: deliver iff intendedFor addresses THIS drainer. identityDeliversTo
323
+ // treats a daemon-form-fallback runId (no real coordinatorRunId wired yet) as
324
+ // form-agnostic so a `daemon_mach_X`-addressed event reaches a bare-`mach_X`
325
+ // drainer (heterogeneous-form same coordinator), while keeping two REAL
326
+ // distinct runIds on one daemon separated.
327
+ if (validated.intendedFor && identityDeliversTo(validated.intendedFor, drainer)) {
328
+ ctx.batchSeen.add(eventId);
329
+ bump('v2Delivered');
330
+ kept.push(event);
331
+ continue;
332
+ }
333
+
334
+ // Not delivered by identity. Apply the re-attribution fallback (plan risk
335
+ // §4): if intendedFor addresses the SAME MACHINE as the drainer AND the
336
+ // mismatch is a genuine coordinatorRunId change (a restart minted a fresh
337
+ // runId), deliver it to the current coordinator rather than orphaning it.
338
+ //
339
+ // Guard: when BOTH sides carry only a daemon-form-fallback runId, a mismatch
340
+ // that survived identityDeliversTo is a SESSION mismatch (a sibling
341
+ // coordinator on the same daemon) — that is a legitimate route-away, NOT an
342
+ // orphaned event, so re-attribution must not fire. Re-attribution requires a
343
+ // REAL runId difference, which means at least one side carries a real runId.
344
+ const realRunIdMismatch = !runIdIsDaemonFormFallback(validated.intendedFor!)
345
+ || !runIdIsDaemonFormFallback(drainer);
346
+ if (
347
+ validated.intendedFor
348
+ && realRunIdMismatch
349
+ && daemonIdsEquivalent(validated.intendedFor.daemonId, drainer.daemonId)
350
+ ) {
351
+ ctx.batchSeen.add(eventId);
352
+ bump('v2ReattributedToDrainer');
353
+ warnV2Once(
354
+ `${event.meshId}::${eventId}::reattributed`,
355
+ `v2 unicast ${event.event} on mesh ${event.meshId} re-attributed to current coordinator ${coordinatorIdentityKey(drainer)} (originating coordinatorRunId no longer live)`,
356
+ );
357
+ kept.push(event);
358
+ continue;
359
+ }
360
+
361
+ // Addressed to a genuinely different coordinator (different machine, or
362
+ // system scope) → not for this drainer. Skipped (left for its own drainer).
363
+ bump('v2RoutedAway');
364
+ }
365
+ return kept;
366
+ }
367
+
368
+ export function readRefineJobId(event: { metadataEvent?: Record<string, unknown> } | Record<string, unknown>): string {
369
+ const metadata = readRecord((event as any).metadataEvent) || event as Record<string, unknown>;
370
+ const result = readRecord(metadata.result);
371
+ const refineJob = readRecord(result?.refineJob);
372
+ return readNonEmptyString(metadata.jobId) || readNonEmptyString(refineJob?.jobId);
373
+ }
374
+
375
+ function buildRefineTerminalEventFingerprint(meshId: string, eventName: string, metadataEvent: Record<string, unknown>): string {
376
+ const jobId = readRefineJobId({ metadataEvent });
377
+ return jobId && REFINE_TERMINAL_EVENTS.has(eventName) ? `${meshId}::${eventName}::${jobId}` : '';
378
+ }
379
+
380
+ function hasPendingRefineTerminalEventDuplicate(event: PendingMeshCoordinatorEvent): boolean {
381
+ if (!REFINE_TERMINAL_EVENTS.has(event.event)) return false;
382
+ const jobId = readRefineJobId(event);
383
+ if (!jobId) return false;
384
+ return readPendingMeshCoordinatorEventsFromDisk(event.meshId).some((pending) =>
385
+ pending.event === event.event && readRefineJobId(pending) === jobId,
386
+ );
387
+ }
388
+
389
+ // CANON-B / DUPNOTIF: terminal completion events that the coordinator surfaces as a
390
+ // notification. The native completion path (handleMeshCoordinatorEvent) and the transcript
391
+ // reconciliation fallback (reconcileDirectDispatchCompletionFromTranscript) BOTH queue one of
392
+ // these for the same finished task — with DIFFERENT timestamps — so a timestamp-bearing
393
+ // fingerprint lets both surface and the coordinator notifies twice. When the event carries a
394
+ // taskId we anchor the fingerprint on the taskId (dropping the timestamp), collapsing the two
395
+ // paths into a single surface. A weakness marker keeps a tentative false-idle completion
396
+ // distinct from the genuine completion that supersedes it, so the genuine one is never
397
+ // swallowed by the earlier weak one.
398
+ const TERMINAL_COMPLETION_EVENTS = new Set(['agent:generating_completed', 'agent:stopped']);
399
+
400
+ export function buildPendingEventFingerprint(event: PendingMeshCoordinatorEvent): string {
401
+ const metadata = readRecord(event.metadataEvent) || {};
402
+ // Bootstrap events are node-scoped: dedup by meshId+event+nodeId only.
403
+ // They carry no sessionId/taskId/timestamp — using those fields would produce
404
+ // an empty fingerprint that defeats dedup entirely.
405
+ if (event.event === 'worktree_bootstrap_complete' || event.event === 'worktree_bootstrap_failed') {
406
+ return [event.meshId, event.event, event.nodeId || ''].join('::');
407
+ }
408
+ // DUPNOTIF: a terminal completion carrying a taskId is deduped by taskId (+ weakness),
409
+ // NOT by timestamp — the native and transcript-reconciliation paths timestamp the same
410
+ // completion differently, and only taskId is stable across both.
411
+ if (TERMINAL_COMPLETION_EVENTS.has(event.event)) {
412
+ const terminalTaskId = readNonEmptyString(metadata.taskId) || readNonEmptyString(readRecord(metadata.payload)?.taskId);
413
+ if (terminalTaskId) {
414
+ return [
415
+ event.meshId,
416
+ event.event,
417
+ terminalTaskId,
418
+ isWeakCompletionMetadata(metadata) ? 'weak' : 'genuine',
419
+ ].join('::');
420
+ }
421
+ }
422
+ // MAGI consensus-group exemption: a consensusGroupId marks an INTENTIONAL
423
+ // same-prompt fan-out across N replicas — the exact opposite of the accidental
424
+ // duplicates this dedup collapses. Anchor the fingerprint on the unique
425
+ // (taskId, consensusGroupId) so grouped replicas can NEVER be collapsed by any
426
+ // future prompt-content-based tightening of this builder. Mirrors the
427
+ // bootstrap-event exemption above and serves as the explicit fan-out marker.
428
+ // (Today this is belt-and-suspenders: each replica already gets a distinct
429
+ // taskId, so the generic key below would not collapse them either.)
430
+ const consensusGroupId = readNonEmptyString(metadata.consensusGroupId)
431
+ || readNonEmptyString(readRecord(metadata.payload)?.consensusGroupId);
432
+ if (consensusGroupId) {
433
+ const groupTaskId = readNonEmptyString(metadata.taskId)
434
+ || readNonEmptyString(readRecord(metadata.payload)?.taskId);
435
+ return [event.meshId, event.event, groupTaskId || '', consensusGroupId, 'group'].join('::');
436
+ }
437
+ const sessionId = resolveEventSessionId(metadata);
438
+ const providerSessionId = readNonEmptyString(metadata.providerSessionId);
439
+ const taskId = readNonEmptyString(metadata.taskId) || readNonEmptyString(readRecord(metadata.payload)?.taskId);
440
+ const jobId = readRefineJobId(event);
441
+ const timestamp = metadata.timestamp !== undefined && metadata.timestamp !== null ? String(metadata.timestamp) : '';
442
+ return [
443
+ event.meshId,
444
+ event.event,
445
+ event.nodeId || '',
446
+ sessionId || '',
447
+ providerSessionId || '',
448
+ taskId || '',
449
+ jobId || '',
450
+ timestamp || '',
451
+ ].join('::');
452
+ }
453
+
454
+ // NOTE: the former R3 "direct-delivered" marker (markMeshCoordinatorEventDirectDelivered /
455
+ // wasDirectDeliveredToCoordinator) was removed when spontaneous PTY direct-inject was retired.
456
+ // Delivery is now queue-drain-only: an event is consumed by exactly one drainer via the atomic
457
+ // SQLite drained=1 marking, so there is no PTY-vs-poll double-delivery left to dedup against.
458
+
459
+ export function hasPendingCoordinatorEventDuplicate(event: PendingMeshCoordinatorEvent): boolean {
460
+ const fingerprint = buildPendingEventFingerprint(event);
461
+ if (!fingerprint.trim()) return false;
462
+ // Check SQLite inbox first (G3 primary path)
463
+ try {
464
+ if (MeshRuntimeStore.getInstance().hasPendingEventFingerprint(event.meshId, fingerprint)) return true;
465
+ } catch { /* fall through to JSONL check */ }
466
+ return readPendingMeshCoordinatorEventsFromDisk(event.meshId).some((pending) => buildPendingEventFingerprint(pending) === fingerprint);
467
+ }
468
+
469
+ function getPendingEventsPath(meshId: string, coordinatorDaemonId?: string): string {
470
+ const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, '_');
471
+ if (coordinatorDaemonId) {
472
+ const safeDaemon = coordinatorDaemonId.replace(/[^a-zA-Z0-9_-]/g, '_');
473
+ return join(getLedgerDir(), `${safe}-${safeDaemon}.pending-events.jsonl`);
474
+ }
475
+ return join(getLedgerDir(), `${safe}.pending-events.jsonl`);
476
+ }
477
+
478
+ function readPendingMeshCoordinatorEventsFromDisk(meshId?: string, coordinatorDaemonId?: string | ReadonlyArray<string>): PendingMeshCoordinatorEvent[] {
479
+ if (!meshId) return [];
480
+ const daemonIds = normalizeCoordinatorDaemonIds(coordinatorDaemonId);
481
+ const primaryDaemonId = daemonIds[0];
482
+ // Read coordinator-scoped file first; fall back to legacy shared file.
483
+ const paths = primaryDaemonId
484
+ ? [getPendingEventsPath(meshId, primaryDaemonId), getPendingEventsPath(meshId)]
485
+ : [getPendingEventsPath(meshId)];
486
+ const events: PendingMeshCoordinatorEvent[] = [];
487
+ for (const path of paths) {
488
+ if (!existsSync(path)) continue;
489
+ try {
490
+ const raw = readFileSync(path, 'utf-8');
491
+ const parsed = raw.split('\n').filter(Boolean).flatMap(line => {
492
+ try { return [JSON.parse(line) as PendingMeshCoordinatorEvent]; } catch { return []; }
493
+ });
494
+ // If reading the shared file, filter to events that target this coordinator or are unscoped.
495
+ const filtered = (primaryDaemonId && path === getPendingEventsPath(meshId))
496
+ ? parsed.filter(e => !e.targetCoordinatorDaemonId || daemonIds.includes(e.targetCoordinatorDaemonId))
497
+ : parsed;
498
+ events.push(...filtered);
499
+ } catch { /* skip unreadable files */ }
500
+ }
501
+ return events;
502
+ }
503
+
504
+ function refineTerminalEventFromLedger(meshId: string, pending: readonly PendingMeshCoordinatorEvent[]): PendingMeshCoordinatorEvent[] {
505
+ const acceptedJobIds = new Set(
506
+ pending
507
+ .filter(event => event.event === 'refine:accepted')
508
+ .map(event => readRefineJobId(event))
509
+ .filter(Boolean),
510
+ );
511
+ if (acceptedJobIds.size === 0) return [];
512
+ const existingTerminalJobIds = new Set(
513
+ pending
514
+ .filter(event => REFINE_TERMINAL_EVENTS.has(event.event))
515
+ .map(event => `${event.event}:${readRefineJobId(event)}`)
516
+ .filter(value => !value.endsWith(':')),
517
+ );
518
+ const backfilled: PendingMeshCoordinatorEvent[] = [];
519
+ const entries = readLedgerEntries(meshId, { tail: 200 });
520
+ for (let i = entries.length - 1; i >= 0; i--) {
521
+ const entry = entries[i];
522
+ if (entry.kind !== 'task_completed' && entry.kind !== 'task_failed') continue;
523
+ const payload = readRecord(entry.payload);
524
+ if (payload?.source !== 'refine_mesh_node_async_job') continue;
525
+ const refineJob = readRecord(payload.refineJob);
526
+ const jobId = readNonEmptyString(refineJob?.jobId);
527
+ if (!jobId || !acceptedJobIds.has(jobId)) continue;
528
+ const eventName = entry.kind === 'task_completed' ? 'refine:completed' : 'refine:failed';
529
+ if (existingTerminalJobIds.has(`${eventName}:${jobId}`)) continue;
530
+ existingTerminalJobIds.add(`${eventName}:${jobId}`);
531
+ const result = readRecord(payload.result);
532
+ const metadataEvent = {
533
+ source: 'refine_mesh_node_async_job',
534
+ jobId,
535
+ interactionId: readNonEmptyString(refineJob?.interactionId),
536
+ meshId,
537
+ nodeId: readNonEmptyString(refineJob?.nodeId) || entry.nodeId,
538
+ targetDaemonId: readNonEmptyString(refineJob?.targetDaemonId),
539
+ workspace: readNonEmptyString(refineJob?.workspace),
540
+ status: eventName === 'refine:completed' ? 'completed' : 'failed',
541
+ startedAt: readNonEmptyString(refineJob?.startedAt),
542
+ completedAt: readNonEmptyString(refineJob?.completedAt) || entry.timestamp,
543
+ retryOfJobId: readNonEmptyString(refineJob?.retryOfJobId) || readNonEmptyString(payload.retryOfJobId),
544
+ ...(result ? { result } : {}),
545
+ };
546
+ const nodeLabel = readNonEmptyString(refineJob?.nodeId) || entry.nodeId || 'refine job';
547
+ backfilled.push({
548
+ event: eventName,
549
+ meshId,
550
+ nodeLabel,
551
+ nodeId: readNonEmptyString(refineJob?.nodeId) || entry.nodeId,
552
+ workspace: readNonEmptyString(refineJob?.workspace),
553
+ metadataEvent,
554
+ coordinatorMessage: buildMeshSystemMessage({ event: eventName, nodeLabel, metadataEvent }),
555
+ queuedAt: Date.now(),
556
+ });
557
+ }
558
+ return backfilled.reverse();
559
+ }
560
+
561
+ function reconcilePendingMeshCoordinatorEvents(meshId: string, events: PendingMeshCoordinatorEvent[]): PendingMeshCoordinatorEvent[] {
562
+ const backfilled = refineTerminalEventFromLedger(meshId, events);
563
+ // A refine:accepted event is a provisional "job accepted, result to follow" signal.
564
+ // Once its terminal (completed/failed) counterpart for the same jobId exists — whether
565
+ // already direct-queued into the pending store OR backfilled from the ledger here — the
566
+ // accepted is superseded and is dropped so the coordinator isn't shown stale duplicate
567
+ // noise alongside the terminal outcome.
568
+ const terminalJobIds = new Set(
569
+ [...events.filter(event => REFINE_TERMINAL_EVENTS.has(event.event)), ...backfilled]
570
+ .map(event => readRefineJobId(event))
571
+ .filter(Boolean),
572
+ );
573
+ const reconciled = terminalJobIds.size === 0
574
+ ? events
575
+ : events.filter(event => !(event.event === 'refine:accepted' && terminalJobIds.has(readRefineJobId(event))));
576
+ return backfilled.length === 0 ? reconciled : [...reconciled, ...backfilled];
577
+ }
578
+
579
+ const MAX_PENDING_EVENTS_BYTES = 100 * 1024; // 100 KB — keep the pending file small
580
+ const MAX_PENDING_EVENTS_KEEP = 50; // keep the last 50 events when trimming
581
+
582
+ function trimPendingEventsIfNeeded(path: string): void {
583
+ try {
584
+ if (!existsSync(path)) return;
585
+ if (statSync(path).size <= MAX_PENDING_EVENTS_BYTES) return;
586
+ const lines = readFileSync(path, 'utf-8').split('\n').filter(Boolean);
587
+ if (lines.length <= MAX_PENDING_EVENTS_KEEP) return;
588
+ // C1 (data safety): this trim discards the OLDEST queued lines to keep the file
589
+ // bounded. An undelivered terminal completion among them would otherwise lose its
590
+ // worker summary silently (the JSONL is the only copy when the SQLite dual-write
591
+ // failed). Before dropping, mirror any meaningful (coordinator-facing / summary-
592
+ // bearing) dropped event into the ledger so it stays auditable and recoverable,
593
+ // and LOG.warn so the drop is observable instead of silent.
594
+ const dropped = lines.slice(0, lines.length - MAX_PENDING_EVENTS_KEEP);
595
+ for (const line of dropped) {
596
+ let event: PendingMeshCoordinatorEvent | undefined;
597
+ try { event = JSON.parse(line) as PendingMeshCoordinatorEvent; } catch { continue; }
598
+ if (!event || !event.meshId) continue;
599
+ const finalSummary = readMeshCompletionSummary(event.metadataEvent || {});
600
+ // "Meaningful" = would have been delivered to a coordinator (carries a message)
601
+ // or carries worker output worth preserving. Silent lifecycle events are not
602
+ // logged — losing them on trim is harmless (they re-drive nothing once stale).
603
+ if (!readNonEmptyString(event.coordinatorMessage) && !finalSummary) continue;
604
+ try {
605
+ appendLedgerEntry(event.meshId, {
606
+ kind: 'event_held',
607
+ ...(event.nodeId ? { nodeId: event.nodeId } : {}),
608
+ payload: {
609
+ event: event.event,
610
+ reason: 'pending_trim_dropped',
611
+ recoverable: true,
612
+ nodeLabel: event.nodeLabel,
613
+ ...(event.workspace ? { workspace: event.workspace } : {}),
614
+ targetCoordinatorDaemonId: event.targetCoordinatorDaemonId ?? null,
615
+ queuedAt: event.queuedAt,
616
+ ...(finalSummary ? { finalSummary } : {}),
617
+ },
618
+ });
619
+ LOG.warn('MeshEvents', `Pending-events trim dropping undelivered ${event.event} for mesh ${event.meshId} — recorded to ledger (recoverable)`);
620
+ } catch (e: any) {
621
+ LOG.warn('MeshEvents', `Failed to ledger-record trim-dropped ${event.event} for mesh ${event.meshId}: ${e?.message || e}`);
622
+ }
623
+ }
624
+ writeFileSync(path, lines.slice(-MAX_PENDING_EVENTS_KEEP).join('\n') + '\n', 'utf-8');
625
+ } catch { /* best-effort; if trim fails, append still proceeds */ }
626
+ }
627
+
628
+ /**
629
+ * Stamp the v2 protocol envelope onto a pending event at emit time (B2a).
630
+ *
631
+ * Non-breaking: returns a NEW event object with protocolVersion/eventId/scope/
632
+ * dispatchedBy/intendedFor added when a coordinator identity can be derived,
633
+ * otherwise returns the input unchanged (a v1 event, broadcast-treated during
634
+ * rollout). Identity and the unicast target are derived from the event's own
635
+ * targetCoordinatorDaemonId / targetCoordinatorSessionId (already carried by
636
+ * every producer), so most call sites need no change; the optional `hint`
637
+ * overrides scope/identity where a site knows better.
638
+ *
639
+ * The eventId is generated here (randomUUID) exactly once, so re-queues that
640
+ * pass an already-stamped event keep their original eventId — the idempotency
641
+ * key is stable across re-delivery. An already-stamped event is returned as-is.
642
+ */
643
+ export function stampPendingEventV2(
644
+ event: PendingMeshCoordinatorEvent,
645
+ hint?: PendingEventEmitHint,
646
+ ): PendingMeshCoordinatorEvent {
647
+ // Preserve idempotency across re-queues: never re-stamp an event that already
648
+ // carries a v2 eventId (mesh-reconcile-loop / flushPendingForMeshIdleCoordinators
649
+ // re-queue built events verbatim).
650
+ if (event.protocolVersion === MESH_PROTOCOL_VERSION_V2 && readNonEmptyString(event.eventId)) {
651
+ return event;
652
+ }
653
+
654
+ const dispatchedBy = hint?.dispatchedBy ?? coordinatorIdentityFromEmitFields({
655
+ daemonId: event.targetCoordinatorDaemonId,
656
+ coordinatorRunId: hint?.coordinatorRunId,
657
+ sessionId: event.targetCoordinatorSessionId,
658
+ });
659
+ // The unicast target is, by default, the same coordinator the event is already
660
+ // routed to (its originating coordinator). A hint may override it.
661
+ const intendedFor: CoordinatorIdentity | undefined = hint?.intendedFor ?? dispatchedBy;
662
+
663
+ const stamp = buildPendingEventEmitStamp({
664
+ eventName: event.event,
665
+ eventId: randomUUID(),
666
+ dispatchedBy,
667
+ intendedFor,
668
+ scope: hint?.scope,
669
+ });
670
+ if (!stamp) return event; // no coordinator identity → stays a v1 event
671
+
672
+ return {
673
+ ...event,
674
+ protocolVersion: stamp.protocolVersion,
675
+ eventId: stamp.eventId,
676
+ scope: stamp.scope,
677
+ dispatchedBy: stamp.dispatchedBy,
678
+ ...(stamp.intendedFor ? { intendedFor: stamp.intendedFor } : {}),
679
+ };
680
+ }
681
+
682
+ // ─── v2 envelope: remote (P2P) boundary preservation (B3b/T4) ─────────────
683
+ //
684
+ // The remote pull round-trip (mesh-reconcile-loop pullRemoteNodeQueues →
685
+ // get_pending_mesh_events → buildForwardPayloadFromPending → handleMeshForwardEvent
686
+ // → queuePendingMeshCoordinatorEvent) flattens a queued PendingMeshCoordinatorEvent
687
+ // into a flat wire payload and rebuilds it on the receiving daemon. The v2 envelope
688
+ // fields (protocolVersion / eventId / scope / dispatchedBy / intendedFor) live at the
689
+ // TOP LEVEL of the event, not inside metadataEvent, so the flatten/rebuild whitelist
690
+ // dropped them: the re-queue then re-stamped a FRESH eventId, breaking cross-machine
691
+ // idempotency and downgrading the relayed completion to v1 (broadcast) routing.
692
+ //
693
+ // These two helpers are the single serialization/deserialization pair for that
694
+ // boundary. serializeV2EnvelopeToWire copies the present v2 fields onto the flat
695
+ // payload; readV2EnvelopeFromWire validates and restores them for the re-queue. The
696
+ // eventId is carried verbatim so stampPendingEventV2's already-stamped short-circuit
697
+ // preserves it (no new UUID). Kept pure + exported so the round-trip is unit-testable.
698
+
699
+ /** Read a CoordinatorIdentity off an untrusted wire object, or undefined if malformed. */
700
+ function readCoordinatorIdentityFromWire(raw: unknown): CoordinatorIdentity | undefined {
701
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return undefined;
702
+ const obj = raw as Record<string, unknown>;
703
+ const daemonId = readNonEmptyString(obj.daemonId);
704
+ const coordinatorRunId = readNonEmptyString(obj.coordinatorRunId);
705
+ if (!daemonId || !coordinatorRunId) return undefined;
706
+ const sessionId = readNonEmptyString(obj.sessionId);
707
+ return { daemonId, coordinatorRunId, ...(sessionId ? { sessionId } : {}) };
708
+ }
709
+
710
+ /**
711
+ * Copy the v2 envelope fields that are present on `event` onto a flat wire
712
+ * payload. Only sets a field when it is present, so a v1 event contributes
713
+ * nothing (the payload stays v1-shaped and version-skew safe).
714
+ */
715
+ export function serializeV2EnvelopeToWire(event: PendingMeshCoordinatorEvent): Record<string, unknown> {
716
+ const out: Record<string, unknown> = {};
717
+ if (event.protocolVersion) out.protocolVersion = event.protocolVersion;
718
+ if (readNonEmptyString(event.eventId)) out.eventId = event.eventId;
719
+ if (event.scope) out.scope = event.scope;
720
+ if (event.dispatchedBy) out.dispatchedBy = event.dispatchedBy;
721
+ if (event.intendedFor) out.intendedFor = event.intendedFor;
722
+ return out;
723
+ }
724
+
725
+ /**
726
+ * Restore the v2 envelope fields from a flat wire payload for a re-queue. Only
727
+ * returns fields that survive validation; a payload missing/malforming a field
728
+ * yields a partial (or empty) object so the re-queue path stays v1-safe. The
729
+ * eventId is returned verbatim — its preservation is the idempotency guarantee.
730
+ */
731
+ export function readV2EnvelopeFromWire(payload: Record<string, unknown>): Partial<Pick<
732
+ PendingMeshCoordinatorEvent,
733
+ 'protocolVersion' | 'eventId' | 'scope' | 'dispatchedBy' | 'intendedFor'
734
+ >> {
735
+ const out: Partial<Pick<
736
+ PendingMeshCoordinatorEvent,
737
+ 'protocolVersion' | 'eventId' | 'scope' | 'dispatchedBy' | 'intendedFor'
738
+ >> = {};
739
+ if (payload.protocolVersion === MESH_PROTOCOL_VERSION_V2) out.protocolVersion = MESH_PROTOCOL_VERSION_V2;
740
+ const eventId = readNonEmptyString(payload.eventId);
741
+ if (eventId) out.eventId = eventId;
742
+ if (isMeshEventScope(payload.scope)) out.scope = payload.scope;
743
+ const dispatchedBy = readCoordinatorIdentityFromWire(payload.dispatchedBy);
744
+ if (dispatchedBy) out.dispatchedBy = dispatchedBy;
745
+ const intendedFor = readCoordinatorIdentityFromWire(payload.intendedFor);
746
+ if (intendedFor) out.intendedFor = intendedFor;
747
+ return out;
748
+ }
749
+
750
+ export function queuePendingMeshCoordinatorEvent(
751
+ rawEvent: PendingMeshCoordinatorEvent,
752
+ hint?: PendingEventEmitHint,
753
+ ): boolean {
754
+ // B2a: stamp the v2 envelope before dedup/persist so the eventId/scope ride
755
+ // into both stores and the fingerprint/dedup logic sees the final shape.
756
+ const event = stampPendingEventV2(rawEvent, hint);
757
+ try {
758
+ if (hasPendingRefineTerminalEventDuplicate(event)) {
759
+ LOG.info('MeshEvents', `Suppressed duplicate pending ${event.event} for refine job ${readRefineJobId(event)}`);
760
+ return true;
761
+ }
762
+ if (hasPendingCoordinatorEventDuplicate(event)) {
763
+ LOG.info('MeshEvents', `Suppressed duplicate pending ${event.event} for mesh ${event.meshId}`);
764
+ return true;
765
+ }
766
+
767
+ const fingerprint = buildPendingEventFingerprint(event);
768
+
769
+ // G3: Write to SQLite inbox (primary path going forward)
770
+ let sqliteOk = false;
771
+ try {
772
+ MeshRuntimeStore.getInstance().insertPendingEvent({
773
+ id: randomUUID(),
774
+ meshId: event.meshId,
775
+ coordinatorDaemonId: event.targetCoordinatorDaemonId ?? null,
776
+ event: event.event,
777
+ payload: event,
778
+ fingerprint: fingerprint || null,
779
+ queuedAt: event.queuedAt,
780
+ // v2 envelope columns (B2a) — all nullable so v1 rows coexist. The
781
+ // authoritative copy still rides inside `payload`; these columns exist
782
+ // for queryable idempotency (event_id) and scope-based drain filtering
783
+ // (scope / intended_for) without JSON-parsing every row.
784
+ protocolVersion: event.protocolVersion ?? null,
785
+ eventId: event.eventId ?? null,
786
+ scope: event.scope ?? null,
787
+ dispatchedBy: event.dispatchedBy ? JSON.stringify(event.dispatchedBy) : null,
788
+ intendedFor: event.intendedFor ? JSON.stringify(event.intendedFor) : null,
789
+ });
790
+ sqliteOk = true;
791
+ } catch {
792
+ // SQLite write failure is non-fatal; JSONL fallback below still works.
793
+ }
794
+
795
+ // Also write to JSONL (retained as legacy/export artifact). Best-effort once
796
+ // SQLite (the primary store) has the event: a JSONL append failure (disk full,
797
+ // permissions) must NOT report the whole persist as failed when SQLite holds it.
798
+ try {
799
+ const path = getPendingEventsPath(event.meshId, event.targetCoordinatorDaemonId);
800
+ trimPendingEventsIfNeeded(path);
801
+ appendFileSync(path, JSON.stringify(event) + '\n', 'utf-8');
802
+ } catch (e: any) {
803
+ if (!sqliteOk) throw e; // neither store has it — surface as a real failure
804
+ LOG.warn('MeshEvents', `JSONL append failed for mesh ${event.meshId}; SQLite holds the event: ${e?.message || e}`);
805
+ }
806
+ return true;
807
+ } catch (e: any) {
808
+ LOG.warn('MeshEvents', `Failed to persist pending coordinator event: ${e?.message || e}`);
809
+ return false;
810
+ }
811
+ }
812
+
813
+ // Atomically rename the file before reading so concurrent drains can't both consume
814
+ // the same events. renameSync is atomic on POSIX (same filesystem); only one caller
815
+ // wins the rename — the other gets ENOENT and returns null, preventing duplicate delivery.
816
+ function atomicDrainFile(path: string): string | null {
817
+ const tmpPath = `${path}.draining`;
818
+ try {
819
+ renameSync(path, tmpPath);
820
+ } catch {
821
+ return null; // another drain already renamed it, or file doesn't exist
822
+ }
823
+ try {
824
+ const content = readFileSync(tmpPath, 'utf-8');
825
+ try { unlinkSync(tmpPath); } catch { /* already cleaned up */ }
826
+ return content;
827
+ } catch {
828
+ try { unlinkSync(tmpPath); } catch { /* best-effort cleanup */ }
829
+ return null;
830
+ }
831
+ }
832
+
833
+ // Selectively drain a JSONL pending-events file: atomically claim it (rename), then
834
+ // consume only the lines whose parsed event matches `predicate` and rewrite the
835
+ // remaining (kept) lines back to the original path. Unparseable lines are kept
836
+ // untouched. Returns the consumed events. The rename makes claiming exclusive —
837
+ // only one concurrent caller wins, so there is no double-consume of the same lines.
838
+ function selectiveDrainFile(
839
+ path: string,
840
+ predicate: (event: PendingMeshCoordinatorEvent) => boolean,
841
+ ): PendingMeshCoordinatorEvent[] {
842
+ const tmpPath = `${path}.draining`;
843
+ try {
844
+ renameSync(path, tmpPath);
845
+ } catch {
846
+ return []; // another drain claimed it, or the file doesn't exist
847
+ }
848
+ let content: string;
849
+ try {
850
+ content = readFileSync(tmpPath, 'utf-8');
851
+ } catch {
852
+ try { unlinkSync(tmpPath); } catch { /* best-effort */ }
853
+ return [];
854
+ }
855
+
856
+ const consumed: PendingMeshCoordinatorEvent[] = [];
857
+ const keptLines: string[] = [];
858
+ for (const line of content.split('\n')) {
859
+ if (!line) continue;
860
+ let parsed: PendingMeshCoordinatorEvent | undefined;
861
+ try { parsed = JSON.parse(line) as PendingMeshCoordinatorEvent; } catch { parsed = undefined; }
862
+ if (parsed && predicate(parsed)) {
863
+ consumed.push(parsed);
864
+ } else {
865
+ keptLines.push(line); // non-matching or unparseable → leave queued
866
+ }
867
+ }
868
+
869
+ try {
870
+ if (keptLines.length > 0) {
871
+ writeFileSync(path, keptLines.join('\n') + '\n', 'utf-8');
872
+ }
873
+ unlinkSync(tmpPath);
874
+ } catch {
875
+ // If the rewrite/cleanup fails, restore the claimed file so no events are
876
+ // lost — the next drain retries the whole file.
877
+ try { if (existsSync(tmpPath) && !existsSync(path)) renameSync(tmpPath, path); } catch { /* best-effort */ }
878
+ return [];
879
+ }
880
+ return consumed;
881
+ }
882
+
883
+ /**
884
+ * Drain and return pending coordinator events for meshId, removing the drained
885
+ * ones from both the SQLite inbox and the JSONL legacy file.
886
+ *
887
+ * When `opts.onlyEvents` is supplied, ONLY events whose name is in that set are
888
+ * drained; every other event stays queued (undrained in SQLite, rewritten back to
889
+ * the JSONL file). The reconcile loop uses this to force-drain terminal/force-inject
890
+ * events into a *generating* coordinator while leaving non-force progress events for
891
+ * the coordinator's next idle transition. The atomic SQLite drained=1 marking and the
892
+ * atomic JSONL rename keep force-drain and a concurrent full drain from double-consuming.
893
+ */
894
+ export function drainPendingMeshCoordinatorEvents(
895
+ meshId?: string,
896
+ coordinatorDaemonId?: string | ReadonlyArray<string>,
897
+ opts?: { onlyEvents?: ReadonlySet<string>; drainerIdentity?: CoordinatorIdentity },
898
+ ): PendingMeshCoordinatorEvent[] {
899
+ if (!meshId) return [];
900
+
901
+ // A daemon may answer to more than one coordinator-id form (its canonical
902
+ // status id like `standalone_<machineId>` AND the bare machineId). Normalise
903
+ // to a list so both the SQLite IN-filter and the JSONL targeting predicate
904
+ // accept any of them.
905
+ const daemonIds = normalizeCoordinatorDaemonIds(coordinatorDaemonId);
906
+ const primaryDaemonId = daemonIds[0];
907
+
908
+ // B3a: the drainer's v2 identity, for unicast routing + eventId dedup. Derived
909
+ // from the daemon ids the (untouchable) reconcile-loop already passes, so no
910
+ // caller change is required; a caller may still pass the full identity.
911
+ const drainer = resolveDrainerIdentity(daemonIds, opts?.drainerIdentity);
912
+ // Snapshot the ALREADY-drained v2 eventIds BEFORE the SQLite drain marks this
913
+ // batch drained=1 — the re-delivery dedup baseline. Reading it after would
914
+ // self-match the batch's own rows.
915
+ let priorDrainedEventIds = new Set<string>();
916
+ try {
917
+ priorDrainedEventIds = MeshRuntimeStore.getInstance().drainedEventIdsForMesh(meshId);
918
+ } catch { /* store unavailable — no durable baseline; batch guard still applies */ }
919
+
920
+ const onlyEvents = opts?.onlyEvents;
921
+ const matchesFilter = (eventName: string): boolean => !onlyEvents || onlyEvents.has(eventName);
922
+
923
+ // Dual-write means SQLite and JSONL hold the same events. Both stores must be
924
+ // emptied in one drain call — draining only one leaves the other to re-deliver
925
+ // the same events on the next call. Merge with fingerprint dedup.
926
+ const merged: PendingMeshCoordinatorEvent[] = [];
927
+ const seenFingerprints = new Set<string>();
928
+ const pushUnique = (event: PendingMeshCoordinatorEvent) => {
929
+ const fingerprint = buildPendingEventFingerprint(event);
930
+ if (fingerprint.trim()) {
931
+ if (seenFingerprints.has(fingerprint)) return;
932
+ seenFingerprints.add(fingerprint);
933
+ }
934
+ merged.push(event);
935
+ };
936
+
937
+ // G3: SQLite inbox
938
+ try {
939
+ const store = MeshRuntimeStore.getInstance();
940
+ if (store.pendingEventCount(meshId) > 0) {
941
+ for (const row of store.drainPendingEvents(meshId, daemonIds.length > 0 ? daemonIds : undefined, onlyEvents ? { onlyEvents } : undefined)) {
942
+ const event = row.payload as PendingMeshCoordinatorEvent;
943
+ if (event) pushUnique(event);
944
+ }
945
+ }
946
+ } catch (e: any) {
947
+ // SQLite drain failed — JSONL below still drains. Surface it: a silent
948
+ // failure here means the JSONL copy is emptied while the SQLite rows
949
+ // survive undrained, so the next drain re-delivers the same events to the
950
+ // coordinator (duplicate refine:completed etc.) with no diagnostic trail.
951
+ LOG.warn('MeshEvents', `SQLite pending-event drain failed for mesh ${meshId}; JSONL fallback only: ${e?.message || e}`);
952
+ }
953
+
954
+ // JSONL (legacy / migration path) — always drained alongside SQLite.
955
+ // The scoped per-daemon file is keyed by a single id; use the primary. The
956
+ // shared (unscoped) file's targeting predicate accepts ANY of this daemon's ids.
957
+ const paths = primaryDaemonId
958
+ ? [getPendingEventsPath(meshId, primaryDaemonId), getPendingEventsPath(meshId)]
959
+ : [getPendingEventsPath(meshId)];
960
+ for (const path of paths) {
961
+ const isSharedFile = !!primaryDaemonId && path === getPendingEventsPath(meshId);
962
+ // Targeting predicate for the shared (unscoped) file: only this coordinator's
963
+ // events (or legacy untargeted ones) are eligible.
964
+ const targets = (e: PendingMeshCoordinatorEvent): boolean =>
965
+ !isSharedFile || !e.targetCoordinatorDaemonId || daemonIds.includes(e.targetCoordinatorDaemonId);
966
+
967
+ if (onlyEvents) {
968
+ // Selective JSONL drain: consume only matching events, rewrite the rest back.
969
+ for (const event of selectiveDrainFile(path, e => targets(e) && matchesFilter(e.event))) {
970
+ pushUnique(event);
971
+ }
972
+ continue;
973
+ }
974
+ const content = atomicDrainFile(path);
975
+ if (!content) continue;
976
+ const parsed = content.split('\n').filter(Boolean).flatMap(line => {
977
+ try { return [JSON.parse(line) as PendingMeshCoordinatorEvent]; } catch { return []; }
978
+ });
979
+ // If reading the shared file, filter to events that target this coordinator or are unscoped.
980
+ const filtered = isSharedFile ? parsed.filter(targets) : parsed;
981
+ for (const event of filtered) pushUnique(event);
982
+ }
983
+ if (merged.length === 0) return [];
984
+ // (Former R3 direct-delivered dedup removed.) Spontaneous PTY direct-inject no
985
+ // longer exists — delivery is now queue-drain-only (reconcile loop or MCP pull),
986
+ // so an event is consumed by exactly one drainer via the atomic SQLite drained=1
987
+ // marking. There is no PTY-vs-poll double path left to dedup against.
988
+ //
989
+ // B3a: v2 receive-side routing (accept-and-warn) — unicast targeting, eventId
990
+ // idempotency, malformed-envelope pass-through-with-warn. v1 events broadcast.
991
+ // Runs AFTER the merge/reconcile so a single eventId dedup batch covers both
992
+ // stores. Non-destructive to v1 behaviour when no drainer identity is known.
993
+ const routed = routeV2EventsForDrainer(merged, drainer, {
994
+ alreadyDrained: (eventId) => priorDrainedEventIds.has(eventId),
995
+ batchSeen: new Set<string>(),
996
+ countMetrics: true,
997
+ });
998
+ return reconcilePendingMeshCoordinatorEvents(meshId, routed);
999
+ }
1000
+
1001
+ /**
1002
+ * FALSE-BLOCKER-CLONE-QUEUE: retract any still-UNDELIVERED `mesh:dispatch_blocked`
1003
+ * actionable-skip event for a task whose blocker has since resolved (the task was
1004
+ * claimed, or its skip transitioned to a self-resolving transient reason). Without
1005
+ * this, a `target_node_id_unmatched` blocker paged during the brief clone/bootstrap
1006
+ * propagation window would linger in the coordinator's pending queue and surface as a
1007
+ * false "actionable blocker — will NOT clear on its own" even after the task dispatched.
1008
+ *
1009
+ * Only removes events that have NOT yet been drained/delivered to the coordinator — an
1010
+ * already-delivered message cannot be unsent, but de-dup re-arm (caller side) plus this
1011
+ * retraction guarantee no NEW stale blocker accumulates. Best-effort across both the
1012
+ * SQLite inbox and the JSONL legacy files (scoped + shared). Returns rows removed.
1013
+ */
1014
+ export function retractPendingDispatchBlockedEvent(
1015
+ meshId: string | undefined,
1016
+ taskId: string | undefined,
1017
+ coordinatorDaemonId?: string,
1018
+ ): number {
1019
+ if (!meshId || !taskId) return 0;
1020
+ let removed = 0;
1021
+ const matchesTask = (event: PendingMeshCoordinatorEvent | undefined): boolean => {
1022
+ if (!event || event.event !== 'mesh:dispatch_blocked') return false;
1023
+ const rowTaskId = readNonEmptyString((event.metadataEvent as Record<string, unknown> | undefined)?.taskId);
1024
+ return rowTaskId === taskId;
1025
+ };
1026
+
1027
+ // SQLite inbox: peek undrained rows for the mesh, hard-delete the matching ones by id.
1028
+ try {
1029
+ const store = MeshRuntimeStore.getInstance();
1030
+ const ids: string[] = [];
1031
+ for (const row of store.peekPendingEvents(meshId)) {
1032
+ if (row.event !== 'mesh:dispatch_blocked') continue;
1033
+ if (matchesTask(row.payload as PendingMeshCoordinatorEvent)) ids.push(row.id);
1034
+ }
1035
+ if (ids.length) removed += store.deletePendingEventsById(ids);
1036
+ } catch { /* best-effort — JSONL retraction below still runs */ }
1037
+
1038
+ // JSONL legacy files: selectively drop matching lines (rewrites the rest back).
1039
+ const daemonIds = normalizeCoordinatorDaemonIds(coordinatorDaemonId);
1040
+ const primaryDaemonId = daemonIds[0];
1041
+ const paths = primaryDaemonId
1042
+ ? [getPendingEventsPath(meshId, primaryDaemonId), getPendingEventsPath(meshId)]
1043
+ : [getPendingEventsPath(meshId)];
1044
+ for (const path of paths) {
1045
+ try {
1046
+ removed += selectiveDrainFile(path, matchesTask).length;
1047
+ } catch { /* best-effort */ }
1048
+ }
1049
+ return removed;
1050
+ }
1051
+
1052
+ /** Peek at pending coordinator events without draining (non-destructive). */
1053
+ export function getPendingMeshCoordinatorEvents(
1054
+ meshId?: string,
1055
+ coordinatorDaemonId?: string | ReadonlyArray<string>,
1056
+ opts?: { drainerIdentity?: CoordinatorIdentity },
1057
+ ): readonly PendingMeshCoordinatorEvent[] {
1058
+ if (!meshId) return [];
1059
+ const daemonIds = normalizeCoordinatorDaemonIds(coordinatorDaemonId);
1060
+ // B3a: same v2 routing the destructive drain applies, so a peek (mesh_status
1061
+ // count, reconcile pre-check) sees the SAME set the drain would deliver — a
1062
+ // unicast event for another coordinator is not counted for this one.
1063
+ const drainer = resolveDrainerIdentity(daemonIds, opts?.drainerIdentity);
1064
+ let priorDrainedEventIds = new Set<string>();
1065
+ try {
1066
+ priorDrainedEventIds = MeshRuntimeStore.getInstance().drainedEventIdsForMesh(meshId);
1067
+ } catch { /* store unavailable — batch guard still applies */ }
1068
+
1069
+ // Merge SQLite (primary) + JSONL (legacy) with fingerprint dedup.
1070
+ const merged: PendingMeshCoordinatorEvent[] = [];
1071
+ const seenFingerprints = new Set<string>();
1072
+ const pushUnique = (event: PendingMeshCoordinatorEvent) => {
1073
+ const fingerprint = buildPendingEventFingerprint(event);
1074
+ if (fingerprint.trim()) {
1075
+ if (seenFingerprints.has(fingerprint)) return;
1076
+ seenFingerprints.add(fingerprint);
1077
+ }
1078
+ merged.push(event);
1079
+ };
1080
+
1081
+ // G3: SQLite inbox (non-destructive peek at undrained rows)
1082
+ try {
1083
+ const store = MeshRuntimeStore.getInstance();
1084
+ if (store.pendingEventCount(meshId) > 0) {
1085
+ for (const row of store.peekPendingEvents(meshId, daemonIds.length > 0 ? daemonIds : undefined)) {
1086
+ const event = row.payload as PendingMeshCoordinatorEvent;
1087
+ if (event) pushUnique(event);
1088
+ }
1089
+ }
1090
+ } catch { /* SQLite unavailable — JSONL fallback below */ }
1091
+
1092
+ // JSONL (legacy)
1093
+ for (const event of readPendingMeshCoordinatorEventsFromDisk(meshId, daemonIds)) {
1094
+ pushUnique(event);
1095
+ }
1096
+
1097
+ // (Former R3 direct-delivered filter removed — no PTY direct-inject path exists
1098
+ // anymore, so a peeked pending event has genuinely not yet been consumed.)
1099
+ // B3a: apply the SAME v2 routing the drain applies (non-destructive: no counter
1100
+ // inflation on the frequent status-poll path).
1101
+ const routed = routeV2EventsForDrainer(merged, drainer, {
1102
+ alreadyDrained: (eventId) => priorDrainedEventIds.has(eventId),
1103
+ batchSeen: new Set<string>(),
1104
+ countMetrics: false,
1105
+ });
1106
+ return reconcilePendingMeshCoordinatorEvents(meshId, routed);
1107
+ }
1108
+
1109
+ /**
1110
+ * Test helper: purge all pending-event state for a mesh — SQLite rows
1111
+ * (including drained fingerprint history) and JSONL files.
1112
+ */
1113
+ export function __clearMeshPendingEventsForTests(meshId: string): void {
1114
+ try {
1115
+ MeshRuntimeStore.getInstance().clearPendingEventsForMesh(meshId);
1116
+ } catch { /* store unavailable — nothing to clear */ }
1117
+ clearPendingMeshCoordinatorEvents(meshId);
1118
+ }
1119
+
1120
+ /** Explicitly clear all pending coordinator events for a mesh (and coordinator if scoped). */
1121
+ export function clearPendingMeshCoordinatorEvents(meshId?: string, coordinatorDaemonId?: string): void {
1122
+ if (!meshId) return;
1123
+ // Clear SQLite rows
1124
+ try { MeshRuntimeStore.getInstance().clearPendingEventsForMesh(meshId); } catch { /* store unavailable */ }
1125
+ // Clear JSONL files
1126
+ const paths = coordinatorDaemonId
1127
+ ? [getPendingEventsPath(meshId, coordinatorDaemonId), getPendingEventsPath(meshId)]
1128
+ : [getPendingEventsPath(meshId)];
1129
+ for (const path of paths) {
1130
+ if (existsSync(path)) try { unlinkSync(path); } catch { /* already removed */ }
1131
+ }
1132
+ }