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(code): the prompt-transport branch reads tool calls written as a json block

purple1eac1a813036
diff --git a/src/hooks/codex/__tests__/hermes-zweig-liest-json-aufrufe.test.ts b/src/hooks/codex/__tests__/hermes-zweig-liest-json-aufrufe.test.ts
new file mode 100644
index 00000000..0204a0f7
--- /dev/null
+++ b/src/hooks/codex/__tests__/hermes-zweig-liest-json-aufrufe.test.ts
@@ -0,0 +1,34 @@
+// T1 Nacht-Durchtest 3.0.9 (Mac, Code, Bypass, Ollama, Qwen2.5-Coder 1.5B unter
+// eigenem Namen -> Prompt-Transport): das Modell schrieb seine zwei Aufrufe als
+// ```json-Block in den Text. Der native und der Streaming-Zweig holen solche
+// Aufrufe heraus, der Hermes-Zweig nicht: der Lauf endete mit "break-no-toolcalls"
+// und rohem JSON in der Blase, app.py wurde nie geschrieben.
+import { readFileSync } from 'node:fs'
+import { resolve } from 'node:path'
+import { describe, expect, it } from 'vitest'
+import { recoverKnownToolCallsFromContent } from '../tool-call-recovery'
+
+const SEEN = '```json\n{\n "name": "file_write",\n "arguments": {\n "path": "app.py",\n "content": "print(\'hello from t1\')\\n"\n }\n}\n{\n "name": "shell_execute",\n "arguments": {\n "command": "python3 app.py",\n "timeout": 120000,\n "shell": "bash"\n }\n}\n```'
+const known = (names: string[]) => (name: string) => names.includes(name)
+
+describe('a tool call written as a json block, on the prompt transport', () => {
+ it('becomes two calls and leaves no raw JSON behind', () => {
+ const r = recoverKnownToolCallsFromContent([], SEEN, known(['file_write', 'shell_execute']))
+ expect(r.toolCalls.map((c) => c.function.name)).toEqual(['file_write', 'shell_execute'])
+ expect(r.content).not.toContain('"name"')
+ })
+
+ it('is not run when one name is not a tool of this agent', () => {
+ const r = recoverKnownToolCallsFromContent([], SEEN, known(['file_write']))
+ expect(r.toolCalls).toEqual([])
+ expect(r.content).toBe(SEEN)
+ })
+
+ it('the Hermes branch of useCodex asks for it when there are no <tool_call> tags', () => {
+ const src = readFileSync(resolve(__dirname, '../../useCodex.ts'), 'utf8')
+ const from = src.indexOf('const raw = hermesTurn.content')
+ const hermes = src.slice(from, src.indexOf('overflowRetries = 0', from))
+ expect(from).toBeGreaterThan(0)
+ expect(hermes).toContain('recoverKnownToolCallsFromContent(')
+ })
+})
diff --git a/src/hooks/codex/tool-call-recovery.ts b/src/hooks/codex/tool-call-recovery.ts
index 43dd0ba6..aef6dcb6 100644
--- a/src/hooks/codex/tool-call-recovery.ts
+++ b/src/hooks/codex/tool-call-recovery.ts
@@ -87,3 +87,27 @@ export function recoverToolCallsFromContent(toolCalls: ToolCall[], content: stri
if (extractedFromContent && !/[A-Za-z0-9]/.test(turnContent)) turnContent = ''
return { toolCalls: calls, content: turnContent, extractedFromContent }
}
+
+/**
+ * Dasselbe, aber ein Aufruf AUS DEM TEXT zaehlt nur, wenn jeder Name darin ein
+ * Werkzeug dieses Agenten ist (GH #147): Beispiel-JSON in einer Erklaerung
+ * wird so nie ausgefuehrt. Native Aufrufe bleiben, wie sie sind.
+ *
+ * Der Streaming-Zweig von useCodex hatte diese Regel inline, der Hermes-Zweig
+ * gar keine: ein Modell auf dem Prompt-Transport, das seine Aufrufe als
+ * ```json-Block schrieb, beendete den Lauf mit rohem JSON (T1-Nachttest 3.0.9).
+ */
+export function recoverKnownToolCallsFromContent(
+ toolCalls: ToolCall[],
+ content: string,
+ isKnown: (name: string, args: Record<string, unknown>) => boolean,
+): RecoveredTurn {
+ const recovered = recoverToolCallsFromContent(toolCalls, content)
+ const fromText = toolCalls.length === 0 && recovered.toolCalls.length > 0
+ if (!fromText) return recovered
+ const allKnown = recovered.toolCalls.every((tc) => {
+ const args = tc.function.arguments
+ return isKnown(tc.function.name, args && typeof args === 'object' && !Array.isArray(args) ? args as Record<string, unknown> : {})
+ })
+ return allKnown ? recovered : { toolCalls, content, extractedFromContent: false }
+}
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 98858190..f4f00336 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -115,7 +115,7 @@ import { createLivePaint } from './codex/live-paint'
import { seedEstimatedUsage, reportTurnUsage } from './codex/turn-usage'
import { lastRequestUsageNotice, requestUsageTracker } from '../lib/request-usage'
import { shouldDowngradeThinking, engineDeniedThinking } from './codex/thinking-downgrade'
-import { recoverToolCallsFromContent } from './codex/tool-call-recovery'
+import { recoverToolCallsFromContent, recoverKnownToolCallsFromContent } from './codex/tool-call-recovery'
import { codexStallVerdict } from './codex/stall-verdict'
import { createStagedWriter } from './codex/staged-writes'
import { codexCutoffNote, dropCutOffCall } from './codex/turn-cutoff'
@@ -125,6 +125,14 @@ import { withHouseConduct } from '../lib/system-prompt'
import { yieldSilentRunsTo } from '../lib/silent-runs-yield'
import { gatePlanTool, planToolAllowed } from '../lib/plan-gate'
+/** A name the model wrote in its text is a call only when it is one of the
+ * agent's tools, after the usual name repair (GH #147). */
+function isCodexTool(name: string, args: Record<string, unknown>): boolean {
+ const tools = toolRegistry.getAll()
+ const repaired = repairToolCall(name, args, tools).name
+ return tools.some((t) => t.name === repaired)
+}
+
// Why a run ended, or was pushed on, belongs in the support log. This used to
// be a no-op, and a run that never finished its task left nothing to read on
// the user's machine: which guard ended each run was unknowable. The run-shaping tags go out as warn,
@@ -1742,16 +1750,9 @@ export function useCodex() {
// only when every name in it is a tool this agent has, so example
// JSON in an explanation is never run.
{
- const recovered = recoverToolCallsFromContent(toolCalls, turnContent)
- const tools = toolRegistry.getAll()
- const known = new Set(tools.map((t) => t.name))
- const fromText = toolCalls.length === 0 && recovered.toolCalls.length > 0
- const allKnown = recovered.toolCalls.every((tc) =>
- known.has(repairToolCall(tc.function.name, isRecord(tc.function.arguments) ? tc.function.arguments : {}, tools).name))
- if (!fromText || allKnown) {
- toolCalls = recovered.toolCalls
- turnContent = recovered.content
- }
+ const recovered = recoverKnownToolCallsFromContent(toolCalls, turnContent, isCodexTool)
+ toolCalls = recovered.toolCalls
+ turnContent = recovered.content
}
if (keepThinking && turn.thinking) {
thinkingContent += (thinkingContent ? '\n\n' : '') + turn.thinking
@@ -1895,7 +1896,12 @@ export function useCodex() {
}))
turnContent = stripToolCallTags(raw)
} else {
- turnContent = raw
+ // Kein <tool_call>-Tag, aber vielleicht ein ```json-Aufruf: dieselbe
+ // Wiederherstellung wie in den beiden Zweigen darueber, nur fuer
+ // bekannte Werkzeuge (T1-Nachttest 3.0.9).
+ const recovered = recoverKnownToolCallsFromContent([], raw, isCodexTool)
+ toolCalls = recovered.toolCalls
+ turnContent = recovered.content
}
}
overflowRetries = 0