mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-25 09:41:04 +01:00
When executionTargetIsRemote, Codex runs with CODEX_HOME set to the remote sandbox home. Any token rotation that occurs inside the sandbox updates only the remote copy, so the prior post-run snapshot read from the local effectiveCodexHome would always report rotationsDetected:false. - Add readPostRunCodexCredentialTelemetrySnapshot: for remote targets, shell-cats the remote auth.json via runAdapterExecutionTargetShellCommand so the post-run fingerprint reflects the actual remote token state - Add parseCodexCredentialTelemetrySnapshot as an exported function so the remote reader can parse the raw JSON without touching the local fs - Add shellQuote and missingCodexCredentialTelemetrySnapshot helpers - Tighten CODEX_REFRESH_TOKEN_INVALIDATED_RE (remove broad 401/unauthorized to avoid false positives) and add CODEX_OAUTH_INVALID_GRANT_RE plus CODEX_CONTEXTUAL_REFRESH_AUTH_INVALIDATED_RE for more precise matching - Add execute.remote.test coverage: reads post-run telemetry from remote CODEX_HOME and verifies rotationsDetected:true + no token material leaked Co-Authored-By: Paperclip <noreply@paperclip.ing>
508 lines
17 KiB
TypeScript
508 lines
17 KiB
TypeScript
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const {
|
|
runChildProcess,
|
|
ensureCommandResolvable,
|
|
resolveCommandForLogs,
|
|
prepareWorkspaceForSshExecution,
|
|
restoreWorkspaceFromSshExecution,
|
|
runSshCommand,
|
|
syncDirectoryToSsh,
|
|
startAdapterExecutionTargetPaperclipBridge,
|
|
} = vi.hoisted(() => ({
|
|
runChildProcess: vi.fn(async () => ({
|
|
exitCode: 1,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: "",
|
|
stderr: "remote failure",
|
|
pid: 123,
|
|
startedAt: new Date().toISOString(),
|
|
})),
|
|
ensureCommandResolvable: vi.fn(async () => undefined),
|
|
resolveCommandForLogs: vi.fn(async () => "/usr/bin/codex"),
|
|
prepareWorkspaceForSshExecution: vi.fn(async () => ({ gitBacked: false })),
|
|
restoreWorkspaceFromSshExecution: vi.fn(async () => undefined),
|
|
runSshCommand: vi.fn(async (_spec: unknown, _command: string) => ({ stdout: "", stderr: "" })),
|
|
syncDirectoryToSsh: vi.fn(async () => undefined),
|
|
startAdapterExecutionTargetPaperclipBridge: vi.fn(async () => ({
|
|
env: {
|
|
PAPERCLIP_API_URL: "http://127.0.0.1:4310",
|
|
PAPERCLIP_API_KEY: "bridge-token",
|
|
PAPERCLIP_API_BRIDGE_MODE: "queue_v1",
|
|
},
|
|
stop: async () => {},
|
|
})),
|
|
}));
|
|
|
|
vi.mock("@paperclipai/adapter-utils/server-utils", async () => {
|
|
const actual = await vi.importActual<typeof import("@paperclipai/adapter-utils/server-utils")>(
|
|
"@paperclipai/adapter-utils/server-utils",
|
|
);
|
|
return {
|
|
...actual,
|
|
ensureCommandResolvable,
|
|
resolveCommandForLogs,
|
|
runChildProcess,
|
|
};
|
|
});
|
|
|
|
vi.mock("@paperclipai/adapter-utils/ssh", async () => {
|
|
const actual = await vi.importActual<typeof import("@paperclipai/adapter-utils/ssh")>(
|
|
"@paperclipai/adapter-utils/ssh",
|
|
);
|
|
return {
|
|
...actual,
|
|
prepareWorkspaceForSshExecution,
|
|
restoreWorkspaceFromSshExecution,
|
|
syncDirectoryToSsh,
|
|
};
|
|
});
|
|
|
|
vi.mock("@paperclipai/adapter-utils/execution-target", async () => {
|
|
const actual = await vi.importActual<typeof import("@paperclipai/adapter-utils/execution-target")>(
|
|
"@paperclipai/adapter-utils/execution-target",
|
|
);
|
|
return {
|
|
...actual,
|
|
runAdapterExecutionTargetShellCommand: runSshCommand,
|
|
startAdapterExecutionTargetPaperclipBridge,
|
|
};
|
|
});
|
|
|
|
import { execute } from "./execute.js";
|
|
|
|
describe("codex remote execution", () => {
|
|
const cleanupDirs: string[] = [];
|
|
|
|
afterEach(async () => {
|
|
vi.clearAllMocks();
|
|
while (cleanupDirs.length > 0) {
|
|
const dir = cleanupDirs.pop();
|
|
if (!dir) continue;
|
|
await rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
|
}
|
|
});
|
|
|
|
it("prepares the workspace, syncs CODEX_HOME, and restores workspace changes for remote SSH execution", async () => {
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-remote-"));
|
|
cleanupDirs.push(rootDir);
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const codexHomeDir = path.join(rootDir, "codex-home");
|
|
const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-1/workspace";
|
|
await mkdir(workspaceDir, { recursive: true });
|
|
await mkdir(codexHomeDir, { recursive: true });
|
|
await writeFile(path.join(rootDir, "instructions.md"), "Use the remote workspace.\n", "utf8");
|
|
await writeFile(path.join(codexHomeDir, "auth.json"), "{}", "utf8");
|
|
const alternateWorkspaceDir = path.join(rootDir, "alternate-workspace");
|
|
await mkdir(alternateWorkspaceDir, { recursive: true });
|
|
|
|
await execute({
|
|
runId: "run-1",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: null,
|
|
sessionParams: null,
|
|
sessionDisplayId: null,
|
|
taskKey: null,
|
|
},
|
|
config: {
|
|
command: "codex",
|
|
env: {
|
|
CODEX_HOME: codexHomeDir,
|
|
},
|
|
},
|
|
context: {
|
|
paperclipWorkspace: {
|
|
cwd: workspaceDir,
|
|
source: "project_primary",
|
|
strategy: "git_worktree",
|
|
workspaceId: "workspace-1",
|
|
repoUrl: "https://github.com/paperclipai/paperclip.git",
|
|
repoRef: "main",
|
|
branchName: "feature/remote-codex",
|
|
worktreePath: workspaceDir,
|
|
},
|
|
paperclipWorkspaces: [
|
|
{
|
|
workspaceId: "workspace-1",
|
|
cwd: workspaceDir,
|
|
repoUrl: "https://github.com/paperclipai/paperclip.git",
|
|
repoRef: "main",
|
|
},
|
|
{
|
|
workspaceId: "workspace-2",
|
|
cwd: alternateWorkspaceDir,
|
|
repoUrl: "https://github.com/paperclipai/paperclip.git",
|
|
repoRef: "feature/other",
|
|
},
|
|
],
|
|
},
|
|
executionTransport: {
|
|
remoteExecution: {
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/remote/workspace",
|
|
remoteCwd: "/remote/workspace",
|
|
privateKey: "PRIVATE KEY",
|
|
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA",
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
});
|
|
|
|
expect(prepareWorkspaceForSshExecution).toHaveBeenCalledTimes(1);
|
|
expect(prepareWorkspaceForSshExecution).toHaveBeenCalledWith(expect.objectContaining({
|
|
localDir: workspaceDir,
|
|
remoteDir: managedRemoteWorkspace,
|
|
}));
|
|
expect(syncDirectoryToSsh).toHaveBeenCalledTimes(1);
|
|
expect(syncDirectoryToSsh).toHaveBeenCalledWith(expect.objectContaining({
|
|
localDir: codexHomeDir,
|
|
remoteDir: `${managedRemoteWorkspace}/.paperclip-runtime/codex/home`,
|
|
followSymlinks: true,
|
|
}));
|
|
|
|
expect(runChildProcess).toHaveBeenCalledTimes(1);
|
|
const call = runChildProcess.mock.calls[0] as unknown as
|
|
| [string, string, string[], { env: Record<string, string>; remoteExecution?: { remoteCwd: string } | null }]
|
|
| undefined;
|
|
expect(call?.[2]).not.toContain("--skip-git-repo-check");
|
|
expect(call?.[3].env.CODEX_HOME).toBe(`${managedRemoteWorkspace}/.paperclip-runtime/codex/home`);
|
|
expect(call?.[3].env.PAPERCLIP_WORKSPACE_CWD).toBe(managedRemoteWorkspace);
|
|
expect(call?.[3].env.PAPERCLIP_WORKSPACE_WORKTREE_PATH).toBeUndefined();
|
|
expect(JSON.parse(call?.[3].env.PAPERCLIP_WORKSPACES_JSON ?? "[]")).toEqual([
|
|
{
|
|
workspaceId: "workspace-1",
|
|
cwd: managedRemoteWorkspace,
|
|
repoUrl: "https://github.com/paperclipai/paperclip.git",
|
|
repoRef: "main",
|
|
},
|
|
{
|
|
workspaceId: "workspace-2",
|
|
repoUrl: "https://github.com/paperclipai/paperclip.git",
|
|
repoRef: "feature/other",
|
|
},
|
|
]);
|
|
expect(call?.[3].env.PAPERCLIP_API_URL).toBe("http://127.0.0.1:4310");
|
|
expect(call?.[3].env.PAPERCLIP_API_BRIDGE_MODE).toBe("queue_v1");
|
|
expect(call?.[3].remoteExecution?.remoteCwd).toBe(managedRemoteWorkspace);
|
|
expect(startAdapterExecutionTargetPaperclipBridge).toHaveBeenCalledTimes(1);
|
|
expect(restoreWorkspaceFromSshExecution).toHaveBeenCalledTimes(1);
|
|
expect(restoreWorkspaceFromSshExecution).toHaveBeenCalledWith(expect.objectContaining({
|
|
localDir: workspaceDir,
|
|
remoteDir: managedRemoteWorkspace,
|
|
}));
|
|
});
|
|
|
|
it("does not resume saved Codex sessions for remote SSH execution without a matching remote identity", async () => {
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-remote-resume-"));
|
|
cleanupDirs.push(rootDir);
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const codexHomeDir = path.join(rootDir, "codex-home");
|
|
await mkdir(workspaceDir, { recursive: true });
|
|
await mkdir(codexHomeDir, { recursive: true });
|
|
await writeFile(path.join(codexHomeDir, "auth.json"), "{}", "utf8");
|
|
|
|
await execute({
|
|
runId: "run-ssh-no-resume",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: "session-123",
|
|
sessionParams: {
|
|
sessionId: "session-123",
|
|
cwd: "/remote/workspace",
|
|
},
|
|
sessionDisplayId: "session-123",
|
|
taskKey: null,
|
|
},
|
|
config: {
|
|
command: "codex",
|
|
env: {
|
|
CODEX_HOME: codexHomeDir,
|
|
},
|
|
},
|
|
context: {
|
|
paperclipWorkspace: {
|
|
cwd: workspaceDir,
|
|
source: "project_primary",
|
|
},
|
|
},
|
|
executionTransport: {
|
|
remoteExecution: {
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/remote/workspace",
|
|
remoteCwd: "/remote/workspace",
|
|
privateKey: "PRIVATE KEY",
|
|
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA",
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
});
|
|
|
|
expect(runChildProcess).toHaveBeenCalledTimes(1);
|
|
const call = runChildProcess.mock.calls[0] as unknown as [string, string, string[]] | undefined;
|
|
expect(call?.[2]).toEqual([
|
|
"exec",
|
|
"--json",
|
|
"-",
|
|
]);
|
|
});
|
|
|
|
it("resumes saved Codex sessions for remote SSH execution when the remote identity matches", async () => {
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-remote-resume-match-"));
|
|
cleanupDirs.push(rootDir);
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const codexHomeDir = path.join(rootDir, "codex-home");
|
|
const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-ssh-resume/workspace";
|
|
await mkdir(workspaceDir, { recursive: true });
|
|
await mkdir(codexHomeDir, { recursive: true });
|
|
await writeFile(path.join(codexHomeDir, "auth.json"), "{}", "utf8");
|
|
|
|
await execute({
|
|
runId: "run-ssh-resume",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: "session-123",
|
|
sessionParams: {
|
|
sessionId: "session-123",
|
|
cwd: managedRemoteWorkspace,
|
|
remoteExecution: {
|
|
transport: "ssh",
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteCwd: managedRemoteWorkspace,
|
|
},
|
|
},
|
|
sessionDisplayId: "session-123",
|
|
taskKey: null,
|
|
},
|
|
config: {
|
|
command: "codex",
|
|
env: {
|
|
CODEX_HOME: codexHomeDir,
|
|
},
|
|
},
|
|
context: {
|
|
paperclipWorkspace: {
|
|
cwd: workspaceDir,
|
|
source: "project_primary",
|
|
},
|
|
},
|
|
executionTransport: {
|
|
remoteExecution: {
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/remote/workspace",
|
|
remoteCwd: "/remote/workspace",
|
|
privateKey: "PRIVATE KEY",
|
|
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA",
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
});
|
|
|
|
expect(runChildProcess).toHaveBeenCalledTimes(1);
|
|
const call = runChildProcess.mock.calls[0] as unknown as [string, string, string[]] | undefined;
|
|
expect(call?.[2]).toEqual([
|
|
"exec",
|
|
"--json",
|
|
"resume",
|
|
"session-123",
|
|
"-",
|
|
]);
|
|
});
|
|
|
|
it("uses the provider-neutral execution target contract for remote SSH execution", async () => {
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-target-"));
|
|
cleanupDirs.push(rootDir);
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const codexHomeDir = path.join(rootDir, "codex-home");
|
|
const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-target/workspace";
|
|
await mkdir(workspaceDir, { recursive: true });
|
|
await mkdir(codexHomeDir, { recursive: true });
|
|
await writeFile(path.join(codexHomeDir, "auth.json"), "{}", "utf8");
|
|
|
|
await execute({
|
|
runId: "run-target",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: "session-123",
|
|
sessionParams: {
|
|
sessionId: "session-123",
|
|
cwd: managedRemoteWorkspace,
|
|
remoteExecution: {
|
|
transport: "ssh",
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteCwd: managedRemoteWorkspace,
|
|
},
|
|
},
|
|
sessionDisplayId: "session-123",
|
|
taskKey: null,
|
|
},
|
|
config: {
|
|
command: "codex",
|
|
env: {
|
|
CODEX_HOME: codexHomeDir,
|
|
},
|
|
},
|
|
context: {
|
|
paperclipWorkspace: {
|
|
cwd: workspaceDir,
|
|
source: "project_primary",
|
|
},
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "ssh",
|
|
remoteCwd: "/remote/workspace",
|
|
spec: {
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/remote/workspace",
|
|
remoteCwd: "/remote/workspace",
|
|
privateKey: "PRIVATE KEY",
|
|
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA",
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
});
|
|
|
|
expect(syncDirectoryToSsh).toHaveBeenCalledTimes(1);
|
|
expect(runChildProcess).toHaveBeenCalledTimes(1);
|
|
const call = runChildProcess.mock.calls[0] as unknown as
|
|
| [string, string, string[], { env: Record<string, string>; remoteExecution?: { remoteCwd: string } | null }]
|
|
| undefined;
|
|
expect(call?.[2]).toEqual([
|
|
"exec",
|
|
"--json",
|
|
"resume",
|
|
"session-123",
|
|
"-",
|
|
]);
|
|
expect(call?.[3].env.CODEX_HOME).toBe(`${managedRemoteWorkspace}/.paperclip-runtime/codex/home`);
|
|
expect(call?.[3].remoteExecution?.remoteCwd).toBe(managedRemoteWorkspace);
|
|
});
|
|
|
|
it("reads post-run credential telemetry from the remote CODEX_HOME", async () => {
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-codex-remote-telemetry-"));
|
|
cleanupDirs.push(rootDir);
|
|
const workspaceDir = path.join(rootDir, "workspace");
|
|
const codexHomeDir = path.join(rootDir, "codex-home");
|
|
const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-telemetry/workspace";
|
|
const remoteAuthPath = `${managedRemoteWorkspace}/.paperclip-runtime/codex/home/auth.json`;
|
|
await mkdir(workspaceDir, { recursive: true });
|
|
await mkdir(codexHomeDir, { recursive: true });
|
|
await writeFile(
|
|
path.join(codexHomeDir, "auth.json"),
|
|
JSON.stringify({
|
|
tokens: { refresh_token: "seed-refresh-token-secret" },
|
|
last_refresh: "2999-01-01T00:00:00.000Z",
|
|
}),
|
|
"utf8",
|
|
);
|
|
runSshCommand.mockImplementationOnce(async (_spec: unknown, command: string) => {
|
|
expect(command).toContain(remoteAuthPath);
|
|
return {
|
|
stdout: JSON.stringify({
|
|
tokens: { refresh_token: "remote-rotated-refresh-token-secret" },
|
|
last_refresh: "2999-01-01T00:05:00.000Z",
|
|
}),
|
|
stderr: "",
|
|
};
|
|
});
|
|
|
|
const result = await execute({
|
|
runId: "run-telemetry",
|
|
agent: {
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
name: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
},
|
|
runtime: {
|
|
sessionId: null,
|
|
sessionParams: null,
|
|
sessionDisplayId: null,
|
|
taskKey: null,
|
|
},
|
|
config: {
|
|
command: "codex",
|
|
env: {
|
|
CODEX_HOME: codexHomeDir,
|
|
},
|
|
},
|
|
context: {
|
|
paperclipWorkspace: {
|
|
cwd: workspaceDir,
|
|
source: "project_primary",
|
|
},
|
|
},
|
|
executionTransport: {
|
|
remoteExecution: {
|
|
host: "127.0.0.1",
|
|
port: 2222,
|
|
username: "fixture",
|
|
remoteWorkspacePath: "/remote/workspace",
|
|
remoteCwd: "/remote/workspace",
|
|
privateKey: "PRIVATE KEY",
|
|
knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA",
|
|
strictHostKeyChecking: true,
|
|
},
|
|
},
|
|
onLog: async () => {},
|
|
});
|
|
|
|
const telemetry = (result.resultJson as Record<string, unknown>).codexCredentialTelemetry;
|
|
expect(telemetry).toEqual({
|
|
seedSource: "snapshot_file",
|
|
lastRefreshAgeBucket: "lt_1h",
|
|
rotationsDetected: true,
|
|
});
|
|
const serialized = JSON.stringify(telemetry);
|
|
expect(serialized).not.toContain("seed-refresh-token-secret");
|
|
expect(serialized).not.toContain("remote-rotated-refresh-token-secret");
|
|
expect(runSshCommand).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|