Skip to content

FC-5: compiler-enforced voice-turn ownership — extract VoiceTurnDomain target, end the convergence churn #9832

Closed

Description

@Git-on-my-level

Problem

The desktop voice-turn subsystem has re-litigated the same "split mutation authority" failure class at least seven times in one week: #9597 (the 100-file "atomic convergence" closing #9515) → #9660 → #9698 → #9734 (regressed same-day, fixed by #9748) → #9770 (re-closes #9081, already closed once by #9390) → #9775. Each PR claims to close the class with a "durable guard"; each is followed within 1–3 days by a new split-authority variant. Churn evidence: RealtimeHubController.swift touched 121 times in a month, ~15 "ground/harden/fence/enforce" commits in one 48-hour stretch. This is registry class FC-5 (docs/product/failure-classes.md, #9822).

Root cause of the recurrence (not of any individual bug): #9597 converged state behind VoiceTurnCoordinator/VoiceTurnReducer, but the whole app is one SwiftPM module (Desktop/Package.swift, target Omi Computer), so nothing prevents any source file from constructing and sending reducer events directly — there are 62 production .send call sites across 7 files today, plus caller-side guards that act as de facto turn authorities (#9748's regression was exactly this: an availability guard vetoing escalation from outside the owner). Human inventory of these sites has failed five times; visibility can't help while everything is internal to one module.

Fix: compiler-enforced ownership via a domain target, one PR

1. Extract a VoiceTurnDomain library target.
Move the reducer, state model, and event types into a new SwiftPM library target. Events and mutating APIs stay internal to that target. The app target gets exactly two surfaces: typed fact-publishing ports for drivers, and read-only state projections. After this, "who can mutate turn state" is answered by the compiler: illegal event construction outside the domain target does not build. The compile-error list from flipping the visibility is the exhaustive syntactic mutation-site inventory that five human passes couldn't produce.

What the compiler proves: no code outside the domain target constructs or sends lifecycle events. What it cannot prove: a driver hiding a semantic veto behind its fact-publishing (the #9748 class). That is closed by the contract rule below (facts vs decisions) plus behavioral tests — stated honestly, not hand-waved as "unrepresentable."

2. Facts vs decisions contract.
Drivers (PTT capture, hub transport, screen evidence, playback) publish typed facts ("silence detected", "transport ready", "evidence receipt arrived"); only the domain owner converts facts into lifecycle decisions. No driver-side conditional may gate whether a fact is published — availability, permission, and usage-limit logic lives in the owner's transition rules where it is testable in isolation.

3. Remove the decision surface from RealtimeHubController — keep the transport driver.
The controller is not deleted: it legitimately owns the provider socket, warm/reconnect, callback correlation, PCM playback, and the journal retry ledger. What goes: its lifecycle decision API and derived route booleans — the facade surface kept in #9597 so call sites wouldn't have to change. PushToTalkManager's ~23 hub calls migrate to domain ports or read-only projections. No shims, no deprecated aliases (policy below).

4. Authority table (the contract doc, same PR).
The week's PRs show "voice turn" is several adjacent authorities; each convergence answered one and exposed the next. The contract doc (docs/product/invariants/desktop-voice-turns.md, as a design note — it locks as an invariant only after 7 quiet days, tracked in the FC registry per #9822) distinguishes logical decision owner (always the domain reducer) from physical side-effect owners:

Authority Logical owner Physical owner
Turn lifecycle (begin/commit/cancel) domain reducer —
Escalation domain reducer (typed transition, never a caller guard) —
Audio capture, device, mic generation reducer arbitrates PushToTalkManager / AudioCaptureService
Provider session, warm/reconnect, correlation reducer arbitrates leases RealtimeHubController / session
Playback (native PCM vs selected voice) reducer owns leases hub PCM player / FloatingBarVoicePlaybackService
Screen evidence reducer owns token + deadline hub owns bytes/encoding/receipts
Journal reducer owns completion fence kernel journal (durable), hub retry ledger (in-process)

5. Correlation audit on the provider edge (not new stamps).
The turn-affecting wire is Swift↔OpenAI/Gemini, not Swift↔Rust (Rust only mints session tokens and receives usage pings — out of scope). The staleness fences already exist in three domains (VoiceEffectIdentity.generation, RealtimeHubEventIdentity turn+response correlation, hub-local generations); this PR audits and behaviorally tests each rather than collapsing them into one generic stamp, and the contract doc enumerates which fence guards which race. No provider-schema changes.

6. Test seam.
The domain owner is constructable with an injectable clock (falls out of the target split). Behavioral tests drive legal transitions and prove escalation/veto is impossible except via owner rules — the #9748 regression class. The source-scrape tripwires (PTTAudioCaptureRaceTests, PTTSilentTurnRecoveryWiringTests) are kept until PTT has an injectable capture/diagnostics driver — a constructable reducer cannot prove CoreAudio recovery wiring; deleting them now would drop real coverage. Their retirement is a named follow-up gated on that driver seam.

Doc changes (same PR — these generalize beyond FC-5)

Two new cross-component rules in AGENTS.md (two bullets, per the lean budget):

  • Never preserve compatibility for in-repo consumers. When refactoring code whose only consumers live in this repo, change the consumers in the same PR — no facades, shims, deprecated aliases, or pass-throughs. Compatibility layers are only for real boundaries — public APIs, wire protocols, persisted formats, cross-process schemas, hardware/rollout timing — or when explicitly requested. If a temporary adapter is unavoidable, it carries a LIFECYCLE: one-time + DELETE-AFTER header (existing check_lifecycle_headers.py enforces removal). Rationale: Converge desktop agent, voice, and journal ownership #9597 kept a facade over internal code and hid the exact call sites the refactor needed to find; FC-5 recurred five times as a result.
  • Compiler-first invariants. Prefer encoding an invariant in types and visibility (separate targets/modules, opaque types, exhaustive enums, strict typing) over runtime checks, tests, or CI greps. A check catches a violation after it's written; the compiler makes it unwritable. Note the limit honestly: visibility closes syntactic classes; semantic contracts still need a behavioral test. (Recent proof: refactor(desktop-backend): delete dormant data plane #9542's visibility narrowing; backend strict typing.)

Proof

Cut list

  • RealtimeHubController transport internals stay put — only its decision surface moves. No 5k-line relocation.
  • No Rust wire changes; Rust session-mint/usage is out of scope.
  • No provider (OpenAI/Gemini) schema changes.
  • No BLE (refactor(desktop): converge Bluetooth session ownership #9550), chat routing, or chat_completions.rs changes (separate issue).
  • Source-scrape test deletion deferred to the injectable-capture-driver follow-up.
  • No broader singleton eradication — only the voice-turn domain gets the constructable seam here.
  • No behavior changes to turn semantics (timings, escalation rules) — this relocates authority, it does not redesign it.

Sizing

One PR, large but compile-bounded: new SwiftPM target, contract doc, decision-surface extraction from the hub, migration of 62 production .send sites across 7 files (+ test migrations off @testable reducer internals where they construct events directly), correlation-fence tests, two AGENTS.md bullets. Missed mutation sites don't compile, so the diff cannot silently under-count.

Activity

  1. added a commit that references this issue on Jul 16, 2026
    79bdd79
  2. added a commit that references this issue on Sep 29, 2026
    f72d2cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions