The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): drive the pet from any working session, not just the foreground one (#84282)
PRATHAMESH759c2c1855aaf1
diff --git a/apps/desktop/src/app/contrib/hooks/use-pet-bridge.test.ts b/apps/desktop/src/app/contrib/hooks/use-pet-bridge.test.tsnew file mode 100644index 00000000000..72723b76208--- /dev/null+++ b/apps/desktop/src/app/contrib/hooks/use-pet-bridge.test.ts@@ -0,0 +1,64 @@+import { afterEach, beforeEach, describe, expect, it } from 'vitest'++import { createClientSessionState } from '@/lib/chat-runtime'+import { $petActivity, $petState, setPetActivity } from '@/store/pet'+import { clearAllSessionStates, publishSessionState } from '@/store/session-states'++import { syncPetBusyFromSessions } from './use-pet-bridge'++const busySession = (storedId: string) => ({ ...createClientSessionState(storedId), busy: true })++describe('syncPetBusyFromSessions', () => {+ beforeEach(() => {+ clearAllSessionStates()+ setPetActivity({ busy: false, toolRunning: false, reasoning: false, awaitingInput: false })+ })++ afterEach(() => {+ clearAllSessionStates()+ setPetActivity({ busy: false, toolRunning: false, reasoning: false, awaitingInput: false })+ })++ it('marks the pet busy while any session is working, foreground or not', () => {+ // A background session is running — its per-session stream hook is NOT+ // mounted, so nothing ever set toolRunning/reasoning on $petActivity.+ publishSessionState('runtime-background', busySession('stored-background'))++ syncPetBusyFromSessions()++ expect($petActivity.get().busy).toBe(true)+ // busy maps to the `run` pose even without a foreground toolRunning signal.+ expect($petState.get()).toBe('run')+ })++ it('clears busy and returns to idle once every session is at rest', () => {+ publishSessionState('runtime-background', busySession('stored-background'))+ syncPetBusyFromSessions()+ expect($petActivity.get().busy).toBe(true)++ // Turn ends: the session is no longer busy.+ publishSessionState('runtime-background', {+ ...createClientSessionState('stored-background'),+ busy: false+ })++ syncPetBusyFromSessions()++ expect($petActivity.get().busy).toBe(false)+ expect($petState.get()).toBe('idle')+ })++ it('stays busy when only one of several sessions finishes', () => {+ publishSessionState('runtime-a', busySession('stored-a'))+ publishSessionState('runtime-b', busySession('stored-b'))+ syncPetBusyFromSessions()+ expect($petActivity.get().busy).toBe(true)++ // Session A finishes; B is still running.+ publishSessionState('runtime-a', { ...createClientSessionState('stored-a'), busy: false })+ syncPetBusyFromSessions()++ expect($petActivity.get().busy).toBe(true)+ expect($petState.get()).toBe('run')+ })+})diff --git a/apps/desktop/src/app/contrib/hooks/use-pet-bridge.ts b/apps/desktop/src/app/contrib/hooks/use-pet-bridge.tsindex 0334a9a83f6..011cf2380c5 100644--- a/apps/desktop/src/app/contrib/hooks/use-pet-bridge.ts+++ b/apps/desktop/src/app/contrib/hooks/use-pet-bridge.ts@@ -4,11 +4,29 @@ import { setPetActivity } from '@/store/pet'import { setPetScale } from '@/store/pet-gallery'import { setPetOverlayOpenAppHandler, setPetOverlayScaleHandler, setPetOverlaySubmitHandler } from '@/store/pet-overlay'import { $sessions } from '@/store/session'-import { $attentionSessionIds } from '@/store/session-states'+import { $attentionSessionIds, $workingSessionIds } from '@/store/session-states'import { isAuxiliaryWindow } from '@/store/windows'import type { GatewayRequester } from '../types'+/**+ * Mirror "any open session is running the agent" into the pet's `busy` flag.+ *+ * `toolRunning` / `reasoning` are set only inside the per-session message-stream+ * hook (`use-message-stream/gateway-event.ts`), which is mounted for the+ * foreground session alone — so a background session running tools left the pet+ * stuck in `idle` (#84282). `$workingSessionIds` is a store-level computed over+ * every session's authoritative `busy` state (the same cross-session source+ * `$attentionSessionIds` provides for the `waiting` pose), so it stays live+ * regardless of which session is rendered. `derivePetState` maps `busy` to the+ * `run` pose, so any working session — foreground or background — now animates+ * the pet, while the foreground stream's finer `toolRunning` / `reasoning`+ * signals still refine run-vs-review for the visible session.+ */+export function syncPetBusyFromSessions(): void {+ setPetActivity({ busy: $workingSessionIds.get().length > 0 })+}+interface PetBridgeParams {requestGateway: GatewayRequesterresumeSession: (sessionId: string) => Promise<unknown> | unknown@@ -65,4 +83,12 @@ export function usePetBridge({ requestGateway, resumeSession, submitText }: PetBreturn $attentionSessionIds.listen(sync)}, [])++ // Mirror "any session is running the agent" into the pet's busy flag so the+ // pet reacts to background sessions too, not just the foreground one (#84282).+ useEffect(() => {+ syncPetBusyFromSessions()++ return $workingSessionIds.listen(syncPetBusyFromSessions)+ }, [])}