Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): a turn with every call refused sends no empty tool_calls list

purple33f707453ffe
diff --git a/src/hooks/__tests__/an-all-refused-turn-sends-no-empty-call-list.test.ts b/src/hooks/__tests__/an-all-refused-turn-sends-no-empty-call-list.test.ts
new file mode 100644
index 00000000..4ef12b8c
--- /dev/null
+++ b/src/hooks/__tests__/an-all-refused-turn-sends-no-empty-call-list.test.ts
@@ -0,0 +1,24 @@
+/**
+ * Bug hunt 01.10.2026 (A9). A read-only turn (/review, plan) or a turn whose
+ * tools are all switched off can lose every call to the filters. The turn
+ * then went on with an empty batch and wrote `tool_calls: []` into the
+ * history, which a strict upstream rejects, behind the refusal it answers.
+ */
+import { it, expect } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { resolve } from 'node:path'
+
+const agent = readFileSync(resolve(__dirname, '..', 'useAgentChat.ts'), 'utf8')
+
+it('a turn with every call refused goes to the next round before any batch is built', () => {
+ const mark = agent.indexOf('const refusalsFrom = agentMessages.length')
+ const readOnly = agent.indexOf('if (opts?.readOnly) {', mark)
+ const skip = agent.indexOf('if (toolCalls.length === 0 && agentMessages.length > refusalsFrom) {')
+ expect(mark).toBeGreaterThan(0)
+ expect(readOnly).toBeGreaterThan(mark)
+ expect(skip).toBeGreaterThan(agent.indexOf('is switched off for this conversation in the tool permissions'))
+ expect(skip).toBeLessThan(agent.indexOf('type BatchEntry = {'))
+ const block = agent.slice(skip, agent.indexOf('continue', skip) + 8)
+ // The model's words sit before the refusal, never after it.
+ expect(block).toContain("agentMessages.splice(refusalsFrom, 0, { role: 'assistant', content: turnContent })")
+})
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index df1d6346..0129abdc 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -2285,6 +2285,7 @@ export function useAgentChat() {
// this turn was allowed to offer it. Proven live on the Code side
// 2026-07-25, where a read-only /plan created a file while every request
// carried a read-only catalog.
+ const refusalsFrom = agentMessages.length
if (opts?.readOnly) {
const blocked = toolCalls.filter((tc) => !allowedInReadOnlyTurn(tc.function?.name ?? ''))
if (blocked.length) {
@@ -2327,6 +2328,18 @@ export function useAgentChat() {
}
}
+ // Every call of this turn was refused (bug hunt 01.10.2026, A9). The
+ // turn went on with an empty batch and wrote `tool_calls: []` into the
+ // history, which a strict upstream rejects, behind the refusal it
+ // answers. The model's own words go in first, then the refusal, and
+ // the next round answers it.
+ if (toolCalls.length === 0 && agentMessages.length > refusalsFrom) {
+ if (turnContent.trim()) {
+ agentMessages.splice(refusalsFrom, 0, { role: 'assistant', content: turnContent })
+ }
+ continue
+ }
+
// Loop-detector, parity with Codex: narration first (the same line
// re-emitted every iteration), then the batch (windowed signature
// repeats + identical reads against an unchanged workspace). Steer is