Files
PaperClipAI/server/src/__tests__/claude-local-adapter-environment.test.ts
T
DottaandPaperclip 45c99a0d06 fix(adapters): default legacy harnesses and connected tools to full auto (#13693)
## Thinking Path

> - Paperclip lets people manage AI agents and their work.
> - Legacy adapters launch provider CLIs and expose connected tools.
> - Existing defaults did not consistently grant full automatic
permission.
> - Remote Claude used a fixed tool list that omitted MCP tools and
future tools.
> - Direct Codex launches and OpenCode configuration also used narrower
defaults.
> - This change gives all these paths the same full-auto default as
native runners.
> - Explicit restrictive settings continue to work.

## Linked Issues or Issue Description

Refs #13686. This PR is stacked on that native-runner and
task-reassignment PR. Merge #13686 first.

Related: #831 (constructed Claude agents), #1935 (adapter-switching
permission defaults).

## What Changed

- Use actual Claude permission bypass for local and remote runs and
probes. Remove the fixed tool list so MCP and future provider tools are
included.
- Identify actual managed sandbox targets to Claude with `IS_SANDBOX=1`.
Do not mark ordinary host execution as a sandbox.
- Default direct Codex execution to approval and sandbox bypass,
matching agent creation. Preserve explicit false, CLI profiles, sandbox
modes, approval policy, and network restrictions.
- Set OpenCode's full-auto runtime permission to `allow` for every tool
and connection. Preserve the existing explicit opt-out.
- Default Gemini probes to the same YOLO mode as execution. Make the
legacy ACP `default` alias use `approve-all` for fresh and resumed
sessions.
- Add default, opt-out, remote, probe, connected-tool, and resume
regression tests. Update adapter configuration documentation.
- Other adapter paths already request full automatic permission or have
no provider approval gate.

## Verification

