@caupulican/pi-adaptative 0.86.2 → 0.86.5

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 (226) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/core/agent-paths.d.ts +13 -0
  3. package/dist/core/agent-paths.d.ts.map +1 -1
  4. package/dist/core/agent-paths.js +23 -0
  5. package/dist/core/agent-paths.js.map +1 -1
  6. package/dist/core/autonomy/bounded-completion.d.ts +7 -0
  7. package/dist/core/autonomy/bounded-completion.d.ts.map +1 -1
  8. package/dist/core/autonomy/bounded-completion.js +12 -4
  9. package/dist/core/autonomy/bounded-completion.js.map +1 -1
  10. package/dist/core/autonomy/lane-tool-surface.d.ts +2 -0
  11. package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
  12. package/dist/core/autonomy/lane-tool-surface.js +6 -3
  13. package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
  14. package/dist/core/autonomy/subagent-prompt.js +1 -1
  15. package/dist/core/autonomy/subagent-prompt.js.map +1 -1
  16. package/dist/core/background-lane-controller.d.ts +25 -2
  17. package/dist/core/background-lane-controller.d.ts.map +1 -1
  18. package/dist/core/background-lane-controller.js +67 -0
  19. package/dist/core/background-lane-controller.js.map +1 -1
  20. package/dist/core/delegation/replay-safe-mailbox-bounds.d.ts +4 -0
  21. package/dist/core/delegation/replay-safe-mailbox-bounds.d.ts.map +1 -0
  22. package/dist/core/delegation/replay-safe-mailbox-bounds.js +20 -0
  23. package/dist/core/delegation/replay-safe-mailbox-bounds.js.map +1 -0
  24. package/dist/core/delegation/sanitized-context-fork.d.ts +25 -0
  25. package/dist/core/delegation/sanitized-context-fork.d.ts.map +1 -0
  26. package/dist/core/delegation/sanitized-context-fork.js +299 -0
  27. package/dist/core/delegation/sanitized-context-fork.js.map +1 -0
  28. package/dist/core/delegation/session-root-mailbox.d.ts +75 -0
  29. package/dist/core/delegation/session-root-mailbox.d.ts.map +1 -0
  30. package/dist/core/delegation/session-root-mailbox.js +627 -0
  31. package/dist/core/delegation/session-root-mailbox.js.map +1 -0
  32. package/dist/core/delegation/worker-agent-control-coordinator.d.ts +102 -9
  33. package/dist/core/delegation/worker-agent-control-coordinator.d.ts.map +1 -1
  34. package/dist/core/delegation/worker-agent-control-coordinator.js +1307 -125
  35. package/dist/core/delegation/worker-agent-control-coordinator.js.map +1 -1
  36. package/dist/core/delegation/worker-agent-control.d.ts +183 -8
  37. package/dist/core/delegation/worker-agent-control.d.ts.map +1 -1
  38. package/dist/core/delegation/worker-agent-control.js +778 -48
  39. package/dist/core/delegation/worker-agent-control.js.map +1 -1
  40. package/dist/core/delegation/worker-attempt-executor.d.ts +9 -0
  41. package/dist/core/delegation/worker-attempt-executor.d.ts.map +1 -1
  42. package/dist/core/delegation/worker-attempt-executor.js +479 -214
  43. package/dist/core/delegation/worker-attempt-executor.js.map +1 -1
  44. package/dist/core/delegation/worker-authority-resolver.d.ts +9 -0
  45. package/dist/core/delegation/worker-authority-resolver.d.ts.map +1 -1
  46. package/dist/core/delegation/worker-authority-resolver.js +22 -14
  47. package/dist/core/delegation/worker-authority-resolver.js.map +1 -1
  48. package/dist/core/delegation/worker-context-fork-store.d.ts +71 -0
  49. package/dist/core/delegation/worker-context-fork-store.d.ts.map +1 -0
  50. package/dist/core/delegation/worker-context-fork-store.js +437 -0
  51. package/dist/core/delegation/worker-context-fork-store.js.map +1 -0
  52. package/dist/core/delegation/worker-context-inheritance-policy.d.ts +18 -0
  53. package/dist/core/delegation/worker-context-inheritance-policy.d.ts.map +1 -0
  54. package/dist/core/delegation/worker-context-inheritance-policy.js +28 -0
  55. package/dist/core/delegation/worker-context-inheritance-policy.js.map +1 -0
  56. package/dist/core/delegation/worker-conversation-revision.d.ts +31 -0
  57. package/dist/core/delegation/worker-conversation-revision.d.ts.map +1 -0
  58. package/dist/core/delegation/worker-conversation-revision.js +203 -0
  59. package/dist/core/delegation/worker-conversation-revision.js.map +1 -0
  60. package/dist/core/delegation/worker-conversation-store.d.ts +130 -18
  61. package/dist/core/delegation/worker-conversation-store.d.ts.map +1 -1
  62. package/dist/core/delegation/worker-conversation-store.js +1263 -79
  63. package/dist/core/delegation/worker-conversation-store.js.map +1 -1
  64. package/dist/core/delegation/worker-delegation-controller.d.ts +29 -2
  65. package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -1
  66. package/dist/core/delegation/worker-delegation-controller.js +625 -111
  67. package/dist/core/delegation/worker-delegation-controller.js.map +1 -1
  68. package/dist/core/delegation/worker-delegation-request.d.ts +2 -0
  69. package/dist/core/delegation/worker-delegation-request.d.ts.map +1 -1
  70. package/dist/core/delegation/worker-delegation-request.js.map +1 -1
  71. package/dist/core/delegation/worker-dispatch-scheduler.d.ts +20 -1
  72. package/dist/core/delegation/worker-dispatch-scheduler.d.ts.map +1 -1
  73. package/dist/core/delegation/worker-dispatch-scheduler.js +210 -37
  74. package/dist/core/delegation/worker-dispatch-scheduler.js.map +1 -1
  75. package/dist/core/delegation/worker-fleet-limits.d.ts +38 -0
  76. package/dist/core/delegation/worker-fleet-limits.d.ts.map +1 -0
  77. package/dist/core/delegation/worker-fleet-limits.js +113 -0
  78. package/dist/core/delegation/worker-fleet-limits.js.map +1 -0
  79. package/dist/core/delegation/worker-lane-projection.d.ts +1 -0
  80. package/dist/core/delegation/worker-lane-projection.d.ts.map +1 -1
  81. package/dist/core/delegation/worker-lane-projection.js +6 -2
  82. package/dist/core/delegation/worker-lane-projection.js.map +1 -1
  83. package/dist/core/delegation/worker-lifecycle.d.ts +20 -3
  84. package/dist/core/delegation/worker-lifecycle.d.ts.map +1 -1
  85. package/dist/core/delegation/worker-lifecycle.js +42 -18
  86. package/dist/core/delegation/worker-lifecycle.js.map +1 -1
  87. package/dist/core/delegation/worker-notification-coordinator.d.ts +4 -0
  88. package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -1
  89. package/dist/core/delegation/worker-notification-coordinator.js +45 -4
  90. package/dist/core/delegation/worker-notification-coordinator.js.map +1 -1
  91. package/dist/core/delegation/worker-provider-turn-protocol.d.ts +50 -0
  92. package/dist/core/delegation/worker-provider-turn-protocol.d.ts.map +1 -0
  93. package/dist/core/delegation/worker-provider-turn-protocol.js +185 -0
  94. package/dist/core/delegation/worker-provider-turn-protocol.js.map +1 -0
  95. package/dist/core/delegation/worker-recovery-coordinator.d.ts +45 -4
  96. package/dist/core/delegation/worker-recovery-coordinator.d.ts.map +1 -1
  97. package/dist/core/delegation/worker-recovery-coordinator.js +223 -17
  98. package/dist/core/delegation/worker-recovery-coordinator.js.map +1 -1
  99. package/dist/core/delegation/worker-retry-policy.d.ts +36 -0
  100. package/dist/core/delegation/worker-retry-policy.d.ts.map +1 -0
  101. package/dist/core/delegation/worker-retry-policy.js +41 -0
  102. package/dist/core/delegation/worker-retry-policy.js.map +1 -0
  103. package/dist/core/delegation/worker-runner.d.ts +3 -1
  104. package/dist/core/delegation/worker-runner.d.ts.map +1 -1
  105. package/dist/core/delegation/worker-runner.js +4 -2
  106. package/dist/core/delegation/worker-runner.js.map +1 -1
  107. package/dist/core/delegation/worker-task-view.d.ts +43 -0
  108. package/dist/core/delegation/worker-task-view.d.ts.map +1 -0
  109. package/dist/core/delegation/worker-task-view.js +194 -0
  110. package/dist/core/delegation/worker-task-view.js.map +1 -0
  111. package/dist/core/delegation/worker-terminal-handoff-coordinator.d.ts +55 -0
  112. package/dist/core/delegation/worker-terminal-handoff-coordinator.d.ts.map +1 -0
  113. package/dist/core/delegation/worker-terminal-handoff-coordinator.js +179 -0
  114. package/dist/core/delegation/worker-terminal-handoff-coordinator.js.map +1 -0
  115. package/dist/core/delegation/worker-tree-budget-coordinator.d.ts +2 -2
  116. package/dist/core/delegation/worker-tree-budget-coordinator.d.ts.map +1 -1
  117. package/dist/core/delegation/worker-tree-budget-coordinator.js +12 -46
  118. package/dist/core/delegation/worker-tree-budget-coordinator.js.map +1 -1
  119. package/dist/core/keybindings.d.ts +10 -0
  120. package/dist/core/keybindings.d.ts.map +1 -1
  121. package/dist/core/keybindings.js +2 -0
  122. package/dist/core/keybindings.js.map +1 -1
  123. package/dist/core/orchestration/attempt-ordering.d.ts +9 -0
  124. package/dist/core/orchestration/attempt-ordering.d.ts.map +1 -0
  125. package/dist/core/orchestration/attempt-ordering.js +29 -0
  126. package/dist/core/orchestration/attempt-ordering.js.map +1 -0
  127. package/dist/core/orchestration/capability-gateway.d.ts +19 -0
  128. package/dist/core/orchestration/capability-gateway.d.ts.map +1 -1
  129. package/dist/core/orchestration/capability-gateway.js +19 -2
  130. package/dist/core/orchestration/capability-gateway.js.map +1 -1
  131. package/dist/core/orchestration/contracts.d.ts +43 -3
  132. package/dist/core/orchestration/contracts.d.ts.map +1 -1
  133. package/dist/core/orchestration/contracts.js +24 -0
  134. package/dist/core/orchestration/contracts.js.map +1 -1
  135. package/dist/core/orchestration/delegation-ledger.d.ts +8 -3
  136. package/dist/core/orchestration/delegation-ledger.d.ts.map +1 -1
  137. package/dist/core/orchestration/delegation-ledger.js +148 -55
  138. package/dist/core/orchestration/delegation-ledger.js.map +1 -1
  139. package/dist/core/orchestration/event-store.d.ts +6 -3
  140. package/dist/core/orchestration/event-store.d.ts.map +1 -1
  141. package/dist/core/orchestration/event-store.js +228 -30
  142. package/dist/core/orchestration/event-store.js.map +1 -1
  143. package/dist/core/orchestration/task-runtime-codecs.d.ts +31 -0
  144. package/dist/core/orchestration/task-runtime-codecs.d.ts.map +1 -0
  145. package/dist/core/orchestration/task-runtime-codecs.js +1471 -0
  146. package/dist/core/orchestration/task-runtime-codecs.js.map +1 -0
  147. package/dist/core/orchestration/task-runtime-projection.d.ts +30 -0
  148. package/dist/core/orchestration/task-runtime-projection.d.ts.map +1 -0
  149. package/dist/core/orchestration/task-runtime-projection.js +340 -0
  150. package/dist/core/orchestration/task-runtime-projection.js.map +1 -0
  151. package/dist/core/orchestration/task-runtime-reducer.d.ts +38 -0
  152. package/dist/core/orchestration/task-runtime-reducer.d.ts.map +1 -0
  153. package/dist/core/orchestration/task-runtime-reducer.js +1270 -0
  154. package/dist/core/orchestration/task-runtime-reducer.js.map +1 -0
  155. package/dist/core/orchestration/task-runtime-state.d.ts +142 -0
  156. package/dist/core/orchestration/task-runtime-state.d.ts.map +1 -0
  157. package/dist/core/orchestration/task-runtime-state.js +14 -0
  158. package/dist/core/orchestration/task-runtime-state.js.map +1 -0
  159. package/dist/core/orchestration/task-runtime.d.ts +44 -98
  160. package/dist/core/orchestration/task-runtime.d.ts.map +1 -1
  161. package/dist/core/orchestration/task-runtime.js +357 -1113
  162. package/dist/core/orchestration/task-runtime.js.map +1 -1
  163. package/dist/core/orchestration/worker-context-fork-reference.d.ts +20 -0
  164. package/dist/core/orchestration/worker-context-fork-reference.d.ts.map +1 -0
  165. package/dist/core/orchestration/worker-context-fork-reference.js +53 -0
  166. package/dist/core/orchestration/worker-context-fork-reference.js.map +1 -0
  167. package/dist/core/research/research-runner.d.ts +2 -0
  168. package/dist/core/research/research-runner.d.ts.map +1 -1
  169. package/dist/core/research/research-runner.js +1 -0
  170. package/dist/core/research/research-runner.js.map +1 -1
  171. package/dist/core/runtime-builder.d.ts.map +1 -1
  172. package/dist/core/runtime-builder.js +8 -0
  173. package/dist/core/runtime-builder.js.map +1 -1
  174. package/dist/core/secrets/credential-project.d.ts.map +1 -1
  175. package/dist/core/secrets/credential-project.js +24 -2
  176. package/dist/core/secrets/credential-project.js.map +1 -1
  177. package/dist/core/security/secret-text.d.ts +3 -0
  178. package/dist/core/security/secret-text.d.ts.map +1 -1
  179. package/dist/core/security/secret-text.js +15 -0
  180. package/dist/core/security/secret-text.js.map +1 -1
  181. package/dist/core/settings-manager.d.ts +3 -0
  182. package/dist/core/settings-manager.d.ts.map +1 -1
  183. package/dist/core/settings-manager.js +160 -8
  184. package/dist/core/settings-manager.js.map +1 -1
  185. package/dist/core/tools/delegate.d.ts +31 -4
  186. package/dist/core/tools/delegate.d.ts.map +1 -1
  187. package/dist/core/tools/delegate.js +751 -98
  188. package/dist/core/tools/delegate.js.map +1 -1
  189. package/dist/main.d.ts.map +1 -1
  190. package/dist/main.js +1 -0
  191. package/dist/main.js.map +1 -1
  192. package/dist/modes/interactive/components/activity-lane.d.ts +5 -0
  193. package/dist/modes/interactive/components/activity-lane.d.ts.map +1 -1
  194. package/dist/modes/interactive/components/activity-lane.js +133 -49
  195. package/dist/modes/interactive/components/activity-lane.js.map +1 -1
  196. package/dist/modes/interactive/components/agents-overlay.d.ts +40 -0
  197. package/dist/modes/interactive/components/agents-overlay.d.ts.map +1 -0
  198. package/dist/modes/interactive/components/agents-overlay.js +153 -0
  199. package/dist/modes/interactive/components/agents-overlay.js.map +1 -0
  200. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  201. package/dist/modes/interactive/components/settings-selector.js +18 -13
  202. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  203. package/dist/modes/interactive/interactive-event-controller.d.ts +1 -1
  204. package/dist/modes/interactive/interactive-event-controller.d.ts.map +1 -1
  205. package/dist/modes/interactive/interactive-event-controller.js +6 -3
  206. package/dist/modes/interactive/interactive-event-controller.js.map +1 -1
  207. package/dist/modes/interactive/interactive-mode.d.ts +5 -0
  208. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  209. package/dist/modes/interactive/interactive-mode.js +51 -0
  210. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  211. package/dist/modes/interactive/key-handlers.d.ts +1 -0
  212. package/dist/modes/interactive/key-handlers.d.ts.map +1 -1
  213. package/dist/modes/interactive/key-handlers.js +1 -0
  214. package/dist/modes/interactive/key-handlers.js.map +1 -1
  215. package/docs/harness-architecture.md +61 -17
  216. package/docs/settings.md +17 -9
  217. package/docs/worker-profiles.md +6 -2
  218. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  219. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  220. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  221. package/examples/extensions/sandbox/package-lock.json +2 -2
  222. package/examples/extensions/sandbox/package.json +1 -1
  223. package/examples/extensions/with-deps/package-lock.json +2 -2
  224. package/examples/extensions/with-deps/package.json +1 -1
  225. package/npm-shrinkwrap.json +12 -12
  226. package/package.json +4 -4
