diff --git a/apps/desktop/src/app/agents/index.tsx b/apps/desktop/src/app/agents/index.tsx index 3921d5e98c..e84458877a 100644 --- a/apps/desktop/src/app/agents/index.tsx +++ b/apps/desktop/src/app/agents/index.tsx @@ -323,7 +323,7 @@ function StreamLine({ export function SubagentRow({ node, depth = 0, nowMs }: { node: SubagentNode; depth?: number; nowMs: number }) { const { t } = useI18n() const running = node.status === 'running' || node.status === 'queued' - const elapsed = useElapsedSeconds(running, `subagent:${node.id}`) + const elapsed = useElapsedSeconds(running, `subagent:${node.id}`, node.startedAt) const durationSeconds = typeof node.durationSeconds === 'number' ? Math.max(0, Math.round(node.durationSeconds)) : elapsed diff --git a/apps/desktop/src/app/chat/composer/status-stack/subagent-controls.tsx b/apps/desktop/src/app/chat/composer/status-stack/subagent-controls.tsx index 5f20481c04..678c5442d9 100644 --- a/apps/desktop/src/app/chat/composer/status-stack/subagent-controls.tsx +++ b/apps/desktop/src/app/chat/composer/status-stack/subagent-controls.tsx @@ -8,11 +8,12 @@ import { requestForOwnedSession } from '@/store/session-states' interface SubagentControlsProps { sessionId: string subagentId: string + text: string + setText: (text: string) => void } -export function SubagentControls({ sessionId, subagentId }: SubagentControlsProps) { +export function SubagentControls({ sessionId, subagentId, text, setText }: SubagentControlsProps) { const { t } = useI18n() - const [text, setText] = useState('') const [pending, setPending] = useState(false) const [feedback, setFeedback] = useState('') const [failed, setFailed] = useState(false) diff --git a/apps/desktop/src/app/chat/composer/status-stack/subagent-lifecycle.test.tsx b/apps/desktop/src/app/chat/composer/status-stack/subagent-lifecycle.test.tsx new file mode 100644 index 0000000000..5b5e79623d --- /dev/null +++ b/apps/desktop/src/app/chat/composer/status-stack/subagent-lifecycle.test.tsx @@ -0,0 +1,49 @@ +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, expect, it, vi } from 'vitest' + +import { $subagentsBySession, upsertSubagent } from '@/store/subagents' + +import { SubagentSection } from './subagent-section' + +vi.stubGlobal( + 'ResizeObserver', + class { + disconnect() {} + observe() {} + unobserve() {} + } +) +Element.prototype.animate = vi.fn(() => ({ cancel() {} }) as Animation) +afterEach(() => { + cleanup() + $subagentsBySession.set({}) + vi.restoreAllMocks() +}) + +it('keeps each worker draft while inspecting siblings and removes settled selection', () => { + upsertSubagent('parent', { subagent_id: 'a', goal: 'Worker A' }) + upsertSubagent('parent', { subagent_id: 'b', goal: 'Worker B' }) + render() + fireEvent.click(screen.getByRole('button', { name: /Worker A/ })) + fireEvent.change(screen.getByRole('textbox'), { target: { value: 'Preserve my instruction' } }) + fireEvent.click(screen.getByRole('button', { name: /Worker B/ })) + expect((screen.getByRole('textbox') as HTMLInputElement).value).toBe('') + fireEvent.click(screen.getByRole('button', { name: /Worker A/ })) + expect((screen.getByRole('textbox') as HTMLInputElement).value).toBe('Preserve my instruction') + act(() => upsertSubagent('parent', { subagent_id: 'a', status: 'completed' }, false, 'subagent.complete')) + expect(screen.queryByText('Worker A')).toBeNull() + expect(screen.queryByRole('textbox')).toBeNull() +}) + +it('measures detail elapsed from worker start rather than first inspection', () => { + const now = vi.spyOn(Date, 'now').mockReturnValue(100000) + upsertSubagent('parent', { subagent_id: 'timed', goal: 'Timed worker' }) + now.mockReturnValue(117000) + const { container } = render() + fireEvent.click(screen.getByRole('button', { name: /Timed worker/ })) + expect( + container.querySelector( + '[data-slot="composer-subagent-detail"] [data-slot="tool-block"] > button > span:last-child' + )?.textContent + ).toBe('17s') +}) diff --git a/apps/desktop/src/app/chat/composer/status-stack/subagent-section.tsx b/apps/desktop/src/app/chat/composer/status-stack/subagent-section.tsx index a1a2a19768..bac54f320d 100644 --- a/apps/desktop/src/app/chat/composer/status-stack/subagent-section.tsx +++ b/apps/desktop/src/app/chat/composer/status-stack/subagent-section.tsx @@ -1,10 +1,11 @@ -import { useEffect, useState } from 'react' +import { useState } from 'react' import { SubagentRow } from '@/app/agents' import { ActivityTimerText } from '@/components/chat/activity-timer-text' import { StatusSection } from '@/components/chat/status-section' import { Codicon } from '@/components/ui/codicon' import { GlyphSpinner } from '@/components/ui/glyph-spinner' +import { useViewedInterval } from '@/hooks/use-viewed-interval' import { useI18n } from '@/i18n' import { useSessionSlice } from '@/lib/use-session-slice' import { $subagentsBySession, type SubagentProgress } from '@/store/subagents' @@ -22,17 +23,10 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) { const live = items.filter(item => item.status === 'running' || item.status === 'queued') const [nowMs, setNowMs] = useState(Date.now) const [selected, setSelected] = useState(null) + const [drafts, setDrafts] = useState>({}) const hasLive = live.length > 0 - useEffect(() => { - if (!hasLive) { - return - } - - const timer = setInterval(() => setNowMs(Date.now()), 1000) - - return () => clearInterval(timer) - }, [hasLive]) + useViewedInterval(() => setNowMs(Date.now()), 1000, hasLive) if (!hasLive) { return null @@ -46,7 +40,11 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) { onClick={() => setSelected(selected === item.id ? null : item.id)} type="button" > - + {item.goal} @@ -60,7 +58,7 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) { ) - const detail = items.find(item => item.id === selected) + const detail = live.find(item => item.id === selected) return (
@@ -81,7 +79,13 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) { {detail && (
{(detail.status === 'running' || detail.status === 'queued') && ( - + setDrafts(previous => ({ ...previous, [detail.id]: text }))} + subagentId={detail.id} + text={drafts[detail.id] ?? ''} + /> )}