From 4dca460b3aa3b47a00e13ea94ae337fb9f3d0844 Mon Sep 17 00:00:00 2001 From: "microsoft-agent-framework-for-go[bot]" <324824725+microsoft-agent-framework-for-go[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:31:37 +0000 Subject: [PATCH] Fall back when a restored agent mode is no longer configured Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- agent/harness/agentmode/agentmode.go | 15 ++++++++++ agent/harness/agentmode/agentmode_test.go | 35 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/agent/harness/agentmode/agentmode.go b/agent/harness/agentmode/agentmode.go index 17bbc641..651aabb4 100644 --- a/agent/harness/agentmode/agentmode.go +++ b/agent/harness/agentmode/agentmode.go @@ -238,11 +238,26 @@ func (p *Provider) loadSessionState(session *agent.Session) *state { } var s state if found, _ := session.Get(stateKey, &s); found { + p.ensureConfiguredMode(session, &s) return &s } return &state{CurrentMode: p.defaultMode} } +// ensureConfiguredMode resets a restored mode that is no longer configured to +// the default mode and persists the correction. +func (p *Provider) ensureConfiguredMode(session *agent.Session, s *state) { + if _, ok := p.validModes[s.CurrentMode]; ok { + return + } + s.CurrentMode = p.defaultMode + // A pending notification must not announce a default-to-default change. + if s.PreviousMode == p.defaultMode { + s.PreviousMode = "" + } + p.saveSessionState(session, s) +} + func (p *Provider) saveSessionState(session *agent.Session, s *state) { if session == nil || s == nil { return diff --git a/agent/harness/agentmode/agentmode_test.go b/agent/harness/agentmode/agentmode_test.go index 75274a86..13a8d364 100644 --- a/agent/harness/agentmode/agentmode_test.go +++ b/agent/harness/agentmode/agentmode_test.go @@ -197,6 +197,41 @@ func TestCustomModes_SetModeValidatesAgainstList(t *testing.T) { } } +func TestRestoredUnconfiguredModeFallsBackToDefault(t *testing.T) { + v1 := agentmode.New(agentmode.Config{ + Modes: []agentmode.Mode{ + {Name: "draft", Instructions: "Draft instructions."}, + {Name: "review", Instructions: "Review instructions."}, + }, + }) + opts := sessionOpts() + session := mustSession(t, opts) + if err := v1.SetMode(session, "review", false); err != nil { + t.Fatal(err) + } + + v2 := agentmode.New(agentmode.Config{ + Modes: []agentmode.Mode{ + {Name: "draft", Instructions: "Draft instructions."}, + {Name: "publish", Instructions: "Publish instructions."}, + }, + }) + if mode := v2.Mode(session); mode != "draft" { + t.Fatalf("expected fallback to 'draft', got %q", mode) + } + + ctx := context.Background() + msgs, _, err := invokeProvider(v2, ctx, newMessages("hi"), opts...) + if err != nil { + t.Fatal(err) + } + for _, m := range msgs { + if strings.Contains(m.Contents.Text(), "Mode changed") { + t.Errorf("unexpected mode change notification: %q", m.Contents.Text()) + } + } +} + // 5. Options_CustomDefaultMode_IsUsed func TestCustomDefaultMode_IsUsed(t *testing.T) { p := agentmode.New(agentmode.Config{