Skip to content

Commit 3dca04a

Browse files
committed
fix(app): count running subagents from their session status
1 parent 4572b5d commit 3dca04a

6 files changed

Lines changed: 307 additions & 210 deletions

File tree

‎packages/app/e2e/regression/session-queue.spec.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test, type Page } from "@playwright/test"
2-
import type { OpenCodeEvent, SessionInboxInfo, SessionMessageInfo } from "@opencode/client/promise"
2+
import type { OpenCodeEvent, SessionInboxInfo, SessionMessageInfo, ShellInfo } from "@opencode/client/promise"
33
import { PromptInput } from "@opencode/schema/prompt-input"
44
import { SessionInbox } from "@opencode/schema/session-inbox"
55
import { SessionMessage } from "@opencode/schema/session-message"
@@ -65,6 +65,8 @@ function createQueueMock(seed: string[], messages: SessionMessageInfo[] = []) {
6565
const compactions: SessionInboxInfo[] = []
6666
const changes: { inboxID: string; action: "cancel" | "steer" | "queue" }[] = []
6767
const log: string[] = []
68+
// Live background shell commands (`GET /api/shell`).
69+
const shells: ShellInfo[] = []
6870
let sequence = 0
6971

7072
const emit = <Type extends OpenCodeEvent["type"]>(
@@ -89,6 +91,7 @@ function createQueueMock(seed: string[], messages: SessionMessageInfo[] = []) {
8991
changes,
9092
log,
9193
messages,
94+
shells,
9295
emit,
9396
events: () => events.splice(0),
9497
onPrompt: (input: Parameters<NonNullable<MockServerConfig["onPrompt"]>>[0]) => {
@@ -184,6 +187,7 @@ async function openQueue(
184187
onCompact: mock.onCompact,
185188
onInboxChange: mock.onInboxChange,
186189
onBackground: mock.onBackground,
190+
shellCommands: () => mock.shells,
187191
events: mock.events,
188192
}
189193

@@ -1324,6 +1328,17 @@ test("a steer moves a blocking shell to the background after its admission, so t
13241328
],
13251329
)
13261330

1331+
// The command is live from its start; its blocking call keeps it out of the background until the move.
1332+
mock.shells.push({
1333+
id: "sh_steer_background",
1334+
status: "running",
1335+
command: "bun test --timeout 600000",
1336+
cwd: "C:/OpenCode/SessionQueueRegression",
1337+
shell: "bash",
1338+
file: "/tmp/steer-background.out",
1339+
metadata: { sessionID },
1340+
time: { started: 1700000000002 },
1341+
})
13271342
const view = await openQueue(page, mock, "steer", undefined, "running", { id: "alpha", name: "Alpha" })
13281343
const transcript = page.locator("[data-timeline-virtual-content]")
13291344
const hint = page.getByRole("button", { name: /move running work to the background/i })

‎packages/app/e2e/regression/timeline-stream.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,18 @@ test.describe("Working", () => {
829829
{ completed: false },
830830
),
831831
],
832+
shellCommands: [
833+
{
834+
id: "sh_working_background",
835+
status: "running",
836+
command: "sleep 10",
837+
cwd: directory,
838+
shell: "bash",
839+
file: "/tmp/working-background.out",
840+
metadata: { sessionID },
841+
time: { started: 2 },
842+
},
843+
],
832844
})
833845
const working = page.locator('[data-component="session-working"]')
834846

@@ -998,6 +1010,18 @@ test.describe("background shortcut", () => {
9981010
time: { created: 2, completed: 3 },
9991011
},
10001012
],
1013+
shellCommands: [
1014+
{
1015+
id: "shell_backgrounded",
1016+
status: "running",
1017+
command: "sleep 120",
1018+
cwd: directory,
1019+
shell: "bash",
1020+
file: "/tmp/background.out",
1021+
metadata: { sessionID },
1022+
time: { started: 2 },
1023+
},
1024+
],
10011025
})
10021026

