@leitwerk-dev/server 0.3.1 → 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 (309) hide show
  1. package/dist/{app-BuJCdkvy.d.ts → app-CsuxFZT5.d.ts} +262 -121
  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-QCHS3X33.js → chunk-FPMDWQF7.js} +2617 -562
  8. package/dist/chunk-FPMDWQF7.js.map +1 -0
  9. package/dist/{chunk-HK6BLLHA.js → chunk-MBHRCQFE.js} +2303 -1576
  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/process-event-repo.d.ts +2 -0
  23. package/dist/db/process-event-repo.d.ts.map +1 -1
  24. package/dist/db/process-instance-repo.d.ts +4 -1
  25. package/dist/db/process-instance-repo.d.ts.map +1 -1
  26. package/dist/db/process-leaf-outcome-snapshot-repo.d.ts +2 -1
  27. package/dist/db/process-leaf-outcome-snapshot-repo.d.ts.map +1 -1
  28. package/dist/db/process-project-repo.d.ts +2 -0
  29. package/dist/db/process-project-repo.d.ts.map +1 -1
  30. package/dist/db/process-skill-repo.d.ts.map +1 -1
  31. package/dist/db/process-turn-annotation-repo.d.ts +4 -1
  32. package/dist/db/process-turn-annotation-repo.d.ts.map +1 -1
  33. package/dist/db/process-turn-record-repo.d.ts +4 -0
  34. package/dist/db/process-turn-record-repo.d.ts.map +1 -1
  35. package/dist/db/publication-repo.d.ts +5 -0
  36. package/dist/db/publication-repo.d.ts.map +1 -0
  37. package/dist/db/repositories.d.ts +12 -0
  38. package/dist/db/repositories.d.ts.map +1 -1
  39. package/dist/db/schema.d.ts +693 -2
  40. package/dist/db/schema.d.ts.map +1 -1
  41. package/dist/db/scoped-settings-repo.d.ts +37 -0
  42. package/dist/db/scoped-settings-repo.d.ts.map +1 -0
  43. package/dist/db/skill-repo.d.ts +3 -0
  44. package/dist/db/skill-repo.d.ts.map +1 -1
  45. package/dist/db/turn-start-record-repo.d.ts +2 -1
  46. package/dist/db/turn-start-record-repo.d.ts.map +1 -1
  47. package/dist/db/turn-summary-repo.d.ts +1 -1
  48. package/dist/db/turn-summary-repo.d.ts.map +1 -1
  49. package/dist/db/worker-lease-repo.d.ts +2 -1
  50. package/dist/db/worker-lease-repo.d.ts.map +1 -1
  51. package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
  52. package/dist/external-source-service.d.ts.map +1 -1
  53. package/dist/future-execution/execution.d.ts +4 -1
  54. package/dist/future-execution/execution.d.ts.map +1 -1
  55. package/dist/future-execution/launch-lifecycle.d.ts +9 -25
  56. package/dist/future-execution/launch-lifecycle.d.ts.map +1 -1
  57. package/dist/future-execution/lifecycle.d.ts +3 -28
  58. package/dist/future-execution/lifecycle.d.ts.map +1 -1
  59. package/dist/future-execution/model-projection.d.ts.map +1 -1
  60. package/dist/future-execution/reconciliation.d.ts +1 -0
  61. package/dist/future-execution/reconciliation.d.ts.map +1 -1
  62. package/dist/future-execution-presenter.d.ts.map +1 -1
  63. package/dist/index.js +2 -2
  64. package/dist/integration-tool-registry.d.ts +11 -2
  65. package/dist/integration-tool-registry.d.ts.map +1 -1
  66. package/dist/launch-coordinator.d.ts.map +1 -1
  67. package/dist/launcher-model-config-service.d.ts +7 -1
  68. package/dist/launcher-model-config-service.d.ts.map +1 -1
  69. package/dist/leaf-outcome-snapshot-capture.d.ts +1 -0
  70. package/dist/leaf-outcome-snapshot-capture.d.ts.map +1 -1
  71. package/dist/main.js +3 -3
  72. package/dist/primary-path-snapshot.d.ts.map +1 -1
  73. package/dist/process-action-presenter.d.ts +1 -6
  74. package/dist/process-action-presenter.d.ts.map +1 -1
  75. package/dist/process-action-registry.d.ts.map +1 -1
  76. package/dist/process-engine/decision.d.ts +2 -0
  77. package/dist/process-engine/decision.d.ts.map +1 -1
  78. package/dist/process-engine/engine.d.ts.map +1 -1
  79. package/dist/process-engine/mapped-turns.d.ts.map +1 -1
  80. package/dist/process-engine/ops/continue-failed-turn.d.ts.map +1 -1
  81. package/dist/process-engine/ops/process-observation.d.ts +6 -0
  82. package/dist/process-engine/ops/process-observation.d.ts.map +1 -0
  83. package/dist/process-engine/ops/retry-failed-turn.d.ts.map +1 -1
  84. package/dist/process-engine/ops/retry-startup.d.ts.map +1 -1
  85. package/dist/process-engine/ops/start-process.d.ts +2 -0
  86. package/dist/process-engine/ops/start-process.d.ts.map +1 -1
  87. package/dist/process-engine/ops/turn-outcome.d.ts.map +1 -1
  88. package/dist/process-engine/ops/turn-wait.d.ts +30 -0
  89. package/dist/process-engine/ops/turn-wait.d.ts.map +1 -0
  90. package/dist/process-engine/ops/update-model-config.d.ts +9 -0
  91. package/dist/process-engine/ops/update-model-config.d.ts.map +1 -0
  92. package/dist/process-engine/recorder.d.ts.map +1 -1
  93. package/dist/process-engine/turn-start-preflight.d.ts +6 -0
  94. package/dist/process-engine/turn-start-preflight.d.ts.map +1 -1
  95. package/dist/process-engine/turn-wait-state.d.ts +24 -0
  96. package/dist/process-engine/turn-wait-state.d.ts.map +1 -0
  97. package/dist/process-engine/turn-worker-requirement.d.ts +2 -0
  98. package/dist/process-engine/turn-worker-requirement.d.ts.map +1 -1
  99. package/dist/process-engine/types.d.ts +5 -1
  100. package/dist/process-engine/types.d.ts.map +1 -1
  101. package/dist/process-engine/writes/build-process-abort-writes.d.ts +3 -1
  102. package/dist/process-engine/writes/build-process-abort-writes.d.ts.map +1 -1
  103. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts +2 -0
  104. package/dist/process-engine/writes/build-turn-outcome-writes.d.ts.map +1 -1
  105. package/dist/process-engine/writes/commit-writes.d.ts +2 -0
  106. package/dist/process-engine/writes/commit-writes.d.ts.map +1 -1
  107. package/dist/process-engine/writes/writes.d.ts +28 -60
  108. package/dist/process-engine/writes/writes.d.ts.map +1 -1
  109. package/dist/process-graph.d.ts +1 -1
  110. package/dist/process-graph.d.ts.map +1 -1
  111. package/dist/process-inspection-reader.d.ts +21 -0
  112. package/dist/process-inspection-reader.d.ts.map +1 -0
  113. package/dist/process-inspection-trace.d.ts +13 -0
  114. package/dist/process-inspection-trace.d.ts.map +1 -0
  115. package/dist/process-inspection.d.ts +22 -0
  116. package/dist/process-inspection.d.ts.map +1 -0
  117. package/dist/process-instance-tree-presenter.d.ts +4 -3
  118. package/dist/process-instance-tree-presenter.d.ts.map +1 -1
  119. package/dist/process-launch-executor.d.ts +2 -0
  120. package/dist/process-launch-executor.d.ts.map +1 -1
  121. package/dist/process-launch-plan.d.ts +1 -0
  122. package/dist/process-launch-plan.d.ts.map +1 -1
  123. package/dist/process-launcher-registry.d.ts.map +1 -1
  124. package/dist/process-model-config.d.ts +19 -0
  125. package/dist/process-model-config.d.ts.map +1 -0
  126. package/dist/process-model-policy/construct.d.ts.map +1 -1
  127. package/dist/process-model-policy/evaluate.d.ts.map +1 -1
  128. package/dist/process-model-policy/index.d.ts +4 -1
  129. package/dist/process-model-policy/index.d.ts.map +1 -1
  130. package/dist/process-model-policy/project.d.ts.map +1 -1
  131. package/dist/process-model-policy/types.d.ts +18 -0
  132. package/dist/process-model-policy/types.d.ts.map +1 -1
  133. package/dist/process-model-policy-presenter.d.ts.map +1 -1
  134. package/dist/process-runtime-availability.d.ts +2 -1
  135. package/dist/process-runtime-availability.d.ts.map +1 -1
  136. package/dist/process-turn-trace.d.ts.map +1 -1
  137. package/dist/process-ui-snapshot-presenter.d.ts +14 -3
  138. package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
  139. package/dist/routes/future-executions.d.ts.map +1 -1
  140. package/dist/routes/launchers.d.ts.map +1 -1
  141. package/dist/routes/process-actions.d.ts.map +1 -1
  142. package/dist/routes/process-detail.d.ts.map +1 -1
  143. package/dist/routes/process-model-config.d.ts +4 -0
  144. package/dist/routes/process-model-config.d.ts.map +1 -0
  145. package/dist/routes/process-route-helpers.d.ts +8 -9
  146. package/dist/routes/process-route-helpers.d.ts.map +1 -1
  147. package/dist/routes/processes.d.ts.map +1 -1
  148. package/dist/routes/settings.d.ts +11 -0
  149. package/dist/routes/settings.d.ts.map +1 -0
  150. package/dist/routes/ticket-creation.d.ts.map +1 -1
  151. package/dist/scoped-settings-service.d.ts +83 -0
  152. package/dist/scoped-settings-service.d.ts.map +1 -0
  153. package/dist/server-bootstrap/build-host-capabilities.d.ts +2 -0
  154. package/dist/server-bootstrap/build-host-capabilities.d.ts.map +1 -1
  155. package/dist/server-bootstrap/register-http.d.ts.map +1 -1
  156. package/dist/session-summary-projection.d.ts.map +1 -1
  157. package/dist/skills/bundled-skills.d.ts +5 -0
  158. package/dist/skills/bundled-skills.d.ts.map +1 -0
  159. package/dist/skills/catalog-service.d.ts +1 -0
  160. package/dist/skills/catalog-service.d.ts.map +1 -1
  161. package/dist/skills/extension-importer.d.ts +11 -0
  162. package/dist/skills/extension-importer.d.ts.map +1 -0
  163. package/dist/skills/launch-skills.d.ts +11 -0
  164. package/dist/skills/launch-skills.d.ts.map +1 -0
  165. package/dist/skills/skill-frontmatter.d.ts +5 -0
  166. package/dist/skills/skill-frontmatter.d.ts.map +1 -1
  167. package/dist/skills/source-importer.d.ts +3 -0
  168. package/dist/skills/source-importer.d.ts.map +1 -1
  169. package/dist/startup-evidence.d.ts +3 -0
  170. package/dist/startup-evidence.d.ts.map +1 -1
  171. package/dist/supervisor/execution-inspection-ingestor.d.ts +9 -0
  172. package/dist/supervisor/execution-inspection-ingestor.d.ts.map +1 -0
  173. package/dist/supervisor/ipc-handler.d.ts +2 -0
  174. package/dist/supervisor/ipc-handler.d.ts.map +1 -1
  175. package/dist/supervisor/worker-event-ingestor.d.ts +1 -0
  176. package/dist/supervisor/worker-event-ingestor.d.ts.map +1 -1
  177. package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
  178. package/dist/supervisor/worker-supervisor.d.ts +2 -0
  179. package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
  180. package/dist/test-helpers/prepared-fixture-start.d.ts.map +1 -1
  181. package/dist/test-helpers/process-model-fixtures.d.ts +1 -0
  182. package/dist/test-helpers/process-model-fixtures.d.ts.map +1 -1
  183. package/dist/test-helpers/scoped-settings-fixtures.d.ts +16 -0
  184. package/dist/test-helpers/scoped-settings-fixtures.d.ts.map +1 -0
  185. package/dist/test-helpers/skill-fixtures.d.ts +4 -0
  186. package/dist/test-helpers/skill-fixtures.d.ts.map +1 -0
  187. package/dist/testing.js +2 -1
  188. package/dist/testing.js.map +1 -1
  189. package/dist/tool-approval-gate.d.ts +3 -0
  190. package/dist/tool-approval-gate.d.ts.map +1 -1
  191. package/dist/turn-progress.d.ts.map +1 -1
  192. package/dist/turn-wait-service.d.ts +28 -0
  193. package/dist/turn-wait-service.d.ts.map +1 -0
  194. package/dist/worker-runtime-profile-selection.d.ts +2 -0
  195. package/dist/worker-runtime-profile-selection.d.ts.map +1 -1
  196. package/package.json +20 -19
  197. package/src/app.ts +115 -5
  198. package/src/auth/fastify-auth.ts +13 -0
  199. package/src/config/config-loader.ts +40 -2
  200. package/src/config/config-types.ts +14 -1
  201. package/src/config/kubernetes-docker-isolation.ts +21 -0
  202. package/src/db/database.ts +131 -40
  203. package/src/db/execution-inspection-repo.ts +163 -0
  204. package/src/db/history-window.ts +18 -0
  205. package/src/db/process-event-repo.ts +88 -10
  206. package/src/db/process-instance-repo.ts +9 -2
  207. package/src/db/process-leaf-outcome-snapshot-repo.ts +8 -2
  208. package/src/db/process-project-repo.ts +4 -0
  209. package/src/db/process-skill-repo.ts +9 -2
  210. package/src/db/process-turn-annotation-repo.ts +27 -9
  211. package/src/db/process-turn-record-repo.ts +45 -12
  212. package/src/db/publication-repo.ts +63 -0
  213. package/src/db/repositories.ts +17 -0
  214. package/src/db/schema.ts +103 -1
  215. package/src/db/scoped-settings-repo.ts +234 -0
  216. package/src/db/skill-repo.ts +89 -27
  217. package/src/db/turn-start-record-repo.ts +8 -2
  218. package/src/db/turn-summary-repo.ts +12 -3
  219. package/src/db/worker-lease-repo.ts +8 -2
  220. package/src/domain-logic/outcome-tools.ts +4 -193
  221. package/src/external-source-service.ts +33 -7
  222. package/src/future-execution/execution.ts +9 -2
  223. package/src/future-execution/launch-lifecycle.ts +87 -78
  224. package/src/future-execution/lifecycle.ts +5 -34
  225. package/src/future-execution/model-projection.ts +2 -0
  226. package/src/future-execution/reconciliation.ts +7 -0
  227. package/src/future-execution-presenter.ts +7 -8
  228. package/src/integration-tool-registry.ts +59 -26
  229. package/src/launch-coordinator.ts +10 -1
  230. package/src/launcher-model-config-service.ts +24 -3
  231. package/src/leaf-outcome-snapshot-capture.ts +2 -0
  232. package/src/primary-path-snapshot.ts +3 -4
  233. package/src/process-action-presenter.ts +22 -57
  234. package/src/process-action-registry.ts +31 -39
  235. package/src/process-engine/decision.ts +10 -0
  236. package/src/process-engine/engine.ts +11 -2
  237. package/src/process-engine/mapped-turns.ts +5 -7
  238. package/src/process-engine/ops/continue-failed-turn.ts +2 -13
  239. package/src/process-engine/ops/process-observation.ts +119 -0
  240. package/src/process-engine/ops/retry-failed-turn.ts +2 -12
  241. package/src/process-engine/ops/retry-startup.ts +9 -13
  242. package/src/process-engine/ops/start-process.ts +34 -2
  243. package/src/process-engine/ops/turn-outcome.ts +61 -60
  244. package/src/process-engine/ops/turn-wait.ts +225 -0
  245. package/src/process-engine/ops/update-model-config.ts +66 -0
  246. package/src/process-engine/recorder.ts +21 -5
  247. package/src/process-engine/turn-start-preflight.ts +168 -105
  248. package/src/process-engine/turn-wait-state.ts +126 -0
  249. package/src/process-engine/turn-worker-requirement.ts +4 -0
  250. package/src/process-engine/types.ts +9 -0
  251. package/src/process-engine/writes/build-process-abort-writes.ts +30 -20
  252. package/src/process-engine/writes/build-turn-outcome-writes.ts +39 -2
  253. package/src/process-engine/writes/commit-writes.ts +15 -2
  254. package/src/process-engine/writes/writes.ts +59 -74
  255. package/src/process-graph.ts +0 -1
  256. package/src/process-inspection-reader.ts +305 -0
  257. package/src/process-inspection-trace.ts +176 -0
  258. package/src/process-inspection.ts +169 -0
  259. package/src/process-instance-tree-presenter.ts +56 -216
  260. package/src/process-launch-executor.ts +5 -2
  261. package/src/process-launch-plan.ts +17 -2
  262. package/src/process-launcher-registry.ts +28 -16
  263. package/src/process-model-config.ts +100 -0
  264. package/src/process-model-policy/construct.ts +1 -0
  265. package/src/process-model-policy/evaluate.ts +34 -32
  266. package/src/process-model-policy/index.ts +45 -3
  267. package/src/process-model-policy/project.ts +62 -76
  268. package/src/process-model-policy/types.ts +26 -0
  269. package/src/process-model-policy-presenter.ts +7 -3
  270. package/src/process-runtime-availability.ts +9 -4
  271. package/src/process-turn-trace.ts +12 -24
  272. package/src/process-ui-snapshot-presenter.ts +210 -295
  273. package/src/routes/future-executions.ts +5 -5
  274. package/src/routes/launchers.ts +51 -1
  275. package/src/routes/process-actions.ts +9 -9
  276. package/src/routes/process-detail.ts +25 -2
  277. package/src/routes/process-model-config.ts +70 -0
  278. package/src/routes/process-route-helpers.ts +153 -162
  279. package/src/routes/processes.ts +2 -0
  280. package/src/routes/settings.ts +173 -0
  281. package/src/routes/ticket-creation.ts +28 -5
  282. package/src/scoped-settings-service.ts +736 -0
  283. package/src/server-bootstrap/build-host-capabilities.ts +42 -0
  284. package/src/server-bootstrap/register-http.ts +7 -1
  285. package/src/session-summary-projection.ts +1 -2
  286. package/src/skills/bundled-skills.ts +64 -0
  287. package/src/skills/catalog-service.ts +35 -6
  288. package/src/skills/extension-importer.ts +97 -0
  289. package/src/skills/launch-skills.ts +19 -0
  290. package/src/skills/skill-frontmatter.ts +13 -0
  291. package/src/skills/source-importer.ts +3 -0
  292. package/src/startup-evidence.ts +20 -0
  293. package/src/supervisor/execution-inspection-ingestor.ts +55 -0
  294. package/src/supervisor/ipc-handler.ts +2 -0
  295. package/src/supervisor/worker-event-ingestor.ts +30 -14
  296. package/src/supervisor/worker-start-payload-builder.ts +17 -0
  297. package/src/supervisor/worker-supervisor.ts +3 -0
  298. package/src/test-helpers/prepared-fixture-start.ts +1 -0
  299. package/src/test-helpers/process-model-fixtures.ts +2 -0
  300. package/src/test-helpers/scoped-settings-fixtures.ts +126 -0
  301. package/src/test-helpers/skill-fixtures.ts +28 -0
  302. package/src/tool-approval-gate.ts +16 -0
  303. package/src/turn-progress.ts +3 -2
  304. package/src/turn-wait-service.ts +228 -0
  305. package/src/worker-runtime-profile-selection.ts +8 -1
  306. package/dist/app-MSJXFK33.js +0 -8
  307. package/dist/chunk-HK6BLLHA.js.map +0 -1
  308. package/dist/chunk-QCHS3X33.js.map +0 -1
  309. /package/dist/{app-MSJXFK33.js.map → app-JM4CBTND.js.map} +0 -0
