Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions packages/tui/src/util/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,17 @@ export type ActivitySubGroup = {
parts: ActivityGroupPart[]
}

// Splits one activity group into per-kind blocks in first-appearance order,
// merging same-kind calls even when other kinds interleave (read, bash, read
// -> one Explored block + one Ran block). Reasoning attaches to the block of
// the tool call that follows it, since that is the action it plans; trailing
// reasoning lands in the last block. Block ids derive from the group id and
// the kind, so they stay stable while the run grows during streaming.
// Splits one activity group into per-kind blocks in chronological order: a
// change of activity kind closes the current block, and same-kind work after
// an interleaving kind opens a NEW block (read, bash, read -> Explored, Ran,
// Explored). Reasoning attaches to the block of the tool call that follows it,
// since that is the action it plans; trailing reasoning lands in the last
// block. Block ids are occurrence-indexed (act-<firstToolId>:<kind>:<n>), so
// growing a run during streaming either grows the tail block or appends a new
// one without changing earlier blocks' ids. The ActivityGroup <For> keys rows
// by id (see the comment by subIDs in src/routes/session/index.tsx), so
// streamed updates never remount earlier sub-blocks or reset their collapse
// state (which is keyed on the same id).
export function computeActivitySubGroups(group: ActivityGroup): ActivitySubGroup[] {
const toolKinds = group.parts.map((item) =>
item.part.type === "tool" ? activityKindOf(item.part.tool) : undefined,
Expand All @@ -247,20 +252,21 @@ export function computeActivitySubGroups(group: ActivityGroup): ActivitySubGroup
}
kinds[i] = nextTool[i] ?? prev ?? "misc"
}
const order: ActivityKind[] = []
const byKind = new Map<ActivityKind, ActivitySubGroup>()
const subs: ActivitySubGroup[] = []
const occurrence = new Map<ActivityKind, number>()
let current: ActivitySubGroup | undefined
for (let i = 0; i < group.parts.length; i++) {
const kind = kinds[i]!
let sub = byKind.get(kind)
if (!sub) {
sub = { id: `${group.id}:${kind}`, kind, items: [], parts: [] }
byKind.set(kind, sub)
order.push(kind)
if (!current || current.kind !== kind) {
const n = (occurrence.get(kind) ?? 0) + 1
occurrence.set(kind, n)
current = { id: `${group.id}:${kind}:${n}`, kind, items: [], parts: [] }
subs.push(current)
}
sub.parts.push(group.parts[i]!)
if (group.parts[i]!.part.type === "tool") sub.items.push(group.parts[i] as ActivityItem)
current.parts.push(group.parts[i]!)
if (group.parts[i]!.part.type === "tool") current.items.push(group.parts[i] as ActivityItem)
}
return order.map((kind) => byKind.get(kind)!)
return subs
}

// The target a call counts towards in an Explored/Edited header. Distinct
Expand Down
27 changes: 22 additions & 5 deletions packages/tui/test/activity-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,34 @@ describe("typedActivityHeader", () => {
})

