@leitwerk-dev/server 0.3.0 → 0.4.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 (355) hide show
  1. package/dist/{app-WfwKzrVk.d.ts → app-CsuxFZT5.d.ts} +332 -111
  2. package/dist/app-JM4CBTND.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-LK3RO6C7.js → chunk-FPMDWQF7.js} +3050 -645
  8. package/dist/chunk-FPMDWQF7.js.map +1 -0
  9. package/dist/{chunk-KIOQNNG6.js → chunk-MBHRCQFE.js} +2781 -1822
  10. package/dist/chunk-MBHRCQFE.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 +14 -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/db/database.d.ts.map +1 -1
  18. package/dist/db/execution-inspection-repo.d.ts +15 -0
  19. package/dist/db/execution-inspection-repo.d.ts.map +1 -0
  20. package/dist/db/history-window.d.ts +11 -0
  21. package/dist/db/history-window.d.ts.map +1 -0
  22. package/dist/db/mapped-llm-run-repo.d.ts +56 -0
  23. package/dist/db/mapped-llm-run-repo.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 +4 -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 +7 -1
  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/repo-helpers.d.ts +6 -0
  40. package/dist/db/repo-helpers.d.ts.map +1 -1
  41. package/dist/db/repositories.d.ts +16 -0
  42. package/dist/db/repositories.d.ts.map +1 -1
  43. package/dist/db/schema.d.ts +1177 -71
  44. package/dist/db/schema.d.ts.map +1 -1
  45. package/dist/db/scoped-settings-repo.d.ts +37 -0
  46. package/dist/db/scoped-settings-repo.d.ts.map +1 -0
  47. package/dist/db/skill-repo.d.ts +3 -0
  48. package/dist/db/skill-repo.d.ts.map +1 -1
  49. package/dist/db/turn-start-record-repo.d.ts +2 -1
  50. package/dist/db/turn-start-record-repo.d.ts.map +1 -1
  51. package/dist/db/turn-summary-repo.d.ts +1 -1
  52. package/dist/db/turn-summary-repo.d.ts.map +1 -1
  53. package/dist/db/worker-lease-repo.d.ts +2 -1
  54. package/dist/db/worker-lease-repo.d.ts.map +1 -1
  55. package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
  56. package/dist/external-source-service.d.ts.map +1 -1
  57. package/dist/future-execution/execution.d.ts +4 -1
  58. package/dist/future-execution/execution.d.ts.map +1 -1
  59. package/dist/future-execution/launch-lifecycle.d.ts +9 -25
  60. package/dist/future-execution/launch-lifecycle.d.ts.map +1 -1
  61. package/dist/future-execution/lifecycle.d.ts +3 -28
  62. package/dist/future-execution/lifecycle.d.ts.map +1 -1
  63. package/dist/future-execution/model-projection.d.ts.map +1 -1
  64. package/dist/future-execution/reconciliation.d.ts +1 -0
  65. package/dist/future-execution/reconciliation.d.ts.map +1 -1
  66. package/dist/future-execution-presenter.d.ts +4 -14
  67. package/dist/future-execution-presenter.d.ts.map +1 -1
  68. package/dist/index.js +2 -2
  69. package/dist/integration-tool-registry.d.ts +11 -2
  70. package/dist/integration-tool-registry.d.ts.map +1 -1
  71. package/dist/launch-coordinator-test-harness.d.ts.map +1 -1
  72. package/dist/launch-coordinator.d.ts.map +1 -1
  73. package/dist/launcher-model-config-service.d.ts +7 -1
  74. package/dist/launcher-model-config-service.d.ts.map +1 -1
  75. package/dist/leaf-outcome-snapshot-capture.d.ts +1 -0
  76. package/dist/leaf-outcome-snapshot-capture.d.ts.map +1 -1
  77. package/dist/main.js +3 -3
  78. package/dist/primary-path-snapshot.d.ts.map +1 -1
  79. package/dist/process-action-presenter.d.ts +11 -0
  80. package/dist/process-action-presenter.d.ts.map +1 -0
  81. package/dist/process-action-registry.d.ts.map +1 -1
  82. package/dist/process-engine/decision.d.ts +2 -0
  83. package/dist/process-engine/decision.d.ts.map +1 -1
  84. package/dist/process-engine/engine.d.ts.map +1 -1
  85. package/dist/process-engine/mapped-turns.d.ts +45 -0
  86. package/dist/process-engine/mapped-turns.d.ts.map +1 -0
  87. package/dist/process-engine/operation.d.ts +1 -1
  88. package/dist/process-engine/operation.d.ts.map +1 -1
  89. package/dist/process-engine/ops/accept-worker-turn-start.d.ts +2 -0
  90. package/dist/process-engine/ops/accept-worker-turn-start.d.ts.map +1 -1
  91. package/dist/process-engine/ops/continue-failed-turn.d.ts.map +1 -1
  92. package/dist/process-engine/ops/process-observation.d.ts +6 -0
  93. package/dist/process-engine/ops/process-observation.d.ts.map +1 -0
  94. package/dist/process-engine/ops/retry-failed-turn.d.ts.map +1 -1
  95. package/dist/process-engine/ops/retry-startup.d.ts.map +1 -1
  96. package/dist/process-engine/ops/start-process.d.ts +2 -0
  97. package/dist/process-engine/ops/start-process.d.ts.map +1 -1
  98. package/dist/process-engine/ops/turn-outcome.d.ts.map +1 -1
  99. package/dist/process-engine/ops/turn-wait.d.ts +30 -0
  100. package/dist/process-engine/ops/turn-wait.d.ts.map +1 -0
  101. package/dist/process-engine/ops/update-model-config.d.ts +9 -0
  102. package/dist/process-engine/ops/update-model-config.d.ts.map +1 -0
  103. package/dist/process-engine/recorder.d.ts.map +1 -1
  104. package/dist/process-engine/turn-start-preflight.d.ts +6 -0
  105. package/dist/process-engine/turn-start-preflight.d.ts.map +1 -1
  106. package/dist/process-engine/turn-wait-state.d.ts +24 -0
  107. package/dist/process-engine/turn-wait-state.d.ts.map +1 -0
  108. package/dist/process-engine/turn-worker-requirement.d.ts +2 -0
  109. package/dist/process-engine/turn-worker-requirement.d.ts.map +1 -1
  110. package/dist/process-engine/types.d.ts +10 -1
  111. package/dist/process-engine/types.d.ts.map +1 -1
  112. package/dist/process-engine/writes/build-process-abort-writes.d.ts +3 -1
  113. package/dist/process-engine/writes/build-process-abort-writes.d.ts.map +1 -1
  114. package/dist/process-engine/writes/build-recovery-start-writes.d.ts.map +1 -1
  115. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts +10 -1
  116. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts.map +1 -1
  117. package/dist/process-engine/writes/build-turn-selection-writes.d.ts.map +1 -1
  118. package/dist/process-engine/writes/commit-writes.d.ts +2 -0
  119. package/dist/process-engine/writes/commit-writes.d.ts.map +1 -1
  120. package/dist/process-engine/writes/reserve-selected-turn-start.d.ts +5 -0
  121. package/dist/process-engine/writes/reserve-selected-turn-start.d.ts.map +1 -0
  122. package/dist/process-engine/writes/writes.d.ts +35 -49
  123. package/dist/process-engine/writes/writes.d.ts.map +1 -1
  124. package/dist/process-graph.d.ts +1 -1
  125. package/dist/process-graph.d.ts.map +1 -1
  126. package/dist/process-inspection-reader.d.ts +21 -0
  127. package/dist/process-inspection-reader.d.ts.map +1 -0
  128. package/dist/process-inspection-trace.d.ts +13 -0
  129. package/dist/process-inspection-trace.d.ts.map +1 -0
  130. package/dist/process-inspection.d.ts +22 -0
  131. package/dist/process-inspection.d.ts.map +1 -0
  132. package/dist/process-instance-tree-presenter.d.ts +4 -3
  133. package/dist/process-instance-tree-presenter.d.ts.map +1 -1
  134. package/dist/process-launch-executor.d.ts +2 -0
  135. package/dist/process-launch-executor.d.ts.map +1 -1
  136. package/dist/process-launch-plan.d.ts +1 -0
  137. package/dist/process-launch-plan.d.ts.map +1 -1
  138. package/dist/process-launcher-registry.d.ts.map +1 -1
  139. package/dist/process-model-config.d.ts +19 -0
  140. package/dist/process-model-config.d.ts.map +1 -0
  141. package/dist/process-model-policy/construct.d.ts.map +1 -1
  142. package/dist/process-model-policy/evaluate.d.ts.map +1 -1
  143. package/dist/process-model-policy/index.d.ts +4 -1
  144. package/dist/process-model-policy/index.d.ts.map +1 -1
  145. package/dist/process-model-policy/project.d.ts.map +1 -1
  146. package/dist/process-model-policy/types.d.ts +18 -0
  147. package/dist/process-model-policy/types.d.ts.map +1 -1
  148. package/dist/process-model-policy-presenter.d.ts.map +1 -1
  149. package/dist/process-runtime-availability.d.ts +2 -1
  150. package/dist/process-runtime-availability.d.ts.map +1 -1
  151. package/dist/process-turn-navigation.d.ts +12 -2
  152. package/dist/process-turn-navigation.d.ts.map +1 -1
  153. package/dist/process-turn-trace.d.ts.map +1 -1
  154. package/dist/process-ui-snapshot-presenter.d.ts +15 -3
  155. package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
  156. package/dist/routes/future-executions.d.ts.map +1 -1
  157. package/dist/routes/launchers.d.ts.map +1 -1
  158. package/dist/routes/process-actions.d.ts.map +1 -1
  159. package/dist/routes/process-detail.d.ts.map +1 -1
  160. package/dist/routes/process-diagnostics-assembler.d.ts +3 -1
  161. package/dist/routes/process-diagnostics-assembler.d.ts.map +1 -1
  162. package/dist/routes/process-model-config.d.ts +4 -0
  163. package/dist/routes/process-model-config.d.ts.map +1 -0
  164. package/dist/routes/process-primary-path-assembler.d.ts +4 -12
  165. package/dist/routes/process-primary-path-assembler.d.ts.map +1 -1
  166. package/dist/routes/process-route-helpers.d.ts +15 -41
  167. package/dist/routes/process-route-helpers.d.ts.map +1 -1
  168. package/dist/routes/processes.d.ts.map +1 -1
  169. package/dist/routes/settings.d.ts +11 -0
  170. package/dist/routes/settings.d.ts.map +1 -0
  171. package/dist/routes/ticket-creation.d.ts.map +1 -1
  172. package/dist/scoped-settings-service.d.ts +83 -0
  173. package/dist/scoped-settings-service.d.ts.map +1 -0
  174. package/dist/server-bootstrap/build-host-capabilities.d.ts +2 -0
  175. package/dist/server-bootstrap/build-host-capabilities.d.ts.map +1 -1
  176. package/dist/server-bootstrap/register-http.d.ts.map +1 -1
  177. package/dist/session-summary-projection.d.ts.map +1 -1
  178. package/dist/skills/bundled-skills.d.ts +5 -0
  179. package/dist/skills/bundled-skills.d.ts.map +1 -0
  180. package/dist/skills/catalog-service.d.ts +1 -0
  181. package/dist/skills/catalog-service.d.ts.map +1 -1
  182. package/dist/skills/extension-importer.d.ts +11 -0
  183. package/dist/skills/extension-importer.d.ts.map +1 -0
  184. package/dist/skills/launch-skills.d.ts +11 -0
  185. package/dist/skills/launch-skills.d.ts.map +1 -0
  186. package/dist/skills/skill-frontmatter.d.ts +5 -0
  187. package/dist/skills/skill-frontmatter.d.ts.map +1 -1
  188. package/dist/skills/source-importer.d.ts +3 -0
  189. package/dist/skills/source-importer.d.ts.map +1 -1
  190. package/dist/startup-evidence.d.ts +3 -0
  191. package/dist/startup-evidence.d.ts.map +1 -1
  192. package/dist/supervisor/execution-inspection-ingestor.d.ts +9 -0
  193. package/dist/supervisor/execution-inspection-ingestor.d.ts.map +1 -0
  194. package/dist/supervisor/ipc-handler.d.ts +2 -0
  195. package/dist/supervisor/ipc-handler.d.ts.map +1 -1
  196. package/dist/supervisor/worker-event-ingestor.d.ts +1 -0
  197. package/dist/supervisor/worker-event-ingestor.d.ts.map +1 -1
  198. package/dist/supervisor/worker-start-payload-builder.d.ts +2 -0
  199. package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
  200. package/dist/supervisor/worker-supervisor.d.ts +4 -0
  201. package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
  202. package/dist/supervisor/worker-turn-ipc-recorder.d.ts +2 -2
  203. package/dist/supervisor/worker-turn-ipc-recorder.d.ts.map +1 -1
  204. package/dist/test-helpers/accepted-turn-start.d.ts +18 -4
  205. package/dist/test-helpers/accepted-turn-start.d.ts.map +1 -1
  206. package/dist/test-helpers/owned-test-deps.d.ts +18 -0
  207. package/dist/test-helpers/owned-test-deps.d.ts.map +1 -0
  208. package/dist/test-helpers/prepared-fixture-start.d.ts.map +1 -1
  209. package/dist/test-helpers/process-model-fixtures.d.ts +1 -0
  210. package/dist/test-helpers/process-model-fixtures.d.ts.map +1 -1
  211. package/dist/test-helpers/scoped-settings-fixtures.d.ts +16 -0
  212. package/dist/test-helpers/scoped-settings-fixtures.d.ts.map +1 -0
  213. package/dist/test-helpers/skill-fixtures.d.ts +4 -0
  214. package/dist/test-helpers/skill-fixtures.d.ts.map +1 -0
  215. package/dist/testing.d.ts +1 -1
  216. package/dist/testing.d.ts.map +1 -1
  217. package/dist/testing.js +14 -10
  218. package/dist/testing.js.map +1 -1
  219. package/dist/tool-approval-gate.d.ts +3 -0
  220. package/dist/tool-approval-gate.d.ts.map +1 -1
  221. package/dist/turn-progress.d.ts.map +1 -1
  222. package/dist/turn-wait-service.d.ts +28 -0
  223. package/dist/turn-wait-service.d.ts.map +1 -0
  224. package/dist/worker-runtime-profile-selection.d.ts +2 -0
  225. package/dist/worker-runtime-profile-selection.d.ts.map +1 -1
  226. package/package.json +20 -19
  227. package/src/app.ts +117 -5
  228. package/src/auth/fastify-auth.ts +13 -0
  229. package/src/config/config-loader.ts +40 -2
  230. package/src/config/config-types.ts +14 -1
  231. package/src/config/kubernetes-docker-isolation.ts +21 -0
  232. package/src/db/database.ts +159 -40
  233. package/src/db/execution-inspection-repo.ts +163 -0
  234. package/src/db/history-window.ts +18 -0
  235. package/src/db/mapped-llm-run-repo.ts +177 -0
  236. package/src/db/process-event-repo.ts +88 -10
  237. package/src/db/process-instance-repo.ts +9 -2
  238. package/src/db/process-leaf-outcome-snapshot-repo.ts +8 -2
  239. package/src/db/process-project-repo.ts +4 -0
  240. package/src/db/process-skill-repo.ts +9 -2
  241. package/src/db/process-turn-annotation-repo.ts +27 -9
  242. package/src/db/process-turn-record-repo.ts +53 -13
  243. package/src/db/publication-repo.ts +63 -0
  244. package/src/db/repo-helpers.ts +11 -1
  245. package/src/db/repositories.ts +22 -0
  246. package/src/db/schema.ts +173 -2
  247. package/src/db/scoped-settings-repo.ts +234 -0
  248. package/src/db/skill-repo.ts +89 -27
  249. package/src/db/turn-start-record-repo.ts +13 -3
  250. package/src/db/turn-summary-repo.ts +12 -3
  251. package/src/db/worker-lease-repo.ts +8 -2
  252. package/src/domain-logic/outcome-tools.ts +4 -149
  253. package/src/external-source-service.ts +33 -7
  254. package/src/future-execution/execution.ts +9 -2
  255. package/src/future-execution/launch-lifecycle.ts +87 -78
  256. package/src/future-execution/lifecycle.ts +5 -34
  257. package/src/future-execution/model-projection.ts +2 -0
  258. package/src/future-execution/reconciliation.ts +7 -0
  259. package/src/future-execution-presenter.ts +14 -22
  260. package/src/integration-tool-registry.ts +59 -26
  261. package/src/launch-coordinator-test-harness.ts +8 -4
  262. package/src/launch-coordinator.ts +10 -1
  263. package/src/launcher-model-config-service.ts +24 -3
  264. package/src/leaf-outcome-snapshot-capture.ts +2 -0
  265. package/src/primary-path-snapshot.ts +3 -4
  266. package/src/process-action-presenter.ts +98 -0
  267. package/src/process-action-registry.ts +31 -39
  268. package/src/process-engine/decision.ts +10 -0
  269. package/src/process-engine/engine.ts +13 -4
  270. package/src/process-engine/mapped-turns.ts +328 -0
  271. package/src/process-engine/operation.ts +1 -1
  272. package/src/process-engine/ops/accept-worker-turn-start.ts +6 -0
  273. package/src/process-engine/ops/continue-failed-turn.ts +2 -13
  274. package/src/process-engine/ops/process-observation.ts +119 -0
  275. package/src/process-engine/ops/retry-failed-turn.ts +2 -12
  276. package/src/process-engine/ops/retry-startup.ts +10 -13
  277. package/src/process-engine/ops/start-process.ts +34 -2
  278. package/src/process-engine/ops/turn-outcome.ts +65 -62
  279. package/src/process-engine/ops/turn-wait.ts +225 -0
  280. package/src/process-engine/ops/update-model-config.ts +66 -0
  281. package/src/process-engine/recorder.ts +33 -5
  282. package/src/process-engine/runner.ts +8 -8
  283. package/src/process-engine/turn-start-preflight.ts +168 -105
  284. package/src/process-engine/turn-wait-state.ts +126 -0
  285. package/src/process-engine/turn-worker-requirement.ts +4 -0
  286. package/src/process-engine/types.ts +15 -0
  287. package/src/process-engine/writes/build-process-abort-writes.ts +30 -19
  288. package/src/process-engine/writes/build-recovery-start-writes.ts +1 -0
  289. package/src/process-engine/writes/build-turn-outcome-writes.ts +118 -21
  290. package/src/process-engine/writes/build-turn-selection-writes.ts +2 -31
  291. package/src/process-engine/writes/commit-writes.ts +20 -2
  292. package/src/process-engine/writes/reserve-selected-turn-start.ts +44 -0
  293. package/src/process-engine/writes/writes.ts +73 -61
  294. package/src/process-graph.ts +0 -1
  295. package/src/process-inspection-reader.ts +305 -0
  296. package/src/process-inspection-trace.ts +176 -0
  297. package/src/process-inspection.ts +169 -0
  298. package/src/process-instance-tree-presenter.ts +56 -216
  299. package/src/process-launch-executor.ts +5 -2
  300. package/src/process-launch-plan.ts +17 -2
  301. package/src/process-launcher-registry.ts +28 -16
  302. package/src/process-model-config.ts +100 -0
  303. package/src/process-model-policy/construct.ts +1 -0
  304. package/src/process-model-policy/evaluate.ts +34 -32
  305. package/src/process-model-policy/index.ts +45 -3
  306. package/src/process-model-policy/project.ts +62 -76
  307. package/src/process-model-policy/types.ts +26 -0
  308. package/src/process-model-policy-presenter.ts +7 -3
  309. package/src/process-runtime-availability.ts +9 -4
  310. package/src/process-turn-navigation.ts +86 -8
  311. package/src/process-turn-trace.ts +12 -24
  312. package/src/process-ui-snapshot-presenter.ts +249 -298
  313. package/src/routes/future-executions.ts +5 -5
  314. package/src/routes/launchers.ts +51 -1
  315. package/src/routes/process-actions.ts +10 -10
  316. package/src/routes/process-detail.ts +25 -2
  317. package/src/routes/process-diagnostics-assembler.ts +32 -6
  318. package/src/routes/process-model-config.ts +70 -0
  319. package/src/routes/process-primary-path-assembler.ts +17 -33
  320. package/src/routes/process-route-helpers.ts +161 -327
  321. package/src/routes/processes.ts +2 -0
  322. package/src/routes/settings.ts +173 -0
  323. package/src/routes/ticket-creation.ts +28 -5
  324. package/src/scoped-settings-service.ts +736 -0
  325. package/src/server-bootstrap/build-host-capabilities.ts +42 -0
  326. package/src/server-bootstrap/register-http.ts +7 -1
  327. package/src/session-summary-projection.ts +1 -2
  328. package/src/skills/bundled-skills.ts +64 -0
  329. package/src/skills/catalog-service.ts +35 -6
  330. package/src/skills/extension-importer.ts +97 -0
  331. package/src/skills/launch-skills.ts +19 -0
  332. package/src/skills/skill-frontmatter.ts +13 -0
  333. package/src/skills/source-importer.ts +3 -0
  334. package/src/startup-evidence.ts +20 -0
  335. package/src/supervisor/execution-inspection-ingestor.ts +55 -0
  336. package/src/supervisor/ipc-handler.ts +29 -18
  337. package/src/supervisor/worker-event-ingestor.ts +30 -14
  338. package/src/supervisor/worker-start-payload-builder.ts +42 -0
  339. package/src/supervisor/worker-supervisor.ts +5 -0
  340. package/src/supervisor/worker-turn-ipc-recorder.ts +13 -1
  341. package/src/test-helpers/accepted-turn-start.ts +51 -27
  342. package/src/test-helpers/owned-test-deps.ts +45 -0
  343. package/src/test-helpers/prepared-fixture-start.ts +1 -0
  344. package/src/test-helpers/process-model-fixtures.ts +2 -0
  345. package/src/test-helpers/scoped-settings-fixtures.ts +126 -0
  346. package/src/test-helpers/skill-fixtures.ts +28 -0
  347. package/src/testing.ts +4 -1
  348. package/src/tool-approval-gate.ts +16 -0
  349. package/src/turn-progress.ts +3 -2
  350. package/src/turn-wait-service.ts +228 -0
  351. package/src/worker-runtime-profile-selection.ts +8 -1
  352. package/dist/app-4QSV4FUQ.js +0 -8
  353. package/dist/chunk-KIOQNNG6.js.map +0 -1
  354. package/dist/chunk-LK3RO6C7.js.map +0 -1
  355. /package/dist/{app-4QSV4FUQ.js.map → app-JM4CBTND.js.map} +0 -0
