Brings the branch up to the current base (executor settings, project skills, Chinese run messages, cloud IDE removal, wish attachments) and resolves the ten conflicted files, keeping both sides: - wish-refiner: the tenant's executor settings pick the models first, then the Copilot model setting; the tenant credential still goes before the operator's login - worker providers: executor settings apply, and a run with its own credential still gets an empty home - main.ts: Copilot screenshot uploads and wish attachments each keep their own body limit - contracts, http, web types: default budget and skill auto-activation, Copilot and attachment entity kinds, ./text and ./attachments exports - globals.css: Copilot styles and attachment styles, one after the other - tests: the canceled-release reason is Chinese; the refiner's default models have no [1m] suffix, as this branch names them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
287 lines
14 KiB
TypeScript
287 lines
14 KiB
TypeScript
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { modelName, providerBaseUrl, providerCatalog, providers, type Actor } from '@wishforge/contracts';
|
||
import { MODEL_NAME_PATTERN } from '@wishforge/contracts/models';
|
||
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 default base URL from the operator', () => {
|
||
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('tenant executor settings in the worker', () => {
|
||
const start = (id: 'claude-code' | 'codebuddy', executorSettings?: RunContext['executorSettings']) =>
|
||
(
|
||
providerRegistry[id](
|
||
{ providerBinaries: { [id]: '/usr/bin/true' } } as never,
|
||
{ providerCredential: { apiKey: 'fixture-key' }, executorSettings } as unknown as RunContext,
|
||
'/tmp/home',
|
||
) as unknown as { config: Record<string, unknown> }
|
||
).config;
|
||
it('hands Claude Code the time limit and models, without touching its credential variables', () => {
|
||
const config = start('claude-code', {
|
||
smallFastModel: 'deepseek-flash@latest[1m]',
|
||
fallbackModel: 'deepseek-pro@latest[1m]',
|
||
timeoutMinutes: 90,
|
||
});
|
||
expect(config).toMatchObject({
|
||
timeoutMs: 90 * 60_000,
|
||
smallFastModel: 'deepseek-flash@latest[1m]',
|
||
fallbackModel: 'deepseek-pro@latest[1m]',
|
||
credentialEnv: { ANTHROPIC_API_KEY: 'fixture-key' },
|
||
});
|
||
});
|
||
it('gives CodeBuddy the time limit only, and ignores what is out of bounds', () => {
|
||
const config = start('codebuddy', { smallFastModel: 'x', timeoutMinutes: 20 });
|
||
expect(config.timeoutMs).toBe(20 * 60_000);
|
||
expect(config).not.toHaveProperty('smallFastModel');
|
||
expect(start('claude-code', { timeoutMinutes: 9999 })).not.toHaveProperty('timeoutMs');
|
||
expect(start('claude-code')).not.toHaveProperty('fallbackModel');
|
||
});
|
||
it('lets the console’s model fields take gateway names, as the API does', () => {
|
||
const pattern = new RegExp(`^(?:${MODEL_NAME_PATTERN})$`, 'v');
|
||
for (const name of ['deepseek-flash@latest[1m]', 'gpt-5.4', 'glm-5:cloud', 'org/model_v2', 'x'.repeat(160)]) {
|
||
expect(pattern.test(name)).toBe(true);
|
||
expect(modelName.safeParse(name).success).toBe(true);
|
||
}
|
||
for (const name of ['-dash', 'with space', '@first', 'x'.repeat(161)]) {
|
||
expect(pattern.test(name)).toBe(false);
|
||
expect(modelName.safeParse(name).success).toBe(false);
|
||
}
|
||
});
|
||
});
|
||
|
||
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: '执行器已启动', 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(/没有自己的命令沙箱.*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('执行器启动,但没有自己的命令沙箱(CodeBuddy has none);运行的开发沙箱是唯一的隔离边界');
|
||
});
|
||
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: '执行器已启动;它的命令在无网络的沙箱里运行',
|
||
data: { commands: 'sandboxed' },
|
||
});
|
||
expect(
|
||
describeProviderEvent({ type: 'started', data: { commands: 'none', reason: 'socat is not available' } }),
|
||
).toEqual({
|
||
message: '执行器已启动,仅能使用文件工具:它的命令沙箱在这里无法启动(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('认证优先级尚未验证'),
|
||
});
|
||
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('凭据隔离代理') });
|
||
});
|
||
});
|