mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 18:26:40 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Native runs give agents managed GitHub access through `gh` and `git` launchers on the provider's `PATH` > - A provider process can keep running while the server restarts, and the next controller adopts it > - The detaching controller deleted the launcher directory, and the adopting controller staged launchers in a new random directory > - The running provider still names the old directory, so every later `gh` or `git` call ran without credentials > - This pull request keeps the launcher directory stable for one provider session and task, and re-arms it after the restart > - The benefit is that a long run keeps its GitHub identity across a server deploy or restart ## Linked Issues or Issue Description No public issue exists. This describes the bug in-PR, with the bug template labels. **What happened?** An agent run on a Daytona sandbox used `gh` with managed GitHub access. The server restarted during the run (a rolling deploy), and the controller re-attached to the live provider process. After that, every `gh` call failed with `You are not logged into any GitHub hosts`. `git credential fill` returned no token. The agent reported that its token had "expired" and used the unauthenticated public API for the rest of the run. **Steps to reproduce** 1. Start a native run with managed GitHub access on a remote sandbox. 2. Run a `gh` command in the run. It succeeds. 3. Restart the server while the provider process is still running. The new controller re-attaches to the process. 4. Run a `gh` command again in the same run. It fails with "not logged in", and no `Paperclip: GitHub ...` diagnostic appears. **Expected behavior** After the new controller adopts the provider, `gh` and `git` get credentials from the new controller, as they did before the restart. **Root cause** - `createNativeGitHubAccess` staged the launchers in `native-session-<random UUID>` and put that path in the provider's `PATH`, `BASH_ENV`, and `GH_CONFIG_DIR`. - On a restart detach, `githubAccess.stop()` ran `rm -rf` on that directory (`detachReleasedWarmNativeSession`, and the run's final `github_stop` while `nativeRunsDetachingForRestart` is set). - The adopting controller made a new random directory. The provider environment cannot change after spawn, so the provider never saw it. - The shims were gone. `gh` resolved to the real binary with an empty `GH_CONFIG_DIR`, so no Paperclip diagnostic appeared. Refs #15354. That PR keeps ACPX sessions across launcher rotation in the legacy adapter path. This PR covers the native runtime path. ## What Changed - `createNativeGitHubAccess` takes an optional `launcherKey`. The launcher directory is `native-github-<sha256(key)>`, so the same provider session and task always get the same directory. - The native session executor passes a key built from `nativeSessionScopeKey` and the issue id. - `prepareGitHubOperationLaunchers` takes an optional `broker`. It then stages broker-file launchers and an owner-only `broker.json`. The launchers already read `broker.json` on each operation in this mode (`githubLauncherSource({ brokerFile: true })`). A restaged directory therefore gives the running provider the new endpoint. - Native access passes `broker` only for remote sandbox targets. A sandbox provider is the one that outlives a controller restart. Local launchers live in the shared host temp directory, so they keep the capability in the provider environment only, as before. - `stop({ preserveLaunchers: true })` keeps the launcher directory. A restart detach of an active run and of a released warm session use this option. A normal stop still removes the directory. - A final stop does not remove a directory that a newer access in the same process has already restaged. - The callback bridge runtime stays in its own per-access directory. A new bridge never shares a queue directory with an old bridge. - `syncRemoteTextFiles` takes a `private` flag. It sets `umask 077` for the whole staging script, so the upload copy, the partial file, and the final file are never readable by group or other users. Local launcher files are written to a temporary file and renamed, so a running launcher never reads a partly written file. ## Verification - New `native-github-access` test on a sandbox target: a live parent process gets credentials, the access stops with `preserveLaunchers`, and a new access with the same key re-arms the directory. The same parent PID then gets the new controller's credentials, with empty stderr. `broker.json` has mode `600`. The final stop removes the directory. - New previous-release cutover test: a provider launched with the old random-directory layout keeps working without managed GitHub after its controller removes the directory. The new controller does not resolve credentials for it. A provider that the new controller launches gets credentials. - New local regression: local launcher files do not contain the broker capability, and there is no `broker.json`. - New local restart test: a local provider adopted after a restart gets a Paperclip diagnostic and no credentials. The new controller resolves nothing for the old endpoint. - New `sandbox-callback-bridge` test: staging a private file runs with umask `077` and leaves the file at mode `600`. - New `native-github-access` test: when a superseded access stops, its successor's launchers stay usable. A different key gives a different directory. - New `native-session-executor` test: a run detached for restart while active stops its GitHub access with `{ preserveLaunchers: true }`, and creates it with a `launcherKey`. The existing overlapping-failure test now asserts `{ preserveLaunchers: false }`. - The new tests fail on the old behavior. I checked this by forcing `brokerFile: false`, forcing `preserveLaunchers = false`, removing the `umask 077` line, and writing `broker.json` for local targets. - Passed: `native-github-access` (26), `native-session-executor` + `github-operation-credentials` + `heartbeat-github-launchers` + `native-stop-accounting` + `heartbeat-project-env` (1640 passed, 7 skipped), `native-runner-restart-recovery.integration` and `heartbeat-native-restart-suspension`. - Passed: adapter-utils `execution-target`, `github-launcher`, `github-launcher-environment`, `sandbox-callback-bridge`; runner `acpx/github-launcher`, `acpx/environment`, `codex/codex-security-config`. - `pnpm --filter @paperclipai/adapter-utils typecheck` and the server `pnpm typecheck` pass. - Not run locally: `native-warm-checkpoint-ownership.integration` needs the compiled `paperclip-runnerd` binary. CI builds it. ## Previous-release story - No persisted run, session, checkpoint, or database state changes. Only the in-sandbox launcher layout changes. - A provider launched by the previous release has a random `native-session-*` launcher directory in its environment. The previous release's controller removes that directory at the cutover restart. The new controller cannot find it, so it does not re-arm it. - That provider carries forward without managed GitHub until its next spawn, which is the current behavior. The run is not refused and is not retried. `gh` and `git` run without managed credentials, as they do today after a restart. The cutover test covers this. - A provider that the new release launches gets the stable directory and survives later restarts. ## Risks - On sandbox targets, the provider can now read the bridge capability in `broker.json`, as well as in its environment. The file holds the same per-access transport token that the environment already holds, not a run token or a GitHub credential. The file and its staging copies are mode `600`, and the broker still checks the active run and grants on each request. Local targets write no `broker.json`. - Local providers that outlive a restart still lose managed GitHub. This is not a regression: master deletes the launcher directory at the detach, so the old local provider also runs without credentials there. With this PR, the local launchers stay in place and print a `Paperclip: GitHub ...` diagnostic instead of a silent "not logged in". Re-arming local providers needs a broker channel that other local runs under the same user cannot read. That is out of scope here, because `broker.json` in the shared temp directory was a security finding on this PR. - On sandbox targets, `git` and `gh` children no longer inherit `PAPERCLIP_API_URL`, `PAPERCLIP_API_KEY`, or `PAPERCLIP_GITHUB_BRIDGE_TOKEN`. Broker-file mode removes them, as it already did in its existing tests. - After a restart detach, a launcher directory stays in the workspace runtime directory. If no controller adopts the provider, that directory stays until a later access with the same key stops, or until the workspace is removed. It contains no GitHub credential. - Between the detach and the adoption, `gh` gets a transport error from the old endpoint. It then prints `broker_transport_unavailable` and continues without credentials, as before. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, test runs). Standard context window. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
430 lines
26 KiB
TypeScript
430 lines
26 KiB
TypeScript
import { afterEach, expect, it, vi } from "vitest";
|
|
import { spawn, execFile } from "node:child_process";
|
|
import { mkdtemp, mkdir, readdir, readFile, rm, stat, writeFile, access } from "node:fs/promises";
|
|
import { promisify } from "node:util";
|
|
import type { CommandManagedDuplexChannel, CommandManagedRuntimeRunner } from "@paperclipai/adapter-utils/command-managed-runtime";
|
|
import type { DuplexObservabilityRecorder } from "@paperclipai/adapter-utils/duplex-observability";
|
|
import { startAdapterExecutionTargetPaperclipBridge } from "@paperclipai/adapter-utils/execution-target";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { createInterface } from "node:readline";
|
|
import { createNativeGitHubAccess, type NativeGitHubAccess } from "./native-github-access.js";
|
|
import { OpenCodeServerDriver } from "@paperclipai/paperclip-runner";
|
|
import { createCapabilityRunnerdProviderEnvironment } from "@paperclipai/paperclip-runner/live";
|
|
const scope = { companyId: "company-a", agentId: "agent-a", issueId: "task-a" };
|
|
const run = (runId: string) => ({ ...scope, runId });
|
|
const cleanups: (() => Promise<unknown>)[] = [];
|
|
afterEach(async () => { for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); });
|
|
function localDuplexChannel(command: readonly string[], cwd: string): CommandManagedDuplexChannel {
|
|
const child = spawn(command[0]!, command.slice(1), { cwd, stdio: ["pipe", "pipe", "pipe"] });
|
|
const buffered: Buffer[] = [];
|
|
let dataListener: ((chunk: Uint8Array) => void) | undefined;
|
|
let exitListener: ((exit: { exitCode: number | null }) => void) | undefined;
|
|
let exit: { exitCode: number | null } | undefined;
|
|
child.stdin.on("error", () => undefined);
|
|
child.stderr.resume();
|
|
child.stdout.on("data", (chunk: Buffer) => dataListener ? dataListener(chunk) : buffered.push(chunk));
|
|
const closed = new Promise<void>(resolve => child.once("close", code => {
|
|
exit = { exitCode: code };
|
|
exitListener?.(exit);
|
|
resolve();
|
|
}));
|
|
return {
|
|
write: data => { child.stdin.write(data); },
|
|
onData: listener => { dataListener = listener; for (const chunk of buffered.splice(0)) listener(chunk); },
|
|
onExit: listener => { exitListener = listener; if (exit) listener(exit); },
|
|
stop: () => { child.kill(); },
|
|
close: async () => { child.kill(); await closed; },
|
|
};
|
|
}
|
|
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; fileProbeFails?: boolean; recorder?: DuplexObservabilityRecorder },
|
|
placement: { root?: string; launcherKey?: string } = {}) {
|
|
const root = placement.root ?? await mkdtemp(path.join(tmpdir(), "native-github-reuse-"));
|
|
if (!placement.root) cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const bin = path.join(root, "real-bin");
|
|
await mkdir(bin, { recursive: true });
|
|
await writeFile(path.join(bin, "gh"), `#!${process.execPath}\nprocess.stdout.write(process.env.GH_TOKEN || 'anonymous');`, { mode: 0o700 });
|
|
const execute = vi.fn<CommandManagedRuntimeRunner["execute"]>(async (input) => {
|
|
const startedAt = new Date().toISOString();
|
|
const child = promisify(execFile)(input.command, input.args ?? [], {
|
|
cwd: input.cwd ?? root, env: { ...process.env, ...input.env }, timeout: 15_000, maxBuffer: 4 * 1024 * 1024,
|
|
});
|
|
child.child.stdin?.on("error", () => undefined);
|
|
child.child.stdin?.end(input.stdin ?? "");
|
|
const result = await child;
|
|
return { ...result, exitCode: 0, signal: null, timedOut: false, pid: null, startedAt };
|
|
});
|
|
const openDuplexChannel = vi.fn<NonNullable<CommandManagedRuntimeRunner["openDuplexChannel"]>>(async input => {
|
|
if (duplex?.unavailable) throw new Error("fixture channel unavailable");
|
|
return localDuplexChannel(input.command, root);
|
|
});
|
|
const target = remote ? { kind: "remote" as const, transport: "sandbox" as const, providerKey: "test",
|
|
remoteCwd: root, runner: { execute, ...(duplex ? { openDuplexChannel } : {}) }, streamRunLogs: false,
|
|
...(duplex ? { enableSandboxDuplexBridge: duplex.enabled, duplexObservabilityRecorder: duplex.recorder, effectiveCapabilities: {
|
|
reusableLeases: true, nativeSyncIn: false, nativeSyncOut: false, persistentProcessSessions: false,
|
|
independentControlCommands: true, incrementalSessionOutput: false, concurrentSyncOperations: false,
|
|
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,
|
|
launcherKey: placement.launcherKey },
|
|
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();
|
|
await vi.waitFor(() => expect(bridge.readRunDisposition?.().failed).toBe(true));
|
|
if (duplex.startupFailure === "unreachable") {
|
|
// A transport can stop serving before its control channel reports a loss.
|
|
bridge.readRunDisposition = () => ({ failed: false, lossReason: null });
|
|
}
|
|
}
|
|
return bridge;
|
|
});
|
|
cleanups.push(result.stop);
|
|
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, {
|
|
method: "POST", body: "{}", headers: { authorization: `Bearer ${broker.env.PAPERCLIP_GITHUB_BRIDGE_TOKEN}`, "content-type": "application/json" }, ...extra,
|
|
});
|
|
}
|
|
it.each([false, true])("resolves managed GitHub credentials from OpenCode shell operations (runner projection: %s)", async throughRunner => {
|
|
const b = await broker();
|
|
const command = path.join(b.root, "fake-opencode-server.mjs");
|
|
const fixture = new URL("../../../../packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs", import.meta.url);
|
|
await writeFile(command, await readFile(fixture), { mode: 0o700 });
|
|
const source = { ...b.env, OPENROUTER_API_KEY: "fixture-provider-key", PAPERCLIP_API_KEY: "controller-must-not-cross" };
|
|
const environment = throughRunner ? createCapabilityRunnerdProviderEnvironment({
|
|
provider: "opencode",
|
|
options: { environment: source, stateDirectory: b.root },
|
|
identity: { runnerInstanceId: "runner-a", environmentLeaseId: "lease-a", runId: "a", normalizedSessionId: "github", turnId: "turn-a", itemId: "item-a" },
|
|
codexHome: path.join(b.root, "codex-home"),
|
|
runtimeContextPath: path.join(b.root, "context.json"),
|
|
hasRuntimeContext: false,
|
|
}) : source;
|
|
const driver = new OpenCodeServerDriver({
|
|
model: "openrouter/z-ai/glm-5.3", runtimeDirectory: b.root, command, environment,
|
|
});
|
|
const session = await driver.openSession({ runId: "a", normalizedSessionId: "github", workingDirectory: b.root });
|
|
cleanups.push(() => session.close({ reason: "test complete" }));
|
|
const operate = async () => {
|
|
await session.startTurn({ message: { role: "user", text: "managed-github-operation" } });
|
|
for await (const event of session.events()) {
|
|
if (event.eventType === "turn.completed") break;
|
|
if (event.eventType === "turn.failed") throw new Error("OpenCode fixture turn failed");
|
|
}
|
|
return JSON.parse(await readFile(path.join(b.root, "github", "data", "fake-github-operation.json"), "utf8"));
|
|
};
|
|
const releaseA = b.activate(run("a"));
|
|
expect(await operate()).toMatchObject({ stdout: "fixture-a", error: null });
|
|
releaseA();
|
|
const releaseB = b.activate(run("b"));
|
|
expect(await operate()).toMatchObject({ stdout: "fixture-b", error: null });
|
|
releaseB();
|
|
expect(await operate()).toMatchObject({ stdout: "anonymous", error: null });
|
|
expect(b.resolveCredentials.mock.calls.map(([binding]) => binding.runId)).toEqual(["a", "b"]);
|
|
const childEnvironment = JSON.parse(await readFile(path.join(b.root, "github", "data", "fake-environment.json"), "utf8"));
|
|
expect(childEnvironment.keys).not.toContain("PAPERCLIP_API_KEY");
|
|
expect(childEnvironment.githubTokenPresent).toBe(false);
|
|
}, 30000);
|
|
it("uses the selected duplex transport across credential rotation without file polling", async () => {
|
|
const recorder = { recordSpan: vi.fn(), incrementCounter: vi.fn(), emitEvent: vi.fn() };
|
|
const b = await broker(undefined, true, false, undefined, { enabled: true, capability: true, recorder });
|
|
expect(b.bridgeModes).toEqual(["http2_v1"]);
|
|
expect(b.openDuplexChannel).toHaveBeenCalledOnce();
|
|
expect(recorder.recordSpan).toHaveBeenCalledWith(expect.objectContaining({ name: "sandbox.duplex.channel_open", dimensions: {
|
|
provider: "other", transport: "http2", outcome: "ok",
|
|
} }));
|
|
const commandsAfterStartup = b.execute.mock.calls.length;
|
|
expect((await request(b)).status).toBe(403);
|
|
const releaseA = b.activate(run("a"));
|
|
expect(await (await request(b)).json()).toMatchObject({ env: { GH_TOKEN: "fixture-a" } });
|
|
releaseA();
|
|
expect((await request(b)).status).toBe(403);
|
|
const releaseB = b.activate(run("b"));
|
|
releaseA();
|
|
expect(await (await request(b)).json()).toMatchObject({ env: { GH_TOKEN: "fixture-b" } });
|
|
expect(b.resolveCredentials.mock.calls.map(([binding]) => binding.runId)).toEqual(["a", "b"]);
|
|
expect(recorder.recordSpan).toHaveBeenCalledWith(expect.objectContaining({ name: "sandbox.duplex.request" }));
|
|
expect(recorder.emitEvent).not.toHaveBeenCalledWith(expect.objectContaining({ dimensions: expect.objectContaining({ transport: "file" }) }));
|
|
expect(b.execute.mock.calls).toHaveLength(commandsAfterStartup);
|
|
releaseB();
|
|
await b.stop();
|
|
expect(() => b.activate(run("c"))).toThrow("closed");
|
|
});
|
|
it("marks a lost HTTP/2 bridge unavailable before the next warm admission", async () => {
|
|
const b = await broker(undefined, true, false, undefined, { enabled: true, capability: true });
|
|
const release = b.activate(run("a"));
|
|
expect((await request(b)).status).toBe(200);
|
|
release();
|
|
expect(b.ready).toBe(true);
|
|
const channel = await b.openDuplexChannel.mock.results[0]!.value;
|
|
channel.stop();
|
|
await vi.waitFor(() => expect(b.ready).toBe(false), { timeout: 2000 });
|
|
});
|
|
it.each(["lost", "unreachable"] as const)("recovers a %s GitHub bridge after its greeting before starting managed commands", async startupFailure => {
|
|
const b = await broker(undefined, true, false, undefined, { enabled: true, capability: true, startupFailure });
|
|
expect(b.bridgeModes).toEqual(["http2_v1", "queue_v1"]);
|
|
expect(b.ready).toBe(true);
|
|
expect(b.resolveCredentials).not.toHaveBeenCalled();
|
|
const release = b.activate(run("recovered"));
|
|
const result = await promisify(execFile)(path.join(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, "gh"), [], {
|
|
env: { ...process.env, ...b.env, GH_TOKEN: "ambient-must-not-leak" },
|
|
timeout: 15_000,
|
|
});
|
|
expect(result.stdout).toBe("fixture-recovered");
|
|
expect(result.stderr).toBe("");
|
|
expect(b.resolveCredentials.mock.calls.map(([binding]) => binding.runId)).toEqual(["recovered"]);
|
|
release();
|
|
}, 30_000);
|
|
it("keeps host credentials isolated when neither GitHub startup transport works", async () => {
|
|
const b = await broker(undefined, true, false, undefined, {
|
|
enabled: true, capability: true, startupFailure: "lost", fallbackUnavailable: true,
|
|
});
|
|
expect(b.ready).toBe(false);
|
|
expect(b.openDuplexChannel).toHaveBeenCalledOnce();
|
|
expect(b.env.PAPERCLIP_GITHUB_BROKER_TOKEN).toBe("");
|
|
b.activate(run("unavailable"));
|
|
const result = await promisify(execFile)(path.join(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, "gh"), [], {
|
|
env: { ...process.env, ...b.env, GH_TOKEN: "ambient-must-not-leak" },
|
|
});
|
|
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 });
|
|
const release = b.activate(run("a"));
|
|
expect((await request(b)).status).toBe(200);
|
|
release();
|
|
await b.stop();
|
|
expect(b.ready).toBe(false);
|
|
expect(onLog.mock.calls.filter(([, chunk]) => chunk.includes("channel lost"))).toEqual([]);
|
|
});
|
|
it.each([
|
|
{ enabled: false, capability: true, unavailable: false, opens: 0 },
|
|
{ enabled: undefined, capability: true, unavailable: false, opens: 0 },
|
|
{ enabled: true, capability: false, unavailable: false, opens: 0 },
|
|
{ enabled: true, capability: true, unavailable: true, opens: 1 },
|
|
])("retains authorized file fallback with duplex settings %j", async options => {
|
|
const b = await broker(undefined, true, false, undefined, options);
|
|
expect(b.bridgeModes).toEqual(["queue_v1"]);
|
|
expect(b.openDuplexChannel).toHaveBeenCalledTimes(options.opens);
|
|
const release = b.activate(run("fallback"));
|
|
expect(await (await request(b)).json()).toMatchObject({ env: { GH_TOKEN: "fixture-fallback" } });
|
|
release();
|
|
expect((await request(b)).status).toBe(403);
|
|
});
|
|
it.each([false, true])("keeps one live parent and its original launcher environment across two authorized runs (callback bridge: %s)", async (remote) => {
|
|
const b = await broker(undefined, remote);
|
|
const parent = spawn(process.execPath, ["-e", `
|
|
const {execFile}=require('node:child_process');
|
|
require('node:readline').createInterface({input:process.stdin}).on('line', () => {
|
|
execFile('gh', [], (error, stdout, stderr) => console.log(JSON.stringify({pid:process.pid, value:stdout, error:error?.message, stderr})));
|
|
});
|
|
`], { env: { ...process.env, ...b.env, PAPERCLIP_API_KEY: "stale-api-key" }, stdio: ["pipe", "pipe", "pipe"] });
|
|
cleanups.push(async () => { const exited = new Promise<void>(resolve => parent.once("exit", () => resolve())); parent.kill(); await exited; });
|
|
const lines = createInterface({ input: parent.stdout });
|
|
const operation = () => new Promise<{ pid: number; value: string; stderr: string }>(resolve => {
|
|
lines.once("line", line => resolve(JSON.parse(line))); parent.stdin.write("run\n");
|
|
});
|
|
expect((await request(b)).status).toBe(403);
|
|
const releaseA = b.activate(run("run-a"));
|
|
const a = await operation();
|
|
expect(a.value).toBe("fixture-run-a");
|
|
releaseA();
|
|
expect((await operation()).value).toBe("anonymous");
|
|
const releaseB = b.activate(run("run-b"));
|
|
releaseA(); // A's delayed cleanup cannot revoke B.
|
|
const second = await operation();
|
|
expect(second.value).toBe("fixture-run-b");
|
|
expect(second.pid).toBe(a.pid);
|
|
expect(b.resolveCredentials.mock.calls.map(([binding]) => binding.runId)).toEqual(["run-a", "run-b"]);
|
|
expect(await readFile(path.join(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, "gh"), "utf8")).not.toContain(b.env.PAPERCLIP_GITHUB_BROKER_TOKEN);
|
|
releaseB(); await b.stop();
|
|
await expect(access(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR)).rejects.toThrow();
|
|
}, 45_000);
|
|
function liveParent(env: Record<string, string>) {
|
|
const parent = spawn(process.execPath, ["-e", `
|
|
const {execFile}=require('node:child_process');
|
|
require('node:readline').createInterface({input:process.stdin}).on('line', () => {
|
|
execFile('gh', [], (error, stdout, stderr) => console.log(JSON.stringify({pid:process.pid, value:stdout, error:error?.message, stderr})));
|
|
});
|
|
`], { env: { ...process.env, ...env }, stdio: ["pipe", "pipe", "pipe"] });
|
|
cleanups.push(async () => { const exited = new Promise<void>(resolve => parent.once("exit", () => resolve())); parent.kill(); await exited; });
|
|
const lines = createInterface({ input: parent.stdout });
|
|
return () => new Promise<{ pid: number; value: string; stderr: string }>(resolve => {
|
|
lines.once("line", line => resolve(JSON.parse(line))); parent.stdin.write("run\n");
|
|
});
|
|
}
|
|
it("re-arms a live sandbox provider's launchers after its controller restarts", async () => {
|
|
const remote = true;
|
|
const root = await mkdtemp(path.join(tmpdir(), "native-github-restart-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const before = await broker(undefined, remote, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
const operation = liveParent(before.env);
|
|
const releaseBefore = before.activate(run("run-a"));
|
|
const first = await operation();
|
|
expect(first.value).toBe("fixture-run-a");
|
|
releaseBefore();
|
|
await before.stop({ preserveLaunchers: true });
|
|
const launchers = before.env.PAPERCLIP_GITHUB_LAUNCHER_DIR;
|
|
await access(path.join(launchers, "gh"));
|
|
const after = await broker(vi.fn(async (binding: ReturnType<typeof run>) => ({
|
|
status: "available", env: { GH_TOKEN: `adopted-${binding.runId}` },
|
|
})), remote, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
expect(after.env.PAPERCLIP_GITHUB_LAUNCHER_DIR).toBe(launchers);
|
|
expect(after.env.PAPERCLIP_GITHUB_BROKER_TOKEN).not.toBe(before.env.PAPERCLIP_GITHUB_BROKER_TOKEN);
|
|
const releaseAfter = after.activate(run("run-a"));
|
|
const adopted = await operation();
|
|
expect(adopted).toMatchObject({ pid: first.pid, value: "adopted-run-a", stderr: "" });
|
|
expect(await readFile(path.join(launchers, "gh"), "utf8")).not.toContain(after.env.PAPERCLIP_GITHUB_BROKER_TOKEN);
|
|
expect((await stat(path.join(launchers, "broker.json"))).mode & 0o777).toBe(0o600);
|
|
releaseAfter();
|
|
await after.stop();
|
|
await expect(access(launchers)).rejects.toThrow();
|
|
}, 45_000);
|
|
it("keeps launchers a successor re-armed when a superseded access stops", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "native-github-successor-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const superseded = await broker(undefined, true, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
const successor = await broker(vi.fn(async () => ({ status: "available", env: { GH_TOKEN: "successor" } })),
|
|
true, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
const operation = liveParent(superseded.env);
|
|
await superseded.stop();
|
|
successor.activate(run("run-b"));
|
|
expect(await operation()).toMatchObject({ value: "successor", stderr: "" });
|
|
const other = await broker(undefined, true, false, undefined, undefined, { root, launcherKey: "session-b" });
|
|
expect(other.env.PAPERCLIP_GITHUB_LAUNCHER_DIR).not.toBe(successor.env.PAPERCLIP_GITHUB_LAUNCHER_DIR);
|
|
});
|
|
it("keeps a previous-release provider running without managed GitHub after the cutover restart", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "native-github-cutover-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const previous = await broker(undefined, true, false, undefined, undefined, { root });
|
|
const operation = liveParent(previous.env);
|
|
const releasePrevious = previous.activate(run("run-a"));
|
|
expect((await operation()).value).toBe("fixture-run-a");
|
|
releasePrevious();
|
|
await previous.stop();
|
|
await expect(access(previous.env.PAPERCLIP_GITHUB_LAUNCHER_DIR)).rejects.toThrow();
|
|
const current = await broker(undefined, true, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
expect(current.env.PAPERCLIP_GITHUB_LAUNCHER_DIR).not.toBe(previous.env.PAPERCLIP_GITHUB_LAUNCHER_DIR);
|
|
current.activate(run("run-a"));
|
|
expect(await operation()).toMatchObject({ value: "anonymous", stderr: "" });
|
|
expect(current.resolveCredentials).not.toHaveBeenCalled();
|
|
await expect(liveParent(current.env)()).resolves.toMatchObject({ value: "fixture-run-a" });
|
|
}, 45_000);
|
|
it("tells a local provider adopted after a restart that managed GitHub is unavailable", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "native-github-local-restart-"));
|
|
cleanups.push(() => rm(root, { recursive: true, force: true }));
|
|
const before = await broker(undefined, false, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
const operation = liveParent(before.env);
|
|
const releaseBefore = before.activate(run("run-a"));
|
|
expect((await operation()).value).toBe("fixture-run-a");
|
|
releaseBefore();
|
|
await before.stop({ preserveLaunchers: true });
|
|
const after = await broker(undefined, false, false, undefined, undefined, { root, launcherKey: "session-a" });
|
|
after.activate(run("run-a"));
|
|
const adopted = await operation();
|
|
expect(adopted.value).toBe("anonymous");
|
|
expect(adopted.stderr).toMatch(/^Paperclip: GitHub (broker_transport_unavailable|capability_rejected|broker_response_unavailable); continuing without managed credentials\.\n$/);
|
|
expect(after.resolveCredentials).not.toHaveBeenCalled();
|
|
}, 45_000);
|
|
it("keeps the broker capability out of local launcher files", async () => {
|
|
const b = await broker(undefined, false, false, undefined, undefined, { launcherKey: "session-a" });
|
|
const files = await readdir(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, { recursive: true });
|
|
expect(files).not.toContain("broker.json");
|
|
for (const file of files) {
|
|
const target = path.join(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, file);
|
|
if ((await stat(target)).isFile()) expect(await readFile(target, "utf8")).not.toContain(b.env.PAPERCLIP_GITHUB_BROKER_TOKEN);
|
|
}
|
|
b.activate(run("local"));
|
|
expect(await liveParent(b.env)()).toMatchObject({ value: "fixture-local", stderr: "" });
|
|
});
|
|
it("rejects other scopes, concurrent bindings, browser requests and forged authority", async () => {
|
|
const b = await broker();
|
|
for (const field of ["companyId", "agentId", "issueId"] as const) {
|
|
expect(() => b.activate({ ...run("a"), [field]: "other" })).toThrow("scope mismatch");
|
|
}
|
|
const release = b.activate(run("a"));
|
|
expect(() => b.activate(run("b"))).toThrow("busy");
|
|
const rejectedHeaders: Record<string, string>[] = [{ authorization: "Bearer wrong" }, { authorization: "é".repeat(71) },
|
|
{ authorization: `Bearer ${b.env.PAPERCLIP_GITHUB_BRIDGE_TOKEN}`, origin: "https://browser.test" }];
|
|
for (const headers of rejectedHeaders) {
|
|
expect((await request(b, { headers })).status).toBe(403);
|
|
}
|
|
expect((await request(b, {}, "/api/companies")).status).toBe(404);
|
|
expect((await request(b, { method: "GET", body: undefined })).status).toBe(404);
|
|
const response = await request(b, { body: JSON.stringify({ runId: "b", responsibleUserId: "other" }) });
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("cache-control")).toBe("no-store");
|
|
expect(b.resolveCredentials).toHaveBeenCalledExactlyOnceWith(run("a"));
|
|
release(); await b.stop();
|
|
expect(() => b.activate(run("b"))).toThrow("closed");
|
|
});
|
|
it("does not release an in-flight credential after its run has been replaced", async () => {
|
|
let complete!: (value: { status: string; env: { GH_TOKEN: string } }) => void;
|
|
const resolver = vi.fn(() => new Promise<{ status: string; env: { GH_TOKEN: string } }>(resolve => { complete = resolve; }));
|
|
const b = await broker(resolver);
|
|
const release = b.activate(run("a"));
|
|
const pending = request(b);
|
|
await vi.waitFor(() => expect(resolver).toHaveBeenCalledOnce());
|
|
release(); b.activate(run("b"));
|
|
complete({ status: "available", env: { GH_TOKEN: "must-not-escape" } });
|
|
const response = await pending;
|
|
expect(response.status).toBe(403);
|
|
expect(await response.text()).not.toContain("must-not-escape");
|
|
});
|
|
it.each([403, 409, 500])("preserves denial/steering without leaking errors (%s)", async (status) => {
|
|
const b = await broker(vi.fn(async () => { throw Object.assign(new Error("secret-value"), { status }); }));
|
|
b.activate(run("a"));
|
|
const response = await request(b);
|
|
expect(response.status).toBe(status === 500 ? 503 : status);
|
|
expect(await response.text()).not.toContain("secret-value");
|
|
});
|
|
|
|
it.each([false, true])("keeps anonymous launchers usable when the remote broker cannot start (logging fails: %s)", async (loggingFails) => {
|
|
const onLog = vi.fn(async () => { if (loggingFails) throw new Error("fixture log sink unavailable"); });
|
|
const b = await broker(undefined, true, true, onLog);
|
|
expect(onLog).toHaveBeenCalledWith("stderr", expect.stringContaining("continuing without managed GitHub access"));
|
|
b.activate(run("a"));
|
|
expect(b.ready).toBe(false);
|
|
expect(b.env.PAPERCLIP_GITHUB_BROKER_TOKEN).toBe("");
|
|
const result = await promisify(execFile)(path.join(b.env.PAPERCLIP_GITHUB_LAUNCHER_DIR, "gh"), [], {
|
|
env: { ...process.env, ...b.env, GH_TOKEN: "ambient-must-not-leak" },
|
|
});
|
|
expect(result.stdout).toBe("anonymous");
|
|
expect(b.resolveCredentials).not.toHaveBeenCalled();
|
|
});
|