@@ -44,6 +44,17 @@ export class DatabaseSchemaMismatchError extends Error {
44
44
  }
45
45
 
46
46
  const ALL_TABLES = [
47
+ schema.wikiTopics,
48
+ schema.wikiPages,
49
+ schema.wikiRevisions,
50
+ schema.topicPublications,
51
+ schema.inspectionContents,
52
+ schema.executionInspections,
53
+
54
+ schema.settingsSubjects,
55
+ schema.settingsAliases,
56
+ schema.settingsOverrides,
57
+ schema.settingsSubjectRedirects,
47
58
  schema.startupObservations,
48
59
  schema.processInstances,
49
60
  schema.launchRuns,
@@ -68,6 +79,8 @@ const ALL_TABLES = [
68
79
  schema.processLeafOutcomeSnapshots,
69
80
  schema.turnRecords,
70
81
  schema.turnStartRecords,
82
+ schema.mappedLlmRuns,
83
+ schema.mappedLlmItems,
71
84
  schema.apiTokens,
72
85
  schema.authSessions,
73
86
  schema.authLoginFlows,
@@ -336,25 +349,82 @@ interface KnownMigration {
336
349
  apply(sqlite: DatabaseSync): void;
337
350
  }
338
351
 
339
- const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
340
- {
341
- id: "20260911_add_startup_observations",
342
- tableNames: ["startup_observations"],
343
- matches: (sqlite) =>
344
- hasExistingSchema(sqlite) && existingTableSql(sqlite, "startup_observations") === null,
352
+ function addTablesMigration(
353
+ id: string,
354
+ tables: readonly SQLiteTable[],
355
+ matches: KnownMigration["matches"],
356
+ ): KnownMigration {
357
+ return {
358
+ id,
359
+ tableNames: tables.map(getTableName),
360
+ matches,
345
361
  apply(sqlite) {
346
- createTableWithIndexes(sqlite, schema.startupObservations);
362
+ for (const table of tables) createTableWithIndexes(sqlite, table);
347
363
  },
348
- },
364
+ };
365
+ }
366
+ const HISTORY_INDEXES = [
367
+ "idx_turn_records_page",
368
+ "idx_turn_annotations_created",
369
+ "idx_worker_leases_started",
370
+ "idx_worker_leases_exited",
371
+ ] as const;
372
+
373
+ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
349
374
  {
350
- id: "20260823_add_ticket_destination_recents",
351
- tableNames: ["ticket_destination_recents"],
375
+ id: "20261008_remove_repository_instruction_overrides",
376
+ tableNames: [],
352
377
  matches: (sqlite) =>
353
- hasExistingSchema(sqlite) && existingTableSql(sqlite, "ticket_destination_recents") === null,
378
+ existingTableSql(sqlite, "settings_overrides") !== null &&
379
+ Boolean(
380
+ sqlite
381
+ .prepare("SELECT 1 FROM settings_overrides WHERE key = ? LIMIT 1")
382
+ .get("coding.repository_instructions"),
383
+ ),
354
384
  apply(sqlite) {
355
- createTableWithIndexes(sqlite, schema.ticketDestinationRecents);
385
+ sqlite
386
+ .prepare("DELETE FROM settings_overrides WHERE key = ?")
387
+ .run("coding.repository_instructions");
356
388
  },
357
389
  },
390
+ addTablesMigration(
391
+ "20260930_add_topic_wiki",
392
+ [schema.wikiTopics, schema.wikiPages, schema.wikiRevisions, schema.topicPublications],
393
+ (sqlite) => hasExistingSchema(sqlite) && existingTableSql(sqlite, "wiki_topics") === null,
394
+ ),
395
+ addTablesMigration(
396
+ "20260925_add_execution_inspections",
397
+ [schema.inspectionContents, schema.executionInspections],
398
+ (sqlite) =>
399
+ hasExistingSchema(sqlite) && existingTableSql(sqlite, "execution_inspections") === null,
400
+ ),
401
+ addTablesMigration(
402
+ "20260926_add_scoped_settings",
403
+ [schema.settingsSubjects, schema.settingsAliases, schema.settingsOverrides],
404
+ (sqlite) =>
405
+ existingTableSql(sqlite, "process_instances") !== null &&
406
+ existingTableSql(sqlite, "settings_subjects") === null &&
407
+ existingTableSql(sqlite, "settings_aliases") === null &&
408
+ existingTableSql(sqlite, "settings_overrides") === null,
409
+ ),
410
+ addTablesMigration(
411
+ "20260926_add_settings_subject_redirects",
412
+ [schema.settingsSubjectRedirects],
413
+ (sqlite) =>
414
+ hasExistingSchema(sqlite) && existingTableSql(sqlite, "settings_subject_redirects") === null,
415
+ ),
416
+ addTablesMigration(
417
+ "20260911_add_startup_observations",
418
+ [schema.startupObservations],
419
+ (sqlite) =>
420
+ hasExistingSchema(sqlite) && existingTableSql(sqlite, "startup_observations") === null,
421
+ ),
422
+ addTablesMigration(
423
+ "20260823_add_ticket_destination_recents",
424
+ [schema.ticketDestinationRecents],
425
+ (sqlite) =>
426
+ hasExistingSchema(sqlite) && existingTableSql(sqlite, "ticket_destination_recents") === null,
427
+ ),
358
428
  {
359
429
  id: "20260909_add_reasoning_summaries",
360
430
  tableNames: ["process_events", "turn_summaries", "session_summaries"],
@@ -389,14 +459,13 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
389
459
  createTableWithIndexes(sqlite, schema.sessionSummaries);
390
460
  },
391
461
  },
392
- {
393
- id: "20260908_add_api_tokens",
394
- tableNames: ["api_tokens"],
395
- matches: (sqlite) =>
462
+ addTablesMigration(
463
+ "20260908_add_api_tokens",
464
+ [schema.apiTokens],
465
+ (sqlite) =>
396
466
  existingTableSql(sqlite, "auth_sessions") !== null &&
397
467
  existingTableSql(sqlite, "api_tokens") === null,
398
- apply: (sqlite) => createTableWithIndexes(sqlite, schema.apiTokens),
399
- },
468
+ ),
400
469
  {
401
470
  id: "20260901_add_session_transfers",
402
471
  tableNames: ["session_transfer_grants", "session_transfer_attempts"],
@@ -551,18 +620,13 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
551
620
  }
552
621
  },
