The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): align subagent hydration with owned roster contract
Tekniumc2a0a188c194
diff --git a/apps/desktop/src/app/chat/composer/status-stack/subagent-hydration.test.tsx b/apps/desktop/src/app/chat/composer/status-stack/subagent-hydration.test.tsxindex 8ce59a6d33c..4f8f47929ee 100644--- a/apps/desktop/src/app/chat/composer/status-stack/subagent-hydration.test.tsx+++ b/apps/desktop/src/app/chat/composer/status-stack/subagent-hydration.test.tsx@@ -25,12 +25,14 @@ afterEach(() => {})it('hydrates the empty owner composer and exposes an extended owner-routed transcript without leaking on session change', async () => {- const text = 'Full transcript line with preserved whitespace\n'.repeat(100)+ const text = ' Full transcript\tline with preserved whitespace \n\n'.repeat(100)const request = vi.spyOn(gateway, 'requestGatewayForAgent').mockImplementation(async (_c, _p, method) => {if (method === 'subagent.list') {return {- subagents: [{ subagent_id: 'worker', goal: 'Recovered work', started_at: 1000, status: 'running' }],+ subagents: [+ { subagent_id: 'worker', goal: 'Recovered work', started_at: 1000, status: 'running', last_tool: 'read_file' }+ ],delegations: []} as never}@@ -51,6 +53,8 @@ it('hydrates the empty owner composer and exposes an extended owner-routed trans)await screen.findByText('Recovered work')+ expect(screen.getByText('Read File')).toBeTruthy()+ expect(request).toHaveBeenCalledWith('remote-owner', 'research', 'subagent.list', { session_id: 'parent' })expect($subagentsBySession.get().parent[0].startedAt).toBe(1000000)fireEvent.click(screen.getByRole('button', { name: /Recovered work/ }))await waitFor(() => expect(document.querySelector('[data-slot="subagent-transcript"]')?.textContent).toContain(text))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.tsxindex 230cc7fdfb3..e9c3410ab79 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@@ -79,19 +79,15 @@ export function SubagentSection({ sessionId }: SubagentSectionProps) {</StatusSection>{detail && (<div className="max-h-[25vh] overflow-y-auto overscroll-contain px-3 py-2" data-slot="composer-subagent-detail">- {!detail.id.startsWith('delegation:') && (- <SubagentControls- key={`${sessionId}:${detail.id}`}- sessionId={sessionId}- setText={text => setDrafts(previous => ({ ...previous, [detail.id]: text }))}- subagentId={detail.id}- text={drafts[detail.id] ?? ''}- />- )}+ <SubagentControls+ key={`${sessionId}:${detail.id}`}+ sessionId={sessionId}+ setText={text => setDrafts(previous => ({ ...previous, [detail.id]: text }))}+ subagentId={detail.id}+ text={drafts[detail.id] ?? ''}+ /><SubagentRow node={{ ...detail, children: [] }} nowMs={nowMs} />- {!detail.id.startsWith('delegation:') && (- <SubagentTranscript key={`tail:${sessionId}:${detail.id}`} sessionId={sessionId} subagentId={detail.id} />- )}+ <SubagentTranscript key={`tail:${sessionId}:${detail.id}`} sessionId={sessionId} subagentId={detail.id} /></div>)}</div>diff --git a/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts b/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.tsindex 8550121423a..191f60f130b 100644--- a/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts+++ b/apps/desktop/src/app/chat/composer/status-stack/use-subagent-snapshot.ts@@ -31,7 +31,7 @@ export function useSubagentSnapshot(sessionId: string | null) {const owner = JSON.stringify(knownOwnerForSession(sessionId))try {- const snapshot = await requestForOwnedSession<{ subagents: SubagentPayload[]; delegations: SubagentPayload[] }>(+ const snapshot = await requestForOwnedSession<{ subagents: SubagentPayload[] }>(sessionId,rejectUnownedSubagentRequest,'subagent.list',@@ -44,7 +44,7 @@ export function useSubagentSnapshot(sessionId: string | null) {before === $subagentsBySession.get()[sessionId] &&Array.isArray(snapshot.subagents)) {- reconcileSubagentSnapshot(sessionId, snapshot.subagents, snapshot.delegations)+ reconcileSubagentSnapshot(sessionId, snapshot.subagents)}failures = 0diff --git a/apps/desktop/src/store/subagent-snapshot.test.ts b/apps/desktop/src/store/subagent-snapshot.test.tsindex 4605fd74e01..1533bc0b024 100644--- a/apps/desktop/src/store/subagent-snapshot.test.ts+++ b/apps/desktop/src/store/subagent-snapshot.test.ts@@ -1,32 +1,31 @@import { afterEach, expect, it } from 'vitest'-import { $subagentsBySession, reconcileSubagentSnapshot } from './subagents'+import { $subagentsBySession, reconcileSubagentSnapshot, upsertSubagent } from './subagents'afterEach(() => $subagentsBySession.set({}))-it('projects unresolved running delegations once, replaces them with real workers, and retires completed units', () => {- const delegation = {- delegation_id: 'batch',- goal: 'Queued unit',- status: 'running',- dispatched_at: 1000,- subagent_ids: []- }- reconcileSubagentSnapshot('owner', [], [delegation])- expect($subagentsBySession.get().owner).toMatchObject([- { id: 'delegation:batch', goal: 'Queued unit', startedAt: 1000000 }- ])+it('hydrates last tool activity without claiming it is active and preserves stream history on refresh', () => {const child = {subagent_id: 'worker',- delegation_id: 'batch',goal: 'Actual worker',status: 'running',- started_at: 1001+ started_at: 1001,+ last_tool: 'read_file'}- reconcileSubagentSnapshot('owner', [child], [delegation])- expect($subagentsBySession.get().owner.map(row => row.id)).toEqual(['worker'])- const before = $subagentsBySession.get().owner- reconcileSubagentSnapshot('owner', [child], [delegation])- expect($subagentsBySession.get().owner).toBe(before)- reconcileSubagentSnapshot('owner', [], [{ ...delegation, status: 'completed' }])++ reconcileSubagentSnapshot('owner', [child])+ const first = $subagentsBySession.get().owner+ expect(first[0].stream).toMatchObject([{ kind: 'tool', text: 'Read File' }])+ expect(first[0].currentTool).toBeUndefined()+ expect(first[0].startedAt).toBe(child.started_at * 1000)+ reconcileSubagentSnapshot('owner', [child])+ expect($subagentsBySession.get().owner).toBe(first)+ upsertSubagent('other', { subagent_id: 'worker', goal: 'Other owner' })+ const other = $subagentsBySession.get().other+ upsertSubagent('owner', { subagent_id: 'worker', text: 'New progress' }, false, 'subagent.progress')+ const stream = $subagentsBySession.get().owner[0].stream+ reconcileSubagentSnapshot('owner', [child])+ expect($subagentsBySession.get().owner[0].stream).toBe(stream)+ reconcileSubagentSnapshot('owner', [])expect($subagentsBySession.get().owner).toEqual([])+ expect($subagentsBySession.get().other).toBe(other)})diff --git a/apps/desktop/src/store/subagents.ts b/apps/desktop/src/store/subagents.tsindex fc2103e41f0..4c43c1890e5 100644--- a/apps/desktop/src/store/subagents.ts+++ b/apps/desktop/src/store/subagents.ts@@ -213,31 +213,13 @@ function toProgress(payload: SubagentPayload, prev: SubagentProgress | undefined}/** Reconcile a scoped, race-checked snapshot without replacing stream history. */-export function reconcileSubagentSnapshot(- sid: string,- children: SubagentPayload[],- delegations: SubagentPayload[] = []-) {- const represented = new Set(children.map(child => str(child.delegation_id)).filter(Boolean))-- const payloads: SubagentPayload[] = [- ...children,- ...delegations- .filter(unit => unit.status === 'running' && !represented.has(str(unit.delegation_id)) && str(unit.delegation_id))- .map(unit => ({- ...unit,- subagent_id: `delegation:${str(unit.delegation_id)}`,- started_at: unit.dispatched_at,- status: 'queued'- }))- ]-+export function reconcileSubagentSnapshot(sid: string, children: SubagentPayload[]) {const map = $subagentsBySession.get()const previous = map[sid] ?? []- const ids = new Set(payloads.map(p => str(p.subagent_id)).filter(Boolean))+ const ids = new Set(children.map(p => str(p.subagent_id)).filter(Boolean))const next = previous.filter(item => TERMINAL.has(item.status) || ids.has(item.id))- for (const payload of payloads) {+ for (const payload of children) {const id = str(payload.subagent_id)if (!id) {@@ -254,7 +236,12 @@ export function reconcileSubagentSnapshot(const projected = toProgress(payload, prev)projected.startedAt = (num(payload.started_at) ?? 0) * 1000 || prev?.startedAt || projected.startedAtprojected.updatedAt = prev?.updatedAt ?? projected.startedAt- projected.currentTool = str(payload.current_tool) || undefined++ // A roster records the last tool, not a currently executing call. Seed cold+ // activity only; a repeated snapshot must not append over newer live text.+ if (!projected.stream.length && str(payload.last_tool)) {+ projected.stream = [{ at: projected.updatedAt, kind: 'tool', text: formatTool(str(payload.last_tool)) }]+ }if (index < 0) {next.push(projected)