@loucompanion/forge-bridge 0.1.3-dev.b3bce6ba65a6 → 0.1.3-dev.ba7d36726eda
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.
- package/README.md +78 -0
- package/dist/bridge/bridge.base.d.ts +5 -1
- package/dist/bridge/bridge.service.d.ts +52 -3
- package/dist/bridge/bridge.service.js +776 -19
- package/dist/bridge/bridge.types.d.ts +131 -1
- package/dist/cleanup/cleanup.service.d.ts +13 -2
- package/dist/cleanup/cleanup.service.js +435 -3
- package/dist/cleanup/cleanup.types.d.ts +10 -0
- package/dist/cleanup/index.d.ts +1 -1
- package/dist/cleanup/index.js +1 -1
- package/dist/cleanup/internals.d.ts +2 -2
- package/dist/cleanup/internals.js +1 -1
- package/dist/cli/cli.help.js +46 -0
- package/dist/cli/cli.service.d.ts +42 -2
- package/dist/cli/cli.service.js +350 -23
- package/dist/cli/local-config.d.ts +4 -0
- package/dist/cli/local-config.js +39 -0
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +15 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +50 -0
- package/dist/runtime/attempt-process-supervisor-registry.d.ts +20 -0
- package/dist/runtime/attempt-process-supervisor-registry.js +64 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +74 -0
- package/dist/runtime/attempt-process-supervisor.js +290 -0
- package/dist/runtime/host-listener-inspector.d.ts +28 -0
- package/dist/runtime/host-listener-inspector.js +185 -0
- package/dist/runtime/host-port-broker.d.ts +76 -0
- package/dist/runtime/host-port-broker.js +333 -0
- package/dist/runtime/host-runtime-contract.d.ts +39 -0
- package/dist/runtime/host-runtime-contract.js +295 -0
- package/dist/runtime/host-runtime-lease.d.ts +122 -0
- package/dist/runtime/host-runtime-lease.js +1241 -0
- package/dist/runtime/host-runtime-promotion.d.ts +21 -0
- package/dist/runtime/host-runtime-promotion.js +128 -0
- package/dist/runtime/node-host-runtime-adapter.d.ts +44 -0
- package/dist/runtime/node-host-runtime-adapter.js +205 -0
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +20 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +136 -0
- package/dist/runtime/runtime-isolation-lease.d.ts +67 -0
- package/dist/runtime/runtime-isolation-lease.js +158 -0
- package/dist/sandbox/command-runner.d.ts +4 -0
- package/dist/sandbox/command-runner.js +47 -0
- package/dist/sandbox/contract.d.ts +5 -0
- package/dist/sandbox/contract.js +276 -0
- package/dist/sandbox/event-budget.d.ts +11 -0
- package/dist/sandbox/event-budget.js +27 -0
- package/dist/sandbox/image-policy.d.ts +35 -0
- package/dist/sandbox/image-policy.js +247 -0
- package/dist/sandbox/index.d.ts +5 -0
- package/dist/sandbox/index.js +5 -0
- package/dist/sandbox/lease-watchdog.d.ts +18 -0
- package/dist/sandbox/lease-watchdog.js +67 -0
- package/dist/sandbox/preflight.d.ts +45 -0
- package/dist/sandbox/preflight.js +300 -0
- package/dist/sandbox/process.d.ts +20 -0
- package/dist/sandbox/process.js +209 -0
- package/dist/sandbox/provider-auth/codex.d.ts +26 -0
- package/dist/sandbox/provider-auth/codex.js +212 -0
- package/dist/sandbox/repository-lock.d.ts +5 -0
- package/dist/sandbox/repository-lock.js +119 -0
- package/dist/sandbox/runtime.d.ts +297 -0
- package/dist/sandbox/runtime.js +2338 -0
- package/dist/sandbox/sandbox.types.d.ts +86 -0
- package/dist/sandbox/sandbox.types.js +1 -0
- package/dist/sandbox/smoke.d.ts +76 -0
- package/dist/sandbox/smoke.js +380 -0
- package/dist/sandbox/worker-supervisor.d.ts +13 -0
- package/dist/sandbox/worker-supervisor.js +36 -0
- package/dist/sandbox/worker.d.ts +1 -0
- package/dist/sandbox/worker.js +225 -0
- package/dist/sandbox/workspace.service.d.ts +13 -0
- package/dist/sandbox/workspace.service.js +67 -0
- package/dist/session/phase-completion.artifacts.js +23 -2
- package/dist/session/phase-completion.helpers.js +6 -1
- package/dist/session/phase-completion.runtime.js +3 -1
- package/dist/session/provider-cli/provider-cli.artifacts.d.ts +1 -1
- package/dist/session/provider-cli/provider-cli.artifacts.js +28 -4
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic.d.ts +2 -0
- package/dist/session/provider-cli/provider-cli.deterministic.js +20 -9
- package/dist/session/provider-cli/provider-cli.process.d.ts +3 -0
- package/dist/session/provider-cli/provider-cli.process.js +65 -14
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +14 -2
- package/dist/session/provider-cli/provider-cli.prompts.js +99 -15
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +1 -1
- package/dist/session/provider-cli/provider-cli.runtime.js +3 -2
- package/dist/session/provider-cli/provider-cli.service.d.ts +4 -1
- package/dist/session/provider-cli/provider-cli.service.js +167 -28
- package/dist/session/provider-cli/provider-cli.types.d.ts +2 -0
- package/dist/session/session-redaction.d.ts +3 -0
- package/dist/session/session-redaction.js +23 -0
- package/dist/session/session.base.d.ts +4 -0
- package/dist/session/session.service.d.ts +6 -1
- package/dist/session/session.service.js +184 -38
- package/dist/session/session.types.d.ts +58 -1
- package/dist/session/session.types.js +172 -2
- package/dist/shared/path/path.js +3 -1
- package/dist/shared/process/host-process-recovery.d.ts +12 -0
- package/dist/shared/process/host-process-recovery.js +78 -0
- package/dist/shared/process/host-process-supervisor.d.ts +40 -0
- package/dist/shared/process/host-process-supervisor.js +195 -0
- package/dist/shared/provider-capabilities.d.ts +1 -0
- package/dist/shared/provider-capabilities.js +3 -0
- package/dist/shared/redaction/redaction.js +6 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/index.d.ts +1 -0
- package/dist/worktree/index.js +1 -0
- package/dist/worktree/prior-phase-artifact-hydrator.d.ts +2 -0
- package/dist/worktree/prior-phase-artifact-hydrator.js +136 -0
- package/dist/worktree/worktree-paths.d.ts +4 -0
- package/dist/worktree/worktree-paths.js +30 -0
- package/dist/worktree/worktree.base.d.ts +3 -0
- package/dist/worktree/worktree.service.d.ts +6 -2
- package/dist/worktree/worktree.service.js +60 -43
- package/dist/worktree/worktree.types.d.ts +3 -1
- package/package.json +6 -3
- package/runtime/windows-job-supervisor.ps1 +287 -0
- package/runtime/windows-listener-owner.ps1 +33 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { captureHostProcessIdentity } from "./host-process-supervisor.js";
|
|
2
|
+
/** Exact, birth-marker-fenced recovery for a process tree left by an old bridge. */
|
|
3
|
+
export class ExactHostProcessRecovery {
|
|
4
|
+
platform;
|
|
5
|
+
graceMs;
|
|
6
|
+
constructor(platform = process.platform, graceMs = 2_000) {
|
|
7
|
+
this.platform = platform;
|
|
8
|
+
this.graceMs = graceMs;
|
|
9
|
+
}
|
|
10
|
+
async recover(metadata) {
|
|
11
|
+
if (!processExists(metadata.rootPid))
|
|
12
|
+
return "absent";
|
|
13
|
+
if (!metadata.rootIdentity)
|
|
14
|
+
return "unverifiable";
|
|
15
|
+
const currentIdentity = captureHostProcessIdentity(metadata.rootPid, this.platform);
|
|
16
|
+
if (!currentIdentity)
|
|
17
|
+
return processExists(metadata.rootPid) ? "unverifiable" : "absent";
|
|
18
|
+
if (currentIdentity !== metadata.rootIdentity)
|
|
19
|
+
return "pid_reused";
|
|
20
|
+
if (this.platform === "win32") {
|
|
21
|
+
signalExactProcess(metadata.rootPid, "SIGTERM");
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
signalExactProcessGroup(metadata.rootPid, "SIGTERM");
|
|
25
|
+
}
|
|
26
|
+
await waitForExit(metadata.rootPid, this.graceMs);
|
|
27
|
+
if (processExists(metadata.rootPid)) {
|
|
28
|
+
if (captureHostProcessIdentity(metadata.rootPid, this.platform) !== metadata.rootIdentity) {
|
|
29
|
+
return "pid_reused";
|
|
30
|
+
}
|
|
31
|
+
if (this.platform === "win32") {
|
|
32
|
+
signalExactProcess(metadata.rootPid, "SIGKILL");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
signalExactProcessGroup(metadata.rootPid, "SIGKILL");
|
|
36
|
+
}
|
|
37
|
+
await waitForExit(metadata.rootPid, this.graceMs);
|
|
38
|
+
}
|
|
39
|
+
if (processExists(metadata.rootPid)
|
|
40
|
+
&& captureHostProcessIdentity(metadata.rootPid, this.platform) === metadata.rootIdentity) {
|
|
41
|
+
throw new Error("host_process_recovery_degraded: exact supervised process tree did not stop.");
|
|
42
|
+
}
|
|
43
|
+
return "terminated";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function processExists(pid) {
|
|
47
|
+
try {
|
|
48
|
+
process.kill(pid, 0);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
return error.code === "EPERM";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function signalExactProcess(pid, signal) {
|
|
56
|
+
try {
|
|
57
|
+
process.kill(pid, signal);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error.code !== "ESRCH")
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function signalExactProcessGroup(pid, signal) {
|
|
65
|
+
try {
|
|
66
|
+
process.kill(-pid, signal);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code !== "ESRCH")
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function waitForExit(pid, timeoutMs) {
|
|
74
|
+
const deadline = Date.now() + timeoutMs;
|
|
75
|
+
while (Date.now() < deadline && processExists(pid)) {
|
|
76
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
export type HostProcessSupervisorStrategy = "windows_job_object" | "unix_process_group";
|
|
3
|
+
export interface HostProcessSupervisionRequest {
|
|
4
|
+
readonly command: string;
|
|
5
|
+
readonly args: readonly string[];
|
|
6
|
+
readonly cwd: string;
|
|
7
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
8
|
+
}
|
|
9
|
+
export interface HostProcessSupervisedLaunch {
|
|
10
|
+
readonly command: string;
|
|
11
|
+
readonly args: readonly string[];
|
|
12
|
+
readonly detached: boolean;
|
|
13
|
+
readonly supervisorId: string;
|
|
14
|
+
readonly strategy: HostProcessSupervisorStrategy;
|
|
15
|
+
readonly metadataPath: string;
|
|
16
|
+
readonly commandDigest: string;
|
|
17
|
+
}
|
|
18
|
+
export interface HostProcessSupervisorMetadata {
|
|
19
|
+
readonly schemaVersion: 1;
|
|
20
|
+
readonly supervisorId: string;
|
|
21
|
+
readonly strategy: HostProcessSupervisorStrategy;
|
|
22
|
+
readonly rootPid: number;
|
|
23
|
+
readonly rootCreatedAt: string;
|
|
24
|
+
readonly rootIdentity?: string | null;
|
|
25
|
+
readonly commandDigest: string;
|
|
26
|
+
readonly worktreePath: string;
|
|
27
|
+
readonly runtimeLeaseId: string;
|
|
28
|
+
readonly dispatchAttemptId: string;
|
|
29
|
+
readonly fencingToken: string;
|
|
30
|
+
readonly bridgePid: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function resolveHostProcessSupervisedLaunch(request: HostProcessSupervisionRequest, resolvedCommand: string, resolvedArgs: readonly string[], platform?: NodeJS.Platform): HostProcessSupervisedLaunch | null;
|
|
33
|
+
export declare function persistHostProcessSupervisorMetadata(launch: HostProcessSupervisedLaunch, request: HostProcessSupervisionRequest, rootPid: number, rootCreatedAt: string): HostProcessSupervisorMetadata;
|
|
34
|
+
/**
|
|
35
|
+
* Captures an OS-issued process birth marker. Recovery compares this marker
|
|
36
|
+
* before signalling a persisted PID so PID reuse can never redirect cleanup
|
|
37
|
+
* at an unrelated process.
|
|
38
|
+
*/
|
|
39
|
+
export declare function captureHostProcessIdentity(pid: number, platform?: NodeJS.Platform): string | null;
|
|
40
|
+
export declare function terminateHostProcessTree(child: ChildProcess, launch: HostProcessSupervisedLaunch | null, graceMs?: number): Promise<void>;
|
|
@@ -0,0 +1,195 @@
|
|
|
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 payload = Buffer.from(JSON.stringify({
|
|
23
|
+
command: resolvedCommand,
|
|
24
|
+
args: resolvedArgs,
|
|
25
|
+
cwd: request.cwd,
|
|
26
|
+
parentPid: process.pid,
|
|
27
|
+
supervisorId,
|
|
28
|
+
}), "utf8").toString("base64");
|
|
29
|
+
return {
|
|
30
|
+
command: windowsPowerShellPath(),
|
|
31
|
+
args: [
|
|
32
|
+
"-NoLogo",
|
|
33
|
+
"-NoProfile",
|
|
34
|
+
"-NonInteractive",
|
|
35
|
+
"-ExecutionPolicy",
|
|
36
|
+
"Bypass",
|
|
37
|
+
"-File",
|
|
38
|
+
windowsJobSupervisorPath,
|
|
39
|
+
"-Mode",
|
|
40
|
+
"Outer",
|
|
41
|
+
"-Payload",
|
|
42
|
+
payload,
|
|
43
|
+
],
|
|
44
|
+
detached: false,
|
|
45
|
+
supervisorId,
|
|
46
|
+
strategy: "windows_job_object",
|
|
47
|
+
metadataPath,
|
|
48
|
+
commandDigest,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
command: resolvedCommand,
|
|
53
|
+
args: resolvedArgs,
|
|
54
|
+
detached: true,
|
|
55
|
+
supervisorId,
|
|
56
|
+
strategy: "unix_process_group",
|
|
57
|
+
metadataPath,
|
|
58
|
+
commandDigest,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function persistHostProcessSupervisorMetadata(launch, request, rootPid, rootCreatedAt) {
|
|
62
|
+
if (!Number.isSafeInteger(rootPid) || rootPid <= 0) {
|
|
63
|
+
throw new Error("host_process_supervisor_invalid: supervised root PID was not assigned.");
|
|
64
|
+
}
|
|
65
|
+
const env = request.env ?? {};
|
|
66
|
+
const metadata = {
|
|
67
|
+
schemaVersion: 1,
|
|
68
|
+
supervisorId: launch.supervisorId,
|
|
69
|
+
strategy: launch.strategy,
|
|
70
|
+
rootPid,
|
|
71
|
+
rootCreatedAt,
|
|
72
|
+
rootIdentity: captureHostProcessIdentity(rootPid),
|
|
73
|
+
commandDigest: launch.commandDigest,
|
|
74
|
+
worktreePath: request.cwd,
|
|
75
|
+
runtimeLeaseId: requiredEnvironment(env, "FORGE_RESOURCE_LEASE_ID"),
|
|
76
|
+
dispatchAttemptId: requiredEnvironment(env, "FORGE_DISPATCH_ATTEMPT_ID"),
|
|
77
|
+
fencingToken: requiredEnvironment(env, "FORGE_FENCING_TOKEN"),
|
|
78
|
+
bridgePid: process.pid,
|
|
79
|
+
};
|
|
80
|
+
fs.writeFileSync(launch.metadataPath, `${JSON.stringify(metadata)}\n`, {
|
|
81
|
+
encoding: "utf8",
|
|
82
|
+
flag: "wx",
|
|
83
|
+
mode: 0o600,
|
|
84
|
+
});
|
|
85
|
+
return metadata;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Captures an OS-issued process birth marker. Recovery compares this marker
|
|
89
|
+
* before signalling a persisted PID so PID reuse can never redirect cleanup
|
|
90
|
+
* at an unrelated process.
|
|
91
|
+
*/
|
|
92
|
+
export function captureHostProcessIdentity(pid, platform = process.platform) {
|
|
93
|
+
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
94
|
+
return null;
|
|
95
|
+
try {
|
|
96
|
+
if (platform === "linux") {
|
|
97
|
+
const bootId = fs.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim();
|
|
98
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
99
|
+
const close = stat.lastIndexOf(")");
|
|
100
|
+
const fields = close >= 0 ? stat.slice(close + 2).trim().split(/\s+/u) : [];
|
|
101
|
+
const startTicks = fields[19];
|
|
102
|
+
return bootId && startTicks && /^\d+$/u.test(startTicks)
|
|
103
|
+
? `linux:${bootId}:${startTicks}`
|
|
104
|
+
: null;
|
|
105
|
+
}
|
|
106
|
+
if (platform === "win32") {
|
|
107
|
+
const output = execFileSync(windowsPowerShellPath(), [
|
|
108
|
+
"-NoLogo",
|
|
109
|
+
"-NoProfile",
|
|
110
|
+
"-NonInteractive",
|
|
111
|
+
"-Command",
|
|
112
|
+
`(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToUniversalTime().Ticks`,
|
|
113
|
+
], {
|
|
114
|
+
encoding: "utf8",
|
|
115
|
+
windowsHide: true,
|
|
116
|
+
timeout: 2_000,
|
|
117
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
118
|
+
}).trim();
|
|
119
|
+
return /^\d+$/u.test(output) ? `win32:${output}` : null;
|
|
120
|
+
}
|
|
121
|
+
// Other Unix variants do not expose a sufficiently strong birth marker
|
|
122
|
+
// through Node. Fail closed rather than trust a second-resolution ps time.
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export async function terminateHostProcessTree(child, launch, graceMs = 2_000) {
|
|
130
|
+
if (!launch) {
|
|
131
|
+
try {
|
|
132
|
+
child.kill("SIGTERM");
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// The process already settled.
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (launch.strategy === "windows_job_object") {
|
|
140
|
+
try {
|
|
141
|
+
child.kill("SIGTERM");
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
// Closing the launcher is best effort when the Job Object already settled.
|
|
145
|
+
}
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const processGroupId = child.pid;
|
|
149
|
+
if (!processGroupId || !signalUnixProcessGroup(processGroupId, "SIGTERM"))
|
|
150
|
+
return;
|
|
151
|
+
await new Promise(resolve => setTimeout(resolve, graceMs));
|
|
152
|
+
if (unixProcessGroupExists(processGroupId)) {
|
|
153
|
+
signalUnixProcessGroup(processGroupId, "SIGKILL");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function requiredEnvironment(env, name) {
|
|
157
|
+
const value = env[name]?.trim();
|
|
158
|
+
if (!value)
|
|
159
|
+
throw new Error(`host_process_supervisor_invalid: ${name} is required.`);
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
function assertMetadataPath(logDirectory, metadataPath) {
|
|
163
|
+
const root = path.resolve(logDirectory);
|
|
164
|
+
const target = path.resolve(metadataPath);
|
|
165
|
+
if (target !== root && !target.startsWith(`${root}${path.sep}`)) {
|
|
166
|
+
throw new Error("host_process_supervisor_invalid: metadata path must be inside FORGE_LOG_DIR.");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function windowsPowerShellPath() {
|
|
170
|
+
const systemRoot = process.env.SystemRoot?.trim() || "C:\\Windows";
|
|
171
|
+
const candidate = path.win32.join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
172
|
+
return fs.existsSync(candidate) ? candidate : "powershell.exe";
|
|
173
|
+
}
|
|
174
|
+
function signalUnixProcessGroup(processGroupId, signal) {
|
|
175
|
+
try {
|
|
176
|
+
process.kill(-processGroupId, signal);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (error.code === "ESRCH")
|
|
181
|
+
return false;
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function unixProcessGroupExists(processGroupId) {
|
|
186
|
+
try {
|
|
187
|
+
process.kill(-processGroupId, 0);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
if (error.code === "ESRCH")
|
|
192
|
+
return false;
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function supportsDraftImageAttachmentsFor(cliProviders: readonly string[]): boolean;
|
|
@@ -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(
|
|
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.
|
|
1
|
+
export declare const forgeBridgeVersion = "0.1.3-dev.ba7d36726eda";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const forgeBridgeVersion = "0.1.3-dev.
|
|
1
|
+
export const forgeBridgeVersion = "0.1.3-dev.ba7d36726eda";
|
package/dist/worktree/index.d.ts
CHANGED
|
@@ -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";
|
package/dist/worktree/index.js
CHANGED
|
@@ -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,136 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isSubPath } from "../shared/path/path.js";
|
|
5
|
+
const MAX_ARTIFACT_COUNT = 128;
|
|
6
|
+
const MAX_CONTENT_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
const MAX_TOTAL_BYTES = 12 * 1024 * 1024;
|
|
8
|
+
const phaseFiles = {
|
|
9
|
+
implementation: {
|
|
10
|
+
verdict: "implementation.completeness.json",
|
|
11
|
+
report: "implementation.report.md"
|
|
12
|
+
},
|
|
13
|
+
self_review: {
|
|
14
|
+
verdict: "self-review.report.json",
|
|
15
|
+
report: "self-review.summary.md"
|
|
16
|
+
},
|
|
17
|
+
validation: {
|
|
18
|
+
verdict: "validation.verdict.json",
|
|
19
|
+
report: "validation.report.md"
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export async function hydratePriorPhaseArtifacts(worktreePath, artifacts = []) {
|
|
23
|
+
if (artifacts.length === 0)
|
|
24
|
+
return 0;
|
|
25
|
+
if (artifacts.length > MAX_ARTIFACT_COUNT) {
|
|
26
|
+
throw new Error(`Prior phase artifact handoff exceeds ${MAX_ARTIFACT_COUNT} artifacts.`);
|
|
27
|
+
}
|
|
28
|
+
const targets = new Set();
|
|
29
|
+
let totalBytes = 0;
|
|
30
|
+
for (const artifact of artifacts) {
|
|
31
|
+
const workItemId = safeArtifactSegment(artifact.workItemId, "work item id");
|
|
32
|
+
const files = phaseFiles[artifact.phase];
|
|
33
|
+
if (!files) {
|
|
34
|
+
throw new Error(`Unsupported prior phase artifact phase '${artifact.phase}'.`);
|
|
35
|
+
}
|
|
36
|
+
const verdictBytes = Buffer.byteLength(artifact.verdictJson, "utf8");
|
|
37
|
+
const reportBytes = Buffer.byteLength(artifact.report, "utf8");
|
|
38
|
+
if (verdictBytes > MAX_CONTENT_BYTES || reportBytes > MAX_CONTENT_BYTES) {
|
|
39
|
+
throw new Error(`Prior phase artifact '${artifact.artifactId}' exceeds the per-file hydration limit.`);
|
|
40
|
+
}
|
|
41
|
+
totalBytes += verdictBytes + reportBytes;
|
|
42
|
+
if (totalBytes > MAX_TOTAL_BYTES) {
|
|
43
|
+
throw new Error("Prior phase artifact handoff exceeds the total hydration limit.");
|
|
44
|
+
}
|
|
45
|
+
verifyContentHash(artifact.verdictJson, artifact.verdictContentHash, artifact.artifactId, "verdict");
|
|
46
|
+
verifyContentHash(artifact.report, artifact.reportContentHash, artifact.artifactId, "report");
|
|
47
|
+
validateVerdict(artifact);
|
|
48
|
+
const artifactRoot = path.resolve(worktreePath, ".forge", "work-items", workItemId, "artifacts");
|
|
49
|
+
if (!isSubPath(worktreePath, artifactRoot)) {
|
|
50
|
+
throw new Error("Prior phase artifact target must stay under the worktree.");
|
|
51
|
+
}
|
|
52
|
+
await ensureNoSymlinkPathSegments(worktreePath, artifactRoot);
|
|
53
|
+
await fs.mkdir(artifactRoot, { recursive: true });
|
|
54
|
+
await ensureNoSymlinkPathSegments(worktreePath, artifactRoot);
|
|
55
|
+
const verdictPath = path.join(artifactRoot, files.verdict);
|
|
56
|
+
const reportPath = path.join(artifactRoot, files.report);
|
|
57
|
+
const targetKey = verdictPath.toLowerCase();
|
|
58
|
+
if (targets.has(targetKey)) {
|
|
59
|
+
throw new Error(`Duplicate prior phase artifact target for work item '${workItemId}' phase '${artifact.phase}'.`);
|
|
60
|
+
}
|
|
61
|
+
targets.add(targetKey);
|
|
62
|
+
await writeAtomic(verdictPath, artifact.verdictJson);
|
|
63
|
+
await writeAtomic(reportPath, artifact.report);
|
|
64
|
+
}
|
|
65
|
+
return artifacts.length;
|
|
66
|
+
}
|
|
67
|
+
function validateVerdict(artifact) {
|
|
68
|
+
let parsed;
|
|
69
|
+
try {
|
|
70
|
+
parsed = JSON.parse(artifact.verdictJson);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
throw new Error(`Prior phase artifact '${artifact.artifactId}' verdict is not valid JSON.`);
|
|
74
|
+
}
|
|
75
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
76
|
+
throw new Error(`Prior phase artifact '${artifact.artifactId}' verdict must be a JSON object.`);
|
|
77
|
+
}
|
|
78
|
+
const recordedPhase = parsed["phase"];
|
|
79
|
+
const normalizedPhase = typeof recordedPhase === "string"
|
|
80
|
+
? recordedPhase.trim().toLowerCase().replace(/-/g, "_")
|
|
81
|
+
: "";
|
|
82
|
+
if (normalizedPhase !== artifact.phase) {
|
|
83
|
+
throw new Error(`Prior phase artifact '${artifact.artifactId}' phase does not match its verdict.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function verifyContentHash(content, expected, artifactId, label) {
|
|
87
|
+
const normalizedExpected = expected.trim().toLowerCase().replace(/^sha256:/, "");
|
|
88
|
+
if (!/^[0-9a-f]{64}$/.test(normalizedExpected)) {
|
|
89
|
+
throw new Error(`Prior phase artifact '${artifactId}' ${label} hash is invalid.`);
|
|
90
|
+
}
|
|
91
|
+
const actual = createHash("sha256").update(content, "utf8").digest("hex");
|
|
92
|
+
if (actual !== normalizedExpected) {
|
|
93
|
+
throw new Error(`Prior phase artifact '${artifactId}' ${label} content_hash mismatch.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function safeArtifactSegment(value, label) {
|
|
97
|
+
const trimmed = value.trim();
|
|
98
|
+
if (!/^[a-zA-Z0-9_-]{1,128}$/.test(trimmed)) {
|
|
99
|
+
throw new Error(`Prior phase artifact ${label} is not a safe path segment.`);
|
|
100
|
+
}
|
|
101
|
+
return trimmed;
|
|
102
|
+
}
|
|
103
|
+
async function writeAtomic(targetPath, content) {
|
|
104
|
+
const temporaryPath = `${targetPath}.tmp-${randomUUID()}`;
|
|
105
|
+
try {
|
|
106
|
+
await fs.writeFile(temporaryPath, content, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
|
107
|
+
await fs.rename(temporaryPath, targetPath);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
await fs.rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function ensureNoSymlinkPathSegments(root, targetPath) {
|
|
114
|
+
const resolvedRoot = path.resolve(root);
|
|
115
|
+
const resolvedTarget = path.resolve(targetPath);
|
|
116
|
+
const relative = path.relative(resolvedRoot, resolvedTarget);
|
|
117
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
118
|
+
throw new Error("Prior phase artifact path must stay under the worktree.");
|
|
119
|
+
}
|
|
120
|
+
let current = resolvedRoot;
|
|
121
|
+
for (const segment of relative.split(path.sep)) {
|
|
122
|
+
if (!segment)
|
|
123
|
+
continue;
|
|
124
|
+
current = path.join(current, segment);
|
|
125
|
+
const stat = await fs.lstat(current).catch((error) => {
|
|
126
|
+
if (error.code === "ENOENT")
|
|
127
|
+
return null;
|
|
128
|
+
throw error;
|
|
129
|
+
});
|
|
130
|
+
if (!stat)
|
|
131
|
+
return;
|
|
132
|
+
if (stat.isSymbolicLink()) {
|
|
133
|
+
throw new Error("Prior phase artifact path must not traverse symlinked path segments.");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function managedRepoRoot(allowedRoot: string): string;
|
|
2
|
+
export declare function resolveManagedWorktreeRoot(allowedRoot: string, repoPath: string): string;
|
|
3
|
+
export declare function resolveManagedWorktreeRootForRepo(repoPath: string, allowedRoot?: string): string;
|
|
4
|
+
export declare function defaultWorktreeCoordinatorStateRoot(): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isSubPath } from "../shared/path/path.js";
|
|
4
|
+
export function managedRepoRoot(allowedRoot) {
|
|
5
|
+
return path.join(path.resolve(allowedRoot), ".forge-worktrees", "_repos");
|
|
6
|
+
}
|
|
7
|
+
export function resolveManagedWorktreeRoot(allowedRoot, repoPath) {
|
|
8
|
+
const resolvedAllowedRoot = path.resolve(allowedRoot);
|
|
9
|
+
const resolvedRepoPath = path.resolve(repoPath);
|
|
10
|
+
const baseName = path.basename(resolvedRepoPath);
|
|
11
|
+
return isSubPath(managedRepoRoot(resolvedAllowedRoot), resolvedRepoPath)
|
|
12
|
+
? path.join(resolvedAllowedRoot, ".forge-worktrees", baseName)
|
|
13
|
+
: path.join(path.dirname(resolvedRepoPath), ".forge-worktrees", baseName);
|
|
14
|
+
}
|
|
15
|
+
export function resolveManagedWorktreeRootForRepo(repoPath, allowedRoot) {
|
|
16
|
+
const resolvedRepoPath = path.resolve(repoPath);
|
|
17
|
+
if (allowedRoot && isSubPath(path.resolve(allowedRoot), resolvedRepoPath)) {
|
|
18
|
+
return resolveManagedWorktreeRoot(allowedRoot, resolvedRepoPath);
|
|
19
|
+
}
|
|
20
|
+
const repoParent = path.dirname(resolvedRepoPath);
|
|
21
|
+
const managedRoot = path.dirname(repoParent);
|
|
22
|
+
if (path.basename(repoParent) === "_repos" && path.basename(managedRoot) === ".forge-worktrees") {
|
|
23
|
+
return path.join(managedRoot, path.basename(resolvedRepoPath));
|
|
24
|
+
}
|
|
25
|
+
return path.join(repoParent, ".forge-worktrees", path.basename(resolvedRepoPath));
|
|
26
|
+
}
|
|
27
|
+
export function defaultWorktreeCoordinatorStateRoot() {
|
|
28
|
+
return process.env.FORGE_SANDBOX_STATE_ROOT
|
|
29
|
+
?? path.join(os.homedir(), ".forge", "bridge", "sandboxes");
|
|
30
|
+
}
|
|
@@ -3,3 +3,6 @@ export interface WorktreeService {
|
|
|
3
3
|
prepare(request: WorktreePrepareRequest): Promise<WorktreeHandle>;
|
|
4
4
|
release(handle: WorktreeHandle): Promise<void>;
|
|
5
5
|
}
|
|
6
|
+
export interface WorktreeLifecycleCoordinator {
|
|
7
|
+
withLock<T>(repoPath: string, action: () => Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
8
|
+
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { GitClient } from "../shared/git/git.js";
|
|
2
|
-
import type { WorktreeService } from "./worktree.base.js";
|
|
2
|
+
import type { WorktreeLifecycleCoordinator, WorktreeService } from "./worktree.base.js";
|
|
3
3
|
import type { WorktreeHandle, WorktreePrepareRequest } from "./worktree.types.js";
|
|
4
4
|
export declare class GitWorktreeService implements WorktreeService {
|
|
5
5
|
private readonly git;
|
|
6
|
-
|
|
6
|
+
private readonly coordinator;
|
|
7
|
+
constructor(git?: GitClient, coordinator?: WorktreeLifecycleCoordinator);
|
|
7
8
|
prepare(request: WorktreePrepareRequest): Promise<WorktreeHandle>;
|
|
9
|
+
private repoCreationCoordinationPath;
|
|
8
10
|
release(handle: WorktreeHandle): Promise<void>;
|
|
9
11
|
private resolveRepoPath;
|
|
12
|
+
private managedRepoPath;
|
|
10
13
|
private findExistingRepoPath;
|
|
11
14
|
private resolveMatchingRepo;
|
|
12
15
|
private resolveBaseStartPoint;
|
|
@@ -15,4 +18,5 @@ export declare class GitWorktreeService implements WorktreeService {
|
|
|
15
18
|
private currentBranch;
|
|
16
19
|
private ensureGitIdentity;
|
|
17
20
|
}
|
|
21
|
+
export declare function clearRunArtifactRoots(worktreePath: string, request: WorktreePrepareRequest): Promise<void>;
|
|
18
22
|
export declare function normalizeBranchName(value: string | null | undefined, fallback: string): string;
|