553
622
  },
554
- {
555
- id: "20260726_add_sql_backed_skills",
556
- tableNames: ["skills", "skill_revisions", "process_skills"],
557
- matches: (sqlite) =>
623
+ addTablesMigration(
624
+ "20260726_add_sql_backed_skills",
625
+ [schema.skills, schema.skillRevisions, schema.processSkills],
626
+ (sqlite) =>
558
627
  existingTableSql(sqlite, "process_instances") !== null &&
559
628
  existingTableSql(sqlite, "skills") === null,
560
- apply(sqlite) {
561
- for (const table of [schema.skills, schema.skillRevisions, schema.processSkills]) {
562
- createTableWithIndexes(sqlite, table);
563
- }
564
- },
565
- },
629
+ ),
566
630
  {
567
631
  id: "20260728_add_skill_catalog_and_invocations",
568
632
  tableNames: ["skills", "skill_catalog_entries", "skill_invocations"],
@@ -597,21 +661,30 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
597
661
  },
598
662
  },
599
663
  {
600
- id: "20260729_add_skill_revision_dependencies",
601
- tableNames: ["skill_revision_dependencies"],
602
- matches: (sqlite) => existingTableSql(sqlite, "skill_revision_dependencies") === null,
603
- apply(sqlite) {
604
- createTableWithIndexes(sqlite, schema.skillRevisionDependencies);
605
- },
606
- },
607
- {
608
- id: "20260726_add_process_question_requests",
609
- tableNames: ["process_question_requests"],
610
- matches: (sqlite) => existingTableSql(sqlite, "process_question_requests") === null,
664
+ id: "20261006_add_extension_skill_provenance",
665
+ tableNames: ["skills", "skill_revisions"],
666
+ matches: (sqlite) =>
667
+ !tableHasColumn(sqlite, "skills", "owner_extension_id") ||
668
+ !tableHasColumn(sqlite, "skill_revisions", "provenance_json"),
611
669
  apply(sqlite) {
612
- createTableWithIndexes(sqlite, schema.processQuestionRequests);
670
+ if (!tableHasColumn(sqlite, "skills", "owner_extension_id")) {
671
+ sqlite.exec("ALTER TABLE skills ADD COLUMN owner_extension_id text");
672
+ }
673
+ if (!tableHasColumn(sqlite, "skill_revisions", "provenance_json")) {
674
+ sqlite.exec("ALTER TABLE skill_revisions ADD COLUMN provenance_json text");
675
+ }
613
676
  },
614
677
  },
