Most desktop wrappers around AI coding agents start the same way: every time the user hits Return, spawn claude -p or agy -p as a subprocess, scrape stdout lines with regular expressions, and kill the PID if the user clicks Stop.

It works for simple one-shot questions, then breaks everywhere that matters in real engineering work: clicking Stop tears down the entire agent state; switching models or approval modes requires restarting or global settings; tool-approval prompts only work on one CLI; and if two agents emit slightly different text, UI elements like live subagent lists can get stuck spinning forever.

Inside Vitruvian Code—our native Swift macOS app and notch coding assistant—we replaced per-prompt CLI spawning with a persistent connection per open chat, speaking the open Agent Client Protocol (ACP) alongside the Claude Agent SDK, plus live mid-chat handoffs when switching agents mid-conversation.

Geometric polyhedral nodes connected inside a dark architectural Vitruvian ring diagram Figure 1: Vitruvian Code routes one native Swift chat surface over persistent ACP and Agent SDK sessions, letting new registry agents plug in with zero per-agent parser code.

Much of this design draws directly from T3 Code, whose open-source TypeScript architecture (AntigravityAcpSupport, ClaudeAdapterV2, and provider-acp-registry) served as our concrete reference model when porting these patterns to native Swift.


1. Per-Prompt CLI Subprocesses vs. Persistent Connections

Comparing the old and new execution models explains why a live bidirectional connection changes the feel of a coding UI immediately:

Before and after comparison of per-prompt CLI subprocesses vs live persistent AgentConnection sessions Figure 2: Disposable claude -p / agy -p runs forced stdout scraping and hard SIGTERM stops; persistent ACP and Agent SDK connections stream typed events and support mid-turn approvals, elicitations, and non-destructive interrupts.

When the connection stays alive across turns:

  • Interrupting does not destroy the session: Clicking Stop sends session/cancel over ACP (or interrupt() to the Claude Agent SDK) while keeping conversation memory warm.
  • Interactive mid-turn approvals and forms work uniformly: Both session/request_permission / elicitation/create in ACP and canUseTool / AskUserQuestion in the Claude SDK drive the exact same native Swift approval card (VitruvianCodeApprovalCardView).
  • Per-chat controls reflect real capabilities: During handshake (initialize), agents advertise their supported models, effort/thinking levels, slash commands (available_commands_update), and plan mode—and the UI automatically hides any toggle an agent does not support.

2. One Swift AgentConnection Contract, Plus the Public ACP Registry

Every chat inside Vitruvian Code—whether in the quick notch overlay, a popped-out workspace window, a git worktree runner, or a scheduled task—reads a single stream of typed AgentEvent values from Swift’s AgentConnection protocol:

Architecture diagram of Swift AgentConnection connecting UI to Swift ACPClient, ACP Registry installer, Claude SDK helper, and CLI fallback Figure 3: VitruvianCodeCore shields the UI behind AgentConnection, dispatching agy acp and installed ACP Registry agents through one shared Swift ACPClient, Claude Code through the Agent SDK helper by default, and CLI mode as an explicit per-chat fallback.

That single boundary gives us four paths without multiplying UI code:

  1. Native Swift ACPClient (agy & any ACP agent): Speaks line-delimited JSON-RPC 2.0 over stdio (initialize, session/new, session/resume, session/load, session/prompt, session/cancel).
  2. Zero-parser ACP Registry support: ACPRegistry fetches cdn.agentclientprotocol.com/registry/v1/latest/registry.json, letting users install, update, and run external registry agents (such as OpenCode, Cursor, Kiro, Pi, or @agentclientprotocol/claude-agent-acp) directly through the same ACPClient path.
  3. Bundled Node Claude Agent SDK helper (default for Claude): Wraps @anthropic-ai/claude-agent-sdk using the developer’s existing claude auth login credentials—no separate API key required unless desired.
  4. Explicit failure recovery & fallback: External terminal sessions still use the existing transcript readers cleanly, and if a live connection fails to initialize, the chat explicitly offers Retry or Use command line for this chat instead of silently masking failures.

3. Switching Agents Mid-Chat Without Wiping Context

Supporting multiple agents in one app creates an obvious workflow question: what happens when you start debugging an architectural bug in Claude, hit a wall or want a second opinion, and switch the active chat over to agy (or a registry agent) on turn 6?

Throwing away the thread forces the human to summarize everything by hand. Replaying all 50,000 tokens of history wastes money and blows past smaller context windows.

Instead, AgentContextHandoff tracks a per-target cursor recording the exact conversation row count up to which each agent has already answered:

State diagram of AgentContextHandoff showing full_thread_summary vs delta_since_target_last_seen under a 16k token / 64 KB cap Figure 4: Switching to an agent that has never seen the chat packs a bounded full_thread_summary (up to 16,000 tokens / 64 KB); switching back to a previously active agent sends only delta_since_target_last_seen.

Two reliability details proved critical in tests and code review:

  • Anchor the goal + fill newest-first (full_thread_summary): Within a 16,000-token / 64 KB cap (at most 12.5% of a 128,000-token context window), selectHistory() always retains the first user prompt (so the overall goal survives), the latest user turn, and the latest assistant answer, then fills remaining headroom newest-first while noting how many older messages were omitted.
  • Handoff debt stays owed until an answer lands: If you switch from Claude to agy, send a prompt, and agy errors out or you cancel before it responds, AgentContextHandoff does not mark the handoff delivered. The next retry still carries the missing history packet, and row-count extraction (userText(fromPrompt:)) ensures pasted [Historical ...] text inside a user prompt can never spoof the boundary when rendering chat history.

Try Vitruvian Code

All of VitruvianCodeCore—the Swift ACP client, the ACP registry installer, the Claude Agent SDK bridge, and the mid-chat handoff tests—lives in apps/desktop/vitruvian-code in our open-source monorepo. You can explore the architecture spec in vitruvian-code-acp.md or grab a macOS build from the Downloads page.