Skip to content

Commit 8142494

Browse files
nftpoetristteknium1
authored andcommitted
fix(tui): render nested todo subtasks via the parent field
CLI, ACP, and the desktop app all got nested-subtask rendering (the optional `parent` field on a todo item), but the TUI never did. Its TodoItem type had no `parent` field, parseTodos() in turnController.ts dropped it even if the tool payload sent it, and TodoPanel rendered the list with a flat map() and a single fixed indent — a session using nested subtasks showed every subtask at the same visual level as its parent, with no hierarchy cue, in the terminal UI. - types.ts: add the optional `parent` field to TodoItem, matching apps/desktop/src/lib/todos.ts's TodoItem exactly. - turnController.ts: parseTodos() now preserves parent (trimmed, dropped if empty or self-referential), the same normalization desktop's parseArray() applies. - lib/todo.ts: port todoTree() from apps/desktop/src/lib/todos.ts verbatim — same DFS-with-depth algorithm, same dangling/cycle handling, so both surfaces render identical hierarchy from the same `parent` field. - todoPanel.tsx: render todoTree(todos) instead of a flat map(), with per-row indentation scaled by depth (capped at 4 levels, mirroring desktop's status-row.tsx cap).
1 parent 1a47a36 commit 8142494

6 files changed

Lines changed: 156 additions & 9 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { beforeEach, describe, expect, it } from 'vitest'
2+
3+
import { turnController } from '../app/turnController.js'
4+
import { getTurnState, resetTurnState } from '../app/turnStore.js'
5+
6+
// turnController.recordTodos() parses the raw `todo` tool payload into
7+
// TodoItem[]. Nested subtasks (apps/desktop's `parent` field) must survive
8+
// this parse — the TUI todo panel renders hierarchy from it via todoTree().
9+
describe('turnController.recordTodos — preserves the parent field', () => {
10+
beforeEach(() => {
11+
resetTurnState()
12+
turnController.fullReset()
13+
})
14+
15+
it('keeps parent on a valid nested subtask', () => {
16+
turnController.recordTodos([
17+
{ content: 'Ship feature', id: 'wp1', status: 'in_progress' },
18+
{ content: 'Write tests', id: 't1', parent: 'wp1', status: 'pending' }
19+
])
20+
21+
expect(getTurnState().todos).toEqual([
22+
{ content: 'Ship feature', id: 'wp1', status: 'in_progress' },
23+
{ content: 'Write tests', id: 't1', parent: 'wp1', status: 'pending' }
24+
])
25+
})
26+
27+
it('drops a self-referential parent instead of keeping a self-loop', () => {
28+
turnController.recordTodos([{ content: 'x', id: 'a', parent: 'a', status: 'pending' }])
29+
30+
expect(getTurnState().todos).toEqual([{ content: 'x', id: 'a', status: 'pending' }])
31+
})
32+
33+
it('omits parent entirely when absent, matching pre-nesting payloads', () => {
34+
turnController.recordTodos([{ content: 'x', id: 'a', status: 'pending' }])
35+
36+
expect(getTurnState().todos).toEqual([{ content: 'x', id: 'a', status: 'pending' }])
37+
})
38+
})

‎ui-tui/src/app/turnController.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ const parseTodos = (value: unknown): null | TodoItem[] => {
6666
return null
6767
}
6868

69+
const id = String(row.id ?? '').trim()
70+
const parent = String(row.parent ?? '').trim()
71+
6972
return {
7073
content: String(row.content ?? '').trim(),
71-
id: String(row.id ?? '').trim(),
72-
status
74+
id,
75+
status,
76+
...(parent && parent !== id ? { parent } : {})
7377
}
7478
})
7579
.filter((item): item is TodoItem => Boolean(item?.id && item.content))

‎ui-tui/src/components/todoPanel.tsx‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Box, Text } from '@hermes/ink'
22
import { memo, useState } from 'react'
33

44
import { countPendingTodos } from '../lib/liveProgress.js'
5-
import { todoGlyph, todoTone } from '../lib/todo.js'
5+
import { todoGlyph, todoTone, todoTree } from '../lib/todo.js'
66
import type { Theme } from '../theme.js'
77
import type { TodoItem } from '../types.js'
88

