Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 自愈的产出判据两侧对齐——纯空白不算产出,thinking 不算边界 (#1097)
Dashb26af7091369
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/interruptedTurnAutoResume.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/interruptedTurnAutoResume.test.tsindex 39d5cf7851..f1f79c3af9 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/interruptedTurnAutoResume.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/interruptedTurnAutoResume.test.ts@@ -195,6 +195,33 @@ describe('isSubstantiveProgressEvent', () => {expect(isSubstantiveProgressEvent({ type: 'text', data: { text: 123 } })).toBe(false);});+ // 两侧 translator 转发的 text block / delta 内容是任意的,纯空白同样会原样透出。算成产出+ // 的话:空白 delta 可以让连续失败计数永远停在 1/5(绕过上限),历史里还会显示成+ // 「已重新连接」—— 而用户一个字都没看到(codex P1)。+ it('rejects whitespace-only text (用户什么都没看到,不能算产出)', () => {+ for (const text of [' ', '\n', '\n\n', '\t', ' \r\n ']) {+ expect(+ isSubstantiveProgressEvent({ type: 'text', data: { text } }),+ `text=${JSON.stringify(text)} 不该算产出`,+ ).toBe(false);+ }+ // 夹着空白的真实产出仍然算:实义字符是唯一判据。+ expect(isSubstantiveProgressEvent({ type: 'text', data: { text: '\n好的' } })).toBe(true);+ });++ // 零宽字符是 trim() 的盲区(它只认 \s):只有零宽字符的 text 会被 trim 误判成有内容,+ // 于是自动重连绕过 5 次上限、历史里还显示「已重新连接」(greptile P2)。判据已收敛到+ // shared/visibleText.ts,这条锁的是 main 侧真的接上了它。+ it('rejects zero-width-only text (trim 的盲区)', () => {+ for (const text of ['\u200B', '\uFEFF', '\u200B\u200B\n \u200D', '\u00AD']) {+ expect(+ isSubstantiveProgressEvent({ type: 'text', data: { text } }),+ `text=${JSON.stringify(text)} 用户看不见,不该算产出`,+ ).toBe(false);+ }+ expect(isSubstantiveProgressEvent({ type: 'text', data: { text: '\u200B好' } })).toBe(true);+ });+it('rejects thinking / status / done and other event types', () => {for (const type of ['thinking', 'status', 'done', 'error', 'tool_result', undefined]) {expect(diff --git a/apps/desktop/src/main/maker-ipc/interruptedTurnAutoResume.ts b/apps/desktop/src/main/maker-ipc/interruptedTurnAutoResume.tsindex edbddd3845..5169ad1d04 100644--- a/apps/desktop/src/main/maker-ipc/interruptedTurnAutoResume.ts+++ b/apps/desktop/src/main/maker-ipc/interruptedTurnAutoResume.ts@@ -24,6 +24,8 @@* 社区同型:anthropics/claude-code#38905(SSE 静默中断)。*/+import { hasUserVisibleText } from '../../shared/visibleText.js';+import {isNetworkishErrorMessage,isOverloadErrorMessage,@@ -133,11 +135,21 @@ export function isAutoResumeUserMessage(agentMeta: unknown): boolean {* 这个 agent 事件算不算「模型有实质产出」——即连续失败计数该不该归零、上一次重连该不该* 判成成功的唯一证据。*- * 只认两种:非空 assistant 文本、工具调用。**空文本必须排除**:translator 在若干路径上- * 会推 `text: ''`(流式兜底、空 assistant 消息),把它算成产出的话,一个什么都没产出的- * 重连会既绕过连续失败上限、又在历史里错误显示「已重新连接」(greptile / codex 双报 P1)。+ * 只认两种:**用户看得见的** assistant 文本、工具调用。看不见的一律排除:translator 在若干+ * 路径上会推 `text: ''`(流式兜底、空 assistant 消息),而两侧 translator 转发的 text block /+ * delta 内容是任意的,纯空白(`'\n'`、`' '`)与零宽字符(U+200B/200C/200D、U+FEFF …)同样会+ * 原样透出(`claude-code/translator.ts` 与 `codex/translator.ts` 的 text 分支)。把它们算成产出+ * 的话,一个用户什么都没看到的重连会既绕过连续失败上限(这类 delta 可以让计数永远停在 1/5),+ * 又在历史里错误显示「已重新连接」(greptile / codex 连报四轮 P1/P2)。* thinking / status / 我们自己补发的续跑指令同理都不算。*+ * 「看得见」的判定收敛在 `shared/visibleText.ts` 一处,renderer 的折叠边界+ * (`makerChatStore.isSubstantiveChatRow`)用的是**同一个函数** —— 两边各写一份就会分叉,+ * 而分叉的表现是静默的(main 认为还在同一段、UI 却把卡片拆成多行)。+ *+ * 真实产出里夹着的空白 delta 不受影响:紧随其后的那个 delta 带可见字符,那时才记产出,+ * 而"下一次失败之前有没有产出"这个判据不在乎它发生在哪一拍。+ ** 抽成纯函数是为了能被单测锁住 —— 它原本长在 register 的巨型 wiring 里,测不到。*/export function isSubstantiveProgressEvent(event: {@@ -146,8 +158,7 @@ export function isSubstantiveProgressEvent(event: {}): boolean {if (event.type === 'tool_use') return true;if (event.type !== 'text') return false;- const text = (event.data as { text?: unknown } | null | undefined)?.text;- return typeof text === 'string' && text.length > 0;+ return hasUserVisibleText((event.data as { text?: unknown } | null | undefined)?.text);}/**diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreAutoResumeCard.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreAutoResumeCard.test.tsindex 744ab0f988..809ebad582 100644--- a/apps/desktop/src/renderer/__tests__/makerChatStoreAutoResumeCard.test.ts+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreAutoResumeCard.test.ts@@ -362,6 +362,37 @@ describe('同一次中断事件的多次重连折叠成一行', () => {expect(cards.map((m) => m.clientId)).toEqual(['r1', 'r2']);});+ it('中间只有 thinking 时仍折叠(main 也把它算在同一段里)', async () => {+ // 折叠边界必须与 main 的产出判据同语义(isSubstantiveProgressEvent:实义文本 / 工具调用,+ // thinking 不算)。不一致的话:开了 reasoning 的重连只吐 thinking 就再次失败时,main 记+ // attempt 2/5 属同一段,UI 却把卡片拆成两行(codex P1)。空白文本同理。+ vi.mocked(messageService.list).mockResolvedValueOnce([+ resumeRow('r1', 1, 'failed'),+ serverMessage({+ clientId: 't1',+ role: 'thinking',+ content: '在想怎么接着做',+ createdAt: '2026-06-12T00:00:05.000Z',+ }),+ serverMessage({+ clientId: 'ws1',+ role: 'assistant',+ // 纯空白 + 零宽字符:两者都是"用户看不见",都不该成为折叠边界(greptile P2)。+ content: ' \n \u200B\uFEFF ',+ createdAt: '2026-06-12T00:00:06.000Z',+ }),+ resumeRow('r2', 2, undefined, '2026-06-12T00:00:1'),+ ]);+ makerChatStore.ensureInitialMessages(SID);+ await flush();+ await flush();++ const cards = makerChatStore+ .getSnapshot(SID)+ .messages.filter((m) => m.systemCardType === 'auto-resume');+ expect(cards.map((m) => m.clientId), '同一次中断只该留最新那一行').toEqual(['r2']);+ });+it('进行中的 ephemeral 行会盖掉它前面那条已落库的重连行', async () => {vi.mocked(messageService.list).mockResolvedValueOnce([resumeRow('r1', 1, 'failed')]);makerChatStore.ensureInitialMessages(SID);diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.tsindex c2dfd3085d..b2385dcfe6 100644--- a/apps/desktop/src/renderer/lib/makerChatStore.ts+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts@@ -38,6 +38,7 @@ import type {AgentInputSessionRef,AgentInputReference,} from '../../shared/agentInputQueue';+import { hasUserVisibleText } from '../../shared/visibleText';import {deriveAutoTitleSeed,getAgentFacingText,@@ -9071,13 +9072,29 @@ type RemoteRowsOrder = 'newest-first' | 'oldest-first';/** 自愈活动行的两种卡型(ephemeral 进行中 + 落库记录)。 */const AUTO_RESUME_CARD_TYPES = new Set(['auto-resume', 'auto-resume-pending']);-/** 这一行对用户是不是"实质内容"(用来切分自愈事件的边界)。 */+/**+ * 这一行对用户是不是"实质内容"(用来切分自愈事件的边界)。+ *+ * **必须与 main 的产出判据同语义**(`interruptedTurnAutoResume.isSubstantiveProgressEvent`:+ * 用户看得见的文本、或工具调用)。不一致的后果是两边对"还在不在同一次中断里"的判断分叉:+ * 开了 reasoning 的重连只吐 thinking 就再次失败时,main 仍把它算在同一段(attempt 2/5),+ * 而这里若把 thinking 行当成边界,卡片就不再折叠——一次中断在流里堆出多行,正是「连续重连+ * 原地更新同一行」要避免的(codex P1)。+ *+ * 「看得见」不自己判:与 main 共用 `shared/visibleText.ts` 的 `hasUserVisibleText`(纯空白与+ * 零宽字符都算看不见)。各写一份必然漂移 —— `trim()` 挡不住 U+200B 这类零宽字符就是实例+ * (greptile P2)。+ */function isSubstantiveChatRow(message: ChatMessage): boolean {// 其它系统卡(compact / goal 分隔条等)算边界:它们之后的重连属于新一段。if (message.systemCardType && !AUTO_RESUME_CARD_TYPES.has(message.systemCardType)) return true;// 隐藏的合成指令行(含我们自己补发的续跑指令)不算内容。if (message.isSyntheticTrigger === true) return false;- return typeof message.content === 'string' ? message.content.length > 0 : message.content != null;+ // thinking 不算产出(与 main 一致):用户没看到任何回答,那次重连也就没成功。+ if (message.role === 'thinking') return false;+ return typeof message.content === 'string'+ ? hasUserVisibleText(message.content)+ : message.content != null;}/**diff --git a/apps/desktop/src/shared/__tests__/visibleText.test.ts b/apps/desktop/src/shared/__tests__/visibleText.test.tsnew file mode 100644index 0000000000..12b079d887Binary files /dev/null and b/apps/desktop/src/shared/__tests__/visibleText.test.ts differdiff --git a/apps/desktop/src/shared/visibleText.ts b/apps/desktop/src/shared/visibleText.tsnew file mode 100644index 0000000000..5079134bb3--- /dev/null+++ b/apps/desktop/src/shared/visibleText.ts@@ -0,0 +1,28 @@+/**+ * 「这段文本对用户来说看得见吗」—— main 与 renderer **共用**的一份判定。+ *+ * 为什么必须共用而不是各写一份:中断自愈用它回答「模型这次到底有没有产出」,main 据此决定+ * 连续失败计数要不要归零(也就是 5 次上限拦不拦得住),renderer 据此决定聊天流里的重连行要+ * 不要折叠成一行。两边分叉的后果不会抛异常,只会表现成「自动重连绕过上限」或「一次中断堆+ * 出多行」这类静默不一致——#1071 的 review 里这一族连着被抓出好几轮,所以判定收敛成一处。+ *+ * 判据:去掉所有**不可见**字符后还剩东西,就算用户看得见。不可见包括三类:+ * - 空白(`\s`:空格 / 制表 / 换行 / NBSP / U+2000–U+200A / U+FEFF …);+ * - Unicode 格式字符(`\p{Cf}`:U+200B 零宽空格、U+200C/200D 零宽连接符、U+2060 word+ * joiner、U+00AD 软连字符 …)。**`trim()` 挡不住这一类**——它只认 `\s`,而零宽字符不在+ * 其中,于是「只有零宽字符的文本」会被误判成产出(greptile P2);+ * - 控制字符(`\p{Cc}`:C0/C1,如裸 `\x00`、`\x1b`)。+ *+ * 刻意**不**排除的:盲文空白 U+2800、Hangul filler U+3164 这类"看着空但确实是字形"的字符。+ * 本判定服务的是「上游流式产出里夹了不可见填充」这个真实场景,不是对抗刻意构造的输入;把+ * 合法字形也算成不可见反而会把真实产出误判成空。+ */++/** 所有不可见字符:空白 + Unicode 格式字符 + 控制字符。 */+const INVISIBLE_PATTERN = /[\s\p{Cf}\p{Cc}]/gu;++/** 这段文本去掉不可见字符后还有内容吗(非字符串一律 false)。 */+export function hasUserVisibleText(text: unknown): boolean {+ if (typeof text !== 'string' || text.length === 0) return false;+ return text.replace(INVISIBLE_PATTERN, '').length > 0;+}