Files
paperclip/server/src/__tests__/cursor-local-adapter-environment.test.ts
T
Devin Foley f9cf1d2f6a Add cursor sandbox support and fix SSH workspace sync (#4803)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies
> - Agents can run inside sandboxed environments like E2B, or on remote
hosts via SSH
> - The cursor adapter needs to resolve `cursor-agent` inside sandbox
environments where it's installed in `~/.local/bin`
> - But when using the default `agent` command on a sandbox target, the
adapter didn't know to look in `~/.local/bin/cursor-agent`, causing
"command not found" failures
> - Additionally, repeated SSH runs failed because `git checkout` during
workspace sync conflicted with leftover `.paperclip-runtime` files from
previous runs
> - This PR adds sandbox-aware command resolution for cursor and fixes
the SSH workspace sync conflict
> - The benefit is cursor works in E2B sandboxes out of the box, and
repeated SSH runs don't fail on workspace sync

## What Changed

- `cursor-local`: Added `prepareCursorSandboxCommand` — on sandbox
targets, reads the remote `$HOME`, prepends `~/.local/bin` to PATH, and
prefers `~/.local/bin/cursor-agent` when the default command is
requested; tightened the sandbox command probe to validate the binary
exists before launching; preserves explicit custom command overrides
- `adapter-utils/ssh.ts`: Added `--force` to git checkout in SSH
workspace sync to handle `.paperclip-runtime` untracked file conflicts
from previous runs

## Verification

- `pnpm test` — all existing and new tests pass, including cursor
sandbox probe, sandbox execution, and custom command override tests
- `pnpm typecheck` — clean
- Manual: configure an E2B environment, run a cursor-local task, verify
it resolves cursor-agent from the sandbox install path

## Risks

- Low-medium. The `--force` flag on git checkout could discard
uncommitted changes in the remote workspace, but the workspace is
managed by Paperclip and should not contain user edits.

## Model Used

Codex GPT 5.4 high via Paperclip.

## 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 run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-04-29 16:12:06 -07:00

303 lines
9.8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
import { testEnvironment } from "@paperclipai/adapter-cursor-local/server";
async function writeFakeAgentCommand(binDir: string, argsCapturePath: string): Promise<string> {
const commandPath = path.join(binDir, "agent");
const script = `#!/usr/bin/env node
const fs = require("node:fs");
const outPath = process.env.PAPERCLIP_TEST_ARGS_PATH;
if (outPath) {
fs.writeFileSync(outPath, JSON.stringify(process.argv.slice(2)), "utf8");
}
console.log(JSON.stringify({
type: "assistant",
message: { content: [{ type: "output_text", text: "hello" }] },
}));
console.log(JSON.stringify({
type: "result",
subtype: "success",
result: "hello",
}));
`;
await fs.writeFile(commandPath, script, "utf8");
await fs.chmod(commandPath, 0o755);
return commandPath;
}
async function writeFakeCursorAgentCommand(commandPath: string): Promise<void> {
const script = `#!/usr/bin/env node
const fs = require("node:fs");
const outPath = process.env.PAPERCLIP_TEST_ARGS_PATH;
if (outPath) {
fs.writeFileSync(outPath, JSON.stringify({
command: process.argv[1],
argv: process.argv.slice(2),
path: process.env.PATH || "",
}), "utf8");
}
console.log(JSON.stringify({
type: "assistant",
message: { content: [{ type: "output_text", text: "hello" }] },
}));
console.log(JSON.stringify({
type: "result",
subtype: "success",
result: "hello",
}));
`;
await fs.mkdir(path.dirname(commandPath), { recursive: true });
await fs.writeFile(commandPath, script, "utf8");
await fs.chmod(commandPath, 0o755);
}
function createLocalSandboxRunner() {
let counter = 0;
return {
execute: async (input: {
command: string;
args?: string[];
cwd?: string;
env?: Record<string, string>;
stdin?: string;
timeoutMs?: number;
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
onSpawn?: (meta: { pid: number; startedAt: string }) => Promise<void>;
}) => {
counter += 1;
return await runChildProcess(`cursor-sandbox-env-${counter}`, input.command, input.args ?? [], {
cwd: input.cwd ?? process.cwd(),
env: input.env ?? {},
stdin: input.stdin,
timeoutSec: Math.max(1, Math.ceil((input.timeoutMs ?? 30_000) / 1000)),
graceSec: 5,
onLog: input.onLog ?? (async () => {}),
onSpawn: input.onSpawn
? async (meta) => input.onSpawn?.({ pid: meta.pid, startedAt: meta.startedAt })
: undefined,
});
},
};
}
describe("cursor environment diagnostics", () => {
beforeEach(() => {
vi.stubEnv("CURSOR_API_KEY", "");
});
afterEach(() => {
vi.unstubAllEnvs();
});
it("creates a missing working directory when cwd is absolute", async () => {
const cwd = path.join(
os.tmpdir(),
`paperclip-cursor-local-cwd-${Date.now()}-${Math.random().toString(16).slice(2)}`,
"workspace",
);
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: process.execPath,
cwd,
},
});
expect(result.checks.some((check) => check.code === "cursor_cwd_valid")).toBe(true);
expect(result.checks.some((check) => check.level === "error")).toBe(false);
const stats = await fs.stat(cwd);
expect(stats.isDirectory()).toBe(true);
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
});
it("adds --yolo to hello probe args by default", async () => {
const root = path.join(
os.tmpdir(),
`paperclip-cursor-local-probe-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);
const binDir = path.join(root, "bin");
const cwd = path.join(root, "workspace");
const argsCapturePath = path.join(root, "args.json");
await fs.mkdir(binDir, { recursive: true });
await writeFakeAgentCommand(binDir, argsCapturePath);
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: "agent",
cwd,
env: {
CURSOR_API_KEY: "test-key",
PAPERCLIP_TEST_ARGS_PATH: argsCapturePath,
PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`,
},
},
});
expect(result.status).toBe("pass");
const args = JSON.parse(await fs.readFile(argsCapturePath, "utf8")) as string[];
expect(args).toContain("--yolo");
await fs.rm(root, { recursive: true, force: true });
});
it("does not auto-add --yolo when extraArgs already bypass trust", async () => {
const root = path.join(
os.tmpdir(),
`paperclip-cursor-local-probe-extra-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);
const binDir = path.join(root, "bin");
const cwd = path.join(root, "workspace");
const argsCapturePath = path.join(root, "args.json");
await fs.mkdir(binDir, { recursive: true });
await writeFakeAgentCommand(binDir, argsCapturePath);
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: "agent",
cwd,
extraArgs: ["--yolo"],
env: {
CURSOR_API_KEY: "test-key",
PAPERCLIP_TEST_ARGS_PATH: argsCapturePath,
PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`,
},
},
});
expect(result.status).toBe("pass");
const args = JSON.parse(await fs.readFile(argsCapturePath, "utf8")) as string[];
expect(args).toContain("--yolo");
expect(args).not.toContain("--trust");
await fs.rm(root, { recursive: true, force: true });
});
it("prefers ~/.local/bin/cursor-agent for remote sandbox probes when using the default command", async () => {
const root = path.join(
os.tmpdir(),
`paperclip-cursor-sandbox-probe-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);
const homeDir = path.join(root, "home");
const remoteCwd = path.join(root, "workspace");
const argsCapturePath = path.join(root, "args.json");
const cursorAgentPath = path.join(homeDir, ".local", "bin", "cursor-agent");
await fs.mkdir(remoteCwd, { recursive: true });
await writeFakeCursorAgentCommand(cursorAgentPath);
const previousHome = process.env.HOME;
process.env.HOME = homeDir;
try {
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
executionTarget: {
kind: "remote",
transport: "sandbox",
remoteCwd,
runner: createLocalSandboxRunner(),
timeoutMs: 30_000,
},
config: {
command: "agent",
cwd: remoteCwd,
env: {
CURSOR_API_KEY: "test-key",
PAPERCLIP_TEST_ARGS_PATH: argsCapturePath,
},
},
});
expect(result.status).toBe("pass");
const capture = JSON.parse(await fs.readFile(argsCapturePath, "utf8")) as {
command: string;
argv: string[];
path: string;
};
expect(capture.command).toBe(cursorAgentPath);
expect(capture.path.split(":")[0]).toBe(path.join(homeDir, ".local", "bin"));
} finally {
if (previousHome === undefined) delete process.env.HOME;
else process.env.HOME = previousHome;
await fs.rm(root, { recursive: true, force: true });
}
});
it("emits cursor_native_auth_present when cli-config.json has authInfo and CURSOR_API_KEY is unset", async () => {
const root = path.join(
os.tmpdir(),
`paperclip-cursor-auth-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);
const cursorHome = path.join(root, ".cursor");
const cwd = path.join(root, "workspace");
try {
await fs.mkdir(cursorHome, { recursive: true });
await fs.writeFile(
path.join(cursorHome, "cli-config.json"),
JSON.stringify({
authInfo: {
email: "test@example.com",
displayName: "Test User",
userId: 12345,
},
}),
);
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: process.execPath,
cwd,
env: { CURSOR_HOME: cursorHome },
},
});
expect(result.checks.some((check) => check.code === "cursor_native_auth_present")).toBe(true);
expect(result.checks.some((check) => check.code === "cursor_api_key_missing")).toBe(false);
const authCheck = result.checks.find((check) => check.code === "cursor_native_auth_present");
expect(authCheck?.detail).toContain("test@example.com");
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
it("emits cursor_api_key_missing when neither env var nor native auth exists", async () => {
const root = path.join(
os.tmpdir(),
`paperclip-cursor-noauth-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);
const cursorHome = path.join(root, ".cursor");
const cwd = path.join(root, "workspace");
try {
await fs.mkdir(cursorHome, { recursive: true });
// No cli-config.json written
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: process.execPath,
cwd,
env: { CURSOR_HOME: cursorHome },
},
});
expect(result.checks.some((check) => check.code === "cursor_api_key_missing")).toBe(true);
expect(result.checks.some((check) => check.code === "cursor_native_auth_present")).toBe(false);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
});