From fce18e721a2e2fb9f75146e7d68965c1a1d68018 Mon Sep 17 00:00:00 2001 From: imadalin Date: Sat, 21 Nov 2026 01:53:52 +0200 Subject: [PATCH] fix(session): preserve prompt routing on follow-ups --- packages/opencode/src/session/prompt.ts | 19 ++++-- .../test/server/httpapi-session.test.ts | 59 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 0f85d44f209b..5792487b0ea7 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -633,7 +633,13 @@ const layer = Layer.effect( }) const createUserMessage = Effect.fn("SessionPrompt.createUserMessage")(function* (input: PromptInput) { - const agentName = input.agent + const current = yield* db + .select({ agent: SessionTable.agent, model: SessionTable.model }) + .from(SessionTable) + .where(eq(SessionTable.id, input.sessionID)) + .get() + .pipe(Effect.orDie) + const agentName = input.agent ?? current?.agent const ag = agentName ? yield* agents.get(agentName) : yield* agents.defaultInfo() if (!ag) { const available = (yield* agents.list()).filter((a) => !a.hidden).map((a) => a.name) @@ -651,7 +657,13 @@ const layer = Layer.effect( .getModel(model.providerID, model.modelID) .pipe(Effect.catchIf(Provider.ModelNotFoundError.isInstance, () => Effect.succeed(undefined))) : undefined - const variant = input.variant ?? (ag.variant && full?.variants?.[ag.variant] ? ag.variant : undefined) + const sessionVariant = + current?.model?.providerID === model.providerID && + current.model.id === model.modelID && + current.model.variant !== "default" + ? current.model.variant + : undefined + const variant = input.variant ?? (ag.variant && full?.variants?.[ag.variant] ? ag.variant : sessionVariant) const info: SessionV1.User = { id: input.messageID ?? MessageID.ascending(), @@ -669,9 +681,8 @@ const layer = Layer.effect( format: input.format, } - const current = yield* sessions.get(input.sessionID).pipe(Effect.orDie) if ( - current.agent !== info.agent || + current?.agent !== info.agent || current.model?.providerID !== info.model.providerID || current.model?.id !== info.model.modelID || (current.model?.variant === "default" ? undefined : current.model?.variant) !== info.model.variant diff --git a/packages/opencode/test/server/httpapi-session.test.ts b/packages/opencode/test/server/httpapi-session.test.ts index 9d7643cb3309..3b0921689a64 100644 --- a/packages/opencode/test/server/httpapi-session.test.ts +++ b/packages/opencode/test/server/httpapi-session.test.ts @@ -427,6 +427,65 @@ describe("session HttpApi", () => { }).pipe(Effect.provide(TestLLMServer.layer), Effect.provide(AppNodeBuilder.build(CrossSpawnSpawner.node))), ) + it.instance( + "preserves session routing when prompt_async omits agent, model, and variant", + () => + Effect.gen(function* () { + const test = yield* TestInstance + const session = yield* createSession({ title: "prompt routing" }) + const headers = { "x-opencode-directory": test.directory, "content-type": "application/json" } + const path = pathFor(SessionPaths.promptAsync, { sessionID: session.id }) + + const first = yield* request(path, { + method: "POST", + headers, + body: JSON.stringify({ + agent: "plan", + model: { providerID: "test", modelID: "test-model" }, + variant: "medium", + parts: [{ type: "text", text: "first" }], + noReply: true, + }), + }) + expect(first.status).toBe(204) + yield* pollWithTimeout( + requestJson(pathFor(SessionPaths.messages, { sessionID: session.id }), { + headers, + }).pipe( + Effect.map((messages) => { + const prompts = messages.filter((message) => message.info.role === "user") + return prompts.length === 1 ? prompts : undefined + }), + ), + "timed out waiting for the first prompt_async message", + ) + + const second = yield* request(path, { + method: "POST", + headers, + body: JSON.stringify({ parts: [{ type: "text", text: "second" }], noReply: true }), + }) + expect(second.status).toBe(204) + + const users = yield* pollWithTimeout( + requestJson(pathFor(SessionPaths.messages, { sessionID: session.id }), { + headers, + }).pipe( + Effect.map((messages) => { + const prompts = messages.filter((message) => message.info.role === "user") + return prompts.length === 2 ? prompts : undefined + }), + ), + "timed out waiting for both prompt_async messages", + ) + expect(users.at(-1)?.info).toMatchObject({ + agent: "plan", + model: { providerID: "test", modelID: "test-model", variant: "medium" }, + }) + }), + { git: true, config: { formatter: false, lsp: false } }, + ) + it.instance( "returns v2 public request errors for cursor and workspace query failures", () =>