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,818 @@
1
+ // Generated fixed launch domain from packages/pi-better-subagents/cleanup.ts. Do not edit directly.
2
+ import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync, } from "node:fs";
3
+ import { join, relative, sep } from "node:path";
4
+ import { ABANDONED_LAUNCH_GRACE_MS, abandonedLaunchRecord, baseDir, listMetas, observeArchivePublication, ownedByThisParent, orphanedTaskRuntimeProvenance, processStartToken, purgeAbandonedLaunch, readMeta, removeMetaArtifacts, removeRunDirectory, removeTaskRuntimeProvenance, runDir, listPendingParkedRuns, } from "./registry.domain.mjs";
5
+ import { ConversationBusyError, ConversationLockUnavailableError, conversationLeaseDir, readConversationLease, readRetention, retainedRunIds, retainedSessionIds, withConversationMutation, } from "./continuation.domain.mjs";
6
+ import { processExists } from "./spawn.domain.mjs";
7
+ import { archiveCompletedAttempt, archivePublicationPending, listArchivedAttempts } from "./session-store.domain.mjs";
8
+ import { assignmentRetainsEvidence } from "./assignment-outcomes.domain.mjs";
9
+ function outcomeProtected(meta) {
10
+ try {
11
+ return assignmentRetainsEvidence(meta);
12
+ }
13
+ catch {
14
+ return true;
15
+ }
16
+ }
17
+ export const DEFAULT_CLEANUP_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;
18
+ /**
19
+ * Total registry budget. Age alone cannot bound this directory: 63 GB
20
+ * accumulated inside five days, so a seven-day window never saw it. A
21
+ * `message_update` event re-serialises the whole accumulated message, so one
22
+ * long-running subagent can write gigabytes in an afternoon.
23
+ *
24
+ * Deliberately tight. A single run's log has been observed past 3 GB, so a
25
+ * budget generous enough to hold several of those defeats the purpose: what a
26
+ * developer wants back is the disk, and what they lose is a log nothing reads
27
+ * once its result has been delivered.
28
+ */
29
+ export const DEFAULT_MAX_REGISTRY_BYTES = 2 * 1024 * 1024 * 1024; // 2 GiB
30
+ /**
31
+ * How often the size bound may be enforced. Independent of the daily marker:
32
+ * the point of a size cap is to react inside a day, and it only stats run
33
+ * directories rather than walking the session tree.
34
+ */
35
+ export const SIZE_SWEEP_INTERVAL_MS = 10 * 60 * 1000; // 10min
36
+ /**
37
+ * How long one conversation's cleanup step may wait for its mutation lock.
38
+ * Admission fails fast (0) and release/retention wait a second; cleanup is a
39
+ * background sweep, so it waits just long enough to lose gracefully to an
40
+ * in-flight admission and then retains the resource.
41
+ */
42
+ const CLEANUP_LOCK_WAIT_MS = 250;
43
+ /** Conversation ids the per-conversation mutation lock accepts. */
44
+ const SESSION_ID_PATTERN = /^[A-Za-z0-9_-]+$/;
45
+ const TERMINAL_CLEANUP_STATUSES = new Set(["completed", "failed", "killed", "lost"]);
46
+ /**
47
+ * Choose run directories to retire so the registry fits `maxBytes`,
48
+ * oldest-terminal-first. Pure; the caller supplies the entries.
49
+ */
50
+ export function planRegistrySizeCap(entries, limits) {
51
+ let kept = entries.reduce((sum, entry) => sum + entry.bytes, 0);
52
+ const remove = [];
53
+ if (kept <= limits.maxBytes) {
54
+ return { remove, reclaimedBytes: 0, keptBytes: kept, overBudget: false };
55
+ }
56
+ const candidates = entries
57
+ .filter((entry) => entry.status !== undefined
58
+ && TERMINAL_CLEANUP_STATUSES.has(entry.status)
59
+ && !limits.protectedIds?.has(entry.id))
60
+ .sort((a, b) => (a.endedAt ?? a.dirMtimeMs) - (b.endedAt ?? b.dirMtimeMs));
61
+ let reclaimed = 0;
62
+ for (const candidate of candidates) {
63
+ if (kept <= limits.maxBytes)
64
+ break;
65
+ remove.push(candidate.id);
66
+ kept -= candidate.bytes;
67
+ reclaimed += candidate.bytes;
68
+ }
69
+ return { remove, reclaimedBytes: reclaimed, keptBytes: kept, overBudget: kept > limits.maxBytes };
70
+ }
71
+ export function cleanupStatePath() {
72
+ return join(baseDir(), "cleanup-state.json");
73
+ }
74
+ export function localDateKey(now = Date.now()) {
75
+ const d = new Date(now);
76
+ const year = d.getFullYear();
77
+ const month = String(d.getMonth() + 1).padStart(2, "0");
78
+ const day = String(d.getDate()).padStart(2, "0");
79
+ return `${year}-${month}-${day}`;
80
+ }
81
+ function readCleanupState() {
82
+ try {
83
+ return JSON.parse(readFileSync(cleanupStatePath(), "utf8"));
84
+ }
85
+ catch {
86
+ return {};
87
+ }
88
+ }
89
+ function writeCleanupState(state) {
90
+ mkdirSync(baseDir(), { recursive: true });
91
+ writeFileSync(cleanupStatePath(), JSON.stringify(state, null, 2));
92
+ }
93
+ function retentionMs(explicit, configured) {
94
+ const raw = explicit ?? configured;
95
+ return Number.isFinite(raw) && raw >= 0 ? raw : DEFAULT_CLEANUP_RETENTION_MS;
96
+ }
97
+ function terminalTimestamp(meta) {
98
+ return meta.endedAt ?? meta.lostAt ?? meta.startedAt;
99
+ }
100
+ function safeStatMtime(path) {
101
+ try {
102
+ return statSync(path).mtimeMs;
103
+ }
104
+ catch {
105
+ return undefined;
106
+ }
107
+ }
108
+ /** A metadata-less run directory, with its provenance record (#325). */
109
+ function removeRun(id, errors) {
110
+ try {
111
+ removeRunDirectory(id);
112
+ return true;
113
+ }
114
+ catch (err) {
115
+ errors.push(`${runDir(id)}: ${err instanceof Error ? err.message : String(err)}`);
116
+ return false;
117
+ }
118
+ }
119
+ function removePath(path, errors) {
120
+ try {
121
+ rmSync(path, { recursive: true, force: true });
122
+ return true;
123
+ }
124
+ catch (err) {
125
+ errors.push(`${path}: ${err instanceof Error ? err.message : String(err)}`);
126
+ return false;
127
+ }
128
+ }
129
+ /** True when `path` is `root` itself or lies beneath it. */
130
+ function pathWithin(path, root) {
131
+ return path === root || path.startsWith(root.endsWith(sep) ? root : root + sep);
132
+ }
133
+ function pathOverlapsProtected(path, protectedPaths) {
134
+ for (const protectedPath of protectedPaths) {
135
+ if (pathWithin(path, protectedPath) || pathWithin(protectedPath, path))
136
+ return true;
137
+ }
138
+ return false;
139
+ }
140
+ /**
141
+ * Whether this attempt's recorded child may still be writing its conversation.
142
+ *
143
+ * A `running`/`orphaned` record is occupancy by itself. `killed` is recorded as
144
+ * soon as the stop policy decides, while the child can still be alive and
145
+ * appending; `lost` means supervision lost track, not that the process proved
146
+ * gone. So only an actual process-identity match counts there, and an
147
+ * unavailable start token proves nothing and counts as alive.
148
+ */
149
+ function recordedChildMayWrite(meta) {
150
+ if (!TERMINAL_CLEANUP_STATUSES.has(meta.status))
151
+ return true;
152
+ if (meta.status !== "killed" && meta.status !== "lost")
153
+ return false;
154
+ const pid = meta.pid;
155
+ if (typeof pid !== "number" || pid <= 0 || !processExists(pid))
156
+ return false;
157
+ if (meta.pidStartTime === undefined)
158
+ return true;
159
+ const current = processStartToken(pid);
160
+ return current === undefined || current === meta.pidStartTime;
161
+ }
162
+ /**
163
+ * Whether a writer lease says this conversation is in use.
164
+ *
165
+ * The lease is taken by the supervising foreground *before* the detached child
166
+ * is spawned and before the attempt's metadata exists, so it is the only
167
+ * evidence of occupancy during that admission window. This reads the same
168
+ * record continuation admission uses without recovering or deleting it. A dead
169
+ * supervisor can have spawned a child before publishing its attempt metadata;
170
+ * missing writer evidence therefore keeps the shared workspace protected.
171
+ */
172
+ function leaseMayHaveWriter(sessionId) {
173
+ const lease = readConversationLease(sessionId);
174
+ if (!lease)
175
+ return false;
176
+ if (lease.pid === process.pid)
177
+ return true;
178
+ if (processExists(lease.pid)) {
179
+ if (lease.start === undefined)
180
+ return true;
181
+ const current = processStartToken(lease.pid);
182
+ if (current === undefined || current === lease.start)
183
+ return true;
184
+ }
185
+ const writer = readMeta(lease.attemptId);
186
+ return !writer || recordedChildMayWrite(writer);
187
+ }
188
+ /**
189
+ * Decide which conversations are live enough that no run of theirs (the shared
190
+ * root clone/scratch and saved history included) may be retired yet.
191
+ *
192
+ * Continuation made the workspace and saved session outlive the attempt that
193
+ * created them: a later attempt runs inside the root's clone and appends to the
194
+ * root's history. Age and size sweeps therefore protect conversation-level
195
+ * occupancy, not attempt-local status alone.
196
+ */
197
+ function planCleanupOccupancy(metas, protect) {
198
+ const runIds = new Set(protect?.runIds ?? []);
199
+ const paths = [...(protect?.paths ?? [])];
200
+ for (const meta of metas) {
201
+ if (outcomeProtected(meta)) {
202
+ runIds.add(meta.id);
203
+ }
204
+ }
205
+ // A protected path may be the shared workspace or something inside it. The
206
+ // run whose removal would delete it is protected instead, because
207
+ // `removeMetaArtifacts` removes an owned workspace/scratch with its run.
208
+ for (const meta of metas) {
209
+ if (ownedResources(meta).some((owned) => paths.some((keep) => pathWithin(keep, owned)))) {
210
+ runIds.add(meta.id);
211
+ }
212
+ }
213
+ // A parked worker still owns its assignment: its saved conversation and its
214
+ // owned workspace must survive until the wait is consumed or cancelled,
215
+ // otherwise the automatic continuation would reopen nothing. The park
216
+ // disposition is the evidence, so an unprotected park cannot be swept.
217
+ for (const parked of listPendingParkedRuns(metas)) {
218
+ runIds.add(parked.id);
219
+ paths.push(...ownedResources(parked));
220
+ }
221
+ const sessions = new Set(protect?.sessionIds ?? []);
222
+ for (const meta of metas) {
223
+ if (runIds.has(meta.id))
224
+ sessions.add(meta.sessionId);
225
+ if (recordedChildMayWrite(meta) || leaseMayHaveWriter(meta.sessionId)) {
226
+ sessions.add(meta.sessionId);
227
+ }
228
+ }
229
+ for (const meta of metas) {
230
+ if (sessions.has(meta.sessionId))
231
+ runIds.add(meta.id);
232
+ }
233
+ return { sessions, runIds, paths };
234
+ }
235
+ /**
236
+ * Conversations with a durable continuation record: a completed attempt saved
237
+ * its compact metadata and frozen recipe under the Pi agent directory. Their
238
+ * saved history and owned clone workspace are kept indefinitely; only the
239
+ * volatile log, metadata, and task scratch are reclaimed.
240
+ *
241
+ * A completed durable-layout attempt whose compact record could not be published
242
+ * belongs here too: it is not resumable yet, but its recipe, saved history, and
243
+ * clone are the only copy of that conversation.
244
+ *
245
+ * Legacy attempts (recipes pointing at the old temporary session directory, or
246
+ * fixtures without a recipe) never publish that record and keep the existing
247
+ * reclaimable behavior. A conversation that only ever failed is not resumable,
248
+ * so its artifacts stay ordinary aged history and do not pin a clone forever.
249
+ */
250
+ function persistentSessionIds(metas, archivePending) {
251
+ const sessions = new Set(listArchivedAttempts().map((meta) => meta.sessionId));
252
+ for (const meta of metas) {
253
+ if (archivePending.has(meta.id))
254
+ sessions.add(meta.sessionId);
255
+ }
256
+ return sessions;
257
+ }
258
+ /**
259
+ * Retry publication of every completed attempt whose compact durable record is
260
+ * missing, BEFORE either bound decides what to delete.
261
+ *
262
+ * The volatile run directory is the only place a completed attempt's recipe
263
+ * lives until that record is published, so this pass is what makes the age and
264
+ * size bounds safe: while it keeps failing, the runs it names are retained (see
265
+ * `CleanupScope.archivePending`), and once it succeeds they are ordinary
266
+ * persistent conversations whose raw logs may be reclaimed.
267
+ *
268
+ * Returns the run ids still unpublished.
269
+ */
270
+ function retryPendingArchives(metas) {
271
+ const pending = new Set();
272
+ for (const meta of metas) {
273
+ if (!archivePublicationPending(meta))
274
+ continue;
275
+ try {
276
+ if (archiveCompletedAttempt(meta))
277
+ observeArchivePublication(meta.id);
278
+ }
279
+ catch (error) {
280
+ observeArchivePublication(meta.id, error);
281
+ }
282
+ if (archivePublicationPending(meta))
283
+ pending.add(meta.id);
284
+ }
285
+ return pending;
286
+ }
287
+ function runIdsBySession(metas) {
288
+ const bySession = new Map();
289
+ for (const meta of metas) {
290
+ const ids = bySession.get(meta.sessionId);
291
+ if (ids)
292
+ ids.push(meta.id);
293
+ else
294
+ bySession.set(meta.sessionId, [meta.id]);
295
+ }
296
+ return bySession;
297
+ }
298
+ /** Session ids with a published writer lease file. */
299
+ function leasedSessionIds() {
300
+ let names;
301
+ try {
302
+ names = readdirSync(conversationLeaseDir());
303
+ }
304
+ catch {
305
+ return [];
306
+ }
307
+ return names
308
+ .filter((name) => name.endsWith(".lock"))
309
+ .map((name) => name.slice(0, -".lock".length))
310
+ .filter((id) => SESSION_ID_PATTERN.test(id));
311
+ }
312
+ /**
313
+ * Conversations this sweep must serialize with admission: every conversation it
314
+ * has a run record for, plus every retained and leased one. Admission for a
315
+ * conversation always has either the run record of the attempt it continues or
316
+ * the writer lease it just took, so history that resolves to none of these is
317
+ * foreign and keeps the ordinary, unserialized cleanup path.
318
+ */
319
+ function serializedConversations(metas) {
320
+ const sessions = new Set();
321
+ for (const meta of metas)
322
+ sessions.add(meta.sessionId);
323
+ for (const sessionId of retainedSessionIds())
324
+ sessions.add(sessionId);
325
+ for (const sessionId of leasedSessionIds())
326
+ sessions.add(sessionId);
327
+ return sessions;
328
+ }
329
+ /**
330
+ * Run `body` inside one conversation's mutation transaction — the same
331
+ * kernel-owned critical section admission, lease release, and retention writes
332
+ * take. A lock that is busy or unavailable retains the resource for this sweep
333
+ * instead of blocking the foreground, stalling forever, or failing every other
334
+ * deletion: keeping work is always the safe direction.
335
+ */
336
+ function withConversationSerialization(sessionId, body, retained) {
337
+ if (!sessionId || !SESSION_ID_PATTERN.test(sessionId))
338
+ return body();
339
+ try {
340
+ return withConversationMutation(sessionId, body, { waitMs: CLEANUP_LOCK_WAIT_MS });
341
+ }
342
+ catch (error) {
343
+ if (error instanceof ConversationBusyError || error instanceof ConversationLockUnavailableError) {
344
+ return retained();
345
+ }
346
+ throw error;
347
+ }
348
+ }
349
+ /**
350
+ * Is this conversation provably quiescent right now?
351
+ *
352
+ * Snapshot occupancy is not proof. Between the snapshot and a removal another
353
+ * process can publish a writer lease, re-request retention, or re-write an
354
+ * attempt, so this re-reads the same evidence inside the conversation's
355
+ * mutation transaction: a successful admission can therefore only be seen here
356
+ * (and retained) or happen after the removal it now follows.
357
+ *
358
+ * A brand-new attempt of the same conversation is admitted only under that
359
+ * conversation's lease, so re-reading the lease also covers an attempt this
360
+ * sweep has never seen in its snapshot.
361
+ */
362
+ function conversationQuiescentUnderLock(sessionId, runIds) {
363
+ if (leaseMayHaveWriter(sessionId))
364
+ return false;
365
+ const retention = readRetention(sessionId);
366
+ if (retention && retention.releasedAt === undefined)
367
+ return false;
368
+ for (const id of runIds) {
369
+ const meta = readMeta(id);
370
+ if (meta && recordedChildMayWrite(meta))
371
+ return false;
372
+ }
373
+ return true;
374
+ }
375
+ /** Paths whose removal follows this run's artifacts (see `removeMetaArtifacts`). */
376
+ function ownedResources(meta) {
377
+ return meta.taskScratch ? [meta.cwd, meta.taskScratch] : [meta.cwd];
378
+ }
379
+ function cleanRunDirs(cutoff, scope, errors) {
380
+ let removed = 0;
381
+ let ids;
382
+ try {
383
+ ids = readdirSync(join(baseDir(), "runs"));
384
+ }
385
+ catch {
386
+ return 0;
387
+ }
388
+ // Explicit retention (`retain_session`) pins every attempt of a
389
+ // conversation, not just the attempt that requested it, so its saved
390
+ // session and owned clone/scratch workspace stay available for a
391
+ // continuation until the owner releases them.
392
+ const retained = retainedSessionIds();
393
+ for (const id of ids) {
394
+ const dir = runDir(id);
395
+ const meta = readMeta(id);
396
+ if (meta) {
397
+ if (!ageSweepEligible(meta, cutoff, retained, scope))
398
+ continue;
399
+ if (removeAgedRunArtifacts(meta, cutoff, retained, scope, errors))
400
+ removed += 1;
401
+ continue;
402
+ }
403
+ const mtimeMs = safeStatMtime(dir);
404
+ if (mtimeMs !== undefined && mtimeMs < cutoff && removeRun(id, errors))
405
+ removed += 1;
406
+ }
407
+ // Provenance whose run is already gone (#325).
408
+ for (const id of orphanedTaskRuntimeProvenance())
409
+ removeTaskRuntimeProvenance(id);
410
+ return removed;
411
+ }
412
+ /**
413
+ * Snapshot eligibility for the age bound. Re-applied to the fresh record inside
414
+ * the conversation's lock, because a record can be rewritten between the two:
415
+ * a run that finished later, or one another sweep already retired, is decided
416
+ * on what it says now.
417
+ */
418
+ function ageSweepEligible(meta, cutoff, retained, scope) {
419
+ if (!TERMINAL_CLEANUP_STATUSES.has(meta.status))
420
+ return false;
421
+ if (outcomeProtected(meta))
422
+ return false;
423
+ if (retained.has(meta.sessionId))
424
+ return false;
425
+ // The run directory still holds the only copy of this conversation's recipe.
426
+ if (scope.archivePending.has(meta.id))
427
+ return false;
428
+ // A released conversation can still be in use: a later attempt may run
429
+ // inside this root's clone, or a stop/kill may not have landed.
430
+ if (scope.occupancy.runIds.has(meta.id))
431
+ return false;
432
+ return terminalTimestamp(meta) < cutoff;
433
+ }
434
+ /**
435
+ * Remove one aged attempt's artifacts, serialized with its conversation.
436
+ *
437
+ * This is the deletion the snapshot cannot authorize: an admission that
438
+ * published its writer lease after this sweep listed the registry would have
439
+ * its shared workspace, task scratch, and saved history removed underneath it.
440
+ * The recheck inside the lock sees that admission, so the attempt is retained.
441
+ *
442
+ * The removal itself is inside the transaction because it is the commit point:
443
+ * `removeMetaArtifacts` unlinks the shared workspace, and splitting the
444
+ * decision from that unlink would reopen the race. A conversation whose lock is
445
+ * held by an admission or a retention write is therefore retained for this
446
+ * sweep, and the holder's own bounded wait is what bounds this section.
447
+ */
448
+ function removeAgedRunArtifacts(meta, cutoff, retained, scope, errors) {
449
+ const runIds = scope.runIdsBySession.get(meta.sessionId) ?? [meta.id];
450
+ return withConversationSerialization(meta.sessionId, () => {
451
+ if (!conversationQuiescentUnderLock(meta.sessionId, runIds))
452
+ return false;
453
+ const fresh = readMeta(meta.id) ?? meta;
454
+ if (!ageSweepEligible(fresh, cutoff, retained, scope))
455
+ return false;
456
+ if (removeMetaArtifacts(fresh, { keepWorkspace: scope.persistent.has(fresh.sessionId) }))
457
+ return true;
458
+ errors.push(`${runDir(meta.id)}: could not remove run artifacts`);
459
+ return false;
460
+ }, () => false);
461
+ }
462
+ /**
463
+ * Conversation a session-tree path names, resolved against conversations this
464
+ * sweep tracks: the per-run session directory `<runId>/`, a
465
+ * `<timestamp>_<sessionId>.jsonl` name, or the bare conversation name. History
466
+ * that resolves to nothing this sweep knows stays on the ordinary path.
467
+ */
468
+ function sessionIdForPath(root, path, serialized) {
469
+ const rel = relative(root, path);
470
+ if (!rel || rel.startsWith(".."))
471
+ return undefined;
472
+ const known = [...new Set(pathNameCandidates(rel.split(sep)))].filter((candidate) => serialized.has(candidate));
473
+ // Two different tracked conversations in one path is not a layout this
474
+ // package writes. Refusing to remove it keeps both, instead of serializing
475
+ // one and deleting under the other.
476
+ return known.length === 1 ? known[0] : undefined;
477
+ }
478
+ function* pathNameCandidates(parts) {
479
+ yield parts[0]; // per-run session directory
480
+ const name = parts[parts.length - 1];
481
+ const stem = name.endsWith(".jsonl") ? name.slice(0, -".jsonl".length) : name;
482
+ yield stem;
483
+ // `<timestamp>_<sessionId>.jsonl`: the timestamp carries no underscore.
484
+ const separator = stem.indexOf("_");
485
+ if (separator > 0)
486
+ yield stem.slice(separator + 1);
487
+ }
488
+ /**
489
+ * Remove one aged session-tree entry, serialized with the conversation it names
490
+ * when this sweep tracks that conversation.
491
+ */
492
+ function removeSessionPath(root, path, serialized, scope, errors) {
493
+ const sessionId = sessionIdForPath(root, path, serialized);
494
+ if (!sessionId)
495
+ return removePath(path, errors);
496
+ const runIds = scope.runIdsBySession.get(sessionId) ?? [];
497
+ return withConversationSerialization(sessionId, () => {
498
+ if (!conversationQuiescentUnderLock(sessionId, runIds))
499
+ return false;
500
+ return removePath(path, errors);
501
+ }, () => false);
502
+ }
503
+ function activeSessionIds(metas, occupancy, persistent) {
504
+ const active = new Set(occupancy.sessions);
505
+ for (const meta of metas) {
506
+ if (meta.status === "running" || meta.status === "orphaned")
507
+ active.add(meta.sessionId);
508
+ }
509
+ // A retained completed conversation is not active work, but its saved
510
+ // session file is the history a continuation reopens.
511
+ for (const sessionId of retainedSessionIds())
512
+ active.add(sessionId);
513
+ // Durable conversations never expire with the volatile sweeps either.
514
+ for (const sessionId of persistent)
515
+ active.add(sessionId);
516
+ return active;
517
+ }
518
+ function pathMentionsActiveSession(root, path, active) {
519
+ if (active.size === 0)
520
+ return false;
521
+ const rel = relative(root, path);
522
+ for (const sessionId of active) {
523
+ if (sessionId && rel.includes(sessionId))
524
+ return true;
525
+ }
526
+ return false;
527
+ }
528
+ function cleanSessionTree(root, cutoff, active, protectedPaths, scope, serialized, errors) {
529
+ if (!existsSync(root))
530
+ return 0;
531
+ let removed = 0;
532
+ const visit = (dir) => {
533
+ let entries;
534
+ try {
535
+ entries = readdirSync(dir, { withFileTypes: true });
536
+ }
537
+ catch (err) {
538
+ errors.push(`${dir}: ${err instanceof Error ? err.message : String(err)}`);
539
+ return false;
540
+ }
541
+ let empty = true;
542
+ for (const entry of entries) {
543
+ const path = join(dir, entry.name);
544
+ if (entry.isDirectory()) {
545
+ const childEmpty = visit(path);
546
+ if (childEmpty
547
+ && !pathMentionsActiveSession(root, path, active)
548
+ && !pathOverlapsProtected(path, protectedPaths)) {
549
+ const mtimeMs = safeStatMtime(path);
550
+ if (mtimeMs !== undefined && mtimeMs < cutoff && removeSessionPath(root, path, serialized, scope, errors)) {
551
+ removed += 1;
552
+ continue;
553
+ }
554
+ }
555
+ empty = false;
556
+ continue;
557
+ }
558
+ if (pathMentionsActiveSession(root, path, active) || pathOverlapsProtected(path, protectedPaths)) {
559
+ empty = false;
560
+ continue;
561
+ }
562
+ const mtimeMs = safeStatMtime(path);
563
+ if (mtimeMs !== undefined && mtimeMs < cutoff && removeSessionPath(root, path, serialized, scope, errors)) {
564
+ removed += 1;
565
+ continue;
566
+ }
567
+ empty = false;
568
+ }
569
+ return empty;
570
+ };
571
+ visit(root);
572
+ return removed;
573
+ }
574
+ /**
575
+ * Reclaim launch attempts that never produced a supervised run (#623).
576
+ *
577
+ * A launch that throws while preparing (or a pi that dies in that window) can
578
+ * leave a run directory with only `prompt.md` and a sandbox `control/` dir: no
579
+ * `meta.json`, no `output.log`. Such a directory is unreadable run evidence
580
+ * forever and skews `subagent_list` counts, so it is reclaimed once it is older
581
+ * than {@link ABANDONED_LAUNCH_GRACE_MS}. The grace is the only guard needed:
582
+ * launch preparation is synchronous and short, and a directory with a log is
583
+ * never debris (the child may have started and its output is real evidence).
584
+ *
585
+ * Age alone is not an ownership proof: `purgeAbandonedLaunch` re-classifies and
586
+ * removes under the run's metadata lock, and a launch claim owned by a live
587
+ * process makes the record not-debris at any age, so a preparer that is paused
588
+ * past the grace is preserved and the sweep never deletes a live attempt's
589
+ * artifacts (#623 R1). A record a writer retained is left for the next sweep
590
+ * with an error so the retention stays visible.
591
+ *
592
+ * Runs before the daily marker in {@link runDailyCleanupOnce}, so every launch
593
+ * reclaims earlier debris instead of waiting for the next day's sweep.
594
+ * Best-effort; never throws into its caller.
595
+ */
596
+ export function sweepAbandonedLaunches(options = {}) {
597
+ const now = options.now ?? Date.now();
598
+ const grace = Number.isFinite(options.graceMs) && options.graceMs >= 0 ? options.graceMs : ABANDONED_LAUNCH_GRACE_MS;
599
+ const cutoff = now - grace;
600
+ let ids;
601
+ try {
602
+ ids = readdirSync(join(baseDir(), "runs"));
603
+ }
604
+ catch {
605
+ return { removed: [], errors: [] };
606
+ }
607
+ const removed = [];
608
+ const errors = [];
609
+ for (const id of ids) {
610
+ const attempt = abandonedLaunchRecord(id);
611
+ if (!attempt || attempt.mtimeMs >= cutoff)
612
+ continue;
613
+ const purge = purgeAbandonedLaunch(id);
614
+ if (purge === "purged")
615
+ removed.push(id);
616
+ else if (purge === "retained")
617
+ errors.push(`${runDir(id)}: abandoned launch record could not be removed (in use, or its lock is unavailable)`);
618
+ }
619
+ return { removed, errors };
620
+ }
621
+ export function runDailyCleanupOnce(options = {}) {
622
+ const now = options.now ?? Date.now();
623
+ const abandoned = sweepAbandonedLaunches({ now });
624
+ const dateKey = localDateKey(now);
625
+ const state = readCleanupState();
626
+ if (state.lastLocalDate === dateKey) {
627
+ return { ran: false, dateKey, removedRunDirs: 0, removedSessionPaths: 0, removedAbandonedLaunches: abandoned.removed.length, errors: [...abandoned.errors] };
628
+ }
629
+ const errors = [];
630
+ const terminalRetention = retentionMs(options.terminalRunRetentionMs, options.config?.cleanupTerminalRunRetentionMs);
631
+ const sessionRetention = retentionMs(options.sessionRetentionMs, options.config?.cleanupSessionRetentionMs);
632
+ const metas = listMetas();
633
+ // Publish any outstanding compact record before this sweep reclaims the only
634
+ // copy of a completed attempt's recipe.
635
+ const archivePending = retryPendingArchives(metas);
636
+ const persistent = persistentSessionIds(metas, archivePending);
637
+ const scope = {
638
+ occupancy: planCleanupOccupancy(metas, options.protect),
639
+ runIdsBySession: runIdsBySession(metas),
640
+ persistent,
641
+ archivePending,
642
+ };
643
+ const removedRunDirs = cleanRunDirs(now - terminalRetention, scope, errors);
644
+ // Native saved history is durable regardless of the latest attempt status.
645
+ // Sweep only legacy temporary sessions; eligibility to resume must never
646
+ // determine whether a durable native conversation is kept.
647
+ const sessionRoots = [join(baseDir(), "sessions")];
648
+ let removedSessionPaths = 0;
649
+ for (const root of sessionRoots) {
650
+ removedSessionPaths += cleanSessionTree(root, now - sessionRetention, activeSessionIds(metas, scope.occupancy, persistent), scope.occupancy.paths, scope, serializedConversations(metas), errors);
651
+ }
652
+ try {
653
+ writeCleanupState({ lastLocalDate: dateKey, lastRunAt: now });
654
+ }
655
+ catch (err) {
656
+ errors.push(`${cleanupStatePath()}: ${err instanceof Error ? err.message : String(err)}`);
657
+ }
658
+ return { ran: true, dateKey, removedRunDirs, removedSessionPaths, removedAbandonedLaunches: abandoned.removed.length, errors: [...abandoned.errors, ...errors] };
659
+ }
660
+ /** Bytes held under one run directory. Best-effort; unreadable entries count 0. */
661
+ function dirBytes(dir) {
662
+ let bytes = 0;
663
+ let entries;
664
+ try {
665
+ entries = readdirSync(dir, { withFileTypes: true });
666
+ }
667
+ catch {
668
+ return 0;
669
+ }
670
+ for (const entry of entries) {
671
+ const path = join(dir, entry.name);
672
+ try {
673
+ bytes += entry.isDirectory() ? dirBytes(path) : statSync(path).size;
674
+ }
675
+ catch { /* vanished mid-scan */ }
676
+ }
677
+ return bytes;
678
+ }
679
+ /** Read the registry into size-cap entries. */
680
+ export function collectSizeCapEntries() {
681
+ let ids;
682
+ try {
683
+ ids = readdirSync(join(baseDir(), "runs"));
684
+ }
685
+ catch {
686
+ return [];
687
+ }
688
+ const entries = [];
689
+ for (const id of ids) {
690
+ const dir = runDir(id);
691
+ const meta = readMeta(id);
692
+ entries.push({
693
+ id,
694
+ status: meta?.status,
695
+ endedAt: meta ? terminalTimestamp(meta) : undefined,
696
+ dirMtimeMs: safeStatMtime(dir) ?? 0,
697
+ bytes: dirBytes(dir),
698
+ });
699
+ }
700
+ return entries;
701
+ }
702
+ /**
703
+ * Enforce the registry byte budget, at most once per SIZE_SWEEP_INTERVAL_MS.
704
+ * Complements the daily age sweep: age retires history, this bounds the peak a
705
+ * single busy day can reach. Best-effort; never throws into a caller.
706
+ */
707
+ export function enforceRegistrySizeCapOnce(options = {}) {
708
+ const now = options.now ?? Date.now();
709
+ const idle = {
710
+ ran: false,
711
+ remove: [],
712
+ removed: [],
713
+ reclaimedBytes: 0,
714
+ keptBytes: 0,
715
+ overBudget: false,
716
+ errors: [],
717
+ };
718
+ const state = readCleanupState();
719
+ if (!options.force
720
+ && typeof state.lastSizeSweepAt === "number"
721
+ && now - state.lastSizeSweepAt >= 0
722
+ && now - state.lastSizeSweepAt < SIZE_SWEEP_INTERVAL_MS) {
723
+ return idle;
724
+ }
725
+ const configured = options.config?.maxRegistryBytes;
726
+ const maxBytes = options.maxBytes
727
+ ?? (Number.isFinite(configured) && configured > 0 ? configured : DEFAULT_MAX_REGISTRY_BYTES);
728
+ const errors = [];
729
+ // Same ordering as the age sweep: retry archival first, so a run whose
730
+ // recipe only exists in its volatile directory is never a removal candidate.
731
+ const metas = listMetas();
732
+ const archivePending = retryPendingArchives(metas);
733
+ const entries = collectSizeCapEntries();
734
+ // Retained conversations and their workspaces are exempt from the byte
735
+ // bound until the owner releases them; a size sweep must not delete the
736
+ // history or uncommitted workspace a continuation depends on. An occupied
737
+ // conversation is protected the same way: its root clone may be the oldest
738
+ // terminal run in the registry while a new attempt is still running in it.
739
+ // Occupancy costs a metadata scan and lease reads, so it is only computed
740
+ // when this sweep would actually remove something.
741
+ const totalBytes = entries.reduce((sum, entry) => sum + entry.bytes, 0);
742
+ const occupancy = totalBytes > maxBytes ? planCleanupOccupancy(metas, options.protect) : undefined;
743
+ const protectedIds = new Set([
744
+ ...(options.protectedIds ?? ownRunIds(metas)),
745
+ ...retainedRunIds(metas),
746
+ ...(occupancy?.runIds ?? []),
747
+ // An unpublished compact record is the recipe's only copy.
748
+ ...archivePending,
749
+ ]);
750
+ const plan = planRegistrySizeCap(entries, { maxBytes, protectedIds });
751
+ const scope = {
752
+ occupancy: occupancy ?? { sessions: new Set(), runIds: new Set(), paths: [] },
753
+ runIdsBySession: runIdsBySession(metas),
754
+ persistent: persistentSessionIds(metas, archivePending),
755
+ archivePending,
756
+ };
757
+ const bytesById = new Map(entries.map((entry) => [entry.id, entry.bytes]));
758
+ const removed = [];
759
+ let retainedBytes = 0;
760
+ for (const id of plan.remove) {
761
+ const outcome = removeSizeCappedRun(id, scope, errors);
762
+ if (outcome === "removed")
763
+ removed.push(id);
764
+ else if (outcome === "retained")
765
+ retainedBytes += bytesById.get(id) ?? 0;
766
+ }
767
+ // A candidate the recheck retained is still on disk, so the byte account and
768
+ // the shortfall it reports stay honest.
769
+ const keptBytes = plan.keptBytes + retainedBytes;
770
+ try {
771
+ writeCleanupState({ ...state, lastSizeSweepAt: now });
772
+ }
773
+ catch (err) {
774
+ errors.push(`${cleanupStatePath()}: ${err instanceof Error ? err.message : String(err)}`);
775
+ }
776
+ return { ...plan, ran: true, removed, keptBytes, overBudget: keptBytes > maxBytes, errors };
777
+ }
778
+ /**
779
+ * Remove one planned size-cap candidate, serialized with its conversation.
780
+ *
781
+ * The plan is a snapshot of both bytes and occupancy. Re-reading inside the
782
+ * lock keeps a run that became active, retained, or newly leased since then:
783
+ * a continuation admitted after planning runs inside the root the plan chose to
784
+ * delete, and its shared workspace is exactly what a size sweep goes after.
785
+ */
786
+ function removeSizeCappedRun(id, scope, errors) {
787
+ const meta = readMeta(id);
788
+ if (!meta)
789
+ return removeRun(id, errors) ? "removed" : "failed";
790
+ const runIds = scope.runIdsBySession.get(meta.sessionId) ?? [meta.id];
791
+ return withConversationSerialization(meta.sessionId, () => {
792
+ if (!conversationQuiescentUnderLock(meta.sessionId, runIds))
793
+ return "retained";
794
+ const fresh = readMeta(id) ?? meta;
795
+ if (!TERMINAL_CLEANUP_STATUSES.has(fresh.status))
796
+ return "retained";
797
+ if (outcomeProtected(fresh))
798
+ return "retained";
799
+ if (scope.occupancy.runIds.has(fresh.id))
800
+ return "retained";
801
+ if (removeMetaArtifacts(fresh, { keepWorkspace: scope.persistent.has(fresh.sessionId) }))
802
+ return "removed";
803
+ errors.push(`${runDir(id)}: could not remove run artifacts`);
804
+ return "failed";
805
+ }, () => "retained");
806
+ }
807
+ /** Run ids owned by this pi process, which a size sweep must never remove. */
808
+ function ownRunIds(metas = listMetas()) {
809
+ const own = new Set();
810
+ try {
811
+ for (const meta of metas) {
812
+ if (ownedByThisParent(meta))
813
+ own.add(meta.id);
814
+ }
815
+ }
816
+ catch { /* registry unreadable: protect nothing, remove nothing new */ }
817
+ return own;
818
+ }