Repository navigation
fix(web): batch subagent rows per user message instead of per turn #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1927,6 +1927,60 @@ describe("deriveTimelineEntries", () => { | |
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("batches a turn's direct spawns per segment, so a steer splits the CTA row", () => { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 新增测试:同一 turn 里 steer 前后各生成两个子代理,期望得到两行,分别是 [task-a, task-b] 和 [task-c, task-d],第一行保留 spawn-1 的 id、时间和 turn。 |
||
| const userMessage = (id: string, createdAt: string) => ({ | ||
| id: MessageId.make(id), | ||
| role: "user" as const, | ||
| text: "go", | ||
| createdAt, | ||
| turnId: null, | ||
| updatedAt: createdAt, | ||
| streaming: false, | ||
| }); | ||
| const spawn = (id: string, createdAt: string, taskId: string) => ({ | ||
| id, | ||
| createdAt, | ||
| label: "Kicked off subagent", | ||
| tone: "info" as const, | ||
| turnId: TurnId.make("turn-1"), | ||
| taskId, | ||
| agentSpawn: { workflowId: null, agentTaskIds: [taskId] }, | ||
| }); | ||
|
|
||
| const entries = deriveTimelineEntries( | ||
| [ | ||
| userMessage("user-1", "2026-02-23T00:00:00.000Z"), | ||
| userMessage("user-steer", "2026-02-23T00:00:03.000Z"), | ||
| ], | ||
| [], | ||
| [ | ||
| spawn("spawn-1", "2026-02-23T00:00:01.000Z", "task-a"), | ||
| spawn("spawn-2", "2026-02-23T00:00:02.000Z", "task-b"), | ||
| spawn("spawn-3", "2026-02-23T00:00:04.000Z", "task-c"), | ||
| spawn("spawn-4", "2026-02-23T00:00:05.000Z", "task-d"), | ||
| ], | ||
| ); | ||
|
|
||
| expect(entries.map((entry) => entry.id)).toEqual([ | ||
| "user-1", | ||
| "spawn-1", | ||
| "user-steer", | ||
| "spawn-3", | ||
| ]); | ||
| expect( | ||
| entries.flatMap((entry) => (entry.kind === "work" ? [entry.entry.agentSpawn] : [])), | ||
| ).toEqual([ | ||
| { workflowId: null, agentTaskIds: ["task-a", "task-b"] }, | ||
| { workflowId: null, agentTaskIds: ["task-c", "task-d"] }, | ||
| ]); | ||
| // The surviving row keeps the first spawn's identity, so the CTA renders | ||
| // where the fleet started. | ||
| expect(entries[1]).toMatchObject({ | ||
| createdAt: "2026-02-23T00:00:01.000Z", | ||
| entry: { id: "spawn-1", turnId: "turn-1" }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("deriveWorkLogEntries context window handling", () => { | ||
|
|
@@ -2135,12 +2189,18 @@ describe("deriveWorkLogEntries quiet-timeline guarantee", () => { | |
| } | ||
|
|
||
| const entries = deriveWorkLogEntries(activities); | ||
| // A1 CTA design: all direct spawns in one turn collapse into ONE | ||
| // call-to-action row carrying the batch's agent ids. | ||
| // A1 CTA design: every tick and completion of one agent collapses into | ||
| // that agent's single call-to-action row. Batching those rows into one | ||
| // "Kicked off 5 subagents" CTA happens per timeline segment, later. | ||
| const spawnRows = entries.filter((entry) => entry.agentSpawn !== undefined); | ||
| expect(spawnRows).toHaveLength(1); | ||
| expect(spawnRows[0]!.agentSpawn!.agentTaskIds).toHaveLength(5); | ||
| expect(spawnRows[0]!.agentSpawn!.workflowId).toBeNull(); | ||
| expect(spawnRows.map((row) => row.agentSpawn!.agentTaskIds)).toEqual([ | ||
| ["task-0"], | ||
| ["task-1"], | ||
| ["task-2"], | ||
| ["task-3"], | ||
| ["task-4"], | ||
| ]); | ||
| expect(spawnRows.every((row) => row.agentSpawn!.workflowId === null)).toBe(true); | ||
| // No agent-attributed tool rows leak into the main log. | ||
| expect(entries.some((entry) => entry.sourceActivityKind?.startsWith("tool."))).toBe(false); | ||
| }); | ||
|
|
@@ -2188,9 +2248,7 @@ describe("deriveWorkLogEntries quiet-timeline guarantee", () => { | |
| expect(entries).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("folds timelineBypass agent rows into one CTA (Codex children, workflow members)", () => { | ||
| // Codex children carry their parent's spawn turn (spawnTurnId stamping), | ||
| // which is what batches a fleet into one CTA. | ||
| it("keeps timelineBypass agent rows as CTA rows (Codex children, workflow members)", () => { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codex 子代理和工作流成员的测试:work log 层现在每个直接生成的子代理各一行,所以期望值从一行改成每个子代理一行;工作流成员仍合并成一行。 |
||
| const entries = deriveWorkLogEntries([ | ||
| makeActivity({ | ||
| kind: "task.progress", | ||
|
|
@@ -2207,10 +2265,12 @@ describe("deriveWorkLogEntries quiet-timeline guarantee", () => { | |
| turnId: "turn-spawn", | ||
| }), | ||
| ]); | ||
| // Not suppressed outright (a Codex fleet's rows are ALL bypassed and | ||
| // still need a CTA anchor) — but never more than the batch's single row. | ||
| expect(entries).toHaveLength(1); | ||
| expect(entries[0]!.agentSpawn?.agentTaskIds).toEqual(["child-1", "child-2"]); | ||
| // Not suppressed outright: a Codex fleet's rows are ALL bypassed and | ||
| // still need a CTA anchor — one per child, never one per progress tick. | ||
| expect(entries.map((entry) => entry.agentSpawn?.agentTaskIds)).toEqual([ | ||
| ["child-1"], | ||
| ["child-2"], | ||
| ]); | ||
| }); | ||
|
|
||
| it("timelineBypass non-agent rows (background shells) stay suppressed", () => { | ||
|
|
@@ -2245,9 +2305,9 @@ describe("deriveWorkLogEntries quiet-timeline guarantee", () => { | |
| }); | ||
|
|
||
| describe("rerun workflows", () => { | ||
| it("turn-less direct spawns do not collapse into one global batch", () => { | ||
| // Rows that lost their turn id (defensive path) group per task, so two | ||
| // unrelated turn-less spawns never merge into one immortal CTA. | ||
| it("direct spawns do not collapse into one global batch", () => { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 原来的“无 turn id 兜底”测试改写成新规则:每个直接生成的子代理在 work log 层各自一行。 |
||
| // Direct spawns group per task, so two unrelated spawns never merge into | ||
| // one immortal CTA accumulating every agent the thread ever ran. | ||
| const entries = deriveWorkLogEntries([ | ||
| makeActivity({ | ||
| kind: "task.started", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,8 +90,8 @@ export interface WorkLogEntry { | |
| /** Agent role (subagent_type) for labeled timeline rows. */ | ||
| agentRole?: string; | ||
| /** | ||
| * Present on agent-spawn CTA rows: one per workflow run or per-turn batch | ||
| * of direct spawns. The row renders as a call-to-action ("Kicked off N | ||
| * Present on agent-spawn CTA rows: one per workflow run or per-segment | ||
|
orbitingflea marked this conversation as resolved.
Resolved
|
||
| * batch of direct spawns. The row renders as a call-to-action ("Kicked off N | ||
| * subagents") whose live status is derived from the agent panel model at | ||
| * render time; clicking opens the Agents panel. | ||
| */ | ||
|
|
@@ -987,7 +987,9 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo | |
|
|
||
| /** | ||
| * Spawn-group key for a subagent lifecycle row. Workflow members and their | ||
| * coordinator share the coordinator's group; direct spawns batch per turn. | ||
| * coordinator share the coordinator's group; a direct spawn is its own group, | ||
| * so its progress and completion rows collapse into its own row. Direct | ||
| * spawns batch one step later, per timeline segment, in deriveTimelineEntries. | ||
|
orbitingflea marked this conversation as resolved.
Resolved
|
||
| * One CTA row per group (A1 design): "Kicked off N subagents". | ||
| */ | ||
| function agentSpawnGroupKey(entry: DerivedWorkLogEntry): string { | ||
|
|
@@ -1002,13 +1004,7 @@ function agentSpawnGroupKey(entry: DerivedWorkLogEntry): string { | |
| if (entry.isWorkflowCoordinator) { | ||
| return `wf:${taskId}`; | ||
| } | ||
| // No turn id means no batch signal at all: fall back to one group per | ||
| // task. Unrelated turn-less spawns (separate fleets whose rows lost their | ||
| // turn) must not collapse into one immortal "direct:no-turn" CTA | ||
| // accumulating every agent the thread ever ran (review finding). Adapters | ||
| // stamp spawn turns (Codex spawnTurnId; Claude rows ride real turns), so | ||
| // this path is defensive. | ||
| return entry.turnId ? `direct:${entry.turnId}` : `direct:task:${taskId}`; | ||
| return `direct:task:${taskId}`; | ||
|
orbitingflea marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| function toolLifecycleCollapseMapKey(entry: DerivedWorkLogEntry): string | undefined { | ||
|
|
@@ -1026,16 +1022,14 @@ function collapseDerivedWorkLogEntries( | |
| ): DerivedWorkLogEntry[] { | ||
| const collapsed: DerivedWorkLogEntry[] = []; | ||
| // Subagent rows collapse by spawn group, not adjacency: a workflow run (or | ||
| // a turn's batch of direct spawns) is ONE narrative event in the chat — a | ||
| // CTA row that opens the Agents panel — no matter how many agents it | ||
| // contains or how their progress rows interleave (quiet-timeline | ||
| // guarantee). | ||
| // a single direct spawn) is ONE narrative event in the chat — a CTA row | ||
|
orbitingflea marked this conversation as resolved.
Resolved
|
||
| // that opens the Agents panel — no matter how many progress rows it emits | ||
| // or how they interleave (quiet-timeline guarantee). | ||
| const spawnRowIndex = new Map<string, number>(); | ||
| // Batch membership is decided once, at the FIRST row seen for a taskId. | ||
| // Claude background subagents settle between turns, so their completion | ||
| // rows carry fresh synthetic turn ids (or none) — keying each row by its | ||
| // own turn splintered one batch into a stream of "Kicked off N subagents" | ||
| // rows (live-test finding, thread 7ac7ef05). | ||
| // Group membership is decided once, at the FIRST row seen for a taskId: a | ||
| // later row can lose the fields the key reads (a workflow coordinator's | ||
| // completion payload may drop its workflow name) and must stay in the | ||
| // group its spawn joined. | ||
| const groupKeyByTaskId = new Map<string, string>(); | ||
| const toolLifecycleRowIndex = new Map<string, number>(); | ||
| for (const entry of entries) { | ||
|
|
@@ -1807,11 +1801,44 @@ export function deriveTimelineEntries( | |
| createdAt: entry.createdAt, | ||
| entry, | ||
| })); | ||
| return [...messageRows, ...proposedPlanRows, ...workRows].toSorted((a, b) => | ||
| a.createdAt.localeCompare(b.createdAt), | ||
| return batchDirectSpawnsPerSegment( | ||
|
orbitingflea marked this conversation as resolved.
Resolved
|
||
| [...messageRows, ...proposedPlanRows, ...workRows].toSorted((a, b) => | ||
| a.createdAt.localeCompare(b.createdAt), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * User messages cut the timeline into segments, and the direct spawns a | ||
| * segment holds are one launch: later ones merge into the segment's first | ||
| * spawn row, which keeps its id, time and turn so the CTA renders where the | ||
| * fleet started. A steer opens a new segment, so the agents kicked off before | ||
| * it and the ones it caused each get their own CTA row. | ||
| */ | ||
| function batchDirectSpawnsPerSegment(entries: ReadonlyArray<TimelineEntry>): TimelineEntry[] { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 新增的合并函数:顺序扫一遍时间线,遇到用户消息就清空锚点;遇到直接生成的子代理行,第一条成为该 segment 的锚点并保留,后面的把自己的 agentTaskIds 并进锚点然后被过滤掉。锚点保留原来的 id、时间和 turn,所以行显示在第一个子代理启动的位置。工作流子代理(workflowId 非 null)不参与,仍然一行一个工作流。 |
||
| let anchor: Extract<TimelineEntry, { kind: "work" }> | null = null; | ||
| return entries.filter((entry) => { | ||
| if (entry.kind === "message" && entry.message.role === "user") { | ||
| anchor = null; | ||
| } else if (entry.kind === "work" && entry.entry.agentSpawn?.workflowId === null) { | ||
| if (anchor === null) { | ||
| anchor = entry; | ||
| } else { | ||
| const agentTaskIds = new Set([ | ||
| ...anchor.entry.agentSpawn!.agentTaskIds, | ||
| ...entry.entry.agentSpawn.agentTaskIds, | ||
| ]); | ||
| anchor.entry = { | ||
| ...anchor.entry, | ||
| agentSpawn: { workflowId: null, agentTaskIds: [...agentTaskIds] }, | ||
| }; | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| export function inferCheckpointTurnCountByTurnId( | ||
| summaries: ReadonlyArray<TurnDiffSummary>, | ||
| ): Record<TurnId, number> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来这个测试断言折叠层会合并两条子代理行;现在折叠层不再合并,改为断言两条行都保持可见、不进折叠。