diff --git a/src/cliproxy/codex-plan-compatibility.ts b/src/cliproxy/codex-plan-compatibility.ts index 91a30380..41177e00 100644 --- a/src/cliproxy/codex-plan-compatibility.ts +++ b/src/cliproxy/codex-plan-compatibility.ts @@ -66,7 +66,9 @@ export async function reconcileCodexModelForActivePlan(options: { } if (quota.planType === 'free') { - updateSettingsModel(settingsPath, fallbackModel, 'codex'); + updateSettingsModel(settingsPath, fallbackModel, 'codex', { + rewriteHaikuModel: (haikuModel) => getFreePlanFallbackCodexModel(haikuModel) ?? haikuModel, + }); console.error( info( `Codex free plan detected. Switched unsupported model "${normalizeCodexModelId(currentModel)}" ` + diff --git a/src/cliproxy/services/variant-settings.ts b/src/cliproxy/services/variant-settings.ts index 4dbca1fc..7c7d2038 100644 --- a/src/cliproxy/services/variant-settings.ts +++ b/src/cliproxy/services/variant-settings.ts @@ -290,7 +290,10 @@ export function deleteSettingsFile(settingsPath: string): boolean { export function updateSettingsModel( settingsPath: string, model: string, - provider?: CLIProxyProfileName + provider?: CLIProxyProfileName, + options?: { + rewriteHaikuModel?: (model: string) => string; + } ): void { const fileName = path.basename(settingsPath); if (fileName.startsWith('composite-')) { @@ -316,10 +319,13 @@ export function updateSettingsModel( settings.env.ANTHROPIC_DEFAULT_OPUS_MODEL = normalizedModel; settings.env.ANTHROPIC_DEFAULT_SONNET_MODEL = normalizedModel; if (provider === 'codex' && settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL) { - settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL = canonicalizeModelForProvider( + const normalizedHaikuModel = canonicalizeModelForProvider( provider, settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL ); + settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL = options?.rewriteHaikuModel + ? options.rewriteHaikuModel(normalizedHaikuModel) + : normalizedHaikuModel; } } else { // Clear model settings to use defaults diff --git a/tests/unit/cliproxy/codex-plan-compatibility-reconcile.test.ts b/tests/unit/cliproxy/codex-plan-compatibility-reconcile.test.ts index d11d6e6e..b22ac1fa 100644 --- a/tests/unit/cliproxy/codex-plan-compatibility-reconcile.test.ts +++ b/tests/unit/cliproxy/codex-plan-compatibility-reconcile.test.ts @@ -7,7 +7,10 @@ afterEach(() => { mock.restore(); }); -function createCodexSettingsFixture(): { tmpDir: string; settingsPath: string } { +function createCodexSettingsFixture(haikuModel: string = 'gpt-5-codex-mini'): { + tmpDir: string; + settingsPath: string; +} { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-codex-plan-compat-')); const settingsPath = path.join(tmpDir, 'codex.settings.json'); @@ -21,7 +24,7 @@ function createCodexSettingsFixture(): { tmpDir: string; settingsPath: string } ANTHROPIC_MODEL: 'gpt-5.3-codex', ANTHROPIC_DEFAULT_OPUS_MODEL: 'gpt-5.3-codex', ANTHROPIC_DEFAULT_SONNET_MODEL: 'gpt-5.3-codex', - ANTHROPIC_DEFAULT_HAIKU_MODEL: 'gpt-5-codex-mini', + ANTHROPIC_DEFAULT_HAIKU_MODEL: haikuModel, }, }, null, @@ -39,7 +42,7 @@ async function importCompatibilityModule(cacheTag: string) { describe('codex plan compatibility reconcile', () => { it('repairs stale paid-only Codex settings for free-plan accounts before launch', async () => { - const { tmpDir, settingsPath } = createCodexSettingsFixture(); + const { tmpDir, settingsPath } = createCodexSettingsFixture('gpt-5.3-codex-spark'); mock.module('../../../src/cliproxy/account-manager', () => ({ getDefaultAccount: () => ({ id: 'free@example.com' }), @@ -230,4 +233,54 @@ describe('codex plan compatibility reconcile', () => { fs.rmSync(tmpDir, { recursive: true, force: true }); } }); + + it('warns and keeps settings unchanged when quota succeeds without a plan type', async () => { + const { tmpDir, settingsPath } = createCodexSettingsFixture(); + + mock.module('../../../src/cliproxy/account-manager', () => ({ + getDefaultAccount: () => ({ id: 'missing-plan@example.com' }), + })); + mock.module('../../../src/cliproxy/quota-fetcher-codex', () => ({ + fetchCodexQuota: async () => ({ + success: true, + windows: [], + coreUsage: { fiveHour: null, weekly: null }, + planType: null, + lastUpdated: Date.now(), + accountId: 'missing-plan@example.com', + }), + })); + mock.module('../../../src/cliproxy/quota-response-cache', () => ({ + getCachedQuota: () => null, + setCachedQuota: () => {}, + })); + mock.module('../../../src/utils/ui', () => ({ + info: (message: string) => message, + warn: (message: string) => message, + })); + + const errorSpy = spyOn(console, 'error').mockImplementation(() => {}); + + try { + const { reconcileCodexModelForActivePlan } = + await importCompatibilityModule('missing-plan-type'); + + await reconcileCodexModelForActivePlan({ + settingsPath, + currentModel: 'gpt-5.3-codex', + verbose: false, + }); + + const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8')) as { + env: Record; + }; + expect(settings.env.ANTHROPIC_MODEL).toBe('gpt-5.3-codex'); + expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5-codex-mini'); + expect(errorSpy).toHaveBeenCalledWith( + 'Could not verify Codex plan for model "gpt-5.3-codex". If startup fails with model_not_supported, switch to "gpt-5-codex" via "ccs codex --config".' + ); + } finally { + fs.rmSync(tmpDir, { recursive: true, force: true }); + } + }); });