From 4aa5f5c28497768933f1561b5a7eae8544104959 Mon Sep 17 00:00:00 2001 From: kim Date: Sat, 3 Oct 2026 22:50:36 +0900 Subject: [PATCH] =?UTF-8?q?fix(thinking-separation):=20=EB=A7=88=ED=81=AC?= =?UTF-8?q?=EB=8B=A4=EC=9A=B4=20=ED=91=9C/=EB=AA=A9=EB=A1=9D=20=EB=AC=B8?= =?UTF-8?q?=EB=8B=A8=EC=9D=84=20=EC=B6=94=EB=A1=A0=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A5=98=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=8C=20?= =?UTF-8?q?=E2=80=94=20=ED=91=9C=20=EC=A0=9C=EB=AA=A9=EC=9D=98=20'the=20cu?= =?UTF-8?q?rrent'=EA=B0=80=20=ED=82=A4=EC=9B=8C=EB=93=9C=EC=97=90=20?= =?UTF-8?q?=EA=B1=B8=EB=A0=A4=20=ED=91=9C=20=EC=A0=84=EC=B2=B4=EA=B0=80=20?= =?UTF-8?q?=EC=B6=94=EB=A1=A0=EC=9D=B4=20=EB=90=98=EA=B3=A0=20'|'=20?= =?UTF-8?q?=ED=95=9C=20=EA=B8=80=EC=9E=90=EB=A7=8C=20=EB=8B=B5=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=82=A8=EB=8D=98=20=EC=82=AC=EA=B3=A0.=20?= =?UTF-8?q?=EC=B6=94=EB=A1=A0>=EB=8B=B5=EB=B3=80=20=EC=9D=98=EC=8B=AC=20?= =?UTF-8?q?=EC=8B=9C=20=EC=9B=90=EB=AC=B8=EC=9D=84=20=EB=82=A8=EA=B8=B0?= =?UTF-8?q?=EB=8A=94=20=EC=A7=84=EB=8B=A8=20=EB=A1=9C=EA=B7=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- src/gateway/chat/handle-chat.ts | 5 ++++ src/gateway/chat/thinking-separation.ts | 7 ++++++ tests/thinking-separation.test.ts | 31 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/gateway/chat/handle-chat.ts b/src/gateway/chat/handle-chat.ts index 94beb1c..ad40621 100644 --- a/src/gateway/chat/handle-chat.ts +++ b/src/gateway/chat/handle-chat.ts @@ -1882,6 +1882,11 @@ async function handleChat( console.log(`[v2] INLINE REASONING [${resolvedModelThisRound || '?'}] (${inlineThinking.length} chars): ${inlineThinking.slice(0, 100)}...`); allThinking += (allThinking ? '\n\n' : '') + inlineThinking; sendSSE('thinking', { thinking: inlineThinking }); + // 진단(2026-10-03): 추론으로 분류된 분량이 답변보다 압도적으로 큰 경우(실사례: 표 전체가 추론으로 빠지고 + // "Done!"만 답으로 남음) 원문을 남겨 분류 오판을 재현할 수 있게 한다. + if (String(reply || '').trim().length < 40 && inlineThinking.length > 800) { + console.log(`[v2] THINKING-SEPARATION SUSPECT [${resolvedModelThisRound || '?'}]: reply=${JSON.stringify(String(reply || '').slice(0, 80))} thinkingLen=${inlineThinking.length} RAW=${JSON.stringify(String(response.content || '').slice(0, 6000))}`); + } } const rawAssistantText = String(response.content || '').trim(); diff --git a/src/gateway/chat/thinking-separation.ts b/src/gateway/chat/thinking-separation.ts index 4060ece..0db1232 100644 --- a/src/gateway/chat/thinking-separation.ts +++ b/src/gateway/chat/thinking-separation.ts @@ -68,8 +68,15 @@ function classify(cleaned: string, opts?: SeparationOptions): { reply: string; t const reasoningRE = /\b(the user|the tools|the instructions|I need to|I should|let me|the problem|the question|the answer|looking at|first,|second,|wait,|hmm|the response|the correct|the assistant|check the rules|according to|the file|the current|the plan)\b/i; const starterRE = /^(Okay|Ok|Alright|Let me|First|Hmm|So,? |Wait|The user|Looking|I need|I should|Now,? |Since|Given|Based on|Check)/i; + // 마크다운 표/목록/코드블록 문단은 추론 판정에서 제외한다(2026-10-03 실사례: 논문 목록 표의 제목 + // "A Review of the Current Literature"가 `the current`에 걸려 표 전체가 추론으로 분류되고, 마지막 문장 + // 조각 "|" 한 글자만 답으로 남았다). 구조화된 내용은 키워드 한 줄로 추론이라 단정할 수 없다. + const isStructured = (p: string) => + p.split('\n').filter(l => /^\s*(\||[-*]\s|\d+[.)]\s|```)/.test(l)).length >= 3; + let lastIdx = -1; for (let i = 0; i < paragraphs.length; i++) { + if (isStructured(paragraphs[i])) continue; if (reasoningRE.test(paragraphs[i]) || starterRE.test(paragraphs[i])) lastIdx = i; } diff --git a/tests/thinking-separation.test.ts b/tests/thinking-separation.test.ts index c8f464a..41f7ec5 100644 --- a/tests/thinking-separation.test.ts +++ b/tests/thinking-separation.test.ts @@ -164,3 +164,34 @@ describe('glm 영어 셀프토크 접두사 분리', () => { }); }); + +// ── 2026-10-03 실측: 논문 목록 표가 통째로 추론으로 분류돼 "|" 한 글자만 답으로 남은 사고 ── +// 표 첫 행 제목 "A Review of the Current Literature"가 reasoningRE의 `the current`에 걸렸다. +describe('markdown table is never classified as reasoning', () => { + const TABLE = `전체 146건의 검색 결과 중 벌크필 관련 논문 3건을 추출하여 정리해 드립니다. + +| 번호 | PMID | 저널 | 연도 | 제목 | +| :--- | :--- | :--- | :--- | :--- | +| 1 | 28443833 | J Adhes Dent | 2017 | Bulk-Fill Composites: A Review of the Current Literature. | +| 2 | 32030379 | J Adhes Dent | 2020 | Structural/Chemical Characterization and Bond Strength of a New Self-Adhesive Bulk-fill Restorative. | +| 3 | 29178706 | Am J Dent | 2017 | Stress distribution of bulk-fill resin composite in class II restorations. |`; + + test('표 전체가 답변으로 남는다', () => { + const r = separateThinkingFromContent(TABLE, { model: 'gemma4:31b-cloud' }); + assert.equal(r.thinking, ''); + assert.equal(r.reply, TABLE); + }); + + test('표 뒤에 짧은 맺음말이 있어도 표를 지우지 않는다', () => { + const r = separateThinkingFromContent(TABLE + '\n\nDone!', { model: 'gemma4:31b-cloud' }); + assert.ok(r.reply.includes('| 3 | 29178706 |')); + assert.ok(r.reply.includes('A Review of the Current Literature')); + }); + + test('실제 추론 문단은 여전히 분리된다', () => { + const t = 'The user wants a list. Let me check the tools first.\n\n핵심만 말씀드리면 3편입니다. 첫 번째 논문이 가장 최신입니다.'; + const r = separateThinkingFromContent(t, { model: 'gemma4:31b-cloud' }); + assert.ok(r.thinking.includes('The user wants')); + assert.ok(r.reply.startsWith('핵심만')); + }); +});