678
+ addTablesMigration(
679
+ "20260729_add_skill_revision_dependencies",
680
+ [schema.skillRevisionDependencies],
681
+ (sqlite) => existingTableSql(sqlite, "skill_revision_dependencies") === null,
682
+ ),
683
+ addTablesMigration(
684
+ "20260726_add_process_question_requests",
685
+ [schema.processQuestionRequests],
686
+ (sqlite) => existingTableSql(sqlite, "process_question_requests") === null,
687
+ ),
615
688
  {
616
689
  id: "20260725_add_model_policy_provenance_and_future_blocks",
617
690
  tableNames: ["process_instances", "future_executions", "turn_records"],
@@ -704,6 +777,32 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
704
777
  sqlite.exec("DROP TABLE provider_credentials_before_20260724");
705
778
  },
706
779
  },
780
+ {
781
+ // Runs before 20260821 so its legacy turn_records validation sees these columns.
782
+ id: "20260922_add_mapped_llm_runs",
783
+ tableNames: ["mapped_llm_runs", "mapped_llm_items", "turn_records", "turn_start_records"],
784
+ matches: (sqlite) =>
785
+ hasExistingSchema(sqlite) &&
786
+ (existingTableSql(sqlite, "mapped_llm_runs") === null ||
787
+ existingTableSql(sqlite, "mapped_llm_items") === null ||
788
+ !tableHasColumn(sqlite, "turn_records", "mapped_run_id") ||
789
+ !tableHasColumn(sqlite, "turn_start_records", "mapped_run_id")),
790
+ apply(sqlite) {
791
+ for (const table of ["turn_records", "turn_start_records"]) {
792
+ if (!tableHasColumn(sqlite, table, "mapped_run_id")) {
793
+ sqlite.exec(`ALTER TABLE ${table} ADD COLUMN mapped_run_id text;
794
+ ALTER TABLE ${table} ADD COLUMN mapped_item_key text;
795
+ ALTER TABLE ${table} ADD COLUMN mapped_item_index integer;`);
796
+ }
797
+ }
798
+ if (existingTableSql(sqlite, "mapped_llm_runs") === null) {
799
+ createTableWithIndexes(sqlite, schema.mappedLlmRuns);
800
+ }
801
+ if (existingTableSql(sqlite, "mapped_llm_items") === null) {
802
+ createTableWithIndexes(sqlite, schema.mappedLlmItems);
803
+ }
804
+ },
805
+ },
707
806
  {
708
807
  id: "20260821_remove_server_automatic_execution",
709
808
  tableNames: ["process_instances", "turn_records"],
@@ -836,6 +935,26 @@ const KNOWN_MIGRATIONS: readonly KnownMigration[] = [
836
935
  }
837
936
  },
