sessionfile: defer backend/model overrides until after agent= in handleCfg

Ensures manual model/backend settings always win over agent.json defaults
regardless of line order in ctl writes.
This commit is contained in:
Levi Neely 2026-05-18 12:16:34 +02:00
parent 06b0775b66
commit 479ac1b9d4
1 changed files with 24 additions and 2 deletions

View File

@ -328,6 +328,7 @@ func (h *sessionHelper) cfgContent() string {
func (h *sessionHelper) handleCfg(input string) error { func (h *sessionHelper) handleCfg(input string) error {
p := h.sess.Core.GenerationParams() p := h.sess.Core.GenerationParams()
hasParams := false hasParams := false
var deferredBackend, deferredModel string
for _, line := range strings.Split(input, "\n") { for _, line := range strings.Split(input, "\n") {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line == "" { if line == "" {
@ -339,14 +340,28 @@ func (h *sessionHelper) handleCfg(input string) error {
} }
k, v = strings.TrimSpace(k), strings.TrimSpace(v) k, v = strings.TrimSpace(k), strings.TrimSpace(v)
switch k { switch k {
case "backend", "model", "agent": case "agent":
if v == "" { if v == "" {
continue continue
} }
if h.sess.Core.IsRunning() { if h.sess.Core.IsRunning() {
return fmt.Errorf("cannot switch %s while agent is running", k) return fmt.Errorf("cannot switch %s while agent is running", k)
} }
h.sess.Core.Submit(h.sess.Ctx, "/"+k+" "+v, h.makePublish()) h.sess.Core.Submit(h.sess.Ctx, "/agent "+v, h.makePublish())
case "backend", "model":
if v == "" {
continue
}
if h.sess.Core.IsRunning() {
return fmt.Errorf("cannot switch %s while agent is running", k)
}
// Defer backend/model until after the loop so they override
// any values set by agent=.
if k == "backend" {
deferredBackend = v
} else {
deferredModel = v
}
case "cwd": case "cwd":
if v == "" { if v == "" {
continue continue
@ -458,6 +473,13 @@ func (h *sessionHelper) handleCfg(input string) error {
// state → read-only, silently ignored // state → read-only, silently ignored
} }
} }
// Apply backend/model after agent= so manual overrides win.
if deferredBackend != "" {
h.sess.Core.Submit(h.sess.Ctx, "/backend "+deferredBackend, h.makePublish())
}
if deferredModel != "" {
h.sess.Core.Submit(h.sess.Ctx, "/model "+deferredModel, h.makePublish())
}
if hasParams { if hasParams {
if h.sess.Core.IsRunning() { if h.sess.Core.IsRunning() {
return fmt.Errorf("cannot change params while agent is running") return fmt.Errorf("cannot change params while agent is running")