perf(ui): swap the session activity spinner for a dot and a turn timer

The spinner ran a CSS animation on every active row for the whole turn,
repainting a composited layer at frame rate. Rows now carry a static dot —
primary while running, info while unread — and the metadata slot on the right
shows how long the turn has been going, updating once per second in the dot's
colour. The counter is the motion the spinner used to provide, at 1 fps.
Collapsed groups, folders and projects take the dot only, since one counter
cannot speak for several running turns.

Elapsed time is measured client-side because SessionStatus carries no
timestamps, and starts are persisted so a reload resumes the same count. Two
rules keep that honest. Only a liveness stamp — refreshed while a session is
observed active, stamped as the page hides, and compared against the page's
navigation start so a slow bootstrap is not charged to the absence — and a 90s
adoption window may expire a record; a snapshot that cannot yet see a session
is not evidence its turn ended. And a busy event is never read as a turn
boundary, because OpenCode republishes busy at every step of the agent loop, so
after a reload one of those repeats normally beats the first status snapshot.
Idle and error events do end a turn, and retire the record with it.

Snapshot reconciliation walks the running turns and asks whether the snapshot
covers each one, rather than being handed everything it covers: only a live
start can settle, so the pass scales with timing work instead of with the
directory's session list, and allocates nothing per poll.

