Skip to content

fix(desktop): agent-pill router strips markdown fences from Haiku response - #7251

Merged
kodjima33 merged 1 commit into
mainfrom
worktree-fix-router-strip-fences
May 12, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
worktree-fix-router-strip-fences

Conversation

@kodjima33

Copy link
Copy Markdown
Collaborator

What

Live log from a user session:

AgentPill: router JSON parse failed — raw: ```json
{"route":"agent","title":"Build Snake Game Project","ack":"Starting your snake game builder now"}

Haiku classified the request as agent correctly, but wrapped the JSON in json … markdown fences. The Swift parser passes the whole response to JSONSerialization which then fails because the bytes start with backticks. Falls back to chat → agent never spawns.

Fix

Extract the first balanced {…} from the response before JSON parsing. Robust to markdown fences, leading prose, trailing prose. No prompt change needed.

This is the second of two bugs blocking natural-language agent-pill spawning. The first (server-side, model not allowlisted) was just fixed in #7249.

🤖 Generated with Claude Code

…n fences

Live log proof from a real user session:
  AgentPill: router JSON parse failed — raw: ```json
  {"route":"agent","title":"Build Snake Game Project",...}

Haiku's "Reply with ONLY a single-line JSON object" instruction is
non-deterministic; sometimes the model wraps the JSON in ```json …
``` markdown fences. The parser passed the whole response straight to
`JSONSerialization.jsonObject` which then failed because the bytes
start with backticks, not `{`. Falls back to chat, agent never spawns
even when Haiku correctly classified it as agent.

Robust fix: extract the first balanced `{…}` from the response before
JSON parsing. Resilient to markdown fences, leading prose, trailing
prose. No prompt change needed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@kodjima33
kodjima33 merged commit 264c2f5 into main May 12, 2026
@kodjima33
kodjima33 deleted the worktree-fix-router-strip-fences branch May 12, 2026 19:56
@greptile-apps

greptile-apps Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a JSON parse failure in the AgentPill router that occurred when Claude Haiku wrapped its routing decision in markdown code fences, causing the Swift parser to receive raw backtick bytes instead of JSON and silently fall back to chat mode.

  • runRouterCall: Extracts the first balanced {…} block from the LLM response before passing it to JSONSerialization, making the router resilient to markdown fences and leading prose.
  • generateTitleAndAck (line 587): Has the same direct trimmed → JSONSerialization pattern and is NOT patched by this PR — if Haiku fences its title/ack response the same way, the title gen call will still silently fail.
  • CHANGELOG.json: Adds an unreleased entry describing the fix.

Confidence Score: 3/5

The router fix is correct and unblocks agent spawning, but the same markdown-fence failure mode survives in generateTitleAndAck and will silently degrade pill titles and voice acknowledgements.

The runRouterCall change is a clean, targeted fix for the described bug. However, generateTitleAndAck makes the same Haiku call with the same no-markdown instruction, parses the result the same way, and was not updated — leaving the title/ack generation broken under the same conditions.

The generateTitleAndAck function in AgentPill.swift (around line 587) needs the same brace-extraction applied before its JSONSerialization call.

Important Files Changed

Filename Overview
desktop/Desktop/Sources/FloatingControlBar/AgentPill.swift Applies the brace-extraction fix to runRouterCall to handle markdown-fenced Haiku responses, but the identical parsing pattern in generateTitleAndAck (line 587) is left unpatched and will fail the same way.
desktop/CHANGELOG.json Adds a one-line unreleased changelog entry describing the markdown-fence fix for agent pill spawning.

Sequence Diagram

sequenceDiagram
    participant User
    participant AgentPillsManager
    participant Haiku as Claude Haiku (via proxy)

    User->>AgentPillsManager: classify(query)
    AgentPillsManager->>Haiku: POST /v2/chat/completions (router prompt)
    Haiku-->>AgentPillsManager: response (may be wrapped in fences)
    Note over AgentPillsManager: NEW: extract first brace block before JSON parse
    AgentPillsManager->>AgentPillsManager: parse route / title / ack
    AgentPillsManager-->>User: RouterDecision(route: .agent)

    AgentPillsManager->>Haiku: POST /v2/chat/completions (title/ack prompt)
    Haiku-->>AgentPillsManager: response (may be wrapped in fences)
    Note over AgentPillsManager: NOT FIXED: still uses raw trimmed string
    AgentPillsManager->>AgentPillsManager: JSON parse fails silently
    AgentPillsManager-->>User: heuristic title, no ack
Loading

Comments Outside Diff (1)

  1. desktop/Desktop/Sources/FloatingControlBar/AgentPill.swift, line 587-594 (link)

    P1 Same markdown-fence vulnerability in generateTitleAndAck

    The {…} extraction fix was applied to runRouterCall but not to generateTitleAndAck, which hits the same model (claude-haiku-4-5-20251001) with a nearly identical "no markdown" instruction and parses the result with bare trimmed. If Haiku wraps its title/ack reply in ```json ``` fences (which is the exact failure mode this PR fixes), JSONSerialization will fail here too, and the pill will silently fall back to the heuristic title with no ack text — meaning the router fix lands but the title call stays broken under the same condition.

Reviews (1): Last reviewed commit: "fix(desktop): agent-pill router parses H..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant