fix: centralize model prefix handling to prevent provider errors

Moves prefix stripping from individual providers to AgentService/IdeationService
and adds validation to ensure providers receive bare model IDs. This prevents
bugs like the Codex CLI receiving "codex-gpt-5.1-codex-max" instead of the
expected "gpt-5.1-codex-max".

- Add validateBareModelId() helper with fail-fast validation
- Add originalModel field to ExecuteOptions for logging
- Update all providers to validate model has no prefix
- Centralize prefix stripping in service layer
- Remove redundant prefix stripping from individual providers

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
DhanushSantosh
2026-01-09 01:40:29 +05:30
parent a815be6a20
commit b2e5ff1460
9 changed files with 83 additions and 10 deletions

View File

@@ -185,6 +185,7 @@ export {
addProviderPrefix,
getBareModelId,
normalizeModelString,
validateBareModelId,
} from './provider-utils.js';
// Pipeline types

View File

@@ -196,3 +196,37 @@ export function normalizeModelString(model: string | undefined | null): string {
return model;
}
/**
* Validate that a model ID does not contain a provider prefix
*
* Providers should receive bare model IDs (e.g., "gpt-5.1-codex-max", "composer-1")
* without provider prefixes (e.g., NOT "codex-gpt-5.1-codex-max", NOT "cursor-composer-1").
*
* This validation ensures the ProviderFactory properly stripped prefixes before
* passing models to providers.
*
* @param model - Model ID to validate
* @param providerName - Name of the provider for error messages
* @throws Error if model contains a provider prefix
*
* @example
* validateBareModelId("gpt-5.1-codex-max", "CodexProvider"); // ✅ OK
* validateBareModelId("codex-gpt-5.1-codex-max", "CodexProvider"); // ❌ Throws error
*/
export function validateBareModelId(model: string, providerName: string): void {
if (!model || typeof model !== 'string') {
throw new Error(`[${providerName}] Invalid model ID: expected string, got ${typeof model}`);
}
for (const [provider, prefix] of Object.entries(PROVIDER_PREFIXES)) {
if (model.startsWith(prefix)) {
throw new Error(
`[${providerName}] Model ID should not contain provider prefix '${prefix}'. ` +
`Got: '${model}'. ` +
`This is likely a bug in ProviderFactory - it should strip the '${provider}' prefix ` +
`before passing the model to the provider.`
);
}
}
}

View File

@@ -81,7 +81,10 @@ export interface McpHttpServerConfig {
*/
export interface ExecuteOptions {
prompt: string | Array<{ type: string; text?: string; source?: object }>;
/** Bare model ID without provider prefix (e.g., "gpt-5.1-codex-max", "composer-1") */
model: string;
/** Original model ID with provider prefix for logging (e.g., "codex-gpt-5.1-codex-max") */
originalModel?: string;
cwd: string;
systemPrompt?: string | SystemPromptPreset;
maxTurns?: number;