Skip to content

Commit c5d4d7f

Browse files
committed
Don't clear chat blocks on error!
1 parent 635eb3a commit c5d4d7f

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

cli/src/utils/__tests__/message-updater.test.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ describe('createMessageUpdater', () => {
5353
expect((state[0].metadata as any).runState).toEqual({ id: 'run-1' })
5454
})
5555

56-
test('setError clears blocks and marks complete', () => {
57-
let state = [...baseMessages]
56+
test('setError preserves blocks and marks complete', () => {
57+
let state: ChatMessage[] = [
58+
{
59+
id: 'ai-1',
60+
variant: 'ai',
61+
content: '',
62+
blocks: [{ type: 'text', content: 'existing block' }],
63+
timestamp: 'now',
64+
},
65+
]
5866

5967
const updater = createMessageUpdater('ai-1', (fn) => {
6068
state = fn(state)
@@ -64,7 +72,8 @@ describe('createMessageUpdater', () => {
6472

6573
expect(state[0].content).toBe('boom')
6674
expect(state[0].isComplete).toBe(true)
67-
expect(state[0].blocks).toBeUndefined()
75+
expect(state[0].blocks).toHaveLength(1)
76+
expect((state[0].blocks![0] as any).content).toBe('existing block')
6877
})
6978
})
7079

@@ -155,8 +164,16 @@ describe('createBatchedMessageUpdater', () => {
155164
expect(state[0].credits).toBe(0.5)
156165
})
157166

158-
test('setError discards pending updates and applies error', () => {
159-
let state = [...baseMessages]
167+
test('setError discards pending updates but preserves existing blocks', () => {
168+
let state: ChatMessage[] = [
169+
{
170+
id: 'ai-1',
171+
variant: 'ai',
172+
content: '',
173+
blocks: [{ type: 'text', content: 'existing block' }],
174+
timestamp: 'now',
175+
},
176+
]
160177
let setMessagesCallCount = 0
161178

162179
const updater = createBatchedMessageUpdater(
@@ -169,15 +186,17 @@ describe('createBatchedMessageUpdater', () => {
169186
)
170187

171188
// Queue an update (will be discarded by error)
172-
updater.addBlock({ type: 'text', content: 'will be cleared' })
189+
updater.addBlock({ type: 'text', content: 'pending block' })
173190

174191
updater.setError('something went wrong')
175192

176193
// Should have 1 call: setError (pending updates discarded, not flushed)
177194
expect(setMessagesCallCount).toBe(1)
178195
expect(state[0].content).toBe('something went wrong')
179196
expect(state[0].isComplete).toBe(true)
180-
expect(state[0].blocks).toBeUndefined()
197+
// Existing blocks are preserved, but pending block was discarded
198+
expect(state[0].blocks).toHaveLength(1)
199+
expect((state[0].blocks![0] as any).content).toBe('existing block')
181200
})
182201

183202
test('updates after dispose are applied immediately', () => {

cli/src/utils/message-updater.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export const createMessageUpdater = (
7777
const nextMessage: ChatMessage = {
7878
...msg,
7979
content: message,
80-
blocks: undefined,
8180
isComplete: true,
8281
}
8382
return nextMessage
@@ -192,14 +191,13 @@ export const createBatchedMessageUpdater = (
192191
pendingUpdaters.length = 0
193192
dispose()
194193

195-
// Apply error immediately, clearing blocks
194+
// Apply error immediately, preserving blocks for debugging context
196195
setMessages((prev) =>
197196
prev.map((msg) => {
198197
if (msg.id !== aiMessageId) return msg
199198
return {
200199
...msg,
201200
content: message,
202-
blocks: undefined,
203201
isComplete: true,
204202
}
205203
}),

0 commit comments

Comments
 (0)