Skip to content

Commit 0d0704d

Browse files
author
anqiang.ex
committed
fix(core): drop unclosed and uppercase reasoning blocks from titles
Follow-up to the previous commit: a title stream truncated mid-reasoning leaves `<think>…` with no closing tag, which the closed-block pattern did not match, so the tag still became the title. Uppercase tags and a stray closing tag without an opening block leaked the same way. Match case-insensitively, treat end of output as closing an unterminated block, and drop any leftover tag. Reasoning-only output then yields no title at all, so the session keeps its fallback instead of gaining a reasoning-only one. Adds cases for a truncated uppercase block and for a stray closing tag.
1 parent cdd6abd commit 0d0704d

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ export const layer = Layer.effect(
9191
yield* recordUsage
9292
if (failed) return
9393
// Some OpenAI-compatible gateways stream the title model's reasoning inline rather than as a
94-
// separate part, and its first line would otherwise become the title.
94+
// separate part, and its first line would otherwise become the title. A truncated stream can stop
95+
// mid-reasoning, so an unclosed block and any leftover tag are dropped too.
9596
return chunks
9697
.join("")
97-
.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
98+
.replace(/<think>[\s\S]*?(<\/think>|$)\s*/gi, "")
99+
.replace(/<\/?think>/gi, "")
98100
.split("\n")
99101
.map((line) => line.trim())
100102
.find((line) => line.length > 0)

‎packages/core/test/session-title.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,48 @@ it.effect("drops inline reasoning blocks split across stream deltas", () =>
276276
}),
277277
)
278278

279+
it.effect("drops an unclosed or uppercase reasoning block", () =>
280+
Effect.gen(function* () {
281+
yield* enableTitleAgent
282+
const sessionID = Session.ID.make("ses_title_partial_reasoning")
283+
yield* insertSession(sessionID)
284+
yield* prompt(sessionID, "list my mcp servers")
285+
// A truncated stream stops mid-reasoning, and some models tag it in uppercase.
286+
titleStream = () =>
287+
Stream.make(
288+
LLMEvent.textDelta({ id: "title", text: "<THINK>Need a brief title." }),
289+
LLMEvent.finish({ reason: { normalized: "stop" } }),
290+
)
291+
292+
const title = yield* SessionTitle.Service
293+
yield* title.generate(sessionID)
294+
295+
const store = yield* SessionStore.Service
296+
// Reasoning alone leaves no title, so the fallback stays instead of a tag being stored.
297+
expect((yield* store.get(sessionID))?.title).toBeUndefined()
298+
}),
299+
)
300+
301+
it.effect("strips a stray closing tag that has no opening block", () =>
302+
Effect.gen(function* () {
303+
yield* enableTitleAgent
304+
const sessionID = Session.ID.make("ses_title_stray_tag")
305+
yield* insertSession(sessionID)
306+
yield* prompt(sessionID, "list my mcp servers")
307+
titleStream = () =>
308+
Stream.make(
309+
LLMEvent.textDelta({ id: "title", text: "</think>MCP servers list" }),
310+
LLMEvent.finish({ reason: { normalized: "stop" } }),
311+
)
312+
313+
const title = yield* SessionTitle.Service
314+
yield* title.generate(sessionID)
315+
316+
const store = yield* SessionStore.Service
317+
expect((yield* store.get(sessionID))?.title).toBe("MCP servers list")
318+
}),
319+
)
320+
279321
it.effect("runs title hooks instead of context hooks", () =>
280322
Effect.gen(function* () {
281323
yield* enableTitleAgent

0 commit comments

Comments
 (0)