feat(session-ui): count a script's steps instead of the script with full_codemode (#66650)

This commit is contained in:
James Long 2026-11-26 14:22:00 -05:00 • committed by GitHub
parent 8876471936
commit 82d8fc5bec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 275 additions and 18 deletions

View file

@ -18,7 +18,7 @@ const modeVerb = {
* A group's parts as facts. What background work reported counts as its outcome; notices count nothing. A script's
* files count where they are placed, so only those in the group.
*/
export function activityFacts(parts: readonly ActivityGroupPart[]) {
export function activityFacts(parts: readonly ActivityGroupPart[], scriptSteps = false) {
const facts = factsOf(
parts.flatMap((part, index): FactPart[] => {
// Thoughts one after another count as one, as they show.
@ -31,6 +31,8 @@ export function activityFacts(parts: readonly ActivityGroupPart[]) {
return [{ type: "outcome", id: part.id, outcome: { kind, verb, failed, key: workKey(part.event) } }]
}),
true,
scriptSteps,
)
return [
@ -43,12 +45,18 @@ export function activityFacts(parts: readonly ActivityGroupPart[]) {
* 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". A background shell is counted once by its outcome.
*/
export function activityLabel(parts: readonly ActivityGroupPart[], i18n: UiI18n) {
const summary = summarize(activityFacts(parts))
export function activityLabel(parts: readonly ActivityGroupPart[], i18n: UiI18n, scriptSteps = false) {
const summary = summarize(activityFacts(parts, scriptSteps))
const launches = summary.background.verbs
const launched = launches.map((bucket) => i18n.plural(launchKeys[bucket.verb], bucket.count))
// Files a script changed name the group as edits do, where they are placed in it.
const mode = modePriority.find((item) => summary.modes[item] > 0 || (item === "edit" && summary.files.length > 0))
// Counting a script's steps, an edit step leads only once its files have landed.
const mode = modePriority.find((item) =>
item === "edit" && scriptSteps
? summary.files.length > 0
: summary.modes[item] > 0 || (item === "edit" && summary.files.length > 0),
)
if (!mode) {
const [first, ...rest] = launches

View file

@ -5,6 +5,7 @@ import { type UiI18n, useI18n } from "@opencode/ui/context/i18n"
import { TextShimmer } from "@opencode/ui/text-shimmer"
import { createMemo, For, Index, Match, Show, Switch } from "solid-js"
import { BasicTool } from "../../components/basic-tool"
import { useData } from "../../context"
import {
currentToolError,
currentToolGroupedRead,
@ -66,13 +67,14 @@ export function ContextGroup(props: {
onRevealShell?: (shellID: string) => void
}) {
const i18n = useI18n()
const data = useData()
const reasoning = createMemo(() => props.parts.flatMap((part) => (part.type === "reasoning" ? [part] : [])))
const thoughtsOnly = createMemo(() => reasoning().length > 0 && reasoning().length === props.parts.length)
const events = createMemo(() => [...workEvents(props.parts).values()])
const workOnly = createMemo(() => events().length > 0 && props.parts.every((part) => part.type === "event"))
const activity = createMemo(() => activityLabel(props.parts, i18n))
const activity = createMemo(() => activityLabel(props.parts, i18n, data.flag("full_codemode")))
const label = createMemo(() => activityText(activity()) ?? usedLabel(props.parts, i18n))

View file

@ -1,3 +1,4 @@
import type { JsonValue } from "@opencode/client/promise"
import { describe, expect, test } from "bun:test"
import { useI18n } from "@opencode/ui/context/i18n"
import { storyTool } from "../../storybook/current-session-scenarios"
@ -93,4 +94,88 @@ describe("script labels", () => {
expect(next([script, ...files])).toBe("Edited 3 files · 1 command")
expect(next([script])).toBe("Ran 1 command")
})
describe("counting a script's steps", () => {
const step = (tool: string, input: Record<string, string>, status = "completed") => ({ tool, status, input })
const changes = script.state.status === "completed" ? (script.state.metadata?.files ?? []) : []
const stepped = (toolCalls: JsonValue[], status: "running" | "completed" = "completed") =>
storyTool("exec_1", "execute", status, { code: "x" }, { metadata: { files: changes, toolCalls } })
const steps = stepped([
step("read", { path: "/repo/a.ts" }),
step("shell", { command: "ls" }),
step("edit", { path: "/repo/a.ts" }),
step("write", { path: "/repo/b.ts" }),
step("patch", { patchText: "x" }),
step("Bun", { method: "write", path: "/repo/c.bin" }),
])
const stepsNext = (parts: ActivityGroupPart[]) =>
(activityText(activityLabel(parts, i18n, true)) ?? usedLabel(parts, i18n)).text
test("Compact: a script beside its files counts its steps, with no patch", () => {
expect(contextGroupLabel([steps, ...files], i18n, true).text).toBe("Used 6 Read, Shell, Edit, Write, Patch")
expect(stepsNext([steps, ...files])).toBe("Edited 3 files · 1 command, 1 read")
})
test("Quiet: a hidden script's files still read as a patch", () => {
expect(contextGroupLabel(files, i18n, true).text).toBe("Used 1 Patch")
expect(stepsNext(files)).toBe("Edited 3 files")
})
test("a script with no steps to count counts as itself", () => {
expect(contextGroupLabel([stepped([], "running")], i18n, true).text).toBe("Used 1 Execute")
expect(contextGroupLabel([stepped([step("Bun", { method: "spawn" })])], i18n, true).text).toBe("Used 1 Execute")
})
test("live Bun steps count as recorded, and old Bun writes as edits", () => {
const live = storyTool(
"exec_3",
"execute",
"completed",
{ code: "x" },
{
metadata: {
toolCalls: [
step("read", { path: "/repo/a.ts" }),
step("write", { path: "/repo/b.ts" }),
step("glob", { pattern: "src/**/*.ts" }),
],
files: [{ file: "/repo/b.ts", status: "added", additions: 1, deletions: 0 }],
},
},
)
const liveFiles = factsOf([live]).flatMap((fact) =>
fact.kind === "file" && fact.via === "script"
? [{ type: "file" as const, id: fact.id, file: fact, render: () => null }]
: [],
)
expect(contextGroupLabel([live, ...liveFiles], i18n, true).text).toBe("Used 3 Read, Write, Glob")
expect(stepsNext([live, ...liveFiles])).toBe("Edited b.ts · 1 read, 1 search")
expect(
contextGroupLabel([stepped([step("Bun", { method: "write", path: "/repo/b.ts" })])], i18n, true).text,
).toBe("Used 1 Edit")
})
test("an edit step before its files land does not name the group as edits", () => {
const running = storyTool(
"exec_2",
"execute",
"running",
{ code: "x" },
{
metadata: {
files: [],
toolCalls: [step("read", { path: "/repo/a.ts" }), step("edit", { path: "/repo/a.ts" }, "running")],
},
},
)
expect(stepsNext([running])).not.toContain("Edited")
})
})
})

View file

@ -55,6 +55,91 @@ describe("timeline facts", () => {
})
})
test("counts a script's steps in place of the script with scriptSteps", () => {
const step = (tool: string, input: Record<string, JsonValue>) => ({ tool, status: "completed", input })
const script = storyTool(
"x1",
"execute",
"completed",
{},
{
metadata: {
toolCalls: [
step("read", { path: "/repo/a.ts" }),
step("read", { path: "/repo/b.ts" }),
step("shell", { command: "ls" }),
step("edit", { path: "/repo/c.ts" }),
step("Bun", { method: "write", path: "/repo/c.ts" }),
step("Bun", { method: "file", path: "/repo/a.ts" }),
step("Bun", { method: "other", path: "/repo/d.ts" }),
],
files: [{ file: "/repo/c.ts", additions: 1 }],
},
},
)
const facts = factsOf([script], true, true)
expect(facts.flatMap((fact) => (fact.kind === "call" ? [[fact.tool, fact.mode]] : []))).toEqual([
["read", "explore"],
["read", "explore"],
["shell", "run"],
["edit", "edit"],
["edit", "edit"],
["read", "explore"],
])
expect(summarize(facts)).toMatchObject({
calls: 6,
reads: 2,
commands: 1,
files: ["/repo/c.ts"],
modes: { explore: 3, edit: 2, run: 1, tool: 0 },
})
expect(summarize(factsOf([script], true, false))).toMatchObject({
calls: 1,
reads: 0,
commands: 1,
files: ["/repo/c.ts"],
toolNames: [{ tool: "execute" }],
})
const direct = [
storyTool("r1", "read", "completed", { path: "/repo/a.ts" }),
storyTool("e1", "edit", "completed", { path: "/repo/c.ts" }),
]
expect(factsOf(direct, true, true)).toEqual(factsOf(direct))
})
test("counts live Bun steps as recorded", () => {
const script = storyTool(
"x1",
"execute",
"completed",
{},
{
metadata: {
toolCalls: [
{ tool: "read", status: "completed", input: { path: "/repo/a.ts" } },
{ tool: "write", status: "completed", input: { path: "/repo/b.ts" } },
{ tool: "glob", status: "completed", input: { pattern: "src/**/*.ts" } },
],
files: [{ file: "/repo/b.ts" }],
},
},
)
expect(summarize(factsOf([script], true, true))).toMatchObject({
calls: 3,
reads: 1,
searches: 1,
commands: 0,
files: ["/repo/b.ts"],
modes: { explore: 2, edit: 1, run: 0, tool: 0 },
})
})
test("follows a part updated in place", () => {
const part = storyTool("s1", "shell", "running", { command: "false" })
const before = summarize(factsOf([part]))

View file

@ -121,13 +121,14 @@ const modes = new Map<string, FactMode>(
/**
* The facts in a list of parts, in order. Not cached: stores update a part in place as it streams, so its identity
* says nothing about whether its facts changed. Callers read it inside a memo. Without `scriptDiffs` (the server's
* `full_codemode` flag off), a script's changes are not facts: they neither show nor count.
* `full_codemode` flag off), a script's changes are not facts: they neither show nor count. With `scriptSteps`, a script
* counts as the calls it made rather than as itself.
*/
export function factsOf(parts: readonly FactPart[], scriptDiffs = true): Fact[] {
return parts.flatMap((part) => partFacts(part, scriptDiffs))
export function factsOf(parts: readonly FactPart[], scriptDiffs = true, scriptSteps = false): Fact[] {
return parts.flatMap((part) => partFacts(part, scriptDiffs, scriptSteps))
}
function partFacts(part: FactPart, scriptDiffs: boolean): Fact[] {
function partFacts(part: FactPart, scriptDiffs: boolean, scriptSteps: boolean): Fact[] {
const id = part.messageID ? `${part.messageID}:${part.id ?? ""}` : (part.id ?? "")
if (part.type === "text") return [{ kind: "text", id }]
@ -156,7 +157,7 @@ function partFacts(part: FactPart, scriptDiffs: boolean): Fact[] {
},
]
return toolFacts(part, id, scriptDiffs)
return toolFacts(part, id, scriptDiffs, scriptSteps)
}
const decodeFile = Schema.decodeUnknownOption(
@ -257,7 +258,7 @@ export function launchOf(tool: Tool) {
return placeFact(tool, tool.id, read).background ? launch(tool, toolName(tool.name), read) : undefined
}
function toolFacts(tool: Tool, id: string, scriptDiffs: boolean): Fact[] {
function toolFacts(tool: Tool, id: string, scriptDiffs: boolean, scriptSteps = false): Fact[] {
const read = reads(tool)
const fact = placeFact(tool, id, read, scriptDiffs)
const raw = read.input().path
@ -282,9 +283,12 @@ function toolFacts(tool: Tool, id: string, scriptDiffs: boolean): Fact[] {
if (fact.name === "execute" && !scriptDiffs) return [call]
const steps = fact.name === "execute" && scriptSteps ? stepFacts(metadata, id) : []
if (fact.name === "execute")
return [
call,
// A script with no steps to count yet, or none at all, counts as itself.
...(steps.length ? steps : [call]),
// One fact per change, in order: a script can change the same file more than once.
...scriptChanges(tool, read).map((file, index) => scriptFileFactOf(file, index, id, state.status)),
]
@ -300,6 +304,53 @@ function toolFacts(tool: Tool, id: string, scriptDiffs: boolean): Fact[] {
return [call, ...files.map((file) => fileFact(file, id, state.status, "direct"))]
}
// The tool a script's `Bun` file call counts as: `Bun.write` edits a file and `Bun.file` reads one.
const bunTools = new Map([
["write", "edit"],
["file", "read"],
])
// A script's steps, each counted as a direct call of its tool. Other `Bun` calls count nothing. A script can make
// thousands of calls, so they are read as they are rather than decoded.
function stepFacts(metadata: Schema.JsonObject, id: string): CallFact[] {
const calls = Array.isArray(metadata.toolCalls) ? metadata.toolCalls : []
return calls.flatMap((call, index) => {
if (!Predicate.isObject(call) || !Predicate.isString(call.tool)) return []
const status = call.status
if (status !== "running" && status !== "completed" && status !== "error") return []
const input = Predicate.isObject(call.input) ? call.input : {}
const bun = call.tool === "Bun" && Predicate.isString(input.method) ? bunTools.get(input.method) : undefined
if (call.tool === "Bun" && !bun) return []
const name = bun ?? toolName(call.tool)
const fact: CallFact = {
kind: "call",
id: `${id}:step:${index}`,
name,
tool: bun ?? call.tool,
mode: modes.get(name) ?? "tool",
status,
failed: status === "error",
stopped: false,
called: true,
groupedRead: name === "read",
// Code Mode's shell has no background.
background: false,
changes: 0,
}
if (Predicate.isString(input.path) && input.path) fact.path = input.path
return [fact]
})
}
function fileFact(
file: { file: string; additions?: number; deletions?: number; status?: FileFact["change"]; patch?: string },
by: string,

View file

@ -34,4 +34,29 @@ describe("group labels", () => {
expect(contextGroupLabel(parts, i18n).text).toBe("Used 2 Shell, Read")
expect(contextGroupLabel([{ type: "notice", id: "n1", render: () => null }], i18n).text).toBe("Updates")
})
test("the current timeline counts a script's steps, without a patch for its changes, with scriptSteps", () => {
const step = (tool: string, input: Record<string, string>) => ({ tool, status: "completed", input })
const script = storyTool(
"x1",
"execute",
"completed",
{},
{
metadata: {
toolCalls: [
step("read", { path: "/repo/a.ts" }),
step("shell", { command: "ls" }),
step("edit", { path: "/repo/b.ts" }),
step("Bun", { method: "write", path: "/repo/c.ts" }),
],
files: [{ file: "/repo/b.ts" }, { file: "/repo/c.ts" }],
},
},
)
expect(contextGroupLabel([script], i18n, true).text).toBe("Used 4 Read, Shell, Edit")
expect(contextGroupLabel([script], i18n).text).toBe("Used 1 Execute")
})
})

View file

@ -56,7 +56,7 @@ function toolTitle(tool: Summary["toolNames"][number], i18n: UiI18n) {
* A context group's parts as facts. A user's shell command counts as a call; notices and text count nothing. A script's
* files count where they are placed, so only those in the group.
*/
export function contextGroupFacts(parts: readonly ContextGroupPart[]) {
export function contextGroupFacts(parts: readonly ContextGroupPart[], scriptSteps = false) {
return parts.flatMap((part) => {
if (part.type === "file") return [part.file]
@ -67,17 +67,17 @@ export function contextGroupFacts(parts: readonly ContextGroupPart[]) {
? [{ type: "shell", id: part.id }]
: []
return factsOf(own).filter((fact) => fact.kind !== "file" || fact.via !== "script")
return factsOf(own, true, scriptSteps).filter((fact) => fact.kind !== "file" || fact.via !== "script")
})
}
/** The closed label of a group of calls and thoughts: "Used 3 Read, Grep". */
export function contextGroupLabel(parts: readonly ContextGroupPart[], i18n: UiI18n) {
const facts = contextGroupFacts(parts)
export function contextGroupLabel(parts: readonly ContextGroupPart[], i18n: UiI18n, scriptSteps = false) {
const facts = contextGroupFacts(parts, scriptSteps)
const summary = summarize(facts)
const thoughts = summary.thoughts
// Rows pass calls by their part id and a script's changes by message and part id, so a change names its script by either.
const calls = new Set(facts.flatMap((fact) => (fact.kind === "call" ? [fact.id] : [])))
const calls = new Set(parts.flatMap((part) => (part.type === "tool" ? [part.id] : [])))
const shown = (by: string) => calls.has(by) || calls.has(by.slice(by.indexOf(":") + 1))
// A hidden script's changes read as the patch a direct edit shows as, one call per script, in the order they came.

View file

@ -387,6 +387,7 @@ export function CurrentContextToolGroup(props: {
patchGroupKey?: (tools: SessionMessageAssistantTool[]) => string
}) {
const i18n = useI18n()
const data = useData()
const tools = createMemo(() => props.parts.filter((part) => part.type === "tool"))
const pending = createMemo(
@ -434,7 +435,7 @@ export function CurrentContextToolGroup(props: {
return { text, title: text, before: "", count: undefined, between: "", after: "" }
}
return contextGroupLabel(props.parts, i18n)
return contextGroupLabel(props.parts, i18n, data.flag("full_codemode"))
})
const items = createMemo(() =>