fix(desktop): merge todo patches instead of replacing the Tasks list

tool.start for a merge:true todo write used args.todos as a full replace. A one-item status patch became Tasks 1/1, or vanished if content was omitted, so the panel looked stuck at 0/5 until the final complete result. Apply merge by id on start, keep replace for the full result, and show the in-progress spinner on the expanded header too.
This commit is contained in:
Adolanium
2026-08-29 12:41:29 +03:00
committed by Teknium
parent e298dcce49
commit cf692532cf
5 changed files with 198 additions and 6 deletions
@@ -22,6 +22,22 @@ describe('ComposerStatusStack collapsed todo indicator', () => {
$todosBySession.set({})
})
it('shows a running indicator while the todo group is expanded', () => {
$todosBySession.set({
'session-1': [{ content: 'Wire the status stack', id: '1', status: 'in_progress' }]
})
render(
<MemoryRouter>
<ComposerStatusStack queue={null} sessionId="session-1" />
</MemoryRouter>
)
expect(screen.getByText('Wire the status stack')).toBeTruthy()
expect(screen.getAllByRole('status').length).toBeGreaterThan(0)
expect(screen.getByText('Tasks 0/1')).toBeTruthy()
})
it('shows a running indicator next to the collapsed todo label', () => {
$todosBySession.set({
'session-1': [{ content: 'Wire the status stack', id: '1', status: 'in_progress' }]
@@ -23,12 +23,12 @@ import {
generatedImageEchoSources,
stripGeneratedImageEchoes
} from '@/lib/generated-images'
import { parseTodos } from '@/lib/todos'
import { nextTodosFromToolEvent } from '@/lib/todos'
import { dispatchNativeNotification } from '@/store/native-notifications'
import { isDiskFullErrorMessage, notifyError } from '@/store/notifications'
import { broadcastSessionsChanged } from '@/store/session-sync'
import { upsertSubagent } from '@/store/subagents'
import { setSessionTodos } from '@/store/todos'
import { $todosBySession, setSessionTodos } from '@/store/todos'
import type { ClientSessionState } from '../../../types'
@@ -462,7 +462,7 @@ export function useMessageStream({
// The composer status stack owns todo display now (no inline panel) —
// mirror every todo state the tool reports into its session store.
if (payload?.name === 'todo') {
const todos = parseTodos(payload.todos) ?? parseTodos(payload.result) ?? parseTodos(payload.args)
const todos = nextTodosFromToolEvent($todosBySession.get()[sessionId] ?? [], payload)
if (todos) {
setSessionTodos(sessionId, todos)
@@ -7,7 +7,7 @@ interface StatusSectionProps {
* `Button` with `size="micro"` + `variant="text"` or `"link"`. */
accessory?: ReactNode
children: ReactNode
/** Optional inline status shown only while the group is collapsed. */
/** Optional inline status next to the label (running spinner, etc). */
collapsedIndicator?: ReactNode
defaultCollapsed?: boolean
/** Optional glyph between the caret and the label (e.g. a `Codicon`). */
@@ -42,7 +42,7 @@ export function StatusSection({
<DisclosureCaret className="shrink-0" open={!collapsed} size="1em" />
{icon && <span className="flex shrink-0 items-center">{icon}</span>}
<span className="min-w-0 truncate">{label}</span>
{collapsed && collapsedIndicator && <span className="flex shrink-0 items-center">{collapsedIndicator}</span>}
{collapsedIndicator && <span className="flex shrink-0 items-center">{collapsedIndicator}</span>}
</button>
{accessory && <div className="flex shrink-0 items-center gap-1">{accessory}</div>}
</div>
+75 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { latestSessionTodos, parseTodos, todoTree } from './todos'
import { latestSessionTodos, mergeTodoItems, nextTodosFromToolEvent, parseTodoPatch, parseTodos, todoTree } from './todos'
describe('todoTree', () => {
it('orders parents before children with depths', () => {
@@ -123,3 +123,77 @@ describe('latestSessionTodos', () => {
expect(latestSessionTodos([])).toBeNull()
})
})
describe('mergeTodoItems', () => {
const list = [
{ content: 'Fix C', id: 'c', status: 'in_progress' as const },
{ content: 'Fix D', id: 'd', status: 'pending' as const },
{ content: 'Fix A', id: 'a', status: 'pending' as const }
]
it('updates status by id and keeps the rest of the list', () => {
expect(mergeTodoItems(list, [{ id: 'c', status: 'completed' }])).toEqual([
{ content: 'Fix C', id: 'c', status: 'completed' },
{ content: 'Fix D', id: 'd', status: 'pending' },
{ content: 'Fix A', id: 'a', status: 'pending' }
])
})
it('appends a new item and fills missing content', () => {
expect(mergeTodoItems(list, [{ id: 'v', status: 'pending' }])).toEqual([
...list,
{ content: '(no description)', id: 'v', status: 'pending' }
])
})
})
describe('nextTodosFromToolEvent', () => {
const current = [
{ content: 'Fix C', id: 'c', status: 'pending' as const },
{ content: 'Fix D', id: 'd', status: 'pending' as const }
]
it('replaces from the full tool result', () => {
expect(
nextTodosFromToolEvent(current, {
todos: [
{ content: 'Fix C', id: 'c', status: 'completed' },
{ content: 'Fix D', id: 'd', status: 'in_progress' }
]
})
).toEqual([
{ content: 'Fix C', id: 'c', status: 'completed' },
{ content: 'Fix D', id: 'd', status: 'in_progress' }
])
})
it('merges a status-only start payload instead of replacing the list', () => {
expect(
nextTodosFromToolEvent(current, {
args: { merge: true, todos: [{ id: 'c', status: 'completed' }] }
})
).toEqual([
{ content: 'Fix C', id: 'c', status: 'completed' },
{ content: 'Fix D', id: 'd', status: 'pending' }
])
})
it('does not wipe the list when a merge payload has no usable items', () => {
expect(nextTodosFromToolEvent(current, { args: { merge: true, todos: [] } })).toBeNull()
})
it('still replaces when merge is off', () => {
expect(
nextTodosFromToolEvent(current, {
args: { todos: [{ content: 'Only this', id: 'c', status: 'completed' }] }
})
).toEqual([{ content: 'Only this', id: 'c', status: 'completed' }])
})
})
describe('parseTodoPatch', () => {
it('keeps status-only items that parseTodos would drop', () => {
expect(parseTodos([{ id: 'c', status: 'completed' }])).toEqual([])
expect(parseTodoPatch([{ id: 'c', status: 'completed' }])).toEqual([{ id: 'c', status: 'completed' }])
})
})
+102
View File
@@ -8,6 +8,14 @@ export interface TodoItem {
status: TodoStatus
}
/** One item from a `merge: true` write. Content is optional so a status-only
* patch still applies to an existing row. */
export interface TodoPatch {
content?: string
id: string
status: TodoStatus
}
const STATUSES: readonly TodoStatus[] = ['pending', 'in_progress', 'completed', 'cancelled']
const isRecord = (v: unknown): v is Record<string, unknown> => Boolean(v && typeof v === 'object' && !Array.isArray(v))
@@ -27,6 +35,24 @@ function parseArray(value: unknown[]): TodoItem[] {
})
}
function parsePatchArray(value: unknown[]): TodoPatch[] {
return value.flatMap(item => {
if (!isRecord(item) || !isStatus(item.status)) {
return []
}
const id = String(item.id ?? '').trim()
if (!id) {
return []
}
const content = String(item.content ?? '').trim()
return content ? [{ content, id, status: item.status }] : [{ id, status: item.status }]
})
}
function parse(value: unknown, depth: number): null | TodoItem[] {
if (depth > 2) {
return null
@@ -101,6 +127,82 @@ export function todoTree(todos: readonly TodoItem[]): [TodoItem, number][] {
return out
}
function parsePatch(value: unknown, depth: number): null | TodoPatch[] {
if (depth > 2) {
return null
}
if (Array.isArray(value)) {
return parsePatchArray(value)
}
if (typeof value === 'string' && value.trim()) {
try {
return parsePatch(JSON.parse(value), depth + 1)
} catch {
return null
}
}
if (isRecord(value) && Object.hasOwn(value, 'todos')) {
return parsePatch(value.todos, depth + 1)
}
return null
}
export const parseTodoPatch = (value: unknown): null | TodoPatch[] => parsePatch(value, 0)
export const todoArgsWantMerge = (args: unknown): boolean => isRecord(args) && args.merge === true
/** Same as TodoStore.write(merge=True): update by id, append new items. */
export function mergeTodoItems(current: readonly TodoItem[], patch: readonly TodoPatch[]): TodoItem[] {
const next = current.map(item => ({ ...item }))
const indexById = new Map(next.map((item, index) => [item.id, index]))
for (const item of patch) {
const index = indexById.get(item.id)
if (index === undefined) {
next.push({ content: item.content?.trim() || '(no description)', id: item.id, status: item.status })
indexById.set(item.id, next.length - 1)
continue
}
if (item.content) {
next[index].content = item.content
}
next[index].status = item.status
}
return next
}
/** Live tool event to the next list. `payload.todos` / `result` is the full
* store, so replace. `args` with `merge: true` patches by id so a status-only
* start event does not wipe the rest of the checklist. */
export function nextTodosFromToolEvent(
current: readonly TodoItem[],
payload: { args?: unknown; arguments?: unknown; result?: unknown; todos?: unknown }
): null | TodoItem[] {
const fromResult = parseTodos(payload.todos) ?? parseTodos(payload.result)
if (fromResult) {
return fromResult
}
const args = payload.args ?? payload.arguments
if (todoArgsWantMerge(args)) {
const patch = parseTodoPatch(args)
return patch && patch.length > 0 ? mergeTodoItems(current, patch) : null
}
return parseTodos(args)
}
/** Latest parseable todo list from one message's aui content parts (tool-call
* parts named `todo`; live parts carry `todos`, hydrated ones args/result). */
export function todosFromMessageContent(content: unknown): null | TodoItem[] {