fix(cliproxy): make codex defaults free-plan safe

This commit is contained in:
Tam Nhu Tran committed 2026-03-16 13:43:23 -04:00
1 parent 3783ddab38
commit 551591ef18
10 files changed
+314 -25

No files matched your search

@@ -0,0 +1,114 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { afterEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { getProviderCatalog, getModelMaxLevel } from '../../../src/cliproxy/model-catalog';
import {
getDefaultCodexModel,
getFreePlanFallbackCodexModel,
} from '../../../src/cliproxy/codex-plan-compatibility';
afterEach(() => {
mock.restore();
});
describe('codex plan compatibility', () => {
it('uses a cross-plan safe Codex default', () => {
expect(getDefaultCodexModel()).toBe('gpt-5-codex');
expect(getProviderCatalog('codex')?.defaultModel).toBe('gpt-5-codex');
});
it('maps paid-only free-plan models to safe fallbacks', () => {
expect(getFreePlanFallbackCodexModel('gpt-5.3-codex')).toBe('gpt-5-codex');
expect(getFreePlanFallbackCodexModel('gpt-5.3-codex-xhigh')).toBe('gpt-5-codex');
expect(getFreePlanFallbackCodexModel('gpt-5.3-codex(high)')).toBe('gpt-5-codex');
expect(getFreePlanFallbackCodexModel('gpt-5.4')).toBe('gpt-5-codex');
expect(getFreePlanFallbackCodexModel('gpt-5.3-codex-spark')).toBe('gpt-5-codex-mini');
});
it('does not rewrite cross-plan or already-safe Codex models', () => {
expect(getFreePlanFallbackCodexModel('gpt-5-codex')).toBeNull();
expect(getFreePlanFallbackCodexModel('gpt-5.2-codex')).toBeNull();
expect(getFreePlanFallbackCodexModel('gpt-5.1-codex-mini')).toBeNull();
});
it('tracks Codex thinking caps for current safe defaults and paid models', () => {
expect(getModelMaxLevel('codex', 'gpt-5-codex')).toBe('high');
expect(getModelMaxLevel('codex', 'gpt-5-codex-mini')).toBe('high');
expect(getModelMaxLevel('codex', 'gpt-5.2-codex')).toBe('xhigh');
expect(getModelMaxLevel('codex', 'gpt-5.3-codex')).toBe('xhigh');
});
it('repairs stale paid-only Codex settings for free-plan accounts before launch', async () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-codex-plan-compat-'));
const settingsPath = path.join(tmpDir, 'codex.settings.json');
fs.writeFileSync(
settingsPath,
JSON.stringify(
{
env: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:8317/api/provider/codex',
ANTHROPIC_AUTH_TOKEN: 'ccs-internal-managed',
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',
},
},
null,
2
),
'utf-8'
);
mock.module('../../../src/cliproxy/account-manager', () => ({
getDefaultAccount: () => ({ id: 'free@example.com' }),
}));
mock.module('../../../src/cliproxy/quota-fetcher-codex', () => ({
fetchCodexQuota: async () => ({
success: true,
windows: [],
coreUsage: { fiveHour: null, weekly: null },
planType: 'free',
lastUpdated: Date.now(),
accountId: 'free@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 import(
`../../../src/cliproxy/codex-plan-compatibility?free-plan=${Date.now()}`
);
await reconcileCodexModelForActivePlan({
settingsPath,
currentModel: 'gpt-5.3-codex',
verbose: false,
});
const repaired = JSON.parse(fs.readFileSync(settingsPath, 'utf-8')) as {
env: Record<string, string>;
};
expect(repaired.env.ANTHROPIC_MODEL).toBe('gpt-5-codex');
expect(repaired.env.ANTHROPIC_DEFAULT_OPUS_MODEL).toBe('gpt-5-codex');
expect(repaired.env.ANTHROPIC_DEFAULT_SONNET_MODEL).toBe('gpt-5-codex');
expect(repaired.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5-codex-mini');
expect(errorSpy).toHaveBeenCalledWith(
'Codex free plan detected. Switched unsupported model "gpt-5.3-codex" to "gpt-5-codex".'
);
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
});
@@ -112,7 +112,7 @@ cliproxy:
expect(settings.env.ANTHROPIC_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_OPUS_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_SONNET_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5-codex-mini');
expect(settings.env.CUSTOM_FLAG).toBe('keep-me');
expect(settings.hooks.PreToolUse.length).toBe(1);
@@ -134,7 +134,7 @@ cliproxy:
expect(settings.env.ANTHROPIC_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_OPUS_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_SONNET_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5-codex-mini');
const modelOnly = updateVariant('demo', { model: 'gpt-5.3-codex' });
expect(modelOnly.success).toBe(true);
@@ -145,6 +145,6 @@ cliproxy:
expect(settings.env.ANTHROPIC_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_OPUS_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_SONNET_MODEL).toBe('gpt-5.3-codex');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5.1-codex-mini');
expect(settings.env.ANTHROPIC_DEFAULT_HAIKU_MODEL).toBe('gpt-5-codex-mini');
});
});