@leitwerk-dev/worker 0.1.9 → 0.2.1

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 (240) 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-O6332GRG.js → chunk-2LLFMCR6.js} +1511 -1071
  4. package/dist/chunk-2LLFMCR6.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 +84 -0
  14. package/dist/development-tool-environment.d.ts.map +1 -0
  15. package/dist/diagnostics.d.ts +4 -0
  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 +19 -0
  20. package/dist/entry-runtime.d.ts.map +1 -1
  21. package/dist/index.d.ts +3 -5
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +10 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/input-consumer.d.ts +7 -11
  26. package/dist/input-consumer.d.ts.map +1 -1
  27. package/dist/ipc.d.ts +10 -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-agent-dir.d.ts +8 -1
  31. package/dist/managed-pi-agent-dir.d.ts.map +1 -1
  32. package/dist/managed-pi-bootstrap.d.ts +1 -0
  33. package/dist/managed-pi-bootstrap.d.ts.map +1 -1
  34. package/dist/missing-tool-recovery-runner.d.ts +4 -3
  35. package/dist/missing-tool-recovery-runner.d.ts.map +1 -1
  36. package/dist/pi-adapter.d.ts +88 -1
  37. package/dist/pi-adapter.d.ts.map +1 -1
  38. package/dist/pi-event-reporter.d.ts.map +1 -1
  39. package/dist/pi-resource-bundle.d.ts +17 -1
  40. package/dist/pi-resource-bundle.d.ts.map +1 -1
  41. package/dist/pre-turn-targeted-inputs.d.ts +8 -7
  42. package/dist/pre-turn-targeted-inputs.d.ts.map +1 -1
  43. package/dist/prompt-guards.d.ts +6 -0
  44. package/dist/prompt-guards.d.ts.map +1 -1
  45. package/dist/repository-bash-tool.d.ts +4 -0
  46. package/dist/repository-bash-tool.d.ts.map +1 -0
  47. package/dist/resource-loader-factory.d.ts +1 -2
  48. package/dist/resource-loader-factory.d.ts.map +1 -1
  49. package/dist/result-image-upload.d.ts.map +1 -1
  50. package/dist/runtime/adapters.d.ts +38 -0
  51. package/dist/runtime/adapters.d.ts.map +1 -1
  52. package/dist/runtime/bootstrap-session.d.ts +9 -5
  53. package/dist/runtime/bootstrap-session.d.ts.map +1 -1
  54. package/dist/runtime/bootstrap.d.ts +38 -18
  55. package/dist/runtime/bootstrap.d.ts.map +1 -1
  56. package/dist/runtime/failure-policy.d.ts +1 -0
  57. package/dist/runtime/failure-policy.d.ts.map +1 -1
  58. package/dist/runtime/index.d.ts +4 -1
  59. package/dist/runtime/index.d.ts.map +1 -1
  60. package/dist/runtime/lifecycle-reducer.d.ts.map +1 -1
  61. package/dist/runtime/llm-turn-execution.d.ts +3 -8
  62. package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
  63. package/dist/runtime/turn-execution.d.ts +2 -9
  64. package/dist/runtime/turn-execution.d.ts.map +1 -1
  65. package/dist/session-snapshot-exchange.d.ts +10 -4
  66. package/dist/session-snapshot-exchange.d.ts.map +1 -1
  67. package/dist/turn-continuation.d.ts.map +1 -1
  68. package/dist/turn-execution-error.d.ts +1 -1
  69. package/dist/turn-execution-error.d.ts.map +1 -1
  70. package/dist/turn-execution-result.d.ts +1 -0
  71. package/dist/turn-execution-result.d.ts.map +1 -1
  72. package/dist/turn-outcome-tool-session.d.ts +0 -2
  73. package/dist/turn-outcome-tool-session.d.ts.map +1 -1
  74. package/dist/turn-result-markdown.d.ts +2 -1
  75. package/dist/turn-result-markdown.d.ts.map +1 -1
  76. package/dist/turn-tree-strategy.d.ts +0 -1
  77. package/dist/turn-tree-strategy.d.ts.map +1 -1
  78. package/dist/worker-entry.js +16 -4
  79. package/dist/worker-entry.js.map +1 -1
  80. package/dist/worker-ipc-reporter.d.ts.map +1 -1
  81. package/dist/workspace/manifest.d.ts +4 -7
  82. package/dist/workspace/manifest.d.ts.map +1 -1
  83. package/dist/workspace/node-run-root-git-ops.d.ts +2 -2
  84. package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -1
  85. package/dist/workspace/resource-aggregator.d.ts.map +1 -1
  86. package/dist/workspace/run-root.d.ts +21 -20
  87. package/dist/workspace/run-root.d.ts.map +1 -1
  88. package/package.json +8 -8
  89. package/src/automatic-turn-executor.ts +2 -0
  90. package/src/connection-diagnostics.ts +27 -0
  91. package/src/container-entry-main.ts +24 -0
  92. package/src/container-entry.ts +246 -0
  93. package/src/development-tool-environment.ts +490 -0
  94. package/src/diagnostics.ts +1 -0
  95. package/src/docker-registry-credentials.ts +65 -0
  96. package/src/entry-runtime.ts +29 -5
  97. package/src/index.ts +1 -39
  98. package/src/input-consumer.ts +8 -29
  99. package/src/ipc.ts +50 -2
  100. package/src/logical-prompt-runner.ts +23 -29
  101. package/src/managed-pi-agent-dir.ts +8 -1
  102. package/src/managed-pi-bootstrap.ts +4 -3
  103. package/src/missing-tool-recovery-runner.ts +4 -18
  104. package/src/pi-adapter.ts +125 -56
  105. package/src/pi-event-reporter.ts +16 -38
  106. package/src/pi-resource-bundle.ts +77 -3
  107. package/src/pre-turn-targeted-inputs.ts +9 -11
  108. package/src/prompt-guards.ts +50 -75
  109. package/src/repository-bash-tool.ts +15 -0
  110. package/src/resource-loader-factory.ts +1 -18
  111. package/src/result-image-upload.ts +16 -27
  112. package/src/runtime/adapters.ts +46 -4
  113. package/src/runtime/bootstrap-session.ts +70 -39
  114. package/src/runtime/bootstrap.ts +184 -101
  115. package/src/runtime/failure-policy.ts +36 -42
  116. package/src/runtime/index.ts +47 -15
  117. package/src/runtime/lifecycle-reducer.ts +71 -173
  118. package/src/runtime/llm-turn-execution.ts +13 -30
  119. package/src/runtime/turn-execution.ts +78 -31
  120. package/src/session-snapshot-exchange.ts +46 -68
  121. package/src/turn-continuation.ts +9 -17
  122. package/src/turn-execution-error.ts +3 -11
  123. package/src/turn-execution-result.ts +1 -0
  124. package/src/turn-outcome-tool-session.ts +25 -21
  125. package/src/turn-result-markdown.ts +31 -34
  126. package/src/turn-tree-strategy.ts +6 -19
  127. package/src/worker-entry.ts +8 -0
  128. package/src/worker-ipc-reporter.ts +8 -16
  129. package/src/workspace/manifest.ts +5 -7
  130. package/src/workspace/node-run-root-git-ops.ts +65 -25
  131. package/src/workspace/resource-aggregator.ts +5 -16
  132. package/src/workspace/run-root.ts +50 -89
  133. package/dist/automatic-turn-executor.js +0 -58
  134. package/dist/automatic-turn-executor.js.map +0 -1
  135. package/dist/chunk-O6332GRG.js.map +0 -1
  136. package/dist/diagnostics.js +0 -51
  137. package/dist/diagnostics.js.map +0 -1
  138. package/dist/entry-runtime.js +0 -160
  139. package/dist/entry-runtime.js.map +0 -1
  140. package/dist/event-translator.js +0 -12
  141. package/dist/event-translator.js.map +0 -1
  142. package/dist/extension-entry-roots.js +0 -41
  143. package/dist/extension-entry-roots.js.map +0 -1
  144. package/dist/identified-pi-entry.js +0 -125
  145. package/dist/identified-pi-entry.js.map +0 -1
  146. package/dist/input-consumer.js +0 -34
  147. package/dist/input-consumer.js.map +0 -1
  148. package/dist/integration-tool-bridge.js +0 -53
  149. package/dist/integration-tool-bridge.js.map +0 -1
  150. package/dist/ipc.js +0 -192
  151. package/dist/ipc.js.map +0 -1
  152. package/dist/logical-prompt-runner.js +0 -225
  153. package/dist/logical-prompt-runner.js.map +0 -1
  154. package/dist/managed-pi-agent-dir.js +0 -66
  155. package/dist/managed-pi-agent-dir.js.map +0 -1
  156. package/dist/managed-pi-bootstrap.js +0 -135
  157. package/dist/managed-pi-bootstrap.js.map +0 -1
  158. package/dist/missing-tool-recovery-runner.js +0 -73
  159. package/dist/missing-tool-recovery-runner.js.map +0 -1
  160. package/dist/pi-adapter.js +0 -1648
  161. package/dist/pi-adapter.js.map +0 -1
  162. package/dist/pi-branch-guard.js +0 -75
  163. package/dist/pi-branch-guard.js.map +0 -1
  164. package/dist/pi-event-reporter.js +0 -136
  165. package/dist/pi-event-reporter.js.map +0 -1
  166. package/dist/pi-resource-bundle.js +0 -58
  167. package/dist/pi-resource-bundle.js.map +0 -1
  168. package/dist/pre-turn-targeted-inputs.js +0 -101
  169. package/dist/pre-turn-targeted-inputs.js.map +0 -1
  170. package/dist/prompt-guards.js +0 -318
  171. package/dist/prompt-guards.js.map +0 -1
  172. package/dist/question-bridge.js +0 -24
  173. package/dist/question-bridge.js.map +0 -1
  174. package/dist/question-tool.js +0 -54
  175. package/dist/question-tool.js.map +0 -1
  176. package/dist/replaying-request-bridge.js +0 -71
  177. package/dist/replaying-request-bridge.js.map +0 -1
  178. package/dist/resource-loader-factory.js +0 -87
  179. package/dist/resource-loader-factory.js.map +0 -1
  180. package/dist/result-image-upload.js +0 -183
  181. package/dist/result-image-upload.js.map +0 -1
  182. package/dist/runtime/adapters.js +0 -15
  183. package/dist/runtime/adapters.js.map +0 -1
  184. package/dist/runtime/bootstrap-session.js +0 -199
  185. package/dist/runtime/bootstrap-session.js.map +0 -1
  186. package/dist/runtime/bootstrap.js +0 -440
  187. package/dist/runtime/bootstrap.js.map +0 -1
  188. package/dist/runtime/failure-policy.js +0 -182
  189. package/dist/runtime/failure-policy.js.map +0 -1
  190. package/dist/runtime/index.js +0 -339
  191. package/dist/runtime/index.js.map +0 -1
  192. package/dist/runtime/lifecycle-reducer.js +0 -1142
  193. package/dist/runtime/lifecycle-reducer.js.map +0 -1
  194. package/dist/runtime/llm-turn-execution.js +0 -494
  195. package/dist/runtime/llm-turn-execution.js.map +0 -1
  196. package/dist/runtime/settings.js +0 -24
  197. package/dist/runtime/settings.js.map +0 -1
  198. package/dist/runtime/snapshot-policy.js +0 -16
  199. package/dist/runtime/snapshot-policy.js.map +0 -1
  200. package/dist/runtime/turn-execution.js +0 -151
  201. package/dist/runtime/turn-execution.js.map +0 -1
  202. package/dist/session-snapshot-exchange.js +0 -102
  203. package/dist/session-snapshot-exchange.js.map +0 -1
  204. package/dist/test-helpers/fake-websocket.js +0 -40
  205. package/dist/test-helpers/fake-websocket.js.map +0 -1
  206. package/dist/test-helpers/ipc-harness.d.ts +0 -11
  207. package/dist/test-helpers/ipc-harness.d.ts.map +0 -1
  208. package/dist/test-helpers/ipc-harness.js +0 -17
  209. package/dist/test-helpers/ipc-harness.js.map +0 -1
  210. package/dist/turn-continuation.js +0 -80
  211. package/dist/turn-continuation.js.map +0 -1
  212. package/dist/turn-execution-error.js +0 -64
  213. package/dist/turn-execution-error.js.map +0 -1
  214. package/dist/turn-execution-result.js +0 -21
  215. package/dist/turn-execution-result.js.map +0 -1
  216. package/dist/turn-outcome-tool-session.js +0 -210
  217. package/dist/turn-outcome-tool-session.js.map +0 -1
  218. package/dist/turn-record-id.js +0 -10
  219. package/dist/turn-record-id.js.map +0 -1
  220. package/dist/turn-result-markdown.js +0 -263
  221. package/dist/turn-result-markdown.js.map +0 -1
  222. package/dist/turn-tree-strategy.js +0 -80
  223. package/dist/turn-tree-strategy.js.map +0 -1
  224. package/dist/worker-http.js +0 -9
  225. package/dist/worker-http.js.map +0 -1
  226. package/dist/worker-ipc-reporter.js +0 -58
  227. package/dist/worker-ipc-reporter.js.map +0 -1
  228. package/dist/worker-payloads.js +0 -66
  229. package/dist/worker-payloads.js.map +0 -1
  230. package/dist/worker-start-payload.js +0 -5
  231. package/dist/worker-start-payload.js.map +0 -1
  232. package/dist/workspace/manifest.js +0 -79
  233. package/dist/workspace/manifest.js.map +0 -1
  234. package/dist/workspace/node-run-root-git-ops.js +0 -96
  235. package/dist/workspace/node-run-root-git-ops.js.map +0 -1
  236. package/dist/workspace/resource-aggregator.js +0 -39
  237. package/dist/workspace/resource-aggregator.js.map +0 -1
  238. package/dist/workspace/run-root.js +0 -209
  239. package/dist/workspace/run-root.js.map +0 -1
  240. package/src/test-helpers/ipc-harness.ts +0 -19
