The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): recover incomplete transcript turns
szzhoujiaruif1e01f4daf7e
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.tsindex 65e0e2f50b1..40143a232d6 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/session-info.ts@@ -272,11 +272,10 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {// mutates the per-runtime cache entry, and syncSessionStateToView// guards the view publish to the active session, so this is safe.if (runningChanged && sessionId) {- // Set when THIS event released a turn that ended without ever- // producing an assistant payload, so the catch-up side effects below- // run on that edge only. The updater is invoked exactly once,+ // Set when THIS event releases a confirmed live turn whose terminal+ // message never arrived. The updater is invoked exactly once,// synchronously, by updateSessionState.- let recoveredWithoutPayload = false+ let recoveredIncompleteTurn = falseconst nextState = updateSessionState(sessionId,@@ -348,15 +347,14 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {}// Past that gate the turn DID start and the backend now reports it- // finished. When no assistant payload ever arrived (gateway crash- // mid-stream, provider error before the first delta, agent-build- // failure) message.complete never fires, so this is the only event- // that can release the session. Bailing here instead left+ // finished. When message.complete never fires (gateway crash+ // mid-stream, provider error, reconnect gap), this is the only+ // event that can release the session. Bailing here instead left// awaitingResponse/busy latched until app restart (#46517): the// per-session busy flag is authoritative for isTargetSessionBusy,// so submitPrompt and the slash dispatcher silently returned false// and the session accepted no further input.- recoveredWithoutPayload = state.awaitingResponse && !state.sawAssistantPayload+ recoveredIncompleteTurn = state.turnLivereturn {...state,@@ -381,13 +379,13 @@ export function handleSessionInfoEvent(ctx: GatewayEventContext): boolean {payload?.stored_session_id || undefined)- if (recoveredWithoutPayload) {+ if (recoveredIncompleteTurn) {// Stays unscoped, like the settle above: a background session's// sidebar row has to drop its working dot without the user opening- // it. This fires on the recovery edge only — once awaitingResponse- // is false the `state.busy === busy` guard above short-circuits- // every later heartbeat — so it costs one coalesced refresh per- // broken turn, not one per tick.+ // it. This fires on the recovery edge only — once turnLive is false+ // the `state.busy === busy` guard above short-circuits every later+ // heartbeat — so it costs one coalesced refresh per broken turn,+ // not one per tick.scheduleSessionsRefresh()// The transcript catch-up IS scoped. The stream died, but the turndiff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.tsindex 41a65a83770..01738aff650 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts@@ -744,7 +744,13 @@ export function useMessageStream({shouldHydrate =!completionError &&!hasInlineError &&- !unresolvedUserTail &&+ // A visible user message with no reply after the terminal frame+ // means this window never rendered the turn's output. When the+ // frame also carries no text, the reply only exists in stored+ // history — hydrate to catch up instead of leaving the transcript+ // blank until restart (#88036). A non-empty frame still settles+ // locally, so the user-tail guard keeps applying there.+ (!unresolvedUserTail || !finalText) &&!(localVisibleText && !finalText) &&(state.adoptedRunningTurn || !state.sawAssistantPayload || !finalText)diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsxindex 1f84dc0e7f6..82f806fc074 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-message-stream/interim-sealing.test.tsx@@ -2,7 +2,8 @@ import { act, cleanup } from '@testing-library/react'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import type { ClientSessionState } from '@/app/types'-import { chatMessageText } from '@/lib/chat-messages'+import { chatMessageText, textPart } from '@/lib/chat-messages'+import { createClientSessionState } from '@/lib/chat-runtime'import { clearSessionTodos } from '@/store/todos'import type { RpcEvent } from '@/types/hermes'@@ -79,6 +80,31 @@ describe('useMessageStream interim text sealing', () => {expect(texts).toContain('All checks passed.')})+ it('hydrates an empty completion when the live transcript still ends with the user message', async () => {+ // #88036: the terminal frame arrived empty while this window never+ // rendered any assistant output — the reply exists only in stored+ // history, so the settle must hydrate instead of leaving the+ // transcript ending on the user's message until restart.+ const hydrateFromStoredSession = vi.fn<() => Promise<void>>(async () => undefined)++ stream = renderMessageStream(SID, {+ hydrateFromStoredSession,+ states: new Map([+ [+ SID,+ createClientSessionState('stored-session-1', [+ { id: 'user-1', parts: [textPart('finish the task')], role: 'user' }+ ])+ ]+ ])+ })+ await start()++ await complete('')++ expect(hydrateFromStoredSession).toHaveBeenCalledWith(3, 'stored-session-1', SID)+ })+it('marks sealed interim bubbles interim and leaves the final reply unmarked', async () => {mountStream()await start()diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/session-info-side-effects.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/session-info-side-effects.test.tsxindex 3e0b6f24f34..dc76c94a936 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/session-info-side-effects.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-message-stream/session-info-side-effects.test.tsx@@ -120,7 +120,7 @@ describe('session.info model-options invalidation gating', () => {})})-describe('session.info settles a turn that produced no assistant payload', () => {+describe('session.info settles an incomplete live turn', () => {// #46517: a turn that ends without ever emitting an assistant payload (gateway// crash mid-stream, provider error before the first delta, agent-build// failure) never reaches message.complete, so session.info running=false is@@ -262,6 +262,25 @@ describe('session.info settles a turn that produced no assistant payload', () =>expect(hydrateFromStoredSession).toHaveBeenCalledTimes(1)expect(refreshSessions).toHaveBeenCalledTimes(1)})++ it('hydrates a live turn that settles after an interim without message.complete', async () => {+ mountStream()++ startTurn(ACTIVE_SID)+ act(() =>+ stream.handleEvent({+ payload: { already_streamed: true, text: 'Now checking the details before answering.' },+ session_id: ACTIVE_SID,+ type: 'message.interim'+ })+ )++ expect(sessionStates!.get(ACTIVE_SID)?.sawAssistantPayload).toBe(true)++ sessionInfo(ACTIVE_SID, { running: false })++ expect(hydrateFromStoredSession).toHaveBeenCalledWith(3, null, ACTIVE_SID)+ })})describe('empty message.complete after streamed text (#95514)', () => {