diff --git a/packages/adapter-utils/src/execution-target.ts b/packages/adapter-utils/src/execution-target.ts index 566d3fe4dd6..8709b590644 100644 --- a/packages/adapter-utils/src/execution-target.ts +++ b/packages/adapter-utils/src/execution-target.ts @@ -1721,6 +1721,20 @@ async function githubOperationLauncherBasePath( return remotePath; } +export const EXECUTION_TARGET_GIT_CONTEXT_UNAVAILABLE_CODE = "execution_target_git_context_unavailable"; + +function gitContextUnavailable(reason: string): Error { + return Object.assign(new Error(`Could not read execution-target Git context: ${reason}`), + { code: EXECUTION_TARGET_GIT_CONTEXT_UNAVAILABLE_CODE }); +} + +function gitContextProbeFailureReason(result: Pick): string { + if (result.timedOut) return "the probe timed out"; + if (typeof result.signal === "string" && /^SIG[A-Z0-9]{1,12}$/.test(result.signal)) return `the probe was stopped by ${result.signal}`; + if (Number.isSafeInteger(result.exitCode)) return `the probe exited with code ${result.exitCode}`; + return "the probe reported no exit status"; +} + /** Read only execution-target Git context; never import the controller's credentials into SSH. */ export async function prepareGitHubExecutionEnvironment(input: { target: AdapterExecutionTarget | null | undefined; @@ -1807,9 +1821,9 @@ printf '\0PAPERCLIP_GIT_CONTEXT_END\0' // workspaces can live at a different path on the execution target. cwd: remote.remoteCwd, timeoutMs: 15_000, }); - if (result.exitCode !== 0) throw new Error("Could not read execution-target Git context"); + if (result.exitCode !== 0) throw gitContextUnavailable(gitContextProbeFailureReason(result)); const payload = result.stdout.split("\0PAPERCLIP_GIT_CONTEXT_V1\0")[1]?.split("\0PAPERCLIP_GIT_CONTEXT_END\0")[0]; - if (payload === undefined) throw new Error("Could not read execution-target Git context"); + if (payload === undefined) throw gitContextUnavailable("the probe output was incomplete"); discovered = {}; const records = payload.split("\0"); const roots: string[] = []; @@ -1826,7 +1840,7 @@ printf '\0PAPERCLIP_GIT_CONTEXT_END\0' } else { const result = await promisify(execFile)(process.execPath, args, { cwd: input.cwd, timeout: 15_000, maxBuffer: 1024 * 1024 }); try { discovered = JSON.parse(result.stdout.split("\0")[1] ?? ""); } - catch { throw new Error("Could not read execution-target Git context"); } + catch { throw gitContextUnavailable("the probe output was not valid"); } } // Controller-derived roots and mode must not be replaced by agent bindings. return { ...discovered, ...input.env, diff --git a/packages/adapter-utils/src/github-launcher-environment.test.ts b/packages/adapter-utils/src/github-launcher-environment.test.ts index b678e33725c..7b2debf1d64 100644 --- a/packages/adapter-utils/src/github-launcher-environment.test.ts +++ b/packages/adapter-utils/src/github-launcher-environment.test.ts @@ -15,6 +15,7 @@ import { cleanupGitHubOperationLaunchers, prepareGitHubExecutionEnvironment, runAdapterExecutionTargetProcess, + EXECUTION_TARGET_GIT_CONTEXT_UNAVAILABLE_CODE, } from "./execution-target.js"; const exec = promisify(execFile); @@ -361,6 +362,37 @@ describe("managed GitHub launcher environment", () => { expect(fixture.runner.execute).toHaveBeenCalledWith(expect.objectContaining({ cwd: fixture.root })); }); + it.each([ + [{ exitCode: 2, signal: null, timedOut: false, stdout: "" }, "the probe exited with code 2"], + [{ exitCode: null, signal: null, timedOut: true, stdout: "" }, "the probe timed out"], + [{ exitCode: null, signal: "SIGKILL", timedOut: false, stdout: "" }, "the probe was stopped by SIGKILL"], + [{ exitCode: null, signal: null, timedOut: false, stdout: "" }, "the probe reported no exit status"], + [{ exitCode: 0, signal: null, timedOut: false, stdout: "login banner only" }, "the probe output was incomplete"], + ])("reports an incomplete remote Git context probe with a stable code: %o", async (outcome, reason) => { + const fixture = await sandbox("usr/bin"); + fixture.runner.execute.mockResolvedValue({ ...outcome, stderr: "fatal: GH_TOKEN=probe-secret /private/path", + pid: null, startedAt: new Date().toISOString() }); + + const failure = await prepareGitHubExecutionEnvironment({ + target: fixture.target, cwd: fixture.root, env: {}, hostCredentials: false, networkAccess: true, + }).catch((error: unknown) => error); + + expect(failure).toBeInstanceOf(Error); + expect(failure).toMatchObject({ code: EXECUTION_TARGET_GIT_CONTEXT_UNAVAILABLE_CODE, + message: `Could not read execution-target Git context: ${reason}` }); + expect((failure as Error).message).not.toMatch(/probe-secret|private|banner/); + }); + + it("leaves a runner that cannot execute the probe with its own error", async () => { + const fixture = await sandbox("usr/bin"); + const unavailable = Object.assign(new Error("Daytona exec failed"), { status: 503 }); + fixture.runner.execute.mockRejectedValue(unavailable); + + await expect(prepareGitHubExecutionEnvironment({ + target: fixture.target, cwd: fixture.root, env: {}, hostCredentials: false, networkAccess: true, + })).rejects.toBe(unavailable); + }); + it("reads Git metadata from the SSH workspace instead of an existing controller directory", async () => { const fixture = await sandbox("ssh-toolchain/bin"); // Use real Git for the probe, not the launcher fixture's stub.