@leitwerk-dev/worker 0.1.8 → 0.2.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 (235) hide show
  1. package/dist/automatic-turn-executor.d.ts +1 -0
  2. package/dist/automatic-turn-executor.d.ts.map +1 -1
  3. package/dist/{chunk-ZTJ7UAOC.js → chunk-Y7S2MZD3.js} +1423 -917
  4. package/dist/chunk-Y7S2MZD3.js.map +1 -0
  5. package/dist/connection-diagnostics.d.ts +5 -0
  6. package/dist/connection-diagnostics.d.ts.map +1 -0
  7. package/dist/container-entry-main.d.ts +2 -0
  8. package/dist/container-entry-main.d.ts.map +1 -0
  9. package/dist/container-entry-main.js +232 -0
  10. package/dist/container-entry-main.js.map +1 -0
  11. package/dist/container-entry.d.ts +14 -0
  12. package/dist/container-entry.d.ts.map +1 -0
  13. package/dist/development-tool-environment.d.ts +48 -0
  14. package/dist/development-tool-environment.d.ts.map +1 -0
  15. package/dist/diagnostics.d.ts +9 -1
  16. package/dist/diagnostics.d.ts.map +1 -1
  17. package/dist/docker-registry-credentials.d.ts +8 -0
  18. package/dist/docker-registry-credentials.d.ts.map +1 -0
  19. package/dist/entry-runtime.d.ts +1 -0
  20. package/dist/entry-runtime.d.ts.map +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +44 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/input-consumer.d.ts +6 -11
  26. package/dist/input-consumer.d.ts.map +1 -1
  27. package/dist/ipc.d.ts +1 -0
  28. package/dist/ipc.d.ts.map +1 -1
  29. package/dist/logical-prompt-runner.d.ts.map +1 -1
  30. package/dist/managed-pi-bootstrap.d.ts.map +1 -1
  31. package/dist/missing-tool-recovery-runner.d.ts +4 -3
  32. package/dist/missing-tool-recovery-runner.d.ts.map +1 -1
  33. package/dist/pi-adapter.d.ts +1 -0
  34. package/dist/pi-adapter.d.ts.map +1 -1
  35. package/dist/pi-event-reporter.d.ts.map +1 -1
  36. package/dist/pi-resource-bundle.d.ts +11 -0
  37. package/dist/pi-resource-bundle.d.ts.map +1 -1
  38. package/dist/pre-turn-targeted-inputs.d.ts +8 -7
  39. package/dist/pre-turn-targeted-inputs.d.ts.map +1 -1
  40. package/dist/prompt-guards.d.ts.map +1 -1
  41. package/dist/repository-bash-tool.d.ts +4 -0
  42. package/dist/repository-bash-tool.d.ts.map +1 -0
  43. package/dist/resource-loader-factory.d.ts +1 -2
  44. package/dist/resource-loader-factory.d.ts.map +1 -1
  45. package/dist/result-image-upload.d.ts.map +1 -1
  46. package/dist/runtime/adapters.d.ts +2 -0
  47. package/dist/runtime/adapters.d.ts.map +1 -1
  48. package/dist/runtime/bootstrap-session.d.ts +8 -5
  49. package/dist/runtime/bootstrap-session.d.ts.map +1 -1
  50. package/dist/runtime/bootstrap.d.ts +14 -18
  51. package/dist/runtime/bootstrap.d.ts.map +1 -1
  52. package/dist/runtime/failure-policy.d.ts +1 -0
  53. package/dist/runtime/failure-policy.d.ts.map +1 -1
  54. package/dist/runtime/index.d.ts.map +1 -1
  55. package/dist/runtime/lifecycle-reducer.d.ts +4 -1
  56. package/dist/runtime/lifecycle-reducer.d.ts.map +1 -1
  57. package/dist/runtime/llm-turn-execution.d.ts +3 -8
  58. package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
  59. package/dist/runtime/turn-execution.d.ts +2 -9
  60. package/dist/runtime/turn-execution.d.ts.map +1 -1
  61. package/dist/session-snapshot-exchange.d.ts +0 -4
  62. package/dist/session-snapshot-exchange.d.ts.map +1 -1
  63. package/dist/turn-continuation.d.ts.map +1 -1
  64. package/dist/turn-execution-error.d.ts +1 -1
  65. package/dist/turn-execution-error.d.ts.map +1 -1
  66. package/dist/turn-execution-result.d.ts +1 -0
  67. package/dist/turn-execution-result.d.ts.map +1 -1
  68. package/dist/turn-outcome-tool-session.d.ts +0 -2
  69. package/dist/turn-outcome-tool-session.d.ts.map +1 -1
  70. package/dist/turn-result-markdown.d.ts +2 -1
  71. package/dist/turn-result-markdown.d.ts.map +1 -1
  72. package/dist/turn-tree-strategy.d.ts +0 -1
  73. package/dist/turn-tree-strategy.d.ts.map +1 -1
  74. package/dist/worker-entry.js +16 -4
  75. package/dist/worker-entry.js.map +1 -1
  76. package/dist/worker-ipc-reporter.d.ts.map +1 -1
  77. package/dist/workspace/manifest.d.ts +4 -7
  78. package/dist/workspace/manifest.d.ts.map +1 -1
  79. package/dist/workspace/node-run-root-git-ops.d.ts +2 -2
  80. package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -1
  81. package/dist/workspace/resource-aggregator.d.ts.map +1 -1
  82. package/dist/workspace/run-root.d.ts +9 -20
  83. package/dist/workspace/run-root.d.ts.map +1 -1
  84. package/package.json +8 -8
  85. package/src/automatic-turn-executor.ts +2 -0
  86. package/src/connection-diagnostics.ts +27 -0
  87. package/src/container-entry-main.ts +24 -0
  88. package/src/container-entry.ts +246 -0
  89. package/src/development-tool-environment.ts +452 -0
  90. package/src/diagnostics.ts +3 -1
  91. package/src/docker-registry-credentials.ts +65 -0
  92. package/src/entry-runtime.ts +5 -4
  93. package/src/index.ts +11 -0
  94. package/src/input-consumer.ts +7 -29
  95. package/src/ipc.ts +37 -2
  96. package/src/logical-prompt-runner.ts +23 -29
  97. package/src/managed-pi-bootstrap.ts +3 -3
  98. package/src/missing-tool-recovery-runner.ts +4 -18
  99. package/src/pi-adapter.ts +19 -50
  100. package/src/pi-event-reporter.ts +16 -38
  101. package/src/pi-resource-bundle.ts +67 -1
  102. package/src/pre-turn-targeted-inputs.ts +9 -11
  103. package/src/prompt-guards.ts +44 -75
  104. package/src/repository-bash-tool.ts +15 -0
  105. package/src/resource-loader-factory.ts +1 -18
  106. package/src/result-image-upload.ts +16 -27
  107. package/src/runtime/adapters.ts +2 -0
  108. package/src/runtime/bootstrap-session.ts +69 -39
  109. package/src/runtime/bootstrap.ts +160 -103
  110. package/src/runtime/failure-policy.ts +36 -42
  111. package/src/runtime/index.ts +49 -14
  112. package/src/runtime/lifecycle-reducer.ts +140 -173
  113. package/src/runtime/llm-turn-execution.ts +13 -30
  114. package/src/runtime/turn-execution.ts +91 -9
  115. package/src/session-snapshot-exchange.ts +23 -64
  116. package/src/turn-continuation.ts +9 -17
  117. package/src/turn-execution-error.ts +3 -11
  118. package/src/turn-execution-result.ts +1 -0
  119. package/src/turn-outcome-tool-session.ts +25 -21
  120. package/src/turn-result-markdown.ts +31 -34
  121. package/src/turn-tree-strategy.ts +6 -19
  122. package/src/worker-entry.ts +8 -0
  123. package/src/worker-ipc-reporter.ts +8 -16
  124. package/src/workspace/manifest.ts +5 -7
  125. package/src/workspace/node-run-root-git-ops.ts +65 -25
  126. package/src/workspace/resource-aggregator.ts +5 -16
  127. package/src/workspace/run-root.ts +38 -89
  128. package/dist/automatic-turn-executor.js +0 -58
  129. package/dist/automatic-turn-executor.js.map +0 -1
  130. package/dist/chunk-ZTJ7UAOC.js.map +0 -1
  131. package/dist/diagnostics.js +0 -51
  132. package/dist/diagnostics.js.map +0 -1
  133. package/dist/entry-runtime.js +0 -160
  134. package/dist/entry-runtime.js.map +0 -1
  135. package/dist/event-translator.js +0 -12
  136. package/dist/event-translator.js.map +0 -1
  137. package/dist/extension-entry-roots.js +0 -41
  138. package/dist/extension-entry-roots.js.map +0 -1
  139. package/dist/identified-pi-entry.js +0 -125
  140. package/dist/identified-pi-entry.js.map +0 -1
  141. package/dist/input-consumer.js +0 -34
  142. package/dist/input-consumer.js.map +0 -1
  143. package/dist/integration-tool-bridge.js +0 -53
  144. package/dist/integration-tool-bridge.js.map +0 -1
  145. package/dist/ipc.js +0 -192
  146. package/dist/ipc.js.map +0 -1
  147. package/dist/logical-prompt-runner.js +0 -225
  148. package/dist/logical-prompt-runner.js.map +0 -1
  149. package/dist/managed-pi-agent-dir.js +0 -66
  150. package/dist/managed-pi-agent-dir.js.map +0 -1
  151. package/dist/managed-pi-bootstrap.js +0 -135
  152. package/dist/managed-pi-bootstrap.js.map +0 -1
  153. package/dist/missing-tool-recovery-runner.js +0 -73
  154. package/dist/missing-tool-recovery-runner.js.map +0 -1
  155. package/dist/pi-adapter.js +0 -1648
  156. package/dist/pi-adapter.js.map +0 -1
  157. package/dist/pi-branch-guard.js +0 -75
  158. package/dist/pi-branch-guard.js.map +0 -1
  159. package/dist/pi-event-reporter.js +0 -136
  160. package/dist/pi-event-reporter.js.map +0 -1
  161. package/dist/pi-resource-bundle.js +0 -58
  162. package/dist/pi-resource-bundle.js.map +0 -1
  163. package/dist/pre-turn-targeted-inputs.js +0 -101
  164. package/dist/pre-turn-targeted-inputs.js.map +0 -1
  165. package/dist/prompt-guards.js +0 -318
  166. package/dist/prompt-guards.js.map +0 -1
  167. package/dist/question-bridge.js +0 -24
  168. package/dist/question-bridge.js.map +0 -1
  169. package/dist/question-tool.js +0 -54
  170. package/dist/question-tool.js.map +0 -1
  171. package/dist/replaying-request-bridge.js +0 -71
  172. package/dist/replaying-request-bridge.js.map +0 -1
  173. package/dist/resource-loader-factory.js +0 -87
  174. package/dist/resource-loader-factory.js.map +0 -1
  175. package/dist/result-image-upload.js +0 -183
  176. package/dist/result-image-upload.js.map +0 -1
  177. package/dist/runtime/adapters.js +0 -15
  178. package/dist/runtime/adapters.js.map +0 -1
  179. package/dist/runtime/bootstrap-session.js +0 -199
  180. package/dist/runtime/bootstrap-session.js.map +0 -1
  181. package/dist/runtime/bootstrap.js +0 -440
  182. package/dist/runtime/bootstrap.js.map +0 -1
  183. package/dist/runtime/failure-policy.js +0 -182
  184. package/dist/runtime/failure-policy.js.map +0 -1
  185. package/dist/runtime/index.js +0 -336
  186. package/dist/runtime/index.js.map +0 -1
  187. package/dist/runtime/lifecycle-reducer.js +0 -1100
  188. package/dist/runtime/lifecycle-reducer.js.map +0 -1
  189. package/dist/runtime/llm-turn-execution.js +0 -494
  190. package/dist/runtime/llm-turn-execution.js.map +0 -1
  191. package/dist/runtime/settings.js +0 -24
  192. package/dist/runtime/settings.js.map +0 -1
  193. package/dist/runtime/snapshot-policy.js +0 -16
  194. package/dist/runtime/snapshot-policy.js.map +0 -1
  195. package/dist/runtime/turn-execution.js +0 -121
  196. package/dist/runtime/turn-execution.js.map +0 -1
  197. package/dist/session-snapshot-exchange.js +0 -102
  198. package/dist/session-snapshot-exchange.js.map +0 -1
  199. package/dist/test-helpers/fake-websocket.js +0 -40
  200. package/dist/test-helpers/fake-websocket.js.map +0 -1
  201. package/dist/test-helpers/ipc-harness.d.ts +0 -11
  202. package/dist/test-helpers/ipc-harness.d.ts.map +0 -1
  203. package/dist/test-helpers/ipc-harness.js +0 -17
  204. package/dist/test-helpers/ipc-harness.js.map +0 -1
  205. package/dist/turn-continuation.js +0 -80
  206. package/dist/turn-continuation.js.map +0 -1
  207. package/dist/turn-execution-error.js +0 -64
  208. package/dist/turn-execution-error.js.map +0 -1
  209. package/dist/turn-execution-result.js +0 -21
  210. package/dist/turn-execution-result.js.map +0 -1
  211. package/dist/turn-outcome-tool-session.js +0 -210
  212. package/dist/turn-outcome-tool-session.js.map +0 -1
  213. package/dist/turn-record-id.js +0 -10
  214. package/dist/turn-record-id.js.map +0 -1
  215. package/dist/turn-result-markdown.js +0 -263
  216. package/dist/turn-result-markdown.js.map +0 -1
  217. package/dist/turn-tree-strategy.js +0 -80
  218. package/dist/turn-tree-strategy.js.map +0 -1
  219. package/dist/worker-http.js +0 -9
  220. package/dist/worker-http.js.map +0 -1
  221. package/dist/worker-ipc-reporter.js +0 -58
  222. package/dist/worker-ipc-reporter.js.map +0 -1
  223. package/dist/worker-payloads.js +0 -66
  224. package/dist/worker-payloads.js.map +0 -1
  225. package/dist/worker-start-payload.js +0 -5
  226. package/dist/worker-start-payload.js.map +0 -1
  227. package/dist/workspace/manifest.js +0 -79
  228. package/dist/workspace/manifest.js.map +0 -1
  229. package/dist/workspace/node-run-root-git-ops.js +0 -96
  230. package/dist/workspace/node-run-root-git-ops.js.map +0 -1
  231. package/dist/workspace/resource-aggregator.js +0 -39
  232. package/dist/workspace/resource-aggregator.js.map +0 -1
  233. package/dist/workspace/run-root.js +0 -209
  234. package/dist/workspace/run-root.js.map +0 -1
  235. package/src/test-helpers/ipc-harness.ts +0 -19
