Skip to content

Commit f901b9c

Browse files
author
tbartik
committed
fix(tui,app): keep manually selected model across turns
A selected model was overwritten by the agent's configured model after every turn, so staying on a non-default model was impossible. TUI: Prompt restored the model from the last user message on session hydration. Drop that, and instead seed the model from durable session state only when the agent has no local override. App: the agent Select calls agents.select even when the value is unchanged, and agent.set always rewrote the model as `item.model ?? prev?.model`. Skip re-applying the already-persisted agent so an explicit pick survives. Switching agents still applies that agent's model. Closes anomalyco#39319
1 parent 7336cc5 commit f901b9c

6 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎packages/app/src/context/local-agent.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2-
import { hasCustomAgent, resolveAgent } from "./local-agent"
2+
import { hasCustomAgent, resolveAgent, shouldApplyAgent } from "./local-agent"
33

44
describe("hasCustomAgent", () => {
55
test("detects explicitly custom agents", () => {
@@ -27,3 +27,17 @@ describe("resolveAgent", () => {
2727
expect(resolveAgent([{ name: "custom" }], "missing")?.name).toBe("custom")
2828
})
2929
})
30+
31+
describe("shouldApplyAgent", () => {
32+
test("applies the first selection so state is persisted", () => {
33+
expect(shouldApplyAgent({ agent: "build", persisted: undefined })).toBe(true)
34+
})
35+
36+
test("applies a real agent switch", () => {
37+
expect(shouldApplyAgent({ agent: "plan", persisted: "build" })).toBe(true)
38+
})
39+
40+
test("skips re-applying the already-persisted agent", () => {
41+
expect(shouldApplyAgent({ agent: "build", persisted: "build" })).toBe(false)
42+
})
43+
})

‎packages/app/src/context/local-agent.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ export function hasCustomAgent(items: Array<{ native?: boolean }>) {
55
export function resolveAgent<T extends { name: string }>(items: T[], name?: string) {
66
return items.find((item) => item.name === name) ?? items.find((item) => item.name === "build") ?? items[0]
77
}
8+
9+
// An agent carries a configured model, so applying an agent also overwrites the
10+
// active model. Re-applying the already-persisted agent must therefore be skipped,
11+
// otherwise a control that re-emits the current agent silently discards an explicit
12+
// model pick. The first write for an agent must still land, so this compares against
13+
// persisted state rather than the resolved current agent (which falls back to build).
14+
export function shouldApplyAgent(input: { agent: string; persisted: string | undefined }) {
15+
return input.persisted !== input.agent
16+
}

‎packages/app/src/context/local.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useModels } from "@/context/models"
77
import { useSettings } from "@/context/settings"
88
import { useProviders } from "@/hooks/use-providers"
99
import { Persist, persisted } from "@/utils/persist"
10-
import { hasCustomAgent, resolveAgent } from "./local-agent"
10+
import { hasCustomAgent, resolveAgent, shouldApplyAgent } from "./local-agent"
1111
import { cycleModelVariant, getConfiguredAgentVariant, resolveModelVariant } from "./model-variant"
1212
import { useSDK } from "./sdk"
1313
import { useSync } from "./sync"
@@ -193,6 +193,8 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
193193
return
194194
}
195195

196+
if (!shouldApplyAgent({ agent: item.name, persisted: scope()?.agent })) return
197+
196198
batch(() => {
197199
setStore("current", item.name)
198200
setStore("last", {

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ export function Prompt(props: PromptProps) {
307307
),
308308
)
309309

310-
// Initialize agent/model/variant from last user message when session changes
310+
// Initialize the agent from the last user message when the session changes.
311+
// The model follows durable session updates while preserving local picks.
311312
let syncedSessionID: string | undefined
312313
createEffect(() => {
313314
const sessionID = props.sessionID
@@ -323,10 +324,6 @@ export function Prompt(props: PromptProps) {
323324
if (msg.agent && isPrimaryAgent) {
324325
// Keep command line --agent if specified.
325326
if (!args.agent) local.agent.set(msg.agent)
326-
if (msg.model) {
327-
local.model.set(msg.model)
328-
local.model.variant.set(msg.model.variant)
329-
}
330327
}
331328
}
332329
})

‎packages/tui/src/context/local.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export function parseModel(model: string) {
3232
}
3333
}
3434

35+
// A session carries the model used for its last turn, which is the agent's
36+
// configured model unless the user picked one. Seeding from it must therefore be
37+
// skipped once the agent has a local override, or the pick is silently reverted
38+
// on the next session update.
39+
export function shouldSeedSessionModel(override: { providerID: string; modelID: string } | undefined) {
40+
return !override
41+
}
42+
3543
export function recentModels(
3644
model: { providerID: string; modelID: string },
3745
recent: { providerID: string; modelID: string }[],
@@ -244,6 +252,27 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
244252
)
245253
})
246254

255+
// Seed the model from durable session state, but never overwrite a model the
256+
// user picked for this agent. Otherwise a session update mid-conversation
257+
// reverts the selection to the agent's configured model.
258+
createEffect(() => {
259+
const currentRoute = route.data
260+
if (currentRoute.type !== "session") return
261+
262+
const session = sync.data.session.find((item) => item.id === currentRoute.sessionID)
263+
const selected = session?.model
264+
const a = agent.current()
265+
if (!a || !selected) return
266+
if (!shouldSeedSessionModel(modelStore.model[a.name])) return
267+
268+
const model = { providerID: selected.providerID, modelID: selected.id }
269+
if (!isModelValid(model)) return
270+
batch(() => {
271+
setModelStore("model", a.name, model)
272+
setModelStore("variant", `${selected.providerID}/${selected.id}`, selected.variant ?? "default")
273+
})
274+
})
275+
247276
return {
248277
current: currentModel,
249278
get ready() {

‎packages/tui/test/context/local.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "bun:test"
2-
import { parseModel, recentModels } from "../../src/context/local"
2+
import { parseModel, recentModels, shouldSeedSessionModel } from "../../src/context/local"
33

44
test("parses model IDs containing slashes", () => {
55
expect(parseModel("provider/family/model")).toEqual({
@@ -20,3 +20,11 @@ test("moves a model to the front, deduplicates, and limits recents", () => {
2020
...recent.slice(6, 10),
2121
])
2222
})
23+
24+
test("seeds the model from session state when the agent has no override", () => {
25+
expect(shouldSeedSessionModel(undefined)).toBe(true)
26+
})
27+
28+
test("keeps a picked model instead of reseeding it from session state", () => {
29+
expect(shouldSeedSessionModel({ providerID: "provider", modelID: "model" })).toBe(false)
30+
})

0 commit comments

Comments
 (0)