Skip to content

Commit 29961e5

Browse files
sahrizviclaude
andauthored
fix(session): flatten tool history when a request declares no tools (AltimateAI#1319)
* fix(session): flatten tool history when a request declares no tools Compaction, title and summary run with `"*": "deny"`, so they send no `tools` array while summarizing a session's own history — which is full of `tool-call` and `tool-result` parts. The gateway's Responses-API namespace conversion only runs when `tools` is present, so the history references functions the request never declares, every provider in its fallback chain fails, and the client sees only: UnknownError: "Could not get a response from the agent. Please try again later." The failure therefore looks like a gateway outage while ordinary turns keep working, because ordinary turns do declare their tools. Verified against the live gateway: an otherwise identical compaction request fails with tool messages and no `tools` array, and succeeds once the same history is flattened to text. Measured directly, OpenAI and Anthropic both ACCEPT that shape, so the root defect is gateway-side and is tracked separately. Flattening is still applied to every provider rather than branching on one: a request that declares no tools has no use for structured tool parts either way, and a single code path avoids a provider-specific branch that would rot. `ProviderTransform.flattenToolParts` rewrites `tool-call` parts into `[tool call: name(args)]` and `tool-result` parts into `[tool result: name]` plus the output, merging results into the preceding assistant turn so role alternation is preserved. Toolless requests are the only ones affected; every other request keeps its structured tool parts untouched. Closes AltimateAI#1315 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01V4pzHPsqfbTdLMKzMaSYcF * fix(session): address consensus review on the toolless flattening Findings verified by execution before fixing; each was reproduced first. - Consecutive assistant turns (Major AltimateAI#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 AltimateAI#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 AltimateAI#3); they previously survived structurally intact. - The blank-text filter no longer touches parts this transform did not create (Minor AltimateAI#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 AltimateAI#8), which also shrinks the flattened payload that Minor AltimateAI#5 flagged as escaping the token estimate. - The test docstring no longer claims OpenAI and Anthropic reject the shape (Minor AltimateAI#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 AltimateAI#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 AltimateAI#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 * fix: wrap toolChoice: "none" with altimate_change markers The consensus-review fix commit added `toolChoice: "none" as const` to `ensureTitle`'s `LLM.stream` call without wrapping it in `altimate_change start`/`end` markers, tripping the Marker Guard CI check on this upstream-shared file. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WJKLg6TKfGMPntHvkXZd7Z --------- Co-authored-by: Claude Opus 5 <[email protected]>
1 parent e7c97fa commit 29961e5

4 files changed

Lines changed: 357 additions & 1 deletion

File tree

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,126 @@ export namespace ProviderTransform {
369369
}
370370

371371
// altimate_change start — expose the pure request projection used before input-budget estimation
372+
// altimate_change start — flatten tool history when a request declares no tools
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. */
383+
function renderToolCall(part: any): string {
384+
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
385+
const raw = part?.input ?? part?.args
386+
const args = typeof raw === "string" ? raw : raw === undefined ? "" : safeJson(raw)
387+
return args ? `[tool call: ${name}(${args})]` : `[tool call: ${name}]`
388+
}
389+
390+
/**
391+
* 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.
395+
*/
396+
function renderToolResult(part: any): string {
397+
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
398+
const out = part?.output ?? part?.result
399+
let body = ""
400+
if (typeof out === "string") body = out
401+
else if (out && typeof out === "object") {
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)
415+
}
416+
} else if (out !== undefined) body = String(out)
417+
return `[tool result: ${name}]${body ? `\n${body}` : ""}`
418+
}
419+
420+
/**
421+
* Rewrite `tool-call` / `tool-result` parts into text when the outgoing request declares
422+
* no tools.
423+
*
424+
* The Altimate gateway rejects a request that carries tool-call messages while declaring no
425+
* `tools`: its Responses-API namespace conversion only runs when `tools` is present, so the
426+
* history references functions the request never declares and every provider in its fallback
427+
* chain fails, surfacing one generic error. Measured directly, OpenAI and Anthropic both
428+
* ACCEPT that shape — this is a gateway-side defect, tracked separately.
429+
*
430+
* Flattening is applied to every provider rather than branching on one, because a request
431+
* that declares no tools has no use for structured tool parts either way: the toolless
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.
440+
*
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.
444+
*/
445+
export function flattenToolParts(msgs: ModelMessage[]): ModelMessage[] {
446+
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+
459+
for (const msg of msgs) {
460+
if (msg.role === "tool") {
461+
const parts = Array.isArray(msg.content) ? msg.content : []
462+
const text = parts
463+
.map((part) => renderToolResult(part))
464+
.filter((line) => line.trim().length > 0)
465+
.join("\n")
466+
if (!text) continue
467+
appendAssistant([{ type: "text", text }])
468+
continue
469+
}
470+
if (msg.role === "assistant" && Array.isArray(msg.content)) {
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)
484+
continue
485+
}
486+
result.push(msg)
487+
}
488+
return result
489+
}
490+
// altimate_change end
491+
372492
export function messagesForInputEstimate(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
373493
const projected = unsupportedParts(msgs, model)
374494
const mistral =

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ export namespace LLM {
267267
const requestOptions = clampReasoningBudget(params.options, maxOutputTokens)
268268
// altimate_change end
269269

270+
// altimate_change start — detect a toolless request once, for the message flattening below
271+
const declaresNoTools = Object.keys(tools).filter((x) => x !== "invalid").length === 0
272+
// altimate_change end
273+
270274
return streamText({
271275
onError(error) {
272276
l.error("stream error", {
@@ -322,7 +326,13 @@ export namespace LLM {
322326
content: x,
323327
}),
324328
),
325-
...input.messages,
329+
// altimate_change start — a request that declares no tools must not carry tool-call
330+
// messages. The toolless agents (compaction, title, summary) summarize a session's own
331+
// history, so they would otherwise send tool calls referencing functions the request
332+
// never declares. The Altimate gateway fails every provider in its fallback chain on
333+
// that shape and reports one generic error; see ProviderTransform.flattenToolParts.
334+
...(declaresNoTools ? ProviderTransform.flattenToolParts(input.messages) : input.messages),
335+
// altimate_change end
326336
],
327337
model: wrapLanguageModel({
328338
model: language,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,12 @@ 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 start — 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,
4070+
// altimate_change end
40654071
model,
40664072
abort: new AbortController().signal,
40674073
sessionID: input.session.id,
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { ProviderTransform } from "@/provider/transform"
3+
import type { ModelMessage } from "ai"
4+
5+
/**
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.
10+
*/
11+
describe("ProviderTransform.flattenToolParts", () => {
12+
const history = (): ModelMessage[] => [
13+
{ role: "user", content: "list the files" },
14+
{
15+
role: "assistant",
16+
content: [
17+
{ type: "text", text: "Checking." },
18+
{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: { cmd: "ls" } },
19+
],
20+
} as unknown as ModelMessage,
21+
{
22+
role: "tool",
23+
content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { value: "a.txt\nb.txt" } }],
24+
} as unknown as ModelMessage,
25+
{ role: "user", content: "Summarize the conversation above." },
26+
]
27+
28+
function toolPartTypes(msgs: ModelMessage[]) {
29+
return msgs.flatMap((m) => (Array.isArray(m.content) ? m.content.map((p: any) => p?.type) : []))
30+
}
31+
32+
test("removes every tool-call and tool-result part", () => {
33+
const out = ProviderTransform.flattenToolParts(history())
34+
expect(toolPartTypes(out)).not.toContain("tool-call")
35+
expect(toolPartTypes(out)).not.toContain("tool-result")
36+
expect(out.some((m) => m.role === "tool")).toBe(false)
37+
})
38+
39+
test("preserves the tool name, arguments and output as readable text", () => {
40+
const text = JSON.stringify(ProviderTransform.flattenToolParts(history()))
41+
expect(text).toContain("bash")
42+
expect(text).toContain("ls")
43+
expect(text).toContain("a.txt")
44+
})
45+
46+
test("keeps surrounding conversation intact", () => {
47+
const out = ProviderTransform.flattenToolParts(history())
48+
expect(out[0]).toEqual({ role: "user", content: "list the files" })
49+
expect(out.at(-1)).toEqual({ role: "user", content: "Summarize the conversation above." })
50+
})
51+
52+
test("an assistant turn holding only tool calls does not become an empty message", () => {
53+
const out = ProviderTransform.flattenToolParts([
54+
{ role: "user", content: "go" },
55+
{
56+
role: "assistant",
57+
content: [{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: {} }],
58+
} as unknown as ModelMessage,
59+
{ role: "user", content: "summarize" },
60+
])
61+
for (const msg of out) {
62+
if (Array.isArray(msg.content)) expect(msg.content.length).toBeGreaterThan(0)
63+
}
64+
})
65+
66+
test("an orphaned tool result (head truncated away its call) still flattens", () => {
67+
const out = ProviderTransform.flattenToolParts([
68+
{ role: "user", content: "go" },
69+
{
70+
role: "tool",
71+
content: [{ type: "tool-result", toolCallId: "gone", toolName: "bash", output: { value: "out" } }],
72+
} as unknown as ModelMessage,
73+
{ role: "user", content: "summarize" },
74+
])
75+
expect(out.some((m) => m.role === "tool")).toBe(false)
76+
expect(JSON.stringify(out)).toContain("out")
77+
})
78+
79+
test("messages with no tool parts are returned unchanged", () => {
80+
const plain: ModelMessage[] = [
81+
{ role: "user", content: "hi" },
82+
{ role: "assistant", content: [{ type: "text", text: "hello" }] } as unknown as ModelMessage,
83+
]
84+
expect(ProviderTransform.flattenToolParts(plain)).toEqual(plain)
85+
})
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+
})
220+
})

0 commit comments

Comments
 (0)