Refactor EditorPane and SaveJournal lifecycles into explicit state machines
Summary
Context: EditorPane and SaveJournal previously tracked document loading, dirty edits, non-conflict save errors, CAS save conflicts, deferred remote revisions, remote deletion, and attachment imports across loose boolean and Option flags. That allowed contradictory states such as non-conflict save errors colliding with CAS conflict resolution (SaveJournal::resolve_conflict returning Busy), early document watch events getting stranded across Loading -> Loaded, stale use_external resolutions applying over newer remote revisions, unguarded IME unmark_text publishing from clean or read-only editors, SaveJournal::flush returning early on pending revision-0 drafts, and unguarded attachment callbacks surviving read-only transitions or vault switches. Clean external reloads also surfaced an intrusive undo-history-reset notice banner.
Intention: Make editor synchronization, conflict recovery, persistence phases, and attachment import lifecycles explicit so invalid combinations are unrepresentable, and apply clean external reloads silently without showing the undo-history-reset banner.
Direction:
- Replace
EditorPane's loose sync/loading/attachment flags with explicit typed state machines (DocumentLifecycle,SyncState,ConflictRemote,PendingWatchEvent, andAttachmentImportState). - Replace
SaveJournal's loose pending/saving/error/retry flags with an explicitJournalPhasestate machine (Clean,Pending,Saving,Failed,Conflicted) andFlushWaitertracking soflushwaits for pending revision-0 drafts and non-conflict failures stay separate from CASConflict. - Reconcile early document watch events across
Loading -> Loadedwithout stranding revisions or clearing remote-deletion notices, guarduse_externalagainst newer external revisions, clear conflict/error state atomically during resolution, guard IMEunmark_texton clean or read-only editors, and guard attachment paste/choose/drop and async preview/backup/open callbacks against read-only state, document mutations, and vault switches. - Apply clean external reloads silently instead of displaying the
"Remote revision loaded. Undo history was reset to protect remote edits."banner, and use a 20s wall-clock deadline ineditor::pane_tests::settle.
Demo
N/A — internal state-machine refactor and removal of the clean-reload undo-reset banner, verified via GPUI editor and persistence tests.
Verification
- Local (macOS,
CARGO_INCREMENTAL=0, head721ad0e57arebased ontomainf87d56223c):cargo fmt --check(fromapps/desktop-gpui): passedcargo test --locked -p desktop-gpui --lib editor::: passed (165 passed, 1 ignored; an initial run hit a 20ssettletimeout inlink_editor_defers_remote_reload_and_modifier_click_hits_last_glyph_and_hard_break_paintsunder heavy concurrent load, which then passed in the fulleditor::suite and 5/5 isolated reruns)cargo test --locked -p desktop-gpui --lib workspace::open_note: passed (41 passed)cargo test --locked -p desktop-gpui --lib workspace::undo_delete: passed (9 passed)cargo clippy --locked --workspace --all-targets --no-deps -- -D warnings: passed (0 warnings)dprint check --allow-no-fileson all 5 changed files (repo-pinneddprint 0.51.1and systemdprint 0.64.3): passedpython3 .github/scripts/test_check_license_boundary.py,python3 .github/scripts/check_license_boundary.py, andpython3 -m unittest discover -s apps/desktop-gpui/packaging: passed
- CI: pending on GitHub Actions (macOS/Linux GPUI and repo checks).
Summary by cubic
Refactors EditorPane and SaveJournal so document loading, sync, persistence, conflict, and attachment import lifecycles are modeled as explicit typed state machines instead of loose boolean and Option flags. Contradictory states such as non-conflict save errors colliding with CAS conflict resolution are now unrepresentable, and clean external reloads apply silently instead of showing the undo-history-reset banner.
- Enforces ordering by stamping each
DocumentSnapshot— saves and watch events alike — with a monotonic runtime observation sequence in the serialized database worker, and gatesremote_revision,handle_save_event,load_clean_revision, andrebase_if_cleanon revisions so stale or delayed snapshots never overwrite newer saves. Deferred revisions are cleared on revert, which coalesces watch events. - Reconciles early watch events across the
Loading -> Loadedtransition without stranding revisions or clearing remote-deletion notices, routes restored notes and unrelated document writes through the watch stream with deduplication, and syncs focus state before save events. - Retries first-save conflicts that arrive before a stored revision exists, validates the merge ancestor before applying a merged revision, and preserves pending IME edits on restore.
- Guards IME
unmark_texton clean or read-only editors, clearing marked ranges without emitting writes, and makesSaveJournal::flushwait for pending revision-0 drafts. - Guards attachment paste/choose/drop and async preview/backup/open callbacks against read-only state, document mutations, and vault switches, retaining dropped imports as pending inserts and tracking attachment ownership through conflicts.
- Replaces the fixed 400-iteration settle loop with a 20s wall-clock deadline and fixed polling with deterministic task draining.