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,942 @@
1
+ // Generated fixed launch domain from packages/pi-better-subagents/continuation.ts. Do not edit directly.
2
+ /**
3
+ * Subagent continuation.
4
+ *
5
+ * A subagent attempt is one detached `pi -p` process; its conversation is a
6
+ * saved native session JSONL that outlives the process. Continuation keeps the
7
+ * conversation and allocates a NEW attempt, so old logs, results, callback
8
+ * markers, and timing records stay intact under their own run id.
9
+ *
10
+ * A completed attempt and an interrupted attempt stopped by `subagent_stop`
11
+ * (status `killed`) are both continuable: stopping ends an execution, not the
12
+ * saved conversation or the uncommitted workspace it leaves behind. The stopped
13
+ * attempt is never rewritten; the next attempt keeps `rootRunId` and records
14
+ * `resumedFrom`, so the interruption and its recovery stay distinguishable.
15
+ *
16
+ * This module owns the four seams that decision needs and that the rest of the
17
+ * package must not duplicate:
18
+ *
19
+ * 1. **Saved-conversation identity.** Find the exact JSONL for a session id and
20
+ * prove its header still names that id and the expected workspace. Pi's
21
+ * `--session` opens a missing path by creating a fresh conversation, so a
22
+ * continuation validates first and refuses instead of silently replacing
23
+ * history.
24
+ * 2. **Launch recipe.** Capture the effective launch (tools, exclusions,
25
+ * clean/nesting/approval intent, confinement, model, effort, role identity,
26
+ * workspace intent) on run metadata.
27
+ * 3. **Explicit retention.** `retain_session` records a bounded promise that a
28
+ * completed conversation and its workspace stay out of cleanup until a
29
+ * foreground session releases it. Retention lives in the existing runtime
30
+ * storage, so system-temp reaping can still remove it; a missing
31
+ * conversation refuses.
32
+ *
33
+ * Foreground-session ownership is provenance, not an authorization boundary:
34
+ * Pi sessions are ordinary files on disk, so any foreground session that can
35
+ * name a continuable attempt with a verifiable saved conversation, workspace,
36
+ * and launch recipe may continue it (or release its optional retention). The new
37
+ * attempt records the INVOKING foreground as its callback origin and its
38
+ * permission admission is re-resolved from the current profile, so continuing
39
+ * transfers the run to the caller instead of borrowing the original owner's
40
+ * authority.
41
+ * 4. **Conversation writer lease.** A cross-process token lease held from
42
+ * admission through child lifetime and finalization. Duplicate continuations
43
+ * of one conversation admit exactly one writer. Lease-path and retention
44
+ * mutations run inside a short kernel-owned per-conversation transaction, so
45
+ * stale recovery cannot expose an empty path to a competing admission.
46
+ * Stale leases are recovered only with process-identity or terminal-attempt
47
+ * evidence, and never while the attempt's own detached child is still alive:
48
+ * the lease holder and the writer are different processes, so the holder
49
+ * dying is not quiescence. Missing attempt metadata is also not quiescence —
50
+ * the launch path spawns before publishing it — so that state needs an
51
+ * explicit operator recovery.
52
+ *
53
+ * No worker, RPC daemon, or new poller exists between attempts: the next
54
+ * attempt is an ordinary detached launch, and a parked owner occupies no
55
+ * capacity slot.
56
+ */
57
+ import { randomBytes } from "node:crypto";
58
+ import { closeSync, linkSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
59
+ import { dirname, join } from "node:path";
60
+ import { createRequire } from "node:module";
61
+ import { baseDir, listMetas, processStartToken, readMeta, } from "./registry.domain.mjs";
62
+ import { processExists } from "./spawn.domain.mjs";
63
+ import { durableSubagentRoot } from "./session-store.domain.mjs";
64
+ const nodeRequire = createRequire(import.meta.url);
65
+ /** A continuation that cannot proceed. The message is the model-facing refusal. */
66
+ export class ContinuationError extends Error {
67
+ constructor(message) {
68
+ super(message);
69
+ this.name = "ContinuationError";
70
+ }
71
+ }
72
+ /** Header lines are small; this bound only prevents reading a whole session. */
73
+ const SESSION_HEADER_BYTES = 256 * 1024;
74
+ function canonicalPath(path) {
75
+ try {
76
+ return realpathSync.native(path);
77
+ }
78
+ catch {
79
+ return path;
80
+ }
81
+ }
82
+ /** First JSON line of a session file, or undefined when it is not a session header. */
83
+ export function readSessionHeader(path) {
84
+ let fd;
85
+ try {
86
+ fd = openSync(path, "r");
87
+ const buffer = Buffer.alloc(SESSION_HEADER_BYTES);
88
+ const read = readSync(fd, buffer, 0, buffer.length, 0);
89
+ if (read <= 0)
90
+ return undefined;
91
+ const text = buffer.subarray(0, read).toString("utf-8");
92
+ const newline = text.indexOf("\n");
93
+ const line = (newline === -1 ? text : text.slice(0, newline)).trim();
94
+ if (!line)
95
+ return undefined;
96
+ const parsed = JSON.parse(line);
97
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
98
+ ? parsed
99
+ : undefined;
100
+ }
101
+ catch {
102
+ return undefined;
103
+ }
104
+ finally {
105
+ if (fd !== undefined) {
106
+ try {
107
+ closeSync(fd);
108
+ }
109
+ catch { /* read-only handle */ }
110
+ }
111
+ }
112
+ }
113
+ /**
114
+ * Find the saved conversation for `sessionId` and prove it still belongs to
115
+ * this attempt's workspace. Ambiguity is refused, never guessed: a wrong file
116
+ * would silently continue someone else's history.
117
+ */
118
+ export function findSavedConversation(options) {
119
+ const { sessionDir, sessionId, expectedCwd } = options;
120
+ if (!isSafeRunId(sessionId))
121
+ return { kind: "invalid", detail: `session id ${JSON.stringify(sessionId)} is not a valid run id` };
122
+ let names;
123
+ try {
124
+ names = readdirSync(sessionDir);
125
+ }
126
+ catch {
127
+ return { kind: "missing" };
128
+ }
129
+ const matches = [];
130
+ let headerCwd;
131
+ // Pi names session files `<timestamp>_<sessionId>.jsonl`. Try that exact
132
+ // suffix first so a resume does not read the header prefix of every
133
+ // conversation in a busy session directory; fall back to a header scan for
134
+ // renamed or foreign layouts instead of guessing.
135
+ const direct = names.filter((name) => name.endsWith(`_${sessionId}.jsonl`));
136
+ if (direct.length > 1)
137
+ return { kind: "ambiguous", paths: direct.map((name) => join(sessionDir, name)).sort() };
138
+ const candidates = direct.length === 1 ? direct : names.filter((name) => name.endsWith(".jsonl"));
139
+ for (const name of candidates) {
140
+ const path = join(sessionDir, name);
141
+ const header = readSessionHeader(path);
142
+ if (!header || header.type !== "session") {
143
+ if (direct.length === 1)
144
+ return { kind: "invalid", detail: `${path} is not a saved Pi session` };
145
+ continue;
146
+ }
147
+ if (header.id !== sessionId) {
148
+ if (direct.length === 1)
149
+ return { kind: "invalid", detail: `${path} names session ${JSON.stringify(header.id)}` };
150
+ continue;
151
+ }
152
+ matches.push(path);
153
+ if (headerCwd === undefined && typeof header.cwd === "string")
154
+ headerCwd = header.cwd;
155
+ }
156
+ if (matches.length === 0)
157
+ return { kind: "missing" };
158
+ if (matches.length > 1)
159
+ return { kind: "ambiguous", paths: matches.sort() };
160
+ const path = matches[0];
161
+ const expected = canonicalPath(expectedCwd);
162
+ if (headerCwd === undefined || headerCwd.trim() === "") {
163
+ return { kind: "mismatch", detail: `saved conversation ${path} records no workspace` };
164
+ }
165
+ const recorded = canonicalPath(headerCwd);
166
+ if (recorded !== expected) {
167
+ return { kind: "mismatch", detail: `saved conversation ${path} belongs to workspace ${recorded}, not ${expected}` };
168
+ }
169
+ return { kind: "ok", conversation: { sessionId, path, cwd: recorded } };
170
+ }
171
+ // ---- launch recipe -------------------------------------------------------
172
+ /** Freeze the effective launch of one attempt for later continuation. */
173
+ export function buildLaunchRecipe(input) {
174
+ const recipe = { version: 1, ...input };
175
+ if (input.excludeTools === "")
176
+ delete recipe.excludeTools;
177
+ if (input.sandboxDir === "")
178
+ delete recipe.sandboxDir;
179
+ // Absent means discovery. Never write an explicit undefined key, so a recipe
180
+ // that inherited skills reads back exactly like one written before #664.
181
+ if (input.skills === undefined)
182
+ delete recipe.skills;
183
+ return recipe;
184
+ }
185
+ /** Compare implementation identity separately from current permission admission. */
186
+ export function assertContinuationExtensions(priorId, recipe, baseArgs, trusted) {
187
+ const expected = recipe.baseExtensionArgs ?? recipe.extensionArgs;
188
+ if (expected && JSON.stringify(baseArgs) !== JSON.stringify(expected)) {
189
+ throw new ContinuationError(`Cannot continue ${priorId}: resolved extension implementations changed; launch a new run rather than changing this conversation's runtime silently.`);
190
+ }
191
+ if (!recipe.trustedExtensions)
192
+ return;
193
+ for (const current of trusted) {
194
+ const original = recipe.trustedExtensions.find((tool) => tool.name === current.name);
195
+ if (!original || original.package !== current.package
196
+ || canonicalPath(original.loadPath) !== canonicalPath(current.loadPath)) {
197
+ throw new ContinuationError(`Cannot continue ${priorId}: resolved extension implementation changed for ${current.name}; launch a new run instead.`);
198
+ }
199
+ }
200
+ }
201
+ export function retentionDir() {
202
+ return join(baseDir(), "retained");
203
+ }
204
+ function retentionPath(sessionId) {
205
+ if (!isSafeRunId(sessionId))
206
+ throw new Error("Invalid session id.");
207
+ return join(retentionDir(), `${sessionId}.json`);
208
+ }
209
+ function isSafeRunId(id) {
210
+ return typeof id === "string" && /^[A-Za-z0-9_-]+$/.test(id);
211
+ }
212
+ function parseRetention(raw, sessionId) {
213
+ let parsed;
214
+ try {
215
+ parsed = JSON.parse(raw);
216
+ }
217
+ catch {
218
+ return undefined;
219
+ }
220
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
221
+ return undefined;
222
+ const record = parsed;
223
+ if (record.version !== 1 || record.sessionId !== sessionId)
224
+ return undefined;
225
+ if (typeof record.ownerRunId !== "string" || typeof record.workspace !== "string")
226
+ return undefined;
227
+ if (typeof record.retainedAt !== "number" || typeof record.lastAttemptId !== "string")
228
+ return undefined;
229
+ const origin = record.ownerOrigin;
230
+ if (!origin || typeof origin.cwd !== "string")
231
+ return undefined;
232
+ return record;
233
+ }
234
+ export function readRetention(sessionId) {
235
+ if (!isSafeRunId(sessionId))
236
+ return undefined;
237
+ try {
238
+ return parseRetention(readFileSync(retentionPath(sessionId), "utf-8"), sessionId);
239
+ }
240
+ catch {
241
+ return undefined;
242
+ }
243
+ }
244
+ function writeRetention(record) {
245
+ const path = retentionPath(record.sessionId);
246
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
247
+ const temp = join(dirname(path), `.${randomBytes(8).toString("hex")}.tmp`);
248
+ try {
249
+ writeFileSync(temp, JSON.stringify(record, null, 2), { mode: 0o600, flag: "wx" });
250
+ renameSync(temp, path);
251
+ }
252
+ catch (error) {
253
+ try {
254
+ unlinkSync(temp);
255
+ }
256
+ catch { /* best effort */ }
257
+ throw error;
258
+ }
259
+ }
260
+ /** Every parseable retention record, including released ones. */
261
+ export function listRetentionRecords() {
262
+ let names;
263
+ try {
264
+ names = readdirSync(retentionDir());
265
+ }
266
+ catch {
267
+ return [];
268
+ }
269
+ const records = [];
270
+ for (const name of names) {
271
+ if (!name.endsWith(".json"))
272
+ continue;
273
+ const sessionId = name.slice(0, -5);
274
+ if (!isSafeRunId(sessionId))
275
+ continue;
276
+ const record = readRetention(sessionId);
277
+ if (record)
278
+ records.push(record);
279
+ }
280
+ return records;
281
+ }
282
+ /** Session ids whose conversation and workspace must survive cleanup. */
283
+ export function retainedSessionIds() {
284
+ const ids = new Set();
285
+ for (const record of listRetentionRecords()) {
286
+ if (record.releasedAt === undefined)
287
+ ids.add(record.sessionId);
288
+ }
289
+ return ids;
290
+ }
291
+ /** Run ids belonging to a retained conversation (every attempt, not just the root). */
292
+ export function retainedRunIds(metas = listMetas()) {
293
+ const sessions = retainedSessionIds();
294
+ const ids = new Set();
295
+ if (sessions.size === 0)
296
+ return ids;
297
+ for (const meta of metas) {
298
+ if (sessions.has(meta.sessionId))
299
+ ids.add(meta.id);
300
+ }
301
+ return ids;
302
+ }
303
+ /** Record an explicit retention request. An existing record keeps its first timestamp. */
304
+ export function retainConversation(options) {
305
+ return withConversationMutation(options.sessionId, () => {
306
+ const existing = readRetention(options.sessionId);
307
+ const record = existing
308
+ ? { ...existing, lastAttemptId: options.attemptId }
309
+ : {
310
+ version: 1,
311
+ sessionId: options.sessionId,
312
+ ownerRunId: options.ownerRunId,
313
+ workspace: canonicalPath(options.workspace),
314
+ ownerOrigin: { cwd: options.ownerOrigin.cwd, sessionId: options.ownerOrigin.sessionId },
315
+ retainedAt: options.now ?? Date.now(),
316
+ lastAttemptId: options.attemptId,
317
+ };
318
+ writeRetention(record);
319
+ return record;
320
+ }, { waitMs: RETENTION_LOCK_WAIT_MS });
321
+ }
322
+ /** Extend retention to a continuation attempt, but never revive a released one. */
323
+ export function refreshRetentionForAttempt(options) {
324
+ return withConversationMutation(options.sessionId, () => {
325
+ // Read and write inside one critical section: a release acknowledged by
326
+ // another process before this section began is visible to the re-read
327
+ // and is never overwritten by an attempt refresh.
328
+ const existing = readRetention(options.sessionId);
329
+ if (!existing || existing.releasedAt !== undefined)
330
+ return undefined;
331
+ const record = { ...existing, lastAttemptId: options.attemptId };
332
+ writeRetention(record);
333
+ return record;
334
+ }, { waitMs: RETENTION_LOCK_WAIT_MS });
335
+ }
336
+ /** Mark a validated retention record released. Deletion is left to normal cleanup. */
337
+ export function markRetentionReleased(record, origin, now = Date.now()) {
338
+ return withConversationMutation(record.sessionId, () => {
339
+ // Re-read under the same lock the refresh path uses. An acknowledged
340
+ // release is monotonic: it is never revived by a stale attempt refresh
341
+ // that was planned before this section.
342
+ const current = readRetention(record.sessionId) ?? record;
343
+ if (current.releasedAt !== undefined)
344
+ return current;
345
+ const released = {
346
+ ...current,
347
+ releasedAt: now,
348
+ releasedBy: { cwd: origin.cwd, sessionId: origin.sessionId },
349
+ };
350
+ writeRetention(released);
351
+ return released;
352
+ }, { waitMs: RETENTION_LOCK_WAIT_MS });
353
+ }
354
+ // ---- ownership and continuation plan ------------------------------------
355
+ /**
356
+ * True when `origin` is the foreground session recorded on the run.
357
+ *
358
+ * This is a provenance/labelling helper, deliberately NOT an admission gate: a
359
+ * different foreground session may continue a continuable attempt (see the
360
+ * module header). Callers that need to route a notification use it to decide
361
+ * which foreground a run belongs to, not to deny an operation.
362
+ */
363
+ export function ownedByOrigin(meta, origin) {
364
+ const recorded = meta.callbackOrigin;
365
+ if (!origin?.sessionId || typeof origin.cwd !== "string")
366
+ return false;
367
+ if (!recorded?.sessionId)
368
+ return false;
369
+ return recorded.cwd === origin.cwd && recorded.sessionId === origin.sessionId;
370
+ }
371
+ function refusal(id, reason) {
372
+ return new ContinuationError(`Cannot continue ${id}: ${reason}`);
373
+ }
374
+ function effectiveAttemptStatus(meta) {
375
+ if (meta.status !== "running")
376
+ return meta.status;
377
+ return processExists(meta.pid) ? "running" : "exited";
378
+ }
379
+ function isActiveAttempt(meta) {
380
+ const status = effectiveAttemptStatus(meta);
381
+ return status === "running" || status === "orphaned";
382
+ }
383
+ /**
384
+ * Terminal statuses whose saved conversation may be reopened by a new attempt.
385
+ * A stopped (`killed`) run is an interrupted execution, not a destroyed
386
+ * conversation; other terminal outcomes keep their existing refusal.
387
+ */
388
+ const CONTINUABLE_STATUSES = new Set(["completed", "killed"]);
389
+ /**
390
+ * A recorded writer that may still be appending to the conversation.
391
+ *
392
+ * A fresh launch opens a brand-new session and takes no lease, so a killed
393
+ * attempt can be recorded terminal while its child is still alive (for example
394
+ * SIGTERM is pending behind SIGSTOP). In that state no lease blocks the next
395
+ * writer. Check the prior attempt explicitly — the active-sibling scan excludes
396
+ * it — plus every same-conversation attempt that is not known to have exited.
397
+ * `completed`/`failed` attempts are omitted because their compact durable
398
+ * records drop the process-start identity, so a recycled pid could otherwise
399
+ * block an unrelated conversation. `recordedWriterLive` applies the same pid
400
+ * and start-token evidence the conversation lease uses.
401
+ */
402
+ function findLiveConversationWriter(prior, others, probe) {
403
+ const candidates = [
404
+ prior,
405
+ ...others.filter((meta) => meta.sessionId === prior.sessionId
406
+ && meta.id !== prior.id
407
+ && meta.status !== "completed"
408
+ && meta.status !== "failed"),
409
+ ];
410
+ return candidates.find((meta) => recordedWriterLive(meta, probe));
411
+ }
412
+ /**
413
+ * Validate one continuable attempt for continuation and return the exact
414
+ * conversation plus frozen recipe to relaunch. Every refusal is fail-closed:
415
+ * nothing is spawned and no history is created or replaced.
416
+ */
417
+ export function planContinuation(options) {
418
+ const { id, prior, origin, others } = options;
419
+ if (!prior)
420
+ throw refusal(id, "no such run id.");
421
+ // The invoking foreground is still required, because the new attempt's
422
+ // completion callback and navigator entry are routed to it. It is not
423
+ // required to be the attempt's original owner: the saved session, workspace,
424
+ // and recipe are the thing being continued, and Pi sessions are shared
425
+ // on-disk state rather than a per-foreground boundary.
426
+ if (!origin?.sessionId)
427
+ throw refusal(id, "the active foreground session identity is unavailable.");
428
+ if (!CONTINUABLE_STATUSES.has(prior.status)) {
429
+ throw refusal(id, `its status is ${prior.status}; only a completed or stopped run can be continued. Stop a running attempt or launch a new run instead.`);
430
+ }
431
+ const recipe = prior.recipe;
432
+ if (!recipe || recipe.version !== 1)
433
+ throw refusal(id, "it was launched before continuation support, so no launch recipe was saved.");
434
+ if (recipe.sessionId !== prior.sessionId)
435
+ throw refusal(id, "its launch recipe and metadata disagree about the conversation identity.");
436
+ if (canonicalPath(recipe.cwd) !== canonicalPath(prior.cwd))
437
+ throw refusal(id, "its launch recipe and metadata disagree about the workspace.");
438
+ const active = others.find((meta) => meta.sessionId === prior.sessionId && meta.id !== prior.id && isActiveAttempt(meta));
439
+ if (active)
440
+ throw refusal(id, `attempt ${active.id} is still active in the same conversation; stop it first.`);
441
+ // A killed record means stop was requested, not that the writer exited. A
442
+ // fresh launch has no lease, so this check is the only admission barrier
443
+ // while the prior child can still append to the saved conversation.
444
+ const writer = findLiveConversationWriter(prior, others, options.probe);
445
+ if (writer) {
446
+ throw refusal(id, `attempt ${writer.id} is still active and may still be writing the conversation (pid ${writer.pid}); wait for it to exit before continuing.`);
447
+ }
448
+ let workspaceIsDir = false;
449
+ try {
450
+ workspaceIsDir = statSync(recipe.cwd).isDirectory();
451
+ }
452
+ catch {
453
+ workspaceIsDir = false;
454
+ }
455
+ if (!workspaceIsDir)
456
+ throw refusal(id, `its workspace ${recipe.cwd} is missing.`);
457
+ if (recipe.sandboxDir !== undefined) {
458
+ let sandboxIsDir = false;
459
+ try {
460
+ sandboxIsDir = statSync(recipe.sandboxDir).isDirectory();
461
+ }
462
+ catch {
463
+ sandboxIsDir = false;
464
+ }
465
+ if (!sandboxIsDir)
466
+ throw refusal(id, `its sandbox workspace ${recipe.sandboxDir} is missing.`);
467
+ }
468
+ const lookup = findSavedConversation({
469
+ sessionDir: recipe.sessionDir,
470
+ sessionId: recipe.sessionId,
471
+ expectedCwd: recipe.cwd,
472
+ });
473
+ switch (lookup.kind) {
474
+ case "ok":
475
+ return { prior, recipe, conversation: lookup.conversation, rootRunId: prior.rootRunId ?? prior.id };
476
+ case "missing":
477
+ throw refusal(id, `its saved conversation is gone (session ${recipe.sessionId} under ${recipe.sessionDir}). Retained history lives in temporary storage and can be removed; launch a new run instead.`);
478
+ case "ambiguous":
479
+ throw refusal(id, `its saved conversation is ambiguous (${lookup.paths.length} files name session ${recipe.sessionId}); refusing to guess which history to continue.`);
480
+ case "invalid":
481
+ case "mismatch":
482
+ throw refusal(id, `its saved conversation cannot be verified: ${lookup.detail}.`);
483
+ }
484
+ }
485
+ /** Validate an explicit retention release. Returns the record to mark released. */
486
+ export function planRetentionRelease(options) {
487
+ const { id, prior, origin, others } = options;
488
+ if (!prior)
489
+ throw new ContinuationError(`Cannot release ${id}: no such run id.`);
490
+ // Release is the same on-disk convenience promise as continuation: any
491
+ // foreground that can name the attempt may drop optional retention. The
492
+ // invoking identity is still required, because the record keeps `releasedBy`
493
+ // and the decision must be attributable to a real foreground session.
494
+ if (!origin?.sessionId)
495
+ throw new ContinuationError(`Cannot release ${id}: the active foreground session identity is unavailable.`);
496
+ const record = readRetention(prior.sessionId);
497
+ if (!record)
498
+ throw new ContinuationError(`Cannot release ${id}: it was not launched with retain_session, so it has no retained conversation.`);
499
+ if (record.releasedAt !== undefined) {
500
+ throw new ContinuationError(`Cannot release ${id}: its retention was already released at ${new Date(record.releasedAt).toISOString()}.`);
501
+ }
502
+ const active = others.find((meta) => meta.sessionId === prior.sessionId && isActiveAttempt(meta));
503
+ if (active)
504
+ throw new ContinuationError(`Cannot release ${id}: attempt ${active.id} is still active in the same conversation. Stop it first.`);
505
+ return record;
506
+ }
507
+ // ---- cross-process conversation writer lease -----------------------------
508
+ export class ConversationBusyError extends Error {
509
+ constructor(message) {
510
+ super(message);
511
+ this.name = "ConversationBusyError";
512
+ }
513
+ }
514
+ /** A live holder must be visible for this long before its terminal attempt frees the lease. */
515
+ const TERMINAL_ATTEMPT_RECLAIM_MS = 5_000;
516
+ const UNPARSABLE_LEASE_GRACE_MS = 1_000;
517
+ /** Admission is fail-fast: another foreground mutation is a refusal, not a wait. */
518
+ const ADMISSION_LOCK_WAIT_MS = 0;
519
+ /** Release and retention are short cleanup decisions; wait briefly for their lock. */
520
+ const RELEASE_LOCK_WAIT_MS = 1_000;
521
+ const RETENTION_LOCK_WAIT_MS = 1_000;
522
+ /**
523
+ * Stable writer-lease root, independent of TMPDIR: a conversation is ordinary
524
+ * on-disk Pi state, so a foreground that restarts under a different temp root
525
+ * must still see the one writer slot. Retention records stay in the volatile
526
+ * registry as before.
527
+ */
528
+ export function conversationLeaseDir() {
529
+ return join(durableSubagentRoot(), "conversation-leases");
530
+ }
531
+ function leasePathFor(sessionId) {
532
+ return join(conversationLeaseDir(), `${sessionId}.lock`);
533
+ }
534
+ function errnoOf(error) {
535
+ return error.code;
536
+ }
537
+ // ---- per-conversation mutation lock --------------------------------------
538
+ /**
539
+ * Refuse rather than proceed when the kernel lock that makes lease and
540
+ * retention mutations serial cannot be created. Node 24 ships `node:sqlite`;
541
+ * on a runtime without it the one-writer guarantee is unavailable, so every
542
+ * admission, release, and retention write fails closed instead of racing.
543
+ */
544
+ export class ConversationLockUnavailableError extends Error {
545
+ constructor(message) {
546
+ super(message);
547
+ this.name = "ConversationLockUnavailableError";
548
+ }
549
+ }
550
+ const sqliteWarningFilter = Symbol.for("pi-better-subagents.continuation-sqlite-warning-filter");
551
+ function installSqliteWarningFilter() {
552
+ const marked = process;
553
+ if (marked[sqliteWarningFilter])
554
+ return;
555
+ marked[sqliteWarningFilter] = true;
556
+ const emit = process.emitWarning.bind(process);
557
+ process.emitWarning = ((warning, ...args) => {
558
+ const message = typeof warning === "string"
559
+ ? warning
560
+ : warning?.message;
561
+ if (typeof message === "string" && message.includes("SQLite is an experimental feature"))
562
+ return;
563
+ return emit(warning, ...args);
564
+ });
565
+ }
566
+ let databaseSyncCtor;
567
+ function databaseSync() {
568
+ if (databaseSyncCtor !== undefined)
569
+ return databaseSyncCtor;
570
+ installSqliteWarningFilter();
571
+ try {
572
+ const loaded = nodeRequire("node:sqlite");
573
+ databaseSyncCtor = loaded.DatabaseSync ?? null;
574
+ }
575
+ catch (error) {
576
+ const code = errnoOf(error);
577
+ const message = error instanceof Error ? error.message : String(error);
578
+ const missing = code === "ERR_UNKNOWN_BUILTIN_MODULE"
579
+ || message.includes("Cannot find package")
580
+ || message.includes("Cannot find module")
581
+ || message.includes("No such built-in module");
582
+ if (!missing)
583
+ throw error;
584
+ databaseSyncCtor = null;
585
+ }
586
+ return databaseSyncCtor;
587
+ }
588
+ function isSqliteBusy(error) {
589
+ const message = error instanceof Error ? error.message : String(error);
590
+ return /database is locked|SQLITE_BUSY/i.test(message);
591
+ }
592
+ /**
593
+ * Stable transaction file for one conversation. It is never renamed or deleted
594
+ * while the conversation is in use: SQLite's reserved lock lives in this file,
595
+ * and the kernel drops that lock when its holder exits.
596
+ */
597
+ function conversationMutationPath(sessionId) {
598
+ return join(conversationLeaseDir(), `${sessionId}.mutations.sqlite`);
599
+ }
600
+ /**
601
+ * Serialize one conversation's lease-path and retention mutations across
602
+ * processes with a short kernel-owned critical section.
603
+ *
604
+ * The body runs while SQLite holds `BEGIN IMMEDIATE` on the conversation's
605
+ * stable transaction file. No other process can enter a conversation mutation
606
+ * at the same time, so a reclaim cannot expose an empty lease path to an
607
+ * admission, and a retention refresh cannot publish a stale unreleased record
608
+ * over an acknowledged release. The transaction is released immediately after
609
+ * the body returns: it covers the mutation, never the detached child's lifetime.
610
+ *
611
+ * `waitMs` defaults to 0 so admission fails fast. Callers that own cleanup may
612
+ * pass a small budget. A body must not call another public conversation
613
+ * mutation for the same conversation; private `*Unlocked` helpers exist for
614
+ * that reason and keep the lock non-reentrant.
615
+ */
616
+ export function withConversationMutation(sessionId, body, options = {}) {
617
+ if (!isSafeRunId(sessionId))
618
+ throw new Error("Invalid session id for a conversation mutation.");
619
+ const Ctor = databaseSync();
620
+ if (!Ctor) {
621
+ throw new ConversationLockUnavailableError(`Cannot update conversation ${sessionId}: this Pi runtime does not provide node:sqlite, so the cross-process conversation lock required for one-writer continuation is unavailable. Upgrade to Node 24 or newer.`);
622
+ }
623
+ const directory = conversationLeaseDir();
624
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
625
+ const db = new Ctor(conversationMutationPath(sessionId));
626
+ const requestedWait = options.waitMs ?? 0;
627
+ const waitMs = Number.isFinite(requestedWait)
628
+ ? Math.min(60_000, Math.max(0, Math.floor(requestedWait)))
629
+ : 0;
630
+ try {
631
+ db.exec(`PRAGMA busy_timeout = ${waitMs}`);
632
+ try {
633
+ db.exec("BEGIN IMMEDIATE");
634
+ }
635
+ catch (error) {
636
+ if (isSqliteBusy(error)) {
637
+ throw new ConversationBusyError(`Cannot update conversation ${sessionId}: another foreground process is inside a conversation mutation.`);
638
+ }
639
+ throw error;
640
+ }
641
+ }
642
+ catch (error) {
643
+ try {
644
+ db.close();
645
+ }
646
+ catch { /* preserve the lock error */ }
647
+ throw error;
648
+ }
649
+ let result;
650
+ try {
651
+ result = body();
652
+ }
653
+ catch (error) {
654
+ try {
655
+ db.exec("ROLLBACK");
656
+ }
657
+ catch { /* closing rolls back too */ }
658
+ try {
659
+ db.close();
660
+ }
661
+ catch { /* preserve the body error */ }
662
+ throw error;
663
+ }
664
+ try {
665
+ db.exec("ROLLBACK");
666
+ }
667
+ finally {
668
+ db.close();
669
+ }
670
+ return result;
671
+ }
672
+ function parseLeasePayload(raw) {
673
+ let parsed;
674
+ try {
675
+ parsed = JSON.parse(raw);
676
+ }
677
+ catch {
678
+ return undefined;
679
+ }
680
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
681
+ return undefined;
682
+ const payload = parsed;
683
+ if (typeof payload.pid !== "number" || !Number.isInteger(payload.pid) || payload.pid <= 0)
684
+ return undefined;
685
+ if (typeof payload.token !== "string" || typeof payload.sessionId !== "string")
686
+ return undefined;
687
+ if (typeof payload.attemptId !== "string" || typeof payload.at !== "number")
688
+ return undefined;
689
+ return payload;
690
+ }
691
+ /** Metadata of the attempt a lease names, when that id is a real run id. */
692
+ function readAttemptMeta(attemptId) {
693
+ if (!isSafeRunId(attemptId))
694
+ return undefined;
695
+ try {
696
+ return readMeta(attemptId);
697
+ }
698
+ catch {
699
+ return undefined;
700
+ }
701
+ }
702
+ /**
703
+ * Whether the attempt's recorded child may still be writing the conversation.
704
+ *
705
+ * The lease is taken by the supervising pi process, but the writer is the
706
+ * detached child it spawned: a supervisor can exit — normally, by crash, or as a
707
+ * `killed`/`lost` record whose kill has not landed — while its child keeps
708
+ * appending to the saved session. Recorded process-start identity is compared so
709
+ * a recycled pid cannot stand in for the old writer; an unavailable token proves
710
+ * nothing and counts as alive.
711
+ *
712
+ * The process group is deliberately not writer evidence: only the child pi writes
713
+ * the session, and treating a surviving tool descendant as a writer would strand
714
+ * the conversation until that descendant exits.
715
+ */
716
+ function recordedWriterLive(meta, probe) {
717
+ const pid = meta.pid;
718
+ if (typeof pid !== "number" || pid <= 0 || !processExists(pid))
719
+ return false;
720
+ if (meta.pidStartTime === undefined)
721
+ return true;
722
+ const current = (probe?.startToken ?? processStartToken)(pid);
723
+ if (current === undefined)
724
+ return true;
725
+ return current === meta.pidStartTime;
726
+ }
727
+ /** True when the held pid now belongs to an unrelated process. */
728
+ function holderRecycled(payload, probe) {
729
+ if (payload.start === undefined)
730
+ return false;
731
+ const current = (probe?.startToken ?? processStartToken)(payload.pid);
732
+ return current !== undefined && current !== payload.start;
733
+ }
734
+ /**
735
+ * Whether an existing lease may be recovered. Conservative by design: a live
736
+ * holder keeps its lease unless its recorded start identity proves pid
737
+ * recycling, and a dead or recycled holder is still not quiescence while the
738
+ * attempt's own child is alive. That child outlives the process that took the
739
+ * lease, so it is checked before any creator-death or terminal-status
740
+ * conclusion, including `killed`/`lost` whose kill may not have landed yet.
741
+ *
742
+ * A missing attempt record proves nothing about the child: the launch sequence
743
+ * acquires the lease, spawns the detached child, and only then publishes
744
+ * metadata. A dead holder with no record can therefore be a live writer whose
745
+ * supervisor crashed in that window. Such a lease is refused until an operator
746
+ * explicitly recovers the conversation or starts a new one.
747
+ */
748
+ function leaseReclaimable(leasePath, raw, now, probe, explicitRecovery = false) {
749
+ const payload = parseLeasePayload(raw);
750
+ if (!payload) {
751
+ try {
752
+ return now - statSync(leasePath).mtimeMs > UNPARSABLE_LEASE_GRACE_MS;
753
+ }
754
+ catch (error) {
755
+ if (errnoOf(error) === "ENOENT")
756
+ return true;
757
+ return false;
758
+ }
759
+ }
760
+ const own = payload.pid === process.pid;
761
+ if (!own && processExists(payload.pid) && !holderRecycled(payload, probe))
762
+ return false;
763
+ const meta = readAttemptMeta(payload.attemptId);
764
+ if (!meta) {
765
+ if (!explicitRecovery)
766
+ return false;
767
+ // Even an explicit recovery keeps the same grace for this process's own
768
+ // just-published lease, so an in-flight admission is not stolen from.
769
+ if (own && now - payload.at < TERMINAL_ATTEMPT_RECLAIM_MS)
770
+ return false;
771
+ return true;
772
+ }
773
+ if (recordedWriterLive(meta, probe))
774
+ return false;
775
+ if (!own)
776
+ return true;
777
+ if (now - payload.at < TERMINAL_ATTEMPT_RECLAIM_MS)
778
+ return false;
779
+ return isTerminalStatus(meta.status);
780
+ }
781
+ function isTerminalStatus(status) {
782
+ return status === "completed" || status === "failed" || status === "killed" || status === "lost";
783
+ }
784
+ function conversationBusy(sessionId, attemptId) {
785
+ return new ConversationBusyError(`Cannot continue ${attemptId}: conversation ${sessionId} is already being written by another attempt.`);
786
+ }
787
+ function readLeaseRaw(leasePath) {
788
+ try {
789
+ return readFileSync(leasePath, "utf-8");
790
+ }
791
+ catch (error) {
792
+ if (errnoOf(error) === "ENOENT")
793
+ return undefined;
794
+ throw error;
795
+ }
796
+ }
797
+ /**
798
+ * Publish one complete lease at its path. The caller holds the conversation
799
+ * mutation lock, so an existing lease was either judged recoverable or
800
+ * admission already refused.
801
+ *
802
+ * A fresh path is published with a hard link (never overwriting an unknown
803
+ * holder). Reclaiming an existing path atomically renames the new complete
804
+ * lease over it, so no reader — including cleanup, which does not take this
805
+ * lock — can observe an empty path while a stale holder is being replaced.
806
+ */
807
+ function publishLeaseUnlocked(leasePath, serialized, replace) {
808
+ const creating = join(dirname(leasePath), `.${randomBytes(8).toString("hex")}.creating`);
809
+ writeFileSync(creating, serialized, { mode: 0o600, flag: "wx" });
810
+ try {
811
+ if (replace)
812
+ renameSync(creating, leasePath);
813
+ else
814
+ linkSync(creating, leasePath);
815
+ }
816
+ catch (error) {
817
+ try {
818
+ unlinkSync(creating);
819
+ }
820
+ catch { /* best effort */ }
821
+ if (errnoOf(error) === "EEXIST") {
822
+ throw new ConversationBusyError("Conversation lease changed during a serialized admission.");
823
+ }
824
+ throw error;
825
+ }
826
+ if (!replace) {
827
+ try {
828
+ unlinkSync(creating);
829
+ }
830
+ catch { /* best effort */ }
831
+ }
832
+ }
833
+ /** Remove only the exact lease this holder published. Caller holds the lock. */
834
+ function releaseLeaseUnlocked(leasePath, serialized) {
835
+ const observed = readLeaseRaw(leasePath);
836
+ if (observed === undefined || observed !== serialized)
837
+ return;
838
+ try {
839
+ unlinkSync(leasePath);
840
+ }
841
+ catch (error) {
842
+ if (errnoOf(error) !== "ENOENT")
843
+ throw error;
844
+ }
845
+ }
846
+ /**
847
+ * Acquire the conversation writer lease.
848
+ *
849
+ * Admission is one serialized critical section: existing bytes are inspected
850
+ * and a recoverable lease is replaced atomically with this holder's complete
851
+ * lease before the transaction ends. No other admission can enter the section,
852
+ * and the path is never empty during replacement, which is what makes stale
853
+ * recovery unable to displace a newer holder or admit a second writer.
854
+ *
855
+ * `waitMs` defaults to 0 so duplicate admission fails fast instead of stalling
856
+ * the foreground event loop. `explicitRecovery` is an operator escape for a
857
+ * conversation whose writer evidence is permanently unavailable (for example a
858
+ * supervisor killed between spawning its child and publishing metadata); it is
859
+ * never used by ordinary continuation.
860
+ */
861
+ export function acquireConversationLease(options) {
862
+ const { sessionId, attemptId } = options;
863
+ if (!isSafeRunId(sessionId))
864
+ throw new Error("Invalid session id for a conversation lease.");
865
+ const now = options.now ?? Date.now;
866
+ try {
867
+ return withConversationMutation(sessionId, () => {
868
+ const leasePath = leasePathFor(sessionId);
869
+ const payload = {
870
+ pid: process.pid,
871
+ start: processStartToken(process.pid),
872
+ token: randomBytes(16).toString("hex"),
873
+ sessionId,
874
+ attemptId,
875
+ at: now(),
876
+ };
877
+ if (payload.start === undefined)
878
+ delete payload.start;
879
+ const serialized = JSON.stringify(payload);
880
+ const existing = readLeaseRaw(leasePath);
881
+ let replace = false;
882
+ if (existing !== undefined) {
883
+ if (!leaseReclaimable(leasePath, existing, now(), options.probe, options.explicitRecovery)) {
884
+ throw conversationBusy(sessionId, attemptId);
885
+ }
886
+ replace = true;
887
+ }
888
+ publishLeaseUnlocked(leasePath, serialized, replace);
889
+ let released = false;
890
+ return {
891
+ sessionId,
892
+ attemptId,
893
+ token: payload.token,
894
+ path: leasePath,
895
+ release() {
896
+ if (released)
897
+ return;
898
+ withConversationMutation(sessionId, () => releaseLeaseUnlocked(leasePath, serialized), { waitMs: RELEASE_LOCK_WAIT_MS });
899
+ released = true;
900
+ },
901
+ };
902
+ }, { waitMs: Math.max(0, options.waitMs ?? ADMISSION_LOCK_WAIT_MS) });
903
+ }
904
+ catch (error) {
905
+ if (error instanceof ConversationBusyError)
906
+ throw conversationBusy(sessionId, attemptId);
907
+ throw error;
908
+ }
909
+ }
910
+ /** Test/diagnostic seam: the lease currently published for a conversation. */
911
+ export function readConversationLease(sessionId) {
912
+ if (!isSafeRunId(sessionId))
913
+ return undefined;
914
+ try {
915
+ const raw = readFileSync(leasePathFor(sessionId), "utf-8");
916
+ const payload = parseLeasePayload(raw);
917
+ return payload ? { ...payload, raw } : undefined;
918
+ }
919
+ catch {
920
+ return undefined;
921
+ }
922
+ }
923
+ /** Release exact undispatched server ownership under the ordinary conversation lock. */
924
+ export function releaseUnspawnedConversationLease(input) {
925
+ return withConversationMutation(input.sessionId, () => {
926
+ const lease = readConversationLease(input.sessionId);
927
+ if (!lease)
928
+ return true;
929
+ if (lease.attemptId !== input.attemptId || lease.pid !== input.managerPid || !lease.start
930
+ || Buffer.from(lease.start).toString("base64url") !== input.managerStart)
931
+ return false;
932
+ if (processExists(lease.pid)) {
933
+ const start = processStartToken(lease.pid);
934
+ if (!start || start === lease.start)
935
+ return false;
936
+ }
937
+ if (!input.canProveUnspawned())
938
+ return false;
939
+ releaseLeaseUnlocked(leasePathFor(input.sessionId), lease.raw);
940
+ return readConversationLease(input.sessionId) === undefined;
941
+ }, { waitMs: RELEASE_LOCK_WAIT_MS });
942
+ }