@leitwerk-dev/worker 0.1.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 (269) hide show
  1. package/dist/LICENSE +13 -0
  2. package/dist/automatic-turn-executor.d.ts +28 -0
  3. package/dist/automatic-turn-executor.d.ts.map +1 -0
  4. package/dist/automatic-turn-executor.js +58 -0
  5. package/dist/automatic-turn-executor.js.map +1 -0
  6. package/dist/chunk-N5HLJDWA.js +8124 -0
  7. package/dist/chunk-N5HLJDWA.js.map +1 -0
  8. package/dist/diagnostics.d.ts +23 -0
  9. package/dist/diagnostics.d.ts.map +1 -0
  10. package/dist/diagnostics.js +51 -0
  11. package/dist/diagnostics.js.map +1 -0
  12. package/dist/entry-runtime.d.ts +24 -0
  13. package/dist/entry-runtime.d.ts.map +1 -0
  14. package/dist/entry-runtime.js +160 -0
  15. package/dist/entry-runtime.js.map +1 -0
  16. package/dist/event-translator.d.ts +8 -0
  17. package/dist/event-translator.d.ts.map +1 -0
  18. package/dist/event-translator.js +12 -0
  19. package/dist/event-translator.js.map +1 -0
  20. package/dist/extension-entry-roots.d.ts +7 -0
  21. package/dist/extension-entry-roots.d.ts.map +1 -0
  22. package/dist/extension-entry-roots.js +41 -0
  23. package/dist/extension-entry-roots.js.map +1 -0
  24. package/dist/identified-pi-entry.d.ts +56 -0
  25. package/dist/identified-pi-entry.d.ts.map +1 -0
  26. package/dist/identified-pi-entry.js +125 -0
  27. package/dist/identified-pi-entry.js.map +1 -0
  28. package/dist/index.d.ts +7 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +6 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/input-consumer.d.ts +24 -0
  33. package/dist/input-consumer.d.ts.map +1 -0
  34. package/dist/input-consumer.js +34 -0
  35. package/dist/input-consumer.js.map +1 -0
  36. package/dist/ipc.d.ts +22 -0
  37. package/dist/ipc.d.ts.map +1 -0
  38. package/dist/ipc.js +192 -0
  39. package/dist/ipc.js.map +1 -0
  40. package/dist/logical-prompt-runner.d.ts +49 -0
  41. package/dist/logical-prompt-runner.d.ts.map +1 -0
  42. package/dist/logical-prompt-runner.js +225 -0
  43. package/dist/logical-prompt-runner.js.map +1 -0
  44. package/dist/managed-pi-agent-dir.d.ts +13 -0
  45. package/dist/managed-pi-agent-dir.d.ts.map +1 -0
  46. package/dist/managed-pi-agent-dir.js +66 -0
  47. package/dist/managed-pi-agent-dir.js.map +1 -0
  48. package/dist/managed-pi-bootstrap.d.ts +24 -0
  49. package/dist/managed-pi-bootstrap.d.ts.map +1 -0
  50. package/dist/managed-pi-bootstrap.js +135 -0
  51. package/dist/managed-pi-bootstrap.js.map +1 -0
  52. package/dist/missing-tool-recovery-runner.d.ts +23 -0
  53. package/dist/missing-tool-recovery-runner.d.ts.map +1 -0
  54. package/dist/missing-tool-recovery-runner.js +73 -0
  55. package/dist/missing-tool-recovery-runner.js.map +1 -0
  56. package/dist/pi-adapter.d.ts +134 -0
  57. package/dist/pi-adapter.d.ts.map +1 -0
  58. package/dist/pi-adapter.js +1648 -0
  59. package/dist/pi-adapter.js.map +1 -0
  60. package/dist/pi-branch-guard.d.ts +39 -0
  61. package/dist/pi-branch-guard.d.ts.map +1 -0
  62. package/dist/pi-branch-guard.js +75 -0
  63. package/dist/pi-branch-guard.js.map +1 -0
  64. package/dist/pi-event-reporter.d.ts +15 -0
  65. package/dist/pi-event-reporter.d.ts.map +1 -0
  66. package/dist/pi-event-reporter.js +136 -0
  67. package/dist/pi-event-reporter.js.map +1 -0
  68. package/dist/pi-resource-bundle.d.ts +12 -0
  69. package/dist/pi-resource-bundle.d.ts.map +1 -0
  70. package/dist/pi-resource-bundle.js +58 -0
  71. package/dist/pi-resource-bundle.js.map +1 -0
  72. package/dist/pre-turn-targeted-inputs.d.ts +44 -0
  73. package/dist/pre-turn-targeted-inputs.d.ts.map +1 -0
  74. package/dist/pre-turn-targeted-inputs.js +101 -0
  75. package/dist/pre-turn-targeted-inputs.js.map +1 -0
  76. package/dist/prompt-guards.d.ts +52 -0
  77. package/dist/prompt-guards.d.ts.map +1 -0
  78. package/dist/prompt-guards.js +318 -0
  79. package/dist/prompt-guards.js.map +1 -0
  80. package/dist/question-bridge.d.ts +16 -0
  81. package/dist/question-bridge.d.ts.map +1 -0
  82. package/dist/question-bridge.js +55 -0
  83. package/dist/question-bridge.js.map +1 -0
  84. package/dist/question-tool.d.ts +14 -0
  85. package/dist/question-tool.d.ts.map +1 -0
  86. package/dist/question-tool.js +54 -0
  87. package/dist/question-tool.js.map +1 -0
  88. package/dist/resource-loader-factory.d.ts +11 -0
  89. package/dist/resource-loader-factory.d.ts.map +1 -0
  90. package/dist/resource-loader-factory.js +87 -0
  91. package/dist/resource-loader-factory.js.map +1 -0
  92. package/dist/result-image-upload.d.ts +14 -0
  93. package/dist/result-image-upload.d.ts.map +1 -0
  94. package/dist/result-image-upload.js +183 -0
  95. package/dist/result-image-upload.js.map +1 -0
  96. package/dist/runtime/adapters.d.ts +48 -0
  97. package/dist/runtime/adapters.d.ts.map +1 -0
  98. package/dist/runtime/adapters.js +15 -0
  99. package/dist/runtime/adapters.js.map +1 -0
  100. package/dist/runtime/bootstrap-session.d.ts +95 -0
  101. package/dist/runtime/bootstrap-session.d.ts.map +1 -0
  102. package/dist/runtime/bootstrap-session.js +198 -0
  103. package/dist/runtime/bootstrap-session.js.map +1 -0
  104. package/dist/runtime/bootstrap.d.ts +81 -0
  105. package/dist/runtime/bootstrap.d.ts.map +1 -0
  106. package/dist/runtime/bootstrap.js +440 -0
  107. package/dist/runtime/bootstrap.js.map +1 -0
  108. package/dist/runtime/failure-policy.d.ts +79 -0
  109. package/dist/runtime/failure-policy.d.ts.map +1 -0
  110. package/dist/runtime/failure-policy.js +182 -0
  111. package/dist/runtime/failure-policy.js.map +1 -0
  112. package/dist/runtime/index.d.ts +9 -0
  113. package/dist/runtime/index.d.ts.map +1 -0
  114. package/dist/runtime/index.js +326 -0
  115. package/dist/runtime/index.js.map +1 -0
  116. package/dist/runtime/lifecycle-reducer.d.ts +328 -0
  117. package/dist/runtime/lifecycle-reducer.d.ts.map +1 -0
  118. package/dist/runtime/lifecycle-reducer.js +1100 -0
  119. package/dist/runtime/lifecycle-reducer.js.map +1 -0
  120. package/dist/runtime/llm-turn-execution.d.ts +42 -0
  121. package/dist/runtime/llm-turn-execution.d.ts.map +1 -0
  122. package/dist/runtime/llm-turn-execution.js +472 -0
  123. package/dist/runtime/llm-turn-execution.js.map +1 -0
  124. package/dist/runtime/settings.d.ts +11 -0
  125. package/dist/runtime/settings.d.ts.map +1 -0
  126. package/dist/runtime/settings.js +23 -0
  127. package/dist/runtime/settings.js.map +1 -0
  128. package/dist/runtime/snapshot-policy.d.ts +11 -0
  129. package/dist/runtime/snapshot-policy.d.ts.map +1 -0
  130. package/dist/runtime/snapshot-policy.js +16 -0
  131. package/dist/runtime/snapshot-policy.js.map +1 -0
  132. package/dist/runtime/turn-execution.d.ts +49 -0
  133. package/dist/runtime/turn-execution.d.ts.map +1 -0
  134. package/dist/runtime/turn-execution.js +120 -0
  135. package/dist/runtime/turn-execution.js.map +1 -0
  136. package/dist/session-snapshot-exchange.d.ts +39 -0
  137. package/dist/session-snapshot-exchange.d.ts.map +1 -0
  138. package/dist/session-snapshot-exchange.js +102 -0
  139. package/dist/session-snapshot-exchange.js.map +1 -0
  140. package/dist/test-helpers/fake-websocket.d.ts +16 -0
  141. package/dist/test-helpers/fake-websocket.d.ts.map +1 -0
  142. package/dist/test-helpers/fake-websocket.js +40 -0
  143. package/dist/test-helpers/fake-websocket.js.map +1 -0
  144. package/dist/test-helpers/ipc-harness.d.ts +11 -0
  145. package/dist/test-helpers/ipc-harness.d.ts.map +1 -0
  146. package/dist/test-helpers/ipc-harness.js +17 -0
  147. package/dist/test-helpers/ipc-harness.js.map +1 -0
  148. package/dist/test-helpers/worker-message-builders.d.ts +21 -0
  149. package/dist/test-helpers/worker-message-builders.d.ts.map +1 -0
  150. package/dist/test-helpers/worker-message-builders.js +62 -0
  151. package/dist/test-helpers/worker-message-builders.js.map +1 -0
  152. package/dist/turn-continuation.d.ts +28 -0
  153. package/dist/turn-continuation.d.ts.map +1 -0
  154. package/dist/turn-continuation.js +80 -0
  155. package/dist/turn-continuation.js.map +1 -0
  156. package/dist/turn-execution-error.d.ts +39 -0
  157. package/dist/turn-execution-error.d.ts.map +1 -0
  158. package/dist/turn-execution-error.js +64 -0
  159. package/dist/turn-execution-error.js.map +1 -0
  160. package/dist/turn-execution-result.d.ts +35 -0
  161. package/dist/turn-execution-result.d.ts.map +1 -0
  162. package/dist/turn-execution-result.js +21 -0
  163. package/dist/turn-execution-result.js.map +1 -0
  164. package/dist/turn-outcome-tool-session.d.ts +50 -0
  165. package/dist/turn-outcome-tool-session.d.ts.map +1 -0
  166. package/dist/turn-outcome-tool-session.js +209 -0
  167. package/dist/turn-outcome-tool-session.js.map +1 -0
  168. package/dist/turn-record-id.d.ts +3 -0
  169. package/dist/turn-record-id.d.ts.map +1 -0
  170. package/dist/turn-record-id.js +10 -0
  171. package/dist/turn-record-id.js.map +1 -0
  172. package/dist/turn-result-markdown.d.ts +74 -0
  173. package/dist/turn-result-markdown.d.ts.map +1 -0
  174. package/dist/turn-result-markdown.js +263 -0
  175. package/dist/turn-result-markdown.js.map +1 -0
  176. package/dist/turn-tree-strategy.d.ts +29 -0
  177. package/dist/turn-tree-strategy.d.ts.map +1 -0
  178. package/dist/turn-tree-strategy.js +80 -0
  179. package/dist/turn-tree-strategy.js.map +1 -0
  180. package/dist/worker-entry.d.ts +2 -0
  181. package/dist/worker-entry.d.ts.map +1 -0
  182. package/dist/worker-entry.js +7 -0
  183. package/dist/worker-entry.js.map +1 -0
  184. package/dist/worker-http.d.ts +2 -0
  185. package/dist/worker-http.d.ts.map +1 -0
  186. package/dist/worker-http.js +9 -0
  187. package/dist/worker-http.js.map +1 -0
  188. package/dist/worker-ipc-reporter.d.ts +29 -0
  189. package/dist/worker-ipc-reporter.d.ts.map +1 -0
  190. package/dist/worker-ipc-reporter.js +58 -0
  191. package/dist/worker-ipc-reporter.js.map +1 -0
  192. package/dist/worker-payloads.d.ts +10 -0
  193. package/dist/worker-payloads.d.ts.map +1 -0
  194. package/dist/worker-payloads.js +66 -0
  195. package/dist/worker-payloads.js.map +1 -0
  196. package/dist/worker-start-payload.d.ts +7 -0
  197. package/dist/worker-start-payload.d.ts.map +1 -0
  198. package/dist/worker-start-payload.js +5 -0
  199. package/dist/worker-start-payload.js.map +1 -0
  200. package/dist/workspace/manifest.d.ts +35 -0
  201. package/dist/workspace/manifest.d.ts.map +1 -0
  202. package/dist/workspace/manifest.js +79 -0
  203. package/dist/workspace/manifest.js.map +1 -0
  204. package/dist/workspace/node-run-root-git-ops.d.ts +17 -0
  205. package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -0
  206. package/dist/workspace/node-run-root-git-ops.js +96 -0
  207. package/dist/workspace/node-run-root-git-ops.js.map +1 -0
  208. package/dist/workspace/resource-aggregator.d.ts +15 -0
  209. package/dist/workspace/resource-aggregator.d.ts.map +1 -0
  210. package/dist/workspace/resource-aggregator.js +39 -0
  211. package/dist/workspace/resource-aggregator.js.map +1 -0
  212. package/dist/workspace/run-root.d.ts +52 -0
  213. package/dist/workspace/run-root.d.ts.map +1 -0
  214. package/dist/workspace/run-root.js +209 -0
  215. package/dist/workspace/run-root.js.map +1 -0
  216. package/package.json +55 -0
  217. package/src/automatic-turn-executor.ts +106 -0
  218. package/src/diagnostics.ts +82 -0
  219. package/src/entry-runtime.ts +235 -0
  220. package/src/event-translator.ts +22 -0
  221. package/src/extension-entry-roots.ts +43 -0
  222. package/src/identified-pi-entry.ts +188 -0
  223. package/src/index.ts +54 -0
  224. package/src/input-consumer.ts +71 -0
  225. package/src/ipc.ts +250 -0
  226. package/src/logical-prompt-runner.ts +320 -0
  227. package/src/managed-pi-agent-dir.ts +86 -0
  228. package/src/managed-pi-bootstrap.ts +208 -0
  229. package/src/missing-tool-recovery-runner.ts +112 -0
  230. package/src/pi-adapter.ts +2197 -0
  231. package/src/pi-branch-guard.ts +104 -0
  232. package/src/pi-event-reporter.ts +164 -0
  233. package/src/pi-resource-bundle.ts +90 -0
  234. package/src/pre-turn-targeted-inputs.ts +167 -0
  235. package/src/prompt-guards.ts +385 -0
  236. package/src/question-bridge.ts +69 -0
  237. package/src/question-tool.ts +65 -0
  238. package/src/resource-loader-factory.ts +115 -0
  239. package/src/result-image-upload.ts +256 -0
  240. package/src/runtime/adapters.ts +67 -0
  241. package/src/runtime/bootstrap-session.ts +294 -0
  242. package/src/runtime/bootstrap.ts +621 -0
  243. package/src/runtime/failure-policy.ts +274 -0
  244. package/src/runtime/index.ts +362 -0
  245. package/src/runtime/lifecycle-reducer.ts +1537 -0
  246. package/src/runtime/llm-turn-execution.ts +696 -0
  247. package/src/runtime/settings.ts +41 -0
  248. package/src/runtime/snapshot-policy.ts +28 -0
  249. package/src/runtime/turn-execution.ts +203 -0
  250. package/src/session-snapshot-exchange.ts +155 -0
  251. package/src/test-helpers/fake-websocket.ts +46 -0
  252. package/src/test-helpers/ipc-harness.ts +19 -0
  253. package/src/test-helpers/worker-message-builders.ts +92 -0
  254. package/src/turn-continuation.ts +147 -0
  255. package/src/turn-execution-error.ts +102 -0
  256. package/src/turn-execution-result.ts +48 -0
  257. package/src/turn-outcome-tool-session.ts +311 -0
  258. package/src/turn-record-id.ts +17 -0
  259. package/src/turn-result-markdown.ts +355 -0
  260. package/src/turn-tree-strategy.ts +141 -0
  261. package/src/worker-entry.ts +8 -0
  262. package/src/worker-http.ts +6 -0
  263. package/src/worker-ipc-reporter.ts +100 -0
  264. package/src/worker-payloads.ts +94 -0
  265. package/src/worker-start-payload.ts +10 -0
  266. package/src/workspace/manifest.ts +126 -0
  267. package/src/workspace/node-run-root-git-ops.ts +137 -0
  268. package/src/workspace/resource-aggregator.ts +43 -0
  269. package/src/workspace/run-root.ts +361 -0
