@telora/daemon 0.17.33 → 0.17.40

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 (344) hide show
  1. package/build-info.json +5 -3
  2. package/dist/assembly-engine.d.ts +6 -0
  3. package/dist/assembly-engine.d.ts.map +1 -1
  4. package/dist/assembly-engine.js +19 -0
  5. package/dist/assembly-engine.js.map +1 -1
  6. package/dist/assembly-resolvers.d.ts +17 -8
  7. package/dist/assembly-resolvers.d.ts.map +1 -1
  8. package/dist/assembly-resolvers.js +19 -8
  9. package/dist/assembly-resolvers.js.map +1 -1
  10. package/dist/cli/connect.d.ts.map +1 -1
  11. package/dist/cli/connect.js +4 -0
  12. package/dist/cli/connect.js.map +1 -1
  13. package/dist/cli/session-state.d.ts +10 -0
  14. package/dist/cli/session-state.d.ts.map +1 -1
  15. package/dist/cli/session-state.js +31 -0
  16. package/dist/cli/session-state.js.map +1 -1
  17. package/dist/completion/completion-decision.d.ts +83 -0
  18. package/dist/completion/completion-decision.d.ts.map +1 -0
  19. package/dist/completion/completion-decision.js +48 -0
  20. package/dist/completion/completion-decision.js.map +1 -0
  21. package/dist/completion/event-escalations.d.ts +97 -0
  22. package/dist/completion/event-escalations.d.ts.map +1 -0
  23. package/dist/completion/event-escalations.js +213 -0
  24. package/dist/completion/event-escalations.js.map +1 -0
  25. package/dist/completion/event.d.ts +1 -72
  26. package/dist/completion/event.d.ts.map +1 -1
  27. package/dist/completion/event.js +149 -329
  28. package/dist/completion/event.js.map +1 -1
  29. package/dist/completion/exit-classification.d.ts +82 -0
  30. package/dist/completion/exit-classification.d.ts.map +1 -0
  31. package/dist/completion/exit-classification.js +61 -0
  32. package/dist/completion/exit-classification.js.map +1 -0
  33. package/dist/completion/index.d.ts +14 -5
  34. package/dist/completion/index.d.ts.map +1 -1
  35. package/dist/completion/index.js +14 -5
  36. package/dist/completion/index.js.map +1 -1
  37. package/dist/completion/merge-phase.d.ts +114 -0
  38. package/dist/completion/merge-phase.d.ts.map +1 -0
  39. package/dist/completion/merge-phase.js +198 -0
  40. package/dist/completion/merge-phase.js.map +1 -0
  41. package/dist/completion/review-exit-phase.d.ts +82 -0
  42. package/dist/completion/review-exit-phase.d.ts.map +1 -0
  43. package/dist/completion/review-exit-phase.js +228 -0
  44. package/dist/completion/review-exit-phase.js.map +1 -0
  45. package/dist/completion/status-advance-phase.d.ts +61 -0
  46. package/dist/completion/status-advance-phase.d.ts.map +1 -0
  47. package/dist/completion/status-advance-phase.js +182 -0
  48. package/dist/completion/status-advance-phase.js.map +1 -0
  49. package/dist/completion/team-completion.d.ts +28 -269
  50. package/dist/completion/team-completion.d.ts.map +1 -1
  51. package/dist/completion/team-completion.js +145 -676
  52. package/dist/completion/team-completion.js.map +1 -1
  53. package/dist/config.d.ts.map +1 -1
  54. package/dist/config.js +70 -41
  55. package/dist/config.js.map +1 -1
  56. package/dist/daemon-process.d.ts +18 -2
  57. package/dist/daemon-process.d.ts.map +1 -1
  58. package/dist/daemon-process.js +15 -2
  59. package/dist/daemon-process.js.map +1 -1
  60. package/dist/directive/close-loop-stage.d.ts +50 -0
  61. package/dist/directive/close-loop-stage.d.ts.map +1 -0
  62. package/dist/directive/close-loop-stage.js +196 -0
  63. package/dist/directive/close-loop-stage.js.map +1 -0
  64. package/dist/directive/directive-assembly.d.ts +33 -0
  65. package/dist/directive/directive-assembly.d.ts.map +1 -0
  66. package/dist/directive/directive-assembly.js +77 -0
  67. package/dist/directive/directive-assembly.js.map +1 -0
  68. package/dist/directive/directive-dispatch.d.ts +103 -0
  69. package/dist/directive/directive-dispatch.d.ts.map +1 -0
  70. package/dist/directive/directive-dispatch.js +279 -0
  71. package/dist/directive/directive-dispatch.js.map +1 -0
  72. package/dist/directive/phase-sync.d.ts +89 -0
  73. package/dist/directive/phase-sync.d.ts.map +1 -0
  74. package/dist/directive/phase-sync.js +173 -0
  75. package/dist/directive/phase-sync.js.map +1 -0
  76. package/dist/directive-executor.d.ts +21 -223
  77. package/dist/directive-executor.d.ts.map +1 -1
  78. package/dist/directive-executor.js +28 -687
  79. package/dist/directive-executor.js.map +1 -1
  80. package/dist/focus-engine.d.ts.map +1 -1
  81. package/dist/focus-engine.js +16 -10
  82. package/dist/focus-engine.js.map +1 -1
  83. package/dist/focus-executor.d.ts +29 -8
  84. package/dist/focus-executor.d.ts.map +1 -1
  85. package/dist/focus-executor.js +29 -10
  86. package/dist/focus-executor.js.map +1 -1
  87. package/dist/focus-stage-lifecycle.d.ts +7 -5
  88. package/dist/focus-stage-lifecycle.d.ts.map +1 -1
  89. package/dist/focus-stage-lifecycle.js +11 -6
  90. package/dist/focus-stage-lifecycle.js.map +1 -1
  91. package/dist/index.js +8 -0
  92. package/dist/index.js.map +1 -1
  93. package/dist/merge-back-loop.d.ts.map +1 -1
  94. package/dist/merge-back-loop.js +4 -10
  95. package/dist/merge-back-loop.js.map +1 -1
  96. package/dist/pipeline-config.d.ts +13 -0
  97. package/dist/pipeline-config.d.ts.map +1 -1
  98. package/dist/pipeline-config.js +15 -0
  99. package/dist/pipeline-config.js.map +1 -1
  100. package/dist/resolvers/agent-escalations.d.ts +8 -1
  101. package/dist/resolvers/agent-escalations.d.ts.map +1 -1
  102. package/dist/resolvers/agent-escalations.js +25 -22
  103. package/dist/resolvers/agent-escalations.js.map +1 -1
  104. package/dist/resolvers/agent-session-summaries.d.ts +8 -1
  105. package/dist/resolvers/agent-session-summaries.d.ts.map +1 -1
  106. package/dist/resolvers/agent-session-summaries.js +21 -18
  107. package/dist/resolvers/agent-session-summaries.js.map +1 -1
  108. package/dist/resolvers/delivery-acceptance-criteria.d.ts +7 -1
  109. package/dist/resolvers/delivery-acceptance-criteria.d.ts.map +1 -1
  110. package/dist/resolvers/delivery-acceptance-criteria.js +14 -11
  111. package/dist/resolvers/delivery-acceptance-criteria.js.map +1 -1
  112. package/dist/resolvers/delivery-description.d.ts +7 -1
  113. package/dist/resolvers/delivery-description.d.ts.map +1 -1
  114. package/dist/resolvers/delivery-description.js +14 -11
  115. package/dist/resolvers/delivery-description.js.map +1 -1
  116. package/dist/resolvers/delivery-issues.d.ts +8 -1
  117. package/dist/resolvers/delivery-issues.d.ts.map +1 -1
  118. package/dist/resolvers/delivery-issues.js +51 -48
  119. package/dist/resolvers/delivery-issues.js.map +1 -1
  120. package/dist/resolvers/delivery-listing.d.ts +16 -1
  121. package/dist/resolvers/delivery-listing.d.ts.map +1 -1
  122. package/dist/resolvers/delivery-listing.js +36 -33
  123. package/dist/resolvers/delivery-listing.js.map +1 -1
  124. package/dist/resolvers/delivery-tech-context.d.ts +7 -1
  125. package/dist/resolvers/delivery-tech-context.d.ts.map +1 -1
  126. package/dist/resolvers/delivery-tech-context.js +14 -11
  127. package/dist/resolvers/delivery-tech-context.js.map +1 -1
  128. package/dist/resolvers/deployment-profile.d.ts +8 -1
  129. package/dist/resolvers/deployment-profile.d.ts.map +1 -1
  130. package/dist/resolvers/deployment-profile.js +20 -17
  131. package/dist/resolvers/deployment-profile.js.map +1 -1
  132. package/dist/resolvers/focus-anchoring-injections.d.ts +26 -1
  133. package/dist/resolvers/focus-anchoring-injections.d.ts.map +1 -1
  134. package/dist/resolvers/focus-anchoring-injections.js +91 -88
  135. package/dist/resolvers/focus-anchoring-injections.js.map +1 -1
  136. package/dist/resolvers/focus-context.d.ts +9 -1
  137. package/dist/resolvers/focus-context.d.ts.map +1 -1
  138. package/dist/resolvers/focus-context.js +38 -35
  139. package/dist/resolvers/focus-context.js.map +1 -1
  140. package/dist/resolvers/focus-injections.d.ts +13 -1
  141. package/dist/resolvers/focus-injections.d.ts.map +1 -1
  142. package/dist/resolvers/focus-injections.js +60 -57
  143. package/dist/resolvers/focus-injections.js.map +1 -1
  144. package/dist/resolvers/focus-last-review-report.d.ts +2 -1
  145. package/dist/resolvers/focus-last-review-report.d.ts.map +1 -1
  146. package/dist/resolvers/focus-last-review-report.js +33 -30
  147. package/dist/resolvers/focus-last-review-report.js.map +1 -1
  148. package/dist/resolvers/focus-reality-tree.d.ts +15 -1
  149. package/dist/resolvers/focus-reality-tree.d.ts.map +1 -1
  150. package/dist/resolvers/focus-reality-tree.js +35 -32
  151. package/dist/resolvers/focus-reality-tree.js.map +1 -1
  152. package/dist/resolvers/git-diff-against-base.d.ts +8 -1
  153. package/dist/resolvers/git-diff-against-base.d.ts.map +1 -1
  154. package/dist/resolvers/git-diff-against-base.js +33 -30
  155. package/dist/resolvers/git-diff-against-base.js.map +1 -1
  156. package/dist/resolvers/guards-evaluation-results.d.ts +7 -1
  157. package/dist/resolvers/guards-evaluation-results.d.ts.map +1 -1
  158. package/dist/resolvers/guards-evaluation-results.js +25 -22
  159. package/dist/resolvers/guards-evaluation-results.js.map +1 -1
  160. package/dist/resolvers/index.d.ts +22 -40
  161. package/dist/resolvers/index.d.ts.map +1 -1
  162. package/dist/resolvers/index.js +112 -41
  163. package/dist/resolvers/index.js.map +1 -1
  164. package/dist/resolvers/loop-context.d.ts +18 -1
  165. package/dist/resolvers/loop-context.d.ts.map +1 -1
  166. package/dist/resolvers/loop-context.js +21 -18
  167. package/dist/resolvers/loop-context.js.map +1 -1
  168. package/dist/resolvers/loop-delivery-index.d.ts +17 -1
  169. package/dist/resolvers/loop-delivery-index.d.ts.map +1 -1
  170. package/dist/resolvers/loop-delivery-index.js +51 -48
  171. package/dist/resolvers/loop-delivery-index.js.map +1 -1
  172. package/dist/resolvers/loop-documents.d.ts +9 -1
  173. package/dist/resolvers/loop-documents.d.ts.map +1 -1
  174. package/dist/resolvers/loop-documents.js +22 -19
  175. package/dist/resolvers/loop-documents.js.map +1 -1
  176. package/dist/resolvers/loop-expected-effects.d.ts +11 -1
  177. package/dist/resolvers/loop-expected-effects.d.ts.map +1 -1
  178. package/dist/resolvers/loop-expected-effects.js +53 -50
  179. package/dist/resolvers/loop-expected-effects.js.map +1 -1
  180. package/dist/resolvers/loop-frt-statement.d.ts +11 -1
  181. package/dist/resolvers/loop-frt-statement.d.ts.map +1 -1
  182. package/dist/resolvers/loop-frt-statement.js +28 -25
  183. package/dist/resolvers/loop-frt-statement.js.map +1 -1
  184. package/dist/resolvers/loop-injection.d.ts +10 -1
  185. package/dist/resolvers/loop-injection.d.ts.map +1 -1
  186. package/dist/resolvers/loop-injection.js +38 -35
  187. package/dist/resolvers/loop-injection.js.map +1 -1
  188. package/dist/resolvers/loop-persona.d.ts +20 -1
  189. package/dist/resolvers/loop-persona.d.ts.map +1 -1
  190. package/dist/resolvers/loop-persona.js +20 -17
  191. package/dist/resolvers/loop-persona.js.map +1 -1
  192. package/dist/resolvers/loop-questions.d.ts +8 -1
  193. package/dist/resolvers/loop-questions.d.ts.map +1 -1
  194. package/dist/resolvers/loop-questions.js +39 -36
  195. package/dist/resolvers/loop-questions.js.map +1 -1
  196. package/dist/resolvers/loop-upstream-udes.d.ts +11 -1
  197. package/dist/resolvers/loop-upstream-udes.d.ts.map +1 -1
  198. package/dist/resolvers/loop-upstream-udes.js +74 -71
  199. package/dist/resolvers/loop-upstream-udes.js.map +1 -1
  200. package/dist/resolvers/prd-context.d.ts +2 -0
  201. package/dist/resolvers/prd-context.d.ts.map +1 -1
  202. package/dist/resolvers/prd-context.js +16 -13
  203. package/dist/resolvers/prd-context.js.map +1 -1
  204. package/dist/resolvers/prd-persona.d.ts +2 -0
  205. package/dist/resolvers/prd-persona.d.ts.map +1 -1
  206. package/dist/resolvers/prd-persona.js +6 -3
  207. package/dist/resolvers/prd-persona.js.map +1 -1
  208. package/dist/resolvers/reality-metrics.d.ts +8 -1
  209. package/dist/resolvers/reality-metrics.d.ts.map +1 -1
  210. package/dist/resolvers/reality-metrics.js +73 -70
  211. package/dist/resolvers/reality-metrics.js.map +1 -1
  212. package/dist/resolvers/reality-projections.d.ts +7 -1
  213. package/dist/resolvers/reality-projections.d.ts.map +1 -1
  214. package/dist/resolvers/reality-projections.js +35 -32
  215. package/dist/resolvers/reality-projections.js.map +1 -1
  216. package/dist/resolvers/reality-tree-snapshot.d.ts +10 -1
  217. package/dist/resolvers/reality-tree-snapshot.d.ts.map +1 -1
  218. package/dist/resolvers/reality-tree-snapshot.js +34 -31
  219. package/dist/resolvers/reality-tree-snapshot.js.map +1 -1
  220. package/dist/resolvers/retired-injections.d.ts +10 -1
  221. package/dist/resolvers/retired-injections.d.ts.map +1 -1
  222. package/dist/resolvers/retired-injections.js +68 -65
  223. package/dist/resolvers/retired-injections.js.map +1 -1
  224. package/dist/resolvers/review-outcomes.d.ts +11 -1
  225. package/dist/resolvers/review-outcomes.d.ts.map +1 -1
  226. package/dist/resolvers/review-outcomes.js +27 -24
  227. package/dist/resolvers/review-outcomes.js.map +1 -1
  228. package/dist/resolvers/security-advisory.d.ts +2 -1
  229. package/dist/resolvers/security-advisory.d.ts.map +1 -1
  230. package/dist/resolvers/security-advisory.js +47 -44
  231. package/dist/resolvers/security-advisory.js.map +1 -1
  232. package/dist/resolvers/wiki-search.d.ts +8 -1
  233. package/dist/resolvers/wiki-search.d.ts.map +1 -1
  234. package/dist/resolvers/wiki-search.js +17 -14
  235. package/dist/resolvers/wiki-search.js.map +1 -1
  236. package/dist/resolvers/wiki-topic.d.ts +9 -1
  237. package/dist/resolvers/wiki-topic.d.ts.map +1 -1
  238. package/dist/resolvers/wiki-topic.js +21 -18
  239. package/dist/resolvers/wiki-topic.js.map +1 -1
  240. package/dist/resolvers/workflow-stages.d.ts +7 -1
  241. package/dist/resolvers/workflow-stages.d.ts.map +1 -1
  242. package/dist/resolvers/workflow-stages.js +20 -17
  243. package/dist/resolvers/workflow-stages.js.map +1 -1
  244. package/dist/review-defect-detector.d.ts +4 -1
  245. package/dist/review-defect-detector.d.ts.map +1 -1
  246. package/dist/review-defect-detector.js +6 -8
  247. package/dist/review-defect-detector.js.map +1 -1
  248. package/dist/self-update.d.ts +6 -0
  249. package/dist/self-update.d.ts.map +1 -1
  250. package/dist/self-update.js +6 -1
  251. package/dist/self-update.js.map +1 -1
  252. package/dist/spawner/index.d.ts +2 -1
  253. package/dist/spawner/index.d.ts.map +1 -1
  254. package/dist/spawner/index.js +2 -1
  255. package/dist/spawner/index.js.map +1 -1
  256. package/dist/spawner/spawn-team.d.ts +14 -52
  257. package/dist/spawner/spawn-team.d.ts.map +1 -1
  258. package/dist/spawner/spawn-team.js +14 -469
  259. package/dist/spawner/spawn-team.js.map +1 -1
  260. package/dist/staleness.d.ts +124 -0
  261. package/dist/staleness.d.ts.map +1 -0
  262. package/dist/staleness.js +215 -0
  263. package/dist/staleness.js.map +1 -0
  264. package/dist/state-cascade.d.ts +3 -2
  265. package/dist/state-cascade.d.ts.map +1 -1
  266. package/dist/state-cascade.js +7 -3
  267. package/dist/state-cascade.js.map +1 -1
  268. package/dist/types/focus.d.ts +5 -4
  269. package/dist/types/focus.d.ts.map +1 -1
  270. package/dist/types/index.d.ts +0 -1
  271. package/dist/types/index.d.ts.map +1 -1
  272. package/dist/types/index.js +0 -1
  273. package/dist/types/index.js.map +1 -1
  274. package/dist/types/session.d.ts +2 -3
  275. package/dist/types/session.d.ts.map +1 -1
  276. package/dist/unified-engine-lifecycle.d.ts.map +1 -1
  277. package/dist/unified-engine-lifecycle.js +2 -23
  278. package/dist/unified-engine-lifecycle.js.map +1 -1
  279. package/dist/unified-shell-config.d.ts +2 -7
  280. package/dist/unified-shell-config.d.ts.map +1 -1
  281. package/dist/unified-shell-config.js +2 -23
  282. package/dist/unified-shell-config.js.map +1 -1
  283. package/dist/unified-shell-status.d.ts.map +1 -1
  284. package/dist/unified-shell-status.js +2 -4
  285. package/dist/unified-shell-status.js.map +1 -1
  286. package/dist/unified-shell.d.ts.map +1 -1
  287. package/dist/unified-shell.js +21 -24
  288. package/dist/unified-shell.js.map +1 -1
  289. package/dist/version-check.d.ts.map +1 -1
  290. package/dist/version-check.js +6 -4
  291. package/dist/version-check.js.map +1 -1
  292. package/package.json +3 -3
  293. package/dist/pm/adaptive-poller.d.ts +0 -26
  294. package/dist/pm/adaptive-poller.d.ts.map +0 -1
  295. package/dist/pm/adaptive-poller.js +0 -53
  296. package/dist/pm/adaptive-poller.js.map +0 -1
  297. package/dist/pm/cascade-evaluator.d.ts +0 -29
  298. package/dist/pm/cascade-evaluator.d.ts.map +0 -1
  299. package/dist/pm/cascade-evaluator.js +0 -135
  300. package/dist/pm/cascade-evaluator.js.map +0 -1
  301. package/dist/pm/channel-email.d.ts +0 -42
  302. package/dist/pm/channel-email.d.ts.map +0 -1
  303. package/dist/pm/channel-email.js +0 -47
  304. package/dist/pm/channel-email.js.map +0 -1
  305. package/dist/pm/channel-slack.d.ts +0 -40
  306. package/dist/pm/channel-slack.d.ts.map +0 -1
  307. package/dist/pm/channel-slack.js +0 -46
  308. package/dist/pm/channel-slack.js.map +0 -1
  309. package/dist/pm/communication-drafter.d.ts +0 -44
  310. package/dist/pm/communication-drafter.d.ts.map +0 -1
  311. package/dist/pm/communication-drafter.js +0 -84
  312. package/dist/pm/communication-drafter.js.map +0 -1
  313. package/dist/pm/dialog-handler.d.ts +0 -32
  314. package/dist/pm/dialog-handler.d.ts.map +0 -1
  315. package/dist/pm/dialog-handler.js +0 -107
  316. package/dist/pm/dialog-handler.js.map +0 -1
  317. package/dist/pm/mitigation-correlator.d.ts +0 -29
  318. package/dist/pm/mitigation-correlator.d.ts.map +0 -1
  319. package/dist/pm/mitigation-correlator.js +0 -147
  320. package/dist/pm/mitigation-correlator.js.map +0 -1
  321. package/dist/pm/risk-scanner.d.ts +0 -28
  322. package/dist/pm/risk-scanner.d.ts.map +0 -1
  323. package/dist/pm/risk-scanner.js +0 -112
  324. package/dist/pm/risk-scanner.js.map +0 -1
  325. package/dist/pm-engine.d.ts +0 -96
  326. package/dist/pm-engine.d.ts.map +0 -1
  327. package/dist/pm-engine.js +0 -540
  328. package/dist/pm-engine.js.map +0 -1
  329. package/dist/pm-process.d.ts +0 -45
  330. package/dist/pm-process.d.ts.map +0 -1
  331. package/dist/pm-process.js +0 -195
  332. package/dist/pm-process.js.map +0 -1
  333. package/dist/pm-prompt-builder.d.ts +0 -23
  334. package/dist/pm-prompt-builder.d.ts.map +0 -1
  335. package/dist/pm-prompt-builder.js +0 -64
  336. package/dist/pm-prompt-builder.js.map +0 -1
  337. package/dist/session-lifecycle.d.ts +0 -78
  338. package/dist/session-lifecycle.d.ts.map +0 -1
  339. package/dist/session-lifecycle.js +0 -382
  340. package/dist/session-lifecycle.js.map +0 -1
  341. package/dist/types/pm.d.ts +0 -53
  342. package/dist/types/pm.d.ts.map +0 -1
  343. package/dist/types/pm.js +0 -10
  344. package/dist/types/pm.js.map +0 -1
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Close-loop stage dispatcher.
3
+ *
4
+ * Drives the `close_loop` lifecycle stage for a focus: classify anchored
5
+ * injections by terminal state, then short-circuit to done, spawn the
6
+ * one-shot bookkeeper team, advance with an injection sweep, or hold.
7
+ * The pure decision logic lives in close-loop-dispatcher.ts; this module
8
+ * is the impure dispatch around it. Extracted from directive-executor.ts;
9
+ * the executor re-exports the public surface so existing importers are
10
+ * unaffected.
11
+ */
12
+ import { getActiveTeams } from '../focus-team-state.js';
13
+ import { ESCALATION_KINDS } from '@telora/daemon-core';
14
+ import { CLOSE_LOOP_MODEL, advanceFocusToDoneWithSweep, buildCloseLoopDirective, decideCloseLoop, getAnchoringDeliveries, } from '../close-loop-dispatcher.js';
15
+ import { verifyInjectionWithEvidence } from '../queries/verification.js';
16
+ import { getFocusDeliveries, updateFocusStage } from '../queries/focuses.js';
17
+ import { listPendingEscalations } from '../queries/issues.js';
18
+ import { callApi } from '../queries/shared.js';
19
+ import { terminateTeam, waitForTeamExit } from '../completion/index.js';
20
+ import { pendingSpawnDirectives, computeDirectiveHash } from './directive-queue.js';
21
+ import { assembleDirectiveContent } from './directive-assembly.js';
22
+ /**
23
+ * Per-focus tracker for whether the close_loop bookkeeper has been spawned
24
+ * at least once. Used to avoid respawning the bookkeeper on every poll
25
+ * cycle while non-terminal injections are still being worked.
26
+ *
27
+ * Cleared when the focus leaves close_loop (advances to done or is
28
+ * manually reset). The set is in-process state -- on daemon restart it is
29
+ * empty, which is fine because the dispatcher's decideCloseLoop function
30
+ * will see the bookkeeper has not run, respawn it, and the new bookkeeper
31
+ * will sweep idempotently (already-verified injections stay verified;
32
+ * already-escalated ones still surface their open escalation).
33
+ */
34
+ const closeLoopBookkeeperSpawned = new Set();
35
+ /** Test helper: clear the bookkeeper-spawned set. */
36
+ export function __resetCloseLoopStateForTesting() {
37
+ closeLoopBookkeeperSpawned.clear();
38
+ }
39
+ /**
40
+ * Inspect open escalations and return the set of injection ids that have
41
+ * an active `injection_unverified` escalation (status pending or
42
+ * in_review). Used by the close_loop dispatcher to classify "escalated"
43
+ * injections as terminal.
44
+ *
45
+ * The `metadata.injection_id` field is set by the bookkeeper persona when
46
+ * filing the escalation -- see the close_loop directive prompt.
47
+ */
48
+ export async function getOpenInjectionUnverifiedEscalationIds() {
49
+ const escalations = await listPendingEscalations();
50
+ const ids = new Set();
51
+ for (const e of escalations) {
52
+ if (e.escalationKind !== ESCALATION_KINDS.INJECTION_UNVERIFIED)
53
+ continue;
54
+ const injectionId = e.metadata?.injection_id;
55
+ if (typeof injectionId === 'string' && injectionId.length > 0) {
56
+ ids.add(injectionId);
57
+ }
58
+ }
59
+ return ids;
60
+ }
61
+ /**
62
+ * Dispatch the close_loop stage for one focus.
63
+ *
64
+ * Reads the focus's anchored deliveries + injection statuses + open
65
+ * escalations, then runs the pure `decideCloseLoop` to determine whether
66
+ * to short-circuit, spawn a bookkeeper, advance to done, or hold.
67
+ *
68
+ * Returns the decision so the caller (or a test) can log/assert.
69
+ *
70
+ * Idempotent across poll cycles -- the `closeLoopBookkeeperSpawned` set
71
+ * ensures the bookkeeper is spawned at most once per focus visit to
72
+ * close_loop. Tests reset this via `__resetCloseLoopStateForTesting`.
73
+ */
74
+ export async function dispatchCloseLoopStage(config, focus) {
75
+ const deliveries = await getFocusDeliveries(focus.focus_id);
76
+ const anchored = getAnchoringDeliveries(deliveries);
77
+ // Build the per-injection terminal-state snapshot.
78
+ //
79
+ // `statusById` is hoisted to function scope so the close_loop sweep (in
80
+ // the advance_to_done branch below) can reuse the snapshot without a
81
+ // second round-trip to reality_tree_focus_injections.
82
+ const states = [];
83
+ const statusById = new Map();
84
+ if (anchored.length > 0) {
85
+ let openEscalationIds;
86
+ try {
87
+ openEscalationIds = await getOpenInjectionUnverifiedEscalationIds();
88
+ }
89
+ catch (err) {
90
+ console.warn(`[close-loop-dispatcher] Failed to fetch open injection_unverified escalations ` +
91
+ `for focus ${focus.focus_id.slice(0, 8)}: ${err.message}`);
92
+ openEscalationIds = new Set();
93
+ }
94
+ let snapshots = [];
95
+ try {
96
+ const result = await callApi('reality_tree_focus_injections', { focusId: focus.focus_id });
97
+ snapshots = result.items ?? [];
98
+ }
99
+ catch (err) {
100
+ console.warn(`[close-loop-dispatcher] Failed to fetch focus injections for ${focus.focus_id.slice(0, 8)}: ` +
101
+ `${err.message}`);
102
+ }
103
+ for (const item of snapshots) {
104
+ statusById.set(item.injection.id, item.injection.injectionStatus);
105
+ }
106
+ for (const delivery of anchored) {
107
+ const injectionId = delivery.injectionId;
108
+ states.push({
109
+ injectionId,
110
+ injectionStatus: statusById.get(injectionId) ?? null,
111
+ hasOpenUnverifiedEscalation: openEscalationIds.has(injectionId),
112
+ });
113
+ }
114
+ }
115
+ const bookkeeperHasRun = closeLoopBookkeeperSpawned.has(focus.focus_id);
116
+ const decision = decideCloseLoop(deliveries, states, bookkeeperHasRun);
117
+ switch (decision.kind) {
118
+ case 'short_circuit_to_done': {
119
+ try {
120
+ await updateFocusStage(focus.focus_id, 'done');
121
+ console.log(`[close-loop-dispatcher] Focus "${focus.focus_name}" has no anchoring injections -- ` +
122
+ `short-circuiting close_loop -> done`);
123
+ }
124
+ catch (err) {
125
+ console.warn(`[close-loop-dispatcher] Failed to short-circuit focus ${focus.focus_id.slice(0, 8)} to done: ` +
126
+ `${err.message}`);
127
+ }
128
+ closeLoopBookkeeperSpawned.delete(focus.focus_id);
129
+ return decision;
130
+ }
131
+ case 'advance_to_done': {
132
+ // Focus-level injection sweep + stage advance. The helper realizes
133
+ // anchored injections whose anchoring deliveries closed successfully
134
+ // but whose status hasn't yet flipped to 'verified' (e.g. deliveries
135
+ // that exited via review instead of the per-delivery verify gate),
136
+ // then advances close_loop -> done. On sweep failure, the helper
137
+ // returns early WITHOUT calling updateFocusStage -- the focus stays
138
+ // at close_loop and the next poll cycle retries.
139
+ const outcome = await advanceFocusToDoneWithSweep(focus, anchored, statusById, { verify: verifyInjectionWithEvidence, updateFocusStage });
140
+ // Clear the bookkeeper-spawned flag only when the focus has
141
+ // terminally exited close_loop -- either advanced, or attempted to
142
+ // advance and the stage-update itself failed. A sweep failure leaves
143
+ // the flag in place so the same bookkeeper context is reused on the
144
+ // next poll cycle's retry.
145
+ if (outcome.advanced || outcome.advanceError) {
146
+ closeLoopBookkeeperSpawned.delete(focus.focus_id);
147
+ }
148
+ return decision;
149
+ }
150
+ case 'spawn_bookkeeper': {
151
+ const team = getActiveTeams().get(focus.focus_id);
152
+ const worktreePath = team?.worktreePath ?? null;
153
+ try {
154
+ const message = await assembleDirectiveContent(config, focus.focus_id, decision.directive, worktreePath);
155
+ const contentHash = computeDirectiveHash(decision.directive);
156
+ pendingSpawnDirectives.set(focus.focus_id, {
157
+ message,
158
+ model: decision.directive.model ?? CLOSE_LOOP_MODEL,
159
+ sessionType: 'coding',
160
+ // The close_loop bookkeeper is a distinct one-shot session: its own
161
+ // lineage slot, always fresh -- it must not resume (or clobber) the
162
+ // coder's context (INJ-B).
163
+ lineage: 'close_loop',
164
+ continuity: 'fresh',
165
+ contentHash,
166
+ });
167
+ if (team) {
168
+ terminateTeam(focus.focus_id, 'handoff');
169
+ await waitForTeamExit(focus.focus_id, 30000);
170
+ }
171
+ closeLoopBookkeeperSpawned.add(focus.focus_id);
172
+ console.log(`[close-loop-dispatcher] Spawned bookkeeper team for focus "${focus.focus_name}" ` +
173
+ `(${anchored.length} anchored injection(s), model: ${CLOSE_LOOP_MODEL})`);
174
+ }
175
+ catch (err) {
176
+ console.warn(`[close-loop-dispatcher] Failed to spawn bookkeeper for focus ${focus.focus_id.slice(0, 8)}: ` +
177
+ `${err.message}`);
178
+ }
179
+ return decision;
180
+ }
181
+ case 'hold': {
182
+ console.log(`[close-loop-dispatcher] Focus "${focus.focus_name}" held at close_loop: ${decision.reason}`);
183
+ return decision;
184
+ }
185
+ }
186
+ }
187
+ /**
188
+ * Pure helper exported for tests: build the close_loop directive and verify
189
+ * it carries the expected assembly + model. The buildCloseLoopDirective
190
+ * function in close-loop-dispatcher.ts already does this; this is a
191
+ * dispatcher-side smoke export so the tests can grep through it.
192
+ */
193
+ export function getCloseLoopDirectiveForTesting() {
194
+ return buildCloseLoopDirective();
195
+ }
196
+ //# sourceMappingURL=close-loop-stage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"close-loop-stage.js","sourceRoot":"","sources":["../../src/directive/close-loop-stage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,2BAA2B,EAC3B,uBAAuB,EACvB,eAAe,EACf,sBAAsB,GAGvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACpF,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAEnE;;;;;;;;;;;GAWG;AACH,MAAM,0BAA0B,GAAG,IAAI,G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C,CAAC,CAAC;YACZ,OAAO,CAAC,GAAG,CACT,kCAAkC,KAAK,CAAC,UAAU,yBAAyB,QAAQ,CAAC,MAAM,EAAE,CAC7F,CAAC;YACF,OAAO,QAAQ,CAAC;QAClB,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,+BAA+B;IAC7C,OAAO,uBAAuB,EAAE,CAAC;AACnC,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Directive assembly: compose a stage directive's deliverable content.
3
+ *
4
+ * Resolves the directive's assembly recipe (via assembly-engine + the
5
+ * registered source resolvers) and appends the static prompt + tool guidance.
6
+ * Shared by both inject and spawn dispatch modes, by the close-loop
7
+ * dispatcher, and by focus-executor for initial spawn with a stage directive.
8
+ * Extracted from directive-executor.ts; the executor re-exports the public
9
+ * surface so existing importers are unaffected.
10
+ */
11
+ import type { DaemonConfig, StageDirective } from '../types.js';
12
+ import { type AssemblyManifestEntry } from '../assembly-engine.js';
13
+ import '../assembly-resolvers.js';
14
+ /** Assembled directive content paired with its per-source attribution manifest. */
15
+ export interface AssembledDirective {
16
+ content: string;
17
+ manifest: AssemblyManifestEntry[];
18
+ }
19
+ /**
20
+ * Assemble directive content from a StageDirective's recipe and prompt.
21
+ * Shared by both inject and spawn modes, and by focus-executor for
22
+ * initial spawn with a stage directive.
23
+ */
24
+ export declare function assembleDirectiveContent(config: DaemonConfig, focusId: string, directive: StageDirective, worktreePath: string | null): Promise<string>;
25
+ /**
26
+ * Assemble directive content AND return the assembly manifest (one entry per
27
+ * recipe source). The composed `content` is identical to
28
+ * assembleDirectiveContent; the manifest lets the spawn path persist what
29
+ * context was injected. The manifest covers ONLY the assembly recipe -- the
30
+ * static directive.prompt / tool guidance are not assembly sources.
31
+ */
32
+ export declare function assembleDirectiveContentWithManifest(config: DaemonConfig, focusId: string, directive: StageDirective, worktreePath: string | null): Promise<AssembledDirective>;
33
+ //# sourceMappingURL=directive-assembly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directive-assembly.d.ts","sourceRoot":"","sources":["../../src/directive/directive-assembly.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,uBAAuB,CAAC;AAE/B,OAAO,0BAA0B,CAAC;AAGlC,mFAAmF;AACnF,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,qBAAqB,EAAE,CAAC;CACnC;AAED;;;;GAIG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,cAAc,EACzB,YAAY,EAAE,MAAM,GAAG,IAAI,GAC1B,OAAO,CAAC,MAAM,CAAC,CAGjB;AAED;;;;;;GAMG;AACH,wBAAsB,oCAAoC,CACxD,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,cAAc,EACzB,YAAY,EAAE,MAAM,GAAG,IAAI,GAC1B,OAAO,CAAC,kBAAkB,CAAC,CAiD7B"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Directive assembly: compose a stage directive's deliverable content.
3
+ *
4
+ * Resolves the directive's assembly recipe (via assembly-engine + the
5
+ * registered source resolvers) and appends the static prompt + tool guidance.
6
+ * Shared by both inject and spawn dispatch modes, by the close-loop
7
+ * dispatcher, and by focus-executor for initial spawn with a stage directive.
8
+ * Extracted from directive-executor.ts; the executor re-exports the public
9
+ * surface so existing importers are unaffected.
10
+ */
11
+ import { resolveAssemblyRecipeWithManifest, } from '../assembly-engine.js';
12
+ // Side-effect import: registers assembly resolvers with assembly-engine at module load. Required - do not remove.
13
+ import '../assembly-resolvers.js';
14
+ import { getFocusDeliveries } from '../queries/focuses.js';
15
+ /**
16
+ * Assemble directive content from a StageDirective's recipe and prompt.
17
+ * Shared by both inject and spawn modes, and by focus-executor for
18
+ * initial spawn with a stage directive.
19
+ */
20
+ export async function assembleDirectiveContent(config, focusId, directive, worktreePath) {
21
+ const { content } = await assembleDirectiveContentWithManifest(config, focusId, directive, worktreePath);
22
+ return content;
23
+ }
24
+ /**
25
+ * Assemble directive content AND return the assembly manifest (one entry per
26
+ * recipe source). The composed `content` is identical to
27
+ * assembleDirectiveContent; the manifest lets the spawn path persist what
28
+ * context was injected. The manifest covers ONLY the assembly recipe -- the
29
+ * static directive.prompt / tool guidance are not assembly sources.
30
+ */
31
+ export async function assembleDirectiveContentWithManifest(config, focusId, directive, worktreePath) {
32
+ const recipe = [...directive.assembly];
33
+ let assembledContext = '';
34
+ let manifest = [];
35
+ if (recipe.length > 0) {
36
+ try {
37
+ const deliveries = await getFocusDeliveries(focusId);
38
+ const assemblyContext = {
39
+ focusId,
40
+ deliveryIds: deliveries.map(d => d.id),
41
+ worktreePath,
42
+ config,
43
+ organizationId: config.organizationId,
44
+ productId: config.productId,
45
+ };
46
+ const resolved = await resolveAssemblyRecipeWithManifest(recipe, assemblyContext);
47
+ assembledContext = resolved.content;
48
+ manifest = resolved.manifest;
49
+ }
50
+ catch (err) {
51
+ console.warn(`[directive-executor] Assembly resolution failed for focus ${focusId.slice(0, 8)}: ${err.message}`);
52
+ }
53
+ }
54
+ const parts = [];
55
+ if (assembledContext.trim()) {
56
+ parts.push(assembledContext);
57
+ }
58
+ if (directive.prompt) {
59
+ parts.push(directive.prompt);
60
+ }
61
+ if (directive.tools) {
62
+ const toolLines = ['## Tool Guidance', ''];
63
+ if (directive.tools.include.length > 0) {
64
+ toolLines.push(`**Preferred tools:** ${directive.tools.include.join(', ')}`);
65
+ }
66
+ if (directive.tools.discouraged.length > 0) {
67
+ toolLines.push(`**Discouraged tools:** ${directive.tools.discouraged.join(', ')}`);
68
+ }
69
+ if (directive.tools.guidance) {
70
+ toolLines.push('');
71
+ toolLines.push(directive.tools.guidance);
72
+ }
73
+ parts.push(toolLines.join('\n'));
74
+ }
75
+ return { content: parts.join('\n\n'), manifest };
76
+ }
77
+ //# sourceMappingURL=directive-assembly.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directive-assembly.js","sourceRoot":"","sources":["../../src/directive/directive-assembly.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EACL,iCAAiC,GAGlC,MAAM,uBAAuB,CAAC;AAC/B,kHAAkH;AAClH,OAAO,0BAA0B,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAQ3D;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,MAAoB,EACpB,OAAe,EACf,SAAyB,EACzB,YAA2B;IAE3B,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,oCAAoC,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IACzG,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oCAAoC,CACxD,MAAoB,EACpB,OAAe,EACf,SAAyB,EACzB,YAA2B;IAE3B,MAAM,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;IAEvC,IAAI,gBAAgB,GAAG,EAAE,CAAC;IAC1B,IAAI,QAAQ,GAA4B,EAAE,CAAC;IAC3C,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,MAAM,kBAAkB,CAAC,OAAO,CAAC,CAAC;YACrD,MAAM,eAAe,GAAoB;gBACvC,OAAO;gBACP,WAAW,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACtC,YAAY;gBACZ,MAAM;gBACN,cAAc,EAAE,MAAM,CAAC,cAAc;gBACrC,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B,CAAC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AAE,CAAC,CAAC;YACnB,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC3C,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC;AACnD,CAAC"}
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Directive dispatch: execute a stage directive against a focus.
3
+ *
4
+ * Resolves the directive's execution mode, then dispatches: inject mode
5
+ * writes /compact + assembled content to the live team's stdin; spawn mode
6
+ * terminates the current team and stashes a PendingSpawnDirective for the
7
+ * next spawn cycle. Also holds the bounded-retry runner + the escalation
8
+ * path for exhausted retries. Extracted from directive-executor.ts; the
9
+ * executor re-exports the public surface so existing importers are
10
+ * unaffected.
11
+ */
12
+ import type { DaemonConfig, StageDirective, DirectiveExecutionMode } from '../types.js';
13
+ import { type LineageSpec } from '../session-lineage.js';
14
+ import { createEscalation } from '../queries/issues.js';
15
+ /**
16
+ * Resolve the execution mode a directive should dispatch under.
17
+ *
18
+ * Pure + exported for testing. The rule:
19
+ * - A valid mode ('inject' | 'spawn') passes through unchanged.
20
+ * - A review-lineage directive whose execution is missing/invalid defaults
21
+ * to 'spawn'. Review always spawns a fresh team, so 'spawn' is the only
22
+ * sensible mode for that lineage; defaulting here self-heals malformed or
23
+ * seed-drifted review directives (e.g. the focus 'review' stage whose
24
+ * agent_directive lost its execution field -- see the backfill migration)
25
+ * so the review-spawn path can no longer be silently stranded.
26
+ * - Anything else is UNRESOLVABLE (returns null). The caller treats null as
27
+ * a fail-loud condition (throw -> escalate + loop trigger) rather than a
28
+ * silent no-op.
29
+ *
30
+ * Note: a review-lineage directive defaults to spawn even for a non-empty but
31
+ * unrecognized execution value -- review is always spawnable, so we self-heal
32
+ * rather than fail. Non-review lineages have no safe default and fail loud.
33
+ */
34
+ export declare function resolveDirectiveExecutionMode(execution: string | null | undefined, lineage: string): DirectiveExecutionMode | null;
35
+ /**
36
+ * Execute a stage directive for a focus.
37
+ *
38
+ * inject mode: sends /compact + assembled context + prompt to the active team's stdin.
39
+ * spawn mode: terminates the current team, assembles directive content, stores for respawn.
40
+ *
41
+ * When the directive's execution mode cannot be resolved to a valid mode
42
+ * (see resolveDirectiveExecutionMode), this THROWS instead of silently
43
+ * no-oping. The throw propagates through runDirectiveWithRetry in
44
+ * checkDirectives, which escalates and emits a loop trigger -- so a malformed
45
+ * directive surfaces a signal instead of stranding the focus.
46
+ *
47
+ * Exported for testing.
48
+ */
49
+ export declare function executeDirective(config: DaemonConfig, focusId: string, directive: StageDirective, focusWorkflowStageId: string, stageName?: string): Promise<void>;
50
+ /**
51
+ * Inject mode: send /compact + assembled context + prompt to the active team's stdin.
52
+ */
53
+ export declare function executeInjectDirective(config: DaemonConfig, focusId: string, directive: StageDirective, sessionType?: 'coding' | 'review', spec?: LineageSpec): Promise<void>;
54
+ /**
55
+ * Result of a bounded-retry attempt to run a directive. The retry is on
56
+ * ANY thrown error (assembly resolver throws, fetch failures, Supabase
57
+ * timeouts) -- broader than `withRetry` from daemon-core, which classifies
58
+ * by error type. Transient failures during directive execution are not
59
+ * always classified as network errors, but they're still worth retrying
60
+ * before treating as permanent.
61
+ */
62
+ export interface DirectiveRunResult {
63
+ /** Whether the directive ran to completion within the retry bound. */
64
+ ok: boolean;
65
+ /** Number of attempts made (1 = first attempt succeeded). */
66
+ attempts: number;
67
+ /** Last error if the retry bound was exhausted, null on success. */
68
+ error: Error | null;
69
+ }
70
+ export interface RunDirectiveOptions {
71
+ maxAttempts?: number;
72
+ baseDelayMs?: number;
73
+ label: string;
74
+ /** Sleep implementation -- override in tests to avoid real delays. */
75
+ sleep?: (ms: number) => Promise<void>;
76
+ /** Called once per failed attempt before the backoff sleep. */
77
+ onAttemptFailed?: (attempt: number, error: Error) => void;
78
+ }
79
+ /**
80
+ * Run a directive function with bounded retry and exponential backoff.
81
+ * Returns a result tuple instead of throwing so the caller can decide
82
+ * whether to escalate or continue.
83
+ */
84
+ export declare function runDirectiveWithRetry(fn: () => Promise<void>, options: RunDirectiveOptions): Promise<DirectiveRunResult>;
85
+ /**
86
+ * Escalate when bounded retry of a stage directive is exhausted.
87
+ *
88
+ * Visible signal so operators can find which focus + stage failed and
89
+ * why, instead of a buried console.warn. Mirrors escalatePlanningFailure
90
+ * from focus-completion-event.ts.
91
+ */
92
+ export declare function escalateDirectiveFailure(params: {
93
+ organizationId: string;
94
+ sessionId: string;
95
+ focusId: string;
96
+ focusName: string;
97
+ stageName: string | undefined;
98
+ error: Error;
99
+ attempts: number;
100
+ }, deps?: {
101
+ createEscalation: typeof createEscalation;
102
+ }): Promise<void>;
103
+ //# sourceMappingURL=directive-dispatch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directive-dispatch.d.ts","sourceRoot":"","sources":["../../src/directive/directive-dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAExF,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAG7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAWxD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACpC,OAAO,EAAE,MAAM,GACd,sBAAsB,GAAG,IAAI,CAI/B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,cAAc,EACzB,oBAAoB,EAAE,MAAM,EAC5B,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAiCf;AAED;;GAE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