feat(desktop): steer and stop subagents through their parent owner
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||
import { afterEach, expect, it, vi } from 'vitest'
|
||||
|
||||
import * as gateway from '@/store/gateway'
|
||||
import { _resetSessionOwnerHintsForTests, setSessionOwnerHint } from '@/store/session'
|
||||
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({})
|
||||
_resetSessionOwnerHintsForTests()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('sends steer and stop to the child parent owner, never the active gateway or child transcript', async () => {
|
||||
const request = vi.spyOn(gateway, 'requestGatewayForAgent').mockResolvedValue({ status: 'queued', found: true })
|
||||
setSessionOwnerHint('parent', { connectionId: 'remote-owner', profile: 'research' })
|
||||
upsertSubagent('parent', { subagent_id: 'worker', child_session_id: 'child-transcript', goal: 'Owned work' })
|
||||
render(<SubagentSection sessionId="parent" />)
|
||||
fireEvent.click(screen.getByRole('button', { name: /Owned work/ }))
|
||||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'Check the negative control' } })
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Steer' }))
|
||||
await waitFor(() =>
|
||||
expect(request).toHaveBeenCalledWith('remote-owner', 'research', 'subagent.steer', {
|
||||
session_id: 'parent',
|
||||
subagent_id: 'worker',
|
||||
text: 'Check the negative control'
|
||||
})
|
||||
)
|
||||
expect(screen.getByText('Queued for the next checkpoint')).toBeTruthy()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Stop' }))
|
||||
await waitFor(() =>
|
||||
expect(request).toHaveBeenCalledWith('remote-owner', 'research', 'subagent.interrupt', {
|
||||
session_id: 'parent',
|
||||
subagent_id: 'worker'
|
||||
})
|
||||
)
|
||||
expect($subagentsBySession.get().parent?.[0]?.status).toBe('running')
|
||||
})
|
||||
|
||||
it('keeps rejected steer text and does not retarget when the owner is unknown', async () => {
|
||||
const request = vi.spyOn(gateway, 'requestGatewayForAgent').mockResolvedValue({ status: 'rejected' })
|
||||
upsertSubagent('unknown', { subagent_id: 'worker', goal: 'Unbound work' })
|
||||
render(<SubagentSection sessionId="unknown" />)
|
||||
fireEvent.click(screen.getByRole('button', { name: /Unbound work/ }))
|
||||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'Keep this instruction' } })
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Steer' }))
|
||||
await waitFor(() => expect(screen.getByRole('alert')).toBeTruthy())
|
||||
expect(request).not.toHaveBeenCalled()
|
||||
expect((screen.getByRole('textbox') as HTMLInputElement).value).toBe('Keep this instruction')
|
||||
})
|
||||
@@ -0,0 +1,89 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { useI18n } from '@/i18n'
|
||||
import { requestForOwnedSession } from '@/store/session-states'
|
||||
|
||||
interface SubagentControlsProps {
|
||||
sessionId: string
|
||||
subagentId: string
|
||||
}
|
||||
|
||||
export function SubagentControls({ sessionId, subagentId }: SubagentControlsProps) {
|
||||
const { t } = useI18n()
|
||||
const [text, setText] = useState('')
|
||||
const [pending, setPending] = useState(false)
|
||||
const [feedback, setFeedback] = useState('')
|
||||
const [failed, setFailed] = useState(false)
|
||||
|
||||
const send = async (action: 'steer' | 'interrupt') => {
|
||||
setPending(true)
|
||||
setFeedback('')
|
||||
setFailed(false)
|
||||
|
||||
try {
|
||||
// Unlike global chrome, a child control must NEVER fall back to whichever
|
||||
// gateway happens to be active, even on a legacy unbound session.
|
||||
const result = await requestForOwnedSession<{ found?: boolean; status?: string }>(
|
||||
sessionId,
|
||||
async () => {
|
||||
throw new Error(t.agents.requestRejected)
|
||||
},
|
||||
`subagent.${action}`,
|
||||
{ session_id: sessionId, subagent_id: subagentId, ...(action === 'steer' ? { text: text.trim() } : {}) }
|
||||
)
|
||||
|
||||
if (action === 'steer' ? result.status !== 'queued' : !result.found) {
|
||||
throw new Error(t.agents.requestRejected)
|
||||
}
|
||||
|
||||
setFeedback(action === 'steer' ? t.agents.steerQueued : t.agents.stopRequested)
|
||||
|
||||
if (action === 'steer') {
|
||||
setText('')
|
||||
}
|
||||
} catch {
|
||||
setFailed(true)
|
||||
setFeedback(t.agents.requestRejected)
|
||||
} finally {
|
||||
setPending(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form
|
||||
className="grid gap-1 pb-3"
|
||||
onKeyDown={event => event.stopPropagation()}
|
||||
onSubmit={event => {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
if (text.trim() && !pending) {
|
||||
void send('steer')
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="flex min-w-0 items-center gap-1">
|
||||
<Input
|
||||
aria-label={t.agents.steerPlaceholder}
|
||||
disabled={pending}
|
||||
onChange={event => setText(event.target.value)}
|
||||
placeholder={t.agents.steerPlaceholder}
|
||||
value={text}
|
||||
/>
|
||||
<Button disabled={pending || !text.trim()} size="xs" type="submit" variant="secondary">
|
||||
{t.agents.steer}
|
||||
</Button>
|
||||
<Button disabled={pending} onClick={() => void send('interrupt')} size="xs" type="button" variant="text">
|
||||
{t.statusStack.stop}
|
||||
</Button>
|
||||
</div>
|
||||
{feedback && (
|
||||
<p className="text-xs text-(--ui-text-secondary)" role={failed ? 'alert' : 'status'}>
|
||||
{feedback}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -6,9 +6,18 @@ import { $subagentsBySession, upsertSubagent } from '@/store/subagents'
|
||||
|
||||
import { ComposerStatusStack } from './index'
|
||||
|
||||
vi.stubGlobal('ResizeObserver', class { disconnect() {} observe() {} })
|
||||
vi.stubGlobal(
|
||||
'ResizeObserver',
|
||||
class {
|
||||
disconnect() {}
|
||||
observe() {}
|
||||
}
|
||||
)
|
||||
|
||||
afterEach(() => { cleanup(); $subagentsBySession.set({}) })
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
$subagentsBySession.set({})
|
||||
})
|
||||
|
||||
it('automatically previews bounded live work only from the composer session, including queued children', () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
@@ -17,7 +26,13 @@ it('automatically previews bounded live work only from the composer session, inc
|
||||
|
||||
upsertSubagent('other-profile', { subagent_id: 'foreign', goal: 'Private foreign task' })
|
||||
upsertSubagent('owner', { subagent_id: 'child-0', text: 'Reading actual source' }, false, 'subagent.progress')
|
||||
const view = render(<MemoryRouter><ComposerStatusStack queue={null} sessionId="owner" /></MemoryRouter>)
|
||||
|
||||
const view = render(
|
||||
<MemoryRouter>
|
||||
<ComposerStatusStack queue={null} sessionId="owner" />
|
||||
</MemoryRouter>
|
||||
)
|
||||
|
||||
expect(screen.getByText('Task 0')).toBeTruthy()
|
||||
expect(screen.getByText('Reading actual source')).toBeTruthy()
|
||||
expect(screen.queryByText('Task 4')).toBeNull()
|
||||
@@ -25,13 +40,21 @@ it('automatically previews bounded live work only from the composer session, inc
|
||||
expect(screen.getByRole('button', { name: /5 Subagents/ })).toBeTruthy()
|
||||
fireEvent.click(screen.getByRole('button', { name: /5 Subagents/ }))
|
||||
expect(screen.getByText('Task 4')).toBeTruthy()
|
||||
view.rerender(<MemoryRouter><ComposerStatusStack queue={null} sessionId="empty" /></MemoryRouter>)
|
||||
view.rerender(
|
||||
<MemoryRouter>
|
||||
<ComposerStatusStack queue={null} sessionId="empty" />
|
||||
</MemoryRouter>
|
||||
)
|
||||
expect(screen.queryByText('Task 0')).toBeNull()
|
||||
})
|
||||
|
||||
it('retires the live frame only after every child settles, without depending on the parent busy state', () => {
|
||||
upsertSubagent('owner', { subagent_id: 'child', goal: 'Live task' })
|
||||
render(<MemoryRouter><ComposerStatusStack queue={null} sessionId="owner" /></MemoryRouter>)
|
||||
render(
|
||||
<MemoryRouter>
|
||||
<ComposerStatusStack queue={null} sessionId="owner" />
|
||||
</MemoryRouter>
|
||||
)
|
||||
expect(screen.getByText('Live task')).toBeTruthy()
|
||||
act(() => upsertSubagent('owner', { subagent_id: 'child', status: 'completed' }, false, 'subagent.complete'))
|
||||
expect(screen.queryByText('Live task')).toBeNull()
|
||||
|
||||
@@ -9,6 +9,8 @@ import { useI18n } from '@/i18n'
|
||||
import { useSessionSlice } from '@/lib/use-session-slice'
|
||||
import { $subagentsBySession, type SubagentProgress } from '@/store/subagents'
|
||||
|
||||
import { SubagentControls } from './subagent-controls'
|
||||
|
||||
interface SubagentSectionProps {
|
||||
sessionId: string
|
||||
}
|
||||
@@ -23,36 +25,67 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) {
|
||||
const hasLive = live.length > 0
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasLive) { return }
|
||||
if (!hasLive) {
|
||||
return
|
||||
}
|
||||
|
||||
const timer = setInterval(() => setNowMs(Date.now()), 1000)
|
||||
|
||||
return () => clearInterval(timer)
|
||||
}, [hasLive])
|
||||
|
||||
if (!hasLive) { return null }
|
||||
if (!hasLive) {
|
||||
return null
|
||||
}
|
||||
|
||||
const row = (item: SubagentProgress) => (
|
||||
<button aria-expanded={selected === item.id} className="flex w-full min-w-0 items-start gap-2 px-2 py-1 text-left"
|
||||
key={item.id} onClick={() => setSelected(selected === item.id ? null : item.id)} type="button">
|
||||
<button
|
||||
aria-expanded={selected === item.id}
|
||||
className="flex w-full min-w-0 items-start gap-2 px-2 py-1 text-left"
|
||||
key={item.id}
|
||||
onClick={() => setSelected(selected === item.id ? null : item.id)}
|
||||
type="button"
|
||||
>
|
||||
<GlyphSpinner ariaLabel={t.agents.running} className="mt-0.5 shrink-0 text-(--ui-purple)" spinner="braille" />
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-xs text-(--ui-text-primary)">{item.goal}</span>
|
||||
<span className="block truncate text-[0.68rem] text-(--ui-text-tertiary)">{item.stream.at(-1)?.text || (item.status === 'queued' ? t.agents.queued : t.agents.waitingActivity)}</span>
|
||||
<span className="block truncate text-[0.68rem] text-(--ui-text-tertiary)">
|
||||
{item.stream.at(-1)?.text || (item.status === 'queued' ? t.agents.queued : t.agents.waitingActivity)}
|
||||
</span>
|
||||
</span>
|
||||
<ActivityTimerText className="shrink-0 text-[0.65rem]" seconds={Math.max(0, Math.floor((nowMs - item.startedAt) / 1000))} />
|
||||
<ActivityTimerText
|
||||
className="shrink-0 text-[0.65rem]"
|
||||
seconds={Math.max(0, Math.floor((nowMs - item.startedAt) / 1000))}
|
||||
/>
|
||||
</button>
|
||||
)
|
||||
|
||||
const detail = items.find(item => item.id === selected)
|
||||
|
||||
return <div className="composer-no-drag min-w-0" data-slot="composer-subagents">
|
||||
<StatusSection icon={<Codicon className="text-(--ui-purple)" name="agent" size="0.8rem" />}
|
||||
label={t.statusStack.subagents(live.length)}
|
||||
preview={<>{live.slice(0, 3).map(row)}{live.length > 3 && <p className="px-2 text-[0.68rem] text-(--ui-text-tertiary)">{t.agents.moreAgents(live.length - 3)}</p>}</>}>
|
||||
<div className="max-h-[25vh] overflow-y-auto overscroll-contain">{live.map(row)}</div>
|
||||
</StatusSection>
|
||||
{detail && <div className="max-h-[25vh] overflow-y-auto overscroll-contain px-3 py-2" data-slot="composer-subagent-detail">
|
||||
<SubagentRow node={{...detail, children: []}} nowMs={nowMs} />
|
||||
</div>}
|
||||
</div>
|
||||
return (
|
||||
<div className="composer-no-drag min-w-0" data-slot="composer-subagents">
|
||||
<StatusSection
|
||||
icon={<Codicon className="text-(--ui-purple)" name="agent" size="0.8rem" />}
|
||||
label={t.statusStack.subagents(live.length)}
|
||||
preview={
|
||||
<>
|
||||
{live.slice(0, 3).map(row)}
|
||||
{live.length > 3 && (
|
||||
<p className="px-2 text-[0.68rem] text-(--ui-text-tertiary)">{t.agents.moreAgents(live.length - 3)}</p>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="max-h-[25vh] overflow-y-auto overscroll-contain">{live.map(row)}</div>
|
||||
</StatusSection>
|
||||
{detail && (
|
||||
<div className="max-h-[25vh] overflow-y-auto overscroll-contain px-3 py-2" data-slot="composer-subagent-detail">
|
||||
{(detail.status === 'running' || detail.status === 'queued') && (
|
||||
<SubagentControls key={`${sessionId}:${detail.id}`} sessionId={sessionId} subagentId={detail.id} />
|
||||
)}
|
||||
<SubagentRow node={{ ...detail, children: [] }} nowMs={nowMs} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user