launch: clean legacy codex profile before launch (#16467)

This commit is contained in:
Eva H
2026-06-03 14:49:31 -04:00
committed by GitHub
parent 3e1b4fe39d
commit ad8cda255d
2 changed files with 90 additions and 0 deletions

View File

@@ -215,6 +215,9 @@ func ensureCodexConfig(modelName string, models []LaunchModel) error {
if err := os.MkdirAll(codexDir, 0o755); err != nil {
return err
}
if err := cleanupCodexLegacyProfileConfig(configPath); err != nil {
return err
}
catalogPath := codexModelCatalogPathForConfig(configPath)
if err := writeCodexModelCatalog(catalogPath, codexCatalogModel(modelName, models)); err != nil {
@@ -261,6 +264,36 @@ func codexNamedProfileConfigPathForConfig(configPath, profileName string) string
return filepath.Join(filepath.Dir(configPath), profileName+".config.toml")
}
func cleanupCodexLegacyProfileConfig(configPath string) error {
content, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
text := string(content)
parsed, err := codexParseConfig(text)
if err != nil {
return err
}
updated := text
if profile, ok := parsed.RootStringOK(codexRootProfileKey); ok && profile == codexProfileName {
updated = codexRemoveRootValue(updated, codexRootProfileKey)
}
if parsed.Exists("profiles", codexProfileName) {
updated = codexRemoveSection(updated, codexProfileHeader())
}
if updated == text {
return nil
}
if err := codexValidateConfigText(updated); err != nil {
return err
}
return fileutil.WriteWithBackup(configPath, []byte(updated), "")
}
// writeCodexProfileConfig ensures ~/.codex/ollama-launch.config.toml selects
// the Ollama provider and catalog for CLI launches without changing root config.
func writeCodexProfileConfig(profilePath, model, modelCatalogPath string) error {

View File

@@ -312,6 +312,63 @@ func TestEnsureCodexConfig(t *testing.T) {
t.Errorf("expected exactly one [model_providers.ollama-launch] section after two calls, got %d", strings.Count(content, "[model_providers.ollama-launch]"))
}
})
t.Run("cleans legacy root profile that conflicts with --profile", func(t *testing.T) {
tmpDir := t.TempDir()
setTestHome(t, tmpDir)
configPath := filepath.Join(tmpDir, ".codex", "config.toml")
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
t.Fatal(err)
}
existing := "" +
`profile = "ollama-launch"` + "\n" +
`model = "gpt-5.5"` + "\n" +
`model_provider = "openai"` + "\n\n" +
"[profiles.ollama-launch]\n" +
`model = "old-local"` + "\n" +
`model_provider = "ollama-launch"` + "\n\n" +
"[profiles.default]\n" +
`model = "gpt-5.5"` + "\n"
if err := os.WriteFile(configPath, []byte(existing), 0o644); err != nil {
t.Fatal(err)
}
if err := ensureCodexConfig("llama3.2", launchModelsFromNames([]string{"llama3.2"})); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}
content := string(data)
if got, ok := codexRootStringValueOK(content, codexRootProfileKey); ok {
t.Fatalf("legacy root profile should be removed, got %q in:\n%s", got, content)
}
if strings.Contains(content, codexProfileHeader()) {
t.Fatalf("legacy profile table should be removed, got:\n%s", content)
}
for _, want := range []string{
`model = "gpt-5.5"`,
`model_provider = "openai"`,
"[profiles.default]",
} {
if !strings.Contains(content, want) {
t.Fatalf("expected %q to be preserved in:\n%s", want, content)
}
}
profilePath := filepath.Join(tmpDir, ".codex", "ollama-launch.config.toml")
profileData, err := os.ReadFile(profilePath)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(profileData), `model = "llama3.2"`) {
t.Fatalf("managed profile was not written with selected model:\n%s", profileData)
}
assertBackupContains(t, filepath.Join(fileutil.BackupDir(), "config.toml.*"), `profile = "ollama-launch"`)
})
}
func TestCodexRestoreRemovesCLIProfileAndCatalogWithoutChangingUserRootConfig(t *testing.T) {