@@ -36,6 +36,8 @@ export interface WorkerDelegationRequest {
36
36
  instructions: string;
37
37
  profileId?: string;
38
38
  authority?: WorkerDelegationAuthorityRequest;
39
+ /** Tool-facing immutable birth-context selection for a newly admitted logical worker. */
40
+ forkTurns?: string;
39
41
  /** Runtime-owned creator identity for recursive delegation; never accepted from a model argument. */
40
42
  parentAgentId?: string;
41
43
  /** Runtime-owned correlation for an automatically dispatched verifier; never model-settable. */
@@ -1 +1 @@
1
- {"version":3,"file":"worker-delegation-request.d.ts","sourceRoot":"","sources":["../../../src/core/delegation/worker-delegation-request.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,iBAAiB,EAOtB,KAAK,0BAA0B,EAC/B,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,MAAM,+BAA+B,CAAC;AAIvC,oGAAoG;AACpG,MAAM,WAAW,2BAA2B;IAC3C,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,sBAAsB,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1C;;;;OAIG;IACH,kBAAkB,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAED,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,gGAAgG;AAChG,MAAM,WAAW,gCAAgC;IAChD,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,KAAK,CAAC,EAAE,4BAA4B,CAAC;IACrC,aAAa,CAAC,EAAE,0BAA0B,CAAC;IAC3C,YAAY,CAAC,EAAE,SAAS,iBAAiB,EAAE,CAAC;IAC5C,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB;AAED,qBAAa,4BAA6B,SAAQ,KAAK;IACtD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAYD,6FAA6F;AAC7F,wBAAgB,qCAAqC,CAAC,KAAK,EAAE,OAAO,GAAG,gCAAgC,CA4EtG;AAED,wFAAwF;AACxF,MAAM,WAAW,uBAAuB;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,gCAAgC,CAAC;IAC7C,qGAAqG;IACrG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,gGAAgG;IAChG,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iGAAiG;IACjG,WAAW,CAAC,EAAE,2BAA2B,CAAC;CAC1C"}
1
+ {"version":3,"file":"worker-delegation-request.d.ts","sourceRoot":"","sources":["../../../src/core/delegation/worker-delegation-request.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,iBAAiB,EAOtB,KAAK,0BAA0B,EAC/B,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,MAAM,+BAA+B,CAAC;AAIvC,oGAAoG;AACpG,MAAM,WAAW,2BAA2B;IAC3C,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,gBAAgB,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,sBAAsB,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1C;;;;OAIG;IACH,kBAAkB,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAED,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,gGAAgG;AAChG,MAAM,WAAW,gCAAgC;IAChD,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,KAAK,CAAC,EAAE,4BAA4B,CAAC;IACrC,aAAa,CAAC,EAAE,0BAA0B,CAAC;IAC3C,YAAY,CAAC,EAAE,SAAS,iBAAiB,EAAE,CAAC;IAC5C,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB;AAED,qBAAa,4BAA6B,SAAQ,KAAK;IACtD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAYD,6FAA6F;AAC7F,wBAAgB,qCAAqC,CAAC,KAAK,EAAE,OAAO,GAAG,gCAAgC,CA4EtG;AAED,wFAAwF;AACxF,MAAM,WAAW,uBAAuB;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,gCAAgC,CAAC;IAC7C,yFAAyF;IACzF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qGAAqG;IACrG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,gGAAgG;IAChG,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iGAAiG;IACjG,WAAW,CAAC,EAAE,2BAA2B,CAAC;CAC1C"}
@@ -1 +1 @@
1
- {"version":3,"file":"worker-delegation-request.js","sourceRoot":"","sources":["../../../src/core/delegation/worker-delegation-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAEN,mBAAmB,EACnB,iCAAiC,EACjC,uCAAuC,EACvC,gCAAgC,EAChC,0BAA0B,EAC1B,6BAA6B,EAG7B,YAAY,GAEZ,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAgCrE,MAAM,OAAO,4BA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G,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACzC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7B,CAAC;AACH,CAAC","sourcesContent":["import { parseBoundedStringArray } from \"../orchestration/bounded-string-array.ts\";\nimport {\n\ttype HarnessCapability,\n\tisHarnessCapability,\n\tMAX_ORCHESTRATION_MODEL_ID_LENGTH,\n\tMAX_ORCHESTRATION_MODEL_PROVIDER_LENGTH,\n\tMAX_WORKER_AUTHORITY_PATH_LENGTH,\n\tMAX_WORKER_AUTHORITY_PATHS,\n\tORCHESTRATION_THINKING_LEVELS,\n\ttype OrchestrationThinkingLevel,\n\ttype RiskBudget,\n\tWORKER_ROLES,\n\ttype WorkerRole,\n} from \"../orchestration/contracts.ts\";\nimport { parseRiskBudget } from \"../orchestration/risk-budget.ts\";\nimport { hasOnlyKeys, isPlainRecord } from \"../util/value-guards.ts\";\n\n/** Runtime-owned task correlation retained with a durable worker dispatch. Never model-settable. */\nexport interface WorkerDelegationTaskContext {\n\trequirementIds: readonly string[];\n\tdependsOnTaskIds: readonly string[];\n\tacceptanceCriterionIds: readonly string[];\n\t/**\n\t * Optional runtime narrowing of the owner-admitted profile resources. An empty list does not\n\t * suppress owner resources: admission expands it to the immutable profile pointer set and\n\t * persists that exact selection with the attempt.\n\t */\n\tresourcePointerIds: readonly string[];\n}\n\nexport interface WorkerDelegationModelRequest {\n\tprovider: string;\n\tmodelId: string;\n}\n\n/** Model-authored execution preferences compiled into an immutable, inherited runtime grant. */\nexport interface WorkerDelegationAuthorityRequest {\n\trole?: WorkerRole;\n\tmodel?: WorkerDelegationModelRequest;\n\tthinkingLevel?: OrchestrationThinkingLevel;\n\tcapabilities?: readonly HarnessCapability[];\n\ttoolNames?: readonly string[];\n\treadPaths?: readonly string[];\n\twritePaths?: readonly string[];\n\tbudget?: RiskBudget;\n}\n\nexport class WorkerDelegationRequestError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"WorkerDelegationRequestError\";\n\t}\n}\n\nfunction uniqueStringArray(value: unknown, label: string, options: { maxEntries?: number; maxLength?: number } = {}) {\n\treturn parseBoundedStringArray(value, {\n\t\t...options,\n\t\ttrim: true,\n\t\tinvalidMessage: `${label} must contain bounded, non-empty strings.`,\n\t\tduplicateMessage: `${label} must contain unique strings.`,\n\t\tcreateError: (message) => new WorkerDelegationRequestError(message),\n\t});\n}\n\n/** Parse the model-facing authority request before it reaches admission or durable state. */\nexport function parseWorkerDelegationAuthorityRequest(value: unknown): WorkerDelegationAuthorityRequest {\n\tif (!isPlainRecord(value)) throw new WorkerDelegationRequestError(\"Delegation authority must be an object.\");\n\tif (\n\t\t!hasOnlyKeys(value, [\n\t\t\t\"role\",\n\t\t\t\"model\",\n\t\t\t\"thinkingLevel\",\n\t\t\t\"capabilities\",\n\t\t\t\"toolNames\",\n\t\t\t\"readPaths\",\n\t\t\t\"writePaths\",\n\t\t\t\"budget\",\n\t\t])\n\t) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority contains an unsupported field.\");\n\t}\n\tif (value.role !== undefined && !WORKER_ROLES.includes(value.role as WorkerRole)) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority role is invalid.\");\n\t}\n\tif (\n\t\tvalue.thinkingLevel !== undefined &&\n\t\t!ORCHESTRATION_THINKING_LEVELS.includes(value.thinkingLevel as OrchestrationThinkingLevel)\n\t) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority thinkingLevel is invalid.\");\n\t}\n\tlet model: WorkerDelegationModelRequest | undefined;\n\tif (value.model !== undefined) {\n\t\tif (\n\t\t\t!isPlainRecord(value.model) ||\n\t\t\t!hasOnlyKeys(value.model, [\"provider\", \"modelId\"]) ||\n\t\t\ttypeof value.model.provider !== \"string\" ||\n\t\t\t!value.model.provider.trim() ||\n\t\t\tvalue.model.provider.length > MAX_ORCHESTRATION_MODEL_PROVIDER_LENGTH ||\n\t\t\ttypeof value.model.modelId !== \"string\" ||\n\t\t\t!value.model.modelId.trim() ||\n\t\t\tvalue.model.modelId.length > MAX_ORCHESTRATION_MODEL_ID_LENGTH\n\t\t) {\n\t\t\tthrow new WorkerDelegationRequestError(\"Delegation authority model is invalid.\");\n\t\t}\n\t\tmodel = { provider: value.model.provider.trim(), modelId: value.model.modelId.trim() };\n\t}\n\tlet capabilities: HarnessCapability[] | undefined;\n\tif (value.capabilities !== undefined) {\n\t\tconst parsed = uniqueStringArray(value.capabilities, \"Delegation authority capabilities\");\n\t\tif (!parsed.every(isHarnessCapability)) {\n\t\t\tthrow new WorkerDelegationRequestError(\"Delegation authority contains an unknown capability.\");\n\t\t}\n\t\tcapabilities = parsed;\n\t}\n\tconst toolNames =\n\t\tvalue.toolNames === undefined ? undefined : uniqueStringArray(value.toolNames, \"Delegation authority toolNames\");\n\tconst pathOptions = { maxEntries: MAX_WORKER_AUTHORITY_PATHS, maxLength: MAX_WORKER_AUTHORITY_PATH_LENGTH };\n\tconst readPaths =\n\t\tvalue.readPaths === undefined\n\t\t\t? undefined\n\t\t\t: uniqueStringArray(value.readPaths, \"Delegation authority readPaths\", pathOptions);\n\tconst writePaths =\n\t\tvalue.writePaths === undefined\n\t\t\t? undefined\n\t\t\t: uniqueStringArray(value.writePaths, \"Delegation authority writePaths\", pathOptions);\n\tlet budget: RiskBudget | undefined;\n\ttry {\n\t\tbudget = value.budget === undefined ? undefined : parseRiskBudget(value.budget, \"Delegation authority budget\");\n\t} catch (error) {\n\t\tthrow new WorkerDelegationRequestError(error instanceof Error ? error.message : String(error));\n\t}\n\treturn {\n\t\t...(value.role ? { role: value.role as WorkerRole } : {}),\n\t\t...(model ? { model } : {}),\n\t\t...(value.thinkingLevel ? { thinkingLevel: value.thinkingLevel as OrchestrationThinkingLevel } : {}),\n\t\t...(capabilities ? { capabilities } : {}),\n\t\t...(toolNames ? { toolNames } : {}),\n\t\t...(readPaths ? { readPaths } : {}),\n\t\t...(writePaths ? { writePaths } : {}),\n\t\t...(budget ? { budget } : {}),\n\t};\n}\n\n/** A profile is an optional preset; authority is compiled and persisted by the host. */\nexport interface WorkerDelegationRequest {\n\tinstructions: string;\n\tprofileId?: string;\n\tauthority?: WorkerDelegationAuthorityRequest;\n\t/** Runtime-owned creator identity for recursive delegation; never accepted from a model argument. */\n\tparentAgentId?: string;\n\t/** Runtime-owned correlation for an automatically dispatched verifier; never model-settable. */\n\tverificationOfTaskId?: string;\n\t/** Runtime-owned durable task correlation; the delegate tool schema intentionally omits this. */\n\ttaskContext?: WorkerDelegationTaskContext;\n}\n"]}
1
+ {"version":3,"file":"worker-delegation-request.js","sourceRoot":"","sources":["../../../src/core/delegation/worker-delegation-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAEN,mBAAmB,EACnB,iCAAiC,EACjC,uCAAuC,EACvC,gCAAgC,EAChC,0BAA0B,EAC1B,6BAA6B,EAG7B,YAAY,GAEZ,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAgCrE,MAAM,OAAO,4BAA6B,SAAQ,KAAK;IACtD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,8BAA8B,CAAC;IAC5C,CAAC;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AAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7B,CAAC;AACH,CAAC","sourcesContent":["import { parseBoundedStringArray } from \"../orchestration/bounded-string-array.ts\";\nimport {\n\ttype HarnessCapability,\n\tisHarnessCapability,\n\tMAX_ORCHESTRATION_MODEL_ID_LENGTH,\n\tMAX_ORCHESTRATION_MODEL_PROVIDER_LENGTH,\n\tMAX_WORKER_AUTHORITY_PATH_LENGTH,\n\tMAX_WORKER_AUTHORITY_PATHS,\n\tORCHESTRATION_THINKING_LEVELS,\n\ttype OrchestrationThinkingLevel,\n\ttype RiskBudget,\n\tWORKER_ROLES,\n\ttype WorkerRole,\n} from \"../orchestration/contracts.ts\";\nimport { parseRiskBudget } from \"../orchestration/risk-budget.ts\";\nimport { hasOnlyKeys, isPlainRecord } from \"../util/value-guards.ts\";\n\n/** Runtime-owned task correlation retained with a durable worker dispatch. Never model-settable. */\nexport interface WorkerDelegationTaskContext {\n\trequirementIds: readonly string[];\n\tdependsOnTaskIds: readonly string[];\n\tacceptanceCriterionIds: readonly string[];\n\t/**\n\t * Optional runtime narrowing of the owner-admitted profile resources. An empty list does not\n\t * suppress owner resources: admission expands it to the immutable profile pointer set and\n\t * persists that exact selection with the attempt.\n\t */\n\tresourcePointerIds: readonly string[];\n}\n\nexport interface WorkerDelegationModelRequest {\n\tprovider: string;\n\tmodelId: string;\n}\n\n/** Model-authored execution preferences compiled into an immutable, inherited runtime grant. */\nexport interface WorkerDelegationAuthorityRequest {\n\trole?: WorkerRole;\n\tmodel?: WorkerDelegationModelRequest;\n\tthinkingLevel?: OrchestrationThinkingLevel;\n\tcapabilities?: readonly HarnessCapability[];\n\ttoolNames?: readonly string[];\n\treadPaths?: readonly string[];\n\twritePaths?: readonly string[];\n\tbudget?: RiskBudget;\n}\n\nexport class WorkerDelegationRequestError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"WorkerDelegationRequestError\";\n\t}\n}\n\nfunction uniqueStringArray(value: unknown, label: string, options: { maxEntries?: number; maxLength?: number } = {}) {\n\treturn parseBoundedStringArray(value, {\n\t\t...options,\n\t\ttrim: true,\n\t\tinvalidMessage: `${label} must contain bounded, non-empty strings.`,\n\t\tduplicateMessage: `${label} must contain unique strings.`,\n\t\tcreateError: (message) => new WorkerDelegationRequestError(message),\n\t});\n}\n\n/** Parse the model-facing authority request before it reaches admission or durable state. */\nexport function parseWorkerDelegationAuthorityRequest(value: unknown): WorkerDelegationAuthorityRequest {\n\tif (!isPlainRecord(value)) throw new WorkerDelegationRequestError(\"Delegation authority must be an object.\");\n\tif (\n\t\t!hasOnlyKeys(value, [\n\t\t\t\"role\",\n\t\t\t\"model\",\n\t\t\t\"thinkingLevel\",\n\t\t\t\"capabilities\",\n\t\t\t\"toolNames\",\n\t\t\t\"readPaths\",\n\t\t\t\"writePaths\",\n\t\t\t\"budget\",\n\t\t])\n\t) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority contains an unsupported field.\");\n\t}\n\tif (value.role !== undefined && !WORKER_ROLES.includes(value.role as WorkerRole)) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority role is invalid.\");\n\t}\n\tif (\n\t\tvalue.thinkingLevel !== undefined &&\n\t\t!ORCHESTRATION_THINKING_LEVELS.includes(value.thinkingLevel as OrchestrationThinkingLevel)\n\t) {\n\t\tthrow new WorkerDelegationRequestError(\"Delegation authority thinkingLevel is invalid.\");\n\t}\n\tlet model: WorkerDelegationModelRequest | undefined;\n\tif (value.model !== undefined) {\n\t\tif (\n\t\t\t!isPlainRecord(value.model) ||\n\t\t\t!hasOnlyKeys(value.model, [\"provider\", \"modelId\"]) ||\n\t\t\ttypeof value.model.provider !== \"string\" ||\n\t\t\t!value.model.provider.trim() ||\n\t\t\tvalue.model.provider.length > MAX_ORCHESTRATION_MODEL_PROVIDER_LENGTH ||\n\t\t\ttypeof value.model.modelId !== \"string\" ||\n\t\t\t!value.model.modelId.trim() ||\n\t\t\tvalue.model.modelId.length > MAX_ORCHESTRATION_MODEL_ID_LENGTH\n\t\t) {\n\t\t\tthrow new WorkerDelegationRequestError(\"Delegation authority model is invalid.\");\n\t\t}\n\t\tmodel = { provider: value.model.provider.trim(), modelId: value.model.modelId.trim() };\n\t}\n\tlet capabilities: HarnessCapability[] | undefined;\n\tif (value.capabilities !== undefined) {\n\t\tconst parsed = uniqueStringArray(value.capabilities, \"Delegation authority capabilities\");\n\t\tif (!parsed.every(isHarnessCapability)) {\n\t\t\tthrow new WorkerDelegationRequestError(\"Delegation authority contains an unknown capability.\");\n\t\t}\n\t\tcapabilities = parsed;\n\t}\n\tconst toolNames =\n\t\tvalue.toolNames === undefined ? undefined : uniqueStringArray(value.toolNames, \"Delegation authority toolNames\");\n\tconst pathOptions = { maxEntries: MAX_WORKER_AUTHORITY_PATHS, maxLength: MAX_WORKER_AUTHORITY_PATH_LENGTH };\n\tconst readPaths =\n\t\tvalue.readPaths === undefined\n\t\t\t? undefined\n\t\t\t: uniqueStringArray(value.readPaths, \"Delegation authority readPaths\", pathOptions);\n\tconst writePaths =\n\t\tvalue.writePaths === undefined\n\t\t\t? undefined\n\t\t\t: uniqueStringArray(value.writePaths, \"Delegation authority writePaths\", pathOptions);\n\tlet budget: RiskBudget | undefined;\n\ttry {\n\t\tbudget = value.budget === undefined ? undefined : parseRiskBudget(value.budget, \"Delegation authority budget\");\n\t} catch (error) {\n\t\tthrow new WorkerDelegationRequestError(error instanceof Error ? error.message : String(error));\n\t}\n\treturn {\n\t\t...(value.role ? { role: value.role as WorkerRole } : {}),\n\t\t...(model ? { model } : {}),\n\t\t...(value.thinkingLevel ? { thinkingLevel: value.thinkingLevel as OrchestrationThinkingLevel } : {}),\n\t\t...(capabilities ? { capabilities } : {}),\n\t\t...(toolNames ? { toolNames } : {}),\n\t\t...(readPaths ? { readPaths } : {}),\n\t\t...(writePaths ? { writePaths } : {}),\n\t\t...(budget ? { budget } : {}),\n\t};\n}\n\n/** A profile is an optional preset; authority is compiled and persisted by the host. */\nexport interface WorkerDelegationRequest {\n\tinstructions: string;\n\tprofileId?: string;\n\tauthority?: WorkerDelegationAuthorityRequest;\n\t/** Tool-facing immutable birth-context selection for a newly admitted logical worker. */\n\tforkTurns?: string;\n\t/** Runtime-owned creator identity for recursive delegation; never accepted from a model argument. */\n\tparentAgentId?: string;\n\t/** Runtime-owned correlation for an automatically dispatched verifier; never model-settable. */\n\tverificationOfTaskId?: string;\n\t/** Runtime-owned durable task correlation; the delegate tool schema intentionally omits this. */\n\ttaskContext?: WorkerDelegationTaskContext;\n}\n"]}
@@ -1,17 +1,20 @@
1
1
  import type { WorkerDelegationRunOutcome } from "../agent-session-contracts.ts";
2
2
  import type { LaneRecord } from "../autonomy/lane-tracker.ts";
3
+ import { registerInFlightWork } from "../reload-blockers.ts";
3
4
  import type { WorkerDelegationRequest } from "./worker-delegation-request.ts";
4
5
  export type WorkerDispatchAdmission = {
5
6
  action: "start";
6
7
  } | {
7
8
  action: "wait";
8
- reason?: "capacity" | "write_reservation";
9
+ reason?: "capacity" | "dependencies" | "objective" | "write_reservation";
9
10
  } | {
10
11
  action: "cancel";
11
12
  reasonCode: string;
12
13
  };
13
14
  export interface WorkerDispatchSchedulerOptions {
14
15
  agentDir: string;
16
+ /** Test seam for proving queue insertion rollback when reload-gate registration fails. */
17
+ registerInFlightWork?: typeof registerInFlightWork;
15
18
  isDisposed(): boolean;
16
19
  admit(request: WorkerDelegationRequest, record: LaneRecord): WorkerDispatchAdmission;
17
20
  getRecord(laneId: string): LaneRecord | undefined;
@@ -28,15 +31,31 @@ export declare class WorkerDispatchScheduler {
28
31
  private readonly queued;
29
32
  private readonly queuedDeregisters;
30
33
  private readonly running;
34
+ private readonly pendingCancellations;
31
35
  private readonly reservationBlocked;
36
+ private readonly queueCapacityListeners;
32
37
  private draining;
38
+ private redrainRequested;
39
+ private reservationAvailabilityRequested;
40
+ private queueCapacityNotificationPending;
33
41
  constructor(options: WorkerDispatchSchedulerOptions);
34
42
  get queuedCount(): number;
43
+ hasQueueCapacity(priority?: boolean): boolean;
44
+ /** Notify retained priority work when a bounded queue slot is released. */
45
+ onQueueCapacityAvailable(listener: () => void): () => void;
35
46
  enqueue(record: LaneRecord, request: WorkerDelegationRequest, recovered?: boolean, priority?: boolean): void;
36
47
  track(laneId: string, promise: Promise<WorkerDelegationRunOutcome>): void;
48
+ private retainPendingCancellation;
49
+ private registerPendingCancellation;
50
+ private retryPendingCancellations;
51
+ private removePendingCancellation;
52
+ private cancelBestEffort;
53
+ private finishTrackedRun;
54
+ private warnBestEffort;
37
55
  drain(reservationAvailable?: boolean): void;
38
56
  cancelQueued(): void;
39
57
  dropQueued(laneId: string): boolean;
40
58
  private removeQueued;
59
+ private notifyQueueCapacityAvailable;
41
60
  }
42
61
  //# sourceMappingURL=worker-dispatch-scheduler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"worker-dispatch-scheduler.d.ts","sourceRoot":"","sources":["../../../src/core/delegation/worker-dispatch-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAChF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAE9E,MAAM,MAAM,uBAAuB,GAChC;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GACnB;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,UAAU,GAAG,mBAAmB,CAAA;CAAE,GAC7D;IAA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AED,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,4BAA4B;CAgBpC"}
@@ -1,4 +1,5 @@
1
1
  import { registerInFlightWork } from "../reload-blockers.js";
2
+ import { workerQueueHasCapacity } from "./worker-fleet-limits.js";
2
3
  /**
3
4
  * Single owner of worker queue and promise transitions. Execution policy and durable lifecycle stay
4
5
  * outside; this class only decides when a prepared durable attempt moves from queued to running.
@@ -8,16 +9,34 @@ export class WorkerDispatchScheduler {
8
9
  this.queued = new Map();
9
10
  this.queuedDeregisters = new Map();
10
11
  this.running = new Map();
12
+ this.pendingCancellations = new Map();
11
13
  this.reservationBlocked = new Set();
14
+ this.queueCapacityListeners = new Set();
12
15
  this.draining = false;
16
+ this.redrainRequested = false;
17
+ this.reservationAvailabilityRequested = false;
18
+ this.queueCapacityNotificationPending = false;
13
19
  this.options = options;
14
20
  }
15
21
  get queuedCount() {
16
22
  return this.queued.size;
17
23
  }
24
+ hasQueueCapacity(priority = false) {
25
+ return workerQueueHasCapacity(this.queued.size, priority);
26
+ }
27
+ /** Notify retained priority work when a bounded queue slot is released. */
28
+ onQueueCapacityAvailable(listener) {
29
+ this.queueCapacityListeners.add(listener);
30
+ return () => this.queueCapacityListeners.delete(listener);
31
+ }
18
32
  enqueue(record, request, recovered = false, priority = false) {
19
- if (this.queued.has(record.laneId) || this.running.has(record.laneId))
33
+ if (this.queued.has(record.laneId) ||
34
+ this.running.has(record.laneId) ||
35
+ this.pendingCancellations.has(record.laneId)) {
20
36
  return;
37
+ }
38
+ if (!this.hasQueueCapacity(priority))
39
+ throw new Error("worker_dispatch_queue_full");
21
40
  if (priority) {
22
41
  const waiting = [...this.queued];
23
42
  this.queued.clear();
@@ -28,51 +47,173 @@ export class WorkerDispatchScheduler {
28
47
  else {
29
48
  this.queued.set(record.laneId, request);
30
49
  }
31
- this.queuedDeregisters.set(record.laneId, registerInFlightWork(this.options.agentDir, "lane", recovered ? `worker-recovered:${record.laneId}` : `worker-queued:${record.laneId}`));
50
+ try {
51
+ this.queuedDeregisters.set(record.laneId, (this.options.registerInFlightWork ?? registerInFlightWork)(this.options.agentDir, "lane", recovered ? `worker-recovered:${record.laneId}` : `worker-queued:${record.laneId}`));
52
+ }
53
+ catch (error) {
54
+ // Queue insertion and reload-gate registration are one process-local transition. A failed
55
+ // registration must not leave a lane that appears queued but has no matching blocker.
56
+ this.queued.delete(record.laneId);
57
+ this.reservationBlocked.delete(record.laneId);
58
+ throw error;
59
+ }
32
60
  }
33
61
  track(laneId, promise) {
34
62
  this.running.set(laneId, promise);
35
63
  void promise.then((outcome) => {
36
- if (!outcome.started)
37
- this.options.cancel(laneId, outcome.skipReason ?? "worker_not_started");
38
- this.running.delete(laneId);
39
- if (!this.options.isDisposed())
40
- this.drain();
64
+ try {
65
+ if (!outcome.started) {
66
+ const reasonCode = outcome.skipReason ?? "worker_not_started";
67
+ if (!this.cancelBestEffort(laneId, reasonCode)) {
68
+ this.retainPendingCancellation(laneId, reasonCode);
69
+ }
70
+ }
71
+ }
72
+ finally {
73
+ this.finishTrackedRun(laneId);
74
+ }
41
75
  }, (error) => {
42
- this.options.cancel(laneId, "worker_background_error");
43
- this.options.warn(`Worker ${laneId} rejected: ${error instanceof Error ? error.message : String(error)}`);
44
- this.running.delete(laneId);
45
- if (!this.options.isDisposed())
46
- this.drain();
76
+ try {
77
+ if (!this.cancelBestEffort(laneId, "worker_background_error")) {
78
+ this.retainPendingCancellation(laneId, "worker_background_error");
79
+ }
80
+ this.warnBestEffort(`Worker ${laneId} rejected: ${error instanceof Error ? error.message : String(error)}`);
81
+ }
82
+ finally {
83
+ this.finishTrackedRun(laneId);
84
+ }
47
85
  });
48
86
  }
87
+ retainPendingCancellation(laneId, reasonCode) {
88
+ if (this.pendingCancellations.has(laneId))
89
+ return;
90
+ const pending = { reasonCode };
91
+ this.pendingCancellations.set(laneId, pending);
92
+ this.registerPendingCancellation(laneId, pending);
93
+ }
94
+ registerPendingCancellation(laneId, pending) {
95
+ if (pending.deregister)
96
+ return;
97
+ try {
98
+ pending.deregister = (this.options.registerInFlightWork ?? registerInFlightWork)(this.options.agentDir, "lane", `worker-cancellation-pending:${laneId}`);
99
+ }
100
+ catch (error) {
101
+ this.warnBestEffort(`Worker ${laneId} cancellation reload-gate registration failed: ${error instanceof Error ? error.message : String(error)}`);
102
+ }
103
+ }
104
+ retryPendingCancellations() {
105
+ for (const [laneId, pending] of [...this.pendingCancellations]) {
106
+ this.registerPendingCancellation(laneId, pending);
107
+ if (!this.cancelBestEffort(laneId, pending.reasonCode))
108
+ continue;
109
+ this.removePendingCancellation(laneId);
110
+ }
111
+ }
112
+ removePendingCancellation(laneId) {
113
+ const pending = this.pendingCancellations.get(laneId);
114
+ if (!pending)
115
+ return;
116
+ this.pendingCancellations.delete(laneId);
117
+ try {
118
+ pending.deregister?.();
119
+ }
120
+ catch (error) {
121
+ this.warnBestEffort(`Worker ${laneId} cancellation reload-gate deregistration failed: ${error instanceof Error ? error.message : String(error)}`);
122
+ }
123
+ }
124
+ cancelBestEffort(laneId, reasonCode) {
125
+ try {
126
+ this.options.cancel(laneId, reasonCode);
127
+ return true;
128
+ }
129
+ catch (error) {
130
+ this.warnBestEffort(`Worker ${laneId} cancellation failed: ${error instanceof Error ? error.message : String(error)}`);
131
+ return false;
132
+ }
133
+ }
134
+ finishTrackedRun(laneId) {
135
+ this.running.delete(laneId);
136
+ if (this.options.isDisposed()) {
137
+ // A disposed generation has no future scheduler signal. Its durable state is recovered by the
138
+ // next generation, so do not leak this generation's process-local reload blocker.
139
+ this.removePendingCancellation(laneId);
140
+ return;
141
+ }
142
+ try {
143
+ this.drain();
144
+ }
145
+ catch (error) {
146
+ this.warnBestEffort(`Worker ${laneId} scheduler redrain failed: ${error instanceof Error ? error.message : String(error)}`);
147
+ }
148
+ }
149
+ warnBestEffort(message) {
150
+ try {
151
+ this.options.warn(message);
152
+ }
153
+ catch {
154
+ // Diagnostics cannot retain a completed promise in the running set.
155
+ }
156
+ }
49
157
  drain(reservationAvailable = false) {
50
- if (this.draining || this.options.isDisposed())
158
+ if (this.options.isDisposed())
159
+ return;
160
+ if (reservationAvailable)
161
+ this.reservationAvailabilityRequested = true;
162
+ if (this.draining) {
163
+ this.redrainRequested = true;
51
164
  return;
165
+ }
52
166
  this.draining = true;
53
167
  try {
54
- for (const [laneId, request] of [...this.queued]) {
55
- if (this.reservationBlocked.has(laneId) && !reservationAvailable)
56
- continue;
57
- const record = this.options.getRecord(laneId);
58
- if (!record) {
168
+ // Promise settlement and every later scheduler signal retry each retained durable cancellation
169
+ // once. Keep this outside the redrain loop so a reentrant signal cannot create a busy retry.
170
+ this.retryPendingCancellations();
171
+ do {
172
+ this.redrainRequested = false;
173
+ const passReservationAvailable = this.reservationAvailabilityRequested;
174
+ this.reservationAvailabilityRequested = false;
175
+ for (const [laneId, request] of [...this.queued]) {
176
+ if (this.reservationBlocked.has(laneId) && !passReservationAvailable)
177
+ continue;
178
+ const record = this.options.getRecord(laneId);
179
+ if (!record) {
180
+ this.removeQueued(laneId);
181
+ continue;
182
+ }
183
+ const admission = this.options.admit(request, record);
184
+ if (admission.action === "wait") {
185
+ if (admission.reason === "write_reservation")
186
+ this.reservationBlocked.add(laneId);
187
+ else
188
+ this.reservationBlocked.delete(laneId);
189
+ continue;
190
+ }
191
+ if (admission.action === "cancel") {
192
+ this.reservationBlocked.delete(laneId);
193
+ // Durable cancellation owns this transition. Retain the scheduler entry when
194
+ // that write fails so a later explicit drain can retry it without a busy loop.
195
+ if (!this.cancelBestEffort(laneId, admission.reasonCode))
196
+ continue;
197
+ this.removeQueued(laneId);
198
+ // Cancellation can synchronously block another queued task that appeared earlier.
199
+ // Re-evaluate the bounded queue until that dependency cascade reaches a fixed point.
200
+ this.redrainRequested = true;
201
+ continue;
202
+ }
203
+ this.reservationBlocked.delete(laneId);
59
204
  this.removeQueued(laneId);
60
- continue;
61
- }
62
- const admission = this.options.admit(request, record);
63
- if (admission.action === "wait") {
64
- if (admission.reason === "write_reservation")
65
- this.reservationBlocked.add(laneId);
66
- continue;
205
+ let run;
206
+ try {
207
+ run = this.options.run(request, record);
208
+ }
209
+ catch (error) {
210
+ // Preserve synchronous start semantics while routing a throwing implementation
211
+ // through the same cancellation and cleanup owner as a rejected run promise.
212
+ run = Promise.reject(error);
213
+ }
214
+ this.track(laneId, run);
67
215
  }
68
- this.reservationBlocked.delete(laneId);
69
- this.removeQueued(laneId);
70
- if (admission.action === "cancel") {
71
- this.options.cancel(laneId, admission.reasonCode);
72
- continue;
73
- }
74
- this.track(laneId, this.options.run(request, record));
75
- }
216
+ } while ((this.redrainRequested || this.reservationAvailabilityRequested) && !this.options.isDisposed());
76
217
  }
77
218
  finally {
78
219
  this.draining = false;
@@ -81,10 +222,18 @@ export class WorkerDispatchScheduler {
81
222
  cancelQueued() {
82
223
  for (const laneId of [...this.queued.keys()]) {
83
224
  // The controller owns durable cancellation and any pre-admission resources (for example a
84
- // write reservation). Dropping this scheduler-only reference would leak those resources.
85
- this.options.cancel(laneId, "session_disposed");
225
+ // write reservation). Disposal must still visit every lane and release every process-local
226
+ // blocker when one durable write fails; the next controller generation recovers any retained
227
+ // durable queued attempt.
228
+ this.cancelBestEffort(laneId, "session_disposed");
86
229
  this.removeQueued(laneId);
87
230
  }
231
+ for (const [laneId, pending] of [...this.pendingCancellations]) {
232
+ this.cancelBestEffort(laneId, pending.reasonCode);
233
+ // Disposal hands any remaining durable recovery to the next controller generation. Release
234
+ // this generation's process-local reload blocker even when its last cancellation attempt fails.
235
+ this.removePendingCancellation(laneId);
236
+ }
88
237
  }
89
238
  dropQueued(laneId) {
90
239
  if (!this.queued.has(laneId))
@@ -93,10 +242,34 @@ export class WorkerDispatchScheduler {
93
242
  return true;
94
243
  }
95
244
  removeQueued(laneId) {
96
- this.queued.delete(laneId);
245
+ const removed = this.queued.delete(laneId);
97
246
  this.reservationBlocked.delete(laneId);
98
- this.queuedDeregisters.get(laneId)?.();
247
+ const deregister = this.queuedDeregisters.get(laneId);
99
248
  this.queuedDeregisters.delete(laneId);
249
+ try {
250
+ deregister?.();
251
+ }
252
+ catch (error) {
253
+ this.warnBestEffort(`Worker ${laneId} reload-gate deregistration failed: ${error instanceof Error ? error.message : String(error)}`);
254
+ }
255
+ if (removed)
256
+ this.notifyQueueCapacityAvailable();
257
+ }
258
+ notifyQueueCapacityAvailable() {
259
+ if (this.queueCapacityNotificationPending || this.queueCapacityListeners.size === 0)
260
+ return;
261
+ this.queueCapacityNotificationPending = true;
262
+ queueMicrotask(() => {
263
+ this.queueCapacityNotificationPending = false;
264
+ for (const listener of this.queueCapacityListeners) {
265
+ try {
266
+ listener();
267
+ }
268
+ catch (error) {
269
+ this.warnBestEffort(`Worker queue-capacity listener failed: ${error instanceof Error ? error.message : String(error)}`);
270
+ }
271
+ }
272
+ });
100
273
  }
101
274
  }
102
275
  //# sourceMappingURL=worker-dispatch-scheduler.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"worker-dispatch-scheduler.js","sourceRoot":"","sources":["../../../src/core/delegation/worker-dispatch-scheduler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAkB7D;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAQnC,YAAY,OAAuC;QANlC,WAAM,GAAG,IAAI,GAAG,EAAmC,CAAC;QACpD,sBAAiB,GAAG,IAAI,GAAG,EAAsB,CAAC;QAClD,YAAO,GAAG,IAAI,GAAG,EAA+C,CAAC;QACjE,uBAAkB,GAAG,IAAI,GAAG,EAAU,CAAC;QAChD,aAAQ,GAAG,KAAK,CAAC;QAGxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;IAED,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IACzB,CAAC;IAED,OAAO,CAAC,MAAkB,EAAE,OAAgC,EAAE,SAAS,GAAG,KAAK,EAAE,QAAQ,GAAG,KAAK;QAChG,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CA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;QAC3B,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;QACvC,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;CACD","sourcesContent":["import type { WorkerDelegationRunOutcome } from \"../agent-session-contracts.ts\";\nimport type { LaneRecord } from \"../autonomy/lane-tracker.ts\";\nimport { registerInFlightWork } from \"../reload-blockers.ts\";\nimport type { WorkerDelegationRequest } from \"./worker-delegation-request.ts\";\n\nexport type WorkerDispatchAdmission =\n\t| { action: \"start\" }\n\t| { action: \"wait\"; reason?: \"capacity\" | \"write_reservation\" }\n\t| { action: \"cancel\"; reasonCode: string };\n\nexport interface WorkerDispatchSchedulerOptions {\n\tagentDir: string;\n\tisDisposed(): boolean;\n\tadmit(request: WorkerDelegationRequest, record: LaneRecord): WorkerDispatchAdmission;\n\tgetRecord(laneId: string): LaneRecord | undefined;\n\trun(request: WorkerDelegationRequest, record: LaneRecord): Promise<WorkerDelegationRunOutcome>;\n\tcancel(laneId: string, reasonCode: string): void;\n\twarn(message: string): void;\n}\n\n/**\n * Single owner of worker queue and promise transitions. Execution policy and durable lifecycle stay\n * outside; this class only decides when a prepared durable attempt moves from queued to running.\n */\nexport class WorkerDispatchScheduler {\n\tprivate readonly options: WorkerDispatchSchedulerOptions;\n\tprivate readonly queued = new Map<string, WorkerDelegationRequest>();\n\tprivate readonly queuedDeregisters = new Map<string, () => void>();\n\tprivate readonly running = new Map<string, Promise<WorkerDelegationRunOutcome>>();\n\tprivate readonly reservationBlocked = new Set<string>();\n\tprivate draining = false;\n\n\tconstructor(options: WorkerDispatchSchedulerOptions) {\n\t\tthis.options = options;\n\t}\n\n\tget queuedCount(): number {\n\t\treturn this.queued.size;\n\t}\n\n\tenqueue(record: LaneRecord, request: WorkerDelegationRequest, recovered = false, priority = false): void {\n\t\tif (this.queued.has(record.laneId) || this.running.has(record.laneId)) return;\n\t\tif (priority) {\n\t\t\tconst waiting = [...this.queued];\n\t\t\tthis.queued.clear();\n\t\t\tthis.queued.set(record.laneId, request);\n\t\t\tfor (const [laneId, queuedRequest] of waiting) this.queued.set(laneId, queuedRequest);\n\t\t} else {\n\t\t\tthis.queued.set(record.laneId, request);\n\t\t}\n\t\tthis.queuedDeregisters.set(\n\t\t\trecord.laneId,\n\t\t\tregisterInFlightWork(\n\t\t\t\tthis.options.agentDir,\n\t\t\t\t\"lane\",\n\t\t\t\trecovered ? `worker-recovered:${record.laneId}` : `worker-queued:${record.laneId}`,\n\t\t\t),\n\t\t);\n\t}\n\n\ttrack(laneId: string, promise: Promise<WorkerDelegationRunOutcome>): void {\n\t\tthis.running.set(laneId, promise);\n\t\tvoid promise.then(\n\t\t\t(outcome) => {\n\t\t\t\tif (!outcome.started) this.options.cancel(laneId, outcome.skipReason ?? \"worker_not_started\");\n\t\t\t\tthis.running.delete(laneId);\n\t\t\t\tif (!this.options.isDisposed()) this.drain();\n\t\t\t},\n\t\t\t(error: unknown) => {\n\t\t\t\tthis.options.cancel(laneId, \"worker_background_error\");\n\t\t\t\tthis.options.warn(`Worker ${laneId} rejected: ${error instanceof Error ? error.message : String(error)}`);\n\t\t\t\tthis.running.delete(laneId);\n\t\t\t\tif (!this.options.isDisposed()) this.drain();\n\t\t\t},\n\t\t);\n\t}\n\n\tdrain(reservationAvailable = false): void {\n\t\tif (this.draining || this.options.isDisposed()) return;\n\t\tthis.draining = true;\n\t\ttry {\n\t\t\tfor (const [laneId, request] of [...this.queued]) {\n\t\t\t\tif (this.reservationBlocked.has(laneId) && !reservationAvailable) continue;\n\t\t\t\tconst record = this.options.getRecord(laneId);\n\t\t\t\tif (!record) {\n\t\t\t\t\tthis.removeQueued(laneId);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst admission = this.options.admit(request, record);\n\t\t\t\tif (admission.action === \"wait\") {\n\t\t\t\t\tif (admission.reason === \"write_reservation\") this.reservationBlocked.add(laneId);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tthis.reservationBlocked.delete(laneId);\n\t\t\t\tthis.removeQueued(laneId);\n\t\t\t\tif (admission.action === \"cancel\") {\n\t\t\t\t\tthis.options.cancel(laneId, admission.reasonCode);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tthis.track(laneId, this.options.run(request, record));\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.draining = false;\n\t\t}\n\t}\n\n\tcancelQueued(): void {\n\t\tfor (const laneId of [...this.queued.keys()]) {\n\t\t\t// The controller owns durable cancellation and any pre-admission resources (for example a\n\t\t\t// write reservation). Dropping this scheduler-only reference would leak those resources.\n\t\t\tthis.options.cancel(laneId, \"session_disposed\");\n\t\t\tthis.removeQueued(laneId);\n\t\t}\n\t}\n\n\tdropQueued(laneId: string): boolean {\n\t\tif (!this.queued.has(laneId)) return false;\n\t\tthis.removeQueued(laneId);\n\t\treturn true;\n\t}\n\n\tprivate removeQueued(laneId: string): void {\n\t\tthis.queued.delete(laneId);\n\t\tthis.reservationBlocked.delete(laneId);\n\t\tthis.queuedDeregisters.get(laneId)?.();\n\t\tthis.queuedDeregisters.delete(laneId);\n\t}\n}\n"]}
1
+ {"version":3,"file":"worker-dispatch-scheduler.js","sourceRoot":"","sources":["../../../src/core/delegation/worker-dispatch-scheduler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAwBlE;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAanC,YAAY,OAAuC;QAXlC,WAAM,GAAG,IAAI,GAAG,EAAmC,CAAC;QACpD,sBAAiB,GAAG,IAAI,GAAG,EAAsB,CAAC;QAClD,YAAO,GAAG,IAAI,GAAG,EAA+C,CAAC;QACjE,yBAAoB,GAAG,IAAI,GAAG,EAA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,CAAC;IAClD,CAAC;IAEO,4BAA4B;QACnC,IAAI,IAAI,CAAC,gCAAgC,IAAI,IAAI,CAAC,sBAAsB,CAAC,IAAI,KAAK,CAAC;YAAE,OAAO;QAC5F,IAAI,CAAC,gCAAgC,GAAG,IAAI,CAAC;QAC7C,cAAc,CAAC,GAAG,EAAE;YACnB,IAAI,CAAC,gCAAgC,GAAG,KAAK,CAAC;YAC9C,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,sBAAsB,EAAE,CAAC;gBACpD,IAAI,CAAC;oBACJ,QAAQ,EAAE,CAAC;gBACZ,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBAChB,IAAI,CAAC,cAAc,CAClB,0CAA0C,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAClG,CAAC;gBACH,CAAC;YACF,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import type { WorkerDelegationRunOutcome } from \"../agent-session-contracts.ts\";\nimport type { LaneRecord } from \"../autonomy/lane-tracker.ts\";\nimport { registerInFlightWork } from \"../reload-blockers.ts\";\nimport type { WorkerDelegationRequest } from \"./worker-delegation-request.ts\";\nimport { workerQueueHasCapacity } from \"./worker-fleet-limits.ts\";\n\nexport type WorkerDispatchAdmission =\n\t| { action: \"start\" }\n\t| { action: \"wait\"; reason?: \"capacity\" | \"dependencies\" | \"objective\" | \"write_reservation\" }\n\t| { action: \"cancel\"; reasonCode: string };\n\nexport interface WorkerDispatchSchedulerOptions {\n\tagentDir: string;\n\t/** Test seam for proving queue insertion rollback when reload-gate registration fails. */\n\tregisterInFlightWork?: typeof registerInFlightWork;\n\tisDisposed(): boolean;\n\tadmit(request: WorkerDelegationRequest, record: LaneRecord): WorkerDispatchAdmission;\n\tgetRecord(laneId: string): LaneRecord | undefined;\n\trun(request: WorkerDelegationRequest, record: LaneRecord): Promise<WorkerDelegationRunOutcome>;\n\tcancel(laneId: string, reasonCode: string): void;\n\twarn(message: string): void;\n}\n\ninterface PendingCancellation {\n\treasonCode: string;\n\tderegister?: () => void;\n}\n\n/**\n * Single owner of worker queue and promise transitions. Execution policy and durable lifecycle stay\n * outside; this class only decides when a prepared durable attempt moves from queued to running.\n */\nexport class WorkerDispatchScheduler {\n\tprivate readonly options: WorkerDispatchSchedulerOptions;\n\tprivate readonly queued = new Map<string, WorkerDelegationRequest>();\n\tprivate readonly queuedDeregisters = new Map<string, () => void>();\n\tprivate readonly running = new Map<string, Promise<WorkerDelegationRunOutcome>>();\n\tprivate readonly pendingCancellations = new Map<string, PendingCancellation>();\n\tprivate readonly reservationBlocked = new Set<string>();\n\tprivate readonly queueCapacityListeners = new Set<() => void>();\n\tprivate draining = false;\n\tprivate redrainRequested = false;\n\tprivate reservationAvailabilityRequested = false;\n\tprivate queueCapacityNotificationPending = false;\n\n\tconstructor(options: WorkerDispatchSchedulerOptions) {\n\t\tthis.options = options;\n\t}\n\n\tget queuedCount(): number {\n\t\treturn this.queued.size;\n\t}\n\n\thasQueueCapacity(priority = false): boolean {\n\t\treturn workerQueueHasCapacity(this.queued.size, priority);\n\t}\n\n\t/** Notify retained priority work when a bounded queue slot is released. */\n\tonQueueCapacityAvailable(listener: () => void): () => void {\n\t\tthis.queueCapacityListeners.add(listener);\n\t\treturn () => this.queueCapacityListeners.delete(listener);\n\t}\n\n\tenqueue(record: LaneRecord, request: WorkerDelegationRequest, recovered = false, priority = false): void {\n\t\tif (\n\t\t\tthis.queued.has(record.laneId) ||\n\t\t\tthis.running.has(record.laneId) ||\n\t\t\tthis.pendingCancellations.has(record.laneId)\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif (!this.hasQueueCapacity(priority)) throw new Error(\"worker_dispatch_queue_full\");\n\t\tif (priority) {\n\t\t\tconst waiting = [...this.queued];\n\t\t\tthis.queued.clear();\n\t\t\tthis.queued.set(record.laneId, request);\n\t\t\tfor (const [laneId, queuedRequest] of waiting) this.queued.set(laneId, queuedRequest);\n\t\t} else {\n\t\t\tthis.queued.set(record.laneId, request);\n\t\t}\n\t\ttry {\n\t\t\tthis.queuedDeregisters.set(\n\t\t\t\trecord.laneId,\n\t\t\t\t(this.options.registerInFlightWork ?? registerInFlightWork)(\n\t\t\t\t\tthis.options.agentDir,\n\t\t\t\t\t\"lane\",\n\t\t\t\t\trecovered ? `worker-recovered:${record.laneId}` : `worker-queued:${record.laneId}`,\n\t\t\t\t),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\t// Queue insertion and reload-gate registration are one process-local transition. A failed\n\t\t\t// registration must not leave a lane that appears queued but has no matching blocker.\n\t\t\tthis.queued.delete(record.laneId);\n\t\t\tthis.reservationBlocked.delete(record.laneId);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\ttrack(laneId: string, promise: Promise<WorkerDelegationRunOutcome>): void {\n\t\tthis.running.set(laneId, promise);\n\t\tvoid promise.then(\n\t\t\t(outcome) => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!outcome.started) {\n\t\t\t\t\t\tconst reasonCode = outcome.skipReason ?? \"worker_not_started\";\n\t\t\t\t\t\tif (!this.cancelBestEffort(laneId, reasonCode)) {\n\t\t\t\t\t\t\tthis.retainPendingCancellation(laneId, reasonCode);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} finally {\n\t\t\t\t\tthis.finishTrackedRun(laneId);\n\t\t\t\t}\n\t\t\t},\n\t\t\t(error: unknown) => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!this.cancelBestEffort(laneId, \"worker_background_error\")) {\n\t\t\t\t\t\tthis.retainPendingCancellation(laneId, \"worker_background_error\");\n\t\t\t\t\t}\n\t\t\t\t\tthis.warnBestEffort(\n\t\t\t\t\t\t`Worker ${laneId} rejected: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t\t);\n\t\t\t\t} finally {\n\t\t\t\t\tthis.finishTrackedRun(laneId);\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\t}\n\n\tprivate retainPendingCancellation(laneId: string, reasonCode: string): void {\n\t\tif (this.pendingCancellations.has(laneId)) return;\n\t\tconst pending: PendingCancellation = { reasonCode };\n\t\tthis.pendingCancellations.set(laneId, pending);\n\t\tthis.registerPendingCancellation(laneId, pending);\n\t}\n\n\tprivate registerPendingCancellation(laneId: string, pending: PendingCancellation): void {\n\t\tif (pending.deregister) return;\n\t\ttry {\n\t\t\tpending.deregister = (this.options.registerInFlightWork ?? registerInFlightWork)(\n\t\t\t\tthis.options.agentDir,\n\t\t\t\t\"lane\",\n\t\t\t\t`worker-cancellation-pending:${laneId}`,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthis.warnBestEffort(\n\t\t\t\t`Worker ${laneId} cancellation reload-gate registration failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate retryPendingCancellations(): void {\n\t\tfor (const [laneId, pending] of [...this.pendingCancellations]) {\n\t\t\tthis.registerPendingCancellation(laneId, pending);\n\t\t\tif (!this.cancelBestEffort(laneId, pending.reasonCode)) continue;\n\t\t\tthis.removePendingCancellation(laneId);\n\t\t}\n\t}\n\n\tprivate removePendingCancellation(laneId: string): void {\n\t\tconst pending = this.pendingCancellations.get(laneId);\n\t\tif (!pending) return;\n\t\tthis.pendingCancellations.delete(laneId);\n\t\ttry {\n\t\t\tpending.deregister?.();\n\t\t} catch (error) {\n\t\t\tthis.warnBestEffort(\n\t\t\t\t`Worker ${laneId} cancellation reload-gate deregistration failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate cancelBestEffort(laneId: string, reasonCode: string): boolean {\n\t\ttry {\n\t\t\tthis.options.cancel(laneId, reasonCode);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tthis.warnBestEffort(\n\t\t\t\t`Worker ${laneId} cancellation failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tprivate finishTrackedRun(laneId: string): void {\n\t\tthis.running.delete(laneId);\n\t\tif (this.options.isDisposed()) {\n\t\t\t// A disposed generation has no future scheduler signal. Its durable state is recovered by the\n\t\t\t// next generation, so do not leak this generation's process-local reload blocker.\n\t\t\tthis.removePendingCancellation(laneId);\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tthis.drain();\n\t\t} catch (error) {\n\t\t\tthis.warnBestEffort(\n\t\t\t\t`Worker ${laneId} scheduler redrain failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate warnBestEffort(message: string): void {\n\t\ttry {\n\t\t\tthis.options.warn(message);\n\t\t} catch {\n\t\t\t// Diagnostics cannot retain a completed promise in the running set.\n\t\t}\n\t}\n\n\tdrain(reservationAvailable = false): void {\n\t\tif (this.options.isDisposed()) return;\n\t\tif (reservationAvailable) this.reservationAvailabilityRequested = true;\n\t\tif (this.draining) {\n\t\t\tthis.redrainRequested = true;\n\t\t\treturn;\n\t\t}\n\t\tthis.draining = true;\n\t\ttry {\n\t\t\t// Promise settlement and every later scheduler signal retry each retained durable cancellation\n\t\t\t// once. Keep this outside the redrain loop so a reentrant signal cannot create a busy retry.\n\t\t\tthis.retryPendingCancellations();\n\t\t\tdo {\n\t\t\t\tthis.redrainRequested = false;\n\t\t\t\tconst passReservationAvailable = this.reservationAvailabilityRequested;\n\t\t\t\tthis.reservationAvailabilityRequested = false;\n\t\t\t\tfor (const [laneId, request] of [...this.queued]) {\n\t\t\t\t\tif (this.reservationBlocked.has(laneId) && !passReservationAvailable) continue;\n\t\t\t\t\tconst record = this.options.getRecord(laneId);\n\t\t\t\t\tif (!record) {\n\t\t\t\t\t\tthis.removeQueued(laneId);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tconst admission = this.options.admit(request, record);\n\t\t\t\t\tif (admission.action === \"wait\") {\n\t\t\t\t\t\tif (admission.reason === \"write_reservation\") this.reservationBlocked.add(laneId);\n\t\t\t\t\t\telse this.reservationBlocked.delete(laneId);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tif (admission.action === \"cancel\") {\n\t\t\t\t\t\tthis.reservationBlocked.delete(laneId);\n\t\t\t\t\t\t// Durable cancellation owns this transition. Retain the scheduler entry when\n\t\t\t\t\t\t// that write fails so a later explicit drain can retry it without a busy loop.\n\t\t\t\t\t\tif (!this.cancelBestEffort(laneId, admission.reasonCode)) continue;\n\t\t\t\t\t\tthis.removeQueued(laneId);\n\t\t\t\t\t\t// Cancellation can synchronously block another queued task that appeared earlier.\n\t\t\t\t\t\t// Re-evaluate the bounded queue until that dependency cascade reaches a fixed point.\n\t\t\t\t\t\tthis.redrainRequested = true;\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tthis.reservationBlocked.delete(laneId);\n\t\t\t\t\tthis.removeQueued(laneId);\n\t\t\t\t\tlet run: Promise<WorkerDelegationRunOutcome>;\n\t\t\t\t\ttry {\n\t\t\t\t\t\trun = this.options.run(request, record);\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t// Preserve synchronous start semantics while routing a throwing implementation\n\t\t\t\t\t\t// through the same cancellation and cleanup owner as a rejected run promise.\n\t\t\t\t\t\trun = Promise.reject(error);\n\t\t\t\t\t}\n\t\t\t\t\tthis.track(laneId, run);\n\t\t\t\t}\n\t\t\t} while ((this.redrainRequested || this.reservationAvailabilityRequested) && !this.options.isDisposed());\n\t\t} finally {\n\t\t\tthis.draining = false;\n\t\t}\n\t}\n\n\tcancelQueued(): void {\n\t\tfor (const laneId of [...this.queued.keys()]) {\n\t\t\t// The controller owns durable cancellation and any pre-admission resources (for example a\n\t\t\t// write reservation). Disposal must still visit every lane and release every process-local\n\t\t\t// blocker when one durable write fails; the next controller generation recovers any retained\n\t\t\t// durable queued attempt.\n\t\t\tthis.cancelBestEffort(laneId, \"session_disposed\");\n\t\t\tthis.removeQueued(laneId);\n\t\t}\n\t\tfor (const [laneId, pending] of [...this.pendingCancellations]) {\n\t\t\tthis.cancelBestEffort(laneId, pending.reasonCode);\n\t\t\t// Disposal hands any remaining durable recovery to the next controller generation. Release\n\t\t\t// this generation's process-local reload blocker even when its last cancellation attempt fails.\n\t\t\tthis.removePendingCancellation(laneId);\n\t\t}\n\t}\n\n\tdropQueued(laneId: string): boolean {\n\t\tif (!this.queued.has(laneId)) return false;\n\t\tthis.removeQueued(laneId);\n\t\treturn true;\n\t}\n\n\tprivate removeQueued(laneId: string): void {\n\t\tconst removed = this.queued.delete(laneId);\n\t\tthis.reservationBlocked.delete(laneId);\n\t\tconst deregister = this.queuedDeregisters.get(laneId);\n\t\tthis.queuedDeregisters.delete(laneId);\n\t\ttry {\n\t\t\tderegister?.();\n\t\t} catch (error) {\n\t\t\tthis.warnBestEffort(\n\t\t\t\t`Worker ${laneId} reload-gate deregistration failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t\tif (removed) this.notifyQueueCapacityAvailable();\n\t}\n\n\tprivate notifyQueueCapacityAvailable(): void {\n\t\tif (this.queueCapacityNotificationPending || this.queueCapacityListeners.size === 0) return;\n\t\tthis.queueCapacityNotificationPending = true;\n\t\tqueueMicrotask(() => {\n\t\t\tthis.queueCapacityNotificationPending = false;\n\t\t\tfor (const listener of this.queueCapacityListeners) {\n\t\t\t\ttry {\n\t\t\t\t\tlistener();\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.warnBestEffort(\n\t\t\t\t\t\t`Worker queue-capacity listener failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n}\n"]}
@@ -0,0 +1,38 @@
1
+ import { type AgentBindingContract } from "../orchestration/contracts.ts";
2
+ import type { TaskRuntimeProjection } from "../orchestration/task-runtime.ts";
3
+ /**
4
+ * Process-independent safety ceilings for one durable session fleet. These limits bound persisted
5
+ * identity growth and scheduler fan-out; model/provider budgets remain separately authoritative.
6
+ */
7
+ export interface WorkerFleetLimits {
8
+ maxDepth: number;
9
+ maxAgentsPerSession: number;
10
+ maxChildrenPerAgent: number;
11
+ maxQueuedDispatches: number;
12
+ }
13
+ export declare const DEFAULT_WORKER_FLEET_LIMITS: Readonly<WorkerFleetLimits>;
14
+ export type NewWorkerAdmission = {
15
+ ok: true;
16
+ depth: number;
17
+ } | {
18
+ ok: false;
19
+ reasonCode: "worker_agent_parent_unknown" | "worker_agent_parent_retired" | "worker_agent_depth_limit_reached" | "worker_agent_child_limit_reached" | "worker_agent_session_limit_reached";
20
+ };
21
+ export type WorkerIdentityHeadroomAdmission = {
22
+ ok: true;
23
+ } | {
24
+ ok: false;
25
+ reasonCode: "worker_agent_session_limit_reached";
26
+ };
27
+ /** Subjects whose admitted verifier identity has not yet been materialized. */
28
+ export declare function pendingVerifierSubjectTaskIds(snapshot: Pick<TaskRuntimeProjection, "agents" | "tasks" | "attempts">): Set<string>;
29
+ /**
30
+ * A persistent-agent turn consumes no implementation identity, but its retained immutable contract
31
+ * can still require a fresh verifier. Protect both that verifier and every earlier reservation.
32
+ */
33
+ export declare function evaluateReusableWorkerTaskAdmission(snapshot: Pick<TaskRuntimeProjection, "agents" | "tasks" | "attempts">, agentId: string, limits?: Readonly<WorkerFleetLimits>): WorkerIdentityHeadroomAdmission;
34
+ /** Evaluate a new durable identity before a task, conversation, or scheduler entry is created. */
35
+ export declare function evaluateNewWorkerAdmission(agents: Readonly<Record<string, AgentBindingContract>>, parentAgentId?: string, limits?: Readonly<WorkerFleetLimits>, requiredAgentSlots?: number): NewWorkerAdmission;
36
+ /** Keep one queue slot reserved for an already-admitted mandatory verifier. */
37
+ export declare function workerQueueHasCapacity(queuedCount: number, priority: boolean, limits?: Readonly<WorkerFleetLimits>): boolean;
38
+ //# sourceMappingURL=worker-fleet-limits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-fleet-limits.d.ts","sourceRoot":"","sources":["../../../src/core/del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