Skip to content

The agent that grows with you

fix(desktop): arm the turn progress timer at submit instead of waiting for message.start

Tekniumf70277bc704e
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 50ce3d7ac31..08acaec29ef 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
@@ -674,6 +674,16 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
triggerHaptic('streamStart')
}
+ // Submit→accept latency: seedOptimistic armed the clock at Enter; this
+ // event is the backend accepting the turn. Debug-only visibility into
+ // how long the arm actually took (the "no progress box for seconds"
+ // complaint) — reads the pre-update cache, costs nothing when clean.
+ const seededAt = sessionStateByRuntimeIdRef.current.get(sessionId)?.turnStartedAt
+
+ if (typeof seededAt === 'number') {
+ console.debug('[turn-accept-latency]', { sessionId, ms: Date.now() - seededAt })
+ }
+
updateSessionState(sessionId, state => {
// If the user clicked Stop (cancelRun set interrupted=true), don't
// let a stale message.start from a chained turn (goal follow-up,
@@ -693,12 +703,21 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
sawAssistantPayload: false,
interrupted: false,
interimBoundaryPending: false,
- turnStartedAt: Date.now()
+ // 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)
+ // have no seed and arm here.
+ turnStartedAt: state.turnStartedAt ?? Date.now()
}
})
if (isActiveEvent) {
- setTurnStartedAt(Date.now())
+ // Belt-and-suspenders mirror of the ACTIVE session's per-session
+ // clock (the load-bearing mirror is the view-sync flush in
+ // use-session-state-cache). Mirror the seeded value, not Date.now():
+ // resetting to accept-time here would visibly snap the timer back
+ // after the submit-time seed above already started it.
+ setTurnStartedAt(sessionStateByRuntimeIdRef.current.get(sessionId)?.turnStartedAt ?? Date.now())
}
} else if (event.type === 'message.delta') {
if (sessionId) {
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 12ae168265b..1c14665d444 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -106,6 +106,7 @@ function Harness({
runtimeIdByStoredSessionIdRef: runtimeIdByStoredSessionIdRefProp,
seedMessages,
seedStreamId,
+ seedTurnStartedAt,
selectedStoredSessionIdRef: selectedStoredSessionIdRefProp,
storedSessionId,
activeSessionId,
@@ -130,6 +131,7 @@ function Harness({
runtimeIdByStoredSessionIdRef?: MutableRefObject<Map<string, string>>
seedMessages?: unknown[]
seedStreamId?: null | string
+ seedTurnStartedAt?: null | number
selectedStoredSessionIdRef?: MutableRefObject<string | null>
storedSessionId?: null | string
activeSessionId?: null | string
@@ -163,6 +165,7 @@ function Harness({
awaitingResponse: false,
interrupted: true,
streamId: seedStreamId ?? null,
+ turnStartedAt: seedTurnStartedAt ?? null,
interimBoundaryPending: false
} as never)
@@ -1720,6 +1723,59 @@ describe('usePromptActions submit / queue drain semantics', () => {
)
})
+ it('arms turnStartedAt at submit time instead of waiting for message.start', async () => {
+ const seeds: Record<string, unknown>[] = []
+ const requestGateway = vi.fn(async () => ({}) as never)
+
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness
+ onReady={h => (handle = h)}
+ onSeedState={s => seeds.push(s)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ />
+ )
+
+ const before = Date.now()
+ await handle!.submitText('arm the clock now')
+
+ // The optimistic seed carries the clock — the progress box's timer must
+ // not be hostage to the submit→gateway-accept round trip (which can take
+ // seconds under load). message.start later keeps this value (?? guard).
+ expect(seeds.length).toBeGreaterThan(0)
+ const armed = seeds[0].turnStartedAt
+
+ expect(typeof armed).toBe('number')
+ expect(armed as number).toBeGreaterThanOrEqual(before)
+ expect(armed as number).toBeLessThanOrEqual(Date.now())
+ })
+
+ it('keeps a live turn clock when a second seed races it (?? guard)', async () => {
+ const seeds: Record<string, unknown>[] = []
+ const requestGateway = vi.fn(async () => ({}) as never)
+ const preArmed = Date.now() - 12_345
+
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness
+ onReady={h => (handle = h)}
+ onSeedState={s => seeds.push(s)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ seedTurnStartedAt={preArmed}
+ />
+ )
+
+ // Submit into state that already carries a live clock (queued send racing
+ // a running turn): the seed must preserve it, not restart the visible
+ // elapsed time.
+ await handle!.submitText('send racing a live clock')
+
+ expect(seeds.length).toBeGreaterThan(0)
+ expect(seeds[0].turnStartedAt).toBe(preArmed)
+ })
+
it('flags prompt.submit with interrupted:true after a voice-playback barge', async () => {
const { markVoicePlaybackInterrupted } = await import('@/lib/voice-playback')
const requestGateway = vi.fn(async () => ({}) as never)
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index bbc11910b84..67f13044c4f 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -371,7 +371,15 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// Fresh submit = new turn — clear any leftover interrupt flag, else
// mutateStream/completeAssistantMessage drop every delta of this turn
// (what made drained-after-interrupt sends go silent).
- interrupted: false
+ interrupted: false,
+ // Arm the turn clock at send, not at the backend's message.start —
+ // the round trip (submit RPC → gateway accept → WS event) can take
+ // seconds under load, and the honest latency clock starts when the
+ // user hit Enter. message.start keeps this seed (?? Date.now()),
+ // and the settle paths clear it as before. `??` on our side too:
+ // a queued send that loses the settle race against a still-live
+ // turn must not restart that turn's clock.
+ turnStartedAt: state.turnStartedAt ?? Date.now()
}),
targetStoredSessionId
)
@@ -405,7 +413,11 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
messages: state.messages.filter(m => m.id !== optimisticId),
busy: false,
awaitingResponse: false,
- pendingBranchGroup: null
+ pendingBranchGroup: null,
+ // Retire the submit-time clock seed with the turn it belonged to —
+ // only when no live stream claimed it (a queued send aborting must
+ // not wipe a running turn's clock).
+ turnStartedAt: state.streamId || state.sawAssistantPayload ? state.turnStartedAt : null
}),
targetStoredSessionId
)
@@ -762,7 +774,9 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
busy: false,
awaitingResponse: false,
pendingBranchGroup: null,
- sawAssistantPayload: true
+ sawAssistantPayload: true,
+ // The failed submit's clock seed dies with the turn it never got.
+ turnStartedAt: null
}),
targetStoredSessionId
)