@loucompanion/forge-bridge 0.1.3-dev.bd0c67bbf352 → 0.1.3-dev.c24058d57ab3

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 (88) hide show
  1. package/README.md +44 -0
  2. package/dist/bridge/bridge.base.d.ts +5 -1
  3. package/dist/bridge/bridge.service.d.ts +36 -2
  4. package/dist/bridge/bridge.service.js +588 -18
  5. package/dist/bridge/bridge.types.d.ts +114 -1
  6. package/dist/cli/cli.help.d.ts +2 -0
  7. package/dist/cli/cli.help.js +183 -0
  8. package/dist/cli/cli.service.d.ts +12 -0
  9. package/dist/cli/cli.service.js +176 -184
  10. package/dist/cli/local-config.d.ts +2 -0
  11. package/dist/cli/local-config.js +6 -2
  12. package/dist/sandbox/command-runner.d.ts +4 -0
  13. package/dist/sandbox/command-runner.js +47 -0
  14. package/dist/sandbox/contract.d.ts +5 -0
  15. package/dist/sandbox/contract.js +276 -0
  16. package/dist/sandbox/event-budget.d.ts +11 -0
  17. package/dist/sandbox/event-budget.js +27 -0
  18. package/dist/sandbox/image-policy.d.ts +35 -0
  19. package/dist/sandbox/image-policy.js +247 -0
  20. package/dist/sandbox/index.d.ts +5 -0
  21. package/dist/sandbox/index.js +5 -0
  22. package/dist/sandbox/lease-watchdog.d.ts +18 -0
  23. package/dist/sandbox/lease-watchdog.js +67 -0
  24. package/dist/sandbox/preflight.d.ts +36 -0
  25. package/dist/sandbox/preflight.js +239 -0
  26. package/dist/sandbox/process.d.ts +20 -0
  27. package/dist/sandbox/process.js +124 -0
  28. package/dist/sandbox/provider-auth/codex.d.ts +26 -0
  29. package/dist/sandbox/provider-auth/codex.js +212 -0
  30. package/dist/sandbox/repository-lock.d.ts +5 -0
  31. package/dist/sandbox/repository-lock.js +83 -0
  32. package/dist/sandbox/runtime.d.ts +234 -0
  33. package/dist/sandbox/runtime.js +1767 -0
  34. package/dist/sandbox/sandbox.types.d.ts +83 -0
  35. package/dist/sandbox/sandbox.types.js +1 -0
  36. package/dist/sandbox/worker-supervisor.d.ts +13 -0
  37. package/dist/sandbox/worker-supervisor.js +36 -0
  38. package/dist/sandbox/worker.d.ts +1 -0
  39. package/dist/sandbox/worker.js +228 -0
  40. package/dist/sandbox/workspace.service.d.ts +12 -0
  41. package/dist/sandbox/workspace.service.js +49 -0
  42. package/dist/session/phase-completion.artifacts.d.ts +10 -0
  43. package/dist/session/phase-completion.artifacts.js +620 -0
  44. package/dist/session/phase-completion.d.ts +32 -44
  45. package/dist/session/phase-completion.helpers.d.ts +112 -0
  46. package/dist/session/phase-completion.helpers.js +660 -0
  47. package/dist/session/phase-completion.js +28 -3501
  48. package/dist/session/phase-completion.prompts.d.ts +14 -0
  49. package/dist/session/phase-completion.prompts.js +220 -0
  50. package/dist/session/phase-completion.runtime.d.ts +36 -0
  51. package/dist/session/phase-completion.runtime.js +609 -0
  52. package/dist/session/phase-completion.schemas.d.ts +3 -0
  53. package/dist/session/phase-completion.schemas.js +707 -0
  54. package/dist/session/phase-completion.validation.d.ts +34 -0
  55. package/dist/session/phase-completion.validation.js +724 -0
  56. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +93 -0
  57. package/dist/session/provider-cli/provider-cli.artifacts.js +754 -0
  58. package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
  59. package/dist/session/provider-cli/provider-cli.deterministic-payloads.d.ts +12 -0
  60. package/dist/session/provider-cli/provider-cli.deterministic-payloads.js +279 -0
  61. package/dist/session/provider-cli/provider-cli.deterministic.d.ts +14 -0
  62. package/dist/session/provider-cli/provider-cli.deterministic.js +329 -0
  63. package/dist/session/provider-cli/provider-cli.process.d.ts +32 -0
  64. package/dist/session/provider-cli/provider-cli.process.js +287 -0
  65. package/dist/session/provider-cli/provider-cli.prompts.d.ts +44 -0
  66. package/dist/session/provider-cli/provider-cli.prompts.js +528 -0
  67. package/dist/session/provider-cli/provider-cli.qa-sidecars.d.ts +11 -0
  68. package/dist/session/provider-cli/provider-cli.qa-sidecars.js +65 -0
  69. package/dist/session/provider-cli/provider-cli.runtime.d.ts +9 -0
  70. package/dist/session/provider-cli/provider-cli.runtime.js +40 -0
  71. package/dist/session/provider-cli/provider-cli.service.d.ts +9 -53
  72. package/dist/session/provider-cli/provider-cli.service.js +161 -2561
  73. package/dist/session/provider-cli/provider-cli.types.d.ts +1 -0
  74. package/dist/session/provider-cli/provider-output-state.d.ts +20 -0
  75. package/dist/session/provider-cli/provider-output-state.js +445 -0
  76. package/dist/session/session.service.js +34 -24
  77. package/dist/session/session.types.d.ts +89 -1
  78. package/dist/session/session.types.js +209 -3
  79. package/dist/shared/process/windows-launch.d.ts +8 -0
  80. package/dist/shared/process/windows-launch.js +41 -0
  81. package/dist/shared/provider-capabilities.d.ts +1 -0
  82. package/dist/shared/provider-capabilities.js +3 -0
  83. package/dist/shared/redaction/redaction.js +4 -2
  84. package/dist/version.d.ts +1 -1
  85. package/dist/version.js +1 -1
  86. package/dist/worktree/dependency-warmer.d.ts +11 -0
  87. package/dist/worktree/dependency-warmer.js +45 -1
  88. package/package.json +4 -2
