@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,361 @@
1
+ import path from "node:path";
2
+ import {
3
+ type ComponentManifest,
4
+ type ComponentManifestEntry,
5
+ deserializeManifest,
6
+ diffManifest,
7
+ type ManifestDiff,
8
+ serializeManifest,
9
+ } from "./manifest.js";
10
+ import { aggregateAgentsMd, collectSkills } from "./resource-aggregator.js";
11
+
12
+ export interface GitOps {
13
+ clone(repoLocator: string, targetDir: string): Promise<void>;
14
+ checkout(repoDir: string, branch: string): Promise<void>;
15
+ createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void>;
16
+ branchExists(repoDir: string, branchName: string): Promise<boolean>;
17
+ getHeadSha(repoDir: string): Promise<string>;
18
+ readFile(repoDir: string, filePath: string): Promise<string | null>;
19
+ listFiles(repoDir: string, pattern: string): Promise<string[]>;
20
+ }
21
+
22
+ export type RunRootGitOps = GitOps & {
23
+ writeFile(repoDir: string, filePath: string, content: string): Promise<void>;
24
+ configureRepositoryCredentials?(
25
+ credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerGitSshCredential[],
26
+ ): void;
27
+ cleanupRepositoryCredentials?(): void;
28
+ };
29
+
30
+ export interface RunRootPlan {
31
+ workspaceRoot: string;
32
+ instanceId: string;
33
+ components: Array<{
34
+ key: string;
35
+ repoLocator: string;
36
+ baseBranch: string;
37
+ workBranch: string;
38
+ }>;
39
+ }
40
+
41
+ export interface MaterializeResult {
42
+ ok: boolean;
43
+ manifest: ComponentManifest;
44
+ aggregatedAgentsMdSources: string[];
45
+ loadedSkills: string[];
46
+ errors: string[];
47
+ }
48
+
49
+ export interface ValidationResult {
50
+ valid: boolean;
51
+ diff: ManifestDiff;
52
+ existingManifest: ComponentManifest | null;
53
+ }
54
+
55
+ const MANIFEST_REL = path.join(".leitwerk", "components.json");
56
+ const AGGREGATED_AGENTS_REL = "AGENTS.md";
57
+ const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
58
+
59
+ const SKILL_GLOB = "**/.cursor/skills/**/SKILL.md";
60
+ const AGENTS_GLOB = "**/AGENTS.md";
61
+
62
+ function isPathInside(parent: string, child: string): boolean {
63
+ const relative = path.relative(parent, child);
64
+ return (
65
+ relative !== "" &&
66
+ relative !== ".." &&
67
+ !relative.startsWith(`..${path.sep}`) &&
68
+ !path.isAbsolute(relative)
69
+ );
70
+ }
71
+
72
+ function safeJoinInsideWorkspace(workspaceRoot: string, childPath: string): string {
73
+ const root = path.resolve(workspaceRoot);
74
+ const target = path.resolve(root, childPath);
75
+ if (!isPathInside(root, target)) {
76
+ throw new Error(`Component key '${childPath}' resolves outside workspace root '${root}'`);
77
+ }
78
+ return target;
79
+ }
80
+
81
+ function componentDir(workspaceRoot: string, key: string): string {
82
+ return safeJoinInsideWorkspace(workspaceRoot, key);
83
+ }
84
+
85
+ function skillIdFromSkillPath(filePath: string): string | null {
86
+ const normalized = filePath.replace(/\\/g, "/");
87
+ const m = normalized.match(/\.cursor\/skills\/([^/]+)\/SKILL\.md$/);
88
+ return m?.[1] ?? null;
89
+ }
90
+
91
+ export function planRunRoot(
92
+ workspaceRoot: string,
93
+ instanceId: string,
94
+ projectSnapshots: Array<{
95
+ key: string;
96
+ repoLocator: string;
97
+ baseBranch: string;
98
+ workBranch: string;
99
+ }>,
100
+ ): RunRootPlan {
101
+ return {
102
+ workspaceRoot,
103
+ instanceId,
104
+ components: projectSnapshots.map((p) => ({ ...p })),
105
+ };
106
+ }
107
+
108
+ async function materializeComponentEntry(
109
+ plan: RunRootPlan,
110
+ comp: RunRootPlan["components"][number],
111
+ git: RunRootGitOps,
112
+ ): Promise<ComponentManifestEntry> {
113
+ const dir = componentDir(plan.workspaceRoot, comp.key);
114
+ await git.clone(comp.repoLocator, dir);
115
+ await git.checkout(dir, comp.baseBranch);
116
+ if (await git.branchExists(dir, comp.workBranch)) {
117
+ await git.checkout(dir, comp.workBranch);
118
+ } else {
119
+ await git.createBranch(dir, comp.workBranch, comp.baseBranch);
120
+ await git.checkout(dir, comp.workBranch);
121
+ }
122
+ return {
123
+ key: comp.key,
124
+ repoLocator: comp.repoLocator,
125
+ baseBranch: comp.baseBranch,
126
+ workBranch: comp.workBranch,
127
+ clonedAt: new Date().toISOString(),
128
+ headSha: await git.getHeadSha(dir),
129
+ };
130
+ }
131
+
132
+ async function collectMaterializedEntries(
133
+ plan: RunRootPlan,
134
+ git: RunRootGitOps,
135
+ errors: string[],
136
+ shouldMaterialize: (componentKey: string) => boolean = () => true,
137
+ existingEntryForKey?: (componentKey: string) => ComponentManifestEntry | null,
138
+ ): Promise<ComponentManifestEntry[]> {
139
+ const entries: ComponentManifestEntry[] = [];
140
+ for (const comp of plan.components) {
141
+ if (!shouldMaterialize(comp.key)) {
142
+ const existing = existingEntryForKey?.(comp.key) ?? null;
143
+ if (existing) {
144
+ entries.push(existing);
145
+ } else {
146
+ errors.push(`${comp.key}: no existing manifest entry for unchanged key`);
147
+ }
148
+ continue;
149
+ }
150
+ try {
151
+ entries.push(await materializeComponentEntry(plan, comp, git));
152
+ } catch (e) {
153
+ errors.push(`${comp.key}: ${e instanceof Error ? e.message : String(e)}`);
154
+ }
155
+ }
156
+ return entries;
157
+ }
158
+
159
+ async function gatherAgentsAndSkills(
160
+ plan: RunRootPlan,
161
+ git: RunRootGitOps,
162
+ ): Promise<{
163
+ aggregatedAgentsMdSources: string[];
164
+ agentsMd: string;
165
+ skillsMap: Map<string, { content: string; source: string }>;
166
+ }> {
167
+ const agentSources: Array<{
168
+ componentKey: string;
169
+ filePath: string;
170
+ content: string;
171
+ }> = [];
172
+ const skillSources: Array<{
173
+ componentKey: string;
174
+ skillId: string;
175
+ content: string;
176
+ }> = [];
177
+ const aggregatedAgentsMdSources: string[] = [];
178
+
179
+ for (const comp of plan.components) {
180
+ const root = componentDir(plan.workspaceRoot, comp.key);
181
+ const agentPaths = await git.listFiles(root, AGENTS_GLOB);
182
+ const sortedAgentPaths = [...agentPaths].sort((a, b) => a.localeCompare(b));
183
+ for (const p of sortedAgentPaths) {
184
+ const content = await git.readFile(root, p);
185
+ if (content !== null) {
186
+ agentSources.push({
187
+ componentKey: comp.key,
188
+ filePath: p,
189
+ content,
190
+ });
191
+ aggregatedAgentsMdSources.push(`${comp.key}/${p}`);
192
+ }
193
+ }
194
+
195
+ const skillPaths = await git.listFiles(root, SKILL_GLOB);
196
+ for (const p of skillPaths) {
197
+ const skillId = skillIdFromSkillPath(p);
198
+ if (!skillId) {
199
+ continue;
200
+ }
201
+ const content = await git.readFile(root, p);
202
+ if (content !== null) {
203
+ skillSources.push({ componentKey: comp.key, skillId, content });
204
+ }
205
+ }
206
+ }
207
+
208
+ const agentsMd = aggregateAgentsMd(agentSources);
209
+ const skillsMap = collectSkills(skillSources);
210
+ aggregatedAgentsMdSources.sort((a, b) => a.localeCompare(b));
211
+ return { aggregatedAgentsMdSources, agentsMd, skillsMap };
212
+ }
213
+
214
+ async function writeAggregatedArtifacts(
215
+ plan: RunRootPlan,
216
+ git: RunRootGitOps,
217
+ manifest: ComponentManifest,
218
+ agentsMd: string,
219
+ skillsMap: Map<string, { content: string; source: string }>,
220
+ ): Promise<void> {
221
+ const root = plan.workspaceRoot;
222
+ await git.writeFile(root, MANIFEST_REL, serializeManifest(manifest));
223
+ await git.writeFile(root, AGGREGATED_AGENTS_REL, agentsMd);
224
+ for (const [skillId, { content }] of skillsMap) {
225
+ const rel = path.join(SKILLS_DIR_REL, `${skillId}.md`);
226
+ await git.writeFile(root, rel, content);
227
+ }
228
+ }
229
+
230
+ async function finalizeRunRoot(
231
+ plan: RunRootPlan,
232
+ git: RunRootGitOps,
233
+ entries: ComponentManifestEntry[],
234
+ errors: string[],
235
+ ) {
236
+ const manifest: ComponentManifest = {
237
+ version: 1,
238
+ instanceId: plan.instanceId,
239
+ createdAt: new Date().toISOString(),
240
+ components: entries,
241
+ };
242
+
243
+ const { aggregatedAgentsMdSources, agentsMd, skillsMap } = await gatherAgentsAndSkills(plan, git);
244
+ const loadedSkills = [...skillsMap.keys()].sort((a, b) => a.localeCompare(b));
245
+
246
+ try {
247
+ await writeAggregatedArtifacts(plan, git, manifest, agentsMd, skillsMap);
248
+ } catch (e) {
249
+ errors.push(`write: ${e instanceof Error ? e.message : String(e)}`);
250
+ }
251
+
252
+ return { manifest, aggregatedAgentsMdSources, loadedSkills };
253
+ }
254
+
255
+ export async function materializeRunRoot(
256
+ plan: RunRootPlan,
257
+ git: RunRootGitOps,
258
+ ): Promise<MaterializeResult> {
259
+ const errors: string[] = [];
260
+ const entries = await collectMaterializedEntries(plan, git, errors);
261
+ const { manifest, aggregatedAgentsMdSources, loadedSkills } = await finalizeRunRoot(
262
+ plan,
263
+ git,
264
+ entries,
265
+ errors,
266
+ );
267
+
268
+ const ok = errors.length === 0 && plan.components.length === entries.length;
269
+ return {
270
+ ok,
271
+ manifest,
272
+ aggregatedAgentsMdSources,
273
+ loadedSkills,
274
+ errors,
275
+ };
276
+ }
277
+
278
+ export async function validateRunRoot(
279
+ workspaceRoot: string,
280
+ serverProjects: Array<{
281
+ key: string;
282
+ repoLocator: string;
283
+ baseBranch: string;
284
+ workBranch: string;
285
+ }>,
286
+ git: GitOps,
287
+ ): Promise<ValidationResult> {
288
+ const raw = await git.readFile(workspaceRoot, MANIFEST_REL);
289
+ if (raw === null) {
290
+ return {
291
+ valid: false,
292
+ diff: {
293
+ stale: [],
294
+ missing: serverProjects.map((p) => p.key),
295
+ extra: [],
296
+ unchanged: [],
297
+ },
298
+ existingManifest: null,
299
+ };
300
+ }
301
+ const parsed = deserializeManifest(raw);
302
+ if (!parsed.ok) {
303
+ return {
304
+ valid: false,
305
+ diff: {
306
+ stale: [],
307
+ missing: serverProjects.map((p) => p.key),
308
+ extra: [],
309
+ unchanged: [],
310
+ },
311
+ existingManifest: null,
312
+ };
313
+ }
314
+ const diff = diffManifest(parsed.manifest, serverProjects);
315
+ const valid = diff.stale.length === 0 && diff.missing.length === 0 && diff.extra.length === 0;
316
+ return {
317
+ valid,
318
+ diff,
319
+ existingManifest: parsed.manifest,
320
+ };
321
+ }
322
+
323
+ export async function repairRunRoot(
324
+ _workspaceRoot: string,
325
+ validation: ValidationResult,
326
+ plan: RunRootPlan,
327
+ git: RunRootGitOps,
328
+ ): Promise<MaterializeResult> {
329
+ const errors: string[] = [];
330
+ const toRepair = new Set([...validation.diff.stale, ...validation.diff.missing]);
331
+ const manifestEntries = await collectMaterializedEntries(
332
+ plan,
333
+ git,
334
+ errors,
335
+ (componentKey) => toRepair.has(componentKey),
336
+ (componentKey) =>
337
+ validation.existingManifest?.components.find((component) => component.key === componentKey) ??
338
+ null,
339
+ );
340
+
341
+ const { manifest, aggregatedAgentsMdSources, loadedSkills } = await finalizeRunRoot(
342
+ plan,
343
+ git,
344
+ manifestEntries,
345
+ errors,
346
+ );
347
+
348
+ const expectedKeys = new Set(plan.components.map((c) => c.key));
349
+ const ok =
350
+ errors.length === 0 &&
351
+ manifestEntries.length === plan.components.length &&
352
+ [...expectedKeys].every((k) => manifestEntries.some((e) => e.key === k));
353
+
354
+ return {
355
+ ok,
356
+ manifest,
357
+ aggregatedAgentsMdSources,
358
+ loadedSkills,
359
+ errors,
360
+ };
361
+ }