fix(worker): don't double-append streamed answer when completed event carries content

This commit is contained in:
Gabriel Brown
2026-07-11 11:47:11 -04:00
parent 2311679802
commit 432e3b501e
3 changed files with 52 additions and 3 deletions
@@ -1,6 +1,36 @@
import { describe, expect, test } from 'vitest';
import { resolveTurnOutcome } from '../../src/runtime/turn-outcome';
import {
resolveTurnOutcome,
shouldAppendCompletedContent,
} from '../../src/runtime/turn-outcome';
describe('shouldAppendCompletedContent', () => {
test('does not re-append when deltas already streamed the answer', () => {
// Claude/OpenCode emit the final answer as assistant_delta AND as the
// content-bearing assistant_completed event. Folding the completed
// content in again would produce "<answer><answer>".
expect(shouldAppendCompletedContent('the answer', 'the answer')).toBe(
false,
);
});
test('appends completed content when nothing has streamed yet', () => {
expect(shouldAppendCompletedContent('', 'the final result')).toBe(true);
});
test('treats whitespace-only streamed text as empty', () => {
expect(shouldAppendCompletedContent(' \n', 'the final result')).toBe(
true,
);
});
test('does not append when the completed event carries no content', () => {
expect(shouldAppendCompletedContent('the answer', undefined)).toBe(false);
expect(shouldAppendCompletedContent('', undefined)).toBe(false);
expect(shouldAppendCompletedContent('', '')).toBe(false);
});
});
describe('resolveTurnOutcome', () => {
test('partial streamed text + error is surfaced as failed and text preserved', () => {