@@ -0,0 +1,83 @@
1
+ import type { RunOnceDispatchPacket } from "../session/session.types.js";
2
+ export interface SandboxContract {
3
+ readonly schemaVersion: 1;
4
+ readonly workingDirectory: "/workspace";
5
+ readonly commands: Readonly<Record<string, string>>;
6
+ readonly caches: readonly SandboxCache[];
7
+ readonly services: readonly SandboxService[];
8
+ readonly environment: readonly string[];
9
+ readonly artifacts: readonly string[];
10
+ readonly logs: readonly string[];
11
+ readonly browser: boolean;
12
+ }
13
+ export interface SandboxCache {
14
+ readonly name: string;
15
+ readonly mode: "readonly" | "per_attempt";
16
+ }
17
+ export interface SandboxService {
18
+ readonly type: "postgres" | "redis" | "browser";
19
+ readonly port: number;
20
+ }
21
+ export interface ValidatedSandboxContract {
22
+ readonly contract: SandboxContract;
23
+ readonly normalizedJson: string;
24
+ readonly digest: string;
25
+ }
26
+ export interface SandboxPreflightResult {
27
+ readonly available: boolean;
28
+ readonly healthy: boolean;
29
+ readonly generation: number;
30
+ readonly runtimeVersion: string | null;
31
+ readonly maxConcurrentSandboxes: number;
32
+ readonly availableMemoryBytes: number;
33
+ readonly availableCpuMillis: number;
34
+ readonly availableWorkspaceBytes: number;
35
+ readonly diskLow: boolean;
36
+ readonly unavailableCode: string | null;
37
+ readonly unavailableReason: string | null;
38
+ readonly features: SandboxFeatureMatrix;
39
+ }
40
+ export interface SandboxFeatureMatrix {
41
+ readonly rootless: boolean;
42
+ readonly userNamespaces: boolean;
43
+ readonly cgroupV2: boolean;
44
+ readonly seccomp: boolean;
45
+ readonly lsm: boolean;
46
+ readonly storageQuota: boolean;
47
+ readonly storagePolicy: boolean;
48
+ readonly compose: boolean;
49
+ readonly internalNetwork: boolean;
50
+ readonly buildkit: boolean;
51
+ readonly imagePolicy: boolean;
52
+ readonly securityProfile: boolean;
53
+ readonly egressProxy: boolean;
54
+ }
55
+ export interface SandboxReservation {
56
+ readonly dispatchAttemptId: string;
57
+ readonly leaseId: string;
58
+ readonly fencingToken: string;
59
+ readonly runnerInstanceId: string;
60
+ readonly expiresAt: string;
61
+ readonly reservedUntil: string;
62
+ readonly dispatch: RunOnceDispatchPacket;
63
+ readonly contract?: ValidatedSandboxContract;
64
+ readonly inputCommitSha?: string;
65
+ readonly sandboxConfigCommitSha?: string;
66
+ readonly imageDigest?: string;
67
+ }
68
+ export interface CommandResult {
69
+ readonly exitCode: number;
70
+ readonly stdout: string;
71
+ readonly stderr: string;
72
+ }
73
+ export interface CommandRunner {
74
+ run(command: string, args: readonly string[], options?: CommandOptions): Promise<CommandResult>;
75
+ }
76
+ export interface CommandOptions {
77
+ readonly cwd?: string;
78
+ readonly env?: Readonly<Record<string, string>>;
79
+ readonly stdin?: string | Uint8Array;
80
+ readonly timeoutMs?: number;
81
+ readonly signal?: AbortSignal;
82
+ readonly maxOutputBytes?: number;
83
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { MonotonicLeaseDeadline } from "./lease-watchdog.js";
2
+ export type SandboxTerminationCode = "sandbox_lease_expired" | "sandbox_wall_timeout" | "sandbox_idle_timeout";
3
+ export declare class WorkerDeadlineSupervisor {
4
+ readonly lease: MonotonicLeaseDeadline;
5
+ readonly hardWallMs: number;
6
+ readonly idleMs: number;
7
+ private readonly startedAtMs;
8
+ private lastActivityMs;
9
+ constructor(lease: MonotonicLeaseDeadline, hardWallMs: number, idleMs: number, startedAtMs?: number);
10
+ touch(nowMs?: number): void;
11
+ termination(nowMs?: number): SandboxTerminationCode | null;
12
+ }
13
+ export declare function terminationMessage(code: SandboxTerminationCode): string;
@@ -0,0 +1,36 @@
1
+ export class WorkerDeadlineSupervisor {
2
+ lease;
3
+ hardWallMs;
4
+ idleMs;
5
+ startedAtMs;
6
+ lastActivityMs;
7
+ constructor(lease, hardWallMs, idleMs, startedAtMs = performance.now()) {
8
+ this.lease = lease;
9
+ this.hardWallMs = hardWallMs;
10
+ this.idleMs = idleMs;
11
+ this.startedAtMs = startedAtMs;
12
+ if (!Number.isFinite(hardWallMs) || hardWallMs <= 0 || !Number.isFinite(idleMs) || idleMs <= 0) {
13
+ throw new Error("Sandbox watchdog limits must be positive.");
14
+ }
15
+ this.lastActivityMs = startedAtMs;
16
+ }
17
+ touch(nowMs = performance.now()) {
18
+ this.lastActivityMs = Math.max(this.lastActivityMs, nowMs);
19
+ }
20
+ termination(nowMs = performance.now()) {
21
+ if (this.lease.expired(nowMs))
22
+ return "sandbox_lease_expired";
23
+ if (nowMs - this.startedAtMs >= this.hardWallMs)
24
+ return "sandbox_wall_timeout";
25
+ if (nowMs - this.lastActivityMs >= this.idleMs)
26
+ return "sandbox_idle_timeout";
27
+ return null;
28
+ }
29
+ }
30
+ export function terminationMessage(code) {
31
+ switch (code) {
32
+ case "sandbox_lease_expired": return "The durable execution lease expired inside the sandbox.";
33
+ case "sandbox_wall_timeout": return "The sandbox reached its hard wall-clock limit.";
34
+ case "sandbox_idle_timeout": return "The sandbox produced no control or process activity before its idle limit.";
35
+ }
36
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,228 @@
1
+ import { createHash } from "node:crypto";
2
+ import { spawn } from "node:child_process";
3
+ import fs from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { MonotonicLeaseDeadline } from "./lease-watchdog.js";
7
+ import { terminationMessage, WorkerDeadlineSupervisor } from "./worker-supervisor.js";
8
+ const controlRoot = "/control";
9
+ const runtimeRoot = "/run/forge";
10
+ const workspace = process.env.FORGE_WORKSPACE ?? "/workspace";
11
+ async function main() {
12
+ const dispatchText = await readBoundedStdin(16 * 1024 * 1024);
13
+ const bootstrap = JSON.parse(dispatchText);
14
+ if (bootstrap.schemaVersion !== 1 || !bootstrap.dispatch || typeof bootstrap.leaseControlKey !== "string") {
15
+ throw new Error("Invalid sandbox bootstrap packet.");
16
+ }
17
+ const dispatch = bootstrap.dispatch;
18
+ const sandbox = dispatch.sandbox;
19
+ const attemptId = requiredId(sandbox?.dispatchAttemptId, "dispatchAttemptId");
20
+ const lease = dispatch.lease;
21
+ const fencingToken = requiredId(lease?.fencingToken, "fencingToken");
22
+ const leaseDeadline = new MonotonicLeaseDeadline(attemptId, fencingToken, requiredString(lease?.expiresAt, "expiresAt"), bootstrap.leaseControlKey);
23
+ const limits = requiredLimits(bootstrap.limits);
24
+ await installServiceEnvironment(attemptId);
25
+ await fs.mkdir(runtimeRoot, { recursive: true, mode: 0o700 });
26
+ await fs.mkdir(path.join(controlRoot, "output"), { recursive: true, mode: 0o700 });
27
+ const dispatchPath = path.join(runtimeRoot, "dispatch.json");
28
+ const resultPath = path.join(controlRoot, "output", "result.json");
29
+ await fs.writeFile(dispatchPath, JSON.stringify(dispatch), { mode: 0o600 });
30
+ const entrypoint = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../cli/bin.js");
31
+ const runOutcome = await run(process.execPath, [entrypoint, "run-once", "--dispatch", dispatchPath, "--result", resultPath], {
32
+ deadline: leaseDeadline,
33
+ updatePath: path.join(runtimeRoot, "lease-update.json"),
34
+ supervisor: new WorkerDeadlineSupervisor(leaseDeadline, limits.hardWallMs, limits.idleMs)
35
+ });
36
+ await fs.rm(dispatchPath, { force: true });
37
+ if (runOutcome.terminationCode) {
38
+ await writeTerminationResult(resultPath, runOutcome.terminationCode);
39
+ }
40
+ let resultCommitSha = null;
41
+ let bundleSha256 = null;
42
+ let bundleBytes = 0;
43
+ const bundlePath = path.join(controlRoot, "output", "result.bundle");
44
+ if (!runOutcome.terminationCode && await exists(path.join(workspace, ".git"))) {
45
+ resultCommitSha = (await capture("git", ["-C", workspace, "rev-parse", "HEAD"])).trim();
46
+ const resultRef = `refs/forge/results/${attemptId.replace(/-/g, "")}`;
47
+ await runRequired("git", ["-C", workspace, "update-ref", resultRef, resultCommitSha]);
48
+ try {
49
+ await runRequired("git", ["-C", workspace, "bundle", "create", bundlePath, resultRef]);
50
+ }
51
+ finally {
52
+ await run("git", ["-C", workspace, "update-ref", "-d", resultRef]);
53
+ }
54
+ const bundle = await fs.readFile(bundlePath);
55
+ bundleBytes = bundle.length;
56
+ bundleSha256 = createHash("sha256").update(bundle).digest("hex");
57
+ }
58
+ const resultBytes = await fs.readFile(resultPath);
59
+ const manifest = {
60
+ schemaVersion: 1,
61
+ dispatchAttemptId: attemptId,
62
+ executionLeaseId: sandbox?.executionLeaseId,
63
+ fencingToken: dispatch.lease?.fencingToken,
64
+ runnerInstanceId: sandbox?.runnerInstanceId,
65
+ inputCommitSha: sandbox?.inputCommitSha,
66
+ resultCommitSha,
67
+ branch: dispatch.lease?.worktreeKey,
68
+ contractDigest: sandbox?.contractDigest,
69
+ imageDigest: sandbox?.imageDigest,
70
+ result: { path: "result.json", bytes: resultBytes.length, sha256: createHash("sha256").update(resultBytes).digest("hex") },
71
+ bundle: bundleSha256 ? { path: "result.bundle", bytes: bundleBytes, sha256: bundleSha256 } : null,
72
+ workerExitCode: runOutcome.exitCode,
73
+ workerExitClassification: runOutcome.terminationCode ?? "agent_exit"
74
+ };
75
+ const temporary = path.join(controlRoot, "output", "manifest.json.tmp");
76
+ await fs.writeFile(temporary, `${JSON.stringify(manifest)}\n`, { mode: 0o600 });
77
+ await fs.rename(temporary, path.join(controlRoot, "output", "manifest.json"));
78
+ return runOutcome.exitCode;
79
+ }
80
+ async function readBoundedStdin(limit) {
81
+ const chunks = [];
82
+ let total = 0;
83
+ for await (const chunk of process.stdin) {
84
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
85
+ total += buffer.length;
86
+ if (total > limit)
87
+ throw new Error("Dispatch bootstrap exceeds 16 MiB.");
88
+ chunks.push(buffer);
89
+ }
90
+ return Buffer.concat(chunks).toString("utf8");
91
+ }
92
+ async function run(command, args, watchdog) {
93
+ return await new Promise((resolve, reject) => {
94
+ const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"], shell: false });
95
+ let readingUpdate = false;
96
+ let killTimer = null;
97
+ let terminationCode = null;
98
+ const terminate = (code) => {
99
+ if (child.exitCode !== null || child.signalCode !== null)
100
+ return;
101
+ terminationCode ??= code;
102
+ emitTerminationEvent(terminationCode);
103
+ child.kill("SIGTERM");
104
+ killTimer = setTimeout(() => child.kill("SIGKILL"), 10_000);
105
+ killTimer.unref?.();
106
+ };
107
+ child.stdout.on("data", chunk => {
108
+ watchdog?.supervisor.touch();
109
+ process.stdout.write(chunk);
110
+ });
111
+ child.stderr.on("data", chunk => {
112
+ watchdog?.supervisor.touch();
113
+ process.stderr.write(chunk);
114
+ });
115
+ const monitor = watchdog ? setInterval(() => {
116
+ const reason = watchdog.supervisor.termination();
117
+ if (reason) {
118
+ terminate(reason);
119
+ return;
120
+ }
121
+ if (readingUpdate)
122
+ return;
123
+ readingUpdate = true;
124
+ fs.readFile(watchdog.updatePath, "utf8")
125
+ .then(value => {
126
+ const applied = watchdog.deadline.apply(JSON.parse(value));
127
+ if (applied)
128
+ watchdog.supervisor.touch();
129
+ return applied;
130
+ })
131
+ .catch(() => false)
132
+ .finally(() => { readingUpdate = false; });
133
+ }, 250) : null;
134
+ monitor?.unref?.();
135
+ child.on("error", error => {
136
+ if (monitor)
137
+ clearInterval(monitor);
138
+ if (killTimer)
139
+ clearTimeout(killTimer);
140
+ reject(error);
141
+ });
142
+ child.on("close", code => {
143
+ if (monitor)
144
+ clearInterval(monitor);
145
+ if (killTimer)
146
+ clearTimeout(killTimer);
147
+ resolve({ exitCode: terminationCode ? 124 : code ?? 1, terminationCode });
148
+ });
149
+ });
150
+ }
151
+ async function runRequired(command, args) {
152
+ const outcome = await run(command, args);
153
+ if (outcome.exitCode !== 0)
154
+ throw new Error(`${command} failed with exit ${outcome.exitCode}.`);
155
+ }
156
+ async function capture(command, args) {
157
+ return await new Promise((resolve, reject) => {
158
+ const child = spawn(command, args, { stdio: ["ignore", "pipe", "inherit"], shell: false });
159
+ const chunks = [];
160
+ child.stdout.on("data", chunk => chunks.push(Buffer.from(chunk)));
161
+ child.on("error", reject);
162
+ child.on("close", code => code === 0 ? resolve(Buffer.concat(chunks).toString("utf8")) : reject(new Error(`${command} failed with exit ${code}.`)));
163
+ });
164
+ }
165
+ async function exists(target) { return fs.stat(target).then(() => true).catch(() => false); }
166
+ async function installServiceEnvironment(attemptId) {
167
+ const attemptHex = attemptId.replace(/-/gu, "").toLowerCase();
168
+ const postgresPassword = await readServiceCredential("postgres-password");
169
+ if (postgresPassword)
170
+ process.env.FORGE_TEST_DATABASE = `postgresql://forge:${encodeURIComponent(postgresPassword)}@forge-${attemptHex}-postgres:5432/forge_test`;
171
+ const redisPassword = await readServiceCredential("redis-password");
172
+ if (redisPassword)
173
+ process.env.FORGE_TEST_REDIS = `redis://:${encodeURIComponent(redisPassword)}@forge-${attemptHex}-redis:6379/0`;
174
+ const browserToken = await readServiceCredential("browser-token");
175
+ if (browserToken)
176
+ process.env.FORGE_BROWSER_TOKEN = browserToken;
177
+ }
178
+ async function readServiceCredential(name) {
179
+ const target = `/run/forge-secrets/${name}`;
180
+ const stat = await fs.lstat(target).catch(() => null);
181
+ if (!stat)
182
+ return null;
183
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size < 32 || stat.size > 256) {
184
+ throw new Error(`Invalid ${name} service credential file.`);
185
+ }
186
+ return (await fs.readFile(target, "utf8")).trim();
187
+ }
188
+ function requiredId(value, label) {
189
+ if (typeof value !== "string" || !/^[0-9a-f-]{32,36}$/i.test(value))
190
+ throw new Error(`Invalid ${label}.`);
191
+ return value;
192
+ }
193
+ function requiredString(value, label) {
194
+ if (typeof value !== "string" || value.length === 0)
195
+ throw new Error(`Invalid ${label}.`);
196
+ return value;
197
+ }
198
+ function requiredLimits(value) {
199
+ if (!value || typeof value !== "object" || Array.isArray(value))
200
+ throw new Error("Invalid sandbox watchdog limits.");
201
+ const limits = value;
202
+ if (typeof limits.hardWallMs !== "number" || !Number.isSafeInteger(limits.hardWallMs) || limits.hardWallMs < 60_000 || limits.hardWallMs > 28_800_000
203
+ || typeof limits.idleMs !== "number" || !Number.isSafeInteger(limits.idleMs) || limits.idleMs < 60_000 || limits.idleMs > limits.hardWallMs) {
204
+ throw new Error("Invalid sandbox watchdog limits.");
205
+ }
206
+ return { hardWallMs: limits.hardWallMs, idleMs: limits.idleMs };
207
+ }
208
+ async function writeTerminationResult(resultPath, code) {
209
+ const message = terminationMessage(code);
210
+ const result = {
211
+ schemaVersion: 1,
212
+ exitCode: 124,
213
+ summary: message,
214
+ events: [{ role: "runner", kind: "error", content: message, eventType: code }]
215
+ };
216
+ const temporary = `${resultPath}.timeout.tmp`;
217
+ await fs.writeFile(temporary, `${JSON.stringify(result)}\n`, { mode: 0o600 });
218
+ await fs.rename(temporary, resultPath);
219
+ }
220
+ function emitTerminationEvent(code) {
221
+ const message = terminationMessage(code);
222
+ process.stdout.write(`${JSON.stringify({ type: "runner_event", event: { role: "runner", kind: "error", content: message, eventType: code } })}\n`);
223
+ process.stderr.write(`${code}: ${message}\n`);
224
+ }
225
+ main().then(code => { process.exitCode = code; }).catch(error => {
226
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
227
+ process.exitCode = 1;
228
+ });
@@ -0,0 +1,12 @@
1
+ import { GitClient } from "../shared/git/git.js";
2
+ import type { WorktreeService } from "../worktree/worktree.base.js";
3
+ import type { WorktreeHandle, WorktreePrepareRequest } from "../worktree/worktree.types.js";
4
+ export declare class SandboxWorkspaceService implements WorktreeService {
5
+ private readonly workspace;
6
+ private readonly bundlePath;
7
+ private readonly inputCommitSha;
8
+ private readonly git;
9
+ constructor(workspace?: string, bundlePath?: string, inputCommitSha?: string, git?: GitClient);
10
+ prepare(request: WorktreePrepareRequest): Promise<WorktreeHandle>;
11
+ release(_handle: WorktreeHandle): Promise<void>;
12
+ }
@@ -0,0 +1,49 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { GitClient } from "../shared/git/git.js";
4
+ import { injectWorktreeArtifacts, filterArtifactsForLane } from "../worktree/artifact-injector.js";
5
+ import { normalizeBranchName } from "../worktree/worktree.service.js";
6
+ export class SandboxWorkspaceService {
7
+ workspace;
8
+ bundlePath;
9
+ inputCommitSha;
10
+ git;
11
+ constructor(workspace = process.env.FORGE_WORKSPACE ?? "/workspace", bundlePath = process.env.FORGE_INPUT_BUNDLE ?? "/control/input/repo.bundle", inputCommitSha = process.env.FORGE_INPUT_COMMIT_SHA ?? "", git = new GitClient()) {
12
+ this.workspace = workspace;
13
+ this.bundlePath = bundlePath;
14
+ this.inputCommitSha = inputCommitSha;
15
+ this.git = git;
16
+ }
17
+ async prepare(request) {
18
+ if (!/^[0-9a-f]{40,64}$/i.test(this.inputCommitSha))
19
+ throw new Error("Sandbox input commit SHA is missing or invalid.");
20
+ const stat = await fs.stat(this.workspace).catch(() => null);
21
+ if (!stat?.isDirectory())
22
+ throw new Error("Sandbox workspace volume is unavailable.");
23
+ const entries = await fs.readdir(this.workspace);
24
+ if (entries.length > 0)
25
+ throw new Error("Sandbox workspace must be empty before bundle import.");
26
+ await this.git.run(path.dirname(this.workspace), ["clone", "--no-checkout", "--no-local", this.bundlePath, this.workspace]);
27
+ const actualSha = (await this.git.run(this.workspace, ["rev-parse", "--verify", `${this.inputCommitSha}^{commit}`])).trim();
28
+ if (actualSha.toLowerCase() !== this.inputCommitSha.toLowerCase())
29
+ throw new Error("Imported bundle does not contain the exact recorded input commit.");
30
+ const branchName = normalizeBranchName(request.worktreeKey, `forge/${request.workItem.id.replace(/-/g, "")}`);
31
+ await this.git.run(this.workspace, ["checkout", "-B", branchName, this.inputCommitSha]);
32
+ await this.git.run(this.workspace, ["config", "user.email", "forge-agent@forge.local"]);
33
+ await this.git.run(this.workspace, ["config", "user.name", "Forge Agent"]);
34
+ await this.git.run(this.workspace, ["config", "core.hooksPath", "/dev/null"]);
35
+ const artifacts = filterArtifactsForLane(request.artifacts, request.lane, request.phase);
36
+ if (artifacts.length > 0)
37
+ await injectWorktreeArtifacts(this.workspace, artifacts);
38
+ return {
39
+ repoPath: this.workspace,
40
+ worktreePath: this.workspace,
41
+ branchName,
42
+ baseBranch: request.baseBranch,
43
+ cleanupWorktrees: false
44
+ };
45
+ }
46
+ async release(_handle) {
47
+ // The host supervisor owns the workspace volume lifecycle.
48
+ }
49
+ }
@@ -0,0 +1,10 @@
1
+ import { type CompletionArtifactReference, type CompletionPhase, type CompletionToolServerConfig, type DecomposeCompletionGate, type DraftCompletionGate, type QaCompletionGate } from "./phase-completion.js";
2
+ import type { RunnerDispatchArtifact } from "./session.types.js";
3
+ export declare function writeDecomposeGateCompletionArtifact(config: CompletionToolServerConfig, gate: DecomposeCompletionGate, toolName: string, input: unknown): Promise<CompletionArtifactReference>;
4
+ export declare function writeDraftGateCompletionArtifact(config: CompletionToolServerConfig, gate: DraftCompletionGate, toolName: string, input: unknown): Promise<CompletionArtifactReference>;
5
+ export declare function writeCompletionArtifact(config: CompletionToolServerConfig, toolName: string, input: unknown): Promise<CompletionArtifactReference>;
6
+ export declare function writeArchitectCompletionArtifact(config: CompletionToolServerConfig, payload: Record<string, unknown>, status: string, producedBy: string): Promise<CompletionArtifactReference>;
7
+ export declare function writeQaGateCompletionArtifact(config: CompletionToolServerConfig, gate: QaCompletionGate, input: unknown): Promise<CompletionArtifactReference>;
8
+ export declare function writeQaCompletionArtifact(config: CompletionToolServerConfig, payload: Record<string, unknown>, status: string, producedBy: string): Promise<CompletionArtifactReference>;
9
+ export declare function readQaGateArtifact(config: CompletionToolServerConfig, gate: QaCompletionGate): Promise<Record<string, unknown>>;
10
+ export declare function readCompletionArtifact(worktreePath: string, workItemId: string, phase: CompletionPhase, ownerType?: string | null, ownerId?: string | null): Promise<RunnerDispatchArtifact | null>;