Skip to content

Commit 3a94ff0

Browse files
sahrizviclaude
andcommitted
fix(session): address consensus review on the toolless flattening
Findings verified by execution before fixing; each was reproduced first. - Consecutive assistant turns (Major #1). Removing the intervening `tool` message left the ordinary agentic shape `user -> assistant(call) -> tool(result) -> assistant(text) -> user` with two adjacent assistant messages, contradicting the docstring's claim that role alternation is preserved. Assistant turns now coalesce. Checked against the live gateway: it accepts consecutive assistant messages, so this was a correctness and documentation defect rather than a live failure. - Title generation never reached the flattening (Major #2). `ensureTitle` passed `tools: {}` without a `toolChoice`, so `addHistoricalToolStubs` repopulated `tools` from tool parts in the context, `declaresNoTools` became false, and the transform never ran. It now passes `toolChoice: "none"`, as `compaction.ts` already did. - `tool-result` parts embedded in an assistant message are flattened too (Minor #3); they previously survived structurally intact. - The blank-text filter no longer touches parts this transform did not create (Minor #4). It was stripping the single space `message-v2.ts` preserves as a separator between Anthropic signed-reasoning blocks. - `content`-array tool output is unwrapped to its text instead of being JSON-stringified (Minor #8), which also shrinks the flattened payload that Minor #5 flagged as escaping the token estimate. - The test docstring no longer claims OpenAI and Anthropic reject the shape (Minor #9); they accept it, as the implementation and PR body already said. Seven regression tests added, covering the canonical shape, the preserved separator, embedded results, unwrapped content output, parallel calls, output shapes with no `value`, and caller-input immutability. Mutation-checked: disabling the merge fails 2, widening the blank filter fails 1. Deferred, with reasons: marker escaping and output truncation (Minor #7) — after the merge fix a call and its result are adjacent in one turn, so ordering already carries correlation; `any` in the render helpers (NIT #10) — deliberate glue for SDK shape variability. The `summary` agent named in the PR description has no `Agent.get("summary")` call site anywhere in the tree, so only compaction and title actually reach this code; the PR body is corrected accordingly. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01V4pzHPsqfbTdLMKzMaSYcF
1 parent 1ca2491 commit 3a94ff0

3 files changed

Lines changed: 207 additions & 41 deletions

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -370,41 +370,48 @@ export namespace ProviderTransform {
370370

371371
// altimate_change start — expose the pure request projection used before input-budget estimation
372372
// altimate_change start — flatten tool history when a request declares no tools
373-
/**
374-
* Render a tool call as the plain text a summarizer can still read.
375-
*/
373+
/** JSON.stringify that never throws on circular or non-serializable values. */
374+
function safeJson(value: unknown): string {
375+
try {
376+
return JSON.stringify(value) ?? ""
377+
} catch {
378+
return String(value)
379+
}
380+
}
381+
382+
/** Render a tool call as the plain text a summarizer can still read. */
376383
function renderToolCall(part: any): string {
377384
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
378385
const raw = part?.input ?? part?.args
379-
let args = ""
380-
if (typeof raw === "string") args = raw
381-
else if (raw !== undefined) {
382-
try {
383-
args = JSON.stringify(raw)
384-
} catch {
385-
args = String(raw)
386-
}
387-
}
386+
const args = typeof raw === "string" ? raw : raw === undefined ? "" : safeJson(raw)
388387
return args ? `[tool call: ${name}(${args})]` : `[tool call: ${name}]`
389388
}
390389

391390
/**
392391
* Render a tool result as plain text, tolerating every output shape the SDK emits.
392+
*
393+
* The `content` array shape carries readable text parts; stringifying it whole would hand the
394+
* summarizer `[{"type":"text","text":"…"}]` instead of the text it needs, so it is unwrapped.
393395
*/
394396
function renderToolResult(part: any): string {
395397
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
396398
const out = part?.output ?? part?.result
397399
let body = ""
398400
if (typeof out === "string") body = out
399401
else if (out && typeof out === "object") {
400-
const value = (out as any).value ?? out
401-
if (typeof value === "string") body = value
402-
else {
403-
try {
404-
body = JSON.stringify(value)
405-
} catch {
406-
body = String(value)
407-
}
402+
const shape = out as any
403+
if (shape.type === "content" && Array.isArray(shape.value)) {
404+
body = shape.value
405+
.map((item: any) => {
406+
if (item?.type === "text" && typeof item.text === "string") return item.text
407+
if (item?.type === "media") return `[${item.mediaType ?? "media"}]`
408+
return safeJson(item)
409+
})
410+
.filter((line: string) => line.length > 0)
411+
.join("\n")
412+
} else {
413+
const value = shape.value ?? shape
414+
body = typeof value === "string" ? value : safeJson(value)
408415
}
409416
} else if (out !== undefined) body = String(out)
410417
return `[tool result: ${name}]${body ? `\n${body}` : ""}`
@@ -422,14 +429,33 @@ export namespace ProviderTransform {
422429
*
423430
* Flattening is applied to every provider rather than branching on one, because a request
424431
* that declares no tools has no use for structured tool parts either way: the toolless
425-
* agents — compaction, title and summary — only summarize. One code path avoids a
426-
* provider-specific branch that would silently rot as the gateway changes.
432+
* agents only summarize. One code path avoids a provider-specific branch that would silently
433+
* rot as the gateway changes.
434+
*
435+
* Consecutive assistant turns are coalesced. Removing the intervening `tool` message would
436+
* otherwise leave two adjacent assistant messages for the ordinary agentic shape
437+
* (`user → assistant(call) → tool(result) → assistant(text) → user`). The AI SDK's Anthropic
438+
* provider happens to coalesce those before transport, but the gateway's own request path is
439+
* not known to, and this fix exists precisely because the gateway is stricter than upstream.
427440
*
428-
* Flattening keeps the information a summarizer needs (which tool ran, with what arguments,
429-
* and what it returned) while removing the structure the provider would validate.
441+
* Parts that were not converted pass through untouched, so an intentionally blank assistant
442+
* text part — the separator `message-v2.ts` preserves between Anthropic signed-reasoning
443+
* blocks — is never dropped here.
430444
*/
431445
export function flattenToolParts(msgs: ModelMessage[]): ModelMessage[] {
432446
const result: ModelMessage[] = []
447+
448+
// Every message this function appends is freshly constructed, so extending `content` in
449+
// place can never mutate the caller's input.
450+
const appendAssistant = (parts: any[]) => {
451+
const previous = result.at(-1)
452+
if (previous && previous.role === "assistant" && Array.isArray(previous.content)) {
453+
previous.content = [...previous.content, ...parts] as typeof previous.content
454+
return
455+
}
456+
result.push({ role: "assistant", content: parts } as ModelMessage)
457+
}
458+
433459
for (const msg of msgs) {
434460
if (msg.role === "tool") {
435461
const parts = Array.isArray(msg.content) ? msg.content : []
@@ -438,23 +464,23 @@ export namespace ProviderTransform {
438464
.filter((line) => line.trim().length > 0)
439465
.join("\n")
440466
if (!text) continue
441-
const previous = result.at(-1)
442-
// Merge into the preceding assistant turn so role alternation is preserved; providers
443-
// that reject a bare trailing assistant message never see a new one appear.
444-
if (previous && previous.role === "assistant" && Array.isArray(previous.content)) {
445-
previous.content = [...previous.content, { type: "text", text }]
446-
continue
447-
}
448-
result.push({ role: "assistant", content: [{ type: "text", text }] })
467+
appendAssistant([{ type: "text", text }])
449468
continue
450469
}
451470
if (msg.role === "assistant" && Array.isArray(msg.content)) {
452-
const content = msg.content
453-
.map((part: any) => (part?.type === "tool-call" ? { type: "text" as const, text: renderToolCall(part) } : part))
454-
.filter((part: any) => !(part?.type === "text" && typeof part.text === "string" && part.text.trim() === ""))
455-
// An assistant turn that held nothing but tool calls must not become an empty message.
456-
if (content.length === 0) continue
457-
result.push({ ...msg, content } as ModelMessage)
471+
const converted: any[] = []
472+
for (const part of msg.content as any[]) {
473+
if (part?.type === "tool-call" || part?.type === "tool-result") {
474+
const text = part.type === "tool-call" ? renderToolCall(part) : renderToolResult(part)
475+
// Only a part this function produced may be dropped for being blank.
476+
if (text.trim().length > 0) converted.push({ type: "text", text })
477+
continue
478+
}
479+
converted.push(part)
480+
}
481+
// An assistant turn that held nothing but tool parts must not become an empty message.
482+
if (converted.length === 0) continue
483+
appendAssistant(converted)
458484
continue
459485
}
460486
result.push(msg)

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,11 @@ NOTE: At any point in time through this workflow you should feel free to ask the
40624062
system: [],
40634063
small: true,
40644064
tools: {},
4065+
// altimate_change — title generation is toolless, but without an explicit "none" the
4066+
// historical-tool-stub injection in LLM.stream repopulates `tools` from any tool parts in
4067+
// the context, which both re-declares tools this request cannot use and suppresses the
4068+
// toolless message flattening. compaction.ts passes the same flag for the same reason.
4069+
toolChoice: "none" as const,
40654070
model,
40664071
abort: new AbortController().signal,
40674072
sessionID: input.session.id,

‎packages/opencode/test/provider/flatten-tool-parts.test.ts‎

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { ProviderTransform } from "@/provider/transform"
33
import type { ModelMessage } from "ai"
44

55
/**
6-
* A request that declares no tools must not carry tool-call messages: OpenAI, Azure's Responses
7-
* API and Anthropic all reject a call referencing an undeclared function, so every provider in a
8-
* fallback chain fails and the gateway can only report a generic error (issue #1315).
6+
* A request that declares no tools must not carry tool-call messages: the Altimate gateway
7+
* rejects that shape, failing every provider in its fallback chain and reporting one generic
8+
* error (issue #1315). OpenAI and Anthropic both accept it — the defect is gateway-side, and
9+
* this transform is the client-side mitigation.
910
*/
1011
describe("ProviderTransform.flattenToolParts", () => {
1112
const history = (): ModelMessage[] => [
@@ -82,4 +83,138 @@ describe("ProviderTransform.flattenToolParts", () => {
8283
]
8384
expect(ProviderTransform.flattenToolParts(plain)).toEqual(plain)
8485
})
86+
87+
// --- regressions from the consensus review of PR #1319 ---
88+
89+
test("the canonical agentic shape does not leave consecutive assistant messages", () => {
90+
// user -> assistant(text+call) -> tool(result) -> assistant(text) -> user is the ordinary
91+
// shape of every session compaction summarizes. Dropping the tool message used to leave two
92+
// adjacent assistant turns.
93+
const out = ProviderTransform.flattenToolParts([
94+
{ role: "user", content: "go" },
95+
{
96+
role: "assistant",
97+
content: [
98+
{ type: "text", text: "Checking." },
99+
{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: { cmd: "ls" } },
100+
],
101+
},
102+
{
103+
role: "tool",
104+
content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { type: "text", value: "a.txt" } }],
105+
},
106+
{ role: "assistant", content: [{ type: "text", text: "I found a.txt." }] },
107+
{ role: "user", content: "summarize" },
108+
] as unknown as ModelMessage[])
109+
110+
expect(out.map((m) => m.role)).toEqual(["user", "assistant", "user"])
111+
for (let i = 1; i < out.length; i++) expect(out[i].role === out[i - 1].role).toBe(false)
112+
// nothing is lost in the merge
113+
const text = JSON.stringify(out)
114+
expect(text).toContain("Checking.")
115+
expect(text).toContain("a.txt")
116+
expect(text).toContain("I found a.txt.")
117+
})
118+
119+
test("an intentionally blank assistant text part survives", () => {
120+
// message-v2.ts preserves a single space between Anthropic signed-reasoning blocks; the
121+
// blank-part filter must only ever drop parts this transform itself produced.
122+
const out = ProviderTransform.flattenToolParts([
123+
{ role: "user", content: "go" },
124+
{ role: "assistant", content: [{ type: "reasoning", text: "thinking" }, { type: "text", text: " " }] },
125+
{ role: "user", content: "summarize" },
126+
] as unknown as ModelMessage[])
127+
const parts = (out[1] as any).content
128+
expect(parts.some((p: any) => p.type === "text" && p.text === " ")).toBe(true)
129+
expect(parts.some((p: any) => p.type === "reasoning")).toBe(true)
130+
})
131+
132+
test("a tool-result embedded in an assistant message is flattened too", () => {
133+
const out = ProviderTransform.flattenToolParts([
134+
{ role: "user", content: "go" },
135+
{
136+
role: "assistant",
137+
content: [{ type: "tool-result", toolCallId: "c9", toolName: "bash", output: { type: "text", value: "x" } }],
138+
},
139+
{ role: "user", content: "summarize" },
140+
] as unknown as ModelMessage[])
141+
expect(JSON.stringify(out)).not.toContain('"tool-result"')
142+
expect(JSON.stringify(out)).toContain("x")
143+
})
144+
145+
test("content-array output is unwrapped into readable text, not JSON", () => {
146+
const out = ProviderTransform.flattenToolParts([
147+
{ role: "user", content: "go" },
148+
{ role: "assistant", content: [{ type: "tool-call", toolCallId: "c1", toolName: "read", input: {} }] },
149+
{
150+
role: "tool",
151+
content: [
152+
{
153+
type: "tool-result",
154+
toolCallId: "c1",
155+
toolName: "read",
156+
output: { type: "content", value: [{ type: "text", text: "hello world" }] },
157+
},
158+
],
159+
},
160+
{ role: "user", content: "s" },
161+
] as unknown as ModelMessage[])
162+
const rendered = JSON.stringify(out)
163+
expect(rendered).toContain("hello world")
164+
expect(rendered).not.toContain('\\"type\\":\\"text\\"')
165+
})
166+
167+
test("parallel tool calls and multiple results keep their order", () => {
168+
const out = ProviderTransform.flattenToolParts([
169+
{ role: "user", content: "go" },
170+
{
171+
role: "assistant",
172+
content: [
173+
{ type: "tool-call", toolCallId: "a", toolName: "first", input: {} },
174+
{ type: "tool-call", toolCallId: "b", toolName: "second", input: {} },
175+
],
176+
},
177+
{
178+
role: "tool",
179+
content: [
180+
{ type: "tool-result", toolCallId: "a", toolName: "first", output: { type: "text", value: "R1" } },
181+
{ type: "tool-result", toolCallId: "b", toolName: "second", output: { type: "text", value: "R2" } },
182+
],
183+
},
184+
{ role: "user", content: "s" },
185+
] as unknown as ModelMessage[])
186+
const flat = (out[1] as any).content.map((p: any) => p.text).join("\n")
187+
expect(flat.indexOf("first")).toBeLessThan(flat.indexOf("second"))
188+
expect(flat.indexOf("R1")).toBeLessThan(flat.indexOf("R2"))
189+
})
190+
191+
test("output shapes without a value do not throw", () => {
192+
for (const output of [
193+
{ type: "error-text", value: "boom" },
194+
{ type: "json", value: { a: 1 } },
195+
{ type: "execution-denied" },
196+
"plain string",
197+
]) {
198+
const run = () =>
199+
ProviderTransform.flattenToolParts([
200+
{ role: "user", content: "go" },
201+
{
202+
role: "tool",
203+
content: [{ type: "tool-result", toolCallId: "c1", toolName: "t", output }],
204+
},
205+
] as unknown as ModelMessage[])
206+
expect(run).not.toThrow()
207+
}
208+
})
209+
210+
test("the caller's input array is never mutated", () => {
211+
const input = [
212+
{ role: "user", content: "go" },
213+
{ role: "assistant", content: [{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: {} }] },
214+
{ role: "tool", content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { type: "text", value: "o" } }] },
215+
] as unknown as ModelMessage[]
216+
const before = JSON.stringify(input)
217+
ProviderTransform.flattenToolParts(input)
218+
expect(JSON.stringify(input)).toEqual(before)
219+
})
85220
})

0 commit comments

Comments
 (0)