- Full workspace `pnpm -r typecheck` and `pnpm build` passed locally
after rebasing onto current master. Targeted adapter/server and legacy
ACP tests passed, including defaults, explicit opt-outs, remote
launches, connected tools, and fresh/resumed sessions.
- Greptile reviewed current head
`8ca135eaffcf9cfdba6f1368e896a781a0891d50` at **5/5**. The security
reviewer acknowledged the documented full-auto requirement. Acknowledged
discussions are resolved.
- Current head has **54 passing checks**. [PR
checks](https://github.com/paperclipai/paperclip/pull/13693/checks). The
process-adapter signoff browser shard passed on one retry after its
first attempt exceeded a three-second issue-run wait.
- **Six native Claude/Codex real-provider cases passed on their first
attempt, with cleanup passing**, against the combined branch: plans,
reassignment, and backlog creation/status. [Campaign and downloadable
evidence](https://github.com/paperclipai/paperclip/actions/runs/35469926548).
This does not claim a real-provider run of every legacy adapter.
- The live-tested revision is
`a37881c824dcd7170380fc4b788732fc743e5da7`. The current head differs
only in the corrected heartbeat test expectation; application code is
identical.
- The campaign result-enforcement job passed. The separate report
publisher failed during frozen dependency installation because the
trusted workflow's patched-dependency configuration does not match its
lockfile. Passing case evidence remains downloadable from the workflow.
- Full-suite coverage comes from CI partitions. The separate unsharded
local run was stopped after the corresponding CI partitions passed; it
is not counted as a completed local run.

## Risks

- Missing permission settings now grant all provider operations,
including connected tools. OpenCode full-auto also overrides ambient
provider permission rules. An explicit Paperclip permission opt-out
preserves restrictive behavior.
- Claude refuses full bypass as root outside an identified sandbox.
Ordinary host deployments must run Claude as a non-root user. Managed
sandbox launches include the required marker.
- These defaults do not grant additional Paperclip roles, connections,
or company access. Existing controller authorization and governance
still apply.
- This PR depends on #13686. Retarget it to master after that PR merges.

## Model Used

OpenAI Codex, based on GPT-6, with code execution and repository tools.
The exact deployment model ID and context-window size are not exposed in
this session.

## 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 searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-19 16:49:18 -05:00

475 lines
17 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 { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
import { resetClaudeCliCapabilitiesCacheForTests, 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;
const ORIGINAL_CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR;
const ORIGINAL_PAPERCLIP_HOME = process.env.PAPERCLIP_HOME;
const ORIGINAL_PAPERCLIP_INSTANCE_ID = process.env.PAPERCLIP_INSTANCE_ID;
afterEach(() => {
resetClaudeCliCapabilitiesCacheForTests();
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;
}
if (ORIGINAL_CLAUDE_CONFIG_DIR === undefined) {
delete process.env.CLAUDE_CONFIG_DIR;
} else {
process.env.CLAUDE_CONFIG_DIR = ORIGINAL_CLAUDE_CONFIG_DIR;
}
if (ORIGINAL_PAPERCLIP_HOME === undefined) {
delete process.env.PAPERCLIP_HOME;
} else {
process.env.PAPERCLIP_HOME = ORIGINAL_PAPERCLIP_HOME;
}
if (ORIGINAL_PAPERCLIP_INSTANCE_ID === undefined) {
delete process.env.PAPERCLIP_INSTANCE_ID;
} else {
process.env.PAPERCLIP_INSTANCE_ID = ORIGINAL_PAPERCLIP_INSTANCE_ID;
}
});
async function writeHelpWithoutEffortClaudeCommand(commandPath: string): Promise<void> {
const script = `#!/usr/bin/env node
const argv = process.argv.slice(2);
if (argv.includes("--help")) {
process.stdout.write("Usage: claude [options]\\n --print\\n --model <id>\\n");
process.exit(0);
}
if (argv.includes("--effort")) {
process.stderr.write("error: unknown option '--effort'\\n");
process.exit(1);
}
console.log(JSON.stringify({ type: "assistant", message: { content: [{ type: "text", text: "hello" }] } }));
console.log(JSON.stringify({ type: "result", result: "hello", usage: { input_tokens: 1, cache_read_input_tokens: 0, output_tokens: 1 } }));
`;
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 runChildProcess(
`claude-envtest-sandbox-${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("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: {
engine: "cli",
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("reports an explicitly configured API key as normal authentication", 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: {
engine: "cli",
command: process.execPath,
cwd: process.cwd(),
env: {
ANTHROPIC_API_KEY: "sk-test-config",
},
},
});
expect(result.status).toBe("pass");
expect(
result.checks.some(
(check) =>
check.code === "claude_anthropic_api_key_overrides_subscription" &&
check.level === "info",
),
).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: {
engine: "cli",
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: {
engine: "cli",
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: {
engine: "cli",
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: {
engine: "cli",
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: {
engine: "cli",
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 full permission bypass for sandbox hello probes", async () => {
const executeCalls: Array<{ command: string; args?: string[]; env?: Record<string, string> }> = [];
const result = await testEnvironment({
companyId: "company-1",
adapterType: "claude_local",
config: {
engine: "cli",
command: "claude",
},
executionTarget: {
kind: "remote",
transport: "sandbox",
providerKey: "cloudflare",
remoteCwd: "/workspace/paperclip",
runner: {
execute: async (input) => {
executeCalls.push({ command: input.command, args: input.args, env: input.env });
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).toContain("--dangerously-skip-permissions");
expect(probeCall?.args).not.toContain("--permission-mode");
expect(probeCall?.args).not.toContain("--allowedTools");
expect(probeCall?.env?.IS_SANDBOX).toBe("1");
});
it("uses the managed Claude config seed for sandbox hello probes", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-claude-envtest-managed-config-"));
const sourceConfigDir = path.join(root, "host-claude");
const remoteHome = path.join(root, "remote-home");
const remoteWorkspace = path.join(root, "remote-workspace");
const commandPath = path.join(root, "claude");
await fs.mkdir(sourceConfigDir, { recursive: true });
await fs.mkdir(path.join(remoteHome, ".claude"), { recursive: true });
await fs.mkdir(remoteWorkspace, { recursive: true });
await fs.writeFile(path.join(sourceConfigDir, "settings.json"), JSON.stringify({
theme: "dark",
permissions: { defaultMode: "bypassPermissions" },
hooks: { PreToolUse: [{ matcher: "*" }] },
mcpServers: { local: { command: "secret-local-server" } },
permissionMode: "dontAsk",
skipDangerousModePermissionPrompt: true,
}), "utf8");
await fs.writeFile(path.join(sourceConfigDir, "CLAUDE.md"), "seed instructions", "utf8");
await fs.writeFile(path.join(sourceConfigDir, "credentials.json"), JSON.stringify({ token: "local" }), "utf8");
await fs.writeFile(path.join(remoteHome, ".claude", ".credentials.json"), JSON.stringify({ token: "remote" }), "utf8");
await fs.writeFile(commandPath, `#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const configDir = process.env.CLAUDE_CONFIG_DIR || "";
function fail(message) {
process.stderr.write(message + "\\n");
process.exit(2);
}
if (!configDir.includes(".paperclip-runtime/claude/config")) {
fail("missing managed CLAUDE_CONFIG_DIR: " + configDir);
}
const settings = JSON.parse(fs.readFileSync(path.join(configDir, "settings.json"), "utf8"));
if (settings.permissions?.defaultMode !== "default") fail("permissions were not sanitized");
if (settings.hooks || settings.mcpServers || settings.permissionMode || settings.skipDangerousModePermissionPrompt) {
fail("local-only settings leaked into sandbox config");
}
if (fs.existsSync(path.join(configDir, "credentials.json"))) fail("host credentials leaked into sandbox config");
const remoteCredentials = JSON.parse(fs.readFileSync(path.join(configDir, ".credentials.json"), "utf8"));
if (remoteCredentials.token !== "remote") fail("sandbox credentials were not preserved");
if (fs.readFileSync(path.join(configDir, "CLAUDE.md"), "utf8") !== "seed instructions") {
fail("CLAUDE.md seed was not materialized");
}
console.log(JSON.stringify({ type: "assistant", message: { content: [{ type: "text", text: "hello" }] } }));
console.log(JSON.stringify({ type: "result", result: "hello", usage: { input_tokens: 1, cache_read_input_tokens: 0, output_tokens: 1 } }));
`, "utf8");
await fs.chmod(commandPath, 0o755);
process.env.CLAUDE_CONFIG_DIR = sourceConfigDir;
process.env.PAPERCLIP_HOME = path.join(root, "paperclip-home");
process.env.PAPERCLIP_INSTANCE_ID = "test-instance";
try {
const result = await testEnvironment({
companyId: "company-1",
adapterType: "claude_local",
config: {
engine: "cli",
command: commandPath,
env: { HOME: remoteHome },
},
executionTarget: {
kind: "remote",
transport: "sandbox",
providerKey: "daytona",
remoteCwd: remoteWorkspace,
runner: createLocalSandboxRunner(),
},
environmentName: "QA Daytona",
});
expect(result.checks.some((check) => check.code === "claude_managed_config_dir")).toBe(true);
expect(result.checks.some((check) => check.code === "claude_hello_probe_passed")).toBe(true);
expect(result.checks.some((check) => check.code === "claude_hello_probe_failed")).toBe(false);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
it("warns and omits --effort for sandbox probes when the installed Claude CLI does not advertise it", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-claude-envtest-sandbox-effort-"));
const workspace = path.join(root, "workspace");
const remoteWorkspace = path.join(root, "remote-workspace");
const commandPath = path.join(root, "claude");
await fs.mkdir(workspace, { recursive: true });
await fs.mkdir(remoteWorkspace, { recursive: true });
await writeHelpWithoutEffortClaudeCommand(commandPath);
try {
const result = await testEnvironment({
companyId: "company-1",
adapterType: "claude_local",
config: {
engine: "cli",
command: commandPath,
cwd: workspace,
effort: "low",
},
executionTarget: {
kind: "remote",
transport: "sandbox",
providerKey: "daytona",
remoteCwd: remoteWorkspace,
runner: createLocalSandboxRunner(),
},
environmentName: "QA Daytona",
});
expect(result.checks.some((check) => check.code === "claude_effort_flag_unsupported")).toBe(true);
expect(result.checks.some((check) => check.code === "claude_hello_probe_passed")).toBe(true);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
});
});