From 897643db18de21a4f498378e34fa9b06ff8accaa Mon Sep 17 00:00:00 2001 From: sausheong Date: Mon, 7 Sep 2026 00:22:07 +0800 Subject: [PATCH] Add a regression test proving a mid-session model switch is real Verifies, at the Runtime.Run level, that mutating Model and DynamicIdentityHint between two Run calls in the same session (exactly what hand's tui.Controller.SwitchModel does) actually changes the ChatRequest.Model sent on the next call, not just the system prompt text. If this ever regressed, a caller-side "/model" fix would look correct in its own unit tests while the model actually being queried silently stayed the old one. --- runtime/model_switch_test.go | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 runtime/model_switch_test.go diff --git a/runtime/model_switch_test.go b/runtime/model_switch_test.go new file mode 100644 index 0000000..86377bf --- /dev/null +++ b/runtime/model_switch_test.go @@ -0,0 +1,62 @@ +package runtime + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/sausheong/harness/session" + "github.com/sausheong/harness/tool" +) + +// TestModelSwitchMidSession_ActuallyChangesRequestModelAndHint reproduces, +// at the harness level, exactly what a caller like hand's tui.Controller +// does on a live model switch: mutate Model and DynamicIdentityHint on an +// already-running Runtime between two Run calls in the same session. This +// is the mechanism a caller-side fix for "the model still thinks it's the +// old one" ultimately rests on — if the second Run's actual ChatRequest +// didn't target the new model, no amount of system-prompt wording could +// have fixed the symptom, since the model actually being queried would +// still be the old one. +func TestModelSwitchMidSession_ActuallyChangesRequestModelAndHint(t *testing.T) { + rec := &recordingProvider{reply: "ok"} + sess := session.NewSession("test-agent", "test-key") + reg := tool.NewRegistry() + + rt := &Runtime{ + LLM: rec, + Tools: reg, + Session: sess, + Model: "meta/muse-spark-1.3", + Provider: "openrouter", + Workspace: t.TempDir(), + MaxTurns: 5, + DynamicIdentityHint: "You are running as the model \"openrouter/meta/muse-spark-1.3\".", + } + + events, err := rt.Run(context.Background(), "which model am I using", nil) + require.NoError(t, err) + for range events { + } + + // Simulate exactly what Controller.SwitchModel does. + rt.Model = "anthropic/claude-sonnet-5" + rt.DynamicIdentityHint = "You are running as the model \"openrouter/anthropic/claude-sonnet-5\"." + + events, err = rt.Run(context.Background(), "which model am I using now", nil) + require.NoError(t, err) + for range events { + } + + rec.mu.Lock() + defer rec.mu.Unlock() + require.Len(t, rec.requests, 2) + require.Equal(t, "meta/muse-spark-1.3", rec.requests[0].Model) + require.Equal(t, "anthropic/claude-sonnet-5", rec.requests[1].Model, "second request must target the switched model") + + part2 := rec.requests[1].SystemPromptParts + require.Len(t, part2, 2, "expected static + dynamic system prompt parts") + require.Contains(t, part2[1].Text, "openrouter/anthropic/claude-sonnet-5") + require.NotContains(t, part2[1].Text, "muse-spark-1.3", "dynamic hint must not still name the old model") +}