Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions internal/client/codex/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,8 @@ func TestMarshalCompactJSONIsSingleLine(t *testing.T) {

func TestCodexClientModelsResponse_NonTemplateCatalogStaysWithinOneMiB(t *testing.T) {
ids := []string{
"gpt-6.2-sol",
"gpt-6.2-astra",
"gpt-6-astra",
"gpt-6-sol",
"gpt-6-luna",
Expand Down Expand Up @@ -1718,22 +1720,29 @@ func TestCodexClientModelsResponse_NonTemplateCatalogStaysWithinOneMiB(t *testin
if len(body) >= 1<<20 {
t.Fatalf("catalog size = %d bytes, want < 1MiB", len(body))
}
t.Logf("catalog size = %d bytes", len(body))

entries, _ := resp["models"].([]map[string]any)
var templateModel, customModel map[string]any
var templateModel, solModel, astraModel, customModel map[string]any
for _, entry := range entries {
switch stringModelValue(entry, "slug") {
case "gpt-5.5":
templateModel = entry
case "gpt-6.2-sol":
solModel = entry
case "gpt-6.2-astra":
astraModel = entry
case "custom-model-0":
customModel = entry
}
}
if templateModel == nil || customModel == nil {
t.Fatal("expected both template and custom models")
if templateModel == nil || solModel == nil || astraModel == nil || customModel == nil {
t.Fatal("expected template and custom models")
}
if len(stringModelValue(templateModel, "base_instructions")) < 1000 {
t.Fatal("expected official template models to keep their full instructions")
for _, model := range []map[string]any{templateModel, solModel, astraModel} {
if len(stringModelValue(model, "base_instructions")) < 1000 {
t.Fatal("expected official template models to keep their full instructions")
}
}
if stringModelValue(customModel, "base_instructions") != codexClientFallbackInstructions {
t.Fatalf("custom base_instructions = %q", stringModelValue(customModel, "base_instructions"))
Expand Down
68 changes: 67 additions & 1 deletion internal/registry/codex_client_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
//go:embed models/codex_client_models.json
var embeddedCodexClientModelsJSON []byte

//go:embed models/codex_client_model_fallbacks.json
var embeddedCodexClientModelFallbacksJSON []byte

var codexClientModelFallbacks = parseCodexClientModelFallbacks(embeddedCodexClientModelFallbacksJSON)

type codexClientModelsPayload struct {
Models []map[string]any `json:"models"`
}
Expand Down Expand Up @@ -59,7 +64,12 @@ func loadCodexClientModelsFromBytes(data []byte, source string) (bool, error) {
return false, fmt.Errorf("%s: %w", source, err)
}

cloned := append([]byte(nil), data...)
merged, err := withCodexClientModelFallbacks(data)
if err != nil {
return false, fmt.Errorf("%s: %w", source, err)
}

cloned := append([]byte(nil), merged...)
codexClientCatalogStore.mu.Lock()
defer codexClientCatalogStore.mu.Unlock()
if bytes.Equal(codexClientCatalogStore.data, cloned) {
Expand All @@ -70,6 +80,62 @@ func loadCodexClientModelsFromBytes(data []byte, source string) (bool, error) {
return true, nil
}

func parseCodexClientModelFallbacks(data []byte) []json.RawMessage {
var models []json.RawMessage
if err := json.Unmarshal(data, &models); err != nil {
log.Warnf("registry: failed to parse embedded codex_client_model_fallbacks.json: %v", err)
return nil
}
wrapped, err := json.Marshal(map[string]any{"models": models})
if err == nil {
err = ValidateCodexClientModelsJSON(wrapped)
}
if err != nil {
log.Warnf("registry: invalid embedded codex_client_model_fallbacks.json: %v", err)
return nil
}
return models
}

func withCodexClientModelFallbacks(data []byte) ([]byte, error) {
var catalog map[string]json.RawMessage
if err := json.Unmarshal(data, &catalog); err != nil {
return nil, fmt.Errorf("decode Codex client model catalog: %w", err)
}
var models []json.RawMessage
if err := json.Unmarshal(catalog["models"], &models); err != nil {
return nil, fmt.Errorf("decode Codex client model catalog models: %w", err)
}
present := make(map[string]struct{}, len(models))
for _, model := range models {
present[codexClientModelSlug(model)] = struct{}{}
}
added := false
for _, fallback := range codexClientModelFallbacks {
if _, exists := present[codexClientModelSlug(fallback)]; !exists {
models = append(models, fallback)
added = true
Comment thread
ericlitman marked this conversation as resolved.
Resolved
}
}
if !added {
return data, nil
}
encodedModels, err := json.Marshal(models)
if err != nil {
return nil, fmt.Errorf("encode Codex client model catalog models: %w", err)
}
catalog["models"] = encodedModels
return json.Marshal(catalog)
}

func codexClientModelSlug(model json.RawMessage) string {
var entry struct {
Slug string `json:"slug"`
}
_ = json.Unmarshal(model, &entry)
return strings.TrimSpace(entry.Slug)
}

// ValidateCodexClientModelsJSON validates the fields required to serve a
// complete Codex client model catalog.
func ValidateCodexClientModelsJSON(data []byte) error {
Expand Down
192 changes: 192 additions & 0 deletions internal/registry/codex_model_fallbacks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package registry

import (
"encoding/json"
"os"
"slices"
"testing"
)

func TestCodexPaidModelFallbacksYieldToRemoteCatalog(t *testing.T) {
original := getModels()
t.Cleanup(func() {
modelsCatalogStore.mu.Lock()
modelsCatalogStore.data = original
modelsCatalogStore.mu.Unlock()
})

var remote map[string][]map[string]any
if err := json.Unmarshal(embeddedModelsJSON, &remote); err != nil {
t.Fatalf("decode embedded models: %v", err)
}
remote["claude"][0]["display_name"] = "Remote Claude"
for _, tier := range []string{"codex-free", "codex-team", "codex-plus", "codex-pro"} {
for _, model := range remote[tier] {
if model["id"] == "gpt-5.5" {
model["description"] = "Remote gpt-5.5"
}
}
}
remote["codex-pro"] = append(remote["codex-pro"], map[string]any{
"id": "gpt-6.2-sol", "object": "model", "owned_by": "openai", "type": "openai",
"description": "Upstream Sol",
})
data, err := json.Marshal(remote)
if err != nil {
t.Fatalf("encode remote models: %v", err)
}
if _, err = publishCatalogBytes(data); err != nil {
t.Fatalf("publish remote models: %v", err)
}

if got := GetClaudeModels()[0].DisplayName; got != "Remote Claude" {
t.Fatalf("Claude display name = %q, want remote change", got)
}
tiers := map[string][]*ModelInfo{
"free": GetCodexFreeModels(),
"team": GetCodexTeamModels(),
"plus": GetCodexPlusModels(),
"pro": GetCodexProModels(),
}
for tier, models := range tiers {
byID := make(map[string]*ModelInfo, len(models))
for _, model := range models {
if _, exists := byID[model.ID]; exists {
t.Fatalf("%s: duplicate model %q", tier, model.ID)
}
byID[model.ID] = model
}
if got := byID["gpt-5.5"]; got == nil || got.Description != "Remote gpt-5.5" {
t.Fatalf("%s: gpt-5.5 = %+v, want remote change", tier, got)
}
sol, astra := byID["gpt-6.2-sol"], byID["gpt-6.2-astra"]
if tier == "free" {
if sol != nil || astra != nil {
t.Fatalf("free exposes 6.2 models")
}
continue
}
if sol == nil || astra == nil {
t.Fatalf("%s: 6.2 models missing", tier)
}
wantSol := "Latest workhorse model for coding and everyday work."
if tier == "pro" {
wantSol = "Upstream Sol"
}
if sol.Description != wantSol {
t.Fatalf("%s: gpt-6.2-sol description = %q, want %q", tier, sol.Description, wantSol)
}
if astra.NativeCapabilities == nil || astra.NativeCapabilities.WebSearch == nil ||
!*astra.NativeCapabilities.WebSearch ||
!astra.SupportConfigurationUpdate || !slices.Contains(astra.Thinking.Levels, "ultra") {
t.Fatalf("%s: gpt-6.2-astra lost native metadata: %+v", tier, astra)
}
}

for _, model := range tiers["team"] {
if model.ID == "gpt-6.2-astra" {
model.ID = "mutated"
}
}
for _, model := range GetCodexTeamModels() {
if model.ID == "mutated" {
t.Fatal("caller mutation reached shared fallback models")
}
}
}

func TestCodexClientModelFallbacksYieldToRemoteCatalog(t *testing.T) {
original, _ := GetCodexClientModelsSnapshot()
t.Cleanup(func() {
if _, err := loadCodexClientModelsFromBytes(original, "test cleanup"); err != nil {
t.Fatalf("restore original catalog: %v", err)
}
})

fallbackData, err := os.ReadFile("models/codex_client_model_fallbacks.json")
if err != nil {
t.Fatalf("read native fallback fixture: %v", err)
}
var fallbacks []map[string]any
if err := json.Unmarshal(fallbackData, &fallbacks); err != nil {
Comment thread
ericlitman marked this conversation as resolved.
Resolved
t.Fatalf("decode fallbacks: %v", err)
}
var embedded struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(embeddedCodexClientModelsJSON, &embedded); err != nil {
t.Fatalf("decode embedded catalog: %v", err)
}
remoteModels := embedded.Models
for _, model := range remoteModels {
if model["slug"] == "gpt-5.5" {
model["description"] = "Remote gpt-5.5"
}
}
load := func(models []map[string]any) ([]byte, uint64) {
t.Helper()
data, err := json.Marshal(map[string]any{"etag": "remote", "models": models})
if err != nil {
t.Fatalf("encode remote catalog: %v", err)
}
if _, err = loadCodexClientModelsFromBytes(data, "test"); err != nil {
t.Fatalf("load remote catalog: %v", err)
}
snapshot, revision := GetCodexClientModelsSnapshot()
if _, err = loadCodexClientModelsFromBytes(data, "test reload"); err != nil {
t.Fatalf("reload remote catalog: %v", err)
}
reloaded, reloadedRevision := GetCodexClientModelsSnapshot()
if string(reloaded) != string(snapshot) || reloadedRevision != revision {
t.Fatalf("identical reload changed snapshot: revision %d -> %d", revision, reloadedRevision)
}
return snapshot, revision
}
decode := func(snapshot []byte) (map[string]any, []map[string]any) {
t.Helper()
var catalog map[string]any
var payload struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(snapshot, &catalog); err != nil {
t.Fatalf("decode snapshot: %v", err)
}
if err := json.Unmarshal(snapshot, &payload); err != nil {
t.Fatalf("decode snapshot models: %v", err)
}
return catalog, payload.Models
}

snapshot, _ := load(remoteModels)
catalog, models := decode(snapshot)
if catalog["etag"] != "remote" {
t.Fatalf("top-level field lost: %v", catalog["etag"])
}
want := append(append([]map[string]any(nil), remoteModels...), fallbacks...)
wantData, _ := json.Marshal(want)
gotData, _ := json.Marshal(models)
if string(gotData) != string(wantData) {
t.Fatal("snapshot models are not the remote models followed by both fallbacks")
}
if err := ValidateCodexClientModelsJSON(snapshot); err != nil {
t.Fatalf("merged snapshot invalid: %v", err)
}

upstreamSol := testCodexClientModel("gpt-6.2-sol", 1)
upstreamSol["description"] = "Upstream Sol"
snapshot, _ = load(append(remoteModels, upstreamSol))
_, models = decode(snapshot)
var slugs []any
for _, model := range models {
slugs = append(slugs, model["slug"])
}
if got := slugs[len(slugs)-2:]; got[0] != "gpt-6.2-sol" || got[1] != "gpt-6.2-astra" {
t.Fatalf("trailing slugs = %v", got)
}
if models[len(models)-2]["description"] != "Upstream Sol" {
t.Fatal("upstream gpt-6.2-sol did not supersede fallback")
}
if models[len(models)-1]["base_instructions"] != fallbacks[1]["base_instructions"] {
t.Fatal("fallback gpt-6.2-astra lost native instructions")
}
}
39 changes: 36 additions & 3 deletions internal/registry/model_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
package registry

import (
_ "embed"
"encoding/json"
"strings"

log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -66,17 +70,46 @@ func GetCodexFreeModels() []*ModelInfo {

// GetCodexTeamModels returns model definitions for the Codex team plan tier.
func GetCodexTeamModels() []*ModelInfo {
return WithCodexBuiltins(cloneModelInfos(getModels().CodexTeam))
return WithCodexBuiltins(withCodexPaidModelFallbacks(cloneModelInfos(getModels().CodexTeam)))
}

// GetCodexPlusModels returns model definitions for the Codex plus plan tier.
func GetCodexPlusModels() []*ModelInfo {
return WithCodexBuiltins(cloneModelInfos(getModels().CodexPlus))
return WithCodexBuiltins(withCodexPaidModelFallbacks(cloneModelInfos(getModels().CodexPlus)))
}

// GetCodexProModels returns model definitions for the Codex pro plan tier.
func GetCodexProModels() []*ModelInfo {
return WithCodexBuiltins(cloneModelInfos(getModels().CodexPro))
return WithCodexBuiltins(withCodexPaidModelFallbacks(cloneModelInfos(getModels().CodexPro)))
Comment thread
ericlitman marked this conversation as resolved.
Resolved
}

//go:embed models/codex_paid_model_fallbacks.json
var embeddedCodexPaidModelFallbacksJSON []byte

var codexPaidModelFallbacks = parseCodexPaidModelFallbacks(embeddedCodexPaidModelFallbacksJSON)

func parseCodexPaidModelFallbacks(data []byte) []*ModelInfo {
var models []*ModelInfo
if err := json.Unmarshal(data, &models); err != nil {
log.Warnf("registry: failed to parse embedded codex_paid_model_fallbacks.json: %v", err)
return nil
}
return models
}

func withCodexPaidModelFallbacks(models []*ModelInfo) []*ModelInfo {
present := make(map[string]struct{}, len(models))
for _, model := range models {
if model != nil {
present[model.ID] = struct{}{}
}
}
for _, fallback := range codexPaidModelFallbacks {
if _, exists := present[fallback.ID]; !exists {
models = append(models, cloneModelInfo(fallback))
}
}
return models
}

// GetKimiModels returns the standard Kimi (Moonshot AI) model definitions.
Expand Down
Loading
Loading