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): repair tool names that arrive with harmony control tokens

purpled03779b3b471
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 65572739..ba60e6e9 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -37,6 +37,7 @@ import { budgetFromSettings } from '../api/agents/budget'
import { finalStripThinkingTags } from '../lib/thinking-stripper'
import { streamOllamaChatWithTools } from '../lib/ollama-stream-tools'
import { extractToolCallsWithRanges, stripRanges } from '../lib/tool-call-repair'
+import { canonicalToolName } from '../lib/loose-tool-parse'
import { selectRelevantTools, selectRelevantToolsAsync } from '../lib/tool-selection'
import { generateEmbeddings } from '../api/rag'
import { truncateToolResult } from '../lib/truncate-tool-result'
@@ -1005,6 +1006,25 @@ export function useCodex() {
})
}
+ // Repair near-miss tool names before anything tries to resolve them.
+ // The chat agent has done this since 2026-06-03 (gemma4 calling
+ // `video_generation`); Code never did, so the same class of miss ended
+ // as "Unknown tool" here. Live on the ship exe 2026-07-24, gpt-oss on
+ // LU Cloud sent `file_edit<|channel|>commentary` — a harmony control
+ // token welded onto the recipient — and every write call died while the
+ // model retried the identical name for a minute before falling back to
+ // a full-file rewrite that failed the same way. canonicalToolName only
+ // rewrites a name when the repaired form is a REGISTERED tool, so a
+ // genuinely unknown tool still errors instead of being rerouted.
+ if (toolCalls.length > 0) {
+ const knownToolNames = toolRegistry.getAll().map((t) => t.name)
+ toolCalls = toolCalls.map((tc) => {
+ const raw = tc.function?.name ?? ''
+ const fixed = canonicalToolName(raw, knownToolNames)
+ return fixed === raw ? tc : { ...tc, function: { ...tc.function, name: fixed } }
+ })
+ }
+
// No tool calls in this turn. For a strong model that means "task
// done". For a small local model it's usually a PREMATURE stop — it
// narrated the next step or asked for info instead of acting. Nudge it
diff --git a/src/lib/__tests__/loose-tool-parse.test.ts b/src/lib/__tests__/loose-tool-parse.test.ts
index b66c5d02..d9369b42 100644
--- a/src/lib/__tests__/loose-tool-parse.test.ts
+++ b/src/lib/__tests__/loose-tool-parse.test.ts
@@ -115,6 +115,44 @@ describe('canonicalToolName — map near-miss tool names', () => {
expect(canonicalToolName('teleport', KN)).toBe('teleport')
})
+ // Live on the ship exe, 2026-07-24: gpt-oss through LU Cloud sent the
+ // recipient as `file_edit<|channel|>commentary`. Every write tool call came
+ // back "Unknown tool" and the model spent a minute retrying the same name.
+ describe('harmony control tokens welded onto the name', () => {
+ const CODE = ['file_read', 'file_write', 'file_edit', 'shell_execute']
+
+ it('recovers the exact name that failed live', () => {
+ expect(canonicalToolName('file_edit<|channel|>commentary', CODE)).toBe('file_edit')
+ })
+
+ it('recovers other tools carrying the same marker', () => {
+ expect(canonicalToolName('file_write<|channel|>commentary', CODE)).toBe('file_write')
+ expect(canonicalToolName('shell_execute<|channel|>analysis', CODE)).toBe('shell_execute')
+ })
+
+ it('strips the harmony recipient namespace', () => {
+ expect(canonicalToolName('functions.file_edit', CODE)).toBe('file_edit')
+ expect(canonicalToolName('functions.file_edit<|channel|>commentary', CODE)).toBe('file_edit')
+ })
+
+ it('tolerates surrounding whitespace and a trailing call arrow', () => {
+ expect(canonicalToolName(' file_edit ', CODE)).toBe('file_edit')
+ expect(canonicalToolName('file_edit<|constrain|>json', CODE)).toBe('file_edit')
+ })
+
+ // The cut can only SHORTEN a name, so it can never turn one registered
+ // tool into a different one, and an unknown stem still errors.
+ it('does not invent a tool out of noise', () => {
+ expect(canonicalToolName('<|channel|>commentary', CODE)).toBe('<|channel|>commentary')
+ expect(canonicalToolName('teleport<|channel|>commentary', CODE)).toBe('teleport<|channel|>commentary')
+ })
+
+ it('keeps a dotted name intact when the full name is the registered one', () => {
+ const mcp = ['mcp.server.do_thing']
+ expect(canonicalToolName('mcp.server.do_thing', mcp)).toBe('mcp.server.do_thing')
+ })
+ })
+
it('never maps an alias to a tool that is not registered', () => {
expect(canonicalToolName('video_generation', ['image_generate'])).toBe('video_generation')
})
diff --git a/src/lib/loose-tool-parse.ts b/src/lib/loose-tool-parse.ts
index d77996e9..431a9fb8 100644
--- a/src/lib/loose-tool-parse.ts
+++ b/src/lib/loose-tool-parse.ts
@@ -208,14 +208,9 @@ const TOOL_NAME_ALIASES: Record<string, string> = {
list_files: 'file_list', search_files: 'file_search', run_shell: 'shell_execute', run_code: 'code_execute',
}
-/**
- * Map a model-emitted tool name to a registered one. Tries exact, lowercase,
- * an explicit alias table, then a punctuation-insensitive equality. Returns the
- * original name unchanged when no confident match exists (so genuinely unknown
- * tools still error rather than being silently rerouted).
- */
-export function canonicalToolName(name: string, known: string[]): string {
- if (!name) return name
+/** Exact → lowercase → alias → punctuation-insensitive. Undefined if none hit. */
+function matchKnown(name: string, known: string[]): string | undefined {
+ if (!name) return undefined
if (known.includes(name)) return name
const lc = name.toLowerCase()
if (known.includes(lc)) return lc
@@ -225,6 +220,48 @@ export function canonicalToolName(name: string, known: string[]): string {
const norm = (s: string) => s.toLowerCase().replace(/[^a-z0-9]/g, '')
const target = norm(name)
for (const k of known) if (norm(k) === target) return k
+ return undefined
+}
+
+/**
+ * Transport noise a model (or a proxy that half-parses its format) can wrap
+ * around the bare tool name. Two shapes seen live on gpt-oss via LU Cloud,
+ * 2026-07-24:
+ *
+ * file_edit<|channel|>commentary harmony control token leaked into the
+ * recipient field, so EVERY write tool call
+ * came back "Unknown tool" and the model
+ * burned a minute retrying the same name
+ * functions.file_edit the harmony recipient namespace, verbatim
+ *
+ * Cutting at the first character a registered name can never contain is safe:
+ * builtin names are [a-z_], so the cut can shorten a name but can never turn
+ * one tool into a different one. The namespace strip is only ever ACCEPTED by
+ * the caller when the tail matches a registered tool, which keeps MCP tools
+ * that legitimately carry dots intact.
+ */
+function toolNameCandidates(name: string): string[] {
+ const cut = name.trim().match(/^[A-Za-z0-9_.-]+/)?.[0] ?? ''
+ if (!cut) return []
+ const dot = cut.lastIndexOf('.')
+ return dot > 0 ? [cut, cut.slice(dot + 1)] : [cut]
+}
+
+/**
+ * Map a model-emitted tool name to a registered one. Tries the name as sent,
+ * then the same ladder on the name with transport noise stripped. Returns the
+ * original name unchanged when no confident match exists (so genuinely unknown
+ * tools still error rather than being silently rerouted).
+ */
+export function canonicalToolName(name: string, known: string[]): string {
+ if (!name) return name
+ const direct = matchKnown(name, known)
+ if (direct) return direct
+ for (const candidate of toolNameCandidates(name)) {
+ if (candidate === name) continue
+ const hit = matchKnown(candidate, known)
+ if (hit) return hit
+ }
return name
}