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,1233 @@
1
+ // Generated fixed launch domain from packages/pi-better-subagents/navigator.mjs. Do not edit directly.
2
+ /**
3
+ * Pure seams for the TUI subagent navigator (issues #45 list, #46 detail,
4
+ * #47 two-press close, #69 health display).
5
+ *
6
+ * Kept free of pi / pi-tui imports (mirrors widget.mjs) so unit tests pin the
7
+ * behavior contracts without a live TUI. index.ts injects the pi-specific
8
+ * pieces (matchesKey, truncateToWidth, CustomEditor, ctx, getDetail, closeRun)
9
+ * at the boundary.
10
+ *
11
+ * The navigator is a HUMAN organization surface: it reads the #44 registry
12
+ * visibility seams (`navigatorVisibleRuns` / `navigatorVisibleCount`). List and
13
+ * detail views are read-only; the #47 Close action is the sole mutator, and it
14
+ * goes through the shared #44 stop/dismiss seams (never a second persistence
15
+ * model). The passive live widget is untouched by this module.
16
+ *
17
+ * Detail view (#46) refreshes via an injected interval (default 1s) and must
18
+ * dispose that timer on every exit path (back, Escape, overlay close, teardown).
19
+ * Close arm timers (#47) dispose on the same paths plus selection change and
20
+ * list↔detail transitions.
21
+ *
22
+ * Health (#69) reuses #66/#67 observation helpers: navigator rows keep the
23
+ * scan order name/id · model[+effort] · elapsed · tool · spend, then append
24
+ * durable status + ≤2 compact health facts. Status is colorized semantically
25
+ * with visible-width-aware truncation.
26
+ */
27
+ import { assignmentFor, createAssignmentProjector } from "./assignment-projection.mjs";
28
+ import { formatNavigatorHealthFacts, statusThemeColor, truncateToVisibleWidth, visibleWidth, } from "./health-surface.mjs";
29
+ /** Detail-view refresh cadence (ms). Mirrors the live widget tick. */
30
+ export const DETAIL_TICK_MS = 1000;
31
+ /** Two-press Close confirmation window (ms). Exclusive at the boundary. */
32
+ export const CLOSE_ARM_MS = 3000;
33
+ /** Legacy status keys retained for compatibility. */
34
+ export const NAVIGATOR_STATUS_KEY = "subagents-nav";
35
+ export const CLOSE_CONFIRM_STATUS_KEY = "subagents-close";
36
+ function isStoppableView(row) {
37
+ const processStatus = row?.assignment?.processStatus ?? row?.attemptStatus ?? row?.status;
38
+ return processStatus === "running" || processStatus === "orphaned";
39
+ }
40
+ /** Legacy footer helpers no longer write status. */
41
+ export function navigatorFooterHint(count) {
42
+ return null;
43
+ }
44
+ export function applyNavigatorFooter(ui, count) {
45
+ return navigatorFooterHint(count);
46
+ }
47
+ // ---------------------------------------------------------------------------
48
+ // Close confirmation (issue #47)
49
+ // ---------------------------------------------------------------------------
50
+ /**
51
+ * Footer confirmation hint while Close is armed.
52
+ * Running/orphaned → `x again to stop <name>`; terminal → `x again to dismiss <name>`.
53
+ * Name falls back to id when missing/null. Orphaned uses the stop wording because
54
+ * Close runs the shared #68 cleanup path (kill related work or finalize).
55
+ */
56
+ export function closeConfirmHint(row) {
57
+ if (!row)
58
+ return null;
59
+ const label = (row.name != null && String(row.name).length > 0) ? String(row.name) : String(row.id);
60
+ const stoppable = isStoppableView(row);
61
+ return stoppable ? `x again to stop ${label}` : `x again to dismiss ${label}`;
62
+ }
63
+ /** Preserve the hint for compatibility without writing status. */
64
+ export function applyCloseConfirmFooter(ui, hint) {
65
+ return hint ?? null;
66
+ }
67
+ /** Mutable Close-arm record. `id` null means disarmed. */
68
+ export function createCloseArm(id, armedAt) {
69
+ return { id: id ?? null, armedAt: armedAt ?? 0 };
70
+ }
71
+ /** True when `arm` is live for `id` at `now` (window is [armedAt, armedAt+CLOSE_ARM_MS)). */
72
+ export function isCloseArmed(arm, id, now, windowMs = CLOSE_ARM_MS) {
73
+ if (!arm || arm.id == null || id == null)
74
+ return false;
75
+ if (arm.id !== id)
76
+ return false;
77
+ if (typeof now !== "number" || typeof arm.armedAt !== "number")
78
+ return false;
79
+ return now >= arm.armedAt && now < arm.armedAt + windowMs;
80
+ }
81
+ /** Clear an arm record in place. */
82
+ export function disarmClose(arm) {
83
+ if (!arm)
84
+ return arm;
85
+ arm.id = null;
86
+ arm.armedAt = 0;
87
+ return arm;
88
+ }
89
+ /**
90
+ * Perform Close on a run id: reread meta + effective status, then either
91
+ * stop+dismiss (running / orphaned) or dismiss-only (terminal / finished-during-arm).
92
+ *
93
+ * Reuses #44/#68 `stopRun` / `dismissRun` — never invents a second kill path.
94
+ * For orphaned runs, dismiss happens only AFTER `stopRun` writes a terminal
95
+ * status (killed / completed / failed / lost).
96
+ * Unknown ids return `{ action: "missing" }` without throwing.
97
+ *
98
+ * @param {string} id
99
+ * @param {object} deps
100
+ * @param {(id: string) => object|undefined} deps.readMeta
101
+ * @param {(m: object) => string} deps.effectiveStatus
102
+ * @param {(id: string) => { action: string, id: string, status?: string }} deps.stopRun
103
+ * @param {(id: string, at?: number) => object|undefined} deps.dismissRun
104
+ * @param {() => number} [deps.now]
105
+ */
106
+ export function executeNavigatorClose(id, deps) {
107
+ const meta = deps.readMeta(id);
108
+ if (!meta)
109
+ return { action: "missing", id };
110
+ const status = deps.effectiveStatus(meta);
111
+ const at = typeof deps.now === "function" ? deps.now() : Date.now();
112
+ // running + orphaned share the stop path (#68). stopRun itself rereads;
113
+ // we still branch on our fresh effective status so a finish-during-arm
114
+ // never reaches the kill path from a stale row.
115
+ if (status === "running" || status === "orphaned") {
116
+ const outcome = deps.stopRun(id);
117
+ // Dismiss only after a terminal write. stopRun always finalizes
118
+ // orphaned runs (killed / completed / failed / lost) when accepted.
119
+ const terminalStatus = outcome?.action === "stopped"
120
+ ? "killed"
121
+ : outcome?.action === "finalized"
122
+ ? outcome.status
123
+ : undefined;
124
+ if (!terminalStatus) {
125
+ // Defensive: stop declined (race to another terminal). Leave
126
+ // non-terminal orphaned undismissed so Close can be retried.
127
+ const after = deps.readMeta(id);
128
+ const afterStatus = after ? deps.effectiveStatus(after) : status;
129
+ if (afterStatus === "orphaned" || afterStatus === "running") {
130
+ return { action: "not-closed", id, status: afterStatus };
131
+ }
132
+ deps.dismissRun(id, at);
133
+ return { action: "dismissed", id, status: afterStatus };
134
+ }
135
+ deps.dismissRun(id, at);
136
+ return { action: "stopped-and-dismissed", id, status: terminalStatus };
137
+ }
138
+ deps.dismissRun(id, at);
139
+ return { action: "dismissed", id, status };
140
+ }
141
+ /**
142
+ * True only in an interactive TUI session with a UI present. Every navigator
143
+ * entry point is gated on this so print/RPC sessions stay untouched.
144
+ *
145
+ * Pi exposes `hasUI: true` (and a `ui` object) in BOTH TUI and RPC modes
146
+ * (extensions.md: `hasUI` guards dialog/notification methods that work in
147
+ * both). The navigator uses terminal-only features — the `custom()` overlay
148
+ * and the editor component factory — so the guard must require an explicit
149
+ * `ctx.mode === "tui"` on top of UI availability; `hasUI` alone would leak
150
+ * `setStatus`/`setEditorComponent`/`custom` calls into RPC sessions.
151
+ */
152
+ export function isNavigatorUiAvailable(ctx) {
153
+ return Boolean(ctx && ctx.mode === "tui" && ctx.hasUI === true && ctx.ui);
154
+ }
155
+ /**
156
+ * Display rows for the navigator list. Input order is preserved — callers pass
157
+ * `navigatorVisibleRuns(listMetas())`, which is newest first and already
158
+ * excludes dismissed and foreign-parent runs (running AND terminal included).
159
+ *
160
+ * @param {Array<object>} metas - visible RunMeta records (newest first)
161
+ * @param {object} deps
162
+ * @param {(m: object) => string} deps.effectiveStatus - registry effectiveStatus
163
+ * @param {(model?: string) => string} deps.shortModel - widget shortModel
164
+ * @param {(ms: number) => string} deps.fmtElapsed - widget fmtElapsed
165
+ * @param {(m: object) => string} deps.spendFor - spend summary or "" (e.g. fmtSpend(parseRun(id).usage))
166
+ * @param {(m: object) => string} [deps.toolFor] - current/used tool label or ""
167
+ * @param {(m: object) => string|undefined} [deps.effortFor] - model effort when available
168
+ * @param {(m: object) => object|undefined} [deps.healthFor] - #66 HealthObservation
169
+ * @param {(m: object, status: string) => object} [deps.project] - refresh-local assignment results shared with expiry filtering
170
+ * @param {number} [deps.now]
171
+ */
172
+ export function pendingNavigatorPark(meta) {
173
+ const park = meta?.park;
174
+ return park?.version === 1 && ["parking", "waiting", "ready", "resuming", "held"].includes(park.phase) ? park : undefined;
175
+ }
176
+ export function buildNavigatorRows(metas, deps) {
177
+ const now = deps.now ?? Date.now();
178
+ const project = deps.project ?? createAssignmentProjector();
179
+ return metas.map((m) => {
180
+ const assignment = project(m, deps.effectiveStatus(m));
181
+ const status = assignment.state;
182
+ const health = typeof deps.healthFor === "function" ? deps.healthFor(m) : undefined;
183
+ const effort = typeof deps.effortFor === "function" ? deps.effortFor(m) : (m.effort ?? m.modelEffort);
184
+ const tool = typeof deps.toolFor === "function" ? (deps.toolFor(m) || "") : "";
185
+ return {
186
+ id: m.id,
187
+ name: m.name,
188
+ assignment,
189
+ status,
190
+ model: deps.shortModel(m.model),
191
+ // Optional effort stays adjacent to model when the host exposes it.
192
+ effort: effort ? String(effort) : undefined,
193
+ // Terminal runs freeze elapsed at endedAt; running runs tick to now.
194
+ elapsed: deps.fmtElapsed((m.endedAt ?? now) - m.startedAt),
195
+ tool,
196
+ spend: deps.spendFor(m) || "",
197
+ // At most two compact facts; empty for healthy/quiet (#67/#69).
198
+ healthFacts: formatNavigatorHealthFacts(health),
199
+ };
200
+ });
201
+ }
202
+ // ---------------------------------------------------------------------------
203
+ // Selection state
204
+ // ---------------------------------------------------------------------------
205
+ /** Navigator list state: the rows plus the selected index (starts at top). */
206
+ export function createNavigatorState(rows) {
207
+ const state = { rows: rows ?? [], selected: 0 };
208
+ clampSelection(state);
209
+ return state;
210
+ }
211
+ /** Keep the selection inside the current row list. */
212
+ export function clampSelection(state) {
213
+ if (state.rows.length === 0)
214
+ state.selected = 0;
215
+ else if (state.selected > state.rows.length - 1)
216
+ state.selected = state.rows.length - 1;
217
+ else if (state.selected < 0)
218
+ state.selected = 0;
219
+ return state.selected;
220
+ }
221
+ /**
222
+ * Move the selection by delta (−1 up, +1 down), clamped at both ends.
223
+ * Returns true when the selection actually changed (callers repaint then).
224
+ */
225
+ export function moveSelection(state, delta) {
226
+ const before = state.selected;
227
+ state.selected = Math.min(Math.max(state.selected + delta, 0), Math.max(0, state.rows.length - 1));
228
+ return state.selected !== before;
229
+ }
230
+ // ---------------------------------------------------------------------------
231
+ // List rendering
232
+ // ---------------------------------------------------------------------------
233
+ const SHEET_RULE = "━";
234
+ const SECTION_RULE = "─";
235
+ const LIST_ROW_START = 3;
236
+ function repeatRule(glyph, width) {
237
+ const n = Math.max(0, Math.floor(width));
238
+ return glyph.repeat(n);
239
+ }
240
+ function commandSheetRule(label, width) {
241
+ const w = Math.max(0, Math.floor(width));
242
+ if (w <= 0)
243
+ return "";
244
+ const text = label != null && String(label).length > 0 ? String(label) : "";
245
+ if (!text)
246
+ return repeatRule(SHEET_RULE, w);
247
+ const prefix = `${SHEET_RULE}${SHEET_RULE} ${text} `;
248
+ if (visibleWidth(prefix) >= w)
249
+ return truncateToVisibleWidth(prefix, w);
250
+ return prefix + repeatRule(SHEET_RULE, w - visibleWidth(prefix));
251
+ }
252
+ function sectionRule(label, width) {
253
+ const w = Math.max(0, Math.floor(width));
254
+ if (w <= 0)
255
+ return "";
256
+ const prefix = `${SECTION_RULE}${SECTION_RULE} ${label} `;
257
+ if (visibleWidth(prefix) >= w)
258
+ return truncateToVisibleWidth(prefix, w);
259
+ return prefix + repeatRule(SECTION_RULE, w - visibleWidth(prefix));
260
+ }
261
+ function stripSectionRule(line) {
262
+ const plain = String(line ?? "");
263
+ const match = plain.match(/^──\s+(.+?)\s+─*$/u);
264
+ return match ? match[1] : null;
265
+ }
266
+ export function navigatorSectionLabel(line) {
267
+ return stripSectionRule(line);
268
+ }
269
+ /**
270
+ * Format one navigator row in scan order (#60/#69):
271
+ * name/id · model [· effort] · elapsed [· tool] [· spend] · status [· fact · fact]
272
+ * Status is colorized when `opts.colorizeStatus` is true (overlay path).
273
+ *
274
+ * @param {object} r - buildNavigatorRows entry
275
+ * @param {object} [opts]
276
+ * @param {boolean} [opts.colorizeStatus]
277
+ * @param {(color: string, s: string) => string} [opts.fg]
278
+ */
279
+ export function formatNavigatorRowText(r, opts = {}) {
280
+ const name = r.name ?? r.id ?? "?";
281
+ const model = r.model ?? "?";
282
+ const modelPart = r.effort ? `${model} ${r.effort}` : model;
283
+ const parts = [name, modelPart, r.elapsed ?? "?"];
284
+ if (r.tool)
285
+ parts.push(r.tool);
286
+ if (r.spend)
287
+ parts.push(r.spend);
288
+ const statusRaw = String(r.status ?? "?");
289
+ const statusText = opts.colorizeStatus && typeof opts.fg === "function"
290
+ ? opts.fg(statusThemeColor(statusRaw), statusRaw)
291
+ : statusRaw;
292
+ parts.push(statusText);
293
+ const facts = Array.isArray(r.healthFacts) ? r.healthFacts.filter(Boolean).slice(0, 2) : [];
294
+ for (const f of facts)
295
+ parts.push(f);
296
+ return parts.join(" · ");
297
+ }
298
+ /**
299
+ * Plain-text navigator lines: title, one `> `/` `-prefixed line per row, and
300
+ * a help line. Every line is passed through a visible-width-aware truncate so
301
+ * the TUI width contract holds even when status is ANSI-colorized. index.ts
302
+ * injects pi-tui's truncateToWidth; when colorization is applied first we fall
303
+ * back to `truncateToVisibleWidth` if the injected truncator is width-naive.
304
+ *
305
+ * @param {object} state
306
+ * @param {object} [opts]
307
+ * @param {number} [opts.width]
308
+ * @param {(s: string, w: number) => string} [opts.truncate]
309
+ * @param {boolean} [opts.colorizeStatus] - colorize durable status (overlay)
310
+ * @param {(color: string, s: string) => string} [opts.fg]
311
+ */
312
+ export function buildNavigatorLines(state, opts = {}) {
313
+ const width = opts.width ?? 80;
314
+ const truncate = opts.truncate ?? truncateToVisibleWidth;
315
+ const rows = state.rows ?? [];
316
+ const lines = [commandSheetRule(`Subagents · ${rows.length}`, width)];
317
+ const selected = rows.length > 0 ? rows[state.selected] : null;
318
+ const stoppable = isStoppableView(selected);
319
+ const action = stoppable ? "x stop" : (selected ? "x dismiss" : null);
320
+ lines.push(` ${["↑↓ select", "Enter view", action, "Esc close"].filter(Boolean).join(" · ")}`);
321
+ lines.push("");
322
+ if (rows.length === 0) {
323
+ lines.push(" (no visible subagent runs)");
324
+ }
325
+ const colorize = opts.colorizeStatus === true;
326
+ const fg = opts.fg;
327
+ for (let i = 0; i < rows.length; i++) {
328
+ const r = rows[i];
329
+ const prefix = i === state.selected ? "› " : " ";
330
+ const body = formatNavigatorRowText(r, { colorizeStatus: colorize, fg });
331
+ lines.push(prefix + body);
332
+ }
333
+ lines.push("");
334
+ lines.push(commandSheetRule("", width));
335
+ return lines.map((l) => {
336
+ const cut = truncate(l, width);
337
+ // Guarantee visible width even if host truncator counts ANSI bytes.
338
+ return visibleWidth(cut) > width ? truncateToVisibleWidth(cut, width) : cut;
339
+ });
340
+ }
341
+ // ---------------------------------------------------------------------------
342
+ // Detail view (issue #46)
343
+ // ---------------------------------------------------------------------------
344
+ /**
345
+ * Assemble a detail snapshot for one run. All I/O lives behind deps so the
346
+ * seam stays unit-testable; index.ts wires readMeta/parseRun/fmt*.
347
+ *
348
+ * @param {string} id
349
+ * @param {object} deps
350
+ * @param {(id: string) => object|undefined} deps.readMeta
351
+ * @param {(m: object) => string} deps.effectiveStatus
352
+ * @param {(id: string) => { finalText: string, lastActivity: string, toolCalls: string[], usage: object }} deps.parseRun
353
+ * @param {(model?: string) => string} deps.shortModel
354
+ * @param {(ms: number) => string} deps.fmtElapsed
355
+ * @param {(u: object) => string} deps.fmtSpend
356
+ * @param {(meta: object) => object|undefined} [deps.healthFor] - #66 HealthObservation
357
+ * @param {(meta: object) => string|undefined} [deps.effortFor]
358
+ * @param {number} [deps.now]
359
+ * @returns {object|null} detail fields, or null when the run is unknown
360
+ */
361
+ export function buildNavigatorDetail(id, deps) {
362
+ const meta = deps.readMeta(id);
363
+ if (!meta)
364
+ return null;
365
+ const now = deps.now ?? Date.now();
366
+ const parsed = deps.parseRun(id);
367
+ const toolCalls = parsed.toolCalls ?? [];
368
+ const assignment = assignmentFor(meta, deps.effectiveStatus(meta));
369
+ const status = assignment.state;
370
+ const running = status === "running" || status === "orphaned";
371
+ const health = typeof deps.healthFor === "function" ? deps.healthFor(meta) : undefined;
372
+ const effortRaw = typeof deps.effortFor === "function"
373
+ ? deps.effortFor(meta)
374
+ : (meta.effort ?? meta.modelEffort);
375
+ return {
376
+ id: meta.id,
377
+ name: meta.name,
378
+ role: meta.catalog?.roleName || (meta.catalog?.roleId ? String(meta.catalog.roleId).replace(/^role\./, "") : undefined) || undefined,
379
+ park: pendingNavigatorPark(meta),
380
+ attemptStatus: deps.effectiveStatus(meta),
381
+ assignment,
382
+ status,
383
+ model: deps.shortModel(meta.model),
384
+ effort: effortRaw ? String(effortRaw) : undefined,
385
+ elapsed: deps.fmtElapsed((meta.endedAt ?? now) - meta.startedAt),
386
+ tools: toolCalls.length ? toolCalls.join(", ") : "",
387
+ // Only a live run has a "current" tool (the latest invocation).
388
+ currentTool: running && toolCalls.length ? toolCalls[toolCalls.length - 1] : undefined,
389
+ spend: deps.fmtSpend(parsed.usage) || "",
390
+ // Terminal prefers the final answer; running shows the live activity.
391
+ output: (running ? (parsed.lastActivity || parsed.finalText) : (parsed.finalText || parsed.lastActivity)) || "",
392
+ // Process identity from durable metadata (#63).
393
+ pid: meta.pid,
394
+ pgid: meta.pgid,
395
+ pidStartTime: meta.pidStartTime,
396
+ orphanedAt: meta.orphanedAt,
397
+ lostAt: meta.lostAt,
398
+ orphanedCallbackSentAt: meta.orphanedCallbackSentAt,
399
+ lostCallbackSentAt: meta.lostCallbackSentAt,
400
+ // Full #66 observation for sectioned detail rendering (#69).
401
+ health: health ?? null,
402
+ now,
403
+ };
404
+ }
405
+ /** Format a ms age as a short label, or "—" when unknown. */
406
+ function fmtDetailAge(ms) {
407
+ if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0)
408
+ return "—";
409
+ const s = Math.floor(ms / 1000);
410
+ if (s < 60)
411
+ return `${s}s`;
412
+ const m = Math.floor(s / 60);
413
+ if (m < 60)
414
+ return `${m}m`;
415
+ const h = Math.floor(m / 60);
416
+ return `${h}h${m % 60}m`;
417
+ }
418
+ /** Format an absolute ms timestamp relative to `now`, or absolute ISO-ish. */
419
+ function fmtDetailTs(ts, now) {
420
+ if (typeof ts !== "number" || !Number.isFinite(ts))
421
+ return "—";
422
+ if (typeof now === "number" && Number.isFinite(now)) {
423
+ return `${fmtDetailAge(Math.max(0, now - ts))} ago`;
424
+ }
425
+ return String(ts);
426
+ }
427
+ /**
428
+ * Plain-text detail lines with sectioned health (#69). Truncated with
429
+ * visible-width awareness so ANSI status colorization stays width-safe.
430
+ *
431
+ * Sections (when data is present):
432
+ * header (title, status, model/effort, elapsed, spend, tools summary)
433
+ * process / liveness
434
+ * activity
435
+ * compaction (separate from tool + model)
436
+ * active tool
437
+ * model call / error
438
+ * last meaningful activity / last log write
439
+ * thresholds
440
+ * callbacks
441
+ * output
442
+ */
443
+ export function buildDetailLines(detail, opts = {}) {
444
+ const width = opts.width ?? 80;
445
+ const truncate = opts.truncate ?? truncateToVisibleWidth;
446
+ const fg = typeof opts.fg === "function" ? opts.fg : null;
447
+ const colorize = opts.colorizeStatus === true && fg;
448
+ if (!detail) {
449
+ return [
450
+ commandSheetRule("Run unavailable", width),
451
+ " ← back · Esc close",
452
+ "",
453
+ commandSheetRule("", width),
454
+ ].map((l) => truncate(l, width));
455
+ }
456
+ const title = detail.name || detail.id || "?";
457
+ const lines = [];
458
+ lines.push(commandSheetRule(title, width));
459
+ const statusRaw = String(detail.status ?? "?");
460
+ const statusText = colorize ? fg(statusThemeColor(statusRaw), statusRaw) : statusRaw;
461
+ const stoppable = isStoppableView(detail);
462
+ const action = stoppable ? "x stop" : "x dismiss";
463
+ lines.push(` ← back · ${action} · Esc close`);
464
+ lines.push("");
465
+ lines.push(` status ${statusText}`);
466
+ if (detail.assignment) {
467
+ lines.push(` revision ${detail.assignment.revision}`);
468
+ lines.push(` reason ${detail.assignment.reason}`);
469
+ lines.push(` process ${detail.assignment.processStatus}; termination ${detail.assignment.termination}`);
470
+ for (const ref of detail.assignment.evidence)
471
+ lines.push(` evidence ${ref}`);
472
+ }
473
+ lines.push(` id ${detail.id ?? "?"}`);
474
+ if (detail.role)
475
+ lines.push(` role ${detail.role}`);
476
+ const model = detail.model ?? "?";
477
+ lines.push(detail.effort ? ` model ${model} · effort ${detail.effort}` : ` model ${model}`);
478
+ lines.push(` elapsed ${detail.elapsed ?? "?"}`);
479
+ // Legacy tools summary (still useful as a used-tools list).
480
+ const toolsLabel = detail.currentTool
481
+ ? `tools current ${detail.currentTool}`
482
+ : (detail.tools ? `tools ${detail.tools}` : "tools (none)");
483
+ lines.push(` ${toolsLabel}`);
484
+ lines.push(detail.spend ? ` spend ${detail.spend}` : " spend (none)");
485
+ const h = detail.health;
486
+ const now = detail.now;
487
+ // Process / liveness.
488
+ lines.push("");
489
+ lines.push(sectionRule("process", width));
490
+ const pid = detail.pid != null ? String(detail.pid) : "—";
491
+ const pgid = detail.pgid != null ? String(detail.pgid) : "—";
492
+ const startTok = detail.pidStartTime != null ? String(detail.pidStartTime) : "—";
493
+ lines.push(` pid ${pid} · pgid ${pgid}`);
494
+ lines.push(` start-token ${startTok}`);
495
+ // Prefer observation liveness, but never claim "supervised" when the
496
+ // detail header already shows a non-running effective status (legacy
497
+ // dead-running metadata → effective "exited" must read as terminal).
498
+ let live = h?.process?.liveness;
499
+ if (detail.assignment)
500
+ live = `${detail.assignment.processStatus}; termination ${detail.assignment.termination}`;
501
+ if (!live || (detail.status !== "running" && live === "supervised")) {
502
+ live = detail.status === "orphaned" ? "orphaned"
503
+ : detail.status === "lost" ? "lost"
504
+ : detail.status === "running" ? "supervised"
505
+ : "terminal";
506
+ }
507
+ lines.push(` liveness ${live}`);
508
+ if (detail.orphanedAt != null)
509
+ lines.push(` orphaned ${fmtDetailTs(detail.orphanedAt, now)}`);
510
+ if (detail.lostAt != null)
511
+ lines.push(` lost ${fmtDetailTs(detail.lostAt, now)}`);
512
+ // Activity.
513
+ lines.push(sectionRule("activity", width));
514
+ lines.push(` ${h?.activity ?? "—"}`);
515
+ if (h?.meaningfulAgeMs != null) {
516
+ lines.push(` last meaningful ${fmtDetailAge(h.meaningfulAgeMs)} ago`);
517
+ }
518
+ else if (h?.lastMeaningfulAt != null) {
519
+ lines.push(` last meaningful ${fmtDetailTs(h.lastMeaningfulAt, now)}`);
520
+ }
521
+ // Compaction, separate from tool and model state.
522
+ lines.push(sectionRule("compaction", width));
523
+ if (h?.compaction) {
524
+ const c = h.compaction;
525
+ const age = c.ageMs != null ? ` · ${fmtDetailAge(c.ageMs)}` : "";
526
+ lines.push(` ${c.state}${age}`);
527
+ if (c.last) {
528
+ const bits = [];
529
+ if (c.last.reason)
530
+ bits.push(c.last.reason);
531
+ if (c.last.aborted)
532
+ bits.push("aborted");
533
+ if (bits.length)
534
+ lines.push(` last ${bits.join(" · ")}`);
535
+ }
536
+ }
537
+ else {
538
+ lines.push(" —");
539
+ }
540
+ // Active tool, separate from compaction and model state.
541
+ lines.push(sectionRule("active tool", width));
542
+ if (h?.tool) {
543
+ const t = h.tool;
544
+ if (t.state === "idle" || !t.active) {
545
+ lines.push(" idle");
546
+ }
547
+ else {
548
+ const age = t.ageMs != null ? ` · ${fmtDetailAge(t.ageMs)}` : "";
549
+ lines.push(` ${t.state} · ${t.active.toolName}${age}`);
550
+ }
551
+ }
552
+ else if (detail.currentTool) {
553
+ lines.push(` ${detail.currentTool}`);
554
+ }
555
+ else {
556
+ lines.push(" idle");
557
+ }
558
+ // Model activity only; incident summaries stay in result tools and logs.
559
+ lines.push(sectionRule("model", width));
560
+ if (h?.model) {
561
+ const m = h.model;
562
+ if (m.state !== "error")
563
+ lines.push(` state ${m.state}`);
564
+ if (m.state !== "error" && m.listWarning)
565
+ lines.push(` warning ${m.listWarning}`);
566
+ if (m.retry) {
567
+ const a = m.retry.attempt != null ? String(m.retry.attempt) : "?";
568
+ const max = m.retry.maxAttempts != null ? String(m.retry.maxAttempts) : "?";
569
+ lines.push(` retry ${a}/${max}`);
570
+ }
571
+ if (m.longModelCall) {
572
+ const age = m.longModelCall.ageMs != null ? ` · ${fmtDetailAge(m.longModelCall.ageMs)}` : "";
573
+ lines.push(` long call${age}`);
574
+ }
575
+ }
576
+ else {
577
+ lines.push(" —");
578
+ }
579
+ // Last log write.
580
+ lines.push(sectionRule("log", width));
581
+ if (h?.rawLog && (h.rawLog.mtimeMs != null || h.rawLog.sizeBytes != null || h.rawLog.error)) {
582
+ if (h.rawLog.mtimeMs != null)
583
+ lines.push(` last write ${fmtDetailTs(h.rawLog.mtimeMs, now)}`);
584
+ if (h.rawLog.sizeBytes != null)
585
+ lines.push(` size ${h.rawLog.sizeBytes}B`);
586
+ if (h.rawLog.error)
587
+ lines.push(` error ${h.rawLog.error}`);
588
+ }
589
+ else {
590
+ lines.push(" —");
591
+ }
592
+ // Thresholds.
593
+ lines.push(sectionRule("thresholds", width));
594
+ if (h?.thresholds) {
595
+ const t = h.thresholds;
596
+ lines.push(` quiet ${fmtDetailAge(t.quietMs)} · stale ${fmtDetailAge(t.staleMs)}`);
597
+ lines.push(` long-tool ${fmtDetailAge(t.longToolMs)} · long-compact ${fmtDetailAge(t.longCompactionMs)}`);
598
+ }
599
+ else {
600
+ lines.push(" —");
601
+ }
602
+ // Callback notification timestamps.
603
+ lines.push(sectionRule("callbacks", width));
604
+ const cbOrphan = detail.orphanedCallbackSentAt;
605
+ const cbLost = detail.lostCallbackSentAt;
606
+ if (cbOrphan != null || cbLost != null) {
607
+ if (cbOrphan != null)
608
+ lines.push(` orphaned notified ${fmtDetailTs(cbOrphan, now)}`);
609
+ if (cbLost != null)
610
+ lines.push(` lost notified ${fmtDetailTs(cbLost, now)}`);
611
+ }
612
+ else {
613
+ lines.push(" (none)");
614
+ }
615
+ lines.push(sectionRule("output", width));
616
+ const body = detail.output && String(detail.output).trim() ? String(detail.output) : "(no output yet)";
617
+ for (const raw of body.split(/\r?\n/)) {
618
+ lines.push(raw.length ? ` ${raw}` : " ");
619
+ }
620
+ lines.push("");
621
+ lines.push(` ← back · ${action} · Esc close`);
622
+ lines.push(commandSheetRule("", width));
623
+ return lines.map((l) => {
624
+ const cut = truncate(l, width);
625
+ return visibleWidth(cut) > width ? truncateToVisibleWidth(cut, width) : cut;
626
+ });
627
+ }
628
+ /** Keep selection on `id` when still present; otherwise clamp in place. */
629
+ export function selectById(state, id) {
630
+ if (id == null) {
631
+ clampSelection(state);
632
+ return state.selected;
633
+ }
634
+ const idx = (state.rows ?? []).findIndex((r) => r && r.id === id);
635
+ // Missing id: leave the index alone and clamp so a neighbor (or 0) is
636
+ // selected — never leave selected out of range after a dismiss/disappear.
637
+ state.selected = idx >= 0 ? idx : state.selected;
638
+ return clampSelection(state);
639
+ }
640
+ /**
641
+ * Replace the visible row list while keeping selection stable by run id.
642
+ *
643
+ * Status refreshes and dismissals rebuild the visible set (reorder, status
644
+ * text change, or a run disappearing). Selection must follow the previously
645
+ * selected id when it is still present; otherwise clamp safely. Returns the
646
+ * resulting selected index.
647
+ */
648
+ export function applyNavigatorRows(state, nextRows) {
649
+ const prevId = state && state.rows && state.rows.length > 0 && state.selected >= 0
650
+ ? state.rows[state.selected]?.id
651
+ : null;
652
+ state.rows = Array.isArray(nextRows) ? nextRows : [];
653
+ return selectById(state, prevId);
654
+ }
655
+ // ---------------------------------------------------------------------------
656
+ // Overlay component (opened via ctx.ui.custom(..., { overlay: true }))
657
+ // ---------------------------------------------------------------------------
658
+ /**
659
+ * Build the custom component pi renders as the focused navigator overlay.
660
+ *
661
+ * @param {Array<object>} rows - buildNavigatorRows output (newest first)
662
+ * @param {object} deps
663
+ * @param {(data: string, keyId: string) => boolean} deps.matchKey - pi-tui matchesKey
664
+ * @param {(s: string, w: number) => string} deps.truncate - pi-tui truncateToWidth
665
+ * @param {object} tui - pi TUI (requestRender)
666
+ * @param {object} [theme] - pi theme (fg); optional in tests
667
+ * @param {(v: null) => void} done - pi close callback
668
+ */
669
+ export function createNavigatorOverlayComponent(rows, deps, tui, theme, done) {
670
+ const state = createNavigatorState(rows);
671
+ /** @type {'list' | 'detail'} */
672
+ let mode = "list";
673
+ /** @type {object|null} */
674
+ let detail = null;
675
+ /** @type {string|null} id of the run currently shown in detail */
676
+ let detailId = null;
677
+ /** @type {ReturnType<typeof setInterval>|null} */
678
+ let detailTimer = null;
679
+ /** @type {ReturnType<typeof setTimeout>|null} */
680
+ let closeArmTimer = null;
681
+ const closeArm = createCloseArm(null, 0);
682
+ let closed = false;
683
+ const setIntervalFn = deps.setInterval ?? globalThis.setInterval.bind(globalThis);
684
+ const clearIntervalFn = deps.clearInterval ?? globalThis.clearInterval.bind(globalThis);
685
+ const setTimeoutFn = deps.setTimeout ?? globalThis.setTimeout.bind(globalThis);
686
+ const clearTimeoutFn = deps.clearTimeout ?? globalThis.clearTimeout.bind(globalThis);
687
+ const nowFn = deps.now ?? (() => Date.now());
688
+ const tickMs = deps.tickMs ?? DETAIL_TICK_MS;
689
+ const fg = (color, s) => theme && typeof theme.fg === "function" ? theme.fg(color, s) : s;
690
+ function publishCloseHint(hint) {
691
+ if (typeof deps.onCloseConfirmHint === "function") {
692
+ try {
693
+ deps.onCloseConfirmHint(hint);
694
+ }
695
+ catch { /* ignore */ }
696
+ }
697
+ }
698
+ function stopCloseArmTimer() {
699
+ if (closeArmTimer != null) {
700
+ try {
701
+ clearTimeoutFn(closeArmTimer);
702
+ }
703
+ catch { /* ignore */ }
704
+ closeArmTimer = null;
705
+ }
706
+ }
707
+ function clearCloseArm() {
708
+ const wasArmed = closeArm.id != null;
709
+ stopCloseArmTimer();
710
+ disarmClose(closeArm);
711
+ if (wasArmed)
712
+ publishCloseHint(null);
713
+ }
714
+ function armCloseFor(row) {
715
+ if (!row || row.id == null)
716
+ return;
717
+ stopCloseArmTimer();
718
+ const at = nowFn();
719
+ closeArm.id = row.id;
720
+ closeArm.armedAt = at;
721
+ const hint = closeConfirmHint(row);
722
+ publishCloseHint(hint);
723
+ // Auto-disarm exactly at the window boundary.
724
+ closeArmTimer = setTimeoutFn(() => {
725
+ closeArmTimer = null;
726
+ // Only clear if this arm is still the live one.
727
+ if (closeArm.id === row.id && closeArm.armedAt === at) {
728
+ disarmClose(closeArm);
729
+ publishCloseHint(null);
730
+ try {
731
+ tui.requestRender();
732
+ }
733
+ catch { /* ignore */ }
734
+ }
735
+ }, CLOSE_ARM_MS);
736
+ try {
737
+ tui.requestRender();
738
+ }
739
+ catch { /* ignore */ }
740
+ }
741
+ function stopDetailTimer() {
742
+ if (detailTimer != null) {
743
+ try {
744
+ clearIntervalFn(detailTimer);
745
+ }
746
+ catch { /* ignore */ }
747
+ detailTimer = null;
748
+ }
749
+ }
750
+ function loadDetail(id) {
751
+ if (typeof deps.getDetail !== "function")
752
+ return null;
753
+ try {
754
+ return deps.getDetail(id) ?? null;
755
+ }
756
+ catch {
757
+ return null;
758
+ }
759
+ }
760
+ function refreshRows() {
761
+ if (typeof deps.getRows !== "function")
762
+ return;
763
+ try {
764
+ const next = deps.getRows();
765
+ // Keep selection by id across status refreshes / dismissals so the
766
+ // highlight does not jump when the visible set reorders or shrinks.
767
+ if (Array.isArray(next))
768
+ applyNavigatorRows(state, next);
769
+ }
770
+ catch { /* keep prior rows */ }
771
+ }
772
+ function currentTargetRow() {
773
+ if (mode === "detail") {
774
+ if (detailId == null)
775
+ return null;
776
+ // Prefer live detail snapshot (status may have changed while armed).
777
+ if (detail && detail.id === detailId) {
778
+ return { id: detail.id, name: detail.name, status: detail.status, assignment: detail.assignment, attemptStatus: detail.attemptStatus };
779
+ }
780
+ const fromList = (state.rows ?? []).find((r) => r && r.id === detailId);
781
+ return fromList ?? { id: detailId, name: detailId, status: "completed" };
782
+ }
783
+ if (state.rows.length === 0)
784
+ return null;
785
+ return state.rows[state.selected] ?? null;
786
+ }
787
+ function handleCloseKey() {
788
+ const row = currentTargetRow();
789
+ if (!row || row.id == null)
790
+ return;
791
+ const wasDetail = mode === "detail";
792
+ const now = nowFn();
793
+ if (isCloseArmed(closeArm, row.id, now)) {
794
+ // Second press within the window on the same run — act.
795
+ const armedId = closeArm.id;
796
+ clearCloseArm();
797
+ let outcome = { action: "missing", id: armedId };
798
+ if (typeof deps.closeRun === "function") {
799
+ try {
800
+ outcome = deps.closeRun(armedId) ?? outcome;
801
+ }
802
+ catch {
803
+ outcome = { action: "missing", id: armedId };
804
+ }
805
+ }
806
+ if (typeof deps.onClosed === "function") {
807
+ try {
808
+ deps.onClosed(outcome);
809
+ }
810
+ catch { /* ignore */ }
811
+ }
812
+ // Leave detail (if any) and refresh the visible list.
813
+ if (mode === "detail") {
814
+ stopDetailTimer();
815
+ mode = "list";
816
+ detail = null;
817
+ detailId = null;
818
+ }
819
+ refreshRows();
820
+ if (wasDetail && deps.closeDetailToMainPage === true) {
821
+ close();
822
+ return;
823
+ }
824
+ // Prefer keeping selection near the closed run's former neighbors.
825
+ selectById(state, armedId);
826
+ clampSelection(state);
827
+ try {
828
+ tui.requestRender();
829
+ }
830
+ catch { /* ignore */ }
831
+ return;
832
+ }
833
+ // First press (or expired / different selection): arm only — never mutate.
834
+ armCloseFor(row);
835
+ }
836
+ function enterDetail() {
837
+ if (state.rows.length === 0)
838
+ return;
839
+ const row = state.rows[state.selected];
840
+ if (!row)
841
+ return;
842
+ clearCloseArm();
843
+ detailId = row.id;
844
+ detail = loadDetail(detailId) ?? {
845
+ id: detailId,
846
+ name: row.name,
847
+ status: row.status,
848
+ model: row.model,
849
+ elapsed: row.elapsed,
850
+ spend: row.spend,
851
+ tools: "",
852
+ output: "",
853
+ };
854
+ mode = "detail";
855
+ stopDetailTimer();
856
+ // Refresh once per second while the detail view is open.
857
+ detailTimer = setIntervalFn(() => {
858
+ if (mode !== "detail" || detailId == null)
859
+ return;
860
+ detail = loadDetail(detailId) ?? detail;
861
+ try {
862
+ tui.requestRender();
863
+ }
864
+ catch { /* ignore */ }
865
+ }, tickMs);
866
+ try {
867
+ tui.requestRender();
868
+ }
869
+ catch { /* ignore */ }
870
+ }
871
+ function leaveDetail() {
872
+ if (mode !== "detail")
873
+ return;
874
+ if (deps.closeDetailToMainPage === true) {
875
+ close();
876
+ return;
877
+ }
878
+ const viewedId = detailId;
879
+ clearCloseArm();
880
+ stopDetailTimer();
881
+ mode = "list";
882
+ detail = null;
883
+ detailId = null;
884
+ // Optional live list refresh so a disappeared run can clamp cleanly.
885
+ refreshRows();
886
+ selectById(state, viewedId);
887
+ try {
888
+ tui.requestRender();
889
+ }
890
+ catch { /* ignore */ }
891
+ }
892
+ function close() {
893
+ if (closed)
894
+ return;
895
+ closed = true;
896
+ clearCloseArm();
897
+ stopDetailTimer();
898
+ mode = "list";
899
+ detail = null;
900
+ detailId = null;
901
+ try {
902
+ done(null);
903
+ }
904
+ catch { /* ignore */ }
905
+ }
906
+ function dispose() {
907
+ // Session teardown / host overlay dismiss — always safe, idempotent.
908
+ clearCloseArm();
909
+ stopDetailTimer();
910
+ mode = "list";
911
+ detail = null;
912
+ detailId = null;
913
+ }
914
+ /** True when input is the Close key (`x` / `X`). */
915
+ function isCloseKey(data) {
916
+ if (deps.matchKey(data, "x") || deps.matchKey(data, "X"))
917
+ return true;
918
+ // Literal fallback for tests / hosts that don't map a Key.x id.
919
+ return data === "x" || data === "X";
920
+ }
921
+ if (deps.initialDetailId != null) {
922
+ const idx = (state.rows ?? []).findIndex((r) => r && r.id === deps.initialDetailId);
923
+ if (idx >= 0) {
924
+ state.selected = idx;
925
+ enterDetail();
926
+ }
927
+ }
928
+ return {
929
+ render(width) {
930
+ // Colorize durable status inside the line (visible-width-aware),
931
+ // then apply row-level accent/dim only to non-status chrome so the
932
+ // semantic status color is not overwritten by whole-line theming.
933
+ if (mode === "detail") {
934
+ const lines = buildDetailLines(detail, {
935
+ width,
936
+ truncate: deps.truncate,
937
+ colorizeStatus: true,
938
+ fg,
939
+ });
940
+ return lines.map((line, i) => {
941
+ if (i === 0)
942
+ return fg("accent", line);
943
+ if (i === 1)
944
+ return fg("dim", line);
945
+ if (i === lines.length - 1)
946
+ return fg("dim", line);
947
+ if (stripSectionRule(line))
948
+ return fg("dim", line);
949
+ if (line.trimStart().startsWith("← back"))
950
+ return fg("dim", line);
951
+ return line;
952
+ });
953
+ }
954
+ const lines = buildNavigatorLines(state, {
955
+ width,
956
+ truncate: deps.truncate,
957
+ colorizeStatus: true,
958
+ fg,
959
+ });
960
+ return lines.map((line, i) => {
961
+ if (i === 0)
962
+ return fg("accent", line);
963
+ if (i === 1 || i === lines.length - 1)
964
+ return fg("dim", line);
965
+ // Selected row: keep the line readable without recoloring the
966
+ // already-semantic status token (status is colored in-body).
967
+ if (state.rows.length > 0 && i === LIST_ROW_START + state.selected) {
968
+ return line.startsWith("› ")
969
+ ? fg("accent", "› ") + line.slice(3)
970
+ : fg("accent", line);
971
+ }
972
+ return line;
973
+ });
974
+ },
975
+ handleInput(data) {
976
+ if (closed)
977
+ return;
978
+ if (mode === "detail") {
979
+ if (isCloseKey(data)) {
980
+ handleCloseKey();
981
+ }
982
+ else if (deps.matchKey(data, "left")) {
983
+ leaveDetail();
984
+ }
985
+ else if (deps.matchKey(data, "escape")) {
986
+ close();
987
+ }
988
+ return;
989
+ }
990
+ if (isCloseKey(data)) {
991
+ handleCloseKey();
992
+ }
993
+ else if (deps.matchKey(data, "up")) {
994
+ if (moveSelection(state, -1)) {
995
+ clearCloseArm();
996
+ tui.requestRender();
997
+ }
998
+ }
999
+ else if (deps.matchKey(data, "down")) {
1000
+ if (moveSelection(state, 1)) {
1001
+ clearCloseArm();
1002
+ tui.requestRender();
1003
+ }
1004
+ }
1005
+ else if (deps.matchKey(data, "enter")) {
1006
+ enterDetail();
1007
+ }
1008
+ else if (deps.matchKey(data, "escape")) {
1009
+ close();
1010
+ }
1011
+ },
1012
+ invalidate() {
1013
+ // Stateless render — nothing cached to clear on theme changes.
1014
+ },
1015
+ /** Clear detail + close-arm timers. Safe to call multiple times / after close. */
1016
+ dispose,
1017
+ };
1018
+ }
1019
+ /** Factory in the shape ctx.ui.custom expects: (tui, theme, keybindings, done). */
1020
+ export function createNavigatorOverlayFactory(rows, deps) {
1021
+ return (tui, theme, _keybindings, done) => createNavigatorOverlayComponent(rows, deps, tui, theme, done);
1022
+ }
1023
+ /**
1024
+ * Open the navigator as a focused overlay. `ui.custom(factory, { overlay: true })`
1025
+ * makes pi render it on top of existing content and focus it on show.
1026
+ *
1027
+ * Pi's `custom()` resolves with the value passed to `done()` (typically `null`),
1028
+ * NOT the component instance. Callers that need the component (e.g. to capture
1029
+ * `dispose` for session_shutdown) must use `deps.onComponent`, which is invoked
1030
+ * synchronously inside the factory when pi constructs the overlay.
1031
+ *
1032
+ * @param {object} ui
1033
+ * @param {Array<object>} rows
1034
+ * @param {object} [deps]
1035
+ * @param {(component: object) => void} [deps.onComponent] - sync capture seam
1036
+ */
1037
+ export function showNavigator(ui, rows, deps = {}) {
1038
+ const baseFactory = createNavigatorOverlayFactory(rows, deps);
1039
+ return ui.custom((tui, theme, keybindings, done) => {
1040
+ const component = baseFactory(tui, theme, keybindings, done);
1041
+ if (typeof deps.onComponent === "function") {
1042
+ try {
1043
+ deps.onComponent(component);
1044
+ }
1045
+ catch { /* never break overlay open */ }
1046
+ }
1047
+ return component;
1048
+ }, { overlay: true });
1049
+ }
1050
+ /**
1051
+ * Open the navigator and track its dispose hook for session teardown.
1052
+ *
1053
+ * Pi's `ui.custom()` promise resolves to `done()`'s value (`null`), not the
1054
+ * component — so dispose MUST be captured synchronously via `onComponent`.
1055
+ * The active dispose reference is cleared when the overlay promise settles
1056
+ * (fulfill OR reject) and is safe to invoke from session_shutdown while open.
1057
+ *
1058
+ * Settlement cleanup uses `.then(clear, clear)` rather than `.finally(...)`:
1059
+ * `finally` rethrows into a second promise, and `void` does not consume that
1060
+ * rejection — Pi rejects `custom()` when overlay factory/show setup fails, which
1061
+ * would emit `unhandledRejection` (Node 22 exits 1). Both branches are handled
1062
+ * and `clear` never rethrows. Callers (index `openNavigator`) may discard the
1063
+ * returned promise; attach their own handler only if they need the settle value.
1064
+ *
1065
+ * @param {object} ui - pi UI with `custom()`
1066
+ * @param {Array<object>} rows
1067
+ * @param {object} deps - showNavigator deps (matchKey, truncate, getDetail, …)
1068
+ * @param {{ get: () => (undefined|(() => void)), set: (fn: undefined|(() => void)) => void }} disposeSlot
1069
+ * @returns {Promise<unknown>} pi custom() promise (done value on fulfill; rejects if custom() rejects).
1070
+ * Safe to discard: internal handlers consume both settle paths (no unhandledRejection).
1071
+ */
1072
+ export function openTrackedNavigator(ui, rows, deps, disposeSlot) {
1073
+ // Drop any prior overlay's timers before opening a new one (defensive;
1074
+ // pi normally only allows one focused custom overlay at a time).
1075
+ try {
1076
+ disposeSlot.get()?.();
1077
+ }
1078
+ catch { /* ignore */ }
1079
+ disposeSlot.set(undefined);
1080
+ let disposeToken;
1081
+ const opened = showNavigator(ui, rows, {
1082
+ ...deps,
1083
+ onComponent: (component) => {
1084
+ if (typeof deps?.onComponent === "function") {
1085
+ try {
1086
+ deps.onComponent(component);
1087
+ }
1088
+ catch { /* ignore */ }
1089
+ }
1090
+ if (component && typeof component.dispose === "function") {
1091
+ disposeToken = () => {
1092
+ try {
1093
+ component.dispose();
1094
+ }
1095
+ catch { /* ignore */ }
1096
+ };
1097
+ disposeSlot.set(disposeToken);
1098
+ }
1099
+ },
1100
+ });
1101
+ // Token-guarded slot cleanup on BOTH settle paths. `.then(clear, clear)`
1102
+ // (not `.finally`) so a rejected custom() does not create a second promise
1103
+ // that rethrows into an unhandledRejection when callers discard the return.
1104
+ const clear = () => {
1105
+ if (disposeSlot.get() === disposeToken) {
1106
+ disposeSlot.set(undefined);
1107
+ }
1108
+ };
1109
+ void Promise.resolve(opened).then(clear, clear);
1110
+ return opened;
1111
+ }
1112
+ /** Invoke and clear a tracked navigator dispose slot (session_shutdown path). */
1113
+ export function disposeTrackedNavigator(disposeSlot) {
1114
+ try {
1115
+ disposeSlot.get()?.();
1116
+ }
1117
+ catch { /* ignore */ }
1118
+ disposeSlot.set(undefined);
1119
+ }
1120
+ // ---------------------------------------------------------------------------
1121
+ // Editor wrapper (empty-editor ← interception by composition)
1122
+ // ---------------------------------------------------------------------------
1123
+ /**
1124
+ * Wrap any editor component so bare ← on an EMPTY editor (with visible runs)
1125
+ * enters subagent navigation, and everything else delegates unchanged.
1126
+ *
1127
+ * Composition, not replacement: a delegating Proxy forwards every property get
1128
+ * (methods bound to the inner editor) and every set to the wrapped component,
1129
+ * so the duck-typing pi applies to whatever `setEditorComponent` produces
1130
+ * (onSubmit/onChange assignment, setText, borderColor, setPaddingX,
1131
+ * setAutocompleteProvider, the `actionHandlers` app-keybinding wiring) keeps
1132
+ * working against the inner editor.
1133
+ *
1134
+ * @param {object} inner - the wrapped editor component
1135
+ * @param {object} deps
1136
+ * @param {(data: string) => boolean} deps.isOpenTrigger - bare ← match (pi-tui matchesKey(data, Key.left))
1137
+ * @param {() => boolean} deps.canOpen - visible current-parent runs exist
1138
+ * @param {() => boolean} [deps.isNavigating] - true while main-window navigation owns empty-editor keys
1139
+ * @param {(data: string) => boolean} [deps.isReturnTrigger] - Down returns to the input line while navigating
1140
+ * @param {(data: string) => boolean} [deps.isPreviousTrigger] - Up moves to the previous main-window row while navigating
1141
+ * @param {(data: string) => boolean} [deps.isViewTrigger] - Enter opens selected detail while navigating
1142
+ * @param {(data: string) => boolean} [deps.isStopTrigger] - x stops selected run while navigating
1143
+ * @param {() => void} [deps.onReturn]
1144
+ * @param {() => void} [deps.onPrevious]
1145
+ * @param {() => void} [deps.onView]
1146
+ * @param {() => void} [deps.onStop]
1147
+ * @param {() => void} [deps.onCancel]
1148
+ * @param {() => void} deps.onOpen - enter main-window navigation
1149
+ */
1150
+ export function wrapEditor(inner, deps) {
1151
+ return new Proxy(inner, {
1152
+ get(target, prop) {
1153
+ if (prop === "handleInput") {
1154
+ return (data) => {
1155
+ if (target.getText() === "") {
1156
+ if (typeof deps.isNavigating === "function" && deps.isNavigating()) {
1157
+ if (deps.isReturnTrigger?.(data)) {
1158
+ deps.onReturn?.();
1159
+ return;
1160
+ }
1161
+ if (deps.isPreviousTrigger?.(data)) {
1162
+ deps.onPrevious?.();
1163
+ return;
1164
+ }
1165
+ if (deps.isViewTrigger?.(data)) {
1166
+ deps.onView?.();
1167
+ return;
1168
+ }
1169
+ if (deps.isStopTrigger?.(data)) {
1170
+ deps.onStop?.();
1171
+ return;
1172
+ }
1173
+ deps.onCancel?.();
1174
+ target.handleInput(data);
1175
+ return;
1176
+ }
1177
+ if (deps.isOpenTrigger(data) && deps.canOpen()) {
1178
+ deps.onOpen();
1179
+ return;
1180
+ }
1181
+ }
1182
+ target.handleInput(data);
1183
+ };
1184
+ }
1185
+ const v = Reflect.get(target, prop);
1186
+ return typeof v === "function" ? v.bind(target) : v;
1187
+ },
1188
+ set(target, prop, value) {
1189
+ return Reflect.set(target, prop, value);
1190
+ },
1191
+ });
1192
+ }
1193
+ /** String marks (not Symbols) so a RELOADED module instance recognizes a
1194
+ * factory installed by its previous incarnation. */
1195
+ export const NAVIGATOR_FACTORY_MARK = "__piBetterSubagentsNavigatorFactory";
1196
+ const NAVIGATOR_FACTORY_REFRESH = "__piBetterSubagentsNavigatorRefresh";
1197
+ /**
1198
+ * Install the navigator editor factory exactly once per UI.
1199
+ *
1200
+ * - No factory configured: wrap a default editor built by deps.createDefaultEditor.
1201
+ * - A factory from ANOTHER extension is configured: wrap its product (compose).
1202
+ * - Our own marked factory is already installed (repeat session_start, or a
1203
+ * reloaded module seeing the previous incarnation's factory): refresh its
1204
+ * deps and keep it — never stack a second wrapper.
1205
+ *
1206
+ * @param {object} ui - pi ctx.ui (getEditorComponent / setEditorComponent)
1207
+ * @param {object} deps - wrapEditor deps + createDefaultEditor(tui, theme, keybindings)
1208
+ * @returns the installed (or refreshed) factory
1209
+ */
1210
+ export function installNavigatorEditor(ui, deps) {
1211
+ const prev = typeof ui.getEditorComponent === "function" ? ui.getEditorComponent() : undefined;
1212
+ if (prev && prev[NAVIGATOR_FACTORY_MARK] === true) {
1213
+ prev[NAVIGATOR_FACTORY_REFRESH](deps);
1214
+ return prev;
1215
+ }
1216
+ let currentDeps = deps;
1217
+ const base = prev;
1218
+ const factory = (tui, theme, keybindings) => {
1219
+ const inner = base
1220
+ ? base(tui, theme, keybindings)
1221
+ : currentDeps.createDefaultEditor(tui, theme, keybindings);
1222
+ return wrapEditor(inner, currentDeps);
1223
+ };
1224
+ factory[NAVIGATOR_FACTORY_MARK] = true;
1225
+ factory[NAVIGATOR_FACTORY_REFRESH] = (next) => {
1226
+ // Mutate in place: wrappers already built from this factory captured the
1227
+ // deps OBJECT, so refreshed callbacks must reach them too (a /reload
1228
+ // keeps the live editor instance alive).
1229
+ Object.assign(currentDeps, next);
1230
+ };
1231
+ ui.setEditorComponent(factory);
1232
+ return factory;
1233
+ }