Skip to content

Commit d6f0886

Browse files
committed
fix(cli): task visual feedback, child session linking, and auto-wakeup (#109)
- Add auto-wakeup: enable Session.enableAutoWakeup() when session starts - Fix child session linking: use Bus subscription only (remove race condition) - Fix memory leak: clear maps on Esc cancel and streamResponse start - Add partIdToToolId map to track tool callIDs for metadata updates Users now see task progress immediately and results auto-deliver on completion.
1 parent 1b44b9c commit d6f0886

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ let isOperationInProgress = false
4949
let currentSessionID: string | null = null
5050
let currentModel: string | null = null
5151
let currentAgent: string | undefined
52+
let autoWakeupSessionID: string | null = null
5253

5354
// Live block for tool/task display
5455
const block = createLiveBlock()
@@ -59,6 +60,9 @@ setBlockFreeze(() => block.freeze())
5960
// Track task tool ID to child session ID mapping
6061
const taskToChildSession = new Map<string, string>()
6162

63+
// Track part IDs to tool IDs for linking metadata updates
64+
const partIdToToolId = new Map<string, string>()
65+
6266
// Store Bus subscription cleanup function
6367
let busUnsubscribe: (() => void) | undefined
6468

@@ -76,6 +80,7 @@ async function main() {
7680
})
7781

7882
function cleanup() {
83+
if (autoWakeupSessionID) Session.disableAutoWakeup(autoWakeupSessionID)
7984
if (busUnsubscribe) busUnsubscribe()
8085
block.freeze()
8186
write(cursor.show)
@@ -104,6 +109,20 @@ async function main() {
104109
const part = event.properties.part
105110
if (part.type !== "tool") return
106111

112+
// Check for task metadata updates (sessionId becomes available after session creation)
113+
if (part.tool === "task" && part.callID) {
114+
const metadata =
115+
part.state.status === "running" || part.state.status === "completed" ? part.state.metadata : undefined
116+
const sessionId = metadata?.sessionId as string | undefined
117+
if (sessionId) {
118+
const toolId = partIdToToolId.get(part.callID)
119+
if (toolId && !taskToChildSession.has(toolId)) {
120+
Panel.addChild(sessionId)
121+
taskToChildSession.set(toolId, sessionId)
122+
}
123+
}
124+
}
125+
107126
// Find which task this child session belongs to
108127
let taskId: string | null = null
109128
for (const [id, childSessionID] of taskToChildSession.entries()) {
@@ -168,6 +187,8 @@ async function main() {
168187
if (key.name === "escape" && isOperationInProgress && currentSessionID) {
169188
block.freeze()
170189
SessionPrompt.cancel(currentSessionID)
190+
taskToChildSession.clear()
191+
partIdToToolId.clear()
171192
isOperationInProgress = false
172193
currentSessionID = null
173194
write("\n")
@@ -396,6 +417,7 @@ interface StreamOptions {
396417
async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamOptions) {
397418
block.reset()
398419
taskToChildSession.clear()
420+
partIdToToolId.clear()
399421
const spinner = new Spinner("Thinking")
400422
spinner.start()
401423

@@ -424,6 +446,11 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
424446
for await (const chunk of source) {
425447
if (chunk.type === "start" && chunk.sessionID && !currentSessionID) {
426448
currentSessionID = chunk.sessionID
449+
if (autoWakeupSessionID && autoWakeupSessionID !== currentSessionID) {
450+
Session.disableAutoWakeup(autoWakeupSessionID)
451+
}
452+
Session.enableAutoWakeup(currentSessionID)
453+
autoWakeupSessionID = currentSessionID
427454
}
428455

429456
if (first && chunk.type !== "done" && chunk.type !== "start") {
@@ -475,7 +502,10 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
475502

476503
const label = dedupCount > 1 ? `${summary} (×${dedupCount})` : summary
477504
block.toolStart(lastToolId, tool, label)
478-
if (chunk.callID) idMap.set(chunk.callID, lastToolId)
505+
if (chunk.callID) {
506+
idMap.set(chunk.callID, lastToolId)
507+
partIdToToolId.set(chunk.callID, lastToolId)
508+
}
479509

480510
if (tool === "task" && chunk.input && typeof chunk.input === "object") {
481511
const input = chunk.input as TaskInput
@@ -484,11 +514,6 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
484514
if (agent && description) {
485515
block.taskStart(lastToolId, agent, description)
486516
}
487-
const childSessionID = typeof chunk.metadata?.sessionId === "string" ? chunk.metadata.sessionId : undefined
488-
if (childSessionID) {
489-
Panel.addChild(childSessionID)
490-
taskToChildSession.set(lastToolId, childSessionID)
491-
}
492517
}
493518
lastChunkType = "tool"
494519
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const theme = {
3636
// Text content
3737
prose: {
3838
text: "", // No color override, use terminal default
39+
background: "\x1b[48;5;236m", // Subtle dark gray background for response area
3940
},
4041

4142
// Status indicators

‎packages/opencode/src/tool/rg.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ Parameters:
3535
throw new Error("pattern is required when files_only is false")
3636
}
3737

38-
if (params.files_only && !params.pattern && !params.include) {
39-
throw new Error("files_only mode requires either 'pattern' or 'include' to specify which files to list")
38+
if (params.files_only) {
39+
const hasPattern = params.pattern && params.pattern.trim().length > 0
40+
const hasInclude = params.include && params.include.trim().length > 0
41+
if (!hasPattern && !hasInclude) {
42+
throw new Error("files_only mode requires non-empty 'pattern' or 'include'")
43+
}
4044
}
4145

4246
await ctx.ask({

0 commit comments

Comments
 (0)