Skip to content

Commit 450bee8

Browse files
committed
fix(cli): task rendering and init progress improvements (#103, #105)
- Show init progress: "Loading plugins" → "Loading providers" → etc. - Fix task rendering: tasks now show inline with other tools - Fix taskToChildSession memory leak: clear on stream reset - Remove premature taskToChildSession.delete() that broke child tool routing
1 parent 082078e commit 450bee8

4 files changed

Lines changed: 161 additions & 138 deletions

File tree

‎packages/opencode/src/cli/bootstrap.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { InstanceBootstrap } from "../project/bootstrap"
22
import { Instance } from "../project/instance"
33

4-
export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
4+
export async function bootstrap<T>(directory: string, cb: () => Promise<T>, onProgress?: (step: string) => void) {
55
return Instance.provide({
66
directory,
7-
init: InstanceBootstrap,
7+
init: () => InstanceBootstrap(onProgress),
88
fn: async () => {
99
try {
1010
const result = await cb()

‎packages/opencode/src/cli/lite/index.ts‎

Lines changed: 122 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -89,145 +89,157 @@ async function main() {
8989
setup.start()
9090

9191
try {
92-
await bootstrap(process.cwd(), async () => {
93-
// Subscribe to Bus events for child session tools
94-
// This must be inside bootstrap so Instance.provide() context is established
95-
busUnsubscribe = Bus.subscribe(MessageV2.Event.PartUpdated, (event) => {
96-
const part = event.properties.part
97-
if (part.type !== "tool") return
98-
99-
// Find which task this child session belongs to
100-
let taskId: string | null = null
101-
for (const [id, childSessionID] of taskToChildSession.entries()) {
102-
if (childSessionID === part.sessionID) {
103-
taskId = id
104-
break
92+
await bootstrap(
93+
process.cwd(),
94+
async () => {
95+
// Subscribe to Bus events for child session tools
96+
// This must be inside bootstrap so Instance.provide() context is established
97+
busUnsubscribe = Bus.subscribe(MessageV2.Event.PartUpdated, (event) => {
98+
const part = event.properties.part
99+
if (part.type !== "tool") return
100+
101+
// Find which task this child session belongs to
102+
let taskId: string | null = null
103+
for (const [id, childSessionID] of taskToChildSession.entries()) {
104+
if (childSessionID === part.sessionID) {
105+
taskId = id
106+
break
107+
}
105108
}
106-
}
107-
108-
if (!taskId) return
109-
110-
// Update task child tool display
111-
if (part.state.status === "running") {
112-
const arg = summarizeInput(part.tool, part.state.input)
113-
block.setTaskChildTool(taskId, part.tool, arg || "")
114-
} else if (part.state.status === "completed" || part.state.status === "error") {
115-
block.clearTaskChildTool(taskId)
116-
}
117-
})
118-
await Promise.all([Provider.list(), Agent.list(), MCP.clients(), Command.list()])
119109

120-
setup.stop(true)
121-
write(`${fg.green}✓${style.reset} Ready\n\n`)
110+
if (!taskId) return
122111

123-
currentAgent = await Agent.defaultAgent()
124-
const agentList = await Agent.list()
112+
// Update task child tool display
113+
if (part.state.status === "running") {
114+
const arg = summarizeInput(part.tool, part.state.input)
115+
block.setTaskChildTool(taskId, part.tool, arg || "")
116+
} else if (part.state.status === "completed" || part.state.status === "error") {
117+
block.clearTaskChildTool(taskId)
118+
}
119+
})
125120

126-
write(`${fg.gray}Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${style.reset}\n\n`)
121+
setup.update("Loading providers")
122+
await Provider.list()
123+
setup.update("Loading agents")
124+
await Agent.list()
125+
setup.update("Connecting to MCP servers")
126+
await MCP.clients()
127+
setup.update("Loading commands")
128+
await Command.list()
127129

128-
const editor = new LineEditor()
129-
process.stdin.setRawMode(true)
130-
process.stdin.resume()
130+
setup.stop(true)
131+
write(`${fg.green}✓${style.reset} Ready\n\n`)
131132

132-
editor.render(renderPrompt(currentAgent))
133+
currentAgent = await Agent.defaultAgent()
134+
const agentList = await Agent.list()
133135

134-
process.stdin.on("data", async (data: Buffer) => {
135-
const key = parseKey(data)
136+
write(`${fg.gray}Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${style.reset}\n\n`)
136137

137-
if (key.ctrl && key.name === "c") {
138-
cleanup()
139-
process.exit(0)
140-
}
138+
const editor = new LineEditor()
139+
process.stdin.setRawMode(true)
140+
process.stdin.resume()
141141

142-
if (key.name === "shift_tab") {
143-
const available = agentList.filter((a) => a.mode !== "subagent" && !a.hidden)
144-
const index = available.findIndex((a) => a.name === currentAgent)
145-
const next = index === -1 ? 0 : (index + 1) % available.length
146-
currentAgent = available[next].name
147-
write(`\r${clear.line}`)
148-
editor.render(renderPrompt(currentAgent))
149-
return
150-
}
142+
editor.render(renderPrompt(currentAgent))
151143

152-
if (key.name === "escape" && isOperationInProgress && currentSessionID) {
153-
block.freeze()
154-
SessionPrompt.cancel(currentSessionID)
155-
isOperationInProgress = false
156-
currentSessionID = null
157-
write("\n")
158-
editor.line = ""
159-
editor.cursor = 0
160-
editor.render(renderPrompt(currentAgent))
161-
return
162-
}
144+
process.stdin.on("data", async (data: Buffer) => {
145+
const key = parseKey(data)
163146

164-
if (isOperationInProgress) {
165-
return
166-
}
147+
if (key.ctrl && key.name === "c") {
148+
cleanup()
149+
process.exit(0)
150+
}
167151

168-
if (key.ctrl && key.name === "x") {
169-
Panel.enterNavigationMode()
170-
return
171-
}
152+
if (key.name === "shift_tab") {
153+
const available = agentList.filter((a) => a.mode !== "subagent" && !a.hidden)
154+
const index = available.findIndex((a) => a.name === currentAgent)
155+
const next = index === -1 ? 0 : (index + 1) % available.length
156+
currentAgent = available[next].name
157+
write(`\r${clear.line}`)
158+
editor.render(renderPrompt(currentAgent))
159+
return
160+
}
172161

173-
if (Panel.isInNavigationMode()) {
174-
if (key.name === "escape") {
175-
Panel.exitNavigationMode()
162+
if (key.name === "escape" && isOperationInProgress && currentSessionID) {
163+
block.freeze()
164+
SessionPrompt.cancel(currentSessionID)
165+
isOperationInProgress = false
166+
currentSessionID = null
167+
write("\n")
168+
editor.line = ""
169+
editor.cursor = 0
176170
editor.render(renderPrompt(currentAgent))
177171
return
178172
}
179173

180-
let direction: "left" | "right" | "up" | null = null
174+
if (isOperationInProgress) {
175+
return
176+
}
181177

182-
if (key.name === "left" || key.name === "h") {
183-
direction = "left"
184-
} else if (key.name === "right" || key.name === "l") {
185-
direction = "right"
186-
} else if (key.name === "up" || key.name === "k") {
187-
direction = "up"
178+
if (key.ctrl && key.name === "x") {
179+
Panel.enterNavigationMode()
180+
return
188181
}
189182

190-
if (direction) {
191-
const navigated = Panel.navigate(direction)
183+
if (Panel.isInNavigationMode()) {
184+
if (key.name === "escape") {
185+
Panel.exitNavigationMode()
186+
editor.render(renderPrompt(currentAgent))
187+
return
188+
}
189+
190+
let direction: "left" | "right" | "up" | null = null
192191

193-
if (navigated) {
194-
await Panel.renderPanel()
195-
const hint = Panel.getHint()
196-
if (hint) {
197-
write(`\n${hint}\n`)
192+
if (key.name === "left" || key.name === "h") {
193+
direction = "left"
194+
} else if (key.name === "right" || key.name === "l") {
195+
direction = "right"
196+
} else if (key.name === "up" || key.name === "k") {
197+
direction = "up"
198+
}
199+
200+
if (direction) {
201+
const navigated = Panel.navigate(direction)
202+
203+
if (navigated) {
204+
await Panel.renderPanel()
205+
const hint = Panel.getHint()
206+
if (hint) {
207+
write(`\n${hint}\n`)
208+
}
209+
write(clear.screen)
210+
write(cursor.home)
198211
}
199-
write(clear.screen)
200-
write(cursor.home)
201212
}
213+
214+
Panel.exitNavigationMode()
215+
editor.render(renderPrompt(currentAgent))
216+
return
202217
}
203218

204-
Panel.exitNavigationMode()
205-
editor.render(renderPrompt(currentAgent))
206-
return
207-
}
219+
if (key.ctrl && key.name === "t") {
220+
block.toggleTasksVisible()
221+
editor.render(renderPrompt(currentAgent))
222+
}
208223

209-
if (key.ctrl && key.name === "t") {
210-
block.toggleTasksVisible()
211-
editor.render(renderPrompt(currentAgent))
212-
}
224+
const result = editor.handle(key)
213225

214-
const result = editor.handle(key)
226+
if (result !== null) {
227+
write("\n")
215228

216-
if (result !== null) {
217-
write("\n")
229+
if (result.startsWith("/")) {
230+
await handleCommand(result)
231+
} else if (result.trim()) {
232+
await handleMessage(result)
233+
}
218234

219-
if (result.startsWith("/")) {
220-
await handleCommand(result)
221-
} else if (result.trim()) {
222-
await handleMessage(result)
235+
editor.render(renderPrompt(currentAgent))
236+
} else {
237+
editor.render(renderPrompt(currentAgent))
223238
}
224-
225-
editor.render(renderPrompt(currentAgent))
226-
} else {
227-
editor.render(renderPrompt(currentAgent))
228-
}
229-
})
230-
})
239+
})
240+
},
241+
(step) => setup.update(step),
242+
)
231243
} catch (err) {
232244
setup.stop(false)
233245
write(cursor.show)
@@ -377,6 +389,7 @@ interface StreamOptions {
377389

378390
async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamOptions) {
379391
block.reset()
392+
taskToChildSession.clear()
380393
const spinner = new Spinner("Thinking")
381394
spinner.start()
382395

@@ -520,7 +533,6 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
520533
taskToChildSession.set(id, childSessionID)
521534
}
522535
block.taskEnd(id)
523-
taskToChildSession.delete(id)
524536
}
525537
}
526538

‎packages/opencode/src/cli/lite/liveblock.ts‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,34 @@ export function createLiveBlock() {
4242
const cols = process.stdout.columns || 80
4343
const lines: string[] = []
4444

45-
// Render active + recently completed tools (skip "task" tools as they're rendered separately)
45+
// Render active + recently completed tools (tasks now render inline with tools)
4646
for (const tool of tools.values()) {
47-
if (tool.name === "task") continue
47+
if (tool.name === "task") {
48+
// Render tasks with distinctive styling
49+
const task = tasks.get(tool.id)
50+
if (!task) continue
51+
52+
const colors = task.status === "running" ? theme.task.running : theme.task.done
53+
const spinner =
54+
task.status === "running" ? `${colors.icon}${frames[frame]}${style.reset}` : `${colors.icon}✓${style.reset}`
55+
const elapsed = task.status === "running" ? Math.floor((Date.now() - task.startTime) / 1000) : task.elapsed
56+
const time = elapsed > 0 ? ` ${fg.gray}(${elapsed}s)${style.reset}` : ""
57+
lines.push(
58+
` ${spinner} ${colors.text}task${style.reset} ${colors.text}@${task.agent}:${style.reset} ${task.description}${time}`,
59+
)
60+
61+
// Render nested child tool if present
62+
if (task.status === "running" && task.childTool) {
63+
const childColors = theme.tool.running
64+
const childIcon = `${childColors.icon}◇${style.reset}`
65+
const maxLen = Math.max(0, cols - task.childTool.name.length - 15)
66+
const showEllipsis = task.childTool.summary.length > maxLen
67+
const summary = showEllipsis ? `${task.childTool.summary.slice(0, maxLen)}…` : task.childTool.summary
68+
lines.push(` └─ ${childIcon} ${childColors.text}${task.childTool.name} ${summary}${style.reset}`)
69+
}
70+
continue
71+
}
72+
4873
const sep = tool.summary ? " " : ""
4974
const maxLen = Math.max(0, cols - tool.name.length - 6)
5075
const showEllipsis = tool.summary.length > maxLen
@@ -69,26 +94,6 @@ export function createLiveBlock() {
6994
}
7095
}
7196

72-
// Render running tasks
73-
for (const task of tasks.values()) {
74-
const colors = task.status === "running" ? theme.task.running : theme.task.done
75-
const spinner =
76-
task.status === "running" ? `${colors.icon}${frames[frame]}${style.reset}` : `${colors.icon}✓${style.reset}`
77-
const elapsed = task.status === "running" ? Math.floor((Date.now() - task.startTime) / 1000) : task.elapsed
78-
const time = elapsed > 0 ? ` ${fg.gray}(${elapsed}s)${style.reset}` : ""
79-
lines.push(` ${spinner} ${colors.text}@${task.agent}:${style.reset} ${task.description}${time}`)
80-
81-
// Render nested child tool if present
82-
if (task.status === "running" && task.childTool) {
83-
const childColors = theme.tool.running
84-
const childIcon = `${childColors.icon}◇${style.reset}`
85-
const maxLen = Math.max(0, cols - task.childTool.name.length - 15)
86-
const showEllipsis = task.childTool.summary.length > maxLen
87-
const summary = showEllipsis ? `${task.childTool.summary.slice(0, maxLen)}…` : task.childTool.summary
88-
lines.push(` └─ ${childIcon} ${childColors.text}${task.childTool.name} ${summary}${style.reset}`)
89-
}
90-
}
91-
9297
// Render todos if any
9398
if (todos.length > 0 && !tasksVisible) {
9499
lines.push(` ${fg.gray}(${todos.length} tasks - ctrl+t to show)${style.reset}`)
@@ -150,6 +155,7 @@ export function createLiveBlock() {
150155

151156
taskStart(id: string, agent: string, description: string, childSessionID?: string) {
152157
tasks.set(id, { id, agent, description, elapsed: 0, status: "running", startTime: Date.now(), childSessionID })
158+
tools.set(id, { id, name: "task", summary: `${agent}: ${description}`, status: "running" })
153159
if (!active) {
154160
active = true
155161
startAnimation()
@@ -163,6 +169,7 @@ export function createLiveBlock() {
163169
task.elapsed = Math.floor((Date.now() - task.startTime) / 1000)
164170
task.status = "done"
165171
task.childTool = undefined
172+
tools.set(id, { id, name: "task", summary: `${task.agent}: ${task.description}`, status: "done" })
166173
render()
167174
}
168175
},

‎packages/opencode/src/project/bootstrap.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ import { ShareNext } from "@/share/share-next"
1414
import { Snapshot } from "../snapshot"
1515
import { Truncate } from "../tool/truncation"
1616

17-
export async function InstanceBootstrap() {
17+
export async function InstanceBootstrap(onProgress?: (step: string) => void) {
1818
Log.Default.info("bootstrapping", { directory: Instance.directory })
19+
onProgress?.("Loading plugins")
1920
await Plugin.init()
20-
Share.init()
21-
ShareNext.init()
22-
Format.init()
21+
onProgress?.("Initializing LSP")
2322
await LSP.init()
23+
onProgress?.("Initializing file system")
2424
FileWatcher.init()
2525
File.init()
26+
onProgress?.("Initializing components")
27+
Share.init()
28+
ShareNext.init()
29+
Format.init()
2630
Vcs.init()
2731
Snapshot.init()
2832
Truncate.init()

0 commit comments

Comments
 (0)