@loucompanion/forge-bridge 0.1.3-dev.0b2f81d660c0 → 0.1.3-dev.0d26c10244e1

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 (148) hide show
  1. package/README.md +79 -0
  2. package/dist/bridge/bridge.base.d.ts +8 -1
  3. package/dist/bridge/bridge.service.d.ts +77 -3
  4. package/dist/bridge/bridge.service.js +1113 -34
  5. package/dist/bridge/bridge.types.d.ts +168 -1
  6. package/dist/cleanup/cleanup.service.d.ts +14 -2
  7. package/dist/cleanup/cleanup.service.js +469 -3
  8. package/dist/cleanup/cleanup.types.d.ts +10 -0
  9. package/dist/cleanup/index.d.ts +1 -1
  10. package/dist/cleanup/index.js +1 -1
  11. package/dist/cleanup/internals.d.ts +2 -2
  12. package/dist/cleanup/internals.js +1 -1
  13. package/dist/cli/cli.help.js +47 -1
  14. package/dist/cli/cli.service.d.ts +42 -2
  15. package/dist/cli/cli.service.js +357 -26
  16. package/dist/cli/local-config.d.ts +4 -0
  17. package/dist/cli/local-config.js +41 -1
  18. package/dist/runtime/aspnet-runtime-prebuild.d.ts +35 -0
  19. package/dist/runtime/aspnet-runtime-prebuild.js +168 -0
  20. package/dist/runtime/attempt-node-runtime-launcher.d.ts +17 -0
  21. package/dist/runtime/attempt-node-runtime-launcher.js +105 -0
  22. package/dist/runtime/attempt-process-supervisor-registry.d.ts +20 -0
  23. package/dist/runtime/attempt-process-supervisor-registry.js +64 -0
  24. package/dist/runtime/attempt-process-supervisor.d.ts +86 -0
  25. package/dist/runtime/attempt-process-supervisor.js +350 -0
  26. package/dist/runtime/generated-output-reset.d.ts +10 -0
  27. package/dist/runtime/generated-output-reset.js +103 -0
  28. package/dist/runtime/host-listener-inspector.d.ts +46 -0
  29. package/dist/runtime/host-listener-inspector.js +349 -0
  30. package/dist/runtime/host-port-broker.d.ts +76 -0
  31. package/dist/runtime/host-port-broker.js +333 -0
  32. package/dist/runtime/host-runtime-contract.d.ts +47 -0
  33. package/dist/runtime/host-runtime-contract.js +700 -0
  34. package/dist/runtime/host-runtime-lease.d.ts +179 -0
  35. package/dist/runtime/host-runtime-lease.js +1990 -0
  36. package/dist/runtime/host-runtime-promotion.d.ts +23 -0
  37. package/dist/runtime/host-runtime-promotion.js +159 -0
  38. package/dist/runtime/node-host-runtime-adapter.d.ts +54 -0
  39. package/dist/runtime/node-host-runtime-adapter.js +284 -0
  40. package/dist/runtime/playwright-host-runtime-adapter.d.ts +20 -0
  41. package/dist/runtime/playwright-host-runtime-adapter.js +220 -0
  42. package/dist/runtime/runtime-isolation-lease.d.ts +67 -0
  43. package/dist/runtime/runtime-isolation-lease.js +158 -0
  44. package/dist/runtime/workspace-resource-estimator.d.ts +16 -0
  45. package/dist/runtime/workspace-resource-estimator.js +165 -0
  46. package/dist/sandbox/command-runner.d.ts +4 -0
  47. package/dist/sandbox/command-runner.js +47 -0
  48. package/dist/sandbox/contract.d.ts +5 -0
  49. package/dist/sandbox/contract.js +276 -0
  50. package/dist/sandbox/event-budget.d.ts +11 -0
  51. package/dist/sandbox/event-budget.js +27 -0
  52. package/dist/sandbox/image-policy.d.ts +35 -0
  53. package/dist/sandbox/image-policy.js +247 -0
  54. package/dist/sandbox/index.d.ts +5 -0
  55. package/dist/sandbox/index.js +5 -0
  56. package/dist/sandbox/lease-watchdog.d.ts +18 -0
  57. package/dist/sandbox/lease-watchdog.js +67 -0
  58. package/dist/sandbox/preflight.d.ts +55 -0
  59. package/dist/sandbox/preflight.js +343 -0
  60. package/dist/sandbox/process.d.ts +20 -0
  61. package/dist/sandbox/process.js +209 -0
  62. package/dist/sandbox/provider-auth/codex.d.ts +26 -0
  63. package/dist/sandbox/provider-auth/codex.js +212 -0
  64. package/dist/sandbox/repository-lock.d.ts +5 -0
  65. package/dist/sandbox/repository-lock.js +119 -0
  66. package/dist/sandbox/runtime.d.ts +297 -0
  67. package/dist/sandbox/runtime.js +2338 -0
  68. package/dist/sandbox/sandbox.types.d.ts +86 -0
  69. package/dist/sandbox/sandbox.types.js +1 -0
  70. package/dist/sandbox/smoke.d.ts +76 -0
  71. package/dist/sandbox/smoke.js +380 -0
  72. package/dist/sandbox/worker-supervisor.d.ts +13 -0
  73. package/dist/sandbox/worker-supervisor.js +36 -0
  74. package/dist/sandbox/worker.d.ts +1 -0
  75. package/dist/sandbox/worker.js +225 -0
  76. package/dist/sandbox/workspace.service.d.ts +13 -0
  77. package/dist/sandbox/workspace.service.js +72 -0
  78. package/dist/session/managed-attempt-tools.d.ts +18 -0
  79. package/dist/session/managed-attempt-tools.js +797 -0
  80. package/dist/session/managed-build-coordinator.d.ts +5 -0
  81. package/dist/session/managed-build-coordinator.js +105 -0
  82. package/dist/session/phase-completion.artifacts.js +21 -3
  83. package/dist/session/phase-completion.d.ts +11 -2
  84. package/dist/session/phase-completion.helpers.d.ts +5 -0
  85. package/dist/session/phase-completion.helpers.js +51 -10
  86. package/dist/session/phase-completion.js +12 -3
  87. package/dist/session/phase-completion.prompts.d.ts +3 -3
  88. package/dist/session/phase-completion.prompts.js +21 -6
  89. package/dist/session/phase-completion.runtime.d.ts +13 -1
  90. package/dist/session/phase-completion.runtime.js +187 -8
  91. package/dist/session/phase-completion.schemas.js +108 -3
  92. package/dist/session/phase-completion.validation.d.ts +4 -1
  93. package/dist/session/phase-completion.validation.js +150 -21
  94. package/dist/session/provider-cli/ci-repair-readiness.d.ts +14 -0
  95. package/dist/session/provider-cli/ci-repair-readiness.js +105 -0
  96. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +3 -2
  97. package/dist/session/provider-cli/provider-cli.artifacts.js +65 -7
  98. package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
  99. package/dist/session/provider-cli/provider-cli.deterministic.d.ts +2 -0
  100. package/dist/session/provider-cli/provider-cli.deterministic.js +66 -9
  101. package/dist/session/provider-cli/provider-cli.process.d.ts +16 -0
  102. package/dist/session/provider-cli/provider-cli.process.js +212 -25
  103. package/dist/session/provider-cli/provider-cli.prompts.d.ts +6 -1
  104. package/dist/session/provider-cli/provider-cli.prompts.js +129 -18
  105. package/dist/session/provider-cli/provider-cli.qa-sidecars.js +1 -0
  106. package/dist/session/provider-cli/provider-cli.runtime.d.ts +1 -1
  107. package/dist/session/provider-cli/provider-cli.runtime.js +3 -2
  108. package/dist/session/provider-cli/provider-cli.service.d.ts +23 -2
  109. package/dist/session/provider-cli/provider-cli.service.js +507 -88
  110. package/dist/session/provider-cli/provider-cli.types.d.ts +5 -0
  111. package/dist/session/provider-cli/provider-output-state.d.ts +2 -0
  112. package/dist/session/provider-cli/provider-output-state.js +69 -7
  113. package/dist/session/session-redaction.d.ts +3 -0
  114. package/dist/session/session-redaction.js +38 -0
  115. package/dist/session/session.base.d.ts +6 -1
  116. package/dist/session/session.service.d.ts +8 -2
  117. package/dist/session/session.service.js +490 -42
  118. package/dist/session/session.types.d.ts +111 -1
  119. package/dist/session/session.types.js +192 -3
  120. package/dist/shared/format/format-bytes.d.ts +1 -0
  121. package/dist/shared/format/format-bytes.js +9 -0
  122. package/dist/shared/path/path.js +3 -1
  123. package/dist/shared/process/host-process-recovery.d.ts +12 -0
  124. package/dist/shared/process/host-process-recovery.js +78 -0
  125. package/dist/shared/process/host-process-supervisor.d.ts +57 -0
  126. package/dist/shared/process/host-process-supervisor.js +282 -0
  127. package/dist/shared/process/process.d.ts +1 -0
  128. package/dist/shared/process/process.js +1 -0
  129. package/dist/shared/redaction/redaction.js +6 -1
  130. package/dist/version.d.ts +1 -1
  131. package/dist/version.js +1 -1
  132. package/dist/worktree/artifact-injector.d.ts +3 -2
  133. package/dist/worktree/artifact-injector.js +43 -4
  134. package/dist/worktree/dependency-warmer.d.ts +10 -1
  135. package/dist/worktree/dependency-warmer.js +94 -5
  136. package/dist/worktree/index.d.ts +1 -0
  137. package/dist/worktree/index.js +1 -0
  138. package/dist/worktree/prior-phase-artifact-hydrator.d.ts +2 -0
  139. package/dist/worktree/prior-phase-artifact-hydrator.js +136 -0
  140. package/dist/worktree/worktree-paths.d.ts +4 -0
  141. package/dist/worktree/worktree-paths.js +30 -0
  142. package/dist/worktree/worktree.base.d.ts +3 -0
  143. package/dist/worktree/worktree.service.d.ts +8 -2
  144. package/dist/worktree/worktree.service.js +206 -43
  145. package/dist/worktree/worktree.types.d.ts +4 -1
  146. package/package.json +6 -3
  147. package/runtime/windows-job-supervisor.ps1 +356 -0
  148. package/runtime/windows-listener-owner.ps1 +33 -0
