Skip to content

Commit 799f9b5

Browse files
committed
fix(tui): the Subagents panel lists a subagent waiting on its own running work as active
A subagent that backgrounds a shell (or a subagent of its own) and ends its turn is idle to the host, so the panel listed it as inactive while its work ran. sessionActivity reads it from live state only: running while its turn runs, waiting while a shell it started still runs (the shell registry) or a session under it works. The panel's active filter takes both and says Waiting.
1 parent bb16af5 commit 799f9b5

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

‎packages/tui/src/routes/session/composer/subagents-tab.tsx‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import { Locale } from "../../../util/locale"
1212
import { Keymap } from "../../../context/keymap"
1313
import { useComposerTab } from "./context"
1414
import { withTimestampedFallback } from "@opencode/util/session-title-fallback"
15-
import { sessionFamily } from "../../../util/session"
15+
import { sessionActivity, sessionFamily, type SessionActivity } from "../../../util/session"
1616

1717
interface SubagentEntry {
1818
sessionID: string
1919
agent: string
2020
title: string
21-
status: string
21+
activity: SessionActivity | undefined
2222
current: boolean
2323
prefix: string
2424
}
@@ -42,7 +42,11 @@ export function SubagentsTab(props: { sessionID: string }) {
4242
const current = session()
4343
if (!current) return []
4444

45-
const result = sessionFamily<SessionInfo>(data.session.list(), current.id).map(
45+
const sessions = data.session.list()
46+
const activity = sessionActivity(sessions, data.session.status, (sessionID) =>
47+
data.shell.listBySession(sessionID).filter((shell) => shell.status === "running").length,
48+
)
49+
const result = sessionFamily<SessionInfo>(sessions, current.id).map(
4650
({ session, prefix }): SubagentEntry => {
4751
const title = withTimestampedFallback(session)
4852
const agentMatch = title.match(/@(\w+) subagent/)
@@ -54,14 +58,15 @@ export function SubagentsTab(props: { sessionID: string }) {
5458
? Locale.titlecase(agentMatch[1])
5559
: "Subagent",
5660
title: agentMatch ? title.replace(agentMatch[0], "").trim() || title : title,
57-
status: data.session.status(session.id),
61+
activity: activity(session.id),
5862
current: session.id === route.sessionID,
5963
prefix,
6064
}
6165
},
6266
)
6367

64-
return result.filter((entry) => (filter.active ? entry.status === "running" : entry.status !== "running"))
68+
// Active: its turn runs, or it waits on a shell or subagent of its own that still runs.
69+
return result.filter((entry) => (entry.activity !== undefined) === filter.active)
6570
})
6671

6772
let selectedSessionID = ""
@@ -122,7 +127,7 @@ export function SubagentsTab(props: { sessionID: string }) {
122127
hints: () => {
123128
const entry = selectedEntry()
124129
return [
125-
...(entry?.status === "running"
130+
...(entry?.activity === "running"
126131
? [{ label: "interrupt", shortcut: shortcuts.get("composer.subagent.interrupt") ?? "" }]
127132
: []),
128133
{
@@ -188,7 +193,7 @@ export function SubagentsTab(props: { sessionID: string }) {
188193
group: "Composer",
189194
run() {
190195
const entry = selectedEntry()
191-
if (!entry || entry.status !== "running") return
196+
if (!entry || entry.activity !== "running") return
192197
void client.api.session.interrupt({ sessionID: entry.sessionID })
193198
},
194199
},
@@ -206,7 +211,8 @@ export function SubagentsTab(props: { sessionID: string }) {
206211
{(entry, index) => {
207212
const active = createMemo(() => index() === store.selected)
208213
const status = createMemo(() => {
209-
if (entry.status === "running") return "Running"
214+
if (entry.activity === "running") return "Running"
215+
if (entry.activity === "waiting") return "Waiting"
210216
return ""
211217
})
212218
return (

‎packages/tui/src/util/session.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ export function sessionFamily<T extends SessionNode>(sessions: readonly T[], ses
4343
return walk(root(current).id, [])
4444
}
4545

46+
export type SessionActivity = "running" | "waiting"
47+
48+
/**
49+
* How each session works now, from live state only: `running` while its turn runs, `waiting` while
50+
* it is idle yet a shell it started still runs or a session under it works. Undefined for one that
51+
* does not. A background call whose completion notice never arrives keeps nothing working.
52+
*/
53+
export function sessionActivity<T extends SessionNode>(
54+
sessions: readonly T[],
55+
status: (sessionID: string) => "idle" | "running",
56+
runningShells: (sessionID: string) => number,
57+
) {
58+
const children = new Map<string, string[]>()
59+
sessions.forEach((session) => {
60+
if (session.parentID) children.set(session.parentID, [...(children.get(session.parentID) ?? []), session.id])
61+
})
62+
const known = new Map<string, SessionActivity | undefined>()
63+
function of(sessionID: string, path: ReadonlySet<string>): SessionActivity | undefined {
64+
if (known.has(sessionID)) return known.get(sessionID)
65+
if (path.has(sessionID)) return undefined
66+
const inner = new Set([...path, sessionID])
67+
const activity: SessionActivity | undefined =
68+
status(sessionID) === "running"
69+
? "running"
70+
: runningShells(sessionID) > 0 || (children.get(sessionID) ?? []).some((child) => of(child, inner) !== undefined)
71+
? "waiting"
72+
: undefined
73+
known.set(sessionID, activity)
74+
return activity
75+
}
76+
return (sessionID: string) => of(sessionID, new Set())
77+
}
78+
4679
export function contextUsage(
4780
messages: ReadonlyArray<SessionMessageInfo>,
4881
models: ReadonlyArray<ModelInfo> | undefined,

‎packages/tui/test/util/session.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2-
import { sessionFamily } from "../../src/util/session"
2+
import { sessionActivity, sessionFamily } from "../../src/util/session"
33

44
describe("util.session", () => {
55
test("flattens nested subagents from any session in the family", () => {
@@ -22,4 +22,39 @@ describe("util.session", () => {
2222
{ session: sessions[6], prefix: "└─ " },
2323
])
2424
})
25+
26+
test("a session works while its turn runs, or while a shell it started or a session under it still works", () => {
27+
const sessions = [
28+
{ id: "root" },
29+
{ id: "run", parentID: "root" },
30+
{ id: "shell", parentID: "root" },
31+
{ id: "parent", parentID: "root" },
32+
{ id: "grandchild", parentID: "parent" },
33+
{ id: "done", parentID: "root" },
34+
{ id: "done-child", parentID: "done" },
35+
]
36+
const activity = sessionActivity(
37+
sessions,
38+
(id) => (id === "run" || id === "grandchild" ? "running" : "idle"),
39+
(id) => (id === "shell" ? 1 : 0),
40+
)
41+
expect(sessions.map((session) => [session.id, activity(session.id)])).toEqual([
42+
["root", "waiting"],
43+
["run", "running"],
44+
["shell", "waiting"],
45+
["parent", "waiting"],
46+
["grandchild", "running"],
47+
["done", undefined],
48+
["done-child", undefined],
49+
])
50+
})
51+
52+
test("a cycle in the parents never loops", () => {
53+
const sessions = [
54+
{ id: "a", parentID: "b" },
55+
{ id: "b", parentID: "a" },
56+
]
57+
expect(sessionActivity(sessions, () => "idle", () => 0)("a")).toBeUndefined()
58+
expect(sessionActivity(sessions, () => "idle", (id) => (id === "b" ? 1 : 0))("a")).toBe("waiting")
59+
})
2560
})

0 commit comments

Comments
 (0)