@@ -1,6 +1,14 @@
1
1
  import { createWorkerEntryRuntime } from "./entry-runtime.js";
2
2
 
3
3
  const runtime = createWorkerEntryRuntime();
4
+ for (const signal of ["SIGTERM", "SIGINT"] as const) {
5
+ process.once(signal, () => {
6
+ void runtime.stop(signal).then(
7
+ () => process.exit(0),
8
+ () => process.exit(1),
9
+ );
10
+ });
11
+ }
4
12
 
5
13
  void runtime.start().catch((e) => {
6
14
  console.error(e);
@@ -77,24 +77,16 @@ export function createWorkerIpcReporter(options: {
77
77
  options.emitExtensionEvent?.("worker.event", payload);
78
78
  return payload;
79
79
  };
80
+ const diagnosticReporter =
81
+ (eventType: "worker.trace" | "worker.error") =>
82
+ (input: WorkerDiagnosticPayload, selectedTurnId: string | null = null): void => {
83
+ const payload = workerEvent(eventType, buildWorkerDiagnosticPayload(input), selectedTurnId);
84
+ options.emitExtensionEvent?.(eventType, payload);
85
+ };
80
86
  return {
81
87
  project,
82
88
  workerEvent,
83
- workerTrace(input, selectedTurnId = null) {
84
- const payload = workerEvent(
85
- "worker.trace",
86
- buildWorkerDiagnosticPayload(input),
87
- selectedTurnId,
88
- );
89
- options.emitExtensionEvent?.("worker.trace", payload);
90
- },
91
- workerError(input, selectedTurnId = null) {
92
- const payload = workerEvent(
93
- "worker.error",
94
- buildWorkerDiagnosticPayload(input),
95
- selectedTurnId,
96
- );
97
- options.emitExtensionEvent?.("worker.error", payload);
98
- },
89
+ workerTrace: diagnosticReporter("worker.trace"),
90
+ workerError: diagnosticReporter("worker.error"),
99
91
  };
100
92
  }
@@ -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,
@@ -9,33 +11,40 @@ import {
9
11
  } from "./manifest.js";
10
12
  import { aggregateAgentsMd, collectSkills } from "./resource-aggregator.js";
11
13
 
14
+ /** @internal */
12
15
  export interface GitOps {
16
+ /** @internal */
13
17
  clone(repoLocator: string, targetDir: string): Promise<void>;
18
+ /** @internal */
14
19
  checkout(repoDir: string, branch: string): Promise<void>;
20
+ /** @internal */
15
21
  createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void>;
22
+ /** @internal */
16
23
  branchExists(repoDir: string, branchName: string): Promise<boolean>;
24
+ /** @internal */
17
25
  getHeadSha(repoDir: string): Promise<string>;
26
+ /** @internal */
18
27
  readFile(repoDir: string, filePath: string): Promise<string | null>;
28
+ /** @internal */
19
29
  listFiles(repoDir: string, pattern: string): Promise<string[]>;
20
30
  }
21
31
 
32
+ /** @internal */
22
33
  export type RunRootGitOps = GitOps & {
34
+ /** @internal */
23
35
  writeFile(repoDir: string, filePath: string, content: string): Promise<void>;
36
+ /** @internal */
24
37
  configureRepositoryCredentials?(
25
- credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerGitSshCredential[],
38
+ credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerRepositoryCredential[],
26
39
  ): void;
40
+ /** @internal */
27
41
  cleanupRepositoryCredentials?(): void;
28
42
  };
29
43
 
30
44
  export interface RunRootPlan {
31
45
  workspaceRoot: string;
32
46
  instanceId: string;
33
- components: Array<{
34
- key: string;
35
- repoLocator: string;
36
- baseBranch: string;
37
- workBranch: string;
38
- }>;
47
+ components: ComponentCheckoutPlan[];
39
48
  }
40
49
 
41
50
  export interface MaterializeResult {
@@ -52,6 +61,16 @@ export interface ValidationResult {
52
61
  existingManifest: ComponentManifest | null;
53
62
  }
54
63
 
64
+ export class RunRootPreparationError extends Error {
65
+ readonly errors: readonly string[];
66
+
67
+ constructor(errors: readonly string[]) {
68
+ super(`Workspace preparation failed: ${errors.join("; ")}`);
69
+ this.name = "RunRootPreparationError";
70
+ this.errors = [...errors];
71
+ }
72
+ }
73
+
55
74
  const MANIFEST_REL = path.join(".leitwerk", "components.json");
56
75
  const AGGREGATED_AGENTS_REL = "AGENTS.md";
57
76
  const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
@@ -59,17 +78,7 @@ const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
59
78
  const SKILL_GLOB = "**/.cursor/skills/**/SKILL.md";
60
79
  const AGENTS_GLOB = "**/AGENTS.md";
61
80
 
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 {
81
+ function componentDir(workspaceRoot: string, childPath: string): string {
73
82
  const root = path.resolve(workspaceRoot);
74
83
  const target = path.resolve(root, childPath);
75
84
  if (!isPathInside(root, target)) {
@@ -78,10 +87,6 @@ function safeJoinInsideWorkspace(workspaceRoot: string, childPath: string): stri
78
87
  return target;
79
88
  }
80
89
 
81
- function componentDir(workspaceRoot: string, key: string): string {
82
- return safeJoinInsideWorkspace(workspaceRoot, key);
83
- }
84
-
85
90
  function skillIdFromSkillPath(filePath: string): string | null {
86
91
  const normalized = filePath.replace(/\\/g, "/");
87
92
  const m = normalized.match(/\.cursor\/skills\/([^/]+)\/SKILL\.md$/);
@@ -91,12 +96,7 @@ function skillIdFromSkillPath(filePath: string): string | null {
91
96
  export function planRunRoot(
92
97
  workspaceRoot: string,
93
98
  instanceId: string,
94
- projectSnapshots: Array<{
95
- key: string;
96
- repoLocator: string;
97
- baseBranch: string;
98
- workBranch: string;
99
- }>,
99
+ projectSnapshots: ComponentCheckoutPlan[],
100
100
  ): RunRootPlan {
101
101
  return {
102
102
  workspaceRoot,
@@ -113,12 +113,10 @@ async function materializeComponentEntry(
113
113
  const dir = componentDir(plan.workspaceRoot, comp.key);
114
114
  await git.clone(comp.repoLocator, dir);
115
115
  await git.checkout(dir, comp.baseBranch);
116
- if (await git.branchExists(dir, comp.workBranch)) {
117
- await git.checkout(dir, comp.workBranch);
118
- } else {
116
+ if (!(await git.branchExists(dir, comp.workBranch))) {
119
117
  await git.createBranch(dir, comp.workBranch, comp.baseBranch);
120
- await git.checkout(dir, comp.workBranch);
121
118
  }
119
+ await git.checkout(dir, comp.workBranch);
122
120
  return {
123
121
  key: comp.key,
124
122
  repoLocator: comp.repoLocator,
@@ -232,7 +230,7 @@ async function finalizeRunRoot(
232
230
  git: RunRootGitOps,
233
231
  entries: ComponentManifestEntry[],
234
232
  errors: string[],
235
- ) {
233
+ ): Promise<MaterializeResult> {
236
234
  const manifest: ComponentManifest = {
237
235
  version: 1,
238
236
  instanceId: plan.instanceId,
@@ -249,7 +247,20 @@ async function finalizeRunRoot(
249
247
  errors.push(`write: ${e instanceof Error ? e.message : String(e)}`);
250
248
  }
251
249
 
252
- return { manifest, aggregatedAgentsMdSources, loadedSkills };
250
+ const expectedKeys = new Set(plan.components.map((component) => component.key));
251
+ const hasAllExpectedEntries =
252
+ entries.length === expectedKeys.size &&
253
+ [...expectedKeys].every((key) => entries.some((entry) => entry.key === key));
254
+ if (errors.length > 0 || !hasAllExpectedEntries) {
255
+ throw new RunRootPreparationError(errors);
256
+ }
257
+ return {
258
+ ok: true,
259
+ manifest,
260
+ aggregatedAgentsMdSources,
261
+ loadedSkills,
262
+ errors,
263
+ };
253
264
  }
254
265
 
255
266
  export async function materializeRunRoot(
@@ -258,48 +269,17 @@ export async function materializeRunRoot(
258
269
  ): Promise<MaterializeResult> {
259
270
  const errors: string[] = [];
260
271
  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
- };
272
+ return finalizeRunRoot(plan, git, entries, errors);
276
273
  }
277
274
 
278
275
  export async function validateRunRoot(
279
276
  workspaceRoot: string,
280
- serverProjects: Array<{
281
- key: string;
282
- repoLocator: string;
283
- baseBranch: string;
284
- workBranch: string;
285
- }>,
277
+ serverProjects: ComponentCheckoutPlan[],
286
278
  git: GitOps,
287
279
  ): Promise<ValidationResult> {
288
280
  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) {
281
+ const parsed = raw === null ? null : deserializeManifest(raw);
282
+ if (!parsed?.ok) {
303
283
  return {
304
284
  valid: false,
305
285
  diff: {
@@ -338,24 +318,5 @@ export async function repairRunRoot(
338
318
  null,
339
319
  );
340
320
 
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
- };
321
+ return finalizeRunRoot(plan, git, manifestEntries, errors);
361
322
  }
@@ -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","sourceRoot":"","sources":["../src/automatic-turn-executor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACrF,OAAO,EAAE,oBAAoB,EAA0B,MAAM,4BAA4B,CAAC;AAC1F,OAAO,EAAE,+BAA+B,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,4BAA4B,EAAE,MAAM,sBAAsB,CAAC;AAqBpE,MAAM,UAAU,2BAA2B,CAAC,KAM3C;IACA,MAAM,QAAQ,GAAoB,WAAW,CAAC;IAC9C,MAAM,QAAQ,GAA8B,SAAS,CAAC;IACtD,MAAM,YAAY,GAAG,+BAA+B,CACnD,KAAK,CAAC,eAAe,EACrB,KAAK,CAAC,aAAa,EACnB,KAAK,CAAC,oBAAoB,CAC1B,CAAC;IACF,IAAI,MAAM,GAA+B,IAAI,CAAC;IAE9C,MAAM,OAAO,GAAG,CAAC,KAAc,EAAwB,EAAE;QACxD,MAAM,UAAU,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC;QAC/C,MAAM,SAAS,GACd,KAAK,YAAY,kBAAkB;YAClC,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,IAAI,kBAAkB,CAAC,KAAK,CAAC,aAAa,EAAE,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;QAC3F,OAAO,IAAI,oBAAoB,CAAC;YAC/B,YAAY;YACZ,MAAM,EAAE,KAAK,CAAC,aAAa;YAC3B,QAAQ;YACR,QAAQ;YACR,OAAO,EAAE,SAAS;SAClB,CAAC,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO;QACN,YAAY;QACZ,IAAI,MAAM;YACT,OAAO,MAAM,CAAC;QACf,CAAC;QACD,KAAK,CAAC,QAAQ,CAAC,aAAa;YAC3B,IAAI,MAAM,EAAE,CAAC;gBACZ,MAAM,IAAI,kBAAkB,CAC3B,KAAK,CAAC,aAAa,EACnB,gBAAgB,EAChB,mBAAmB,KAAK,CAAC,aAAa,4BAA4B,CAClE,CAAC;YACH,CAAC;YACD,MAAM,GAAG;gBACR,MAAM,EAAE,KAAK,CAAC,aAAa;gBAC3B,OAAO,EAAE,aAAa,CAAC,OAAO;gBAC9B,MAAM,EAAE,aAAa,CAAC,MAAM,IAAI,EAAE;gBAClC,IAAI,EAAE;oBACL,YAAY;oBACZ,QAAQ;oBACR,QAAQ;oBACR,aAAa,EAAE,IAAI;oBACnB,eAAe,EAAE,IAAI;oBACrB,kBAAkB,EAAE,aAAa,CAAC,QAAQ,IAAI,IAAI;oBAClD,WAAW,EAAE,KAAK,CAAC,QAAQ;wBAC1B,CAAC,CAAC,4BAA4B,CAAC,KAAK,CAAC,QAAQ,CAAC;wBAC9C,CAAC,CAAC,CAAC,4BAA4B,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,OAAO,IAAI,IAAI,CAAC;iBAC1E;aACD,CAAC;QACH,CAAC;QACD,OAAO;QACP,2BAA2B,CAAC,MAAM;YACjC,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;gBACxB,MAAM,OAAO,CACZ,IAAI,kBAAkB,CACrB,KAAK,CAAC,aAAa,EACnB,gBAAgB,EAChB,mBAAmB,KAAK,CAAC,aAAa,2CAA2C,CACjF,CACD,CAAC;YACH,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC"}