69 KiB
Native macOS App Architecture (libhex + AppKit / SwiftUI)
"This desktop app for Mac looks like shit. Can we make a native AppKit one? It should use the same Rust internals for the shared code. Think about it like how Ghostty works for the most part, even though some of the Rust code does go back to the Swift and AppKit code. Use both AppKit and SwiftUI depending on what is most performant. We could use the Swift Composable Architecture for some of this. The core needs to be architected perfectly."
Status & Scope
- Status: Approved & In Implementation (
feat/native-macos, M0–M3 implemented, M4 cutover pending) - Baseline:
origin/main(1c3dacf, including PR #134 Meetings +hex mcp) andorigin/fix/meeting-echo(f2f273d, PR #135 WAV sample-timeline echo suppression, double-talk preservation, rolling live transcript commits, and single-threaded Moonshine ONNX Runtime sessions) merged intofeat/native-macos. - Relationship to prior plans: Supersedes the macOS half of
docs/plans/shared-desktop-ui.mdwhile preserving the Linux GPUI desktop shell (src/linux_app.rs),hexCLI,local_api, and TypeScript SDK contracts (docs/plans/linux.md,docs/plans/typescript-sdk.md). Continues to honor every non-migration rule indocs/plans/swift-app-handoff.md: bundle IDcom.kitlangton.hex2, app nameHex.app, Sparkle ZIP rootHEX.app, and~/Library/Application Support/voice-control/as the authoritative data root. - Tunable Constants Policy:
KnobsKitintegration is deferred; all tunable motion, spring, and sizing constants live in a single centralizedHexTuningdefinition so they can be wired to live knobs later without touching call sites.
2025–2026 macOS Best-Practices Check & AppKit-First Refinement
Before M0 implementation, we reviewed current 2025–2026 macOS engineering guidance and empirical benchmarks:
- Menu Bar Apps, Activation Policy & Settings Windows (Peter Steinberger, Showing Settings from macOS Menu Bar Items, 2025; Apple Developer Forums macOS 26/27
NSStatusItem&openSettingsthreads): SwiftUI'sMenuBarExtra,Settingsscene,SettingsLink, and@Environment(\.openSettings)fail in.accessory(dockless) menu-bar apps on macOS 15–26 without fragile hidden-window hacks, scene-declaration order workarounds, andTask.sleepactivation delays. Decision: Never use SwiftUI@main App,Settingsscene, orMenuBarExtra. Own the app lifecycle inHexAppDelegate(NSApplicationDelegate), the status bar inStatusBarController(NSStatusItem+NSMenu/NSMenuDelegate), and the main window in an explicitNSWindowControllerthat synchronously promotesNSApp.setActivationPolicy(.regular)beforemakeKeyAndOrderFrontand restores.accessoryonwindowWillClosewhenshow_dock_iconis off (preserving#103). - Non-Activating Floating Panels & Overlays (Matthew Diakonov / Fazm, SwiftUI Floating Panel: NSPanel Patterns for macOS Apps, Apr 2026): SwiftUI has no native floating-panel primitive. The canonical pattern is an
NSPanelsubclass withstyleMask: [.borderless, .nonactivatingPanel],isFloatingPanel = true,becomesKeyOnlyIfNeeded = true,hidesOnDeactivate = false, andcollectionBehavior: [.canJoinAllSpaces, .canJoinAllApplications, .fullScreenAuxiliary]. Furthermore,NSHostingViewrecalculates layout on every frame during live panel resizes. Decision: Use pure AppKitNSPanel+CAMetalLayer+NSView.displayLink(target:selector:)(reusingsrc/dictation_indicator.metal) for the 60–120 Hz Dictation/Voice Action HUD pill, and non-activatingNSPaneloverlays with fixed-size container frames for the Meeting offer and active recording capsules. - AppKit vs. SwiftUI Layout, Lists & Focus on macOS (Blake Cerecero, AppKit vs SwiftUI on macOS: Layout, Performance, and Trade-offs, Apr/Jul 2026; Michael Tsai, NSTableView With SwiftUI): SwiftUI
List/LazyVStackdoes not recycle a fixed pool ofNSViewinstances the wayNSTableView.makeView(withIdentifier:owner:)/NSCollectionViewdoes, causing allocations and scroll stutter at scale; SwiftUINavigationSplitViewand@FocusStatealso fight macOS column resize constraints, first-responder tab loops, and custom key capture. Decision (Updated Split — More AppKit):- Window Chrome & Split Container: Use AppKit
NSWindowController+NSSplitViewController(NSSplitViewItem(sidebarWithViewController:)with nativeNSVisualEffectViewsidebar vibrancy andNSOutlineView/NSTableViewsource list) instead of SwiftUINavigationSplitView. - Lists, Tables, Text Editors & Key Capture: Use AppKit
NSTableView(with cell reuse) forHistory,Commandscatalog,Activityevents,Modeslist, andMeetingsturns;NSTextViewfor multi-line prompts and meeting notes; and a custom AppKitNSViewfirst-responder control for Shortcut Recorders. - Bounded Inspector Cards & Settings Forms: Use leaf
NSHostingView/NSHostingControllerviews (orNSStackViews) for bounded settings sections and modal sheets, keepingView.bodystrictly free of synchronous parsing or I/O.
- Window Chrome & Split Container: Use AppKit
- TCA (
pointfreeco/swift-composable-architecture1.26.2, 2026) + AppKit & Swift 6 Strict Concurrency (Point-Free TCA Releases): TCA1.26.2pairs@Reducerand@ObservableStatewith@MainActor-isolatedStoreandobserve { [weak self] in ... }(viaSwiftNavigation), letting AppKitNSViewController/NSWindowControllercontrollers observe state transitions directly withoutCombineViewStoreoverhead or forcing the outer container into SwiftUI. High-frequency telemetry (60–120 Hz HUD levels/phase) bypasses TCA completely via lock-free C struct polling (hex_engine_read_hud_telemetry).
Research & Prior Art
1. How Ghostty Splits Core (libghostty) and macOS (AppKit + SwiftUI)
Inspection of ghostty-org/ghostty (include/ghostty.h and macos/Sources/Ghostty/Ghostty.App.swift) shows five architectural patterns that map directly to Hex:
Ghostty.app (Swift / AppKit @main)
├── AppDelegate.swift (NSApplicationDelegate, Sparkle SPUStandardUpdaterController, NSMenu, Dock policy)
├── Ghostty.App.swift (owns opaque ghostty_app_t + ghostty_config_t; registers ghostty_runtime_config_s)
│ ├── userdata: Unmanaged.passUnretained(self).toOpaque()
│ ├── wakeup_cb(userdata) ──► DispatchQueue.main.async { state.appTick() } ──► ghostty_app_tick(app)
│ ├── action_cb(app, ghostty_target_s, ghostty_action_s) ──► tagged-union switch on main thread
│ └── read/write_clipboard_cb(userdata, ...) ──► NSPasteboard bridge with async completion handle
└── GhosttyKit.xcframework (static C archive + module.modulemap exposing include/ghostty.h)
└── Core Engine (terminal state, config parser/diagnostics, keybinding tables, Metal surface renderer)
- Opaque Handle + Runtime Callback Table (
ghostty.h): The C header exposes opaque pointers (typedef void* ghostty_app_t; typedef void* ghostty_config_t;) initialized viaghostty_app_new(const ghostty_runtime_config_s*, ghostty_config_t). The embedder passes an unretaineduserdatapointer alongside C function pointers (wakeup_cb,action_cb,read_clipboard_cb,write_clipboard_cb). - Wakeup + Main-Thread Tick Drainage: Core background threads never touch AppKit or Swift state directly. When background work produces UI-visible state or platform requests, the core invokes
wakeup_cb(userdata), which schedulesDispatchQueue.main.async { state.appTick() }.ghostty_app_tick(app)drains the core's internal mailbox on the main thread and invokesaction_cb(app, target, action)synchronously on@MainActor. This eliminates cross-thread lock contention and Swift concurrency data races. - Tagged Action Union (
ghostty_action_s): Core-to-host notifications and platform imperatives travel through a single C tagged union (ghostty_action_tag_e+ghostty_action_u). Swift switches exhaustively overaction.taginGhostty.App.action(_:target:action:). - Authoritative Core Config: Configuration parsing, validation, defaults, and diagnostics stay inside the core (
ghostty_config_new,ghostty_config_load_default_files,ghostty_config_finalize). Swift reads typed projections and reloads viaghostty_app_update_config. - AppKit Lifecycle + Selective SwiftUI:
AppDelegate(NSApplicationDelegate),NSWindowController,NSMenu, Sparkle, andCAMetalLayerviews are pure AppKit. SwiftUI (NSHostingController/NSHostingView) is used inside window chrome for settings sheets, command palettes, inspector panels, and confirmation dialogs.
2. Rust ↔ Swift FFI Evaluation
We evaluated four FFI mechanisms against Hex's constraints (real-time audio/hotkey threads, streaming transcript/audio-level updates, main-actor callbacks, build speed, binary size, and LLDB debuggability):
FFI Option Comparison
├── 1. Hand-written C ABI + cbindgen (Ghostty style) [RECOMMENDED]
│ ├── Main-actor callbacks: Native C fn pointers + wakeup_cb/hex_engine_tick() on @MainActor
│ ├── Streaming events: Zero-copy ring/mailbox drained during tick, plus direct 60 Hz HUD atomic poll
│ ├── Ownership/lifetimes: Explicit opaque handles (hex_engine_t) + arena/slice views valid for tick duration
│ ├── Build integration: cargo build --lib (staticlib) + cbindgen -> HexEngineFFI.xcframework
│ ├── Binary size & overhead: Zero runtime/codegen bloat; zero serialization for hot-path audio/HUD frames
│ └── Debuggability: Single LLDB stack frame across Swift -> C -> Rust with exact DWARF symbols
├── 2. UniFFI (Mozilla)
│ ├── Pros: Ergonomic proc-macro IDL for CRUD/records, async Future bridging
│ └── Cons: Marshals every struct/enum through a RustBuffer byte-serialization pass; callbacks require Arc<dyn Trait> vtables that hop threads awkwardly; high macro/codegen churn
├── 3. swift-bridge
│ ├── Pros: Generates C headers + Swift classes directly from Rust modules
│ └── Cons: Fragile maintenance on Swift 6 strict concurrency; weak enum/tagged-union ergonomics; opaque generated glue complicates LLDB debugging
└── 4. BoltFFI
├── Pros: Newer high-performance Rust->Swift generator with lower buffer overhead than UniFFI
└── Cons: Young ecosystem and tooling risk compared to a stable C header; unnecessary when the UI-facing surface is a coarse-grained engine boundary
Recommendation: Hand-written C ABI + cbindgen (HexEngineFFI), paired with typed JSON snapshots only for cold/complex editor trees (Modes, Commands catalog).
- Why: Hex already uses
@_cdeclC bridges insrc/apple_speech.swift(hex_apple_speech_*) andsrc/apple_intelligence.swift(hex_apple_intelligence_*). A Ghostty-style C ABI (include/hex.hgenerated/verified bycbindgen) keeps hot-path callbacks, lifecycle, audio levels, and HUD state zero-copy and trivially inspectable in LLDB, while cold nested configuration trees (e.g., Mode correction rules and rewrite profiles) can cross the C boundary either as C structs or versioned UTF-8 JSON slices backed by Rust's existingserdetypes without maintaining 50parallel C structs by hand.
3. TCA (pointfreeco/swift-composable-architecture) & Lessons from Legacy kitlangton/Hex
In Kit's archived Swift repository (github.com/kitlangton/Hex), the app split into Hex (Xcode app target) and HexCore (SPM package using swift-dependencies and swift-log), with TCA reducers in Hex/Features/{App,Settings,History,Remappings,Transcription} and @DependencyClient wrappers in Hex/Clients/.
- What worked well in
kitlangton/Hex(Reuse):@Reducer+StoreOf<Feature>for multi-step, branched state machines: Onboarding permission flows, Model download/preparation confirmation alerts, Mode/Remapping CRUD editors, and History search/retention confirmation.@DependencyClient(HexEngineClient) for deterministicTestStoretests and instant Xcode#Previewfixtures without touching hardware microphones,CGEventTap, orApplication Support.- Unified
os.Loggercategories (HexLog) with.privatetranscript redaction.
- What hurt in
kitlangton/Hex(Do NOT repeat):- Running high-frequency audio metering, hotkey state machines, or streaming transcript tokens through TCA
Actiondispatch caused main-thread reducer churn and equality-diffing overhead. - Persisting settings in Swift
@Shared(.fileStorage)/UserDefaultscreated a split brain once Rust became the engine.
- Running high-frequency audio metering, hotkey state machines, or streaming transcript tokens through TCA
- Architectural Rule for TCA in
Hex.app:- Rust (
libhex) is the single source of truth for all domain state, persistence (settings.json,history.json,live.ndjson, meetings), and real-time state machines. - Use TCA (
@ObservableState,@Reducer) for windowed user workflows and complex CRUD navigation:SettingsFeature,ModesFeature,VoiceActionFeature,CommandsFeature,HistoryFeature,OnboardingFeature, and theMeetingsFeaturebrowser/detail workspace. - Do NOT use TCA for 60–120 Hz / low-latency surfaces: the Dictation/Voice Action HUD pill, the focus-free Meeting live transcript/prompt capsule, the
NSStatusItemmenu bar controller, and the Activity live event tail. Those bind directly to lightweight@MainActor @Observablemodels or AppKit/Metal layers fed bylibhex.
- Rust (
1. Process & Ownership Boundary
Single-Process Architecture (Hex.app as @main, libhex linked statically)
On macOS, Hex.app is a native Swift/AppKit executable (Contents/MacOS/hex) that links libhex.a (crate-type = ["staticlib", "rlib"]). There is no daemon/XPC split on macOS because macOS TCC grants (Microphone, Accessibility, Input Monitoring, Screen Recording, Automation) bind to the host app bundle and process tree.
Hex.app Process Boundary (macOS arm64)
├── Swift Host (@MainActor: AppKit + SwiftUI + TCA)
│ ├── HexAppDelegate (NSApplicationDelegate)
│ │ ├── Startup gate: calls hex_keyboard_initialize_layout_on_main_thread() first
│ │ ├── Boots HexEngine (hex_engine_new) + schedules hex_engine_tick() on wakeup_cb
│ │ ├── Owns StatusBarController (NSStatusItem + NSMenu)
│ │ ├── Owns SparkleController (SPUStandardUpdaterController)
│ │ ├── Owns LoginItemController (SMAppService.mainAppService)
│ │ ├── Owns DictationHUDPanelController (click-through NSPanel + CAMetalLayer)
│ │ ├── Owns MeetingCapsulePanelController (focus-free NSPanel for live meeting transcript/notes)
│ │ └── Owns MainWindowController (NSWindow + NSToolbar + NSHostingController<RootSplitView>)
│ ├── Native Swift Platform Bridges (registered with libhex via vtable at init)
│ │ ├── AppleSpeechBridge (SpeechAnalyzer / SpeechTranscriber, macOS 26+)
│ │ ├── AppleIntelligenceBridge (FoundationModels SystemLanguageModel, macOS 26+)
│ │ └── ApplicationCatalogBridge (NSWorkspace app discovery + icon rendering)
│ └── Windowed Feature Reducers (TCA StoreOf<AppFeature> fed by HexEngineClient)
│ ├── OnboardingFeature (permissions + initial model readiness)
│ ├── SettingsFeature, ModesFeature, VoiceActionFeature, CommandsFeature
│ └── HistoryFeature, MeetingsFeature, ActivityFeature, HUDLabFeature
└── Rust Core Engine (`libhex`)
├── Authoritative Audio & Capture: audio (cpal), dictation_audio, dictation, recording_environment
├── Authoritative Input & Hotkeys: suppression (CGEventTap), keyboard (TIS snapshot), paste
├── Authoritative Recognition & ASR: recognition, parakeet, gguf_session, moonshine, transcription*
├── Authoritative Post-Processing: dictation_processor (OpenCode HTTP), text_replacements, personal_commands (Bun)
├── Authoritative Context & Actions: context, accessibility (AXUIElement), commands, command_grammar
├── Authoritative Meetings Engine: meeting (ScreenCaptureKit), meeting_live, meeting_transcript, meeting_echo, meeting_notes, pause_segmenter, microphone_activity, meeting_detection
├── Authoritative Storage & Logs: app_settings (settings.json), history (history.json), events (live.ndjson), app_paths, instance lock
└── Local API & Dev Control: local_api (loopback HTTP for TS SDK), developer_control (hex dev automation)
Subsystem Ownership Matrix
- Stays 100% in Rust (
libhex):- Audio & Timeline (
audio,dictation_audio,dictation,recording_environment,feedback):cpalCoreAudio streams, timestamped mono PCM ring buffer, 450 ms / 1 s pre-roll, warm vs release-while-idle lifecycle, bounded backoff recovery, sleep assertion, output mute, media pause, and feedback tones. - Hotkey & Event Tap (
suppression,keyboard,paste,accessibility): Annotated-sessionCGEventTapon its dedicated high-priority Rust thread, boot-time nanosecond timestamp reconciliation, 300 ms hold/double-tap state machine, 100 ms neutral stale-shortcut recovery, Escape cancellation priority, AX selection/window reads, and generation-safe clipboard paste/restoration. - ASR, Models & Post-Processing (
parakeet,gguf_session,moonshine,transcription*,dictation_processor,personal_commands,text_replacements): Strict-Metaltranscribe.cppinference, checksum-verified model downloads, single preparation worker with latest-choice admission, OpenCodeopencode2 apirewrite pipeline, and Bun@hex/commandshost. - Meetings Capture & Segmentation (
meeting*,pause_segmenter,microphone_activity):ScreenCaptureKitsystem-audio + mic capture, echo cancellation (meeting_echo), pause segmentation, atomic final transcript publication, and note generation orchestration (meeting_notes). - Persistence & Observability (
app_settings,history,events,desktop_activity,local_api,developer_control,instance):settings.json,history.json,live.ndjson,process.log, loopback HTTP server, and single-instance lock.
- Audio & Timeline (
- Moves to Native Swift (
Hex.app):- Application Lifecycle & Dock Policy:
NSApplicationDelegatereplacesgpui::Applicationand rawobjc2setActivationPolicycalls. - Menu Bar (
StatusBarController): Native SwiftNSStatusItem+NSMenu(NSMenuDelegate) replacessrc/status_item.rs, callinglibhexfor installed-only model switching, Paste Last Dictation, and meeting recording controls. - Main Window & All Panes: Native
NSWindow+NavigationSplitView/NSSplitViewControllerreplaces the 10,275-linesrc/app_window.rs,src/desktop_ui.rs,src/desktop_transcription_picker.rs, andsrc/text_input.rson macOS. - Focus-Free Overlays (
DictationHUDPanelController,MeetingCapsulePanelController): AppKitNSPanel(nonactivatingPanel,canJoinAllSpaces,fullScreenAuxiliary,ignoresMouseEventswhere applicable) replacessrc/dictation_indicator.rswindow setup and GPUIMeetingPanelwindows insrc/meeting_watcher.rs. - System Integrations (
Sparkle,SMAppService, Permission Guides, App Icons): Swift ownsSPUStandardUpdaterControllerdirectly (eliminatingsrc/sparkle.rsObjective-C runtime glue),SMAppService.mainAppService(replacingsrc/login_item.rs), native permission animations (replacingnative/permission_guide.m), andNSWorkspaceicon/bundle lookup (replacingsrc/application_catalog.rsObjective-C bridges). - In-Process Apple Frameworks (
SpeechAnalyzer,FoundationModels):src/apple_speech.swiftandsrc/apple_intelligence.swiftmove into the Xcode Swift build and register their C function pointers withlibhexat startup, eliminating the fragileswiftcinvocation insidebuild.rs.
- Application Lifecycle & Dock Policy:
Deep-Dive: Event Tap, Hotkey Capture, and TCC
- Why the
CGEventTapMUST remain in Rust (src/suppression.rs):- Nanosecond Clock Alignment:
suppression.rsreconciles annotated-sessionCGEventGetTimestamp(macOS boot-time nanoseconds) directly against CoreAudio input buffer timestamps indictation_audio.rsso delayed press handling reconstructs speech onset from the pre-roll ring and delayed release handling trims post-release audio. Routing key events through Swift would introduce cross-language marshaling on every system keystroke. - Callback Latency & Watchdog Safety: macOS disables any
CGEventTapthat blocks the event server. Rust's lock-free/boundedPendingInputEventsandDictationHotkeystate machine already enforce the 100 ms keyboard neutrality check,SecondaryFntimestamped modifier-change verification, and shortcut suppression without allocating. - Keyboard Layout Initialization Invariant:
HexAppDelegate.applicationWillFinishLaunchingcallshex_keyboard_initialize_layout()on the main thread before startinglibhexworkers or opening any UI, preserving the exact TIS/TSM snapshot invariant fromAGENTS.md. - Shortcut Recorder Handshake: When the user clicks a shortcut recorder control in Swift Settings/Modes/Voice Action, Swift calls
hex_engine_begin_shortcut_capture(engine, target). Rust suspends global hotkey matching insuppression.rs, captures the raw physical/modifier chord (including standaloneFn/Globe and modifier-only taps that standardNSEventresponders miss or swallow), and streams candidate/committedHexShortcutCaptureEventupdates back to Swift viaaction_cb.
- Nanosecond Clock Alignment:
Deep-Dive: HUD Rendering (Metal vs Core Animation vs SwiftUI)
Hex has two distinct overlay classes on macOS:
- Dictation & Voice Action HUD Pill (
DictationHUDPanelController):- Recommendation: AppKit
NSPanel(ignoresMouseEvents = true,styleMask = [.borderless, .nonactivatingPanel],level = .statusBar) +CAMetalLayerdriven byCADisplayLink(macOS 14+NSView.displayLink(target:selector:)), reusing the existing shadersrc/dictation_indicator.metal. - Why not SwiftUI
TimelineView/Canvas? During active capture and transcription, the HUD animates continuous spring physics (width/height expansion, audio-reactive waveform bars, shimmer sweep, and cancel shake) at 60–120 Hz ProMotion while the user is typing or scrolling in another app.src/dictation_indicator.rs+src/dictation_indicator.metalalready computes the entire rounded-pill SDF, border glow, waveform bars, and processing dots in a single fragment shader draw call (160 bytesofHudUniforms) at<0.2%GPU and near-zero CPU with zero heap allocations per frame. Hosting SwiftUI in the HUD would incur AttributeGraph evaluation on the main thread every frame. - Where Swift improves it: Moving the
NSPanel,CAMetalLayer, and spring animator into ~250 lines of clean Swift (DictationHUDMetalView) deletes 1,400 lines of verboseobjc2message-send boilerplate insrc/dictation_indicator.rs, keeps the exactsrc/dictation_indicator.metalshader compiled natively by Xcode's Metal toolchain intodefault.metallib, and reads a lock-freehex_hud_telemetry_sstruct (phase,rms_level,mode_accent_rgba,is_voice_action,queue_depth) fromlibhexon eachCADisplayLinkvsync.
- Recommendation: AppKit
- Meeting Live Capsule (
MeetingCapsulePanelControllerfromfeat/meetings-ux):- Recommendation: AppKit
NSPanel(nonactivatingPanel) hosting a lightweight SwiftUI view (NSHostingView) backed byNSVisualEffectView(.hudWindow,.withinWindowvibrancy) and Core Animation transitions. - Why: Unlike the 120 Hz audio waveform pill, the Meeting capsule updates at speech-segment cadence (1–4 Hz draft/committed transcript turns, speaker labels, and hover-expanded action buttons for Stop / Copy / Notes). SwiftUI inside a non-activating
NSPanelgives crisp Dynamic Type text layout, native vibrancy, smooth spring height resizing, and interactive hover/click buttons without stealing focus from Zoom/Meet/Slack.
- Recommendation: AppKit
2. libhex Core FFI Surface
C ABI Architecture (include/hex.h)
Following ghostty.h, libhex exposes a single opaque engine handle (hex_engine_t), an embedder callback table (hex_runtime_config_s), a wakeup-driven main-thread tick (hex_engine_tick), a lock-free HUD telemetry poll (hex_engine_read_hud_telemetry), and synchronous/asynchronous command entry points.
libhex FFI Data Flow
├── Swift @MainActor ──► hex_engine_send_command(engine, cmd_tag, payload_json)
│ └── Validated by Rust, mutates authoritative state / spawns worker, persists settings.json
├── Rust Worker Thread (audio / ASR / meeting / preparation / events)
│ ├── Updates atomic HUD telemetry (rms, phase, queue depth)
│ ├── Pushes typed HexCoreEvent into bounded lock-free main-thread mailbox
│ └── Calls runtime.wakeup_cb(userdata) [coalesced AtomicBool gate]
└── Swift DispatchQueue.main ──► hex_engine_tick(engine)
└── Drains mailbox on @MainActor ──► invokes runtime.event_cb(userdata, &event)
Sketch: include/hex.h (Generated & Verified via cbindgen)
#ifndef HEX_H
#define HEX_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct hex_engine_s* hex_engine_t;
typedef struct {
const char* ptr;
size_t len;
} hex_slice_s;
typedef enum {
HEX_HUD_PHASE_HIDDEN = 0,
HEX_HUD_PHASE_LISTENING = 1,
HEX_HUD_PHASE_LOCKED = 2,
HEX_HUD_PHASE_TRANSCRIBING = 3,
HEX_HUD_PHASE_VOICE_ACTION = 4,
HEX_HUD_PHASE_CANCELLED = 5,
HEX_HUD_PHASE_ERROR = 6,
} hex_hud_phase_e;
// Lock-free 64-byte POD struct polled by CADisplayLink at 60-120 Hz.
typedef struct {
hex_hud_phase_e phase;
float rms_level;
float peak_level;
uint32_t active_queue_count;
uint32_t mode_color_rgba;
uint64_t phase_started_ns;
bool is_command_mode_awake;
bool is_voice_action;
bool is_muted_or_silent;
} hex_hud_telemetry_s;
typedef enum {
HEX_EVENT_READINESS_CHANGED = 1,
HEX_EVENT_SETTINGS_CHANGED = 2,
HEX_EVENT_MODEL_PREP_UPDATED = 3,
HEX_EVENT_SHORTCUT_CAPTURE_UPDATED = 4,
HEX_EVENT_HISTORY_UPDATED = 5,
HEX_EVENT_ACTIVITY_APPENDED = 6,
HEX_EVENT_MEETING_STATE_CHANGED = 7,
HEX_EVENT_MEETING_TRANSCRIPT_DELTA = 8,
HEX_EVENT_OPENCODE_CATALOG_UPDATED = 9,
HEX_EVENT_PERSONAL_COMMANDS_UPDATED = 10,
HEX_EVENT_DEV_COMMAND_REQUESTED = 11,
HEX_EVENT_REQUEST_CHECK_FOR_UPDATES = 12,
} hex_event_tag_e;
typedef struct {
hex_event_tag_e tag;
uint64_t generation;
// Borrowed UTF-8 JSON payload valid for the duration of event_cb.
hex_slice_s json_payload;
} hex_event_s;
typedef void (*hex_wakeup_cb)(void* userdata);
typedef void (*hex_event_cb)(void* userdata, const hex_event_s* event);
typedef struct {
void* userdata;
hex_wakeup_cb wakeup_cb;
hex_event_cb event_cb;
bool developer_features_enabled;
} hex_runtime_config_s;
// Must be called on the OS main thread before hex_engine_new.
bool hex_keyboard_initialize_layout_on_main_thread(void);
hex_engine_t hex_engine_new(const hex_runtime_config_s* runtime_cfg);
void hex_engine_free(hex_engine_t engine);
// Drains pending events on the main thread and invokes event_cb synchronously.
void hex_engine_tick(hex_engine_t engine);
// Lock-free read for the 60-120 Hz Metal HUD. Safe from main/display-link thread.
hex_hud_telemetry_s hex_engine_read_hud_telemetry(const hex_engine_t engine);
// Synchronous snapshot queries (returns allocated UTF-8 JSON; free with hex_string_free).
hex_slice_s hex_engine_copy_bootstrap_snapshot_json(const hex_engine_t engine);
hex_slice_s hex_engine_copy_history_page_json(const hex_engine_t engine, hex_slice_s query_utf8, uint32_t limit);
hex_slice_s hex_engine_copy_meeting_detail_json(const hex_engine_t engine, hex_slice_s meeting_id_utf8);
void hex_string_free(hex_slice_s str);
// Command dispatch (settings mutations, model prep, shortcut capture, meeting actions).
bool hex_engine_dispatch_json(hex_engine_t engine, hex_slice_s action_json, hex_slice_s* out_error_json);
#ifdef __cplusplus
}
#endif
#endif /* HEX_H */
Sketch: Swift HexEngineBridge & TCA HexEngineClient
import Foundation
import HexEngineFFI
import ComposableArchitecture
@MainActor
public final class HexEngineRuntime {
public static let shared = HexEngineRuntime()
private var engine: hex_engine_t?
private var continuations: [UUID: AsyncStream<HexEngineEvent>.Continuation] = [:]
public func start(developerFeaturesEnabled: Bool) {
precondition(Thread.isMainThread, "Must start on OS main thread")
let layoutReady = hex_keyboard_initialize_layout_on_main_thread()
precondition(layoutReady, "Failed to initialize TIS keyboard layout snapshot")
var config = hex_runtime_config_s(
userdata: Unmanaged.passUnretained(self).toOpaque(),
wakeup_cb: { userdata in
guard let userdata else { return }
// Hop to @MainActor and drain the Rust mailbox
DispatchQueue.main.async {
let runtime = Unmanaged<HexEngineRuntime>.fromOpaque(userdata).takeUnretainedValue()
runtime.tick()
}
},
event_cb: { userdata, eventPtr in
guard let userdata, let eventPtr else { return }
let runtime = Unmanaged<HexEngineRuntime>.fromOpaque(userdata).takeUnretainedValue()
runtime.handleCABIEvent(eventPtr.pointee)
},
developer_features_enabled: developerFeaturesEnabled
)
self.engine = hex_engine_new(&config)
}
public func tick() {
guard let engine else { return }
hex_engine_tick(engine)
}
public nonisolated func readHUDTelemetry() -> hex_hud_telemetry_s {
// Lock-free atomic snapshot read for CADisplayLink
guard let engine = MainActor.assumeIsolated({ self.engine }) else {
return hex_hud_telemetry_s()
}
return hex_engine_read_hud_telemetry(engine)
}
public func events() -> AsyncStream<HexEngineEvent> {
let id = UUID()
return AsyncStream { continuation in
self.continuations[id] = continuation
continuation.onTermination = { _ in
Task { @MainActor in self.continuations.removeValue(forKey: id) }
}
}
}
public func dispatch(_ command: HexEngineCommand) throws {
guard let engine else { return }
let data = try JSONEncoder().encode(command)
var errSlice = hex_slice_s(ptr: nil, len: 0)
let ok = data.withUnsafeBytes { rawBuf -> Bool in
let slice = hex_slice_s(
ptr: rawBuf.bindMemory(to: CChar.self).baseAddress,
len: rawBuf.count
)
return hex_engine_dispatch_json(engine, slice, &errSlice)
}
if !ok {
defer { hex_string_free(errSlice) }
let message = String(bytesNoCopy: UnsafeMutableRawPointer(mutating: errSlice.ptr!),
length: errSlice.len, encoding: .utf8, freeWhenDone: false) ?? "Unknown engine error"
throw HexEngineError.rejected(message)
}
}
private func handleCABIEvent(_ raw: hex_event_s) {
let data = Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: raw.json_payload.ptr!),
count: raw.json_payload.len,
deallocator: .none)
guard let decoded = try? JSONDecoder().decode(HexEngineEvent.self, from: data) else { return }
for continuation in continuations.values {
continuation.yield(decoded)
}
}
}
@DependencyClient
public struct HexEngineClient: Sendable {
public var bootstrapSnapshot: @MainActor @Sendable () throws -> HexBootstrapSnapshot
public var events: @MainActor @Sendable () -> AsyncStream<HexEngineEvent> = { .finished }
public var dispatch: @MainActor @Sendable (HexEngineCommand) throws -> Void
public var queryHistory: @MainActor @Sendable (_ query: String, _ limit: UInt32) throws -> [HexHistoryEntry]
public var loadMeetingDetail: @MainActor @Sendable (_ id: String) throws -> HexMeetingDetail
}
3. Swift Application Architecture (Hex.app)
Module & Package Layout
We organize the macOS front end into a single local Swift Package (macos/HexMacPackage/Package.swift) consumed by a thin Xcode app project (macos/Hex.xcodeproj), mirroring the Hex + HexCore ergonomics of kitlangton/Hex while keeping nearly all Swift files in SPM so folder structure is git-merge-friendly and testable via swift test:
macos/
├── Hex.xcodeproj (thin wrapper: signing, entitlements, Assets.xcassets, AppIcon.icon, Sparkle.framework)
├── HexApp/ (Xcode @main executable target)
│ ├── main.swift (installs HexAppDelegate, runs NSApplication)
│ ├── Info.plist (com.kitlangton.hex2, LSUIElement/Dock policy, Sparkle keys, usage descriptions)
│ └── Hex.entitlements
└── HexMacPackage/ (local Swift 6 SPM package)
├── Package.swift
├── Binaries/
│ └── HexEngineFFI.xcframework (libhex.a + include/hex.h + module.modulemap)
├── Sources/
│ ├── HexEngineClient/ (Typed Swift models, HexEngineRuntime, @DependencyClient, preview fixtures)
│ ├── HexPlatformBridges/ (AppleSpeechBridge, AppleIntelligenceBridge, AppCatalogBridge, Sparkle, LoginItem)
│ ├── HexDesignSystem/ (Native AppKit/SwiftUI tokens, shortcut recorder chip, audio level meter, pane headers)
│ ├── HexOverlays/ (DictationHUDPanelController + Metal shader view, MeetingCapsulePanelController)
│ ├── HexMenuBar/ (StatusBarController: NSStatusItem + NSMenu with installed-only model picker)
│ └── HexFeatures/ (TCA Reducers + SwiftUI/AppKit Views)
│ ├── AppFeature/ (Root split view coordinator, sidebar navigation, modal/sheet routing)
│ ├── OnboardingFeature/ (Microphone, Input Monitoring, Accessibility, ASR model gate)
│ ├── SettingsFeature/ (Hotkeys, Microphone, Language/Model picker, Recording & Environment, Updates)
│ ├── ModesFeature/ (App/Browser context rules, replacements, OpenCode rewrite profile, transformations)
│ ├── VoiceActionFeature/ (Opt-in toggle, shortcut, OpenCode model & deadline, selection status)
│ ├── CommandsFeature/ (Opt-in toggle, Moonshine prep status, release-mic conflict sheet, catalog & TS workspace)
│ ├── HistoryFeature/ (Searchable retained dictations, retention policy, copy/re-paste, two-step clear)
│ ├── MeetingsFeature/ (Opt-in beta + debug Meetings workspace: live/past turns, echo-cancelled tracks, AI notes)
│ ├── ActivityFeature/ (Debug-only live NDJSON event tail & diagnostics)
│ └── HUDLabFeature/ (Debug-only interactive HUD physics & state previewer)
└── Tests/
├── HexEngineClientTests/ (FFI round-trip & snapshot decoding tests)
└── HexFeaturesTests/ (TCA TestStore unit tests & view snapshot tests)
Component-by-Component UI Mapping (AppKit vs SwiftUI vs TCA)
Surface & Pane Architecture
├── Main Window Shell (NSWindowController + NSToolbar + NSSplitViewController)
│ ├── Sidebar: AppKit NSSplitViewItem(sidebarWithViewController:) + NSOutlineView/NSTableView source list
│ ├── Pane Scaffold: AppKit split controllers + leaf NSHostingController/NSStackView cards
│ ├── Settings Pane (Leaf SwiftUI/AppKit cards + TCA SettingsFeature observed via observe { })
│ │ ├── Shortcut Recorder: Custom AppKit NSView first-responder control + libhex capture hook
│ │ ├── Transcription Model Picker: Modal sheet/popover with search, language filter, progress ring
│ │ └── Microphone & Volume: Native pop-up button & slider wired to immediate Rust tone preview
│ ├── Modes Pane (AppKit NSSplitViewController + NSTableView mode list + TCA ModesFeature)
│ │ ├── Left column: NSTableView mode list with NSWorkspace app/browser icons
│ │ └── Right column: Context matchers, Replacements NSTableView, OpenCode prompt NSTextView, Transformations
│ ├── Voice Action Pane (Leaf SwiftUI/AppKit form + TCA VoiceActionFeature)
│ ├── Commands Pane (AppKit NSSplitViewController + NSTableView catalog + TCA CommandsFeature)
│ ├── History Pane (AppKit NSTableView with cell reuse + detail inspector + TCA HistoryFeature)
│ │ └── Uses NSTableView makeView(withIdentifier:owner:) for 10,000-entry zero-jank scrolling
│ ├── Meetings Pane (AppKit NSSplitViewController + NSTableView turns + NSTextView notes + TCA MeetingsFeature)
│ │ ├── Transcript Stream: Virtualized NSTableView speaker turns ("You" / "Computer"), live draft tail
│ │ └── Notes & Summary: Native NSTextView markdown editor + Apple Intelligence / OpenCode summary controls
│ └── Activity & HUD Lab Panes (Debug-only; @Observable ring buffer + NSTableView for 1,000+ NDJSON events)
├── Menu Bar Extra (AppKit NSStatusItem + NSMenuDelegate — NO SwiftUI MenuBarExtra)
│ └── Why: SwiftUI MenuBarExtra has broken submenu state updates and window focus quirks; NSMenu is instant
├── Dictation / Voice Action HUD (AppKit NSPanel + CAMetalLayer + NSView.displayLink)
│ └── Zero focus steal, click-through, 60-120 Hz spring animation + dictation_indicator.metal
└── Meeting Offer & Active Capsule (AppKit NSPanel nonactivatingPanel + fixed-size capsule view)
└── Focus-free floating pills during detected-meeting offers and active meeting recordings
Design System, Accessibility, and Performance Budget
- Native macOS Look & Feel (Why it stops "looking like shit"):
- System Materials & Colors: Uses real
NSVisualEffectViewsidebar vibrancy, semanticNSColor(windowBackgroundColor,controlBackgroundColor,labelColor,secondaryLabelColor,separatorColor), and native grouped inset forms (.formStyle(.grouped)) instead of flat custom-painted GPUI rectangles. - Automatic Light & Dark Mode: Supports both macOS Dark and Light appearances natively (with an optional override in Settings), while keeping the floating Dictation HUD pill on its signature high-contrast translucent dark glass material.
- Typography & Icons: San Francisco (
NSFont.systemFont/.monospacedDigitSystemFontfor timers and sizes) and hierarchical SF Symbols with smooth symbol effects (.symbolEffect(.pulse)for recording states).
- System Materials & Colors: Uses real
- Accessibility & Keyboard Navigation:
- Full VoiceOver support (
accessibilityLabel,accessibilityValue,accessibilityHint) on every control, model row, and transcript turn—something GPUI currently lacks on macOS. - Full keyboard tab-loop (
NSWindow.initialFirstResponder,⌘Fsearch focus in History/Commands/Meetings,⌘,Settings,⌘1–⌘6pane switching, standardNSTextViewIME and Undo/Redo stack).
- Full VoiceOver support (
- Explicit Performance Budgets:
- Idle memory (window closed, menu bar + warm Parakeet/Cohere model): Swift UI overhead
≤ 18 MBresident abovelibhexmodel weights; Main WindowNSWindowControllerand its SwiftUI view hierarchy are deallocated when the window closes (recreated on reopen fromHexEngineClient.bootstrapSnapshot()). - Idle CPU (window closed):
0.0%main-thread CPU (CADisplayLinkpaused when HUD phase isHEX_HUD_PHASE_HIDDEN). - Hotkey press-to-HUD-visible latency:
≤ 8 ms(one vsync at 120 Hz). - Main Window cold open time:
≤ 35 msfrom menu-bar click to first rendered frame.
- Idle memory (window closed, menu bar + warm Parakeet/Cohere model): Swift UI overhead
4. Build, Packaging, Signing, and Release Continuity
Project Generator & Build Graph
We use Keel (macos/Project.swift) (keel ensure materializes gitignored macos/Hex.xcodeproj) so that adding Swift files under macos/HexMacPackage/Sources/ or macos/HexMacApp/ never touches .pbxproj merge conflicts.
Build Pipeline (scripts/build-app.sh)
├── 1. bun run --cwd sdk/commands build
│ └── Produces managed @hex/commands JS bundle embedded by Rust
├── 2. cargo build --release --lib --target aarch64-apple-darwin
│ ├── Compiles libhex.a (with transcribe.cpp Metal, Moonshine, Audio, Suppression, Meetings)
│ └── cbindgen verifies/emits include/hex.h ──► packaged into HexEngineFFI.xcframework
├── 3. cargo build --release --bin hex --no-default-features --features cli-macos
│ └── Produces lightweight headless CLI binary (or single universal entrypoint; see below)
├── 4. xcodebuild -project macos/Hex.xcodeproj -scheme Hex -configuration Release
│ ├── Compiles Swift 6 targets (HexFeatures, HexOverlays, HexPlatformBridges, AppleSpeech, AppleIntelligence)
│ ├── Compiles src/dictation_indicator.metal ──► Contents/Resources/default.metallib
│ ├── Compiles assets/AppIcon.icon via Xcode 26 Icon Composer (actool)
│ └── Links libhex.a + Sparkle.framework + system frameworks into Hex.app/Contents/MacOS/hex
└── 5. scripts/validate-app.sh & scripts/test-app-identity.sh
└── Verifies com.kitlangton.hex2, Team ID QC99C9JE59, hardened runtime, notarization, and Sparkle keys
Preserving Single-Binary CLI + GUI Entrypoint (Contents/MacOS/hex)
Today, Hex.app/Contents/MacOS/hex serves both as the GUI bundle executable (when launched with no args or --app / preview) and as the CLI (hex listen, hex status, hex dev, hex internal-local-api, hex meeting ...).
We preserve this exact contract with zero overhead:
macos/HexApp/main.swiftcallshex_cli_maybe_run_from_process_args()as its very first line.- If the process was invoked with CLI subcommands (
status,listen,internal-local-api,dev,meeting,--help,--version), Rust executes the CLI synchronously and exits (std::process::exit(code)) beforeNSApplication.sharedis ever initialized! - If launched as the macOS app (no CLI subcommand, or
--app/preview),hex_cli_maybe_run_from_process_args()returnsfalseandmain.swiftbootsNSApplication.sharedwithHexAppDelegate. - This means the TypeScript SDK (
@kitlangton/hex),local_apidirect-child spawning, andhex devcontinue to invokeHex.app/Contents/MacOS/hexwithout bundling a second binary or duplicatinglibhex.a.
Release & Sparkle Continuity Checklist
Every invariant in AGENTS.md, scripts/validate-app.sh, and scripts/release-app.sh remains unbroken:
- Bundle Identity:
CFBundleName = Hex,CFBundleDisplayName = Hex,CFBundleIdentifier = com.kitlangton.hex2,CFBundleExecutable = hex, packaged asHex.app. - Signing & Notarization: Signed with Developer ID Application under team
QC99C9JE59(VOICE_CONTROL_TEAM_ID=QC99C9JE59) and notarized withHEX_NOTARY_PROFILE, hardened runtime (--options runtime), and existing entitlements (com.apple.security.device.audio-input,com.apple.security.automation.apple-events). - Sparkle Archive Root Quirk:
scripts/release-app.shcontinues to packageHex.appinside the manual DMG and stage a copy asHEX.appat the root of the Sparkle.ziparchive so any legacy 2.0.x / 2.1.x installs update cleanly. - Monotonic
CFBundleVersion: Advances monotonically above current production builds (compute_bundle_versioninscripts/build-app.sh). - Dynamic Libraries:
libmoonshine.dylibandlibonnxruntime.*.dylibremain bundled inContents/Frameworks/with@executable_path/../Frameworksrpath and inner-out Developer ID code signing.
5. Linux, CLI, Local API, and SDK Impact
Cross-Platform Target Split (Cargo.toml)
├── Shared Engine (`libhex` core modules — 100% shared across macOS & Linux)
│ └── audio, transcription_models, transcription_preparation,transcription_service,
│ gguf_session, text_replacements, history, events, desktop_activity, local_api
├── macOS Library + CLI (`cfg(target_os = "macos")`)
│ ├── Exposes `ffi_macos` (include/hex.h) for Swift Hex.app
│ ├── Retains macOS domain modules (suppression, dictation_audio, parakeet, moonshine,
│ │ recognition, commands, personal_commands, dictation_processor, meeting*, app_settings)
│ └── Drops `gpui` dependency on macOS at M4 (saving ~120 crates and ~45s clean compile time on macOS)
└── Linux Binary (`cfg(target_os = "linux")`)
└── Keeps `gpui` in `[target.'cfg(target_os = "linux")'.dependencies]` for `linux_app.rs`,
`desktop_ui.rs`, `desktop_transcription_picker.rs`, and `text_input.rs`
- Linux GPUI (
src/linux_app.rs): Untouched. Movinggpuiunder[target.'cfg(target_os = "linux")'.dependencies]inCargo.toml(at Milestone M4) keeps the Linux X11/Wayland Settings app (src/linux_app.rs,src/desktop_host.rs,src/desktop_ui.rs) working exactly as documented indocs/plans/linux.md, while removinggpuifrom macOS builds. local_api& TypeScript SDK (@kitlangton/hex): Untouched.src/local_api.rsandsrc/transcription_service.rslive insidelibhexand run in both the GUI process andhex internal-local-api.
6. Strangler Migration Plan & Milestones
We migrate incrementally using a dual-shell Cargo/Xcode strangler pattern so main stays shippable at every commit and feat/meetings-ux lands cleanly without a multi-week freeze.
Strangler Migration Timeline
├── M0: FFI Spine & Dual-Shell Harness (3–4 days)
│ └── Extract libhex C ABI (hex.h), move apple_speech.swift & apple_intelligence.swift into Xcode,
│ boot empty Hex.app shell alongside existing GPUI shell (--legacy-gpui flag)
├── M1: Menu Bar, 120 Hz Metal HUD, Meeting Capsule & Onboarding Gate (4–5 days)
│ └── Native NSStatusItem, DictationHUDPanelController (reusing dictation_indicator.metal),
│ MeetingCapsulePanelController, SMAppService, Sparkle, and Onboarding window
├── M2: Core Settings, Modes, Voice Action & Commands Panes (5–6 days)
│ └── Native NavigationSplitView window + TCA reducers for Settings, Modes, Voice Action, Commands;
│ parity with scripts/capture-preview.sh
├── M3: History, Meetings Workspace (feat/meetings-ux), Activity & HUD Lab (4–5 days)
│ └── Virtualized History table, full Meetings transcript/echo/notes workspace, debug panes
└── M4: Cutover & GPUI Removal on macOS (2 days)
└── Delete src/app_window.rs, src/status_item.rs, src/dictation_indicator.rs, src/sparkle.rs,
gate gpui to target_os = "linux", validate signed release & Sparkle upgrade
Milestone Details
M0 — libhex FFI Spine & Xcode Workspace (~3–4 days)
- Scope:
- Add
[lib] name = "hex" crate-type = ["staticlib", "rlib"]toCargo.toml. - Create
src/ffi.rsimplementinghex_engine_new,hex_engine_tick,hex_engine_read_hud_telemetry,hex_engine_copy_bootstrap_snapshot_json, andhex_engine_dispatch_json, pluscbindgen.tomlgeneratinginclude/hex.h. - Create
macos/Hex.xcodeproj+macos/HexMacPackage/Package.swiftlinkinglibhex.a. - Move
src/apple_speech.swiftandsrc/apple_intelligence.swiftintoHexPlatformBridgesso Xcode compiles them directly with the macOS 26 SDK (FoundationModelsweak-linked forMACOSX_DEPLOYMENT_TARGET=15.0). - Wire
main.swiftto delegate CLI subcommands to Rust (hex_cli_maybe_run_from_process_args()).
- Add
- Risks: Symbol collisions or missing framework link flags when moving from
cargo rustcbinary linking toxcodebuildstatic library linking. - Exit Criteria:
scripts/build-app.shproduces a validHex.appviaxcodebuild;cargo testandswift testpass;hex status,hex listen, andsdk/typescripttests pass against the new binary.
M1 — Menu Bar, Metal Dictation HUD, Meeting Capsule & Onboarding (~4–5 days)
- Scope:
- Implement
StatusBarController(NSStatusItem+NSMenu) with installed-only model switching, Paste Last Dictation, Meeting controls, and Check for Updates. - Implement
DictationHUDPanelController(NSPanel+CAMetalLayer+CADisplayLink) usingsrc/dictation_indicator.metaland pollinghex_engine_read_hud_telemetry. - Implement
MeetingCapsulePanelController(NSPanel+NSHostingView) for the focus-free live meeting bar and detected-meeting prompt fromfeat/meetings-ux. - Implement
OnboardingFeature(TCA + SwiftUI) enforcing the release startup gate (Microphone, Input Monitoring, Accessibility, and selected ASR model). - Replace
src/sparkle.rsandsrc/login_item.rswith native SwiftSPUStandardUpdaterControllerandSMAppService.mainAppService.
- Implement
- Risks: Window level, collection behavior (
canJoinAllSpaces | fullScreenAuxiliary), or mouse-event passthrough regressions on the HUD or Meeting capsule. - Exit Criteria: Hotkey dictation, double-tap lock, Escape cancel, Voice Action promotion, and Meeting live capsule work end-to-end with zero focus stealing;
sfltool dumpbtmverifies Launch at Login registration.
M2 — Settings, Modes, Voice Action & Commands Panes (~5–6 days)
- Scope:
- Build the main
NSWindowController(NavigationSplitViewwith native vibrant sidebar) and TCA features:SettingsFeature: Shortcut recorders (Dictation, Paste Last Dictation, Double-tap lock), Microphone picker, Language & Model picker sheet with live download progress and cancellation fence, Release Microphone while Idle confirmation alert, Feedback volume slider with tone preview, History retention picker, Launch at Login, and Dock visibility.ModesFeature: Master-detail mode list, app bundle / browser domain activation rules (using nativeNSWorkspaceicons), text replacement table, OpenCode rewrite model/variant/prompt editor, and ordered@hex/commandstransformation chain picker.VoiceActionFeature: Persisted opt-in toggle, shortcut recorder, OpenCode model/variant/timeout controls, and live OpenCode availability banner with one-click retry.CommandsFeature: Opt-in toggle, Moonshine preparation status/retry, mutual-exclusion confirmation with Release Microphone while Idle, command catalog browser, and@hex/commandsworkspace opener.
- Build the main
- Risks: Maintaining exact parity on mutual-exclusion alerts (Commands vs Release Microphone while Idle) and uncommitted shortcut capture cancellation on window close.
- Exit Criteria: Every state in
docs/features/{settings,modes,voice-action,voice-commands,model-management,onboarding}.mdis implemented and verified via deterministicscripts/capture-preview.shtargets and TCATestStoresuites.
M3 — History, Meetings Workspace (feat/meetings-ux), Activity & HUD Lab (~4–5 days)
- Scope:
HistoryFeature: High-performance searchable transcript table (NSTableView/ virtualized split detail view), copy/re-paste actions, retention policy selector, and two-step Clear History confirmation.MeetingsFeature: Full native workspace for thefeat/meetings-uxcapability set: Meetings beta opt-in toggle in Settings (plus debug-enabled access), live & recorded meeting list, speaker-segmented transcript view (You/Computer) with echo-cancellation status, inline markdown notes editor, and Apple Intelligence / OpenCode summary generation.ActivityFeature&HUDLabFeature(Debug builds only): Live NDJSON event stream table overdesktop_activityand interactive HUD spring/state tuner.
- Risks: Smooth scrolling during live meeting transcript streaming and markdown note editing.
- Exit Criteria: 10,000-row History synthetic benchmark scrolls at 120 fps; Meetings live recording, draft streaming, atomic final publication, and AI note generation match
docs/features/meetings.md.
M4 — Cutover, Cleanup & Release Validation (~2 days)
- Scope:
- Delete macOS-only GPUI &
objc2UI bridges:src/app_window.rs,src/status_item.rs,src/dictation_indicator.rs,src/sparkle.rs,src/login_item.rs,src/permission_guide.rs,native/permission_guide.m, andsrc/application_catalog.rs. - Move
gpuiandraw-window-handleinCargo.tomlunder[target.'cfg(target_os = "linux")'.dependencies]. - Update
docs/features/*.mdcheck references to point at the new Swift feature modules and tests.
- Delete macOS-only GPUI &
- Risks: Ensuring Linux
cargo check --target x86_64-unknown-linux-gnu/ CI remains green afterCargo.tomldependency gating. - Exit Criteria:
scripts/validate-app.sh,scripts/test-app-identity.sh,cargo clippy --all-targets --all-features -- -D warnings,swift test, and Sparkle upgrade smoke test from current productionHex.appall pass.
Deterministic Preview & developer_control Support
scripts/capture-preview.sh and hex dev remain first-class in the native Swift app:
- Launching
Hex.app/Contents/MacOS/hex preview <pane> [--update-available] [--model-missing] ...passes the parsedPreviewConfigfrom Rust intoHexAppDelegate, which initializesStoreOf<AppFeature>with deterministicHexEngineClient.preview(config)fixtures, sizes theNSWindowto the canonical frame, disables window fade animations, and renders immediately forscreencapture -l <windowID>. developer_control.rs(hex dev open-pane,hex dev snapshot, etc.) emitsHEX_EVENT_DEV_COMMAND_REQUESTEDthroughevent_cb, allowinghex devCLI automation to drive the native Swift UI identically to the old GPUI window.
7. Testing Strategy & Invariant Verification
Verification Pyramid
├── Layer 1: Rust Engine Unit & Integration Tests (`cargo test`)
│ └── Audio timeline, hotkey state machine, model prep worker, history caps, meetings, local_api
├── Layer 2: C ABI & FFI Contract Tests (`cargo test` + `HexEngineClientTests`)
│ └── cbindgen header diff check, JSON snapshot round-trips, wakeup/tick mailbox ordering, leak checks
├── Layer 3: TCA Reducer & Effect Tests (`swift test` via TCA TestStore)
│ └── Exhaustive state/effect assertions for Onboarding, Settings, Modes, Voice Action, Commands, History, Meetings
├── Layer 4: Deterministic Visual Preview Captures (`./scripts/capture-preview.sh`)
│ └── Pixel-accurate window captures for every pane and error/confirmation state in Light and Dark mode
└── Layer 5: Bundle Identity & Live Smoke Verification (`./scripts/test-app-identity.sh` + `hex dev`)
└── Bundle ID com.kitlangton.hex2, Team QC99C9JE59, SMAppService sfltool check, Sparkle feed upgrade
Preserving Critical AGENTS.md Invariants Across the FFI Seam
keyboard::initialize_layoutMain-Thread Startup Gate: Enforced byhex_keyboard_initialize_layout_on_main_thread()inHexAppDelegate.applicationWillFinishLaunching, which assertspthread_main_np() != 0in Rust and abortshex_engine_newif called before layout initialization.- Release Startup Gate vs Non-Blocking Command Model Prep:
hex_engine_newstarts the hotkey listener only whenonboarding::all_required_ready()holds; enabling Commands triggers background Moonshine preparation in Rust and streamsHEX_EVENT_MODEL_PREP_UPDATEDtoCommandsFeaturewithout blocking hotkey dictation. - Shortcut Capture Suspension & Disconnect Cleanup: Starting shortcut capture in
SettingsFeature/ModesFeature/VoiceActionFeatureregisters a scoped cancellation token inSettingsFeature.Action.onDisappearandNSWindowWillCloseNotificationthat invokeshex_engine_cancel_shortcut_capture(engine), restoring the previous binding if uncommitted. - Mutual Exclusion of
Release microphone while idleandCommands: Enforced both in the TCA reducer (presenting the native confirmationAlertState) and authoritatively insideapp_settings.rswhen handlingHexEngineCommand.setReleaseMicrophoneWhileIdle/setCommandsEnabled. - Single Model-Preparation Owner: Both the
NSMenumodel submenu (StatusBarController) and the SwiftUITranscriptionPickerSheet(SettingsFeature) dispatchHexEngineCommand.selectTranscriptionModelto Rust's singleTranscriptionPreparationController(src/transcription_preparation.rs), withallow_download = falsefrom the menu bar andallow_download = truefrom Settings. - HUD Click-Through &
TranscribingVisibility:DictationHUDPanelControllersetspanel.ignoresMouseEvents = trueandpanel.becomesKeyOnlyIfNeeded = false; Rust'shex_hud_telemetry_s.phasestaysHEX_HUD_PHASE_TRANSCRIBINGuntil the ordered output/paste worker finishes inserting text.
8. Key Risks, Tradeoffs, and Recommendations
- Risk: Dual-State Drift Between TCA Stores and Rust
app_settings/history- Mitigation & Recommendation: TCA stores never persist their own state or optimistically diverge from Rust on committed settings. Every setting mutation dispatches a
HexEngineCommandto Rust; Rust validates, writessettings.json, updates atomic projections, and emitsHEX_EVENT_SETTINGS_CHANGEDon the nexthex_engine_tick, which updates the TCA store's@ObservableState.
- Mitigation & Recommendation: TCA stores never persist their own state or optimistically diverge from Rust on committed settings. Every setting mutation dispatches a
- Decision: C Structs vs JSON Slices for Complex Payloads Over
hex.h- Recommendation: Use flat C structs (
#[repr(C)]) for hot, high-frequency telemetry (hex_hud_telemetry_s) and lifecycle handles, and use borrowed UTF-8 JSON slices (hex_slice_s) for cold/structured snapshots and CRUD commands (Modes,History,Meetings,Settings). At human UI interaction rates (< 20 Hz),serde_json+JSONDecoderover an in-memory buffer takes< 40 µswhile eliminating thousands of lines of manual C struct array allocation/deallocation code.
- Recommendation: Use flat C structs (
- Decision:
NSMenuvs SwiftUIMenuBarExtrafor the Status Item- Recommendation: Use AppKit
NSStatusItem+NSMenu(NSMenuDelegate). SwiftUIMenuBarExtra(both.menuand.windowstyles) has well-known macOS bugs with dynamic submenu updates, keyboard shortcuts, and stealing focus from the active application when invoking "Paste Last Dictation".
- Recommendation: Use AppKit
- Decision: Xcode Project Management (
Hex.xcodeprojvsXcodeGen)- Recommendation: Keep all Swift feature, bridge, and UI code in
macos/HexMacPackage/Package.swift(where source files are discovered automatically by directory without project file edits) and use a tiny, stablemacos/Hex.xcodeproj(generated frommacos/Project.swiftviakeel ensure) solely for the@mainapp bundle wrapper,Assets.xcassets,AppIcon.icon, entitlements, andSparkle.frameworkembedding.
- Recommendation: Keep all Swift feature, bridge, and UI code in
- Decision: Merging
origin/feat/meetings-uxBefore vs During M0- Recommendation: Merge
feat/meetings-uxintomainfirst (or rebaseM0directly onfeat/meetings-ux), becausefeat/meetings-uxalready cleanly separates the Rust meeting backend (meeting_transcript.rs,meeting_echo.rs,meeting_notes.rs,pause_segmenter.rs,apple_intelligence.rs) from the UI presentation.
- Recommendation: Merge
9. Behavior-ID Parity Checklist (docs/features/*.md)
Every user-facing behavior ID across docs/features/README.md, dictation.md, commands.md, voice-action.md, meetings.md, and recovery.md is tracked below against the native macOS implementation (macos/HexMacPackage + src/ffi.rs).
Setup, Models, Output, Settings & Maintenance (docs/features/README.md)
setup.download-recovery— Done: Model transfer host guidance & retry surfaced viaHexTranscriptionModelPickerSheetandHexCommandsPane(src/ffi.rs,HexGeneralSettingsPane.swift).setup— Done: Startup gate (HexOnboardingView+HexGeneralSettingsPanetop banners) checks Microphone, Input Monitoring, Accessibility, and local model installation before starting dictation (src/ffi.rs,HexAppDelegate.swift).setup.language-conditioning— Done: Language selector + Parakeet v3 auto-detect notice and Whisper language-hint support inHexTranscriptionModelPickerSheet(HexGeneralSettingsPane.swift).setup.quick-model-switch— Done: Installed-onlyTranscription Modelsubmenu inStatusBarControllerwith remembered language/hints,Cancel Model Switch, andManage Models…(StatusBarController.swift,src/ffi.rs).setup.delete-model— Done: Model deletion inHexTranscriptionModelPickerSheetspares active/preparing models and acquires the download lock (src/ffi.rs,HexGeneralSettingsPane.swift).dictation— Done: Hold-to-dictate, double-tap lock,double_tap_only, and Escape cancellation vialibhex+HexGeneralSettingsPane(src/ffi.rs,HexGeneralSettingsPane.swift).output— Done: Submission-order paste, spoken Send,Paste Last Dictation(⌥⇧V+ menu item), and fullHexHistoryPane(search, copy text/raw transcript, retention picker, entry delete, two-step Clear History) (HexHistoryCommandsMeetingsPanes.swift).settings— Done: NativeHexGeneralSettingsPanewithHexShortcutRecorderControl(plus Left/Either/Right side picker), Microphone picker + refresh, Keep Ready vs Release When Idle confirmation alert, While Recording behavior, Transcribe During Pauses, and Feedback Volume slider (HexGeneralSettingsPane.swift,HexSettingsStore.swift).recording.environment— Done: Rustrecording_environmentRAII sleep assertion, mute, and media pause owned bylibhexworker.modes— Done:HexModesPanemaster-detail editor for Global and custom modes: app bundle & Brave domain activation rules, phrase-boundary corrections, OpenCode AI rewrite (model, thinking variant, prompt), and ordered@hex/commandstransformations (HexModesAndVoiceActionPanes.swift).recovery/maintenance— Done: Menu bar reachable when dockless; orderly shutdown inHexAppDelegate.applicationWillTerminate.maintain.startup/maintain.dock-policy— Done:HexAppDelegate.applyDockPolicypromotes.regularwhile Settings is visible and restores.accessoryon close whenshow_dock_iconis off (HexAppDelegate.swift).maintain.login-item— Done:LaunchAtLoginController(SMAppService.mainApp) handles.enabled,.notRegistered/.notFound, and.requiresApprovalwith deep link to System Settings > Login Items (HexSettingsStore.swift,HexGeneralSettingsPane.swift).maintain.updates/maintain.managed-updates— Done:SparkleController+DisableUpdatesmanaged-organization policy inStatusBarControllerandHexGeneralSettingsPane(SparkleController.swift,StatusBarController.swift).maintain.linux-service/dictate.feedback.linux— Done (N/A on macOS): Preserved untouched in Linux targets.
Dictation (docs/features/dictation.md)
dictate.hold/dictate.short-tap— Done: 300 ms hold threshold and short-tap discard inlibhex(suppression.rs,recognition.rs).dictate.lock/dictate.double-tap-only— Done: Toggles and key-binding gate inHexGeneralSettingsPane(HexGeneralSettingsPane.swift,src/ffi.rs).dictate.boundaries/dictate.release-tail/dictate.cold-microphone— Done: Authoritative timeline and warm/cold microphone policy inlibhex(dictation_audio.rs).dictate.model-windows/dictate.incremental/dictate.model-memory/dictate.inference-threads— Done:Transcribe during pausestoggle inHexGeneralSettingsPanebacked bylibhex(parakeet.rs).dictate.silence-gate/dictate.overlap/dictate.cancel— Done: Silence gate, queued overlap, and Escape cancellation inlibhex.dictate.feedback— Done: Click-through 120 Hz MetalDictationHUDPanelController(src/dictation_indicator.metal) + immediate volume preview tones (DictationHUDPanelController.swift,src/ffi.rs).
Voice Commands (docs/features/commands.md)
commands/commands.opt-in— Done:HexCommandsPaneopt-in toggle, mutual-exclusion confirmation withRelease microphone while idle, background Moonshine preparation status, Retry, and Turn Off actions (HexHistoryCommandsMeetingsPanes.swift,HexSettingsStore.swift).commands.awake-sleep/dictate.voice-controls— Done: Handled bylibhexrecognition worker and reflected inDictationHUDPanelControllertelemetry (is_command_mode_awake).commands.catalog/commands.context— Done: Searchable command catalog and detail inspector with live foreground context label inHexCommandsPane(HexHistoryCommandsMeetingsPanes.swift).commands.personal-config— Done:Create Custom Config,Open hex.config.ts,Copy Agent Prompt, and reload error banner inHexCommandsPane(src/ffi.rs,HexHistoryCommandsMeetingsPanes.swift).
Voice Action (docs/features/voice-action.md)
voice-action/voice-action.opt-in— Done:HexVoiceActionPaneindependent opt-in toggle (reachable when OpenCode is unavailable), conflict validation, and saved state preservation (HexModesAndVoiceActionPanes.swift,src/ffi.rs).voice-action.shortcut/voice-action.selection— Done:HexShortcutRecorderControlfor theedittarget + Option-to-Option+Command promotion and Accessibility selection capture inlibhex.voice-action.processing/voice-action.output/voice-action.cancel— Done: OpenCode model & thinking-variant pickers, catalog refresh, setup docs link, and HUDvoiceActionphase inHexVoiceActionPaneandDictationHUDPanelController.
Meetings (docs/features/meetings.md)
meetings.opt-in— Done:Meetings (Beta)section inHexGeneralSettingsPane(enable toggle, Screen & System Audio permission check, transcript folder picker/reveal/reset, Apple Intelligence summary toggle) (HexGeneralSettingsPane.swift).meetings.detect/meetings.offer/meetings.record/meetings.call-ended— Done: Focus-freeMeetingCapsulePanelControllerfor detected-call offers, active recording status, call-ended prompt, and ready summary (MeetingCapsulePanelController.swift,StatusBarController.swift).meetings.rolling/meetings.echo/meetings.output— Done: Dual-stream capture, WAV sample-timeline echo suppression with double-talk preservation (f2f273d), rollingtranscript.rolling.ndjsondelta tracking (HEX_EVENT_MEETING_TRANSCRIPT_DELTA), coalesced speaker turns (You/Them), single-threaded Moonshine ONNX Runtime initialization (moonshine::run_sessions_single_threaded()in both CLI andlibhexFFI startup), and Markdown export inlibhex(src/ffi.rs,src/meeting_echo.rs,src/meeting_live.rs,src/meeting_transcript.rs).meetings.rename/meetings.delete/meetings.title.provisional/meetings.notes— Done: FullHexMeetingsPaneworkspace with search, start/stop recording, inline rename, two-step delete (disabled while busy), Apple Intelligence notes card (Summarize / Regenerate / Copy Notes), Copy Transcript, and Reveal Markdown (HexHistoryCommandsMeetingsPanes.swift).meetings.mcp/meetings.mcp.mirror— Done: Preserved via CLI passthrough (hex mcp) inmacos/HexApp/main.swift(src/lib.rs).
Recovery (docs/features/recovery.md)
recover.cancel/recover.shortcut/recover.tap-interruption/recover.microphone/recover.cold-open/recover.late-notification/recover.command-pressure/recover.optional-processing— Done: Enforced bylibhexworkers and surfaced inHexGeneralSettingsPane,HexCommandsPane,HexVoiceActionPane, andHexActivityPane.recover.keyboard-layout— Done:HexAppDelegate.applicationDidFinishLaunchingcallsHexEngineRuntime.initializeKeyboardLayoutOnMainThread()beforehex_engine_new(HexAppDelegate.swift,HexEngineClientTests.swift).
Remaining Cutover Work (Milestone M4)
- Pending M4 — Release Build Script Cutover (
scripts/build-app.sh):scripts/build-app.shstill packages the Rust binary (target/release/hex) rather than runningcargo build --release --lib+xcodebuild -project macos/Hex.xcodeproj -scheme Hex -configuration Release. Until M4 switchesscripts/build-app.sh, nativeHex.appbuilds are produced viaxcodebuildinto~/Library/Caches/hex-native-build/Hex.appand tested locally withscripts/swap-in-native-app.sh. - Partial — Legacy GPUI Preview Flag Parity (
scripts/capture-preview.sh): The native app supports--preview-dictation,HexHudLabPane(live-wired toHexTuning.HUD), andhex dev(HEX_EVENT_DEV_COMMAND_REQUESTED), while the legacy CLIpreview <target>flags still route tosrc/app_window.rsuntil M4 removes GPUI on macOS. - Architectural Simplification — Single
HexMacFeatureTarget (@ObservableStore): Inmacos/HexMacPackage, M0–M3 were implemented in a singleHexMacFeaturemodule using@MainActor @Observable(HexSettingsStore+HexEngineRuntime) rather than splitting into multiple SPM targets or pulling in externalswift-composable-architecture. - Pending M4 — Delete Legacy macOS GPUI Modules: Removing
src/app_window.rs,src/status_item.rs,src/dictation_indicator.rs,src/sparkle.rs,src/meeting_watcher.rs, and gatinggpuitocfg(target_os = "linux")inCargo.tomlis scheduled for Milestone M4 after local testing.