Skip to content

fix(artifacts): ask before an in-app exit drops unsaved artifact edits - #20355

Merged
buluoray merged 2 commits into
mainfrom
fix/artifact-edit-leave-guard
Oct 14, 2026
Merged

buluoray merged 2 commits into
mainfrom
fix/artifact-edit-leave-guard

Conversation

@buluoray

@buluoray buluoray commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Unsaved edits on an artifact are not lost when the user leaves the page through the app's own navigation.

While an artifact is open in the editor with unsaved changes, clicking a sidebar entry, running a command-palette jump, pressing the browser's Back button, or following a navigation forwarded from a popped-out artifact window replaces the page at once. The edits are gone with no prompt. Only a real unload (closing the tab, reloading) asks, through beforeunload, because client-side route changes never fire it.

Why it matters

Editing an artifact by hand is the one place on the page where the user's own typing is at stake. A stray sidebar click or Back press silently throws that work away, and nothing on screen says it happened.

Not a goal

  • Resuming the refused navigation after the user confirms the discard; the shell's leave registry does not pass the target, so the user repeats the navigation.
  • Changing the artifact detail toolbar or adding new artifact actions.
  • Guarding other pages' unsaved state.

What changed (motivation → approach → change)

  • Root cause: in-app exits ask the shell's leave registry (useMayLeaveForNavigation), but the artifact detail page never registered with it, so a dirty edit buffer answered "may leave" by default.
  • Page guard: ArtifactDetailPage registers a guard (useRegisterNavigationLeaveGuard) and publishes its stake (usePublishNavigationStake(dirty)), so the sidebar, command palette and browser Back ask first. The registry needs a synchronous answer, so a dirty buffer refuses the exit and opens the page's own async discard dialog (useConfirm, not window.confirm, which windowConfirmRatchet caps). A second ask while the dialog is up shows one dialog. A confirmed discard calls endEditing(), as Cancel does, so a save still on the wire cannot raise a failure or conflict notice for the buffer that is gone.
  • Popout-forwarded navigation: applyNavIntentInMain takes an optional mayLeave check, and App.tsx passes the shell's leave answer. A refusal drops the whole intent: no prefill is staged, no slot switches, nothing navigates. The window is raised before the check so its confirm appears on a window the user can see. The App-level variable is renamed from mayLeaveForErrorHandoff to mayLeaveForNavigation, since it now serves both callers.
  • Native desktop navigation: the desktop app's menu entries (Settings…, About) and the Companion's "Open session" (subscribeNativeNavigate) run the same leave check before they navigate. mayLeave is now a required argument of applyNavIntentInMain, so no caller can skip the check by leaving it out.
  • NavigationLeaveGuard.tsx lists the forwarded intent among the wired exits; docs/system-specs/modules/artifacts.md documents the guard.

This is the edit-buffer leave guard split out of #17194 at the reviewers' request, so it can be reviewed and land on its own.

Backwards compatibility

Compatible: frontend-only; no API, stored data or config change. applyNavIntentInMain's new mayLeave dependency is required; both in-tree callers pass it.

Removes nothing: the change only adds a confirm step in front of existing navigations; every exit still works once the buffer is clean or the discard is confirmed.

Tests

  • ArtifactDetailPage.leaveGuard.test.tsx (new): with a dirty buffer the shell's leave check refuses and opens the app discard dialog (no window.confirm; a repeated ask still shows one dialog); discarding lets the repeated navigation leave; cancelling keeps the edit; a clean buffer leaves without asking; a save in flight at the discard shows no failure notice afterwards. Making the guard always return true reddens three of the four.
  • App.navLeaveGuard.test.tsx: a popout-forwarded intent asks the page on screen, keeps the draft on a declined confirm and navigates on an accepted one. Wiring mayLeave to always-true reddens the declined case. A native menu path and a native session deep link likewise keep the draft on a declined confirm and navigate on an accepted one; removing the native check reddens both declined cases.
  • popoutNavForwarding.test.ts: a refused leave drops the intent (no prefill, no slot switch, no navigate); an allowed one navigates. Removing the veto check reddens both.

Manual verification

Local: tsc, eslint --max-warnings=0 on the changed files, docs-lint.sh, the comment-history and brand gates, and 71 related frontend test files (801 tests: ArtifactDetailPage, App, navIntent, popout, NavigationLeave) pass. local-gate.py's backend half was stopped at its time budget; this diff touches no backend file, so CI runs it.

Screenshots / video

Real built SPA, API stubbed, dark theme. A Settings sidebar click with a dirty buffer: the page stays put and its own dialog asks (no native confirm fired).

  1. Editing with an unsaved line (• unsaved changes in the footer):
    Artifact in edit mode with an added unsaved line six and the unsaved changes marker
  2. Settings clicked in the sidebar: the Discard unsaved changes dialog over the artifact:
    Discard unsaved changes dialog with Cancel and Discard changes over the artifact editor
  3. Cancel: still editing, line six intact:
    Artifact still in edit mode after Cancel with the unsaved line six intact
  4. Discard changes: the edit ends and the saved content shows; the user is still on the artifact and repeats the click, which then leaves without asking (see Not a goal):
    Artifact back in view mode after Discard showing only the saved content

Related Issues

Split from #17194.

Pattern harvest

Rule candidate: review-prompt
Pattern: an optional veto callback (mayLeave?: () => boolean) lets a navigation caller skip the leave check by omission; make the check a required argument so every exit decides.

A dirty artifact edit buffer now registers with the app shell's leave
registry and publishes its stake, so the global sidebar, the command
palette, browser Back and a popout's forwarded navigation intent ask
before they replace the page. The registry needs a synchronous answer, so
the guard refuses the exit and opens the page's own discard dialog; a
confirmed discard ends the edit through endEditing, so a save still on the
wire cannot raise a stale failure or conflict notice.

Split out of #17194 at the reviewers' request.

Co-authored-by: Yusheng Xu <buluoray@gmail.com>
@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners October 14, 2026 15:32
@buluoray
buluoray requested a review from mbriones98 October 14, 2026 15:32
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — 🟡 CONCERNS

UX-level review of 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: CONCERNS

After "Discard changes" the user stays on the artifact, so the Settings click looks swallowed and they have to click it again.

Watch

  • Discard does not finish the navigation. The guard calls if (ok) endEditing() and return false, and nothing replays the exit (shot-04). A user who clicked Settings or pressed Back confirms the discard, sees the edits vanish and stays on the artifact page. It reads as a broken click. The PR lists resuming as "Not a goal", but users get this every time they confirm a discard. The smallest fix is to make the dialog say so up front, for example a description line: "You'll stay on this page; repeat your click to leave."
  • The dialog doesn't say why it appeared. Its only text is the title "Discard unsaved changes?". The blind reader wrote: "I can't tell what opened the pop-up in shot 02… The pop-up doesn't say." The toolbar Cancel and Back already show this same dialog, so it doesn't tell the user that their sidebar or Back press triggered it. Add a description naming the exit and what will be lost.
  • "Cancel" means two opposite things. The toolbar Cancel drops the edits; the dialog's Cancel keeps them. The reader flagged this ("Someone who clicks the toolbar Cancel and then sees another Cancel could easily pick the wrong one"). Before this PR, this dialog only opened from the page's own Back, toolbar Cancel and version-switch buttons. Now it also opens from global navigation, so more users will meet the clash. The suggestion below fixes it.

Suggestions

  • For this guard's confirm({...}), set the cancel label to "Keep editing" instead of "Cancel".

[UX-REVIEWED] 0b2fa56

Description read

[DESCRIPTION-READ] f2d2507773727653241718278ab106dcf7774feb473c493d43abfe91717ed556

That sha256 names everything this verdict read: the PR description, plus the 7 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check one thing before merge: a refused popout intent such as "Ask agent to address" drops its prefill, so after confirming the discard the user has to click it again in the popout.

[DESIGN-REVIEWED] 0b2fa56

Description read

[DESCRIPTION-READ] 17c6af52536e6d42f9f60e05df1a0d3d13ec2e688163c836055e1c0c517b97b2

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Check before merge: after the user confirms a discard, the navigation does not continue on its own. They have to click again, as ## Not a goal says.

What this change ships

Inventory (8 items) — 8 justified

