fix(cliproxy): remap codex haiku fallback on free plans

This commit is contained in:
Tam Nhu Tran
2026-03-16 14:31:55 -04:00
parent ce600a7f22
commit e6b363524b
3 changed files with 67 additions and 6 deletions
+3 -1
View File
@@ -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)}" ` +
+8 -2
View File
@@ -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
@@ -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<string, string>;
};
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 });
}
});
});