@caupulican/pi-adaptative 0.81.38 → 0.81.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 (881) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/README.md +2 -0
  3. package/dist/bundled-resources/extensions/tmux-agent-manager/README.md +18 -1
  4. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +133 -0
  5. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +1 -0
  6. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +198 -0
  7. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +1 -0
  8. package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +311 -0
  9. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +31 -2
  10. package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
  11. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +823 -79
  12. package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
  13. package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +1043 -86
  14. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/__init__.py +43 -0
  15. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/fs.py +270 -0
  16. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/search.py +252 -0
  17. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/strings.py +399 -0
  18. package/dist/bundled-resources/runtimes/pi-shell-engine/commands/text.py +575 -0
  19. package/dist/bundled-resources/runtimes/pi-shell-engine/context.py +52 -0
  20. package/dist/bundled-resources/runtimes/pi-shell-engine/errors.py +49 -0
  21. package/dist/bundled-resources/runtimes/pi-shell-engine/exec.py +734 -0
  22. package/dist/bundled-resources/runtimes/pi-shell-engine/expand.py +238 -0
  23. package/dist/bundled-resources/runtimes/pi-shell-engine/main.py +132 -0
  24. package/dist/bundled-resources/runtimes/pi-shell-engine/nodes.py +116 -0
  25. package/dist/bundled-resources/runtimes/pi-shell-engine/parser.py +287 -0
  26. package/dist/bundled-resources/runtimes/pi-shell-engine/proc.py +137 -0
  27. package/dist/bundled-resources/runtimes/pi-shell-engine/state.py +100 -0
  28. package/dist/bundled-resources/runtimes/pi-shell-engine/tokens.py +579 -0
  29. package/dist/bundled-resources/skills/tool-call-repair/SKILL.md +19 -9
  30. package/dist/bundled-resources/skills/tool-call-repair/references/failure-grammar.md +22 -5
  31. package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +7 -7
  32. package/dist/bundled-resources/skills/tool-call-repair/references/text-protocol-grammar.md +28 -7
  33. package/dist/cli/args.d.ts +5 -0
  34. package/dist/cli/args.d.ts.map +1 -1
  35. package/dist/cli/args.js +24 -0
  36. package/dist/cli/args.js.map +1 -1
  37. package/dist/core/agent-directory-layout.d.ts +15 -0
  38. package/dist/core/agent-directory-layout.d.ts.map +1 -0
  39. package/dist/core/agent-directory-layout.js +72 -0
  40. package/dist/core/agent-directory-layout.js.map +1 -0
  41. package/dist/core/agent-paths.d.ts +66 -0
  42. package/dist/core/agent-paths.d.ts.map +1 -0
  43. package/dist/core/agent-paths.js +170 -0
  44. package/dist/core/agent-paths.js.map +1 -0
  45. package/dist/core/agent-session-runtime.d.ts +52 -8
  46. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  47. package/dist/core/agent-session-runtime.js +397 -79
  48. package/dist/core/agent-session-runtime.js.map +1 -1
  49. package/dist/core/agent-session-services.d.ts +1 -0
  50. package/dist/core/agent-session-services.d.ts.map +1 -1
  51. package/dist/core/agent-session-services.js +4 -3
  52. package/dist/core/agent-session-services.js.map +1 -1
  53. package/dist/core/agent-session.d.ts +255 -103
  54. package/dist/core/agent-session.d.ts.map +1 -1
  55. package/dist/core/agent-session.js +754 -1173
  56. package/dist/core/agent-session.js.map +1 -1
  57. package/dist/core/autonomy/approval-gate.d.ts +3 -2
  58. package/dist/core/autonomy/approval-gate.d.ts.map +1 -1
  59. package/dist/core/autonomy/approval-gate.js +6 -28
  60. package/dist/core/autonomy/approval-gate.js.map +1 -1
  61. package/dist/core/autonomy/bounded-completion.d.ts +1 -1
  62. package/dist/core/autonomy/bounded-completion.d.ts.map +1 -1
  63. package/dist/core/autonomy/bounded-completion.js +1 -1
  64. package/dist/core/autonomy/bounded-completion.js.map +1 -1
  65. package/dist/core/autonomy/contracts.d.ts +26 -9
  66. package/dist/core/autonomy/contracts.d.ts.map +1 -1
  67. package/dist/core/autonomy/contracts.js.map +1 -1
  68. package/dist/core/autonomy/foreground-envelope.d.ts +8 -2
  69. package/dist/core/autonomy/foreground-envelope.d.ts.map +1 -1
  70. package/dist/core/autonomy/foreground-envelope.js +8 -25
  71. package/dist/core/autonomy/foreground-envelope.js.map +1 -1
  72. package/dist/core/autonomy/gates.d.ts.map +1 -1
  73. package/dist/core/autonomy/gates.js +8 -10
  74. package/dist/core/autonomy/gates.js.map +1 -1
  75. package/dist/core/autonomy/lane-private-paths.d.ts +2 -0
  76. package/dist/core/autonomy/lane-private-paths.d.ts.map +1 -0
  77. package/dist/core/autonomy/lane-private-paths.js +16 -0
  78. package/dist/core/autonomy/lane-private-paths.js.map +1 -0
  79. package/dist/core/autonomy/lane-tool-surface.d.ts +10 -0
  80. package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
  81. package/dist/core/autonomy/lane-tool-surface.js +46 -8
  82. package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
  83. package/dist/core/autonomy/lane-tracker.d.ts +24 -1
  84. package/dist/core/autonomy/lane-tracker.d.ts.map +1 -1
  85. package/dist/core/autonomy/lane-tracker.js +43 -1
  86. package/dist/core/autonomy/lane-tracker.js.map +1 -1
  87. package/dist/core/autonomy/risk-assessment.d.ts +1 -2
  88. package/dist/core/autonomy/risk-assessment.d.ts.map +1 -1
  89. package/dist/core/autonomy/risk-assessment.js +1 -123
  90. package/dist/core/autonomy/risk-assessment.js.map +1 -1
  91. package/dist/core/autonomy/session-lane-record.d.ts +4 -4
  92. package/dist/core/autonomy/session-lane-record.d.ts.map +1 -1
  93. package/dist/core/autonomy/session-lane-record.js +16 -29
  94. package/dist/core/autonomy/session-lane-record.js.map +1 -1
  95. package/dist/core/autonomy/telemetry-events.d.ts +1 -1
  96. package/dist/core/autonomy/telemetry-events.d.ts.map +1 -1
  97. package/dist/core/autonomy/telemetry-events.js +1 -1
  98. package/dist/core/autonomy/telemetry-events.js.map +1 -1
  99. package/dist/core/autonomy-telemetry.d.ts +7 -7
  100. package/dist/core/autonomy-telemetry.d.ts.map +1 -1
  101. package/dist/core/autonomy-telemetry.js +9 -9
  102. package/dist/core/autonomy-telemetry.js.map +1 -1
  103. package/dist/core/background-lane-controller.d.ts +90 -156
  104. package/dist/core/background-lane-controller.d.ts.map +1 -1
  105. package/dist/core/background-lane-controller.js +192 -968
  106. package/dist/core/background-lane-controller.js.map +1 -1
  107. package/dist/core/bash-execution-controller.d.ts +4 -0
  108. package/dist/core/bash-execution-controller.d.ts.map +1 -1
  109. package/dist/core/bash-execution-controller.js +7 -1
  110. package/dist/core/bash-execution-controller.js.map +1 -1
  111. package/dist/core/capability-contract.d.ts +4 -0
  112. package/dist/core/capability-contract.d.ts.map +1 -0
  113. package/dist/core/capability-contract.js +28 -0
  114. package/dist/core/capability-contract.js.map +1 -0
  115. package/dist/core/compaction-controller.d.ts +90 -0
  116. package/dist/core/compaction-controller.d.ts.map +1 -0
  117. package/dist/core/compaction-controller.js +452 -0
  118. package/dist/core/compaction-controller.js.map +1 -0
  119. package/dist/core/compaction-support.d.ts +24 -13
  120. package/dist/core/compaction-support.d.ts.map +1 -1
  121. package/dist/core/compaction-support.js +43 -7
  122. package/dist/core/compaction-support.js.map +1 -1
  123. package/dist/core/context/context-artifacts.d.ts +6 -2
  124. package/dist/core/context/context-artifacts.d.ts.map +1 -1
  125. package/dist/core/context/context-artifacts.js +34 -2
  126. package/dist/core/context/context-artifacts.js.map +1 -1
  127. package/dist/core/context/context-audit.d.ts +33 -1
  128. package/dist/core/context/context-audit.d.ts.map +1 -1
  129. package/dist/core/context/context-audit.js +31 -5
  130. package/dist/core/context/context-audit.js.map +1 -1
  131. package/dist/core/context/context-store-retention.d.ts +8 -2
  132. package/dist/core/context/context-store-retention.d.ts.map +1 -1
  133. package/dist/core/context/context-store-retention.js +158 -35
  134. package/dist/core/context/context-store-retention.js.map +1 -1
  135. package/dist/core/context/okf-memory.d.ts.map +1 -1
  136. package/dist/core/context/okf-memory.js +9 -11
  137. package/dist/core/context/okf-memory.js.map +1 -1
  138. package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -1
  139. package/dist/core/context/sqlite-runtime-index.js +6 -8
  140. package/dist/core/context/sqlite-runtime-index.js.map +1 -1
  141. package/dist/core/context-gc.d.ts +3 -0
  142. package/dist/core/context-gc.d.ts.map +1 -1
  143. package/dist/core/context-gc.js +26 -6
  144. package/dist/core/context-gc.js.map +1 -1
  145. package/dist/core/context-pipeline.d.ts +26 -4
  146. package/dist/core/context-pipeline.d.ts.map +1 -1
  147. package/dist/core/context-pipeline.js +146 -18
  148. package/dist/core/context-pipeline.js.map +1 -1
  149. package/dist/core/cost-guard.d.ts +19 -3
  150. package/dist/core/cost-guard.d.ts.map +1 -1
  151. package/dist/core/cost-guard.js +18 -3
  152. package/dist/core/cost-guard.js.map +1 -1
  153. package/dist/core/default-tool-surface.d.ts.map +1 -1
  154. package/dist/core/default-tool-surface.js +1 -0
  155. package/dist/core/default-tool-surface.js.map +1 -1
  156. package/dist/core/delegation/managed-lane-controller.d.ts +34 -0
  157. package/dist/core/delegation/managed-lane-controller.d.ts.map +1 -0
  158. package/dist/core/delegation/managed-lane-controller.js +217 -0
  159. package/dist/core/delegation/managed-lane-controller.js.map +1 -0
  160. package/dist/core/delegation/session-worker-claim.d.ts +44 -0
  161. package/dist/core/delegation/session-worker-claim.d.ts.map +1 -0
  162. package/dist/core/delegation/session-worker-claim.js +98 -0
  163. package/dist/core/delegation/session-worker-claim.js.map +1 -0
  164. package/dist/core/delegation/worker-actions.d.ts +7 -5
  165. package/dist/core/delegation/worker-actions.d.ts.map +1 -1
  166. package/dist/core/delegation/worker-actions.js +20 -4
  167. package/dist/core/delegation/worker-actions.js.map +1 -1
  168. package/dist/core/delegation/worker-claim.d.ts +56 -0
  169. package/dist/core/delegation/worker-claim.d.ts.map +1 -0
  170. package/dist/core/delegation/worker-claim.js +239 -0
  171. package/dist/core/delegation/worker-claim.js.map +1 -0
  172. package/dist/core/delegation/worker-delegation-controller.d.ts +110 -0
  173. package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -0
  174. package/dist/core/delegation/worker-delegation-controller.js +861 -0
  175. package/dist/core/delegation/worker-delegation-controller.js.map +1 -0
  176. package/dist/core/delegation/worker-delegation-request.d.ts +11 -0
  177. package/dist/core/delegation/worker-delegation-request.d.ts.map +1 -0
  178. package/dist/core/delegation/worker-delegation-request.js +2 -0
  179. package/dist/core/delegation/worker-delegation-request.js.map +1 -0
  180. package/dist/core/delegation/worker-dispatch-scheduler.d.ts +40 -0
  181. package/dist/core/delegation/worker-dispatch-scheduler.d.ts.map +1 -0
  182. package/dist/core/delegation/worker-dispatch-scheduler.js +93 -0
  183. package/dist/core/delegation/worker-dispatch-scheduler.js.map +1 -0
  184. package/dist/core/delegation/worker-execution-policy.d.ts +62 -0
  185. package/dist/core/delegation/worker-execution-policy.d.ts.map +1 -0
  186. package/dist/core/delegation/worker-execution-policy.js +160 -0
  187. package/dist/core/delegation/worker-execution-policy.js.map +1 -0
  188. package/dist/core/delegation/worker-lane-projection.d.ts +14 -0
  189. package/dist/core/delegation/worker-lane-projection.d.ts.map +1 -0
  190. package/dist/core/delegation/worker-lane-projection.js +100 -0
  191. package/dist/core/delegation/worker-lane-projection.js.map +1 -0
  192. package/dist/core/delegation/worker-lifecycle.d.ts +97 -0
  193. package/dist/core/delegation/worker-lifecycle.d.ts.map +1 -0
  194. package/dist/core/delegation/worker-lifecycle.js +339 -0
  195. package/dist/core/delegation/worker-lifecycle.js.map +1 -0
  196. package/dist/core/delegation/worker-notification-coordinator.d.ts +36 -0
  197. package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -0
  198. package/dist/core/delegation/worker-notification-coordinator.js +90 -0
  199. package/dist/core/delegation/worker-notification-coordinator.js.map +1 -0
  200. package/dist/core/delegation/worker-profile-resolver.d.ts +55 -0
  201. package/dist/core/delegation/worker-profile-resolver.d.ts.map +1 -0
  202. package/dist/core/delegation/worker-profile-resolver.js +113 -0
  203. package/dist/core/delegation/worker-profile-resolver.js.map +1 -0
  204. package/dist/core/delegation/worker-runner.d.ts +25 -14
  205. package/dist/core/delegation/worker-runner.d.ts.map +1 -1
  206. package/dist/core/delegation/worker-runner.js +122 -53
  207. package/dist/core/delegation/worker-runner.js.map +1 -1
  208. package/dist/core/delegation/worker-task-label.d.ts +3 -0
  209. package/dist/core/delegation/worker-task-label.d.ts.map +1 -0
  210. package/dist/core/delegation/worker-task-label.js +11 -0
  211. package/dist/core/delegation/worker-task-label.js.map +1 -0
  212. package/dist/core/doctor.d.ts +8 -0
  213. package/dist/core/doctor.d.ts.map +1 -1
  214. package/dist/core/doctor.js +32 -1
  215. package/dist/core/doctor.js.map +1 -1
  216. package/dist/core/extensions/index.d.ts +1 -1
  217. package/dist/core/extensions/index.d.ts.map +1 -1
  218. package/dist/core/extensions/index.js.map +1 -1
  219. package/dist/core/extensions/loader.d.ts +5 -1
  220. package/dist/core/extensions/loader.d.ts.map +1 -1
  221. package/dist/core/extensions/loader.js +68 -13
  222. package/dist/core/extensions/loader.js.map +1 -1
  223. package/dist/core/extensions/runner.d.ts.map +1 -1
  224. package/dist/core/extensions/runner.js +7 -0
  225. package/dist/core/extensions/runner.js.map +1 -1
  226. package/dist/core/extensions/storage.d.ts +8 -0
  227. package/dist/core/extensions/storage.d.ts.map +1 -0
  228. package/dist/core/extensions/storage.js +28 -0
  229. package/dist/core/extensions/storage.js.map +1 -0
  230. package/dist/core/extensions/types.d.ts +101 -1
  231. package/dist/core/extensions/types.d.ts.map +1 -1
  232. package/dist/core/extensions/types.js.map +1 -1
  233. package/dist/core/failure-corpus.d.ts +1 -15
  234. package/dist/core/failure-corpus.d.ts.map +1 -1
  235. package/dist/core/failure-corpus.js +9 -37
  236. package/dist/core/failure-corpus.js.map +1 -1
  237. package/dist/core/foreground-recovery-controller.d.ts +52 -0
  238. package/dist/core/foreground-recovery-controller.d.ts.map +1 -0
  239. package/dist/core/foreground-recovery-controller.js +111 -0
  240. package/dist/core/foreground-recovery-controller.js.map +1 -0
  241. package/dist/core/gateways/channel-provider.d.ts +3 -0
  242. package/dist/core/gateways/channel-provider.d.ts.map +1 -1
  243. package/dist/core/gateways/channel-provider.js +31 -6
  244. package/dist/core/gateways/channel-provider.js.map +1 -1
  245. package/dist/core/goal-loop-controller.d.ts +17 -1
  246. package/dist/core/goal-loop-controller.d.ts.map +1 -1
  247. package/dist/core/goal-loop-controller.js +79 -11
  248. package/dist/core/goal-loop-controller.js.map +1 -1
  249. package/dist/core/goals/goal-acceptance.d.ts +11 -0
  250. package/dist/core/goals/goal-acceptance.d.ts.map +1 -0
  251. package/dist/core/goals/goal-acceptance.js +25 -0
  252. package/dist/core/goals/goal-acceptance.js.map +1 -0
  253. package/dist/core/goals/goal-auto-continue-controller.d.ts +27 -0
  254. package/dist/core/goals/goal-auto-continue-controller.d.ts.map +1 -0
  255. package/dist/core/goals/goal-auto-continue-controller.js +93 -0
  256. package/dist/core/goals/goal-auto-continue-controller.js.map +1 -0
  257. package/dist/core/goals/goal-continuation-controller.d.ts +48 -2
  258. package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -1
  259. package/dist/core/goals/goal-continuation-controller.js +87 -0
  260. package/dist/core/goals/goal-continuation-controller.js.map +1 -1
  261. package/dist/core/goals/goal-continuation-defaults.d.ts +39 -0
  262. package/dist/core/goals/goal-continuation-defaults.d.ts.map +1 -1
  263. package/dist/core/goals/goal-continuation-defaults.js +42 -0
  264. package/dist/core/goals/goal-continuation-defaults.js.map +1 -1
  265. package/dist/core/goals/goal-continuation-prompt.d.ts +5 -1
  266. package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -1
  267. package/dist/core/goals/goal-continuation-prompt.js +56 -15
  268. package/dist/core/goals/goal-continuation-prompt.js.map +1 -1
  269. package/dist/core/goals/goal-lifecycle.d.ts +20 -0
  270. package/dist/core/goals/goal-lifecycle.d.ts.map +1 -0
  271. package/dist/core/goals/goal-lifecycle.js +17 -0
  272. package/dist/core/goals/goal-lifecycle.js.map +1 -0
  273. package/dist/core/goals/goal-runtime-snapshot.d.ts +105 -4
  274. package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -1
  275. package/dist/core/goals/goal-runtime-snapshot.js +91 -6
  276. package/dist/core/goals/goal-runtime-snapshot.js.map +1 -1
  277. package/dist/core/goals/goal-state.d.ts +90 -1
  278. package/dist/core/goals/goal-state.d.ts.map +1 -1
  279. package/dist/core/goals/goal-state.js +78 -15
  280. package/dist/core/goals/goal-state.js.map +1 -1
  281. package/dist/core/goals/goal-tool-core.d.ts +58 -2
  282. package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
  283. package/dist/core/goals/goal-tool-core.js +82 -6
  284. package/dist/core/goals/goal-tool-core.js.map +1 -1
  285. package/dist/core/goals/session-goal-state.d.ts +13 -6
  286. package/dist/core/goals/session-goal-state.d.ts.map +1 -1
  287. package/dist/core/goals/session-goal-state.js +20 -28
  288. package/dist/core/goals/session-goal-state.js.map +1 -1
  289. package/dist/core/human-input.d.ts +100 -0
  290. package/dist/core/human-input.d.ts.map +1 -0
  291. package/dist/core/human-input.js +362 -0
  292. package/dist/core/human-input.js.map +1 -0
  293. package/dist/core/index.d.ts +9 -1
  294. package/dist/core/index.d.ts.map +1 -1
  295. package/dist/core/index.js +9 -1
  296. package/dist/core/index.js.map +1 -1
  297. package/dist/core/{telemetry.d.ts → install-telemetry.d.ts} +1 -1
  298. package/dist/core/install-telemetry.d.ts.map +1 -0
  299. package/dist/core/{telemetry.js → install-telemetry.js} +1 -1
  300. package/dist/core/install-telemetry.js.map +1 -0
  301. package/dist/core/keybindings.d.ts +27 -1
  302. package/dist/core/keybindings.d.ts.map +1 -1
  303. package/dist/core/keybindings.js +34 -8
  304. package/dist/core/keybindings.js.map +1 -1
  305. package/dist/core/learning/learning-audit.d.ts +2 -4
  306. package/dist/core/learning/learning-audit.d.ts.map +1 -1
  307. package/dist/core/learning/learning-audit.js +9 -29
  308. package/dist/core/learning/learning-audit.js.map +1 -1
  309. package/dist/core/learning/learning-gate.d.ts.map +1 -1
  310. package/dist/core/learning/learning-gate.js +1 -6
  311. package/dist/core/learning/learning-gate.js.map +1 -1
  312. package/dist/core/learning/observation-store.d.ts +12 -3
  313. package/dist/core/learning/observation-store.d.ts.map +1 -1
  314. package/dist/core/learning/observation-store.js +30 -15
  315. package/dist/core/learning/observation-store.js.map +1 -1
  316. package/dist/core/learning/session-learning-decision.d.ts +2 -4
  317. package/dist/core/learning/session-learning-decision.d.ts.map +1 -1
  318. package/dist/core/learning/session-learning-decision.js +9 -29
  319. package/dist/core/learning/session-learning-decision.js.map +1 -1
  320. package/dist/core/learning/skill-curator.d.ts +5 -1
  321. package/dist/core/learning/skill-curator.d.ts.map +1 -1
  322. package/dist/core/learning/skill-curator.js +21 -19
  323. package/dist/core/learning/skill-curator.js.map +1 -1
  324. package/dist/core/local-runtime-controller.d.ts +65 -3
  325. package/dist/core/local-runtime-controller.d.ts.map +1 -1
  326. package/dist/core/local-runtime-controller.js +186 -26
  327. package/dist/core/local-runtime-controller.js.map +1 -1
  328. package/dist/core/memory/providers/file-store.d.ts +1 -1
  329. package/dist/core/memory/providers/file-store.d.ts.map +1 -1
  330. package/dist/core/memory/providers/file-store.js +6 -6
  331. package/dist/core/memory/providers/file-store.js.map +1 -1
  332. package/dist/core/memory-controller.d.ts.map +1 -1
  333. package/dist/core/memory-controller.js +4 -4
  334. package/dist/core/memory-controller.js.map +1 -1
  335. package/dist/core/model-capability.d.ts +34 -0
  336. package/dist/core/model-capability.d.ts.map +1 -1
  337. package/dist/core/model-capability.js +43 -1
  338. package/dist/core/model-capability.js.map +1 -1
  339. package/dist/core/model-resolver.d.ts +1 -1
  340. package/dist/core/model-resolver.d.ts.map +1 -1
  341. package/dist/core/model-resolver.js +6 -9
  342. package/dist/core/model-resolver.js.map +1 -1
  343. package/dist/core/model-router/intent-classifier.d.ts +1 -4
  344. package/dist/core/model-router/intent-classifier.d.ts.map +1 -1
  345. package/dist/core/model-router/intent-classifier.js +1 -156
  346. package/dist/core/model-router/intent-classifier.js.map +1 -1
  347. package/dist/core/model-router/tool-escalation.d.ts +15 -0
  348. package/dist/core/model-router/tool-escalation.d.ts.map +1 -1
  349. package/dist/core/model-router/tool-escalation.js +23 -1
  350. package/dist/core/model-router/tool-escalation.js.map +1 -1
  351. package/dist/core/model-router-controller.d.ts +34 -7
  352. package/dist/core/model-router-controller.d.ts.map +1 -1
  353. package/dist/core/model-router-controller.js +95 -16
  354. package/dist/core/model-router-controller.js.map +1 -1
  355. package/dist/core/model-selection-controller.d.ts +4 -4
  356. package/dist/core/model-selection-controller.d.ts.map +1 -1
  357. package/dist/core/model-selection-controller.js.map +1 -1
  358. package/dist/core/model-tool-protocol.d.ts +23 -0
  359. package/dist/core/model-tool-protocol.d.ts.map +1 -0
  360. package/dist/core/model-tool-protocol.js +43 -0
  361. package/dist/core/model-tool-protocol.js.map +1 -0
  362. package/dist/core/models/adaptation-store.d.ts +10 -5
  363. package/dist/core/models/adaptation-store.d.ts.map +1 -1
  364. package/dist/core/models/adaptation-store.js +129 -81
  365. package/dist/core/models/adaptation-store.js.map +1 -1
  366. package/dist/core/models/default-model-suggestions.d.ts.map +1 -1
  367. package/dist/core/models/default-model-suggestions.js +17 -0
  368. package/dist/core/models/default-model-suggestions.js.map +1 -1
  369. package/dist/core/models/fitness-store.d.ts +4 -11
  370. package/dist/core/models/fitness-store.d.ts.map +1 -1
  371. package/dist/core/models/fitness-store.js +87 -51
  372. package/dist/core/models/fitness-store.js.map +1 -1
  373. package/dist/core/models/host-state-store.d.ts +34 -0
  374. package/dist/core/models/host-state-store.d.ts.map +1 -0
  375. package/dist/core/models/host-state-store.js +86 -0
  376. package/dist/core/models/host-state-store.js.map +1 -0
  377. package/dist/core/models/llamacpp-runtime.d.ts +180 -0
  378. package/dist/core/models/llamacpp-runtime.d.ts.map +1 -0
  379. package/dist/core/models/llamacpp-runtime.js +475 -0
  380. package/dist/core/models/llamacpp-runtime.js.map +1 -0
  381. package/dist/core/models/local-registration.d.ts +40 -0
  382. package/dist/core/models/local-registration.d.ts.map +1 -1
  383. package/dist/core/models/local-registration.js +94 -5
  384. package/dist/core/models/local-registration.js.map +1 -1
  385. package/dist/core/models/local-runtime.d.ts.map +1 -1
  386. package/dist/core/models/local-runtime.js +6 -5
  387. package/dist/core/models/local-runtime.js.map +1 -1
  388. package/dist/core/models/model-ref.d.ts +7 -0
  389. package/dist/core/models/model-ref.d.ts.map +1 -1
  390. package/dist/core/models/model-ref.js +26 -0
  391. package/dist/core/models/model-ref.js.map +1 -1
  392. package/dist/core/models/needle-runtime.d.ts +257 -0
  393. package/dist/core/models/needle-runtime.d.ts.map +1 -0
  394. package/dist/core/models/needle-runtime.js +519 -0
  395. package/dist/core/models/needle-runtime.js.map +1 -0
  396. package/dist/core/models/prism-llamacpp-lifecycle.d.ts +89 -0
  397. package/dist/core/models/prism-llamacpp-lifecycle.d.ts.map +1 -0
  398. package/dist/core/models/prism-llamacpp-lifecycle.js +121 -0
  399. package/dist/core/models/prism-llamacpp-lifecycle.js.map +1 -0
  400. package/dist/core/orchestration/agent-resume.d.ts +24 -0
  401. package/dist/core/orchestration/agent-resume.d.ts.map +1 -0
  402. package/dist/core/orchestration/agent-resume.js +90 -0
  403. package/dist/core/orchestration/agent-resume.js.map +1 -0
  404. package/dist/core/orchestration/capability-gateway.d.ts +61 -0
  405. package/dist/core/orchestration/capability-gateway.d.ts.map +1 -0
  406. package/dist/core/orchestration/capability-gateway.js +152 -0
  407. package/dist/core/orchestration/capability-gateway.js.map +1 -0
  408. package/dist/core/orchestration/contracts.d.ts +343 -0
  409. package/dist/core/orchestration/contracts.d.ts.map +1 -0
  410. package/dist/core/orchestration/contracts.js +135 -0
  411. package/dist/core/orchestration/contracts.js.map +1 -0
  412. package/dist/core/orchestration/delegation-ledger.d.ts +56 -0
  413. package/dist/core/orchestration/delegation-ledger.d.ts.map +1 -0
  414. package/dist/core/orchestration/delegation-ledger.js +197 -0
  415. package/dist/core/orchestration/delegation-ledger.js.map +1 -0
  416. package/dist/core/orchestration/event-store.d.ts +83 -0
  417. package/dist/core/orchestration/event-store.d.ts.map +1 -0
  418. package/dist/core/orchestration/event-store.js +560 -0
  419. package/dist/core/orchestration/event-store.js.map +1 -0
  420. package/dist/core/orchestration/lane-tool-manifests.d.ts +9 -0
  421. package/dist/core/orchestration/lane-tool-manifests.d.ts.map +1 -0
  422. package/dist/core/orchestration/lane-tool-manifests.js +37 -0
  423. package/dist/core/orchestration/lane-tool-manifests.js.map +1 -0
  424. package/dist/core/orchestration/model-binding.d.ts +15 -0
  425. package/dist/core/orchestration/model-binding.d.ts.map +1 -0
  426. package/dist/core/orchestration/model-binding.js +29 -0
  427. package/dist/core/orchestration/model-binding.js.map +1 -0
  428. package/dist/core/orchestration/policy-compiler.d.ts +49 -0
  429. package/dist/core/orchestration/policy-compiler.d.ts.map +1 -0
  430. package/dist/core/orchestration/policy-compiler.js +191 -0
  431. package/dist/core/orchestration/policy-compiler.js.map +1 -0
  432. package/dist/core/orchestration/policy-gate.d.ts +18 -0
  433. package/dist/core/orchestration/policy-gate.d.ts.map +1 -0
  434. package/dist/core/orchestration/policy-gate.js +24 -0
  435. package/dist/core/orchestration/policy-gate.js.map +1 -0
  436. package/dist/core/orchestration/profile-registry.d.ts +51 -0
  437. package/dist/core/orchestration/profile-registry.d.ts.map +1 -0
  438. package/dist/core/orchestration/profile-registry.js +393 -0
  439. package/dist/core/orchestration/profile-registry.js.map +1 -0
  440. package/dist/core/orchestration/profile-store.d.ts +31 -0
  441. package/dist/core/orchestration/profile-store.d.ts.map +1 -0
  442. package/dist/core/orchestration/profile-store.js +136 -0
  443. package/dist/core/orchestration/profile-store.js.map +1 -0
  444. package/dist/core/orchestration/risk-budget.d.ts +9 -0
  445. package/dist/core/orchestration/risk-budget.d.ts.map +1 -0
  446. package/dist/core/orchestration/risk-budget.js +48 -0
  447. package/dist/core/orchestration/risk-budget.js.map +1 -0
  448. package/dist/core/orchestration/task-runtime.d.ts +168 -0
  449. package/dist/core/orchestration/task-runtime.d.ts.map +1 -0
  450. package/dist/core/orchestration/task-runtime.js +1600 -0
  451. package/dist/core/orchestration/task-runtime.js.map +1 -0
  452. package/dist/core/orchestration/work-state-projection.d.ts +32 -0
  453. package/dist/core/orchestration/work-state-projection.d.ts.map +1 -0
  454. package/dist/core/orchestration/work-state-projection.js +116 -0
  455. package/dist/core/orchestration/work-state-projection.js.map +1 -0
  456. package/dist/core/orchestration/worker-execution-contract.d.ts +21 -0
  457. package/dist/core/orchestration/worker-execution-contract.d.ts.map +1 -0
  458. package/dist/core/orchestration/worker-execution-contract.js +179 -0
  459. package/dist/core/orchestration/worker-execution-contract.js.map +1 -0
  460. package/dist/core/orchestration/worker-result-adapter.d.ts +21 -0
  461. package/dist/core/orchestration/worker-result-adapter.d.ts.map +1 -0
  462. package/dist/core/orchestration/worker-result-adapter.js +109 -0
  463. package/dist/core/orchestration/worker-result-adapter.js.map +1 -0
  464. package/dist/core/package-manager.d.ts.map +1 -1
  465. package/dist/core/package-manager.js +11 -10
  466. package/dist/core/package-manager.js.map +1 -1
  467. package/dist/core/process-identity.d.ts +11 -0
  468. package/dist/core/process-identity.d.ts.map +1 -0
  469. package/dist/core/process-identity.js +26 -0
  470. package/dist/core/process-identity.js.map +1 -0
  471. package/dist/core/process-matrix/codes.d.ts +83 -0
  472. package/dist/core/process-matrix/codes.d.ts.map +1 -0
  473. package/dist/core/process-matrix/codes.js +15 -0
  474. package/dist/core/process-matrix/codes.js.map +1 -0
  475. package/dist/core/process-matrix/resume-launcher.d.ts +26 -0
  476. package/dist/core/process-matrix/resume-launcher.d.ts.map +1 -0
  477. package/dist/core/process-matrix/resume-launcher.js +62 -0
  478. package/dist/core/process-matrix/resume-launcher.js.map +1 -0
  479. package/dist/core/process-matrix/runtime.d.ts +78 -0
  480. package/dist/core/process-matrix/runtime.d.ts.map +1 -0
  481. package/dist/core/process-matrix/runtime.js +633 -0
  482. package/dist/core/process-matrix/runtime.js.map +1 -0
  483. package/dist/core/process-matrix/store.d.ts +25 -0
  484. package/dist/core/process-matrix/store.d.ts.map +1 -0
  485. package/dist/core/process-matrix/store.js +189 -0
  486. package/dist/core/process-matrix/store.js.map +1 -0
  487. package/dist/core/process-matrix/supervisor.d.ts +75 -0
  488. package/dist/core/process-matrix/supervisor.d.ts.map +1 -0
  489. package/dist/core/process-matrix/supervisor.js +161 -0
  490. package/dist/core/process-matrix/supervisor.js.map +1 -0
  491. package/dist/core/profile-registry.d.ts +7 -0
  492. package/dist/core/profile-registry.d.ts.map +1 -1
  493. package/dist/core/profile-registry.js +25 -7
  494. package/dist/core/profile-registry.js.map +1 -1
  495. package/dist/core/python-runtime.d.ts.map +1 -1
  496. package/dist/core/python-runtime.js +3 -3
  497. package/dist/core/python-runtime.js.map +1 -1
  498. package/dist/core/reflection-controller.d.ts +33 -9
  499. package/dist/core/reflection-controller.d.ts.map +1 -1
  500. package/dist/core/reflection-controller.js +217 -127
  501. package/dist/core/reflection-controller.js.map +1 -1
  502. package/dist/core/reload-blockers.d.ts +36 -0
  503. package/dist/core/reload-blockers.d.ts.map +1 -1
  504. package/dist/core/reload-blockers.js +50 -8
  505. package/dist/core/reload-blockers.js.map +1 -1
  506. package/dist/core/research/evidence-bundle.d.ts.map +1 -1
  507. package/dist/core/research/evidence-bundle.js +1 -6
  508. package/dist/core/research/evidence-bundle.js.map +1 -1
  509. package/dist/core/research/lane-model-resolver.d.ts +33 -0
  510. package/dist/core/research/lane-model-resolver.d.ts.map +1 -0
  511. package/dist/core/research/lane-model-resolver.js +65 -0
  512. package/dist/core/research/lane-model-resolver.js.map +1 -0
  513. package/dist/core/research/model-fitness-controller.d.ts +38 -0
  514. package/dist/core/research/model-fitness-controller.d.ts.map +1 -0
  515. package/dist/core/research/model-fitness-controller.js +106 -0
  516. package/dist/core/research/model-fitness-controller.js.map +1 -0
  517. package/dist/core/research/model-fitness.d.ts +1 -1
  518. package/dist/core/research/model-fitness.d.ts.map +1 -1
  519. package/dist/core/research/model-fitness.js +4 -4
  520. package/dist/core/research/model-fitness.js.map +1 -1
  521. package/dist/core/research/research-gate.d.ts.map +1 -1
  522. package/dist/core/research/research-gate.js +7 -7
  523. package/dist/core/research/research-gate.js.map +1 -1
  524. package/dist/core/research/research-lane-controller.d.ts +62 -0
  525. package/dist/core/research/research-lane-controller.d.ts.map +1 -0
  526. package/dist/core/research/research-lane-controller.js +292 -0
  527. package/dist/core/research/research-lane-controller.js.map +1 -0
  528. package/dist/core/research/session-evidence-bundle.d.ts +2 -4
  529. package/dist/core/research/session-evidence-bundle.d.ts.map +1 -1
  530. package/dist/core/research/session-evidence-bundle.js +10 -46
  531. package/dist/core/research/session-evidence-bundle.js.map +1 -1
  532. package/dist/core/resource-loader.d.ts.map +1 -1
  533. package/dist/core/resource-loader.js +15 -9
  534. package/dist/core/resource-loader.js.map +1 -1
  535. package/dist/core/risk-classifier.d.ts +6 -0
  536. package/dist/core/risk-classifier.d.ts.map +1 -0
  537. package/dist/core/risk-classifier.js +225 -0
  538. package/dist/core/risk-classifier.js.map +1 -0
  539. package/dist/core/runtime-builder.d.ts +76 -17
  540. package/dist/core/runtime-builder.d.ts.map +1 -1
  541. package/dist/core/runtime-builder.js +367 -124
  542. package/dist/core/runtime-builder.js.map +1 -1
  543. package/dist/core/scout-controller.d.ts +6 -0
  544. package/dist/core/scout-controller.d.ts.map +1 -1
  545. package/dist/core/scout-controller.js +66 -52
  546. package/dist/core/scout-controller.js.map +1 -1
  547. package/dist/core/sdk.d.ts +6 -3
  548. package/dist/core/sdk.d.ts.map +1 -1
  549. package/dist/core/sdk.js +64 -27
  550. package/dist/core/sdk.js.map +1 -1
  551. package/dist/core/session-analytics.d.ts.map +1 -1
  552. package/dist/core/session-analytics.js +28 -1
  553. package/dist/core/session-analytics.js.map +1 -1
  554. package/dist/core/session-image-store.d.ts +33 -0
  555. package/dist/core/session-image-store.d.ts.map +1 -0
  556. package/dist/core/session-image-store.js +283 -0
  557. package/dist/core/session-image-store.js.map +1 -0
  558. package/dist/core/session-role.d.ts +33 -0
  559. package/dist/core/session-role.d.ts.map +1 -0
  560. package/dist/core/session-role.js +58 -0
  561. package/dist/core/session-role.js.map +1 -0
  562. package/dist/core/session-snapshot.d.ts +34 -0
  563. package/dist/core/session-snapshot.d.ts.map +1 -0
  564. package/dist/core/session-snapshot.js +68 -0
  565. package/dist/core/session-snapshot.js.map +1 -0
  566. package/dist/core/session-supervision-runtime.d.ts +31 -0
  567. package/dist/core/session-supervision-runtime.d.ts.map +1 -0
  568. package/dist/core/session-supervision-runtime.js +97 -0
  569. package/dist/core/session-supervision-runtime.js.map +1 -0
  570. package/dist/core/settings-manager.d.ts +51 -7
  571. package/dist/core/settings-manager.d.ts.map +1 -1
  572. package/dist/core/settings-manager.js +168 -82
  573. package/dist/core/settings-manager.js.map +1 -1
  574. package/dist/core/system-prompt-builder.d.ts +12 -0
  575. package/dist/core/system-prompt-builder.d.ts.map +1 -1
  576. package/dist/core/system-prompt-builder.js +6 -0
  577. package/dist/core/system-prompt-builder.js.map +1 -1
  578. package/dist/core/system-prompt.d.ts +14 -1
  579. package/dist/core/system-prompt.d.ts.map +1 -1
  580. package/dist/core/system-prompt.js +20 -3
  581. package/dist/core/system-prompt.js.map +1 -1
  582. package/dist/core/tasks/session-task-state.d.ts +13 -6
  583. package/dist/core/tasks/session-task-state.d.ts.map +1 -1
  584. package/dist/core/tasks/session-task-state.js +20 -22
  585. package/dist/core/tasks/session-task-state.js.map +1 -1
  586. package/dist/core/tasks/task-contract-monitor.d.ts +45 -0
  587. package/dist/core/tasks/task-contract-monitor.d.ts.map +1 -0
  588. package/dist/core/tasks/task-contract-monitor.js +56 -0
  589. package/dist/core/tasks/task-contract-monitor.js.map +1 -0
  590. package/dist/core/tasks/task-projection.d.ts +20 -0
  591. package/dist/core/tasks/task-projection.d.ts.map +1 -0
  592. package/dist/core/tasks/task-projection.js +47 -0
  593. package/dist/core/tasks/task-projection.js.map +1 -0
  594. package/dist/core/tasks/task-state.d.ts +12 -0
  595. package/dist/core/tasks/task-state.d.ts.map +1 -1
  596. package/dist/core/tasks/task-state.js +56 -12
  597. package/dist/core/tasks/task-state.js.map +1 -1
  598. package/dist/core/tool-capability-policy.d.ts +13 -0
  599. package/dist/core/tool-capability-policy.d.ts.map +1 -0
  600. package/dist/core/tool-capability-policy.js +85 -0
  601. package/dist/core/tool-capability-policy.js.map +1 -0
  602. package/dist/core/tool-gate-controller.d.ts.map +1 -1
  603. package/dist/core/tool-gate-controller.js +5 -0
  604. package/dist/core/tool-gate-controller.js.map +1 -1
  605. package/dist/core/tool-protocol-controller.d.ts +91 -0
  606. package/dist/core/tool-protocol-controller.d.ts.map +1 -0
  607. package/dist/core/tool-protocol-controller.js +512 -0
  608. package/dist/core/tool-protocol-controller.js.map +1 -0
  609. package/dist/core/tool-recovery-log-records.d.ts +6 -0
  610. package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
  611. package/dist/core/tool-recovery-log-records.js +13 -31
  612. package/dist/core/tool-recovery-log-records.js.map +1 -1
  613. package/dist/core/tool-selection/promotion.d.ts +54 -0
  614. package/dist/core/tool-selection/promotion.d.ts.map +1 -0
  615. package/dist/core/tool-selection/promotion.js +81 -0
  616. package/dist/core/tool-selection/promotion.js.map +1 -0
  617. package/dist/core/tool-selection/tool-performance-store.d.ts +39 -6
  618. package/dist/core/tool-selection/tool-performance-store.d.ts.map +1 -1
  619. package/dist/core/tool-selection/tool-performance-store.js +153 -110
  620. package/dist/core/tool-selection/tool-performance-store.js.map +1 -1
  621. package/dist/core/tool-selection/tool-selection-controller.d.ts +45 -0
  622. package/dist/core/tool-selection/tool-selection-controller.d.ts.map +1 -1
  623. package/dist/core/tool-selection/tool-selection-controller.js +96 -0
  624. package/dist/core/tool-selection/tool-selection-controller.js.map +1 -1
  625. package/dist/core/tools/ask-question.d.ts +124 -0
  626. package/dist/core/tools/ask-question.d.ts.map +1 -0
  627. package/dist/core/tools/ask-question.js +693 -0
  628. package/dist/core/tools/ask-question.js.map +1 -0
  629. package/dist/core/tools/bash.d.ts +18 -0
  630. package/dist/core/tools/bash.d.ts.map +1 -1
  631. package/dist/core/tools/bash.js +95 -16
  632. package/dist/core/tools/bash.js.map +1 -1
  633. package/dist/core/tools/delegate-status.d.ts +44 -2
  634. package/dist/core/tools/delegate-status.d.ts.map +1 -1
  635. package/dist/core/tools/delegate-status.js +214 -19
  636. package/dist/core/tools/delegate-status.js.map +1 -1
  637. package/dist/core/tools/delegate.d.ts +15 -13
  638. package/dist/core/tools/delegate.d.ts.map +1 -1
  639. package/dist/core/tools/delegate.js +124 -22
  640. package/dist/core/tools/delegate.js.map +1 -1
  641. package/dist/core/tools/extensionify.d.ts.map +1 -1
  642. package/dist/core/tools/extensionify.js +1 -0
  643. package/dist/core/tools/extensionify.js.map +1 -1
  644. package/dist/core/tools/goal.d.ts +94 -3
  645. package/dist/core/tools/goal.d.ts.map +1 -1
  646. package/dist/core/tools/goal.js +165 -15
  647. package/dist/core/tools/goal.js.map +1 -1
  648. package/dist/core/tools/model-fitness.d.ts +7 -0
  649. package/dist/core/tools/model-fitness.d.ts.map +1 -1
  650. package/dist/core/tools/model-fitness.js +4 -4
  651. package/dist/core/tools/model-fitness.js.map +1 -1
  652. package/dist/core/tools/orchestration-panel.d.ts +43 -0
  653. package/dist/core/tools/orchestration-panel.d.ts.map +1 -0
  654. package/dist/core/tools/orchestration-panel.js +168 -0
  655. package/dist/core/tools/orchestration-panel.js.map +1 -0
  656. package/dist/core/tools/run-process.d.ts +26 -0
  657. package/dist/core/tools/run-process.d.ts.map +1 -0
  658. package/dist/core/tools/run-process.js +138 -0
  659. package/dist/core/tools/run-process.js.map +1 -0
  660. package/dist/core/tools/shell-contract-router.d.ts +6 -1
  661. package/dist/core/tools/shell-contract-router.d.ts.map +1 -1
  662. package/dist/core/tools/shell-contract-router.js +23 -2
  663. package/dist/core/tools/shell-contract-router.js.map +1 -1
  664. package/dist/core/tools/shell-session.d.ts +89 -0
  665. package/dist/core/tools/shell-session.d.ts.map +1 -0
  666. package/dist/core/tools/shell-session.js +432 -0
  667. package/dist/core/tools/shell-session.js.map +1 -0
  668. package/dist/core/tools/task-steps.d.ts +6 -0
  669. package/dist/core/tools/task-steps.d.ts.map +1 -1
  670. package/dist/core/tools/task-steps.js +165 -8
  671. package/dist/core/tools/task-steps.js.map +1 -1
  672. package/dist/core/tools/tmux-dispatch.d.ts +84 -0
  673. package/dist/core/tools/tmux-dispatch.d.ts.map +1 -0
  674. package/dist/core/tools/tmux-dispatch.js +91 -0
  675. package/dist/core/tools/tmux-dispatch.js.map +1 -0
  676. package/dist/core/tools/windows-shell-engine.d.ts +42 -0
  677. package/dist/core/tools/windows-shell-engine.d.ts.map +1 -0
  678. package/dist/core/tools/windows-shell-engine.js +151 -0
  679. package/dist/core/tools/windows-shell-engine.js.map +1 -0
  680. package/dist/core/tools/windows-shell-state.d.ts +40 -0
  681. package/dist/core/tools/windows-shell-state.d.ts.map +1 -0
  682. package/dist/core/tools/windows-shell-state.js +59 -0
  683. package/dist/core/tools/windows-shell-state.js.map +1 -0
  684. package/dist/core/tools/worktree-sync.d.ts +24 -0
  685. package/dist/core/tools/worktree-sync.d.ts.map +1 -0
  686. package/dist/core/tools/worktree-sync.js +338 -0
  687. package/dist/core/tools/worktree-sync.js.map +1 -0
  688. package/dist/core/trust-manager.d.ts +4 -1
  689. package/dist/core/trust-manager.d.ts.map +1 -1
  690. package/dist/core/trust-manager.js +20 -2
  691. package/dist/core/trust-manager.js.map +1 -1
  692. package/dist/core/util/atomic-file.d.ts +55 -0
  693. package/dist/core/util/atomic-file.d.ts.map +1 -0
  694. package/dist/core/util/atomic-file.js +255 -0
  695. package/dist/core/util/atomic-file.js.map +1 -0
  696. package/dist/core/util/bounded-jsonl.d.ts +10 -0
  697. package/dist/core/util/bounded-jsonl.d.ts.map +1 -0
  698. package/dist/core/util/bounded-jsonl.js +75 -0
  699. package/dist/core/util/bounded-jsonl.js.map +1 -0
  700. package/dist/core/util/minimatch-cache.d.ts +33 -0
  701. package/dist/core/util/minimatch-cache.d.ts.map +1 -0
  702. package/dist/core/util/minimatch-cache.js +0 -0
  703. package/dist/core/util/minimatch-cache.js.map +1 -0
  704. package/dist/core/util/value-guards.d.ts +5 -0
  705. package/dist/core/util/value-guards.d.ts.map +1 -0
  706. package/dist/core/util/value-guards.js +12 -0
  707. package/dist/core/util/value-guards.js.map +1 -0
  708. package/dist/core/worktree-sync/codes.d.ts +227 -0
  709. package/dist/core/worktree-sync/codes.d.ts.map +1 -0
  710. package/dist/core/worktree-sync/codes.js +14 -0
  711. package/dist/core/worktree-sync/codes.js.map +1 -0
  712. package/dist/core/worktree-sync/git-engine.d.ts +156 -0
  713. package/dist/core/worktree-sync/git-engine.d.ts.map +1 -0
  714. package/dist/core/worktree-sync/git-engine.js +1191 -0
  715. package/dist/core/worktree-sync/git-engine.js.map +1 -0
  716. package/dist/core/worktree-sync/lane-gate.d.ts +75 -0
  717. package/dist/core/worktree-sync/lane-gate.d.ts.map +1 -0
  718. package/dist/core/worktree-sync/lane-gate.js +360 -0
  719. package/dist/core/worktree-sync/lane-gate.js.map +1 -0
  720. package/dist/core/worktree-sync/runtime.d.ts +47 -0
  721. package/dist/core/worktree-sync/runtime.d.ts.map +1 -0
  722. package/dist/core/worktree-sync/runtime.js +96 -0
  723. package/dist/core/worktree-sync/runtime.js.map +1 -0
  724. package/dist/core/worktree-sync/store.d.ts +69 -0
  725. package/dist/core/worktree-sync/store.d.ts.map +1 -0
  726. package/dist/core/worktree-sync/store.js +252 -0
  727. package/dist/core/worktree-sync/store.js.map +1 -0
  728. package/dist/core/worktree-sync/watcher.d.ts +29 -0
  729. package/dist/core/worktree-sync/watcher.d.ts.map +1 -0
  730. package/dist/core/worktree-sync/watcher.js +93 -0
  731. package/dist/core/worktree-sync/watcher.js.map +1 -0
  732. package/dist/index.d.ts +13 -3
  733. package/dist/index.d.ts.map +1 -1
  734. package/dist/index.js +12 -2
  735. package/dist/index.js.map +1 -1
  736. package/dist/main.d.ts.map +1 -1
  737. package/dist/main.js +179 -1
  738. package/dist/main.js.map +1 -1
  739. package/dist/migrations.d.ts +7 -0
  740. package/dist/migrations.d.ts.map +1 -1
  741. package/dist/migrations.js +197 -1
  742. package/dist/migrations.js.map +1 -1
  743. package/dist/modes/interactive/auto-learn-controller.d.ts +16 -1
  744. package/dist/modes/interactive/auto-learn-controller.d.ts.map +1 -1
  745. package/dist/modes/interactive/auto-learn-controller.js +50 -8
  746. package/dist/modes/interactive/auto-learn-controller.js.map +1 -1
  747. package/dist/modes/interactive/clipboard-input.d.ts +7 -1
  748. package/dist/modes/interactive/clipboard-input.d.ts.map +1 -1
  749. package/dist/modes/interactive/clipboard-input.js +44 -6
  750. package/dist/modes/interactive/clipboard-input.js.map +1 -1
  751. package/dist/modes/interactive/components/profile-resource-editor.d.ts.map +1 -1
  752. package/dist/modes/interactive/components/profile-resource-editor.js +23 -1
  753. package/dist/modes/interactive/components/profile-resource-editor.js.map +1 -1
  754. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  755. package/dist/modes/interactive/components/settings-selector.js +2 -2
  756. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  757. package/dist/modes/interactive/config-backup.d.ts.map +1 -1
  758. package/dist/modes/interactive/config-backup.js +5 -4
  759. package/dist/modes/interactive/config-backup.js.map +1 -1
  760. package/dist/modes/interactive/extension-ui-host.d.ts +14 -0
  761. package/dist/modes/interactive/extension-ui-host.d.ts.map +1 -1
  762. package/dist/modes/interactive/extension-ui-host.js +76 -15
  763. package/dist/modes/interactive/extension-ui-host.js.map +1 -1
  764. package/dist/modes/interactive/interactive-mode.d.ts +4 -0
  765. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  766. package/dist/modes/interactive/interactive-mode.js +67 -1
  767. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  768. package/dist/modes/interactive/key-handlers.d.ts.map +1 -1
  769. package/dist/modes/interactive/key-handlers.js +1 -1
  770. package/dist/modes/interactive/key-handlers.js.map +1 -1
  771. package/dist/modes/interactive/local-model-commands.d.ts +43 -0
  772. package/dist/modes/interactive/local-model-commands.d.ts.map +1 -1
  773. package/dist/modes/interactive/local-model-commands.js +290 -3
  774. package/dist/modes/interactive/local-model-commands.js.map +1 -1
  775. package/dist/modes/interactive/profile-menu-controller.d.ts.map +1 -1
  776. package/dist/modes/interactive/profile-menu-controller.js +7 -7
  777. package/dist/modes/interactive/profile-menu-controller.js.map +1 -1
  778. package/dist/modes/interactive/report-commands.d.ts.map +1 -1
  779. package/dist/modes/interactive/report-commands.js +21 -0
  780. package/dist/modes/interactive/report-commands.js.map +1 -1
  781. package/dist/modes/interactive/session-flow-commands.d.ts +17 -2
  782. package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
  783. package/dist/modes/interactive/session-flow-commands.js +74 -21
  784. package/dist/modes/interactive/session-flow-commands.js.map +1 -1
  785. package/dist/modes/interactive/session-io-commands.d.ts.map +1 -1
  786. package/dist/modes/interactive/session-io-commands.js +16 -6
  787. package/dist/modes/interactive/session-io-commands.js.map +1 -1
  788. package/dist/modes/interactive/session-replacement-errors.d.ts +7 -0
  789. package/dist/modes/interactive/session-replacement-errors.d.ts.map +1 -0
  790. package/dist/modes/interactive/session-replacement-errors.js +15 -0
  791. package/dist/modes/interactive/session-replacement-errors.js.map +1 -0
  792. package/dist/modes/interactive/startup-checks.d.ts.map +1 -1
  793. package/dist/modes/interactive/startup-checks.js +1 -1
  794. package/dist/modes/interactive/startup-checks.js.map +1 -1
  795. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  796. package/dist/modes/rpc/rpc-mode.js +82 -23
  797. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  798. package/dist/modes/rpc/rpc-types.d.ts +15 -0
  799. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  800. package/dist/modes/rpc/rpc-types.js +138 -1
  801. package/dist/modes/rpc/rpc-types.js.map +1 -1
  802. package/dist/utils/clipboard-image.d.ts.map +1 -1
  803. package/dist/utils/clipboard-image.js +2 -13
  804. package/dist/utils/clipboard-image.js.map +1 -1
  805. package/dist/utils/clipboard-native.d.ts +1 -0
  806. package/dist/utils/clipboard-native.d.ts.map +1 -1
  807. package/dist/utils/clipboard-native.js.map +1 -1
  808. package/dist/utils/clipboard.d.ts +1 -0
  809. package/dist/utils/clipboard.d.ts.map +1 -1
  810. package/dist/utils/clipboard.js +52 -1
  811. package/dist/utils/clipboard.js.map +1 -1
  812. package/dist/utils/fs-watch.d.ts +11 -0
  813. package/dist/utils/fs-watch.d.ts.map +1 -1
  814. package/dist/utils/fs-watch.js +20 -2
  815. package/dist/utils/fs-watch.js.map +1 -1
  816. package/dist/utils/highlight-js-languages.d.ts +4 -0
  817. package/dist/utils/highlight-js-languages.d.ts.map +1 -0
  818. package/dist/utils/highlight-js-languages.js +573 -0
  819. package/dist/utils/highlight-js-languages.js.map +1 -0
  820. package/dist/utils/platform.d.ts +3 -0
  821. package/dist/utils/platform.d.ts.map +1 -0
  822. package/dist/utils/platform.js +8 -0
  823. package/dist/utils/platform.js.map +1 -0
  824. package/dist/utils/shell.d.ts +7 -1
  825. package/dist/utils/shell.d.ts.map +1 -1
  826. package/dist/utils/shell.js +21 -6
  827. package/dist/utils/shell.js.map +1 -1
  828. package/dist/utils/syntax-highlight.d.ts.map +1 -1
  829. package/dist/utils/syntax-highlight.js +53 -5
  830. package/dist/utils/syntax-highlight.js.map +1 -1
  831. package/dist/utils/tools-manager.d.ts.map +1 -1
  832. package/dist/utils/tools-manager.js +112 -1
  833. package/dist/utils/tools-manager.js.map +1 -1
  834. package/dist/utils/work-directory.d.ts +11 -0
  835. package/dist/utils/work-directory.d.ts.map +1 -1
  836. package/dist/utils/work-directory.js +30 -9
  837. package/dist/utils/work-directory.js.map +1 -1
  838. package/docs/UI_POLISH_ORCHESTRATION.md +44 -0
  839. package/docs/UI_QUALITY_ORCHESTRATION.md +30 -0
  840. package/docs/docs.json +4 -0
  841. package/docs/extensions.md +62 -2
  842. package/docs/harness-architecture.md +156 -0
  843. package/docs/keybindings.md +11 -1
  844. package/docs/process-matrix.md +153 -0
  845. package/docs/rpc.md +45 -2
  846. package/docs/sessions.md +15 -0
  847. package/docs/settings.md +128 -11
  848. package/docs/task-steps.md +8 -0
  849. package/docs/telemetry.md +19 -0
  850. package/docs/tmux-agent-manager.md +92 -2
  851. package/docs/ui-reference/README.md +9 -0
  852. package/docs/ui-reference/orchestration-after.txt +7 -0
  853. package/docs/ui-reference/orchestration-before.txt +7 -0
  854. package/docs/ui-reference/orchestration-reference.txt +6 -0
  855. package/docs/usage.md +14 -2
  856. package/docs/windows.md +52 -3
  857. package/docs/work-directory.md +42 -2
  858. package/docs/worktree-sync.md +250 -0
  859. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  860. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  861. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  862. package/examples/extensions/question.ts +34 -245
  863. package/examples/extensions/questionnaire.ts +41 -407
  864. package/examples/extensions/sandbox/package-lock.json +2 -2
  865. package/examples/extensions/sandbox/package.json +1 -1
  866. package/examples/extensions/with-deps/package-lock.json +2 -2
  867. package/examples/extensions/with-deps/package.json +1 -1
  868. package/npm-shrinkwrap.json +12 -12
  869. package/package.json +10 -4
  870. package/dist/core/delegation/session-worker-result.d.ts +0 -16
  871. package/dist/core/delegation/session-worker-result.d.ts.map +0 -1
  872. package/dist/core/delegation/session-worker-result.js +0 -53
  873. package/dist/core/delegation/session-worker-result.js.map +0 -1
  874. package/dist/core/delegation/worker-result.d.ts +0 -15
  875. package/dist/core/delegation/worker-result.d.ts.map +0 -1
  876. package/dist/core/delegation/worker-result.js +0 -161
  877. package/dist/core/delegation/worker-result.js.map +0 -1
  878. package/dist/core/telemetry.d.ts.map +0 -1
  879. package/dist/core/telemetry.js.map +0 -1
  880. package/docs/integration-sweep-builder-blueprint-2026-07-09.md +0 -365
  881. package/docs/integration-sweep-resume-2026-07-09.md +0 -407
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../../src/core/process-matrix/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAEnE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE7F,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,OAAO,EACN,YAAY,EACZ,WAAW,EACX,SAAS,EACT,sBAAsB,EACtB,UAAU,EACV,qBAAqB,EACrB,yBAAyB,GACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EACN,aAAa,EACb,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,qBAAqB,EACrB,UAAU,EACV,aAAa,EACb,YAAY,EACZ,iCAAiC,EACjC,mBAAmB,EACnB,eAAe,GACf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,uBAAuB,EACvB,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,6BAA6B,EAC7B,iBAAiB,EACjB,qBAAqB,EACrB,eAAe,GACf,MAAM,wBAAwB,CAAC;AA2ChC,MAAM,WAAW,GAA+B,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAC,CAAC,EAAE,CAAC;AACnE,MAAM,CAAC,MAAM,q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I,EAAE,CAAC;gBACP,MAAM,CAAC,WAAW,EAAE,CAAC;gBACrB,OAAO;YACR,CAAC;QACF,CAAC;QACD,IAAI,GAAG,EAAE,IAAI,aAAa,EAAE,CAAC;YAC5B,IAAI,EAAE,CAAC;YACP,MAAM,CAAC,WAAW,EAAE,CAAC;QACtB,CAAC;IAAA,CACD,CAAC;IAEF,iBAAiB,EAAE,CAAC;IAEpB,OAAO,EAAE,IAAI,EAAE,CAAC;AAAA,CAChB","sourcesContent":["/**\n * Process-matrix runtime composition: the pieces main.ts wires together for durable,\n * restart-surviving master/worker process supervision.\n *\n * Contract:\n * - A WORKER is any process launched with a known parent (`PI_PARENT_PID` -- set by the\n * `--parent-pid` CLI flag, or directly by a launcher such as tmux dispatch). It self-registers\n * its OWN entry (the single writer of that entry during normal operation) and watches its\n * parent's liveness. On parent death it winds down GRACEFULLY -- never silently -- leaving a\n * `resumable` payload, then exits on its own after a bounded grace window (during which it may\n * instead be adopted by a new parent). \"No new turns\" after that point is automatic: a dead\n * parent injects no further follow-ups, so the worker simply runs out of work to do.\n * - A MASTER is everything else (no known parent). On startup it scans the matrix for orphaned\n * workers (workers whose recorded parent is dead). Workers owned by this exact resumed session\n * are restored automatically; foreign workers remain owner-gated interactively and report-only\n * headlessly.\n *\n * Sanctioned exceptions to \"a worker's entry is written only by that worker\": an exact resumed\n * parent may restore its recorded ownership, and an owner may explicitly approve adoption or\n * cooperative cleanup of a foreign orphan. The worker later confirms/applies the directive via\n * `pollWorkerDirective` and re-writes its own entry -- see `docs/process-matrix.md`. Outside these\n * identity/approval-fenced handshakes, a master NEVER writes another session's entry, and nothing\n * here ever kills a process directly.\n */\n\nimport { hostname as osHostname } from \"node:os\";\nimport { isAgentIdentity } from \"../orchestration/agent-resume.ts\";\nimport type { AgentIdentityContract } from \"../orchestration/contracts.ts\";\nimport { getParentPid, getParentSessionId, getProcessTaskRef } from \"../process-identity.ts\";\nimport type { ResolvedProcessMatrixSettings } from \"../settings-manager.ts\";\nimport { getBoundWorktreeLaneKey } from \"../worktree-sync/runtime.ts\";\nimport type { ProcessMatrixEntry, ResumablePayload } from \"./codes.ts\";\nimport {\n\tbuildEntryId,\n\tlistEntries,\n\treadEntry,\n\tremoveEntryIfUnchanged,\n\twriteEntry,\n\twriteEntryIfUnchanged,\n\twriteEntryIfUnchangedSync,\n} from \"./store.ts\";\nimport {\n\tapplyAdoption,\n\tapplyHeartbeat,\n\tbeginWindDown,\n\tbuildMasterEntry,\n\tbuildWorkerEntry,\n\tdetectOrphanedWorkers,\n\tmarkClosed,\n\tmarkResumable,\n\tmarkTerminal,\n\tmarkTerminalNotificationDelivered,\n\tpollWorkerDirective,\n\treconcileMatrix,\n} from \"./supervisor.ts\";\n\nexport {\n\tgetOrchestrationAgentId,\n\tgetParentPid,\n\tgetParentSessionId,\n\tgetProcessTaskRef,\n\tPI_ORCHESTRATION_AGENT_ID_ENV,\n\tPI_PARENT_PID_ENV,\n\tPI_PARENT_SESSION_ENV,\n\tPI_TASK_REF_ENV,\n} from \"../process-identity.ts\";\n\nexport interface ProcessMatrixRuntimeConfig {\n\tagentDir: string;\n\t/** Canonical logical identity for this process and any exact-session resume. */\n\tagent: AgentIdentityContract;\n\t/** Whether an interactive UI is available to ask the owner (see `promptConfirm`). */\n\thasUI: boolean;\n\tsettings: ResolvedProcessMatrixSettings;\n\tisProcessAlive: (pid: number) => boolean;\n\tnow?: () => number;\n\t/** Structural notice injection into the running session (host `sendCustomMessage` seam). */\n\tnotify: (text: string) => void | Promise<void>;\n\t/** Diagnostics sink (never throws into the session). */\n\tonDiagnostic?: (message: string) => void;\n\t/** The ask seam: resolves false on decline AND on any non-interactive/non-TTY caller. */\n\tpromptConfirm: (message: string) => Promise<boolean>;\n\t/** Cooperative self-exit -- called by a worker once wound down (grace expiry or a\n\t * master-granted cleanup directive). Never called for the master's own lifecycle. */\n\trequestExit: () => void;\n\t/** Stable goal/task identity. Automatic recovery requires an exact match. */\n\ttaskRef?: string;\n\ttaskSummary?: string;\n\t/** False for terminal/blocked owner state; recovery then remains explicit-owner gated. */\n\tallowAutomaticRecovery?: boolean;\n\t/** Starts a replacement OS process for a dead resumable worker. Completion is an event-driven\n\t * terminal signal; worker product remains in its persisted session/artifacts. */\n\tresumeWorker?: (payload: ResumablePayload) => Promise<ResumeWorkerLaunchOutcome>;\n}\n\nexport type ResumeWorkerLaunchOutcome =\n\t| {\n\t\t\tstarted: true;\n\t\t\t/** OS identity of this specific replacement process, used to fence its terminal handoff. */\n\t\t\tpid: number;\n\t\t\tcompletion: Promise<{ code: number | null; signal: NodeJS.Signals | null }>;\n\t }\n\t| { started: false; reason: string };\n\nexport interface ProcessMatrixRuntimeHandle {\n\tstop(): Promise<void> | void;\n}\n\nconst NOOP_HANDLE: ProcessMatrixRuntimeHandle = { stop: () => {} };\nexport const PROCESS_MATRIX_RESUMABLE_RETENTION_MS = 30 * 24 * 60 * 60_000;\n\nfunction describeError(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\nfunction nowIso(now: () => number): string {\n\treturn new Date(now()).toISOString();\n}\n\nfunction emitRuntimeNotice(config: ProcessMatrixRuntimeConfig, text: string): void {\n\ttry {\n\t\tvoid Promise.resolve(config.notify(text)).catch((error: unknown) => {\n\t\t\tconfig.onDiagnostic?.(`process-matrix: failed to notify session: ${describeError(error)}`);\n\t\t});\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: failed to notify session: ${describeError(error)}`);\n\t}\n}\n\n/**\n * Start the per-session process-matrix runtime. No-op when disabled (byte-identical to not\n * calling this at all). Never throws: a broken store must surface as a diagnostic, not a startup\n * crash.\n */\nexport async function startProcessMatrixRuntime(\n\tconfig: ProcessMatrixRuntimeConfig,\n): Promise<ProcessMatrixRuntimeHandle> {\n\tif (!config.settings.enabled) return NOOP_HANDLE;\n\tconst now = config.now ?? Date.now;\n\tconst parentPid = getParentPid();\n\n\ttry {\n\t\tif (!isAgentIdentity(config.agent)) throw new TypeError(\"Process-matrix agent identity is invalid.\");\n\t\tif (parentPid !== undefined) {\n\t\t\treturn await startWorkerBranch(config, parentPid, now);\n\t\t}\n\t\treturn await startMasterBranch(config, now);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: runtime failed to start: ${describeError(error)}`);\n\t\treturn NOOP_HANDLE;\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Master branch\n// ---------------------------------------------------------------------------\n\nasync function startMasterBranch(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tnow: () => number,\n): Promise<ProcessMatrixRuntimeHandle> {\n\tlet entry = buildMasterEntry({\n\t\tagent: config.agent,\n\t\tpid: process.pid,\n\t\thostname: osHostname(),\n\t\tnow: nowIso(now),\n\t});\n\ttry {\n\t\tawait writeEntry(config.agentDir, entry);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: failed to register master entry: ${describeError(error)}`);\n\t}\n\n\tlet stopped = false;\n\tlet ownsEntry = true;\n\tlet heartbeatTask: Promise<void> | undefined;\n\tconst lifetime = new AbortController();\n\tconst heartbeatTimer = setInterval(() => {\n\t\tif (stopped || !ownsEntry || heartbeatTask) return;\n\t\tconst expected = entry;\n\t\tconst next = applyHeartbeat(expected, nowIso(now));\n\t\theartbeatTask = writeEntryIfUnchanged(config.agentDir, expected.entryId, expected, next)\n\t\t\t.then((written) => {\n\t\t\t\tif (written) {\n\t\t\t\t\tentry = next;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\townsEntry = false;\n\t\t\t\tlifetime.abort();\n\t\t\t\tclearInterval(heartbeatTimer);\n\t\t\t\tprocess.off(\"exit\", closeOnExit);\n\t\t\t\tconfig.onDiagnostic?.(\"process-matrix: master entry ownership moved to a newer process generation\");\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tconfig.onDiagnostic?.(`process-matrix: failed to write master heartbeat: ${describeError(error)}`);\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\theartbeatTask = undefined;\n\t\t\t});\n\t}, config.settings.heartbeatMs);\n\theartbeatTimer.unref?.();\n\n\t// Best-effort close on process exit. A SIGKILLed master leaving \"running\" is fine -- reconcile's\n\t// own dead-pid detection covers it; this only makes the common clean-exit case tidy.\n\tconst closeOnExit = (): void => {\n\t\tif (!ownsEntry) return;\n\t\ttry {\n\t\t\tconst closed = markClosed(entry, nowIso(now));\n\t\t\tif (writeEntryIfUnchangedSync(config.agentDir, entry, closed)) entry = closed;\n\t\t} catch {\n\t\t\t// Best-effort only -- see module doc.\n\t\t}\n\t};\n\tprocess.once(\"exit\", closeOnExit);\n\n\tconst maintenance = reconcileAndRunOrphanScan(config, now, lifetime.signal).catch((error: unknown) => {\n\t\tconfig.onDiagnostic?.(`process-matrix: maintenance failed: ${describeError(error)}`);\n\t});\n\n\treturn {\n\t\tasync stop() {\n\t\t\tif (stopped) return;\n\t\t\tstopped = true;\n\t\t\tlifetime.abort();\n\t\t\tclearInterval(heartbeatTimer);\n\t\t\tprocess.off(\"exit\", closeOnExit);\n\t\t\tawait heartbeatTask;\n\t\t\tif (ownsEntry) {\n\t\t\t\tconst closed = markClosed(entry, nowIso(now));\n\t\t\t\tif (await writeEntryIfUnchanged(config.agentDir, entry.entryId, entry, closed)) entry = closed;\n\t\t\t}\n\t\t\tawait maintenance;\n\t\t},\n\t};\n}\n\nasync function reconcileAndRunOrphanScan(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tnow: () => number,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tlet entries: ProcessMatrixEntry[];\n\ttry {\n\t\tentries = await listEntries(config.agentDir);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: reconciliation failed to list entries: ${describeError(error)}`);\n\t\treturn;\n\t}\n\tif (signal.aborted) return;\n\tconst reconciled = reconcileMatrix(entries, {\n\t\tisPidAlive: (pid) => pid === process.pid || config.isProcessAlive(pid),\n\t\tnow: now(),\n\t\tresumableTtlMs: PROCESS_MATRIX_RESUMABLE_RETENTION_MS,\n\t});\n\tconst originalByEntryId = new Map(entries.map((entry) => [entry.entryId, entry]));\n\tconst recoveredEntryIdSet = new Set(reconciled.recoveredEntryIds);\n\tconst [prunedOutcomes, recoveredOutcomes] = await Promise.all([\n\t\tPromise.all(\n\t\t\treconciled.prunedEntryIds.map((entryId) => {\n\t\t\t\tconst original = originalByEntryId.get(entryId);\n\t\t\t\treturn original ? removeEntryIfUnchanged(config.agentDir, original) : false;\n\t\t\t}),\n\t\t),\n\t\tPromise.all(\n\t\t\treconciled.kept\n\t\t\t\t.filter((entry) => recoveredEntryIdSet.has(entry.entryId))\n\t\t\t\t.map((entry) => {\n\t\t\t\t\tconst original = originalByEntryId.get(entry.entryId);\n\t\t\t\t\treturn original ? writeEntryIfUnchanged(config.agentDir, entry.entryId, original, entry) : false;\n\t\t\t\t}),\n\t\t),\n\t]);\n\tif (signal.aborted) return;\n\tconst recoveredCount = recoveredOutcomes.filter(Boolean).length;\n\tconst prunedCount = prunedOutcomes.filter(Boolean).length;\n\tif (recoveredCount > 0) {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: recovered ${recoveredCount} interrupted Pi worker entr${recoveredCount === 1 ? \"y\" : \"ies\"}`,\n\t\t);\n\t}\n\tif (prunedCount > 0) {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: pruned ${prunedCount} terminal or expired entr${prunedCount === 1 ? \"y\" : \"ies\"}`,\n\t\t);\n\t}\n\tconst currentEntries = await listEntries(config.agentDir);\n\tawait deliverTerminalNotifications(config, currentEntries, now, signal);\n\tif (signal.aborted) return;\n\tawait runOrphanScan(config, currentEntries, now, signal);\n}\n\nasync function deliverTerminalNotifications(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tentries: ProcessMatrixEntry[],\n\tnow: () => number,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tfor (const entry of entries) {\n\t\tif (\n\t\t\tsignal.aborted ||\n\t\t\tentry.status !== \"closed\" ||\n\t\t\t!entry.terminal ||\n\t\t\tentry.terminal.notificationDeliveredAt ||\n\t\t\tentry.parentSessionId !== config.agent.resumeContext.sessionId\n\t\t)\n\t\t\tcontinue;\n\t\ttry {\n\t\t\tawait config.notify(formatTerminalNotification(entry));\n\t\t\tif (signal.aborted) return;\n\t\t} catch (error) {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: failed to deliver terminal handoff for ${entry.entryId}: ${describeError(error)}`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\t\ttry {\n\t\t\tconst delivered = markTerminalNotificationDelivered(entry, nowIso(now));\n\t\t\tif (!(await writeEntryIfUnchanged(config.agentDir, entry.entryId, entry, delivered))) {\n\t\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t\t`process-matrix: terminal handoff changed before acknowledgement for ${entry.entryId}`,\n\t\t\t\t);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: failed to acknowledge terminal handoff for ${entry.entryId}: ${describeError(error)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nfunction formatTerminalNotification(entry: ProcessMatrixEntry): string {\n\tconst terminal = entry.terminal;\n\treturn `process-matrix: resumed agent ${entry.agent.agentId} reached a terminal process state (code ${terminal?.code ?? \"none\"}, signal ${terminal?.signal ?? \"none\"}). Inspect its persisted result before continuing.`;\n}\n\nasync function runOrphanScan(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tentries: ProcessMatrixEntry[],\n\tnow: () => number,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tconst orphans = detectOrphanedWorkers(entries, {\n\t\tisPidAlive: config.isProcessAlive,\n\t\townSessionId: config.agent.resumeContext.sessionId,\n\t});\n\tif (orphans.length === 0) return;\n\n\tfor (const orphan of orphans) {\n\t\tif (signal.aborted) return;\n\t\tconst recoveryBoundary = getAutomaticRecoveryBoundary(config, orphan);\n\t\tconst exactResumedParent = recoveryBoundary === undefined;\n\t\tif (!config.isProcessAlive(orphan.pid)) {\n\t\t\tif (!exactResumedParent) {\n\t\t\t\tif (!config.hasUI) {\n\t\t\t\t\treportOwnerGatedOrphan(config, orphan, recoveryBoundary);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst payload = orphan.resumable;\n\t\t\t\tif (!payload) {\n\t\t\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t\t\t`process-matrix: dead worker ${orphan.entryId} is not resumable because its launch context is unavailable`,\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst resume = await config.promptConfirm(\n\t\t\t\t\t`resume agent ${payload.agent.agentId} from its persisted session?`,\n\t\t\t\t);\n\t\t\t\tif (!resume || signal.aborted) continue;\n\t\t\t}\n\t\t\tawait resumeDeadOrphan(config, orphan, signal);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (exactResumedParent) {\n\t\t\tawait adoptLiveOrphan(config, orphan, signal);\n\t\t\tcontinue;\n\t\t}\n\t\tif (!config.hasUI) {\n\t\t\treportOwnerGatedOrphan(config, orphan, recoveryBoundary);\n\t\t\tcontinue;\n\t\t}\n\t\tconst adopt = await config.promptConfirm(\n\t\t\t`adopt worker ${orphan.entryId} (lane ${orphan.agent.resumeContext.worktreeLaneKey ?? \"none\"})?`,\n\t\t);\n\t\tif (signal.aborted) return;\n\t\tif (adopt) {\n\t\t\tawait adoptLiveOrphan(config, orphan, signal);\n\t\t\tcontinue;\n\t\t}\n\t\tconst cleanup = await config.promptConfirm(`clean up worker ${orphan.entryId} gracefully?`);\n\t\tif (!cleanup || signal.aborted) continue;\n\t\tconst windingDown = beginWindDown(orphan, \"user_cleanup\", nowIso(now));\n\t\ttry {\n\t\t\tif (!(await writeEntryIfUnchanged(config.agentDir, orphan.entryId, orphan, windingDown))) {\n\t\t\t\tconfig.onDiagnostic?.(`process-matrix: cleanup skipped because ${orphan.entryId} changed`);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: failed to write cleanup for ${orphan.entryId}: ${describeError(error)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nfunction getAutomaticRecoveryBoundary(\n\tconfig: ProcessMatrixRuntimeConfig,\n\torphan: ProcessMatrixEntry,\n): string | undefined {\n\tif (orphan.parentSessionId !== config.agent.resumeContext.sessionId) return \"foreign parent session\";\n\tconst entryTaskRef = orphan.taskRef;\n\tconst payloadTaskRef = orphan.resumable?.taskRef;\n\tif (entryTaskRef !== undefined && payloadTaskRef !== undefined && entryTaskRef !== payloadTaskRef) {\n\t\treturn \"inconsistent persisted task identity\";\n\t}\n\tif ((payloadTaskRef ?? entryTaskRef) !== config.taskRef) return \"task identity does not match the current goal\";\n\tif (config.allowAutomaticRecovery === false) return \"current goal state does not permit automatic recovery\";\n\treturn undefined;\n}\n\nfunction reportOwnerGatedOrphan(config: ProcessMatrixRuntimeConfig, orphan: ProcessMatrixEntry, reason: string): void {\n\tconfig.onDiagnostic?.(\n\t\t`process-matrix: found owner-gated orphan ${orphan.entryId} (${reason}; report-only, non-interactive; nothing written, nothing killed)`,\n\t);\n}\n\nasync function adoptLiveOrphan(\n\tconfig: ProcessMatrixRuntimeConfig,\n\torphan: ProcessMatrixEntry,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tif (signal.aborted) return;\n\tconst adopted = applyAdoption(orphan, {\n\t\tparentPid: process.pid,\n\t\tparentSessionId: config.agent.resumeContext.sessionId,\n\t});\n\ttry {\n\t\tif (!(await writeEntryIfUnchanged(config.agentDir, orphan.entryId, orphan, adopted))) return;\n\t\tif (signal.aborted) await writeEntryIfUnchanged(config.agentDir, orphan.entryId, adopted, orphan);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: failed to write adoption for ${orphan.entryId}: ${describeError(error)}`);\n\t}\n}\n\nasync function resumeDeadOrphan(\n\tconfig: ProcessMatrixRuntimeConfig,\n\torphan: ProcessMatrixEntry,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tif (signal.aborted) return;\n\tconst payload = orphan.resumable;\n\tif (!payload || !config.resumeWorker) {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: dead worker ${orphan.entryId} is not resumable because its launch context or resume launcher is unavailable`,\n\t\t);\n\t\treturn;\n\t}\n\tconst claimed = applyAdoption(orphan, {\n\t\tparentPid: process.pid,\n\t\tparentSessionId: config.agent.resumeContext.sessionId,\n\t});\n\tlet launched: Extract<ResumeWorkerLaunchOutcome, { started: true }>;\n\ttry {\n\t\tif (!(await writeEntryIfUnchanged(config.agentDir, orphan.entryId, orphan, claimed))) return;\n\t\tif (signal.aborted) {\n\t\t\tawait writeEntryIfUnchanged(config.agentDir, orphan.entryId, claimed, orphan);\n\t\t\treturn;\n\t\t}\n\t\tconst launchOutcome = await config.resumeWorker(payload);\n\t\tif (!launchOutcome.started) {\n\t\t\tawait writeEntryIfUnchanged(config.agentDir, orphan.entryId, claimed, orphan);\n\t\t\tconfig.onDiagnostic?.(`process-matrix: failed to resume ${orphan.entryId}: ${launchOutcome.reason}`);\n\t\t\treturn;\n\t\t}\n\t\tlaunched = launchOutcome;\n\t} catch (error) {\n\t\ttry {\n\t\t\tawait writeEntryIfUnchanged(config.agentDir, orphan.entryId, claimed, orphan);\n\t\t} catch {\n\t\t\t// The original resumable record remains the intended recovery state.\n\t\t}\n\t\tconfig.onDiagnostic?.(`process-matrix: failed to resume ${orphan.entryId}: ${describeError(error)}`);\n\t\treturn;\n\t}\n\tconst launchedEntry = { ...claimed, pid: launched.pid };\n\tif (!signal.aborted) {\n\t\ttry {\n\t\t\t// Bridge the interval before the replacement can self-register. Without its real PID here, a\n\t\t\t// restarted master can mistake this live replacement for the original dead process and spawn\n\t\t\t// a duplicate.\n\t\t\tawait writeEntryIfUnchanged(config.agentDir, claimed.entryId, claimed, launchedEntry);\n\t\t} catch (error) {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: failed to record resumed worker pid for ${orphan.entryId}: ${describeError(error)}`,\n\t\t\t);\n\t\t}\n\t} else {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: resumed worker ${orphan.entryId} launched after owner shutdown; preserving its terminal handoff only`,\n\t\t);\n\t}\n\tvoid launched.completion.then(\n\t\t(result) => persistResumedWorkerTerminal(config, launchedEntry, claimed, result, signal),\n\t\t(error: unknown) => {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: resumed agent ${payload.agent.agentId} terminal signal failed: ${describeError(error)}`,\n\t\t\t);\n\t\t},\n\t);\n}\n\nasync function persistResumedWorkerTerminal(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tclaimed: ProcessMatrixEntry,\n\tpreLaunchClaim: ProcessMatrixEntry,\n\tresult: { code: number | null; signal: NodeJS.Signals | null },\n\tlifetime: AbortSignal,\n): Promise<void> {\n\ttry {\n\t\tconst observedAt = new Date().toISOString();\n\t\tconst terminal = markTerminal(claimed, result, observedAt);\n\t\tif (await writeEntryIfUnchanged(config.agentDir, claimed.entryId, claimed, terminal)) {\n\t\t\tif (lifetime.aborted) return;\n\t\t\tawait notifyResumedWorkerTerminal(config, terminal, lifetime);\n\t\t\treturn;\n\t\t}\n\t\tconst selfRegistered = await readEntry(config.agentDir, claimed.entryId);\n\t\tif (selfRegistered?.pid === claimed.pid) {\n\t\t\tconst registeredTerminal = markTerminal(selfRegistered, result, observedAt);\n\t\t\tif (await writeEntryIfUnchanged(config.agentDir, claimed.entryId, selfRegistered, registeredTerminal)) {\n\t\t\t\tif (lifetime.aborted) return;\n\t\t\t\tawait notifyResumedWorkerTerminal(config, registeredTerminal, lifetime);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t// If recording the spawned PID failed (or shutdown won the race immediately after spawn), the\n\t\t// untouched pre-launch claim proves no newer process has taken this logical entry. Upgrade it\n\t\t// to the actual exited process before persisting its terminal handoff.\n\t\tconst fallbackTerminal = markTerminal({ ...preLaunchClaim, pid: claimed.pid }, result, observedAt);\n\t\tif (await writeEntryIfUnchanged(config.agentDir, claimed.entryId, preLaunchClaim, fallbackTerminal)) {\n\t\t\tif (lifetime.aborted) return;\n\t\t\tawait notifyResumedWorkerTerminal(config, fallbackTerminal, lifetime);\n\t\t\treturn;\n\t\t}\n\t\tif (await writeEntryIfUnchanged(config.agentDir, claimed.entryId, undefined, terminal)) {\n\t\t\tif (lifetime.aborted) return;\n\t\t\tawait notifyResumedWorkerTerminal(config, terminal, lifetime);\n\t\t\treturn;\n\t\t}\n\t\tconfig.onDiagnostic?.(`process-matrix: ignored stale terminal handoff for ${claimed.entryId}`);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: failed to persist terminal handoff for ${claimed.entryId}: ${describeError(error)}`,\n\t\t);\n\t}\n}\n\nasync function notifyResumedWorkerTerminal(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tterminal: ProcessMatrixEntry,\n\tlifetime: AbortSignal,\n): Promise<void> {\n\tif (lifetime.aborted) return;\n\ttry {\n\t\tawait config.notify(formatTerminalNotification(terminal));\n\t\tif (lifetime.aborted) return;\n\t\tconst delivered = markTerminalNotificationDelivered(terminal, new Date().toISOString());\n\t\tif (!(await writeEntryIfUnchanged(config.agentDir, terminal.entryId, terminal, delivered))) {\n\t\t\tconfig.onDiagnostic?.(\n\t\t\t\t`process-matrix: terminal handoff changed before acknowledgement for ${terminal.entryId}`,\n\t\t\t);\n\t\t}\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(\n\t\t\t`process-matrix: failed to deliver terminal handoff for ${terminal.entryId}: ${describeError(error)}`,\n\t\t);\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Worker branch\n// ---------------------------------------------------------------------------\n\nasync function startWorkerBranch(\n\tconfig: ProcessMatrixRuntimeConfig,\n\tinitialParentPid: number,\n\tnow: () => number,\n): Promise<ProcessMatrixRuntimeHandle> {\n\tconst parentSessionId = getParentSessionId();\n\tconst taskRef = (getProcessTaskRef() ?? config.taskRef)?.trim().slice(0, 512) || undefined;\n\tconst taskSummary = config.taskSummary?.trim().slice(0, 2_000) || undefined;\n\tconst laneKey = getBoundWorktreeLaneKey();\n\tconst contextLaneKey = config.agent.resumeContext.worktreeLaneKey;\n\tif (laneKey !== contextLaneKey) {\n\t\tthrow new TypeError(\n\t\t\t`Process-matrix agent lane '${contextLaneKey ?? \"none\"}' does not match active lane '${laneKey ?? \"none\"}'.`,\n\t\t);\n\t}\n\n\tlet entry = buildWorkerEntry({\n\t\tagent: config.agent,\n\t\tpid: process.pid,\n\t\thostname: osHostname(),\n\t\tnow: nowIso(now),\n\t\tparentPid: initialParentPid,\n\t\t...(parentSessionId !== undefined ? { parentSessionId } : {}),\n\t\t...(taskRef !== undefined ? { taskRef } : {}),\n\t\t...(taskSummary !== undefined ? { taskSummary } : {}),\n\t});\n\ttry {\n\t\tawait writeEntry(config.agentDir, entry);\n\t} catch (error) {\n\t\tconfig.onDiagnostic?.(`process-matrix: failed to register worker entry: ${describeError(error)}`);\n\t}\n\n\tlet currentParentPid = initialParentPid;\n\tlet currentParentSessionId = parentSessionId;\n\tlet stopped = false;\n\tlet timer: NodeJS.Timeout | undefined;\n\tlet ticking = false;\n\tlet preserveResumableOnExit = false;\n\tconst closeOnExit = (code: number | null = null): void => {\n\t\tif (preserveResumableOnExit) return;\n\t\ttry {\n\t\t\tconst terminal = markTerminal(entry, { code, signal: null }, nowIso(now));\n\t\t\tif (writeEntryIfUnchangedSync(config.agentDir, entry, terminal)) entry = terminal;\n\t\t} catch {\n\t\t\t// Best-effort. Dead-pid reconciliation remains authoritative.\n\t\t}\n\t};\n\tprocess.once(\"exit\", closeOnExit);\n\n\tconst stop = (): void => {\n\t\tif (stopped) return;\n\t\tstopped = true;\n\t\tif (timer) clearInterval(timer);\n\t\ttimer = undefined;\n\t\tif (!preserveResumableOnExit) closeOnExit();\n\t\tprocess.off(\"exit\", closeOnExit);\n\t};\n\n\tconst persist = async (\n\t\texpected: ProcessMatrixEntry,\n\t\tnext: ProcessMatrixEntry,\n\t\tfailureContext: string,\n\t): Promise<boolean> => {\n\t\ttry {\n\t\t\tif (await writeEntryIfUnchanged(config.agentDir, expected.entryId, expected, next)) {\n\t\t\t\tentry = next;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tconst current = await readEntry(config.agentDir, expected.entryId);\n\t\t\tif (current?.pid === process.pid) entry = current;\n\t\t\telse {\n\t\t\t\tstopped = true;\n\t\t\t\tif (timer) clearInterval(timer);\n\t\t\t\ttimer = undefined;\n\t\t\t\tprocess.off(\"exit\", closeOnExit);\n\t\t\t\tconfig.onDiagnostic?.(\"process-matrix: worker entry ownership moved to a newer process generation\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconfig.onDiagnostic?.(`process-matrix: ${failureContext}: ${describeError(error)}`);\n\t\t}\n\t\treturn false;\n\t};\n\n\tconst startHealthyWatch = (): void => {\n\t\ttimer = setInterval(() => {\n\t\t\tif (ticking) return;\n\t\t\tticking = true;\n\t\t\tvoid healthyTick().finally(() => {\n\t\t\t\tticking = false;\n\t\t\t});\n\t\t}, config.settings.watcherPollMs);\n\t\ttimer.unref?.();\n\t};\n\n\tconst declaredParentIsAlive = async (): Promise<boolean> => {\n\t\t// PID liveness alone is not process identity: a reused PID could otherwise keep a worker\n\t\t// attached to an unrelated process forever. The parent session's own fresh master entry binds\n\t\t// PID to a durable identity and proves that that exact session is still heartbeating.\n\t\tif (!currentParentSessionId || !config.isProcessAlive(currentParentPid)) return false;\n\t\tconst parent = await readEntry(config.agentDir, buildEntryId(\"master\", currentParentSessionId));\n\t\tif (!parent || parent.role !== \"master\" || parent.agent.resumeContext.sessionId !== currentParentSessionId)\n\t\t\treturn false;\n\t\tif (parent.pid !== currentParentPid || parent.status !== \"running\") return false;\n\t\tconst heartbeatAt = Date.parse(parent.heartbeatAt);\n\t\tconst maxAge = config.settings.heartbeatMs * 2 + config.settings.watcherPollMs;\n\t\treturn Number.isFinite(heartbeatAt) && now() - heartbeatAt <= maxAge;\n\t};\n\n\tconst healthyTick = async (): Promise<void> => {\n\t\tif (stopped) return;\n\t\tif (!(await declaredParentIsAlive())) {\n\t\t\tawait enterWindDown();\n\t\t\treturn;\n\t\t}\n\t\t// Still healthy: also poll for a master-initiated cooperative-cleanup directive.\n\t\tconst fresh = await readEntry(config.agentDir, entry.entryId);\n\t\tif (!fresh) return;\n\t\tconst directive = pollWorkerDirective(fresh, currentParentPid, { isPidAlive: config.isProcessAlive });\n\t\tif (directive.code !== \"user_cleanup\") return;\n\t\tif (\n\t\t\t!(await persist(\n\t\t\t\tfresh,\n\t\t\t\tbeginWindDown(fresh, \"user_cleanup\", nowIso(now)),\n\t\t\t\t\"failed to write a master-requested worker wind-down\",\n\t\t\t))\n\t\t)\n\t\t\treturn;\n\t\temitRuntimeNotice(config, \"process-matrix: the parent session requested a cooperative cleanup. Winding down.\");\n\t\tstop();\n\t\tconfig.requestExit();\n\t};\n\n\tconst enterWindDown = async (): Promise<void> => {\n\t\tconst windDownAt = nowIso(now);\n\t\tconst expected = entry;\n\t\tconst resumable: ResumablePayload = { lastCode: \"resumable\", agent: structuredClone(config.agent) };\n\t\tif (expected.taskRef !== undefined) resumable.taskRef = expected.taskRef;\n\t\tif (expected.taskSummary !== undefined) resumable.taskSummary = expected.taskSummary;\n\t\tconst woundDown = markResumable(beginWindDown(expected, \"parent_lost\", windDownAt), resumable, windDownAt);\n\t\tif (!(await persist(expected, woundDown, \"failed to write worker wind-down\"))) return;\n\t\tif (timer) clearInterval(timer);\n\t\ttimer = undefined;\n\t\tpreserveResumableOnExit = true;\n\t\temitRuntimeNotice(\n\t\t\tconfig,\n\t\t\t`process-matrix: parent process (pid ${currentParentPid}) is gone. Winding down gracefully; this task is resumable.`,\n\t\t);\n\t\tstartGraceWatch();\n\t};\n\n\tconst startGraceWatch = (): void => {\n\t\tconst graceDeadline = now() + config.settings.adoptionGraceMs;\n\t\ttimer = setInterval(() => {\n\t\t\tif (ticking) return;\n\t\t\tticking = true;\n\t\t\tvoid graceTick(graceDeadline).finally(() => {\n\t\t\t\tticking = false;\n\t\t\t});\n\t\t}, config.settings.watcherPollMs);\n\t\ttimer.unref?.();\n\t};\n\n\tconst graceTick = async (graceDeadline: number): Promise<void> => {\n\t\tif (stopped) return;\n\t\tconst fresh = await readEntry(config.agentDir, entry.entryId);\n\t\tif (fresh) {\n\t\t\tconst directive = pollWorkerDirective(fresh, currentParentPid, { isPidAlive: config.isProcessAlive });\n\t\t\tif (directive.code === \"adopt\" && fresh.parentSessionId) {\n\t\t\t\t// The adopting master persists its session id with the pid. Require both on the next\n\t\t\t\t// healthy tick; accepting a pid-only adoption would reintroduce the PID-reuse bug.\n\t\t\t\tif (\n\t\t\t\t\t!(await persist(\n\t\t\t\t\t\tfresh,\n\t\t\t\t\t\tapplyAdoption(fresh, { parentPid: directive.parentPid, parentSessionId: fresh.parentSessionId }),\n\t\t\t\t\t\t\"failed to write worker adoption\",\n\t\t\t\t\t))\n\t\t\t\t)\n\t\t\t\t\treturn;\n\t\t\t\temitRuntimeNotice(\n\t\t\t\t\tconfig,\n\t\t\t\t\t`process-matrix: adopted by a new parent (pid ${directive.parentPid}). Resuming.`,\n\t\t\t\t);\n\t\t\t\tcurrentParentPid = directive.parentPid;\n\t\t\t\tcurrentParentSessionId = fresh.parentSessionId;\n\t\t\t\tpreserveResumableOnExit = false;\n\t\t\t\tif (timer) {\n\t\t\t\t\tclearInterval(timer);\n\t\t\t\t\ttimer = undefined;\n\t\t\t\t}\n\t\t\t\tstartHealthyWatch();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (directive.code === \"user_cleanup\") {\n\t\t\t\tstop();\n\t\t\t\tconfig.requestExit();\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tif (now() >= graceDeadline) {\n\t\t\tstop();\n\t\t\tconfig.requestExit();\n\t\t}\n\t};\n\n\tstartHealthyWatch();\n\n\treturn { stop };\n}\n"]}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Process-matrix on-disk store: one file per process (`state/process-matrix/<entryId>.json`),
3
+ * durable across restarts and crashes (see `agent-paths.ts` -- this is machine state, not
4
+ * transient `work/`).
5
+ *
6
+ * Each entry is owned by the process it describes: a master writes and heartbeats its OWN entry,
7
+ * a worker self-registers and heartbeats its OWN entry (see `runtime.ts`'s module doc for the one
8
+ * sanctioned, ask-gated exception during orphan adoption/cleanup). A missing or corrupt entry file
9
+ * reads as absent -- never an error -- matching `worktree-sync/store.ts`'s store doctrine.
10
+ */
11
+ import type { ProcessMatrixEntry, ProcessRole } from "./codes.ts";
12
+ export declare function processMatrixDir(agentDir: string): string;
13
+ export declare function entryPath(agentDir: string, entryId: string): string;
14
+ /** Stable entry identity: one entry per (role, sessionId) pair. */
15
+ export declare function buildEntryId(role: ProcessRole, sessionId: string): string;
16
+ export declare function readEntry(agentDir: string, entryId: string): Promise<ProcessMatrixEntry | undefined>;
17
+ export declare function listEntries(agentDir: string): Promise<ProcessMatrixEntry[]>;
18
+ export declare function writeEntry(agentDir: string, entry: ProcessMatrixEntry): Promise<void>;
19
+ /** Atomically replace one entry only when its full persisted value still equals `expected`. */
20
+ export declare function writeEntryIfUnchanged(agentDir: string, entryId: string, expected: ProcessMatrixEntry | undefined, next: ProcessMatrixEntry): Promise<boolean>;
21
+ /** Sync conditional replacement for best-effort `process.on("exit")` terminal writes. */
22
+ export declare function writeEntryIfUnchangedSync(agentDir: string, expected: ProcessMatrixEntry, next: ProcessMatrixEntry): boolean;
23
+ export declare function removeEntry(agentDir: string, entryId: string): Promise<void>;
24
+ export declare function removeEntryIfUnchanged(agentDir: string, expected: ProcessMatrixEntry): Promise<boolean>;
25
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/core/process-matrix/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAiB,MAAM,YAAY,CAAC;AAoBjF,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,mEAAmE;AACnE,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAEzE;AA4ED,wBAAsB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC,CAG1G;AAED,wBAAsB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAcjF;AAUD,wBAAsB,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAI3F;AAED,+FAA+F;AAC/F,wBAAsB,qBAAqB,CAC1C,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,kBAAkB,GAAG,SAAS,EACxC,IAAI,EAAE,kBAAkB,GACtB,OAAO,CAAC,OAAO,CAAC,CAWlB;AAED,yFAAyF;AACzF,wBAAgB,yBAAyB,CACxC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,kBAAkB,EAC5B,IAAI,EAAE,kBAAkB,GACtB,OAAO,CAeT;AAED,wBAAsB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQlF;AAED,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC,CAQ7G","sourcesContent":["/**\n * Process-matrix on-disk store: one file per process (`state/process-matrix/<entryId>.json`),\n * durable across restarts and crashes (see `agent-paths.ts` -- this is machine state, not\n * transient `work/`).\n *\n * Each entry is owned by the process it describes: a master writes and heartbeats its OWN entry,\n * a worker self-registers and heartbeats its OWN entry (see `runtime.ts`'s module doc for the one\n * sanctioned, ask-gated exception during orphan adoption/cleanup). A missing or corrupt entry file\n * reads as absent -- never an error -- matching `worktree-sync/store.ts`'s store doctrine.\n */\n\nimport { promises as fsPromises, readFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { isDeepStrictEqual } from \"node:util\";\nimport { stateFile } from \"../agent-paths.ts\";\nimport { isAgentIdentity } from \"../orchestration/agent-resume.ts\";\nimport { withFileLock, withFileLockSync, writeFileAtomic, writeFileAtomicSync } from \"../util/atomic-file.ts\";\nimport { isPlainRecord } from \"../util/value-guards.ts\";\nimport type { ProcessMatrixEntry, ProcessRole, ProcessStatus } from \"./codes.ts\";\n\nfunction isProcessStatus(value: unknown): value is ProcessStatus {\n\treturn (\n\t\tvalue === \"running\" ||\n\t\tvalue === \"winding_down\" ||\n\t\tvalue === \"resumable\" ||\n\t\tvalue === \"adopted\" ||\n\t\tvalue === \"closed\"\n\t);\n}\n\nfunction isOptionalString(value: unknown, maxLength: number): boolean {\n\treturn value === undefined || (typeof value === \"string\" && value.length <= maxLength);\n}\n\nfunction isTimestamp(value: unknown): value is string {\n\treturn typeof value === \"string\" && Number.isFinite(Date.parse(value));\n}\n\nexport function processMatrixDir(agentDir: string): string {\n\treturn stateFile(agentDir, \"process-matrix\");\n}\n\nexport function entryPath(agentDir: string, entryId: string): string {\n\treturn join(processMatrixDir(agentDir), `${entryId}.json`);\n}\n\n/** Stable entry identity: one entry per (role, sessionId) pair. */\nexport function buildEntryId(role: ProcessRole, sessionId: string): string {\n\treturn `${role}-${sessionId}`;\n}\n\nasync function readJsonFile(filePath: string): Promise<unknown> {\n\tlet raw: string;\n\ttry {\n\t\traw = await fsPromises.readFile(filePath, \"utf-8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\treturn parseJson(raw);\n}\n\nfunction readJsonFileSync(filePath: string): unknown {\n\tlet raw: string;\n\ttry {\n\t\traw = readFileSync(filePath, \"utf-8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\treturn parseJson(raw);\n}\n\nfunction parseJson(raw: string): unknown {\n\ttry {\n\t\treturn JSON.parse(raw) as unknown;\n\t} catch {\n\t\t// Corrupt entry files are treated as absent -- reconcile/re-registration rebuilds them.\n\t\treturn undefined;\n\t}\n}\n\nfunction isProcessMatrixEntry(value: unknown): value is ProcessMatrixEntry {\n\tif (!isPlainRecord(value) || !isAgentIdentity(value.agent) || (value.role !== \"master\" && value.role !== \"worker\"))\n\t\treturn false;\n\tconst sessionId = value.agent.resumeContext.sessionId;\n\tif (\n\t\tvalue.resumable !== undefined &&\n\t\t(!isPlainRecord(value.resumable) ||\n\t\t\t!isAgentIdentity(value.resumable.agent) ||\n\t\t\t!isDeepStrictEqual(value.resumable.agent, value.agent) ||\n\t\t\t!isProcessStatus(value.resumable.lastCode) ||\n\t\t\t!isOptionalString(value.resumable.taskRef, 512) ||\n\t\t\t!isOptionalString(value.resumable.taskSummary, 2_000))\n\t)\n\t\treturn false;\n\tif (\n\t\tvalue.terminal !== undefined &&\n\t\t(!isPlainRecord(value.terminal) ||\n\t\t\t!(value.terminal.code === null || Number.isSafeInteger(value.terminal.code)) ||\n\t\t\t!(\n\t\t\t\tvalue.terminal.signal === null ||\n\t\t\t\t(typeof value.terminal.signal === \"string\" && value.terminal.signal.length <= 100)\n\t\t\t) ||\n\t\t\t!isTimestamp(value.terminal.observedAt) ||\n\t\t\t!(value.terminal.notificationDeliveredAt === undefined || isTimestamp(value.terminal.notificationDeliveredAt)))\n\t)\n\t\treturn false;\n\tif (value.terminal !== undefined && value.status !== \"closed\") return false;\n\tif (value.status === \"resumable\" && value.resumable === undefined) return false;\n\treturn (\n\t\tvalue.entryId === buildEntryId(value.role, sessionId) &&\n\t\tNumber.isSafeInteger(value.pid) &&\n\t\ttypeof value.hostname === \"string\" &&\n\t\tvalue.hostname.length <= 255 &&\n\t\tisTimestamp(value.startedAt) &&\n\t\tisTimestamp(value.heartbeatAt) &&\n\t\tisProcessStatus(value.status) &&\n\t\t(value.parentPid === undefined || (Number.isSafeInteger(value.parentPid) && Number(value.parentPid) > 0)) &&\n\t\tisOptionalString(value.parentSessionId, 512) &&\n\t\tisOptionalString(value.tmuxSession, 512) &&\n\t\t(value.tmuxPanePid === undefined || Number.isSafeInteger(value.tmuxPanePid)) &&\n\t\tisOptionalString(value.taskRef, 512) &&\n\t\tisOptionalString(value.taskSummary, 2_000)\n\t);\n}\n\nexport async function readEntry(agentDir: string, entryId: string): Promise<ProcessMatrixEntry | undefined> {\n\tconst value = await readJsonFile(entryPath(agentDir, entryId));\n\treturn isProcessMatrixEntry(value) ? value : undefined;\n}\n\nexport async function listEntries(agentDir: string): Promise<ProcessMatrixEntry[]> {\n\tlet names: string[];\n\ttry {\n\t\tnames = await fsPromises.readdir(processMatrixDir(agentDir));\n\t} catch {\n\t\treturn [];\n\t}\n\tconst entries: ProcessMatrixEntry[] = [];\n\tfor (const name of names.sort()) {\n\t\tif (!name.endsWith(\".json\")) continue;\n\t\tconst entry = await readJsonFile(join(processMatrixDir(agentDir), name));\n\t\tif (isProcessMatrixEntry(entry)) entries.push(entry);\n\t}\n\treturn entries;\n}\n\nfunction serialize(entry: ProcessMatrixEntry): string {\n\treturn `${JSON.stringify(entry, null, \"\\t\")}\\n`;\n}\n\nasync function withEntryLock<T>(agentDir: string, entryId: string, operation: () => Promise<T>): Promise<T> {\n\treturn withFileLock(entryPath(agentDir, entryId), operation);\n}\n\nexport async function writeEntry(agentDir: string, entry: ProcessMatrixEntry): Promise<void> {\n\tawait withEntryLock(agentDir, entry.entryId, async () => {\n\t\tawait writeFileAtomic(entryPath(agentDir, entry.entryId), serialize(entry));\n\t});\n}\n\n/** Atomically replace one entry only when its full persisted value still equals `expected`. */\nexport async function writeEntryIfUnchanged(\n\tagentDir: string,\n\tentryId: string,\n\texpected: ProcessMatrixEntry | undefined,\n\tnext: ProcessMatrixEntry,\n): Promise<boolean> {\n\tif (next.entryId !== entryId) {\n\t\tthrow new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${entryId}'.`);\n\t}\n\treturn withEntryLock(agentDir, entryId, async () => {\n\t\tconst value = await readJsonFile(entryPath(agentDir, entryId));\n\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\tawait writeFileAtomic(entryPath(agentDir, entryId), serialize(next));\n\t\treturn true;\n\t});\n}\n\n/** Sync conditional replacement for best-effort `process.on(\"exit\")` terminal writes. */\nexport function writeEntryIfUnchangedSync(\n\tagentDir: string,\n\texpected: ProcessMatrixEntry,\n\tnext: ProcessMatrixEntry,\n): boolean {\n\tif (next.entryId !== expected.entryId) {\n\t\tthrow new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${expected.entryId}'.`);\n\t}\n\treturn withFileLockSync(\n\t\tentryPath(agentDir, expected.entryId),\n\t\t() => {\n\t\t\tconst value = readJsonFileSync(entryPath(agentDir, expected.entryId));\n\t\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\t\twriteFileAtomicSync(entryPath(agentDir, expected.entryId), serialize(next));\n\t\t\treturn true;\n\t\t},\n\t\t{ retries: 0 },\n\t);\n}\n\nexport async function removeEntry(agentDir: string, entryId: string): Promise<void> {\n\tawait withEntryLock(agentDir, entryId, async () => {\n\t\ttry {\n\t\t\tawait fsPromises.rm(entryPath(agentDir, entryId), { force: true });\n\t\t} catch {\n\t\t\t// Best-effort; a missing file is already the desired end state.\n\t\t}\n\t});\n}\n\nexport async function removeEntryIfUnchanged(agentDir: string, expected: ProcessMatrixEntry): Promise<boolean> {\n\treturn withEntryLock(agentDir, expected.entryId, async () => {\n\t\tconst value = await readJsonFile(entryPath(agentDir, expected.entryId));\n\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\tawait fsPromises.rm(entryPath(agentDir, expected.entryId), { force: true });\n\t\treturn true;\n\t});\n}\n"]}
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Process-matrix on-disk store: one file per process (`state/process-matrix/<entryId>.json`),
3
+ * durable across restarts and crashes (see `agent-paths.ts` -- this is machine state, not
4
+ * transient `work/`).
5
+ *
6
+ * Each entry is owned by the process it describes: a master writes and heartbeats its OWN entry,
7
+ * a worker self-registers and heartbeats its OWN entry (see `runtime.ts`'s module doc for the one
8
+ * sanctioned, ask-gated exception during orphan adoption/cleanup). A missing or corrupt entry file
9
+ * reads as absent -- never an error -- matching `worktree-sync/store.ts`'s store doctrine.
10
+ */
11
+ import { promises as fsPromises, readFileSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { isDeepStrictEqual } from "node:util";
14
+ import { stateFile } from "../agent-paths.js";
15
+ import { isAgentIdentity } from "../orchestration/agent-resume.js";
16
+ import { withFileLock, withFileLockSync, writeFileAtomic, writeFileAtomicSync } from "../util/atomic-file.js";
17
+ import { isPlainRecord } from "../util/value-guards.js";
18
+ function isProcessStatus(value) {
19
+ return (value === "running" ||
20
+ value === "winding_down" ||
21
+ value === "resumable" ||
22
+ value === "adopted" ||
23
+ value === "closed");
24
+ }
25
+ function isOptionalString(value, maxLength) {
26
+ return value === undefined || (typeof value === "string" && value.length <= maxLength);
27
+ }
28
+ function isTimestamp(value) {
29
+ return typeof value === "string" && Number.isFinite(Date.parse(value));
30
+ }
31
+ export function processMatrixDir(agentDir) {
32
+ return stateFile(agentDir, "process-matrix");
33
+ }
34
+ export function entryPath(agentDir, entryId) {
35
+ return join(processMatrixDir(agentDir), `${entryId}.json`);
36
+ }
37
+ /** Stable entry identity: one entry per (role, sessionId) pair. */
38
+ export function buildEntryId(role, sessionId) {
39
+ return `${role}-${sessionId}`;
40
+ }
41
+ async function readJsonFile(filePath) {
42
+ let raw;
43
+ try {
44
+ raw = await fsPromises.readFile(filePath, "utf-8");
45
+ }
46
+ catch {
47
+ return undefined;
48
+ }
49
+ return parseJson(raw);
50
+ }
51
+ function readJsonFileSync(filePath) {
52
+ let raw;
53
+ try {
54
+ raw = readFileSync(filePath, "utf-8");
55
+ }
56
+ catch {
57
+ return undefined;
58
+ }
59
+ return parseJson(raw);
60
+ }
61
+ function parseJson(raw) {
62
+ try {
63
+ return JSON.parse(raw);
64
+ }
65
+ catch {
66
+ // Corrupt entry files are treated as absent -- reconcile/re-registration rebuilds them.
67
+ return undefined;
68
+ }
69
+ }
70
+ function isProcessMatrixEntry(value) {
71
+ if (!isPlainRecord(value) || !isAgentIdentity(value.agent) || (value.role !== "master" && value.role !== "worker"))
72
+ return false;
73
+ const sessionId = value.agent.resumeContext.sessionId;
74
+ if (value.resumable !== undefined &&
75
+ (!isPlainRecord(value.resumable) ||
76
+ !isAgentIdentity(value.resumable.agent) ||
77
+ !isDeepStrictEqual(value.resumable.agent, value.agent) ||
78
+ !isProcessStatus(value.resumable.lastCode) ||
79
+ !isOptionalString(value.resumable.taskRef, 512) ||
80
+ !isOptionalString(value.resumable.taskSummary, 2_000)))
81
+ return false;
82
+ if (value.terminal !== undefined &&
83
+ (!isPlainRecord(value.terminal) ||
84
+ !(value.terminal.code === null || Number.isSafeInteger(value.terminal.code)) ||
85
+ !(value.terminal.signal === null ||
86
+ (typeof value.terminal.signal === "string" && value.terminal.signal.length <= 100)) ||
87
+ !isTimestamp(value.terminal.observedAt) ||
88
+ !(value.terminal.notificationDeliveredAt === undefined || isTimestamp(value.terminal.notificationDeliveredAt))))
89
+ return false;
90
+ if (value.terminal !== undefined && value.status !== "closed")
91
+ return false;
92
+ if (value.status === "resumable" && value.resumable === undefined)
93
+ return false;
94
+ return (value.entryId === buildEntryId(value.role, sessionId) &&
95
+ Number.isSafeInteger(value.pid) &&
96
+ typeof value.hostname === "string" &&
97
+ value.hostname.length <= 255 &&
98
+ isTimestamp(value.startedAt) &&
99
+ isTimestamp(value.heartbeatAt) &&
100
+ isProcessStatus(value.status) &&
101
+ (value.parentPid === undefined || (Number.isSafeInteger(value.parentPid) && Number(value.parentPid) > 0)) &&
102
+ isOptionalString(value.parentSessionId, 512) &&
103
+ isOptionalString(value.tmuxSession, 512) &&
104
+ (value.tmuxPanePid === undefined || Number.isSafeInteger(value.tmuxPanePid)) &&
105
+ isOptionalString(value.taskRef, 512) &&
106
+ isOptionalString(value.taskSummary, 2_000));
107
+ }
108
+ export async function readEntry(agentDir, entryId) {
109
+ const value = await readJsonFile(entryPath(agentDir, entryId));
110
+ return isProcessMatrixEntry(value) ? value : undefined;
111
+ }
112
+ export async function listEntries(agentDir) {
113
+ let names;
114
+ try {
115
+ names = await fsPromises.readdir(processMatrixDir(agentDir));
116
+ }
117
+ catch {
118
+ return [];
119
+ }
120
+ const entries = [];
121
+ for (const name of names.sort()) {
122
+ if (!name.endsWith(".json"))
123
+ continue;
124
+ const entry = await readJsonFile(join(processMatrixDir(agentDir), name));
125
+ if (isProcessMatrixEntry(entry))
126
+ entries.push(entry);
127
+ }
128
+ return entries;
129
+ }
130
+ function serialize(entry) {
131
+ return `${JSON.stringify(entry, null, "\t")}\n`;
132
+ }
133
+ async function withEntryLock(agentDir, entryId, operation) {
134
+ return withFileLock(entryPath(agentDir, entryId), operation);
135
+ }
136
+ export async function writeEntry(agentDir, entry) {
137
+ await withEntryLock(agentDir, entry.entryId, async () => {
138
+ await writeFileAtomic(entryPath(agentDir, entry.entryId), serialize(entry));
139
+ });
140
+ }
141
+ /** Atomically replace one entry only when its full persisted value still equals `expected`. */
142
+ export async function writeEntryIfUnchanged(agentDir, entryId, expected, next) {
143
+ if (next.entryId !== entryId) {
144
+ throw new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${entryId}'.`);
145
+ }
146
+ return withEntryLock(agentDir, entryId, async () => {
147
+ const value = await readJsonFile(entryPath(agentDir, entryId));
148
+ const current = isProcessMatrixEntry(value) ? value : undefined;
149
+ if (!isDeepStrictEqual(current, expected))
150
+ return false;
151
+ await writeFileAtomic(entryPath(agentDir, entryId), serialize(next));
152
+ return true;
153
+ });
154
+ }
155
+ /** Sync conditional replacement for best-effort `process.on("exit")` terminal writes. */
156
+ export function writeEntryIfUnchangedSync(agentDir, expected, next) {
157
+ if (next.entryId !== expected.entryId) {
158
+ throw new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${expected.entryId}'.`);
159
+ }
160
+ return withFileLockSync(entryPath(agentDir, expected.entryId), () => {
161
+ const value = readJsonFileSync(entryPath(agentDir, expected.entryId));
162
+ const current = isProcessMatrixEntry(value) ? value : undefined;
163
+ if (!isDeepStrictEqual(current, expected))
164
+ return false;
165
+ writeFileAtomicSync(entryPath(agentDir, expected.entryId), serialize(next));
166
+ return true;
167
+ }, { retries: 0 });
168
+ }
169
+ export async function removeEntry(agentDir, entryId) {
170
+ await withEntryLock(agentDir, entryId, async () => {
171
+ try {
172
+ await fsPromises.rm(entryPath(agentDir, entryId), { force: true });
173
+ }
174
+ catch {
175
+ // Best-effort; a missing file is already the desired end state.
176
+ }
177
+ });
178
+ }
179
+ export async function removeEntryIfUnchanged(agentDir, expected) {
180
+ return withEntryLock(agentDir, expected.entryId, async () => {
181
+ const value = await readJsonFile(entryPath(agentDir, expected.entryId));
182
+ const current = isProcessMatrixEntry(value) ? value : undefined;
183
+ if (!isDeepStrictEqual(current, expected))
184
+ return false;
185
+ await fsPromises.rm(entryPath(agentDir, expected.entryId), { force: true });
186
+ return true;
187
+ });
188
+ }
189
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../../src/core/process-matrix/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAA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n * durable across restarts and crashes (see `agent-paths.ts` -- this is machine state, not\n * transient `work/`).\n *\n * Each entry is owned by the process it describes: a master writes and heartbeats its OWN entry,\n * a worker self-registers and heartbeats its OWN entry (see `runtime.ts`'s module doc for the one\n * sanctioned, ask-gated exception during orphan adoption/cleanup). A missing or corrupt entry file\n * reads as absent -- never an error -- matching `worktree-sync/store.ts`'s store doctrine.\n */\n\nimport { promises as fsPromises, readFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { isDeepStrictEqual } from \"node:util\";\nimport { stateFile } from \"../agent-paths.ts\";\nimport { isAgentIdentity } from \"../orchestration/agent-resume.ts\";\nimport { withFileLock, withFileLockSync, writeFileAtomic, writeFileAtomicSync } from \"../util/atomic-file.ts\";\nimport { isPlainRecord } from \"../util/value-guards.ts\";\nimport type { ProcessMatrixEntry, ProcessRole, ProcessStatus } from \"./codes.ts\";\n\nfunction isProcessStatus(value: unknown): value is ProcessStatus {\n\treturn (\n\t\tvalue === \"running\" ||\n\t\tvalue === \"winding_down\" ||\n\t\tvalue === \"resumable\" ||\n\t\tvalue === \"adopted\" ||\n\t\tvalue === \"closed\"\n\t);\n}\n\nfunction isOptionalString(value: unknown, maxLength: number): boolean {\n\treturn value === undefined || (typeof value === \"string\" && value.length <= maxLength);\n}\n\nfunction isTimestamp(value: unknown): value is string {\n\treturn typeof value === \"string\" && Number.isFinite(Date.parse(value));\n}\n\nexport function processMatrixDir(agentDir: string): string {\n\treturn stateFile(agentDir, \"process-matrix\");\n}\n\nexport function entryPath(agentDir: string, entryId: string): string {\n\treturn join(processMatrixDir(agentDir), `${entryId}.json`);\n}\n\n/** Stable entry identity: one entry per (role, sessionId) pair. */\nexport function buildEntryId(role: ProcessRole, sessionId: string): string {\n\treturn `${role}-${sessionId}`;\n}\n\nasync function readJsonFile(filePath: string): Promise<unknown> {\n\tlet raw: string;\n\ttry {\n\t\traw = await fsPromises.readFile(filePath, \"utf-8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\treturn parseJson(raw);\n}\n\nfunction readJsonFileSync(filePath: string): unknown {\n\tlet raw: string;\n\ttry {\n\t\traw = readFileSync(filePath, \"utf-8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\treturn parseJson(raw);\n}\n\nfunction parseJson(raw: string): unknown {\n\ttry {\n\t\treturn JSON.parse(raw) as unknown;\n\t} catch {\n\t\t// Corrupt entry files are treated as absent -- reconcile/re-registration rebuilds them.\n\t\treturn undefined;\n\t}\n}\n\nfunction isProcessMatrixEntry(value: unknown): value is ProcessMatrixEntry {\n\tif (!isPlainRecord(value) || !isAgentIdentity(value.agent) || (value.role !== \"master\" && value.role !== \"worker\"))\n\t\treturn false;\n\tconst sessionId = value.agent.resumeContext.sessionId;\n\tif (\n\t\tvalue.resumable !== undefined &&\n\t\t(!isPlainRecord(value.resumable) ||\n\t\t\t!isAgentIdentity(value.resumable.agent) ||\n\t\t\t!isDeepStrictEqual(value.resumable.agent, value.agent) ||\n\t\t\t!isProcessStatus(value.resumable.lastCode) ||\n\t\t\t!isOptionalString(value.resumable.taskRef, 512) ||\n\t\t\t!isOptionalString(value.resumable.taskSummary, 2_000))\n\t)\n\t\treturn false;\n\tif (\n\t\tvalue.terminal !== undefined &&\n\t\t(!isPlainRecord(value.terminal) ||\n\t\t\t!(value.terminal.code === null || Number.isSafeInteger(value.terminal.code)) ||\n\t\t\t!(\n\t\t\t\tvalue.terminal.signal === null ||\n\t\t\t\t(typeof value.terminal.signal === \"string\" && value.terminal.signal.length <= 100)\n\t\t\t) ||\n\t\t\t!isTimestamp(value.terminal.observedAt) ||\n\t\t\t!(value.terminal.notificationDeliveredAt === undefined || isTimestamp(value.terminal.notificationDeliveredAt)))\n\t)\n\t\treturn false;\n\tif (value.terminal !== undefined && value.status !== \"closed\") return false;\n\tif (value.status === \"resumable\" && value.resumable === undefined) return false;\n\treturn (\n\t\tvalue.entryId === buildEntryId(value.role, sessionId) &&\n\t\tNumber.isSafeInteger(value.pid) &&\n\t\ttypeof value.hostname === \"string\" &&\n\t\tvalue.hostname.length <= 255 &&\n\t\tisTimestamp(value.startedAt) &&\n\t\tisTimestamp(value.heartbeatAt) &&\n\t\tisProcessStatus(value.status) &&\n\t\t(value.parentPid === undefined || (Number.isSafeInteger(value.parentPid) && Number(value.parentPid) > 0)) &&\n\t\tisOptionalString(value.parentSessionId, 512) &&\n\t\tisOptionalString(value.tmuxSession, 512) &&\n\t\t(value.tmuxPanePid === undefined || Number.isSafeInteger(value.tmuxPanePid)) &&\n\t\tisOptionalString(value.taskRef, 512) &&\n\t\tisOptionalString(value.taskSummary, 2_000)\n\t);\n}\n\nexport async function readEntry(agentDir: string, entryId: string): Promise<ProcessMatrixEntry | undefined> {\n\tconst value = await readJsonFile(entryPath(agentDir, entryId));\n\treturn isProcessMatrixEntry(value) ? value : undefined;\n}\n\nexport async function listEntries(agentDir: string): Promise<ProcessMatrixEntry[]> {\n\tlet names: string[];\n\ttry {\n\t\tnames = await fsPromises.readdir(processMatrixDir(agentDir));\n\t} catch {\n\t\treturn [];\n\t}\n\tconst entries: ProcessMatrixEntry[] = [];\n\tfor (const name of names.sort()) {\n\t\tif (!name.endsWith(\".json\")) continue;\n\t\tconst entry = await readJsonFile(join(processMatrixDir(agentDir), name));\n\t\tif (isProcessMatrixEntry(entry)) entries.push(entry);\n\t}\n\treturn entries;\n}\n\nfunction serialize(entry: ProcessMatrixEntry): string {\n\treturn `${JSON.stringify(entry, null, \"\\t\")}\\n`;\n}\n\nasync function withEntryLock<T>(agentDir: string, entryId: string, operation: () => Promise<T>): Promise<T> {\n\treturn withFileLock(entryPath(agentDir, entryId), operation);\n}\n\nexport async function writeEntry(agentDir: string, entry: ProcessMatrixEntry): Promise<void> {\n\tawait withEntryLock(agentDir, entry.entryId, async () => {\n\t\tawait writeFileAtomic(entryPath(agentDir, entry.entryId), serialize(entry));\n\t});\n}\n\n/** Atomically replace one entry only when its full persisted value still equals `expected`. */\nexport async function writeEntryIfUnchanged(\n\tagentDir: string,\n\tentryId: string,\n\texpected: ProcessMatrixEntry | undefined,\n\tnext: ProcessMatrixEntry,\n): Promise<boolean> {\n\tif (next.entryId !== entryId) {\n\t\tthrow new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${entryId}'.`);\n\t}\n\treturn withEntryLock(agentDir, entryId, async () => {\n\t\tconst value = await readJsonFile(entryPath(agentDir, entryId));\n\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\tawait writeFileAtomic(entryPath(agentDir, entryId), serialize(next));\n\t\treturn true;\n\t});\n}\n\n/** Sync conditional replacement for best-effort `process.on(\"exit\")` terminal writes. */\nexport function writeEntryIfUnchangedSync(\n\tagentDir: string,\n\texpected: ProcessMatrixEntry,\n\tnext: ProcessMatrixEntry,\n): boolean {\n\tif (next.entryId !== expected.entryId) {\n\t\tthrow new TypeError(`Process-matrix replacement entry '${next.entryId}' does not match '${expected.entryId}'.`);\n\t}\n\treturn withFileLockSync(\n\t\tentryPath(agentDir, expected.entryId),\n\t\t() => {\n\t\t\tconst value = readJsonFileSync(entryPath(agentDir, expected.entryId));\n\t\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\t\twriteFileAtomicSync(entryPath(agentDir, expected.entryId), serialize(next));\n\t\t\treturn true;\n\t\t},\n\t\t{ retries: 0 },\n\t);\n}\n\nexport async function removeEntry(agentDir: string, entryId: string): Promise<void> {\n\tawait withEntryLock(agentDir, entryId, async () => {\n\t\ttry {\n\t\t\tawait fsPromises.rm(entryPath(agentDir, entryId), { force: true });\n\t\t} catch {\n\t\t\t// Best-effort; a missing file is already the desired end state.\n\t\t}\n\t});\n}\n\nexport async function removeEntryIfUnchanged(agentDir: string, expected: ProcessMatrixEntry): Promise<boolean> {\n\treturn withEntryLock(agentDir, expected.entryId, async () => {\n\t\tconst value = await readJsonFile(entryPath(agentDir, expected.entryId));\n\t\tconst current = isProcessMatrixEntry(value) ? value : undefined;\n\t\tif (!isDeepStrictEqual(current, expected)) return false;\n\t\tawait fsPromises.rm(entryPath(agentDir, expected.entryId), { force: true });\n\t\treturn true;\n\t});\n}\n"]}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Process-matrix supervisor: PURE state-transition functions over `ProcessMatrixEntry`. Every
3
+ * function here takes its notion of "now" and process liveness as an explicit argument/dependency
4
+ * -- no `Date.now()`, no `process.kill` inside this module -- so the whole lifecycle (orphan
5
+ * detection, wind-down, adoption, reconcile) is deterministically testable without real timers or
6
+ * real processes. `runtime.ts` is the only caller that supplies real deps and real I/O.
7
+ */
8
+ import type { AgentIdentityContract } from "../orchestration/contracts.ts";
9
+ import type { ProcessMatrixEntry, ProcessTerminalHandoff, ReconcileMatrixResult, ResumablePayload, WindDownReason, WorkerDirective } from "./codes.ts";
10
+ export interface BuildMasterEntryFacts {
11
+ agent: AgentIdentityContract;
12
+ pid: number;
13
+ hostname: string;
14
+ now: string;
15
+ }
16
+ export declare function buildMasterEntry(facts: BuildMasterEntryFacts): ProcessMatrixEntry;
17
+ export interface BuildWorkerEntryFacts {
18
+ agent: AgentIdentityContract;
19
+ pid: number;
20
+ hostname: string;
21
+ now: string;
22
+ parentPid: number;
23
+ parentSessionId?: string;
24
+ tmuxSession?: string;
25
+ tmuxPanePid?: number;
26
+ taskRef?: string;
27
+ taskSummary?: string;
28
+ }
29
+ export declare function buildWorkerEntry(facts: BuildWorkerEntryFacts): ProcessMatrixEntry;
30
+ export declare function applyHeartbeat(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry;
31
+ export interface DetectOrphanedWorkersDeps {
32
+ isPidAlive: (pid: number) => boolean;
33
+ /** This session's own sessionId -- never treat yourself as an orphan you found. */
34
+ ownSessionId?: string;
35
+ }
36
+ /**
37
+ * Worker entries whose `parentPid` is dead, excluding this session's own entry and anything
38
+ * already `closed` (a closed worker isn't "orphaned", it's already done).
39
+ */
40
+ export declare function detectOrphanedWorkers(entries: ProcessMatrixEntry[], deps: DetectOrphanedWorkersDeps): ProcessMatrixEntry[];
41
+ export declare function beginWindDown(entry: ProcessMatrixEntry, reason: WindDownReason, now: string): ProcessMatrixEntry;
42
+ export declare function markResumable(entry: ProcessMatrixEntry, payload: ResumablePayload, now: string): ProcessMatrixEntry;
43
+ export declare function markClosed(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry;
44
+ export declare function markTerminal(entry: ProcessMatrixEntry, terminal: Pick<ProcessTerminalHandoff, "code" | "signal">, now: string): ProcessMatrixEntry;
45
+ export declare function markTerminalNotificationDelivered(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry;
46
+ export interface AdoptionFacts {
47
+ parentPid: number;
48
+ parentSessionId?: string;
49
+ }
50
+ /** Claim (or reclaim) an entry under a new parent: back to `running`, wind-down reason cleared. */
51
+ export declare function applyAdoption(entry: ProcessMatrixEntry, adoption: AdoptionFacts): ProcessMatrixEntry;
52
+ export interface PollWorkerDirectiveDeps {
53
+ isPidAlive: (pid: number) => boolean;
54
+ }
55
+ /**
56
+ * A worker calls this against its OWN freshly re-read entry to learn whether a master wrote a
57
+ * directive into it: an adoption (a new, live `parentPid` the worker didn't already know about)
58
+ * or a cooperative cleanup request (`windDownReason === "user_cleanup"`). `knownParentPid` is the
59
+ * parent the worker itself currently believes it has -- an unchanged, still-known parentPid never
60
+ * counts as a new adoption.
61
+ */
62
+ export declare function pollWorkerDirective(freshEntry: ProcessMatrixEntry, knownParentPid: number | undefined, deps: PollWorkerDirectiveDeps): WorkerDirective;
63
+ export interface ReconcileMatrixDeps {
64
+ isPidAlive: (pid: number) => boolean;
65
+ /** Epoch ms "now", compared against a resumable/adopted entry's `heartbeatAt`. */
66
+ now: number;
67
+ resumableTtlMs: number;
68
+ }
69
+ /**
70
+ * Prune `closed` entries and unrecoverable dead processes. A dead Pi worker that was interrupted
71
+ * before persisting its own resumable transition is repaired from its already-durable logical-agent
72
+ * identity. `resumable`/`adopted` entries are kept until they age past `resumableTtlMs`.
73
+ */
74
+ export declare function reconcileMatrix(entries: ProcessMatrixEntry[], deps: ReconcileMatrixDeps): ReconcileMatrixResult;
75
+ //# sourceMappingURL=supervisor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"supervisor.d.ts","sourceRoot":"","sources":["../../../src/core/process-matrix/supervisor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,KAAK,EACX,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,MAAM,YAAY,CAAC;AAGpB,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,qBAAqB,CAAC;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;CACZ;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,kBAAkB,CAWjF;AAED,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,qBAAqB,CAAC;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,kBAAkB,CAkBjF;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,kBAAkB,EAAE,GAAG,EAAE,MAAM,GAAG,kBAAkB,CAEzF;AAED,MAAM,WAAW,yBAAyB;IACzC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,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,mBAAmB;IACnC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IACrC,kFAAkF;IAClF,GAAG,EAAE,MAAM,CAAC;IACZ,cAAc,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,mBAAmB,GAAG,qBAAqB,CAwC/G","sourcesContent":["/**\n * Process-matrix supervisor: PURE state-transition functions over `ProcessMatrixEntry`. Every\n * function here takes its notion of \"now\" and process liveness as an explicit argument/dependency\n * -- no `Date.now()`, no `process.kill` inside this module -- so the whole lifecycle (orphan\n * detection, wind-down, adoption, reconcile) is deterministically testable without real timers or\n * real processes. `runtime.ts` is the only caller that supplies real deps and real I/O.\n */\n\nimport type { AgentIdentityContract } from \"../orchestration/contracts.ts\";\nimport type {\n\tProcessMatrixEntry,\n\tProcessTerminalHandoff,\n\tReconcileMatrixResult,\n\tResumablePayload,\n\tWindDownReason,\n\tWorkerDirective,\n} from \"./codes.ts\";\nimport { buildEntryId } from \"./store.ts\";\n\nexport interface BuildMasterEntryFacts {\n\tagent: AgentIdentityContract;\n\tpid: number;\n\thostname: string;\n\tnow: string;\n}\n\nexport function buildMasterEntry(facts: BuildMasterEntryFacts): ProcessMatrixEntry {\n\treturn {\n\t\tentryId: buildEntryId(\"master\", facts.agent.resumeContext.sessionId),\n\t\trole: \"master\",\n\t\tagent: structuredClone(facts.agent),\n\t\tpid: facts.pid,\n\t\thostname: facts.hostname,\n\t\tstartedAt: facts.now,\n\t\theartbeatAt: facts.now,\n\t\tstatus: \"running\",\n\t};\n}\n\nexport interface BuildWorkerEntryFacts {\n\tagent: AgentIdentityContract;\n\tpid: number;\n\thostname: string;\n\tnow: string;\n\tparentPid: number;\n\tparentSessionId?: string;\n\ttmuxSession?: string;\n\ttmuxPanePid?: number;\n\ttaskRef?: string;\n\ttaskSummary?: string;\n}\n\nexport function buildWorkerEntry(facts: BuildWorkerEntryFacts): ProcessMatrixEntry {\n\tconst entry: ProcessMatrixEntry = {\n\t\tentryId: buildEntryId(\"worker\", facts.agent.resumeContext.sessionId),\n\t\trole: \"worker\",\n\t\tagent: structuredClone(facts.agent),\n\t\tpid: facts.pid,\n\t\thostname: facts.hostname,\n\t\tstartedAt: facts.now,\n\t\theartbeatAt: facts.now,\n\t\tstatus: \"running\",\n\t\tparentPid: facts.parentPid,\n\t};\n\tif (facts.parentSessionId !== undefined) entry.parentSessionId = facts.parentSessionId;\n\tif (facts.tmuxSession !== undefined) entry.tmuxSession = facts.tmuxSession;\n\tif (facts.tmuxPanePid !== undefined) entry.tmuxPanePid = facts.tmuxPanePid;\n\tif (facts.taskRef !== undefined) entry.taskRef = facts.taskRef;\n\tif (facts.taskSummary !== undefined) entry.taskSummary = facts.taskSummary;\n\treturn entry;\n}\n\nexport function applyHeartbeat(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry {\n\treturn { ...entry, heartbeatAt: now };\n}\n\nexport interface DetectOrphanedWorkersDeps {\n\tisPidAlive: (pid: number) => boolean;\n\t/** This session's own sessionId -- never treat yourself as an orphan you found. */\n\townSessionId?: string;\n}\n\n/**\n * Worker entries whose `parentPid` is dead, excluding this session's own entry and anything\n * already `closed` (a closed worker isn't \"orphaned\", it's already done).\n */\nexport function detectOrphanedWorkers(\n\tentries: ProcessMatrixEntry[],\n\tdeps: DetectOrphanedWorkersDeps,\n): ProcessMatrixEntry[] {\n\treturn entries.filter((entry) => {\n\t\tif (entry.role !== \"worker\") return false;\n\t\tif (entry.status === \"closed\") return false;\n\t\tif (deps.ownSessionId !== undefined && entry.agent.resumeContext.sessionId === deps.ownSessionId) return false;\n\t\tif (entry.parentPid === undefined) return false;\n\t\treturn !deps.isPidAlive(entry.parentPid);\n\t});\n}\n\nexport function beginWindDown(entry: ProcessMatrixEntry, reason: WindDownReason, now: string): ProcessMatrixEntry {\n\treturn { ...entry, status: \"winding_down\", windDownReason: reason, heartbeatAt: now };\n}\n\nexport function markResumable(entry: ProcessMatrixEntry, payload: ResumablePayload, now: string): ProcessMatrixEntry {\n\treturn { ...entry, status: \"resumable\", resumable: payload, heartbeatAt: now };\n}\n\nexport function markClosed(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry {\n\treturn { ...entry, status: \"closed\", heartbeatAt: now };\n}\n\nexport function markTerminal(\n\tentry: ProcessMatrixEntry,\n\tterminal: Pick<ProcessTerminalHandoff, \"code\" | \"signal\">,\n\tnow: string,\n): ProcessMatrixEntry {\n\treturn {\n\t\t...markClosed(entry, now),\n\t\tterminal: { code: terminal.code, signal: terminal.signal, observedAt: now },\n\t};\n}\n\nexport function markTerminalNotificationDelivered(entry: ProcessMatrixEntry, now: string): ProcessMatrixEntry {\n\tif (!entry.terminal) return entry;\n\treturn { ...entry, terminal: { ...entry.terminal, notificationDeliveredAt: now } };\n}\n\nexport interface AdoptionFacts {\n\tparentPid: number;\n\tparentSessionId?: string;\n}\n\n/** Claim (or reclaim) an entry under a new parent: back to `running`, wind-down reason cleared. */\nexport function applyAdoption(entry: ProcessMatrixEntry, adoption: AdoptionFacts): ProcessMatrixEntry {\n\tconst next: ProcessMatrixEntry = { ...entry, status: \"running\", parentPid: adoption.parentPid };\n\tdelete next.windDownReason;\n\tif (adoption.parentSessionId !== undefined) next.parentSessionId = adoption.parentSessionId;\n\treturn next;\n}\n\nexport interface PollWorkerDirectiveDeps {\n\tisPidAlive: (pid: number) => boolean;\n}\n\n/**\n * A worker calls this against its OWN freshly re-read entry to learn whether a master wrote a\n * directive into it: an adoption (a new, live `parentPid` the worker didn't already know about)\n * or a cooperative cleanup request (`windDownReason === \"user_cleanup\"`). `knownParentPid` is the\n * parent the worker itself currently believes it has -- an unchanged, still-known parentPid never\n * counts as a new adoption.\n */\nexport function pollWorkerDirective(\n\tfreshEntry: ProcessMatrixEntry,\n\tknownParentPid: number | undefined,\n\tdeps: PollWorkerDirectiveDeps,\n): WorkerDirective {\n\tif (freshEntry.windDownReason === \"user_cleanup\") return { code: \"user_cleanup\" };\n\tif (\n\t\tfreshEntry.parentPid !== undefined &&\n\t\tfreshEntry.parentPid !== knownParentPid &&\n\t\tdeps.isPidAlive(freshEntry.parentPid)\n\t) {\n\t\treturn { code: \"adopt\", parentPid: freshEntry.parentPid };\n\t}\n\treturn { code: \"none\" };\n}\n\nexport interface ReconcileMatrixDeps {\n\tisPidAlive: (pid: number) => boolean;\n\t/** Epoch ms \"now\", compared against a resumable/adopted entry's `heartbeatAt`. */\n\tnow: number;\n\tresumableTtlMs: number;\n}\n\n/**\n * Prune `closed` entries and unrecoverable dead processes. A dead Pi worker that was interrupted\n * before persisting its own resumable transition is repaired from its already-durable logical-agent\n * identity. `resumable`/`adopted` entries are kept until they age past `resumableTtlMs`.\n */\nexport function reconcileMatrix(entries: ProcessMatrixEntry[], deps: ReconcileMatrixDeps): ReconcileMatrixResult {\n\tconst kept: ProcessMatrixEntry[] = [];\n\tconst prunedEntryIds: string[] = [];\n\tconst recoveredEntryIds: string[] = [];\n\tfor (const entry of entries) {\n\t\tif (entry.status === \"closed\") {\n\t\t\tconst terminalAt = Date.parse(entry.terminal?.observedAt ?? entry.heartbeatAt);\n\t\t\tconst terminalExpired = !Number.isFinite(terminalAt) || deps.now - terminalAt > deps.resumableTtlMs;\n\t\t\tif (entry.terminal && !entry.terminal.notificationDeliveredAt && !terminalExpired) kept.push(entry);\n\t\t\telse prunedEntryIds.push(entry.entryId);\n\t\t\tcontinue;\n\t\t}\n\t\tif (entry.status === \"running\" || entry.status === \"winding_down\") {\n\t\t\tif (!deps.isPidAlive(entry.pid)) {\n\t\t\t\tif (entry.role === \"worker\" && entry.agent.resumeContext.provider === \"pi\") {\n\t\t\t\t\tconst payload: ResumablePayload = {\n\t\t\t\t\t\tagent: structuredClone(entry.agent),\n\t\t\t\t\t\tlastCode: \"resumable\",\n\t\t\t\t\t};\n\t\t\t\t\tif (entry.taskRef !== undefined) payload.taskRef = entry.taskRef;\n\t\t\t\t\tif (entry.taskSummary !== undefined) payload.taskSummary = entry.taskSummary;\n\t\t\t\t\tkept.push(markResumable(entry, payload, new Date(deps.now).toISOString()));\n\t\t\t\t\trecoveredEntryIds.push(entry.entryId);\n\t\t\t\t} else {\n\t\t\t\t\tprunedEntryIds.push(entry.entryId);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tkept.push(entry);\n\t\t\tcontinue;\n\t\t}\n\t\t// resumable / adopted: TTL-gated on the entry's own last heartbeat.\n\t\tconst heartbeatMs = Date.parse(entry.heartbeatAt);\n\t\tif (!Number.isFinite(heartbeatMs) || deps.now - heartbeatMs > deps.resumableTtlMs) {\n\t\t\tprunedEntryIds.push(entry.entryId);\n\t\t\tcontinue;\n\t\t}\n\t\tkept.push(entry);\n\t}\n\treturn { code: \"reconciled\", kept, prunedEntryIds, recoveredEntryIds };\n}\n"]}