@@ -0,0 +1,282 @@
1
+ import { createHash } from "node:crypto";
2
+ import { execFileSync } from "node:child_process";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ const windowsJobSupervisorPath = fileURLToPath(new URL("../../../runtime/windows-job-supervisor.ps1", import.meta.url));
7
+ export function resolveHostProcessSupervisedLaunch(request, resolvedCommand, resolvedArgs, platform = process.platform) {
8
+ const env = request.env;
9
+ if (env?.FORGE_RUNTIME_MODE !== "host")
10
+ return null;
11
+ const supervisorId = requiredEnvironment(env, "FORGE_PROCESS_SUPERVISOR_ID");
12
+ const metadataPath = requiredEnvironment(env, "FORGE_PROCESS_SUPERVISOR_METADATA_PATH");
13
+ const logDirectory = requiredEnvironment(env, "FORGE_LOG_DIR");
14
+ assertMetadataPath(logDirectory, metadataPath);
15
+ const commandDigest = createHash("sha256")
16
+ .update(JSON.stringify([resolvedCommand, ...resolvedArgs]))
17
+ .digest("hex");
18
+ if (platform === "win32") {
19
+ if (!fs.existsSync(windowsJobSupervisorPath)) {
20
+ throw new Error(`host_process_supervisor_missing: ${windowsJobSupervisorPath}`);
21
+ }
22
+ const payloadPath = `${metadataPath}.launch.json`;
23
+ const readyPath = `${metadataPath}.provider-ready.json`;
24
+ const startGrantPath = `${metadataPath}.provider-start-granted`;
25
+ fs.writeFileSync(payloadPath, `${JSON.stringify({
26
+ command: resolvedCommand,
27
+ args: resolvedArgs,
28
+ cwd: request.cwd,
29
+ parentPid: process.pid,
30
+ supervisorId,
31
+ readyPath,
32
+ startGrantPath,
33
+ })}\n`, {
34
+ encoding: "utf8",
35
+ flag: "wx",
36
+ mode: 0o600,
37
+ });
38
+ return {
39
+ command: windowsPowerShellPath(),
40
+ args: [
41
+ "-NoLogo",
42
+ "-NoProfile",
43
+ "-NonInteractive",
44
+ "-ExecutionPolicy",
45
+ "Bypass",
46
+ "-File",
47
+ windowsJobSupervisorPath,
48
+ "-Mode",
49
+ "Outer",
50
+ "-PayloadPath",
51
+ payloadPath,
52
+ ],
53
+ detached: false,
54
+ supervisorId,
55
+ strategy: "windows_job_object",
56
+ metadataPath,
57
+ payloadPath,
58
+ readyPath,
59
+ startGrantPath,
60
+ commandDigest,
61
+ };
62
+ }
63
+ return {
64
+ command: resolvedCommand,
65
+ args: resolvedArgs,
66
+ detached: true,
67
+ supervisorId,
68
+ strategy: "unix_process_group",
69
+ metadataPath,
70
+ commandDigest,
71
+ };
72
+ }
73
+ export function persistHostProcessSupervisorMetadata(launch, request, rootPid, rootCreatedAt, providerReady) {
74
+ if (!Number.isSafeInteger(rootPid) || rootPid <= 0) {
75
+ throw new Error("host_process_supervisor_invalid: supervised root PID was not assigned.");
76
+ }
77
+ const env = request.env ?? {};
78
+ const metadata = {
79
+ schemaVersion: 1,
80
+ supervisorId: launch.supervisorId,
81
+ strategy: launch.strategy,
82
+ rootPid,
83
+ rootCreatedAt,
84
+ rootIdentity: providerReady?.rootPid === rootPid
85
+ ? providerReady.rootIdentity ?? null
86
+ : captureHostProcessIdentity(rootPid),
87
+ commandDigest: launch.commandDigest,
88
+ worktreePath: request.cwd,
89
+ runtimeLeaseId: requiredEnvironment(env, "FORGE_RESOURCE_LEASE_ID"),
90
+ dispatchAttemptId: requiredEnvironment(env, "FORGE_DISPATCH_ATTEMPT_ID"),
91
+ fencingToken: requiredEnvironment(env, "FORGE_FENCING_TOKEN"),
92
+ bridgePid: process.pid,
93
+ providerPid: providerReady?.providerPid,
94
+ providerCreatedAt: providerReady?.providerCreatedAt,
95
+ providerIdentity: providerReady?.providerIdentity
96
+ ?? (providerReady ? captureHostProcessIdentity(providerReady.providerPid) : undefined),
97
+ providerStartAttempt: providerReady?.startAttempt,
98
+ };
99
+ fs.writeFileSync(launch.metadataPath, `${JSON.stringify(metadata)}\n`, {
100
+ encoding: "utf8",
101
+ flag: "wx",
102
+ mode: 0o600,
103
+ });
104
+ return metadata;
105
+ }
106
+ /**
107
+ * Captures an OS-issued process birth marker. Recovery compares this marker
108
+ * before signalling a persisted PID so PID reuse can never redirect cleanup
109
+ * at an unrelated process.
110
+ */
111
+ export function captureHostProcessIdentity(pid, platform = process.platform) {
112
+ if (!Number.isSafeInteger(pid) || pid <= 0)
113
+ return null;
114
+ try {
115
+ if (platform === "linux") {
116
+ const bootId = fs.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim();
117
+ const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
118
+ const close = stat.lastIndexOf(")");
119
+ const fields = close >= 0 ? stat.slice(close + 2).trim().split(/\s+/u) : [];
120
+ const startTicks = fields[19];
121
+ return bootId && startTicks && /^\d+$/u.test(startTicks)
122
+ ? `linux:${bootId}:${startTicks}`
123
+ : null;
124
+ }
125
+ if (platform === "win32") {
126
+ const output = execFileSync(windowsPowerShellPath(), [
127
+ "-NoLogo",
128
+ "-NoProfile",
129
+ "-NonInteractive",
130
+ "-Command",
131
+ `(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToUniversalTime().Ticks`,
132
+ ], {
133
+ encoding: "utf8",
134
+ windowsHide: true,
135
+ timeout: 10_000,
136
+ stdio: ["ignore", "pipe", "ignore"],
137
+ }).trim();
138
+ return /^\d+$/u.test(output) ? `win32:${output}` : null;
139
+ }
140
+ // Other Unix variants do not expose a sufficiently strong birth marker
141
+ // through Node. Fail closed rather than trust a second-resolution ps time.
142
+ return null;
143
+ }
144
+ catch {
145
+ return null;
146
+ }
147
+ }
148
+ export async function terminateHostProcessTree(child, launch, graceMs = 2_000) {
149
+ if (!launch) {
150
+ try {
151
+ child.kill("SIGTERM");
152
+ }
153
+ catch {
154
+ // The process already settled.
155
+ }
156
+ return;
157
+ }
158
+ if (launch.strategy === "windows_job_object") {
159
+ const rootPid = child.pid ?? 0;
160
+ const metadata = readSupervisorMetadata(launch.metadataPath);
161
+ const rootIdentity = metadata?.rootPid === rootPid
162
+ ? metadata.rootIdentity ?? null
163
+ : captureHostProcessIdentity(rootPid, "win32");
164
+ try {
165
+ child.kill("SIGTERM");
166
+ }
167
+ catch {
168
+ // Closing the launcher is best effort when the Job Object already settled.
169
+ }
170
+ await waitForExactProcessExit(rootPid, rootIdentity, graceMs, "win32");
171
+ if (exactProcessExists(rootPid, rootIdentity, "win32")) {
172
+ forceWindowsProcessTree(rootPid);
173
+ await waitForExactProcessExit(rootPid, rootIdentity, graceMs, "win32");
174
+ }
175
+ if (exactProcessExists(rootPid, rootIdentity, "win32")) {
176
+ throw new Error("host_process_supervisor_termination_failed: supervised Windows root did not stop.");
177
+ }
178
+ const providerPid = metadata?.providerPid ?? 0;
179
+ const providerIdentity = metadata?.providerIdentity ?? null;
180
+ if (exactProcessExists(providerPid, providerIdentity, "win32")) {
181
+ forceWindowsProcessTree(providerPid);
182
+ await waitForExactProcessExit(providerPid, providerIdentity, graceMs, "win32");
183
+ }
184
+ if (exactProcessExists(providerPid, providerIdentity, "win32")) {
185
+ throw new Error("host_process_supervisor_termination_failed: supervised Windows provider tree did not stop.");
186
+ }
187
+ return;
188
+ }
189
+ const processGroupId = child.pid;
190
+ if (!processGroupId || !signalUnixProcessGroup(processGroupId, "SIGTERM"))
191
+ return;
192
+ await new Promise(resolve => setTimeout(resolve, graceMs));
193
+ if (unixProcessGroupExists(processGroupId)) {
194
+ signalUnixProcessGroup(processGroupId, "SIGKILL");
195
+ }
196
+ }
197
+ function requiredEnvironment(env, name) {
198
+ const value = env[name]?.trim();
199
+ if (!value)
200
+ throw new Error(`host_process_supervisor_invalid: ${name} is required.`);
201
+ return value;
202
+ }
203
+ function assertMetadataPath(logDirectory, metadataPath) {
204
+ const root = path.resolve(logDirectory);
205
+ const target = path.resolve(metadataPath);
206
+ if (target !== root && !target.startsWith(`${root}${path.sep}`)) {
207
+ throw new Error("host_process_supervisor_invalid: metadata path must be inside FORGE_LOG_DIR.");
208
+ }
209
+ }
210
+ function windowsPowerShellPath() {
211
+ const systemRoot = process.env.SystemRoot?.trim() || "C:\\Windows";
212
+ const candidate = path.win32.join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
213
+ return fs.existsSync(candidate) ? candidate : "powershell.exe";
214
+ }
215
+ function signalUnixProcessGroup(processGroupId, signal) {
216
+ try {
217
+ process.kill(-processGroupId, signal);
218
+ return true;
219
+ }
220
+ catch (error) {
221
+ if (error.code === "ESRCH")
222
+ return false;
223
+ throw error;
224
+ }
225
+ }
226
+ function unixProcessGroupExists(processGroupId) {
227
+ try {
228
+ process.kill(-processGroupId, 0);
229
+ return true;
230
+ }
231
+ catch (error) {
232
+ if (error.code === "ESRCH")
233
+ return false;
234
+ throw error;
235
+ }
236
+ }
237
+ function readSupervisorMetadata(metadataPath) {
238
+ try {
239
+ return JSON.parse(fs.readFileSync(metadataPath, "utf8"));
240
+ }
241
+ catch {
242
+ return null;
243
+ }
244
+ }
245
+ function exactProcessExists(pid, identity, platform) {
246
+ if (!identity || !processExists(pid))
247
+ return false;
248
+ return captureHostProcessIdentity(pid, platform) === identity;
249
+ }
250
+ function processExists(pid) {
251
+ if (!Number.isSafeInteger(pid) || pid <= 0)
252
+ return false;
253
+ try {
254
+ process.kill(pid, 0);
255
+ return true;
256
+ }
257
+ catch (error) {
258
+ return error.code === "EPERM";
259
+ }
260
+ }
261
+ async function waitForExactProcessExit(pid, identity, timeoutMs, platform) {
262
+ const deadline = Date.now() + timeoutMs;
263
+ while (Date.now() < deadline && exactProcessExists(pid, identity, platform)) {
264
+ await new Promise(resolve => setTimeout(resolve, 25));
265
+ }
266
+ }
267
+ function forceWindowsProcessTree(pid) {
268
+ if (!Number.isSafeInteger(pid) || pid <= 0)
269
+ return;
270
+ const systemRoot = process.env.SystemRoot?.trim() || "C:\\Windows";
271
+ const taskkill = path.win32.join(systemRoot, "System32", "taskkill.exe");
272
+ try {
273
+ execFileSync(taskkill, ["/PID", String(pid), "/T", "/F"], {
274
+ windowsHide: true,
275
+ timeout: 5_000,
276
+ stdio: "ignore",
277
+ });
278
+ }
279
+ catch {
280
+ // A racing process exit is verified by the caller before it returns.
281
+ }
282
+ }
@@ -2,6 +2,7 @@ export interface ProcessRunRequest {
2
2
  readonly command: string;
3
3
  readonly args: readonly string[];
4
4
  readonly cwd: string;
5
+ readonly env?: NodeJS.ProcessEnv;
5
6
  }
