Skip to content

fix: mark the document dirty for guides, comps and saved selections - #44

Merged
IAmJSD merged 2 commits into
Infrawrench:mainfrom
ProdigyRahul:fix/dirty-flag-document-state
Aug 27, 2026
Merged

IAmJSD merged 2 commits into
Infrawrench:mainfrom
ProdigyRahul:fix/dirty-flag-document-state

Conversation

@ProdigyRahul

Copy link
Copy Markdown
Contributor

fixes #43

add Document::mark_dirty and call it from the seven sites that mutate document state outside begin_edit. that is enough for the close prompt to appear and for autosave to stop skipping the document.

a named method rather than doc.dirty = true scattered around, so the next such mutation has an obvious thing to call and the doc comment explains why it exists. those lists still belong in history; when they get their own EditOp this call becomes redundant rather than wrong, and the comment says so.

the regression test is at the call site, not on the setter: it runs select.save through the real command registry and asserts the document comes back dirty. it fails on main with and the document must count as unsaved. a second test pins that add_damage alone does not imply unsaved changes, since that is the trap that makes this look already-handled.

579 tests pass, clippy and fmt clean.

@IAmJSD IAmJSD left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had some concerns about this PR, so gave it to Fable with my concerns, here's what it said:

Review of ProdigyRahul:fix/dirty-flag-document-state is done, and your gut was right: the fix is inconsistent. Seven findings are reported above; here's the shape of it.

The core inconsistency. A census of every non-test site that mutates the affected collections shows the PR covers 7 of 12. What it missed is the entire plugins/tools-doc plugin — the Note, Count, Artboard, and Slice tools that create and move this state only call add_damage, and the PR's own new test proves damage doesn't set dirty. So the fix covers destruction (the Clear menu items) but not creation: place ten notes or drag out export slices on a saved document, close the tab, and you still get no prompt and autosave still skips it — the exact bug the PR describes, unfixed for its most common entry point. The inconsistency is even visible inside one match: the Frame variant of the rect tool commits through begin_edit and gets dirty for free, while the Artboard and Slice arms right next to it don't.

It also errs in the opposite direction. clear_guides, ClearNotes, and ClearCounts mark dirty unconditionally, so clearing an already-empty list flags an unchanged document — spurious close prompt, plus a full codec export to disk every 30 seconds from autosave until the next manual save. An is_empty() guard at those three sites fixes it.

Two deeper problems worth flagging on the PR:

  • No codec in the repo serializes guides, layer comps, saved selections, notes, or counts. So the new prompt's "Save" button writes a file that omits the very state that made the document dirty, clears the flag, and closes the tab as if it were safe. The protection the PR adds is partly hollow until the exporters carry this state (autosave recovery snapshots have the same gap, and Cmd-Q bypasses the dirty check entirely — both pre-existing, but they bound what this fix actually buys).
  • Structurally, the per-call-site approach can't be enforced: the fields stay pub, two pre-existing doc.dirty = true writes weren't migrated to the new helper, and the PR's own doc comment concedes these lists belong in the edit history. EditOp::SelectionSet is a ready-made template — routing these mutations through edit ops would set dirty centrally and make them undoable, closing the class of bug instead of six instances of it.

On the plus side: the seven sites it did cover were each verified correct (live consumers, no indicator lag, no double-marking through the command dispatch), and both new test suites pass in the review worktree — though the mark_dirty unit test is tautological and would stay green even with every call site deleted.

My recommendation for the PR author: at minimum extend the fix to the tools-doc sites and guard the three clears; ideally replace the scattered calls with edit ops. And it's worth an honest note on the PR that save/autosave can't yet persist most of this state.

@ProdigyRahul

Copy link
Copy Markdown
Contributor Author

thanks, and fable was right on both counts. fixed and pushed.

covered the creation side. every tools-doc site now marks dirty: artboard and slice created, artboard and slice dragged, note placed, moved and alt-removed, count mark added and removed. that was the real hole, since placing a note is how you get this state in the first place.

frame stays untouched there because make_frame commits an edit and is already dirty. i left the matches! guard explicit rather than letting it fall through, so the asymmetry inside that match reads on purpose now.

guarded the three clears. clear_guides, ClearNotes and ClearCounts only mark dirty when the list had something in it. clearing an empty one no longer flags an unchanged document.

the tautological test is gone. it now says what it pins, which is the helper's contract only. the call sites are covered where they live, in tools-doc, with tests that fail if any one call is deleted. i checked that by deleting one.

on the two deeper points, you are right and i am not fixing them here.

no codec serializes guides, comps, saved selections, notes or counts. so save still writes a file without the state that made the document dirty. this pr stops the silent loss on close and gets the document into autosave, it does not make the save complete. same gap in the recovery snapshots, and cmd-q still bypasses the check.

and the per call site approach cannot be enforced while the fields are pub. routing these through EditOp the way SelectionSet already does would set dirty centrally and make them undoable, which closes the class instead of the instances. happy to do that as a follow up if you want it before this lands, or after.

cargo fmt --all --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace all green, 581 passing.

@IAmJSD IAmJSD left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Claude (Fable 5) on Astrid's behalf. Traced every direct-mutation site of guides/comps/saved selections/notes/counts at this revision — all covered, no incorrect markings, and the tests fail on main as they should. The no-op guards on the clear paths are a nice touch. Merging.

@IAmJSD
IAmJSD merged commit c9bd500 into Infrawrench:main Aug 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

guides, layer comps and saved selections never mark the document unsaved

2 participants