838
937
  },
938
+ {
939
+ id: "20261005_add_process_history_indexes",
940
+ tableNames: [],
941
+ legacyIndexNames: [...HISTORY_INDEXES, "idx_leaf_outcomes_anchored"],
942
+ matches(sqlite) {
943
+ const indexes = existingSchemaObjects(sqlite, "index");
944
+ return (
945
+ hasExistingSchema(sqlite) &&
946
+ (indexes.has("idx_leaf_outcomes_anchored") ||
947
+ HISTORY_INDEXES.some((name) => !indexes.has(name)))
948
+ );
949
+ },
950
+ apply(sqlite) {
951
+ sqlite.exec(`CREATE INDEX IF NOT EXISTS idx_turn_records_page ON turn_records(instance_id, started_at, id);
952
+ CREATE INDEX IF NOT EXISTS idx_turn_annotations_created ON turn_annotations(instance_id, created_at);
953
+ CREATE INDEX IF NOT EXISTS idx_worker_leases_started ON worker_leases(instance_id, started_at);
954
+ CREATE INDEX IF NOT EXISTS idx_worker_leases_exited ON worker_leases(instance_id, exited_at);
955
+ DROP INDEX IF EXISTS idx_leaf_outcomes_anchored;`);
956
+ },
957
+ },
839
958
  ];
