forked from farhoodlabs/paperclip
b24c6909e8
## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies > - Each agent runs inside a sandbox environment so its CLI is isolated from the host > - Sandbox-backed adapter runs go through a small set of shared helpers — `ensureAdapterExecutionTargetCommandResolvable`, the sandbox callback bridge runner, and per-adapter `SANDBOX_INSTALL_COMMAND` strings > - When standing up new sandbox provider plugins, the existing helpers timed out, missed install fallbacks, or leaned on assumptions that only held for E2B > - Local adapters (`claude-local`, `codex-local`, `gemini-local`, `opencode-local`) needed slightly hardened probes so they could install themselves and validate inside *any* remote sandbox transport, not just E2B > - This pull request bundles those runtime fixes so future sandbox provider plugins inherit a working baseline > - The benefit is that adding a new sandbox provider plugin no longer requires touching adapter-utils or each local-adapter probe — the supporting infra is already correct ## What Changed - `packages/adapter-utils/src/execution-target.ts`: introduce `DEFAULT_REMOTE_SANDBOX_ADAPTER_TIMEOUT_SEC = 1800` and `resolveAdapterExecutionTargetTimeoutSec(...)`. Local and SSH adapters keep the historical "0 means no adapter timeout" behavior; sandbox-backed runs without an explicit `timeoutSec` get an explicit 30-minute default so remote installs and warm-up don't time out at the per-RPC default. Plumbed `timeoutSec` through `ensureAdapterExecutionTargetCommandResolvable` so install probes inside a sandbox honor adapter-level overrides instead of the bridge's 5-minute default. - `packages/adapters/opencode-local/src/index.ts`: switch `SANDBOX_INSTALL_COMMAND` from `npm install -g opencode-ai` to `curl -fsSL https://opencode.ai/install | bash`. The npm package reifies four large prebuilt-binary subpackages in parallel even though only one matches the host arch; on bandwidth-constrained sandboxes that blew through the 240s install budget. The official installer fetches one arch-specific binary and adds `$HOME/.opencode/bin` to PATH via `~/.bashrc`, which the sandbox-callback-bridge login-shell script already sources. - `packages/adapters/{claude,codex,gemini,opencode}-local/`: harden remote-target probes — pass `--skip-git-repo-check` for Codex when probing outside a repo, normalize permission flags for Claude, and add `*.remote.test.ts` coverage that exercises the remote-sandbox path explicitly for each adapter. - `packages/adapter-utils/src/sandbox-install-command.{ts,test.ts}` (new): add `buildSandboxNpmInstallCommand` helper. `server/src/adapters/registry.ts` + new `server/src/__tests__/adapter-registry.test.ts`: wire adapter install commands so they fall back to a writable `$HOME/.local` prefix when global install isn't available. - `server/src/__tests__/plugin-worker-manager.test.ts` + new `server/src/__tests__/fixtures/plugin-worker-delayed.cjs`: pin per-call timeout overrides so plugin worker exec calls honor the caller's timeout instead of the worker's default. ## Verification - `pnpm typecheck` - `pnpm exec vitest run --no-coverage packages/adapter-utils/src/execution-target-sandbox.test.ts packages/adapter-utils/src/sandbox-install-command.test.ts` - `pnpm exec vitest run --no-coverage server/src/__tests__/plugin-worker-manager.test.ts server/src/__tests__/adapter-registry.test.ts server/src/__tests__/claude-local-adapter-environment.test.ts server/src/__tests__/claude-local-execute.test.ts server/src/__tests__/gemini-local-adapter-environment.test.ts` - `pnpm exec vitest run --no-coverage packages/adapters/codex-local/src/server/test.remote.test.ts packages/adapters/opencode-local/src/server/test.remote.test.ts packages/adapters/codex-local/src/server/codex-args.test.ts packages/adapters/codex-local/src/server/execute.remote.test.ts packages/adapters/gemini-local/src/server/execute.remote.test.ts` All passing locally. ## Risks - Touches shared `adapter-utils` and several `*-local` adapters. The 30-minute default applies only when both (a) the target is `remote+sandbox` and (b) no `timeoutSec` is configured — local + SSH paths are unchanged. New test coverage was added alongside each behavior change to pin the contracts. - Switching OpenCode's install command to the official installer is a behavior change for any operator running OpenCode inside a remote sandbox. Local installs are unaffected (the `SANDBOX_INSTALL_COMMAND` only runs when an adapter is being installed inside a sandbox). - Low risk overall — no migrations, no API surface change. ## Model Used - Provider: Anthropic - Model: Claude Opus 4.7 (1M context) - Capabilities used: extended reasoning, tool use (Read/Edit/Bash/Grep), no code execution beyond local repo commands ## 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 — N/A, no UI change - [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 Co-authored-by: Paperclip <noreply@paperclip.ing>
282 lines
8.9 KiB
TypeScript
282 lines
8.9 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { testEnvironment } from "@paperclipai/adapter-claude-local/server";
|
|
|
|
const ORIGINAL_ANTHROPIC = process.env.ANTHROPIC_API_KEY;
|
|
const ORIGINAL_BEDROCK = process.env.CLAUDE_CODE_USE_BEDROCK;
|
|
const ORIGINAL_BEDROCK_URL = process.env.ANTHROPIC_BEDROCK_BASE_URL;
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_ANTHROPIC === undefined) {
|
|
delete process.env.ANTHROPIC_API_KEY;
|
|
} else {
|
|
process.env.ANTHROPIC_API_KEY = ORIGINAL_ANTHROPIC;
|
|
}
|
|
if (ORIGINAL_BEDROCK === undefined) {
|
|
delete process.env.CLAUDE_CODE_USE_BEDROCK;
|
|
} else {
|
|
process.env.CLAUDE_CODE_USE_BEDROCK = ORIGINAL_BEDROCK;
|
|
}
|
|
if (ORIGINAL_BEDROCK_URL === undefined) {
|
|
delete process.env.ANTHROPIC_BEDROCK_BASE_URL;
|
|
} else {
|
|
process.env.ANTHROPIC_BEDROCK_BASE_URL = ORIGINAL_BEDROCK_URL;
|
|
}
|
|
});
|
|
|
|
describe("claude_local environment diagnostics", () => {
|
|
it("returns a warning (not an error) when ANTHROPIC_API_KEY is set in host environment", async () => {
|
|
delete process.env.CLAUDE_CODE_USE_BEDROCK;
|
|
delete process.env.ANTHROPIC_BEDROCK_BASE_URL;
|
|
process.env.ANTHROPIC_API_KEY = "sk-test-host";
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd: process.cwd(),
|
|
},
|
|
});
|
|
|
|
expect(result.status).toBe("warn");
|
|
expect(
|
|
result.checks.some(
|
|
(check) =>
|
|
check.code === "claude_anthropic_api_key_overrides_subscription" &&
|
|
check.level === "warn",
|
|
),
|
|
).toBe(true);
|
|
expect(result.checks.some((check) => check.level === "error")).toBe(false);
|
|
});
|
|
|
|
it("returns a warning (not an error) when ANTHROPIC_API_KEY is set in adapter env", async () => {
|
|
delete process.env.ANTHROPIC_API_KEY;
|
|
delete process.env.CLAUDE_CODE_USE_BEDROCK;
|
|
delete process.env.ANTHROPIC_BEDROCK_BASE_URL;
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd: process.cwd(),
|
|
env: {
|
|
ANTHROPIC_API_KEY: "sk-test-config",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.status).toBe("warn");
|
|
expect(
|
|
result.checks.some(
|
|
(check) =>
|
|
check.code === "claude_anthropic_api_key_overrides_subscription" &&
|
|
check.level === "warn",
|
|
),
|
|
).toBe(true);
|
|
expect(result.checks.some((check) => check.level === "error")).toBe(false);
|
|
});
|
|
|
|
it("returns bedrock auth info when CLAUDE_CODE_USE_BEDROCK is set in host environment", async () => {
|
|
delete process.env.ANTHROPIC_API_KEY;
|
|
process.env.CLAUDE_CODE_USE_BEDROCK = "1";
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd: process.cwd(),
|
|
},
|
|
});
|
|
|
|
expect(
|
|
result.checks.some(
|
|
(check) =>
|
|
check.code === "claude_bedrock_auth" && check.level === "info",
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
result.checks.some(
|
|
(check) => check.code === "claude_subscription_mode_possible",
|
|
),
|
|
).toBe(false);
|
|
expect(result.checks.some((check) => check.level === "error")).toBe(false);
|
|
});
|
|
|
|
it("returns bedrock auth info when CLAUDE_CODE_USE_BEDROCK is set in adapter env", async () => {
|
|
delete process.env.ANTHROPIC_API_KEY;
|
|
delete process.env.CLAUDE_CODE_USE_BEDROCK;
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd: process.cwd(),
|
|
env: {
|
|
CLAUDE_CODE_USE_BEDROCK: "1",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(
|
|
result.checks.some(
|
|
(check) =>
|
|
check.code === "claude_bedrock_auth" && check.level === "info",
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
result.checks.some(
|
|
(check) => check.code === "claude_subscription_mode_possible",
|
|
),
|
|
).toBe(false);
|
|
expect(result.checks.some((check) => check.level === "error")).toBe(false);
|
|
});
|
|
|
|
it("bedrock auth takes precedence over missing ANTHROPIC_API_KEY", async () => {
|
|
delete process.env.ANTHROPIC_API_KEY;
|
|
process.env.CLAUDE_CODE_USE_BEDROCK = "1";
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd: process.cwd(),
|
|
},
|
|
});
|
|
|
|
const codes = result.checks.map((c) => c.code);
|
|
expect(codes).toContain("claude_bedrock_auth");
|
|
expect(codes).not.toContain("claude_subscription_mode_possible");
|
|
expect(codes).not.toContain("claude_anthropic_api_key_overrides_subscription");
|
|
});
|
|
|
|
it("creates a missing working directory when cwd is absolute", async () => {
|
|
const cwd = path.join(
|
|
os.tmpdir(),
|
|
`paperclip-claude-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: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
cwd,
|
|
},
|
|
});
|
|
|
|
expect(result.checks.some((check) => check.code === "claude_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("defaults remote probes to the environment remote cwd when adapter cwd is unset", async () => {
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: process.execPath,
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "test-provider",
|
|
remoteCwd: "/srv/paperclip/workspace",
|
|
runner: {
|
|
execute: async () => ({
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: "",
|
|
stderr: "",
|
|
pid: null,
|
|
startedAt: new Date().toISOString(),
|
|
}),
|
|
},
|
|
},
|
|
environmentName: "Linux Box",
|
|
});
|
|
|
|
expect(result.checks.some((check) => check.code === "claude_cwd_valid")).toBe(true);
|
|
expect(
|
|
result.checks.some(
|
|
(check) =>
|
|
check.code === "claude_cwd_valid" &&
|
|
check.message === "Working directory is valid: /srv/paperclip/workspace",
|
|
),
|
|
).toBe(true);
|
|
expect(result.checks.some((check) => check.code === "claude_cwd_invalid")).toBe(false);
|
|
});
|
|
|
|
it("uses --allowedTools instead of --dangerously-skip-permissions for sandbox hello probes", async () => {
|
|
const executeCalls: Array<{ command: string; args?: string[] }> = [];
|
|
|
|
const result = await testEnvironment({
|
|
companyId: "company-1",
|
|
adapterType: "claude_local",
|
|
config: {
|
|
command: "claude",
|
|
},
|
|
executionTarget: {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
providerKey: "cloudflare",
|
|
remoteCwd: "/workspace/paperclip",
|
|
runner: {
|
|
execute: async (input) => {
|
|
executeCalls.push({ command: input.command, args: input.args });
|
|
if (input.command === "claude") {
|
|
return {
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: [
|
|
JSON.stringify({ type: "assistant", message: { content: [{ type: "text", text: "hello" }] } }),
|
|
JSON.stringify({
|
|
type: "result",
|
|
result: "hello",
|
|
usage: { input_tokens: 1, cache_read_input_tokens: 0, output_tokens: 1 },
|
|
}),
|
|
].join("\n"),
|
|
stderr: "",
|
|
pid: null,
|
|
startedAt: new Date().toISOString(),
|
|
};
|
|
}
|
|
return {
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
stdout: "",
|
|
stderr: "",
|
|
pid: null,
|
|
startedAt: new Date().toISOString(),
|
|
};
|
|
},
|
|
},
|
|
},
|
|
environmentName: "QA Cloudflare",
|
|
});
|
|
|
|
expect(result.checks.some((check) => check.code === "claude_hello_probe_passed")).toBe(true);
|
|
const probeCall = executeCalls.find((call) => call.command === "claude");
|
|
expect(probeCall?.args).not.toContain("--dangerously-skip-permissions");
|
|
expect(probeCall?.args).not.toContain("--permission-mode");
|
|
// Sandbox probes pass `--allowedTools` so any tool invocation triggered
|
|
// by the probe prompt cannot stall waiting for an interactive permission
|
|
// approval that no human is present to answer.
|
|
expect(probeCall?.args).toContain("--allowedTools");
|
|
});
|
|
});
|