@@ -0,0 +1,234 @@
1
+ import type { SettingsOverride, SettingsSubject } from "@leitwerk-dev/domain";
2
+ import { SYSTEM_ACTOR } from "@leitwerk-dev/domain";
3
+ import { and, eq, notInArray } from "drizzle-orm";
4
+ import type { LeitwerkDb } from "./database.js";
5
+ import { generateId, now } from "./repo-helpers.js";
6
+ import * as s from "./schema.js";
7
+
8
+ /** @internal */
9
+ export type SettingsWrite = Omit<SettingsOverride, "revision" | "createdAt" | "updatedAt"> & {
10
+ /** @internal */
11
+ expectedRevision: number;
12
+ };
13
+ /** @internal */
14
+ type SubjectWrite = Pick<SettingsSubject, "scopeType" | "identity" | "label" | "context"> & {
15
+ /** @internal */
16
+ id?: string;
17
+ };
18
+
19
+ function subject(row: typeof s.settingsSubjects.$inferSelect): SettingsSubject {
20
+ const { contextJson, actorJson, ...rest } = row;
21
+ return { ...rest, context: JSON.parse(contextJson), actor: JSON.parse(actorJson) };
22
+ }
23
+
24
+ function override(row: typeof s.settingsOverrides.$inferSelect): SettingsOverride {
25
+ const { valueJson, actorJson, mode, ...rest } = row;
26
+ return {
27
+ ...rest,
28
+ mode: mode as SettingsOverride["mode"],
29
+ value: JSON.parse(valueJson),
30
+ actor: JSON.parse(actorJson),
31
+ };
32
+ }
33
+
34
+ function overrideRow({ value, actor, ...rest }: SettingsOverride) {
35
+ return { ...rest, valueJson: JSON.stringify(value), actorJson: JSON.stringify(actor) };
36
+ }
37
+
38
+ /** @internal */
39
+ export function createScopedSettingsRepo(db: LeitwerkDb) {
40
+ return {
41
+ /** @internal */
42
+ listSubjects(): SettingsSubject[] {
43
+ return db
44
+ .select()
45
+ .from(s.settingsSubjects)
46
+ .where(
47
+ notInArray(
48
+ s.settingsSubjects.id,
49
+ db
50
+ .select({ id: s.settingsSubjectRedirects.subjectId })
51
+ .from(s.settingsSubjectRedirects),
52
+ ),
53
+ )
54
+ .all()
55
+ .map(subject);
56
+ },
57
+ /** @internal */
58
+ getSubject(id: string): SettingsSubject | null {
59
+ const visited = new Set<string>();
60
+ while (!visited.has(id)) {
61
+ visited.add(id);
62
+ const redirect = db
63
+ .select()
64
+ .from(s.settingsSubjectRedirects)
65
+ .where(eq(s.settingsSubjectRedirects.subjectId, id))
66
+ .get();
67
+ if (!redirect) {
68
+ const row = db
69
+ .select()
70
+ .from(s.settingsSubjects)
71
+ .where(eq(s.settingsSubjects.id, id))
72
+ .get();
73
+ return row ? subject(row) : null;
74
+ }
75
+ id = redirect.canonicalSubjectId;
76
+ }
77
+ throw new Error("Cyclic settings subject redirects");
78
+ },
79
+ /** @internal */
80
+ findIdentity(scopeType: string, identity: string): SettingsSubject | null {
81
+ const row = db
82
+ .select()
83
+ .from(s.settingsSubjects)
84
+ .where(
85
+ and(
86
+ eq(s.settingsSubjects.scopeType, scopeType),
87
+ eq(s.settingsSubjects.identity, identity),
88
+ ),
89
+ )
90
+ .get();
91
+ return row ? this.getSubject(row.id) : null;
92
+ },
93
+ /** @internal */
94
+ findAlias(alias: string): SettingsSubject | null {
95
+ const row = db
96
+ .select()
97
+ .from(s.settingsAliases)
98
+ .where(eq(s.settingsAliases.alias, alias))
99
+ .get();
100
+ return row ? this.getSubject(row.subjectId) : null;
101
+ },
102
+ /** @internal */
103
+ putSubject(input: SubjectWrite): SettingsSubject {
104
+ const ts = now();
105
+ const previous = input.id ? this.getSubject(input.id) : null;
106
+ const values = {
107
+ id: input.id ?? generateId("scope"),
108
+ scopeType: input.scopeType,
109
+ identity: input.identity,
110
+ label: input.label,
111
+ contextJson: JSON.stringify(input.context),
112
+ schemaVersion: 1,
113
+ revision: (previous?.revision ?? 0) + 1,
114
+ createdAt: previous?.createdAt ?? ts,
115
+ updatedAt: ts,
116
+ actorJson: JSON.stringify(SYSTEM_ACTOR),
117
+ };
118
+ db.insert(s.settingsSubjects)
119
+ .values(values)
120
+ .onConflictDoUpdate({ target: s.settingsSubjects.id, set: values })
121
+ .run();
122
+ return subject(values);
123
+ },
124
+ /** @internal */
125
+ putAlias(alias: string, subjectId: string): void {
126
+ db.insert(s.settingsAliases).values({ alias, subjectId }).onConflictDoNothing().run();
127
+ if (this.findAlias(alias)?.id !== subjectId)
128
+ throw new Error(`Repository alias '${alias}' already belongs to another settings subject`);
129
+ },
130
+ /** @internal */
131
+ listOverrides(subjectId?: string): SettingsOverride[] {
132
+ const canonicalId = subjectId ? (this.getSubject(subjectId)?.id ?? subjectId) : undefined;
133
+ return db
134
+ .select()
135
+ .from(s.settingsOverrides)
136
+ .where(
137
+ canonicalId
138
+ ? eq(s.settingsOverrides.subjectId, canonicalId)
139
+ : notInArray(
140
+ s.settingsOverrides.subjectId,
141
+ db
142
+ .select({ id: s.settingsSubjectRedirects.subjectId })
143
+ .from(s.settingsSubjectRedirects),
144
+ ),
145
+ )
146
+ .all()
147
+ .map(override);
148
+ },
149
+ /** @internal */
150
+ getOverride(subjectId: string, key: string): SettingsOverride | null {
151
+ subjectId = this.getSubject(subjectId)?.id ?? subjectId;
152
+ const row = db
153
+ .select()
154
+ .from(s.settingsOverrides)
155
+ .where(and(eq(s.settingsOverrides.subjectId, subjectId), eq(s.settingsOverrides.key, key)))
156
+ .get();
157
+ return row ? override(row) : null;
158
+ },
159
+ /** Atomic compare-and-swap, including resets and first writes. @internal */
160
+ write(input: SettingsWrite): SettingsOverride | null {
161
+ const ts = now();
162
+ const { expectedRevision, ...change } = input;
163
+ const values = overrideRow({
164
+ ...change,
165
+ subjectId: this.getSubject(input.subjectId)?.id ?? input.subjectId,
166
+ revision: expectedRevision + 1,
167
+ createdAt: ts,
168
+ updatedAt: ts,
169
+ });
170
+ const { createdAt: _createdAt, ...update } = values;
171
+ if (input.expectedRevision > 0) {
172
+ const row = db
173
+ .update(s.settingsOverrides)
174
+ .set(update)
175
+ .where(
176
+ and(
177
+ eq(s.settingsOverrides.subjectId, values.subjectId),
178
+ eq(s.settingsOverrides.key, input.key),
179
+ eq(s.settingsOverrides.revision, input.expectedRevision),
180
+ ),
181
+ )
182
+ .returning()
183
+ .get();
184
+ return row ? override(row) : null;
185
+ }
186
+ const row = db
187
+ .insert(s.settingsOverrides)
188
+ .values(values)
189
+ .onConflictDoNothing()
190
+ .returning()
191
+ .get();
192
+ return row ? override(row) : null;
193
+ },
194
+ /** Call inside a transaction after verifying that active overrides agree. @internal */
195
+ mergeLocatorSubject(sourceId: string, targetId: string): void {
196
+ const source = this.getSubject(sourceId);
197
+ const target = this.getSubject(targetId);
198
+ if (
199
+ !source ||
200
+ !target ||
201
+ source.id === target.id ||
202
+ source.scopeType !== "repository" ||
203
+ target.scopeType !== "repository" ||
204
+ !source.identity.startsWith("locator:")
205
+ )
206
+ throw new Error("Only distinct locator subjects can merge into a repository");
207
+ for (const incoming of this.listOverrides(source.id)) {
208
+ const current = this.getOverride(target.id, incoming.key);
209
+ const retained = current && !current.reset ? current : incoming;
210
+ const values = overrideRow({
211
+ ...retained,
212
+ subjectId: target.id,
213
+ revision: Math.max(incoming.revision, current?.revision ?? 0) + 1,
214
+ updatedAt: now(),
215
+ });
216
+ db.insert(s.settingsOverrides)
217
+ .values(values)
218
+ .onConflictDoUpdate({
219
+ target: [s.settingsOverrides.subjectId, s.settingsOverrides.key],
220
+ set: values,
221
+ })
222
+ .run();
223
+ }
224
+ db.update(s.settingsAliases)
225
+ .set({ subjectId: target.id })
226
+ .where(eq(s.settingsAliases.subjectId, source.id))
227
+ .run();
228
+ // Keep original subjects and overrides for historical references and attribution.
229
+ db.insert(s.settingsSubjectRedirects)
230
+ .values({ subjectId: source.id, canonicalSubjectId: target.id })
231
+ .run();
232
+ },
233
+ };
234
+ }
@@ -10,6 +10,7 @@ import type {
10
10
  } from "@leitwerk-dev/protocol";
