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>
349 lines
15 KiB
TypeScript
349 lines
15 KiB
TypeScript
import { afterEach, describe, expect, it } from 'vitest';
|
||
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import { ClaudeCodeAdapter, type ClaudeCodeConfig, type ProviderEvent } from '../src/index.js';
|
||
|
||
const dirs: string[] = [];
|
||
afterEach(async () => {
|
||
await Promise.all(dirs.splice(0).map(directory => rm(directory, { recursive: true, force: true })));
|
||
});
|
||
async function fixture(
|
||
source: string,
|
||
timeoutMs = 5000,
|
||
env: Record<string, string> = {},
|
||
config: Partial<ClaudeCodeConfig> = {},
|
||
) {
|
||
const home = await mkdtemp(path.join(os.tmpdir(), 'wishforge-claude-native-'));
|
||
dirs.push(home);
|
||
await mkdir(path.join(home, '.claude'));
|
||
const settings = JSON.stringify({
|
||
env: {
|
||
ANTHROPIC_AUTH_TOKEN: 'fixture-secret-token',
|
||
ANTHROPIC_BASE_URL: 'https://example.invalid',
|
||
DATABASE_URL: 'private',
|
||
...env,
|
||
},
|
||
hooks: { mustNeverExecute: true },
|
||
});
|
||
await writeFile(path.join(home, '.claude/settings.json'), settings);
|
||
const binary = path.join(home, 'claude');
|
||
await writeFile(binary, `#!/usr/bin/env node\n${source}\n`);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolationConfirmed: true,
|
||
localAuth: { home, developmentHost: true },
|
||
timeoutMs,
|
||
...config,
|
||
});
|
||
const start = () =>
|
||
adapter.start({
|
||
runId: 'run',
|
||
tenantId: 'tenant',
|
||
workspace: home,
|
||
prompt: 'Make the requested edit',
|
||
baseCommit: 'a'.repeat(40),
|
||
model: 'sonnet',
|
||
resumeSessionId: 'previous-session',
|
||
});
|
||
const collect = async () => {
|
||
const events: ProviderEvent[] = [];
|
||
for await (const event of adapter.events('run')) events.push(event);
|
||
return events;
|
||
};
|
||
return { home, binary, adapter, start, collect, settings };
|
||
}
|
||
|
||
describe('Claude native login on the explicit development host', () => {
|
||
it('uses local auth without loading repository hooks or exposing host tools and redacts streamed secrets', async () => {
|
||
const f = await fixture(`
|
||
const assert = require('node:assert/strict');
|
||
const args = process.argv.slice(2);
|
||
for (const flag of ['--safe-mode', '--restricted', '--strict-mcp-config', 'dontAsk']) assert.ok(args.includes(flag));
|
||
assert.ok(!args.includes('bypassPermissions'));
|
||
assert.equal(args[args.indexOf('--tools') + 1], 'Read,Edit,Write,Glob,Grep');
|
||
assert.equal(args[args.indexOf('--allowedTools') + 1], 'Read,Edit,Write,Glob,Grep');
|
||
assert.equal(args[args.indexOf('--model') + 1], 'sonnet');
|
||
assert.ok(!args.includes('--settings'));
|
||
assert.ok(args[1].startsWith('Make the requested edit\\n\\nExecution contract: only Read/Edit/Write/Glob/Grep'));
|
||
assert.equal(args[args.indexOf('--resume') + 1], 'previous-session');
|
||
assert.equal(process.env.ANTHROPIC_AUTH_TOKEN, 'fixture-secret-token');
|
||
assert.equal(process.env.DATABASE_URL, undefined);
|
||
assert.equal(process.env.WF_WORKER_TOKEN, undefined);
|
||
console.log(JSON.stringify({type:'system',subtype:'init',session_id:'native-session'}));
|
||
const result = Buffer.from(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'已完成 fixture-secret-token',usage:{input_tokens:2,output_tokens:3,cache_creation_input_tokens:4,cache_read_input_tokens:5,private:'not exported'}})+'\\n');
|
||
const split = result.indexOf(Buffer.from('已')) + 1;
|
||
process.stdout.write(result.subarray(0, split));
|
||
setTimeout(() => process.stdout.write(result.subarray(split)), 20);
|
||
`);
|
||
expect(f.adapter.capabilities().coding).toBe(true);
|
||
await f.start();
|
||
const events = await f.collect();
|
||
expect(events.find(e => e.type === 'output')?.data.text).toBe('已完成 [REDACTED]');
|
||
expect(events.find(e => e.type === 'usage')?.data).toEqual({
|
||
final: true,
|
||
input_tokens: 2,
|
||
output_tokens: 3,
|
||
cache_creation_input_tokens: 4,
|
||
cache_read_input_tokens: 5,
|
||
});
|
||
expect(await f.adapter.inspect('run')).toMatchObject({ status: 'completed', sessionId: 'native-session' });
|
||
expect(await readFile(path.join(f.home, '.claude/settings.json'), 'utf8')).toBe(f.settings);
|
||
});
|
||
|
||
it('applies the tenant’s background and fallback models over the operator’s settings', async () => {
|
||
const f = await fixture(
|
||
`
|
||
const assert = require('node:assert/strict');
|
||
const args = process.argv.slice(2);
|
||
assert.equal(args[args.indexOf('--model') + 1], 'sonnet');
|
||
assert.equal(args[args.indexOf('--fallback-model') + 1], 'deepseek-pro@latest[1m]');
|
||
assert.equal(process.env.ANTHROPIC_SMALL_FAST_MODEL, 'deepseek-flash@latest[1m]');
|
||
assert.equal(process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL, 'deepseek-flash@latest[1m]');
|
||
assert.equal(process.env.ANTHROPIC_MODEL, 'operator-main');
|
||
assert.equal(process.env.ANTHROPIC_AUTH_TOKEN, 'fixture-secret-token');
|
||
console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'deepseek-flash@latest[1m] done'}));
|
||
`,
|
||
5000,
|
||
{ ANTHROPIC_MODEL: 'operator-main', ANTHROPIC_SMALL_FAST_MODEL: 'operator-small' },
|
||
{ smallFastModel: 'deepseek-flash@latest[1m]', fallbackModel: 'deepseek-pro@latest[1m]' },
|
||
);
|
||
await f.start();
|
||
const events = await f.collect();
|
||
// Model names are not secrets: the timeline shows them as they are.
|
||
expect(events.find(e => e.type === 'output')?.data.text).toBe('deepseek-flash@latest[1m] done');
|
||
expect(await f.adapter.inspect('run')).toMatchObject({ status: 'completed' });
|
||
});
|
||
|
||
it('leaves the operator’s models alone without tenant settings, and skips a fallback equal to the model', async () => {
|
||
const f = await fixture(
|
||
`
|
||
const assert = require('node:assert/strict');
|
||
const args = process.argv.slice(2);
|
||
assert.ok(!args.includes('--fallback-model'));
|
||
assert.equal(process.env.ANTHROPIC_SMALL_FAST_MODEL, 'operator-small');
|
||
assert.equal(process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL, undefined);
|
||
console.log(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'done'}));
|
||
`,
|
||
5000,
|
||
{ ANTHROPIC_SMALL_FAST_MODEL: 'operator-small' },
|
||
{ fallbackModel: 'sonnet', smallFastModel: 'not a model name' },
|
||
);
|
||
await f.start();
|
||
await f.collect();
|
||
expect(await f.adapter.inspect('run')).toMatchObject({ status: 'completed' });
|
||
});
|
||
|
||
it.each([
|
||
{ label: 'success', isError: false, apiKey: 'fixture-api-key', authToken: 'fixture-secret-token' },
|
||
{ label: 'failure', isError: true, apiKey: 'fixture-api-key', authToken: 'fixture-secret-token' },
|
||
{ label: 'one-character credentials', isError: true, apiKey: '7', authToken: '8' },
|
||
])('preserves non-secret settings and redacts credentials in $label', async ({ isError, apiKey, authToken }) => {
|
||
const configuration = {
|
||
ANTHROPIC_BASE_URL: 'https://gateway.invalid/v1',
|
||
ANTHROPIC_MODEL: 'fixture-main-1',
|
||
ANTHROPIC_DEFAULT_OPUS_MODEL: 'fixture-opus-1',
|
||
ANTHROPIC_DEFAULT_SONNET_MODEL: 'fixture-sonnet-1',
|
||
ANTHROPIC_DEFAULT_HAIKU_MODEL: 'fixture-haiku-1',
|
||
ANTHROPIC_SMALL_FAST_MODEL: 'fixture-small-1',
|
||
CLAUDE_CODE_SUBAGENT_MODEL: 'fixture-subagent-1',
|
||
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
||
DISABLE_AUTOUPDATER: '1',
|
||
DISABLE_TELEMETRY: '1',
|
||
DISABLE_ERROR_REPORTING: '1',
|
||
};
|
||
const env = { ...configuration, ANTHROPIC_API_KEY: apiKey, ANTHROPIC_AUTH_TOKEN: authToken };
|
||
const visible = `internal/wishforgeacceptance20261101/name.go A-B_01 ${Object.values(configuration).join(' ')}`;
|
||
const redacted = `${visible} [REDACTED] [REDACTED]`;
|
||
const f = await fixture(
|
||
`
|
||
const assert = require('node:assert/strict');
|
||
for (const [name, value] of Object.entries(${JSON.stringify(env)})) assert.equal(process.env[name], value);
|
||
const text = ${JSON.stringify(visible)} + ' ' + process.env.ANTHROPIC_API_KEY + ' ' + process.env.ANTHROPIC_AUTH_TOKEN;
|
||
const assistant = JSON.stringify({type:'assistant',message:{content:[
|
||
{type:'text',text:'stream: ' + text},
|
||
{type:'tool_use',name:'Bash',input:{command:'printf ' + text}}
|
||
]}}) + '\\n';
|
||
const split = assistant.indexOf(process.env.ANTHROPIC_AUTH_TOKEN) + 1;
|
||
process.stdout.write(assistant.slice(0, split));
|
||
setTimeout(() => {
|
||
process.stdout.write(assistant.slice(split));
|
||
console.log(JSON.stringify({type:'result',subtype:${JSON.stringify(isError ? 'error_during_execution' : 'success')},is_error:${isError},result:'final: ' + text}));
|
||
process.stderr.write('private: ' + text);
|
||
}, 20);
|
||
`,
|
||
5000,
|
||
env,
|
||
);
|
||
await f.start();
|
||
const events = await f.collect();
|
||
expect(events.map(event => event.type)).toEqual([
|
||
'started',
|
||
'output',
|
||
'command',
|
||
'output',
|
||
isError ? 'failed' : 'completed',
|
||
]);
|
||
expect(events[1]?.data).toEqual({ text: `stream: ${redacted}` });
|
||
expect(events[2]?.data).toEqual({ command: `printf ${redacted}` });
|
||
expect(events[3]?.data).toEqual({ text: `final: ${redacted}` });
|
||
expect(events[4]?.data).toEqual(isError ? { message: `final: ${redacted}` } : {});
|
||
expect(await f.adapter.inspect('run')).toMatchObject({
|
||
status: isError ? 'failed' : 'completed',
|
||
...(isError ? { error: `final: ${redacted}` } : {}),
|
||
});
|
||
});
|
||
|
||
it('redacts credentials in child process errors without redacting ordinary digits', async () => {
|
||
const f = await fixture('', 5000, { ANTHROPIC_API_KEY: 'fixture-api-key', DISABLE_TELEMETRY: '1' });
|
||
const binary = path.join(f.home, 'fixture-api-key-fixture-secret-token-01');
|
||
await writeFile(binary, `#!${path.join(f.home, 'missing-interpreter')}\n`);
|
||
await chmod(binary, 0o700);
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolationConfirmed: true,
|
||
localAuth: { home: f.home, developmentHost: true },
|
||
});
|
||
await adapter.start({
|
||
runId: 'run',
|
||
tenantId: 'tenant',
|
||
workspace: f.home,
|
||
baseCommit: 'a'.repeat(40),
|
||
prompt: 'test',
|
||
});
|
||
const events: ProviderEvent[] = [];
|
||
for await (const event of adapter.events('run')) events.push(event);
|
||
const message = `spawn ${path.join(f.home, '[REDACTED]-[REDACTED]-01')} ENOENT`;
|
||
expect(events.at(-1)).toMatchObject({ type: 'failed', data: { message } });
|
||
expect(await adapter.inspect('run')).toMatchObject({ status: 'failed', error: message });
|
||
});
|
||
|
||
it('requires the explicit host opt-in and refuses relative auth homes', async () => {
|
||
const f = await fixture('');
|
||
expect(
|
||
new ClaudeCodeAdapter({ binary: f.binary, localAuth: { home: f.home, developmentHost: true } }).capabilities()
|
||
.coding,
|
||
).toBe(false);
|
||
expect(
|
||
new ClaudeCodeAdapter({
|
||
binary: f.binary,
|
||
isolationConfirmed: true,
|
||
localAuth: { home: 'relative', developmentHost: true },
|
||
}).capabilities().coding,
|
||
).toBe(false);
|
||
});
|
||
|
||
it('does not mistake an authentication error with success subtype for completed work', async () => {
|
||
const f = await fixture(
|
||
`console.log(JSON.stringify({type:'result',subtype:'success',is_error:true,result:'Not logged in'}));`,
|
||
);
|
||
await f.start();
|
||
await f.collect();
|
||
expect((await f.adapter.inspect('run')).status).toBe('failed');
|
||
});
|
||
|
||
it('counts the tokens of a session stopped before its result from the responses it streamed', async () => {
|
||
// One response streamed as two content blocks repeats its usage, the later copy with more
|
||
// output; then the session works on until it is stopped, as a pause stops it.
|
||
const f = await fixture(`
|
||
const line = value => console.log(JSON.stringify(value));
|
||
line({type:'assistant',message:{id:'msg_1',content:[{type:'text',text:'Looking'}],usage:{input_tokens:3,output_tokens:1,cache_creation_input_tokens:100,cache_read_input_tokens:1000}}});
|
||
line({type:'assistant',message:{id:'msg_1',content:[{type:'tool_use',name:'Edit',input:{file_path:'a.ts'}}],usage:{input_tokens:3,output_tokens:40,cache_creation_input_tokens:100,cache_read_input_tokens:1000}}});
|
||
line({type:'assistant',message:{id:'msg_2',content:[{type:'text',text:'Editing'}],usage:{input_tokens:2,output_tokens:5,cache_creation_input_tokens:50,cache_read_input_tokens:1100}}});
|
||
setInterval(() => {}, 1000);
|
||
`);
|
||
await f.start();
|
||
const events: ProviderEvent[] = [];
|
||
for await (const event of f.adapter.events('run')) {
|
||
events.push(event);
|
||
if (event.type === 'output' && event.data.text === 'Editing') await f.adapter.cancel('run');
|
||
}
|
||
expect(events.map(event => event.type)).toEqual([
|
||
'started',
|
||
'output',
|
||
'file_change',
|
||
'output',
|
||
'usage',
|
||
'canceled',
|
||
]);
|
||
expect(events.find(event => event.type === 'usage')?.data).toEqual({
|
||
final: true,
|
||
estimated: true,
|
||
input_tokens: 5,
|
||
output_tokens: 45,
|
||
cache_creation_input_tokens: 150,
|
||
cache_read_input_tokens: 2100,
|
||
});
|
||
});
|
||
|
||
it('reports the total once and the final message once when the session ends normally', async () => {
|
||
const f = await fixture(`
|
||
const line = value => console.log(JSON.stringify(value));
|
||
line({type:'assistant',message:{id:'msg_1',content:[{type:'text',text:'Done: added the label.'}],usage:{input_tokens:3,output_tokens:9}}});
|
||
line({type:'result',subtype:'success',is_error:false,result:'Done: added the label.',usage:{input_tokens:3,output_tokens:12}});
|
||
`);
|
||
await f.start();
|
||
const events = await f.collect();
|
||
expect(events.map(event => event.type)).toEqual(['started', 'output', 'usage', 'completed']);
|
||
expect(events.find(event => event.type === 'usage')?.data).toEqual({
|
||
final: true,
|
||
input_tokens: 3,
|
||
output_tokens: 12,
|
||
});
|
||
});
|
||
|
||
it('reports timeouts and canceled processes separately', async () => {
|
||
const timeout = await fixture('setInterval(() => {}, 1000);', 150);
|
||
await timeout.start();
|
||
await timeout.collect();
|
||
expect(await timeout.adapter.inspect('run')).toMatchObject({
|
||
status: 'failed',
|
||
error: 'CLI exceeded its time limit',
|
||
});
|
||
const canceled = await fixture('setInterval(() => {}, 1000);');
|
||
await canceled.start();
|
||
await canceled.adapter.cancel('run');
|
||
await canceled.collect();
|
||
expect((await canceled.adapter.inspect('run')).status).toBe('canceled');
|
||
});
|
||
});
|
||
|
||
describe('gateway settings for the native Claude CLI', () => {
|
||
it('passes only the gateway address, credentials, model names and traffic switches', async () => {
|
||
const { localCredentialEnv } = await import('../src/providers/codebuddy.js');
|
||
const home = await mkdtemp(path.join(os.tmpdir(), 'wishforge-claude-gateway-'));
|
||
dirs.push(home);
|
||
await mkdir(path.join(home, '.claude'));
|
||
await writeFile(
|
||
path.join(home, '.claude/settings.json'),
|
||
JSON.stringify({
|
||
env: {
|
||
ANTHROPIC_BASE_URL: 'https://gateway.invalid',
|
||
ANTHROPIC_API_KEY: 'sk-fixture',
|
||
ANTHROPIC_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_OPUS_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_SONNET_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_HAIKU_MODEL: 'deepseek-v4-flash',
|
||
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
||
DISABLE_AUTOUPDATER: '1',
|
||
ANTHROPIC_CUSTOM_HEADERS: 'X-Injected: yes',
|
||
ANTHROPIC_SMALL_FAST_MODEL: 'line\nbreak',
|
||
DATABASE_URL: 'private',
|
||
NODE_OPTIONS: '--require evil',
|
||
},
|
||
}),
|
||
);
|
||
expect(await localCredentialEnv(home)).toEqual({
|
||
ANTHROPIC_BASE_URL: 'https://gateway.invalid',
|
||
ANTHROPIC_API_KEY: 'sk-fixture',
|
||
ANTHROPIC_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_OPUS_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_SONNET_MODEL: 'deepseek-v4-pro',
|
||
ANTHROPIC_DEFAULT_HAIKU_MODEL: 'deepseek-v4-flash',
|
||
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
||
DISABLE_AUTOUPDATER: '1',
|
||
});
|
||
});
|
||
});
|