@leitwerk-dev/server 0.3.1 → 0.4.1

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 (327) hide show
  1. package/dist/{app-BuJCdkvy.d.ts → app-CtlotzYm.d.ts} +312 -121
  2. package/dist/app-KR3N2JAE.js +8 -0
  3. package/dist/app.d.ts +9 -0
  4. package/dist/app.d.ts.map +1 -1
  5. package/dist/auth/fastify-auth.d.ts +9 -0
  6. package/dist/auth/fastify-auth.d.ts.map +1 -1
  7. package/dist/{chunk-QCHS3X33.js → chunk-OUA5EZS2.js} +3127 -689
  8. package/dist/chunk-OUA5EZS2.js.map +1 -0
  9. package/dist/{chunk-HK6BLLHA.js → chunk-YLWY4UTT.js} +2424 -1580
  10. package/dist/chunk-YLWY4UTT.js.map +1 -0
  11. package/dist/config/config-loader.d.ts +1 -1
  12. package/dist/config/config-loader.d.ts.map +1 -1
  13. package/dist/config/config-types.d.ts +16 -1
  14. package/dist/config/config-types.d.ts.map +1 -1
  15. package/dist/config/kubernetes-docker-isolation.d.ts +7 -0
  16. package/dist/config/kubernetes-docker-isolation.d.ts.map +1 -0
  17. package/dist/dashboard-model-issues.d.ts +10 -0
  18. package/dist/dashboard-model-issues.d.ts.map +1 -0
  19. package/dist/db/database.d.ts.map +1 -1
  20. package/dist/db/execution-inspection-repo.d.ts +15 -0
  21. package/dist/db/execution-inspection-repo.d.ts.map +1 -0
  22. package/dist/db/history-window.d.ts +11 -0
  23. package/dist/db/history-window.d.ts.map +1 -0
  24. package/dist/db/process-event-repo.d.ts +2 -0
  25. package/dist/db/process-event-repo.d.ts.map +1 -1
  26. package/dist/db/process-instance-repo.d.ts +21 -1
  27. package/dist/db/process-instance-repo.d.ts.map +1 -1
  28. package/dist/db/process-leaf-outcome-snapshot-repo.d.ts +2 -1
  29. package/dist/db/process-leaf-outcome-snapshot-repo.d.ts.map +1 -1
  30. package/dist/db/process-project-repo.d.ts +2 -0
  31. package/dist/db/process-project-repo.d.ts.map +1 -1
  32. package/dist/db/process-skill-repo.d.ts.map +1 -1
  33. package/dist/db/process-turn-annotation-repo.d.ts +4 -1
  34. package/dist/db/process-turn-annotation-repo.d.ts.map +1 -1
  35. package/dist/db/process-turn-record-repo.d.ts +4 -0
  36. package/dist/db/process-turn-record-repo.d.ts.map +1 -1
  37. package/dist/db/publication-repo.d.ts +5 -0
  38. package/dist/db/publication-repo.d.ts.map +1 -0
  39. package/dist/db/repositories.d.ts +12 -0
  40. package/dist/db/repositories.d.ts.map +1 -1
  41. package/dist/db/schema.d.ts +693 -2
  42. package/dist/db/schema.d.ts.map +1 -1
  43. package/dist/db/scoped-settings-repo.d.ts +37 -0
  44. package/dist/db/scoped-settings-repo.d.ts.map +1 -0
  45. package/dist/db/skill-repo.d.ts +3 -0
  46. package/dist/db/skill-repo.d.ts.map +1 -1
  47. package/dist/db/turn-start-record-repo.d.ts +2 -1
  48. package/dist/db/turn-start-record-repo.d.ts.map +1 -1
  49. package/dist/db/turn-summary-repo.d.ts +10 -1
  50. package/dist/db/turn-summary-repo.d.ts.map +1 -1
  51. package/dist/db/worker-lease-repo.d.ts +2 -1
  52. package/dist/db/worker-lease-repo.d.ts.map +1 -1
  53. package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
  54. package/dist/domain-logic/worker-lease-lifecycle.d.ts.map +1 -1
  55. package/dist/external-source-service.d.ts.map +1 -1
  56. package/dist/future-execution/execution.d.ts +4 -1
  57. package/dist/future-execution/execution.d.ts.map +1 -1
  58. package/dist/future-execution/launch-lifecycle.d.ts +9 -25
  59. package/dist/future-execution/launch-lifecycle.d.ts.map +1 -1
  60. package/dist/future-execution/lifecycle.d.ts +3 -28
  61. package/dist/future-execution/lifecycle.d.ts.map +1 -1
  62. package/dist/future-execution/model-projection.d.ts.map +1 -1
  63. package/dist/future-execution/reconciliation.d.ts +1 -0
  64. package/dist/future-execution/reconciliation.d.ts.map +1 -1
  65. package/dist/future-execution-presenter.d.ts.map +1 -1
  66. package/dist/index.js +2 -2
  67. package/dist/integration-tool-registry.d.ts +11 -2
  68. package/dist/integration-tool-registry.d.ts.map +1 -1
  69. package/dist/launch-coordinator.d.ts.map +1 -1
  70. package/dist/launcher-model-config-service.d.ts +7 -1
  71. package/dist/launcher-model-config-service.d.ts.map +1 -1
  72. package/dist/leaf-outcome-snapshot-capture.d.ts +1 -0
  73. package/dist/leaf-outcome-snapshot-capture.d.ts.map +1 -1
  74. package/dist/main.js +3 -3
  75. package/dist/primary-path-snapshot.d.ts.map +1 -1
  76. package/dist/process-action-presenter.d.ts +1 -6
  77. package/dist/process-action-presenter.d.ts.map +1 -1
  78. package/dist/process-action-registry.d.ts.map +1 -1
  79. package/dist/process-engine/decision.d.ts +2 -0
  80. package/dist/process-engine/decision.d.ts.map +1 -1
  81. package/dist/process-engine/engine.d.ts.map +1 -1
  82. package/dist/process-engine/mapped-turns.d.ts.map +1 -1
  83. package/dist/process-engine/ops/automatic-retry-startup.d.ts +8 -0
  84. package/dist/process-engine/ops/automatic-retry-startup.d.ts.map +1 -0
  85. package/dist/process-engine/ops/continue-failed-turn.d.ts.map +1 -1
  86. package/dist/process-engine/ops/process-observation.d.ts +6 -0
  87. package/dist/process-engine/ops/process-observation.d.ts.map +1 -0
  88. package/dist/process-engine/ops/retry-failed-turn.d.ts.map +1 -1
  89. package/dist/process-engine/ops/retry-startup.d.ts +2 -0
  90. package/dist/process-engine/ops/retry-startup.d.ts.map +1 -1
  91. package/dist/process-engine/ops/start-process.d.ts +2 -0
  92. package/dist/process-engine/ops/start-process.d.ts.map +1 -1
  93. package/dist/process-engine/ops/turn-outcome.d.ts.map +1 -1
  94. package/dist/process-engine/ops/turn-wait.d.ts +30 -0
  95. package/dist/process-engine/ops/turn-wait.d.ts.map +1 -0
  96. package/dist/process-engine/ops/update-model-config.d.ts +9 -0
  97. package/dist/process-engine/ops/update-model-config.d.ts.map +1 -0
  98. package/dist/process-engine/recorder.d.ts.map +1 -1
  99. package/dist/process-engine/turn-start-preflight.d.ts +6 -0
  100. package/dist/process-engine/turn-start-preflight.d.ts.map +1 -1
  101. package/dist/process-engine/turn-wait-state.d.ts +24 -0
  102. package/dist/process-engine/turn-wait-state.d.ts.map +1 -0
  103. package/dist/process-engine/turn-worker-requirement.d.ts +2 -0
  104. package/dist/process-engine/turn-worker-requirement.d.ts.map +1 -1
  105. package/dist/process-engine/types.d.ts +13 -2
  106. package/dist/process-engine/types.d.ts.map +1 -1
  107. package/dist/process-engine/writes/build-process-abort-writes.d.ts +3 -1
  108. package/dist/process-engine/writes/build-process-abort-writes.d.ts.map +1 -1
  109. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts +2 -0
  110. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts.map +1 -1
  111. package/dist/process-engine/writes/commit-writes.d.ts +2 -0
  112. package/dist/process-engine/writes/commit-writes.d.ts.map +1 -1
  113. package/dist/process-engine/writes/writes.d.ts +28 -60
  114. package/dist/process-engine/writes/writes.d.ts.map +1 -1
  115. package/dist/process-graph.d.ts +1 -1
  116. package/dist/process-graph.d.ts.map +1 -1
  117. package/dist/process-inspection-reader.d.ts +21 -0
  118. package/dist/process-inspection-reader.d.ts.map +1 -0
  119. package/dist/process-inspection-trace.d.ts +13 -0
  120. package/dist/process-inspection-trace.d.ts.map +1 -0
  121. package/dist/process-inspection.d.ts +22 -0
  122. package/dist/process-inspection.d.ts.map +1 -0
  123. package/dist/process-instance-tree-presenter.d.ts +4 -3
  124. package/dist/process-instance-tree-presenter.d.ts.map +1 -1
  125. package/dist/process-launch-executor.d.ts +2 -0
  126. package/dist/process-launch-executor.d.ts.map +1 -1
  127. package/dist/process-launch-plan.d.ts +1 -0
  128. package/dist/process-launch-plan.d.ts.map +1 -1
  129. package/dist/process-launcher-registry.d.ts.map +1 -1
  130. package/dist/process-model-availability-recovery.d.ts +1 -1
  131. package/dist/process-model-availability-recovery.d.ts.map +1 -1
  132. package/dist/process-model-config.d.ts +19 -0
  133. package/dist/process-model-config.d.ts.map +1 -0
  134. package/dist/process-model-policy/construct.d.ts.map +1 -1
  135. package/dist/process-model-policy/evaluate.d.ts.map +1 -1
  136. package/dist/process-model-policy/index.d.ts +4 -1
  137. package/dist/process-model-policy/index.d.ts.map +1 -1
  138. package/dist/process-model-policy/project.d.ts.map +1 -1
  139. package/dist/process-model-policy/types.d.ts +18 -0
  140. package/dist/process-model-policy/types.d.ts.map +1 -1
  141. package/dist/process-model-policy-presenter.d.ts.map +1 -1
  142. package/dist/process-runtime-availability.d.ts +2 -1
  143. package/dist/process-runtime-availability.d.ts.map +1 -1
  144. package/dist/process-turn-trace.d.ts.map +1 -1
  145. package/dist/process-ui-snapshot-presenter.d.ts +14 -3
  146. package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
  147. package/dist/routes/dashboard.d.ts +17 -0
  148. package/dist/routes/dashboard.d.ts.map +1 -0
  149. package/dist/routes/future-executions.d.ts.map +1 -1
  150. package/dist/routes/launchers.d.ts.map +1 -1
  151. package/dist/routes/process-actions.d.ts.map +1 -1
  152. package/dist/routes/process-detail.d.ts.map +1 -1
  153. package/dist/routes/process-model-config.d.ts +4 -0
  154. package/dist/routes/process-model-config.d.ts.map +1 -0
  155. package/dist/routes/process-route-helpers.d.ts +8 -9
  156. package/dist/routes/process-route-helpers.d.ts.map +1 -1
  157. package/dist/routes/processes.d.ts.map +1 -1
  158. package/dist/routes/settings.d.ts +11 -0
  159. package/dist/routes/settings.d.ts.map +1 -0
  160. package/dist/routes/ticket-creation.d.ts.map +1 -1
  161. package/dist/scoped-settings-service.d.ts +98 -0
  162. package/dist/scoped-settings-service.d.ts.map +1 -0
  163. package/dist/server-bootstrap/build-host-capabilities.d.ts +2 -0
  164. package/dist/server-bootstrap/build-host-capabilities.d.ts.map +1 -1
  165. package/dist/server-bootstrap/register-http.d.ts.map +1 -1
  166. package/dist/session-summary-projection.d.ts.map +1 -1
  167. package/dist/skills/bundled-skills.d.ts +5 -0
  168. package/dist/skills/bundled-skills.d.ts.map +1 -0
  169. package/dist/skills/catalog-service.d.ts +1 -0
  170. package/dist/skills/catalog-service.d.ts.map +1 -1
  171. package/dist/skills/extension-importer.d.ts +11 -0
  172. package/dist/skills/extension-importer.d.ts.map +1 -0
  173. package/dist/skills/launch-skills.d.ts +11 -0
  174. package/dist/skills/launch-skills.d.ts.map +1 -0
  175. package/dist/skills/skill-frontmatter.d.ts +7 -0
  176. package/dist/skills/skill-frontmatter.d.ts.map +1 -1
  177. package/dist/skills/source-importer.d.ts +3 -0
  178. package/dist/skills/source-importer.d.ts.map +1 -1
  179. package/dist/startup-evidence.d.ts +4 -3
  180. package/dist/startup-evidence.d.ts.map +1 -1
  181. package/dist/startup-retry-policy.d.ts +8 -0
  182. package/dist/startup-retry-policy.d.ts.map +1 -0
  183. package/dist/supervisor/execution-inspection-ingestor.d.ts +9 -0
  184. package/dist/supervisor/execution-inspection-ingestor.d.ts.map +1 -0
  185. package/dist/supervisor/ipc-handler.d.ts +2 -0
  186. package/dist/supervisor/ipc-handler.d.ts.map +1 -1
  187. package/dist/supervisor/worker-event-ingestor.d.ts +1 -0
  188. package/dist/supervisor/worker-event-ingestor.d.ts.map +1 -1
  189. package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
  190. package/dist/supervisor/worker-supervisor.d.ts +2 -0
  191. package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
  192. package/dist/test-helpers/prepared-fixture-start.d.ts.map +1 -1
  193. package/dist/test-helpers/process-model-fixtures.d.ts +1 -0
  194. package/dist/test-helpers/process-model-fixtures.d.ts.map +1 -1
  195. package/dist/test-helpers/scoped-settings-fixtures.d.ts +16 -0
  196. package/dist/test-helpers/scoped-settings-fixtures.d.ts.map +1 -0
  197. package/dist/test-helpers/skill-fixtures.d.ts +4 -0
  198. package/dist/test-helpers/skill-fixtures.d.ts.map +1 -0
  199. package/dist/testing.js +2 -1
  200. package/dist/testing.js.map +1 -1
  201. package/dist/tool-approval-gate.d.ts +3 -0
  202. package/dist/tool-approval-gate.d.ts.map +1 -1
  203. package/dist/turn-progress.d.ts.map +1 -1
  204. package/dist/turn-wait-service.d.ts +28 -0
  205. package/dist/turn-wait-service.d.ts.map +1 -0
  206. package/dist/worker-runtime-profile-selection.d.ts +2 -0
  207. package/dist/worker-runtime-profile-selection.d.ts.map +1 -1
  208. package/package.json +20 -19
  209. package/src/app.ts +177 -25
  210. package/src/auth/fastify-auth.ts +13 -0
  211. package/src/config/config-loader.ts +42 -2
  212. package/src/config/config-types.ts +16 -1
  213. package/src/config/kubernetes-docker-isolation.ts +21 -0
  214. package/src/dashboard-model-issues.ts +199 -0
  215. package/src/db/database.ts +131 -40
  216. package/src/db/execution-inspection-repo.ts +163 -0
  217. package/src/db/history-window.ts +18 -0
  218. package/src/db/process-event-repo.ts +88 -10
  219. package/src/db/process-instance-repo.ts +33 -2
  220. package/src/db/process-leaf-outcome-snapshot-repo.ts +8 -2
  221. package/src/db/process-project-repo.ts +4 -0
  222. package/src/db/process-skill-repo.ts +9 -2
  223. package/src/db/process-turn-annotation-repo.ts +27 -9
  224. package/src/db/process-turn-record-repo.ts +45 -12
  225. package/src/db/publication-repo.ts +63 -0
  226. package/src/db/repositories.ts +17 -0
  227. package/src/db/schema.ts +103 -1
  228. package/src/db/scoped-settings-repo.ts +234 -0
  229. package/src/db/skill-repo.ts +89 -27
  230. package/src/db/turn-start-record-repo.ts +13 -3
  231. package/src/db/turn-summary-repo.ts +36 -4
  232. package/src/db/worker-lease-repo.ts +8 -2
  233. package/src/domain-logic/outcome-tools.ts +18 -216
  234. package/src/domain-logic/worker-lease-lifecycle.ts +2 -1
  235. package/src/external-source-service.ts +33 -7
  236. package/src/future-execution/execution.ts +9 -2
  237. package/src/future-execution/launch-lifecycle.ts +87 -78
  238. package/src/future-execution/lifecycle.ts +5 -34
  239. package/src/future-execution/model-projection.ts +2 -0
  240. package/src/future-execution/reconciliation.ts +7 -0
  241. package/src/future-execution-presenter.ts +7 -8
  242. package/src/integration-tool-registry.ts +59 -26
  243. package/src/launch-coordinator.ts +10 -1
  244. package/src/launcher-model-config-service.ts +24 -3
  245. package/src/leaf-outcome-snapshot-capture.ts +2 -0
  246. package/src/primary-path-snapshot.ts +3 -4
  247. package/src/process-action-presenter.ts +22 -57
  248. package/src/process-action-registry.ts +31 -39
  249. package/src/process-engine/decision.ts +10 -0
  250. package/src/process-engine/engine.ts +25 -2
  251. package/src/process-engine/mapped-turns.ts +5 -7
  252. package/src/process-engine/ops/automatic-retry-startup.ts +93 -0
  253. package/src/process-engine/ops/continue-failed-turn.ts +2 -13
  254. package/src/process-engine/ops/process-observation.ts +119 -0
  255. package/src/process-engine/ops/retry-failed-turn.ts +2 -12
  256. package/src/process-engine/ops/retry-startup.ts +20 -16
  257. package/src/process-engine/ops/start-process.ts +34 -2
  258. package/src/process-engine/ops/turn-outcome.ts +61 -60
  259. package/src/process-engine/ops/turn-wait.ts +225 -0
  260. package/src/process-engine/ops/update-model-config.ts +66 -0
  261. package/src/process-engine/recorder.ts +34 -5
  262. package/src/process-engine/turn-start-preflight.ts +168 -105
  263. package/src/process-engine/turn-wait-state.ts +126 -0
  264. package/src/process-engine/turn-worker-requirement.ts +4 -0
  265. package/src/process-engine/types.ts +21 -0
  266. package/src/process-engine/writes/build-process-abort-writes.ts +30 -20
  267. package/src/process-engine/writes/build-turn-outcome-writes.ts +39 -2
  268. package/src/process-engine/writes/commit-writes.ts +16 -4
  269. package/src/process-engine/writes/writes.ts +59 -74
  270. package/src/process-graph.ts +0 -1
  271. package/src/process-inspection-reader.ts +305 -0
  272. package/src/process-inspection-trace.ts +176 -0
  273. package/src/process-inspection.ts +169 -0
  274. package/src/process-instance-tree-presenter.ts +56 -216
  275. package/src/process-launch-executor.ts +5 -2
  276. package/src/process-launch-plan.ts +17 -2
  277. package/src/process-launcher-registry.ts +28 -16
  278. package/src/process-model-availability-recovery.ts +6 -4
  279. package/src/process-model-config.ts +100 -0
  280. package/src/process-model-policy/construct.ts +1 -0
  281. package/src/process-model-policy/evaluate.ts +34 -32
  282. package/src/process-model-policy/index.ts +45 -3
  283. package/src/process-model-policy/project.ts +62 -76
  284. package/src/process-model-policy/types.ts +26 -0
  285. package/src/process-model-policy-presenter.ts +7 -3
  286. package/src/process-runtime-availability.ts +9 -4
  287. package/src/process-turn-trace.ts +12 -24
  288. package/src/process-ui-snapshot-presenter.ts +210 -295
  289. package/src/routes/dashboard.ts +106 -0
  290. package/src/routes/future-executions.ts +5 -5
  291. package/src/routes/launchers.ts +51 -1
  292. package/src/routes/process-actions.ts +10 -10
  293. package/src/routes/process-detail.ts +25 -2
  294. package/src/routes/process-model-config.ts +70 -0
  295. package/src/routes/process-route-helpers.ts +153 -162
  296. package/src/routes/processes.ts +2 -0
  297. package/src/routes/settings.ts +173 -0
  298. package/src/routes/ticket-creation.ts +28 -5
  299. package/src/scoped-settings-service.ts +797 -0
  300. package/src/server-bootstrap/build-host-capabilities.ts +42 -0
  301. package/src/server-bootstrap/register-http.ts +12 -1
  302. package/src/session-summary-projection.ts +1 -2
  303. package/src/skills/bundled-skills.ts +64 -0
  304. package/src/skills/catalog-service.ts +35 -6
  305. package/src/skills/extension-importer.ts +94 -0
  306. package/src/skills/launch-skills.ts +19 -0
  307. package/src/skills/skill-frontmatter.ts +19 -6
  308. package/src/skills/source-importer.ts +7 -13
  309. package/src/startup-evidence.ts +29 -8
  310. package/src/startup-retry-policy.ts +106 -0
  311. package/src/supervisor/execution-inspection-ingestor.ts +55 -0
  312. package/src/supervisor/ipc-handler.ts +2 -0
  313. package/src/supervisor/worker-event-ingestor.ts +30 -14
  314. package/src/supervisor/worker-start-payload-builder.ts +17 -0
  315. package/src/supervisor/worker-supervisor.ts +26 -1
  316. package/src/test-helpers/prepared-fixture-start.ts +1 -0
  317. package/src/test-helpers/process-model-fixtures.ts +2 -0
  318. package/src/test-helpers/scoped-settings-fixtures.ts +126 -0
  319. package/src/test-helpers/skill-fixtures.ts +28 -0
  320. package/src/tool-approval-gate.ts +16 -0
  321. package/src/turn-progress.ts +3 -2
  322. package/src/turn-wait-service.ts +228 -0
  323. package/src/worker-runtime-profile-selection.ts +8 -1
  324. package/dist/app-MSJXFK33.js +0 -8
  325. package/dist/chunk-HK6BLLHA.js.map +0 -1
  326. package/dist/chunk-QCHS3X33.js.map +0 -1
  327. /package/dist/{app-MSJXFK33.js.map → app-KR3N2JAE.js.map} +0 -0
