From f2156d79c9b3c313f999246562ecfba1513880bf Mon Sep 17 00:00:00 2001 From: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:42:03 +0530 Subject: [PATCH] fix(app): show subagents in grouped activity as events in the New timeline (#63685) --- .../src/timeline-next/event-row.tsx | 3 +- .../session-ui/src/timeline-next/events.ts | 11 +- .../src/timeline-next/projection.test.ts | 67 +++- .../src/timeline-next/projection.ts | 41 +- packages/session-ui/src/timeline-next/row.tsx | 10 +- .../session-ui/src/timeline-next/rules.ts | 2 +- .../src/timeline-next/timeline-next.css | 7 +- .../src/timeline-next/tools/context-group.tsx | 377 ++++++++++++------ packages/ui/src/i18n/en.ts | 8 + 9 files changed, 387 insertions(+), 139 deletions(-) diff --git a/packages/session-ui/src/timeline-next/event-row.tsx b/packages/session-ui/src/timeline-next/event-row.tsx index b612d7a39be..b00622c0166 100644 --- a/packages/session-ui/src/timeline-next/event-row.tsx +++ b/packages/session-ui/src/timeline-next/event-row.tsx @@ -70,7 +70,8 @@ export function TimelineEventRow(props: { ) } -function TimelineEventItem(props: { event: TimelineEvent; onRevealShell?: (shellID: string) => void }) { +/** One subagent or shell an events row or an opened activity group lists: who did it and what it was. */ +export function TimelineEventItem(props: { event: TimelineEvent; onRevealShell?: (shellID: string) => void }) { const i18n = useI18n() const data = useData() diff --git a/packages/session-ui/src/timeline-next/events.ts b/packages/session-ui/src/timeline-next/events.ts index d4d159f23bb..fbc3cf699c2 100644 --- a/packages/session-ui/src/timeline-next/events.ts +++ b/packages/session-ui/src/timeline-next/events.ts @@ -35,10 +35,12 @@ export const kindKeys = { shell: "ui.timeline.event.shells", } as const -/** How a group's label counts work it started, beside its other calls: "1 command, started 1 shell". */ +/** How a group's label counts work that runs on, beside its other calls: "1 command, started 1 shell". */ export const launchKeys = { started: { shell: "ui.timeline.activity.startedShells", subagent: "ui.timeline.activity.startedSubagents" }, ran: { shell: "ui.timeline.activity.commands", subagent: "ui.timeline.activity.subagents" }, + finished: { shell: "ui.timeline.activity.finishedShells", subagent: "ui.timeline.activity.finishedSubagents" }, + stopped: { shell: "ui.timeline.activity.stoppedShells", subagent: "ui.timeline.activity.stoppedSubagents" }, } as const // Text a call or notice carries, where it is not empty. @@ -99,6 +101,13 @@ export function noticeEvent(message: SessionMessageInfo): TimelineEvent | undefi return { ...item, verb: "finished", failed: state === "error" } } +/** Which subagent or shell an event is about, where it says. */ +export function workKey(event: TimelineEvent) { + const id = event.kind === "shell" ? event.shellID : event.sessionID + + return id ? `${event.kind}:${id}` : undefined +} + /** * A run of events as one label: each verb once, in the order it first happened, with how many of each kind, and how * many failed overall. Two finishes in a row read "Finished 2 subagents". diff --git a/packages/session-ui/src/timeline-next/projection.test.ts b/packages/session-ui/src/timeline-next/projection.test.ts index 71431a34a69..549e2513abb 100644 --- a/packages/session-ui/src/timeline-next/projection.test.ts +++ b/packages/session-ui/src/timeline-next/projection.test.ts @@ -3,7 +3,7 @@ import type { SessionMessageAssistant, SessionMessageInfo, SessionMessageUser } import { Predicate } from "effect" import { createRoot } from "solid-js" import { subagentReport } from "../storybook/current-session-fixtures" -import { timelinePresets } from "../timeline/detail" +import { timelinePresets, type TimelineDetail } from "../timeline/detail" import { createNextTimelineProjection, workedDuration } from "./projection" import { replayScript, scenarios } from "./stories/session-replay" @@ -149,6 +149,71 @@ describe("New timeline projection", () => { "UserMessage", ]) }) + + test("a subagent finishes in the group that started it only when nothing comes between; otherwise it reports in its own row", () => { + const launched: SessionMessageInfo[] = [ + { id: "user", type: "user", text: "Go", time: { created: 0 } }, + step("launch", 1, { + finish: "tool-calls", + content: [ + { + id: "call", + type: "tool", + name: "subagent", + state: { + status: "completed", + input: { description: "Review the diff", agent: "review" }, + content: [{ type: "text", text: "" }], + metadata: { sessionID: "child", status: "running" }, + }, + time: { created: 1, completed: 2 }, + }, + ], + time: { created: 1, completed: 2 }, + }), + ] + + const report = subagentReport("report", 4, { + childID: "child", + agent: "review", + state: "completed", + description: "Review the diff", + body: "Looks good.", + }) + + const said = step("said", 3, { + finish: "stop", + content: [{ type: "text", text: "Started a review." }], + time: { created: 3, completed: 3 }, + }) + + const groups = (messages: SessionMessageInfo[], detail: TimelineDetail) => + createRoot((dispose) => { + const projection = createNextTimelineProjection({ + sessionMessages: () => messages, + status: () => ({ type: "idle" }), + timelineDetail: () => detail, + }) + + const result = projection + .rows() + .flatMap((row) => + Predicate.isTagged(row, "AssistantPart") && row.group.type !== "part" + ? [`${row.group.type}:${row.group.refs.map((ref) => ref.messageID).join("+")}`] + : [], + ) + + dispose() + + return result + }) + + const grouped = { ...detailed, subagents: { placement: "grouped" as const } } + + expect(groups([...launched, report], grouped)).toEqual(["context:launch+report"]) + expect(groups([...launched, said, report], grouped)).toEqual(["context:launch", "events:report"]) + expect(groups([...launched, report], detailed)).toEqual(["events:launch", "events:report"]) + }) }) describe("workedDuration", () => { diff --git a/packages/session-ui/src/timeline-next/projection.ts b/packages/session-ui/src/timeline-next/projection.ts index c7a5d4c34b2..5edf5cea944 100644 --- a/packages/session-ui/src/timeline-next/projection.ts +++ b/packages/session-ui/src/timeline-next/projection.ts @@ -22,7 +22,7 @@ import { type TurnThinkingPlacement, } from "../timeline/turn-phase-machine" import { errorInfo } from "./errors" -import { noticeEvent } from "./events" +import { noticeEvent, toolEvent, workKey, type TimelineEvent } from "./events" import { contentGroup, contentShows, @@ -31,6 +31,7 @@ import { liveThinkingShows, noticePlacement, textShows, + toolRule, } from "./rules" type Notice = Exclude @@ -651,6 +652,25 @@ function groupNotices( // Live status renders nothing while other feedback shows, so a group below it spaces from the content above it. const above = Predicate.isTagged(previous, "Activity") ? result.at(-2) : previous const message = Predicate.isTagged(row, "Notice") ? messageByID.get(row.messageID) : undefined + const event = message && noticeEvent(message) + + // Work that reports back right after the activity group that started it, with nothing between, finishes there. + if ( + event && + !event.failed && + Predicate.isTagged(previous, "AssistantPart") && + previous.group.type === "context" && + previous.userMessageID === row.userMessageID && + launches(previous.group, event, messageByID) + ) { + result[result.length - 1] = new TimelineRow.AssistantPart({ + ...previous, + group: { ...previous.group, refs: [...previous.group.refs, { messageID: message.id, partID: message.id }] }, + }) + + return result + } + const type = message ? noticeGroup(detail, message) : undefined const current = @@ -710,6 +730,25 @@ function joins( return previous.type === "events" && current.type === "events" && notices(previous) && notices(current) } +// Whether an activity group started the work an event is about. +function launches( + group: Exclude, + event: TimelineEvent, + messageByID: ReadonlyMap, +) { + const key = workKey(event) + + return ( + key !== undefined && + group.refs.some((ref) => { + const content = resolveContent(messageByID.get(ref.messageID), ref.partID) + const launch = content?.type === "tool" && toolRule(content).layout === "event" ? toolEvent(content) : undefined + + return launch !== undefined && workKey(launch) === key + }) + ) +} + function noticeGroup(detail: TimelineDetail, message: SessionMessageInfo) { if (noticeEvent(message)) return "events" as const diff --git a/packages/session-ui/src/timeline-next/row.tsx b/packages/session-ui/src/timeline-next/row.tsx index b2b83513b99..5b0b04ecddf 100644 --- a/packages/session-ui/src/timeline-next/row.tsx +++ b/packages/session-ui/src/timeline-next/row.tsx @@ -32,7 +32,7 @@ import { useSources } from "../context/sources" import { turnSources } from "../sources/sources" import { SessionReadToolGroup, SessionUserMessage } from "../message/current-message" import { SessionCompactionMessage, SessionCompactionQueued } from "../message/message-content" -import { taskAgentName, type ContextGroupPart } from "../tools/tool-renderer" +import { taskAgentName } from "../tools/tool-renderer" import { SessionRetry } from "../components/session-retry" import { SessionError } from "../components/session-error" import type { TimelineDetail } from "../timeline/detail" @@ -57,7 +57,7 @@ import { contentOpens, diffOpens, goal as goalMessage, thoughtOpens, toolRule, u import { Separator } from "./separator" import { timelineGaps } from "./spacing" import { AssistantContent } from "./tools/assistant-content" -import { ContextGroup } from "./tools/context-group" +import { ContextGroup, type ActivityGroupPart } from "./tools/context-group" import { FileGroup } from "./tools/file-group" import { PatchTool } from "./tools/patch" import { ShellMessage } from "./tools/shell" @@ -335,7 +335,7 @@ export function createNextTimelineRowRenderer(input: { const contents = indexGroupContents(group.refs) const streaming = phase(row().userMessageID).streamingReasoningRef - return group.refs.flatMap((ref) => { + return group.refs.flatMap((ref) => { const content = contents.get(ref.messageID)?.get(ref.partID) if (content?.type === "tool") { @@ -364,6 +364,9 @@ export function createNextTimelineRowRenderer(input: { ] if (ref.messageID !== ref.partID || !message) return [] + const event = noticeEvent(message) + + if (event) return [{ type: "event", id: ref.partID, event }] if (message.type !== "assistant" && message.type !== "user") return [{ type: "notice", id: ref.partID, render: () => }] @@ -402,6 +405,7 @@ export function createNextTimelineRowRenderer(input: { open={input.disclosure.value(key()) === true} onOpenChange={(open) => input.disclosure.set(key(), open)} onSizeChange={onSizeChange} + onRevealShell={input.revealPart} /> ) diff --git a/packages/session-ui/src/timeline-next/rules.ts b/packages/session-ui/src/timeline-next/rules.ts index 506668b30b0..6b55eb4a493 100644 --- a/packages/session-ui/src/timeline-next/rules.ts +++ b/packages/session-ui/src/timeline-next/rules.ts @@ -208,7 +208,7 @@ export function echoesUserShell(message: SessionMessageInfo, userShellIDs: Reado /** * Where a notice shows. What the user did and what changed the session always stands on its own. Background work - * reports its outcome as its own row, following the setting its launch follows: a subagent's the subagent setting, a + * reports its outcome as an event, following the setting its launch follows: a subagent's the subagent setting, a * shell's the shell setting. */ export function noticePlacement(detail: TimelineDetail, message: SessionMessageInfo): TimelinePlacement { diff --git a/packages/session-ui/src/timeline-next/timeline-next.css b/packages/session-ui/src/timeline-next/timeline-next.css index d4a7ac38532..326ace238aa 100644 --- a/packages/session-ui/src/timeline-next/timeline-next.css +++ b/packages/session-ui/src/timeline-next/timeline-next.css @@ -323,13 +323,15 @@ } /* An event row opens to the subagents and shells it counts, each read like a nested row and leading to its session or - to the call that ran it. */ + to the call that ran it. An opened activity group lists the ones it started or heard back from the same way. */ [data-timeline="next"] [data-component="timeline-event-list"] { display: flex; flex-direction: column; gap: 8px; padding-top: 4px; +} +[data-timeline="next"] :is([data-component="timeline-event-list"], [data-slot="context-tool-group-item"]) { [data-slot="timeline-event-item"] { display: flex; align-items: center; @@ -376,7 +378,7 @@ color: var(--v2-text-text-base); } - [data-component="tag"] { + [data-slot="timeline-event-item"] [data-component="tag"] { flex-shrink: 0; } } @@ -517,6 +519,7 @@ } [data-component="context-tool-group-list"] [data-slot="context-tool-group-item"], + [data-component="context-tool-group-list"] [data-slot="timeline-event-item"], [data-component="timeline-event-list"] [data-slot="timeline-event-item"] { min-height: var(--line-height-compact); } diff --git a/packages/session-ui/src/timeline-next/tools/context-group.tsx b/packages/session-ui/src/timeline-next/tools/context-group.tsx index a0532fa76af..24fcc42ba70 100644 --- a/packages/session-ui/src/timeline-next/tools/context-group.tsx +++ b/packages/session-ui/src/timeline-next/tools/context-group.tsx @@ -24,19 +24,26 @@ import { toolMetadata, type ContextGroupPart, } from "../../tools/tool-renderer" -import { kindKeys, launchKeys, summarizeEvents, toolEvent, verbKeys } from "../events" +import { TimelineEventRow } from "../event-row" +import { kindKeys, launchKeys, summarizeEvents, toolEvent, verbKeys, workKey, type TimelineEvent } from "../events" import { toolRule, toolStopped, type ToolMode } from "../rules" import { FileGroup } from "./file-group" import { ThoughtRow } from "./thought" import { ToolDisplay } from "./tool-display" +/** A part of an activity group: a call, a thought, a notice, or background work reporting back. */ +export type ActivityGroupPart = ContextGroupPart | { type: "event"; id: string; event: TimelineEvent } + +// Subagents and background work listed one after another in an opened group, as one nested events row. +type WorkRun = { type: "work"; id: string; events: TimelineEvent[] } + /** * A stretch of the agent's work folded into one row, named by what it did: "Explored 3 files · 2 commands". It only * records what happened; the status line below the turn says what is live. A stretch of thoughts alone is those * thoughts, without a group around them. */ export function ContextGroup(props: { - parts: ContextGroupPart[] + parts: ActivityGroupPart[] open: boolean onOpenChange: (open: boolean) => void onSizeChange?: () => void @@ -49,12 +56,24 @@ export function ContextGroup(props: { fileOpen?: (path: string) => boolean | undefined onFileOpenChange?: (path: string, open: boolean) => void patchGroupKey?: (tools: SessionMessageAssistantTool[]) => string + onRevealShell?: (shellID: string) => void }) { const i18n = useI18n() const tools = createMemo(() => props.parts.filter((part) => part.type === "tool")) const reasoning = createMemo(() => props.parts.flatMap((part) => (part.type === "reasoning" ? [part] : []))) const thoughtsOnly = createMemo(() => reasoning().length > 0 && reasoning().length === props.parts.length) - const activity = createMemo(() => activityLabel(tools(), i18n)) + const partEvents = createMemo(() => workEvents(props.parts)) + const events = createMemo(() => [...partEvents().values()]) + + // A group of only subagents and work reporting back reads as the events row it is, not a group around one. Until a + // call has anything to count, it stays a group, so the row never renders empty. + const workOnly = createMemo( + () => + events().length > 0 && + props.parts.every((part) => part.type === "event" || (part.type === "tool" && toolRule(part).name === "subagent")), + ) + + const activity = createMemo(() => activityLabel(tools(), events(), i18n)) const label = createMemo(() => { const value = activity() @@ -69,16 +88,40 @@ export function ContextGroup(props: { const items = createMemo(() => (props.open ? props.parts : []).reduce< - (SessionMessageAssistantTool[] | Exclude)[] + ( + | SessionMessageAssistantTool[] + | Exclude + | WorkRun + )[] >((groups, tool) => { + const previous = groups.at(-1) + + // Subagents started one after another, and work reporting back, read as one events row nested in the group: + // "Started 3 subagents". + if (tool.type === "event" || (tool.type === "tool" && toolRule(tool).name === "subagent")) { + // A subagent still being asked for has nothing to list yet, and a report its launch already says is not + // listed twice. + const event = partEvents().get(tool) + + if (!event) return groups + + if (previous && !Array.isArray(previous) && previous.type === "work") { + previous.events.push(event) + + return groups + } + + groups.push({ type: "work", id: tool.id, events: [event] }) + + return groups + } + if (tool.type !== "tool") { groups.push(tool) return groups } - const previous = groups.at(-1) - if (Array.isArray(previous) && previous[0] && joins(previous[0], tool)) { previous.push(tool) @@ -136,113 +179,152 @@ export function ContextGroup(props: { } > -
part.id).join(",")} - > - - }> - {(value) => ( - - {value().verb} - - {value().main} - - 0}> - - {value().others.join(", ")} - - - )} - -
- } - > -
- - {(item) => { - const group = createMemo(() => { - const value = item() - - return Array.isArray(value) ? value : undefined - }) - - const reasoning = createMemo(() => { - const value = item() - - return !Array.isArray(value) && value.type === "reasoning" ? value : undefined - }) - - const callback = createMemo(() => { - const value = item() - - return !Array.isArray(value) && - (value.type === "notice" || value.type === "shell" || value.type === "text") - ? value - : undefined - }) - - return ( - - {(part) =>
{part().render()}
} -
- } - > - {(part) => ( -
- props.onReasoningOpenChange?.(part().id, open)} - onContentRendered={props.onSizeChange} - /> -
- )} - - } - > - {(group) => ( -
- -
- )} - - ) - }} -
+ part.id).join(",")}> +
- - + } + > +
part.id).join(",")} + > + + }> + {(value) => ( + + {value().verb} + + {value().main} + + 0}> + + {value().others.join(", ")} + + + )} + +
+ } + > +
+ + {(item) => { + const group = createMemo(() => { + const value = item() + + return Array.isArray(value) ? value : undefined + }) + + const reasoning = createMemo(() => { + const value = item() + + return !Array.isArray(value) && value.type === "reasoning" ? value : undefined + }) + + const callback = createMemo(() => { + const value = item() + + return !Array.isArray(value) && + (value.type === "notice" || value.type === "shell" || value.type === "text") + ? value + : undefined + }) + + const work = createMemo(() => { + const value = item() + + return !Array.isArray(value) && value.type === "work" ? value : undefined + }) + + return ( + + {(part) =>
{part().render()}
} +
+ } + > + {(work) => ( +
+ { + props.onToolOpenChange?.(`events:${work().id}`, open) + props.onSizeChange?.() + }} + onRevealShell={props.onRevealShell} + /> +
+ )} + + } + > + {(part) => ( +
+ props.onReasoningOpenChange?.(part().id, open)} + onContentRendered={props.onSizeChange} + /> +
+ )} + + } + > + {(group) => ( +
+ +
+ )} + + ) + }} +
+
+ + + ) } @@ -422,7 +504,7 @@ function UsedTitle(props: { label: ReturnType }) { ) } -function usedLabel(parts: ContextGroupPart[], i18n: UiI18n) { +function usedLabel(parts: ActivityGroupPart[], i18n: UiI18n) { const count = parts.filter((part) => part.type === "reasoning").length if (!count) { @@ -451,6 +533,52 @@ function usedLabel(parts: ContextGroupPart[], i18n: UiI18n) { } } +/** + * What each subagent or background shell in a group did, in order. Work that reports back in the group that started + * it reads once, as how it ended: the launch says "Finished" and the report is not counted again. + */ +function workEvents(parts: ActivityGroupPart[]) { + const launch = (part: ActivityGroupPart) => + part.type === "tool" && toolRule(part).layout === "event" ? toolEvent(part) : undefined + + const launched = new Set( + parts.flatMap((part) => { + const event = launch(part) + const key = event && workKey(event) + + return key ? [key] : [] + }), + ) + + const reports = new Map( + parts.flatMap((part) => { + const key = part.type === "event" ? workKey(part.event) : undefined + + return key && part.type === "event" ? [[key, part.event] as const] : [] + }), + ) + + return new Map( + parts.flatMap((part): [ActivityGroupPart, TimelineEvent][] => { + if (part.type === "event") { + const key = workKey(part.event) + + return key && launched.has(key) ? [] : [[part, part.event]] + } + + const event = launch(part) + + if (!event) return [] + const key = workKey(event) + const outcome = key ? reports.get(key) : undefined + + return [ + [part, outcome && event.verb === "started" ? { ...event, verb: outcome.verb, failed: outcome.failed } : event], + ] + }), + ) +} + // The most consequential work names a group: changing files over running commands, and both over reading. const modePriority: ToolMode[] = ["edit", "run", "tool", "explore"] @@ -465,25 +593,16 @@ const decodeChangedPath = Schema.decodeUnknownOption(Schema.Struct({ file: Schem /** * Names a stretch of the agent's work by its main mode, then what its other calls did: "Edited 4 files · 2 commands, - * started 1 shell". Work that runs on is counted by what was started, not among the calls that started it; a stretch of - * only that reads like an events row: "Started 2 subagents". + * started 1 shell". Work that runs on is counted by what happened to it, not among the calls that started it; a + * stretch of only that reads like an events row: "Started 2 subagents · finished 2 subagents". */ -function activityLabel(group: SessionMessageAssistantTool[], i18n: UiI18n) { +function activityLabel(group: SessionMessageAssistantTool[], events: TimelineEvent[], i18n: UiI18n) { const tools = group.filter((tool) => toolRule(tool).layout !== "event") + const launches = summarizeEvents(events).verbs - const launches = summarizeEvents( - group.flatMap((tool) => { - const event = toolRule(tool).layout === "event" ? toolEvent(tool) : undefined - - return event ? [event] : [] - }), - ).verbs - - const launched = launches.flatMap((bucket) => { - const keys = bucket.verb === "started" || bucket.verb === "ran" ? launchKeys[bucket.verb] : undefined - - return keys ? bucket.kinds.map((item) => i18n.plural(keys[item.kind], item.count)) : [] - }) + const launched = launches.flatMap((bucket) => + bucket.kinds.map((item) => i18n.plural(launchKeys[bucket.verb][item.kind], item.count)), + ) const modes = new Set(tools.map((tool) => toolRule(tool).mode)) const mode = modePriority.find((item) => modes.has(item)) diff --git a/packages/ui/src/i18n/en.ts b/packages/ui/src/i18n/en.ts index 18e84db8ab6..ab7904a432d 100644 --- a/packages/ui/src/i18n/en.ts +++ b/packages/ui/src/i18n/en.ts @@ -180,6 +180,14 @@ const source = { "ui.timeline.activity.startedShells.other": "started {{count}} shells", "ui.timeline.activity.startedSubagents.one": "started {{count}} subagent", "ui.timeline.activity.startedSubagents.other": "started {{count}} subagents", + "ui.timeline.activity.finishedShells.one": "finished {{count}} shell", + "ui.timeline.activity.finishedShells.other": "finished {{count}} shells", + "ui.timeline.activity.finishedSubagents.one": "finished {{count}} subagent", + "ui.timeline.activity.finishedSubagents.other": "finished {{count}} subagents", + "ui.timeline.activity.stoppedShells.one": "stopped {{count}} shell", + "ui.timeline.activity.stoppedShells.other": "stopped {{count}} shells", + "ui.timeline.activity.stoppedSubagents.one": "stopped {{count}} subagent", + "ui.timeline.activity.stoppedSubagents.other": "stopped {{count}} subagents", "ui.timeline.event.started": "Started", "ui.timeline.event.finished": "Finished", "ui.timeline.event.stopped": "Stopped",