Skip to content

Commit 86dd82d

Browse files
committed
fix(tui): show permission prompts from nested subagent chains
Port of upstream anomalyco#41496. The TUI, 'opencode run' bootstrap, and the live subagent reducer only looked one level deep, so permission and question asks from grandchild sessions never rendered and the whole chain hung forever (upstream anomalyco#13715, anomalyco#44747). Collect blockers from the full session subtree instead: collectSubtree BFS in the TUI session view, collectDescendantSessions in run stream transport bootstrap, and grandchild task-part registration in reduceSubagentData.
1 parent 96427b6 commit 86dd82d

7 files changed

Lines changed: 373 additions & 17 deletions

File tree

‎packages/app/src/context/permission-auto-respond.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,45 @@ describe("autoRespondsPermission", () => {
104104

105105
expect(autoRespondsPermission(autoAccept, sessions, permission("child"), directory)).toBe(true)
106106
})
107+
108+
test("inherits an ancestor's directory-scoped auto-accept across a grandchild", () => {
109+
const directory = "/tmp/project"
110+
// root → child → grandchild lineage
111+
const sessions = [
112+
session({ id: "root" }),
113+
session({ id: "child", parentID: "root" }),
114+
session({ id: "grandchild", parentID: "child" }),
115+
]
116+
const autoAccept = {
117+
[`${base64Encode(directory)}/root`]: true,
118+
}
119+
120+
expect(autoRespondsPermission(autoAccept, sessions, permission("grandchild"), directory)).toBe(true)
121+
})
122+
123+
test("inherits a root session's legacy auto-accept key from a grandchild", () => {
124+
const sessions = [
125+
session({ id: "root" }),
126+
session({ id: "child", parentID: "root" }),
127+
session({ id: "grandchild", parentID: "child" }),
128+
]
129+
130+
expect(autoRespondsPermission({ root: true }, sessions, permission("grandchild"), "/tmp/project")).toBe(true)
131+
})
132+
133+
test("requires approval for a grandchild with no ancestor override", () => {
134+
const sessions = [
135+
session({ id: "root" }),
136+
session({ id: "child", parentID: "root" }),
137+
session({ id: "grandchild", parentID: "child" }),
138+
session({ id: "unrelated" }),
139+
]
140+
const autoAccept = {
141+
unrelated: true,
142+
}
143+
144+
expect(autoRespondsPermission(autoAccept, sessions, permission("grandchild"), "/tmp/project")).toBe(false)
145+
})
107146
})
108147

