import { describe, expect, test } from "bun:test" import { Predicate } from "effect" import type { SessionMessageAssistant, SessionMessageAssistantTool, SessionMessageInfo } from "@opencode/client/promise" import { commentaryRuns } from "../storybook/current-session-fixtures" import { storyDocument, storyTool } from "../storybook/current-session-scenarios" import { timelinePresets } from "./detail" import { createTimelineProjection, Timeline, TimelineRow } from "./projection" describe("current session timeline rows", () => { test("derives turns and tagged rows from chronological current messages", () => { const source = [ { id: "msg_1", type: "user", text: "first", time: { created: 1 } }, { id: "msg_2", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "answer" }], time: { created: 2, completed: 3 }, }, { id: "msg_3", type: "user", text: "second", time: { created: 4 } }, { id: "msg_4", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "reasoning", text: "working" }], time: { created: 5 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "busy" }) expect(result.activeMessageID).toBe("msg_3") expect(result.rows.map(TimelineRow.key)).toEqual([ "user-message:msg_1", "assistant-part:part:part:msg_2:msg_2:text:0", "turn-gap:msg_3", "user-message:msg_3", "thinking:msg_3", ]) }) test("renders a current shell message as a standalone turn", () => { const source = [ { id: "msg_shell", type: "shell", shellID: "shell_1", command: "pwd", status: "exited", exit: 0, output: { output: "/repo", cursor: 5, size: 5, truncated: false }, time: { created: 1, completed: 2 }, }, { id: "msg_shell_completion", type: "synthetic", text: "User shell pwd completed: /repo", metadata: { source: "shell", shellID: "shell_1", state: "completed" }, time: { created: 3 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "idle" }) expect(result.activeMessageID).toBe("msg_shell") expect(result.rows.map(TimelineRow.key)).toEqual(["shell:msg_shell"]) }) test("keeps assistant content when no user root is available", () => { const source = [ { id: "msg_notice", type: "synthetic", text: "done", description: "Background work completed", time: { created: 1 }, }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "result" }], time: { created: 2, completed: 3 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "idle" }) expect(result.activeMessageID).toBe("msg_assistant") expect(result.rows.map(TimelineRow.key)).toEqual([ "notice:msg_notice", "assistant-part:part:part:msg_assistant:msg_assistant:text:0", ]) }) test("keeps CLI notice messages between the assistant steps they surround", () => { const source: SessionMessageInfo[] = [ { id: "msg_user", type: "user", text: "run", time: { created: 1 } }, { id: "msg_agent", type: "agent-switched", agent: "explore", time: { created: 2 } }, { id: "msg_assistant_1", type: "assistant", agent: "explore", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "started" }], time: { created: 3, completed: 4 }, }, { id: "msg_background", type: "synthetic", text: "result", description: "Search code", metadata: { source: "subagent", agent: "explore", state: "completed" }, time: { created: 5 }, }, { id: "msg_model", type: "model-switched", model: { id: "next", providerID: "provider" }, time: { created: 6 }, }, { id: "msg_assistant_2", type: "assistant", agent: "explore", model: { id: "next", providerID: "provider" }, content: [{ type: "text", text: "finished" }], time: { created: 7, completed: 8 }, }, { id: "msg_restart", type: "synthetic", text: "continue", description: "Continuing after restart", metadata: { notice: "restart" }, time: { created: 9 }, }, { id: "msg_skill", type: "skill", skill: "review", name: "Review", text: "instructions", time: { created: 10 } }, { id: "msg_compaction", type: "compaction", status: "completed", reason: "auto", summary: "summary", time: { created: 11 }, }, ] const result = Timeline.constructSessionMessageRows(source, true, { type: "idle" }) expect(result.rows.map(TimelineRow.key)).toEqual([ "user-message:msg_user", "notice:msg_agent", "assistant-part:part:part:msg_assistant_1:msg_assistant_1:text:0", "notice:msg_background", "notice:msg_model", "assistant-part:part:part:msg_assistant_2:msg_assistant_2:text:0", "notice:msg_restart", "notice:msg_skill", "notice:msg_compaction", ]) }) test("does not infer thinking from an optimistic busy turn", () => { const source = [ { id: "msg_z", type: "user", text: "existing", time: { created: 1 } }, { id: "msg_a", type: "user", text: "pending", time: { created: 2 } }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "busy" }) expect(result.activeMessageID).toBe("msg_a") expect(result.rows.map(TimelineRow.key)).toEqual(["user-message:msg_z", "turn-gap:msg_a", "user-message:msg_a"]) }) test("does not infer thinking above a queued user message", () => { const source = [ { id: "msg_active", type: "user", text: "active", time: { created: 1 } }, { id: "msg_queued", type: "user", text: "queued", time: { created: 2 } }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "busy" }, new Set(["msg_queued"])) expect(result.activeMessageID).toBe("msg_active") expect(result.rows.map(TimelineRow.key)).toEqual([ "user-message:msg_active", "activity", "turn-gap:msg_queued", "user-message:msg_queued", ]) expect(result.rows[1].userMessageID).toBe("msg_active") }) test("suppresses thinking while a subagent is delegating or running", () => { const statuses = ["streaming", "running"] as const statuses.forEach((status) => { const source = [ { id: "msg_user", type: "user", text: "delegate", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ { type: "tool", id: "tool_subagent", name: "subagent", state: status === "streaming" ? { status, input: "" } : { status, input: { description: "Inspect code" }, metadata: {} }, time: { created: 2 }, }, ], time: { created: 2 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(source, true, { type: "busy" }).rows.map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", ]) }) }) test("renders retry state from the current assistant message", () => { const source = [ { id: "msg_user", type: "user", text: "retry", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], retry: { attempt: 2, at: 10, error: { type: "ProviderError", message: "rate limited" } }, time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "busy" }) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "Retry"]) }) test.each(["hidden", "compact", "full"] as const)("only shows active reasoning in %s mode", (reasoningMode) => { const active = { type: "reasoning", text: "## Current thought", time: { created: 2 } } as const const cases: { content: SessionMessageAssistant["content"]; thinking: boolean }[] = [ { content: [], thinking: false }, { content: [active], thinking: true }, { content: [{ ...active, text: "" }], thinking: true }, { content: [{ ...active, time: { created: 2, completed: 3 } }], thinking: false }, { content: [active, { type: "text", text: "Answer" }], thinking: false }, ...(["streaming", "running", "completed", "error"] as const).flatMap((status) => ["shell", "read", "subagent", "question"].map((name) => ({ content: [active, storyTool("tool", name, status, {})], thinking: false, })), ), ] cases.forEach((profile) => { const document = storyDocument(profile.content, true) const result = createTimelineProjection({ sessionMessages: document.messages, status: document.status, reasoningMode, }) expect(result.rows.some((row) => Predicate.isTagged(row, "Thinking"))).toBe( reasoningMode !== "hidden" && profile.thinking, ) }) }) test("keeps a grouped reasoning-only row while busy", () => { const thought = { type: "reasoning", text: "Thinking", time: { created: 1, completed: 2 } } as const const rows = (content: SessionMessageAssistant["content"], busy: boolean) => createTimelineProjection({ sessionMessages: storyDocument(content, busy).messages, status: { type: busy ? "busy" : "idle" }, reasoningMode: "compact", timelineDetail: timelinePresets[2].value, }).rows expect(rows([{ ...thought, time: { created: 1 } }], true).map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", ]) expect(rows([thought], true).map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart"]) expect(rows([thought, { type: "text", text: "" }], true).map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", ]) expect(rows([thought, { type: "text", text: "Answer" }], true).map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", "AssistantPart", ]) expect( rows([thought, storyTool("read", "read", "running", { filePath: "package.json" })], true).map((row) => row._tag), ).toEqual(["UserMessage", "AssistantPart"]) expect(rows([thought], false).map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart"]) const failed = storyDocument([thought], true) const messages = failed.messages.map((message) => message.type === "assistant" ? { ...message, error: { type: "provider.error", message: "Failed" } } : message, ) expect( createTimelineProjection({ sessionMessages: messages, status: failed.status, reasoningMode: "compact", timelineDetail: timelinePresets[2].value, }).rows.map((row) => row._tag), ).toEqual(["UserMessage", "AssistantPart", "Error"]) expect( createTimelineProjection({ sessionMessages: failed.messages.map((message) => message.type === "assistant" ? { ...message, error: { type: "Interrupted", message: "Stopped" } } : message, ), status: failed.status, reasoningMode: "compact", timelineDetail: { ...timelinePresets[2].value, notices: { placement: "hidden" } }, }).rows.map((row) => row._tag), ).toEqual(["UserMessage", "AssistantPart"]) }) test("stops thinking on idle, message completion, errors and retries", () => { const document = storyDocument([{ type: "reasoning", text: "Current thought" }], true) expect( Timeline.constructSessionMessageRows(document.messages, true, { type: "idle" }).rows.map((row) => row._tag), ).toEqual(["UserMessage", "AssistantPart"]) const endings = [ { time: { created: 1, completed: 2 } }, { error: { type: "Interrupted", message: "Stopped" } }, { retry: { attempt: 1, at: 10, error: { type: "ProviderError", message: "Retry" } } }, ] endings.forEach((ending) => { const messages = document.messages.map((message) => message.type === "assistant" ? { ...message, ...ending } : message, ) expect( Timeline.constructSessionMessageRows(messages, true, { type: "busy" }).rows.some((row) => Predicate.isTagged(row, "Thinking"), ), ).toBe(false) }) }) test("uses the latest reasoning part and groups earlier thoughts with tools", () => { const document = storyDocument( [ { type: "reasoning", text: "Old thought", time: { created: 1, completed: 2 } }, storyTool("read", "read", "completed", {}), { type: "reasoning", text: "New thought", time: { created: 3 } }, ], true, ) const result = Timeline.constructSessionMessageRows(document.messages, true, { type: "busy" }) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart", "Thinking"]) expect(result.rows[1]).toMatchObject({ group: { type: "context", refs: [{ partID: "msg_tool_projection_assistant:reasoning:0" }, { partID: "read" }] }, }) expect(result.rows[2]).toMatchObject({ ref: { partID: "msg_tool_projection_assistant:reasoning:1" } }) }) test("keeps actual thinking with the active prompt above an undelivered prompt", () => { const document = storyDocument([{ type: "reasoning", text: "Active thought" }], true) const result = Timeline.constructSessionMessageRows( [...document.messages, { type: "user", id: "queued", text: "Next task", time: { created: 10 } }], true, document.status, new Set(["queued"]), ) expect(result.rows.map((row) => row._tag)).toEqual([ "UserMessage", "Thinking", "Activity", "TurnGap", "UserMessage", ]) expect(result.rows[1].userMessageID).toBe(document.messages[0].id) }) test("places live status, then a queued compaction, after the active turn and before undelivered prompts", () => { const document = storyDocument([{ type: "text", text: "Working" }], true) const result = Timeline.constructSessionMessageRows( [...document.messages, { type: "user", id: "queued", text: "Next task", time: { created: 10 } }], true, document.status, new Set(["queued"]), false, false, undefined, undefined, ["inb_compact"], ) expect(result.rows.map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", "Activity", "CompactionQueued", "TurnGap", "UserMessage", ]) expect(result.rows[3].userMessageID).toBe(document.messages[0].id) }) test("places a queued compaction ahead of a pending notice, which the server delivers after it", () => { const document = storyDocument([{ type: "text", text: "Working" }], true) const notice = { id: "notice", type: "synthetic", text: "done", description: "Background work completed", time: { created: 10 }, } satisfies SessionMessageInfo const result = Timeline.constructSessionMessageRows( [...document.messages, notice], true, document.status, new Set(["notice"]), false, false, undefined, undefined, ["inb_compact"], ) expect(result.rows.map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", "Activity", "CompactionQueued", "Notice", ]) }) test("keeps a grouped undelivered notice out of accepted work and spaced from it", () => { const document = storyDocument([{ type: "text", text: "Working" }], true) const notice = { id: "notice", type: "synthetic", text: "done", description: "Background work completed", time: { created: 10 }, } satisfies SessionMessageInfo const result = Timeline.constructSessionMessageRows( [...document.messages, notice], true, document.status, new Set(["notice"]), false, false, undefined, timelinePresets[2].value, ) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart", "Activity", "AssistantPart"]) expect(result.rows[3]).toMatchObject({ spacing: "tool", group: { type: "context", refs: [{ messageID: "notice", partID: "notice" }] }, }) }) test("keeps live thinking above a notice that arrives while it streams", () => { const document = storyDocument([{ type: "reasoning", text: "Active thought" }], true) const notice = { id: "notice", type: "synthetic", text: "done", description: "Background work completed", time: { created: 10 }, } satisfies SessionMessageInfo const result = Timeline.constructSessionMessageRows( [...document.messages, notice], true, document.status, undefined, false, false, undefined, timelinePresets[0].value, ) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "Thinking", "Notice"]) }) test.each(["shell", "execute", "subagent"])("does not hide active %s behind a preceding thought", (name) => { const document = storyDocument( [ { type: "reasoning", text: "Finished thought", time: { created: 1, completed: 2 } }, storyTool("active", name, "running", {}), ], true, ) const result = Timeline.constructSessionMessageRows(document.messages, true, document.status) expect(result.rows.flatMap((row) => (Predicate.isTagged(row, "AssistantPart") ? [row.group.type] : []))).toEqual([ "part", "part", ]) }) test("keeps divider notices outside grouped context rows", () => { const source = [ { id: "msg_user", type: "user", text: "move", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [storyTool("tool_read", "read", "completed", {})], time: { created: 2, completed: 3 }, }, { id: "msg_moved", type: "location-switched", location: { directory: "/tmp/next" }, time: { created: 4 } }, { id: "msg_skill", type: "skill", skill: "review", name: "Review", text: "instructions", time: { created: 5 } }, ] satisfies SessionMessageInfo[] const rows = Timeline.constructSessionMessageRows( source, false, { type: "idle" }, undefined, false, false, undefined, timelinePresets.find((preset) => preset.id === "compact")!.value, ).rows expect(rows.map(TimelineRow.key)).toEqual([ "user-message:msg_user", "assistant-part:context:context:msg_assistant:tool_read", "notice:msg_moved", "assistant-part:context:message:msg_skill", ]) }) test.each([ ["everything", ["msg_agent", "msg_shell"]], ["detailed", ["msg_agent", "msg_shell"]], ["compact", ["msg_agent", "msg_shell"]], ["quiet", ["msg_agent"]], ["text-only", []], ] as const)("shows background outcomes as their own rows where they finished: %s", (id, notices) => { const step = (messageID: string, created: number) => ({ id: messageID, type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [storyTool(`${messageID}_read`, "read", "completed", {})], time: { created, completed: created }, }) satisfies SessionMessageInfo const source: SessionMessageInfo[] = [ { id: "msg_user", type: "user", text: "work", time: { created: 1 } }, step("msg_before", 2), { id: "msg_agent", type: "synthetic", text: "done", description: "Map the inbox", metadata: { source: "subagent", childID: "ses_child", agent: "explore", state: "completed" }, time: { created: 3 }, }, { id: "msg_shell", type: "synthetic", text: "done", description: "bun test", metadata: { source: "shell", shellID: "sh_1", state: "completed", exit: 0 }, time: { created: 4 }, }, step("msg_after", 5), ] const rows = Timeline.constructSessionMessageRows( source, false, { type: "idle" }, undefined, false, false, undefined, timelinePresets.find((preset) => preset.id === id)!.value, ).rows const keys = rows.map(TimelineRow.key) expect(keys.filter((key) => key.startsWith("notice:"))).toEqual(notices.map((notice) => `notice:${notice}`)) if (id === "compact") expect(keys).toEqual([ "user-message:msg_user", "assistant-part:context:context:msg_before:msg_before_read", "notice:msg_agent", "notice:msg_shell", "assistant-part:context:context:msg_after:msg_after_read", ]) }) test.each([ ["grouped", "assistant-part:context:context:msg_a1:msg_a1:text:0"], ["hidden", "assistant-part:context:context:msg_a1:tool_read"], ] as const)("%s commentary keeps the answer of each turn", (placement, group) => { const step = (id: string, content: SessionMessageAssistant["content"]) => ({ id, type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content, time: { created: 2, completed: 3 }, }) satisfies SessionMessageInfo const source = [ { id: "msg_user", type: "user", text: "fix", time: { created: 1 } }, step("msg_a1", [{ type: "text", text: "Checking." }, storyTool("tool_read", "read", "completed", {})]), step("msg_a2", [ { type: "text", text: "Checks pass.", native: { phase: "commentary" } }, { type: "text", text: "Fixed." }, ]), { id: "msg_stop", type: "user", text: "stop", time: { created: 4 } }, step("msg_a3", [{ type: "text", text: "Looking." }, storyTool("tool_grep", "grep", "completed", {})]), ] satisfies SessionMessageInfo[] const rows = Timeline.constructSessionMessageRows( source, false, { type: "idle" }, undefined, false, false, undefined, { ...timelinePresets[2].value, commentary: { placement } }, ).rows // The interrupted second turn has no answer, so its last commentary stays visible. expect(rows.map(TimelineRow.key)).toEqual([ "user-message:msg_user", group, "assistant-part:part:part:msg_a2:msg_a2:text:1", "turn-gap:msg_stop", "user-message:msg_stop", "assistant-part:part:part:msg_a3:msg_a3:text:0", "assistant-part:context:context:msg_a3:tool_grep", ]) }) test.each(["grouped", "hidden"] as const)("%s commentary keeps the answer of each run", (placement) => { const runs = commentaryRuns() const detail = { ...timelinePresets[2].value, commentary: { placement } } const rows = (sessionMessages: SessionMessageInfo[], type: "idle" | "busy") => createTimelineProjection({ sessionMessages, status: { type }, timelineDetail: detail, reasoningMode: "compact" }) .rows const groups = (rows: TimelineRow.TimelineRow[]) => rows.flatMap((row) => { if (!Predicate.isTagged(row, "AssistantPart")) return [] if (row.group.type === "part") return [`part:${row.group.ref.partID}`] return [`${row.group.type}:${row.group.refs.map((ref) => ref.partID).join("+")}`] }) const complete = [ "part:reply:text:0", ...[1, 2, 3].flatMap((index) => [ placement === "grouped" ? `context:notice-${index}+work-${index}:text:0+tool-${index}` : `context:notice-${index}+tool-${index}`, `part:reply-${index}:text:0`, ]), ] expect(groups(rows(runs.complete, "idle"))).toEqual(complete) expect(groups(rows(runs.toolOnly, "busy"))).toEqual([...complete, "context:tool-only-notice+tool-only-call"]) const live = [ "part:prior:text:0", placement === "grouped" ? "context:wake+working:text:0+running" : "context:wake+running", ] expect(groups(rows(runs.busy, "busy"))).toEqual(live) expect(groups(rows(runs.failed, "busy"))).toEqual(live) expect({ justClosed: groups(rows(runs.busy.slice(0, -2), "busy")), beforeStep: groups(rows(runs.busy.slice(0, -1), "busy")), }).toEqual({ justClosed: ["part:prior:text:0"], beforeStep: ["part:prior:text:0", "context:wake"], }) }) test("keeps assistant errors and retries before later notices", () => { const result = Timeline.constructSessionMessageRows( [ { id: "msg_user", type: "user", text: "continue", time: { created: 1 } }, { id: "msg_blocked", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "partial" }], error: { type: "provider.content-filter", message: "Provider blocked the response" }, time: { created: 2, completed: 3 }, }, { id: "msg_model", type: "model-switched", model: { id: "next", providerID: "provider" }, time: { created: 4 }, }, ], true, { type: "idle" }, ) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart", "Error", "Notice"]) const retry = Timeline.constructSessionMessageRows( [ { id: "msg_user", type: "user", text: "retry", time: { created: 1 } }, { id: "msg_retry", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], error: { type: "ProviderError", message: "rate limited" }, retry: { attempt: 2, at: 10, error: { type: "ProviderError", message: "rate limited" } }, time: { created: 2 }, }, { id: "msg_model", type: "model-switched", model: { id: "next", providerID: "provider" }, time: { created: 3 }, }, ], true, { type: "retry", attempt: 2, next: 10, message: "rate limited" }, ) expect(retry.rows.map((row) => row._tag)).toEqual(["UserMessage", "Retry", "Notice"]) }) test("suppresses an earlier error when the turn recovers across a notice", () => { const source = [ { id: "msg_user", type: "user", text: "recover", time: { created: 1 } }, { id: "msg_failed", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], error: { type: "ProviderError", message: "temporary failure" }, time: { created: 2, completed: 3 }, }, { id: "msg_model", type: "model-switched", model: { id: "next", providerID: "provider" }, time: { created: 4 }, }, { id: "msg_recovery", type: "assistant", agent: "build", model: { id: "next", providerID: "provider" }, content: [{ type: "text", text: "recovered" }], time: { created: 5, completed: 6 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(source, true, { type: "idle" }).rows.map((row) => row._tag)).toEqual([ "UserMessage", "Notice", "AssistantPart", ]) }) test("does not render the retry error twice", () => { const source = [ { id: "msg_user", type: "user", text: "retry", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], error: { type: "ProviderError", message: "The provider response ended unexpectedly." }, retry: { attempt: 2, at: 10, error: { type: "ProviderError", message: "The provider response ended unexpectedly." }, }, time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "retry", attempt: 2, next: 10, message: "The provider response ended unexpectedly.", }) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "Retry"]) }) test("removes a failed assistant error when the turn continues streaming", () => { const source = [ { id: "msg_user", type: "user", text: "recover", time: { created: 1 } }, { id: "msg_failed", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], error: { type: "ProviderError", message: "temporary failure" }, time: { created: 2, completed: 3 }, }, { id: "msg_recovery", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "streaming again" }], time: { created: 4 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, true, { type: "busy" }) expect(result.rows.map((row) => row._tag)).toEqual(["UserMessage", "AssistantPart"]) }) test("keeps content IDs and groups adjacent context tools", () => { const source = [ { id: "msg_user", type: "user", text: "inspect", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ { type: "text", text: " " }, { type: "reasoning", text: "hidden" }, { type: "tool", id: "tool_read", name: "read", state: { status: "running", input: {}, metadata: {} }, time: { created: 2 }, }, { type: "tool", id: "tool_grep", name: "grep", state: { status: "running", input: {}, metadata: {} }, time: { created: 3 }, }, { type: "tool", id: "tool_todo", name: "todowrite", state: { status: "running", input: {}, metadata: {} }, time: { created: 4 }, }, { type: "tool", id: "tool_question", name: "question", state: { status: "streaming", input: "" }, time: { created: 5 }, }, { type: "text", text: "visible" }, ], time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, false, { type: "idle" }) const groups = result.rows.flatMap((row) => (Predicate.isTagged(row, "AssistantPart") ? [row.group] : [])) expect(groups).toEqual([ { type: "context", key: "context:msg_assistant:tool_read", refs: [ { messageID: "msg_assistant", partID: "tool_read" }, { messageID: "msg_assistant", partID: "tool_grep" }, ], }, { type: "part", key: "part:msg_assistant:msg_assistant:text:1", ref: { messageID: "msg_assistant", partID: "msg_assistant:text:1" }, }, ]) }) test("keeps reads that load files outside context groups", () => { const read = { type: "tool", id: "tool_read", name: "read", state: { status: "completed", input: { path: "packages/cli/AGENTS.md" }, content: [{ type: "text", text: "instructions" }], metadata: { loaded: ["packages/cli/AGENTS.md"] }, }, time: { created: 2, ran: 3, completed: 4 }, } satisfies SessionMessageAssistantTool const grep = { type: "tool", id: "tool_grep", name: "grep", state: { status: "running", input: {}, metadata: {} }, time: { created: 5 }, } satisfies SessionMessageAssistantTool const source = [ { id: "msg_user", type: "user", text: "inspect", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [read, grep], time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const groups = Timeline.constructSessionMessageRows(source, false, { type: "idle" }).rows.flatMap((row) => Predicate.isTagged(row, "AssistantPart") ? [row.group] : [], ) expect(groups).toEqual([ { type: "part", key: "part:msg_assistant:tool_read", ref: { messageID: "msg_assistant", partID: "tool_read" }, }, { type: "context", key: "context:msg_assistant:tool_grep", refs: [{ messageID: "msg_assistant", partID: "tool_grep" }], }, ]) }) test("keeps context row keys unique when tool IDs repeat across assistant messages", () => { const tool = (name: string) => ({ type: "tool" as const, id: "tool_0", name, state: { status: "running" as const, input: {}, metadata: {} }, time: { created: 2 }, }) const assistant = (id: string, name: string) => ({ id, type: "assistant" as const, agent: "build", model: { id: "model", providerID: "provider" }, content: [tool(name)], time: { created: 2 }, }) const source = [ { id: "msg_user", type: "user", text: "inspect", time: { created: 1 } }, assistant("msg_assistant_1", "read"), assistant("msg_assistant_2", "execute"), assistant("msg_assistant_3", "grep"), ] satisfies SessionMessageInfo[] const keys = Timeline.constructSessionMessageRows(source, false, { type: "idle" }, undefined, true).rows.map( TimelineRow.key, ) expect(keys).toEqual([ "user-message:msg_user", "assistant-part:context:context:msg_assistant_1:tool_0", "assistant-part:part:part:msg_assistant_2:tool_0", "assistant-part:context:context:msg_assistant_3:tool_0", ]) }) test("groups adjacent successful patches and leaves failed patches separate", () => { const source = [ { id: "msg_user", type: "user", text: "edit", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ { type: "tool", id: "tool_patch_1", name: "patch", state: { status: "completed", input: {}, content: [{ type: "text", text: "done" }], metadata: { files: [] }, }, time: { created: 2, completed: 3 }, }, { type: "tool", id: "tool_patch_2", name: "patch", state: { status: "running", input: {}, metadata: { files: [] } }, time: { created: 4 }, }, { type: "tool", id: "tool_patch_failed", name: "patch", state: { status: "error", input: {}, error: { type: "ToolError", message: "failed" }, metadata: { files: [] }, }, time: { created: 5, completed: 6 }, }, { type: "tool", id: "tool_patch_3", name: "patch", state: { status: "completed", input: {}, content: [{ type: "text", text: "done" }], metadata: { files: [] }, }, time: { created: 7, completed: 8 }, }, { type: "tool", id: "tool_edit_1", name: "edit", state: { status: "running", input: {}, metadata: { files: [] } }, time: { created: 9 }, }, { type: "tool", id: "tool_edit_2", name: "edit", state: { status: "running", input: {}, metadata: { files: [] } }, time: { created: 10 }, }, { type: "tool", id: "tool_write_1", name: "write", state: { status: "running", input: {}, metadata: { files: [] } }, time: { created: 11 }, }, ], time: { created: 2, completed: 8 }, }, ] satisfies SessionMessageInfo[] const result = Timeline.constructSessionMessageRows(source, false, { type: "idle" }, undefined, false, true) const groups = result.rows.flatMap((row) => (Predicate.isTagged(row, "AssistantPart") ? [row.group] : [])) expect(groups).toEqual([ { type: "file", key: "part:msg_assistant:tool_patch_1", refs: [ { messageID: "msg_assistant", partID: "tool_patch_1" }, { messageID: "msg_assistant", partID: "tool_patch_2" }, ], }, { type: "part", key: "part:msg_assistant:tool_patch_failed", ref: { messageID: "msg_assistant", partID: "tool_patch_failed" }, }, { type: "file", key: "part:msg_assistant:tool_patch_3", refs: [ { messageID: "msg_assistant", partID: "tool_patch_3" }, { messageID: "msg_assistant", partID: "tool_edit_1" }, { messageID: "msg_assistant", partID: "tool_edit_2" }, { messageID: "msg_assistant", partID: "tool_write_1" }, ], }, ]) }) test("groups every consecutive collapsed tool in chronological order", () => { const source = [ { id: "msg_user", type: "user", text: "work", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ ...["shell", "subagent", "patch", "shell", "edit", "write", "grep"].map( (name, index): SessionMessageAssistantTool => ({ type: "tool" as const, id: `tool_${index}`, name, state: { status: "completed" as const, input: {}, content: [{ type: "text" as const, text: "done" }], metadata: {}, }, time: { created: index + 2, completed: index + 3 }, }), ), { type: "text" as const, text: "finished" }, { type: "tool" as const, id: "tool_after_text", name: "shell", state: { status: "completed" as const, input: {}, content: [{ type: "text" as const, text: "done" }], metadata: {}, }, time: { created: 10, completed: 11 }, }, ], time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const groups = Timeline.constructSessionMessageRows(source, false, { type: "idle" }).rows.flatMap((row) => Predicate.isTagged(row, "AssistantPart") ? [row.group] : [], ) expect(groups.map((group) => group.type)).toEqual(["context", "part", "context"]) expect(groups[0]?.type === "context" ? groups[0].refs.map((ref) => ref.partID) : []).toEqual([ "tool_0", "tool_1", "tool_2", "tool_3", "tool_4", "tool_5", "tool_6", ]) expect(groups[2]?.type === "context" ? groups[2].refs.map((ref) => ref.partID) : []).toEqual(["tool_after_text"]) }) test.each([ { shell: false, edit: false, types: ["context"] }, { shell: true, edit: false, types: ["part", "context"] }, { shell: false, edit: true, types: ["context", "file", "context"] }, { shell: true, edit: true, types: ["part", "file", "context"] }, ])("keeps tools expanded by settings outside collapsed groups ($shell, $edit)", ({ shell, edit, types }) => { const source = [ { id: "msg_user", type: "user", text: "work", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: ["shell", "edit", "write", "patch", "read"].map( (name, index): SessionMessageAssistantTool => ({ type: "tool" as const, id: `tool_${name}`, name, state: { status: "completed" as const, input: {}, content: [{ type: "text" as const, text: "done" }], metadata: {}, }, time: { created: index + 2, completed: index + 3 }, }), ), time: { created: 2 }, }, ] satisfies SessionMessageInfo[] const rows = Timeline.constructSessionMessageRows(source, false, { type: "idle" }, undefined, shell, edit).rows expect(rows.flatMap((row) => (Predicate.isTagged(row, "AssistantPart") ? [row.group.type] : []))).toEqual([ ...types, ]) }) test.each([ { name: "default", grouped: false, groups: [["context", "read", "grep"], ["part", "agent_1"], ["part", "agent_2"], ["context", "glob", "read_2"]], }, { name: "opted in", grouped: true, groups: [["context", "read", "grep", "agent_1", "agent_2", "glob", "read_2"]] }, ])("gives each subagent its own row unless subagent grouping is enabled ($name)", (input) => { const compact = timelinePresets.find((preset) => preset.id === "compact")!.value const rows = Timeline.constructSessionMessageRows( storyDocument([ storyTool("read", "read", "completed", { path: "src/a.ts" }), storyTool("grep", "grep", "completed", { pattern: "agent" }), storyTool("agent_1", "subagent", "completed", { agent: "explore", description: "Map the code" }), storyTool("agent_2", "subagent", "completed", { agent: "general", description: "Check the tests" }), storyTool("glob", "glob", "completed", { pattern: "*.ts" }), storyTool("read_2", "read", "completed", { path: "src/b.ts" }), ]).messages, true, { type: "idle" }, undefined, false, false, undefined, input.grouped ? { ...compact, subagents: { placement: "grouped" } } : compact, ).rows expect( rows.flatMap((row) => !Predicate.isTagged(row, "AssistantPart") ? [] : [ [ row.group.type, ...(row.group.type === "part" ? [row.group.ref] : row.group.refs).map((ref) => ref.partID), ], ], ), ).toEqual(input.groups.map((group) => [...group])) }) test("merges adjacent separate reads into one read row split by other parts", () => { const rows = Timeline.constructSessionMessageRows( storyDocument([ storyTool("read_1", "read", "completed", { path: "src/a.ts" }), storyTool("read_2", "read", "running", { path: "src/b.ts", limit: 120 }), { type: "reasoning", text: "Keep reading." }, storyTool("read_3", "read", "completed", { path: "src/c.ts" }), storyTool("read_image", "read", "completed", { path: "src/logo.png" }), storyTool("read_failed", "read", "error", { path: "src/missing.ts" }), storyTool("read_4", "read", "completed", { path: "src/d.ts" }), storyTool("read_5", "read", "completed", { path: "src/e.ts" }), storyTool("glob", "glob", "completed", { pattern: "*.ts" }), storyTool("read_6", "read", "completed", { path: "src/f.ts" }), ]).messages, true, { type: "idle" }, undefined, false, false, undefined, timelinePresets[0].value, ).rows expect( rows.flatMap((row) => !Predicate.isTagged(row, "AssistantPart") ? [] : [ [ row.group.type, ...(row.group.type === "part" ? [row.group.ref] : row.group.refs).map((ref) => ref.partID), ], ], ), ).toEqual([ ["read", "read_1", "read_2"], ["part", "msg_tool_projection_assistant:reasoning:0"], ["read", "read_3"], ["part", "read_image"], ["part", "read_failed"], ["read", "read_4", "read_5"], ["part", "glob"], ["read", "read_6"], ]) }) test.each(["shell", "execute", "subagent"])("keeps %s in an existing group throughout execution", (name) => { const initial = createTimelineProjection({ sessionMessages: storyDocument([storyTool("earlier", "read", "completed", {})]).messages, status: { type: "busy" }, reasoningMode: "hidden", }) const phases = [ { status: "streaming" }, { status: "running" }, { status: "completed", metadata: { status: "running" } }, { status: "completed" }, { status: "error" }, ] as const phases.reduce((previousRows, phase, index) => { const result = createTimelineProjection({ sessionMessages: [ ...storyDocument([storyTool("earlier", "read", "completed", {})]).messages, ...storyDocument([ storyTool("active", name, phase.status, {}, "metadata" in phase ? { metadata: phase.metadata } : {}), ]) .messages.filter((message) => message.type === "assistant") .map((message) => ({ ...message, id: "next-step" })), ], status: { type: "busy" }, reasoningMode: "hidden", previousRows, }) const groups = result.rows.filter((row) => Predicate.isTagged(row, "AssistantPart")) expect(groups).toHaveLength(1) expect(groups[0].group).toMatchObject({ type: "context", refs: [ { messageID: "msg_tool_projection_assistant", partID: "earlier" }, { messageID: "next-step", partID: "active" }, ], }) expect(TimelineRow.key(groups[0])).toBe(TimelineRow.key(initial.rows[1])) if (index > 0) expect(groups[0]).toBe(previousRows.find((row) => Predicate.isTagged(row, "AssistantPart"))!) return result.rows }, initial.rows) }) test.each([ { name: "shell", expanded: true, types: ["context", "part"] }, { name: "execute", expanded: true, types: ["context", "part"] }, { name: "subagent", expanded: true, types: ["context"] }, { name: "shell", separator: "text", types: ["context", "part", "part"] }, { name: "shell", separator: "reasoning", showReasoning: true, types: ["context"] }, { name: "shell", separator: "reasoning", showReasoning: false, types: ["context"] }, ] as const)("respects active tool grouping boundaries: %j", (profile) => { const content = [ storyTool("earlier", "read", "completed", {}), ...(profile.separator ? [{ type: profile.separator, text: "Visible boundary" }] : []), storyTool("active", profile.name, "running", {}), ] const rows = Timeline.constructSessionMessageRows( storyDocument(content).messages, profile.showReasoning ?? false, { type: "busy" }, undefined, profile.expanded ?? false, ).rows expect(rows.flatMap((row) => (Predicate.isTagged(row, "AssistantPart") ? [row.group.type] : []))).toEqual([ ...profile.types, ]) }) test("keeps active and background work standalone when no group precedes them", () => { const source: SessionMessageInfo[] = [ { id: "msg_user", type: "user", text: "work", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ { type: "tool", id: "tool_running_shell", name: "shell", state: { status: "running", input: {}, metadata: {} }, time: { created: 2 }, }, { type: "tool", id: "tool_background_agent", name: "subagent", state: { status: "completed", input: {}, content: [{ type: "text", text: "running" }], metadata: { status: "running" }, }, time: { created: 3, completed: 4 }, }, { type: "tool", id: "tool_completed_shell", name: "shell", state: { status: "completed", input: {}, content: [{ type: "text", text: "done" }], metadata: {}, }, time: { created: 5, completed: 6 }, }, ], time: { created: 2 }, }, ] expect( Timeline.constructSessionMessageRows(source, false, { type: "busy" }).rows.flatMap((row) => Predicate.isTagged(row, "AssistantPart") ? [row.group.type] : [], ), ).toEqual(["part", "part", "context"]) }) test("keeps failed calls inside a collapsed mixed-tool stack", () => { const source: SessionMessageInfo[] = [ { id: "msg_user", type: "user", text: "search", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [ { type: "tool", id: "tool_glob_failed", name: "glob", state: { status: "error", input: { pattern: "*.ts" }, error: { type: "ToolError", message: "Invalid tool input" }, metadata: {}, }, time: { created: 2, completed: 3 }, }, { type: "tool", id: "tool_grep_failed", name: "grep", state: { status: "error", input: { pattern: "value" }, error: { type: "ToolError", message: "Search timed out" }, metadata: {}, }, time: { created: 4, completed: 5 }, }, { type: "tool", id: "tool_shell_failed", name: "shell", state: { status: "error", input: { command: "exit 1" }, error: { type: "ToolError", message: "Command failed" }, metadata: {}, }, time: { created: 6, completed: 7 }, }, ], time: { created: 2, completed: 8 }, }, ] const groups = Timeline.constructSessionMessageRows(source, false, { type: "idle" }).rows.flatMap((row) => Predicate.isTagged(row, "AssistantPart") ? [row.group] : [], ) expect(groups).toEqual([ { type: "context", key: "context:msg_assistant:tool_glob_failed", refs: [ { messageID: "msg_assistant", partID: "tool_glob_failed" }, { messageID: "msg_assistant", partID: "tool_grep_failed" }, { messageID: "msg_assistant", partID: "tool_shell_failed" }, ], }, ]) expect( Timeline.constructSessionMessageRows(source, false, { type: "idle" }, undefined, true).rows.flatMap((row) => Predicate.isTagged(row, "AssistantPart") ? [row.group.type] : [], ), ).toEqual(["context", "part"]) }) test("places a divider after interrupted output unless the turn compacts", () => { const messages = [ { id: "msg_user", type: "user", text: "continue", time: { created: 1 } }, { id: "msg_interrupted", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "before" }], error: { type: "ExecutionInterrupted", message: "stopped" }, time: { created: 2, completed: 3 }, }, { id: "msg_continued", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: "after" }], time: { created: 4, completed: 5 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(messages, true, { type: "idle" }).rows.map((row) => row._tag)).toEqual([ "UserMessage", "AssistantPart", "TurnDivider", "AssistantPart", ]) const compacted = [ messages[0], messages[1], { id: "msg_compaction", type: "compaction", status: "completed", reason: "auto", summary: "summary", time: { created: 4 }, }, messages[2], ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(compacted, true, { type: "idle" }).rows.map((row) => row._tag)).toEqual( ["UserMessage", "AssistantPart", "Notice", "AssistantPart"], ) }) test("surfaces failed idle errors when no assistant step owns the failure", () => { const preStep = [ { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, { id: "msg_idle", type: "idle", outcome: "failed", error: { type: "provider.no-route", message: "Model unavailable: opencode/gpt-5.2" }, time: { created: 2 }, }, { id: "msg_model", type: "model-switched", model: { id: "next", providerID: "provider" }, time: { created: 3 }, }, ] satisfies SessionMessageInfo[] const preStepRows = Timeline.constructSessionMessageRows(preStep, true, { type: "idle" }).rows expect(preStepRows.map((row) => row._tag)).toEqual(["UserMessage", "Error", "Notice"]) expect(preStepRows[1]).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Model unavailable: opencode/gpt-5.2" }), ) const betweenSteps = [ { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [storyTool("tool_read", "read", "completed", {})], finish: "tool-calls", time: { created: 2, completed: 3 }, }, { id: "msg_idle", type: "idle", outcome: "failed", error: { type: "unknown", message: "Failed to execute statement" }, time: { created: 4 }, }, ] satisfies SessionMessageInfo[] expect( Timeline.constructSessionMessageRows(betweenSteps, true, { type: "idle" }).rows.map((row) => row._tag), ).toEqual(["UserMessage", "AssistantPart", "Error"]) const duplicate = [ { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], finish: "error", error: { type: "provider.auth", message: "Your organization does not have access to this model." }, time: { created: 2, completed: 3 }, }, { id: "msg_idle", type: "idle", outcome: "failed", error: { type: "provider.auth", message: "Your organization does not have access to this model." }, time: { created: 4 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(duplicate, true, { type: "idle" }).rows.map((row) => row._tag)).toEqual( ["UserMessage", "Error"], ) const exhausted = [ { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } }, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [], error: { type: "aborted", message: "Step interrupted" }, time: { created: 2, completed: 3 }, }, { id: "msg_idle", type: "idle", outcome: "failed", error: { type: "aborted", message: "Execution was interrupted repeatedly and will not be resumed automatically.", }, time: { created: 4 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(exhausted, true, { type: "idle" }).rows.at(-1)).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Execution was interrupted repeatedly and will not be resumed automatically.", }), ) }) test("marks the free and Go usage limits and their reset times, whichever message records the failure", () => { const error = { type: "provider.free-limit", message: "Free usage limit reached.", retryAfterMs: 60_000 } const user = { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } } satisfies SessionMessageInfo const assistant = { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "big-pickle", providerID: "opencode" }, content: [], finish: "error", error, time: { created: 2, completed: 1_000 }, } satisfies SessionMessageAssistant const step = [user, assistant] satisfies SessionMessageInfo[] const idle = [ user, { id: "msg_idle", type: "idle", outcome: "failed", error, time: { created: 2_000 } }, ] satisfies SessionMessageInfo[] const quota = [ user, { ...assistant, error: { type: "provider.quota", message: "Credit limit exceeded.", retryAfterMs: 60_000 } }, ] satisfies SessionMessageInfo[] const go = [ user, { ...assistant, error: { type: "provider.go-limit", message: "Go usage limit reached.", retryAfterMs: 25 * 86_400_000 }, }, ] satisfies SessionMessageInfo[] expect(Timeline.constructSessionMessageRows(step, true, { type: "idle" }).rows.at(-1)).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Free usage limit reached.", kind: "free-limit", resetAt: 61_000, }), ) expect(Timeline.constructSessionMessageRows(idle, true, { type: "idle" }).rows.at(-1)).toMatchObject({ kind: "free-limit", resetAt: 62_000, }) expect( Timeline.constructSessionMessageRows( [user, { ...assistant, error: { ...error, retryAfterMs: 9_000_000_000_000_000 } }], true, { type: "idle" }, ).rows.at(-1), ).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Free usage limit reached.", kind: "free-limit", }), ) expect(Timeline.constructSessionMessageRows(go, true, { type: "idle" }).rows.at(-1)).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Go usage limit reached.", kind: "go-limit", resetAt: 1_000 + 25 * 86_400_000, }), ) expect(Timeline.constructSessionMessageRows(quota, true, { type: "idle" }).rows.at(-1)).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Credit limit exceeded." }), ) }) test("marks a Console budget refusal with the reset its envelope names", () => { const user = { id: "msg_user", type: "user", text: "prompt", time: { created: 1 } } satisfies SessionMessageInfo const refused = (type: string, body?: string) => { const error = { type, message: "Budget reached.", status: 429 } return Timeline.constructSessionMessageRows( [ user, { id: "msg_assistant", type: "assistant", agent: "build", model: { id: "claude-opus-5", providerID: "opencode" }, content: [], finish: "error", error: body ? { ...error, response: { body } } : error, time: { created: 2, completed: 3 }, }, ], true, { type: "idle" }, ).rows.at(-1) } // Anthropic and OpenAI clients get Console's own envelope; Google clients get the reset in ErrorInfo's metadata. expect( refused( "provider.model-budget-limit", '{"type":"error","error":{"type":"BudgetLimitError","message":"A budget policy for this model has been reached."},"metadata":{"coverage":"models","resetsAt":"2026-11-01T00:00:00.000Z"}}', ), ).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Budget reached.", kind: "model-budget-limit", resetAt: Date.UTC(2026, 10, 1), }), ) expect( refused( "provider.budget-limit", '{"error":{"code":429,"status":"RESOURCE_EXHAUSTED","message":"Budget reached.","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"BudgetLimitError","domain":"opencode.ai","metadata":{"coverage":"all","resetsAt":"2026-11-01T00:00:00.000Z"}}]}}', ), ).toMatchObject({ kind: "budget-limit", resetAt: Date.UTC(2026, 10, 1) }) expect(refused("provider.budget-limit")).toEqual( new TimelineRow.Error({ userMessageID: "msg_user", text: "Budget reached.", kind: "budget-limit" }), ) }) test("summarizes a turn's changed files after the idle marker that ends it", () => { const step = (id: string, files?: string[]) => ({ id, type: "assistant", agent: "build", model: { id: "model", providerID: "provider" }, content: [{ type: "text", text: id }], snapshot: files && { start: "tree_a", end: "tree_b", files }, time: { created: 1, completed: 2 }, }) satisfies SessionMessageAssistant const idle = (id: string) => ({ id, type: "idle", outcome: "succeeded", time: { created: 1 } }) as const const user = (id: string) => ({ id, type: "user", text: id, time: { created: 1 } }) as const const source = [ // A steered prompt joins the running turn; the summary diffs from its opening prompt and lists // every path its steps recorded once, sorted. user("msg_open"), step("msg_edit", ["src/a.ts"]), user("msg_steer"), step("msg_answer", ["src/c.ts", "src/a.ts"]), idle("msg_idle_1"), // A turn that changed nothing gets no summary. user("msg_ask"), step("msg_reply", []), idle("msg_idle_2"), // Neither does a turn still running. user("msg_next"), step("msg_running", ["src/b.ts"]), ] satisfies SessionMessageInfo[] const rows = createTimelineProjection({ sessionMessages: source, status: { type: "busy" }, reasoningMode: "compact", turnDiffs: true, }).rows expect(rows.filter((row) => Predicate.isTagged(row, "TurnDiff"))).toEqual([ new TimelineRow.TurnDiff({ userMessageID: "msg_steer", messageID: "msg_idle_1", from: "msg_open", files: ["src/a.ts", "src/c.ts"], }), ]) expect(rows.map(TimelineRow.key).slice(0, 6)).toEqual([ "user-message:msg_open", "assistant-part:part:part:msg_edit:msg_edit:text:0", "turn-gap:msg_steer", "user-message:msg_steer", "assistant-part:part:part:msg_answer:msg_answer:text:0", "turn-diff:msg_idle_1", ]) // Hosts opt in, so a timeline without the setting lists no summaries. expect( Timeline.constructSessionMessageRows(source, true, { type: "busy" }).rows.some((row) => Predicate.isTagged(row, "TurnDiff"), ), ).toBe(false) }) })