@@ -0,0 +1,94 @@
1
+ import {
2
+ detectRepoLocatorKind,
3
+ type ProcessInstance,
4
+ type ProcessProductRefs,
5
+ type ProcessProject,
6
+ type ProcessSemanticEntryRefs,
7
+ parseProcessProductRefs,
8
+ parseSemanticEntryRefsFromStateJsonStrict,
9
+ parseSemanticEntryRefsLenient,
10
+ } from "@leitwerk-dev/domain";
11
+ import type {
12
+ InputDelivery,
13
+ ProcessInstanceSnapshot,
14
+ ProcessProjectSnapshot,
15
+ } from "@leitwerk-dev/worker-protocol";
16
+ import type { InputItem } from "./input-consumer.js";
17
+
18
+ export function inputDeliveryToItem(delivery: InputDelivery): InputItem {
19
+ return {
20
+ inputId: delivery.inputId,
21
+ sequence: delivery.sequence,
22
+ source: delivery.source,
23
+ kind: delivery.kind,
24
+ target: delivery.target,
25
+ bodyMarkdown: delivery.bodyMarkdown,
26
+ };
27
+ }
28
+
29
+ export function projectSnapshotsFromPayload(raw: ProcessProjectSnapshot[]): ProcessProject[] {
30
+ return raw.map((project) => ({
31
+ id: project.id ?? "",
32
+ instanceId: project.instanceId ?? "",
33
+ key: project.key,
34
+ repoLocator: project.repoLocator,
35
+ repoLocatorKind:
36
+ project.repoLocatorKind ?? detectRepoLocatorKind(project.repoLocator) ?? "remote_url",
37
+ baseBranch: project.baseBranch,
38
+ workBranch: project.workBranch ?? "",
39
+ externalId: project.externalId ?? null,
40
+ externalUrl: project.externalUrl ?? null,
41
+ metadata: project.metadata ?? null,
42
+ pipelineStatus: project.pipelineStatus ?? null,
43
+ createdAt: project.createdAt ?? "",
44
+ updatedAt: project.updatedAt ?? "",
45
+ }));
46
+ }
47
+
48
+ export function buildProcessSnapshotSeed(
49
+ processId: string,
50
+ processSnapshot: ProcessInstanceSnapshot,
51
+ ): ProcessInstance {
52
+ const resolvedProcessId = processSnapshot.processId ?? processId;
53
+ return {
54
+ id: processSnapshot.id ?? "",
55
+ processId: resolvedProcessId,
56
+ selectedTurnId: processSnapshot.selectedTurnId ?? null,
57
+ lifecycleStatus: processSnapshot.lifecycleStatus ?? "discovered",
58
+ currentExecution: processSnapshot.currentExecution ?? null,
59
+ planRevision: processSnapshot.planRevision ?? 0,
60
+ title: processSnapshot.title ?? null,
61
+ externalId: processSnapshot.externalId ?? null,
62
+ externalUrl: processSnapshot.externalUrl ?? null,
63
+ metadata: processSnapshot.metadata ?? null,
64
+ defaultModelProfileId: processSnapshot.defaultModelProfileId ?? null,
65
+ turnConfigsJson: processSnapshot.turnConfigsJson ?? null,
66
+ selectedTurnModelProfileId: processSnapshot.selectedTurnModelProfileId ?? null,
67
+ paramsJson: processSnapshot.paramsJson ?? null,
68
+ stateJson: processSnapshot.stateJson ?? null,
69
+ createdAt: processSnapshot.createdAt ?? "",
70
+ updatedAt: processSnapshot.updatedAt ?? "",
71
+ };
72
+ }
73
+
74
+ export function readProcessSemanticEntryRefs(state: unknown): ProcessSemanticEntryRefs | null {
75
+ if (typeof state !== "object" || state === null || Array.isArray(state)) {
76
+ return null;
77
+ }
78
+ return parseSemanticEntryRefsLenient(
79
+ (state as { semanticEntryRefs?: unknown }).semanticEntryRefs,
80
+ );
81
+ }
82
+
83
+ export function readProcessProductRefs(state: unknown): ProcessProductRefs | null {
84
+ if (typeof state !== "object" || state === null || Array.isArray(state)) {
85
+ return null;
86
+ }
87
+ return parseProcessProductRefs((state as { productRefs?: unknown }).productRefs);
88
+ }
89
+
90
+ export function parseProcessSemanticEntryRefsFromStateJson(
91
+ stateJson: string | null | undefined,
92
+ ): ProcessSemanticEntryRefs {
93
+ return parseSemanticEntryRefsFromStateJsonStrict(stateJson, "process stateJson");
94
+ }
@@ -0,0 +1,10 @@
1
+ import type { WorkerStartPayload } from "@leitwerk-dev/worker-protocol";
2
+
3
+ export type LlmWorkerStartPayload = Extract<WorkerStartPayload, { configSnapshot: unknown }>;
4
+
5
+ /** Correlates the nested bootstrap discriminant with the containing payload variant. */
6
+ export function isLlmWorkerStartPayload(
7
+ payload: WorkerStartPayload,
8
+ ): payload is LlmWorkerStartPayload {
9
+ return payload.bootstrap.kind === "llm";
10
+ }
@@ -0,0 +1,126 @@
1
+ import * as v from "valibot";
2
+
3
+ export interface ComponentManifest {
4
+ version: 1;
5
+ instanceId: string;
6
+ createdAt: string;
7
+ components: ComponentManifestEntry[];
8
+ }
9
+
10
+ export interface ComponentManifestEntry {
11
+ key: string;
12
+ repoLocator: string;
13
+ baseBranch: string;
14
+ workBranch: string;
15
+ clonedAt: string;
16
+ headSha: string;
17
+ }
18
+
19
+ export interface ManifestDiff {
20
+ stale: string[];
21
+ missing: string[];
22
+ extra: string[];
23
+ unchanged: string[];
24
+ }
25
+
26
+ export function serializeManifest(manifest: ComponentManifest): string {
27
+ return JSON.stringify(manifest, null, 2);
28
+ }
29
+
30
+ const jsonObjectSchema = v.custom<Record<string, unknown>>(
31
+ (value): value is Record<string, unknown> =>
32
+ typeof value === "object" && value !== null && !Array.isArray(value),
33
+ "Expected object",
34
+ );
35
+
36
+ const componentManifestEntrySchema = v.pipe(
37
+ jsonObjectSchema,
38
+ v.object({
39
+ key: v.string(),
40
+ repoLocator: v.string(),
41
+ baseBranch: v.string(),
42
+ workBranch: v.string(),
43
+ clonedAt: v.string(),
44
+ headSha: v.string(),
45
+ }),
46
+ );
47
+
48
+ const componentManifestSchema = v.pipe(
49
+ jsonObjectSchema,
50
+ v.object({
51
+ version: v.literal(1),
52
+ instanceId: v.pipe(v.string(), v.minLength(1)),
53
+ createdAt: v.pipe(v.string(), v.minLength(1)),
54
+ components: v.array(componentManifestEntrySchema),
55
+ }),
56
+ );
57
+
58
+ const componentManifestJsonSchema = v.pipe(v.string(), v.parseJson(), componentManifestSchema);
59
+
60
+ function formatManifestValidationError(issues: readonly v.BaseIssue<unknown>[]): string {
61
+ const issue = issues[0];
62
+ if (issue?.type === "parse_json") return "Invalid JSON";
63
+ const path = issue ? v.getDotPath(issue) : null;
64
+ if (!path) return "Manifest must be a JSON object";
65
+ if (path === "version") return "Unsupported manifest version";
66
+ if (path === "instanceId") return "Invalid instanceId";
67
+ if (path === "createdAt") return "Invalid createdAt";
68
+ if (path === "components") return "components must be an array";
69
+ if (path.match(/^components\.\d+$/)) return "Invalid component entry";
70
+ const componentField = path.match(/^components\.\d+\.([^.]*)$/)?.[1];
71
+ if (componentField) return `Invalid component field: ${componentField}`;
72
+ return issue?.message ? `Invalid manifest: ${issue.message}` : "Invalid manifest";
73
+ }
74
+
75
+ export function deserializeManifest(
76
+ content: string,
77
+ ): { ok: true; manifest: ComponentManifest } | { ok: false; error: string } {
78
+ const result = v.safeParse(componentManifestJsonSchema, content, { abortEarly: true });
79
+ if (!result.success) {
80
+ return { ok: false, error: formatManifestValidationError(result.issues) };
81
+ }
82
+ return { ok: true, manifest: result.output };
83
+ }
84
+
85
+ export function diffManifest(
86
+ existing: ComponentManifest,
87
+ serverProjects: Array<{
88
+ key: string;
89
+ repoLocator: string;
90
+ baseBranch: string;
91
+ workBranch: string;
92
+ }>,
93
+ ): ManifestDiff {
94
+ const stale: string[] = [];
95
+ const missing: string[] = [];
96
+ const extra: string[] = [];
97
+ const unchanged: string[] = [];
98
+
99
+ const byKey = new Map(existing.components.map((c) => [c.key, c]));
100
+ const serverByKey = new Map(serverProjects.map((p) => [p.key, p]));
101
+
102
+ for (const p of serverProjects) {
103
+ const m = byKey.get(p.key);
104
+ if (!m) {
105
+ missing.push(p.key);
106
+ continue;
107
+ }
108
+ if (
109
+ m.repoLocator === p.repoLocator &&
110
+ m.baseBranch === p.baseBranch &&
111
+ m.workBranch === p.workBranch
112
+ ) {
113
+ unchanged.push(p.key);
114
+ } else {
115
+ stale.push(p.key);
116
+ }
117
+ }
118
+
119
+ for (const c of existing.components) {
120
+ if (!serverByKey.has(c.key)) {
121
+ extra.push(c.key);
122
+ }
123
+ }
124
+
125
+ return { stale, missing, extra, unchanged };
126
+ }
@@ -0,0 +1,137 @@
1
+ import {
2
+ chmodSync,
3
+ existsSync,
4
+ mkdirSync,
5
+ mkdtempSync,
6
+ readFileSync,
7
+ rmSync,
8
+ writeFileSync,
9
+ } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import path from "node:path";
12
+ import {
13
+ clearRepositoryGitSshWrappers,
14
+ repositoryGitArgs,
15
+ repositoryGitSubprocessEnv,
16
+ setRepositoryGitSshWrapper,
17
+ } from "@leitwerk-dev/process-sdk";
18
+ import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
19
+ import type { WorkerGitSshCredential } from "@leitwerk-dev/worker-protocol";
20
+ import { execa } from "execa";
21
+ import fg from "fast-glob";
22
+ import type { RunRootGitOps } from "./run-root.js";
23
+
24
+ function toAbsolute(targetPath: string): string {
25
+ return path.resolve(targetPath);
26
+ }
27
+
28
+ export class NodeRunRootGitOps implements RunRootGitOps {
29
+ #credentialDir: string | null = null;
30
+
31
+ configureRepositoryCredentials(credentials: readonly WorkerGitSshCredential[]): void {
32
+ this.cleanupRepositoryCredentials();
33
+ if (credentials.length === 0) return;
34
+ const root = mkdtempSync(path.join(tmpdir(), "leitwerk-repo-auth-"));
35
+ chmodSync(root, 0o700);
36
+ this.#credentialDir = root;
37
+ for (const credential of credentials) {
38
+ const dir = path.join(root, encodeURIComponent(credential.projectKey));
39
+ mkdirSync(dir, { mode: 0o700 });
40
+ const key = path.join(dir, "identity");
41
+ const hosts = path.join(dir, "known_hosts");
42
+ const wrapper = path.join(dir, "ssh");
43
+ writeFileSync(key, `${credential.privateKey.trim()}\n`, { mode: 0o600 });
44
+ writeFileSync(hosts, `${credential.knownHosts.trim()}\n`, { mode: 0o600 });
45
+ writeFileSync(
46
+ wrapper,
47
+ `#!/bin/sh\nexec ssh -F /dev/null -o IdentitiesOnly=yes -o IdentityFile="${key}" -o UserKnownHostsFile="${hosts}" -o StrictHostKeyChecking=yes -o BatchMode=yes "$@"\n`,
48
+ { mode: 0o700 },
49
+ );
50
+ setRepositoryGitSshWrapper(credential.projectKey, wrapper);
51
+ }
52
+ }
53
+
54
+ cleanupRepositoryCredentials(): void {
55
+ clearRepositoryGitSshWrappers();
56
+ if (this.#credentialDir) rmSync(this.#credentialDir, { recursive: true, force: true });
57
+ this.#credentialDir = null;
58
+ }
59
+
60
+ private async runGit(
61
+ args: readonly string[],
62
+ cwd?: string,
63
+ projectKey?: string,
64
+ ): Promise<string> {
65
+ const result = await execa(resolveGitBinary(), repositoryGitArgs(args), {
66
+ ...(cwd ? { cwd: toAbsolute(cwd) } : {}),
67
+ env: repositoryGitSubprocessEnv(projectKey ?? "", { GIT_TERMINAL_PROMPT: "0" }),
68
+ extendEnv: false,
69
+ stdin: "ignore",
70
+ });
71
+ return result.stdout;
72
+ }
73
+
74
+ async writeFile(repoDir: string, filePath: string, content: string): Promise<void> {
75
+ const absolutePath = toAbsolute(path.join(repoDir, filePath));
76
+ mkdirSync(path.dirname(absolutePath), { recursive: true });
77
+ writeFileSync(absolutePath, content, "utf8");
78
+ }
79
+
80
+ async readFile(repoDir: string, filePath: string): Promise<string | null> {
81
+ const absolutePath = toAbsolute(path.join(repoDir, filePath));
82
+ if (!existsSync(absolutePath)) {
83
+ return null;
84
+ }
85
+ return readFileSync(absolutePath, "utf8");
86
+ }
87
+
88
+ async clone(repoLocator: string, targetDir: string): Promise<void> {
89
+ const absoluteTargetDir = toAbsolute(targetDir);
90
+ rmSync(absoluteTargetDir, { recursive: true, force: true });
91
+ mkdirSync(path.dirname(absoluteTargetDir), { recursive: true });
92
+ await this.runGit(
93
+ ["clone", repoLocator, absoluteTargetDir],
94
+ undefined,
95
+ path.basename(absoluteTargetDir),
96
+ );
97
+ }
98
+
99
+ async checkout(repoDir: string, branch: string): Promise<void> {
100
+ await this.runGit(["checkout", branch], repoDir);
101
+ }
102
+
103
+ async createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void> {
104
+ await this.runGit(["branch", branchName, startPoint], repoDir);
105
+ }
106
+
107
+ async branchExists(repoDir: string, branchName: string): Promise<boolean> {
108
+ try {
109
+ const output = await this.runGit(
110
+ ["rev-parse", "--verify", "--quiet", `refs/heads/${branchName}`],
111
+ repoDir,
112
+ );
113
+ return output.trim() !== "";
114
+ } catch {
115
+ return false;
116
+ }
117
+ }
118
+
119
+ async getHeadSha(repoDir: string): Promise<string> {
120
+ return (await this.runGit(["rev-parse", "HEAD"], repoDir)).trim();
121
+ }
122
+
123
+ async listFiles(repoDir: string, pattern: string): Promise<string[]> {
124
+ const absoluteRoot = toAbsolute(repoDir);
125
+ if (!existsSync(absoluteRoot)) {
126
+ return [];
127
+ }
128
+ return (
129
+ await fg(pattern, {
130
+ cwd: absoluteRoot,
131
+ dot: true,
132
+ onlyFiles: true,
133
+ ignore: ["**/.git/**"],
134
+ })
135
+ ).sort((a, b) => a.localeCompare(b));
136
+ }
137
+ }
@@ -0,0 +1,43 @@
1
+ export function formatProvenanceMarker(componentKey: string, filePath: string): string {
2
+ const normalizedPath = filePath.replace(/\\/g, "/");
3
+ return `<!-- source: ${componentKey}/${normalizedPath} -->`;
4
+ }
5
+
6
+ export function aggregateAgentsMd(
7
+ sources: Array<{ componentKey: string; filePath: string; content: string }>,
8
+ ): string {
9
+ const sorted = [...sources].sort((a, b) => a.componentKey.localeCompare(b.componentKey));
10
+ if (sorted.length === 0) {
11
+ return "";
12
+ }
13
+ const parts: string[] = [];
14
+ for (const s of sorted) {
15
+ parts.push(formatProvenanceMarker(s.componentKey, s.filePath));
16
+ parts.push(s.content.trimEnd());
17
+ }
18
+ return `${parts.join("\n\n").trimEnd()}\n`;
19
+ }
20
+
21
+ export function collectSkills(
22
+ sources: Array<{ componentKey: string; skillId: string; content: string }>,
23
+ ): Map<string, { content: string; source: string }> {
24
+ const byId = new Map<string, Array<{ componentKey: string; skillId: string; content: string }>>();
25
+ for (const s of sources) {
26
+ const list = byId.get(s.skillId);
27
+ if (list) {
28
+ list.push(s);
29
+ } else {
30
+ byId.set(s.skillId, [s]);
31
+ }
32
+ }
33
+ const out = new Map<string, { content: string; source: string }>();
34
+ for (const [skillId, group] of byId) {
35
+ group.sort((a, b) => a.componentKey.localeCompare(b.componentKey));
36
+ const winner = group[0];
37
+ out.set(skillId, {
38
+ content: winner.content,
39
+ source: winner.componentKey,
40
+ });
41
+ }
42
+ return out;
43
+ }