Skip to content

Commit 0dc2b8b

Browse files
committed
refactor(app): share subagent transcript reader with background tasks
1 parent 718354e commit 0dc2b8b

69 files changed

Lines changed: 554 additions & 1219 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { createRoot } from "solid-js"
3+
import type { SessionMessageAssistantTool, SessionMessageInfo } from "@opencode/client/promise"
4+
import { createSessionBackground } from "./background"
5+
6+
function tool(
7+
state: SessionMessageAssistantTool["state"],
8+
overrides?: Partial<Pick<SessionMessageAssistantTool, "id" | "name">>,
9+
): SessionMessageAssistantTool {
10+
return {
11+
type: "tool",
12+
id: "part-1",
13+
name: "subagent",
14+
time: { created: 1 },
15+
state,
16+
...overrides,
17+
}
18+
}
19+
20+
function assistant(id: string, parts: SessionMessageAssistantTool[], completed: boolean) {
21+
return {
22+
id,
23+
type: "assistant" as const,
24+
agent: "build",
25+
model: { id: "model", providerID: "provider" },
26+
content: parts,
27+
time: completed ? { created: 1, completed: 2 } : { created: 1 },
28+
}
29+
}
30+
31+
function notice(childID: string) {
32+
return {
33+
id: `notice-${childID}`,
34+
type: "synthetic",
35+
metadata: { source: "subagent", childID },
36+
text: "",
37+
time: { created: 3 },
38+
} as const
39+
}
40+
41+
function background(messages: SessionMessageInfo[]) {
42+
return createRoot((dispose) => {
43+
const model = createSessionBackground({
44+
sessionID: () => "parent",
45+
messages: () => messages,
46+
sessions: () => [],
47+
status: () => "idle",
48+
shells: () => [],
49+
})
50+
51+
const result = { blocking: model.blocking(), tasks: model.tasks() }
52+
dispose()
53+
54+
return result
55+
})
56+
}
57+
58+
describe("subagent background tasks", () => {
59+
test("backgrounded completions list until their notice lands", () => {
60+
const part = tool(
61+
{
62+
status: "completed",
63+
input: { description: "Explore", agent: "general" },
64+
content: [{ type: "text", text: "done" }],
65+
metadata: { sessionID: "child", status: "running" },
66+
},
67+
{ id: "a" },
68+
)
69+
70+
expect(background([assistant("m1", [part], true)]).tasks).toEqual([
71+
{ id: "child", type: "subagent", label: "Explore", agent: "general" },
72+
])
73+
expect(background([assistant("m1", [part], true), notice("child")]).tasks).toEqual([])
74+
})
75+
76+
test("running calls in the latest message block", () => {
77+
const live = tool({ status: "running", input: { description: "Live" }, metadata: { sessionID: "a" } }, { id: "a" })
78+
const model = background([assistant("m1", [live], false)])
79+
80+
expect(model.blocking).toEqual([{ type: "subagent", partID: "a", id: "a", label: "Live" }])
81+
expect(model.tasks).toEqual([])
82+
})
83+
84+
test("stale running calls in older messages stay out of blocking", () => {
85+
const stale = tool({ status: "running", input: {}, metadata: { sessionID: "old" } }, { id: "s" })
86+
const model = background([assistant("m1", [stale], true), assistant("m2", [], false)])
87+
88+
expect(model.blocking).toEqual([])
89+
expect(model.tasks).toEqual([])
90+
})
91+
92+
test("shell calls keep working", () => {
93+
const shell = tool(
94+
{
95+
status: "completed",
96+
input: { command: "ls" },
97+
content: [{ type: "text", text: "out" }],
98+
metadata: { status: "running", shellID: "sh-1" },
99+
},
100+
{ id: "s", name: "shell" },
101+
)
102+
103+
expect(background([assistant("m1", [shell], true)]).tasks).toEqual([{ id: "sh-1", type: "shell", label: "ls" }])
104+
})
105+
})

‎packages/app/src/session/requests/background.ts‎

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { SessionInfo, SessionMessageAssistant, SessionMessageInfo, ShellInfo } from "@opencode/client/promise"
2+
import { scanSubagentTranscript } from "@opencode/session-ui/subagent-transcript"
3+
import { Predicate } from "effect"
24
import { createMemo } from "solid-js"
35

