Spawn hermes acp -p museion over NDJSON JSON-RPC stdio via a hand-rolled thin client (no Swift SDK exists). The mentor debug pane streams replies, renders fix-card tool calls as structured cards (ADR-0006), cancels via session/cancel, tears down on quit (willTerminate), and surfaces a restart prompt on engine crash — never data loss. Version verify against state.json is advisory. Fake ACP server test double (zsh speaking real NDJSON) covers handshake, streaming + fix-card, drift, crash latch, cancel, teardown. 36/36 tests green; finish-audit fixes included (wired binaryPath, focus-loss teardown bug, double onCrash latch). Co-Authored-By: Claude <noreply@anthropic.com>
7.4 KiB
ACP bridge spike — M0 (#14)
Date: 2026-09-16 Type: issue Reference: #14
Goal
Implement the M0 ACP bridge spike per the #14 wayfinder map's settled decisions: the app spawns the Engine (hermes acp -p museion) as a child process through a hand-rolled thin Swift ACP client (NDJSON JSON-RPC on stdio), opens a Session, sends prompts, streams replies live in a debug pane, round-trips a fix-card-shaped tool call as structured data, tears down on quit, and surfaces a restart prompt on child crash — never data loss.
What Was Done
Museion/ACPEngineClient.swift—ACPEngineClient(nonisolated, @unchecked Sendable) +JSONValue(decoded-wire value type, Sendable all the way down). Serial-queue-confined state; NDJSON framing; response matching by id; requests time out (15 s default) — a hung engine surfaces as an error → restart prompt, never an eternal await;onCrashon unexpected exit/EOF (never on explicit close); auto-32601reply to server→client requests (M0 handles none — the deny option); SIGPIPE ignored;close()= handlers-off → SIGTERM → 1 s grace → SIGKILL → reap.Museion/MentorSessionModel.swift— pane model: lazy spawn,initializehandshake with advisory version compare vs state.json (recordedVersionProviderinjectable),session/newcwd~, prompt streaming (agent_message_chunk/agent_thought_chunkbuffered, flushed on turn end), fix-card parse from tool-callrawInput(ADR-0006 fields, pattern+problem minimum), raw-JSON rows for everything else,session/cancelnotification on Stop, crash keeps transcript + last prompt, restart re-spawns,shutdown()for app quit (scenePhase →.inactive).Museion/MentorPaneView.swift— the debug pane (post-gate main surface): header with running version + orange drift note, transcript (text / FixCardRow / RawUpdateRow), freeform input + send + Stop, crash card with Restart + Technical details (stderr tail).- Wiring —
AppGateState.mentor(app-scoped model),ContentViewrenders the pane once the gate'scurrentBinaryPathexists (gate's winner, no re-probe),MuseionApptears down on scenePhase.inactive. Hosted-test guard: the pane never auto-opens a session underXCTestConfigurationFilePath(the test host is the runner — a real engine child would kill it). MuseionTests/ACPClientTests.swift— fake ACP server: a zsh script speaking real NDJSON JSON-RPC over the real Process path. Per-binary mode file (full/crashAfterHandshake/hangOnPrompt, self-locating), fix-card payload base64-encoded beside the binary (shell quoting can never break the wire JSON). 7 tests: handshake version, streamed updates + fix-card (model over the real client+fake path), advisory drift note, crash → onCrash exactly once (latched — exit+EOF both fire), cancel fire-and-forget, teardown kills hung child, crash → restart recovery.- DOX:
Museion/AGENTS.mdgained the "ACP bridge (S2.2, #14 map)" section;MuseionTests/AGENTS.mddocuments the fake-server pattern.
Decisions (settled on the map — implemented, not re-decided)
- Transport: NDJSON JSON-RPC on stdio; stdout is the wire, stderr is logs.
- Lifecycle: lazy spawn, app-life child, quit teardown, crash → in-pane Restart.
- Version verify: advisory — os_log + both versions in the pane, never blocks.
- Fix-card round-trip: fake-server test double (real rail is M2/#23 museion-tools).
- Rendering: text + tool calls first-class; every other update type raw JSON.
- Cancel: Stop button →
session/cancelnotification (verified: no response exists).
Verification
xcodebuild … build— BUILD SUCCEEDED.xcodebuild … test— TEST SUCCEEDED, 36/36 (all unit suites incl. 7 ACP/model tests + UI smoke test), re-verified after the finish-audit fixes.swiftformat+swiftlintclean on all touched files.
Notes
- Wire ground truth re-verified against the installed acp SDK 0.9.0 / adapter 0.20.6 before coding:
session/cancelIS a notification (interfaces.pyCancelNotification),SessionNotification.updatediscriminates onsessionUpdate, responses camelCase (agentInfo,sessionId,stopReason), tool calls carryrawInput/title. - Two test-host traps found the hard way: (1) closing pipe file handles with readability handlers still installed crashes Foundation — handlers off first; (2) the hosted unit-test runner IS the app, so any auto-spawned real engine child at pane-mount kills the runner ("test runner exited with code 0") — guarded via
XCTestConfigurationFilePath. A third: requests without a deadline hang forever under parallel-suite load — everyrequest()now carries a 15 s timeout (also correct in production: hung engine → error → restart prompt, not an eternal spinner). - Not done (map's Out of scope): museion-tools MCP server (#23/M2), session lifecycle UI (#28/S2.3), profile backup (#15/S2.6).
Finish-audit pass (2026-09-16)
Three parallel reviewers (ponytail / security / general — the latter two hit API-timeout caps mid-read, findings salvaged from transcripts; the ponytail report landed in full). All three independently caught the same ship-blocker. Accepted and fixed:
- Bug (all reviewers):
binaryPathProviderclosure was never wired in production —AppGateState.mentorused the{ nil }default, so the shipped pane could never spawn the engine. Replaced withvar binaryPath: String?handed in viaMentorPaneView(model:binaryPath:). - Bug: quit teardown keyed on
scenePhase .inactive, which fires on plain focus loss on macOS — engine killed on every app switch. NowNSApplication.willTerminateNotification. - Bug (found by the fix's own test): child-exit AND stdout-EOF both fire for one crash →
onCrashtwice.failAllnow latches; fires exactly once. - Hardening: 1 MB line cap in the NDJSON ingester (unbounded buffer on a corrupt/hostile newline-free stream).
- Shrinks: TurnState enum →
isStreamingBool;handleChildExit/handleEOF→ onefailAll;argumentsparam hardcoded; unusedEquatables deleted; scroll helper dedup; client-level stream test +UpdateRecorderdropped (model test covers the same real path);LockedFlag→ Swift Testingconfirmation. - Deferred: merging
close()intoEngineProbe.terminateAndReap— different shapes (grace period, handle sets, pending-continuation failing); not worth the cross-file refactor.
Files Changed
| File | Change Summary |
|---|---|
Museion/ACPEngineClient.swift |
new — NDJSON JSON-RPC stdio client + JSONValue |
Museion/MentorSessionModel.swift |
new — pane model: lifecycle, streaming, fix-card parse |
Museion/MentorPaneView.swift |
new — the debug pane UI |
MuseionTests/ACPClientTests.swift |
new — fake ACP server + 7 tests |
Museion/ContentView.swift |
pane mounts post-gate with the gate's binary |
Museion/MuseionApp.swift |
app-scoped mentor model + willTerminate teardown |
Museion/AGENTS.md, MuseionTests/AGENTS.md, AGENTS.md, docs/devlog/INDEX.md, CHANGELOG.md |
DOX + records |
Open Items & Next Steps
- Close map #14 on the forge (comment + close — needs Alexander's authorization)
- Live-turn smoke against the real engine + GLM credentials (AC says one-shot ready, #49)
- museion-tools MCP server (#23, M2) — the real fix-card rail
- Session lifecycle UI (#28, S2.3)
Log written by write-log skill