Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/ui/src/__tests__/live-turn-projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,59 @@ describe('reconcileTerminalLiveTurn', () => {
]), textTurn);
});

it('terminalizes live text when persisted history proves a missed terminal event', () => {
const live: LiveTurnProjection = {
turnId: 'turn-1',
phase: 'streamed',
providerRetry: {
type: 'provider_retry',
id: 'retry-1',
turnId: 'turn-1',
ts: 2,
phase: 'started',
attempt: 2,
maxAttempts: 3,
reason: 'network',
},
steps: [{
stepId: 'assistant-1',
thinking: { text: 'reasoning', truncated: false, complete: false },
text: { text: 'answer', truncated: false, complete: false },
tools: [],
}],
};

assert.deepEqual(reconcileTerminalLiveTurn(live, [
{
type: 'assistant',
id: 'assistant-1',
turnId: 'turn-1',
ts: 3,
text: 'answer',
thinking: { text: 'reasoning' },
modelId: 'm',
},
{
type: 'turn_state',
id: 'state-1',
turnId: 'turn-1',
ts: 4,
status: 'completed',
partialOutputRetained: false,
},
]), {
turnId: 'turn-1',
phase: 'streamed',
terminal: true,
steps: [{
stepId: 'assistant-1',
thinking: { text: 'reasoning', truncated: false, complete: true },
text: { text: 'answer', truncated: false, complete: true },
tools: [],
}],
});
});

it('settles a persisted thinking-only step whose text slot is empty', () => {
const thinkingOnly: LiveTurnProjection = {
turnId: 'turn-1',
Expand Down
29 changes: 19 additions & 10 deletions packages/ui/src/live-turn-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,24 @@ export function reconcileTerminalLiveTurn(
const transcriptReachedTerminal = turnMessages.some(
(message) => message.type === 'turn_state' && message.status !== 'running',
);
let projection = current;
if (transcriptReachedTerminal && current.terminal !== true) {
const { providerRetry: _providerRetry, ...withoutRetry } = confirmed(current);
projection = {
...withoutRetry,
terminal: true,
steps: terminalizeLiveSteps(current.steps),
};
}
if (
current.terminal === true
&& liveSteeringMessages(current).length > 0
projection.terminal === true
&& liveSteeringMessages(projection).length > 0
&& !transcriptReachedTerminal
) return current;
) return projection;
const assistantIds = new Set(turnMessages.flatMap((message) => message.type === 'assistant' ? [message.id] : []));
const toolCallIds = new Set(turnMessages.flatMap((message) => message.type === 'tool_call' ? [message.id] : []));
const toolResultIds = new Set(turnMessages.flatMap((message) => message.type === 'tool_result' ? [message.toolUseId] : []));
let steps = current.steps.filter((step) => {
let steps = projection.steps.filter((step) => {
if (step.text?.text.length) return true;
if (step.thinking && !assistantIds.has(step.stepId)) return true;
const toolsCovered = step.tools.every((tool) => {
Expand All @@ -633,19 +642,19 @@ export function reconcileTerminalLiveTurn(
// Once persisted turn_state records the terminal handoff, the transcript is
// authoritative for accepted steering; retaining the live copy would leave
// a duplicate or a nacked ghost instruction on screen.
const steeringSettled = current.terminal === true
const steeringSettled = projection.terminal === true
&& transcriptReachedTerminal
&& liveSteeringMessages(current).length > 0;
&& liveSteeringMessages(projection).length > 0;
if (steeringSettled) {
steps = steps.map((step) => {
if (!step.leadingSteering) return step;
const { leadingSteering: _leadingSteering, ...withoutSteering } = step;
return withoutSteering;
});
}
if (steps.length === current.steps.length && !steeringSettled) return current;
if (steps.length === 0 && current.terminal) return undefined;
if (!steeringSettled) return { ...current, steps };
const { pendingSteering: _pendingSteering, ...withoutSteering } = current;
if (steps.length === projection.steps.length && !steeringSettled) return projection;
if (steps.length === 0 && projection.terminal) return undefined;
if (!steeringSettled) return { ...projection, steps };
const { pendingSteering: _pendingSteering, ...withoutSteering } = projection;
return { ...withoutSteering, steps };
}