@@ -7,11 +7,14 @@ export interface ComponentManifest {
7
7
  components: ComponentManifestEntry[];
8
8
  }
9
9
 
10
- export interface ComponentManifestEntry {
10
+ export interface ComponentCheckoutPlan {
11
11
  key: string;
12
12
  repoLocator: string;
13
13
  baseBranch: string;
14
14
  workBranch: string;
15
+ }
16
+
17
+ export interface ComponentManifestEntry extends ComponentCheckoutPlan {
15
18
  clonedAt: string;
16
19
  headSha: string;
17
20
  }
@@ -84,12 +87,7 @@ export function deserializeManifest(
84
87
 
85
88
  export function diffManifest(
86
89
  existing: ComponentManifest,
87
- serverProjects: Array<{
88
- key: string;
89
- repoLocator: string;
90
- baseBranch: string;
91
- workBranch: string;
92
- }>,
90
+ serverProjects: ComponentCheckoutPlan[],
93
91
  ): ManifestDiff {
94
92
  const stale: string[] = [];
95
93
  const missing: string[] = [];
@@ -10,49 +10,71 @@ import {
10
10
  import { tmpdir } from "node:os";
11
11
  import path from "node:path";
12
12
  import {
13
+ clearRepositoryGitHttpsHelpers,
13
14
  clearRepositoryGitSshWrappers,
14
15
  repositoryGitArgs,
15
16
  repositoryGitSubprocessEnv,
17
+ repositoryHttpsUrl,
18
+ setRepositoryGitHttpsHelper,
16
19
  setRepositoryGitSshWrapper,
17
20
  } from "@leitwerk-dev/process-sdk";
18
21
  import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
19
- import type { WorkerGitSshCredential } from "@leitwerk-dev/worker-protocol";
22
+ import type { WorkerRepositoryCredential } from "@leitwerk-dev/worker-protocol";
20
23
  import { execa } from "execa";
21
24
  import fg from "fast-glob";
22
25
  import type { RunRootGitOps } from "./run-root.js";
23
26
 
24
- function toAbsolute(targetPath: string): string {
25
- return path.resolve(targetPath);
26
- }
27
-
28
27
  export class NodeRunRootGitOps implements RunRootGitOps {
29
28
  #credentialDir: string | null = null;
30
29
 
31
- configureRepositoryCredentials(credentials: readonly WorkerGitSshCredential[]): void {
30
+ configureRepositoryCredentials(credentials: readonly WorkerRepositoryCredential[]): void {
32
31
  this.cleanupRepositoryCredentials();
33
32
  if (credentials.length === 0) return;
34
33
  const root = mkdtempSync(path.join(tmpdir(), "leitwerk-repo-auth-"));
35
34
  chmodSync(root, 0o700);
36
35
  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);
36
+ try {
37
+ for (const credential of credentials) {
38
+ const dir = path.join(root, encodeURIComponent(credential.projectKey));
39
+ mkdirSync(dir, { mode: 0o700 });
40
+ if (credential.kind === "git_https") {
41
+ const url = repositoryHttpsUrl(credential.repositoryUrl);
42
+ writeFileSync(
43
+ path.join(dir, "credential.json"),
44
+ JSON.stringify({
45
+ host: url.host,
46
+ path: url.pathname.slice(1),
47
+ username: credential.username,
48
+ password: credential.password,
49
+ }),
50
+ { mode: 0o600 },
51
+ );
52
+ const helper = path.join(dir, "credential.cjs");
53
+ writeFileSync(helper, HTTPS_CREDENTIAL_HELPER, { mode: 0o700 });
54
+ setRepositoryGitHttpsHelper(credential.projectKey, helper);
55
+ continue;
56
+ }
57
+ const key = path.join(dir, "identity");
58
+ const hosts = path.join(dir, "known_hosts");
59
+ const wrapper = path.join(dir, "ssh");
60
+ writeFileSync(key, `${credential.privateKey.trim()}\n`, { mode: 0o600 });
61
+ writeFileSync(hosts, `${credential.knownHosts.trim()}\n`, { mode: 0o600 });
62
+ writeFileSync(
63
+ wrapper,
64
+ `#!/bin/sh\nexec ssh -F /dev/null -o IdentitiesOnly=yes -o IdentityFile="${key}" -o UserKnownHostsFile="${hosts}" -o StrictHostKeyChecking=yes -o BatchMode=yes "$@"\n`,
65
+ { mode: 0o700 },
66
+ );
67
+ setRepositoryGitSshWrapper(credential.projectKey, wrapper);
68
+ }
69
+ } catch (error) {
70
+ this.cleanupRepositoryCredentials();
71
+ throw error;
51
72
  }
52
73
  }
53
74
 
54
75
  cleanupRepositoryCredentials(): void {
55
76
  clearRepositoryGitSshWrappers();
77
+ clearRepositoryGitHttpsHelpers();
56
78
  if (this.#credentialDir) rmSync(this.#credentialDir, { recursive: true, force: true });
57
79
  this.#credentialDir = null;
58
80
  }
@@ -63,7 +85,7 @@ export class NodeRunRootGitOps implements RunRootGitOps {
63
85
  projectKey?: string,
64
86
  ): Promise<string> {
65
87
  const result = await execa(resolveGitBinary(), repositoryGitArgs(args), {
66
- ...(cwd ? { cwd: toAbsolute(cwd) } : {}),
88
+ ...(cwd ? { cwd: path.resolve(cwd) } : {}),
67
89
  env: repositoryGitSubprocessEnv(projectKey ?? "", { GIT_TERMINAL_PROMPT: "0" }),
68
90
  extendEnv: false,
69
91
  stdin: "ignore",
@@ -72,13 +94,13 @@ export class NodeRunRootGitOps implements RunRootGitOps {
72
94
  }
73
95
 
74
96
  async writeFile(repoDir: string, filePath: string, content: string): Promise<void> {
75
- const absolutePath = toAbsolute(path.join(repoDir, filePath));
97
+ const absolutePath = path.resolve(path.join(repoDir, filePath));
76
98
  mkdirSync(path.dirname(absolutePath), { recursive: true });
77
99
  writeFileSync(absolutePath, content, "utf8");
78
100
  }
79
101
 
80
102
  async readFile(repoDir: string, filePath: string): Promise<string | null> {
81
- const absolutePath = toAbsolute(path.join(repoDir, filePath));
103
+ const absolutePath = path.resolve(path.join(repoDir, filePath));
82
104
  if (!existsSync(absolutePath)) {
83
105
  return null;
84
106
  }
@@ -86,7 +108,7 @@ export class NodeRunRootGitOps implements RunRootGitOps {
86
108
  }
87
109
 
88
110
  async clone(repoLocator: string, targetDir: string): Promise<void> {
89
- const absoluteTargetDir = toAbsolute(targetDir);
111
+ const absoluteTargetDir = path.resolve(targetDir);
90
112
  rmSync(absoluteTargetDir, { recursive: true, force: true });
91
113
  mkdirSync(path.dirname(absoluteTargetDir), { recursive: true });
92
114
  await this.runGit(
@@ -121,7 +143,7 @@ export class NodeRunRootGitOps implements RunRootGitOps {
121
143
  }
122
144
 
123
145
  async listFiles(repoDir: string, pattern: string): Promise<string[]> {
124
- const absoluteRoot = toAbsolute(repoDir);
146
+ const absoluteRoot = path.resolve(repoDir);
125
147
  if (!existsSync(absoluteRoot)) {
126
148
  return [];
127
149
  }
@@ -135,3 +157,21 @@ export class NodeRunRootGitOps implements RunRootGitOps {
135
157
  ).sort((a, b) => a.localeCompare(b));
136
158
  }
137
159
  }
160
+
161
+ // The secret is read only by this short-lived helper, never embedded in its source or argv.
162
+ const HTTPS_CREDENTIAL_HELPER = `const fs = require('node:fs');
163
+ if (process.argv[2] !== 'get') process.exit(0);
164
+ const input = fs.readFileSync(0, 'utf8');
165
+ const fields = Object.create(null);
166
+ for (const line of input.split('\\n')) {
167
+ const i = line.indexOf('=');
168
+ if (i < 0) continue;
169
+ const key = line.slice(0, i);
170
+ if (Object.hasOwn(fields, key)) process.exit(0);
171
+ fields[key] = line.slice(i + 1);
172
+ }
173
+ const c = JSON.parse(fs.readFileSync(require('node:path').join(__dirname, 'credential.json'), 'utf8'));
174
+ if (fields.protocol !== 'https' || fields.host !== c.host || fields.path !== c.path ||
175
+ (fields.username && fields.username !== c.username)) process.exit(0);
176
+ process.stdout.write('username=' + c.username + '\\npassword=' + c.password + '\\n\\n');
177
+ `;
@@ -21,23 +21,12 @@ export function aggregateAgentsMd(
21
21
  export function collectSkills(
22
22
  sources: Array<{ componentKey: string; skillId: string; content: string }>,
23
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
24
  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
- });
25
+ for (const { skillId, componentKey, content } of sources) {
26
+ const winner = out.get(skillId);
27
+ if (!winner || componentKey.localeCompare(winner.source) < 0) {
28
+ out.set(skillId, { content, source: componentKey });
29
+ }
41
30
  }
42
31
  return out;
43
32
  }
@@ -1,5 +1,7 @@
1
1
  import path from "node:path";
2
+ import { isPathInside } from "@leitwerk-dev/process-sdk";
2
3
  import {
4
+ type ComponentCheckoutPlan,
3
5
  type ComponentManifest,
4
6
  type ComponentManifestEntry,
5
7
  deserializeManifest,
@@ -22,7 +24,7 @@ export interface GitOps {
22
24
  export type RunRootGitOps = GitOps & {
23
25
  writeFile(repoDir: string, filePath: string, content: string): Promise<void>;
24
26
  configureRepositoryCredentials?(
25
- credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerGitSshCredential[],
27
+ credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerRepositoryCredential[],
26
28
  ): void;
27
29
  cleanupRepositoryCredentials?(): void;
28
30
  };
@@ -30,12 +32,7 @@ export type RunRootGitOps = GitOps & {
30
32
  export interface RunRootPlan {
31
33
  workspaceRoot: string;
32
34
  instanceId: string;
33
- components: Array<{
34
- key: string;
35
- repoLocator: string;
36
- baseBranch: string;
37
- workBranch: string;
38
- }>;
35
+ components: ComponentCheckoutPlan[];
39
36
  }
40
37
 
41
38
  export interface MaterializeResult {
@@ -52,6 +49,16 @@ export interface ValidationResult {
52
49
  existingManifest: ComponentManifest | null;
53
50
  }
54
51
 
52
+ export class RunRootPreparationError extends Error {
53
+ readonly errors: readonly string[];
54
+
55
+ constructor(errors: readonly string[]) {
56
+ super(`Workspace preparation failed: ${errors.join("; ")}`);
57
+ this.name = "RunRootPreparationError";
58
+ this.errors = [...errors];
59
+ }
60
+ }
61
+
55
62
  const MANIFEST_REL = path.join(".leitwerk", "components.json");
56
63
  const AGGREGATED_AGENTS_REL = "AGENTS.md";
57
64
  const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
@@ -59,17 +66,7 @@ const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
59
66
  const SKILL_GLOB = "**/.cursor/skills/**/SKILL.md";
60
67
  const AGENTS_GLOB = "**/AGENTS.md";
61
68
 
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 {
69
+ function componentDir(workspaceRoot: string, childPath: string): string {
73
70
  const root = path.resolve(workspaceRoot);
74
71
  const target = path.resolve(root, childPath);
75
72
  if (!isPathInside(root, target)) {
@@ -78,10 +75,6 @@ function safeJoinInsideWorkspace(workspaceRoot: string, childPath: string): stri
78
75
  return target;
79
76
  }
80
77
 
81
- function componentDir(workspaceRoot: string, key: string): string {
82
- return safeJoinInsideWorkspace(workspaceRoot, key);
83
- }
84
-
85
78
  function skillIdFromSkillPath(filePath: string): string | null {
86
79
  const normalized = filePath.replace(/\\/g, "/");
87
80
  const m = normalized.match(/\.cursor\/skills\/([^/]+)\/SKILL\.md$/);
@@ -91,12 +84,7 @@ function skillIdFromSkillPath(filePath: string): string | null {
91
84
  export function planRunRoot(
92
85
  workspaceRoot: string,
93
86
  instanceId: string,
94
- projectSnapshots: Array<{
95
- key: string;
96
- repoLocator: string;
97
- baseBranch: string;
98
- workBranch: string;
99
- }>,
87
+ projectSnapshots: ComponentCheckoutPlan[],
100
88
  ): RunRootPlan {
101
89
  return {
102
90
  workspaceRoot,
@@ -113,12 +101,10 @@ async function materializeComponentEntry(
113
101
  const dir = componentDir(plan.workspaceRoot, comp.key);
114
102
  await git.clone(comp.repoLocator, dir);
115
103
  await git.checkout(dir, comp.baseBranch);
116
- if (await git.branchExists(dir, comp.workBranch)) {
117
- await git.checkout(dir, comp.workBranch);
118
- } else {
104
+ if (!(await git.branchExists(dir, comp.workBranch))) {
119
105
  await git.createBranch(dir, comp.workBranch, comp.baseBranch);
120
- await git.checkout(dir, comp.workBranch);
121
106
  }
107
+ await git.checkout(dir, comp.workBranch);
122
108
  return {
123
109
  key: comp.key,
124
110
  repoLocator: comp.repoLocator,
@@ -232,7 +218,7 @@ async function finalizeRunRoot(
232
218
  git: RunRootGitOps,
233
219
  entries: ComponentManifestEntry[],
234
220
  errors: string[],
235
- ) {
221
+ ): Promise<MaterializeResult> {
236
222
  const manifest: ComponentManifest = {
237
223
  version: 1,
238
224
  instanceId: plan.instanceId,
@@ -249,7 +235,20 @@ async function finalizeRunRoot(
249
235
  errors.push(`write: ${e instanceof Error ? e.message : String(e)}`);
250
236
  }
251
237
 
252
- return { manifest, aggregatedAgentsMdSources, loadedSkills };
238
+ const expectedKeys = new Set(plan.components.map((component) => component.key));
239
+ const hasAllExpectedEntries =
240
+ entries.length === expectedKeys.size &&
241
+ [...expectedKeys].every((key) => entries.some((entry) => entry.key === key));
242
+ if (errors.length > 0 || !hasAllExpectedEntries) {
243
+ throw new RunRootPreparationError(errors);
244
+ }
245
+ return {
246
+ ok: true,
247
+ manifest,
248
+ aggregatedAgentsMdSources,
249
+ loadedSkills,
250
+ errors,
251
+ };
253
252
  }
254
253
 
255
254
  export async function materializeRunRoot(
@@ -258,48 +257,17 @@ export async function materializeRunRoot(
258
257
  ): Promise<MaterializeResult> {
259
258
  const errors: string[] = [];
260
259
  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
- };
260
+ return finalizeRunRoot(plan, git, entries, errors);
276
261
  }
277
262
 
278
263
  export async function validateRunRoot(
279
264
  workspaceRoot: string,
280
- serverProjects: Array<{
281
- key: string;
282
- repoLocator: string;
283
- baseBranch: string;
284
- workBranch: string;
285
- }>,
265
+ serverProjects: ComponentCheckoutPlan[],
286
266
  git: GitOps,
287
267
  ): Promise<ValidationResult> {
288
268
  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) {
269
+ const parsed = raw === null ? null : deserializeManifest(raw);
270
+ if (!parsed?.ok) {
303
271
  return {
304
272
  valid: false,
305
273
  diff: {
@@ -338,24 +306,5 @@ export async function repairRunRoot(
338
306
  null,
339
307
  );
340
308
 
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
- };
309
+ return finalizeRunRoot(plan, git, manifestEntries, errors);
361
310
  }
@@ -1,58 +0,0 @@
1
- import { classifyRuntimeError, TurnExecutionError } from "./turn-execution-error.js";
2
- import { TurnExecutionFailure } from "./turn-execution-result.js";
3
- import { resolveTurnRecordIdForExecution } from "./turn-record-id.js";
4
- import { resolveRootEntryIdFromHandle } from "./turn-tree-strategy.js";
5
- import { readProcessSemanticEntryRefs } from "./worker-payloads.js";
6
- export function createAutomaticTurnExecutor(input) {
7
- const turnType = "automatic";
8
- const pathType = "primary";
9
- const turnRecordId = resolveTurnRecordIdForExecution(input.processSnapshot, input.currentTurnId, input.acceptedTurnRecordId);
10
- let result = null;
11
- const failure = (error) => {
12
- const classified = classifyRuntimeError(error);
13
- const turnError = error instanceof TurnExecutionError
14
- ? error
15
- : new TurnExecutionError(input.currentTurnId, classified.errorClass, classified.message);
16
- return new TurnExecutionFailure({
17
- turnRecordId,
18
- turnId: input.currentTurnId,
19
- turnType,
20
- pathType,
21
- failure: turnError,
22
- });
23
- };
24
- return {
25
- turnRecordId,
26
- get result() {
27
- return result;
28
- },
29
- async complete(completeInput) {
30
- if (result) {
31
- throw new TurnExecutionError(input.currentTurnId, "protocol_error", `Automatic turn '${input.currentTurnId}' completed more than once`);
32
- }
33
- result = {
34
- turnId: input.currentTurnId,
35
- outcome: completeInput.outcome,
36
- params: completeInput.params ?? {},
37
- meta: {
38
- turnRecordId,
39
- turnType,
40
- pathType,
41
- forkPiEntryId: null,
42
- resultPiEntryId: null,
43
- turnResultMarkdown: completeInput.markdown ?? null,
44
- rootEntryId: input.piHandle
45
- ? resolveRootEntryIdFromHandle(input.piHandle)
46
- : (readProcessSemanticEntryRefs(input.state)?.rootEntry?.entryId ?? null),
47
- },
48
- };
49
- },
50
- failure,
51
- assertCompletedUnlessParked(parked) {
52
- if (!result && !parked) {
53
- throw failure(new TurnExecutionError(input.currentTurnId, "protocol_error", `Automatic turn '${input.currentTurnId}' returned without calling run.complete()`));
54
- }
55
- },
56
- };
57
- }
58
- //# sourceMappingURL=automatic-turn-executor.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"automatic-turn-executor.js","s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