743 lines
32 KiB
TypeScript
743 lines
32 KiB
TypeScript
import { describe, expect, it, afterEach } from 'vitest';
|
||
import { createHash, generateKeyPairSync } from 'node:crypto';
|
||
import { mkdtemp, writeFile, rm, chmod, mkdir, readFile, realpath } from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import {
|
||
ClaudeCodeAdapter,
|
||
CodeBuddyAdapter,
|
||
CodexAdapter,
|
||
ComposeDeploymentAdapter,
|
||
executeCommand,
|
||
FileNonceStore,
|
||
GitWorkspace,
|
||
KubernetesDeploymentAdapter,
|
||
signRunnerTask,
|
||
validateRelease,
|
||
probeClaudeSandbox,
|
||
probeCodexSandbox,
|
||
verifyRunnerTask,
|
||
verifyWorkspace,
|
||
type CodexFactory,
|
||
type Command,
|
||
type CommandResult,
|
||
type ReleaseManifest,
|
||
type RunnerTask,
|
||
} from '../src/index.js';
|
||
|
||
const dirs: string[] = [];
|
||
async function temp() {
|
||
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-runtime-'));
|
||
dirs.push(directory);
|
||
return directory;
|
||
}
|
||
afterEach(async () => {
|
||
await Promise.all(dirs.splice(0).map(directory => rm(directory, { recursive: true, force: true })));
|
||
});
|
||
const release: ReleaseManifest = {
|
||
schemaVersion: 1,
|
||
id: 'release-1',
|
||
tenantId: 'tenant-1',
|
||
environmentId: 'env-1',
|
||
sourceCommit: 'a'.repeat(40),
|
||
image: `registry.example/web@sha256:${'b'.repeat(64)}`,
|
||
containerPort: 3000,
|
||
healthPath: '/health',
|
||
expectedPreviousReleaseId: null,
|
||
smokeChecks: [{ path: '/', expectedStatus: 200, bodyIncludes: 'ready' }],
|
||
};
|
||
|
||
describe('signed outbound runner tasks', () => {
|
||
const keys = generateKeyPairSync('ed25519');
|
||
const publicKeys = { current: keys.publicKey.export({ type: 'spki', format: 'pem' }).toString() };
|
||
const task: RunnerTask = {
|
||
schemaVersion: 1,
|
||
id: 'task-1',
|
||
tenantId: 'tenant-1',
|
||
environmentId: 'env-1',
|
||
nonce: 'nonce-1',
|
||
issuedAt: new Date(1_000_000).toISOString(),
|
||
expiresAt: new Date(1_300_000).toISOString(),
|
||
action: 'deploy',
|
||
target: 'compose',
|
||
release,
|
||
};
|
||
const policy = { tenantId: task.tenantId, environmentId: task.environmentId, publicKeys, now: 1_100_000 };
|
||
it('accepts a scoped Ed25519 task and rejects tampering, expiry and wrong tenants', () => {
|
||
const envelope = signRunnerTask(task, 'current', keys.privateKey);
|
||
expect(verifyRunnerTask(envelope, policy)).toEqual(task);
|
||
expect(() => verifyRunnerTask({ ...envelope, payload: { ...task, action: 'reconcile' } }, policy)).toThrow(
|
||
'signature',
|
||
);
|
||
expect(() => verifyRunnerTask(envelope, { ...policy, now: 1_400_000 })).toThrow('expired');
|
||
expect(() => verifyRunnerTask(envelope, { ...policy, tenantId: 'tenant-2' })).toThrow('tenant/environment');
|
||
});
|
||
it('claims a nonce once, including concurrent calls and process-local store reconstruction', async () => {
|
||
const directory = await temp();
|
||
const claims = await Promise.all(
|
||
Array.from({ length: 8 }, () => new FileNonceStore(directory).consume('tenant/env', 'nonce')),
|
||
);
|
||
expect(claims.filter(Boolean)).toHaveLength(1);
|
||
expect(await new FileNonceStore(directory).consume('tenant/env', 'nonce')).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('deployments fail closed', () => {
|
||
it('renders both targets without executing commands and never labels dry-run as verified', async () => {
|
||
let calls = 0;
|
||
const execute = async () => {
|
||
calls++;
|
||
throw new Error('must not execute');
|
||
};
|
||
expect((await new ComposeDeploymentAdapter({}, execute).deploy(release)).status).toBe('dry-run');
|
||
expect((await new KubernetesDeploymentAdapter({}, execute).deploy(release)).status).toBe('dry-run');
|
||
expect((await new KubernetesDeploymentAdapter({}, execute).deploy(release, { execute: true })).status).toBe(
|
||
'unconfigured',
|
||
);
|
||
expect(calls).toBe(0);
|
||
});
|
||
it('rejects mutable images and health URLs that can escape the target origin', () => {
|
||
expect(() => validateRelease({ ...release, image: 'registry.example/web:latest' })).toThrow('digest');
|
||
expect(() => validateRelease({ ...release, healthPath: '//metadata.example/' })).toThrow('same-origin');
|
||
});
|
||
it('renders bound architecture and refuses to silently omit secret references', async () => {
|
||
const configured = {
|
||
...release,
|
||
architecture: 'linux/arm64' as const,
|
||
configVersion: 'version-7',
|
||
secretVersionRefs: [],
|
||
};
|
||
expect(JSON.parse(new ComposeDeploymentAdapter().render(configured)).services.web.platform).toBe('linux/arm64');
|
||
const workload = JSON.parse(new KubernetesDeploymentAdapter().render(configured)).items[0];
|
||
expect(workload.spec.template.spec.nodeSelector['kubernetes.io/arch']).toBe('arm64');
|
||
expect(workload.metadata.annotations['wishforge.io/config-version']).toBe('version-7');
|
||
for (const adapter of [new ComposeDeploymentAdapter(), new KubernetesDeploymentAdapter()])
|
||
expect(
|
||
(await adapter.deploy({ ...configured, secretVersionRefs: ['vault/database@version-1'] }, { execute: true }))
|
||
.status,
|
||
).toBe('unconfigured');
|
||
});
|
||
it('requires observed Kubernetes generation, exact image and all replicas before verification', async () => {
|
||
const state = {
|
||
metadata: {
|
||
generation: 3,
|
||
annotations: JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[0].metadata.annotations,
|
||
},
|
||
spec: { template: { spec: { containers: [{ image: release.image }] } } },
|
||
status: { observedGeneration: 2, updatedReplicas: 1, availableReplicas: 1, replicas: 1 },
|
||
};
|
||
const stable = {
|
||
metadata: {
|
||
annotations: JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[0].metadata.annotations,
|
||
},
|
||
spec: {
|
||
selector: {
|
||
'app.kubernetes.io/name': JSON.parse(new KubernetesDeploymentAdapter().render(release)).items[1].spec
|
||
.selector['app.kubernetes.io/name'],
|
||
'wishforge.io/revision': createHash('sha256').update(release.id).digest('hex').slice(0, 12),
|
||
},
|
||
},
|
||
};
|
||
const adapter = new KubernetesDeploymentAdapter(
|
||
{
|
||
context: 'test',
|
||
namespace: 'test',
|
||
healthUrl: 'https://app.example',
|
||
candidateHealthUrlTemplate: 'http://{service}.test.svc',
|
||
},
|
||
async command => ({
|
||
exitCode: 0,
|
||
stdout: JSON.stringify(command.args.includes('service') ? stable : state),
|
||
stderr: '',
|
||
}),
|
||
async () => new Response('ready'),
|
||
);
|
||
expect((await adapter.reconcile(release)).status).toBe('applied');
|
||
state.status.observedGeneration = 3;
|
||
expect((await adapter.reconcile(release)).status).toBe('verified');
|
||
state.spec.template.spec.containers[0]!.image = 'other';
|
||
expect((await adapter.reconcile(release)).status).toBe('conflict');
|
||
});
|
||
});
|
||
|
||
describe('portable provider execution', () => {
|
||
it('never starts from ambient credentials', () => {
|
||
expect(new CodexAdapter().capabilities().availability).toBe('unconfigured');
|
||
expect(new CodeBuddyAdapter().capabilities().availability).toBe('unconfigured');
|
||
});
|
||
it('requires Codex turn.completed and does not forward worker secrets or raw reasoning', async () => {
|
||
const home = await temp();
|
||
let options: Record<string, unknown> = {};
|
||
const adapter = new CodexAdapter({
|
||
apiKey: 'tenant-key',
|
||
isolatedHome: home,
|
||
factory: async config => {
|
||
options = config;
|
||
return {
|
||
startThread: () => ({
|
||
runStreamed: async () => ({
|
||
events: (async function* () {
|
||
yield { type: 'thread.started', thread_id: 'session' };
|
||
yield { type: 'item.completed', item: { type: 'reasoning', text: 'private' } };
|
||
yield { type: 'item.completed', item: { type: 'agent_message', text: 'tenant-key output' } };
|
||
})(),
|
||
}),
|
||
}),
|
||
resumeThread: () => {
|
||
throw new Error('not used');
|
||
},
|
||
};
|
||
},
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect((await adapter.inspect('r')).status).toBe('failed');
|
||
expect(JSON.stringify(events)).not.toContain('private');
|
||
expect(JSON.stringify(events)).not.toContain('tenant-key');
|
||
expect(Object.keys(options.env as object).sort()).toEqual([
|
||
'CODEX_HOME',
|
||
'GIT_AUTHOR_EMAIL',
|
||
'GIT_AUTHOR_NAME',
|
||
'GIT_COMMITTER_EMAIL',
|
||
'GIT_COMMITTER_NAME',
|
||
'HOME',
|
||
'NO_PROXY',
|
||
'PATH',
|
||
'no_proxy',
|
||
]);
|
||
});
|
||
it('reports commands, file changes and plan updates from Codex without command output', async () => {
|
||
const home = await temp();
|
||
const adapter = new CodexAdapter({
|
||
apiKey: 'tenant-key',
|
||
isolatedHome: home,
|
||
factory: async () => ({
|
||
startThread: () => ({
|
||
runStreamed: async () => ({
|
||
events: (async function* () {
|
||
yield {
|
||
type: 'item.updated',
|
||
item: { type: 'todo_list', items: [{ text: 'Write tests', completed: false }] },
|
||
};
|
||
yield {
|
||
type: 'item.completed',
|
||
item: {
|
||
type: 'command_execution',
|
||
command: 'npm test',
|
||
aggregated_output: 'tenant-key leaked',
|
||
exit_code: 1,
|
||
status: 'failed',
|
||
},
|
||
};
|
||
yield {
|
||
type: 'item.completed',
|
||
item: { type: 'file_change', changes: [{ path: 'src/app.ts', kind: 'update' }], status: 'completed' },
|
||
};
|
||
yield { type: 'turn.completed', usage: { input_tokens: 3, output_tokens: 2 } };
|
||
})(),
|
||
}),
|
||
}),
|
||
resumeThread: () => {
|
||
throw new Error('not used');
|
||
},
|
||
}),
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect((await adapter.inspect('r')).status).toBe('completed');
|
||
expect(events.map(event => event.type)).toEqual([
|
||
'started',
|
||
'plan',
|
||
'command',
|
||
'file_change',
|
||
'usage',
|
||
'completed',
|
||
]);
|
||
expect(events[1]?.data).toEqual({ items: [{ text: 'Write tests', completed: false }] });
|
||
expect(events[2]?.data).toEqual({ command: 'npm test', exitCode: 1, status: 'failed' });
|
||
expect(events[3]?.data).toEqual({ status: 'completed', changes: [{ path: 'src/app.ts', kind: 'update' }] });
|
||
expect(JSON.stringify(events)).not.toContain('leaked');
|
||
});
|
||
it('runs Codex commands in its sandbox, and without it only where a development sandbox allows', async () => {
|
||
const home = await temp();
|
||
const threads: Array<Record<string, unknown>> = [];
|
||
const factory: CodexFactory = async () => ({
|
||
startThread: options => {
|
||
threads.push(options);
|
||
return {
|
||
runStreamed: async () => ({
|
||
events: (async function* () {
|
||
yield { type: 'turn.completed', usage: {} };
|
||
})(),
|
||
}),
|
||
};
|
||
},
|
||
resumeThread: () => {
|
||
throw new Error('not used');
|
||
},
|
||
});
|
||
const start = (adapter: CodexAdapter, runId: string) =>
|
||
adapter.start({ runId, tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const denied = async () => 'bwrap: No permissions to create new namespace';
|
||
await start(
|
||
new CodexAdapter({ apiKey: 'k', isolatedHome: home, factory, sandboxProbe: async () => undefined }),
|
||
'available',
|
||
);
|
||
expect(threads[0]?.sandboxMode).toBe('workspace-write');
|
||
// Otherwise every command the agent ran would fail; the run stops before the agent starts.
|
||
await expect(
|
||
start(new CodexAdapter({ apiKey: 'k', isolatedHome: home, factory, sandboxProbe: denied }), 'strict'),
|
||
).rejects.toMatchObject({ code: 'PROVIDER_SANDBOX_UNAVAILABLE' });
|
||
expect(threads).toHaveLength(1);
|
||
const development = new CodexAdapter({
|
||
apiKey: 'k',
|
||
isolatedHome: home,
|
||
factory,
|
||
sandboxProbe: denied,
|
||
allowUnsandboxed: true,
|
||
});
|
||
await start(development, 'development');
|
||
expect(threads[1]?.sandboxMode).toBe('danger-full-access');
|
||
const events = [];
|
||
for await (const event of development.events('development')) events.push(event);
|
||
expect(events[0]).toMatchObject({
|
||
type: 'started',
|
||
data: { unsandboxed: true, reason: 'bwrap: No permissions to create new namespace' },
|
||
});
|
||
});
|
||
it('probes Codex’s sandbox with the workspace-write policy in the isolated home', async () => {
|
||
const home = await temp();
|
||
const commands: Command[] = [];
|
||
const answering =
|
||
(result: CommandResult) =>
|
||
async (command: Command): Promise<CommandResult> => {
|
||
commands.push(command);
|
||
return result;
|
||
};
|
||
expect(await probeCodexSandbox(home, answering({ exitCode: 0, stdout: '', stderr: '' }))).toBeUndefined();
|
||
expect(commands[0]?.executable).toBe(process.execPath);
|
||
expect(commands[0]?.args[0]).toMatch(/@openai[/\\]codex[/\\]bin[/\\]codex\.js$/);
|
||
expect(commands[0]?.args.slice(1)).toEqual(['sandbox', '-c', 'sandbox_mode="workspace-write"', '--', 'true']);
|
||
expect(commands[0]?.env).toMatchObject({ HOME: home, CODEX_HOME: home });
|
||
const stderr = 'WARNING: proceeding without PATH aliases\nbwrap: No permissions to create new namespace\n';
|
||
expect(await probeCodexSandbox(home, answering({ exitCode: 1, stdout: '', stderr }))).toBe(
|
||
'bwrap: No permissions to create new namespace',
|
||
);
|
||
});
|
||
it('reports CodeBuddy messages and tool calls as progress', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'codebuddy-progress');
|
||
const lines = [
|
||
{
|
||
type: 'assistant',
|
||
message: {
|
||
content: [
|
||
{ type: 'text', text: 'Running the tests' },
|
||
{ type: 'tool_use', name: 'Bash', input: { command: 'npm test' } },
|
||
{ type: 'tool_use', name: 'Edit', input: { file_path: 'src/app.ts' } },
|
||
],
|
||
},
|
||
},
|
||
{ type: 'result', subtype: 'success', is_error: false, result: 'done' },
|
||
];
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node\n${lines.map(line => `console.log(${JSON.stringify(JSON.stringify(line))});`).join('\n')}\n`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new CodeBuddyAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { CODEBUDDY_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
allowUnsandboxed: true,
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect(events.map(event => event.type)).toEqual([
|
||
'started',
|
||
'output',
|
||
'command',
|
||
'file_change',
|
||
'output',
|
||
'completed',
|
||
]);
|
||
expect(events[2]?.data).toEqual({ command: 'npm test' });
|
||
expect(events[3]?.data).toEqual({ changes: [{ path: 'src/app.ts' }] });
|
||
});
|
||
it.each([
|
||
['Claude Code', ClaudeCodeAdapter, 'ANTHROPIC'],
|
||
['CodeBuddy', CodeBuddyAdapter, 'CODEBUDDY'],
|
||
] as const)('redacts explicit %s credentials but not gateway configuration', async (_label, Adapter, prefix) => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'cli-redaction');
|
||
const credentialEnv = {
|
||
[`${prefix}_API_KEY`]: 'fixture-explicit-key',
|
||
[`${prefix}_AUTH_TOKEN`]: 'fixture-explicit-token',
|
||
[`${prefix}_CUSTOM_HEADERS`]: 'X-Api-Key: fixture-header-secret',
|
||
ANTHROPIC_BASE_URL: 'https://gateway.invalid/v1',
|
||
ANTHROPIC_MODEL: 'fixture-model-1',
|
||
};
|
||
const visible = 'internal/wishforgeacceptance20261101/name.go A-B_01 https://gateway.invalid/v1 fixture-model-1';
|
||
const text = `${visible} fixture-explicit-key fixture-explicit-token X-Api-Key: fixture-header-secret`;
|
||
const redacted = `${visible} [REDACTED] [REDACTED] [REDACTED]`;
|
||
const lines = [
|
||
{
|
||
type: 'assistant',
|
||
message: {
|
||
content: [
|
||
{ type: 'text', text },
|
||
{ type: 'tool_use', name: 'Bash', input: { command: text } },
|
||
],
|
||
},
|
||
},
|
||
{ type: 'result', subtype: 'error_during_execution', is_error: true, result: `failed: ${text}` },
|
||
];
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node\n${lines.map(line => `console.log(${JSON.stringify(JSON.stringify(line))});`).join('\n')}\n`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new Adapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv,
|
||
isolationConfirmed: true,
|
||
allowUnsandboxed: true,
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect(events.map(event => event.type)).toEqual(['started', 'output', 'command', 'output', 'failed']);
|
||
expect(events[1]?.data).toEqual({ text: redacted });
|
||
expect(events[2]?.data).toEqual({ command: redacted });
|
||
expect(events[3]?.data).toEqual({ text: `failed: ${redacted}` });
|
||
expect(events[4]?.data).toEqual({ message: `failed: ${redacted}` });
|
||
expect(await adapter.inspect('r')).toMatchObject({ status: 'failed', error: `failed: ${redacted}` });
|
||
});
|
||
it('runs Claude Code headless with only workspace file tools and its own credential variable', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'claude-fixture');
|
||
// The fixture insists on print mode streaming JSON in restricted mode: no tools that run
|
||
// commands, file tools confined to the workspace and no repository settings, hooks or MCP.
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node
|
||
const args = process.argv.slice(2);
|
||
const flags = ['--output-format', 'stream-json', '--verbose', '--permission-mode', 'dontAsk', '--safe-mode', '--restricted', '--strict-mcp-config'];
|
||
const tools = 'Read,Edit,Write,Glob,Grep';
|
||
if (
|
||
args[0] !== '-p' ||
|
||
!flags.every(flag => args.includes(flag)) ||
|
||
args.includes('bypassPermissions') ||
|
||
args.includes('--settings') ||
|
||
!args[1].includes('only Read/Edit/Write/Glob/Grep file tools are available') ||
|
||
args[args.indexOf('--tools') + 1] !== tools ||
|
||
args[args.indexOf('--allowedTools') + 1] !== tools ||
|
||
!process.env.ANTHROPIC_API_KEY
|
||
)
|
||
process.exit(2);
|
||
console.log(JSON.stringify({type:'system',subtype:'init',session_id:'claude-session'}));
|
||
console.log(JSON.stringify({type:'assistant',message:{content:[{type:'tool_use',name:'Write',input:{file_path:'notes.md'}}]}}));
|
||
console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'done',usage:{input_tokens:3,output_tokens:4}}));
|
||
`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { ANTHROPIC_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
});
|
||
expect(adapter.capabilities()).toMatchObject({ provider: 'claude-code', availability: 'available' });
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect(events.map(event => event.type)).toEqual(['started', 'file_change', 'output', 'usage', 'completed']);
|
||
expect((await adapter.inspect('r')).sessionId).toBe('claude-session');
|
||
// CodeBuddy's variables are not Claude Code's; nothing else crosses into the CLI.
|
||
const mismatched = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { CODEBUDDY_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
});
|
||
await expect(
|
||
mismatched.start({ runId: 'x', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' }),
|
||
).rejects.toMatchObject({ code: 'CREDENTIAL_ENV_POLICY' });
|
||
});
|
||
it('carries the host egress proxy into Claude Code so a gateway behind it stays reachable', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'claude-fixture');
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node
|
||
if (process.env.https_proxy !== 'http://127.0.0.1:7890' || process.env.DATABASE_URL) process.exit(2);
|
||
console.log(JSON.stringify({type:'system',subtype:'init',session_id:'claude-session'}));
|
||
console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'done',usage:{input_tokens:1,output_tokens:1}}));
|
||
`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const saved = { proxy: process.env.https_proxy, database: process.env.DATABASE_URL };
|
||
process.env.https_proxy = 'http://127.0.0.1:7890';
|
||
process.env.DATABASE_URL = 'postgres://secret';
|
||
try {
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { ANTHROPIC_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
});
|
||
await adapter.start({ runId: 'p', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
const events = [];
|
||
for await (const event of adapter.events('p')) events.push(event);
|
||
expect(events.at(-1)?.type).toBe('completed');
|
||
} finally {
|
||
if (saved.proxy === undefined) delete process.env.https_proxy;
|
||
else process.env.https_proxy = saved.proxy;
|
||
if (saved.database === undefined) delete process.env.DATABASE_URL;
|
||
else process.env.DATABASE_URL = saved.database;
|
||
}
|
||
});
|
||
it('lets Claude Code run commands only inside its own sandbox, and keeps file tools where it cannot start', async () => {
|
||
const home = await temp();
|
||
const workspace = path.join(home, 'workspace');
|
||
await mkdir(workspace);
|
||
const binary = path.join(home, 'claude-sandbox-fixture');
|
||
// The fixture records how it was started for the test to inspect.
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node
|
||
require('node:fs').writeFileSync(${JSON.stringify(path.join(home, 'invocation.json'))}, JSON.stringify({ args: process.argv.slice(2), env: process.env }));
|
||
console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'done'}));
|
||
`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const start = async (probe: () => Promise<string | undefined>) => {
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolatedHome: path.join(home, 'provider-home'),
|
||
credentialEnv: { ANTHROPIC_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
commandSandbox: {
|
||
readable: ['/opt/module-cache'],
|
||
unreadable: ['/srv/worker'],
|
||
env: { GOFLAGS: '-mod=readonly' },
|
||
probe,
|
||
},
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace, baseCommit: 'a'.repeat(40), prompt: 'Change it' });
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
const invocation = JSON.parse(await readFile(path.join(home, 'invocation.json'), 'utf8'));
|
||
return { events, args: invocation.args as string[], env: invocation.env as Record<string, string> };
|
||
};
|
||
const flag = (args: string[], name: string) => args[args.indexOf(name) + 1];
|
||
|
||
const sandboxed = await start(async () => undefined);
|
||
expect(flag(sandboxed.args, '--tools')).toBe('Read,Edit,Write,Glob,Grep,Bash');
|
||
// Only sandboxed commands are allowed, by the sandbox's own setting; no rule allows Bash.
|
||
expect(flag(sandboxed.args, '--allowedTools')).toBe('Read,Edit,Write,Glob,Grep');
|
||
expect(sandboxed.args).toEqual(expect.arrayContaining(['--restricted', 'dontAsk']));
|
||
const { sandbox } = JSON.parse(flag(sandboxed.args, '--settings') ?? '{}');
|
||
expect(sandbox).toMatchObject({
|
||
enabled: true,
|
||
failIfUnavailable: true,
|
||
autoAllowBashIfSandboxed: true,
|
||
allowUnsandboxedCommands: false,
|
||
network: { allowedDomains: [], strictAllowlist: true },
|
||
credentials: { envVars: [{ name: 'ANTHROPIC_API_KEY', mode: 'deny' }] },
|
||
});
|
||
expect(sandbox.network.allowLocalBinding).toBeUndefined();
|
||
const scratch = await realpath(path.join(home, 'provider-home', 'command-scratch'));
|
||
expect(sandbox.filesystem.denyRead).toEqual(
|
||
expect.arrayContaining([os.homedir(), await realpath(path.join(home, 'provider-home')), '/srv/worker']),
|
||
);
|
||
expect(sandbox.filesystem.allowRead).toEqual([await realpath(workspace), scratch, '/opt/module-cache']);
|
||
expect(sandbox.filesystem.allowWrite).toEqual([scratch]);
|
||
expect(sandboxed.env).toMatchObject({
|
||
ANTHROPIC_API_KEY: 'test-key',
|
||
GOCACHE: path.join(scratch, 'go-build'),
|
||
GOFLAGS: '-mod=readonly',
|
||
GIT_CONFIG_GLOBAL: '/dev/null',
|
||
});
|
||
expect(sandboxed.args[1]).toMatch(/^Change it\n\nExecution contract: .*Bash runs in a sandbox/);
|
||
expect(sandboxed.events[0]).toMatchObject({ type: 'started', data: { commands: 'sandboxed' } });
|
||
|
||
const unavailable = await start(async () => 'bwrap: No permissions to create new namespace');
|
||
expect(unavailable.args).not.toContain('--settings');
|
||
expect(flag(unavailable.args, '--tools')).toBe('Read,Edit,Write,Glob,Grep');
|
||
expect(unavailable.args[1]).toContain('only Read/Edit/Write/Glob/Grep file tools are available');
|
||
expect(unavailable.env.GOCACHE).toBeUndefined();
|
||
expect(unavailable.events[0]).toMatchObject({
|
||
type: 'started',
|
||
data: { commands: 'none', reason: 'bwrap: No permissions to create new namespace' },
|
||
});
|
||
});
|
||
it('probes whether Claude Code can start its command sandbox here', async () => {
|
||
const calls: string[][] = [];
|
||
const ready = await probeClaudeSandbox(async command => {
|
||
calls.push([command.executable, ...command.args]);
|
||
return { exitCode: 0, stdout: '', stderr: '' };
|
||
});
|
||
const denied = await probeClaudeSandbox(async () => ({ exitCode: 1, stdout: '', stderr: 'denied here' }));
|
||
if (process.platform === 'darwin') {
|
||
expect(ready).toBeUndefined();
|
||
expect(calls[0]?.[0]).toBe('/usr/bin/sandbox-exec');
|
||
expect(denied).toBe('denied here');
|
||
} else if (process.platform === 'linux') {
|
||
expect(ready).toBeUndefined();
|
||
expect(calls.map(call => call[1])).toEqual(['socat', 'bwrap']);
|
||
expect(calls[1]).toEqual(expect.arrayContaining(['--unshare-all', '--proc']));
|
||
expect(denied).toBe('socat is not available: denied here');
|
||
} else expect(ready).toMatch(/no command sandbox/);
|
||
});
|
||
it('starts CodeBuddy, which has no command sandbox, only in a development sandbox and says so', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'codebuddy-unsandboxed');
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node\nconsole.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'done'}));\n`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const config = { binary, isolatedHome: home, credentialEnv: { CODEBUDDY_API_KEY: 'test-key' } };
|
||
const start = { runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' };
|
||
await expect(new CodeBuddyAdapter({ ...config, isolationConfirmed: true }).start(start)).rejects.toMatchObject({
|
||
code: 'PROVIDER_SANDBOX_UNAVAILABLE',
|
||
});
|
||
const adapter = new CodeBuddyAdapter({ ...config, isolationConfirmed: true, allowUnsandboxed: true });
|
||
await adapter.start(start);
|
||
const events = [];
|
||
for await (const event of adapter.events('r')) events.push(event);
|
||
expect(events[0]).toMatchObject({ type: 'started', data: { unsandboxed: true, reason: 'CodeBuddy has none' } });
|
||
expect((await adapter.inspect('r')).status).toBe('completed');
|
||
});
|
||
it('runs the configured headless executable and requires its structured final result', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'codebuddy-fixture');
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node\nconsole.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'implemented'}));\n`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new CodeBuddyAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { CODEBUDDY_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
allowUnsandboxed: true,
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
for await (const _event of adapter.events('r')) {
|
||
/* consume */
|
||
}
|
||
expect((await adapter.inspect('r')).status).toBe('completed');
|
||
});
|
||
it('stops a CLI whose output passes its limit, which is 256 MiB unless configured', async () => {
|
||
const home = await temp();
|
||
const binary = path.join(home, 'codebuddy-chatty-fixture');
|
||
// 20 lines of 1 MiB each: more than the former 16 MiB cap, less than the default.
|
||
await writeFile(
|
||
binary,
|
||
`#!/usr/bin/env node
|
||
const pad = 'x'.repeat(1024 * 1024);
|
||
for (let i = 0; i < 20; i++) console.log(JSON.stringify({ type: 'user', pad }));
|
||
console.log(JSON.stringify({ type: 'result', subtype: 'success', is_error: false, result: 'done' }));
|
||
`,
|
||
);
|
||
await chmod(binary, 0o700);
|
||
const run = async (maxOutputBytes?: number) => {
|
||
const adapter = new CodeBuddyAdapter({
|
||
binary,
|
||
isolatedHome: home,
|
||
credentialEnv: { CODEBUDDY_API_KEY: 'test-key' },
|
||
isolationConfirmed: true,
|
||
allowUnsandboxed: true,
|
||
...(maxOutputBytes ? { maxOutputBytes } : {}),
|
||
});
|
||
await adapter.start({ runId: 'r', tenantId: 't', workspace: home, baseCommit: 'a'.repeat(40), prompt: 'test' });
|
||
for await (const _event of adapter.events('r')) {
|
||
/* consume */
|
||
}
|
||
return adapter.inspect('r');
|
||
};
|
||
expect(await run()).toMatchObject({ status: 'completed' });
|
||
expect(await run(4 * 1024 * 1024)).toMatchObject({ status: 'failed', error: 'CLI output exceeded 4 MiB' });
|
||
});
|
||
});
|
||
|
||
describe('isolated Git checkpoints and independent verification', () => {
|
||
it('includes new files in a committed portable patch and verifies that exact SHA', async () => {
|
||
const root = await temp();
|
||
const git = new GitWorkspace();
|
||
const prepared = await git.create({ root, runId: 'run-1' });
|
||
await writeFile(path.join(prepared.workspace, 'hello.txt'), 'hello\n');
|
||
const checkpoint = await git.checkpoint(prepared.workspace, prepared.baseCommit, 'run-1', 'test');
|
||
expect(checkpoint.patch).toContain('hello.txt');
|
||
expect(checkpoint.sourceCommit).not.toBe(prepared.baseCommit);
|
||
const report = await verifyWorkspace({
|
||
workspace: prepared.workspace,
|
||
sourceCommit: checkpoint.sourceCommit,
|
||
commands: [[process.execPath, '-e', 'process.exit(0)']],
|
||
isolation: 'host',
|
||
});
|
||
expect(report).toMatchObject({ passed: true, isolation: 'host' });
|
||
await expect(
|
||
verifyWorkspace({
|
||
workspace: prepared.workspace,
|
||
sourceCommit: checkpoint.sourceCommit,
|
||
commands: [],
|
||
isolation: 'host',
|
||
}),
|
||
).rejects.toThrow('Protected');
|
||
});
|
||
it('concludes a resolved merge even when the result equals the branch it merged into', async () => {
|
||
const root = await temp();
|
||
const git = new GitWorkspace();
|
||
const { workspace } = await git.create({ root, runId: 'run-merge' });
|
||
const run = async (...args: string[]) =>
|
||
(
|
||
await executeCommand({
|
||
executable: 'git',
|
||
args: ['-c', 'user.name=Fixture', '-c', 'user.email=fixture@example.invalid', ...args],
|
||
cwd: workspace,
|
||
})
|
||
).stdout.trim();
|
||
const commit = async (content: string) => {
|
||
await writeFile(path.join(workspace, 'greeting.txt'), `${content}\n`);
|
||
await run('commit', '--quiet', '--all', '--allow-empty', '-m', content);
|
||
};
|
||
await writeFile(path.join(workspace, 'greeting.txt'), 'base\n');
|
||
await run('add', 'greeting.txt');
|
||
await commit('base');
|
||
await run('checkout', '--quiet', '-b', 'target');
|
||
await commit('target');
|
||
const targetCommit = await run('rev-parse', 'HEAD');
|
||
await run('checkout', '--quiet', '-');
|
||
await commit('ours');
|
||
await run('merge', '--no-ff', '--no-edit', 'target');
|
||
// The agent keeps its own side: the tree equals HEAD, but the merge must still be recorded.
|
||
await writeFile(path.join(workspace, 'greeting.txt'), 'ours\n');
|
||
const checkpoint = await git.checkpoint(workspace, targetCommit, 'run-merge', 'test');
|
||
expect((await run('rev-list', '--parents', '-n', '1', checkpoint.sourceCommit)).split(' ')).toContain(targetCommit);
|
||
});
|
||
it('rejects unverified promotion and credential-bearing URLs before touching a repository', async () => {
|
||
const git = new GitWorkspace();
|
||
await expect(
|
||
git.promoteVerifiedCandidate({
|
||
repository: '/unused',
|
||
targetRef: 'refs/heads/main',
|
||
expectedTargetCommit: 'a'.repeat(40),
|
||
candidateCommit: 'b'.repeat(40),
|
||
verifiedCommit: 'c'.repeat(40),
|
||
}),
|
||
).rejects.toThrow('exact candidate');
|
||
await expect(
|
||
git.create({ repository: 'https://user:secret@example.test/repo', root: await temp(), runId: 'run' }),
|
||
).rejects.toThrow('inline credentials');
|
||
});
|
||
});
|