Skip to content

Commit b7c1757

Browse files
author
saravmajestic
committed
fix: [AI-6949] fix boolean coercion for transcript query params and strengthen flag tests
- Use z.preprocess to handle explicit "false" string correctly: z.coerce.boolean() coerces Boolean("false") = true, so ?thinking=false was silently treated as true - Fix import to use @/cli alias consistent with other server routes - Update flag test to build a session with real reasoning parts and assert both ?thinking=true includes _Thinking:_ and ?thinking=false excludes it
1 parent ce6058f commit b7c1757

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

‎packages/opencode/src/server/routes/session.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hono } from "hono"
2-
import { formatTranscript } from "../../cli/cmd/tui/util/transcript"
2+
import { formatTranscript } from "@/cli/cmd/tui/util/transcript"
33
import { stream } from "hono/streaming"
44
import { describeRoute, validator, resolver } from "hono-openapi"
55
import { SessionID, MessageID, PartID } from "@/session/schema"
@@ -491,9 +491,9 @@ export const SessionRoutes = lazy(() =>
491491
validator(
492492
"query",
493493
z.object({
494-
thinking: z.coerce.boolean().optional().default(false).meta({ description: "Include reasoning/thinking parts" }),
495-
toolDetails: z.coerce.boolean().optional().default(false).meta({ description: "Include tool input/output details" }),
496-
assistantMetadata: z.coerce.boolean().optional().default(false).meta({ description: "Include assistant model/timing metadata" }),
494+
thinking: z.preprocess(v => v === "false" ? false : v, z.coerce.boolean()).optional().default(false).meta({ description: "Include reasoning/thinking parts" }),
495+
toolDetails: z.preprocess(v => v === "false" ? false : v, z.coerce.boolean()).optional().default(false).meta({ description: "Include tool input/output details" }),
496+
assistantMetadata: z.preprocess(v => v === "false" ? false : v, z.coerce.boolean()).optional().default(false).meta({ description: "Include assistant model/timing metadata" }),
497497
}),
498498
),
499499
async (c) => {

‎packages/opencode/test/server/session-transcript.test.ts‎

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ async function addUserMessage(sessionID: SessionID, text: string) {
4747
return id
4848
}
4949

50+
async function addAssistantMessageWithReasoning(sessionID: SessionID, parentID: MessageID, reasoningText: string) {
51+
const id = MessageID.ascending()
52+
await Session.updateMessage({
53+
id,
54+
sessionID,
55+
role: "assistant",
56+
agent: "build",
57+
modelID: "claude-sonnet",
58+
providerID: "anthropic",
59+
mode: "",
60+
parentID,
61+
path: { cwd: "/test", root: "/test" },
62+
cost: 0,
63+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
64+
time: { created: Date.now() },
65+
} as unknown as MessageV2.Info)
66+
await Session.updatePart({
67+
id: PartID.ascending(),
68+
sessionID,
69+
messageID: id,
70+
type: "reasoning",
71+
text: reasoningText,
72+
time: { start: Date.now() },
73+
} as unknown as Parameters<typeof Session.updatePart>[0])
74+
return id
75+
}
76+
5077
describe("session transcript endpoint", () => {
5178
test("returns 200 with text/plain markdown for a session", async () => {
5279
await using tmp = await tmpdir({ git: true })
@@ -109,22 +136,36 @@ describe("session transcript endpoint", () => {
109136
)
110137
})
111138

112-
test("accepts thinking, toolDetails, and assistantMetadata query params", async () => {
139+
test("thinking=true includes reasoning, thinking=false and default exclude it", async () => {
113140
await using tmp = await tmpdir({ git: true })
114141
await withoutWatcher(() =>
115142
Instance.provide({
116143
directory: tmp.path,
117144
fn: async () => {
118-
const session = await Session.create({ title: "Options Session" })
119-
await addUserMessage(session.id, "test message")
145+
const session = await Session.create({ title: "Reasoning Session" })
146+
const userMsgId = await addUserMessage(session.id, "think about this")
147+
await addAssistantMessageWithReasoning(session.id, userMsgId, "inner thoughts here")
120148
const app = Server.Default()
121149

122-
const res = await app.request(
123-
`/session/${session.id}/transcript?thinking=true&toolDetails=true&assistantMetadata=true`,
124-
)
125-
expect(res.status).toBe(200)
126-
const body = await res.text()
127-
expect(body).toStartWith("# ")
150+
// ?thinking=true must include the reasoning block
151+
const withThinking = await app.request(`/session/${session.id}/transcript?thinking=true`)
152+
expect(withThinking.status).toBe(200)
153+
const bodyWithThinking = await withThinking.text()
154+
expect(bodyWithThinking).toContain("_Thinking:_")
155+
expect(bodyWithThinking).toContain("inner thoughts here")
156+
157+
// ?thinking=false must NOT include it (validates the z.preprocess fix:
158+
// old z.coerce.boolean() coerced "false" string → Boolean("false") = true)
159+
const withFalse = await app.request(`/session/${session.id}/transcript?thinking=false`)
160+
expect(withFalse.status).toBe(200)
161+
const bodyWithFalse = await withFalse.text()
162+
expect(bodyWithFalse).not.toContain("_Thinking:_")
163+
164+
// default (no param) also excludes reasoning
165+
const withDefault = await app.request(`/session/${session.id}/transcript`)
166+
expect(withDefault.status).toBe(200)
167+
const bodyWithDefault = await withDefault.text()
168+
expect(bodyWithDefault).not.toContain("_Thinking:_")
128169

129170
await Session.remove(session.id)
130171
},

0 commit comments

Comments
 (0)