Skip to content

Commit 7f83202

Browse files
committed
fix(session): retain model preference on input promotion (anomalyco#130)
1 parent c9a4440 commit 7f83202

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎packages/core/src/session/projector.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,22 @@ const layer = Layer.effectDiscard(
439439
.where(eq(V1InputTable.request_id, row.request_id))
440440
.run()
441441
.pipe(Effect.orDie)
442+
const current = yield* db
443+
.select({ model: SessionTable.model })
444+
.from(SessionTable)
445+
.where(eq(SessionTable.id, info.sessionID))
446+
.get()
447+
.pipe(Effect.orDie)
442448
yield* db
443449
.update(SessionTable)
444450
.set({
445451
agent: info.agent,
446-
model: { id: info.model.modelID, providerID: info.model.providerID, variant: info.model.variant },
452+
model: {
453+
id: info.model.modelID,
454+
providerID: info.model.providerID,
455+
variant: info.model.variant,
456+
preferred: current?.model?.preferred,
457+
},
447458
time_updated: info.time.created,
448459
})
449460
.where(eq(SessionTable.id, info.sessionID))

‎packages/opencode/test/cli/serve/input-restart.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,63 @@ cliIt.live(
8787
}),
8888
60_000,
8989
)
90+
91+
cliIt.live(
92+
"model preference survives promoted input and process restart without replay",
93+
({ opencode, home, llm }) =>
94+
Effect.gen(function* () {
95+
const client = yield* HttpClient.HttpClient
96+
const env = {
97+
OPENCODE_DB: `${home}/model-preference-restart.sqlite`,
98+
OPENCODE_TEST_MANAGED_CONFIG_DIR: `${home}/managed`,
99+
OPENCODE_CONFIG: "",
100+
OPENCODE_CONFIG_DIR: `${home}/.config/opencode`,
101+
OPENCODE_PERMISSION: "{}",
102+
OPENCODE_EXPERIMENTAL: "false",
103+
OPENCODE_EXPERIMENTAL_EVENT_SYSTEM: "true",
104+
}
105+
const first = yield* opencode.serve({ env, hostname: "127.0.0.1", port: yield* freePort })
106+
const created = yield* client.post(`${first.url}/session`, {
107+
body: HttpBody.jsonUnsafe({ title: "Preference restart", agent: "build", model: { id: "test-model", providerID: "test" } }),
108+
})
109+
expect(created.status).toBe(200)
110+
const session = yield* created.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(SessionV1.SessionInfo)))
111+
const path = `/session/${session.id}`
112+
const preference = yield* client.patch(`${first.url}${path}`, {
113+
body: HttpBody.jsonUnsafe({ model: { providerID: "test", modelID: "test-model" } }),
114+
})
115+
expect(preference.status).toBe(200)
116+
expect(yield* preference.json).toMatchObject({ preferredModel: { id: "test-model" } })
117+
yield* llm.text("synthetic turn")
118+
const payload = { requestID: "model-preference-restart", delivery: "queue", text: "synthetic input" }
119+
expect((yield* client.post(`${first.url}${path}/input`, { body: HttpBody.jsonUnsafe(payload) })).status).toBe(200)
120+
yield* llm.wait(1)
121+
const receipt = yield* pollWithTimeout(
122+
Effect.gen(function* () {
123+
const response = yield* client.get(`${first.url}${path}/input/${payload.requestID}`)
124+
const row = yield* response.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(SessionV1.InputReceipt)))
125+
return row.state === "promoted" ? row : undefined
126+
}),
127+
"input did not promote",
128+
"15 seconds",
129+
)
130+
yield* pollWithTimeout(
131+
Effect.gen(function* () {
132+
const response = yield* client.get(`${first.url}${path}/message`)
133+
const messages = yield* response.json.pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(SessionV1.WithParts))))
134+
return messages.some((item) => item.info.role === "assistant" && item.info.finish === "stop") ? true : undefined
135+
}),
136+
"input turn did not finish",
137+
"15 seconds",
138+
)
139+
expect(yield* (yield* client.get(`${first.url}${path}`)).json).toMatchObject({ preferredModel: { id: "test-model" } })
140+
first.kill()
141+
yield* Effect.promise(() => first.exited)
142+
const second = yield* opencode.serve({ env, hostname: "127.0.0.1", port: yield* freePort })
143+
expect(yield* (yield* client.get(`${second.url}${path}`)).json).toMatchObject({ preferredModel: { id: "test-model" } })
144+
expect(yield* (yield* client.get(`${second.url}${path}/input/${payload.requestID}`)).json).toEqual(receipt)
145+
expect(yield* (yield* client.post(`${second.url}${path}/input`, { body: HttpBody.jsonUnsafe(payload) })).json).toEqual(receipt)
146+
expect(yield* llm.inputs).toHaveLength(1)
147+
}),
148+
60_000,
149+
)

‎packages/opencode/test/server/httpapi-session-model.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ it.live("validated model PATCH freezes earlier inputs and preserves the active p
7575
expect(messages.flatMap((item) => item.info.role === "assistant" ? [item.info.modelID] : [])).toEqual([
7676
ModelV2.ID.make("test-model"), ModelV2.ID.make("test-model"), ModelV2.ID.make("next-model"),
7777
])
78+
expect(yield* (yield* requestInDirectory(path, dir)).json).toMatchObject({ preferredModel: { id: "next-model" } })
7879
expect(yield* (yield* post("input", { requestID: "before", delivery: "queue", text: "queued before switch" })).json)
7980
.toEqual(yield* (yield* requestInDirectory(`${path}/input/before`, dir)).json)
8081
expect((yield* post("prompt_async", { agent: "build", parts: [{ type: "text", text: "normal after switch" }] })).status).toBe(204)

0 commit comments

Comments
 (0)