6
7
  export interface ProcessRunResult {
7
8
  readonly exitCode: number;
@@ -5,6 +5,7 @@ export class NodeProcessRunner {
5
5
  return await new Promise((resolve, reject) => {
6
6
  const child = spawn(request.command, request.args, {
7
7
  cwd: request.cwd,
8
+ env: request.env,
8
9
  stdio: ["ignore", "pipe", "pipe"]
9
10
  });
10
11
  let stdout = "";
@@ -1,6 +1,9 @@
1
1
  const secretKeyPattern = /(api[_-]?key|authorization|bearer|client[_-]?secret|password|secret|token)/i;
2
2
  const attachmentContentKeyPattern = /^(textContent|contentBase64)$/i;
3
3
  const serializedAttachmentContentPattern = /("(?:textContent|contentBase64)"\s*:\s*")(?:(?:\\.)|[^"\\])*(")/gi;
4
+ const authorizationCredentialPattern = /(\bauthorization\s*[:=]\s*(?:bearer|basic)?\s*)([^"',\s]+)/gi;
5
+ const bearerCredentialPattern = /(\bbearer\s+)([^"',\s]+)/gi;
6
+ const secretAssignmentPattern = /(api[_-]?key|client[_-]?secret|password|secret|token)(\s*["']?\s*[:=]\s*["']?\s*)([^"',\s]+)/gi;
4
7
  export function redactSecrets(value, exactSecrets = []) {
5
8
  return redactValue(value, exactSecrets);
6
9
  }
@@ -12,7 +15,9 @@ export function redactText(value, exactSecrets = []) {
12
15
  }
13
16
  }
14
17
  return redacted
15
- .replace(/(api[_-]?key|authorization|bearer|client[_-]?secret|password|secret|token)(["':=\s]+)([^"',\s]+)/gi, "$1$2[REDACTED]")
18
+ .replace(authorizationCredentialPattern, "$1[REDACTED]")
19
+ .replace(bearerCredentialPattern, "$1[REDACTED]")
20
+ .replace(secretAssignmentPattern, "$1$2[REDACTED]")
16
21
  .replace(/(sk-[a-zA-Z0-9_-]{12,})/g, "[REDACTED]");
17
22
  }
18
23
  function redactValue(value, exactSecrets) {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const forgeBridgeVersion = "0.1.3-dev.0b2f81d660c0";
1
+ export declare const forgeBridgeVersion = "0.1.3-dev.0d26c10244e1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const forgeBridgeVersion = "0.1.3-dev.0b2f81d660c0";
1
+ export const forgeBridgeVersion = "0.1.3-dev.0d26c10244e1";
@@ -1,9 +1,10 @@
1
1
  import type { DispatchResolvedArtifact } from "../session/session.types.js";
2
- export declare function injectWorktreeArtifacts(worktreePath: string, artifacts: readonly DispatchResolvedArtifact[]): Promise<void>;
2
+ export declare function injectWorktreeArtifacts(worktreePath: string, artifacts: readonly DispatchResolvedArtifact[], protectedRelativePaths?: ReadonlySet<string>): Promise<void>;
3
3
  export declare function filterArtifactsForLane(artifacts: readonly DispatchResolvedArtifact[], lane: string, phase: string): readonly DispatchResolvedArtifact[];
4
- export declare function writeArtifact(worktreePath: string, artifact: DispatchResolvedArtifact): Promise<void>;
4
+ export declare function writeArtifact(worktreePath: string, artifact: DispatchResolvedArtifact, protectedRelativePaths?: ReadonlySet<string>): Promise<void>;
5
5
  export declare function selectLaneCommand(artifacts: readonly DispatchResolvedArtifact[], lane: string, phase: string): DispatchResolvedArtifact | undefined;
6
6
  export declare function resolveCommandSlug(lane: string, phase: string): string;
7
+ export declare function artifactExecutionBody(artifact: DispatchResolvedArtifact): string;
7
8
  export declare function buildSpecBody(laneCommand: DispatchResolvedArtifact, workItem: {
8
9
  readonly id: string;
9
10
  readonly title: string;
@@ -1,4 +1,5 @@
1
1
  import crypto from "node:crypto";
2
+ import { spawn } from "node:child_process";
2
3
  import fs from "node:fs/promises";
3
4
  import path from "node:path";
4
5
  const DEBUG_OPERATIONAL_SKILLS = new Set(["debug-repro", "debug-root-cause", "debug-fix", "debug-regression"]);
@@ -59,9 +60,9 @@ const DRAFT_WORK_ITEM_OPERATIONAL_HOOKS = new Set([
59
60
  const DRAFT_WORK_ORDER_OPERATIONAL_HOOKS = new Set([
60
61
  "draft-work-order-plan"
61
62
  ]);
62
- export async function injectWorktreeArtifacts(worktreePath, artifacts) {
63
+ export async function injectWorktreeArtifacts(worktreePath, artifacts, protectedRelativePaths = new Set()) {
63
64
  for (const artifact of artifacts) {
64
- await writeArtifact(worktreePath, artifact);
65
+ await writeArtifact(worktreePath, artifact, protectedRelativePaths);
65
66
  }
66
67
  }
67
68
  export function filterArtifactsForLane(artifacts, lane, phase) {
@@ -78,7 +79,7 @@ export function filterArtifactsForLane(artifacts, lane, phase) {
78
79
  }
79
80
  return artifacts;
80
81
  }
81
- export async function writeArtifact(worktreePath, artifact) {
82
+ export async function writeArtifact(worktreePath, artifact, protectedRelativePaths = new Set()) {
82
83
  const writtenHash = sha256(artifact.body);
83
84
  if (writtenHash !== artifact.contentHash.toLowerCase()) {
84
85
  throw new Error(`Injected artifact '${artifact.slug}' content_hash mismatch (expected ${artifact.contentHash}, got ${writtenHash}).`);
@@ -93,6 +94,11 @@ export async function writeArtifact(worktreePath, artifact) {
93
94
  ? path.resolve(scopeDir, slug, "SKILL.md")
94
95
  : path.resolve(scopeDir, `${slug}.md`);
95
96
  assertInside(forgeRoot, filePath, "Artifact file");
97
+ const relativePath = path.relative(worktreePath, filePath).replaceAll(path.sep, "/");
98
+ if (protectedRelativePaths.has(relativePath)) {
99
+ await restoreTrackedArtifactIfItMatchesManagedBody(worktreePath, filePath, relativePath, artifact);
100
+ return;
101
+ }
96
102
  await fs.mkdir(path.dirname(filePath), { recursive: true });
97
103
  await fs.writeFile(filePath, artifact.body, "utf8");
98
104
  }
@@ -120,6 +126,39 @@ export function resolveCommandSlug(lane, phase) {
120
126
  return "dev-prompt";
121
127
  }
122
128
  }
129
+ async function restoreTrackedArtifactIfItMatchesManagedBody(worktreePath, filePath, relativePath, artifact) {
130
+ const current = await fs.readFile(filePath, "utf8").catch(() => null);
131
+ if (current === null
132
+ || (current !== artifact.body && current !== artifactExecutionBody(artifact)))
133
+ return;
134
+ const result = await new Promise((resolve) => {
135
+ const child = spawn("git", ["show", `HEAD:${relativePath}`], {
136
+ cwd: worktreePath,
137
+ windowsHide: true,
138
+ stdio: ["ignore", "pipe", "ignore"],
139
+ });
140
+ let stdout = "";
141
+ child.stdout?.setEncoding("utf8");
142
+ child.stdout?.on("data", chunk => { stdout += chunk; });
143
+ child.on("error", () => resolve({ exitCode: 1, stdout: "" }));
144
+ child.on("close", code => resolve({ exitCode: code ?? 1, stdout }));
145
+ });
146
+ if (result.exitCode === 0)
147
+ await fs.writeFile(filePath, result.stdout, "utf8");
148
+ }
149
+ export function artifactExecutionBody(artifact) {
150
+ const content = artifact.body;
151
+ if (!content.startsWith("---"))
152
+ return content;
153
+ const end = content.indexOf("\n---", 3);
154
+ if (end < 3)
155
+ return content;
156
+ let bodyStart = end + 4;
157
+ while (bodyStart < content.length && (content[bodyStart] === "\n" || content[bodyStart] === "\r")) {
158
+ bodyStart += 1;
159
+ }
160
+ return content.slice(bodyStart);
161
+ }
123
162
  function shouldInjectDebugArtifact(artifact, commandSlug) {
124
163
  if (artifact.scope.trim().toLowerCase() !== "operational") {
125
164
  return false;
@@ -192,7 +231,7 @@ function normalizeDraftPhase(phase) {
192
231
  return phase.trim().toLowerCase().replaceAll("-", "_");
193
232
  }
194
233
  export function buildSpecBody(laneCommand, workItem, phase = "implementation") {
195
- const prompt = laneCommand.body.replaceAll("{workItemId}", workItem.id);
234
+ const prompt = artifactExecutionBody(laneCommand).replaceAll("{workItemId}", workItem.id);
196
235
  return `---
197
236
  work_item_id: ${workItem.id}
198
237
  status: approved
@@ -22,7 +22,10 @@ export interface WarmDependenciesResult {
22
22
  readonly fallbacks: readonly InstallFallbackResult[];
23
23
  }
24
24
  export interface CommandExecutor {
25
- run(cwd: string, command: string, args: readonly string[]): Promise<void>;
25
+ run(cwd: string, command: string, args: readonly string[], options?: CommandExecutionOptions): Promise<void>;
26
+ }
27
+ export interface CommandExecutionOptions {
28
+ readonly env?: NodeJS.ProcessEnv;
26
29
  }
27
30
  export declare function detectWarmPaths(repoRoot: string, warmPathOverrides?: readonly string[]): Promise<WarmPathEntry[]>;
28
31
  export declare function warmWorktreeDependencies(request: WarmDependenciesRequest, executor?: CommandExecutor): Promise<WarmDependenciesResult>;
@@ -41,3 +44,9 @@ export declare function resolveNpmLaunch(args: readonly string[], options?: NpmL
41
44
  command: string;
42
45
  args: readonly string[];
43
46
  };
47
+ export declare function resolvePnpmLaunch(args: readonly string[]): {
48
+ command: string;
49
+ args: readonly string[];
50
+ };
51
+ export declare function formatCommandFailureOutput(stdout: string, stderr: string): string;
52
+ export declare function unattendedPackageManagerEnvironment(env?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
@@ -23,6 +23,7 @@ const NATIVE_MODULE_HINTS = [
23
23
  "better-sqlite3",
24
24
  "@swc/core"
25
25
  ];
26
+ const MAX_COMMAND_FAILURE_OUTPUT_LENGTH = 8_000;
26
27
  export async function detectWarmPaths(repoRoot, warmPathOverrides = []) {
27
28
  const entries = new Map();
28
29
  await collectDetectedPaths(repoRoot, repoRoot, entries, 0);
@@ -50,10 +51,39 @@ export async function warmWorktreeDependencies(request, executor = defaultComman
50
51
  const linked = [];
51
52
  const skipped = [];
52
53
  const fallbacks = [];
54
+ const restoredDotNetRoots = new Set();
55
+ const installedNodeRoots = new Set();
53
56
  for (const entry of warmPaths) {
54
57
  const sourcePath = path.join(baseCheckoutPath, entry.relativePath);
55
58
  const targetPath = path.join(worktreePath, entry.relativePath);
56
59
  assertWithinRoot(allowedRoot, targetPath, "Warm target");
60
+ if (entry.ecosystem === "node" && path.basename(entry.relativePath) === "node_modules") {
61
+ await removeLegacyDirectoryLink(targetPath, allowedRoot);
62
+ const installPackageRoot = await resolveNodeInstallPackageRoot(worktreePath, entry.packageRoot);
63
+ const packageRoot = path.resolve(worktreePath, installPackageRoot === "." ? "" : installPackageRoot);
64
+ const packageKey = process.platform === "win32" ? packageRoot.toLowerCase() : packageRoot;
65
+ if (installedNodeRoots.has(packageKey)) {
66
+ skipped.push(entry.relativePath);
67
+ continue;
68
+ }
69
+ installedNodeRoots.add(packageKey);
70
+ const install = await runInstallFallback({ ...entry, packageRoot: installPackageRoot }, worktreePath, "Managed worktrees require isolated Node dependencies.", executor);
71
+ fallbacks.push(install);
72
+ continue;
73
+ }
74
+ if (entry.ecosystem === "dotnet") {
75
+ await removeLegacyDirectoryLink(targetPath, allowedRoot);
76
+ const packageRoot = path.resolve(worktreePath, entry.packageRoot === "." ? "" : entry.packageRoot);
77
+ const packageKey = process.platform === "win32" ? packageRoot.toLowerCase() : packageRoot;
78
+ if (restoredDotNetRoots.has(packageKey)) {
79
+ skipped.push(entry.relativePath);
80
+ continue;
81
+ }
82
+ restoredDotNetRoots.add(packageKey);
83
+ const install = await runInstallFallback(entry, worktreePath, "Managed worktrees require isolated .NET build outputs.", executor);
84
+ fallbacks.push(install);
85
+ continue;
86
+ }
57
87
  const sourceExists = await directoryExists(sourcePath);
58
88
  const fallbackReason = await resolveFallbackReason(entry, baseCheckoutPath, worktreePath, sourceExists);
59
89
  if (fallbackReason) {
@@ -70,6 +100,16 @@ export async function warmWorktreeDependencies(request, executor = defaultComman
70
100
  }
71
101
  return { linked, skipped, fallbacks };
72
102
  }
103
+ async function removeLegacyDirectoryLink(targetPath, allowedRoot) {
104
+ const resolvedTarget = path.resolve(targetPath);
105
+ const resolvedRoot = path.resolve(allowedRoot);
106
+ assertWithinRoot(resolvedRoot, resolvedTarget, "Legacy warm target");
107
+ const stat = await fs.lstat(resolvedTarget).catch(() => null);
108
+ if (!stat?.isSymbolicLink()) {
109
+ return;
110
+ }
111
+ await fs.rm(resolvedTarget, { force: true });
112
+ }
73
113
  export async function createDirectoryLink(sourcePath, targetPath, allowedRoot) {
74
114
  const resolvedSource = path.resolve(sourcePath);
75
115
  const resolvedTarget = path.resolve(targetPath);
@@ -81,7 +121,10 @@ export async function createDirectoryLink(sourcePath, targetPath, allowedRoot) {
81
121
  if (!stat?.isDirectory()) {
82
122
  throw new Error(`Link source must be an existing directory: ${resolvedSource}`);
83
123
  }
84
- await fs.rm(resolvedTarget, { recursive: true, force: true });
124
+ const existingTarget = await fs.lstat(resolvedTarget).catch(() => null);
125
+ await fs.rm(resolvedTarget, existingTarget?.isSymbolicLink()
126
+ ? { force: true }
127
+ : { recursive: true, force: true });
85
128
  await fs.mkdir(path.dirname(resolvedTarget), { recursive: true });
86
129
  if (process.platform === "win32") {
87
130
  await fs.symlink(resolvedSource, resolvedTarget, "junction");
@@ -185,10 +228,17 @@ export async function lockfilesDiffer(baseCheckoutPath, worktreePath, packageRoo
185
228
  async function runInstallFallback(entry, worktreePath, reason, executor) {
186
229
  const cwd = path.join(worktreePath, entry.packageRoot === "." ? "" : entry.packageRoot);
187
230
  if (entry.ecosystem === "node") {
231
+ const pnpmLockPath = path.join(cwd, "pnpm-lock.yaml");
232
+ if (await pathExists(pnpmLockPath)) {
233
+ const args = ["install", "--frozen-lockfile"];
234
+ const launch = resolvePnpmLaunch(args);
235
+ await executor.run(cwd, launch.command, launch.args, { env: unattendedPackageManagerEnvironment() });
236
+ return { ecosystem: entry.ecosystem, packageRoot: entry.packageRoot, reason, command: `pnpm ${args.join(" ")}` };
237
+ }
188
238
  const lockPath = path.join(cwd, "package-lock.json");
189
239
  const args = (await pathExists(lockPath)) ? ["ci"] : ["install"];
190
240
  const launch = resolveNpmLaunch(args);
191
- await executor.run(cwd, launch.command, launch.args);
241
+ await executor.run(cwd, launch.command, launch.args, { env: unattendedPackageManagerEnvironment() });
192
242
  return { ecosystem: entry.ecosystem, packageRoot: entry.packageRoot, reason, command: `npm ${args.join(" ")}` };
193
243
  }
194
244
  if (entry.ecosystem === "python") {
@@ -219,6 +269,23 @@ async function runInstallFallback(entry, worktreePath, reason, executor) {
219
269
  command: "dotnet restore"
220
270
  };
221
271
  }
272
+ async function resolveNodeInstallPackageRoot(worktreePath, packageRoot) {
273
+ const resolvedWorktree = path.resolve(worktreePath);
274
+ let current = path.resolve(resolvedWorktree, packageRoot === "." ? "" : packageRoot);
275
+ assertWithinRoot(resolvedWorktree, current, "Node package root");
276
+ while (isSubPath(resolvedWorktree, current)) {
277
+ if (await pathExists(path.join(current, "pnpm-lock.yaml"))
278
+ || await pathExists(path.join(current, "pnpm-workspace.yaml"))
279
+ || await pathExists(path.join(current, "package-lock.json"))) {
280
+ const relative = normalizeRelativePath(path.relative(resolvedWorktree, current));
281
+ return relative || ".";
282
+ }
283
+ if (current === resolvedWorktree)
284
+ break;
285
+ current = path.dirname(current);
286
+ }
287
+ return packageRoot;
288
+ }
222
289
  export async function requiresInstallFallback(entry, baseCheckoutPath, worktreePath, sourceExists) {
223
290
  return resolveFallbackReason(entry, baseCheckoutPath, worktreePath, sourceExists);
224
291
  }
@@ -310,6 +377,26 @@ export function resolveNpmLaunch(args, options = {}) {
310
377
  }
311
378
  return { command: "npm", args };
312
379
  }
380
+ export function resolvePnpmLaunch(args) {
381
+ if (process.platform === "win32") {
382
+ return buildWindowsCmdLaunch("pnpm.cmd", args, process.env.ComSpec);
383
+ }
384
+ return { command: "pnpm", args };
385
+ }
386
+ export function formatCommandFailureOutput(stdout, stderr) {
387
+ const sections = [
388
+ stderr.trim().length > 0 ? `stderr:\n${stderr.trim()}` : null,
389
+ stdout.trim().length > 0 ? `stdout:\n${stdout.trim()}` : null,
390
+ ].filter((section) => section !== null);
391
+ const output = sections.join("\n");
392
+ if (output.length <= MAX_COMMAND_FAILURE_OUTPUT_LENGTH) {
393
+ return output;
394
+ }
395
+ return `[last ${MAX_COMMAND_FAILURE_OUTPUT_LENGTH} characters]\n${output.slice(-MAX_COMMAND_FAILURE_OUTPUT_LENGTH)}`;
396
+ }
397
+ export function unattendedPackageManagerEnvironment(env = process.env) {
398
+ return { ...env, CI: "true" };
399
+ }
313
400
  function tryResolveNpmCandidate(candidate) {
314
401
  try {
315
402
  if (!existsSync(candidate)) {
@@ -331,10 +418,12 @@ function defaultNpmCliCandidates(execPath, homeDir) {
331
418
  return candidates;
332
419
  }
333
420
  const defaultCommandExecutor = {
334
- async run(cwd, command, args) {
335
- const result = await defaultProcessRunner.run({ command, cwd, args });
421
+ async run(cwd, command, args, options) {
422
+ const result = await defaultProcessRunner.run({ command, cwd, args, env: options?.env });
336
423
  if (result.exitCode !== 0) {
337
- throw new Error(`${command} ${args.join(" ")} failed (${result.exitCode}): ${result.stderr.trim()}`);
424
+ const output = formatCommandFailureOutput(result.stdout, result.stderr);
425
+ const details = output.length > 0 ? `\n${output}` : " no output was captured";
426
+ throw new Error(`${command} ${args.join(" ")} failed (${result.exitCode}) in ${cwd}:${details}`);
338
427
  }
339
428
  }
340
429
  };
@@ -1,4 +1,5 @@
1
1
  export { GitWorktreeService } from "./worktree.service.js";
2
+ export { managedRepoRoot, resolveManagedWorktreeRoot, resolveManagedWorktreeRootForRepo, } from "./worktree-paths.js";
2
3
  export { detectWarmPaths, warmWorktreeDependencies, createDirectoryLink, lockfilesDiffer, requiresInstallFallback } from "./dependency-warmer.js";
3
4
  export type { WarmDependenciesResult, WarmPathEntry, InstallFallbackResult } from "./dependency-warmer.js";
4
5
  export type { WorktreeService } from "./worktree.base.js";
@@ -1,2 +1,3 @@
1
1
  export { GitWorktreeService } from "./worktree.service.js";
2
+ export { managedRepoRoot, resolveManagedWorktreeRoot, resolveManagedWorktreeRootForRepo, } from "./worktree-paths.js";
2
3
  export { detectWarmPaths, warmWorktreeDependencies, createDirectoryLink, lockfilesDiffer, requiresInstallFallback } from "./dependency-warmer.js";
@@ -0,0 +1,2 @@
1
+ import type { DispatchPriorPhaseArtifact } from "../session/session.types.js";
2
+ export declare function hydratePriorPhaseArtifacts(worktreePath: string, artifacts?: readonly DispatchPriorPhaseArtifact[]): Promise<number>;