brief-25 [failed-flush-discarded-on-document-switch]
7.8 KiB
Document departure guard — a failed flush aborts navigation
Date: 2026-11-16 Type: fix Reference: brief-25
failed-flush-discarded-on-document-switch(unticketed; argus cycle-4 dedicated bugs pass, finding 1 — evidence~/argus-audits/audit/2026-11-16/museion-bugs.md). Base11b196d68a23d10fdbe474848b9472c523ae0279(main tip re-verified viatea api repos/AlexKucera/museion/branches/mainat claim, 20:03 CET — no drift).
Goal
EditorSession.flush() returns false after a write failure (saveState .failed), but teardown(flushFirst:) discarded the result — so every document departure (New Post, New Outline, piece switch, open external, guest dismissal) tore down the outgoing session even when its flush FAILED, then navigation replaced the only retained copy of the unsaved prose. Structural ops (move/archive/rename/convert) already aborted through flushOrAbort; ordinary departure did not. Make departure report failure and abort navigation while retaining the dirty session — one shared guard at every site, placed before any document-specific state clears.
Site re-derivation (mandate — from root cause, not the seed list)
All production teardown( call sites re-enumerated at the pinned base:
| Site | Location | Verdict |
|---|---|---|
newPost |
PostsModel.swift:271 | exposed — guarded |
newOutline |
PostsModel.swift:287 | exposed — guarded |
dismissExternal |
PostsModel.swift:302 | exposed — guarded (guest half) |
select |
PostsModel.swift:359 | exposed — guarded (session half) |
openExternal |
PostsModel.swift:620 | exposed — guarded |
openUnguarded |
PostsModelOutline.swift:72 | exposed — guarded |
| trash:451, archivePost:550, convert:145, rescan:762 | — | flushFirst: false behind flushOrAbort or a vanished post — correctly unguarded |
Six sites, matching the seed list exactly; no seventh. select's potential second teardown through openUnguarded is covered by gating both members of the pair. No seeded site disproved.
What Was Done
sessionMayDepart(_:)(PostsModel.swift): the departure gate for ONE outgoing session —flushOrAbort's two gates (unanswered #67 conflict holds; failed flush holds) lifted to document departure, message strings byte-identical.flushOrAbortnow routes through it (pure refactor, same contract).dismissExternal() -> Bool: the guest departs only when its flush lands; on failure the guest, its buffer, AND its watcher stay live; returns false so callers abort.departOpenDocuments(): the full-switch composition (guest + post session, each gated then torn downflushFirst: false— the gate already flushed) behindnewPost/newOutline/openExternal.select/openUnguarded: gate the guest unconditionally at head (reselect-as-return still dismisses it, now guarded) and gate their same-id-conditional session teardowns throughsessionMayDepartdirectly.- Disclosed semantic addition: departure now also aborts on an unanswered #67 conflict (mirroring
flushOrAbort). Previously, departing while the alert was up silently overwrote the foreign disk body via the teardown flush — the alert promises the writer chooses. Pinned bynew post aborts while the conflict alert is unanswered(asserts disk NOT overwritten). - Tests (
PostsModelDepartureTests.swift, 6 pins): both navigation families with injected write failures (read-only piece folder / guest directory — atomic writes stage their temp beside the destination; FixCardApplyTests' permission-injection convention), the conflict hold, and retry-after-writability-restored proving the edits land and the switch then completes.
Gates
- RED at base:
-only-testing:MuseionTests/PostsModelDepartureTestsat11b196d+ tests-only commit — 6/6 failed, per-case lines printed,** TEST FAILED **(xcresultTest-Museion-2026.11.16_20-13-27-+0100.xcresult, dd$TMPDIR/b25-dd). - GREEN focused: same filter at the fix tree — 6/6 passed,
** TEST SUCCEEDED **. - Full suite ×2 (repo runner
xcodebuild -project Museion.xcodeproj -scheme Museion -destination 'platform=macOS' test, same dd, post-commit tree250a662): xcresult summaries 699 passed / 1 failed / 0 skipped both runs — the 1 isMuseionUITests-Runnerfailing at INIT ("Timed out while enabling automation mode"), a System Failure node with zero test assertions executed; xcresultsTest-Museion-2026.11.16_20-18-05-+0100.xcresultandTest-Museion-2026.11.16_20-20-18-+0100.xcresult. 699 = 693 trunk executions + 6 new pins. - Environmental wall, disclosed: a third full run (fresh dd) failed the UI runner identically, then the hosted unit runner wedged at session init (log frozen, killed per the wedge protocol;
b25-full-test3.log,b25-unit-clean.logkept).killall -9 testmanagerdbetween runs 1 and 2 did not clear it — the machine's automation mode is wedged machine-wide (reboot-class, the #134 lesson). All hosted/UI test INIT was blocked after ~20:20; every green number above predates the wedge. The UI smoke (single XCUITest, no seam touched by this model-layer diff) is UNVERIFIED on this machine tonight — re-run post-reboot. - swiftformat: 1/3 touched files rewrapped. swiftlint: net zero —
file_length775→809 andtype_body_length327→342 both pre-existing at base; no new rule fires; twoidentifier_nameerrors in the first test draft (a/b) fixed before commit. - gitnexus: impact (owner index
f49e4b6, 20 commits stale — disclosed):dismissExternal/openUnguarded/flushOrAbortCRITICAL,newPostHIGH (transitive closure through the@Observablemodel into the view tree — the direct callers are the enumerated sites above).detect-changeson a fresh worktree index: 3 files / 23 symbols / 15 flows / risk high — exactly the intended symbols, nothing unexpected. The analyze step's root-AGENTS.md gitnexus marker rewrite was reverted (brief-15 lesson).
Decisions & Rationale
| Decision | Rationale |
|---|---|
One gate engine (sessionMayDepart) shared by flushOrAbort and departure |
One contract at the flush-or-abort seam; the coupled home-repoint brief (held behind this merge) rebases onto exactly this "flush-or-abort before committing" shape. |
| Guard placed before ANY document-specific state clears | The failure path must leave selection, draft, session, and watcher byte-identical — the retry is then a clean re-entry. |
departOpenDocuments for the three full switches; primitives under same-id rules in select/openUnguarded |
Reselect-as-return still dismisses the guest (existing #92 contract) but must not gate a session that is not departing. |
| Conflict-alert abort included (not just write failure) | Mirrors flushOrAbort; without it, departure resolves an unanswered #67 by silently overwriting the foreign body — the exact class of loss the guard exists to prevent. |
Teardown flags flipped to flushFirst: false behind the gate |
flushOrAbort precedent comment ("already flushed"); avoids a redundant second flush attempt. |
| Guest watcher stays live on a held guest | The retained guest must keep its #67 disk-change check — it is still the open document. |
Docs
Museion/AGENTS.md PostsModel bullet carries two clause edits (guest held by the departure gate; document departure rides sessionMayDepart). AGENTS.md writes are consent-gated in this environment — shipped as an owner-apply patch beside the handoff: ~/daedalus-builds/museion/brief-25-agents-md-owner-apply.patch (git apply verified CLEAN against the branch). PR body to carry: git apply brief-25-agents-md-owner-apply.patch.
Coupling
museion/home-repoint-retains-old-editor-target is HELD behind this PR's merge and rebases onto this branch — sessionMayDepart / departOpenDocuments are its departure seam.