109148
describe("isDirectoryAutoAccepting", () => {

‎packages/opencode/src/cli/cmd/run/stream.transport.ts‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// The tick counter prevents stale idle events from resolving the wrong turn.
1616
// We also re-check live session status before resolving an idle event so a
1717
// delayed idle from an older turn cannot complete a newer busy turn.
18-
import type { Event, GlobalEvent, OpencodeClient } from "@opencode-ai/sdk/v2"
18+
import type { Event, GlobalEvent, OpencodeClient, Session } from "@opencode-ai/sdk/v2"
1919
import { Context, Deferred, Effect, Exit, Layer, Scope, Stream } from "effect"
2020
import { makeRuntime } from "@/effect/run-service"
2121
import {
@@ -388,6 +388,42 @@ function traceTabs(trace: Trace | undefined, prev: FooterSubagentTab[], next: Fo
388388
}
389389
}
390390

391+
const collectDescendantSessions = Effect.fn("RunStreamTransport.collectDescendantSessions")(function* (
392+
sdk: OpencodeClient,
393+
rootSessionID: string,
394+
) {
395+
const visited = new Set<string>([rootSessionID])
396+
const descendants: Session[] = []
397+
let frontier = [rootSessionID]
398+
399+
while (frontier.length > 0) {
400+
const levels = yield* Effect.all(
401+
frontier.map((sessionID) =>
402+
Effect.promise(() => sdk.session.children({ sessionID })).pipe(
403+
Effect.map((item) => item.data ?? []),
404+
Effect.orElseSucceed(() => []),
405+
),
406+
),
407+
{ concurrency: "unbounded" },
408+
)
409+
410+
frontier = []
411+
for (const children of levels) {
412+
for (const child of children) {
413+
if (visited.has(child.id)) {
414+
continue
415+
}
416+
417+
visited.add(child.id)
418+
descendants.push(child)
419+
frontier.push(child.id)
420+
}
421+
}
422+
}
423+
424+
return descendants
425+
})
426+
391427
function createLayer(input: StreamInput) {
392428
return Layer.fresh(
393429
Layer.effect(
@@ -684,14 +720,7 @@ function createLayer(input: StreamInput) {
684720
: Math.max(input.replayLimit, SUBAGENT_BOOTSTRAP_LIMIT)
685721
: SUBAGENT_BOOTSTRAP_LIMIT,
686722
),
687-
Effect.promise(() =>
688-
input.sdk.session.children({
689-
sessionID: input.sessionID,
690-
}),
691-
).pipe(
692-
Effect.map((item) => item.data ?? []),
693-
Effect.orElseSucceed(() => []),
694-
),
723+
collectDescendantSessions(input.sdk, input.sessionID),
695724
Effect.promise(() => input.sdk.permission.list()).pipe(
696725
Effect.map((item) => item.data ?? []),
697726
Effect.orElseSucceed(() => []),

‎packages/opencode/src/cli/cmd/run/subagent-data.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ export function reduceSubagentData(input: {
798798
}) {
799799
const event = input.event
800800

801+
let changed = false
801802
if (event.type === "message.part.updated") {
802803
const part = event.properties.part
803804
if (part.sessionID === input.sessionID) {
@@ -807,6 +808,10 @@ export function reduceSubagentData(input: {
807808

808809
return syncTaskTab(input.data, part)
809810
}
811+
812+
if (part.type === "tool" && knownSession(input.data, part.sessionID)) {
813+
changed = syncTaskTab(input.data, part)
814+
}
810815
}
811816

812817
const sessionID =
@@ -871,6 +876,6 @@ export function reduceSubagentData(input: {
871876
event,
872877
thinking: input.thinking,
873878
limits: input.limits,
874-
}) || cancelled
879+
}) || cancelled || changed
875880
)
876881
}

‎packages/opencode/test/cli/run/stream.transport.test.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,132 @@ describe("run stream transport", () => {
13991399
}
14001400
})
14011401

1402+
test("bootstraps grandchild tabs and permissions from a nested subagent chain", async () => {
1403+
const src = eventFeed()
1404+
const ui = footer()
1405+
const transport = await createSessionTransport({
1406+
sdk: sdk({
1407+
stream: src.stream,
1408+
messages: async ({ sessionID }) => {
1409+
if (sessionID === "session-1") {
1410+
return ok([
1411+
assistantMessage({
1412+
sessionID: "session-1",
1413+
id: "msg-1",
1414+
parts: [
1415+
runningTool({
1416+
sessionID: "session-1",
1417+
messageID: "msg-1",
1418+
id: "task-1",
1419+
callID: "call-1",
1420+
tool: "task",
1421+
body: {
1422+
description: "Explore run folder",
1423+
subagent_type: "explore",
1424+
},
1425+
metadata: {
1426+
sessionId: "child-1",
1427+
},
1428+
}),
1429+
],
1430+
}),
1431+
])
1432+
}
1433+
1434+
if (sessionID === "child-1") {
1435+
return ok([
1436+
assistantMessage({
1437+
sessionID: "child-1",
1438+
id: "msg-child-1",
1439+
parts: [
1440+
runningTool({
1441+
sessionID: "child-1",
1442+
messageID: "msg-child-1",
1443+
id: "task-2",
1444+
callID: "call-2",
1445+
tool: "task",
1446+
body: {
1447+
description: "Grep for callers",
1448+
subagent_type: "explore",
1449+
},
1450+
metadata: {
1451+
sessionId: "grandchild-1",
1452+
},
1453+
}),
1454+
],
1455+
}),
1456+
])
1457+
}
1458+
1459+
return ok([
1460+
assistantMessage({
1461+
sessionID: "grandchild-1",
1462+
id: "msg-grandchild-1",
1463+
parts: [
1464+
runningTool({
1465+
sessionID: "grandchild-1",
1466+
messageID: "msg-grandchild-1",
1467+
id: "grep-1",
1468+
callID: "call-grep-1",
1469+
tool: "grep",
1470+
body: {
1471+
pattern: "callers",
1472+
},
1473+
}),
1474+
],
1475+
}),
1476+
])
1477+
},
1478+
children: async ({ sessionID }) => {
1479+
if (sessionID === "session-1") return ok([child("child-1")])
1480+
if (sessionID === "child-1") return ok([child("grandchild-1")])
1481+
return ok([])
1482+
},
1483+
permissions: async () =>
1484+
ok([
1485+
{
1486+
id: "perm-grandchild-1",
1487+
sessionID: "grandchild-1",
1488+
permission: "grep",
1489+
patterns: ["callers"],
1490+
metadata: {},
1491+
always: [],
1492+
tool: {
1493+
messageID: "msg-grandchild-1",
1494+
callID: "call-grep-1",
1495+
},
1496+
},
1497+
]),
1498+
}),
1499+
sessionID: "session-1",
1500+
thinking: true,
1501+
limits: () => ({}),
1502+
footer: ui.api,
1503+
})
1504+
1505+
try {
1506+
const boot = await waitFor(() => {
1507+
const item = ui.events.findLast((event) => event.type === "stream.subagent")
1508+
const state = item?.type === "stream.subagent" ? item.state : undefined
1509+
return state?.tabs.some((tab) => tab.sessionID === "grandchild-1") &&
1510+
state.permissions.some((req) => req.id === "perm-grandchild-1")
1511+
? state
1512+
: undefined
1513+
})
1514+
1515+
expect(boot.tabs.map((tab) => tab.sessionID).sort()).toEqual(["child-1", "grandchild-1"])
1516+
expect(boot.permissions).toEqual([
1517+
expect.objectContaining({
1518+
id: "perm-grandchild-1",
1519+
sessionID: "grandchild-1",
1520+
}),
1521+
])
1522+
} finally {
1523+
src.close()
1524+
await transport.close()
1525+
}
1526+
})
1527+
14021528
test("bootstraps child session output before selection", async () => {
14031529
const ui = footer()
14041530
const transport = await createSessionTransport({

‎packages/opencode/test/cli/run/subagent-data.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,64 @@ describe("run subagent data", () => {
544544
}),
545545
])
546546
})
547+
548+
test("registers a grandchild tab when a known child session spawns a nested task", () => {
549+
const data = createSubagentData()
550+
551+
bootstrapSubagentData({
552+
data,
553+
messages: [taskMessage("child-1", "running")],
554+
children: [{ id: "child-1" }],
555+
permissions: [],
556+
questions: [],
557+
})
558+
559+
const changed = reduce(data, {
560+
type: "message.part.updated",
561+
properties: {
562+
part: {
563+
id: "part-grandchild-1",
564+
sessionID: "child-1",
565+
messageID: "msg-child-1",
566+
type: "tool",
567+
callID: "call-grandchild-1",
568+
tool: "task",
569+
state: {
570+
status: "running",
571+
input: {
572+
description: "Grep for callers",
573+
subagent_type: "explore",
574+
},
575+
metadata: {
576+
sessionId: "grandchild-1",
577+
},
578+
time: { start: 1 },
579+
},
580+
},
581+
},
582+
})
583+
584+
expect(changed).toBe(true)
585+
586+
const tabs = snapshotSubagentData(data).tabs
587+
expect(tabs.map((tab) => tab.sessionID).sort()).toEqual(["child-1", "grandchild-1"])
588+
589+
reduce(data, {
590+
type: "permission.asked",
591+
properties: {
592+
id: "perm-grandchild-1",
593+
sessionID: "grandchild-1",
594+
permission: "grep",
595+
patterns: ["callers"],
596+
metadata: {},
597+
always: [],
598+
tool: {
599+
messageID: "msg-grandchild-1",
600+
callID: "call-grep-1",
601+
},
602+
},
603+
})
604+
605+
expect(snapshotSubagentData(data).permissions.map((item) => item.id)).toEqual(["perm-grandchild-1"])
606+
})
547607
})

0 commit comments

Comments
 (0)