fix(editor): guard document departure on failed flushes [museion/failed-flush-discarded-on-document-switch] #198

Merged
AlexKucera merged 4 commits from fix/failed-flush-discarded-on-document-switch into main 2026-11-19 09:45:31 +01:00
Owner

Finding — museion/failed-flush-discarded-on-document-switch

EditorSession.flush() returns false after a write failure (saveState .failed), but document-departure paths called teardown(flushFirst:) and discarded the result — the outgoing session was torn down even when its flush FAILED, and the navigation then replaced the only retained copy of the unsaved prose. Structural ops (move/archive/rename/convert) already aborted through flushOrAbort; ordinary departure did not.

Evidence at base 11b196d (file:line in the pre-split PostsModel.swift; symbols relocated by trunk's #193, see integration below): newPost :271, newOutline :287, dismissExternal :302, select :359, openExternal :620 (PostsModel.swift); openUnguarded PostsModelOutline.swift :72 — each tore down unconditionally. Finding source: argus cycle-4 dedicated bugs pass, finding 1 (~/argus-audits/audit/2026-11-16/museion-bugs.md).

Fix

  • sessionMayDepart(_:) — 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. flushOrAbort 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 before clearing anything.
  • departOpenDocuments() — the full-switch composition (guest + post session, each gated, then teardown(flushFirst: false) — the gate already flushed) behind newPost / newOutline / openExternal.
  • select / openUnguarded — gate the guest unconditionally at head (reselect-as-return still dismisses it, now guarded); gate their same-id-conditional session teardowns through sessionMayDepart.
  • Tests (MuseionTests/PostsModelDepartureTests.swift, 6 pins): both navigation families (saved-post session, external guest) with injected write failures (read-only piece folder / read-only guest directory; FixCardApplyTests' permission-injection convention), the #67 conflict hold, and retry-after-writability-restored proving the edits land and the switch then completes.

Site re-derivation (implementer, from root cause at the pinned base): all production teardown( call sites re-enumerated — the six exposed sites above, matching the seed list exactly, no seventh; trash/archivePost/convert/rescan are flushFirst: false behind flushOrAbort or a vanished post (correctly unguarded). Reviewer's independent rail-wide grep at the integrated head: every dismissExternal caller guards; zero unguarded departure sites remain.

Disclosed semantic addition (in-scope by pre-committed brief scope), verbatim from the devlog

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 by new post aborts while the conflict alert is unanswered (asserts disk NOT overwritten).

Trunk integration (#193 PostsModel split) — this PR is the INTEGRATED head

Trunk moved after the branch was cut (main 11b196d → bb4bfa2): #193 split PostsModel.swift into Watcher/Writes/Imports files — the exact file this fix modified. Per repo precedent (7cd5e69), current main was merged into the branch. PR head = ffb9457 (merge of 9497661 + bb4bfa2), pushed. Branch commits: ecd14d1 RED tests → 250a662 fix → 9497661 devlog → ffb9457 integration merge.

Hand-resolved conflict files (sentinel + both-parents checks passed on each):

  • Museion/PostsModel.swift — both conflicts took main's deletions; the fix's guarded bodies relocated with the split: dismissExternal + openExternal → PostsModelImports.swift; flushOrAbort + sessionMayDepart + departOpenDocuments → PostsModelWrites.swift (original adjacency preserved). newPost/newOutline/select auto-merged with guards intact.
  • docs/devlog/INDEX.md — chronological union of trunk's four 2026-11-17 rows and this branch's 2026-11-16 row.

Post-integration sentinels, all verified at ffb9457: sessionMayDepart gate engine (PostsModelWrites.swift:146); departOpenDocuments behind newPost (PostsModel.swift:179) / newOutline (:193) / openExternal (PostsModelImports.swift:47); dismissExternal() -> Bool failure-return contract (PostsModelImports.swift:23); guarded select (PostsModel.swift:245, 256) + openUnguarded (PostsModelOutline.swift:69, 73); 6 test pins present.

Mergeability: git merge-tree --write-tree clean vs main@bb4bfa2 (moment-scoped to that SHA — re-probe if trunk moves in this seam before merge).

Verify

Evidence classes: "reviewer" = this seat's own runs on the integrated head ffb9457 tonight (2026-11-17); "implementer" = pre-integration runs at the fix tree 250a662, quoted, non-authoritative.

  • RED at base tree (reviewer): ecd14d1 (= 11b196d + tests-only commit), focused -only-testing:MuseionTests/PostsModelDepartureTests — 6 failed / 0 passed, ** TEST FAILED **. The pins bite a live defect.
  • Focused at integrated head (reviewer): same filter at ffb9457 — 6/6 passed, 0 failed, ** TEST SUCCEEDED ** (xcresult Test-Museion-2026.11.17_20-15-19-+0100.xcresult: totalTestCount 6, passed 6, failed 0, skipped 0).
  • Full suite at integrated head (reviewer): xcodebuild -project Museion.xcodeproj -scheme Museion -destination 'platform=macOS' test at ffb9457 — 699 passed / 1 failed / 0 skipped (xcresult Test-Museion-2026.11.17_20-15-35-+0100.xcresult: totalTestCount 700, passedTests 699, failedTests 1, skippedTests 0). The 1 is the MuseionUITests-Runner bundle failing at INIT ("Timed out while enabling automation mode", zero assertions executed); zero unit test-case failures (699 pass lines counted in the run log). 699 = trunk's 693 + 6 new pins — the implementer's full-suite number reproduces exactly at the integrated head.
  • swiftformat --lint at ffb9457 (reviewer): 0/150 files require formatting.
  • swiftlint at ffb9457 (reviewer): 0 violations, 0 serious in 139 files — trunk's repo-wide-green contract (#197) holds over the integrated tree.
  • Implementer runs at the fix tree (quoted): GREEN focused 6/6; full suite ×2 699 passed / 1 failed / 0 skipped — the 1 = MuseionUITests-Runner failing at INIT ("Timed out while enabling automation mode"), a System Failure node, zero assertions executed; 699 = 693 trunk executions + 6 new pins.

Unverified — keyed by slug

  • museion/failed-flush-discarded-on-document-switch — UI smoke: UNVERIFIED on this machine. Implementer's environmental wall, verbatim from the devlog:

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.log kept). killall -9 testmanagerd between 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.

Reviewer re-run at ffb9457 (Tuesday 20:16 CET) hits the identical runner-INIT wedge — recorded as the disclosed environmental wall (machine not rebooted since Monday night), not a new finding. The unit and focused numbers above all reproduce; the post-reboot UI smoke remains owed and is the only outstanding verification item for this PR.

AGENTS.md owner-apply patch (consent-gated)

git apply brief-25-agents-md-owner-apply.patch

Status (both seats verified at ffb9457): the original patch — built against the pre-#193 AGENTS.md text — went stale at the integrated head (git apply --check failed at Museion/AGENTS.md:36; trunk's #193 rewrote the same PostsModel bullet and added three split-file bullets). The implementer regenerated it the same night (round 1, 2026-11-17 20:22): consented sentences extracted byte-verbatim from the old patch, woven into the PostsModelImports/PostsModelWrites bullets, plus two accuracy extensions (dismissExternal (-> Bool — false holds the departure); gates named sessionMayDepart/departOpenDocuments), one disclosed 1-char paren deviation (self-contained "(brief-25)."), and an index line so --3way can rescue future drift. Reviewer independently at ffb9457: git apply --check exit 0; git apply --check --3way exit 0 ("Applied patch to 'Museion/AGENTS.md' cleanly"); sentence content verified in the patch lines; review clone clean. Moment-scoped to ffb9457 — if main rewrites those bullets before you apply, use the 3-way fallback or request a regen.

Scope

  • In-scope per the pre-committed criteria: the departure seam + the six sites + the disclosed #67 conflict-hold semantic addition (pinned by its own test). Nothing beyond; no out-of-brief sites found (scope appendix: empty).
  • Review rounds: round 1 (2026-11-17, same night) — one must-fix handoff artifact (owner-apply patch stale at the integrated head) → resolved same round (regenerated patch, both seats verified above); one non-blocking nit (departOpenDocuments doc comment overclaims "retains BOTH" — guest and post session are mutually exclusive by construction) → accepted by the implementer, deferred to the next PostsModelWrites.swift touch so the branch head stays frozen at ffb9457 and the verify numbers stay head-pinned. Zero branch-code findings. Converged in 1 round.
  • Coupling: museion/home-repoint-retains-old-editor-target is HELD behind this PR's merge and rebases onto this branch's merged state (sessionMayDepart / departOpenDocuments are its departure seam). This PR's merge-clean state at main@bb4bfa2 is that brief's release gate.

[Generated by AI]

## Finding — museion/failed-flush-discarded-on-document-switch `EditorSession.flush()` returns false after a write failure (saveState `.failed`), but document-departure paths called `teardown(flushFirst:)` and discarded the result — the outgoing session was torn down even when its flush FAILED, and the navigation then replaced the only retained copy of the unsaved prose. Structural ops (move/archive/rename/convert) already aborted through `flushOrAbort`; ordinary departure did not. Evidence at base `11b196d` (file:line in the pre-split PostsModel.swift; symbols relocated by trunk's #193, see integration below): `newPost` :271, `newOutline` :287, `dismissExternal` :302, `select` :359, `openExternal` :620 (PostsModel.swift); `openUnguarded` PostsModelOutline.swift :72 — each tore down unconditionally. Finding source: argus cycle-4 dedicated bugs pass, finding 1 (`~/argus-audits/audit/2026-11-16/museion-bugs.md`). ## Fix - `sessionMayDepart(_:)` — 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. `flushOrAbort` 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 before clearing anything. - `departOpenDocuments()` — the full-switch composition (guest + post session, each gated, then `teardown(flushFirst: false)` — the gate already flushed) behind `newPost` / `newOutline` / `openExternal`. - `select` / `openUnguarded` — gate the guest unconditionally at head (reselect-as-return still dismisses it, now guarded); gate their same-id-conditional session teardowns through `sessionMayDepart`. - Tests (`MuseionTests/PostsModelDepartureTests.swift`, 6 pins): both navigation families (saved-post session, external guest) with injected write failures (read-only piece folder / read-only guest directory; FixCardApplyTests' permission-injection convention), the #67 conflict hold, and retry-after-writability-restored proving the edits land and the switch then completes. Site re-derivation (implementer, from root cause at the pinned base): all production `teardown(` call sites re-enumerated — the six exposed sites above, matching the seed list exactly, no seventh; trash/archivePost/convert/rescan are `flushFirst: false` behind `flushOrAbort` or a vanished post (correctly unguarded). Reviewer's independent rail-wide grep at the integrated head: every `dismissExternal` caller guards; zero unguarded departure sites remain. ### Disclosed semantic addition (in-scope by pre-committed brief scope), verbatim from the devlog > **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 by `new post aborts while the conflict alert is unanswered` (asserts disk NOT overwritten). ## Trunk integration (#193 PostsModel split) — this PR is the INTEGRATED head Trunk moved after the branch was cut (`main` 11b196d → bb4bfa2): #193 split PostsModel.swift into Watcher/Writes/Imports files — the exact file this fix modified. Per repo precedent (7cd5e69), current `main` was merged into the branch. PR head = `ffb9457` (merge of `9497661` + `bb4bfa2`), pushed. Branch commits: `ecd14d1` RED tests → `250a662` fix → `9497661` devlog → `ffb9457` integration merge. Hand-resolved conflict files (sentinel + both-parents checks passed on each): - `Museion/PostsModel.swift` — both conflicts took main's deletions; the fix's guarded bodies relocated with the split: `dismissExternal` + `openExternal` → PostsModelImports.swift; `flushOrAbort` + `sessionMayDepart` + `departOpenDocuments` → PostsModelWrites.swift (original adjacency preserved). `newPost`/`newOutline`/`select` auto-merged with guards intact. - `docs/devlog/INDEX.md` — chronological union of trunk's four 2026-11-17 rows and this branch's 2026-11-16 row. Post-integration sentinels, all verified at `ffb9457`: `sessionMayDepart` gate engine (PostsModelWrites.swift:146); `departOpenDocuments` behind newPost (PostsModel.swift:179) / newOutline (:193) / openExternal (PostsModelImports.swift:47); `dismissExternal() -> Bool` failure-return contract (PostsModelImports.swift:23); guarded `select` (PostsModel.swift:245, 256) + `openUnguarded` (PostsModelOutline.swift:69, 73); 6 test pins present. Mergeability: `git merge-tree --write-tree` clean vs `main@bb4bfa2` (moment-scoped to that SHA — re-probe if trunk moves in this seam before merge). ## Verify Evidence classes: "reviewer" = this seat's own runs on the integrated head `ffb9457` tonight (2026-11-17); "implementer" = pre-integration runs at the fix tree `250a662`, quoted, non-authoritative. - RED at base tree (reviewer): `ecd14d1` (= `11b196d` + tests-only commit), focused `-only-testing:MuseionTests/PostsModelDepartureTests` — **6 failed / 0 passed**, `** TEST FAILED **`. The pins bite a live defect. - Focused at integrated head (reviewer): same filter at `ffb9457` — **6/6 passed, 0 failed**, `** TEST SUCCEEDED **` (xcresult `Test-Museion-2026.11.17_20-15-19-+0100.xcresult`: totalTestCount 6, passed 6, failed 0, skipped 0). - Full suite at integrated head (reviewer): `xcodebuild -project Museion.xcodeproj -scheme Museion -destination 'platform=macOS' test` at `ffb9457` — **699 passed / 1 failed / 0 skipped** (xcresult `Test-Museion-2026.11.17_20-15-35-+0100.xcresult`: totalTestCount 700, passedTests 699, failedTests 1, skippedTests 0). The 1 is the `MuseionUITests-Runner` bundle failing at INIT ("Timed out while enabling automation mode", zero assertions executed); zero unit test-case failures (699 pass lines counted in the run log). 699 = trunk's 693 + 6 new pins — the implementer's full-suite number reproduces exactly at the integrated head. - swiftformat --lint at `ffb9457` (reviewer): 0/150 files require formatting. - swiftlint at `ffb9457` (reviewer): 0 violations, 0 serious in 139 files — trunk's repo-wide-green contract (#197) holds over the integrated tree. - Implementer runs at the fix tree (quoted): GREEN focused 6/6; full suite ×2 **699 passed / 1 failed / 0 skipped** — the 1 = `MuseionUITests-Runner` failing at INIT ("Timed out while enabling automation mode"), a System Failure node, zero assertions executed; 699 = 693 trunk executions + 6 new pins. ## Unverified — keyed by slug - `museion/failed-flush-discarded-on-document-switch` — UI smoke: UNVERIFIED on this machine. Implementer's environmental wall, verbatim from the devlog: > **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.log` kept). `killall -9 testmanagerd` between 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. Reviewer re-run at `ffb9457` (Tuesday 20:16 CET) hits the identical runner-INIT wedge — recorded as the disclosed environmental wall (machine not rebooted since Monday night), not a new finding. The unit and focused numbers above all reproduce; the post-reboot UI smoke remains owed and is the only outstanding verification item for this PR. ## AGENTS.md owner-apply patch (consent-gated) `git apply brief-25-agents-md-owner-apply.patch` Status (both seats verified at `ffb9457`): the original patch — built against the pre-#193 AGENTS.md text — went stale at the integrated head (`git apply --check` failed at `Museion/AGENTS.md:36`; trunk's #193 rewrote the same PostsModel bullet and added three split-file bullets). The implementer regenerated it the same night (round 1, 2026-11-17 20:22): consented sentences extracted byte-verbatim from the old patch, woven into the PostsModelImports/PostsModelWrites bullets, plus two accuracy extensions (`dismissExternal` (`-> Bool` — false holds the departure); gates named `sessionMayDepart`/`departOpenDocuments`), one disclosed 1-char paren deviation (self-contained "(brief-25)."), and an index line so `--3way` can rescue future drift. Reviewer independently at `ffb9457`: `git apply --check` exit 0; `git apply --check --3way` exit 0 ("Applied patch to 'Museion/AGENTS.md' cleanly"); sentence content verified in the patch lines; review clone clean. Moment-scoped to `ffb9457` — if `main` rewrites those bullets before you apply, use the 3-way fallback or request a regen. ## Scope - In-scope per the pre-committed criteria: the departure seam + the six sites + the disclosed #67 conflict-hold semantic addition (pinned by its own test). Nothing beyond; no out-of-brief sites found (scope appendix: empty). - Review rounds: round 1 (2026-11-17, same night) — one must-fix handoff artifact (owner-apply patch stale at the integrated head) → resolved same round (regenerated patch, both seats verified above); one non-blocking nit (departOpenDocuments doc comment overclaims "retains BOTH" — guest and post session are mutually exclusive by construction) → accepted by the implementer, deferred to the next PostsModelWrites.swift touch so the branch head stays frozen at `ffb9457` and the verify numbers stay head-pinned. Zero branch-code findings. Converged in 1 round. - Coupling: `museion/home-repoint-retains-old-editor-target` is HELD behind this PR's merge and rebases onto this branch's merged state (`sessionMayDepart` / `departOpenDocuments` are its departure seam). This PR's merge-clean state at `main@bb4bfa2` is that brief's release gate. [Generated by AI]
AlexKucera added 4 commits 2026-11-17 20:17:54 +01:00
RED at pinned base 11b196d: all six PostsModelDepartureTests pins fail.
Document departure (newPost, newOutline, select, openExternal,
dismissExternal, openUnguarded) tears the outgoing session down with
teardown(flushFirst: true), discards the flush result, and replaces the
only retained copy of unsaved prose when the write fails. Both
navigation families (saved-post session, external guest) pinned with
injected write failures (read-only piece folder / guest directory), plus
the unanswered #67 conflict hold. Retry after writability is restored
proves the edits land and the switch then completes.

brief-25 [failed-flush-discarded-on-document-switch]
teardown(flushFirst:) discarded flush()'s result, so document
navigation (newPost, newOutline, select, openExternal, dismissExternal,
openUnguarded) dropped the outgoing dirty session after a failed write
and replaced the only retained copy of the unsaved prose.

One shared departure gate now covers every site, placed before any
document-specific state clears:

- sessionMayDepart(_:): flushOrAbort's two gates (unanswered #67
  conflict holds; failed flush holds) lifted to document departure —
  flushOrAbort routes through it unchanged.
- dismissExternal() -> Bool: the guest departs only when its flush
  lands; on failure the guest, its buffer, and its watcher stay live.
- departOpenDocuments(): the full-switch composition (guest + post
  session) behind newPost/newOutline/openExternal.
- select/openUnguarded gate their same-id-conditional session teardowns
  through sessionMayDepart directly.

Departure now aborts with the strip error set and the dirty session
retained; retrying after writability returns lands the edits and
completes the switch. All six PostsModelDepartureTests pins RED at
11b196d, GREEN with this change.

brief-25 [failed-flush-discarded-on-document-switch]
docs(devlog): brief-25 session log + INDEX row
CI / ci (push) Canceled after 0s
9497661145
brief-25 [failed-flush-discarded-on-document-switch]
integrate main: #193 PostsModel split (Watcher/Writes/Imports) into failed-flush departure guard
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s
ffb9457a63
# Conflicts:
#	Museion/PostsModel.swift
#	docs/devlog/INDEX.md
Author
Owner

/review

/review
Collaborator

Failed to review PR

Failed to review PR
AlexKucera added a new dependency 2026-11-19 09:20:42 +01:00
AlexKucera added a new dependency 2026-11-19 09:20:42 +01:00
AlexKucera merged commit b2d01c852a into main 2026-11-19 09:45:31 +01:00
Sign in to join this conversation.