@adhdev/daemon-core 0.9.82-rc.31 → 0.9.82-rc.311

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 (294) hide show
  1. package/dist/boot/daemon-lifecycle.d.ts +8 -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 +23 -2
  8. package/dist/cli-adapters/cli-script-runner.d.ts +117 -0
  9. package/dist/cli-adapters/cli-state-engine.d.ts +198 -0
  10. package/dist/cli-adapters/provider-cli-adapter.d.ts +106 -63
  11. package/dist/cli-adapters/provider-cli-parse.d.ts +4 -0
  12. package/dist/cli-adapters/provider-cli-shared.d.ts +38 -0
  13. package/dist/cli-adapters/raw-terminal-io.d.ts +37 -0
  14. package/dist/cli-adapters/resolve-executable.d.ts +14 -0
  15. package/dist/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +6 -3
  16. package/dist/cli-adapters/terminal-backends/types.d.ts +10 -2
  17. package/dist/cli-adapters/terminal-screen.d.ts +4 -11
  18. package/dist/commands/cli-manager.d.ts +2 -1
  19. package/dist/commands/handler.d.ts +110 -0
  20. package/dist/commands/mesh-coordinator.d.ts +85 -1
  21. package/dist/commands/router.d.ts +214 -1
  22. package/dist/config/chat-history.d.ts +9 -0
  23. package/dist/config/config.d.ts +5 -0
  24. package/dist/config/mesh-config.d.ts +72 -1
  25. package/dist/git/git-commands.d.ts +9 -1
  26. package/dist/git/git-diff.d.ts +6 -0
  27. package/dist/git/git-executor.d.ts +11 -0
  28. package/dist/git/git-status.d.ts +9 -0
  29. package/dist/git/git-types.d.ts +2 -50
  30. package/dist/index.d.ts +51 -12
  31. package/dist/index.js +34705 -11938
  32. package/dist/index.js.map +1 -1
  33. package/dist/index.mjs +38412 -15757
  34. package/dist/index.mjs.map +1 -1
  35. package/dist/installer.d.ts +1 -4
  36. package/dist/ipc/local-ipc-server.d.ts +91 -0
  37. package/dist/launch.d.ts +1 -1
  38. package/dist/logging/async-batch-writer.d.ts +10 -0
  39. package/dist/mesh/contracts.d.ts +164 -0
  40. package/dist/mesh/coordinator-prompt.d.ts +36 -0
  41. package/dist/mesh/coordinator-registry.d.ts +59 -0
  42. package/dist/mesh/mesh-active-work.d.ts +108 -0
  43. package/dist/mesh/mesh-delivery-policy.d.ts +131 -0
  44. package/dist/mesh/mesh-events-coordinator.d.ts +169 -0
  45. package/dist/mesh/mesh-events-pending.d.ts +45 -0
  46. package/dist/mesh/mesh-events-stale.d.ts +40 -0
  47. package/dist/mesh/mesh-events-utils.d.ts +47 -0
  48. package/dist/mesh/mesh-events.d.ts +6 -49
  49. package/dist/mesh/mesh-fast-forward.d.ts +81 -0
  50. package/dist/mesh/mesh-host-ownership.d.ts +9 -0
  51. package/dist/mesh/mesh-init.d.ts +86 -0
  52. package/dist/mesh/mesh-ledger-reconciliation.d.ts +23 -3
  53. package/dist/mesh/mesh-ledger.d.ts +77 -1
  54. package/dist/mesh/mesh-missions.d.ts +116 -0
  55. package/dist/mesh/mesh-reconcile-loop.d.ts +7 -0
  56. package/dist/mesh/mesh-refine-batch.d.ts +68 -0
  57. package/dist/mesh/mesh-refine-status.d.ts +62 -0
  58. package/dist/mesh/mesh-review-inbox.d.ts +90 -0
  59. package/dist/mesh/mesh-routing.d.ts +70 -0
  60. package/dist/mesh/mesh-runtime-store.d.ts +395 -0
  61. package/dist/mesh/mesh-task-stats.d.ts +49 -0
  62. package/dist/mesh/mesh-unresolved-forward-outbox.d.ts +30 -0
  63. package/dist/mesh/mesh-work-queue.d.ts +204 -5
  64. package/dist/mesh/preview-freshness.d.ts +18 -0
  65. package/dist/mesh/refine-config.d.ts +215 -0
  66. package/dist/mesh/worktree-bootstrap-config.d.ts +135 -0
  67. package/dist/providers/acp-provider-instance.d.ts +2 -0
  68. package/dist/providers/approval-utils.d.ts +13 -0
  69. package/dist/providers/cli-provider-instance.d.ts +61 -3
  70. package/dist/providers/contracts.d.ts +130 -6
  71. package/dist/providers/external-sources.d.ts +71 -0
  72. package/dist/providers/native-history/antigravity-cli-transcript.d.ts +100 -0
  73. package/dist/providers/native-history/claude-cli-transcript.d.ts +70 -0
  74. package/dist/providers/native-history/codex-cli-transcript.d.ts +73 -0
  75. package/dist/providers/native-history/dispatcher.d.ts +27 -0
  76. package/dist/providers/native-history/hermes-cli-transcript.d.ts +30 -0
  77. package/dist/providers/native-history/index.d.ts +13 -0
  78. package/dist/providers/provider-instance-manager.d.ts +13 -0
  79. package/dist/providers/provider-instance.d.ts +13 -1
  80. package/dist/providers/provider-loader.d.ts +26 -4
  81. package/dist/providers/provider-trust.d.ts +31 -0
  82. package/dist/providers/read-chat-contract.d.ts +29 -0
  83. package/dist/providers/sdk/v1/builders/acp/detect-status.d.ts +68 -0
  84. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +85 -0
  85. package/dist/providers/sdk/v1/builders/cli/parse-approval-squash.d.ts +59 -0
  86. package/dist/providers/sdk/v1/builders/cli/parse-approval.d.ts +65 -0
  87. package/dist/providers/sdk/v1/builders/cli/parse-session.d.ts +91 -0
  88. package/dist/providers/sdk/v1/builders/cli/visible-region.d.ts +42 -0
  89. package/dist/providers/sdk/v1/fixture-tooling/format.d.ts +126 -0
  90. package/dist/providers/sdk/v1/fixture-tooling/index.d.ts +8 -0
  91. package/dist/providers/sdk/v1/fixture-tooling/replay.d.ts +38 -0
  92. package/dist/providers/sdk/v1/index.d.ts +30 -0
  93. package/dist/providers/sdk/v1/sandbox/README-design.d.ts +193 -0
  94. package/dist/providers/sdk/v1/sandbox/require-whitelist.d.ts +74 -0
  95. package/dist/providers/sdk/v1/sandbox/script-runner.d.ts +98 -0
  96. package/dist/providers/sdk/v1/types/cli/index.d.ts +277 -0
  97. package/dist/providers/sdk/v1/types/common/index.d.ts +203 -0
  98. package/dist/providers/sdk/v1/validators/index.d.ts +5 -0
  99. package/dist/providers/sdk/v1/validators/manifest.d.ts +40 -0
  100. package/dist/providers/sdk/v1/validators/taint.d.ts +52 -0
  101. package/dist/providers/spec/adapter.d.ts +59 -0
  102. package/dist/providers/spec/cli-adapter.d.ts +217 -0
  103. package/dist/providers/spec/evaluator.d.ts +23 -0
  104. package/dist/providers/spec/fsm-driver.d.ts +278 -0
  105. package/dist/providers/spec/fsm-evaluator.d.ts +72 -0
  106. package/dist/providers/spec/fsm-loader.d.ts +14 -0
  107. package/dist/providers/spec/fsm-types.d.ts +128 -0
  108. package/dist/providers/spec/native-history-executor.d.ts +41 -0
  109. package/dist/providers/spec/pre-launch-trust.d.ts +16 -0
  110. package/dist/providers/spec/route.d.ts +4 -0
  111. package/dist/providers/spec/types.d.ts +180 -0
  112. package/dist/providers/transcript-v2.d.ts +176 -0
  113. package/dist/providers/types/interactive-prompt.d.ts +84 -0
  114. package/dist/repo-mesh-types.d.ts +291 -13
  115. package/dist/sessions/registry.d.ts +6 -0
  116. package/dist/shared-types-extra.d.ts +2 -4
  117. package/dist/shared-types.d.ts +42 -1
  118. package/dist/status/normalize.d.ts +1 -1
  119. package/dist/status/normalize.js +1 -0
  120. package/dist/status/normalize.js.map +1 -1
  121. package/dist/status/normalize.mjs +1 -0
  122. package/dist/status/normalize.mjs.map +1 -1
  123. package/dist/status/reporter.d.ts +2 -0
  124. package/dist/status/snapshot.d.ts +1 -0
  125. package/dist/types.d.ts +5 -0
  126. package/package.json +7 -3
  127. package/src/agent-stream/poller.ts +2 -3
  128. package/src/boot/daemon-lifecycle.ts +55 -12
  129. package/src/boot/process-hardening.ts +89 -0
  130. package/src/build-info.ts +73 -0
  131. package/src/chat/source-machine.ts +534 -0
  132. package/src/chat/source-resolver.ts +0 -0
  133. package/src/chat/subscription-updates.ts +20 -1
  134. package/src/cli-adapter-types.d.ts +1 -0
  135. package/src/cli-adapter-types.ts +22 -2
  136. package/src/cli-adapters/cli-script-runner.ts +421 -0
  137. package/src/cli-adapters/cli-state-engine.ts +1215 -0
  138. package/src/cli-adapters/provider-cli-adapter.d.ts +1 -1
  139. package/src/cli-adapters/provider-cli-adapter.ts +674 -1139
  140. package/src/cli-adapters/provider-cli-parse.d.ts +1 -0
  141. package/src/cli-adapters/provider-cli-parse.ts +13 -0
  142. package/src/cli-adapters/provider-cli-runtime.ts +3 -1
  143. package/src/cli-adapters/provider-cli-shared.d.ts +2 -0
  144. package/src/cli-adapters/provider-cli-shared.ts +68 -11
  145. package/src/cli-adapters/pty-transport.ts +2 -1
  146. package/src/cli-adapters/raw-terminal-io.ts +252 -0
  147. package/src/cli-adapters/resolve-executable.ts +45 -0
  148. package/src/cli-adapters/session-host-transport.ts +2 -1
  149. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.ts +53 -29
  150. package/src/cli-adapters/terminal-backends/types.ts +10 -3
  151. package/src/cli-adapters/terminal-screen.ts +16 -81
  152. package/src/commands/chat-commands.ts +1822 -61
  153. package/src/commands/cli-manager.ts +286 -14
  154. package/src/commands/handler.ts +809 -2
  155. package/src/commands/mesh-coordinator.ts +332 -122
  156. package/src/commands/router.ts +6041 -589
  157. package/src/commands/stream-commands.ts +8 -0
  158. package/src/commands/upgrade-helper.ts +29 -0
  159. package/src/config/chat-history.ts +483 -24
  160. package/src/config/config.ts +12 -0
  161. package/src/config/mesh-config.ts +320 -4
  162. package/src/config/recent-activity.ts +8 -2
  163. package/src/daemon/dev-auto-implement.ts +3 -2
  164. package/src/daemon/dev-cli-debug.ts +10 -1
  165. package/src/detection/ide-detector.ts +26 -16
  166. package/src/git/git-commands.ts +42 -12
  167. package/src/git/git-diff.ts +53 -0
  168. package/src/git/git-executor.ts +12 -0
  169. package/src/git/git-status.ts +302 -18
  170. package/src/git/git-types.ts +14 -62
  171. package/src/git/git-worktree.ts +8 -1
  172. package/src/index.ts +142 -11
  173. package/src/installer.d.ts +1 -1
  174. package/src/installer.ts +8 -6
  175. package/src/ipc/local-ipc-server.ts +278 -0
  176. package/src/launch.d.ts +1 -1
  177. package/src/launch.ts +37 -28
  178. package/src/logging/async-batch-writer.ts +55 -0
  179. package/src/logging/logger.ts +2 -1
  180. package/src/mesh/contracts.ts +329 -0
  181. package/src/mesh/coordinator-prompt.ts +234 -31
  182. package/src/mesh/coordinator-registry.ts +121 -0
  183. package/src/mesh/mesh-active-work.ts +469 -0
  184. package/src/mesh/mesh-delivery-policy.ts +315 -0
  185. package/src/mesh/mesh-events-coordinator.ts +1942 -0
  186. package/src/mesh/mesh-events-pending.ts +508 -0
  187. package/src/mesh/mesh-events-stale.ts +290 -0
  188. package/src/mesh/mesh-events-utils.ts +254 -0
  189. package/src/mesh/mesh-events.ts +30 -1035
  190. package/src/mesh/mesh-fast-forward.ts +839 -0
  191. package/src/mesh/mesh-host-ownership.ts +73 -0
  192. package/src/mesh/mesh-init.ts +260 -0
  193. package/src/mesh/mesh-ledger-reconciliation.ts +12 -5
  194. package/src/mesh/mesh-ledger.ts +589 -102
  195. package/src/mesh/mesh-missions.ts +248 -0
  196. package/src/mesh/mesh-reconcile-loop.ts +466 -0
  197. package/src/mesh/mesh-refine-batch.ts +197 -0
  198. package/src/mesh/mesh-refine-status.ts +231 -0
  199. package/src/mesh/mesh-review-inbox.ts +307 -0
  200. package/src/mesh/mesh-routing.ts +291 -0
  201. package/src/mesh/mesh-runtime-store.ts +1565 -0
  202. package/src/mesh/mesh-task-stats.ts +154 -0
  203. package/src/mesh/mesh-unresolved-forward-outbox.ts +185 -0
  204. package/src/mesh/mesh-work-queue.ts +703 -140
  205. package/src/mesh/preview-freshness.ts +118 -0
  206. package/src/mesh/refine-config.ts +403 -0
  207. package/src/mesh/worktree-bootstrap-config.ts +326 -0
  208. package/src/providers/acp-provider-instance.ts +15 -1
  209. package/src/providers/approval-utils.d.ts +4 -0
  210. package/src/providers/approval-utils.ts +47 -5
  211. package/src/providers/chat-message-normalization.ts +4 -11
  212. package/src/providers/cli-provider-instance.ts +953 -87
  213. package/src/providers/contracts.d.ts +55 -0
  214. package/src/providers/contracts.ts +141 -6
  215. package/src/providers/extension-provider-instance.ts +5 -1
  216. package/src/providers/external-sources.ts +218 -0
  217. package/src/providers/ide-provider-instance.ts +25 -6
  218. package/src/providers/native-history/antigravity-cli-transcript.ts +643 -0
  219. package/src/providers/native-history/claude-cli-transcript.ts +396 -0
  220. package/src/providers/native-history/codex-cli-transcript.ts +479 -0
  221. package/src/providers/native-history/dispatcher.ts +340 -0
  222. package/src/providers/native-history/hermes-cli-transcript.ts +230 -0
  223. package/src/providers/native-history/index.ts +30 -0
  224. package/src/providers/provider-instance-manager.ts +30 -0
  225. package/src/providers/provider-instance.ts +10 -1
  226. package/src/providers/provider-loader.ts +584 -50
  227. package/src/providers/provider-schema.ts +87 -14
  228. package/src/providers/provider-trust.ts +114 -0
  229. package/src/providers/read-chat-contract.ts +76 -16
  230. package/src/providers/sdk/README.md +49 -0
  231. package/src/providers/sdk/v1/builders/acp/detect-status.ts +144 -0
  232. package/src/providers/sdk/v1/builders/cli/detect-status.ts +262 -0
  233. package/src/providers/sdk/v1/builders/cli/parse-approval-squash.ts +158 -0
  234. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +250 -0
  235. package/src/providers/sdk/v1/builders/cli/parse-session.ts +276 -0
  236. package/src/providers/sdk/v1/builders/cli/visible-region.ts +143 -0
  237. package/src/providers/sdk/v1/fixture-tooling/format.ts +130 -0
  238. package/src/providers/sdk/v1/fixture-tooling/index.ts +22 -0
  239. package/src/providers/sdk/v1/fixture-tooling/replay.ts +352 -0
  240. package/src/providers/sdk/v1/index.ts +152 -0
  241. package/src/providers/sdk/v1/sandbox/README-design.ts +195 -0
  242. package/src/providers/sdk/v1/sandbox/require-whitelist.ts +472 -0
  243. package/src/providers/sdk/v1/sandbox/script-runner.ts +150 -0
  244. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +504 -0
  245. package/src/providers/sdk/v1/schemas/primitives/acp-session-protocol-v1.json +131 -0
  246. package/src/providers/sdk/v1/schemas/primitives/native-history-codex-rollout-v1.json +66 -0
  247. package/src/providers/sdk/v1/schemas/primitives/tui-approval-squash-v1.json +91 -0
  248. package/src/providers/sdk/v1/schemas/primitives/tui-assistant-block-v1.json +91 -0
  249. package/src/providers/sdk/v1/schemas/primitives/tui-cue-ordering-v1.json +47 -0
  250. package/src/providers/sdk/v1/schemas/primitives/tui-dispatch-order-v1.json +32 -0
  251. package/src/providers/sdk/v1/schemas/primitives/tui-footer-chrome-v1.json +42 -0
  252. package/src/providers/sdk/v1/schemas/primitives/tui-index-finder-v1.json +27 -0
  253. package/src/providers/sdk/v1/schemas/primitives/tui-modal-v1.json +125 -0
  254. package/src/providers/sdk/v1/schemas/primitives/tui-prompt-marker-v1.json +45 -0
  255. package/src/providers/sdk/v1/schemas/primitives/tui-session-id-extraction-v1.json +46 -0
  256. package/src/providers/sdk/v1/schemas/primitives/tui-settled-prompt-v1.json +71 -0
  257. package/src/providers/sdk/v1/schemas/primitives/tui-spinner-v1.json +83 -0
  258. package/src/providers/sdk/v1/schemas/primitives/tui-transcript-pty-v1.json +83 -0
  259. package/src/providers/sdk/v1/schemas/primitives/tui-visible-region-v1.json +57 -0
  260. package/src/providers/sdk/v1/schemas/primitives/tui-welcome-screen-v1.json +35 -0
  261. package/src/providers/sdk/v1/types/cli/index.ts +365 -0
  262. package/src/providers/sdk/v1/types/common/index.ts +229 -0
  263. package/src/providers/sdk/v1/validators/index.ts +19 -0
  264. package/src/providers/sdk/v1/validators/manifest.ts +110 -0
  265. package/src/providers/sdk/v1/validators/taint.ts +309 -0
  266. package/src/providers/spec/adapter.ts +155 -0
  267. package/src/providers/spec/cli-adapter.ts +980 -0
  268. package/src/providers/spec/evaluator.ts +345 -0
  269. package/src/providers/spec/fsm-driver.ts +888 -0
  270. package/src/providers/spec/fsm-evaluator.ts +255 -0
  271. package/src/providers/spec/fsm-loader.ts +112 -0
  272. package/src/providers/spec/fsm-types.ts +222 -0
  273. package/src/providers/spec/native-history-executor.ts +1035 -0
  274. package/src/providers/spec/pre-launch-trust.ts +104 -0
  275. package/src/providers/spec/route.ts +51 -0
  276. package/src/providers/spec/types.ts +226 -0
  277. package/src/providers/transcript-v2.ts +567 -0
  278. package/src/providers/types/interactive-prompt.ts +536 -0
  279. package/src/providers/version-archive.ts +38 -20
  280. package/src/repo-mesh-types.ts +376 -13
  281. package/src/sessions/registry.ts +6 -0
  282. package/src/shared-types-extra.ts +2 -4
  283. package/src/shared-types.ts +45 -1
  284. package/src/status/builders.ts +26 -6
  285. package/src/status/normalize.ts +2 -0
  286. package/src/status/reporter.ts +15 -0
  287. package/src/status/snapshot.ts +84 -25
  288. package/src/system/host-memory.ts +29 -12
  289. package/src/types.ts +5 -0
  290. package/dist/cli-adapters/terminal-backends/xterm-backend.d.ts +0 -17
  291. package/dist/mesh/mesh-sync.d.ts +0 -53
  292. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +0 -16
  293. package/src/cli-adapters/terminal-backends/xterm-backend.ts +0 -97
  294. package/src/mesh/mesh-sync.ts +0 -111
