pi-better-server 0.4.0 → 0.6.0

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 (597) hide show
  1. package/package.json +8 -1
  2. package/scripts/copy-pi-test-providers.mjs +10 -0
  3. package/scripts/event-publication/README.md +26 -0
  4. package/scripts/event-publication/commit.mjs +21 -0
  5. package/scripts/event-publication/measure.mjs +87 -0
  6. package/scripts/event-publication/writer.mjs +6 -0
  7. package/scripts/measure-full-runtime/frontend.ts +24 -0
  8. package/scripts/measure-full-runtime/hosted-extension.ts +28 -0
  9. package/scripts/measure-full-runtime/probe.mjs +29 -0
  10. package/scripts/measure-full-runtime/recovery-trace-loader.mjs +51 -0
  11. package/scripts/measure-full-runtime/recovery-trace.mjs +16 -0
  12. package/scripts/measure-full-runtime.mjs +146 -0
  13. package/scripts/measure-shared-reads/probe.mjs +26 -2
  14. package/scripts/measure-shared-reads.mjs +44 -9
  15. package/scripts/private-runtime/package-lock.json +66 -4
  16. package/scripts/private-runtime/package.json +10 -2
  17. package/scripts/source-provenance.mjs +31 -0
  18. package/scripts/test-on-pi.sh +3 -9
  19. package/src/handshake.ts +1 -1
  20. package/src/hosted-session.ts +32 -4
  21. package/src/maintenance.ts +35 -7
  22. package/src/registry.ts +28 -4
  23. package/src/runtime/attempt-invalidation.ts +54 -0
  24. package/src/runtime/background-history.ts +151 -45
  25. package/src/runtime/background-policy.ts +27 -0
  26. package/src/runtime/background-tools.ts +389 -0
  27. package/src/runtime/background.ts +410 -48
  28. package/src/runtime/cancellation.ts +28 -7
  29. package/src/runtime/catalog-sources.ts +199 -0
  30. package/src/runtime/catalog.ts +53 -21
  31. package/src/runtime/client.ts +22 -2
  32. package/src/runtime/domain/assignment-inbox.mjs +119 -0
  33. package/src/runtime/domain/assignment-observation.d.mts +1 -1
  34. package/src/runtime/domain/assignment-observation.mjs +71 -33
  35. package/src/runtime/domain/background/process-identity.mjs +20 -2
  36. package/src/runtime/domain/background/process.mjs +3 -3
  37. package/src/runtime/domain/background/registry.mjs +89 -4
  38. package/src/runtime/domain/background/types.mjs +1 -0
  39. package/src/runtime/domain/background-service/adapter-contract.domain.mjs +83 -0
  40. package/src/runtime/domain/background-service/adapter-contract.ts +70 -0
  41. package/src/runtime/domain/background-service/batch.domain.mjs +91 -0
  42. package/src/runtime/domain/background-service/batch.ts +111 -0
  43. package/src/runtime/domain/background-service/conditions.domain.mjs +94 -0
  44. package/src/runtime/domain/background-service/conditions.ts +99 -0
  45. package/src/runtime/domain/background-service/delivery-observer.domain.mjs +37 -0
  46. package/src/runtime/domain/background-service/delivery-observer.ts +42 -0
  47. package/src/runtime/domain/background-service/failures.domain.mjs +209 -0
  48. package/src/runtime/domain/background-service/failures.ts +212 -0
  49. package/src/runtime/domain/background-service/goal-provider.domain.mjs +193 -0
  50. package/src/runtime/domain/background-service/goal-provider.ts +209 -0
  51. package/src/runtime/domain/background-service/index.domain.mjs +54 -0
  52. package/src/runtime/domain/background-service/index.ts +55 -0
  53. package/src/runtime/domain/background-service/logs.domain.mjs +129 -0
  54. package/src/runtime/domain/background-service/logs.ts +134 -0
  55. package/src/runtime/domain/background-service/maintenance.domain.mjs +95 -0
  56. package/src/runtime/domain/background-service/maintenance.ts +98 -0
  57. package/src/runtime/domain/background-service/managed-authority.domain.mjs +32 -0
  58. package/src/runtime/domain/background-service/managed-authority.ts +30 -0
  59. package/src/runtime/domain/background-service/managed-client.domain.mjs +79 -0
  60. package/src/runtime/domain/background-service/managed-client.ts +73 -0
  61. package/src/runtime/domain/background-service/managed-frontend.domain.mjs +225 -0
  62. package/src/runtime/domain/background-service/managed-frontend.ts +176 -0
  63. package/src/runtime/domain/background-service/navigator-provider.domain.mjs +492 -0
  64. package/src/runtime/domain/background-service/navigator-provider.ts +485 -0
  65. package/src/runtime/domain/background-service/observed-wait-client.domain.mjs +6 -0
  66. package/src/runtime/domain/background-service/observed-wait-client.ts +10 -0
  67. package/src/runtime/domain/background-service/observed-waits.domain.mjs +4080 -0
  68. package/src/runtime/domain/background-service/observed-waits.ts +4513 -0
  69. package/src/runtime/domain/background-service/output.domain.mjs +867 -0
  70. package/src/runtime/domain/background-service/output.ts +952 -0
  71. package/src/runtime/domain/background-service/process-identity.domain.mjs +129 -0
  72. package/src/runtime/domain/background-service/process-identity.ts +134 -0
  73. package/src/runtime/domain/background-service/process.domain.mjs +512 -0
  74. package/src/runtime/domain/background-service/process.ts +532 -0
  75. package/src/runtime/domain/background-service/prompt.domain.mjs +58 -0
  76. package/src/runtime/domain/background-service/prompt.ts +72 -0
  77. package/src/runtime/domain/background-service/registry.domain.mjs +460 -0
  78. package/src/runtime/domain/background-service/registry.ts +450 -0
  79. package/src/runtime/domain/background-service/remote-task-preset.domain.mjs +67 -0
  80. package/src/runtime/domain/background-service/remote-task-preset.ts +127 -0
  81. package/src/runtime/domain/background-service/runtime.domain.mjs +1511 -0
  82. package/src/runtime/domain/background-service/runtime.ts +1636 -0
  83. package/src/runtime/domain/background-service/sandbox.domain.mjs +735 -0
  84. package/src/runtime/domain/background-service/sandbox.ts +913 -0
  85. package/src/runtime/domain/background-service/shared-callback-batcher.domain.mjs +777 -0
  86. package/src/runtime/domain/background-service/shared-callback-batcher.ts +956 -0
  87. package/src/runtime/domain/background-service/shared-execution-policy.domain.mjs +50 -0
  88. package/src/runtime/domain/background-service/shared-execution-policy.ts +61 -0
  89. package/src/runtime/domain/background-service/shared-failure-observations.domain.mjs +1078 -0
  90. package/src/runtime/domain/background-service/shared-failure-observations.ts +1166 -0
  91. package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +126 -0
  92. package/src/runtime/domain/background-service/shared-harness-settings.ts +116 -0
  93. package/src/runtime/domain/background-service/shared-log-utils.domain.mjs +1223 -0
  94. package/src/runtime/domain/background-service/shared-log-utils.ts +1492 -0
  95. package/src/runtime/domain/background-service/shared-navigator-panels.domain.mjs +289 -0
  96. package/src/runtime/domain/background-service/shared-navigator-panels.ts +272 -0
  97. package/src/runtime/domain/background-service/shared-navigator.domain.mjs +1930 -0
  98. package/src/runtime/domain/background-service/shared-navigator.ts +1962 -0
  99. package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-inbox.mjs +144 -0
  100. package/src/runtime/domain/background-service/shared-operation-coordinator/assignment-observation.mjs +189 -0
  101. package/src/runtime/domain/background-service/shared-operation-coordinator/index.domain.mjs +51 -0
  102. package/src/runtime/domain/background-service/shared-operation-coordinator/index.ts +122 -0
  103. package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
  104. package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-changes.ts +73 -0
  105. package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
  106. package/src/runtime/domain/background-service/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
  107. package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
  108. package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
  109. package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
  110. package/src/runtime/domain/background-service/shared-operation-coordinator/src/background-requests.ts +90 -0
  111. package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
  112. package/src/runtime/domain/background-service/shared-operation-coordinator/src/discovery.ts +139 -0
  113. package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.domain.mjs +12 -0
  114. package/src/runtime/domain/background-service/shared-operation-coordinator/src/errors.ts +79 -0
  115. package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.domain.mjs +342 -0
  116. package/src/runtime/domain/background-service/shared-operation-coordinator/src/operations.ts +513 -0
  117. package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
  118. package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +458 -0
  119. package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
  120. package/src/runtime/domain/background-service/shared-operation-coordinator/src/recovery.ts +55 -0
  121. package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
  122. package/src/runtime/domain/background-service/shared-operation-coordinator/src/result-supervision.ts +140 -0
  123. package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.domain.mjs +280 -0
  124. package/src/runtime/domain/background-service/shared-operation-coordinator/src/rows.ts +442 -0
  125. package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.domain.mjs +420 -0
  126. package/src/runtime/domain/background-service/shared-operation-coordinator/src/schema.ts +441 -0
  127. package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
  128. package/src/runtime/domain/background-service/shared-operation-coordinator/src/sqlite.ts +239 -0
  129. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +275 -0
  130. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +585 -0
  131. package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
  132. package/src/runtime/domain/background-service/shared-operation-coordinator/src/subagent-launch.ts +142 -0
  133. package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
  134. package/src/runtime/domain/background-service/shared-operation-coordinator/src/task-cancellation.ts +224 -0
  135. package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.domain.mjs +25 -0
  136. package/src/runtime/domain/background-service/shared-operation-coordinator/src/types.ts +707 -0
  137. package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
  138. package/src/runtime/domain/background-service/shared-operation-coordinator/src/upgrade.ts +22 -0
  139. package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.domain.mjs +234 -0
  140. package/src/runtime/domain/background-service/shared-operation-coordinator/src/validate.ts +241 -0
  141. package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.domain.mjs +851 -0
  142. package/src/runtime/domain/background-service/shared-operation-coordinator/src/waits.ts +1424 -0
  143. package/src/runtime/domain/background-service/shared-permission-blocker.domain.mjs +40 -0
  144. package/src/runtime/domain/background-service/shared-permission-blocker.ts +48 -0
  145. package/src/runtime/domain/background-service/shared-render-scheduler.domain.mjs +41 -0
  146. package/src/runtime/domain/background-service/shared-render-scheduler.ts +47 -0
  147. package/src/runtime/domain/background-service/shared-sandbox-core.domain.mjs +1531 -0
  148. package/src/runtime/domain/background-service/shared-sandbox-core.ts +1757 -0
  149. package/src/runtime/domain/background-service/shared-sandbox-diagnostics.domain.mjs +591 -0
  150. package/src/runtime/domain/background-service/shared-sandbox-diagnostics.ts +491 -0
  151. package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +698 -0
  152. package/src/runtime/domain/background-service/shared-ssh-core/index.ts +979 -0
  153. package/src/runtime/domain/background-service/shared-stall-detector.domain.mjs +37 -0
  154. package/src/runtime/domain/background-service/shared-stall-detector.ts +69 -0
  155. package/src/runtime/domain/background-service/shared-work-client.mjs +303 -0
  156. package/src/runtime/domain/background-service/stall-attention.domain.mjs +29 -0
  157. package/src/runtime/domain/background-service/stall-attention.ts +31 -0
  158. package/src/runtime/domain/background-service/stall.domain.mjs +57 -0
  159. package/src/runtime/domain/background-service/stall.ts +57 -0
  160. package/src/runtime/domain/background-service/supervisor/README.md +111 -0
  161. package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +90 -0
  162. package/src/runtime/domain/background-service/supervisor/generated/conditions.mjs +96 -0
  163. package/src/runtime/domain/background-service/supervisor/generated/identity.mjs +95 -0
  164. package/src/runtime/domain/background-service/supervisor/generated/process-identity.mjs +131 -0
  165. package/src/runtime/domain/background-service/supervisor/generated/process.mjs +514 -0
  166. package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +872 -0
  167. package/src/runtime/domain/background-service/supervisor/generated/store.mjs +609 -0
  168. package/src/runtime/domain/background-service/supervisor/generated/types.mjs +50 -0
  169. package/src/runtime/domain/background-service/supervisor/identity.domain.mjs +93 -0
  170. package/src/runtime/domain/background-service/supervisor/identity.ts +99 -0
  171. package/src/runtime/domain/background-service/supervisor/index.domain.mjs +22 -0
  172. package/src/runtime/domain/background-service/supervisor/index.ts +37 -0
  173. package/src/runtime/domain/background-service/supervisor/invocation.domain.mjs +30 -0
  174. package/src/runtime/domain/background-service/supervisor/invocation.ts +44 -0
  175. package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +870 -0
  176. package/src/runtime/domain/background-service/supervisor/runner.ts +982 -0
  177. package/src/runtime/domain/background-service/supervisor/store.domain.mjs +605 -0
  178. package/src/runtime/domain/background-service/supervisor/store.ts +651 -0
  179. package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +721 -0
  180. package/src/runtime/domain/background-service/supervisor/supervisor.ts +804 -0
  181. package/src/runtime/domain/background-service/supervisor/types.domain.mjs +48 -0
  182. package/src/runtime/domain/background-service/supervisor/types.ts +394 -0
  183. package/src/runtime/domain/background-service/task-diagnostics.domain.mjs +60 -0
  184. package/src/runtime/domain/background-service/task-diagnostics.ts +71 -0
  185. package/src/runtime/domain/background-service/tools.domain.mjs +1050 -0
  186. package/src/runtime/domain/background-service/tools.ts +1142 -0
  187. package/src/runtime/domain/background-service/types.domain.mjs +5 -0
  188. package/src/runtime/domain/background-service/types.ts +236 -0
  189. package/src/runtime/domain/background-service/work-observation.domain.mjs +176 -0
  190. package/src/runtime/domain/background-service/work-observation.ts +144 -0
  191. package/src/runtime/domain/catalog/catalog-store.d.mts +9 -1
  192. package/src/runtime/domain/catalog/catalog-store.mjs +20 -22
  193. package/src/runtime/domain/coordinator/src/assignment-changes.mjs +75 -0
  194. package/src/runtime/domain/coordinator/src/assignment-outcomes.mjs +2 -0
  195. package/src/runtime/domain/coordinator/src/background-parent-callbacks.mjs +65 -0
  196. package/src/runtime/domain/coordinator/src/background-requests.mjs +86 -0
  197. package/src/runtime/domain/coordinator/src/discovery.mjs +4 -2
  198. package/src/runtime/domain/coordinator/src/operations.mjs +44 -4
  199. package/src/runtime/domain/coordinator/src/result-supervision.mjs +12 -0
  200. package/src/runtime/domain/coordinator/src/rows.mjs +2 -0
  201. package/src/runtime/domain/coordinator/src/schema.mjs +73 -21
  202. package/src/runtime/domain/coordinator/src/store.mjs +32 -3
  203. package/src/runtime/domain/coordinator/src/subagent-launch.mjs +119 -0
  204. package/src/runtime/domain/coordinator/src/types.mjs +1 -1
  205. package/src/runtime/domain/coordinator/src/upgrade.mjs +22 -0
  206. package/src/runtime/domain/health-observation.d.mts +3 -1
  207. package/src/runtime/domain/health-observation.mjs +189 -190
  208. package/src/runtime/domain/log-cursor.d.mts +2 -0
  209. package/src/runtime/domain/log-cursor.mjs +85 -16
  210. package/src/runtime/domain/parse.d.mts +4 -1
  211. package/src/runtime/domain/parse.mjs +50 -5
  212. package/src/runtime/domain/sandbox-policy/README.md +633 -0
  213. package/src/runtime/domain/sandbox-policy/background-admission.domain.mjs +83 -0
  214. package/src/runtime/domain/sandbox-policy/background-admission.ts +75 -0
  215. package/src/runtime/domain/sandbox-policy/commands.domain.mjs +245 -0
  216. package/src/runtime/domain/sandbox-policy/commands.ts +336 -0
  217. package/src/runtime/domain/sandbox-policy/deny-rules.domain.mjs +440 -0
  218. package/src/runtime/domain/sandbox-policy/deny-rules.ts +620 -0
  219. package/src/runtime/domain/sandbox-policy/diagnostics-command.domain.mjs +30 -0
  220. package/src/runtime/domain/sandbox-policy/diagnostics-command.ts +27 -0
  221. package/src/runtime/domain/sandbox-policy/events.domain.mjs +49 -0
  222. package/src/runtime/domain/sandbox-policy/events.ts +85 -0
  223. package/src/runtime/domain/sandbox-policy/files.domain.mjs +153 -0
  224. package/src/runtime/domain/sandbox-policy/files.ts +224 -0
  225. package/src/runtime/domain/sandbox-policy/index.domain.mjs +289 -0
  226. package/src/runtime/domain/sandbox-policy/index.ts +386 -0
  227. package/src/runtime/domain/sandbox-policy/package.json +66 -0
  228. package/src/runtime/domain/sandbox-policy/permission-settings.domain.mjs +36 -0
  229. package/src/runtime/domain/sandbox-policy/permission-settings.ts +46 -0
  230. package/src/runtime/domain/sandbox-policy/permissions-page.domain.mjs +309 -0
  231. package/src/runtime/domain/sandbox-policy/permissions-page.ts +295 -0
  232. package/src/runtime/domain/sandbox-policy/permissions.domain.mjs +98 -0
  233. package/src/runtime/domain/sandbox-policy/permissions.ts +125 -0
  234. package/src/runtime/domain/sandbox-policy/policy.domain.mjs +80 -0
  235. package/src/runtime/domain/sandbox-policy/policy.ts +102 -0
  236. package/src/runtime/domain/sandbox-policy/preferences.domain.mjs +55 -0
  237. package/src/runtime/domain/sandbox-policy/preferences.ts +82 -0
  238. package/src/runtime/domain/sandbox-policy/rules-page.domain.mjs +138 -0
  239. package/src/runtime/domain/sandbox-policy/rules-page.ts +176 -0
  240. package/src/runtime/domain/sandbox-policy/session-permissions.domain.mjs +22 -0
  241. package/src/runtime/domain/sandbox-policy/session-permissions.ts +24 -0
  242. package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +126 -0
  243. package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +116 -0
  244. package/src/runtime/domain/sandbox-policy/shared-permission-blocker.domain.mjs +40 -0
  245. package/src/runtime/domain/sandbox-policy/shared-permission-blocker.ts +48 -0
  246. package/src/runtime/domain/sandbox-policy/shared-sandbox-core.domain.mjs +1531 -0
  247. package/src/runtime/domain/sandbox-policy/shared-sandbox-core.ts +1757 -0
  248. package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.domain.mjs +591 -0
  249. package/src/runtime/domain/sandbox-policy/shared-sandbox-diagnostics.ts +491 -0
  250. package/src/runtime/domain/sandbox-policy/shared-task-application.domain.mjs +185 -0
  251. package/src/runtime/domain/sandbox-policy/shared-task-application.ts +185 -0
  252. package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.domain.mjs +416 -0
  253. package/src/runtime/domain/sandbox-policy/shared-task-apply-patch.ts +409 -0
  254. package/src/runtime/domain/sandbox-policy/shared-task-exec.domain.mjs +325 -0
  255. package/src/runtime/domain/sandbox-policy/shared-task-exec.ts +335 -0
  256. package/src/runtime/domain/sandbox-policy/shared-task-files.domain.mjs +346 -0
  257. package/src/runtime/domain/sandbox-policy/shared-task-files.ts +342 -0
  258. package/src/runtime/domain/sandbox-policy/shared-task-process-identity.domain.mjs +140 -0
  259. package/src/runtime/domain/sandbox-policy/shared-task-process-identity.ts +126 -0
  260. package/src/runtime/domain/sandbox-policy/shared-task-process-list.domain.mjs +220 -0
  261. package/src/runtime/domain/sandbox-policy/shared-task-process-list.ts +209 -0
  262. package/src/runtime/domain/sandbox-policy/shared-task-sandbox.domain.mjs +227 -0
  263. package/src/runtime/domain/sandbox-policy/shared-task-sandbox.ts +224 -0
  264. package/src/runtime/domain/sandbox-policy/shared-task-tools.domain.mjs +92 -0
  265. package/src/runtime/domain/sandbox-policy/shared-task-tools.ts +123 -0
  266. package/src/runtime/domain/sandbox-policy/shell.domain.mjs +93 -0
  267. package/src/runtime/domain/sandbox-policy/shell.ts +129 -0
  268. package/src/runtime/domain/sandbox-policy/state.domain.mjs +307 -0
  269. package/src/runtime/domain/sandbox-policy/state.ts +408 -0
  270. package/src/runtime/domain/sandbox-policy/status.domain.mjs +64 -0
  271. package/src/runtime/domain/sandbox-policy/status.ts +76 -0
  272. package/src/runtime/domain/sandbox-policy/subagent-admission.domain.mjs +69 -0
  273. package/src/runtime/domain/sandbox-policy/subagent-admission.ts +60 -0
  274. package/src/runtime/domain/subagents/CONTEXT.md +202 -0
  275. package/src/runtime/domain/subagents/PRODUCT.md +49 -0
  276. package/src/runtime/domain/subagents/README.md +201 -0
  277. package/src/runtime/domain/subagents/agent-catalog.mjs +64 -0
  278. package/src/runtime/domain/subagents/agent-commands.domain.mjs +822 -0
  279. package/src/runtime/domain/subagents/agent-commands.ts +905 -0
  280. package/src/runtime/domain/subagents/agent-inspection.domain.mjs +398 -0
  281. package/src/runtime/domain/subagents/agent-inspection.ts +574 -0
  282. package/src/runtime/domain/subagents/agent-operations.domain.mjs +40 -0
  283. package/src/runtime/domain/subagents/agent-operations.ts +61 -0
  284. package/src/runtime/domain/subagents/agent-session-settings.domain.mjs +112 -0
  285. package/src/runtime/domain/subagents/agent-session-settings.ts +118 -0
  286. package/src/runtime/domain/subagents/agents-catalog-tool.domain.mjs +84 -0
  287. package/src/runtime/domain/subagents/agents-catalog-tool.ts +118 -0
  288. package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +276 -0
  289. package/src/runtime/domain/subagents/assignment-outcomes.ts +212 -0
  290. package/src/runtime/domain/subagents/assignment-projection.mjs +261 -0
  291. package/src/runtime/domain/subagents/assignment-report.domain.mjs +174 -0
  292. package/src/runtime/domain/subagents/assignment-report.ts +156 -0
  293. package/src/runtime/domain/subagents/batch.mjs +275 -0
  294. package/src/runtime/domain/subagents/callback-fields.domain.mjs +72 -0
  295. package/src/runtime/domain/subagents/callback-fields.ts +95 -0
  296. package/src/runtime/domain/subagents/capacity.mjs +108 -0
  297. package/src/runtime/domain/subagents/catalog-editor.domain.mjs +98 -0
  298. package/src/runtime/domain/subagents/catalog-editor.ts +125 -0
  299. package/src/runtime/domain/subagents/catalog-host-admission.domain.mjs +104 -0
  300. package/src/runtime/domain/subagents/catalog-host-admission.ts +85 -0
  301. package/src/runtime/domain/subagents/catalog-identity.domain.mjs +222 -0
  302. package/src/runtime/domain/subagents/catalog-identity.ts +258 -0
  303. package/src/runtime/domain/subagents/catalog-overlay.domain.mjs +9 -0
  304. package/src/runtime/domain/subagents/catalog-overlay.ts +10 -0
  305. package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +164 -0
  306. package/src/runtime/domain/subagents/catalog-preferences.ts +139 -0
  307. package/src/runtime/domain/subagents/catalog-resolver.domain.mjs +366 -0
  308. package/src/runtime/domain/subagents/catalog-resolver.ts +540 -0
  309. package/src/runtime/domain/subagents/catalog-runtime.domain.mjs +570 -0
  310. package/src/runtime/domain/subagents/catalog-runtime.ts +680 -0
  311. package/src/runtime/domain/subagents/catalog-schema.domain.mjs +1331 -0
  312. package/src/runtime/domain/subagents/catalog-schema.ts +1563 -0
  313. package/src/runtime/domain/subagents/catalog-skill-edits.domain.mjs +95 -0
  314. package/src/runtime/domain/subagents/catalog-skill-edits.ts +116 -0
  315. package/src/runtime/domain/subagents/catalog-store.domain.mjs +808 -0
  316. package/src/runtime/domain/subagents/catalog-store.ts +1006 -0
  317. package/src/runtime/domain/subagents/catalog-ui.domain.mjs +651 -0
  318. package/src/runtime/domain/subagents/catalog-ui.ts +611 -0
  319. package/src/runtime/domain/subagents/child-apply-patch.domain.mjs +13 -0
  320. package/src/runtime/domain/subagents/child-apply-patch.ts +13 -0
  321. package/src/runtime/domain/subagents/child-codemode.domain.mjs +16 -0
  322. package/src/runtime/domain/subagents/child-codemode.ts +17 -0
  323. package/src/runtime/domain/subagents/child-dependencies.mjs +193 -0
  324. package/src/runtime/domain/subagents/child-incidents.domain.mjs +170 -0
  325. package/src/runtime/domain/subagents/child-incidents.ts +169 -0
  326. package/src/runtime/domain/subagents/child-operations.domain.mjs +986 -0
  327. package/src/runtime/domain/subagents/child-operations.ts +1191 -0
  328. package/src/runtime/domain/subagents/child-ownership.domain.mjs +205 -0
  329. package/src/runtime/domain/subagents/child-ownership.ts +160 -0
  330. package/src/runtime/domain/subagents/child-preflight.mjs +38 -0
  331. package/src/runtime/domain/subagents/child-process-list.domain.mjs +7 -0
  332. package/src/runtime/domain/subagents/child-process-list.ts +8 -0
  333. package/src/runtime/domain/subagents/child-runtime.mjs +67 -0
  334. package/src/runtime/domain/subagents/child-steer.domain.mjs +109 -0
  335. package/src/runtime/domain/subagents/child-steer.ts +102 -0
  336. package/src/runtime/domain/subagents/cleanup.domain.mjs +818 -0
  337. package/src/runtime/domain/subagents/cleanup.ts +1017 -0
  338. package/src/runtime/domain/subagents/codex-import.domain.mjs +575 -0
  339. package/src/runtime/domain/subagents/codex-import.ts +717 -0
  340. package/src/runtime/domain/subagents/completion.domain.mjs +6 -0
  341. package/src/runtime/domain/subagents/completion.mjs +152 -0
  342. package/src/runtime/domain/subagents/completion.ts +11 -0
  343. package/src/runtime/domain/subagents/config.domain.mjs +248 -0
  344. package/src/runtime/domain/subagents/config.json +15 -0
  345. package/src/runtime/domain/subagents/config.ts +328 -0
  346. package/src/runtime/domain/subagents/continuation-authority.domain.mjs +38 -0
  347. package/src/runtime/domain/subagents/continuation-authority.ts +31 -0
  348. package/src/runtime/domain/subagents/continuation-runtime.domain.mjs +89 -0
  349. package/src/runtime/domain/subagents/continuation-runtime.ts +88 -0
  350. package/src/runtime/domain/subagents/continuation.domain.mjs +942 -0
  351. package/src/runtime/domain/subagents/continuation.ts +1051 -0
  352. package/src/runtime/domain/subagents/delegation.domain.mjs +19 -0
  353. package/src/runtime/domain/subagents/delegation.ts +24 -0
  354. package/src/runtime/domain/subagents/effective-settings.domain.mjs +130 -0
  355. package/src/runtime/domain/subagents/effective-settings.ts +184 -0
  356. package/src/runtime/domain/subagents/extensions.domain.mjs +6 -0
  357. package/src/runtime/domain/subagents/extensions.mjs +164 -0
  358. package/src/runtime/domain/subagents/extensions.ts +19 -0
  359. package/src/runtime/domain/subagents/failures.domain.mjs +362 -0
  360. package/src/runtime/domain/subagents/failures.ts +319 -0
  361. package/src/runtime/domain/subagents/finalization.domain.mjs +168 -0
  362. package/src/runtime/domain/subagents/finalization.ts +218 -0
  363. package/src/runtime/domain/subagents/git-remotes.domain.mjs +351 -0
  364. package/src/runtime/domain/subagents/git-remotes.ts +413 -0
  365. package/src/runtime/domain/subagents/git-workspace.domain.mjs +337 -0
  366. package/src/runtime/domain/subagents/git-workspace.ts +430 -0
  367. package/src/runtime/domain/subagents/goal-provider.domain.mjs +125 -0
  368. package/src/runtime/domain/subagents/goal-provider.ts +132 -0
  369. package/src/runtime/domain/subagents/health-observation.domain.mjs +617 -0
  370. package/src/runtime/domain/subagents/health-observation.ts +818 -0
  371. package/src/runtime/domain/subagents/health-surface.mjs +284 -0
  372. package/src/runtime/domain/subagents/health.domain.mjs +261 -0
  373. package/src/runtime/domain/subagents/health.ts +343 -0
  374. package/src/runtime/domain/subagents/historical-attention.domain.mjs +37 -0
  375. package/src/runtime/domain/subagents/historical-attention.ts +35 -0
  376. package/src/runtime/domain/subagents/incident-model.domain.mjs +322 -0
  377. package/src/runtime/domain/subagents/incident-model.ts +358 -0
  378. package/src/runtime/domain/subagents/index.domain.mjs +4731 -0
  379. package/src/runtime/domain/subagents/index.ts +4781 -0
  380. package/src/runtime/domain/subagents/launch-abort.domain.mjs +13 -0
  381. package/src/runtime/domain/subagents/launch-abort.ts +21 -0
  382. package/src/runtime/domain/subagents/launch-context.domain.mjs +24 -0
  383. package/src/runtime/domain/subagents/launch-context.ts +23 -0
  384. package/src/runtime/domain/subagents/launch-digest.mjs +6 -0
  385. package/src/runtime/domain/subagents/lifecycle.domain.mjs +265 -0
  386. package/src/runtime/domain/subagents/lifecycle.ts +360 -0
  387. package/src/runtime/domain/subagents/list.domain.mjs +2 -0
  388. package/src/runtime/domain/subagents/list.mjs +152 -0
  389. package/src/runtime/domain/subagents/list.ts +19 -0
  390. package/src/runtime/domain/subagents/log-cursor.domain.mjs +217 -0
  391. package/src/runtime/domain/subagents/log-cursor.ts +268 -0
  392. package/src/runtime/domain/subagents/managed-background-capability.domain.mjs +10 -0
  393. package/src/runtime/domain/subagents/managed-background-capability.ts +9 -0
  394. package/src/runtime/domain/subagents/managed-failure-worker.mjs +36 -0
  395. package/src/runtime/domain/subagents/managed-launch-bootstrap.mjs +48 -0
  396. package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +75 -0
  397. package/src/runtime/domain/subagents/managed-launch-client.ts +59 -0
  398. package/src/runtime/domain/subagents/managed-launch-host.domain.mjs +627 -0
  399. package/src/runtime/domain/subagents/managed-launch-host.ts +504 -0
  400. package/src/runtime/domain/subagents/managed-launch-journal.mjs +17 -0
  401. package/src/runtime/domain/subagents/managed-launch-ownership.domain.mjs +29 -0
  402. package/src/runtime/domain/subagents/managed-launch-ownership.ts +22 -0
  403. package/src/runtime/domain/subagents/managed-launch-port.domain.mjs +2 -0
  404. package/src/runtime/domain/subagents/managed-launch-port.ts +25 -0
  405. package/src/runtime/domain/subagents/managed-launch-recovery.domain.mjs +29 -0
  406. package/src/runtime/domain/subagents/managed-launch-recovery.ts +45 -0
  407. package/src/runtime/domain/subagents/managed-worker-bridge.domain.mjs +63 -0
  408. package/src/runtime/domain/subagents/managed-worker-bridge.ts +48 -0
  409. package/src/runtime/domain/subagents/managed-worker.mjs +13 -0
  410. package/src/runtime/domain/subagents/model-resolution-core.domain.mjs +822 -0
  411. package/src/runtime/domain/subagents/model-resolution-core.ts +1074 -0
  412. package/src/runtime/domain/subagents/model-resolution.domain.mjs +22 -0
  413. package/src/runtime/domain/subagents/model-resolution.ts +32 -0
  414. package/src/runtime/domain/subagents/navigator.domain.mjs +6 -0
  415. package/src/runtime/domain/subagents/navigator.mjs +1233 -0
  416. package/src/runtime/domain/subagents/navigator.ts +39 -0
  417. package/src/runtime/domain/subagents/operation-bridge-types.domain.mjs +1012 -0
  418. package/src/runtime/domain/subagents/operation-bridge-types.ts +1156 -0
  419. package/src/runtime/domain/subagents/outage-observations.domain.mjs +84 -0
  420. package/src/runtime/domain/subagents/outage-observations.ts +73 -0
  421. package/src/runtime/domain/subagents/output-payload.domain.mjs +767 -0
  422. package/src/runtime/domain/subagents/output-payload.ts +987 -0
  423. package/src/runtime/domain/subagents/owned-exit.domain.mjs +12 -0
  424. package/src/runtime/domain/subagents/owned-exit.ts +12 -0
  425. package/src/runtime/domain/subagents/package.json +86 -0
  426. package/src/runtime/domain/subagents/park-checkpoint.domain.mjs +55 -0
  427. package/src/runtime/domain/subagents/park-checkpoint.ts +61 -0
  428. package/src/runtime/domain/subagents/parse.domain.mjs +1218 -0
  429. package/src/runtime/domain/subagents/parse.ts +1451 -0
  430. package/src/runtime/domain/subagents/permission-policy.domain.mjs +232 -0
  431. package/src/runtime/domain/subagents/permission-policy.ts +291 -0
  432. package/src/runtime/domain/subagents/process-identity.mjs +33 -0
  433. package/src/runtime/domain/subagents/registry.domain.mjs +1584 -0
  434. package/src/runtime/domain/subagents/registry.ts +1904 -0
  435. package/src/runtime/domain/subagents/role-assignment.domain.mjs +104 -0
  436. package/src/runtime/domain/subagents/role-assignment.ts +141 -0
  437. package/src/runtime/domain/subagents/roles/role.architect.md +13 -0
  438. package/src/runtime/domain/subagents/roles/role.developer.md +16 -0
  439. package/src/runtime/domain/subagents/roles/role.explorer.md +13 -0
  440. package/src/runtime/domain/subagents/roles/role.product-manager.md +13 -0
  441. package/src/runtime/domain/subagents/roles/role.researcher.md +13 -0
  442. package/src/runtime/domain/subagents/roles/role.reviewer.md +13 -0
  443. package/src/runtime/domain/subagents/roles/role.writer.md +12 -0
  444. package/src/runtime/domain/subagents/sandbox.domain.mjs +64 -0
  445. package/src/runtime/domain/subagents/sandbox.ts +92 -0
  446. package/src/runtime/domain/subagents/session-selection.domain.mjs +32 -0
  447. package/src/runtime/domain/subagents/session-selection.ts +35 -0
  448. package/src/runtime/domain/subagents/session-store.domain.mjs +489 -0
  449. package/src/runtime/domain/subagents/session-store.ts +468 -0
  450. package/src/runtime/domain/subagents/settings-page.domain.mjs +313 -0
  451. package/src/runtime/domain/subagents/settings-page.ts +363 -0
  452. package/src/runtime/domain/subagents/shared-callback-batcher.domain.mjs +777 -0
  453. package/src/runtime/domain/subagents/shared-callback-batcher.ts +956 -0
  454. package/src/runtime/domain/subagents/shared-catalog.domain.mjs +70 -0
  455. package/src/runtime/domain/subagents/shared-catalog.ts +61 -0
  456. package/src/runtime/domain/subagents/shared-execution-policy.domain.mjs +50 -0
  457. package/src/runtime/domain/subagents/shared-execution-policy.ts +61 -0
  458. package/src/runtime/domain/subagents/shared-failure-observations.domain.mjs +1078 -0
  459. package/src/runtime/domain/subagents/shared-failure-observations.ts +1166 -0
  460. package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +126 -0
  461. package/src/runtime/domain/subagents/shared-harness-settings.ts +116 -0
  462. package/src/runtime/domain/subagents/shared-log-utils.domain.mjs +1223 -0
  463. package/src/runtime/domain/subagents/shared-log-utils.ts +1492 -0
  464. package/src/runtime/domain/subagents/shared-navigator-panels.domain.mjs +289 -0
  465. package/src/runtime/domain/subagents/shared-navigator-panels.ts +272 -0
  466. package/src/runtime/domain/subagents/shared-navigator.domain.mjs +1930 -0
  467. package/src/runtime/domain/subagents/shared-navigator.ts +1962 -0
  468. package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-inbox.mjs +144 -0
  469. package/src/runtime/domain/subagents/shared-operation-coordinator/assignment-observation.mjs +189 -0
  470. package/src/runtime/domain/subagents/shared-operation-coordinator/index.domain.mjs +51 -0
  471. package/src/runtime/domain/subagents/shared-operation-coordinator/index.ts +122 -0
  472. package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.domain.mjs +75 -0
  473. package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-changes.ts +73 -0
  474. package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.domain.mjs +163 -0
  475. package/src/runtime/domain/subagents/shared-operation-coordinator/src/assignment-outcomes.ts +174 -0
  476. package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.domain.mjs +66 -0
  477. package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
  478. package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.domain.mjs +87 -0
  479. package/src/runtime/domain/subagents/shared-operation-coordinator/src/background-requests.ts +90 -0
  480. package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.domain.mjs +97 -0
  481. package/src/runtime/domain/subagents/shared-operation-coordinator/src/discovery.ts +139 -0
  482. package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.domain.mjs +12 -0
  483. package/src/runtime/domain/subagents/shared-operation-coordinator/src/errors.ts +79 -0
  484. package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.domain.mjs +342 -0
  485. package/src/runtime/domain/subagents/shared-operation-coordinator/src/operations.ts +513 -0
  486. package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +283 -0
  487. package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +458 -0
  488. package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.domain.mjs +52 -0
  489. package/src/runtime/domain/subagents/shared-operation-coordinator/src/recovery.ts +55 -0
  490. package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.domain.mjs +114 -0
  491. package/src/runtime/domain/subagents/shared-operation-coordinator/src/result-supervision.ts +140 -0
  492. package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.domain.mjs +280 -0
  493. package/src/runtime/domain/subagents/shared-operation-coordinator/src/rows.ts +442 -0
  494. package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.domain.mjs +420 -0
  495. package/src/runtime/domain/subagents/shared-operation-coordinator/src/schema.ts +441 -0
  496. package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.domain.mjs +194 -0
  497. package/src/runtime/domain/subagents/shared-operation-coordinator/src/sqlite.ts +239 -0
  498. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +275 -0
  499. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +585 -0
  500. package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.domain.mjs +120 -0
  501. package/src/runtime/domain/subagents/shared-operation-coordinator/src/subagent-launch.ts +142 -0
  502. package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.domain.mjs +188 -0
  503. package/src/runtime/domain/subagents/shared-operation-coordinator/src/task-cancellation.ts +224 -0
  504. package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.domain.mjs +25 -0
  505. package/src/runtime/domain/subagents/shared-operation-coordinator/src/types.ts +707 -0
  506. package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.domain.mjs +22 -0
  507. package/src/runtime/domain/subagents/shared-operation-coordinator/src/upgrade.ts +22 -0
  508. package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.domain.mjs +234 -0
  509. package/src/runtime/domain/subagents/shared-operation-coordinator/src/validate.ts +241 -0
  510. package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.domain.mjs +851 -0
  511. package/src/runtime/domain/subagents/shared-operation-coordinator/src/waits.ts +1424 -0
  512. package/src/runtime/domain/subagents/shared-permission-blocker.domain.mjs +40 -0
  513. package/src/runtime/domain/subagents/shared-permission-blocker.ts +48 -0
  514. package/src/runtime/domain/subagents/shared-render-scheduler.domain.mjs +41 -0
  515. package/src/runtime/domain/subagents/shared-render-scheduler.ts +47 -0
  516. package/src/runtime/domain/subagents/shared-sandbox-core.domain.mjs +1531 -0
  517. package/src/runtime/domain/subagents/shared-sandbox-core.ts +1757 -0
  518. package/src/runtime/domain/subagents/shared-sandbox-diagnostics.domain.mjs +591 -0
  519. package/src/runtime/domain/subagents/shared-sandbox-diagnostics.ts +491 -0
  520. package/src/runtime/domain/subagents/shared-stall-detector.domain.mjs +37 -0
  521. package/src/runtime/domain/subagents/shared-stall-detector.ts +69 -0
  522. package/src/runtime/domain/subagents/shared-task-application.domain.mjs +185 -0
  523. package/src/runtime/domain/subagents/shared-task-application.ts +185 -0
  524. package/src/runtime/domain/subagents/shared-task-apply-patch.domain.mjs +416 -0
  525. package/src/runtime/domain/subagents/shared-task-apply-patch.ts +409 -0
  526. package/src/runtime/domain/subagents/shared-task-exec.domain.mjs +325 -0
  527. package/src/runtime/domain/subagents/shared-task-exec.ts +335 -0
  528. package/src/runtime/domain/subagents/shared-task-files.domain.mjs +346 -0
  529. package/src/runtime/domain/subagents/shared-task-files.ts +342 -0
  530. package/src/runtime/domain/subagents/shared-task-process-identity.domain.mjs +140 -0
  531. package/src/runtime/domain/subagents/shared-task-process-identity.ts +126 -0
  532. package/src/runtime/domain/subagents/shared-task-process-list.domain.mjs +220 -0
  533. package/src/runtime/domain/subagents/shared-task-process-list.ts +209 -0
  534. package/src/runtime/domain/subagents/shared-task-sandbox.domain.mjs +227 -0
  535. package/src/runtime/domain/subagents/shared-task-sandbox.ts +224 -0
  536. package/src/runtime/domain/subagents/shared-task-tools.domain.mjs +92 -0
  537. package/src/runtime/domain/subagents/shared-task-tools.ts +123 -0
  538. package/src/runtime/domain/subagents/shared-work-client.mjs +303 -0
  539. package/src/runtime/domain/subagents/shared-worktree-context.domain.mjs +142 -0
  540. package/src/runtime/domain/subagents/shared-worktree-context.ts +158 -0
  541. package/src/runtime/domain/subagents/skill-picker.domain.mjs +316 -0
  542. package/src/runtime/domain/subagents/skill-picker.ts +353 -0
  543. package/src/runtime/domain/subagents/skills.domain.mjs +138 -0
  544. package/src/runtime/domain/subagents/skills.ts +183 -0
  545. package/src/runtime/domain/subagents/spawn.domain.mjs +72 -0
  546. package/src/runtime/domain/subagents/spawn.ts +80 -0
  547. package/src/runtime/domain/subagents/stop.domain.mjs +184 -0
  548. package/src/runtime/domain/subagents/stop.ts +217 -0
  549. package/src/runtime/domain/subagents/subagent-tools.domain.mjs +101 -0
  550. package/src/runtime/domain/subagents/subagent-tools.ts +137 -0
  551. package/src/runtime/domain/subagents/task-guard.domain.mjs +163 -0
  552. package/src/runtime/domain/subagents/task-guard.ts +144 -0
  553. package/src/runtime/domain/subagents/task-policy.domain.mjs +201 -0
  554. package/src/runtime/domain/subagents/task-policy.ts +236 -0
  555. package/src/runtime/domain/subagents/task-runtime.mjs +53 -0
  556. package/src/runtime/domain/subagents/task-tool-owner.domain.mjs +18 -0
  557. package/src/runtime/domain/subagents/task-tool-owner.ts +21 -0
  558. package/src/runtime/domain/subagents/thinking.domain.mjs +22 -0
  559. package/src/runtime/domain/subagents/thinking.ts +28 -0
  560. package/src/runtime/domain/subagents/tier-policy.domain.mjs +75 -0
  561. package/src/runtime/domain/subagents/tier-policy.ts +126 -0
  562. package/src/runtime/domain/subagents/timing-notifications.domain.mjs +51 -0
  563. package/src/runtime/domain/subagents/timing-notifications.ts +37 -0
  564. package/src/runtime/domain/subagents/timing.domain.mjs +487 -0
  565. package/src/runtime/domain/subagents/timing.ts +634 -0
  566. package/src/runtime/domain/subagents/tools.domain.mjs +558 -0
  567. package/src/runtime/domain/subagents/tools.ts +629 -0
  568. package/src/runtime/domain/subagents/widget.domain.mjs +6 -0
  569. package/src/runtime/domain/subagents/widget.mjs +246 -0
  570. package/src/runtime/domain/subagents/widget.ts +30 -0
  571. package/src/runtime/domain/subagents/work-observation.domain.mjs +316 -0
  572. package/src/runtime/domain/subagents/work-observation.ts +248 -0
  573. package/src/runtime/domain/subagents/worker-catalog.domain.mjs +120 -0
  574. package/src/runtime/domain/subagents/worker-catalog.ts +162 -0
  575. package/src/runtime/domain/subagents/worker-operations-host.domain.mjs +1009 -0
  576. package/src/runtime/domain/subagents/worker-operations-host.ts +1238 -0
  577. package/src/runtime/domain/subagents/worker-waits.domain.mjs +2336 -0
  578. package/src/runtime/domain/subagents/worker-waits.ts +2878 -0
  579. package/src/runtime/domain/subagents/workspace-scope.domain.mjs +52 -0
  580. package/src/runtime/domain/subagents/workspace-scope.ts +50 -0
  581. package/src/runtime/health-config.ts +27 -11
  582. package/src/runtime/log-checkpoints.ts +117 -0
  583. package/src/runtime/mutation-store.ts +5 -4
  584. package/src/runtime/queries.ts +27 -3
  585. package/src/runtime/query-executor.ts +178 -34
  586. package/src/runtime/query-worker.mjs +119 -22
  587. package/src/runtime/read-model.ts +939 -113
  588. package/src/runtime/read-watchdog-probe.ts +4 -0
  589. package/src/runtime/result-supervision.ts +188 -16
  590. package/src/runtime/source-watch.ts +175 -0
  591. package/src/runtime/subagent-launch.ts +73 -0
  592. package/src/runtime/work-budget.ts +130 -0
  593. package/src/runtime/work-client.d.mts +8 -0
  594. package/src/runtime/work-client.mjs +150 -7
  595. package/src/server.ts +185 -22
  596. package/src/session-activity.ts +26 -4
  597. package/src/version.ts +1 -1