Also applied to the mobile sessions sheet and session switcher. The shared
duration ticker moves to hooks/ now that it has a second consumer.
This commit is contained in:
Bohdan Triapitsyn
2026-08-05 03:06:35 +03:00
parent ce0e1cea27
commit 094f728777
27 changed files with 1094 additions and 66 deletions
+21 -6
View File
@@ -1,7 +1,7 @@
import React from 'react';
import type { Session } from '@opencode-ai/sdk/v2';
import { Icon } from '@/components/icon/Icon';
import { SessionActivityDuration } from '@/components/session/SessionActivityDuration';
import { formatSessionCompactDateLabel } from '@/components/session/sidebar/utils';
import { useSwitcherItems } from '@/components/session/sidebar/hooks/useSwitcherItems';
import { useTabletLayout } from '@/lib/device';
@@ -10,6 +10,7 @@ import { cn } from '@/lib/utils';
import { refreshGlobalSessions, resolveGlobalSessionDirectory } from '@/stores/useGlobalSessionsStore';
import { useProjectsStore } from '@/stores/useProjectsStore';
import { useSessionUnseenCount } from '@/sync/notification-store';
import { useHasSessionActivityDuration } from '@/sync/session-activity-timing';
import { useSessionUIStore } from '@/sync/session-ui-store';
import { useGlobalSessionStatus } from '@/sync/sync-context';
@@ -35,6 +36,8 @@ const SwitcherRow: React.FC<{
const statusType = status?.type ?? 'idle';
const isStreaming = statusType === 'busy' || statusType === 'retry';
const showUnreadDot = !isStreaming && unseenCount > 0 && !active;
const hasActivityDuration = useHasSessionActivityDuration(session.id, isStreaming);
const showActivityDuration = (isStreaming || showUnreadDot) && hasActivityDuration;
const timeLabel = formatSessionCompactDateLabel(session.time?.updated ?? session.time?.created ?? 0);
return (
@@ -56,12 +59,24 @@ const SwitcherRow: React.FC<{
) : null}
</span>
{/* Activity sits on the right, before the time — no reserved left gutter. */}
{isStreaming ? (
<Icon name="loader-4" className="size-3.5 shrink-0 animate-spin text-primary" aria-hidden />
) : showUnreadDot ? (
<span className="size-1.5 shrink-0 rounded-full bg-[var(--status-info)]" aria-hidden />
{isStreaming || showUnreadDot ? (
<span
className={cn(
'size-1.5 shrink-0 rounded-full',
isStreaming ? 'bg-primary' : 'bg-[var(--status-info)]',
)}
aria-hidden
/>
) : null}
{timeLabel ? (
{/* The elapsed turn takes the time slot while it matters, then hands it
back to the relative timestamp. */}
{showActivityDuration ? (
<SessionActivityDuration
sessionId={session.id}
running={isStreaming}
className="typography-micro"
/>
) : timeLabel ? (
<span className="shrink-0 typography-micro text-muted-foreground tabular-nums">{timeLabel}</span>
) : null}
</button>
+21 -5
View File
@@ -64,6 +64,8 @@ import {
import { useSessionUIStore } from '@/sync/session-ui-store';
import { useAllLiveSessions, useGlobalSessionStatus } from '@/sync/sync-context';
import { useSessionUnseenCount } from '@/sync/notification-store';
import { useHasSessionActivityDuration } from '@/sync/session-activity-timing';
import { SessionActivityDuration } from '@/components/session/SessionActivityDuration';
import type { WorktreeMetadata } from '@/types/worktree';
import { MobileDeleteWorktreeDialog } from './MobileDeleteWorktreeDialog';
@@ -476,6 +478,8 @@ const SessionRow: React.FC<{
const statusType = liveStatus?.type ?? 'idle';
const isStreaming = statusType === 'busy' || statusType === 'retry';
const showUnreadDot = !isStreaming && unseenCount > 0 && !active;
const hasActivityDuration = useHasSessionActivityDuration(session.id, isStreaming);
const showActivityDuration = (isStreaming || showUnreadDot) && hasActivityDuration;
const contentRef = React.useRef<HTMLDivElement>(null);
const startRef = React.useRef<{ x: number; y: number } | null>(null);
@@ -616,10 +620,14 @@ const SessionRow: React.FC<{
onToggleChildren?.();
}}
>
{isStreaming ? (
<Icon name="loader-4" className="size-3.5 animate-spin text-primary" />
) : showUnreadDot ? (
<span className="size-1.5 rounded-full bg-[var(--status-info)]" aria-hidden />
{isStreaming || showUnreadDot ? (
<span
className={cn(
'size-1.5 rounded-full',
isStreaming ? 'bg-primary' : 'bg-[var(--status-info)]',
)}
aria-hidden
/>
) : (
<RiArrowDownSLine className={cn('size-[18px] transition-transform duration-150', expanded ? 'rotate-0' : '-rotate-90')} />
)}
@@ -664,7 +672,15 @@ const SessionRow: React.FC<{
>
{title}
</span>
{time ? (
{/* The elapsed turn takes the time slot while it matters, then
hands it back to the relative timestamp. */}
{showActivityDuration ? (
<SessionActivityDuration
sessionId={session.id}
running={isStreaming}
className="typography-micro"
/>
) : time ? (
<span className="shrink-0 typography-micro text-muted-foreground tabular-nums">{time}</span>
) : null}
</span>
@@ -17,6 +17,7 @@ import { useSessionUIStore } from '@/sync/session-ui-store';
import { resetStreamingState } from '@/sync/streaming';
import { useGlobalSessionStatusStore } from '@/sync/global-session-status';
import { resetSessionOrdering } from '@/sync/session-ordering';
import { resetSessionActivityTiming } from '@/sync/session-activity-timing';
import { syncDesktopSettings } from '@/lib/persistence';
// Same-device transport switch (LAN⇄relay for one paired device): rebind the SDK
@@ -56,6 +57,9 @@ export const resetAppForRuntimeEndpointChange = (detail: RuntimeEndpointChangedD
useGlobalSessionsStore.getState().resetForRuntimeSwitch();
useGlobalSessionStatusStore.setState({ statusById: new Map() });
resetSessionOrdering();
// Turn timings belong to the previous instance's sessions, and the reset also
// restarts the resume window so the switch is treated as a fresh load.
resetSessionActivityTiming();
usePermissionStore.getState().reset();
useFileSearchStore.getState().resetForRuntimeSwitch();
useGitStore.getState().resetForRuntimeSwitch(detail.runtimeKey);