From cf692532cf63f02ffe41f641e7a2808633680d93 Mon Sep 17 00:00:00 2001 From: Adolanium <94890352+Adolanium@users.noreply.github.com> Date: Sat, 29 Aug 2026 12:41:29 +0300 Subject: [PATCH] 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. --- .../status-stack/collapsed-indicator.test.tsx | 16 +++ .../session/hooks/use-message-stream/index.ts | 6 +- .../src/components/chat/status-section.tsx | 4 +- apps/desktop/src/lib/todos.test.ts | 76 ++++++++++++- apps/desktop/src/lib/todos.ts | 102 ++++++++++++++++++ 5 files changed, 198 insertions(+), 6 deletions(-) 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.tsx index 0006c4ab25..6698d16e64 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( + + + + ) + + 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.ts index 8f66477a6c..241a923fc4 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.tsx index bdbead9b33..4fbf315220 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?: 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({ {icon && {icon}} {label} - {collapsed && collapsedIndicator && {collapsedIndicator}} + {collapsedIndicator && {collapsedIndicator}} {accessory &&
{accessory}
} diff --git a/apps/desktop/src/lib/todos.test.ts b/apps/desktop/src/lib/todos.test.ts index 4e0963159f..d6d69f20d3 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.ts index d12f9de370..05dcc77d33 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 => 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[] {