In development the worker handed Claude Code both the tenant's key and the operator's logged-in home. The CLI signs in with the login it finds there, so the gateway saw a subscription credential it does not accept and answered 401 "Invalid API key provided" — the run then waited on provider_error with its work saved. A run that carries a credential of its own now gets an empty home, exactly like a Copilot answer; the operator's login stays for runs that have nothing else to sign in with.
246 lines
12 KiB
TypeScript
246 lines
12 KiB
TypeScript
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { providerBaseUrl, providerCatalog, providers, type Actor } from '@wishforge/contracts';
|
||
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
||
import { describeProviderEvent } from '../apps/api/src/domain/provider-events.js';
|
||
import { claudeCommandSandbox, providerRegistry } from '../apps/worker/src/providers.js';
|
||
import type { RunContext } from '../apps/worker/src/common.js';
|
||
import { MemoryStore } from './helpers/memory-store.js';
|
||
|
||
// FR-D1: an executor is an adapter plus one catalog entry and one worker registration.
|
||
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
|
||
const base = { NODE_ENV: 'test' };
|
||
|
||
async function availability(env: Record<string, string>) {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
// Stored connections; how they were encrypted does not matter for availability.
|
||
await store.transaction('alpha', async tx => {
|
||
for (const provider of providers)
|
||
await tx.put('provider_connection', {
|
||
id: `connection-${provider}`,
|
||
tenantId: 'alpha',
|
||
version: 1,
|
||
createdAt: new Date().toISOString(),
|
||
updatedAt: new Date().toISOString(),
|
||
provider,
|
||
label: provider,
|
||
status: 'active',
|
||
});
|
||
});
|
||
return Object.fromEntries(
|
||
(await new ControlPlane(store, { ...base, ...env }).providers(owner)).map(info => [info.id, info]),
|
||
);
|
||
}
|
||
|
||
describe('model gateways', () => {
|
||
it('takes each protocol’s base URL from the operator, never from the tenant', () => {
|
||
const env = {
|
||
WF_OPENAI_BASE_URL: 'https://ai-api.example/v1/',
|
||
WF_ANTHROPIC_BASE_URL: 'https://ai-api.example',
|
||
};
|
||
expect(providerBaseUrl(env, 'codex')).toBe('https://ai-api.example/v1');
|
||
expect(providerBaseUrl(env, 'claude-code')).toBe('https://ai-api.example');
|
||
expect(providerBaseUrl(env, 'codebuddy')).toBeUndefined();
|
||
expect(providerBaseUrl({}, 'codex')).toBeUndefined();
|
||
expect(providerBaseUrl({ WF_OPENAI_BASE_URL: 'http://127.0.0.1:8080/v1' }, 'codex')).toBe(
|
||
'http://127.0.0.1:8080/v1',
|
||
);
|
||
for (const value of [
|
||
'http://gateway.example/v1',
|
||
'https://key@gateway.example/v1',
|
||
'https://gateway.example/v1?x=1',
|
||
])
|
||
expect(() => providerBaseUrl({ WF_OPENAI_BASE_URL: value }, 'codex')).toThrow('WF_OPENAI_BASE_URL');
|
||
expect(() =>
|
||
providerBaseUrl({ WF_DEPLOYMENT_MODE: 'production', WF_OPENAI_BASE_URL: 'http://127.0.0.1:8080/v1' }, 'codex'),
|
||
).toThrow();
|
||
});
|
||
it('hands Claude Code the gateway as ANTHROPIC_BASE_URL next to its key', () => {
|
||
const context = {
|
||
providerCredential: { apiKey: 'fixture-key', baseUrl: 'https://ai-api.example' },
|
||
} as unknown as RunContext;
|
||
const adapter = providerRegistry['claude-code'](
|
||
{ providerBinaries: { 'claude-code': '/usr/bin/true' } } as never,
|
||
context,
|
||
'/tmp/home',
|
||
) as unknown as { config: { credentialEnv: Record<string, string> } };
|
||
expect(adapter.config.credentialEnv).toEqual({
|
||
ANTHROPIC_API_KEY: 'fixture-key',
|
||
ANTHROPIC_BASE_URL: 'https://ai-api.example',
|
||
});
|
||
});
|
||
it('never hands the operator login to a CLI that has a credential of its own', () => {
|
||
const settings = {
|
||
providerBinaries: { 'claude-code': '/usr/bin/true' },
|
||
claudeAuthHome: '/Users/operator',
|
||
sandboxMode: 'host',
|
||
};
|
||
const adapter = (context: RunContext) =>
|
||
providerRegistry['claude-code'](settings as never, context, '/tmp/home') as unknown as {
|
||
config: { localAuth?: unknown };
|
||
};
|
||
// Both at once let the CLI sign in with the login found in that home, and a gateway answers 401.
|
||
expect(adapter({ providerCredential: { apiKey: 'fixture-key' } } as unknown as RunContext).config.localAuth).toBe(
|
||
undefined,
|
||
);
|
||
// With no credential of its own, the development login is the only way to sign in.
|
||
expect(adapter({} as unknown as RunContext).config.localAuth).toEqual({
|
||
home: '/Users/operator',
|
||
developmentHost: true,
|
||
});
|
||
});
|
||
});
|
||
|
||
describe('executor registry', () => {
|
||
it('registers every catalog entry with the worker, which starts the matching adapter', () => {
|
||
expect(Object.keys(providerRegistry).sort()).toEqual([...providers].sort());
|
||
const context = { providerCredential: { apiKey: 'fixture-key' } } as unknown as RunContext;
|
||
for (const id of providers) {
|
||
const adapter = providerRegistry[id](
|
||
{ providerBinaries: { codebuddy: '/usr/bin/true', 'claude-code': '/usr/bin/true' } } as never,
|
||
context,
|
||
'/tmp/home',
|
||
);
|
||
expect(adapter.capabilities().provider).toBe(id);
|
||
}
|
||
});
|
||
it('lets only a development sandbox run Codex without its command sandbox, and says so', () => {
|
||
const context = { providerCredential: { apiKey: 'fixture-key' } } as unknown as RunContext;
|
||
const codex = (sandboxMode: 'host' | 'kata') =>
|
||
providerRegistry.codex({ sandboxMode } as never, context, '/tmp/home') as unknown as {
|
||
config: { allowUnsandboxed: boolean; sandboxProbe?: unknown };
|
||
};
|
||
expect(codex('kata').config.allowUnsandboxed).toBe(false);
|
||
expect(codex('host').config.allowUnsandboxed).toBe(true);
|
||
expect(codex('kata').config.sandboxProbe).toBeTypeOf('function');
|
||
expect(describeProviderEvent({ type: 'started', data: {} })).toEqual({ message: 'Executor started', data: {} });
|
||
const unsandboxed = describeProviderEvent({
|
||
type: 'started',
|
||
data: { unsandboxed: true, reason: 'bwrap: No permissions to create new namespace' },
|
||
});
|
||
expect(unsandboxed.data).toEqual({ unsandboxed: true });
|
||
expect(unsandboxed.message).toMatch(/without its command sandbox.*bwrap: No permissions/);
|
||
});
|
||
it('lets only a development sandbox start CodeBuddy, whose agent runs commands without a sandbox', () => {
|
||
const context = { providerCredential: { apiKey: 'fixture-key' } } as unknown as RunContext;
|
||
const codebuddy = (sandboxMode: 'host' | 'kata') =>
|
||
providerRegistry.codebuddy({ sandboxMode } as never, context, '/tmp/home') as unknown as {
|
||
config: { allowUnsandboxed: boolean };
|
||
};
|
||
expect(codebuddy('kata').config.allowUnsandboxed).toBe(false);
|
||
expect(codebuddy('host').config.allowUnsandboxed).toBe(true);
|
||
expect(
|
||
describeProviderEvent({ type: 'started', data: { unsandboxed: true, reason: 'CodeBuddy has none' } }).message,
|
||
).toBe(
|
||
"Executor started without its command sandbox (CodeBuddy has none); the run's development sandbox is the only boundary",
|
||
);
|
||
});
|
||
it("gives Claude Code sandboxed commands only on the operator's explicit, checked opt-in", () => {
|
||
expect(claudeCommandSandbox({})).toBeUndefined();
|
||
expect(claudeCommandSandbox({ WF_CLAUDE_COMMAND_SANDBOX: 'false' })).toBeUndefined();
|
||
expect(
|
||
claudeCommandSandbox({
|
||
WF_CLAUDE_COMMAND_SANDBOX: 'true',
|
||
WF_CLAUDE_COMMAND_READ_PATHS: ['/srv/go/pkg/mod', '/srv/tools'].join(path.delimiter),
|
||
WF_CLAUDE_COMMAND_ENV: '{"GOMODCACHE":"/srv/go/pkg/mod","GOFLAGS":"-mod=readonly"}',
|
||
}),
|
||
).toEqual({
|
||
readable: ['/srv/go/pkg/mod', '/srv/tools'],
|
||
env: { GOMODCACHE: '/srv/go/pkg/mod', GOFLAGS: '-mod=readonly' },
|
||
});
|
||
for (const invalid of [
|
||
{ WF_CLAUDE_COMMAND_SANDBOX: 'yes' },
|
||
{ WF_CLAUDE_COMMAND_READ_PATHS: 'relative/cache' },
|
||
{ WF_CLAUDE_COMMAND_ENV: 'not json' },
|
||
{ WF_CLAUDE_COMMAND_ENV: '["GOFLAGS"]' },
|
||
{ WF_CLAUDE_COMMAND_ENV: '{"GOFLAGS":1}' },
|
||
// The adapter owns the CLI's home, credentials and settings, and the worker's variables.
|
||
{ WF_CLAUDE_COMMAND_ENV: '{"HOME":"/tmp"}' },
|
||
{ WF_CLAUDE_COMMAND_ENV: '{"ANTHROPIC_BASE_URL":"https://elsewhere.example"}' },
|
||
{ WF_CLAUDE_COMMAND_ENV: '{"CLAUDE_CODE_SANDBOX":"off"}' },
|
||
{ WF_CLAUDE_COMMAND_ENV: '{"WF_WORKER_TOKEN":"x"}' },
|
||
])
|
||
expect(() => claudeCommandSandbox({ WF_CLAUDE_COMMAND_SANDBOX: 'true', ...invalid })).toThrow();
|
||
|
||
const context = { providerCredential: { apiKey: 'fixture-key' } } as unknown as RunContext;
|
||
const adapter = providerRegistry['claude-code'](
|
||
{
|
||
providerBinaries: { 'claude-code': '/usr/bin/true' },
|
||
claudeCommandSandbox: { readable: ['/srv/go/pkg/mod'], env: {} },
|
||
} as never,
|
||
context,
|
||
'/tmp/home',
|
||
) as unknown as { config: { commandSandbox: { readable: string[]; unreadable: string[]; probe: unknown } } };
|
||
expect(adapter.config.commandSandbox.readable).toEqual(['/srv/go/pkg/mod']);
|
||
// The worker's own source or image stays unreadable to the agent's commands.
|
||
expect(adapter.config.commandSandbox.unreadable).toContain(process.cwd());
|
||
expect(adapter.config.commandSandbox.probe).toBeTypeOf('function');
|
||
expect(describeProviderEvent({ type: 'started', data: { commands: 'sandboxed' } })).toEqual({
|
||
message: 'Executor started; its commands run in a sandbox without network access',
|
||
data: { commands: 'sandboxed' },
|
||
});
|
||
expect(
|
||
describeProviderEvent({ type: 'started', data: { commands: 'none', reason: 'socat is not available' } }),
|
||
).toEqual({
|
||
message: 'Executor started with file tools only: its command sandbox cannot start here (socat is not available)',
|
||
data: { commands: 'none' },
|
||
});
|
||
});
|
||
it('enables a CLI executor only where its executable is configured', async () => {
|
||
const info = await availability({ WF_CLAUDE_BIN: '/usr/local/bin/claude' });
|
||
expect(info['claude-code']).toMatchObject({
|
||
name: 'Claude Code',
|
||
configured: true,
|
||
enabled: true,
|
||
credentialSource: 'tenant_credential',
|
||
});
|
||
expect(info.codebuddy).toMatchObject({ enabled: false, reason: expect.stringContaining('WF_CODEBUDDY_BIN') });
|
||
expect(info.codex).toMatchObject({ enabled: true });
|
||
});
|
||
it('distinguishes native account configuration, missing credentials, and unresolved mixed authentication', async () => {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
store.seed('beta');
|
||
const env = {
|
||
...base,
|
||
WF_DEPLOYMENT_MODE: 'development',
|
||
WF_PROVISIONER_BACKEND: 'local',
|
||
WF_TENANT_ID: 'alpha',
|
||
WF_CLAUDE_BIN: '/fixture/claude',
|
||
WF_CLAUDE_LOCAL_AUTH_HOME: '/fixture/operator',
|
||
};
|
||
const control = new ControlPlane(store, env);
|
||
const claude = async (actor = owner) => (await control.providers(actor)).find(info => info.id === 'claude-code');
|
||
expect(await claude()).toMatchObject({ credentialSource: 'native_account', configured: true, enabled: true });
|
||
expect(await claude({ ...owner, tenantId: 'beta' })).toMatchObject({
|
||
credentialSource: 'none',
|
||
configured: false,
|
||
enabled: false,
|
||
});
|
||
await store.transaction('alpha', tx =>
|
||
tx.put('provider_connection', {
|
||
id: 'claude-credential',
|
||
tenantId: 'alpha',
|
||
version: 1,
|
||
createdAt: new Date().toISOString(),
|
||
updatedAt: new Date().toISOString(),
|
||
provider: 'claude-code',
|
||
label: 'Fixture connection',
|
||
status: 'active',
|
||
}),
|
||
);
|
||
expect(await claude()).toMatchObject({
|
||
credentialSource: 'mixed',
|
||
configured: true,
|
||
reason: expect.stringContaining('precedence has not been verified'),
|
||
});
|
||
expect(JSON.stringify(await claude())).not.toContain('/fixture/operator');
|
||
});
|
||
it('runs in production only executors whose credentials the model proxy isolates', async () => {
|
||
const info = await availability({ WF_DEPLOYMENT_MODE: 'production', WF_CLAUDE_BIN: '/usr/local/bin/claude' });
|
||
for (const id of providers.filter(id => !providerCatalog[id].proxied))
|
||
expect(info[id]).toMatchObject({ enabled: false, reason: expect.stringContaining('credential-isolation proxy') });
|
||
});
|
||
});
|