10031027
// The open menu hides the rest of the page from the accessibility tree, the capsule included.
@@ -1029,6 +1053,50 @@ test.describe("background shortcut", () => {
10291053
})
10301054
}
10311055

1056+
test("counts a backgrounded subagent while its session runs, not while its call still reads running", async ({
1057+
page,
1058+
}) => {
1059+
const childID = "ses_background_stale_call"
1060+
1061+
// As after a lost completion notice: the call still reads running, while the child session has finished.
1062+
const timeline = await setupTimeline(page, {
1063+
settings: { timelineDetail: detailed },
1064+
sessionMessages: [
1065+
user,
1066+
{
1067+
id: "msg_stale_call",
1068+
type: "assistant",
1069+
agent: "build",
1070+
model: { id: "model", providerID: "provider" },
1071+
content: [
1072+
{
1073+
type: "tool",
1074+
id: "call_stale",
1075+
name: "subagent",
1076+
state: {
1077+
status: "completed",
1078+
input: { description: "Stale task", agent: "explore", background: true },
1079+
content: [{ type: "text", text: "working" }],
1080+
metadata: { sessionID: childID, status: "running" },
1081+
},
1082+
time: { created: 2, completed: 3 },
1083+
},
1084+
],
1085+
time: { created: 2, completed: 3 },
1086+
},
1087+
],
1088+
sessions: [session(), session({ id: childID, parentID: sessionID, title: "Stale task", agent: "explore" })],
1089+
})
1090+
const capsule = page.locator('[data-slot="session-activity-chip"]')
1091+
1092+
await expect(page.locator('[data-timeline-part-id="call_stale"]')).toContainText("Stale task (background)")
1093+
await expect(capsule).toHaveCount(0)
1094+
await timeline.transport.send(status("busy", 1, childID))
1095+
await expect(page.getByRole("button", { name: "1 subagent", exact: true })).toBeVisible()
1096+
await timeline.transport.send(status("idle", 1, childID))
1097+
await expect(capsule).toHaveCount(0)
1098+
})
1099+
10321100
test("separates blocking and already-backgrounded work into two rows", async ({ page }) => {
10331101
const backgroundID = "ses_background_existing"
10341102
const blockingID = "ses_background_blocking"

‎packages/app/src/session/composer/region.tsx‎

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -378,35 +378,15 @@ export function ActiveSessionComposerRegion(props: {
378378
return { directory: info.location.directory, sessionID: info.id, projectID: info.projectID }
379379
})
380380

381-
// Subagents count while their call still blocks the run; foreground shells stay out, the timeline shows them.
382-
const subagents = createMemo<SessionActivityItem[]>(() => [
383-
...props.model.region.state.background.blocking().flatMap((task) =>
384-
task.type === "subagent"
385-
? [
386-
{
387-
...task,
388-
key: task.id ?? task.partID,
389-
agent: task.id ? data.session.get(task.id)?.agent : undefined,
390-
sessionID: task.id,
391-
target: task.partID,
392-
},
393-
]
394-
: [],
395-
),
396-
...props.model.region.state.background.tasks().flatMap((task) =>
397-
task.type === "subagent"
398-
? [
399-
{
400-
...task,
401-
key: task.id,
402-
agent: task.agent ?? data.session.get(task.id)?.agent,
403-
sessionID: task.id,
404-
target: task.id,
405-
},
406-
]
407-
: [],
408-
),
409-
])
381+
// Subagents count while their session runs, or while their blocking call waits for its child session.
382+
const subagents = createMemo<SessionActivityItem[]>(() =>
383+
props.model.region.state.background.subagents().map((task) => ({
384+
...task,
385+
agent: task.agent ?? (task.sessionID ? data.session.get(task.sessionID)?.agent : undefined),
386+
key: task.id,
387+
target: task.partID ?? task.id,
388+
})),
389+
)
410390

411391
const shells = createMemo<SessionActivityItem[]>(() =>
412392
props.model.region.state.background

0 commit comments

Comments
 (0)