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 <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip committed 2026-11-13 17:50:45 -08:00
1 parent bac360ae33
commit 5d0ebe543c
3 files changed
+37 -6

No files matched your search

+1
View File
@@ -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.
@@ -40,7 +40,7 @@ function localDuplexChannel(command: readonly string[], cwd: string): CommandMan
async function broker(resolveCredentials = vi.fn(async (binding: ReturnType<typeof run>) => ({
status: "available", env: { GH_TOKEN: `fixture-${binding.runId}` },
})), remote = false, bridgeUnavailable = false, onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>,
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<Awaited<ReturnType<typeof startAdapterExecutionTargetPaperclipBridge>>>[] = [];
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 });
@@ -35,6 +35,11 @@ export async function createNativeGitHubAccess(input: {
let ready = true;
let stopping: Promise<void> | undefined;
let bridge: Awaited<ReturnType<typeof startAdapterExecutionTargetPaperclipBridge>> = 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<void>(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<void>(resolve => server.close(() => resolve()));
await input.onLog?.("stderr", "[paperclip] GitHub runtime transport unavailable; continuing without managed GitHub access.\n").catch(() => undefined);
}