Skip to content

Commit 2765196

Browse files
anandgupta42claude
andcommitted
fix: handle unclosed <think> tags from truncated model output
When the small model hits its token limit mid-generation, `<think>` tags may not have a closing `</think>`. The previous regex required a closing tag, which would leak the entire reasoning block into the enhanced prompt. Now `stripThinkTags()` matches both closed and unclosed think blocks. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ea70958 commit 2765196

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

packages/opencode/src/altimate/enhance-prompt.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ User: "migrate this from snowflake to bigquery"
5454
Enhanced: "Migrate the SQL from Snowflake dialect to BigQuery dialect. Convert Snowflake-specific functions (e.g. DATEADD, IFF, QUALIFY) to BigQuery equivalents. Preserve the query logic and verify the translated query is syntactically valid."`
5555

5656
export function stripThinkTags(text: string) {
57-
return text.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
57+
// Match closed <think>...</think> blocks, and also unclosed <think>... to end of string
58+
// (unclosed tags happen when the model hits token limit mid-generation)
59+
return text.replace(/<think>[\s\S]*?(?:<\/think>\s*|$)/g, "")
5860
}
5961

6062
export function clean(text: string) {

packages/opencode/test/altimate/enhance-prompt.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ describe("enhance-prompt stripThinkTags()", () => {
108108
test("handles nested angle brackets inside think tags", () => {
109109
expect(stripThinkTags("<think>check if x < 5 and y > 3</think>result")).toBe("result")
110110
})
111+
112+
test("strips unclosed think tag (model hit token limit)", () => {
113+
expect(stripThinkTags("<think>reasoning that got cut off")).toBe("")
114+
})
115+
116+
test("strips unclosed think tag with content before it", () => {
117+
expect(stripThinkTags("good content <think>trailing reasoning")).toBe("good content ")
118+
})
111119
})
112120

113121
describe("enhance-prompt combined pipeline", () => {

0 commit comments

Comments
 (0)