Skip to content

The agent that grows with you

fix(desktop): discriminate backend-confirmed turns with turnLive so the settle gate survives submit-time clock seeding

Teknium4b583e4476c8
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index 0be9bacb95d..2239ea527d2 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -255,6 +255,7 @@ export function rehydrateLiveSessionStatuses(
needsInput: false,
streamId: null,
turnStartedAt: null,
+ turnLive: false,
// The turn ended without its completion events reaching us — a lost
// `tool.complete` would otherwise leave a spinning tool row in an
// idle session. Seal open tool parts the same way the settle path
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
index c65c35cff10..cd5567fd0d9 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
@@ -602,6 +602,10 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
return {
...state,
busy,
+ // running=true from the backend is turn-live proof, same as
+ // message.start (e.g. resuming an already-running session
+ // that never replays its start event).
+ turnLive: true,
turnStartedAt: state.turnStartedAt ?? Date.now()
}
}
@@ -611,11 +615,13 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
// heartbeat that lands in the gap before the turn spins up is a
// pre-start report, not a finished turn — settling on it would
// drop the spinner and re-open the send guard mid-flight.
- // turnStartedAt is stamped only once the backend reports the turn
+ // turnLive is stamped only once the backend reports the turn
// live (message.start, the running=true edge, or a resumed
- // in-flight turn) and is cleared by every settle, so a null value
+ // in-flight turn) and is cleared by every settle, so false
// here is exactly "no turn has been reported running yet".
- if (state.awaitingResponse && !state.sawAssistantPayload && !state.turnStartedAt) {
+ // (turnStartedAt can't discriminate — it is optimistically
+ // seeded at submit so the visible timer starts at Enter.)
+ if (state.awaitingResponse && !state.sawAssistantPayload && !state.turnLive) {
return state
}
@@ -646,7 +652,8 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
messages: finalizeInterruptedMessages(state.messages, state.streamId, occurredAt),
pendingBranchGroup: null,
streamId: null,
- turnStartedAt: null
+ turnStartedAt: null,
+ turnLive: false
}
},
payload?.stored_session_id || undefined
@@ -756,6 +763,9 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
sawAssistantPayload: false,
interrupted: false,
interimBoundaryPending: false,
+ // Backend accepted the turn — the no-payload settle gate below may
+ // now treat a running=false heartbeat as a real turn end.
+ turnLive: true,
// Keep the submit-time seed (submit.ts seedOptimistic) — resetting
// here would hide the submit→accept round trip from the timer.
// Backend-originated turns (queue drain elsewhere, goal follow-up)
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
index c417ab7c3ed..ad9ac2c49cd 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
@@ -579,7 +579,8 @@ export function useMessageStream({
needsInput: false,
pendingBranchGroup: null,
streamId: null,
- turnStartedAt: null
+ turnStartedAt: null,
+ turnLive: false
}
}
@@ -734,7 +735,8 @@ export function useMessageStream({
busy: false,
needsInput: false,
interimBoundaryPending: false,
- turnStartedAt: null
+ turnStartedAt: null,
+ turnLive: false
}
})
@@ -813,7 +815,8 @@ export function useMessageStream({
busy: false,
needsInput: false,
interimBoundaryPending: false,
- turnStartedAt: null
+ turnStartedAt: null,
+ turnLive: false
}
})
},
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.tsx
index bfe4305412f..0168a9d00d0 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
@@ -180,16 +180,18 @@ describe('session.info settles a turn that produced no assistant payload', () =>
it('keeps waiting when running=false lands before the turn ever started', async () => {
await mountStream()
- // submit arms busy/awaitingResponse optimistically; this heartbeat is the
- // pre-start report, not a finished turn, so the spinner must stay up and
- // the send guard must stay closed.
+ // submit arms busy/awaitingResponse optimistically — and seeds the visible
+ // turn clock (turnStartedAt) at Enter — so this heartbeat is the pre-start
+ // report, not a finished turn: the spinner must stay up and the send guard
+ // must stay closed. turnLive (backend-confirmed) is the discriminator.
act(() => {
sessionStates!.set(ACTIVE_SID, {
...createClientSessionState(),
awaitingResponse: true,
busy: true,
sawAssistantPayload: false,
- turnStartedAt: null
+ turnStartedAt: Date.now(),
+ turnLive: false
})
})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 155f7888362..333bca63e33 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -678,7 +678,8 @@ export function usePromptActions({
pendingBranchGroup: null,
needsInput: false,
interrupted: true,
- turnStartedAt: null
+ turnStartedAt: null,
+ turnLive: false
}
})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index 8c408e5a87b..02aecbd86b4 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -1072,6 +1072,7 @@ describe('resumeSession failure recovery', () => {
storedSessionId: 'stored-1',
streamId: null,
turnStartedAt: null,
+ turnLive: false,
usage: null,
yolo: false
}
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index 66b0a54e708..58c14205c55 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -920,6 +920,9 @@ export function useSessionActions({
messages: activatedMessages,
busy: running,
awaitingResponse: running,
+ // Resumed onto an already-running turn — that IS backend
+ // proof the turn is live (no message.start will replay).
+ turnLive: state.turnLive || running,
needsInput: pendingApproval || pendingClarify || state.needsInput,
// Adopting someone else's turn: we'll stream its reply
// without ever having received its prompt, so the settle
@@ -1200,6 +1203,8 @@ export function useSessionActions({
messages: messagesForView,
busy: resumedRunning,
awaitingResponse: resumedRunning && !recoveredInFlightTail,
+ // Backend reported this turn running at resume time — live proof.
+ turnLive: state.turnLive || resumedRunning,
needsInput: pendingApproval || pendingClarify || state.needsInput,
adoptedRunningTurn: state.adoptedRunningTurn || resumedRunning,
...(inFlightRecovery.applied
diff --git a/apps/desktop/src/app/types.ts b/apps/desktop/src/app/types.ts
index fe9398d7bbf..d5afb3e4795 100644
--- a/apps/desktop/src/app/types.ts
+++ b/apps/desktop/src/app/types.ts
@@ -203,8 +203,15 @@ export interface ClientSessionState {
/** Epoch ms the current turn started, or null when idle. Per-session so a
* background turn's elapsed timer keeps counting while another session is
* focused, and switching sessions doesn't zero a still-running turn's clock.
+ * Seeded optimistically at submit (before the backend accepts), so it is a
+ * CLOCK, not proof the turn is live — gate on turnLive for that.
* The global $turnStartedAt mirrors whichever session is currently viewed. */
turnStartedAt: number | null
+ /** The backend has confirmed this turn is running (message.start, a
+ * running=true session.info edge, or resuming onto an in-flight turn).
+ * False while a submit is only optimistically armed — the discriminator the
+ * no-payload settle gate needs now that turnStartedAt is seeded at send. */
+ turnLive: boolean
/** Cumulative token usage, updated per completed turn. Per-session twin of
* the primary-only $currentUsage — the statusbar reads it for a focused
* tile's context count. Null until the first turn reports. */
diff --git a/apps/desktop/src/lib/chat-runtime.ts b/apps/desktop/src/lib/chat-runtime.ts
index c00dd185ead..e921c873189 100644
--- a/apps/desktop/src/lib/chat-runtime.ts
+++ b/apps/desktop/src/lib/chat-runtime.ts
@@ -43,6 +43,7 @@ export function createClientSessionState(
interimBoundaryPending: false,
needsInput: false,
turnStartedAt: null,
+ turnLive: false,
usage: null
}
}