The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): merge todo patches instead of replacing the Tasks list
Adolaniumcf692532cf63
diff --git a/apps/desktop/src/app/chat/composer/status-stack/collapsed-indicator.test.tsx b/apps/desktop/src/app/chat/composer/status-stack/collapsed-indicator.test.tsxindex 0006c4ab25b..6698d16e64a 100644--- a/apps/desktop/src/app/chat/composer/status-stack/collapsed-indicator.test.tsx+++ b/apps/desktop/src/app/chat/composer/status-stack/collapsed-indicator.test.tsx@@ -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' }]diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.tsindex 8f66477a6c1..241a923fc46 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts@@ -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)diff --git a/apps/desktop/src/components/chat/status-section.tsx b/apps/desktop/src/components/chat/status-section.tsxindex bdbead9b33a..4fbf3152202 100644--- a/apps/desktop/src/components/chat/status-section.tsx+++ b/apps/desktop/src/components/chat/status-section.tsx@@ -7,7 +7,7 @@ interface StatusSectionProps {* `Button` with `size="micro"` + `variant="text"` or `"link"`. */accessory?: ReactNodechildren: ReactNode- /** Optional inline status shown only while the group is collapsed. */+ /** Optional inline status next to the label (running spinner, etc). */collapsedIndicator?: ReactNodedefaultCollapsed?: 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>diff --git a/apps/desktop/src/lib/todos.test.ts b/apps/desktop/src/lib/todos.test.tsindex 4e0963159f3..d6d69f20d3b 100644--- a/apps/desktop/src/lib/todos.test.ts+++ b/apps/desktop/src/lib/todos.test.ts@@ -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' }])+ })+})diff --git a/apps/desktop/src/lib/todos.ts b/apps/desktop/src/lib/todos.tsindex d12f9de3705..05dcc77d33c 100644--- a/apps/desktop/src/lib/todos.ts+++ b/apps/desktop/src/lib/todos.ts@@ -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[] {