From cb1f961ea91ffcb341f36663544c7657afb6fc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Andr=C3=A9?= Date: Fri, 21 Aug 2026 12:00:01 +0200 Subject: [PATCH] fix(chat): handle truncated diff headers --- .../chat/message/parts/ToolPart.test.ts | 25 ++++++++++++++++++- .../components/chat/message/toolRenderers.tsx | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/components/chat/message/parts/ToolPart.test.ts b/packages/ui/src/components/chat/message/parts/ToolPart.test.ts index 2faf34c3..d0697c59 100644 --- a/packages/ui/src/components/chat/message/parts/ToolPart.test.ts +++ b/packages/ui/src/components/chat/message/parts/ToolPart.test.ts @@ -2,7 +2,7 @@ import { describe, expect, test } from 'bun:test'; import { getStreamingOutputAppend, getToolOutput, renderTerminalOutput } from './toolOutput'; import { readTaskTagSessionIdFromOutput } from './taskSessionIdParser'; -import { tryParseJsonOutput } from '../toolRenderers'; +import { parseDiffToUnified, tryParseJsonOutput } from '../toolRenderers'; import { getStreamingThrottleText } from '../../hooks/useStreamingTextThrottle'; import { getToolDescriptionFallback } from './toolRenderUtils'; @@ -42,6 +42,29 @@ describe('getToolOutput', () => { }); }); +describe('parseDiffToUnified', () => { + test('handles a streamed diff with a bare Index header', () => { + expect(parseDiffToUnified('Index:')).toEqual([]); + expect(parseDiffToUnified('Index:\n@@ -1,1 +1,1 @@\n-old\n+new')).toEqual([ + { + file: 'file', + oldStart: 1, + newStart: 1, + lines: [ + { type: 'removed', lineNumber: 1, content: 'old' }, + { type: 'added', lineNumber: 1, content: 'new' }, + ], + }, + ]); + }); + + test('preserves spaces when extracting the indexed filename', () => { + const [hunk] = parseDiffToUnified('Index: src/my file.ts\n@@ -1,1 +1,1 @@\n-old\n+new'); + + expect(hunk?.file).toBe('my file.ts'); + }); +}); + describe('renderTerminalOutput', () => { test('renders carriage-return progress updates as their latest value', () => { expect(renderTerminalOutput('Downloading 10%\r\u001B[2KDownloading 90%')).toBe('Downloading 90%'); diff --git a/packages/ui/src/components/chat/message/toolRenderers.tsx b/packages/ui/src/components/chat/message/toolRenderers.tsx index 3c4dd773..3a5cd403 100644 --- a/packages/ui/src/components/chat/message/toolRenderers.tsx +++ b/packages/ui/src/components/chat/message/toolRenderers.tsx @@ -575,7 +575,7 @@ export const parseDiffToUnified = (diffText: string): UnifiedDiffHunk[] => { if (line.startsWith('Index:') || line.startsWith('===') || line.startsWith('---') || line.startsWith('+++')) { if (line.startsWith('Index:')) { - currentFile = line.split(' ')[1].split('/').pop() || 'file'; + currentFile = line.slice('Index:'.length).trim().split('/').pop() || 'file'; } i++; continue;