Skip to content

Commit 0cfe2c2

Browse files
authored
fix(app): never count subagents in the new timeline (#64017)
1 parent 38bd737 commit 0cfe2c2

12 files changed

Lines changed: 210 additions & 120 deletions

File tree

‎packages/session-ui/src/timeline-next/event-row.tsx‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import { For, Show, createMemo } from "solid-js"
44
import { BasicTool } from "../components/basic-tool"
55
import { useData } from "../context/data"
66
import { taskAgentName } from "../tools/tool-renderer"
7-
import { kindKeys, summarizeEvents, verbKeys, type TimelineEvent } from "./events"
7+
import { summarizeShells, verbKeys, type TimelineEvent } from "./events"
88

99
/**
10-
* A run of subagent and background shell events, read like a group label: "Started 2 subagents · Finished 1 shell".
11-
* Opening it lists each one; a subagent opens its session, and a shell jumps to the call that ran it.
10+
* A run of background shells. Opening its summary lists each command and links back to its call.
1211
*/
1312
export function TimelineEventRow(props: {
1413
events: readonly TimelineEvent[]
@@ -17,7 +16,7 @@ export function TimelineEventRow(props: {
1716
onRevealShell?: (shellID: string) => void
1817
}) {
1918
const i18n = useI18n()
20-
const summary = createMemo(() => summarizeEvents(props.events))
19+
const summary = createMemo(() => summarizeShells(props.events))
2120
const items = createMemo(() => props.events.filter((event) => event.title))
2221

2322
return (
@@ -40,7 +39,7 @@ export function TimelineEventRow(props: {
4039
</Show>
4140
<span data-slot="context-tool-group-verb">{i18n.t(verbKeys[bucket.verb])}</span>
4241
<span data-slot="context-tool-group-detail">
43-
{bucket.kinds.map((item) => i18n.plural(kindKeys[item.kind], item.count)).join(", ")}
42+
{i18n.plural("ui.timeline.event.shells", bucket.count)}
4443
</span>
4544
</>
4645
)}
@@ -75,10 +74,12 @@ export function TimelineEventItem(props: { event: TimelineEvent; onRevealShell?:
7574
const i18n = useI18n()
7675
const data = useData()
7776

78-
const name = () =>
79-
props.event.kind === "shell"
80-
? i18n.t("ui.tool.shell")
81-
: (taskAgentName(props.event.agent, data.store.agent) ?? i18n.t("ui.tool.agent.default"))
77+
const name = () => {
78+
if (props.event.kind === "shell") return i18n.t("ui.tool.shell")
79+
const agent = taskAgentName(props.event.agent, data.store.agent)
80+
81+
return agent ? i18n.t("ui.timeline.subagent.name", { name: agent }) : i18n.t("ui.tool.agent.default")
82+
}
8283

8384
const href = () => (props.event.sessionID ? data.sessionHref?.(props.event.sessionID) : undefined)
8485

@@ -92,17 +93,29 @@ export function TimelineEventItem(props: { event: TimelineEvent; onRevealShell?:
9293

9394
const clickable = () => !!((props.event.sessionID && (data.navigateToSession || href())) || props.event.shellID)
9495

96+
const status = () => {
97+
if (props.event.failed) return i18n.t("ui.toolErrorCard.failed")
98+
99+
if (props.event.verb === "updated") return i18n.t("ui.timeline.subagent.updated")
100+
101+
if (props.event.verb === "ran" || props.event.verb === "finished") return i18n.t("ui.timeline.subagent.completed")
102+
103+
return i18n.t(verbKeys[props.event.verb])
104+
}
105+
95106
return (
96107
<a
97108
data-slot="timeline-event-item"
98109
data-kind={props.event.kind}
99110
href={href()}
100-
role={clickable() && !href() ? "button" : undefined}
111+
role={clickable() && !href() ? (props.event.kind === "subagent" ? "link" : "button") : undefined}
101112
tabIndex={clickable() ? 0 : undefined}
102113
aria-disabled={clickable() ? undefined : "true"}
103114
onClick={(event) => {
104115
if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return
105116

117+
if (href() && !data.navigateToSession) return
118+
106119
if (!clickable()) return
107120
event.preventDefault()
108121
open()
@@ -113,9 +126,23 @@ export function TimelineEventItem(props: { event: TimelineEvent; onRevealShell?:
113126
open()
114127
}}
115128
>
116-
<span data-slot="timeline-event-item-name">{name()}</span>
117-
<span data-slot="timeline-event-item-title">{props.event.title}</span>
118-
<Show when={props.event.failed}>
129+
<Show
130+
when={props.event.kind === "subagent"}
131+
fallback={<span data-slot="timeline-event-item-name">{name()}</span>}
132+
>
133+
<Badge data-slot="timeline-event-item-name" variant="neutral">
134+
{name()}
135+
</Badge>
136+
</Show>
137+
<Show when={props.event.title}>
138+
<bdi dir="auto" data-slot="timeline-event-item-title">
139+
{props.event.title}
140+
</bdi>
141+
</Show>
142+
<Show when={props.event.kind === "subagent"}>
143+
<span class={props.event.failed ? "text-v2-state-fg-danger" : "text-v2-text-text-muted"}>{status()}</span>
144+
</Show>
145+
<Show when={props.event.failed && props.event.kind === "shell"}>
119146
<Badge variant="danger">{i18n.t("ui.toolErrorCard.failed")}</Badge>
120147
</Show>
121148
</a>

‎packages/session-ui/src/timeline-next/events.ts‎

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import { Option, Schema } from "effect"
33
import { currentToolInput, currentToolMetadata } from "../message/current-tool-state"
44
import { toolRule } from "./rules"
55

6-
/**
7-
* Subagents and background shells read as events rather than tool rows: started, ran, finished and stopped, counted by
8-
* kind. Titles and commands stay in the background panel above the composer.
9-
*/
106
export type TimelineEventKind = "subagent" | "shell"
117

128
export type TimelineEventVerb = "started" | "ran" | "finished" | "stopped"
139

1410
export type TimelineEvent = {
15-
verb: TimelineEventVerb
11+
verb: TimelineEventVerb | "updated"
1612
kind: TimelineEventKind
1713
failed: boolean
1814
/** What opening the row lists: a subagent's task or a shell's command, and where each leads. */
@@ -22,25 +18,20 @@ export type TimelineEvent = {
2218
shellID?: string
2319
}
2420

25-
/** How an events row names each verb and kind: "Started 2 subagents". */
21+
/** Status labels shared by task rows and shell summaries. */
2622
export const verbKeys = {
2723
started: "ui.timeline.event.started",
2824
ran: "ui.timeline.activity.ran",
2925
finished: "ui.timeline.event.finished",
3026
stopped: "ui.timeline.event.stopped",
3127
} as const
3228

33-
export const kindKeys = {
34-
subagent: "ui.timeline.event.subagents",
35-
shell: "ui.timeline.event.shells",
36-
} as const
37-
3829
/** How a group's label counts work that runs on, beside its other calls: "1 command, started 1 shell". */
3930
export const launchKeys = {
40-
started: { shell: "ui.timeline.activity.startedShells", subagent: "ui.timeline.activity.startedSubagents" },
41-
ran: { shell: "ui.timeline.activity.commands", subagent: "ui.timeline.activity.subagents" },
42-
finished: { shell: "ui.timeline.activity.finishedShells", subagent: "ui.timeline.activity.finishedSubagents" },
43-
stopped: { shell: "ui.timeline.activity.stoppedShells", subagent: "ui.timeline.activity.stoppedSubagents" },
31+
started: "ui.timeline.activity.startedShells",
32+
ran: "ui.timeline.activity.commands",
33+
finished: "ui.timeline.activity.finishedShells",
34+
stopped: "ui.timeline.activity.stoppedShells",
4435
} as const
4536

4637
// Text a call or notice carries, where it is not empty.
@@ -57,16 +48,17 @@ export function toolEvent(tool: SessionMessageAssistantTool): TimelineEvent | un
5748
if (state.status === "streaming") return undefined
5849
const input = currentToolInput(tool)
5950
const metadata = currentToolMetadata(tool)
51+
const followUp = kind === "subagent" ? Option.getOrUndefined(decodeText(input.sessionID)) : undefined
6052

6153
const item = {
6254
kind,
6355
title: Option.getOrUndefined(decodeText(kind === "shell" ? input.command : input.description)),
64-
agent: kind === "subagent" ? Option.getOrUndefined(decodeText(input.agent)) : undefined,
65-
sessionID: kind === "subagent" ? Option.getOrUndefined(decodeText(metadata.sessionID)) : undefined,
56+
agent: kind === "subagent" ? Option.getOrUndefined(decodeText(input.agent ?? input.subagent_type)) : undefined,
57+
sessionID: kind === "subagent" ? (Option.getOrUndefined(decodeText(metadata.sessionID)) ?? followUp) : undefined,
6658
shellID: kind === "shell" ? Option.getOrUndefined(decodeText(metadata.shellID)) : undefined,
6759
} as const
6860

69-
if (state.status === "running") return { ...item, verb: "started", failed: false }
61+
if (state.status === "running") return { ...item, verb: followUp ? "updated" : "started", failed: false }
7062

7163
const background =
7264
kind === "subagent" ? metadata.status !== "completed" : metadata.status === "running" || input.background === true
@@ -77,6 +69,8 @@ export function toolEvent(tool: SessionMessageAssistantTool): TimelineEvent | un
7769
return { ...item, verb: background ? "started" : "ran", failed: true }
7870
}
7971

72+
if (followUp) return { ...item, verb: "updated", failed: false }
73+
8074
return { ...item, verb: background ? "started" : "ran", failed: false }
8175
}
8276

@@ -108,26 +102,16 @@ export function workKey(event: TimelineEvent) {
108102
return id ? `${event.kind}:${id}` : undefined
109103
}
110104

111-
/**
112-
* A run of events as one label: each verb once, in the order it first happened, with how many of each kind, and how
113-
* many failed overall. Two finishes in a row read "Finished 2 subagents".
114-
*/
115-
export function summarizeEvents(events: readonly TimelineEvent[]) {
116-
const verbs = events.reduce<{ verb: TimelineEventVerb; kinds: { kind: TimelineEventKind; count: number }[] }[]>(
117-
(result, event) => {
118-
const bucket = result.find((item) => item.verb === event.verb) ?? { verb: event.verb, kinds: [] }
119-
120-
if (!result.includes(bucket)) result.push(bucket)
121-
const kind = bucket.kinds.find((item) => item.kind === event.kind)
122-
123-
if (kind) kind.count += 1
124-
125-
if (!kind) bucket.kinds.push({ kind: event.kind, count: 1 })
126-
127-
return result
128-
},
129-
[],
130-
)
131-
132-
return { verbs, failed: events.filter((event) => event.failed).length }
105+
/** Count background shells by verb in the order they happened. Subagents have individual task rows. */
106+
export function summarizeShells(events: readonly TimelineEvent[]) {
107+
const counts = new Map<TimelineEventVerb, number>()
108+
const shells = events.filter((event) => event.kind === "shell")
109+
shells.forEach(({ verb }) => {
110+
if (verb !== "updated") counts.set(verb, (counts.get(verb) ?? 0) + 1)
111+
})
112+
113+
return {
114+
verbs: Array.from(counts, ([verb, count]) => ({ verb, count })),
115+
failed: shells.filter((event) => event.failed).length,
116+
}
133117
}

‎packages/session-ui/src/timeline-next/projection.test.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe("New timeline projection", () => {
150150
])
151151
})
152152

153-
test("a subagent finishes in the group that started it only when nothing comes between; otherwise it reports in its own row", () => {
153+
test("each subagent event stays in order in both visible placements, even without a title or visible launch", () => {
154154
const launched: SessionMessageInfo[] = [
155155
{ id: "user", type: "user", text: "Go", time: { created: 0 } },
156156
step("launch", 1, {
@@ -195,13 +195,15 @@ describe("New timeline projection", () => {
195195
timelineDetail: () => detail,
196196
})
197197

198-
const result = projection
199-
.rows()
200-
.flatMap((row) =>
201-
Predicate.isTagged(row, "AssistantPart") && row.group.type !== "part"
202-
? [`${row.group.type}:${row.group.refs.map((ref) => ref.messageID).join("+")}`]
203-
: [],
204-
)
198+
const result = projection.rows().flatMap((row) => {
199+
if (Predicate.isTagged(row, "Notice")) return [`notice:${row.messageID}`]
200+
201+
if (!Predicate.isTagged(row, "AssistantPart")) return []
202+
203+
if (row.group.type === "part") return [`part:${row.group.ref.messageID}`]
204+
205+
return [`${row.group.type}:${row.group.refs.map((ref) => ref.messageID).join("+")}`]
206+
})
205207

206208
dispose()
207209

@@ -210,9 +212,20 @@ describe("New timeline projection", () => {
210212

211213
const grouped = { ...detailed, subagents: { placement: "grouped" as const } }
212214

213-
expect(groups([...launched, report], grouped)).toEqual(["context:launch+report"])
214-
expect(groups([...launched, said, report], grouped)).toEqual(["context:launch", "events:report"])
215-
expect(groups([...launched, report], detailed)).toEqual(["events:launch", "events:report"])
215+
for (const detail of [grouped, detailed]) {
216+
expect(groups([...launched, report], detail)).toEqual(["subagent:launch", "subagent:report"])
217+
expect(groups([...launched, said, report], detail)).toEqual(["subagent:launch", "part:said", "subagent:report"])
218+
expect(groups([launched[0]!, report], detail)).toEqual(["subagent:report"])
219+
220+
if (report.type !== "synthetic") throw new Error("Expected a subagent report")
221+
expect(groups([...launched, report, { ...report, id: "untitled", description: undefined }], detail)).toEqual([
222+
"subagent:launch",
223+
"subagent:report",
224+
"subagent:untitled",
225+
])
226+
}
227+
228+
expect(groups([...launched, report], messagesOnly)).toEqual([])
216229
})
217230

218231
test("a move shows only when it changes where the session is", () => {

‎packages/session-ui/src/timeline-next/projection.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,8 @@ function turnCommentary(entries: Entry[], settled: boolean, visible: (content: C
639639
}
640640

641641
/**
642-
* Notices fold into the rows around them: grouped notices join the activity group, and background work reporting back
643-
* one after another reads as one events row, "Finished 2 subagents". Anything else between them keeps them apart, so
644-
* the order of events stays true.
642+
* Grouped notices join the activity group, shell reports can share an events row, and each subagent report keeps
643+
* its own place in the timeline.
645644
*/
646645
function groupNotices(
647646
rows: TimelineRow.TimelineRow[],
@@ -657,7 +656,8 @@ function groupNotices(
657656

658657
// Work that reports back right after the activity group that started it, with nothing between, finishes there.
659658
if (
660-
event &&
659+
message &&
660+
event?.kind === "shell" &&
661661
!event.failed &&
662662
Predicate.isTagged(previous, "AssistantPart") &&
663663
previous.group.type === "context" &&
@@ -672,7 +672,7 @@ function groupNotices(
672672
return result
673673
}
674674

675-
const type = message ? noticeGroup(detail, message) : undefined
675+
const type = message ? noticeGroup(detail, message, event) : undefined
676676

677677
const current =
678678
Predicate.isTagged(row, "Notice") && type
@@ -750,8 +750,8 @@ function launches(
750750
)
751751
}
752752

753-
function noticeGroup(detail: TimelineDetail, message: SessionMessageInfo) {
754-
if (noticeEvent(message)) return "events" as const
753+
function noticeGroup(detail: TimelineDetail, message: SessionMessageInfo, event?: TimelineEvent) {
754+
if (event) return event.kind === "subagent" ? ("subagent" as const) : ("events" as const)
755755

756756
return noticePlacement(detail, message) === "grouped" ? ("context" as const) : undefined
757757
}
@@ -929,6 +929,17 @@ function groupContent(
929929
items.forEach((item) => {
930930
const type = contentGroup(detail, item.content, commentary)
931931

932+
if (type === "subagent") {
933+
flush()
934+
groups.push({
935+
type,
936+
key: `subagent:${item.messageID}:${item.partID}`,
937+
refs: [{ messageID: item.messageID, partID: item.partID }],
938+
})
939+
940+
return
941+
}
942+
932943
if (type) {
933944
if (adjacent?.type !== type) flush()
934945
adjacent ??= { type, refs: [] }
@@ -955,5 +966,5 @@ function isNotice(message: SessionMessageInfo): message is Notice {
955966

956967
if (message.type !== "synthetic") return true
957968

958-
return !!message.description?.trim() || timelineNoticeRequired(message)
969+
return message.metadata?.source === "subagent" || !!message.description?.trim() || timelineNoticeRequired(message)
959970
}

‎packages/session-ui/src/timeline-next/row.tsx‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { Badge } from "@opencode/ui/badge"
4949
import { attached } from "../components/message-file"
5050
import { CompactionMessage, CompactionQueued } from "./compaction"
5151
import { unwrapErrorMessage } from "./errors"
52-
import { TimelineEventRow } from "./event-row"
52+
import { TimelineEventItem, TimelineEventRow } from "./event-row"
5353
import { noticeEvent, toolEvent } from "./events"
5454
import { trackNestedRails } from "./nested-rail"
5555
import { contentEntries, createNextTimelineProjection, resolveContent, workedDuration } from "./projection"
@@ -413,11 +413,11 @@ export function createNextTimelineRowRenderer(input: {
413413
)
414414
}
415415

416-
if (row().group.type === "events") {
416+
if (row().group.type === "events" || row().group.type === "subagent") {
417417
const events = createMemo(() => {
418418
const group = row().group
419419

420-
if (group.type !== "events") return []
420+
if (group.type !== "events" && group.type !== "subagent") return []
421421
const contents = indexGroupContents(group.refs)
422422

423423
// A call starts the work; a notice under its message's own ID reports it back.
@@ -436,6 +436,18 @@ export function createNextTimelineRowRenderer(input: {
436436
})
437437
})
438438

439+
if (row().group.type === "subagent") {
440+
return (
441+
<Show when={events()[0]}>
442+
{(event) => (
443+
<div data-component="timeline-subagent">
444+
<TimelineEventItem event={event()} />
445+
</div>
446+
)}
447+
</Show>
448+
)
449+
}
450+
439451
return (
440452
<TimelineEventRow
441453
events={events()}

0 commit comments

Comments
 (0)