mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-21 10:52:06 +01:00
feat(ssh): use the agent AI account in new environments (#21248)
Use the agent-selected AI account for new SSH environments. Preserve
remote-machine authentication for existing environments and expose it as
an advanced option. Keep managed credentials private while preserving
supported conversation state and credential refreshes across turns.
Validated with focused route, adapter, lifecycle, and real-process tests;
final CI passed on d634944df1.
Greptile Apex reviewed that revision at 5/5 with no unresolved threads.
External neutral security reports remain documented in the pull request.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
2a8b485140
commit
12aa08721c
77 files changed
+6115
-3898
No files matched your search
@@ -21613,7 +21613,7 @@ describe("runnerd provider runtime wiring", () => {
|
||||
homeMode: "preconfigured" as const, executionMode: "default" as const, planningContext: null,
|
||||
provider: { kind: "codex" as const, model: null, approvalPolicy: "never" as const }, runtimeContext: nativeRuntimeContextFixture() };
|
||||
const remoteExecute = vi.fn(async (command: { args?: string[] }) => ({
|
||||
exitCode: 0, timedOut: false, stderr: "", stdout: command.args?.[1]?.includes("getent passwd") ? "/home/executor\n" : "",
|
||||
exitCode: 0, timedOut: false, stderr: "", stdout: command.args?.includes("paperclip-remote-home") ? "/home/executor\n" : "",
|
||||
}));
|
||||
await createRunnerdBackend({ db: leaseDb(remoteExecution), execution: remoteExecution, runnerInstanceId: "remote-home-manifest",
|
||||
runnerEnvironment, runnerIngressAuthorized: true, runnerExecutionTarget: {
|
||||
@@ -22092,7 +22092,7 @@ describe("runnerd provider runtime wiring", () => {
|
||||
if (command.args?.[0] === "--version") return {
|
||||
exitCode: 0, timedOut: false, stdout: "codex-cli 0.160.0", stderr: "",
|
||||
};
|
||||
if (command.args?.[1]?.includes("getent passwd")) return {
|
||||
if (command.args?.includes("paperclip-remote-home")) return {
|
||||
exitCode: 0, timedOut: false, stdout: "/home/executor\n", stderr: "",
|
||||
};
|
||||
if (command.args?.[2] === "paperclip-runner-launch") {
|
||||
|
||||
@@ -233,6 +233,7 @@ import type { NativeRunnerAttachmentDownload, NativeRunnerAttachmentStage } from
|
||||
import { discoverRemoteExecutable, parseRemoteExecutableCandidate } from "./codex-model-fallback.js";
|
||||
export { parseRemoteExecutableCandidate } from "./codex-model-fallback.js";
|
||||
import type { CommandManagedRuntimeRunner } from "@paperclipai/adapter-utils/command-managed-runtime";
|
||||
import { readRemoteUserHome } from "./remote-user-home.js";
|
||||
import { NativeRunnerIngressAcquisitionError, prepareNativeRemoteRunner, createNativeRunnerExecutionDriver, resolveRemoteRunnerTransportMode } from "./native-runner-execution-driver.js";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
@@ -18048,14 +18049,7 @@ async function createRunnerdBackendWithinSessionClaim(
|
||||
delete effectiveRunnerEnvironmentBase.PAPERCLIP_RUNNER_PREPARED_HARNESS_HOME;
|
||||
if (preconfiguredHome) {
|
||||
if (remoteCommandRunner) {
|
||||
const discovered = await remoteCommandRunner.execute({
|
||||
command: "sh", args: ["-c", 'getent passwd "$(id -u)" | cut -d: -f6'],
|
||||
bypassSession: true, timeoutMs: 10_000,
|
||||
});
|
||||
const home = discovered.stdout.trim();
|
||||
if (discovered.exitCode !== 0 || discovered.timedOut || !home.startsWith("/") || /[\0\r\n]/.test(home)) {
|
||||
throw new Error("runner_preconfigured_remote_home_unavailable");
|
||||
}
|
||||
const home = await readRemoteUserHome(remoteCommandRunner);
|
||||
// Paths from the controller's account cannot select a remote account's config.
|
||||
const hostPathKeys = new Set(["CODEX_HOME", "CLAUDE_CONFIG_DIR", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"]);
|
||||
const configured = configuredEnvironment(effectiveRunnerEnvironmentBase);
|
||||
|
||||
@@ -58,7 +58,7 @@ describe("native SSH deliverable output budget", () => {
|
||||
});
|
||||
|
||||
it("still delivers every byte when SSH consumes the upload", async () => {
|
||||
await writeFile(join(root, "ssh"), `#!${process.execPath}\nconst hash = require('node:crypto').createHash('sha256');\nprocess.stdin.on('data', chunk => hash.update(chunk));\nprocess.stdin.on('end', () => process.stdout.write(hash.digest('hex')));\n`);
|
||||
await writeFile(join(root, "ssh"), `#!${process.execPath}\nconst chunks = [];\nprocess.stdin.on('data', chunk => chunks.push(chunk));\nprocess.stdin.on('end', () => { const wire = Buffer.concat(chunks); const end = wire.indexOf(10); if (end < 0) process.exit(1); process.stdout.write(require('node:crypto').createHash('sha256').update(wire.subarray(end + 1)).digest('hex')); });\n`);
|
||||
const stdin = "synthetic upload bytes\0".repeat(32_768);
|
||||
const result = await createRunner().execute({ command: "node", args: ["unused"], stdin, timeoutMs: 5000 });
|
||||
expect(result.exitCode).toBe(0);
|
||||
|
||||
@@ -658,6 +658,13 @@ export function resolvePaperclipRunnerProviderProfile(
|
||||
};
|
||||
}
|
||||
|
||||
export function supportsPaperclipRunnerPreconfiguredHome(adapterConfig: unknown, options: { companyId?: string } = {}): boolean {
|
||||
const config = asRecord(adapterConfig);
|
||||
const profile = resolvePaperclipRunnerProviderProfile(config, options);
|
||||
return profile.provider === "codex" || (profile.provider === "acpx" && profile.acpxAgent === "claude"
|
||||
&& (config.acpxPermissionMode === undefined || config.acpxPermissionMode === "approve-all"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Project the operator-owned adapter configuration and a qualified stored
|
||||
* profile into the closed native execution descriptor. Editable adapter
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { CommandManagedRuntimeRunner } from "@paperclipai/adapter-utils/command-managed-runtime";
|
||||
|
||||
export async function readRemoteUserHome(runner: Pick<CommandManagedRuntimeRunner, "execute">): Promise<string> {
|
||||
const result = await runner.execute({ command: "sh", args: ["-c", 'printf "%s\\n" "$HOME"', "paperclip-remote-home"],
|
||||
bypassSession: true, timeoutMs: 10_000 });
|
||||
const home = result.stdout.trim();
|
||||
if (result.exitCode !== 0 || result.timedOut || !home.startsWith("/") || /[\0\r\n]/.test(home)) {
|
||||
throw new Error("runner_preconfigured_remote_home_unavailable");
|
||||
}
|
||||
return home;
|
||||
}
|
||||
@@ -330,6 +330,31 @@ describe("Codex selected native account verification", () => {
|
||||
expect(await readFile(join(home, "account-store.json"), "utf8")).toBe(auth);
|
||||
} finally { await rm(home, { recursive: true, force: true }); }
|
||||
});
|
||||
it.each(["valid", "invalid home"])("probes the native SSH user's login without reading controller credentials (%s)", async mode => {
|
||||
vi.stubEnv("HOME", "/controller-only/home");
|
||||
vi.stubEnv("CODEX_HOME", "/controller-only/codex");
|
||||
vi.stubEnv("OPENAI_API_KEY", "fixture-controller-key");
|
||||
const readCredential = vi.spyOn(localCredentialFile, "readLocalAiCredentialFile");
|
||||
sshExecute.mockImplementation(async command => ({ exitCode: 0, timedOut: false, stderr: "", stdout: command.args?.includes("paperclip-remote-home") ? (mode === "valid" ? "/home/remote-user\n" : "relative-home\n") : "" }));
|
||||
nativeProbe.mockImplementation(async input => {
|
||||
expect(input.environment.HOME).toBe("/home/remote-user");
|
||||
expect(input.environment.CODEX_HOME).toBeUndefined();
|
||||
expect(input.environment.OPENAI_API_KEY).toBeUndefined();
|
||||
expect(input.transportOptions.homeMode).toBe("preconfigured");
|
||||
expect(input.transportOptions.sourceCodexHome).toBe("");
|
||||
expect(input.onCodexCredentialRefresh).toBeUndefined();
|
||||
await input.onCleanupConfirmed();
|
||||
return receipt("codex", "gpt-6.1-sol");
|
||||
});
|
||||
const result = await testNativeRunnerAuthentication({ ...sshContext, config: { homeMode: "preconfigured", env: { CODEX_HOME: "/controller-only/configured-home" } } }, "codex", "gpt-6.1-sol");
|
||||
expect(result.status).toBe(mode === "valid" ? "pass" : "fail");
|
||||
expect(readCredential).not.toHaveBeenCalled();
|
||||
if (mode === "valid") expect(nativeProbe).toHaveBeenCalledOnce();
|
||||
else expect(nativeProbe).not.toHaveBeenCalled();
|
||||
expect(sshExecute.mock.calls.some(([command]) => JSON.stringify(command).includes("controller-only"))).toBe(false);
|
||||
readCredential.mockRestore();
|
||||
});
|
||||
|
||||
it.each(["linux-arm64", "linux-x64"])("uses task staging and the selected %s SSH daemon without a provider pack", async target => {
|
||||
const selectedBinary = `/fixture/bin/${target}/paperclip-runnerd`;
|
||||
selectedRemoteRunner.mockResolvedValue(selectedBinary);
|
||||
|
||||
@@ -9,6 +9,7 @@ import { ADAPTER_AUTH_MISSING_CHECK_CODE } from "@paperclipai/shared";
|
||||
import { NativeEnvironmentProbeCleanupError, probeNativeRunnerEnvironment, resolveSourceCodexHome } from "../../vendor/paperclip-runner/index.js";
|
||||
import { resolvePaperclipRunnerBinary, resolveRemotePaperclipRunnerBinary } from "./native-codex-runner.js";
|
||||
import { readLocalAiCredentialFile } from "../local-ai-credential-file.js";
|
||||
import { readRemoteUserHome } from "./remote-user-home.js";
|
||||
import { createNativeSshCommandRunner } from "./native-ssh-command-runner.js";
|
||||
import { resolvePaperclipRunnerTransport } from "@paperclipai/adapter-utils/runner-connectivity";
|
||||
import { createNativeRunnerExecutionDriver } from "./native-runner-execution-driver.js";
|
||||
@@ -97,6 +98,12 @@ async function probeRemoteNativeCodex(options: {
|
||||
const { createRemoteNativeArtifactPreparation, createRemoteRunnerProcessLauncher, stageRemoteRunnerFile, readRemoteRunnerState } = await import("./native-session-executor.js");
|
||||
const runner = target.transport === "ssh" ? createNativeSshCommandRunner({ spec: target.spec, defaultCwd: target.remoteCwd }) : target.runner;
|
||||
if (!runner) throw new Error("runner_transport_ineligible: remote process runner is unavailable");
|
||||
const preconfigured = target.transport === "ssh" && options.context.config.homeMode === "preconfigured";
|
||||
const environment = { ...options.environment };
|
||||
if (preconfigured) {
|
||||
delete environment.CODEX_HOME;
|
||||
environment.HOME = await readRemoteUserHome(runner);
|
||||
}
|
||||
const id = crypto.randomUUID();
|
||||
const runnerInstanceId = `setup-${id}`;
|
||||
const ingress = target.transport === "sandbox" && target.effectiveCapabilities?.runnerWebSocketIngress === true;
|
||||
@@ -122,14 +129,15 @@ async function probeRemoteNativeCodex(options: {
|
||||
};
|
||||
const runtimeDirectory = await realpath(await mkdtemp(join(tmpdir(), "paperclip-native-setup-")));
|
||||
return probePreparedNativeRuntime(runtimeDirectory, async () => {
|
||||
const sourceCodexHome = await prepareProbeCodexSource(runtimeDirectory, options.sourceCodexHome, options.sourceAuthJson);
|
||||
const sourceCodexHome = preconfigured ? "" : await prepareProbeCodexSource(runtimeDirectory, options.sourceCodexHome, options.sourceAuthJson);
|
||||
let remoteDirectoryCreated = false;
|
||||
let signalSpawned!: () => void;
|
||||
const spawned = new Promise<void>(resolve => { signalSpawned = resolve; });
|
||||
return { runtimeDirectory: runtimeDirectory, provider: "codex", model: options.model, reasoningEffort: options.reasoningEffort,
|
||||
workingDirectory: remoteWorkspace, environment: options.environment, timeoutMs: options.timeoutMs,
|
||||
workingDirectory: remoteWorkspace, environment, timeoutMs: options.timeoutMs,
|
||||
transportOptions: {
|
||||
externallySandboxed: target.transport === "sandbox",
|
||||
...(preconfigured ? { homeMode: "preconfigured" as const } : {}),
|
||||
runnerBinary: controllerRunnerBinary, codexCommand: remoteCodexBinary, sourceCodexHome,
|
||||
runnerFilesystemRoot: remoteFilesystemRoot,
|
||||
prpIdentity: { runnerInstanceId, runId: id, environmentLeaseId: `setup-${id}`, normalizedSessionId: `setup-${id}`, turnId: `turn-${id}`, itemId: `item-${id}` },
|
||||
@@ -157,6 +165,7 @@ async function probeRemoteNativeCodex(options: {
|
||||
runnerProcessLauncher: createRemoteRunnerProcessLauncher({ target, runner, remoteBinary, stateDirectory: remoteStateDirectory,
|
||||
onRunnerProcessSpawned: signalSpawned, processIdentityPath: posix.join(remoteStateDirectory, "runner-process.identity"), diagnosticsDirectory: posix.join(remoteRoot, "diagnostics"), runnerInstanceId,
|
||||
ensureArtifact: async () => {
|
||||
if (preconfigured) return;
|
||||
for (const name of ["auth.json", "config.toml"] as const) {
|
||||
const sourcePath = join(runtimeDirectory, "codex-home", name);
|
||||
try { await stat(sourcePath); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") continue; throw error; }
|
||||
@@ -205,7 +214,7 @@ export async function testNativeRunnerAuthentication(context: AdapterEnvironment
|
||||
let runtimeDirectory: string | undefined;
|
||||
let sourceAuthJson: string | undefined;
|
||||
try {
|
||||
if (provider === "codex") {
|
||||
if (provider === "codex" && !(context.executionTarget?.kind === "remote" && context.executionTarget.transport === "ssh" && context.config.homeMode === "preconfigured")) {
|
||||
const { buildNativeProviderEnvironment } = await import("./native-session-executor.js");
|
||||
sourceCodexHome = context.managedAiCredentialHome ?? resolveSourceCodexHome(buildNativeProviderEnvironment(environment));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user