Intent: stop unsaved artifact edits from being lost when the user leaves through the app's own navigation. This is a FIX.

  1. With unsaved edits, a click in the sidebar or a command-palette jump now stops and opens the page's own "discard unsaved changes" dialog. — justified
  2. With unsaved edits, the browser's Back button asks before leaving. — justified
  3. A navigation sent over from a popped-out artifact window now asks first. A refusal cancels the whole hop: nothing is pre-filled, the slot does not switch, the page does not move. — justified
  4. The desktop menu's Settings… and About entries, and the Companion's "Open session", ask before leaving. — justified
  5. Confirming the discard ends editing but stays on the page, so the user repeats the navigation. — justified
  6. The main window is brought to the front for a forwarded navigation even when the user then refuses it. — justified
  7. applyNavIntentInMain now requires a mayLeave check, and both of its callers pass one. — justified
  8. mayLeaveForErrorHandoff is renamed mayLeaveForNavigation, and the artifacts spec and the guard's comment are updated. — justified

[FIRST-PRINCIPLES-REVIEWED] 0b2fa56

Description read

[DESCRIPTION-READ] 10d07efeb863167a11be57b5cea67fab41a406a305c5e4a6ff6c2ea39ab03ed4

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 0b2fa56

Verdict parsed from the review's SHA-scoped output markers for commit 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 0b2fa56

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 0b2fa564b1cf57bc08e7724b6ddeb0a563fc917d: <one-sentence reason>

The desktop app's menu entries and the Companion's Open session called
applyNavIntentInMain without a leave check, and a bare navigate for other
paths, so they replaced the page without asking. Both now run the shell's
leave check, and mayLeave is a required argument so a caller cannot skip
it by omission.

Co-authored-by: Yusheng Xu <buluoray@gmail.com>
@buluoray

Copy link
Copy Markdown
Contributor Author

🤖 Kiro Crew Auto-Pipeline [operator: buluoray#20355]: Dispositions for the reviews on a9d84de, addressed at 0b2fa56.

  • First Principles CONCERNS (native menu navigation skips the leave check): fixed. subscribeNativeNavigate in App.tsx now runs the leave check for both the session deep link and the bare path, and mayLeave is a required argument of applyNavIntentInMain. Three new tests in App.navLeaveGuard.test.tsx; removing the native check reddens the two declined cases. Spec and NavigationLeaveGuard.tsx updated.
  • UX BLOCK (evidence gap): the description now carries four frames from the real built SPA: editing, the discard dialog after a sidebar click, Cancel keeping the edit, and the page after Discard.
  • UX Watch (Discard does not continue the navigation): kept as is. Resuming the refused navigation is listed under Not a goal: the leave registry does not pass the target. Frame 4 shows the resulting state.
  • PR Hygiene / Screenshot Evidence: Pattern harvest section and screenshots added to the description.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 14, 2026
@buluoray

Copy link
Copy Markdown
Contributor Author

🤖 Kiro Crew Auto-Pipeline [operator: buluoray#20355]: Dispositions for UX Review CONCERNS (advisory) on 0b2fa56. No push for these: a push would restart every lane on this head, and none of them is a defect in what the PR claims.

  • Discard does not finish the navigation: kept, as in the earlier disposition. Resuming the refused exit is under Not a goal because the leave registry does not pass the target. A dialog line telling the user to repeat the click is new copy in every locale; held for the owner to take or drop.
  • The dialog does not say why it appeared: agreed it is terser than ideal. The guard reuses the page's existing discard dialog (pages.artifactDetailPage.discard_unsaved_changes / discard_changes_button) on purpose, so every exit from a dirty buffer asks the same question. ConfirmOptions.body could carry an exit-specific line; that is new copy, held for the owner.
  • "Cancel" means two opposite things: this pairing predates the PR. The page's own Back, toolbar Cancel and version switch already open this same dialog with the shared Cancel button. ConfirmOptions has no cancel-label knob, so "Keep editing" means widening the shared ConfirmDialog component, which is outside this guard-only PR. Changing it for this one caller would also make the same dialog read two ways on one page. Suggested as a follow-up.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention labels Oct 14, 2026
@buluoray
buluoray enabled auto-merge (squash) October 14, 2026 17:52

@mbriones98 mbriones98 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checks green and readiness passed at 0b2fa56; no behavior change.

@buluoray
buluoray merged commit 79d021b into main Oct 14, 2026
107 of 108 checks passed
@buluoray
buluoray deleted the fix/artifact-edit-leave-guard branch October 14, 2026 18:06
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 14, 2026
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.

2 participants