describe("computeActivitySubGroups", () => {
test("merges interleaved kinds in first-appearance order", () => {
test("splits interleaved kinds chronologically: a kind change closes the block", () => {
const group = groupOf([
tool("a", "read", { filePath: "src/a.ts" }),
tool("b", "bash", { command: "ls" }),
tool("c", "read", { filePath: "src/b.ts" }),
])
const subs = computeActivitySubGroups(group)
expect(subs.map((sub) => sub.kind)).toEqual(["explore", "bash"])
expect(subs[0]!.items.map((item) => item.part.id)).toEqual(["a", "c"])
expect(subs[0]!.id).toBe("act-1:explore")
expect(subs[1]!.id).toBe("act-1:bash")
expect(subs.map((sub) => sub.kind)).toEqual(["explore", "bash", "explore"])
expect(subs[0]!.items.map((item) => item.part.id)).toEqual(["a"])
expect(subs[1]!.items.map((item) => item.part.id)).toEqual(["b"])
expect(subs[2]!.items.map((item) => item.part.id)).toEqual(["c"])
expect(subs[0]!.id).toBe("act-1:explore:1")
expect(subs[1]!.id).toBe("act-1:bash:1")
expect(subs[2]!.id).toBe("act-1:explore:2")
})

test("write, bash, write opens a new edit block after the command", () => {
const group = groupOf([
tool("a", "write", { filePath: "src/a.ts" }),
tool("b", "bash", { command: "ls" }),
tool("c", "write", { filePath: "src/b.ts" }),
])
const subs = computeActivitySubGroups(group)
expect(subs.map((sub) => [sub.kind, sub.id])).toEqual([
["edit", "act-1:edit:1"],
["bash", "act-1:bash:1"],
["edit", "act-1:edit:2"],
])
})

test("reasoning joins the block of the tool call that follows it", () => {
Expand Down
84 changes: 74 additions & 10 deletions packages/tui/test/cli/tui/activity-group.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { DialogProvider } from "../../../src/ui/dialog"
import { LocationProvider } from "../../../src/context/location"
import { OPENCODE_BASE_MODE, OpencodeKeymapProvider, registerOpencodeKeymap, useBindings } from "../../../src/keymap"
import { ActivityGroup, AssistantMessageRow, SessionContext } from "../../../src/routes/session"
import { computeActivityGroups, resolveActivityExpanded, toggleActivityOverride } from "../../../src/util/activity"
import { computeActivityGroups, computeActivitySubGroups, resolveActivityExpanded, toggleActivityOverride } from "../../../src/util/activity"
import { captureFrame, waitFor, waitUntil } from "../../fixture/frame"

const SESSION = "ses_activity_ui"
Expand Down Expand Up @@ -1606,7 +1606,7 @@ describe("activity group TUI", () => {
}
})

test("splits a mixed run into per-kind blocks in first-appearance order", async () => {
test("splits a mixed run into per-kind blocks in chronological order", async () => {
const { app, sync } = await mountActivity({ height: 30 })
try {
const m1 = assistant("m1", at(1))
Expand All @@ -1619,29 +1619,93 @@ describe("activity group TUI", () => {
await waitFrame(app, (frame: string) => frame.includes("Working... 1 tool call"))

const frame = await frameOf(app)
// Interleaved kinds merge: the two reads share one Explored block, and
// the webfetch call lands in the unchanged misc Working block.
expect(countOf(frame, "Exploring files...")).toBe(1)
// A kind change closes the block: the second read opens its own Explored
// block instead of joining the first one.
expect(countOf(frame, "Running command...")).toBe(1)
expect(countOf(frame, "Exploring files...")).toBe(2)
expect(countOf(frame, "Editing files...")).toBe(1)
expect(frame).toContain("Working... 1 tool call")
expect(rowOf(frame, "Running command...")).toBeLessThan(rowOf(frame, "Exploring files..."))
expect(rowOf(frame, "Exploring files...")).toBeLessThan(rowOf(frame, "Editing files..."))
expect(rowOf(frame, "Editing files...")).toBeLessThan(rowOf(frame, "Working... 1 tool call"))
expect(rowOf(frame, "Running command...")).toBeLessThan(rowOfNth(frame, "Exploring files...", 1))
expect(rowOfNth(frame, "Exploring files...", 1)).toBeLessThan(rowOf(frame, "Editing files..."))
expect(rowOf(frame, "Editing files...")).toBeLessThan(rowOfNth(frame, "Exploring files...", 2))
expect(rowOfNth(frame, "Exploring files...", 2)).toBeLessThan(rowOf(frame, "Working... 1 tool call"))

// Sub-block ids are occurrence indexed, so the two explore blocks differ
// and earlier blocks keep their ids when the run grows.
const rows = (sync.data.message[SESSION] ?? []).map((message) => ({
message,
parts: sync.data.part[message.id] ?? [],
}))
const group = [...computeActivityGroups(rows).byID.values()][0]!
const subs = computeActivitySubGroups(group)
expect(subs.map((sub) => [sub.kind, sub.id])).toEqual([
["bash", `${group.id}:bash:1`],
["explore", `${group.id}:explore:1`],
["edit", `${group.id}:edit:1`],
["explore", `${group.id}:explore:2`],
["misc", `${group.id}:misc:1`],
])

// Each block expands to exactly its own native rows.
await app.mockMouse.click(5, rowOf(frame, "Exploring files..."))
await app.mockMouse.click(5, rowOfNth(frame, "Exploring files...", 1))
await waitFrame(app, (f: string) => f.includes(readRow("src/a.ts")))
const expanded = await frameOf(app)
expect(expanded).toContain(readRow("src/a.ts"))
expect(expanded).toContain(readRow("src/c.ts"))
expect(expanded).not.toContain(readRow("src/c.ts"))
expect(expanded).not.toContain("bun test")
expect(expanded).not.toContain("example.com")
} finally {
app.renderer.destroy()
}
})

test("opens a new Edited block after a command: Edited -> Ran -> Edited(new)", async () => {
const { app, sync } = await mountActivity({ height: 30 })
try {
const m1 = assistant("m1", at(1))
const settled = (tool: string, input: Record<string, unknown>, start: number, end: number): ToolPart =>
completed(toolPart(m1.id, tool, input, { status: "running", input, time: { start } }), start, end)
seed(sync, [
{
message: m1,
parts: [
settled("write", { filePath: "src/a.ts" }, 1000, 1100),
settled("bash", { command: "ls" }, 1100, 1200),
settled("write", { filePath: "src/b.ts" }, 1200, 1300),
],
},
])
await waitFrame(app, (frame: string) => frame.includes("Edited 1 file") && frame.includes("Ran 1 command"))

const frame = await frameOf(app)
// On-screen chronological order: Edited, Ran, Edited(new) — the second
// write opens a new block instead of joining the one above the command.
expect(countOf(frame, "Edited 1 file")).toBe(2)
expect(countOf(frame, "Ran 1 command")).toBe(1)
expect(rowOf(frame, "Edited 1 file")).toBeLessThan(rowOf(frame, "Ran 1 command"))
expect(rowOf(frame, "Ran 1 command")).toBeLessThan(rowOfNth(frame, "Edited 1 file", 2))
expect(frame).not.toContain(readRow("src/a.ts"))
expect(frame).not.toContain("ls")

// The two Edited blocks are distinct sub-blocks: edit:1, bash:1, edit:2.
const groupRows = (sync.data.message[SESSION] ?? []).map((message) => ({
message,
parts: sync.data.part[message.id] ?? [],
}))
const group = [...computeActivityGroups(groupRows).byID.values()][0]!
const subs = computeActivitySubGroups(group)
expect(subs.map((sub) => sub.kind)).toEqual(["edit", "bash", "edit"])
expect(subs.map((sub) => sub.id)).toEqual([
`${group.id}:edit:1`,
`${group.id}:bash:1`,
`${group.id}:edit:2`,
])
expect(new Set(subs.map((sub) => sub.id)).size).toBe(3)
} finally {
app.renderer.destroy()
}
})

test("counts distinct targets in Explored and Edited summaries", async () => {
const { app, sync } = await mountActivity({ height: 30 })
try {
Expand Down
Loading