46
type Task =
@@ -14,54 +16,53 @@ export function createSessionBackground(input: {
1416
}) {
1517
const history = createMemo(() => {
1618
const completed = new Set<string>()
17-
const subagents: { id: string; type: "subagent"; label: string; agent: string | undefined }[] = []
1819
const shells: { partID: string; task: { id: string; type: "shell"; label: string } }[] = []
1920
const id = input.sessionID()
21+
const messages = id ? input.messages(id) : []
22+
const scan = scanSubagentTranscript(messages)
2023

21-
const assistant = (id ? input.messages(id) : []).reduce<SessionMessageAssistant | undefined>((latest, message) => {
22-
if (message.type === "synthetic") {
23-
if (message.metadata?.source === "subagent" && typeof message.metadata.childID === "string")
24-
completed.add(message.metadata.childID)
24+
for (const childID of scan.completed) completed.add(childID)
2525

26-
if (message.metadata?.source === "shell") {
27-
if (typeof message.metadata.shellID === "string") completed.add(message.metadata.shellID)
26+
for (const message of messages) {
27+
if (message.type !== "synthetic") continue
2828

29-
if (typeof message.metadata.jobID === "string") completed.add(message.metadata.jobID)
30-
}
29+
if (message.metadata?.source === "shell") {
30+
if (Predicate.isString(message.metadata.shellID)) completed.add(message.metadata.shellID)
3131

32-
return latest
32+
if (Predicate.isString(message.metadata.jobID)) completed.add(message.metadata.jobID)
3333
}
34+
}
35+
36+
const subagents = scan.calls.flatMap((call) => {
37+
if (!call.backgrounded || !call.childID || completed.has(call.childID)) return []
38+
39+
return [
40+
{
41+
id: call.childID,
42+
type: "subagent" as const,
43+
label: call.description ?? call.childID,
44+
agent: call.agent,
45+
},
46+
]
47+
})
48+
49+
const assistant = messages.reduce<SessionMessageAssistant | undefined>((latest, message) => {
50+
if (message.type === "synthetic") return latest
3451

3552
if (message.type !== "assistant") return latest
3653
message.content.forEach((part) => {
37-
if (part.type !== "tool" || (part.name !== "subagent" && part.name !== "shell")) return
54+
if (part.type !== "tool" || part.name !== "shell") return
3855

3956
if (part.state.status !== "completed" || part.state.metadata?.status !== "running") return
4057

41-
if (part.name === "subagent") {
42-
const sessionID = part.state.metadata.sessionID
43-
44-
if (typeof sessionID !== "string") return
45-
const description = part.state.input.description
46-
const agent = part.state.input.agent
47-
subagents.push({
48-
id: sessionID,
49-
type: "subagent",
50-
label: typeof description === "string" ? description : sessionID,
51-
agent: typeof agent === "string" ? agent : undefined,
52-
})
53-
54-
return
55-
}
56-
5758
const shellID = part.state.metadata.shellID
5859
const command = part.state.input.command
5960
shells.push({
6061
partID: part.id,
6162
task: {
62-
id: typeof shellID === "string" ? shellID : part.id,
63+
id: Predicate.isString(shellID) ? shellID : part.id,
6364
type: "shell",
64-
label: typeof command === "string" ? command : part.id,
65+
label: Predicate.isString(command) ? command : part.id,
6566
},
6667
})
6768
})
@@ -70,11 +71,11 @@ export function createSessionBackground(input: {
7071
}, undefined)
7172

7273
return {
73-
// Completion notices can identify the shell or its original tool call.
74-
subagents: subagents.filter((task) => !completed.has(task.id)),
75-
shells: shells
76-
.filter((item) => !completed.has(item.partID) && !completed.has(item.task.id))
77-
.map((item) => item.task),
74+
// Backgrounded calls stay listed until their synthetic completion notice lands (filtered at construction).
75+
subagents,
76+
shells: shells.flatMap((item) =>
77+
!completed.has(item.partID) && !completed.has(item.task.id) ? [item.task] : [],
78+
),
7879
blocking:
7980
assistant?.content.flatMap((part) => {
8081
if (part.type !== "tool" || part.state.status !== "running") return []
@@ -85,10 +86,12 @@ export function createSessionBackground(input: {
8586

8687
return [
8788
{
89+
// SAFETY: the guard above leaves only "shell" and "subagent" names.
90+
// oxlint-disable-next-line anti-slop/require-safety-comment-for-type-assertion -- see SAFETY above
8891
type: part.name as "shell" | "subagent",
8992
partID: part.id,
90-
id: typeof value === "string" ? value : undefined,
91-
label: typeof label === "string" ? label : undefined,
93+
id: Predicate.isString(value) ? value : undefined,
94+
label: Predicate.isString(label) ? label : undefined,
9295
},
9396
]
9497
}) ?? [],

‎packages/gui-extensions/src/agents/i18n/am.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/ar.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/az.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/bg.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/bn.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/br.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/bs.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎packages/gui-extensions/src/agents/i18n/ca.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)