@@ -1,28 +1,160 @@
1
- import { existsSync, writeFileSync, readFileSync, openSync, closeSync, unlinkSync } from 'fs';
2
- import { join } from 'path';
3
1
  import { randomUUID } from 'crypto';
4
- import { getLedgerDir } from './mesh-ledger.js';
2
+ import { requireMeshHostQueueOwner } from './mesh-host-ownership.js';
3
+ import type { RepoMeshDaemonRole } from '../repo-mesh-types.js';
4
+ import { MESH_CONVERGE_REFINE_TAG, resolveAutoConvergeCodeChange } from '../repo-mesh-types.js';
5
+ import { MeshRuntimeStore } from './mesh-runtime-store.js';
6
+ import { getMesh } from '../config/mesh-config.js';
5
7
 
6
8
  export type MeshTaskStatus = 'pending' | 'assigned' | 'completed' | 'failed' | 'cancelled';
7
9
  export type MeshActiveTaskStatus = Extract<MeshTaskStatus, 'pending' | 'assigned'>;
8
10
  export type MeshHistoricalTaskStatus = Extract<MeshTaskStatus, 'completed' | 'failed' | 'cancelled'>;
11
+ export type MeshTaskMode = 'code_change' | 'validation' | 'live_debug_readonly' | 'launch_app' | 'convergence';
9
12
 
