# #200 — Gate quit-time flushes through the departure gate > **Date:** 2026-11-19 > **Type:** issue > **Reference:** AlexKucera/museion#200 (follow-up from the PR #198 finish audit) ## Goal Quitting was the one document departure that bypassed the brief-25 gate: `PostsModel.shutdown()` flushed both sessions best-effort and ignored the results. Two harms: quitting with an unanswered #67 conflict wrote local text over the foreign disk body (`flush()` holds for conflicts only in the debounce, not itself — the #114 harm), and a failed quit-time flush silently dropped the only copy of the edits. Deliver the conflict hold at quit, plus an explicit pinned product decision for the failed-flush-at-quit posture. ## What Was Done - `PostsModel.shutdown()` (PostsModel.swift) now routes both sessions through the departure gate via `documentsMayDepart()` and is `void` — the verdict channel is `quitReply`'s, at `applicationShouldTerminate` (finish-audit ponytail finding: a `-> Bool` shutdown had no production reader). - `documentsMayDepart()` (PostsModelWrites.swift, beside `sessionMayDepart` / `departOpenDocuments`): the gate's quit run — external guest then post session, both through `sessionMayDepart`, no early exit (a clean sibling still saves while a conflicted one holds). - BLOCK-QUIT at `WritingLockAppDelegate.applicationShouldTerminate` (WritingLockState.swift): new pure seam `quitReply(posts:)` (nil model passes; the pins inject here) runs BEFORE the Cold Turkey lock ask — never walk away from a block the quit then refuses to complete. The refusal speaks once via a hosted-guarded alert (startLaunchCheck/kiosk convention) naming `lastError`. - `PostsModel.active` weak static (the `WritingLockState.active` reachability precedent), registered ONLY by `AppGateState.init` (MuseionApp.swift) — bare test rigs never register, so parallel suites can't flake the delegate through the static. - Finish-audit fixes: `sessionMayDepart` refuses a never-loaded session's retyped words (`loadFailed && saveState == .changed` — its `flush()` is a vacuous success; the security reviewer's LOW data-loss gap, closed for quit AND navigation at the one shared seam); guest quit-flush pin added (the guest leg of `documentsMayDepart` was unpinned — deleting it kept the rail green). - Pins (5 total): conflict hold at shutdown (foreign body on disk, byte unchanged), clean dirty session flushes, guest flushes, never-loaded refusal (PostsModelDepartureTests), delegate refusal/proceed across all phases incl. chmod-injected flush failure (WritingLockTests). - DOX: Museion/AGENTS.md (WritingLock delegate bullet, PostsModel bullet, PostsModelWrites bullet, Wiring bullet), MuseionTests/AGENTS.md suite map (also added the PR #198 gap: PostsModelDepartureTests had never been listed). ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | BLOCK QUIT (vs silently drop / rescue file) | The ticket's three options. Drop keeps the audited harm; rescue strands a buffer in a recovery file with no discovery UX. Block quit reuses both existing seams (`sessionMayDepart` + the `applicationShouldTerminate` refusal precedent) and is the only posture that never silently destroys the only copy — "edits land or the departure aborts" extended to termination. | | `quitReply(posts:)` injectable pure seam, static written only by `AppGateState.init` | The delegate needs model reachability (`WritingLockState.active` precedent), but a static written by every `PostsModel` init would let parallel test rigs flake the delegate pin. Registration at the real construction site keeps hosted tests deterministic; pins inject the model directly. | | Gate before the Cold Turkey ask | Refusing AFTER "walk away" would strand a walked-away block in a still-running app. | | Never-loaded guard in `sessionMayDepart` (not in `flush()` or callers) | The vacuous success is `flush()`'s correct safety rule (a failed read must never become a destructive write); the gap is the gate trusting it. One guard at the shared seam closes quit AND navigation. | | `shutdown()` void, willTerminate stays as belt | The belt double-flush is idempotent (clean no-op); the Bool return had no production reader. | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | Flip cycle: the guest pin passed with its guard "off" | The `, false` suffix on `if let guest = externalSession, !sessionMayDepart(guest), false` still CALLS `sessionMayDepart` — the flush is a side effect inside the condition, so the pin's observable survived; only the verdict assignment was skipped | Flip by deleting the block, which models the actual threat ("the leg is gone"); then the pin failed by name. Restore re-inserted the block byte-exact. | | `git log` piped output hid merge commits / phantom staged file | The rtk terminal wrapper mangles piped git output (known trap) | Settled facts with `git show -s --format`, `git diff --cached --name-only` direct reads, and count-only greps. | ## Files Changed | File | Change Summary | |------|---------------| | `Museion/PostsModel.swift` | `shutdown()` void + routes through the gate; `PostsModel.active` weak static | | `Museion/PostsModelWrites.swift` | `documentsMayDepart()`; never-loaded guard in `sessionMayDepart` | | `Museion/WritingLockState.swift` | `quitReply(posts:)` pure seam; gate-first quit; hosted-guarded refusal alert | | `Museion/MuseionApp.swift` | `AppGateState.init` registers `PostsModel.active` | | `MuseionTests/PostsModelDepartureTests.swift` | 5 quit-time pins (conflict, clean, guest, never-loaded + navigation family) | | `MuseionTests/WritingLockTests.swift` | Delegate pin: refuses while a piece can't depart, proceeds once it can | | `Museion/AGENTS.md`, `MuseionTests/AGENTS.md`, `AGENTS.md` | DOX pass (quit-gate contracts, suite map) | ## Open Items & Next Steps - [ ] #201 (consented brief-25 AGENTS.md sentences) — open, promoted, next in line. - [ ] Follow-ups from PR #198's audit are otherwise exhausted. ## Verification - Full verify rail (`./scripts/full-test.sh`): **708 executed / 708 passed / 0 failed** (706 pre-audit + the 2 audit pins), xcodebuild exit 0. - swiftformat 0/150 reformatted; swiftlint 0 violations in 139 files. - Finish-audit flips: both new pins fail by name on their guards (loadFailed `, false` suffix; guest-leg block deletion), 8 siblings green, restored byte-exact, 28/0 on both suites post-restore. - GitNexus: analyze 7,134 nodes; detect-changes HIGH = the gate's deliberate fan-in (every departure flow), all affected flows covered by the rail. --- *Log written by write-log skill*