@@ -0,0 +1,532 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, writeSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
4
+ import type { ChildProcess } from "node:child_process";
5
+ import type { CommandResult, CommandSpec, StepGateFailure } from "./types.js";
6
+
7
+ /** Known Git for Windows locations; `bash -lc` needs a real bash, not the WSL shim. */
8
+ const WINDOWS_BASH_CANDIDATES = [
9
+ "C:\\Program Files\\Git\\bin\\bash.exe",
10
+ "C:\\Program Files (x86)\\Git\\bin\\bash.exe",
11
+ "C:\\Program Files\\Git\\usr\\bin\\bash.exe",
12
+ ];
13
+
14
+ /**
15
+ * Resolve the shell used for `command` specs.
16
+ *
17
+ * POSIX keeps `/bin/bash`. Windows has no `/bin/bash`, and the `bash.exe` found
18
+ * on PATH is usually the WSL launcher in System32 or the WindowsApps alias,
19
+ * either of which would run the command inside WSL instead of Windows. Prefer
20
+ * an explicit override, then Git for Windows, then a non-WSL `bash.exe` on PATH.
21
+ *
22
+ * Resolved lazily at spawn time, not module load: env-injection extensions
23
+ * (e.g. pi-env) may apply settings.json `env` values after this module is
24
+ * evaluated, and those overrides must still take effect.
25
+ *
26
+ * Exposed for tests and reuse.
27
+ */
28
+ export function resolveDefaultShell(): string {
29
+ const fromEnv = process.env.PI_BETTER_BACKGROUND_TASKS_SHELL;
30
+ if (fromEnv) return fromEnv;
31
+ if (process.platform !== "win32") return "/bin/bash";
32
+ for (const candidate of WINDOWS_BASH_CANDIDATES) {
33
+ if (existsSync(candidate)) return candidate;
34
+ }
35
+ for (const dir of (process.env.PATH ?? "").split(";")) {
36
+ const trimmed = dir.trim();
37
+ if (!trimmed || /(^|[\\/])(system32|windowsapps)([\\/]|$)/i.test(trimmed)) continue;
38
+ const candidate = join(trimmed, "bash.exe");
39
+ if (existsSync(candidate)) return candidate;
40
+ }
41
+ // Nothing usable found: keep the POSIX default so the failure surfaces as a
42
+ // logged spawn error for the task instead of crashing the whole host process.
43
+ return "/bin/bash";
44
+ }
45
+
46
+ /** How long a timed-out process group has to exit on SIGTERM before SIGKILL. */
47
+ const TERMINATION_GRACE_MS = 2_000;
48
+
49
+ export interface SpawnedProcess {
50
+ child: ChildProcess;
51
+ pgid?: number;
52
+ }
53
+
54
+ export function validateCommandSpec(spec: CommandSpec): void {
55
+ if (spec.shell === false) {
56
+ if (!spec.argv || spec.argv.length === 0 || !spec.argv[0]) {
57
+ throw new Error("argv with at least one element is required when shell:false");
58
+ }
59
+ return;
60
+ }
61
+ if (!spec.command || spec.command.trim().length === 0) {
62
+ throw new Error("command is required unless shell:false with argv is provided");
63
+ }
64
+ }
65
+
66
+ /** Convert a Windows path to the `/c/...` form MSYS bash resolves in redirections. Exposed for tests and reuse. */
67
+ export function toMsysPath(path: string): string {
68
+ const forward = path.replace(/\\/g, "/");
69
+ const [, drive, rest] = /^([A-Za-z]):(\/.+)$/.exec(forward) ?? [];
70
+ return drive && rest ? `/${drive.toLowerCase()}${rest}` : forward;
71
+ }
72
+
73
+ /** Single-quote a value for safe literal use in a bash script line. Exposed for tests and reuse. */
74
+ export function bashSingleQuote(value: string): string {
75
+ return `'${value.replace(/'/g, "'\\''")}'`;
76
+ }
77
+
78
+ /**
79
+ * Env var carrying the operator's own command into a gated launch (#619).
80
+ *
81
+ * The gate script `eval`s this value instead of concatenating it, so a command
82
+ * containing quotes, heredocs, or multiple lines is parsed exactly as bash would
83
+ * parse it alone. The value is also kept out of the spawned argv.
84
+ */
85
+ export const STEP_GATE_COMMAND_ENV = "PI_BG_STEP_GATE_COMMAND";
86
+
87
+ /** Longest failing-step text recorded in task metadata; the log keeps it whole. */
88
+ export const STEP_GATE_COMMAND_CHARS = 160;
89
+
90
+ /**
91
+ * Bash script that runs an opt-in step gate around the operator's command (#619).
92
+ *
93
+ * The gate is a bash `errexit` contract, not shell parsing: `set -e` ends the
94
+ * command at the first unhandled failing step with that step's exit code, so a
95
+ * later command — in the same list, a function, a subshell, or a command
96
+ * substitution — can no longer turn a red gate green. A failure the command
97
+ * handles itself (`|| true`, `if`/`while`/`until` conditions, `!`, `&&`/`||`
98
+ * lists) is not a gate failure, and a discarded process-substitution status is
99
+ * not one either. `set -E` lets the ERR trap name a failure inside a shell
100
+ * function (functions run in the main shell); the trap records only main-shell
101
+ * failures (`BASH_SUBSHELL` 0), so a status bash itself discards is never claimed
102
+ * as the verdict — an unhandled command substitution still fails its assignment
103
+ * in the main shell. The EXIT trap writes the verdict to stderr, where the
104
+ * harness reads it back, and forces a failing step's code when the command
105
+ * disabled errexit and would have exited 0.
106
+ *
107
+ * Exposed for tests and reuse.
108
+ */
109
+ export function stepGateScript(id: string): string {
110
+ return [
111
+ "# Step gate (#619) from pi-better-background-tasks: bash errexit ends the command at its first unhandled failing step.",
112
+ "set -E",
113
+ "set -e",
114
+ `__pi_step_gate_id=${bashSingleQuote(id)}`,
115
+ "__pi_step_gate_first=0",
116
+ "__pi_step_gate_command=",
117
+ "__pi_step_gate_record() {",
118
+ " __pi_step_gate_status=$?",
119
+ " if [ \"${BASH_SUBSHELL:-0}\" -eq 0 ] && [ \"${__pi_step_gate_first:-0}\" -eq 0 ] && [ \"$__pi_step_gate_status\" -ne 0 ]; then",
120
+ " __pi_step_gate_first=$__pi_step_gate_status",
121
+ " __pi_step_gate_command=${BASH_COMMAND:-}",
122
+ " fi",
123
+ " return 0",
124
+ "}",
125
+ "__pi_step_gate_finish() {",
126
+ " __pi_step_gate_status=$?",
127
+ " if [ \"${__pi_step_gate_first:-0}\" -ne 0 ]; then",
128
+ " __pi_step_gate_text=${__pi_step_gate_command//$'\\n'/ }",
129
+ " printf '\\n--- step gate [%s]: first failing step exit=%s; shell exit=%s: %s ---\\n' \"$__pi_step_gate_id\" \"$__pi_step_gate_first\" \"$__pi_step_gate_status\" \"$__pi_step_gate_text\" >&2",
130
+ " if [ \"$__pi_step_gate_status\" -eq 0 ]; then exit \"$__pi_step_gate_first\"; fi",
131
+ " fi",
132
+ " exit \"$__pi_step_gate_status\"",
133
+ "}",
134
+ "trap '__pi_step_gate_record' ERR",
135
+ "trap '__pi_step_gate_finish' EXIT",
136
+ `if [ -z "\${${STEP_GATE_COMMAND_ENV}:-}" ]; then`,
137
+ " printf '\\n--- step gate [%s]: the gated command was not provided; nothing ran ---\\n' \"$__pi_step_gate_id\" >&2",
138
+ " exit 125",
139
+ "fi",
140
+ `eval "\${${STEP_GATE_COMMAND_ENV}}"`,
141
+ ].join("\n");
142
+ }
143
+
144
+ /**
145
+ * Read the gate's verdict marker from a task log (#619). Bash writes it as the last
146
+ * line of stderr, so log retention cannot turn a red gate green; a missing marker
147
+ * only costs the failing command's name, never the verdict carried by the exit code.
148
+ *
149
+ * Exposed for tests and reuse.
150
+ */
151
+ export function parseStepGateFailure(logText: string, id: string): StepGateFailure | undefined {
152
+ const marker = `--- step gate [${id}]: first failing step exit=`;
153
+ let found: StepGateFailure | undefined;
154
+ for (const rawLine of logText.split("\n")) {
155
+ const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
156
+ if (!line.startsWith(marker) || !line.endsWith(" ---")) continue;
157
+ const match = /^--- step gate \[[^\]]+\]: first failing step exit=(-?\d+); shell exit=(-?\d+): (.*) ---$/.exec(line);
158
+ if (!match || match[3] === undefined) continue;
159
+ const stepExitCode = Number(match[1]);
160
+ const shellExitCode = Number(match[2]);
161
+ if (!Number.isInteger(stepExitCode) || stepExitCode === 0) continue;
162
+ // The marker is the newest evidence, and the shell writes only the first failing step.
163
+ found = {
164
+ stepExitCode,
165
+ shellExitCode: Number.isInteger(shellExitCode) ? shellExitCode : null,
166
+ command: boundedStepCommand(match[3]),
167
+ };
168
+ }
169
+ return found;
170
+ }
171
+
172
+ function boundedStepCommand(value: string): string {
173
+ const oneLine = value.replace(/\s+/g, " ").trim();
174
+ return oneLine.length > STEP_GATE_COMMAND_CHARS ? `${oneLine.slice(0, STEP_GATE_COMMAND_CHARS - 1)}…` : oneLine;
175
+ }
176
+
177
+ /**
178
+ * On Windows, numeric fds above 2 are unusable as child stdio: Node spawns the
179
+ * process, but its output handles end up broken, every write fails, and shell
180
+ * tasks exit 1 having produced nothing. (POSIX inherits the fd normally.)
181
+ *
182
+ * Instead of handing the child a log fd, the shell opens and redirects into the
183
+ * log itself. Output stays durable — written by the detached task directly, so
184
+ * logging continues after pi exits — and the child runs with no inherited
185
+ * stdio. Raw argv specs get a bash trampoline (`exec`) that performs the same
186
+ * redirect before replacing itself with the target program.
187
+ *
188
+ * Exposed for tests and reuse.
189
+ */
190
+ export function withWindowsLogRedirect(spec: CommandSpec, logPath: string): CommandSpec {
191
+ const redirectLine = `exec >> ${bashSingleQuote(toMsysPath(logPath))} 2>&1`;
192
+ if (spec.shell === false) {
193
+ const argvText = spec.argv!.map((arg) => bashSingleQuote(String(arg))).join(" ");
194
+ return {
195
+ ...spec,
196
+ shell: true,
197
+ // The trampoline only redirects and execs an absolute program; sourcing
198
+ // the login profile (/etc/profile and friends) adds seconds of cold-start
199
+ // cost on a Windows runner and nothing the exec needs.
200
+ loginShell: false,
201
+ // The MSYS2 runtime rewrites POSIX-looking argv (e.g. `/c`, `/opt/x.sh`)
202
+ // when exec'ing native Windows binaries. Node spawn passed argv verbatim,
203
+ // so conversion is disabled to keep raw-argv semantics unchanged. The
204
+ // redirect target is unaffected: bash resolves it itself, already in
205
+ // `/c/...` form.
206
+ command: `${redirectLine}\nexport MSYS2_ARG_CONV_EXCL='*'\nexec ${argvText}`,
207
+ };
208
+ }
209
+ return { ...spec, command: `${redirectLine}\n${spec.command}` };
210
+ }
211
+
212
+ export function spawnCommand(spec: CommandSpec, logPath: string, detached: boolean): SpawnedProcess {
213
+ validateCommandSpec(spec);
214
+ mkdirSync(dirname(logPath), { recursive: true });
215
+ const windows = process.platform === "win32";
216
+ const launchSpec = windows ? withWindowsLogRedirect(spec, logPath) : spec;
217
+ let fd: number | undefined;
218
+ let stdio: SpawnStdio;
219
+ if (windows) {
220
+ stdio = ["ignore", "ignore", "ignore"];
221
+ } else {
222
+ fd = openSync(logPath, "a");
223
+ stdio = ["ignore", fd, fd];
224
+ }
225
+ const child = spawnArgs(launchSpec, detached, stdio);
226
+ const marker = `\n--- spawn ${new Date().toISOString()} pid=${child.pid ?? "unknown"} ---\n`;
227
+ try {
228
+ if (fd !== undefined) {
229
+ writeSync(fd, marker);
230
+ } else {
231
+ // The detached child is already running; a throw here would orphan it
232
+ // with no task metadata, so the marker write is best effort.
233
+ appendFileSync(logPath, marker);
234
+ }
235
+ } catch {
236
+ // Log unavailable; the runtime close handler still records the failure.
237
+ } finally {
238
+ if (fd !== undefined) {
239
+ try { closeSync(fd); } catch { /* best effort */ }
240
+ }
241
+ }
242
+ // Spawn failures (ENOENT, bad cwd, permission denied) surface as an 'error'
243
+ // event. With no listener Node turns it into an uncaughtException that takes
244
+ // down the whole host process; log it here and let the runtime's 'close'
245
+ // handler finalize the task as failed.
246
+ child.on("error", (error) => {
247
+ try {
248
+ const code = (error as NodeJS.ErrnoException).code ?? "unknown";
249
+ appendFileSync(logPath, `\n--- spawn error ${new Date().toISOString()} code=${code} message=${error.message} ---\n`);
250
+ } catch {
251
+ // Log unavailable; the runtime close handler still records the failure.
252
+ }
253
+ });
254
+ child.on("close", (code, signal) => {
255
+ try {
256
+ appendFileSync(logPath, `\n--- exit ${new Date().toISOString()} code=${code ?? "null"} signal=${signal ?? "null"} ---\n`);
257
+ } catch {
258
+ // Log unavailable (swept tmp dir, ACL change): a throw here would crash
259
+ // the host; the runtime already finalized the task from meta.
260
+ }
261
+ });
262
+ return { child, pgid: detached && child.pid ? child.pid : undefined };
263
+ }
264
+
265
+ /**
266
+ * Run one command to completion and collect its output.
267
+ *
268
+ * A timeout has to reach the whole process tree, not just the process spawned
269
+ * here. Two things make that necessary rather than tidy:
270
+ *
271
+ * - The command may not be what runs. Under the Linux write sandbox the spawned
272
+ * process is `bwrap`, which forks the real command instead of exec'ing it, so
273
+ * a signal to the spawned pid never reaches the command at all. (macOS
274
+ * `sandbox-exec` execs its target, which is why the same signal works there.)
275
+ * - `close` fires when the output pipes close, not when the child exits. A
276
+ * surviving grandchild keeps holding them, so signalling only the direct child
277
+ * leaves this promise pending indefinitely and the caller's deadline unenforced.
278
+ *
279
+ * So the child leads its own process group, and a timeout signals that group.
280
+ */
281
+ export function runCommandOnce(
282
+ spec: CommandSpec,
283
+ maxBufferBytes = 1024 * 1024,
284
+ timeoutMs?: number,
285
+ ): Promise<CommandResult> {
286
+ validateCommandSpec(spec);
287
+ const startedAt = Date.now();
288
+ const child = spawnArgs(spec, true, ["ignore", "pipe", "pipe"]);
289
+ const cap = Math.max(1, Math.floor(maxBufferBytes));
290
+ const stdoutCapture = createCaptureBuffer();
291
+ const stderrCapture = createCaptureBuffer();
292
+ let timedOut = false;
293
+ child.stdout?.on("data", (chunk: Buffer | string) => {
294
+ captureChunk(stdoutCapture, chunk, cap);
295
+ });
296
+ child.stderr?.on("data", (chunk: Buffer | string) => {
297
+ captureChunk(stderrCapture, chunk, cap);
298
+ });
299
+ return new Promise((resolve, reject) => {
300
+ // The group outlives the leader as long as any member is alive, so the
301
+ // kernel keeps the id reserved and this stays addressable after the spawned
302
+ // process itself has been reaped.
303
+ const signalGroup = (signal: NodeJS.Signals): void => {
304
+ if (child.pid === undefined) return;
305
+ try {
306
+ stopProcessGroup(child.pid, undefined, signal);
307
+ } catch (error) {
308
+ if (process.platform === "win32") throw error;
309
+ // Nothing left in the group: the tree is already gone.
310
+ }
311
+ };
312
+ let escalation: NodeJS.Timeout | undefined;
313
+ const timeout = timeoutMs === undefined ? undefined : setTimeout(() => {
314
+ timedOut = true;
315
+ try {
316
+ signalGroup("SIGTERM");
317
+ } catch (error) {
318
+ settle();
319
+ reject(error);
320
+ return;
321
+ }
322
+ // SIGTERM is a request. Whatever still holds the output pipes after the
323
+ // grace period is exactly what would keep this promise pending, so the
324
+ // group is killed outright rather than waited on.
325
+ escalation = setTimeout(() => {
326
+ try {
327
+ signalGroup("SIGKILL");
328
+ } catch (error) {
329
+ settle();
330
+ reject(error);
331
+ }
332
+ }, TERMINATION_GRACE_MS);
333
+ escalation.unref();
334
+ }, Math.max(1, timeoutMs));
335
+ timeout?.unref();
336
+ const settle = (): void => {
337
+ if (timeout) clearTimeout(timeout);
338
+ if (escalation) clearTimeout(escalation);
339
+ };
340
+ child.on("error", (error) => {
341
+ settle();
342
+ reject(error);
343
+ });
344
+ child.on("close", (exitCode, signal) => {
345
+ settle();
346
+ const stdout = finishCapture(stdoutCapture);
347
+ const stderr = finishCapture(stderrCapture);
348
+ const captureTruncated = stdout.discardedBytes > 0 || stderr.discardedBytes > 0;
349
+ resolve({
350
+ exitCode,
351
+ signal,
352
+ stdout: stdout.text,
353
+ stderr: stderr.text,
354
+ startedAt,
355
+ endedAt: Date.now(),
356
+ ...(timedOut ? { timedOut: true } : {}),
357
+ ...(stdout.discardedBytes > 0 ? { stdoutDiscardedBytes: stdout.discardedBytes } : {}),
358
+ ...(stderr.discardedBytes > 0 ? { stderrDiscardedBytes: stderr.discardedBytes } : {}),
359
+ ...(captureTruncated ? { captureTruncated: true } : {}),
360
+ });
361
+ });
362
+ });
363
+ }
364
+
365
+ interface CaptureBuffer {
366
+ chunks: Buffer[];
367
+ storedBytes: number;
368
+ discardedBytes: number;
369
+ truncated: boolean;
370
+ }
371
+
372
+ function createCaptureBuffer(): CaptureBuffer {
373
+ return { chunks: [], storedBytes: 0, discardedBytes: 0, truncated: false };
374
+ }
375
+
376
+ function asBuffer(chunk: Buffer | string): Buffer {
377
+ return Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
378
+ }
379
+
380
+ /** Largest UTF-8 prefix of `buffer` that fits in `maxBytes`. */
381
+ export function utf8PrefixLength(buffer: Buffer, maxBytes: number): number {
382
+ if (maxBytes <= 0) return 0;
383
+ if (buffer.length <= maxBytes) return buffer.length;
384
+ let end = maxBytes;
385
+ while (end > 0 && (buffer[end]! & 0xc0) === 0x80) end -= 1;
386
+ if (end === 0 && (buffer[0]! & 0xc0) === 0x80) return 0;
387
+ const lead = buffer[end]!;
388
+ const need = lead <= 0x7f ? 1
389
+ : (lead & 0xe0) === 0xc0 ? 2
390
+ : (lead & 0xf0) === 0xe0 ? 3
391
+ : (lead & 0xf8) === 0xf0 ? 4
392
+ : 1;
393
+ return end + need <= maxBytes ? end + need : end;
394
+ }
395
+
396
+ function captureChunk(target: CaptureBuffer, chunk: Buffer | string, maxBytes: number): void {
397
+ const buffer = asBuffer(chunk);
398
+ if (target.truncated) {
399
+ target.discardedBytes += buffer.length;
400
+ return;
401
+ }
402
+ const room = maxBytes - target.storedBytes;
403
+ if (buffer.length <= room) {
404
+ target.chunks.push(buffer);
405
+ target.storedBytes += buffer.length;
406
+ return;
407
+ }
408
+ const take = utf8PrefixLength(buffer, room);
409
+ if (take > 0) {
410
+ target.chunks.push(buffer.subarray(0, take));
411
+ target.storedBytes += take;
412
+ }
413
+ target.discardedBytes += buffer.length - take;
414
+ target.truncated = true;
415
+ }
416
+
417
+ /**
418
+ * Length of `buffer` without a trailing UTF-8 sequence that the cap cut short.
419
+ * Chunk boundaries can split a code point; once capture overflows, the bytes
420
+ * that would have completed it were discarded, so the dangling lead bytes are
421
+ * discarded too instead of decoding to U+FFFD.
422
+ */
423
+ export function completeUtf8Length(buffer: Buffer): number {
424
+ let start = buffer.length - 1;
425
+ while (start >= 0 && buffer.length - start < 4 && (buffer[start]! & 0xc0) === 0x80) start -= 1;
426
+ if (start < 0) return buffer.length;
427
+ const lead = buffer[start]!;
428
+ const need = lead <= 0x7f ? 1
429
+ : (lead & 0xe0) === 0xc0 ? 2
430
+ : (lead & 0xf0) === 0xe0 ? 3
431
+ : (lead & 0xf8) === 0xf0 ? 4
432
+ : 1;
433
+ return start + need > buffer.length ? start : buffer.length;
434
+ }
435
+
436
+ function finishCapture(target: CaptureBuffer): { text: string; discardedBytes: number } {
437
+ const stored = Buffer.concat(target.chunks, target.storedBytes);
438
+ const keep = target.truncated ? completeUtf8Length(stored) : stored.length;
439
+ return {
440
+ text: stored.subarray(0, keep).toString("utf8"),
441
+ discardedBytes: target.discardedBytes + (stored.length - keep),
442
+ };
443
+ }
444
+
445
+ /**
446
+ * Stop a task's process tree.
447
+ *
448
+ * POSIX signals the process group (`-target`), falling back to the direct pid
449
+ * when the group is already gone. Windows has no process groups in libuv, and
450
+ * `process.kill(pid)` would only terminate the spawned `bash.exe` while real
451
+ * work survives in grandchildren — so the tree is terminated with
452
+ * `taskkill /T /F` instead. Windows has no graceful signal delivery, so the
453
+ * requested signal is informational there.
454
+ */
455
+ export function stopProcessGroup(
456
+ pid: number,
457
+ pgid?: number,
458
+ signal: NodeJS.Signals = "SIGTERM",
459
+ ): void {
460
+ if (process.platform === "win32") {
461
+ const result = spawnSync("taskkill", ["/T", "/F", "/PID", String(pid)], {
462
+ encoding: "utf8",
463
+ windowsHide: true,
464
+ });
465
+ if (result.error) {
466
+ const code = (result.error as NodeJS.ErrnoException).code;
467
+ throw new Error(`taskkill could not start for PID ${pid}${code ? ` (${code})` : ""}: ${result.error.message}`, {
468
+ cause: result.error,
469
+ });
470
+ }
471
+ if (result.status === 0) return;
472
+ // A child can exit between the caller's liveness check and taskkill. That
473
+ // race is success; any still-live PID means the tree was not terminated.
474
+ if (!processExists(pid)) return;
475
+ const detail = String(result.stderr ?? result.stdout ?? "").replace(/\s+/g, " ").trim();
476
+ throw new Error(
477
+ `taskkill failed with exit ${result.status ?? "unknown"} for PID ${pid}${detail ? `: ${detail}` : ""}`,
478
+ );
479
+ }
480
+ const target = pgid ?? pid;
481
+ try {
482
+ process.kill(-target, signal);
483
+ return;
484
+ } catch {
485
+ process.kill(pid, signal);
486
+ }
487
+ }
488
+
489
+ export function processExists(pid: number): boolean {
490
+ try {
491
+ process.kill(pid, 0);
492
+ return true;
493
+ } catch {
494
+ return false;
495
+ }
496
+ }
497
+
498
+ /**
499
+ * The executable and argument vector a spec runs as.
500
+ *
501
+ * Shell specs become an explicit `bash -lc` invocation rather than a shell
502
+ * string, so every caller — spawning directly, or wrapping the same launch in an
503
+ * OS sandbox — starts from one definition of what actually executes.
504
+ */
505
+ export function commandExecution(spec: CommandSpec): { execPath: string; execArgs: string[] } {
506
+ if (spec.shell === false) {
507
+ const [command, ...args] = spec.argv!;
508
+ return { execPath: command!, execArgs: args };
509
+ }
510
+ return { execPath: resolveDefaultShell(), execArgs: [spec.loginShell === false ? "-c" : "-lc", spec.command!] };
511
+ }
512
+
513
+ /** Stdio for a spawned task: stdin is always ignored; stdout/stderr are piped (collected) or ignored (redirected into the log by the child itself). */
514
+ type SpawnStdio = ["ignore", "pipe" | "ignore" | number, "pipe" | "ignore" | number];
515
+
516
+ function spawnArgs(
517
+ spec: CommandSpec,
518
+ detached: boolean,
519
+ stdio: SpawnStdio,
520
+ ): ChildProcess {
521
+ const env = { ...process.env, ...spec.env };
522
+ const { execPath, execArgs } = commandExecution(spec);
523
+ return spawn(execPath, execArgs, {
524
+ cwd: spec.cwd,
525
+ env,
526
+ detached,
527
+ stdio,
528
+ // A detached Windows child gets its own console window unless hidden; these
529
+ // tasks write to log files and must not flash terminals.
530
+ windowsHide: process.platform === "win32",
531
+ });
532
+ }
@@ -0,0 +1,58 @@
1
+ // Generated fixed launch domain from packages/pi-better-background-tasks/src/prompt.ts. Do not edit directly.
2
+ /**
3
+ * The operational prompt this package owns: how to wait, and how to launch.
4
+ *
5
+ * Published as a named section so pi can diff it. It exists because the tool
6
+ * descriptions cannot carry it: a description explains one call, while the
7
+ * failure this prevents is choosing `sleep` over a watcher in the first place.
8
+ *
9
+ * Instructions only. No rationale, no worked examples, and no defaults that the
10
+ * tool schemas already state: the model has every schema in context, and repeated
11
+ * reference just spends prefix tokens on text it can already read.
12
+ */
13
+ export const BACKGROUND_TASKS_SECTION = "background_tasks";
14
+ import { EXECUTION_POLICY_TEXT, publishExecutionPolicy, supportsPromptSections } from "./shared-execution-policy.domain.mjs";
15
+ export const BACKGROUND_TASKS_PROMPT = [
16
+ "Background tasks: never wait in the foreground.",
17
+ "",
18
+ "- Never use foreground `sleep` to wait or poll, whether alone, before another command, or inside a loop. The sole permitted foreground sleep is at most 3 s immediately after `pkill`, `kill`, or `systemctl stop`, while the process exits.",
19
+ "- Use `bg_task_spawn` and `bg_task_watch` instead of `nohup`, `&`, or `disown`.",
20
+ "- Treat the wait, not the check, as the long part. Waiting for `gh pr checks` to pass is a `bg_task_watch`, not `sleep 300; gh pr checks`.",
21
+ "- Waiting on an external system (`gh run view`, `gh pr checks`, `mergify queue show`, a redeploy, a dev server, `cloud-init`): `bg_task_watch` with the check command and `success_when`. For a PR you just enqueued, `watch_delivery`.",
22
+ "- Waiting on your own long command (tests, build, migration): `bg_task_spawn`, then `wait_for_operations`. Do not sleep and re-read the log.",
23
+ "- Waiting for a server you just started: `bg_task_spawn` the server, then `bg_task_watch` on a readiness check (`curl -fsS ...`).",
24
+ "- Retry until a condition with one `bg_task_watch`, never `for i in ...; do ...; sleep N; done`.",
25
+ "- If a dependency is pending and no watcher is running, start `bg_task_watch`; do not loop short sleeps.",
26
+ "- Nothing here automatically diverts a foreground command. A sleep or monitor loop submitted through foreground `bash` blocks the turn.",
27
+ "- Omit `timeout_seconds` on `bg_task_spawn` unless you need a bound. Raise it on `bg_task_watch` for a wait longer than 15 minutes, and leave `callback` alone: do not poll.",
28
+ "- Write the check so a broken check is visible; do not end it with `exit 0` or `|| true`.",
29
+ "- Stop tasks you started for a turn-scoped purpose before finishing the turn.",
30
+ ].join("\n");
31
+ /** Publish the guidance into `options.sections`; assignment keeps it idempotent across turns. */
32
+ export function publishBackgroundTasksGuidance(options) {
33
+ const target = options;
34
+ const sections = (target.sections ??= {});
35
+ sections[BACKGROUND_TASKS_SECTION] = BACKGROUND_TASKS_PROMPT;
36
+ return options;
37
+ }
38
+ /**
39
+ * Deliver this package's guidance on both kinds of host.
40
+ *
41
+ * Modern pi renders custom sections, so publishing keeps the prompt structured
42
+ * and lets pi diff it. Older pi (the packages declare support back to 0.84.4)
43
+ * renders no sections at all, so publishing there would silently deliver nothing;
44
+ * returning the text is the behaviour those hosts already had.
45
+ */
46
+ export function deliverBackgroundTasksPrompt(event) {
47
+ if (!supportsPromptSections(event.systemPromptOptions)) {
48
+ // Several packages carry the same policy, so appending blindly would print it
49
+ // once per publisher. The text path stays idempotent exactly as it used to be.
50
+ const policy = event.systemPrompt.includes(EXECUTION_POLICY_TEXT) ? "" : `${EXECUTION_POLICY_TEXT}\n\n`;
51
+ const guidance = event.systemPrompt.includes(BACKGROUND_TASKS_PROMPT) ? "" : BACKGROUND_TASKS_PROMPT;
52
+ return { systemPrompt: `${event.systemPrompt}\n\n${policy}${guidance}` };
53
+ }
54
+ const options = event.systemPromptOptions;
55
+ publishExecutionPolicy(options);
56
+ publishBackgroundTasksGuidance(options);
57
+ return undefined;
58
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The operational prompt this package owns: how to wait, and how to launch.
3
+ *
4
+ * Published as a named section so pi can diff it. It exists because the tool
5
+ * descriptions cannot carry it: a description explains one call, while the
6
+ * failure this prevents is choosing `sleep` over a watcher in the first place.
7
+ *
8
+ * Instructions only. No rationale, no worked examples, and no defaults that the
9
+ * tool schemas already state: the model has every schema in context, and repeated
10
+ * reference just spends prefix tokens on text it can already read.
11
+ */
12
+ export const BACKGROUND_TASKS_SECTION = "background_tasks";
13
+
14
+ import { EXECUTION_POLICY_TEXT, publishExecutionPolicy, supportsPromptSections } from "./shared-execution-policy.js";
15
+
16
+ /** `sections` is optional because the repo typechecks against older pi typings. */
17
+ export interface PromptSectionTarget {
18
+ sections?: Record<string, string>;
19
+ }
20
+
21
+ export const BACKGROUND_TASKS_PROMPT: string = [
22
+ "Background tasks: never wait in the foreground.",
23
+ "",
24
+ "- Never use foreground `sleep` to wait or poll, whether alone, before another command, or inside a loop. The sole permitted foreground sleep is at most 3 s immediately after `pkill`, `kill`, or `systemctl stop`, while the process exits.",
25
+ "- Use `bg_task_spawn` and `bg_task_watch` instead of `nohup`, `&`, or `disown`.",
26
+ "- Treat the wait, not the check, as the long part. Waiting for `gh pr checks` to pass is a `bg_task_watch`, not `sleep 300; gh pr checks`.",
27
+ "- Waiting on an external system (`gh run view`, `gh pr checks`, `mergify queue show`, a redeploy, a dev server, `cloud-init`): `bg_task_watch` with the check command and `success_when`. For a PR you just enqueued, `watch_delivery`.",
28
+ "- Waiting on your own long command (tests, build, migration): `bg_task_spawn`, then `wait_for_operations`. Do not sleep and re-read the log.",
29
+ "- Waiting for a server you just started: `bg_task_spawn` the server, then `bg_task_watch` on a readiness check (`curl -fsS ...`).",
30
+ "- Retry until a condition with one `bg_task_watch`, never `for i in ...; do ...; sleep N; done`.",
31
+ "- If a dependency is pending and no watcher is running, start `bg_task_watch`; do not loop short sleeps.",
32
+ "- Nothing here automatically diverts a foreground command. A sleep or monitor loop submitted through foreground `bash` blocks the turn.",
33
+ "- Omit `timeout_seconds` on `bg_task_spawn` unless you need a bound. Raise it on `bg_task_watch` for a wait longer than 15 minutes, and leave `callback` alone: do not poll.",
34
+ "- Write the check so a broken check is visible; do not end it with `exit 0` or `|| true`.",
35
+ "- Stop tasks you started for a turn-scoped purpose before finishing the turn.",
36
+ ].join("\n");
37
+
38
+ /** Publish the guidance into `options.sections`; assignment keeps it idempotent across turns. */
39
+ export function publishBackgroundTasksGuidance<T extends object>(options: T): T {
40
+ const target = options as PromptSectionTarget;
41
+ const sections = (target.sections ??= {});
42
+ sections[BACKGROUND_TASKS_SECTION] = BACKGROUND_TASKS_PROMPT;
43
+ return options;
44
+ }
45
+
46
+ /** What a handler receives; only these two fields are used. */
47
+ export interface BeforeAgentStartEvent {
48
+ systemPrompt: string;
49
+ systemPromptOptions?: unknown;
50
+ }
51
+
52
+ /**
53
+ * Deliver this package's guidance on both kinds of host.
54
+ *
55
+ * Modern pi renders custom sections, so publishing keeps the prompt structured
56
+ * and lets pi diff it. Older pi (the packages declare support back to 0.84.4)
57
+ * renders no sections at all, so publishing there would silently deliver nothing;
58
+ * returning the text is the behaviour those hosts already had.
59
+ */
60
+ export function deliverBackgroundTasksPrompt(event: BeforeAgentStartEvent): { systemPrompt: string } | undefined {
61
+ if (!supportsPromptSections(event.systemPromptOptions)) {
62
+ // Several packages carry the same policy, so appending blindly would print it
63
+ // once per publisher. The text path stays idempotent exactly as it used to be.
64
+ const policy = event.systemPrompt.includes(EXECUTION_POLICY_TEXT) ? "" : `${EXECUTION_POLICY_TEXT}\n\n`;
65
+ const guidance = event.systemPrompt.includes(BACKGROUND_TASKS_PROMPT) ? "" : BACKGROUND_TASKS_PROMPT;
66
+ return { systemPrompt: `${event.systemPrompt}\n\n${policy}${guidance}` };
67
+ }
68
+ const options = event.systemPromptOptions as { sections?: Record<string, string> };
69
+ publishExecutionPolicy(options);
70
+ publishBackgroundTasksGuidance(options);
71
+ return undefined;
72
+ }