Skip to content

The agent that grows with you

fix(desktop): sync runtime timer from session cache

Jakub Wolniewiczcd0601a0df17
diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
index 7ba2d6ad1af..70c623f5cde 100644
--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
@@ -13,6 +13,7 @@ import {
$currentReasoningEffort,
$currentServiceTier,
$messages,
+ $sessionStartedAt,
$turnStartedAt,
setActiveSessionId,
setActiveSessionStoredIdRotation,
@@ -23,6 +24,7 @@ import {
setCurrentServiceTier,
setSelectedStoredSessionId,
setSessions,
+ setSessionStartedAt,
setTurnStartedAt
} from '@/store/session'
import {
@@ -220,7 +222,7 @@ function Harness({ activeSessionId, onReady, selectedStoredSessionId }: HarnessP
return null
}
-describe('useSessionStateCache — per-session turn timer', () => {
+describe('useSessionStateCache — per-session timers', () => {
beforeEach(() => {
// The view-sync flush runs on a real rAF in the browser path; in jsdom we
// want it synchronous so the global mirror is observable immediately. The
@@ -241,6 +243,7 @@ describe('useSessionStateCache — per-session turn timer', () => {
setCurrentReasoningEffort('')
setCurrentServiceTier('')
setCurrentFastMode(false)
+ setSessionStartedAt(null)
})
afterEach(() => {
@@ -252,6 +255,30 @@ describe('useSessionStateCache — per-session turn timer', () => {
setCurrentReasoningEffort('')
setCurrentServiceTier('')
setCurrentFastMode(false)
+ setSessionStartedAt(null)
+ })
+
+ it('mirrors only the focused runtime session anchor into the global timer', () => {
+ let cache!: Cache
+ render(<Harness activeSessionId="fg-runtime" onReady={c => (cache = c)} selectedStoredSessionId="fg-stored" />)
+
+ act(() => {
+ cache.updateSessionState(
+ 'bg-runtime',
+ state => ({ ...state, runtimeStartedAt: 1_700_000_000_000 }),
+ 'bg-stored'
+ )
+ })
+ expect($sessionStartedAt.get()).toBeNull()
+
+ act(() => {
+ cache.updateSessionState(
+ 'fg-runtime',
+ state => ({ ...state, runtimeStartedAt: 1_700_000_111_000 }),
+ 'fg-stored'
+ )
+ })
+ expect($sessionStartedAt.get()).toBe(1_700_000_111_000)
})
it("keeps a background session's running turn clock and never mirrors it to the view", () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts b/apps/desktop/src/app/session/hooks/use-session-state-cache.ts
index a7ed9cfafe7..348049f0f4e 100644
--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.ts
@@ -18,6 +18,7 @@ import {
setCurrentReasoningEffort,
setCurrentReasoningEffortWire,
setCurrentServiceTier,
+ setSessionStartedAt,
setTurnStartedAt,
setYoloActive
} from '@/store/session'
@@ -298,6 +299,9 @@ export function useSessionStateCache({
setBusy(pending.state.busy)
setMutableRef(busyRef, pending.state.busy)
setAwaitingResponse(pending.state.awaitingResponse)
+ // Keep the foreground duration anchored to the runtime's first renderer
+ // attachment. Background state remains cached without stealing this view.
+ setSessionStartedAt(pending.state.runtimeStartedAt)
// Mirror the focused session's per-session turn clock into the global
// atom the statusbar timer reads. Keeps a backgrounded turn's elapsed
// time intact on focus instead of zeroing it (the "timer restarts" bug).
diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
index e39e2097ad4..2a6583b3f32 100644
--- a/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
@@ -99,12 +99,6 @@ describe('statusbar workspace menu — "Open containing folder"', () => {
})
const statusbarOptions = {
- agentsOpen: false,
- chatOpen: true,
- commandCenterOpen: false,
- extraLeftItems: [],
- extraRightItems: [],
- freshDraftReady: false,
gatewayState: 'ready' as const,
inferenceStatus: null,
openAgents: () => {},
@@ -191,3 +185,32 @@ describe('statusbar session timer — focused since (#103123)', () => {
expect(item?.hidden).toBe(true)
})
})
+
+
+describe('useStatusbarItems session timer — runtime cache anchor', () => {
+ it("anchors a focused branch tile to its runtime cache instead of the parent's stored age", () => {
+ const parentRowStartedAt = 1_600_000_000
+ const branchRuntimeStartedAt = 1_800_000_000_000
+
+ $selectedStoredSessionId.set('parent-stored')
+ $sessionStartedAt.set(1_700_000_000_000)
+ $sessions.set([
+ { id: 'parent-stored', started_at: parentRowStartedAt },
+ { id: 'branch-stored', parent_session_id: 'parent-stored', started_at: parentRowStartedAt }
+ ] as never)
+ $sessionTiles.set([
+ {
+ ownerRoute: { connectionId: null, mode: 'local', profile: 'default' },
+ runtimeId: 'branch-runtime',
+ storedSessionId: 'branch-stored'
+ }
+ ] as never)
+ $focusedTreePaneId.set('session-tile:branch-stored')
+
+ const item = sessionTimerItem()
+ const since = timerSince(item)
+
+ expect(since).toBe(branchRuntimeStartedAt)
+ expect(since).not.toBe(parentRowStartedAt * 1000)
+ })
+})
diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx
index 440d25a8e21..60847f3ecd0 100644
--- a/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx
+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx
@@ -182,6 +182,12 @@ export function useStatusbarItems({
// Only the fields read here are selected, so an unchanged readout bails out
// instead of rebuilding all ~9 statusbar items per token.
const focusedBusy = useStoreSelector($focusedSessionState, state => Boolean(state?.busy))
+
+ const focusedRuntimeStartedAt = useStoreSelector(
+ $focusedSessionState,
+ state => state?.runtimeStartedAt ?? null
+ )
+
const focusedTurnStartedAt = useStoreSelector($focusedSessionState, state => state?.turnStartedAt ?? null)
// `usage` is an object, so it can't be compared as a scalar. It IS however
// replaced wholesale rather than mutated, and only changes when the backend
@@ -276,7 +282,8 @@ export function useStatusbarItems({
focusedStoredSessionId,
primaryFocused,
primarySessionStartedAt,
- tileFocus: tileSessionFocusStartedAt
+ tileFocus: tileSessionFocusStartedAt,
+ fallbackRuntimeStartedAt: focusedRuntimeStartedAt
})
// The backend only knows a session's MEASURED occupancy once a turn has run
diff --git a/apps/desktop/src/lib/session-timer-since.ts b/apps/desktop/src/lib/session-timer-since.ts
index 2fa233021da..b451ca99a6b 100644
--- a/apps/desktop/src/lib/session-timer-since.ts
+++ b/apps/desktop/src/lib/session-timer-since.ts
@@ -16,6 +16,8 @@ export function resolveSessionTimerSince(input: {
primaryFocused: boolean
primarySessionStartedAt: number | null
tileFocus: null | TileSessionFocusStamp
+ /** Runtime-cache anchor for the focused tile, when one exists (#64990). */
+ fallbackRuntimeStartedAt?: null | number
}): number | null {
if (input.primaryFocused) {
return input.primarySessionStartedAt
@@ -24,7 +26,18 @@ export function resolveSessionTimerSince(input: {
const focused = input.focusedStoredSessionId
const tile = input.tileFocus
- if (!focused || !tile || tile.storedId !== focused) {
+ if (!focused) {
+ return null
+ }
+
+ // A tile with live runtime state anchors to the session's own runtime
+ // start, so a warm-cache reopen or a branch keeps its true elapsed time
+ // instead of the parent row's stored age (#64990).
+ if (input.fallbackRuntimeStartedAt != null) {
+ return input.fallbackRuntimeStartedAt
+ }
+
+ if (!tile || tile.storedId !== focused) {
return null
}