@@ -75,15 +75,17 @@ export const TodoPanel = memo(function TodoPanel({
7575

7676
{!effectiveCollapsed && (
7777
<Box flexDirection="column" marginLeft={2}>
78-
{todos.map(todo => {
78+
{todoTree(todos).map(([todo, depth]) => {
7979
const tone = todoTone(todo.status)
8080
const color = rowColor(t, todo.status)
8181

8282
return (
83-
<Text color={color} dim={tone === 'dim'} key={todo.id}>
84-
<Text color={color}>{todoGlyph(todo.status)} </Text>
85-
{todo.content}
86-
</Text>
83+
<Box key={todo.id} marginLeft={Math.min(depth, 4) * 2}>
84+
<Text color={color} dim={tone === 'dim'}>
85+
<Text color={color}>{todoGlyph(todo.status)} </Text>
86+
{todo.content}
87+
</Text>
88+
</Box>
8789
)
8890
})}
8991
</Box>

‎ui-tui/src/lib/todo.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22

3-
import { todoGlyph, todoTone } from './todo.js'
3+
import { todoGlyph, todoTone, todoTree } from './todo.js'
44

55
describe('todoGlyph', () => {
66
it('uses fixed-width ASCII markers so the active row does not render wide or emoji-like', () => {
@@ -19,3 +19,54 @@ describe('todoTone', () => {
1919
expect(todoTone('in_progress')).toBe('active')
2020
})
2121
})
22+
23+
describe('todoTree', () => {
24+
it('orders parents before children with depths', () => {
25+
const tree = todoTree([
26+
{ content: 'WP1', id: 'wp1', status: 'in_progress' },
27+
{ content: 'WP2', id: 'wp2', status: 'pending' },
28+
{ content: 'T1', id: 't1', parent: 'wp1', status: 'pending' },
29+
{ content: 'T2', id: 't2', parent: 'wp1', status: 'pending' }
30+
])
31+
32+
expect(tree.map(([t, d]) => [t.id, d])).toEqual([
33+
['wp1', 0],
34+
['t1', 1],
35+
['t2', 1],
36+
['wp2', 0]
37+
])
38+
})
39+
40+
it('degrades dangling and self parents to roots', () => {
41+
const tree = todoTree([
42+
{ content: 'A', id: 'a', parent: 'ghost', status: 'pending' },
43+
{ content: 'B', id: 'b', parent: 'b', status: 'pending' }
44+
])
45+
46+
expect(tree.map(([t, d]) => [t.id, d])).toEqual([
47+
['a', 0],
48+
['b', 0]
49+
])
50+
})
51+
52+
it('keeps cycle members instead of dropping them', () => {
53+
const tree = todoTree([
54+
{ content: 'A', id: 'a', parent: 'b', status: 'pending' },
55+
{ content: 'B', id: 'b', parent: 'a', status: 'pending' }
56+
])
57+
58+
expect(tree.map(([t]) => t.id).sort()).toEqual(['a', 'b'])
59+
})
60+
61+
it('flattens a todo list with no parents unchanged, all at depth 0', () => {
62+
const tree = todoTree([
63+
{ content: 'A', id: 'a', status: 'pending' },
64+
{ content: 'B', id: 'b', status: 'completed' }
65+
])
66+
67+
expect(tree.map(([t, d]) => [t.id, d])).toEqual([
68+
['a', 0],
69+
['b', 0]
70+
])
71+
})
72+
})

‎ui-tui/src/lib/todo.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,53 @@ export const todoGlyph = (status: TodoItem['status']) =>
77

88
export const todoTone = (status: TodoItem['status']): TodoTone =>
99
status === 'in_progress' ? 'active' : status === 'pending' ? 'body' : 'dim'
10+
11+
/** DFS order of a (possibly nested) todo list: [item, depth] pairs, parents
12+
* before children. Dangling/cyclic parents degrade to depth 0. Mirrors
13+
* apps/desktop/src/lib/todos.ts's todoTree() so both surfaces render the
14+
* same hierarchy from the same `parent` field. */
15+
export function todoTree(todos: readonly TodoItem[]): [TodoItem, number][] {
16+
const ids = new Set(todos.map(t => t.id))
17+
const kids = new Map<string, TodoItem[]>()
18+
const roots: TodoItem[] = []
19+
20+
for (const t of todos) {
21+
if (t.parent && ids.has(t.parent) && t.parent !== t.id) {
22+
const list = kids.get(t.parent) ?? []
23+
list.push(t)
24+
kids.set(t.parent, list)
25+
} else {
26+
roots.push(t)
27+
}
28+
}
29+
30+
const out: [TodoItem, number][] = []
31+
const seen = new Set<string>()
32+
33+
const walk = (item: TodoItem, depth: number) => {
34+
if (seen.has(item.id)) {
35+
return
36+
}
37+
38+
seen.add(item.id)
39+
out.push([item, depth])
40+
41+
for (const kid of kids.get(item.id) ?? []) {
42+
walk(kid, depth + 1)
43+
}
44+
}
45+
46+
for (const root of roots) {
47+
walk(root, 0)
48+
}
49+
50+
// Cycle members never reach a root — append them flat so nothing is lost.
51+
for (const t of todos) {
52+
if (!seen.has(t.id)) {
53+
seen.add(t.id)
54+
out.push([t, 0])
55+
}
56+
}
57+
58+
return out
59+
}

‎ui-tui/src/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface ActiveTool {
99
export interface TodoItem {
1010
content: string
1111
id: string
12+
/** Optional id of another item — renders this as a nested subtask. */
13+
parent?: string
1214
status: 'cancelled' | 'completed' | 'in_progress' | 'pending'
1315
}
1416

0 commit comments

Comments
 (0)