Skip to content

Ask before closing a Workspace tab with unsaved edits - #408

Merged
abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
coolestnick:fix/ask-before-closing-unsaved-tab
Nov 5, 2026
Merged

abhishekgahlot2 merged 2 commits into
useagenthq:mainfrom
coolestnick:fix/ask-before-closing-unsaved-tab

Conversation

@coolestnick

@coolestnick coolestnick commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #342

Closing a Workspace file tab threw away edits that were not saved yet: anything typed in the 800 ms before autosave, or left after a failed save. closeWorkpiece removed the tab without reading its dirty state, and unmounting the editor cleared its autosave timer.

What changed

  • A tab with unsaved edits asks first. "Save changes to ?" with Cancel, Discard and Save, on our own Modal and Button (the archive-bot dialog's recipe).
    • Save stores the edits, then closes the tab.
    • Discard closes without saving.
    • Cancel, Escape or the close button keep the tab and its edits.
  • A clean tab still closes at once. So does a tab whose editor is still loading or failed to load: there is nothing to lose.
  • A save that fails keeps the tab open and brings it forward, where the editor already shows its error. Nothing is closed on a failed save.

How

  • components/chat/workpiece-close.ts: the close decision as a small pure function, as the issue asked: close, or ask.
  • components/chat/workpiece-close-guard.tsx: a hook that runs the close for the tab strip and owns the dialog. Each mounted editor registers dirty() and save() under its tab id. session-view.tsx is unchanged (it is at its file-size baseline).
  • workspace-pane.tsx: the editor registers itself; the tab strip's close goes through the guard.
  • artifact-editor-state.ts: save() now resolves true when the edits are stored and false when they are not (an error, a 409 conflict, an invalid state). Its two existing callers ignore the result, so they behave as before.

Edge cases handled:

  • Autosave finished while the dialog was open: Save just closes, with no second save.
  • Two fast clicks on Save: only one save runs.
  • A save that throws instead of returning false: treated as a failed save; the dialog never sticks on "Saving...".
  • The tab went away during the save (for example, leaving the thread): the guard does not close or select it afterwards.
  • On phones, since Make the side panel on phones a proper dialog #399, the side panel is a dialog itself. Escape closes only this dialog, not the panel. Tab stays inside it, it fits the screen.

Screenshots

From the real app against a local backend and database. Same thread, same steps: open Launch notes.docx, type, close the tab straight away.

Before (main): the tab closes at once with no question; no save is sent and the edit is lost (the stored revision does not change).

Before: the tab closes and the edit is lost

After: the tab asks first.

After: Save changes to Launch notes.docx?

After, a save that fails (network cut): the tab stays open with the edit, "Unsaved changes" and the error.

After: a failed save keeps the tab open

After, on a phone (390 px), inside the side panel dialog:

After: the dialog on a phone

Testing

In the app, against a local backend and Postgres, with the result of each step read back from the database:

Step Result
Close a tab with no edits closes at once, no dialog
Type, then close straight away dialog asks; tab stays open
Cancel / Escape tab and edit stay; autosave stores the edit as usual
Save one PATCH .../workpiece (200), tab closes, the edit is stored
Discard tab closes, no PATCH, the edit is not stored
Save with the network cut tab stays open with the edit and the editor's error
Reload the editor shows exactly what was stored
Phone width Escape closes only this dialog; the side panel stays open; Save stores and closes

The same steps on main close the tab with no save.

Tests: frontend/components/chat/workpiece-close.test.tsx, 10 tests:

  • the decision function: clean, dirty, not loaded;
  • through the real tab strip: a clean close, Save, Discard, Cancel, a failed save, autosave already done, a save that throws, a second answer while saving, and a tab that went away during the save.

Putting the old behaviour back fails 4 of them, and removing each guard fails its own test. The tests answer through the guard's choose, which the dialog buttons call, because two other test files load the dialog library before a DOM exists, so its portal cannot mount in the full suite.

Checks:

  • root bun run typecheck: passes;
  • in frontend/, bun run test: 1856 pass, 0 fail;
  • bun run lint: nothing in the changed files;
  • bun run build: passes;
  • bun test scripts/agent-docs.test.ts: passes.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

coolestnick and others added 2 commits November 3, 2026 08:16
Cancel now answers while a save runs, so a save that never returns no
longer traps the page, and a save that ends after Cancel leaves the tab
alone. Autosave pauses for the tab whose close question is open, so
Discard never closes a tab whose edits were already stored and a
background conflict cannot swap the text behind the dialog. Tests now
mount the real WorkspacePane over a stubbed backend.
@abhishekgahlot2
abhishekgahlot2 merged commit aadb64a into useagenthq:main Nov 5, 2026
21 checks passed
@abhishekgahlot2

Copy link
Copy Markdown
Contributor

Merged, thanks @coolestnick. Asking before an unsaved tab closes was a real gap. I added a follow-up commit so Cancel works while a save is running and autosave pauses while the question is open.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ask before closing a Workspace tab with unsaved edits

2 participants