🐛 Align ExecutorSettings across WishRefiner, Copilot, CodeWiki and Run resume #2

Open
zhanghongming wants to merge 1 commits from fix/executor-settings-alignment into review/overhaul
9 changed files with 228 additions and 51 deletions

View File

@ -1,7 +1,8 @@
import { DomainError, providerBaseUrl, type Actor, type ProviderConnection, type Run } from '@wishforge/contracts';
import { DomainError, type Actor, type ProviderConnection, type Run } from '@wishforge/contracts';
import type { Store } from '../db/store.js';
import { CredentialVault } from './connections.js';
import { copilotModel } from './copilot-model.js';
import { effectiveBaseUrl, storedExecutorSettings } from './executor-settings.js';
import { redactSecrets, type CodeClient } from './copilot-tools.js';
import type { ProjectWiki, WikiFile } from './project-wiki.js';
@ -42,18 +43,23 @@ export class CodeWiki {
}
/** The tenant's own gateway credential; a generated page is never the operator's work. */
private async credential(actor: Actor) {
const connection = await this.deps.store.read(actor.tenantId, async tx =>
(await tx.list<ProviderConnection>('provider_connection')).find(
item => item.provider === 'claude-code' && item.status === 'active' && item.ciphertext,
),
const [connection, settings] = await this.deps.store.read(
actor.tenantId,
async tx =>
[
(await tx.list<ProviderConnection>('provider_connection')).find(
item => item.provider === 'claude-code' && item.status === 'active' && item.ciphertext,
),
await storedExecutorSettings(tx, 'claude-code'),
] as const,
);
if (!connection?.ciphertext) return undefined;
const baseUrl = providerBaseUrl(this.deps.env, 'claude-code');
const baseUrl = effectiveBaseUrl(this.deps.env, actor.tenantId, 'claude-code', settings);
if (!baseUrl) return undefined;
return {
apiKey: await this.deps.vault.open(actor.tenantId, String(connection.ciphertext)),
baseUrl,
model: copilotModel(this.deps.env),
model: settings.askModel || copilotModel(this.deps.env),
};
}
private async complete(

View File

@ -2,7 +2,6 @@ import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
import { z } from 'zod';
import {
DomainError,
providerBaseUrl,
providerCatalog,
providers,
type Actor,
@ -14,7 +13,7 @@ import type { Store } from '../db/store.js';
import { record, touch, must, executionLease } from './control-plane.js';
import { requireHuman, requireRole } from './policy.js';
import { ProviderProxy } from './provider-proxy.js';
import { storedExecutorSettings, tenantBaseUrl, tenantBaseUrlBlocked } from './executor-settings.js';
import { effectiveBaseUrl, storedExecutorSettings } from './executor-settings.js';
/** Production uses a Vault transit key. The development AES key never enters source control. */
export class CredentialVault {
@ -155,11 +154,7 @@ export class Connections {
}
const apiKey = await this.vault.open(actor.tenantId, String(connection.ciphertext));
// The tenant's gateway goes only with the tenant's own key; otherwise the operator's, if any.
const tenantGateway =
connection.settings.baseUrl && !tenantBaseUrlBlocked(this.env, actor.tenantId, run.provider)
? tenantBaseUrl(this.env, connection.settings.baseUrl)
: undefined;
const baseUrl = tenantGateway ?? providerBaseUrl(this.env, run.provider);
const baseUrl = effectiveBaseUrl(this.env, actor.tenantId, run.provider, connection.settings);
// Vault I/O runs outside the transaction. Re-check revocation and lease after decrypting.
return this.store.transaction(actor.tenantId, async tx => {
const current = await must<Run>(tx, 'run', run.id);

View File

@ -417,7 +417,6 @@ export class ControlPlane {
400,
);
const { model: chosenModel, attachments: files = [], ...runInput } = data;
const model = chosenModel ?? defaultProjectModel(models);
const sizes = files.map(file => base64Size(file.data));
if (sizes.some(size => size > MAX_ATTACHMENT_BYTES))
throw new DomainError('ATTACHMENT_TOO_LARGE', 'Each attachment may be at most 5 MB', 413);
@ -442,7 +441,6 @@ export class ControlPlane {
} as Wish;
const identity = record(actor.tenantId);
const bundle = await activeBundle(tx);
const executor = await storedExecutorSettings(tx, data.provider);
const run = {
...identity,
projectId,
@ -454,14 +452,7 @@ export class ControlPlane {
requestedBy: actor.userId,
repairCount: 0,
bundleVersion: bundle.version,
providerModel:
model ??
bundle.config.model ??
// The tenant's default for this executor, then the operator's.
executor.defaultModel ??
(data.provider === 'codex'
? (this.env.WF_CODEX_DEFAULT_MODEL ?? this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.split(',')[0]?.trim())
: undefined),
providerModel: chosenModel ?? (await this.defaultRunModel(tx, project, bundle.config.model, data.provider)),
policyVersion: 'policy-v1',
reservedTokens: data.budget.maxTokens,
usedTokens: 0,
@ -696,6 +687,11 @@ export class ControlPlane {
effectiveEntitlements(await tx.tenant()).limits.monthlyModelTokens
)
throw new DomainError('QUOTA_EXCEEDED', 'Tenant token reservation limit is exhausted', 429);
if (!run.providerModel) {
const bundle = await resolveBundle(tx, run.bundleVersion).catch(() => undefined);
const resolvedModel = await this.defaultRunModel(tx, project, bundle?.model, run.provider);
if (resolvedModel) run.providerModel = resolvedModel;
}
run.status = 'queued';
clearWaiting(run);
run.pendingStartId = randomUUID();
@ -707,6 +703,22 @@ export class ControlPlane {
await tx.enqueue('run.signal', { runId: id, action: data.action });
return run;
}
private async defaultRunModel(
tx: Transaction,
project: Project,
bundleModel: string | undefined,
provider: Run['provider'],
) {
return (
defaultProjectModel(project.models ?? []) ??
bundleModel ??
// The tenant's default for this executor, then the operator's.
(await storedExecutorSettings(tx, provider)).defaultModel ??
(provider === 'codex'
? (this.env.WF_CODEX_DEFAULT_MODEL ?? this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.split(',')[0]?.trim())
: undefined)
);
}
/**
* Takes back what a run integrated: a new run that reverses the change on the current target
* branch, then verifies, integrates and delivers it like any other. Later commits are kept.

View File

@ -7,7 +7,6 @@ import {
copilotMessageInput,
copilotWishInput,
plainText,
providerBaseUrl,
withAnswers,
type ProviderConnection,
type Actor,
@ -22,6 +21,7 @@ import { ControlPlane, must, projectAccess, record, touch } from './control-plan
import { nativeClaudeEnabled } from './local-execution.js';
import { CredentialVault } from './connections.js';
import { copilotModel } from './copilot-model.js';
import { effectiveBaseUrl, storedExecutorSettings } from './executor-settings.js';
import {
DIAGNOSIS_GUIDANCE,
TOOL_GUIDANCE,
@ -373,12 +373,12 @@ export class Copilot {
),
);
}
private async tenantCredentials(actor: Actor) {
private async tenantCredentials(actor: Actor, settings: { baseUrl?: string; askModel?: string } = {}) {
const connection = await this.tenantConnection(actor);
if (!connection?.ciphertext) return undefined;
const apiKey = await this.vault.open(actor.tenantId, String(connection.ciphertext));
const baseUrl = providerBaseUrl(this.env, 'claude-code');
const model = copilotModel(this.env);
const baseUrl = effectiveBaseUrl(this.env, actor.tenantId, 'claude-code', settings);
const model = settings.askModel || copilotModel(this.env);
// The CLI is told the model both ways: on the command line and through its environment.
return {
ANTHROPIC_API_KEY: apiKey,
@ -470,6 +470,8 @@ export class Copilot {
const files = overview ? overviewPages(visible, pageAliases(visible)) : retrieve(visible, data.content).files;
// The tenant's credential is the production channel, so it goes first wherever one is
// configured; the operator's local login is only a development fallback.
const settings = await this.store.read(actor.tenantId, tx => storedExecutorSettings(tx, 'claude-code'));
const model = settings.askModel || copilotModel(this.env);
const native = nativeClaudeEnabled(this.env, actor.tenantId);
const attempts: ('native' | 'tenant')[] = (await this.tenantConnection(actor))
? ['tenant', ...(native ? (['native'] as const) : [])]
@ -481,7 +483,7 @@ export class Copilot {
let failure: unknown;
for (const attempt of attempts) {
try {
const credentialEnv = attempt === 'native' ? undefined : await this.tenantCredentials(actor);
const credentialEnv = attempt === 'native' ? undefined : await this.tenantCredentials(actor, settings);
if (attempt === 'tenant' && !credentialEnv) continue;
// The next attempt only gets the time the previous one left, so the whole answer stays
// inside the budget the browser and the proxy are waiting for.
@ -492,7 +494,7 @@ export class Copilot {
// Only the operator's login needs that home; a tenant credential gets an empty one.
...(attempt === 'native' ? { home: this.env.WF_CLAUDE_LOCAL_AUTH_HOME! } : {}),
system: SYSTEM,
model: copilotModel(this.env),
model,
timeoutMs,
schema: responseSchema,
...(credentialEnv ? { credentialEnv } : {}),
@ -606,12 +608,13 @@ export class Copilot {
private async streamCredential(actor: Actor) {
const connection = await this.tenantConnection(actor);
if (!connection?.ciphertext) return undefined;
const baseUrl = providerBaseUrl(this.env, 'claude-code');
const settings = await this.store.read(actor.tenantId, tx => storedExecutorSettings(tx, 'claude-code'));
const baseUrl = effectiveBaseUrl(this.env, actor.tenantId, 'claude-code', settings);
if (!baseUrl) return undefined;
return {
apiKey: await this.vault.open(actor.tenantId, String(connection.ciphertext)),
baseUrl,
model: copilotModel(this.env),
model: settings.askModel || copilotModel(this.env),
};
}
/**

View File

@ -5,6 +5,7 @@ import {
executorSettingsInput,
isProduction,
plainHttpAllowed,
providerBaseUrl,
providerCatalog,
providers,
type Actor,
@ -55,6 +56,23 @@ export function tenantBaseUrl(env: NodeJS.ProcessEnv, value: string) {
return url.href.replace(/\/$/, '');
}
/**
* The gateway a tenant's own key is sent to: the tenant's configured `baseUrl` where allowed,
* otherwise the operator's `providerBaseUrl`. Never used with the operator's native login.
*/
export function effectiveBaseUrl(
env: NodeJS.ProcessEnv,
tenantId: string,
provider: ProviderId,
settings: Pick<Settings, 'baseUrl'> = {},
) {
const configured =
settings.baseUrl && !tenantBaseUrlBlocked(env, tenantId, provider)
? tenantBaseUrl(env, settings.baseUrl)
: undefined;
return configured ?? providerBaseUrl(env, provider);
}
/** The settings of one executor, or none; tolerant of fields the executor no longer takes. */
export async function storedExecutorSettings(tx: Transaction, provider: ProviderId): Promise<Settings> {
const stored = await tx.get<ExecutorSettings>('executor_settings', provider);

View File

@ -1,17 +1,11 @@
import { z } from 'zod';
import { askClaude } from '@wishforge/runtime';
import {
DomainError,
providerBaseUrl,
wishRefinementInput,
type Actor,
type ProviderConnection,
} from '@wishforge/contracts';
import { DomainError, wishRefinementInput, type Actor, type ProviderConnection } from '@wishforge/contracts';
import type { Store } from '../db/store.js';
import { CredentialVault } from './connections.js';
import { copilotModel } from './copilot-model.js';
import { projectAccess } from './control-plane.js';
import { storedExecutorSettings } from './executor-settings.js';
import { effectiveBaseUrl, storedExecutorSettings } from './executor-settings.js';
import { nativeClaudeEnabled } from './local-execution.js';
import { requireRole } from './policy.js';
@ -149,16 +143,13 @@ export class WishRefiner {
return Boolean(await this.tenantConnection(tenantId));
}
/** The same server-held credential and gateway the Copilot answers with. */
private async tenantCredentials(tenantId: string) {
private async tenantCredentials(tenantId: string, settings: { baseUrl?: string } = {}) {
const connection = await this.tenantConnection(tenantId);
if (!connection?.ciphertext) return undefined;
const apiKey = await this.vault.open(tenantId, String(connection.ciphertext));
const baseUrl = providerBaseUrl(this.env, 'claude-code');
const model = copilotModel(this.env);
const baseUrl = effectiveBaseUrl(this.env, tenantId, 'claude-code', settings);
return {
ANTHROPIC_API_KEY: apiKey,
ANTHROPIC_MODEL: model,
ANTHROPIC_SMALL_FAST_MODEL: model,
...(baseUrl ? { ANTHROPIC_BASE_URL: baseUrl } : {}),
};
}
@ -177,7 +168,7 @@ export class WishRefiner {
);
// The tenant's credential is the production channel and goes first wherever one is configured;
// the operator's local login is only used when there is none.
const credentialEnv = await this.tenantCredentials(actor.tenantId);
const credentialEnv = await this.tenantCredentials(actor.tenantId, settings);
if (running >= CONCURRENCY)
throw new DomainError('REFINER_BUSY', 'Other optimizations are running; try again in a moment', 429);
running++;
@ -200,13 +191,26 @@ export class WishRefiner {
),
schema,
timeoutMs: ASK_TIMEOUT_MS,
...(credentialEnv ? { credentialEnv } : {}),
};
const models = askModels(this.env, settings);
let failure: unknown;
for (const model of models) {
try {
const parsed = answer.safeParse(await this.ask({ ...request, model }));
const parsed = answer.safeParse(
await this.ask({
...request,
model,
...(credentialEnv
? {
credentialEnv: {
...credentialEnv,
ANTHROPIC_MODEL: model,
ANTHROPIC_SMALL_FAST_MODEL: model,
},
}
: {}),
}),
);
if (!parsed.success) {
failure = new Error(`${model} did not return a usable wish`);
continue;

View File

@ -72,8 +72,8 @@ export async function askClaude(
...Object.fromEntries(PROXY_VARIABLES.flatMap(key => (process.env[key] ? [[key, process.env[key]]] : []))),
// The operator's settings only when this call signs in with the operator's login.
...(input.home ? await localCredentialEnv(input.home) : {}),
...modelEnv,
...input.credentialEnv,
...modelEnv,
},
timeoutMs: input.timeoutMs ?? 120_000,
signal: input.signal,

View File

@ -71,7 +71,12 @@ describe('asking the local Claude CLI', () => {
{
...input,
model: 'deepseek-flash@latest',
credentialEnv: { ANTHROPIC_API_KEY: 'server-fixture', ANTHROPIC_BASE_URL: 'https://model.example' },
credentialEnv: {
ANTHROPIC_API_KEY: 'server-fixture',
ANTHROPIC_BASE_URL: 'https://model.example',
ANTHROPIC_MODEL: 'stale-default',
ANTHROPIC_SMALL_FAST_MODEL: 'stale-small',
},
timeoutMs: 140_000,
},
async command => {
@ -88,6 +93,8 @@ describe('asking the local Claude CLI', () => {
expect(seen!.env).toMatchObject({
ANTHROPIC_API_KEY: 'server-fixture',
ANTHROPIC_BASE_URL: 'https://model.example',
ANTHROPIC_MODEL: 'deepseek-flash@latest',
ANTHROPIC_SMALL_FAST_MODEL: 'deepseek-flash@latest',
});
expect(seen!.timeoutMs).toBe(140_000);
});

View File

@ -4,8 +4,10 @@
*/
import { beforeEach, describe, expect, it } from 'vitest';
import type { Actor, Run } from '@wishforge/contracts';
import { CodeWiki } from '../apps/api/src/domain/code-wiki.js';
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
import { Connections } from '../apps/api/src/domain/connections.js';
import { Connections, CredentialVault } from '../apps/api/src/domain/connections.js';
import { Copilot } from '../apps/api/src/domain/copilot.js';
import { ExecutorSettingsService } from '../apps/api/src/domain/executor-settings.js';
import { WishRefiner, askModels } from '../apps/api/src/domain/wish-refiner.js';
import { MemoryStore } from './helpers/memory-store.js';
@ -222,4 +224,134 @@ describe('executor settings in runs', () => {
await refiner.refine(owner, project.id, { goal: '下载文件名要带上类型', acceptanceCriteria: [] });
expect(models).toEqual(['tenant-ask']);
});
it('sends the wish refiner, Copilot and code wiki to the tenant’s gateway and ask models', async () => {
const byokEnv = {
NODE_ENV: 'test',
WF_VAULT_KEY: 'd'.repeat(64),
WF_CLAUDE_BIN: '/usr/local/bin/claude',
};
const control = new ControlPlane(db, byokEnv);
const project = await control.createProject(owner, {
name: 'Pilot',
repositoryUrl: 'https://example.org/pilot.git',
});
await new Connections(db, byokEnv).create(owner, {
provider: 'claude-code',
label: 'key',
apiKey: 'tenant-key-1',
});
await new ExecutorSettingsService(db, byokEnv).put(owner, 'claude-code', {
baseUrl: 'https://tenant-gateway.example/v1/',
askModel: 'tenant-ask',
askFallbackModel: 'tenant-ask-fallback',
});
const refinerCalls: Array<{ model?: string; credentialEnv?: Record<string, string> }> = [];
const refiner = new WishRefiner(db, byokEnv, async input => {
refinerCalls.push({ model: input.model, credentialEnv: input.credentialEnv });
if (input.model === 'tenant-ask') throw new Error('primary overloaded');
return { goal: '下载文件名带上数据集类型。', acceptanceCriteria: ['现有测试全部通过'] };
});
await refiner.refine(owner, project.id, { goal: '下载文件名要带上类型', acceptanceCriteria: [] });
expect(refinerCalls).toEqual([
{
model: 'tenant-ask',
credentialEnv: {
ANTHROPIC_API_KEY: 'tenant-key-1',
ANTHROPIC_BASE_URL: 'https://tenant-gateway.example/v1',
ANTHROPIC_MODEL: 'tenant-ask',
ANTHROPIC_SMALL_FAST_MODEL: 'tenant-ask',
},
},
{
model: 'tenant-ask-fallback',
credentialEnv: {
ANTHROPIC_API_KEY: 'tenant-key-1',
ANTHROPIC_BASE_URL: 'https://tenant-gateway.example/v1',
ANTHROPIC_MODEL: 'tenant-ask-fallback',
ANTHROPIC_SMALL_FAST_MODEL: 'tenant-ask-fallback',
},
},
]);
const wiki = {
snapshot: async () => ({
project,
version: 'a'.repeat(40),
files: [{ path: 'wiki/features/wishes.md', content: '# 提交 Wish\n填写目标和验收标准。' }],
}),
write: async () => ({ version: 'b'.repeat(40) }),
};
let copilotInput: { model?: string; credentialEnv?: Record<string, string> } | undefined;
const copilot = new Copilot(
control,
byokEnv,
async input => {
copilotInput = { model: input.model, credentialEnv: input.credentialEnv };
return {
kind: 'faq',
answer: '填写目标和验收标准。',
citations: [{ path: 'wiki/features/wishes.md', quote: '填写目标和验收标准。' }],
};
},
wiki,
);
const session = await copilot.createSession(owner, project.id);
await copilot.ask(owner, project.id, session.id, {
content: '如何提交 Wish?',
clientId: 'client-settings-ask',
});
expect(copilotInput).toEqual({
model: 'tenant-ask',
credentialEnv: {
ANTHROPIC_API_KEY: 'tenant-key-1',
ANTHROPIC_BASE_URL: 'https://tenant-gateway.example/v1',
ANTHROPIC_MODEL: 'tenant-ask',
ANTHROPIC_SMALL_FAST_MODEL: 'tenant-ask',
},
});
const codeWikiRequests: Array<{ url: string; model: string }> = [];
const codeWiki = new CodeWiki({
store: db,
env: byokEnv,
wiki: wiki as never,
vault: new CredentialVault(byokEnv),
runs: async () => [],
code: {
forProject: () => ({
tree: async () => ({ entries: [{ path: 'README.md', type: 'blob' }] }),
blob: async () => ({ content: '# Pilot\n项目说明。' }),
}),
} as never,
fetcher: (async (url, init) => {
const body = JSON.parse(String(init?.body ?? '{}')) as { model: string };
codeWikiRequests.push({ url: String(url), model: body.model });
return new Response(JSON.stringify({ content: [{ type: 'text', text: '# 项目概览\n这是项目概览。' }] }), {
status: 200,
});
}) as typeof fetch,
});
await codeWiki.refresh(owner, project.id, ['overview']);
expect(codeWikiRequests).toEqual([{ url: 'https://tenant-gateway.example/v1/v1/messages', model: 'tenant-ask' }]);
});
it('fills in a waiting run’s missing model from newly configured executor settings on resume', async () => {
const { control, run } = await leasedRun(env);
expect(run.providerModel).toBeUndefined();
await control.releaseLease(worker, run.id, run.lease!.token);
await control.waitRun(worker, run.id, {
reason: 'provider_error',
detail: "403 The requested model 'claude-opus-5-5' is not available",
});
await new ExecutorSettingsService(db, env).put(owner, 'claude-code', {
defaultModel: 'deepseek-pro@latest',
});
const resumed = await control.command(owner, run.id, { action: 'resume' }, 'resume-waiting-run-1');
expect(resumed).toMatchObject({
status: 'queued',
providerModel: 'deepseek-pro@latest',
});
});
});