10
13
  export const ACTIVE_MESH_QUEUE_STATUSES: MeshActiveTaskStatus[] = ['pending', 'assigned'];
11
14
  export const HISTORICAL_MESH_QUEUE_STATUSES: MeshHistoricalTaskStatus[] = ['completed', 'failed', 'cancelled'];
15
+ export const MESH_TASK_MODES: MeshTaskMode[] = ['code_change', 'validation', 'live_debug_readonly', 'launch_app', 'convergence'];
16
+
17
+ export interface MeshTaskModeValidationResult {
18
+ valid: boolean;
19
+ taskMode?: MeshTaskMode;
20
+ violations: string[];
21
+ allowedOperations?: string[];
22
+ }
23
+
24
+ const LIVE_DEBUG_READONLY_FORBIDDEN: Array<{ label: string; pattern: RegExp }> = [
25
+ { label: 'source_edit', pattern: /\b(edit|modify|patch|apply\s+patch|write\s+(?:to\s+)?(?:file|source)|overwrite|delete\s+file|remove\s+file|create\s+file|touch\s+file)\b/i },
26
+ { label: 'checkpoint', pattern: /\b(checkpoint|mesh_checkpoint)\b/i },
27
+ { label: 'deploy_or_version_bump', pattern: /\b(deploy|wrangler\s+deploy|version[-\s]?bump|npm\s+version|release|npm\s+publish|yarn\s+publish|pnpm\s+publish)\b/i },
28
+ { label: 'destructive_shell', pattern: /\b(rm\s+-rf|mv\s+\S+\s+\S+|truncate\s|tee\s+\S+|sed\s+-i|shred\b)\b/i },
29
+ { label: 'package_install', pattern: /\b(npm\s+(?:install|i|add|link|uninstall|remove)|yarn\s+(?:add|remove|link)|pnpm\s+(?:add|remove|link)|pip\s+install|brew\s+install|apt\s+install|cargo\s+install)\b/i },
30
+ { label: 'container_mutation', pattern: /\b(docker\s+(?:build|run|exec|push|tag|rmi|rm|create|start|stop|kill)|kubectl\s+(?:apply|delete|patch|replace|create|scale))\b/i },
31
+ ];
32
+
33
+ /**
34
+ * Git subcommands that mutate the working tree, index, refs, or remote.
35
+ * `stash` and `checkout` are intentionally absent here: they have read-only
36
+ * variants (`git stash list`/`show`, `git checkout-index`) and are classified
37
+ * token-by-token in {@link detectGitMutation} rather than by bare keyword.
38
+ */
39
+ const GIT_MUTATION_SUBCOMMANDS = new Set([
40
+ 'add', 'commit', 'push', 'reset', 'rebase', 'clean', 'switch', 'merge',
41
+ 'tag', 'restore', 'rm', 'mv', 'cherry-pick', 'revert', 'pull', 'fetch',
42
+ 'am', 'apply', 'gc', 'prune',
43
+ ]);
44
+
45
+ /**
46
+ * Read-only `git stash` variants. Any other `git stash <x>` (pop/apply/drop/
47
+ * push/save/clear, or bare `git stash` which defaults to push) is a mutation.
48
+ */
49
+ const GIT_STASH_READONLY_SUBCOMMANDS = new Set(['list', 'show']);
50
+
51
+ /**
52
+ * Detects a true git mutation in free-text task message, token-aware so that
53
+ * read-only diagnostics (`git stash list`, `git stash show --stat`,
54
+ * `git checkout-index`, `git status`, `git diff`, `git log`, ...) are allowed.
55
+ * Returns true only when a genuine mutating git invocation is present.
56
+ */
57
+ function detectGitMutation(message: string): boolean {
58
+ const re = /\bgit\s+([a-z][a-z0-9-]*)/gi;
59
+ let match: RegExpExecArray | null;
60
+ while ((match = re.exec(message)) !== null) {
61
+ const sub = match[1].toLowerCase();
62
+ if (GIT_MUTATION_SUBCOMMANDS.has(sub)) return true;
63
+ if (sub === 'stash') {
64
+ // Token following `git stash`; read-only only for list/show.
65
+ const after = message.slice(re.lastIndex).match(/^\s+([a-z][a-z0-9-]*)/i);
66
+ const next = after ? after[1].toLowerCase() : '';
67
+ if (!GIT_STASH_READONLY_SUBCOMMANDS.has(next)) return true; // bare stash = push, or pop/apply/drop/...
68
+ } else if (sub === 'checkout') {
69
+ // `git checkout <ref/path>` mutates; `git checkout-index` is matched
70
+ // as its own token by the regex (sub === 'checkout-index') and is read-only.
71
+ return true;
72
+ } else if (sub === 'submodule') {
73
+ // `git submodule update` mutates; `git submodule status` is read-only.
74
+ const after = message.slice(re.lastIndex).match(/^\s+([a-z][a-z0-9-]*)/i);
75
+ const next = after ? after[1].toLowerCase() : '';
76
+ if (next === 'update' || next === 'add' || next === 'sync' || next === 'deinit') return true;
77
+ } else if (sub === 'worktree') {
78
+ const after = message.slice(re.lastIndex).match(/^\s+([a-z][a-z0-9-]*)/i);
79
+ const next = after ? after[1].toLowerCase() : '';
80
+ if (next === 'add' || next === 'remove' || next === 'move' || next === 'prune') return true;
81
+ }
82
+ // checkout-index, stash-with-no-next-already-handled, status/diff/log/show/
83
+ // rev-parse/branch/submodule status fall through as read-only.
84
+ }
85
+ return false;
86
+ }
87
+
88
+ export function normalizeMeshTaskMode(value: unknown): MeshTaskMode | undefined {
89
+ if (typeof value !== 'string') return undefined;
90
+ const normalized = value.trim() as MeshTaskMode;
91
+ return (MESH_TASK_MODES as string[]).includes(normalized) ? normalized : undefined;
92
+ }
93
+
94
+ export function validateMeshTaskModeRequest(mode: unknown, message: string): MeshTaskModeValidationResult {
95
+ const taskMode = normalizeMeshTaskMode(mode);
96
+ if (!taskMode) {
97
+ return { valid: true, violations: [] };
98
+ }
99
+ if (taskMode !== 'live_debug_readonly') {
100
+ return { valid: true, taskMode, violations: [] };
101
+ }
102
+ const text = message || '';
103
+ const violations = LIVE_DEBUG_READONLY_FORBIDDEN
104
+ .filter(rule => rule.pattern.test(text))
105
+ .map(rule => rule.label);
106
+ if (detectGitMutation(text)) {
107
+ violations.push('git_mutation');
108
+ }
109
+ return {
110
+ valid: violations.length === 0,
111
+ taskMode,
112
+ violations,
113
+ allowedOperations: [
114
+ 'process/log/window/port/session inspection',
115
+ 'read-only filesystem listing/reading',
116
+ 'status probes and keep-running handle reporting',
117
+ 'diagnostic summaries without source edits, commits, checkpoints, pushes, deploys, resets, rebases, or destructive cleanups',
118
+ ],
119
+ };
120
+ }
12
121
 