11
11
  import { verifyCanonicalPiResourceBundle } from "@leitwerk-dev/worker-protocol";
12
12
  import { and, asc, count, desc, eq, isNotNull, max, notExists, notInArray, sql } from "drizzle-orm";
13
+ import type { ImportedExtensionSkill } from "../skills/extension-importer.js";
13
14
  import { explicitSkillReferenceIds, referencedSkillIds } from "../skills/skill-dependencies.js";
14
15
  import { isSkillModelInvocable } from "../skills/skill-frontmatter.js";
15
16
  import type { ImportedRepositorySkill, ImportedSkill } from "../skills/source-importer.js";
@@ -45,6 +46,7 @@ function revisionForBundle(db: LeitwerkDb, item: ImportedSkill, timestamp: strin
45
46
  bundleDigest: item.bundle.digest,
46
47
  bundleBytes: Buffer.from(item.bundle.bytes),
47
48
  sourceRevision: item.sourceRevision,
49
+ provenance: item.provenance ?? null,
48
50
  importedAt: timestamp,
49
51
  })
50
52
  .run();
@@ -156,11 +158,16 @@ function pruneNeverInstalledCatalogEntries(db: LeitwerkDb): void {
156
158
  .run();
157
159
  }
158
160
 
159
- function recordRevisionDependencies(db: LeitwerkDb, revisionId: string, markdown: string): void {
161
+ function recordRevisionDependencies(
162
+ db: LeitwerkDb,
163
+ revisionId: string,
164
+ markdown: string,
165
+ dependencyIds = explicitSkillReferenceIds(markdown),
166
+ ): void {
160
167
  db.delete(skillRevisionDependencies)
161
168
  .where(eq(skillRevisionDependencies.skillRevisionId, revisionId))
162
169
  .run();
163
- for (const [position, dependencySkillId] of explicitSkillReferenceIds(markdown).entries()) {
170
+ for (const [position, dependencySkillId] of dependencyIds.entries()) {
164
171
  db.insert(skillRevisionDependencies)
165
172
  .values({ skillRevisionId: revisionId, dependencySkillId, position })
166
173
  .run();
@@ -170,7 +177,7 @@ function recordRevisionDependencies(db: LeitwerkDb, revisionId: string, markdown
170
177
  function activateSkill(
171
178
  db: LeitwerkDb,
172
179
  item: ImportedSkill,
173
- registrationKind: "configuration" | "catalog",
180
+ registrationKind: "configuration" | "catalog" | "extension",
174
181
  timestamp: string,
175
182
  markdown = skillMarkdown(item.skillId, item.bundle.bytes, item.bundle.digest),
176
183
  ): string {
@@ -187,13 +194,14 @@ function activateSkill(
187
194
  .onConflictDoNothing()
188
195
  .run();
189
196
  const revisionId = revisionForBundle(db, item, timestamp);
190
- recordRevisionDependencies(db, revisionId, markdown);
197
+ recordRevisionDependencies(db, revisionId, markdown, item.provenance?.dependencies);
191
198
  db.update(skills)
192
199
  .set({
193
200
  label: item.label,
194
201
  description: item.description,
195
202
  activeRevisionId: revisionId,
196
203
  registrationKind,
204
+ ownerExtensionId: item.provenance?.extensionId ?? null,
197
205
  updatedAt: timestamp,
198
206
  })
199
207
  .where(eq(skills.id, item.skillId))
@@ -201,6 +209,20 @@ function activateSkill(
201
209
  return revisionId;
202
210
  }
203
211
 
212
+ function reconcileSkills(
213
+ db: LeitwerkDb,
214
+ imported: readonly ImportedSkill[],
215
+ registrationKind: "configuration" | "extension",
216
+ ): void {
217
+ const timestamp = now();
218
+ const ids = imported.map((item) => item.skillId);
219
+ db.update(skills)
220
+ .set({ activeRevisionId: null, updatedAt: timestamp })
221
+ .where(and(eq(skills.registrationKind, registrationKind), notInArray(skills.id, ids)))
222
+ .run();
223
+ for (const item of imported) activateSkill(db, item, registrationKind, timestamp);
224
+ }
225
+
204
226
  function catalogView(db: LeitwerkDb): {
205
227
  /** @internal */
206
228
  availableSkills: SkillCatalogItem[];
@@ -219,12 +241,22 @@ function catalogView(db: LeitwerkDb): {
219
241
  activeDigest: skillRevisions.bundleDigest,
220
242
  activeBundleBytes: skillRevisions.bundleBytes,
221
243
  registrationKind: skills.registrationKind,
244
+ ownerExtensionId: skills.ownerExtensionId,
245
+ provenance: skillRevisions.provenance,
222
246
  })
223
247
  .from(skills)
224
248
  .innerJoin(skillRevisions, eq(skills.activeRevisionId, skillRevisions.id))
225
249
  .orderBy(asc(skills.label), asc(skills.id))
226
250
  .all();
227
251
  const installedById = new Map(installedRows.map((row) => [row.id, row]));
252
+ const extensionIds = new Set(
253
+ db
254
+ .select({ id: skills.id })
255
+ .from(skills)
256
+ .where(eq(skills.registrationKind, "extension"))
257
+ .all()
258
+ .map((skill) => skill.id),
259
+ );
228
260
  const availableSkills = candidates.map((candidate) => {
229
261
  const active = installedById.get(candidate.skillId);
230
262
  const registered = Boolean(
@@ -241,7 +273,9 @@ function catalogView(db: LeitwerkDb): {
241
273
  updateAvailable: Boolean(
242
274
  candidate.available && active?.registrationKind === "catalog" && !registered,
243
275
  ),
244
- conflict: candidate.available && active?.registrationKind === "configuration",
276
+ conflict:
277
+ candidate.available &&
278
+ (active?.registrationKind === "configuration" || extensionIds.has(candidate.skillId)),
245
279
  stale: !candidate.available,
246
280
  modelInvocable: isSkillModelInvocable(
247
281
  skillMarkdown(candidate.skillId, candidate.bundleBytes, candidate.bundleDigest),
@@ -262,12 +296,17 @@ function catalogView(db: LeitwerkDb): {
262
296
  activeRevisionId: skill.activeRevisionId,
263
297
  activeSourceRevision: skill.activeSourceRevision,
264
298
  registrationKind: skill.registrationKind,
299
+ ...(skill.ownerExtensionId
300
+ ? { ownerExtensionId: skill.ownerExtensionId, provenance: skill.provenance }
301
+ : {}),
265
302
  sourceRepositoryId:
266
- matching.length === 1
267
- ? (matching[0]?.repositoryId ?? null)
268
- : activeSource.length === 1
269
- ? (activeSource[0]?.repositoryId ?? null)
270
- : null,
303
+ skill.registrationKind === "extension"
304
+ ? null
305
+ : matching.length === 1
306
+ ? (matching[0]?.repositoryId ?? null)
307
+ : activeSource.length === 1
308
+ ? (activeSource[0]?.repositoryId ?? null)
309
+ : null,
271
310
  updateAvailable:
272
311
  skill.registrationKind === "catalog" &&
273
312
  available.length === 1 &&
@@ -296,7 +335,11 @@ export function createSkillRepo(db: LeitwerkDb) {
296
335
  /** @internal */
297
336
  resolveActive(ids: readonly string[]): SkillSelection[] {
298
337
  const activeSkills = db
299
- .select({ id: skills.id, revisionId: skillRevisions.id })
338
+ .select({
339
+ id: skills.id,
340
+ revisionId: skillRevisions.id,
341
+ registrationKind: skills.registrationKind,
342
+ })
300
343
  .from(skills)
301
344
  .innerJoin(skillRevisions, eq(skills.activeRevisionId, skillRevisions.id))
302
345
  .all();
@@ -315,6 +358,8 @@ export function createSkillRepo(db: LeitwerkDb) {
315
358
  if (!skill) throw new Error(`Unknown or unavailable skill '${id}'`);
316
359
  visiting.add(id);
317
360
  for (const dependencyId of dependencies.get(skill.revisionId) ?? []) {
361
+ if (skill.registrationKind === "extension" && !byId.has(dependencyId))
362
+ throw new Error(`Missing dependency '${dependencyId}' for skill '${id}'`);
318
363
  if (dependencyId !== id && byId.has(dependencyId)) add(dependencyId);
319
364
  }
320
365
  visiting.delete(id);
@@ -325,20 +370,30 @@ export function createSkillRepo(db: LeitwerkDb) {
325
370
  },
326
371
  /** @internal */
327
372
  reconcile(imported: readonly ImportedSkill[]): void {
328
- const timestamp = now();
329
- db.update(skills)
330
- .set({ activeRevisionId: null, updatedAt: timestamp })
331
- .where(
332
- and(
333
- eq(skills.registrationKind, "configuration"),
334
- notInArray(
335
- skills.id,
336
- imported.map((item) => item.skillId),
337
- ),
338
- ),
339
- )
340
- .run();
341
- for (const item of imported) activateSkill(db, item, "configuration", timestamp);
373
+ reconcileSkills(db, imported, "configuration");
374
+ },
375
+ /** Reconcile all loaded extension packs in the caller's startup transaction. @internal */
376
+ reconcileExtensions(imported: readonly ImportedExtensionSkill[]): void {
377
+ const byId = new Map(imported.map((item) => [item.skillId, item]));
378
+ if (byId.size !== imported.length) throw new Error("Duplicate extension skill ID");
379
+ for (const item of imported) {
380
+ const current = db.select().from(skills).where(eq(skills.id, item.skillId)).get();
381
+ if (
382
+ current &&
383
+ (current.registrationKind !== "extension" ||
384
+ current.ownerExtensionId !== item.provenance.extensionId)
385
+ ) {
386
+ throw new Error(
387
+ `Skill '${item.skillId}' is already owned by '${current.ownerExtensionId ?? current.registrationKind}'`,
388
+ );
389
+ }
390
+ for (const dependency of item.provenance.dependencies) {
391
+ if (byId.get(dependency)?.provenance.extensionId !== item.provenance.extensionId) {
392
+ throw new Error(`Missing pack dependency '${dependency}' for skill '${item.skillId}'`);
393
+ }
394
+ }
395
+ }
396
+ reconcileSkills(db, imported, "extension");
342
397
  },
343
398
  /** @internal */
344
399
  backfillDependencies(): void {
@@ -347,6 +402,7 @@ export function createSkillRepo(db: LeitwerkDb) {
347
402
  db,
348
403
  revision.id,
349
404
  skillMarkdown(revision.skillId, revision.bundleBytes, revision.bundleDigest),
405
+ revision.provenance?.dependencies,
350
406
  );
351
407
  }
352
408
  },
@@ -423,8 +479,13 @@ export function createSkillRepo(db: LeitwerkDb) {
423
479
  );
424
480
  }
425
481
  const current = db.select().from(skills).where(eq(skills.id, id)).get();
426
- if (current?.activeRevisionId && current.registrationKind === "configuration") {
427
- throw new Error(`Skill '${id}' is managed by configuration`);
482
+ if (
483
+ current?.registrationKind === "extension" ||
484
+ (current?.activeRevisionId && current.registrationKind === "configuration")
485
+ ) {
486
+ throw new Error(
487
+ `Skill '${id}' is managed by ${current.ownerExtensionId ?? "configuration"}`,
488
+ );
428
489
  }
429
490
  visiting.add(id);
430
491
  const markdown = skillMarkdown(id, candidate.bundleBytes, candidate.bundleDigest);
@@ -510,6 +571,7 @@ export function createSkillRepo(db: LeitwerkDb) {
510
571
  id: skillRevisions.id,
511
572
  sourceRevision: skillRevisions.sourceRevision,
512
573
  importedAt: skillRevisions.importedAt,
574
+ provenance: skillRevisions.provenance,
513
575
  })
514
576
  .from(skillRevisions)
515
577
  .where(eq(skillRevisions.skillId, skillId))
@@ -6,6 +6,7 @@ import type {
6
6
  } from "@leitwerk-dev/domain";
7
7
  import { and, eq } from "drizzle-orm";
8
8
  import type { LeitwerkDb } from "./database.js";
9
+ import { type HistoryWindow, historyWindow } from "./history-window.js";
9
10
  import { generateId, mappedItemRef, now, parsePersistedJson } from "./repo-helpers.js";
10
11
  import * as s from "./schema.js";
11
12
 
@@ -65,11 +66,16 @@ export function createTurnStartRecordRepo(db: LeitwerkDb) {
65
66
  return value ? map(value) : null;
66
67
  },
67
68
  /** @internal */
68
- listByInstance(instanceId: string): TurnStartRecord[] {
69
+ listByInstance(instanceId: string, window?: HistoryWindow): TurnStartRecord[] {
69
70
  return db
70
71
  .select()
71
72
  .from(s.turnStartRecords)
72
- .where(eq(s.turnStartRecords.instanceId, instanceId))
73
+ .where(
74
+ and(
75
+ eq(s.turnStartRecords.instanceId, instanceId),
76
+ historyWindow(s.turnStartRecords.createdAt, window),
77
+ ),
78
+ )
73
79
  .all()
74
80
  .map(map);
75
81
  },
@@ -3,7 +3,7 @@ import type {
3
3
  PrimaryPathUiSnapshot,
4
4
  TurnTracePreview,
5
5
  } from "@leitwerk-dev/protocol";
6
- import { eq } from "drizzle-orm";
6
+ import { and, eq, inArray } from "drizzle-orm";
7
7
  import type { LeitwerkDb } from "./database.js";
8
8
  import { sessionSummaries, turnSummaries } from "./schema.js";
9
9
 
@@ -60,12 +60,21 @@ export function createTurnSummaryRepo(db: LeitwerkDb) {
60
60
  .run();
61
61
  },
62
62
  /** @internal */
63
- listByInstance(instanceId: string): Record<string, CompactTurnSummary> {
63
+ listByInstance(
64
+ instanceId: string,
65
+ turnRecordIds?: string[],
66
+ ): Record<string, CompactTurnSummary> {
67
+ if (turnRecordIds?.length === 0) return {};
64
68
  return Object.fromEntries(
65
69
  db
66
70
  .select()
67
71
  .from(turnSummaries)
68
- .where(eq(turnSummaries.instanceId, instanceId))
72
+ .where(
73
+ and(
74
+ eq(turnSummaries.instanceId, instanceId),
75
+ turnRecordIds ? inArray(turnSummaries.turnRecordId, turnRecordIds) : undefined,
76
+ ),
77
+ )
69
78
  .all()
70
79
  .map((row) => [row.turnRecordId, JSON.parse(row.summaryJson) as CompactTurnSummary]),
71
80
  );
@@ -2,6 +2,7 @@ import type { WorkerBootstrapReceipt, WorkerLease, WorkerState } from "@leitwerk
2
2
  import { and, asc, eq, isNull } from "drizzle-orm";
3
3
  import type { SQLiteUpdateSetSource } from "drizzle-orm/sqlite-core";
4
4
  import type { LeitwerkDb } from "./database.js";
5
+ import { type HistoryWindow, historyWindow } from "./history-window.js";
5
6
  import { generateId, now } from "./repo-helpers.js";
6
7
  import * as s from "./schema.js";
7
8
 
@@ -120,11 +121,16 @@ export function createWorkerLeaseRepo(db: LeitwerkDb) {
120
121
  },
121
122
 
122
123
  /** @internal */
123
- listByInstance(instanceId: string): WorkerLease[] {
124
+ listByInstance(instanceId: string, window?: HistoryWindow): WorkerLease[] {
124
125
  return db
125
126
  .select()
126
127
  .from(s.workerLeases)
127
- .where(eq(s.workerLeases.instanceId, instanceId))
128
+ .where(
129
+ and(
130
+ eq(s.workerLeases.instanceId, instanceId),
131
+ historyWindow(s.workerLeases.startedAt, window),
132
+ ),
133
+ )
128
134
  .orderBy(asc(s.workerLeases.startedAt), asc(s.workerLeases.id))
129
135
  .all()
130
136
  .map(rowToWorkerLease);