Skip to content

The agent that grows with you

fix(desktop): keep pet activity bound to the live runtime

Ayush Nangia91f679a140a4
diff --git a/apps/desktop/src/store/pet.test.ts b/apps/desktop/src/store/pet.test.ts
index 58c6a534d16..22bca027f9b 100644
--- a/apps/desktop/src/store/pet.test.ts
+++ b/apps/desktop/src/store/pet.test.ts
@@ -1,4 +1,6 @@
-import { describe, expect, it } from 'vitest'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { createClientSessionState } from '@/lib/chat-runtime'
import {
$petActivity,
@@ -13,6 +15,8 @@ import {
setPetActivity,
STEADY_ACTIVITY_TTL_MS
} from './pet'
+import { $activeSessionId, $busy } from './session'
+import { clearAllSessionStates, publishSessionState } from './session-states'
describe('derivePetState', () => {
it('rests at idle by default and uses waiting when awaiting input', () => {
@@ -149,6 +153,14 @@ describe('flashPetActivity', () => {
describe('deriveLivePetState recency guard (#84434 / #84438)', () => {
const now = Date.now()
+ afterEach(() => {
+ vi.useRealTimers()
+ clearAllSessionStates()
+ $activeSessionId.set(null)
+ $busy.set(false)
+ $petActivity.set({})
+ })
+
it('animates a recent toolRunning flag even when $busy reads false', () => {
$petActivity.set({ toolRunning: true, toolRunningAt: now - 5_000 })
expect($petState.get()).toBe('run')
@@ -171,4 +183,36 @@ describe('deriveLivePetState recency guard (#84434 / #84438)', () => {
expect(activity.toolRunningAt).toBeGreaterThanOrEqual(before)
expect(activity.toolRunningAt).toBeLessThanOrEqual(Date.now())
})
+
+ it('keeps a silent long-running tool active while its runtime is busy', () => {
+ vi.useFakeTimers()
+ vi.setSystemTime(now + STEADY_ACTIVITY_TTL_MS * 4)
+ $activeSessionId.set('runtime-long-tool')
+ $busy.set(false)
+ publishSessionState('runtime-long-tool', {
+ ...createClientSessionState(null),
+ busy: true,
+ storedSessionId: 'stored-long-tool'
+ })
+ $petActivity.set({ toolRunning: true, toolRunningAt: now })
+
+ expect($petState.get()).toBe('run')
+
+ publishSessionState('runtime-long-tool', {
+ ...createClientSessionState(null),
+ busy: false,
+ storedSessionId: 'stored-long-tool'
+ })
+ expect($petState.get()).toBe('idle')
+ })
+
+ it('clears the matching recency stamp when a steady flag clears', () => {
+ setPetActivity({ toolRunning: true, reasoning: true })
+ expect($petActivity.get().toolRunningAt).toBeTypeOf('number')
+ expect($petActivity.get().reasoningAt).toBeTypeOf('number')
+
+ setPetActivity({ toolRunning: false, reasoning: false })
+ expect($petActivity.get().toolRunningAt).toBeUndefined()
+ expect($petActivity.get().reasoningAt).toBeUndefined()
+ })
})
diff --git a/apps/desktop/src/store/pet.ts b/apps/desktop/src/store/pet.ts
index db7928c98bb..df3e0f4ea00 100644
--- a/apps/desktop/src/store/pet.ts
+++ b/apps/desktop/src/store/pet.ts
@@ -2,7 +2,8 @@ import { atom, computed } from 'nanostores'
import { persistBoolean, storedBoolean } from '@/lib/storage'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
-import { $busy } from '@/store/session'
+import { $activeSessionId, $busy } from '@/store/session'
+import { $sessionStates } from '@/store/session-states'
/**
* Petdex mascot state for the desktop floating pet.
@@ -177,10 +178,14 @@ export const setPetActivity = (next: Partial<PetActivity>) => {
if (next.toolRunning === true) {
stamped.toolRunningAt = now
+ } else if (next.toolRunning === false) {
+ stamped.toolRunningAt = undefined
}
if (next.reasoning === true) {
stamped.reasoningAt = now
+ } else if (next.reasoning === false) {
+ stamped.reasoningAt = undefined
}
$petActivity.set({ ...$petActivity.get(), ...stamped })
@@ -212,8 +217,12 @@ export const setPetInfo = (info: PetInfo) => $petInfo.set(info)
* mirrored to the pop-out overlay through the same atom, so both surfaces agree
* without the overlay needing the session list.
*/
-function deriveLivePetState(activity: PetActivity, busy: boolean): PetState {
- const live = activity.busy ?? busy
+function deriveLivePetState(activity: PetActivity, busy: boolean, activeSessionBusy?: boolean): PetState {
+ // The per-runtime cache is the authoritative turn state. `$busy` is only a
+ // legacy foreground mirror and can miss the first turn of a draft or a turn
+ // resumed after backend reclaim (#84434/#84438). Keep it as the fallback for
+ // boot/unbound states where the active runtime has not reached the cache yet.
+ const live = activity.busy ?? activeSessionBusy ?? busy
// The global $busy can read false for a session that IS working: a
// reclaimed session reopened after idle timeout (#84434) or a desktop-
@@ -238,6 +247,10 @@ function deriveLivePetState(activity: PetActivity, busy: boolean): PetState {
})
}
+const $activeSessionBusy = computed([$activeSessionId, $sessionStates], (runtimeId, states) =>
+ runtimeId ? states[runtimeId]?.busy : undefined
+)
+
/**
* Opt-in: let the floating mascot wander around the window on its own while
* idle. Pure desktop-client behavior (no agent/config dependency), so it lives
@@ -272,8 +285,8 @@ export const $petRoamDir = atom<-1 | 0 | 1>(0)
* `$petMotion`-driven pose and stall the wander.
*/
export const $petAtRest = computed(
- [$petActivity, $busy],
- (activity, busy): boolean => deriveLivePetState(activity, busy) === 'idle'
+ [$petActivity, $busy, $activeSessionBusy],
+ (activity, busy, activeSessionBusy): boolean => deriveLivePetState(activity, busy, activeSessionBusy) === 'idle'
)
/**
@@ -281,8 +294,11 @@ export const $petAtRest = computed(
* a roam pose (walking → `run`, hopping → `jump`) show through, so the wander
* reads as deliberate movement.
*/
-export const $petState = computed([$petActivity, $busy, $petMotion], (activity, busy, motion): PetState => {
- const base = deriveLivePetState(activity, busy)
+export const $petState = computed(
+ [$petActivity, $busy, $activeSessionBusy, $petMotion],
+ (activity, busy, activeSessionBusy, motion): PetState => {
+ const base = deriveLivePetState(activity, busy, activeSessionBusy)
- return base === 'idle' && motion ? motion : base
-})
+ return base === 'idle' && motion ? motion : base
+ }
+)