13
122
  export interface MeshWorkQueueEntry {
14
123
  id: string;
15
124
  meshId: string;
16
125
  message: string;
17
126
  status: MeshTaskStatus;
127
+ taskMode?: MeshTaskMode;
18
128
  /** If specified, only this node can claim the task (used by legacy mesh_send_task) */
19
129
  targetNodeId?: string;
20
130
  /** If specified, only this runtime session can claim the task */
21
131
  targetSessionId?: string;
132
+ /** If specified, a node must expose all tags before it can claim the task. */
133
+ requiredTags?: string[];
134
+ /**
135
+ * M1: ids of tasks that must reach 'completed' before this task is claimable.
136
+ * Forward references (ids not yet enqueued) are allowed for batch flows and
137
+ * simply keep the task waiting until the referenced task exists and completes.
138
+ */
139
+ dependsOn?: string[];
140
+ /** M1/M3: mission this task belongs to (joins mesh_missions). */
141
+ missionId?: string;
142
+ /**
143
+ * M1: why this task is held back (e.g. "dependency_failed:<taskId>").
144
+ * Only set by the system on dependency failure under the 'block' policy;
145
+ * waiting-on-dependency state is computed at view time, not stored.
146
+ */
147
+ blockedReason?: string;
22
148
  /** The node that actually claimed and is executing the task */
23
149
  assignedNodeId?: string;
24
150
  /** The session currently executing the task */
25
151
  assignedSessionId?: string;
152
+ /**
153
+ * Provider type of the session that claimed the task. Recorded so the queue
154
+ * can enforce per-(node, provider) maxParallel caps (RepoMeshNodePolicy
155
+ * providerRoles) by counting active assignments grouped by node + provider.
156
+ */
157
+ assignedProviderType?: string;
26
158
  /** Human/operator reason for terminal cancellation. */
27
159
  cancelReason?: string;
28
160
  cancelledAt?: string;
@@ -30,6 +162,8 @@ export interface MeshWorkQueueEntry {
30
162
  requeueReason?: string;
31
163
  requeuedAt?: string;
32
164
  requeueCount?: number;
165
+ /** Max automatic requeue attempts. When requeueCount reaches this, task is auto-failed. */
166
+ maxRetries?: number;
33
167
  /** Last automatic queue session spin-up attempt, for mesh_view_queue/debug visibility. */
34
168
  autoLaunch?: {
35
169
  status: 'skipped' | 'started' | 'failed' | 'completed';
@@ -45,50 +179,207 @@ export interface MeshWorkQueueEntry {
45
179
  updatedAt: string;
46
180
  }
47
181
 
48
- function getQueuePath(meshId: string): string {
49
- const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, '_');
50
- return join(getLedgerDir(), `${safe}.queue.json`);
182
+ export interface MeshQueueMutationOptions {
183
+ ownerRole?: RepoMeshDaemonRole;
184
+ }
185
+
186
+ export function normalizeMeshCapabilityTags(value: unknown): string[] {
187
+ if (!Array.isArray(value)) return [];
188
+ const seen = new Set<string>();
189
+ return value
190
+ .map(tag => typeof tag === 'string' ? tag.trim() : '')
191
+ .filter(Boolean)
192
+ .filter(tag => {
193
+ if (seen.has(tag)) return false;
194
+ seen.add(tag);
195
+ return true;
196
+ });
51
197
  }
52
198
 
53
- function getLockPath(meshId: string): string {
54
- const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, '_');
55
- return join(getLedgerDir(), `${safe}.queue.lock`);
199
+ function firstProviderPriority(policy: unknown): string | undefined {
200
+ const raw = policy && typeof policy === 'object' && !Array.isArray(policy)
201
+ ? (policy as Record<string, unknown>).providerPriority
202
+ : undefined;
203
+ if (!Array.isArray(raw)) return undefined;
204
+ return raw.find(type => typeof type === 'string' && type.trim())?.trim();
56
205
  }
57
206
 
58
207
  /**
59
- * Simple advisory file lock using O_EXCL (atomic create) for queue mutations.
60
- * Retries up to 10 times at 30 ms intervals; proceeds without lock on timeout
61
- * to prevent deadlock (best-effort far better than no locking at all).
208
+ * Synthetic `role=<x>` capability tags advertised by a node's policy.providerRoles.
209
+ *
210
+ * When a specific `providerType` is being evaluated, only that provider's declared
211
+ * role is emitted — so role-based routing (requiredTags: ["role=validation"]) gates
212
+ * the *selected* provider through the ordinary capability-tag filter. When no
213
+ * provider is selected (node-level eligibility scan), every declared role is emitted
214
+ * so the node passes the filter if ANY of its providers could satisfy the role; the
215
+ * per-provider tag set then narrows it during provider selection.
216
+ *
217
+ * Roles are lowercased and deduped. Missing/empty providerRoles emits nothing, so a
218
+ * node that never declares roles advertises no `role=` tags and is therefore only
219
+ * matched by role-unconstrained tasks (full backward compatibility).
62
220
  */
63
- function withQueueLock<T>(meshId: string, fn: () => T): T {
64
- const lockPath = getLockPath(meshId);
65
- let fd = -1;
66
- for (let i = 0; i < 10; i++) {
67
- try { fd = openSync(lockPath, 'wx'); break; } catch {
68
- const deadline = Date.now() + 30;
69
- while (Date.now() < deadline) { /* spin */ }
70
- }
71
- }
72
- try { return fn(); } finally {
73
- if (fd !== -1) try { closeSync(fd); } catch { /* noop */ }
74
- try { unlinkSync(lockPath); } catch { /* already removed */ }
221
+ function roleCapabilityTags(policy: unknown, providerType: string | undefined): string[] {
222
+ const roles = policy && typeof policy === 'object' && !Array.isArray(policy)
223
+ ? (policy as Record<string, unknown>).providerRoles
224
+ : undefined;
225
+ if (!Array.isArray(roles)) return [];
226
+ const wantedProvider = typeof providerType === 'string' && providerType.trim()
227
+ ? providerType.trim().toLowerCase()
228
+ : '';
229
+ const out: string[] = [];
230
+ for (const entry of roles) {
231
+ if (!entry || typeof entry !== 'object') continue;
232
+ const type = typeof (entry as any).providerType === 'string'
233
+ ? (entry as any).providerType.trim().toLowerCase()
234
+ : '';
235
+ const role = typeof (entry as any).role === 'string'
236
+ ? (entry as any).role.trim().toLowerCase()
237
+ : '';
238
+ if (!role) continue;
239
+ // When narrowing to a selected provider, only emit that provider's role.
240
+ if (wantedProvider && type && type !== wantedProvider) continue;
241
+ out.push(`role=${role}`);
75
242
  }
243
+ return out;
76
244
  }
77
245
 
78
- function readQueue(meshId: string): MeshWorkQueueEntry[] {
79
- const path = getQueuePath(meshId);
80
- if (!existsSync(path)) return [];
246
+ export function buildMeshNodeCapabilityTags(
247
+ node: { capabilities?: unknown; policy?: unknown; isLocalWorktree?: unknown; worktreeBranch?: unknown } | undefined,
248
+ providerType?: string,
249
+ ): string[] {
250
+ const provider = typeof providerType === 'string' && providerType.trim()
251
+ ? providerType.trim()
252
+ : firstProviderPriority(node?.policy);
253
+ const worktreeBranch = typeof node?.worktreeBranch === 'string' && node.worktreeBranch.trim()
254
+ ? node.worktreeBranch.trim()
255
+ : null;
256
+ return normalizeMeshCapabilityTags([
257
+ ...(Array.isArray(node?.capabilities) ? node.capabilities : []),
258
+ `os=${process.platform}`,
259
+ `arch=${process.arch}`,
260
+ ...(provider ? [`provider=${provider}`] : []),
261
+ // Worktree nodes automatically expose a "worktree=<branch>" tag so that
262
+ // mesh_enqueue_task with required_tags: ["worktree=<branch>"] routes
263
+ // only to the matching worktree node.
264
+ ...(node?.isLocalWorktree === true && worktreeBranch ? [`worktree=${worktreeBranch}`] : []),
265
+ // Convergence routing: advertise how this node can land its work onto base.
266
+ // - converge=refine: local worktree nodes (on ANY machine — refine_mesh_node
267
+ // now forwards to the owning daemon) can run the Refinery merge → push →
268
+ // cleanup against their own checkout, so they accept code_change tasks.
269
+ // - converge=fast_forward: non-worktree nodes (the machine itself) can only
270
+ // ff/push an already-converged branch; they are NOT a destination for
271
+ // code_change work (a worktree is created first, and that worktree node
272
+ // receives the task instead). Reuses the ordinary required-tags filter —
273
+ // the load-balancing scheduler auto-injects converge=refine for code_change
274
+ // so such work is hard-filtered onto refine-capable nodes.
275
+ ...(node?.isLocalWorktree === true ? ['converge=refine'] : ['converge=fast_forward']),
276
+ // Role-based routing: advertise role=<x> for each (node, provider) role
277
+ // declared in policy.providerRoles. Narrowed to the selected provider when
278
+ // one is given so the chosen provider must match a task's required role;
279
+ // when no provider is selected, all declared roles are advertised for the
280
+ // node-level eligibility scan. Reuses the ordinary required-tags filter —
281
+ // no separate role field/gate.
282
+ ...roleCapabilityTags(node?.policy, providerType),
283
+ ]);
284
+ }
285
+
286
+ export function nodeSatisfiesRequiredTags(requiredTags: unknown, capabilityTags: unknown): boolean {
287
+ const required = normalizeMeshCapabilityTags(requiredTags);
288
+ if (required.length === 0) return true;
289
+ const available = new Set(normalizeMeshCapabilityTags(capabilityTags));
290
+ return required.every(tag => available.has(tag));
291
+ }
292
+
293
+ /**
294
+ * Convergence-aware required-tags resolution (load-balancing scheduler, opt-in).
295
+ *
296
+ * When the mesh enables policy.autoConvergeCodeChange, a `converge=refine` required
297
+ * tag is merged into a code_change task's required tags at enqueue time, so the
298
+ * scheduler hard-filters the task onto refine-capable worktree nodes only (on any
299
+ * machine — refine_mesh_node forwards to the owning daemon). Because the tag is
300
+ * persisted on the queue entry, BOTH the eligibility scan (maybeAutoLaunchOneQueueSession)
301
+ * and the claim transaction (claimNextQueueTask → nodeSatisfiesRequiredTags) enforce
302
+ * it consistently.
303
+ *
304
+ * Strict backward compatibility — the injection is skipped (returns the explicit tags
305
+ * unchanged) when ANY of:
306
+ * - the mesh does not opt in (autoConvergeCodeChange !== true), or
307
+ * - the task is not code_change (validation / live_debug_readonly / launch_app /
308
+ * convergence carry no merge cost and may run anywhere), or
309
+ * - the task is explicitly targeted (targetNodeId): the operator chose the node, so
310
+ * we do not second-guess it by filtering on convergence capability.
311
+ * Idempotent: normalizeMeshCapabilityTags dedupes, so re-injection is a no-op.
312
+ */
313
+ export function resolveConvergeRequiredTags(
314
+ meshId: string,
315
+ taskMode: MeshTaskMode | undefined,
316
+ explicitRequiredTags: string[],
317
+ opts?: { targetNodeId?: string },
318
+ ): string[] {
319
+ if (taskMode !== 'code_change') return explicitRequiredTags;
320
+ if (typeof opts?.targetNodeId === 'string' && opts.targetNodeId.trim()) return explicitRequiredTags;
321
+ let optedIn = false;
81
322
  try {
82
- const content = readFileSync(path, 'utf-8');
83
- return JSON.parse(content) as MeshWorkQueueEntry[];
323
+ optedIn = resolveAutoConvergeCodeChange(getMesh(meshId)?.policy as any);
84
324
  } catch {
85
- return [];
325
+ optedIn = false;
86
326
  }
327
+ if (!optedIn) return explicitRequiredTags;
328
+ return normalizeMeshCapabilityTags([...explicitRequiredTags, MESH_CONVERGE_REFINE_TAG]);
329
+ }
330
+
331
+ function withQueueLock<T>(_meshId: string, fn: () => T): T {
332
+ return MeshRuntimeStore.getInstance().transaction(fn);
333
+ }
334
+
335
+ function readQueue(meshId: string): MeshWorkQueueEntry[] {
336
+ return MeshRuntimeStore.getInstance().getQueueEntries(meshId);
87
337
  }
88
338
 
89
339
  function writeQueue(meshId: string, queue: MeshWorkQueueEntry[]): void {
90
- const path = getQueuePath(meshId);
91
- writeFileSync(path, JSON.stringify(queue, null, 2), 'utf-8');
340
+ MeshRuntimeStore.getInstance().replaceQueue(meshId, queue);
341
+ }
342
+
343
+ function normalizeDependsOn(value: unknown): string[] {
344
+ if (!Array.isArray(value)) return [];
345
+ const seen = new Set<string>();
346
+ return value
347
+ .map(id => typeof id === 'string' ? id.trim() : '')
348
+ .filter(Boolean)
349
+ .filter(id => {
350
+ if (seen.has(id)) return false;
351
+ seen.add(id);
352
+ return true;
353
+ });
354
+ }
355
+
356
+ /**
357
+ * M1: detect dependency cycles before enqueue. Walks the dependency graph of
358
+ * existing queue entries plus the new task's edges. Fail-closed: a cycle
359
+ * rejects the enqueue entirely. Synchronous and bounded by queue size.
360
+ */
361
+ export function assertNoDependencyCycle(meshId: string, newTaskId: string, dependsOn: string[]): void {
362
+ if (dependsOn.length === 0) return;
363
+ if (dependsOn.includes(newTaskId)) {
364
+ throw new Error(`dependency_cycle_detected: task '${newTaskId}' cannot depend on itself`);
365
+ }
366
+ const adjacency = new Map<string, string[]>();
367
+ for (const entry of readQueue(meshId)) {
368
+ adjacency.set(entry.id, normalizeDependsOn(entry.dependsOn));
369
+ }
370
+ adjacency.set(newTaskId, dependsOn);
371
+ // DFS from the new task: if we can reach newTaskId again, the edges form a cycle.
372
+ const stack = [...dependsOn];
373
+ const visited = new Set<string>();
374
+ while (stack.length > 0) {
375
+ const current = stack.pop()!;
376
+ if (current === newTaskId) {
377
+ throw new Error(`dependency_cycle_detected: task '${newTaskId}' is part of a dependency cycle via '${dependsOn.join(', ')}'`);
378
+ }
379
+ if (visited.has(current)) continue;
380
+ visited.add(current);
381
+ stack.push(...(adjacency.get(current) ?? []));
382
+ }
92
383
  }
93
384
 
94
385
  /**
@@ -97,22 +388,116 @@ function writeQueue(meshId: string, queue: MeshWorkQueueEntry[]): void {
97
388
  export function enqueueTask(
98
389
  meshId: string,
99
390
  message: string,
100
- opts?: { targetNodeId?: string; targetSessionId?: string }
391
+ opts?: {
392
+ targetNodeId?: string;
393
+ targetSessionId?: string;
394
+ taskMode?: MeshTaskMode | string;
395
+ requiredTags?: string[];
396
+ /** M1: tasks that must complete before this one is claimable. */
397
+ dependsOn?: string[];
398
+ /** M1/M3: mission this task belongs to. */
399
+ missionId?: string;
400
+ /** Explicit task id for batch/template flows (M5). Random UUID when omitted. */
401
+ id?: string;
402
+ } & MeshQueueMutationOptions,
101
403
  ): MeshWorkQueueEntry {
404
+ requireMeshHostQueueOwner(opts);
405
+ const modeValidation = validateMeshTaskModeRequest(opts?.taskMode, message);
406
+ if (!modeValidation.valid) {
407
+ throw new Error(`live_debug_readonly_guardrail_violation: forbidden operations (${modeValidation.violations.join(', ')})`);
408
+ }
409
+ const id = typeof opts?.id === 'string' && opts.id.trim() ? opts.id.trim() : randomUUID();
410
+ const dependsOn = normalizeDependsOn(opts?.dependsOn);
102
411
  return withQueueLock(meshId, () => {
103
- const queue = readQueue(meshId);
412
+ if (MeshRuntimeStore.getInstance().findQueueEntryById(meshId, id)) {
413
+ throw new Error(`duplicate_task_id: task '${id}' already exists in mesh '${meshId}'`);
414
+ }
415
+ assertNoDependencyCycle(meshId, id, dependsOn);
104
416
  const entry: MeshWorkQueueEntry = {
105
- id: randomUUID(),
417
+ id,
106
418
  meshId,
107
419
  message,
108
420
  status: 'pending',
421
+ taskMode: modeValidation.taskMode,
109
422
  targetNodeId: opts?.targetNodeId,
110
423
  targetSessionId: opts?.targetSessionId,
424
+ // Convergence routing (opt-in): auto-inject converge=refine for code_change
425
+ // tasks so they hard-filter onto refine-capable worktree nodes. No-op unless
426
+ // the mesh opts in; explicit target_node_id / required_tags are preserved.
427
+ requiredTags: resolveConvergeRequiredTags(
428
+ meshId,
429
+ modeValidation.taskMode,
430
+ normalizeMeshCapabilityTags(opts?.requiredTags),
431
+ { targetNodeId: opts?.targetNodeId },
432
+ ),
433
+ ...(dependsOn.length > 0 ? { dependsOn } : {}),
434
+ ...(typeof opts?.missionId === 'string' && opts.missionId.trim() ? { missionId: opts.missionId.trim() } : {}),
111
435
  createdAt: new Date().toISOString(),
112
436
  updatedAt: new Date().toISOString(),
113
437
  };
114
- queue.push(entry);
115
- writeQueue(meshId, queue);
438
+ MeshRuntimeStore.getInstance().insertQueueEntry(entry);
439
+ return entry;
440
+ });
441
+ }
442
+
443
+ /**
444
+ * Record a direct-dispatch task (mesh_send_task) as an already-assigned queue
445
+ * entry so it is attributable to a mission.
446
+ *
447
+ * Direct dispatch normally bypasses the queue entirely — the task lives only in
448
+ * the ledger + mesh_direct_dispatches table, neither of which carries a
449
+ * missionId, so {@link summarizeMissionTasks}/{@link computeMeshTaskStats}
450
+ * (which both scan the queue for `task.missionId`) count it as 0. When a
451
+ * mission is attached, we materialise the same queue entry shape an enqueued
452
+ * task would have, but pre-assigned to the dispatched node/session and stamped
453
+ * with the dispatch timestamp. The terminal event path (updateSessionTaskStatus
454
+ * → findAssignedBySession) then flips it to completed/failed exactly like a
455
+ * pulled task, so mission total + completed aggregates work with no extra wiring.
456
+ *
457
+ * Intentionally separate from {@link enqueueTask}: enqueue creates `pending`
458
+ * work for the queue to assign, whereas this records work already dispatched
459
+ * out-of-band. They share the missionId stamping rule and mode validation.
460
+ */
461
+ export function recordDirectDispatchTask(
462
+ meshId: string,
463
+ message: string,
464
+ opts: {
465
+ id: string;
466
+ missionId: string;
467
+ assignedNodeId?: string;
468
+ assignedSessionId?: string;
469
+ taskMode?: MeshTaskMode | string;
470
+ dispatchedAt?: string;
471
+ },
472
+ ): MeshWorkQueueEntry | null {
473
+ const missionId = typeof opts.missionId === 'string' ? opts.missionId.trim() : '';
474
+ if (!missionId) return null;
475
+ const taskId = typeof opts.id === 'string' ? opts.id.trim() : '';
476
+ if (!taskId) return null;
477
+ const modeValidation = validateMeshTaskModeRequest(opts.taskMode, message);
478
+ if (!modeValidation.valid) {
479
+ throw new Error(`live_debug_readonly_guardrail_violation: forbidden operations (${modeValidation.violations.join(', ')})`);
480
+ }
481
+ const now = opts.dispatchedAt && opts.dispatchedAt.trim() ? opts.dispatchedAt : new Date().toISOString();
482
+ return withQueueLock(meshId, () => {
483
+ if (MeshRuntimeStore.getInstance().findQueueEntryById(meshId, taskId)) {
484
+ // Already materialised (e.g. retry of the same dispatch) — leave it untouched.
485
+ return null;
486
+ }
487
+ const entry: MeshWorkQueueEntry = {
488
+ id: taskId,
489
+ meshId,
490
+ message,
491
+ status: 'assigned',
492
+ ...(modeValidation.taskMode ? { taskMode: modeValidation.taskMode } : {}),
493
+ missionId,
494
+ ...(opts.assignedNodeId ? { targetNodeId: opts.assignedNodeId, assignedNodeId: opts.assignedNodeId } : {}),
495
+ ...(opts.assignedSessionId ? { targetSessionId: opts.assignedSessionId, assignedSessionId: opts.assignedSessionId } : {}),
496
+ dispatchTimestamp: now,
497
+ createdAt: now,
498
+ updatedAt: now,
499
+ };
500
+ MeshRuntimeStore.getInstance().insertQueueEntry(entry);
116
501
  return entry;
117
502
  });
118
503
  }
@@ -121,43 +506,83 @@ export function enqueueTask(
121
506
  * Get all tasks in the queue, optionally filtered by status.
122
507
  */
123
508
  export function getQueue(meshId: string, opts?: { status?: MeshTaskStatus[] }): MeshWorkQueueEntry[] {
124
- let queue = readQueue(meshId);
125
- if (opts?.status?.length) {
126
- const statuses = new Set(opts.status);
127
- queue = queue.filter(q => statuses.has(q.status));
128
- }
129
- return queue;
509
+ return MeshRuntimeStore.getInstance().getQueueEntries(meshId, opts?.status?.length ? opts.status : undefined);
510
+ }
511
+
512
+ export function getMeshQueueRevision(meshId: string): string {
513
+ return MeshRuntimeStore.getInstance().getQueueRevision(meshId);
130
514
  }
131
515
 
132
516
  /**
133
517
  * Find the next pending task that this node is allowed to claim, and mark it as assigned.
518
+ *
519
+ * `opts.providerType` is stamped onto the claimed entry (assignedProviderType) so
520
+ * per-(node, provider) caps can be counted. `opts.providerMaxParallel`, when set,
521
+ * is the enforced per-(node, provider) cap from RepoMeshNodePolicy.providerRoles:
522
+ * a task is not assigned to this (node, provider) once it already has that many
523
+ * active assignments. This composes with the global/taskMode caps (stricter wins).
134
524
  */
135
- export function claimNextTask(meshId: string, nodeId: string, sessionId: string): MeshWorkQueueEntry | null {
136
- return withQueueLock(meshId, () => {
137
- const queue = readQueue(meshId);
138
- const hasActiveAssignment = queue.some(q => q.status === 'assigned' && (
139
- q.assignedSessionId === sessionId || q.assignedNodeId === nodeId
140
- ));
141
- if (hasActiveAssignment) return null;
142
- let targetIdx = queue.findIndex(q => q.status === 'pending' && q.targetSessionId === sessionId);
143
- if (targetIdx === -1) {
144
- targetIdx = queue.findIndex(q => q.status === 'pending' && q.targetNodeId === nodeId && !q.targetSessionId);
145
- }
146
- if (targetIdx === -1) {
147
- targetIdx = queue.findIndex(q => q.status === 'pending' && !q.targetNodeId && !q.targetSessionId);
525
+ export function claimNextTask(
526
+ meshId: string,
527
+ nodeId: string,
528
+ sessionId: string,
529
+ capabilityTags?: string[],
530
+ opts?: { providerType?: string; providerMaxParallel?: number },
531
+ ): MeshWorkQueueEntry | null {
532
+ return MeshRuntimeStore.getInstance().claimNextQueueTask(meshId, nodeId, sessionId, capabilityTags, opts);
533
+ }
534
+
535
+ // ─── M1: Dependency Failure Propagation ─────────
536
+
537
+ export type DependencyFailurePolicy = 'block' | 'cancel';
538
+
539
+ function resolveDependencyFailurePolicy(meshId: string): DependencyFailurePolicy {
540
+ try {
541
+ const policy = (getMesh(meshId)?.policy ?? {}) as Record<string, unknown>;
542
+ return policy.onDependencyFailure === 'cancel' ? 'cancel' : 'block';
543
+ } catch {
544
+ return 'block';
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Apply the mesh's onDependencyFailure policy to pending dependents of a task
550
+ * that just reached a failed/cancelled terminal state.
551
+ *
552
+ * - 'block' (default): dependents stay pending with blockedReason
553
+ * "dependency_failed:<taskId>" so an operator can requeue/cancel them.
554
+ * - 'cancel': dependents are cancelled (cascading to their own dependents).
555
+ *
556
+ * Must be called inside the queue lock of the triggering transition.
557
+ */
558
+ function propagateDependencyFailure(meshId: string, failedTaskId: string): void {
559
+ const policy = resolveDependencyFailurePolicy(meshId);
560
+ const store = MeshRuntimeStore.getInstance();
561
+ const frontier = [failedTaskId];
562
+ const seen = new Set<string>(frontier);
563
+ while (frontier.length > 0) {
564
+ const currentId = frontier.pop()!;
565
+ const dependents = store.getQueueEntries(meshId, ['pending'])
566
+ .filter(entry => Array.isArray(entry.dependsOn) && entry.dependsOn.includes(currentId));
567
+ for (const dependent of dependents) {
568
+ if (seen.has(dependent.id)) continue;
569
+ seen.add(dependent.id);
570
+ if (policy === 'cancel') {
571
+ dependent.status = 'cancelled';
572
+ dependent.cancelledAt = new Date().toISOString();
573
+ dependent.cancelReason = `dependency_failed:${currentId}`;
574
+ store.updateQueueEntry(dependent);
575
+ frontier.push(dependent.id); // cascade to transitive dependents
576
+ } else {
577
+ dependent.blockedReason = `dependency_failed:${currentId}`;
578
+ store.updateQueueEntry(dependent);
579
+ }
148
580
  }
149
- if (targetIdx === -1) return null;
150
- const entry = queue[targetIdx];
151
- entry.status = 'assigned';
152
- entry.assignedNodeId = nodeId;
153
- entry.assignedSessionId = sessionId;
154
- entry.dispatchTimestamp = new Date().toISOString();
155
- entry.updatedAt = new Date().toISOString();
156
- writeQueue(meshId, queue);
157
- return entry;
158
- });
581
+ }
159
582
  }
160
583
 
584
+ const DEPENDENCY_FAILURE_TERMINALS = new Set<MeshTaskStatus>(['failed', 'cancelled']);
585
+
161
586
  /**
162
587
  * Update the status of a specific task.
163
588
  * Used when a session completes, fails, or stalls.
@@ -166,15 +591,16 @@ export function updateTaskStatus(
166
591
  meshId: string,
167
592
  taskId: string,
168
593
  status: MeshTaskStatus,
594
+ opts?: MeshQueueMutationOptions,
169
595
  ): MeshWorkQueueEntry | null {
596
+ requireMeshHostQueueOwner(opts);
170
597
  return withQueueLock(meshId, () => {
171
- const queue = readQueue(meshId);
172
- const idx = queue.findIndex(q => q.id === taskId);
173
- if (idx === -1) return null;
174
- queue[idx].status = status;
175
- queue[idx].updatedAt = new Date().toISOString();
176
- writeQueue(meshId, queue);
177
- return queue[idx];
598
+ const entry = MeshRuntimeStore.getInstance().findQueueEntryById(meshId, taskId);
599
+ if (!entry) return null;
600
+ entry.status = status;
601
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
602
+ if (DEPENDENCY_FAILURE_TERMINALS.has(status)) propagateDependencyFailure(meshId, taskId);
603
+ return entry;
178
604
  });
179
605
  }
180
606
 
@@ -184,14 +610,12 @@ export function recordTaskAutoLaunch(
184
610
  autoLaunch: Omit<NonNullable<MeshWorkQueueEntry['autoLaunch']>, 'updatedAt'>,
185
611
  ): MeshWorkQueueEntry | null {
186
612
  return withQueueLock(meshId, () => {
187
- const queue = readQueue(meshId);
188
- const idx = queue.findIndex(q => q.id === taskId);
189
- if (idx === -1) return null;
613
+ const entry = MeshRuntimeStore.getInstance().findQueueEntryById(meshId, taskId);
614
+ if (!entry) return null;
190
615
  const now = new Date().toISOString();
191
- queue[idx].autoLaunch = { ...autoLaunch, updatedAt: now };
192
- queue[idx].updatedAt = now;
193
- writeQueue(meshId, queue);
194
- return queue[idx];
616
+ entry.autoLaunch = { ...autoLaunch, updatedAt: now };
617
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
618
+ return entry;
195
619
  });
196
620
  }
197
621
 
@@ -201,19 +625,19 @@ export function recordTaskAutoLaunch(
201
625
  export function cancelTask(
202
626
  meshId: string,
203
627
  taskId: string,
204
- opts?: { reason?: string },
628
+ opts?: { reason?: string } & MeshQueueMutationOptions,
205
629
  ): MeshWorkQueueEntry | null {
630
+ requireMeshHostQueueOwner(opts);
206
631
  return withQueueLock(meshId, () => {
207
- const queue = readQueue(meshId);
208
- const idx = queue.findIndex(q => q.id === taskId);
209
- if (idx === -1) return null;
632
+ const entry = MeshRuntimeStore.getInstance().findQueueEntryById(meshId, taskId);
633
+ if (!entry) return null;
210
634
  const now = new Date().toISOString();
211
- queue[idx].status = 'cancelled';
212
- queue[idx].updatedAt = now;
213
- queue[idx].cancelledAt = now;
214
- if (opts?.reason) queue[idx].cancelReason = opts.reason;
215
- writeQueue(meshId, queue);
216
- return queue[idx];
635
+ entry.status = 'cancelled';
636
+ entry.cancelledAt = now;
637
+ if (opts?.reason) entry.cancelReason = opts.reason;
638
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
639
+ propagateDependencyFailure(meshId, taskId);
640
+ return entry;
217
641
  });
218
642
  }
219
643
 
@@ -221,6 +645,11 @@ export function cancelTask(
221
645
  * Return a queue task to pending for retry. By default, dead session targeting
222
646
  * and assigned ownership are cleared so stale assignments do not strand again.
223
647
  */
648
+ export type RequeueResult =
649
+ | { status: 'requeued'; entry: MeshWorkQueueEntry }
650
+ | { status: 'failed_max_retries'; entry: MeshWorkQueueEntry; maxRetries: number; requeueCount: number }
651
+ | { status: 'not_found' };
652
+
224
653
  export function requeueTask(
225
654
  meshId: string,
226
655
  taskId: string,
@@ -230,15 +659,34 @@ export function requeueTask(
230
659
  targetSessionId?: string;
231
660
  clearTargetNode?: boolean;
232
661
  clearTargetSession?: boolean;
233
- },
662
+ /**
663
+ * Override the retry cap for this call. Use only for explicit operator actions.
664
+ * If true, the task is requeued even when requeueCount >= maxRetries.
665
+ */
666
+ force?: boolean;
667
+ /** Per-task retry cap override. Falls back to mesh policy maxTaskRetries (default 1). */
668
+ maxRetries?: number;
669
+ } & MeshQueueMutationOptions,
234
670
  ): MeshWorkQueueEntry | null {
671
+ requireMeshHostQueueOwner(opts);
235
672
  return withQueueLock(meshId, () => {
236
- const queue = readQueue(meshId);
237
- const idx = queue.findIndex(q => q.id === taskId);
238
- if (idx === -1) return null;
239
- const entry = queue[idx];
240
- const now = new Date().toISOString();
673
+ const entry = MeshRuntimeStore.getInstance().findQueueEntryById(meshId, taskId);
674
+ if (!entry) return null;
675
+ const currentCount = entry.requeueCount || 0;
676
+ const maxRetries = opts?.maxRetries ?? entry.maxRetries ?? 1;
677
+ if (!opts?.force && currentCount >= maxRetries) {
678
+ // Auto-fail: cap exceeded without explicit force override.
679
+ entry.status = 'failed';
680
+ entry.cancelReason = `max_retries_exceeded: requeued ${currentCount} time(s), limit is ${maxRetries}`;
681
+ entry.updatedAt = new Date().toISOString();
682
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
683
+ propagateDependencyFailure(meshId, taskId);
684
+ return entry;
685
+ }
241
686
  entry.status = 'pending';
687
+ // Operator requeue clears a dependency-failure block — the operator is
688
+ // explicitly overriding the held-back state.
689
+ delete entry.blockedReason;
242
690
  delete entry.assignedNodeId;
243
691
  delete entry.assignedSessionId;
244
692
  delete entry.cancelledAt;
@@ -247,11 +695,10 @@ export function requeueTask(
247
695
  if (typeof opts?.targetNodeId === 'string') entry.targetNodeId = opts.targetNodeId;
248
696
  if (opts?.clearTargetSession !== false) delete entry.targetSessionId;
249
697
  if (typeof opts?.targetSessionId === 'string') entry.targetSessionId = opts.targetSessionId;
250
- entry.updatedAt = now;
251
- entry.requeuedAt = now;
252
- entry.requeueCount = (entry.requeueCount || 0) + 1;
698
+ entry.requeuedAt = new Date().toISOString();
699
+ entry.requeueCount = currentCount + 1;
253
700
  if (opts?.reason) entry.requeueReason = opts.reason;
254
- writeQueue(meshId, queue);
701
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
255
702
  return entry;
256
703
  });
257
704
  }
@@ -266,25 +713,42 @@ export function updateSessionTaskStatus(
266
713
  opts?: { occurredAt?: string },
267
714
  ): MeshWorkQueueEntry | null {
268
715
  return withQueueLock(meshId, () => {
269
- const queue = readQueue(meshId);
270
- const occurredAtTime = opts?.occurredAt ? new Date(opts.occurredAt).getTime() : Number.NaN;
271
- const hasOccurredAt = Number.isFinite(occurredAtTime);
272
- let bestIdx = -1;
273
- let bestTime = 0;
274
- for (let i = queue.length - 1; i >= 0; i--) {
275
- if (queue[i].assignedSessionId !== sessionId || queue[i].status !== 'assigned') continue;
276
- const time = new Date(queue[i].dispatchTimestamp || queue[i].updatedAt).getTime();
277
- if (hasOccurredAt && Number.isFinite(time) && time > occurredAtTime) continue;
278
- if (time > bestTime) { bestTime = time; bestIdx = i; }
279
- }
280
- if (bestIdx === -1) return null;
281
- queue[bestIdx].status = status;
282
- queue[bestIdx].updatedAt = new Date().toISOString();
283
- writeQueue(meshId, queue);
284
- return queue[bestIdx];
716
+ const occurredAtIso = opts?.occurredAt ? new Date(opts.occurredAt).toISOString() : undefined;
717
+ const entry = MeshRuntimeStore.getInstance().findAssignedBySession(meshId, sessionId, occurredAtIso);
718
+ if (!entry) return null;
719
+ entry.status = status;
720
+ MeshRuntimeStore.getInstance().updateQueueEntry(entry);
721
+ if (DEPENDENCY_FAILURE_TERMINALS.has(status)) propagateDependencyFailure(meshId, entry.id);
722
+ return entry;
285
723
  });
286
724
  }
287
725
 
726
+ /**
727
+ * M1-3: true when at least one pending task is waiting on the given task.
728
+ * Used by the completion event path to decide whether to wake the queue.
729
+ */
730
+ export function hasPendingDependents(meshId: string, taskId: string): boolean {
731
+ return MeshRuntimeStore.getInstance().getQueueEntries(meshId, ['pending'])
732
+ .some(entry => Array.isArray(entry.dependsOn) && entry.dependsOn.includes(taskId));
733
+ }
734
+
735
+ /**
736
+ * M1-4: view-time dependency state for a task — unmet dependency ids and
737
+ * whether the task is currently claimable from a dependency standpoint.
738
+ * Not stored (truth stays in task statuses).
739
+ */
740
+ export function describeTaskDependencyState(
741
+ entry: Pick<MeshWorkQueueEntry, 'dependsOn' | 'blockedReason'>,
742
+ statusById: Map<string, MeshTaskStatus | string>,
743
+ ): { waitingOn: string[]; dependenciesSatisfied: boolean } {
744
+ const deps = Array.isArray(entry.dependsOn) ? entry.dependsOn : [];
745
+ const waitingOn = deps.filter(depId => statusById.get(depId) !== 'completed');
746
+ return {
747
+ waitingOn,
748
+ dependenciesSatisfied: waitingOn.length === 0 && !entry.blockedReason,
749
+ };
750
+ }
751
+
288
752
  export interface MeshWorkQueueStats {
289
753
  total: number;
290
754
  active: number;
@@ -310,14 +774,16 @@ export interface MeshWorkQueueStats {
310
774
  * Return aggregate queue statistics for the given mesh.
311
775
  */
312
776
  export function getMeshQueueStats(meshId: string): MeshWorkQueueStats {
313
- const queue = readQueue(meshId);
314
- const pending = queue.filter(q => q.status === 'pending').length;
315
- const assigned = queue.filter(q => q.status === 'assigned').length;
316
- const completed = queue.filter(q => q.status === 'completed').length;
317
- const failed = queue.filter(q => q.status === 'failed').length;
318
- const cancelled = queue.filter(q => q.status === 'cancelled').length;
777
+ const rows = MeshRuntimeStore.getInstance().getQueueStatsByStatus(meshId);
778
+ const counts: Record<string, number> = {};
779
+ for (const r of rows) counts[r.status] = r.count;
780
+ const pending = counts['pending'] ?? 0;
781
+ const assigned = counts['assigned'] ?? 0;
782
+ const completed = counts['completed'] ?? 0;
783
+ const failed = counts['failed'] ?? 0;
784
+ const cancelled = counts['cancelled'] ?? 0;
319
785
  return {
320
- total: queue.length,
786
+ total: pending + assigned + completed + failed + cancelled,
321
787
  active: pending + assigned,
322
788
  historical: completed + failed + cancelled,
323
789
  pending,
@@ -325,22 +791,119 @@ export function getMeshQueueStats(meshId: string): MeshWorkQueueStats {
325
791
  completed,
326
792
  failed,
327
793
  cancelled,
328
- activeCounts: {
329
- pending,
330
- assigned,
331
- },
332
- historicalCounts: {
333
- completed,
334
- failed,
335
- cancelled,
336
- },
337
- activeAssignments: queue
338
- .filter(q => q.status === 'assigned')
339
- .map(q => ({
340
- id: q.id,
341
- nodeId: q.assignedNodeId,
342
- sessionId: q.assignedSessionId,
343
- message: q.message,
344
- })),
794
+ activeCounts: { pending, assigned },
795
+ historicalCounts: { completed, failed, cancelled },
796
+ activeAssignments: MeshRuntimeStore.getInstance().getActiveAssignmentDetails(meshId),
345
797
  };
346
798
  }
799
+
800
+ export function __replaceMeshQueueForTests(meshId: string, queue: MeshWorkQueueEntry[]): void {
801
+ MeshRuntimeStore.getInstance().transaction(() => {
802
+ MeshRuntimeStore.getInstance().replaceQueue(meshId, queue);
803
+ });
804
+ }
805
+
806
+ export function __clearMeshQueueForTests(meshId: string): void {
807
+ MeshRuntimeStore.getInstance().deleteQueue(meshId);
808
+ }
809
+
810
+ export function __clearDirectDispatchesForTests(meshId: string): void {
811
+ MeshRuntimeStore.getInstance().deleteDirectDispatches(meshId);
812
+ }
813
+
814
+ export function __resetMeshRuntimeStoreForTests(): void {
815
+ MeshRuntimeStore.resetForTests();
816
+ }
817
+
818
+ // ── Direct Dispatch Tracking ─────────────────────────────────────────────────
819
+ // Persists direct (non-queue) task dispatches so buildMeshActiveWork can read
820
+ // active work from MeshRuntimeStore instead of scanning ledger JSONL entries.
821
+
822
+ export type DirectDispatchRecord = ReturnType<MeshRuntimeStore['getActiveDirectDispatches']>[number];
823
+
824
+ export function insertDirectDispatch(
825
+ meshId: string,
826
+ data: {
827
+ taskId: string;
828
+ nodeId?: string;
829
+ sessionId?: string;
830
+ providerType?: string;
831
+ message: string;
832
+ taskMode?: string;
833
+ via: string;
834
+ dispatchedToIdleSession?: boolean;
835
+ dispatchedAt: string;
836
+ },
837
+ ): void {
838
+ try {
839
+ MeshRuntimeStore.getInstance().insertDirectDispatch({ ...data, meshId });
840
+ } catch (e: any) {
841
+ process.stderr.write(`[adhdev-mesh] insertDirectDispatch failed for task ${data.taskId}: ${e?.message || e}\n`);
842
+ }
843
+ }
844
+
845
+ export function getActiveDirectDispatches(meshId: string): DirectDispatchRecord[] {
846
+ try {
847
+ return MeshRuntimeStore.getInstance().getActiveDirectDispatches(meshId);
848
+ } catch {
849
+ return [];
850
+ }
851
+ }
852
+
853
+ export function updateDirectDispatchStatus(
854
+ meshId: string,
855
+ sessionId: string,
856
+ status: 'acked' | 'completed' | 'failed' | 'stale',
857
+ ): void {
858
+ try {
859
+ MeshRuntimeStore.getInstance().updateDirectDispatchStatus(meshId, sessionId, status);
860
+ } catch { /* best-effort */ }
861
+ }
862
+
863
+ export function cleanupTerminalDirectDispatches(olderThanMs = 7 * 24 * 60 * 60_000): void {
864
+ try {
865
+ MeshRuntimeStore.getInstance().cleanupTerminalDirectDispatches(olderThanMs);
866
+ } catch { /* best-effort */ }
867
+ }
868
+
869
+ export function markStaleDirectDispatches(meshId: string, olderThanMs = 60 * 60_000): void {
870
+ try {
871
+ MeshRuntimeStore.getInstance().markStaleDirectDispatches(meshId, olderThanMs);
872
+ } catch { /* best-effort */ }
873
+ }
874
+
875
+ /**
876
+ * Delete specific direct dispatch rows by taskId. Returns the number of rows deleted.
877
+ * Used by the staleDirect prune path to evict orphaned/terminal dispatch records from the
878
+ * active staleDirect surface while leaving the append-only mesh ledger (audit history) intact.
879
+ */
880
+ export function deleteDirectDispatchesByTaskId(meshId: string, taskIds: string[]): number {
881
+ try {
882
+ return MeshRuntimeStore.getInstance().deleteDirectDispatchesByTaskId(meshId, taskIds);
883
+ } catch {
884
+ return 0;
885
+ }
886
+ }
887
+
888
+ export type MeshToolCallRateResult = { rateLimitExceeded: boolean; callsInWindow: number; advisory: string | null };
889
+
890
+ /**
891
+ * Record a coordinator tool call and return a rate-limit advisory when the
892
+ * call rate for that tool exceeds the allowed threshold.
893
+ *
894
+ * Defaults: 10-second sliding window, max 5 calls before advisory is raised.
895
+ * Returns { rateLimitExceeded: false } on any store error so callers are not blocked.
896
+ */
897
+ export function recordMeshToolCall(opts: {
898
+ meshId: string;
899
+ tool: string;
900
+ sessionId?: string | null;
901
+ windowMs?: number;
902
+ maxCalls?: number;
903
+ }): MeshToolCallRateResult {
904
+ try {
905
+ return MeshRuntimeStore.getInstance().recordMeshToolCall(opts);
906
+ } catch {
907
+ return { rateLimitExceeded: false, callsInWindow: 0, advisory: null };
908
+ }
909
+ }