Skip to content

Commit fd558f8

Browse files
idoubiclaude
andcommitted
Fix agent switch overwriting external config changes (#38)
Switch from Save() to new SaveDefaultAgent() which reads the current config from disk before writing, updating only the default_agent field. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent acb3eef commit fd558f8

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

cmd/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func runStart(cmd *cobra.Command, args []string) error {
108108
},
109109
func(name string) error {
110110
cfg.DefaultAgent = name
111-
return config.Save(cfg)
111+
return config.SaveDefaultAgent(name)
112112
},
113113
)
114114

config/config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,36 @@ func Save(cfg *Config) error {
138138

139139
return os.WriteFile(path, data, 0o600)
140140
}
141+
142+
// SaveDefaultAgent reads the current config from disk, updates only the
143+
// default_agent field, and writes it back. This avoids overwriting other
144+
// fields that may have been modified externally while the program is running.
145+
func SaveDefaultAgent(name string) error {
146+
path, err := ConfigPath()
147+
if err != nil {
148+
return err
149+
}
150+
151+
raw, err := os.ReadFile(path)
152+
if err != nil {
153+
return fmt.Errorf("read config: %w", err)
154+
}
155+
156+
var doc map[string]json.RawMessage
157+
if err := json.Unmarshal(raw, &doc); err != nil {
158+
return fmt.Errorf("parse config: %w", err)
159+
}
160+
161+
nameJSON, err := json.Marshal(name)
162+
if err != nil {
163+
return fmt.Errorf("marshal agent name: %w", err)
164+
}
165+
doc["default_agent"] = nameJSON
166+
167+
data, err := json.MarshalIndent(doc, "", " ")
168+
if err != nil {
169+
return fmt.Errorf("marshal config: %w", err)
170+
}
171+
172+
return os.WriteFile(path, data, 0o600)
173+
}

0 commit comments

Comments
 (0)