Skip to content

The agent that grows with you

fix(desktop): pet animates for reclaimed/draft sessions via recency-guarded activity

Ayush Nangia1878e5f96901
diff --git a/apps/desktop/src/store/pet.test.ts b/apps/desktop/src/store/pet.test.ts
index c3fa2962f39..53a73ed2453 100644
--- a/apps/desktop/src/store/pet.test.ts
+++ b/apps/desktop/src/store/pet.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
$petActivity,
+ STEADY_ACTIVITY_TTL_MS,
$petAtRest,
$petMotion,
$petState,
@@ -144,3 +145,30 @@ describe('flashPetActivity', () => {
setPetActivity({})
})
})
+
+describe('deriveLivePetState recency guard (#84434 / #84438)', () => {
+ const now = Date.now()
+
+ it('animates a recent toolRunning flag even when $busy reads false', () => {
+ $petActivity.set({ toolRunning: true, toolRunningAt: now - 5_000 })
+ expect($petState.get()).toBe('run')
+ })
+
+ it('animates a recent reasoning flag even when $busy reads false', () => {
+ $petActivity.set({ reasoning: true, reasoningAt: now - 5_000 })
+ expect($petState.get()).toBe('review')
+ })
+
+ it('decays a stale flag back to idle (interrupted-turn guard preserved)', () => {
+ $petActivity.set({ toolRunning: true, toolRunningAt: now - STEADY_ACTIVITY_TTL_MS - 1 })
+ expect($petState.get()).toBe('idle')
+ })
+
+ it('setPetActivity stamps the timestamp on set', () => {
+ const before = Date.now()
+ setPetActivity({ toolRunning: true })
+ const activity = $petActivity.get()
+ expect(activity.toolRunningAt).toBeGreaterThanOrEqual(before)
+ expect(activity.toolRunningAt).toBeLessThanOrEqual(Date.now())
+ })
+})
diff --git a/apps/desktop/src/store/pet.ts b/apps/desktop/src/store/pet.ts
index ed28cc2dbc6..762be31d078 100644
--- a/apps/desktop/src/store/pet.ts
+++ b/apps/desktop/src/store/pet.ts
@@ -94,8 +94,14 @@ export interface PetActivity {
error?: boolean
justCompleted?: boolean
celebrate?: boolean
+ /** Date.now() stamps for the recency guard in deriveLivePetState (#84434/#84438). */
+ toolRunningAt?: number
+ reasoningAt?: number
}
+/** How long a steady flag counts without a $busy confirmation. */
+export const STEADY_ACTIVITY_TTL_MS = 30_000
+
/**
* Resolve the animation state from coarse activity signals.
*
@@ -165,7 +171,17 @@ export const markPetUnread = () => $petUnread.set(true)
export const clearPetUnread = () => $petUnread.set(false)
/** Steady activity flags (toolRunning / reasoning) set + cleared by the stream. */
-export const setPetActivity = (next: Partial<PetActivity>) => $petActivity.set({ ...$petActivity.get(), ...next })
+export const setPetActivity = (next: Partial<PetActivity>) => {
+ const now = Date.now()
+ const stamped: PetActivity = { ...next }
+ if (next.toolRunning === true) {
+ stamped.toolRunningAt = now
+ }
+ if (next.reasoning === true) {
+ stamped.reasoningAt = now
+ }
+ $petActivity.set({ ...$petActivity.get(), ...stamped })
+}
let flashTimer: ReturnType<typeof setTimeout> | undefined
@@ -196,13 +212,23 @@ export const setPetInfo = (info: PetInfo) => $petInfo.set(info)
function deriveLivePetState(activity: PetActivity, busy: boolean): PetState {
const live = activity.busy ?? busy
+ // The global $busy can read false for a session that IS working: a
+ // reclaimed session reopened after idle timeout (#84434) or a desktop-
+ // minted draft (#84438) doesn't always flip the busy mirror on its turn,
+ // so the pet froze idle while tools ran. Honor a steady flag while it is
+ // RECENT (stream-refreshed) even when busy is stuck false — the stamp is
+ // re-set on every tool.start / message.start — and let it decay back to
+ // idle when the stream goes silent, which preserves the original intent
+ // of ignoring stale flags from interrupted turns.
+ const now = Date.now()
+ const recentTool = (activity.toolRunningAt ?? 0) > now - STEADY_ACTIVITY_TTL_MS
+ const recentReasoning = (activity.reasoningAt ?? 0) > now - STEADY_ACTIVITY_TTL_MS
+
return derivePetState({
busy: live,
awaitingInput: activity.awaitingInput,
- // Steady flags only count mid-turn — ignore stale ones once at rest so an
- // interrupted turn can't pin the pet on `run`/`review`.
- toolRunning: live && activity.toolRunning,
- reasoning: live && activity.reasoning,
+ toolRunning: (live || recentTool) && activity.toolRunning,
+ reasoning: (live || recentReasoning) && activity.reasoning,
error: activity.error,
justCompleted: activity.justCompleted,
celebrate: activity.celebrate