From 366438d2ef94775a4515301bcf8a58ab866c1731 Mon Sep 17 00:00:00 2001 From: idoubi Date: Sat, 4 Apr 2026 00:25:08 +0800 Subject: [PATCH] fix: subagent inherits parent agent's provider, model, and apiType Spawned subagents now inherit the parent agent's LLM provider config (apiType, apiKey, baseURL, model) via ToolContext instead of falling back to process.env. This fixes failures when the parent agent is configured via code (not env vars), especially with OpenAI-compatible endpoints. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/engine.ts | 3 +++ src/tools/agent-tool.ts | 16 +++++++++------- src/types.ts | 6 ++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/engine.ts b/src/engine.ts index ca03ceb..00f186a 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -457,6 +457,9 @@ export class QueryEngine { const context: ToolContext = { cwd: this.config.cwd, abortSignal: this.config.abortSignal, + provider: this.provider, + model: this.config.model, + apiType: this.provider.apiType, } const MAX_CONCURRENCY = parseInt( diff --git a/src/tools/agent-tool.ts b/src/tools/agent-tool.ts index fa96f62..b1d608c 100644 --- a/src/tools/agent-tool.ts +++ b/src/tools/agent-tool.ts @@ -101,13 +101,15 @@ export const AgentTool: ToolDefinition = { const systemPrompt = agentDef?.prompt || 'You are a helpful assistant. Complete the given task using the available tools.' - // Resolve model and create provider for subagent - const subModel = input.model || process.env.CODEANY_MODEL || 'claude-sonnet-4-6' - const apiType = (process.env.CODEANY_API_TYPE as ApiType) || 'anthropic-messages' - const provider = createProvider(apiType, { - apiKey: process.env.CODEANY_API_KEY, - baseURL: process.env.CODEANY_BASE_URL, - }) + // Inherit provider and model from parent agent context, fall back to env vars + const subModel = input.model || context.model || process.env.CODEANY_MODEL || 'claude-sonnet-4-6' + const provider = context.provider ?? createProvider( + (context.apiType || process.env.CODEANY_API_TYPE as ApiType) || 'anthropic-messages', + { + apiKey: process.env.CODEANY_API_KEY, + baseURL: process.env.CODEANY_BASE_URL, + }, + ) // Create subagent engine const engine = new QueryEngine({ diff --git a/src/types.ts b/src/types.ts index aafa38f..c5890b5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -191,6 +191,12 @@ export interface ToolInputSchema { export interface ToolContext { cwd: string abortSignal?: AbortSignal + /** Parent agent's LLM provider (inherited by subagents) */ + provider?: import('./providers/types.js').LLMProvider + /** Parent agent's model ID */ + model?: string + /** Parent agent's API type */ + apiType?: import('./providers/types.js').ApiType } export interface ToolResult {