tui: Subagents tab lists a subagent waiting on its own background shell as inactive
Description
The Subagents tab in the composer picker shows a subagent as inactive while it waits on its own background work. A subagent that backgrounds a shell (or a subagent of its own) and ends its turn to wait is idle to the host, so the tab's "active" filter (status === "running") drops it and it shows under inactive with no status, even though its shell is running and it will wake when that ends. Same root cause as #48826, on the TUI side.
Suggested fix: derive activity from live state: running while its turn runs, waiting while a shell it started is still running (the shell registry already has this by session) or a session under it is working. The active filter takes both and the row says Waiting.
Fix with a test on my fork: https://github.com/cevr/opencode/commit/799f9b52b94644a0a95919e4d1dc75fadfa9b995 (about 50 lines in util/session.ts and subagents-tab.tsx).
Plugins
none
OpenCode version
2.2.5, still on dev @ 885df74024
Steps to reproduce
- Ask the agent to start a background subagent whose task is: run
sleep 120; echo donewith the shell tool in the background, then end your turn and wait for it. - Once the subagent says it's waiting, open the composer picker's Subagents tab.
- The subagent isn't listed as active (or is listed with no status), although its shell runs for two more minutes and it resumes afterwards.
Operating System
macOS
Terminal
Ghostty
Thanks for the detailed report. I reproduced this on 2.2.5, and it still happens on the latest dev (885df74).
Steps, using a simulated model:
- The parent starts a background
generalsubagent. - The subagent starts a background shell (
sleep 120; echo done) and ends its turn to wait for it. - With the shell still running, open the composer picker's Subagents tab. The active view shows "No active subagents".
- Switch to the inactive view with ctrl+a. The subagent is listed there with no status.
The recording and screenshots below show this, and the script below reproduces it. The filter in subagents-tab.tsx only treats a subagent as active when its status is running, and an idle subagent waiting on its own background work doesn't match.
The parent also shows "↳ General finished" while the child's shell is still running. That's the core-side problem tracked in #48826. Related reports, most closely related first:
- #48826: subagent with pending background work is reported completed early
- #62296: the subagent completion event fires at the first idle
- #56660: in the web app, the session icon shows no progress while a background shell runs
https://github.com/user-attachments/assets/5ff39cb0-4e5a-44d7-a51d-80c10d5c125a
Reproduction script (drive.ts)
// Repro for #62365: a background subagent waiting on its own background shell
// is listed as inactive in the composer picker's Subagents tab.
import { Effect, Stream } from "effect"
import { Llm, OpenCodeDriver } from "opencode-drive"
const has = (body: unknown, s: string) => JSON.stringify(body).includes(s)
export default OpenCodeDriver.use(
{
...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
tui: { recording: true, keypressOverlay: true },
tools: ["shell"],
},
({ ui, llm, tools }) =>
Effect.gen(function* () {
const shells = yield* tools.control("shell")
yield* llm.serve((req) => {
const child = has(req.body, "You are a subagent spawned")
if (child) {
if (has(req.body, "CHILD_DONE_MARK") || has(req.body, 'state=\\"completed\\"'))
return Stream.make(Llm.text("Shell finished: done. CHILD_FINAL"))
if (has(req.body, "call_shell"))
return Stream.make(Llm.text("Shell is running in the background; waiting for it to finish."))
return Stream.make(
Llm.toolCall({
index: 0,
id: "call_shell",
name: "shell",
input: { command: "sleep 120; echo done", description: "Sleep then echo", background: true },
}),
Llm.finish("tool-calls"),
)
}
if (has(req.body, "call_sub")) return Stream.make(Llm.text("Started the background subagent."))
return Stream.make(
Llm.toolCall({
index: 0,
id: "call_sub",
name: "subagent",
input: {
agent: "general",
description: "Wait on shell",
prompt: "Run `sleep 120; echo done` in the background with the shell tool, then end your turn and wait.",
background: true,
},
}),
Llm.finish("tool-calls"),
)
})
yield* ui.submit("Start a background subagent that runs a background shell and waits for it")
const shell = yield* shells.take("call_shell")
yield* ui.waitFor("General finished", { timeout: 20000 })
yield* Effect.sleep(3000)
yield* ui.screenshot("01-parent-after-spawn")
// open the subagent picker (composer Subagents tab, active filter)
yield* ui.press("down")
yield* Effect.sleep(1500)
yield* ui.screenshot("02-subagents-active-filter")
yield* ui.press("a", { ctrl: true })
yield* Effect.sleep(1500)
yield* ui.screenshot("03-subagents-inactive-filter")
yield* ui.press("escape")
// the child's shell is still running; finish it now
yield* shell.succeed({ output: "done\n", exit: 0 })
yield* Effect.sleep(3000)
yield* ui.screenshot("04-after-shell-finished")
}),
)
Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.
Additional data points from 2.2.6, in case they help scope the fix:
-
The same
status === "running"check drives two more places, so a child that waits on its own background work also drops out of them: the prompt footerN subagentscount (feature-plugins/prompt/footer.tsx:27) and the inline subagent row (routes/session/index.tsx:3308). It would be good if the "waiting" state from the fix above applied there too. -
Plugin-owned child sessions hit the same thing. We run task children from a plugin (child sessions with
parentID) and keep the task open until the child is really done, but the display shows them finished while they wait. A child can also be waiting on something other than a shell it started (for example a plugin-scheduled continuation), which the shell registry cannot see. A small server plugin surface to mark a child as waiting, or to register background work on a parent that the display andGET /api/session/activecount, would cover that case.SessionIdleBackgroundis currently only an input toexperimental.idle, and the TUI plugin API exposesdata.session.status()read-only. -
Probe on a real 2.2.6 server (mock model): a parent started a background
subagentwhose child ransleep 40in the background and ended its turn. The parent receivedstate="completed"for that subagent 0.2 s in, while the shell ran for another 40 s (core/src/session/subagent-job.ts:53-61resolves when the child's drain ends).GET /api/session/activelisted the child for neither part of the wait. This may be the server-side half that #48826 tracks.
Thanks for the detailed report! This is intended: a session (or subagent) that ends its turn and leaves a shell running in the background counts as idle, so the session list, tabs and Subagents tab don't show it as working. You can still see running shells inside the session. Closing as not planned.