From 5d0ebe543cf3f5ad9020c9b8a4d0f4b45ef9f42d Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Fri, 13 Nov 2026 17:50:45 -0800 Subject: [PATCH] fix(runtime): stop GitHub bridges after failed startup probes A failed file-bridge verification left its queue worker polling until the native session retired. Stop and clear an unverified bridge before returning anonymous launchers, both for direct file startup and HTTP/2 fallback. Retain the handle if stopping throws so outer teardown can retry cleanup. Both new regression cases fail before the fix. They verify the gateway listener closes and remote polling stops before startup returns. The GitHub contract suites pass 88 tests, and the final native broker suite passes 23 tests. Server typechecking also passes. Co-Authored-By: Paperclip --- doc/execution-github-identity.md | 1 + .../native-github-access.test.ts | 28 +++++++++++++++++-- .../native-runtime/native-github-access.ts | 14 +++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/doc/execution-github-identity.md b/doc/execution-github-identity.md index da7881f47ab..ad32abd39d2 100644 --- a/doc/execution-github-identity.md +++ b/doc/execution-github-identity.md @@ -199,6 +199,7 @@ environment before publishing the launcher environment. The probe authenticates to the inactive broker and acquires no GitHub credential. If the HTTP/2 bridge fails this startup check, the controller closes it and tries the existing file bridge once. Both transports use the same live identity and credential resolver. +An unverified final bridge is stopped before anonymous launchers are returned. This startup fallback cannot replay an agent command. A later HTTP/2 loss still makes the warm session unavailable for reuse. diff --git a/server/src/services/native-runtime/native-github-access.test.ts b/server/src/services/native-runtime/native-github-access.test.ts index daa3e4099d5..c979240c15f 100644 --- a/server/src/services/native-runtime/native-github-access.test.ts +++ b/server/src/services/native-runtime/native-github-access.test.ts @@ -40,7 +40,7 @@ function localDuplexChannel(command: readonly string[], cwd: string): CommandMan async function broker(resolveCredentials = vi.fn(async (binding: ReturnType) => ({ status: "available", env: { GH_TOKEN: `fixture-${binding.runId}` }, })), remote = false, bridgeUnavailable = false, onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise, -duplex?: { enabled?: boolean; capability: boolean; unavailable?: boolean; startupFailure?: "lost" | "unreachable"; fallbackUnavailable?: boolean; recorder?: DuplexObservabilityRecorder }) { +duplex?: { enabled?: boolean; capability: boolean; unavailable?: boolean; startupFailure?: "lost" | "unreachable"; fallbackUnavailable?: boolean; fileProbeFails?: boolean; recorder?: DuplexObservabilityRecorder }) { const root = await mkdtemp(path.join(tmpdir(), "native-github-reuse-")); cleanups.push(() => rm(root, { recursive: true, force: true })); const bin = path.join(root, "real-bin"); @@ -68,12 +68,20 @@ duplex?: { enabled?: boolean; capability: boolean; unavailable?: boolean; startu duplexCommandStream: duplex.capability, runnerWebSocketIngress: false, } } : {}) } : null; const bridgeModes: (string | undefined)[] = []; + const bridges: NonNullable>>[] = []; const result = await createNativeGitHubAccess({ scope, target, cwd: root, env: { PATH: `${bin}:${path.dirname(process.execPath)}:/usr/bin:/bin`, PAPERCLIP_API_KEY: "old-run-api-key" }, resolveCredentials, onLog }, bridgeUnavailable ? async () => { throw new Error("fixture bridge unavailable"); } : async input => { if (duplex?.fallbackUnavailable && input.enableSandboxDuplexBridge === false) throw new Error("fixture fallback unavailable"); const bridge = await startAdapterExecutionTargetPaperclipBridge(input); bridgeModes.push(bridge?.env.PAPERCLIP_API_BRIDGE_MODE); + if (bridge) { + vi.spyOn(bridge, "stop"); + bridges.push(bridge); + if (duplex?.fileProbeFails && bridge.env.PAPERCLIP_API_BRIDGE_MODE === "queue_v1") { + bridge.env.PAPERCLIP_API_KEY = "fixture-invalid-bridge-token"; + } + } if (duplex?.startupFailure && bridge?.env.PAPERCLIP_API_BRIDGE_MODE === "http2_v1") { const channel = await openDuplexChannel.mock.results.at(-1)!.value; channel.stop(); @@ -86,7 +94,7 @@ duplex?: { enabled?: boolean; capability: boolean; unavailable?: boolean; startu return bridge; }); cleanups.push(result.stop); - return Object.assign(result, { root, resolveCredentials, execute, openDuplexChannel, bridgeModes }); + return Object.assign(result, { root, resolveCredentials, execute, openDuplexChannel, bridgeModes, bridges }); } function request(broker: NativeGitHubAccess, extra: RequestInit = {}, endpoint = "/runtime-tools/github/credentials") { return fetch(broker.env.PAPERCLIP_GITHUB_BROKER_URL + endpoint, { @@ -196,6 +204,22 @@ it("keeps host credentials isolated when neither GitHub startup transport works" expect(result.stdout).toBe("anonymous"); expect(b.resolveCredentials).not.toHaveBeenCalled(); }); +it.each([false, true])("stops failed file-bridge startup before returning anonymous launchers (HTTP/2 fallback: %s)", async fallback => { + const b = await broker(undefined, true, false, undefined, { + enabled: fallback, capability: true, ...(fallback ? { startupFailure: "lost" as const } : {}), fileProbeFails: true, + }); + const failedBridge = b.bridges.at(-1)!; + expect(b.ready).toBe(false); + expect(b.env.PAPERCLIP_GITHUB_BROKER_TOKEN).toBe(""); + expect(failedBridge.stop).toHaveBeenCalledOnce(); + await expect(fetch(failedBridge.env.PAPERCLIP_API_URL, { signal: AbortSignal.timeout(1000) })).rejects.toThrow(); + const commandsAfterStartup = b.execute.mock.calls.length; + await new Promise(resolve => setTimeout(resolve, 250)); + expect(b.execute.mock.calls).toHaveLength(commandsAfterStartup); + expect(b.resolveCredentials).not.toHaveBeenCalled(); + await b.stop(); + expect(failedBridge.stop).toHaveBeenCalledOnce(); +}); it("closes a healthy HTTP/2 bridge without reporting a lost active run", async () => { const onLog = vi.fn(async (_stream: "stdout" | "stderr", _chunk: string) => undefined); const b = await broker(undefined, true, false, onLog, { enabled: true, capability: true }); diff --git a/server/src/services/native-runtime/native-github-access.ts b/server/src/services/native-runtime/native-github-access.ts index 643de78d9e1..dccdf8fc7d6 100644 --- a/server/src/services/native-runtime/native-github-access.ts +++ b/server/src/services/native-runtime/native-github-access.ts @@ -35,6 +35,11 @@ export async function createNativeGitHubAccess(input: { let ready = true; let stopping: Promise | undefined; let bridge: Awaited> = null; + const stopBridge = async () => { + bridge?.markOrderlyCompletion?.(); + await bridge?.stop(); + bridge = null; + }; const server = createServer(async (req, res) => { res.setHeader("Cache-Control", "no-store"); res.setHeader("Content-Type", "application/json"); @@ -80,10 +85,9 @@ export async function createNativeGitHubAccess(input: { const stop = () => stopping ??= (async () => { stopped = true; active = null; - bridge?.markOrderlyCompletion?.(); server.closeAllConnections(); const results = await Promise.allSettled([ - bridge?.stop(), + stopBridge(), new Promise(resolve => server.close(() => resolve())), cleanupGitHubOperationLaunchers(location), ]); @@ -143,8 +147,7 @@ export async function createNativeGitHubAccess(input: { if (!duplexRequested || bridge?.env.PAPERCLIP_API_BRIDGE_MODE !== "http2_v1") throw error; // Only the inactive readiness probe can have crossed this bridge. // Replacing it cannot replay an agent command or change its identity. - await bridge.stop(); - bridge = null; + await stopBridge(); await input.onLog?.("stderr", "[paperclip] GitHub HTTP/2 startup check failed; retrying with the file bridge.\n").catch(() => undefined); bridge = await startVerifiedBridge(false); } @@ -152,6 +155,9 @@ export async function createNativeGitHubAccess(input: { // GitHub remains optional. Stage anonymous wrappers for this session; // the supervisor retries transport setup on the next run. ready = false; + // Stop an unverified transport now, including its remote queue polling. + // Retain ownership if cleanup throws so the outer teardown can retry it. + await stopBridge(); await new Promise(resolve => server.close(() => resolve())); await input.onLog?.("stderr", "[paperclip] GitHub runtime transport unavailable; continuing without managed GitHub access.\n").catch(() => undefined); }