Skip to content

Commit 1ca2491

Browse files
sahrizviclaude
andcommitted
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 #1315 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01V4pzHPsqfbTdLMKzMaSYcF
1 parent 024e800 commit 1ca2491

3 files changed

Lines changed: 190 additions & 1 deletion

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,100 @@ 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+
/**
374+
* Render a tool call as the plain text a summarizer can still read.
375+
*/
376+
function renderToolCall(part: any): string {
377+
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
378+
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+
}
388+
return args ? `[tool call: ${name}(${args})]` : `[tool call: ${name}]`
389+
}
390+
391+
/**
392+
* Render a tool result as plain text, tolerating every output shape the SDK emits.
393+
*/
394+
function renderToolResult(part: any): string {
395+
const name = typeof part?.toolName === "string" ? part.toolName : "tool"
396+
const out = part?.output ?? part?.result
397+
let body = ""
398+
if (typeof out === "string") body = out
399+
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+
}
408+
}
409+
} else if (out !== undefined) body = String(out)
410+
return `[tool result: ${name}]${body ? `\n${body}` : ""}`
411+
}
412+
413+
/**
414+
* Rewrite `tool-call` / `tool-result` parts into text when the outgoing request declares
415+
* no tools.
416+
*
417+
* The Altimate gateway rejects a request that carries tool-call messages while declaring no
418+
* `tools`: its Responses-API namespace conversion only runs when `tools` is present, so the
419+
* history references functions the request never declares and every provider in its fallback
420+
* chain fails, surfacing one generic error. Measured directly, OpenAI and Anthropic both
421+
* ACCEPT that shape — this is a gateway-side defect, tracked separately.
422+
*
423+
* Flattening is applied to every provider rather than branching on one, because a request
424+
* 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.
427+
*
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.
430+
*/
431+
export function flattenToolParts(msgs: ModelMessage[]): ModelMessage[] {
432+
const result: ModelMessage[] = []
433+
for (const msg of msgs) {
434+
if (msg.role === "tool") {
435+
const parts = Array.isArray(msg.content) ? msg.content : []
436+
const text = parts
437+
.map((part) => renderToolResult(part))
438+
.filter((line) => line.trim().length > 0)
439+
.join("\n")
440+
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 }] })
449+
continue
450+
}
451+
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)
458+
continue
459+
}
460+
result.push(msg)
461+
}
462+
return result
463+
}
464+
// altimate_change end
465+
372466
export function messagesForInputEstimate(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
373467
const projected = unsupportedParts(msgs, model)
374468
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,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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: 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).
9+
*/
10+
describe("ProviderTransform.flattenToolParts", () => {
11+
const history = (): ModelMessage[] => [
12+
{ role: "user", content: "list the files" },
13+
{
14+
role: "assistant",
15+
content: [
16+
{ type: "text", text: "Checking." },
17+
{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: { cmd: "ls" } },
18+
],
19+
} as unknown as ModelMessage,
20+
{
21+
role: "tool",
22+
content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { value: "a.txt\nb.txt" } }],
23+
} as unknown as ModelMessage,
24+
{ role: "user", content: "Summarize the conversation above." },
25+
]
26+
27+
function toolPartTypes(msgs: ModelMessage[]) {
28+
return msgs.flatMap((m) => (Array.isArray(m.content) ? m.content.map((p: any) => p?.type) : []))
29+
}
30+
31+
test("removes every tool-call and tool-result part", () => {
32+
const out = ProviderTransform.flattenToolParts(history())
33+
expect(toolPartTypes(out)).not.toContain("tool-call")
34+
expect(toolPartTypes(out)).not.toContain("tool-result")
35+
expect(out.some((m) => m.role === "tool")).toBe(false)
36+
})
37+
38+
test("preserves the tool name, arguments and output as readable text", () => {
39+
const text = JSON.stringify(ProviderTransform.flattenToolParts(history()))
40+
expect(text).toContain("bash")
41+
expect(text).toContain("ls")
42+
expect(text).toContain("a.txt")
43+
})
44+
45+
test("keeps surrounding conversation intact", () => {
46+
const out = ProviderTransform.flattenToolParts(history())
47+
expect(out[0]).toEqual({ role: "user", content: "list the files" })
48+
expect(out.at(-1)).toEqual({ role: "user", content: "Summarize the conversation above." })
49+
})
50+
51+
test("an assistant turn holding only tool calls does not become an empty message", () => {
52+
const out = ProviderTransform.flattenToolParts([
53+
{ role: "user", content: "go" },
54+
{
55+
role: "assistant",
56+
content: [{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: {} }],
57+
} as unknown as ModelMessage,
58+
{ role: "user", content: "summarize" },
59+
])
60+
for (const msg of out) {
61+
if (Array.isArray(msg.content)) expect(msg.content.length).toBeGreaterThan(0)
62+
}
63+
})
64+
65+
test("an orphaned tool result (head truncated away its call) still flattens", () => {
66+
const out = ProviderTransform.flattenToolParts([
67+
{ role: "user", content: "go" },
68+
{
69+
role: "tool",
70+
content: [{ type: "tool-result", toolCallId: "gone", toolName: "bash", output: { value: "out" } }],
71+
} as unknown as ModelMessage,
72+
{ role: "user", content: "summarize" },
73+
])
74+
expect(out.some((m) => m.role === "tool")).toBe(false)
75+
expect(JSON.stringify(out)).toContain("out")
76+
})
77+
78+
test("messages with no tool parts are returned unchanged", () => {
79+
const plain: ModelMessage[] = [
80+
{ role: "user", content: "hi" },
81+
{ role: "assistant", content: [{ type: "text", text: "hello" }] } as unknown as ModelMessage,
82+
]
83+
expect(ProviderTransform.flattenToolParts(plain)).toEqual(plain)
84+
})
85+
})

0 commit comments

Comments
 (0)