840
959
 
841
960
  function backupSqliteFiles(sqlite: DatabaseSync, sqlitePath: string): string {
@@ -0,0 +1,163 @@
1
+ import { createHash } from "node:crypto";
2
+ import type {
3
+ ExecutionInspectionCapture,
4
+ ExecutionInspectionRecord,
5
+ InspectionContextObservation,
6
+ } from "@leitwerk-dev/domain";
7
+ import type { InspectionConfigurationRevision } from "@leitwerk-dev/protocol";
8
+ import { and, asc, eq, inArray, sql } from "drizzle-orm";
9
+ import type { LeitwerkDb } from "./database.js";
10
+ import { executionInspections, inspectionContents } from "./schema.js";
11
+
12
+ /** Immutable content and incremental observation references. No session ancestry is copied. @internal */
13
+ export function createExecutionInspectionRepo(db: LeitwerkDb) {
14
+ function pack(value: unknown): unknown {
15
+ if (Array.isArray(value)) return value.map(pack);
16
+ if (!value || typeof value !== "object") return value;
17
+ const record = value as Record<string, unknown>;
18
+ if ((record.state === "recorded" || record.state === "redacted") && "value" in record) {
19
+ const contentJson = JSON.stringify(record.value);
20
+ const digest = createHash("sha256").update(contentJson).digest("hex");
21
+ db.insert(inspectionContents).values({ digest, contentJson }).onConflictDoNothing().run();
22
+ return { state: record.state, contentDigest: digest };
23
+ }
24
+ return Object.fromEntries(Object.entries(record).map(([key, item]) => [key, pack(item)]));
25
+ }
26
+ function unpack(value: unknown): unknown {
27
+ if (Array.isArray(value)) return value.map(unpack);
28
+ if (!value || typeof value !== "object") return value;
29
+ const record = value as Record<string, unknown>;
30
+ if (typeof record.contentDigest === "string") {
31
+ const row = db
32
+ .select()
33
+ .from(inspectionContents)
34
+ .where(eq(inspectionContents.digest, record.contentDigest))
35
+ .get();
36
+ return row
37
+ ? { state: record.state, value: JSON.parse(row.contentJson) }
38
+ : { state: "unavailable", reason: "Recorded content is unavailable" };
39
+ }
40
+ return Object.fromEntries(Object.entries(record).map(([key, item]) => [key, unpack(item)]));
41
+ }
42
+ return {
43
+ /** Model identities without loading retained prompts or message contents. @internal */
44
+ modelInputs(
45
+ instanceId: string,
46
+ turnRecordId: string,
47
+ ): Pick<InspectionConfigurationRevision, "id" | "timestamp" | "boundaryEntryId" | "model">[] {
48
+ return db
49
+ .select({
50
+ id: executionInspections.id,
51
+ timestamp: executionInspections.capturedAt,
52
+ boundaryEntryId: sql<
53
+ string | null
54
+ >`json_extract(${executionInspections.factJson}, '$.boundaryEntryId')`,
55
+ model: sql<string>`json_extract(${executionInspections.factJson}, '$.model')`,
56
+ })
57
+ .from(executionInspections)
58
+ .where(
59
+ and(
60
+ eq(executionInspections.instanceId, instanceId),
61
+ eq(executionInspections.turnRecordId, turnRecordId),
62
+ sql`json_extract(${executionInspections.factJson}, '$.kind') = 'model_input'`,
63
+ ),
64
+ )
65
+ .orderBy(asc(executionInspections.sequence))
66
+ .all()
67
+ .map((row) => ({
68
+ ...row,
69
+ model: JSON.parse(row.model) as InspectionConfigurationRevision["model"],
70
+ }));
71
+ },
72
+ /** @internal */
73
+ append(input: Omit<ExecutionInspectionRecord, "sequence">): "stored" | "replay" {
74
+ return db.transaction(() => {
75
+ const factJson = JSON.stringify(pack(input.fact));
76
+ const existing = db
77
+ .select()
78
+ .from(executionInspections)
79
+ .where(eq(executionInspections.id, input.id))
80
+ .get();
81
+ if (existing) {
82
+ if (
83
+ existing.instanceId !== input.instanceId ||
84
+ existing.turnRecordId !== input.turnRecordId ||
85
+ existing.factJson !== factJson
86
+ )
87
+ throw new Error("Inspection observation identity was reused with different evidence");
88
+ return "replay";
89
+ }
90
+ db.insert(executionInspections)
91
+ .values({
92
+ id: input.id,
93
+ instanceId: input.instanceId,
94
+ turnRecordId: input.turnRecordId,
95
+ startRecordId: input.startRecordId,
96
+ workerLeaseId: input.workerLeaseId,
97
+ capturedAt: input.timestamp,
98
+ factJson,
99
+ })
100
+ .run();
101
+ return "stored";
102
+ });
103
+ },
104
+ /** Expanded evidence is read only on demand. @internal */
105
+ list(instanceId: string, turnRecordId: string): ExecutionInspectionRecord[] {
106
+ return db
107
+ .select()
108
+ .from(executionInspections)
109
+ .where(
110
+ and(
111
+ eq(executionInspections.instanceId, instanceId),
112
+ eq(executionInspections.turnRecordId, turnRecordId),
113
+ ),
114
+ )
115
+ .orderBy(asc(executionInspections.sequence))
116
+ .all()
117
+ .map((row) => ({
118
+ version: 1,
119
+ id: row.id,
120
+ sequence: row.sequence,
121
+ instanceId: row.instanceId,
122
+ turnRecordId: row.turnRecordId,
123
+ startRecordId: row.startRecordId,
124
+ workerLeaseId: row.workerLeaseId,
125
+ timestamp: row.capturedAt,
126
+ fact: unpack(JSON.parse(row.factJson)) as ExecutionInspectionCapture["fact"],
127
+ }));
128
+ },
129
+ /** Small origin/product references for compact lineage without loading prompts. @internal */
130
+ listContextFacts(instanceId: string, turnRecordIds?: string[]): InspectionContextObservation[] {
131
+ if (turnRecordIds?.length === 0) return [];
132
+ return db
133
+ .select()
134
+ .from(executionInspections)
135
+ .where(
136
+ and(
137
+ eq(executionInspections.instanceId, instanceId),
138
+ turnRecordIds ? inArray(executionInspections.turnRecordId, turnRecordIds) : undefined,
139
+ sql`json_extract(${executionInspections.factJson}, '$.kind') IN ('supplied_context', 'product_consumed', 'entry_link')`,
140
+ ),
141
+ )
142
+ .orderBy(asc(executionInspections.sequence))
143
+ .all()
144
+ .map((row) => {
145
+ const fact = JSON.parse(row.factJson) as Extract<
146
+ ExecutionInspectionCapture["fact"],
147
+ { kind: "supplied_context" | "product_consumed" | "entry_link" }
148
+ >;
149
+ return {
150
+ id: row.id,
151
+ turnRecordId: row.turnRecordId,
152
+ fact:
153
+ fact.kind === "supplied_context"
154
+ ? {
155
+ ...fact,
156
+ products: fact.products.map(({ content: _content, ...source }) => source),
157
+ }
158
+ : fact,
159
+ };
160
+ });
161
+ },
162
+ };
163
+ }
@@ -0,0 +1,18 @@
1
+ import { and, gte, lte } from "drizzle-orm";
2
+ import type { SQLiteColumn } from "drizzle-orm/sqlite-core";
3
+
4
+ /** @internal */
5
+ export interface HistoryWindow {
6
+ /** @internal */
7
+ since?: string;
8
+ /** @internal */
9
+ until?: string;
10
+ }
11
+
12
+ /** @internal */
13
+ export function historyWindow(column: SQLiteColumn, window?: HistoryWindow) {
14
+ return and(
15
+ window?.since ? gte(column, window.since) : undefined,
16
+ window?.until ? lte(column, window.until) : undefined,
17
+ );
18
+ }
@@ -0,0 +1,177 @@
1
+ import type { MappedItem, MappedRun, MappedRunStatus, TurnId } from "@leitwerk-dev/domain";
2
+ import { and, asc, eq } from "drizzle-orm";
3
+ import type { LeitwerkDb } from "./database.js";
4
+ import { now } from "./repo-helpers.js";
5
+ import * as s from "./schema.js";
6
+
7
+ /** @internal */
8
+ export interface CreateMappedRunInput {
9
+ /** @internal */
10
+ id: string;
11
+ /** @internal */
12
+ instanceId: string;
13
+ /** @internal */
14
+ turnId: TurnId;
15
+ /** @internal */
16
+ status: MappedRunStatus;
17
+ /** @internal */
18
+ items: ReadonlyArray<Pick<MappedItem, "itemIndex" | "itemKey" | "label" | "itemJson">>;
19
+ }
20
+
21
+ /** @internal */
22
+ export interface CompleteMappedItemInput {
23
+ /** @internal */
24
+ runId: string;
25
+ /** @internal */
26
+ itemIndex: number;
27
+ /** @internal */
28
+ outcome: string;
29
+ /** @internal */
30
+ resultJson: string;
31
+ /** @internal */
32
+ turnRecordId: string;
33
+ }
34
+
35
+ /** @internal */
36
+ export function createMappedLlmRunRepo(db: LeitwerkDb) {
37
+ return {
38
+ /** @internal */
39
+ create(input: CreateMappedRunInput): MappedRun {
40
+ const ts = now();
41
+ const run = {
42
+ id: input.id,
43
+ instanceId: input.instanceId,
44
+ turnId: input.turnId,
45
+ status: input.status,
46
+ itemCount: input.items.length,
47
+ nextIndex: input.status === "completed" ? input.items.length : 0,
48
+ createdAt: ts,
49
+ updatedAt: ts,
50
+ };
51
+ db.insert(s.mappedLlmRuns).values(run).run();
52
+ for (const item of input.items) {
53
+ db.insert(s.mappedLlmItems)
54
+ .values({
55
+ runId: input.id,
56
+ instanceId: input.instanceId,
57
+ itemIndex: item.itemIndex,
58
+ itemKey: item.itemKey,
59
+ label: item.label,
60
+ itemJson: item.itemJson,
61
+ status: "pending",
62
+ })
63
+ .run();
64
+ }
65
+ return run;
66
+ },
67
+ /** @internal */
68
+ getById(id: string): MappedRun | null {
69
+ return db.select().from(s.mappedLlmRuns).where(eq(s.mappedLlmRuns.id, id)).get() ?? null;
70
+ },
71
+ /** @internal */
72
+ getActiveByInstance(instanceId: string): MappedRun | null {
73
+ return (
74
+ db
75
+ .select()
76
+ .from(s.mappedLlmRuns)
77
+ .where(
78
+ and(eq(s.mappedLlmRuns.instanceId, instanceId), eq(s.mappedLlmRuns.status, "active")),
79
+ )
80
+ .get() ?? null
81
+ );
82
+ },
83
+ /** @internal */
84
+ listByInstance(instanceId: string): MappedRun[] {
85
+ return db
86
+ .select()
87
+ .from(s.mappedLlmRuns)
88
+ .where(eq(s.mappedLlmRuns.instanceId, instanceId))
89
+ .orderBy(asc(s.mappedLlmRuns.createdAt))
90
+ .all();
91
+ },
92
+ /** @internal */
93
+ listItems(runId: string): MappedItem[] {
94
+ return db
95
+ .select()
96
+ .from(s.mappedLlmItems)
97
+ .where(eq(s.mappedLlmItems.runId, runId))
98
+ .orderBy(asc(s.mappedLlmItems.itemIndex))
99
+ .all();
100
+ },
101
+ /** @internal */
102
+ listItemsByInstance(instanceId: string): MappedItem[] {
103
+ return db
104
+ .select()
105
+ .from(s.mappedLlmItems)
106
+ .where(eq(s.mappedLlmItems.instanceId, instanceId))
107
+ .all();
108
+ },
109
+ /** @internal */
110
+ getItem(runId: string, itemIndex: number): MappedItem | null {
111
+ return (
112
+ db
113
+ .select()
114
+ .from(s.mappedLlmItems)
115
+ .where(and(eq(s.mappedLlmItems.runId, runId), eq(s.mappedLlmItems.itemIndex, itemIndex)))
116
+ .get() ?? null
117
+ );
118
+ },
119
+ /**
120
+ * Records one item result and advances the run. Fails unless the item is the
121
+ * run's current pending item, so a replayed outcome cannot complete it twice.
122
+ * @internal
123
+ */
124
+ completeItem(input: CompleteMappedItemInput): MappedRun {
125
+ const run = this.getById(input.runId);
126
+ if (!run || run.status !== "active" || run.nextIndex !== input.itemIndex) {
127
+ throw new Error(`Mapped item ${input.itemIndex} of run '${input.runId}' is not current`);
128
+ }
129
+ const ts = now();
130
+ const updated = db
131
+ .update(s.mappedLlmItems)
132
+ .set({
133
+ status: "completed",
134
+ outcome: input.outcome,
135
+ resultJson: input.resultJson,
136
+ turnRecordId: input.turnRecordId,
137
+ completedAt: ts,
138
+ })
139
+ .where(
140
+ and(
141
+ eq(s.mappedLlmItems.runId, input.runId),
142
+ eq(s.mappedLlmItems.itemIndex, input.itemIndex),
143
+ eq(s.mappedLlmItems.status, "pending"),
144
+ ),
145
+ )
146
+ .run();
147
+ if (updated.changes !== 1) {
148
+ throw new Error(`Mapped item ${input.itemIndex} of run '${input.runId}' is not pending`);
149
+ }
150
+ const nextIndex = input.itemIndex + 1;
151
+ db.update(s.mappedLlmRuns)
152
+ .set({
153
+ nextIndex,
154
+ status: nextIndex === run.itemCount ? "completed" : "active",
155
+ updatedAt: ts,
156
+ })
157
+ .where(eq(s.mappedLlmRuns.id, input.runId))
158
+ .run();
159
+ return this.getById(input.runId) as MappedRun;
160
+ },
161
+ /** @internal */
162
+ abortActiveByInstance(instanceId: string): number {
163
+ return Number(
164
+ db
165
+ .update(s.mappedLlmRuns)
166
+ .set({ status: "aborted", updatedAt: now() })
167
+ .where(
168
+ and(eq(s.mappedLlmRuns.instanceId, instanceId), eq(s.mappedLlmRuns.status, "active")),
169
+ )
170
+ .run().changes,
171
+ );
172
+ },
173
+ };
174
+ }
175
+
176
+ /** @internal */
177
+ export type MappedLlmRunRepo = ReturnType<typeof createMappedLlmRunRepo>;