Preserve ACPX credential bindings through final runner launch

Keep candidate credentials explicit across server fallbacks and reject unqualified direct product backends before driver construction.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-09-28 13:34:02 -05:00
1 parent f80c312cdd
commit 7721662f20
7 files changed
+107 -11

No files matched your search

@@ -31,6 +31,7 @@ import {
validatePrpEvent,
parseNativeExecutionInput,
type NativeExecutionInputV1,
type NativeExecutionInput,
type PrpEvent,
} from "@paperclipai/paperclip-runner";
import { createHash } from "node:crypto";
@@ -254,6 +255,7 @@ vi.mock("./native-codex-runner.js", () => ({
import {
continuingPendingInteractionIds,
buildNativeProviderEnvironment,
resolveNativeProviderEnvironment,
buildNativeHarnessBackupManifest,
cancelNativeSession,
closeWarmNativeSessionsForEnvironment,
@@ -2408,6 +2410,23 @@ describe("runtime question fallback", () => {
});
describe("native provider bootstrap environment", () => {
it.each(["pi", "cursor", "copilot"] as const)("does not promote ambient %s credentials when bindings are omitted", agent => {
const provider = { kind: "acpx", agent } as NativeExecutionInput["provider"];
const host = { PATH: "/host/bin", HOME: "/host/home", OPENROUTER_API_KEY: "ambient-pi",
CURSOR_API_KEY: "ambient-cursor", CURSOR_AUTH_TOKEN: "ambient-cursor-login", COPILOT_GITHUB_TOKEN: "ambient-copilot",
PAPERCLIP_ACPX_CREDENTIAL_BINDING: "ambient-forged-binding" };
expect(resolveNativeProviderEnvironment(provider, undefined, host)).toEqual({ PATH: "/host/bin", HOME: "/host/home" });
const explicit = { COPILOT_GITHUB_TOKEN: "explicit-company-binding" };
expect(resolveNativeProviderEnvironment(provider, explicit, host)).toBe(explicit);
});
it("preserves existing qualified and legacy missing-environment behavior", () => {
const host = { OPENAI_API_KEY: "existing-host-key", OPENROUTER_API_KEY: "existing-opencode-key" };
for (const provider of [{ kind: "codex" }, { kind: "opencode" }, { kind: "acpx", agent: "codex" }, { kind: "acpx", agent: "claude" }]) {
expect(resolveNativeProviderEnvironment(provider as NativeExecutionInput["provider"], undefined, host)).toBe(host);
}
});
it("inherits the host executable and credential-home context", () => {
expect(
buildNativeProviderEnvironment(
@@ -579,6 +579,19 @@ export function buildNativeProviderEnvironment(
return environment;
}
/** Missing candidate bindings must not turn the controller's ambient credentials into explicit input. */
export function resolveNativeProviderEnvironment(
provider: NativeExecutionInput["provider"],
configured: NodeJS.ProcessEnv | undefined,
host: NodeJS.ProcessEnv = process.env,
): NodeJS.ProcessEnv {
if (configured !== undefined) return configured;
if (provider.kind === "acpx" && ["pi", "cursor", "copilot"].includes(provider.agent)) {
return buildNativeProviderEnvironment({}, host);
}
return host;
}
type PlanSynchronization = {
eventId: string;
planId: string;
@@ -8119,7 +8132,7 @@ async function executePaperclipNativeSessionWithinScope(
scope: input.execution.binding,
target: input.runnerExecutionTarget,
cwd: input.execution.workspace.cwd,
env: input.runnerEnvironment ?? process.env,
env: resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment),
resolveCredentials: (binding) => resolveGitHubOperationCredentials(input.db, binding),
onLog: input.onLog,
});
@@ -8184,8 +8197,8 @@ async function executePaperclipNativeSessionWithinScope(
createNativeSessionBackend(input.execution, {
runnerInstanceId: input.runnerInstanceId,
onSpawn: input.onSpawn,
opencodeEnvironment: input.runnerEnvironment ?? process.env,
acpxEnvironment: input.runnerEnvironment ?? process.env,
opencodeEnvironment: resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment),
acpxEnvironment: resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment),
opencodeRuntimeDirectory: resolve(
resolvePaperclipInstanceRoot(),
"runtime",
@@ -12099,7 +12112,7 @@ async function createRunnerdBackendWithinSessionClaim(
}
: input.execution;
const effectiveRunnerEnvironmentBase: NodeJS.ProcessEnv = {
...(input.runnerEnvironment ?? process.env),
...resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment),
};
if (relayAssignedMcp) {
// The server-held tool authority owns this credential. Do not deliver a
@@ -12340,7 +12353,7 @@ async function createRunnerdBackendWithinSessionClaim(
runnerBinary: controllerRunnerBinary,
codexCommand: remoteCodexBinary ?? undefined,
sourceCodexHome: remoteTarget
? resolveSourceCodexHome(input.runnerEnvironment ?? process.env)
? resolveSourceCodexHome(resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment))
: undefined,
runnerProcessLauncher: remoteProcessLauncher,
runnerReconnectGraceMs: remoteTarget ? 120_000 : undefined,