@@ -20,12 +20,12 @@ import {
20
20
  import { parseStructuralProcessState } from "@leitwerk-dev/process-sdk";
21
21
  import {
22
22
  buildActiveTimelineTurnSummary,
23
- buildLiveTurnProjectionFromEvents,
24
23
  buildUsageSnapshotsByTurnRecordId,
25
24
  type CompactActiveTurnSnapshot,
26
25
  type CompactTurnSummary,
27
26
  type CurrentProcessErrorSummary,
28
27
  type CurrentTurnRecoverySummary,
28
+ createLiveTurnProjection,
29
29
  emptyCompactTurnSummary,
30
30
  extractFirstUserPromptOnBranch,
31
31
  extractInitialPromptFromParamsJson,
@@ -47,7 +47,6 @@ import {
47
47
  type ReadonlyEntryTree,
48
48
  reasoningPreviewTail,
49
49
  resolveTurnContinuationUserPrompt,
50
- snapshotTurnTrace,
51
50
  type TurnReasoningDetailResponseBody,
52
51
  type TurnTracePreview,
53
52
  type TurnTraceSnapshot,
@@ -59,6 +58,7 @@ import { getScheduledActionDetailForProcess } from "./future-execution-presenter
59
58
  import { physicalWorkerStarts } from "./physical-worker-starts.js";
60
59
  import type { ReadonlyPiSessionTree } from "./pi-session-tree.js";
61
60
  import { listVisibleActionsForProcess } from "./process-action-presenter.js";
61
+ import { pendingTurnWait } from "./process-engine/turn-wait-state.js";
62
62
  import { resolveCurrentExecutionTurnRecordId } from "./process-execution.js";
63
63
  import { buildProcessFlowViewForProcess, getProcessGraph } from "./process-graph.js";
64
64
  import { presentProcessInstanceTree } from "./process-instance-tree-presenter.js";
@@ -80,40 +80,6 @@ import { presentSessionTransferOperation } from "./session-transfer-service.js";
80
80
  import { buildStartupEvidence, presentProcessStartupSummary } from "./startup-evidence.js";
81
81
  import { normalizeTurnProgressLinks, normalizeTurnProgressReport } from "./turn-progress.js";
82
82
 
83
- const COMPACT_DETAIL_EVENT_TYPES = [
84
- "worker_capacity_queued",
85
- "turn_outcome_recorded",
86
- "lifecycle_parked",
87
- "worker_failed",
88
- "external_trigger_listener_armed",
89
- "external_source_armed",
90
- "external_trigger_failed",
91
- "external_source_failed",
92
- "external_trigger_consumed",
93
- "external_source_consumed",
94
- ] as const;
95
-
96
- function sortEventsAscending(events: readonly ProcessEvent[]): ProcessEvent[] {
97
- return [...events].sort((left, right) => {
98
- const createdAtComparison = left.createdAt.localeCompare(right.createdAt);
99
- return createdAtComparison !== 0 ? createdAtComparison : left.id.localeCompare(right.id);
100
- });
101
- }
102
-
103
- function compactDetailEvents(
104
- deps: Pick<RouteDeps, "events">,
105
- instanceId: string,
106
- turnRecords: readonly ProcessTurnRecord[],
107
- ): ProcessEvent[] {
108
- return sortEventsAscending([
109
- ...deps.events.listByInstanceEventTypes(instanceId, COMPACT_DETAIL_EVENT_TYPES, 1_000),
110
- ...turnRecords.flatMap((turn) => {
111
- const event = deps.events.latestByTurnRecordEventType(instanceId, turn.id, "turn.progress");
112
- return event ? [event] : [];
113
- }),
114
- ]);
115
- }
116
-
117
83
  function compactPrimaryPathSnapshot(
118
84
  snapshot: PrimaryPathSnapshot | PrimaryPathUiSnapshot,
119
85
  ): PrimaryPathUiSnapshot {
@@ -191,52 +157,23 @@ function paramsRecord(value: unknown): Record<string, unknown> {
191
157
  return asRecord(value) ?? {};
192
158
  }
193
159
 
194
- function normalizeOutput(value: string): string {
195
- return value.trim();
196
- }
197
-
198
- function turnRecordIdFromEvent(event: ProcessEvent): string {
199
- return stringValue(paramsRecord(event.data).turnRecordId);
200
- }
201
-
202
- function createTurnOutcomeEventMap(events: readonly ProcessEvent[]): Map<string, ProcessEvent> {
203
- const map = new Map<string, ProcessEvent>();
204
- for (const event of events) {
205
- if (event.eventType !== "turn_outcome_recorded") {
206
- continue;
207
- }
208
- const turnRecordId = turnRecordIdFromEvent(event);
209
- if (turnRecordId) {
210
- map.set(turnRecordId, event);
211
- }
212
- }
213
- return map;
160
+ function indexByTurnRecordId<T>(
161
+ records: readonly T[],
162
+ key: (record: T) => string | null,
163
+ ): Map<string, T> {
164
+ return new Map(
165
+ records.flatMap((record) => {
166
+ const id = key(record);
167
+ return id ? [[id, record] as const] : [];
168
+ }),
169
+ );
214
170
  }
215
171
 
216
172
  function turnRecordIdFromAnnotation(annotation: ProcessTurnAnnotation): string | null {
217
- for (const reference of annotation.references) {
218
- if (reference.kind === "turn_record") {
219
- return reference.turnRecordId;
220
- }
221
- }
222
- return null;
223
- }
224
-
225
- function createTurnAnnotationMap(
226
- turnAnnotations: readonly ProcessTurnAnnotation[],
227
- annotationType: string,
228
- ): Map<string, ProcessTurnAnnotation> {
229
- const map = new Map<string, ProcessTurnAnnotation>();
230
- for (const annotation of turnAnnotations) {
231
- if (annotation.annotationType !== annotationType) {
232
- continue;
233
- }
234
- const turnRecordId = turnRecordIdFromAnnotation(annotation);
235
- if (turnRecordId) {
236
- map.set(turnRecordId, annotation);
237
- }
238
- }
239
- return map;
173
+ return (
174
+ annotation.references.find((reference) => reference.kind === "turn_record")?.turnRecordId ??
175
+ null
176
+ );
240
177
  }
241
178
 
242
179
  function outputFromTurnParams(
@@ -263,16 +200,9 @@ function summarizeTurnOutcome(
263
200
  }
264
201
 
265
202
  function fallbackOutcomeFromTurnRecord(turnRecord: ProcessTurnRecord): string {
266
- switch (turnRecord.status) {
267
- case "failed":
268
- return "failed";
269
- case "superseded":
270
- return "superseded";
271
- case "succeeded":
272
- return "succeeded";
273
- default:
274
- return "completed";
275
- }
203
+ return ["failed", "superseded", "succeeded"].includes(turnRecord.status)
204
+ ? turnRecord.status
205
+ : "completed";
276
206
  }
277
207
 
278
208
  function fallbackSummaryFromTurnRecord(turnRecord: ProcessTurnRecord): string {
@@ -403,47 +333,6 @@ function effectiveTurnRecordForDisplay(
403
333
  };
404
334
  }
405
335
 
406
- function durableTurnLineage(turnRecord: ProcessTurnRecord) {
407
- return {
408
- attemptNumber: turnRecord.attemptNumber,
409
- parentTurnRecordId: turnRecord.parentTurnRecordId,
410
- startedAt: turnRecord.startedAt,
411
- endedAt: turnRecord.endedAt,
412
- };
413
- }
414
-
415
- function createCompletedTurnRecord(args: {
416
- turnRecord: ProcessTurnRecord;
417
- turnId: string;
418
- displayTurn?: string;
419
- outcome: string;
420
- summary: string;
421
- output: string;
422
- turnResultMarkdown: string;
423
- createdAt: string;
424
- actionSource: TimelineActionSource;
425
- progress: ProcessTimelineTurnSummary["progress"];
426
- }): ProcessTimelineTurnSummary {
427
- return {
428
- id: args.turnRecord.id,
429
- turnId: args.turnId,
430
- turnType: args.turnRecord.turnType,
431
- displayTurn: args.displayTurn ?? args.turnId,
432
- outcome: args.outcome,
433
- summary: args.summary,
434
- output: normalizeOutput(args.output) || (args.turnResultMarkdown ? "" : args.summary),
435
- turnResultMarkdown: args.turnResultMarkdown,
436
- pathType: args.turnRecord.pathType,
437
- createdAt: args.createdAt,
438
- presentation: timelinePresentationForTurnType(args.turnRecord.turnType),
439
- status: "completed",
440
- modelProfileId: args.turnRecord.modelProfileId ?? null,
441
- actionSource: args.actionSource,
442
- progress: args.progress,
443
- ...durableTurnLineage(args.turnRecord),
444
- };
445
- }
446
-
447
336
  /** @internal */
448
337
  export function presentProcessTimelineTurns(input: {
449
338
  /** @internal */
@@ -463,20 +352,19 @@ export function presentProcessTimelineTurns(input: {
463
352
  /** @internal */
464
353
  activeModelProfileId?: string | null;
465
354
  }): ProcessTimelineTurnSummary[] {
466
- const outcomeEventsByTurnRecordId = createTurnOutcomeEventMap(input.events);
467
- const progressByTurnRecordId = buildTurnProgressIndex(input.events);
468
- const milestoneAnnotationsByTurnRecordId = createTurnAnnotationMap(
469
- input.turnAnnotations,
470
- "turn_milestone",
471
- );
472
- const acceptanceAnnotationsByTurnRecordId = createTurnAnnotationMap(
473
- input.turnAnnotations,
474
- "acceptance_state",
475
- );
476
- const externalTriggerAnnotationsByTurnRecordId = createTurnAnnotationMap(
477
- input.turnAnnotations,
478
- "external_trigger",
355
+ const outcomeEventsByTurnRecordId = indexByTurnRecordId(
356
+ input.events.filter((event) => event.eventType === "turn_outcome_recorded"),
357
+ (event) => stringValue(paramsRecord(event.data).turnRecordId),
479
358
  );
359
+ const progressByTurnRecordId = buildTurnProgressIndex(input.events);
360
+ const annotationMap = (annotationType: string) =>
361
+ indexByTurnRecordId(
362
+ input.turnAnnotations.filter((annotation) => annotation.annotationType === annotationType),
363
+ turnRecordIdFromAnnotation,
364
+ );
365
+ const milestoneAnnotationsByTurnRecordId = annotationMap("turn_milestone");
366
+ const acceptanceAnnotationsByTurnRecordId = annotationMap("acceptance_state");
367
+ const externalTriggerAnnotationsByTurnRecordId = annotationMap("external_trigger");
480
368
  const actionSourceByTurnRecordId = buildActionSourceIndex(
481
369
  input.turnAnnotations,
482
370
  input.turnRecords,
@@ -504,11 +392,23 @@ export function presentProcessTimelineTurns(input: {
504
392
  continue;
505
393
  }
506
394
  const turnRecord = effectiveTurnRecordForDisplay(durableTurnRecord, input.process);
395
+ const common = {
396
+ id: turnRecord.id,
397
+ turnType: turnRecord.turnType,
398
+ pathType: turnRecord.pathType,
399
+ presentation: timelinePresentationForTurnType(turnRecord.turnType),
400
+ modelProfileId: turnRecord.modelProfileId ?? null,
401
+ actionSource: actionSourceByTurnRecordId.get(turnRecord.id) ?? null,
402
+ attemptNumber: turnRecord.attemptNumber,
403
+ parentTurnRecordId: turnRecord.parentTurnRecordId,
404
+ startedAt: turnRecord.startedAt,
405
+ endedAt: turnRecord.endedAt,
406
+ ...progressByTurnRecordId.get(turnRecord.id),
407
+ };
507
408
  if (turnRecord.status === "running") {
508
409
  turns.push({
509
- id: turnRecord.id,
410
+ ...common,
510
411
  turnId: turnRecord.turnId,
511
- turnType: turnRecord.turnType,
512
412
  displayTurn: turnRecord.turnId,
513
413
  outcome: "in_progress",
514
414
  summary: `Current step: ${formatDefinition(turnRecord.turnId)}`,
@@ -517,14 +417,8 @@ export function presentProcessTimelineTurns(input: {
517
417
  ? input.activeTurn.assistant.text.trim()
518
418
  : "",
519
419
  turnResultMarkdown: "",
520
- pathType: turnRecord.pathType,
521
420
  createdAt: turnRecord.startedAt,
522
- presentation: timelinePresentationForTurnType(turnRecord.turnType),
523
421
  status: "in_progress",
524
- modelProfileId: turnRecord.modelProfileId ?? null,
525
- actionSource: actionSourceByTurnRecordId.get(turnRecord.id) ?? null,
526
- ...progressByTurnRecordId.get(turnRecord.id),
527
- ...durableTurnLineage(turnRecord),
528
422
  });
529
423
  continue;
530
424
  }
@@ -535,24 +429,26 @@ export function presentProcessTimelineTurns(input: {
535
429
  const actionAnnotation =
536
430
  acceptanceAnnotationsByTurnRecordId.get(turnRecord.id) ??
537
431
  externalTriggerAnnotationsByTurnRecordId.get(turnRecord.id);
538
- let presentation: Omit<
539
- Parameters<typeof createCompletedTurnRecord>[0],
540
- "turnRecord" | "turnResultMarkdown" | "actionSource" | "progress"
541
- >;
432
+ let presentation: Pick<
433
+ ProcessTimelineTurnSummary,
434
+ "turnId" | "outcome" | "summary" | "output" | "createdAt"
435
+ > & { displayTurn?: string };
542
436
 
543
- if (outcomeEvent) {
544
- const turnId = stringValue(outcomeEvent.data.turnId) || turnRecord.turnId || "turn";
437
+ const recordedOutcome = outcomeEvent ?? (!actionAnnotation ? milestoneAnnotation : null);
438
+ if (recordedOutcome) {
439
+ const data = "data" in recordedOutcome ? recordedOutcome.data : recordedOutcome.payload;
440
+ const turnId = stringValue(data.turnId) || turnRecord.turnId || "turn";
545
441
  const outcome =
546
- stringValue(outcomeEvent.data.outcome) ||
442
+ stringValue(data.outcome) ||
547
443
  stringValue(milestoneAnnotation?.payload.outcome) ||
548
444
  fallbackOutcomeFromTurnRecord(turnRecord);
549
- const params = paramsRecord(outcomeEvent.data.params);
445
+ const params = paramsRecord(outcomeEvent?.data.params);
550
446
  presentation = {
551
447
  turnId,
552
448
  outcome,
553
449
  summary: summarizeTurnOutcome(turnId, outcome, params),
554
450
  output: outputFromTurnParams(params, { includeMarkdownFields: !turnResultMarkdown }),
555
- createdAt: turnRecord.endedAt ?? outcomeEvent.createdAt ?? turnRecord.startedAt,
451
+ createdAt: turnRecord.endedAt ?? recordedOutcome.createdAt ?? turnRecord.startedAt,
556
452
  };
557
453
  } else if (actionAnnotation) {
558
454
  const label = annotationLabel(actionAnnotation);
@@ -568,18 +464,6 @@ export function presentProcessTimelineTurns(input: {
568
464
  output: annotationOutput(actionAnnotation),
569
465
  createdAt: actionAnnotation.createdAt ?? turnRecord.endedAt ?? turnRecord.startedAt,
570
466
  };
571
- } else if (milestoneAnnotation) {
572
- const turnId = stringValue(milestoneAnnotation.payload.turnId) || turnRecord.turnId || "turn";
573
- const outcome =
574
- stringValue(milestoneAnnotation.payload.outcome) ||
575
- fallbackOutcomeFromTurnRecord(turnRecord);
576
- presentation = {
577
- turnId,
578
- outcome,
579
- summary: summarizeTurnOutcome(turnId, outcome, {}),
580
- output: "",
581
- createdAt: turnRecord.endedAt ?? milestoneAnnotation.createdAt ?? turnRecord.startedAt,
582
- };
583
467
  } else {
584
468
  const summary = fallbackSummaryFromTurnRecord(turnRecord);
585
469
  presentation = {
@@ -590,7 +474,12 @@ export function presentProcessTimelineTurns(input: {
590
474
  createdAt: turnRecord.endedAt ?? turnRecord.startedAt,
591
475
  };
592
476
  }
477
+ const payload = actionAnnotation?.payload ?? milestoneAnnotation?.payload;
478
+ const selectedTurnId =
479
+ stringValue(payload?.selectedTurnIdAfter) || stringValue(payload?.causedSelectedTurnId);
480
+ const targetTurnRecordId = stringValue(payload?.targetTurnRecordId);
593
481
  turns.push({
482
+ ...common,
594
483
  resources:
595
484
  normalizeTurnProgressLinks(
596
485
  paramsRecord(actionAnnotation?.payload.eventDescription).links,
@@ -598,32 +487,24 @@ export function presentProcessTimelineTurns(input: {
598
487
  ...(stringValue(actionAnnotation?.payload.sourceTurnRecordId)
599
488
  ? { reviewedTurnRecordId: stringValue(actionAnnotation?.payload.sourceTurnRecordId) }
600
489
  : {}),
601
- ...(() => {
602
- const payload = actionAnnotation?.payload ?? milestoneAnnotation?.payload;
603
- const selectedTurnId =
604
- stringValue(payload?.selectedTurnIdAfter) || stringValue(payload?.causedSelectedTurnId);
605
- const targetTurnRecordId = stringValue(payload?.targetTurnRecordId);
606
- return selectedTurnId
607
- ? {
608
- transition: {
609
- selectedTurnId,
610
- ...(targetTurnRecordId ? { targetTurnRecordId } : {}),
611
- accepted: Boolean(
612
- targetTurnRecordId &&
613
- input.turnRecords.some((record) => record.id === targetTurnRecordId),
614
- ),
615
- },
616
- }
617
- : {};
618
- })(),
619
- ...createCompletedTurnRecord({
620
- ...presentation,
621
- turnRecord,
622
- turnResultMarkdown,
623
- actionSource: actionSourceByTurnRecordId.get(turnRecord.id) ?? null,
624
- progress: progressByTurnRecordId.get(turnRecord.id)?.progress ?? null,
625
- }),
626
- ...progressByTurnRecordId.get(turnRecord.id),
490
+ ...(selectedTurnId
491
+ ? {
492
+ transition: {
493
+ selectedTurnId,
494
+ ...(targetTurnRecordId ? { targetTurnRecordId } : {}),
495
+ accepted: Boolean(
496
+ targetTurnRecordId &&
497
+ input.turnRecords.some((record) => record.id === targetTurnRecordId),
498
+ ),
499
+ },
500
+ }
501
+ : {}),
502
+ ...presentation,
503
+ displayTurn: presentation.displayTurn ?? presentation.turnId,
504
+ output: presentation.output.trim() || (turnResultMarkdown ? "" : presentation.summary),
505
+ turnResultMarkdown,
506
+ status: "completed",
507
+ progress: common.progress ?? null,
627
508
  ...(stringValue(milestoneAnnotation?.payload.resultSummary)
628
509
  ? { resultSummary: stringValue(milestoneAnnotation?.payload.resultSummary) }
629
510
  : {}),
@@ -648,28 +529,21 @@ export function presentProcessTimelineTurns(input: {
648
529
  ) {
649
530
  const turnId = input.process.selectedTurnId ?? "unknown";
650
531
  turns.push({
651
- id: input.currentExecutionTurnRecordId ?? `current:${turnId}`,
652
- turnId,
653
- turnType: input.selectedTurnType ?? "llm",
654
- displayTurn: turnId,
655
- outcome: "in_progress",
656
- summary: `Current step: ${formatDefinition(turnId)}`,
657
- output: "",
658
- turnResultMarkdown: "",
659
- pathType: "primary",
660
- createdAt: input.process.updatedAt,
661
- presentation: timelinePresentationForTurnType(input.selectedTurnType ?? "llm"),
662
- status: "in_progress",
532
+ ...buildActiveTimelineTurnSummary(
533
+ {
534
+ turnRecordId: input.currentExecutionTurnRecordId ?? `current:${turnId}`,
535
+ turnId,
536
+ turnType: input.selectedTurnType ?? "llm",
537
+ pathType: "primary",
538
+ startedAt: input.process.updatedAt,
539
+ assistant: { text: "", thinking: "", lastUpdatedAt: null },
540
+ },
541
+ { summary: `Current step: ${formatDefinition(turnId)}`, output: "" },
542
+ ),
663
543
  modelProfileId:
664
544
  input.activeModelProfileId === undefined
665
545
  ? (input.process.selectedTurnModelProfileId ?? null)
666
546
  : input.activeModelProfileId,
667
- attemptNumber: 1,
668
- parentTurnRecordId: null,
669
- startedAt: input.process.updatedAt,
670
- endedAt: null,
671
- actionSource: null,
672
- progress: null,
673
547
  });
674
548
  }
675
549
 
@@ -739,46 +613,28 @@ export function buildExternalTriggerSignals(args: {
739
613
  ) ?? null;
740
614
  const latestConfiguredEvent = latestArmedEvent ?? latestEvent;
741
615
  const configuredDetail = setupDetail(latestConfiguredEvent);
742
-
616
+ let state: ProcessExternalTriggerSignal["state"] = "waiting";
617
+ let occurredAt: string | null = null;
618
+ let secondaryDetail = configuredDetail;
743
619
  if (
744
620
  latestEvent?.eventType === "external_trigger_failed" ||
745
621
  latestEvent?.eventType === "external_source_failed"
746
622
  ) {
747
- return {
748
- triggerId: trigger.id,
749
- state: "error",
750
- occurredAt: latestEvent.createdAt,
751
- secondaryDetail: joinDetails(stringValueOrNull(latestEvent.data.message), configuredDetail),
752
- };
753
- }
754
-
755
- if (args.isWaitingForSelectedTurn) {
756
- return {
757
- triggerId: trigger.id,
758
- state: "armed",
759
- occurredAt: latestArmedEvent?.createdAt ?? null,
760
- secondaryDetail: configuredDetail ?? "No trigger attempts recorded yet.",
761
- };
762
- }
763
-
764
- if (
623
+ state = "error";
624
+ occurredAt = latestEvent.createdAt;
625
+ secondaryDetail = joinDetails(stringValueOrNull(latestEvent.data.message), configuredDetail);
626
+ } else if (args.isWaitingForSelectedTurn) {
627
+ state = "armed";
628
+ occurredAt = latestArmedEvent?.createdAt ?? null;
629
+ secondaryDetail = configuredDetail ?? "No trigger attempts recorded yet.";
630
+ } else if (
765
631
  latestEvent?.eventType === "external_trigger_consumed" ||
766
632
  latestEvent?.eventType === "external_source_consumed"
767
633
  ) {
768
- return {
769
- triggerId: trigger.id,
770
- state: "triggered",
771
- occurredAt: latestEvent.createdAt,
772
- secondaryDetail: configuredDetail,
773
- };
634
+ state = "triggered";
635
+ occurredAt = latestEvent.createdAt;
774
636
  }
775
-
776
- return {
777
- triggerId: trigger.id,
778
- state: "waiting",
779
- occurredAt: null,
780
- secondaryDetail: configuredDetail,
781
- };
637
+ return { triggerId: trigger.id, state, occurredAt, secondaryDetail };
782
638
  });
783
639
  }
784
640
 
@@ -845,6 +701,20 @@ export function formatProcessErrorPresentation(
845
701
  };
846
702
  }
847
703
  const withoutPrefix = raw.replace(/^Turn\s+'[^']*'\s+.*?failed:\s*/i, "").trim() || raw;
704
+ if (
705
+ (!errorClass || errorClass === "llm_error") &&
706
+ withoutPrefix.startsWith("Turn aborted by LLM:")
707
+ ) {
708
+ const reason = withoutPrefix.slice("Turn aborted by LLM:".length).trim();
709
+ return {
710
+ summary: reason
711
+ ? `The agent stopped this turn — ${lowerFirst(reason)}`
712
+ : "The agent stopped this turn",
713
+ guidance:
714
+ "Review the reported reason and resolve any blocker before continuing or retrying the turn.",
715
+ technicalDetail: raw,
716
+ };
717
+ }
848
718
  const detail = extractProviderMessage(withoutPrefix) ?? withoutPrefix;
849
719
  const summary =
850
720
  category && detail.toLowerCase() !== category.toLowerCase()
@@ -949,6 +819,16 @@ export function buildCurrentProcessError(input: {
949
819
  if (input.process.lifecycleStatus !== "error" || input.currentTurnRecovery) {
950
820
  return null;
951
821
  }
822
+ const wait = pendingTurnWait(input.process);
823
+ if (wait?.status === "error")
824
+ return {
825
+ title: input.selectedTurnDescription ?? "Waiting condition",
826
+ summary: wait.message ?? "The waiting condition failed",
827
+ canRetry: true,
828
+ guidance:
829
+ "Fix the condition or its configuration, then retry. Readiness is checked before starting a worker.",
830
+ technicalDetail: null,
831
+ };
952
832
  const latestErrorEvent = input.events.findLast(
953
833
  (event) => event.eventType === "lifecycle_parked" || event.eventType === "worker_failed",
954
834
  );
@@ -998,14 +878,10 @@ export function buildUsageEstimate(input: {
998
878
  totalLlmTurnCount += 1;
999
879
  const isActiveTurn = turnRecord.id === input.currentTurnRecordId;
1000
880
  const turnUsage =
1001
- isActiveTurn && input.activeTurn?.turnType === "llm"
1002
- ? (input.activeTurn.usage ??
1003
- input.tracePreviewsByTurnRecordId[turnRecord.id]?.usage ??
1004
- input.usageByTurnRecordId[turnRecord.id] ??
1005
- null)
1006
- : (input.tracePreviewsByTurnRecordId[turnRecord.id]?.usage ??
1007
- input.usageByTurnRecordId[turnRecord.id] ??
1008
- null);
881
+ (isActiveTurn && input.activeTurn?.turnType === "llm" ? input.activeTurn.usage : null) ??
882
+ input.tracePreviewsByTurnRecordId[turnRecord.id]?.usage ??
883
+ input.usageByTurnRecordId[turnRecord.id] ??
884
+ null;
1009
885
  if (!turnUsage) {
1010
886
  missingUsageTurnCount += 1;
1011
887
  continue;
@@ -1205,6 +1081,7 @@ export function buildProcessUiSnapshotProjections(input: {
1205
1081
 
1206
1082
  type ProcessUiSnapshotDeps = Pick<
1207
1083
  RouteDeps,
1084
+ | "executionInspections"
1208
1085
  | "processes"
1209
1086
  | "projects"
1210
1087
  | "inputs"
@@ -1233,10 +1110,23 @@ type ProcessUiSnapshotDeps = Pick<
1233
1110
  | "config"
1234
1111
  >;
1235
1112
 
1113
+ function includeRecord<T extends { id: string }>(
1114
+ records: T[],
1115
+ id: string | null | undefined,
1116
+ repo: { getById(id: string): T | null },
1117
+ ): void {
1118
+ if (!id || records.some((record) => record.id === id)) return;
1119
+ const record = repo.getById(id);
1120
+ if (record) records.push(record);
1121
+ }
1122
+
1236
1123
  export class ProcessUiSnapshotAssembler {
1237
1124
  constructor(private readonly deps: ProcessUiSnapshotDeps) {}
1238
1125
 
1239
- async assemble(instanceId: string): Promise<ProcessDetailUiSnapshotResponseBody | null> {
1126
+ async assemble(
1127
+ instanceId: string,
1128
+ beforeTurnRecordId?: string,
1129
+ ): Promise<ProcessDetailUiSnapshotResponseBody | null> {
1240
1130
  const process = this.deps.processes.getById(instanceId);
1241
1131
  if (!process) {
1242
1132
  return null;
@@ -1244,23 +1134,41 @@ export class ProcessUiSnapshotAssembler {
1244
1134
  // Capture every synchronous durable read before yielding so one response cannot
1245
1135
  // combine process/turn state from opposite sides of a concurrent mutation.
1246
1136
  const projects = this.deps.projects.listByInstance(instanceId);
1247
- const turnRecords = this.deps.turnRecords.listByInstance(instanceId);
1137
+ const before = beforeTurnRecordId ? this.deps.turnRecords.getById(beforeTurnRecordId) : null;
1138
+ if (beforeTurnRecordId && before?.instanceId !== instanceId) return null;
1139
+ const page = this.deps.turnRecords.listPage(instanceId, before ?? undefined, 41);
1140
+ const hasMore = page.length > 40;
1141
+ const turnRecords = hasMore ? page.slice(1) : page;
1142
+ const turnRecordIds = turnRecords.map((turn) => turn.id);
1143
+ const window = {
1144
+ since: hasMore ? turnRecords[0]?.startedAt : undefined,
1145
+ until: before?.startedAt,
1146
+ };
1248
1147
  const mapped = {
1249
1148
  runs: this.deps.mappedRuns?.listByInstance(instanceId) ?? [],
1250
1149
  items: this.deps.mappedRuns?.listItemsByInstance(instanceId) ?? [],
1251
1150
  };
1252
- const events = compactDetailEvents(this.deps, instanceId, turnRecords);
1253
- const turnAnnotations = this.deps.turnAnnotations.listByInstance(instanceId);
1151
+ const events = this.deps.events.listUiSnapshotEvents(instanceId, turnRecordIds);
1152
+ const turnAnnotations = this.deps.turnAnnotations.listForTimeline(instanceId, window);
1254
1153
  const inputs = this.deps.inputs.listByInstance(instanceId);
1255
- const leafOutcomeSnapshots = this.deps.leafOutcomeSnapshots.listByInstance(instanceId);
1154
+ const leafOutcomeSnapshots = this.deps.leafOutcomeSnapshots.listByInstance(instanceId, window);
1256
1155
  const questionRequests = this.deps.questionRequests.listByInstance(instanceId);
1257
1156
  const toolApprovalRequests = this.deps.toolApprovalRequests.listByInstance(instanceId);
1258
- const workerLeases = this.deps.leases.listByInstance(instanceId);
1259
- const workerLease = workerLeases.find((lease) => lease.exitedAt === null) ?? null;
1260
- const startupTurnStarts = this.deps.turnStarts.listByInstance(instanceId);
1157
+ const workerLeases = this.deps.leases.listByInstance(instanceId, window);
1158
+ const workerLease = this.deps.leases.getByInstance(instanceId);
1159
+ if (workerLease && !workerLeases.some((lease) => lease.id === workerLease.id)) {
1160
+ workerLeases.push(workerLease);
1161
+ }
1162
+ const startupTurnStarts = this.deps.turnStarts.listByInstance(instanceId, window);
1163
+ // A worker starts before its accepted turn. Keep boundary records even when
1164
+ // their timestamps precede the oldest turn on this page.
1165
+ for (const turn of turnRecords) {
1166
+ includeRecord(startupTurnStarts, turn.turnStartRecordId, this.deps.turnStarts);
1167
+ includeRecord(workerLeases, turn.acceptedWorkerLeaseId, this.deps.leases);
1168
+ }
1261
1169
  const selectedTurn = getSelectedTurnSummaryForProcess(this.deps, process);
1262
1170
  const session = this.deps.turnSummaries.getSession(instanceId);
1263
- const summaries = this.deps.turnSummaries.listByInstance(instanceId);
1171
+ const summaries = this.deps.turnSummaries.listByInstance(instanceId, turnRecordIds);
1264
1172
  const currentTurnRecordId = resolveCurrentExecutionTurnRecordId(process, this.deps.turnStarts);
1265
1173
  const activeRecord = turnRecords.find(
1266
1174
  (turn) => turn.id === currentTurnRecordId && turn.status === "running",
@@ -1345,6 +1253,10 @@ export class ProcessUiSnapshotAssembler {
1345
1253
  refreshedAt: annotation.payload.refreshedAt,
1346
1254
  });
1347
1255
  }
1256
+ if (hasMore && projections.usageEstimate) {
1257
+ projections.usageEstimate.isPartial = true;
1258
+ projections.usageEstimate.historyLimited = true;
1259
+ }
1348
1260
  const runDetails = buildProcessRunDetailsView(this.deps, process, projects);
1349
1261
  const navigation = presentProcessTurnNavigation({
1350
1262
  graph: getProcessGraph(this.deps.processGraphs, process.processId),
@@ -1357,12 +1269,17 @@ export class ProcessUiSnapshotAssembler {
1357
1269
 
1358
1270
  return {
1359
1271
  ...projections,
1360
- timeline: { ...projections.timeline, turns: navigation.turns },
1272
+ timeline: {
1273
+ ...projections.timeline,
1274
+ turns: navigation.turns,
1275
+ history: { beforeTurnRecordId: hasMore ? turnRecords[0].id : null },
1276
+ },
1361
1277
  plannedNextTurn: navigation.plannedNextTurn,
1362
1278
  instanceTree: presentProcessInstanceTree({
1363
1279
  process,
1364
1280
  turnRecords,
1365
- turnAnnotations,
1281
+ leases: workerLeases,
1282
+ observations: this.deps.executionInspections.listContextFacts(instanceId, turnRecordIds),
1366
1283
  turnDetails: runDetails.turns,
1367
1284
  currentPiEntryId: session?.leafId ?? null,
1368
1285
  }),
@@ -1391,41 +1308,39 @@ export class ProcessUiSnapshotAssembler {
1391
1308
  instanceId: string;
1392
1309
  turnRecordId: string;
1393
1310
  }): Promise<TurnReasoningDetailResponseBody | null> {
1394
- const process = this.deps.processes.getById(input.instanceId);
1395
- if (!process) {
1396
- return null;
1397
- }
1311
+ return (await this.readReasoningEvidence(input))?.detail ?? null;
1312
+ }
1313
+
1314
+ /** Capture durable facts before loading the session, once for all detail projections. @internal */
1315
+ async readReasoningEvidence(input: {
1316
+ instanceId: string;
1317
+ turnRecordId: string;
1318
+ includeSession?: boolean;
1319
+ }) {
1320
+ if (!this.deps.processes.getById(input.instanceId)) return null;
1398
1321
  const turnRecord = this.deps.turnRecords.getById(input.turnRecordId);
1399
- if (!turnRecord || turnRecord.instanceId !== input.instanceId) {
1400
- return null;
1401
- }
1322
+ if (!turnRecord || turnRecord.instanceId !== input.instanceId) return null;
1402
1323
  const throughEventSequence = this.deps.events.latestSequence(input.instanceId);
1403
1324
  const events = this.deps.events.listByTurnRecord(input.instanceId, input.turnRecordId);
1404
- if (turnRecord.status === "running") {
1405
- const trace = snapshotTurnTrace(buildLiveTurnProjectionFromEvents(events));
1406
- return {
1407
- instanceId: input.instanceId,
1408
- turnRecordId: input.turnRecordId,
1409
- state: "live",
1410
- throughEventSequence,
1411
- sessionSignature: this.deps.turnSummaries.getSession(input.instanceId)?.signature ?? null,
1412
- reasoning: trace,
1413
- };
1414
- }
1415
- const session = await this.deps.sessionReader.readSessionTree(input.instanceId);
1416
- const trace = buildCommittedTurnTrace({
1417
- tree: session.piTree,
1418
- turnRecord,
1419
- events,
1420
- });
1421
- trace.usage ??= buildUsageSnapshotsByTurnRecordId(events)[turnRecord.id] ?? null;
1422
- return {
1325
+ const live = turnRecord.status === "running";
1326
+ const liveSignature = this.deps.turnSummaries.getSession(input.instanceId)?.signature ?? null;
1327
+ const session =
1328
+ !live || input.includeSession
1329
+ ? await this.deps.sessionReader.readSessionTree(input.instanceId)
1330
+ : null;
1331
+ const trace =
1332
+ !live && session
1333
+ ? buildCommittedTurnTrace({ tree: session.piTree, turnRecord, events })
1334
+ : createLiveTurnProjection(events).snapshot();
1335
+ if (!live) trace.usage ??= buildUsageSnapshotsByTurnRecordId(events)[turnRecord.id] ?? null;
1336
+ const detail: TurnReasoningDetailResponseBody = {
1423
1337
  instanceId: input.instanceId,
1424
1338
  turnRecordId: input.turnRecordId,
1425
- sessionSignature: session.signature,
1426
- state: "committed",
1339
+ sessionSignature: live ? liveSignature : (session?.signature ?? null),
1340
+ state: live ? "live" : "committed",
1427
1341
  throughEventSequence,
1428
1342
  reasoning: trace,
1429
1343
  };
1344
+ return { detail, turnRecord, events, session };
1430
1345
  }
1431
1346
  }