Files
kilocode/packages/opencode/test/session/message-v2.test.ts
T
Johnny Eric Amancio ef6b152ff8 OpenCode v1.16.2 (#12088)
* feat(worktree): add managed workspace cloning (#30117)

* test(tui): skip crashing keymap textarea renderer

* fix(core): allow skipping migration execution

* fix(opencode): remove automatic full session diffs (#30127)

* chore: generate

* refactor(worktree): move project out of repository

* zen: deepseek flash

* fix(tui): remount session view on session switch (#30129)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>

* go: minimax m3

* refactor(opencode): inline local provider helpers (#30169)

* refactor(opencode): simplify provider setup flow (#30173)

* fix(app): show project sessions before path sync resolves (#30167)

Co-authored-by: LukeParkerDev <10430890+Hona@users.noreply.github.com>

* fix(core): preserve session metadata migration identity (#30176)

* refactor(session): align namespace imports and inline trivial helpers (#30180)

* opencode(run): add queued prompt management (#30103)

Direct run mode previously made submitted follow-up prompts irrevocable while a response was still running. Let users edit or remove queued prompts before dispatch without interrupting the active turn.

* chore: generate

* fix(acp): honor session/cancel by aborting the running turn (#30145)

Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>

* fix(tui): prevent prompt corruption when pasting near wide characters (#29710)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Simon Klee <hello@simonklee.dk>

* fix(opencode): avoid nullable webfetch format schema (#30215)

* chore: generate

* fix(core): contain lsp warmup defects (#30226)

* add run --replay mode (#30239)

* chore: generate

* chore: update nix node_modules hashes

* fix(stats): restore leaderboard spacing

* fix(stats): center top models dot grid

* fix(stats): stabilize top models hover

* fix(stats): align big-pickle provider resolution (#30274)

* feat(app): v2 desktop UI improvements (#29689)

Co-authored-by: Brendan Allan <git@brendonovich.dev>
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com>

* chore: generate

* fix(tui): clarify inline subagent rows (#30051)

* fix(tui): handle events across workspaces (#30281)

* feat(core): update Copilot for token-based billing (#30181)

* fix(tui): keep background marker with subagent label (#30271)

* fix(tui): keep retry attempt before message (#30275)

* chore: generate

* fix(opencode): enforce storage path invariants (#29666)

* chore: generate

* feat(core): add location-based permission service (#30287)

* chore: generate

* fix(tui): preserve live parts during session hydration (#30300)

* fix(app): restore deferred MCP status updates (#30220)

* fix: export v2 stylesheets and declare core node types (#30312)

* chore: update nix node_modules hashes

* fix(app): avoid suspending on pending child path (#30314)

* fix(opencode): remove sunsetted gpt-5.2 and gpt-5.3-codex from allowed models for codex subscriptions (#30316)

* chore: generate

* feat(core): expose session location

* chore: generate

* fix(opencode): preserve websocket api errors (#30321)

* refactor(core): simplify session pagination

* feat(core): add location filesystem contract

* feat(core): add dummy location filesystem layer

* chore: generate

* feat(opencode): add filesystem read and list routes

* chore: generate

* infra: stats

* sync

* feat(app): inset new layout session panels (#30342)

* fix(app): tab title truncation and close button positioning (#30349)

* tui: show model context in run footer (#30380)

* tui: revert OpenTUI upgrade to 0.2.16 (#30383)

* chore: update nix node_modules hashes

* feat(core): add managed repository cache (#30408)

* chore: generate

* chore: generate

* sync

* feat(stats): add cache ratio section

* feat(core): add flagged project references (#30414)

* chore: generate

* feat(core): support named migrations (#30418)

* fix(stats): clean retired provider rows during sync (#30420)

* fix(stats): mention opencode go in top models copy

* feat(core): expose project reference filesystem access (#30423)

* chore: generate

* sync

* fix(tui): scope diff viewer to session directory (#30426)

* test: widen provider header timeout margin (#30427)

* fix(plugin): restore private git install fallback (#30430)

* fix(stats): remove leaderboard nav link

* chore(opencode): remove scout agent (#30435)

* chore: generate

* feat(stats): improve cache ratio chart

* chore: generate

* fix(effect-drizzle-sqlite): preserve transaction begin errors (#30448)

* chore: bump effect beta to 74 (#30449)

* Revert "tui: revert OpenTUI upgrade to 0.2.16 (#30383)" (#30452)

* chore: update nix node_modules hashes

* refactor(opencode): improve startup time by 38% (#30453)

Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box>

* chore: generate

* fix(opencode): patch empty Gemini replay messages (#30463)

* chore: generate

* refactor(core): consolidate filesystem services (#30447)

* chore: generate

* run: enable interactive replay by default (#30465)

* chore: update nix node_modules hashes

* refactor(opencode): remove JSON storage migration (#30461)

* chore: generate

* chore: update nix node_modules hashes

* fix(tui): stop idle background task spinner (#30484)

* refactor(core): move v1 schemas into core (#30473)

* chore: generate

* fix: task id passed to background job for continuation (#30485)

* chore: generate

* feat(core): project copying and tracking directories (#30139)

* chore: generate

* fix(opencode): preserve signed thinking during anthropic reorder (#30182)

* Revert "fix(opencode): preserve signed thinking during anthropic reorder" (#30502)

* fix: rm tool reorder logic from old bug (#30483)

* chore: generate

* feat(app): polish home projects list UI (#30436)

* feat(app): polish select-v2 component (#30446)

Co-authored-by: Brendan Allan <git@brendonovich.dev>

* fix(github): enforce existing git author identity (#30507)

* feat(app): new update button  (#30460)

Co-authored-by: Brendan Allan <git@brendonovich.dev>

* fix(opencode): fallback to sh for curl upgrade (#30499)

Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>

* fix(ui): render whole-file patches as complete diffs (#30516)

* chore: generate

* feat(app): add servers tab to settings dialog (#29675)

* refactor(core): consolidate pty service (#30537)

* chore: generate

* tui: truncate sidebar file paths (#30531)

* chore: update nix node_modules hashes

* feat(stats): add geo breakdown (#30456)

* chore: generate

* chore: update nix node_modules hashes

* fix(acp): classify apply_patch as edit (#30564)

* fix(acp): classify task as think (#30565)

* fix(acp): include external directory permission context (#30567)

* fix(acp): clean read tool display content (#30569)

* fix(tui): route question responses by session directory (#30578)

* fix(stats): serve stats og image from banner

* docs(go): add Qwen3.7 Plus model (#30594)

* fix(openai): preserve websocket idle state (#30586)

* refactor(core): remove ai sdk option fields (#30581)

* chore: generate

* test(core): cover v1 provider option lowering (#30599)

* chore: generate

* refactor(core): nest model api id (#30603)

* fix(core): expose azure openai xhigh efforts (#30620)

* feat(core): add skill registry and file agent loading (#30617)

* chore: generate

* chore: update nix node_modules hashes

* fix(stats): count all go usage

* chore: remove zed extension and automation (#30628)

* fix(opencode): preserve variant for delegated tasks (#30630)

* zen: update nvidia tos

* fix(opencode): route SAP AI Core reasoning variants through modelParams (#30482)

* chore: generate

* fix(app): hide unavailable titlebar update (#30642)

* feat(app): v2 thinking level selector (#30646)

* fix(app,ui): session review reactivity and VCS query cache (#30660)

* feat(core): add embedded v2 session runtime and tool foundation (#30632)

* chore: generate

* chore: update nix node_modules hashes

* docs: correct compaction prune default (#30670)

* fix(opencode): avoid shell cancel race (#30641)

* feat: bump bedrock and add proper mantle support for openai models through aws bedrock (#30464)

* test: wait for shell truncation readiness (#30679)

* chore: update nix node_modules hashes

* refactor(opencode): clean up task tool prompts (#30687)

* feat(core): add command registry (#30624)

* chore: generate

* fix(acp): replay loaded session transcript (#30645)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>

* fix(core): reset pre-launch session projections (#30728)

* feat(tui): improve experimental session switcher (#30738)

* fix(opencode): respect disabled auto compaction on overflow (#30749)

* zen: nemotron 3 ultra

* fix(enterprise): install hono standard validator peer (#30740)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>

* fix build

* chore: update nix node_modules hashes

* make scripts executable

* fix(tui): show toast when variant_list keybind used with no variants (#30724)

* fix(opencode): `ACP.loadSession` should replay all messages (#30761)

Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>

* fix(opencode): attribute task child agent on creation (#30786)

* fix(tui): add Vue syntax highlighting (#30802)

* fix: bump @openrouter/ai-sdk-provider to 2.9.0 (#30800)

* feat(core): moving sessions (#30640)

* chore: generate

* tweak: background agent prompting to avoid polling issues (#30790)

* upgrade opentui to 0.3.2 (#30748)

* chore: update nix node_modules hashes

* feat(desktop): surface local server startup failures (#30822)

* ci: publish

* refactor(core): make v2 session inputs event sourced (#30785)

* chore: generate

* fix(llm): normalize OpenAI function tool schemas

* chore: generate

* feat(stats): refresh stats routes and homepage (#30419)

* fix(stats): sort metric charts by top usage

* feat(core): add public native API (#30828)

* chore: generate

* feat(app): color themes (#30824)

Co-authored-by: LukeParkerDev <10430890+Hona@users.noreply.github.com>

* chore: generate

* sync release versions for v1.16.0

* feat(core): attach global native tools (#30832)

* chore: generate

* feat(core): add Snowflake Cortex provider (#29901)

Co-authored-by: Cortex Code <noreply@snowflake.com>

* chore: generate

* feat(core): persist v2 session context epochs (#30789)

* chore: generate

* feat(tui): allow backgrounding synchronous subagents (#30488)

* fix(app): improve tab handling (#30669)

* chore: generate

* fix(tui): prioritize models slash autocomplete (#30848)

* fix(tui): route permission replies to session directory (#30851)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>

* fix(cli): harden daemon lifecycle (#30844)

* chore: generate

* feat(app): improve desktop multi-server support (#30678)

Co-authored-by: Brendan Allan <git@brendonovich.dev>

* chore: generate

* fix(app): handle tab overflow and scrolling in titlebar (#30886)

* fix(app): tab overflow (#30894)

* tui: guard path formatting inputs (#30469)

Fixes #27726, #25216, #24856, #24294, #17071, #29164, #24837, #16865, #14279, #29895

* opencode/run: refresh themes after terminal reloads (#30917)

* chore: generate

* fix(tui): fall back to local cwd when editor spawns in attach mode (#30583)

* docs: update Go Qwen tiered pricing (#30936)

* chore: generate

* feat(tui): add diff hunk navigation (#30935)

* chore: rm fuzzy search on references (#30931)

* fix: use mapError instead of orDie for context snapshot decoding (#30905)

Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>

* fix(core): recover corrupted models cache (#30947)

* chore: bun install (#30968)

* fix(opencode): resolve Bedrock hang by using node build conditions (#30873)

* fix(workflows): retry nix-hashes compute-hash on transient failure (#30743)

* fix(stats): scroll model charts to latest on mobile

* fix(opencode): prevent destructive edit matches (#30932)

* chore: generate

* fix(core): respect v2 default agents (#30969)

* chore: generate

* test(opencode): remove disposal event wait race (#30971)

* test(opencode): remove shell timeout output race (#30974)

* fix(opencode): gate reasoning summaries by provider (#30973)

* feat(core): admit v2 skill guidance (#30843)

* fix(workflows): serialize desktop release uploads (#30978)

* fix(stats): add mobile chart end spacing

* release: v1.16.2

* refactor: kilo compat for v1.16.2

* fix(opencode): address v1.16.2 merge regressions

* chore: update kilo-vscode visual regression baselines

* fix(opencode): restore Kilo behavior after v1.16.2 merge

* fix(opencode): retry Windows migration cleanup

* test(opencode): restore clone and macOS watcher coverage

* fix(opencode): address second-pass review for #12099

Preserve imported usage and retry partial JSON migrations. Refresh active dependency patches, remove the obsolete GCP patch, and regenerate Kilo HttpApi branding.

---------

Co-authored-by: Dax <mail@thdxr.com>
Co-authored-by: Dax Raad <d@ironbay.co>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Frank <frank@anoma.ly>
Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
Co-authored-by: Michael Hart <mhart@cloudflare.com>
Co-authored-by: LukeParkerDev <10430890+Hona@users.noreply.github.com>
Co-authored-by: Simon Klee <hello@simonklee.dk>
Co-authored-by: smagnuso <smagnuso@gmail.com>
Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>
Co-authored-by: Orca丶 <93272799+dauphinYan@users.noreply.github.com>
Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com>
Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com>
Co-authored-by: Brendan Allan <git@brendonovich.dev>
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com>
Co-authored-by: Kit Langton <kit.langton@gmail.com>
Co-authored-by: James Long <longster@gmail.com>
Co-authored-by: Dustin Deus <deusdustin@gmail.com>
Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box>
Co-authored-by: Ulises Jeremias <ulisescf.24@gmail.com>
Co-authored-by: Jack <jack@anoma.ly>
Co-authored-by: Jérôme Benoit <jerome.benoit@sap.com>
Co-authored-by: Ariane Emory <97994360+ariane-emory@users.noreply.github.com>
Co-authored-by: LIU Xinyu <contact@lxy.cc>
Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
Co-authored-by: Sebastian <hasta84@gmail.com>
Co-authored-by: opencode <opencode@sst.dev>
Co-authored-by: Kamesh Sampath <kamesh.sampath@hotmail.com>
Co-authored-by: Cortex Code <noreply@snowflake.com>
Co-authored-by: pcadena-lila <pcadena@lila.ai>
Co-authored-by: weiconghe <46336277+weiconghe@users.noreply.github.com>
Co-authored-by: alberto <914199+alblez@users.noreply.github.com>
Co-authored-by: kilo-maintainer[bot] <kilo-maintainer[bot]@users.noreply.github.com>
2026-07-13 18:00:35 +02:00

1662 lines
46 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { APICallError } from "ai"
import { MessageV2 } from "../../src/session/message-v2"
import { ProviderTransform } from "@/provider/transform"
import type { Provider } from "@/provider/provider"
import { SessionID, MessageID, PartID } from "../../src/session/schema"
import { Question } from "../../src/question"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"
const sessionID = SessionID.make("session")
const providerID = ProviderV2.ID.make("test")
const model: Provider.Model = {
id: ModelV2.ID.make("test-model"),
providerID,
api: {
id: "test-model",
url: "https://example.com",
npm: "@ai-sdk/openai",
},
name: "Test Model",
capabilities: {
temperature: true,
reasoning: false,
attachment: false,
toolcall: true,
input: {
text: true,
audio: false,
image: false,
video: false,
pdf: false,
},
output: {
text: true,
audio: false,
image: false,
video: false,
pdf: false,
},
interleaved: false,
},
cost: {
input: 0,
output: 0,
cache: {
read: 0,
write: 0,
},
},
limit: {
context: 0,
input: 0,
output: 0,
},
status: "active",
options: {},
headers: {},
release_date: "2026-01-01",
}
function userInfo(id: string): SessionV1.User {
return {
id,
sessionID,
role: "user",
time: { created: 0 },
agent: "user",
model: { providerID, modelID: ModelV2.ID.make("test") },
tools: {},
mode: "",
} as unknown as SessionV1.User
}
function assistantInfo(
id: string,
parentID: string,
error?: SessionV1.Assistant["error"],
meta?: { providerID: string; modelID: string },
): SessionV1.Assistant {
const infoModel = meta ?? { providerID: model.providerID, modelID: model.api.id }
return {
id,
sessionID,
role: "assistant",
time: { created: 0 },
error,
parentID,
modelID: infoModel.modelID,
providerID: infoModel.providerID,
mode: "",
agent: "agent",
path: { cwd: "/", root: "/" },
cost: 0,
tokens: {
input: 0,
output: 0,
reasoning: 0,
cache: { read: 0, write: 0 },
},
} as unknown as SessionV1.Assistant
}
function basePart(messageID: string, id: string) {
return {
id: PartID.make(id.startsWith("prt") ? id : `prt_${id}`),
sessionID,
messageID: MessageID.make(messageID.startsWith("msg") ? messageID : `msg_${messageID}`),
}
}
describe("session.message-v2.toModelMessage", () => {
test("filters out messages with no parts", async () => {
const input: SessionV1.WithParts[] = [
{
info: userInfo("m-empty"),
parts: [],
},
{
info: userInfo("m-user"),
parts: [
{
...basePart("m-user", "p1"),
type: "text",
text: "hello",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "hello" }],
},
])
})
test("filters out messages with only ignored parts", async () => {
const messageID = "m-user"
const input: SessionV1.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "ignored",
ignored: true,
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("filters out user messages with only empty text parts", async () => {
const messageID = "m-user"
const input: SessionV1.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("filters empty user text parts while keeping non-empty parts", async () => {
const messageID = "m-user"
const input: SessionV1.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "",
},
{
...basePart(messageID, "p2"),
type: "text",
text: "hello",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "hello" }],
},
])
})
test("includes synthetic text parts", async () => {
const messageID = "m-user"
const input: SessionV1.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "hello",
synthetic: true,
},
] as SessionV1.Part[],
},
{
info: assistantInfo("m-assistant", messageID),
parts: [
{
...basePart("m-assistant", "a1"),
type: "text",
text: "assistant",
synthetic: true,
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "hello" }],
},
{
role: "assistant",
content: [{ type: "text", text: "assistant" }],
},
])
})
test("converts user text/file parts and injects compaction/subtask prompts", async () => {
const messageID = "m-user"
const input: SessionV1.WithParts[] = [
{
info: userInfo(messageID),
parts: [
{
...basePart(messageID, "p1"),
type: "text",
text: "hello",
},
{
...basePart(messageID, "p2"),
type: "text",
text: "ignored",
ignored: true,
},
{
...basePart(messageID, "p3"),
type: "file",
mime: "image/png",
filename: "img.png",
url: "https://example.com/img.png",
},
{
...basePart(messageID, "p4"),
type: "file",
mime: "text/plain",
filename: "note.txt",
url: "https://example.com/note.txt",
},
{
...basePart(messageID, "p5"),
type: "file",
mime: "application/x-directory",
filename: "dir",
url: "https://example.com/dir",
},
{
...basePart(messageID, "p6"),
type: "compaction",
auto: true,
},
{
...basePart(messageID, "p7"),
type: "subtask",
prompt: "prompt",
description: "desc",
agent: "agent",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [
{ type: "text", text: "hello" },
{
type: "file",
mediaType: "image/png",
filename: "img.png",
data: "https://example.com/img.png",
},
{ type: "text", text: "What did we do so far?" },
{ type: "text", text: "The following tool was executed by the user" },
],
},
])
})
test("converts assistant tool completion into tool-call + tool-result messages with attachments", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "text",
text: "done",
metadata: { openai: { assistant: "meta" } },
},
{
...basePart(assistantID, "a2"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "completed",
input: { cmd: "ls" },
output: "ok",
title: "Bash",
metadata: {},
time: { start: 0, end: 1 },
attachments: [
{
...basePart(assistantID, "file-1"),
type: "file",
mime: "image/png",
filename: "attachment.png",
url: "data:image/png;base64,Zm9v",
},
],
},
metadata: { openai: { tool: "meta" } },
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{ type: "text", text: "done", providerOptions: { openai: { assistant: "meta" } } },
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
providerOptions: { openai: { tool: "meta" } },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: {
type: "content",
value: [
{ type: "text", text: "ok" },
{ type: "media", mediaType: "image/png", data: "Zm9v" },
],
},
providerOptions: { openai: { tool: "meta" } },
},
],
},
])
})
test("preserves jpeg tool-result media for anthropic models", async () => {
const anthropicModel: Provider.Model = {
...model,
id: ModelV2.ID.make("anthropic/claude-opus-4-7"),
providerID: ProviderV2.ID.make("anthropic"),
api: {
id: "claude-opus-4-7-20250805",
url: "https://api.anthropic.com",
npm: "@ai-sdk/anthropic",
},
capabilities: {
...model.capabilities,
attachment: true,
input: {
...model.capabilities.input,
image: true,
pdf: true,
},
},
}
const jpeg = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01]).toString(
"base64",
)
const userID = "m-user-anthropic"
const assistantID = "m-assistant-anthropic"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1-anthropic"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1-anthropic"),
type: "tool",
callID: "call-anthropic-1",
tool: "read",
state: {
status: "completed",
input: { filePath: "/tmp/rails-demo.png" },
output: "Image read successfully",
title: "Read",
metadata: {},
time: { start: 0, end: 1 },
attachments: [
{
...basePart(assistantID, "file-anthropic-1"),
type: "file",
mime: "image/jpeg",
filename: "rails-demo.png",
url: `data:image/jpeg;base64,${jpeg}`,
},
],
},
},
] as SessionV1.Part[],
},
]
const result = ProviderTransform.message(await MessageV2.toModelMessages(input, anthropicModel), anthropicModel, {})
expect(result).toHaveLength(3)
expect(result[2].role).toBe("tool")
expect(result[2].content[0]).toMatchObject({
type: "tool-result",
toolCallId: "call-anthropic-1",
toolName: "read",
output: {
type: "content",
value: [
{ type: "text", text: "Image read successfully" },
{ type: "media", mediaType: "image/jpeg", data: jpeg },
],
},
})
})
test("moves bedrock pdf tool-result media into a separate user message", async () => {
const bedrockModel: Provider.Model = {
...model,
id: ModelV2.ID.make("amazon-bedrock/anthropic.claude-sonnet-4-6"),
providerID: ProviderV2.ID.make("amazon-bedrock"),
api: {
id: "anthropic.claude-sonnet-4-6",
url: "https://bedrock-runtime.us-east-1.amazonaws.com",
npm: "@ai-sdk/amazon-bedrock",
},
capabilities: {
...model.capabilities,
attachment: true,
input: {
...model.capabilities.input,
image: true,
pdf: true,
},
},
}
const pdf = Buffer.from("%PDF-1.4\n").toString("base64")
const userID = "m-user-bedrock-pdf"
const assistantID = "m-assistant-bedrock-pdf"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1-bedrock-pdf"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1-bedrock-pdf"),
type: "tool",
callID: "call-bedrock-pdf-1",
tool: "read",
state: {
status: "completed",
input: { filePath: "/tmp/example.pdf" },
output: "PDF read successfully",
title: "Read",
metadata: {},
time: { start: 0, end: 1 },
attachments: [
{
...basePart(assistantID, "file-bedrock-pdf-1"),
type: "file",
mime: "application/pdf",
filename: "example.pdf",
url: `data:application/pdf;base64,${pdf}`,
},
],
},
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, bedrockModel)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-bedrock-pdf-1",
toolName: "read",
input: { filePath: "/tmp/example.pdf" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-bedrock-pdf-1",
toolName: "read",
output: { type: "text", value: "PDF read successfully" },
},
],
},
{
role: "user",
content: [
{ type: "text", text: "Attached media from tool result:" },
{
type: "file",
mediaType: "application/pdf",
filename: "example.pdf",
data: `data:application/pdf;base64,${pdf}`,
},
],
},
])
})
test("omits provider metadata when assistant model differs", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID, undefined, { providerID: "other", modelID: "other" }),
parts: [
{
...basePart(assistantID, "a1"),
type: "text",
text: "done",
metadata: { openai: { assistant: "meta" } },
},
{
...basePart(assistantID, "a2"),
type: "reasoning",
text: "thinking",
metadata: { openai: { reasoning: "meta" } },
time: { start: 0 },
},
{
...basePart(assistantID, "a3"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "completed",
input: { cmd: "ls" },
output: "ok",
title: "Bash",
metadata: {},
time: { start: 0, end: 1 },
},
metadata: { openai: { tool: "meta" } },
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{ type: "text", text: "done" },
{ type: "text", text: "thinking" },
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "text", value: "ok" },
},
],
},
])
})
test("replaces compacted tool output with placeholder", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "completed",
input: { cmd: "ls" },
output: "this should be cleared",
title: "Bash",
metadata: {},
time: { start: 0, end: 1, compacted: 1 },
},
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "text", value: "[Old tool result content cleared]" },
},
],
},
])
})
test("truncates tool output when requested", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "completed",
input: { cmd: "ls" },
output: "abcdefghij",
title: "Shell",
metadata: {},
time: { start: 0, end: 1 },
},
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model, { toolOutputMaxChars: 4 })).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: {
type: "text",
value: "abcd\n[Tool output truncated for compaction: omitted 6 chars]",
},
},
],
},
])
})
test("converts assistant tool error into error-text tool result", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "error",
input: { cmd: "ls" },
error: "nope",
time: { start: 0, end: 1 },
metadata: {},
},
metadata: { openai: { tool: "meta" } },
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
providerOptions: { openai: { tool: "meta" } },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "error-text", value: "nope" },
providerOptions: { openai: { tool: "meta" } },
},
],
},
])
})
test("forwards partial bash output for aborted tool calls", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const output = [
"31403",
"12179",
"4575",
"",
"<shell_metadata>",
"User aborted the command",
"</shell_metadata>",
].join("\n")
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "tool",
callID: "call-1",
tool: "bash",
state: {
status: "error",
input: { command: "for i in {1..20}; do print -- $RANDOM; sleep 1; done" },
error: "Tool execution aborted",
metadata: { interrupted: true, output },
time: { start: 0, end: 1 },
},
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "bash",
input: { command: "for i in {1..20}; do print -- $RANDOM; sleep 1; done" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "text", value: output },
},
],
},
])
})
test("filters assistant messages with non-abort errors", async () => {
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(
assistantID,
"m-parent",
new SessionV1.APIError({ message: "boom", isRetryable: true }).toObject() as SessionV1.APIError,
),
parts: [
{
...basePart(assistantID, "a1"),
type: "text",
text: "should not render",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("includes aborted assistant messages only when they have non-step-start/reasoning content", async () => {
const assistantID1 = "m-assistant-1"
const assistantID2 = "m-assistant-2"
const aborted = new SessionV1.AbortedError({
message: "aborted",
}).toObject() as SessionV1.Assistant["error"]
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID1, "m-parent", aborted),
parts: [
{
...basePart(assistantID1, "a1"),
type: "reasoning",
text: "thinking",
time: { start: 0 },
},
{
...basePart(assistantID1, "a2"),
type: "text",
text: "partial answer",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID2, "m-parent", aborted),
parts: [
{
...basePart(assistantID2, "b1"),
type: "step-start",
},
{
...basePart(assistantID2, "b2"),
type: "reasoning",
text: "thinking",
time: { start: 0 },
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "assistant",
content: [
{ type: "reasoning", text: "thinking", providerOptions: undefined },
{ type: "text", text: "partial answer" },
],
},
])
})
test("preserves OpenRouter reasoning details through provider transform", async () => {
const assistantID = "m-assistant"
const openrouterModel: Provider.Model = {
...model,
id: ModelV2.ID.make("deepseek/deepseek-v4-pro"),
providerID: ProviderV2.ID.make("openrouter"),
api: {
id: "deepseek/deepseek-v4-pro",
url: "https://openrouter.ai/api/v1",
npm: "@openrouter/ai-sdk-provider",
},
capabilities: {
...model.capabilities,
reasoning: true,
interleaved: { field: "reasoning_details" },
},
}
const reasoningDetails = [
{
type: "reasoning.text",
text: "thinking",
format: "unknown",
index: 0,
},
]
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent", undefined, {
providerID: openrouterModel.providerID,
modelID: openrouterModel.id,
}),
parts: [
{
...basePart(assistantID, "a1"),
type: "reasoning",
text: "thinking",
time: { start: 0 },
metadata: {
openrouter: {
reasoning_details: reasoningDetails,
},
},
},
{
...basePart(assistantID, "a2"),
type: "text",
text: "answer",
},
] as SessionV1.Part[],
},
]
expect(
ProviderTransform.message(await MessageV2.toModelMessages(input, openrouterModel), openrouterModel, {}),
).toStrictEqual([
{
role: "assistant",
content: [
{
type: "reasoning",
text: "thinking",
providerOptions: {
openrouter: {
reasoning_details: reasoningDetails,
},
},
},
{ type: "text", text: "answer" },
],
},
])
})
test("splits assistant messages on step-start boundaries", async () => {
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{
...basePart(assistantID, "p1"),
type: "text",
text: "first",
},
{
...basePart(assistantID, "p2"),
type: "step-start",
},
{
...basePart(assistantID, "p3"),
type: "text",
text: "second",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
{
role: "assistant",
content: [{ type: "text", text: "first" }],
},
{
role: "assistant",
content: [{ type: "text", text: "second" }],
},
])
})
test("drops messages that only contain step-start parts", async () => {
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{
...basePart(assistantID, "p1"),
type: "step-start",
},
] as SessionV1.Part[],
},
]
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([])
})
test("converts pending/running tool calls to error results to prevent dangling tool_use", async () => {
const userID = "m-user"
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: userInfo(userID),
parts: [
{
...basePart(userID, "u1"),
type: "text",
text: "run tool",
},
] as SessionV1.Part[],
},
{
info: assistantInfo(assistantID, userID),
parts: [
{
...basePart(assistantID, "a1"),
type: "tool",
callID: "call-pending",
tool: "bash",
state: {
status: "pending",
input: { cmd: "ls" },
raw: "",
},
},
{
...basePart(assistantID, "a2"),
type: "tool",
callID: "call-running",
tool: "read",
state: {
status: "running",
input: { path: "/tmp" },
time: { start: 0 },
},
},
] as SessionV1.Part[],
},
]
const result = await MessageV2.toModelMessages(input, model)
expect(result).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-pending",
toolName: "bash",
input: { cmd: "ls" },
providerExecuted: undefined,
},
{
type: "tool-call",
toolCallId: "call-running",
toolName: "read",
input: { path: "/tmp" },
providerExecuted: undefined,
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-pending",
toolName: "bash",
output: { type: "error-text", value: "[Tool execution was interrupted]" },
},
{
type: "tool-result",
toolCallId: "call-running",
toolName: "read",
output: { type: "error-text", value: "[Tool execution was interrupted]" },
},
],
},
])
})
test("substitutes space for empty text between signed reasoning blocks", async () => {
// Reproduces the bug pattern: [reasoning(sig), text(""), reasoning(sig), text(full)]
const assistantID = "m-assistant"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "step-start" },
{
...basePart(assistantID, "p2"),
type: "reasoning",
text: "thinking-one",
metadata: { anthropic: { signature: "sig1" } },
},
{ ...basePart(assistantID, "p3"), type: "text", text: "" },
{ ...basePart(assistantID, "p4"), type: "step-start" },
{
...basePart(assistantID, "p5"),
type: "reasoning",
text: "thinking-two",
metadata: { anthropic: { signature: "sig2" } },
},
{ ...basePart(assistantID, "p6"), type: "text", text: "the answer" },
] as SessionV1.Part[],
},
]
const result = await MessageV2.toModelMessages(input, model)
// step-start splits into two assistant messages; SDK's groupIntoBlocks merges them later
expect(result).toHaveLength(2)
expect((result[0].content as any[]).find((p) => p.type === "text").text).toBe(" ")
expect((result[1].content as any[]).find((p) => p.type === "text").text).toBe("the answer")
})
test("leaves empty text alone when reasoning signature is under 'bedrock' namespace", async () => {
// Bedrock signed reasoning is preserved as reasoning metadata, but unlike the
// direct Anthropic path we do not preserve empty text separators for Bedrock.
const assistantID = "m-assistant-bedrock"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{
...basePart(assistantID, "p1"),
type: "reasoning",
text: "thinking-bedrock",
metadata: { bedrock: { signature: "bedrock-sig" } },
},
{ ...basePart(assistantID, "p2"), type: "text", text: "" },
{ ...basePart(assistantID, "p3"), type: "text", text: "answer" },
] as SessionV1.Part[],
},
]
const result = await MessageV2.toModelMessages(input, model)
expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual(["", "answer"])
})
test("leaves empty text alone when reasoning has no Anthropic signature", async () => {
// Non-Anthropic providers' reasoning doesn't position-validate, so empty text
// should be filtered normally rather than substituted.
const assistantID = "m-assistant-unsigned"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "reasoning", text: "thinking" },
{ ...basePart(assistantID, "p2"), type: "text", text: "" },
{ ...basePart(assistantID, "p3"), type: "text", text: "answer" },
] as SessionV1.Part[],
},
]
const result = await MessageV2.toModelMessages(input, model)
expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual(["", "answer"])
})
test("leaves empty text alone in assistant messages without reasoning", async () => {
const assistantID = "m-assistant-no-reasoning"
const input: SessionV1.WithParts[] = [
{
info: assistantInfo(assistantID, "m-parent"),
parts: [
{ ...basePart(assistantID, "p1"), type: "text", text: "" },
{ ...basePart(assistantID, "p2"), type: "text", text: "hello" },
] as SessionV1.Part[],
},
]
const result = await MessageV2.toModelMessages(input, model)
expect(result).toHaveLength(1)
const texts = (result[0].content as any[]).filter((p) => p.type === "text")
expect(texts.map((t) => t.text)).toStrictEqual(["", "hello"])
})
})
describe("session.message-v2.fromError", () => {
test("serializes context_length_exceeded as ContextOverflowError", () => {
const input = {
type: "error",
error: {
code: "context_length_exceeded",
},
}
const result = MessageV2.fromError(input, { providerID })
expect(result).toStrictEqual({
name: "ContextOverflowError",
data: {
message: "Input exceeds context window of this model",
responseBody: JSON.stringify(input),
},
})
})
test("serializes response error codes", () => {
const cases = [
{
code: "insufficient_quota",
message: "Quota exceeded. Check your plan and billing details.",
},
{
code: "usage_not_included",
message: "To use Codex with your ChatGPT plan, upgrade to Plus: https://chatgpt.com/explore/plus.",
},
{
code: "invalid_prompt",
message: "Invalid prompt from test",
},
]
cases.forEach((item) => {
const input = {
type: "error",
error: {
code: item.code,
message: item.code === "invalid_prompt" ? item.message : undefined,
},
}
const result = MessageV2.fromError(input, { providerID })
expect(result).toStrictEqual({
name: "APIError",
data: {
message: item.message,
isRetryable: false,
responseBody: JSON.stringify(input),
},
})
})
})
test("serializes OpenAI response server_error stream chunks as retryable APIError", () => {
const body = {
type: "error",
sequence_number: 2,
error: {
type: "server_error",
code: "server_error",
message:
"An error occurred while processing your request. You can retry your request, or contact us through our help center at help.openai.com if the error persists. Please include the request ID req_77eccd008d984bf6bf82d1b2c2b68715 in your message.",
param: null,
},
}
const result = MessageV2.fromError({ message: JSON.stringify(body) }, { providerID })
expect(result).toStrictEqual({
name: "APIError",
data: {
message: body.error.message,
isRetryable: true,
responseBody: JSON.stringify(body),
},
})
})
test("detects context overflow from APICallError provider messages", () => {
const cases = [
"prompt is too long: 213462 tokens > 200000 maximum",
"Your input exceeds the context window of this model",
"The input token count (1196265) exceeds the maximum number of tokens allowed (1048575)",
"Please reduce the length of the messages or completion",
"400 status code (no body)",
"413 status code (no body)",
]
cases.forEach((message) => {
const error = new APICallError({
message,
url: "https://example.com",
requestBodyValues: {},
statusCode: 400,
responseHeaders: { "content-type": "application/json" },
isRetryable: false,
})
const result = MessageV2.fromError(error, { providerID })
expect(SessionV1.ContextOverflowError.isInstance(result)).toBe(true)
})
})
test("detects context overflow from context_length_exceeded code in response body", () => {
const error = new APICallError({
message: "Request failed",
url: "https://example.com",
requestBodyValues: {},
statusCode: 422,
responseHeaders: { "content-type": "application/json" },
responseBody: JSON.stringify({
error: {
message: "Some message",
type: "invalid_request_error",
code: "context_length_exceeded",
},
}),
isRetryable: false,
})
const result = MessageV2.fromError(error, { providerID })
expect(SessionV1.ContextOverflowError.isInstance(result)).toBe(true)
})
test("does not classify 429 no body as context overflow", () => {
const result = MessageV2.fromError(
new APICallError({
message: "429 status code (no body)",
url: "https://example.com",
requestBodyValues: {},
statusCode: 429,
responseHeaders: { "content-type": "application/json" },
isRetryable: false,
}),
{ providerID },
)
expect(SessionV1.ContextOverflowError.isInstance(result)).toBe(false)
expect(SessionV1.APIError.isInstance(result)).toBe(true)
})
test("serializes unknown inputs", () => {
const result = MessageV2.fromError(123, { providerID })
expect(result).toStrictEqual({
name: "UnknownError",
data: {
message: "123",
},
})
})
test("serializes tagged errors with their message", () => {
const result = MessageV2.fromError(new Question.RejectedError(), { providerID })
expect(result).toStrictEqual({
name: "UnknownError",
data: {
message: "The user dismissed this question",
},
})
})
test("classifies ZlibError from fetch as retryable APIError", () => {
const zlibError = new Error(
'ZlibError fetching "https://opencode.cloudflare.dev/anthropic/messages". For more information, pass `verbose: true` in the second argument to fetch()',
)
;(zlibError as any).code = "ZlibError"
;(zlibError as any).errno = 0
;(zlibError as any).path = ""
const result = MessageV2.fromError(zlibError, { providerID })
expect(SessionV1.APIError.isInstance(result)).toBe(true)
expect((result as SessionV1.APIError).data.isRetryable).toBe(true)
expect((result as SessionV1.APIError).data.message).toInclude("decompression")
})
test("classifies ZlibError as AbortedError when abort context is provided", () => {
const zlibError = new Error(
'ZlibError fetching "https://opencode.cloudflare.dev/anthropic/messages". For more information, pass `verbose: true` in the second argument to fetch()',
)
;(zlibError as any).code = "ZlibError"
;(zlibError as any).errno = 0
const result = MessageV2.fromError(zlibError, { providerID, aborted: true })
expect(result.name).toBe("MessageAbortedError")
})
})
describe("session.message-v2.latest", () => {
const TAIL_USER = MessageID.make("msg_001")
const OVERFLOW_ASSISTANT = MessageID.make("msg_002")
const COMPACTION_USER = MessageID.make("msg_003")
const SUMMARY_ASSISTANT = MessageID.make("msg_004")
const CONTINUE_USER = MessageID.make("msg_005")
const NEW_COMPACTION_USER = MessageID.make("msg_006")
const tailUser: SessionV1.WithParts = {
info: userInfo(TAIL_USER),
parts: [{ ...basePart(TAIL_USER, "p1"), type: "text", text: "original prompt" }] as SessionV1.Part[],
}
const overflowAssistant: SessionV1.WithParts = {
info: {
...assistantInfo(OVERFLOW_ASSISTANT, TAIL_USER),
finish: "tool-calls",
tokens: { input: 280_000, output: 200, reasoning: 0, cache: { read: 0, write: 0 }, total: 280_200 },
} as SessionV1.Assistant,
parts: [],
}
const compactionUser: SessionV1.WithParts = {
info: userInfo(COMPACTION_USER),
parts: [
{
...basePart(COMPACTION_USER, "p1"),
type: "compaction",
auto: true,
tail_start_id: TAIL_USER,
},
] as SessionV1.Part[],
}
const summaryAssistant: SessionV1.WithParts = {
info: {
...assistantInfo(SUMMARY_ASSISTANT, COMPACTION_USER),
summary: true,
finish: "stop",
tokens: { input: 150_000, output: 1_500, reasoning: 0, cache: { read: 0, write: 0 }, total: 151_500 },
} as SessionV1.Assistant,
parts: [],
}
const continueUser: SessionV1.WithParts = {
info: userInfo(CONTINUE_USER),
parts: [
{
...basePart(CONTINUE_USER, "p1"),
type: "text",
text: "Continue if you have next steps...",
synthetic: true,
metadata: { compaction_continue: true },
},
] as SessionV1.Part[],
}
// Regression for double auto-compaction. The reorder in filterCompacted
// (#27145) returns [compaction-user, summary, ...tail..., continue-user],
// so picking lastFinished by array position landed on the pre-compaction
// overflow assistant and bypassed the `summary !== true` overflow guard
// in SessionPrompt.runLoop, firing a second compaction.create immediately.
test("finished is the chronologically-latest finished assistant, not the array-latest", () => {
const filtered = MessageV2.filterCompacted([
continueUser,
summaryAssistant,
compactionUser,
overflowAssistant,
tailUser,
])
const state = MessageV2.latest(filtered)
expect(state.finished?.id).toBe(SUMMARY_ASSISTANT)
expect(state.finished?.summary).toBe(true)
expect(state.user?.id).toBe(CONTINUE_USER)
expect(state.tasks).toEqual([])
})
test("a fresh compaction-user newer than the latest summary surfaces in tasks", () => {
const newCompactionUser: SessionV1.WithParts = {
info: userInfo(NEW_COMPACTION_USER),
parts: [
{
...basePart(NEW_COMPACTION_USER, "p1"),
type: "compaction",
auto: true,
},
] as SessionV1.Part[],
}
const state = MessageV2.latest([
tailUser,
overflowAssistant,
compactionUser,
summaryAssistant,
continueUser,
newCompactionUser,
])
expect(state.finished?.id).toBe(SUMMARY_ASSISTANT)
expect(state.user?.id).toBe(NEW_COMPACTION_USER)
expect(state.tasks).toHaveLength(1)
expect(state.tasks[0]).toMatchObject({ type: "compaction", auto: true })
})
})