Skip to content

Tabs of the same file share one record of unsaved edits - #31974

Merged
nwparker merged 17 commits into
mainfrom
nwparker/editor-buffer-switch-g3
Oct 22, 2026
Merged

nwparker merged 17 commits into
mainfrom
nwparker/editor-buffer-switch-g3

Conversation

@nwparker

@nwparker nwparker commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 26 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1860 $\color{#cf222e}{\Huge{\mathbf{−}}}$​179 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1681
Prod 50 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1409 $\color{#cf222e}{\Huge{\mathbf{−}}}$​337 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1072

ELI5

Today, when the same file is open in two workspaces (or a source and a rich-Markdown view), each tab keeps its own copy of your unsaved edits, and they can drift apart — that is where several "my edits were overwritten / lost" bugs came from. Now every tab of a file shares one record of unsaved edits: type in one, the others show it; it's saved once; you're only asked "save changes?" when you close the file's last tab.

What Changed

  • One record per file. Tabs of the same file (same machine, same path) share one record of unsaved edits, keyed by the file's identity. The record is re-checked when a workspace's location becomes known or changes, and on open, restore and rename.
  • Joining tabs. A clean tab joins the file's record; if two tabs hold the same unsaved text they merge; if they hold different unsaved text, or one has a pending conflict, they stay apart with their own editor model and rejoin as soon as one side is saved or discarded. Two sets of different unsaved edits are never merged automatically.
  • Rich Markdown, notebook and CSV views write through the shared record (each commit is its own undo step), and after any save every view of the file takes the saved text.
  • Workspace removal: removing one workspace leaves the file's record intact for the others. A removal that can't prompt keeps the unsaved record until the recovery toast is dismissed; reopening the file brings the edits back.
  • Restart saves each file once, through a view that is allowed to save.

Why

This is the core of moving to one editor buffer per file. These two parts (shared records, and rich/notebook/CSV writing through them) must ship together: sharing records while those views still wrote per tab would let them overwrite each other. Conflicts are still caught by the existing "refuse if the file changed on disk" save.

Linked Issue

Follow-up to #31970.

Visual Proof

Same file in two workspaces. Typing in A (autosave off):

…shows in B with the unsaved dot:

Closing B asks nothing; closing A, the last tab, asks to save:

Rich view in B, source view in A, stays in sync:

An outside edit while A has unsaved changes: the changed-on-disk banner shows in both workspaces, and the file isn't overwritten:

Testing

  • Every known scenario has a test: two workspaces typing within the autosave delay (one write, newest text); restored tabs with different unsaved text (in both mount orders); rename with a pending conflict; a phone save while edits are pending; rich text still pending during a save; pause/conflict state visible in every tab; read-only views; reconnect; retired remote environments; nested SSH; restart once per file; 200 tabs across 20 files.

  • Real app (hidden window, throwaway profile): shared unsaved text, close prompt only on last tab, single autosave write, rich/source sync, conflict banner in both workspaces. The banner was raised by the save-time check; live file-change events weren't arriving on the test machine, so the instant banner with autosave off wasn't exercised there.

  • pnpm tc, check:code-quality:changed, oxfmt, oxlint; editor/store/hooks/runtime/sidebar/web suites pass.

  • Reviewed by Opus and Sonnet over several rounds; their findings are applied.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platoform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

No wire or persisted-session format change. Tabs mirrored from older paired hosts show unsaved if any of them is. SSH files are keyed by connection; a connection repointed to another machine is caught by the guarded save.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

nwparker and others added 16 commits October 21, 2026 14:42
…oved tabs

Delete the pendingDiskBaselineVerification and pendingLiveDiskVerification
pause flags. Every editor save is already a guarded write; a refusal raises
the changed-on-disk banner, which is what stops autosave until an explicit
save.

A guarded write now succeeds when disk changed but already holds exactly the
text being saved (local/host compare is byte-exact; client-side fallbacks
compare decoded text and refuse when it holds U+FFFD), so a restored draft
that matches disk goes clean instead of showing a banner.

The restore scan only raises the banner early for tabs that arrive dirty; a
watch resync no longer re-checks dirty tabs.

Co-Authored-By: Claude <[email protected]>
Return a compatible optional no-op result from hosts and client fallbacks, clean the tab, and clear its provisional self-write stamp without rewriting identical bytes.

Co-Authored-By: Claude <[email protected]>
…er hosts, flag dirty tabs on resync

- Autosave and pre-restart saves refuse when disk text can't be decoded losslessly, so a
  baseline that decoded to U+FFFD can't authorize overwriting different bytes.
- New files.write.identical-noop.v1 capability; for hosts with only the v1 guarded write the
  client compares first, skips identical saves, then delegates to the host's guarded write.
- Watch resync compares dirty tabs against disk and shows the changed-on-disk banner.
- Local guarded writes check for identical content before the write-only regular-file probe.

Co-Authored-By: Claude <[email protected]>
…false resync banners

- Local guarded writes judge losslessness by bytes, so a valid UTF-8 file containing U+FFFD
  autosaves again. A file that really isn't UTF-8 is refused with its own reason, 'not-utf8'
  (older clients read it as 'changed'), which pauses autosave for that file with one notice
  until an explicit save converts it.
- A watch resync whose disk read fails leaves dirty tabs alone; the guarded save still catches
  a real conflict.
- Resync and restored-tab checks treat disk holding the tab's own just-written save as an echo.
- On hosts with only the v1 guarded write, only automatic saves read the file first; explicit
  saves go straight to the host's guard.

Co-Authored-By: Claude <[email protected]>
…d report disk changes first

- The pause is released once no dirty tab of the file remains (close, reload from disk,
  discard), so a reopened or reloaded file's next autosave asks the host again and notifies.
- Client-side precondition checks compare the signature before looking for U+FFFD, so an
  external rewrite shows the changed-on-disk banner rather than the non-UTF-8 notice.

Co-Authored-By: Claude <[email protected]>
…e sharing switch uses

First of the stacked sharing-switch PRs (G1). Inert while every record has
one view, which is all main produces today; tests drive the shared shape
through hand-built view indexes.

- Autosave timers are decided per record by its first view that may
  autosave, so a read-only or preview view of a record no longer cancels
  the record's timer, and a timer survives the view that scheduled it
  closing (it resolves a live view of the record when it fires).
- A view of a detached record gets its own model owner key, so divergent
  dirty drafts never share text with the file's other record.
- Pure join and rejoin rules: rekey / attach / adopt (dirty wins) / merge
  (identical dirty) / detach (divergent dirty); a detached pair rejoins
  only once one side is clean.
- Mirrored host tabs: a record's dirty flag is the OR of its host-owned
  views, since an older host publishes it per tab; the dev invariant
  tolerates exactly that disagreement.

Co-Authored-By: Claude <[email protected]>
…e view for autosave

- A merge needs equal drafts, baselines, conflict and owner-migration state;
  anything else detaches, so a paused record never resumes autosave by joining.
  A side whose text is still pending (no draft) never counts as identical.
- A clean-flagged view that still holds a draft counts as dirty in joins.
- The invariant tolerates a clean host-owned view on a dirty record only when
  another host-owned view of that record reads dirty.
- Autosave and restart skip CSV-preview and read-only views when picking the
  view a record writes through.

Co-Authored-By: Claude <[email protected]>
…r-switch-g1-fixes

# Conflicts:
#	src/renderer/src/components/editor/editor-save-queue.ts
A dirty view with no draft no longer pushes disk text into the shared model; merge and
deleted-banner joins mirror the record; web-session sync honours detached views.

Co-Authored-By: Claude <[email protected]>
…ompt closes, restore merges

- Rich Markdown, notebook and CSV commits also edit the record's models no editor shows, as one
  undoable edit; a save moves every loaded view's panel text to the written disk text, so a stale
  dirty check can't mark the record clean over unsaved edits.
- A detached pair rejoins once one side is clean (save, Discard, reload) or the partner closes:
  the clean side attaches, a dirty side wins with its own baseline, so its next save is refused
  and the banner shows on every view. Rejoin runs on the next microtask, after the caller has
  finished settling the record; doing it mid-settle let the settle clear the adopted draft.
- A dirty record whose last view a no-prompt removal closed is kept with its draft and model;
  reopening the file joins it, it never autosaves, and dismissing the removal notice drops it.
- Restored identical drafts merge; divergent ones detach with their own models and timers.

Co-Authored-By: Claude <[email protected]>
…e a record through its writable view

Adds save-queue and store tests for rename and owner-resolution re-keys carrying a conflict, a
read-only sibling joining a dirty record, retired-runtime quarantine, runtime-in-SSH identity, the
reconnect writer, restart once per record, and a 200-tab index.

Fix: restart picked a record's first dirty view, so a read-only view listed first refused the
whole restart; it now picks the record's first view that may autosave.

Co-Authored-By: Claude <[email protected]>
… save events

- A no-prompt removal's keep request rides in the write that closes the tabs
  (editorBufferKeepRequest, consumed by the record middleware), so a removal
  closing tabs over several writes keeps every dirty record it closes.
- Kept records carry their removal notice's token; dismissing a notice, or
  failing to show it, releases only that notice's records.
- A divergent dirty view reopening a kept record stays detached on its own
  model; a rename onto a kept record's path drops the kept record.
- Each rich/notebook/CSV commit into a retained model is its own undo step.
- The saved event names the record's views, so the panel updates them even
  when the saving tab closed mid-save.
- A queued save compares the discard epoch of the record it was queued under.
- Typing no longer runs the rejoin check; only clearing a side's draft does.

Co-Authored-By: Claude <[email protected]>
…r-switch-g3

# Conflicts:
#	src/renderer/src/components/editor/editor-model-owner.test.ts
#	src/renderer/src/components/editor/editor-model-owner.ts
#	src/renderer/src/components/editor/editor-save-queue-records.test.ts
#	src/renderer/src/store/editor-buffer-record-lifecycle.ts
#	src/renderer/src/store/slices/editor/buffers/editor-buffer-join.ts
#	src/renderer/src/store/slices/editor/buffers/editor-buffer-mirrored-dirty.test.ts
#	src/renderer/src/store/slices/editor/buffers/editor-buffer-record.ts
#	src/renderer/src/store/slices/editor/buffers/editor-buffer-views.ts
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The changes add record-based editor-buffer identity and reconciliation across views. Shared records synchronize drafts, model text, disk baselines, and autosave requests. Detached records can rejoin when their state permits. No-prompt removals retain dirty records through tokenized notices, and the notice lifecycle releases them. Tests cover identity resolution, shared saves, external changes, detached views, and removal handling.

Priority: ➖ Normal

Merge Risk: 🟠 High · up to e8e9e

Tabs of the same file can end up disagreeing with their shared record after a join or a mirrored session sync. That can show stale dirty state or draft text, or throw an invariant error during editor state updates. Fix both reconciliation paths before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 75 functions across 54 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description check Passed The description covers the user impact, implementation, rationale, linked issue, visual proof, testing, compatibility notes, and checklist. Some checkboxes remain unchecked, but the required informati…
Title check Passed The title clearly and concisely describes the primary change: tabs for the same file share one record of unsaved edits.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description covers the user impact, implementation, rationale, linked issue, visual proof, testing, compatibility notes, and checklist. Some checkboxes remain unchecked, but the required information is mostly present.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…tchet

The record middleware now applies reconcile's per-view mirror fields as a
membership-preserving map over openFiles, and the mirrored-dirty adoption
returns its views for the session-sync patch to write, as it did before.

Co-Authored-By: Claude <[email protected]>

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Update every view that follows a joined record. · editor-buffer-reconcile.ts:98-104

src/renderer/src/store/slices/editor/buffers/editor-buffer-reconcile.ts:98-104
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Update every view that follows a joined record.

When two clean views move together into an existing dirty record, joinView updates the first view. This branch only indexes the second view. The second view retains its clean flag and old draft, so it disagrees with the shared record and can fail the store invariant. Apply the destination record and draft to each following view before placing it.


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8e1d5d0d-7117-462b-9398-7ce949e445ce
📥 Commits

Reviewing files that changed from the base of the PR and between 1798e88 and e8e9ec5.

📒 Files selected for processing: 7
  • src/renderer/src/runtime/web-session-tabs-sync/apply-final-patch.ts
  • src/renderer/src/store/editor-buffer-record-lifecycle.test.ts
  • src/renderer/src/store/editor-buffer-record-lifecycle.ts
  • src/renderer/src/store/slices/editor/buffers/editor-buffer-mirrored-dirty.test.ts
  • src/renderer/src/store/slices/editor/buffers/editor-buffer-reconcile-write.ts
  • src/renderer/src/store/slices/editor/buffers/editor-buffer-reconcile.ts
  • src/renderer/src/store/slices/editor/buffers/editor-buffer-record.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

mirrored = true
return { ...file, isDirty: record.isDirty }
})
return { editorBuffers, views: mirrored ? nextOpenFiles : openFiles }

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.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Do not publish a record with mismatched view fields.

If two mirrored views resolve to one record but have different lastKnownDiskSignature values, adoption takes the first signature for the record and changes only isDirty on the views. The returned second view then disagrees with its record. For existing indexed views, reconciliation keeps both views without rewriting those fields, and the invariant rejects the patch. Reconcile every shared field, or keep views with incompatible baselines separate.

@nwparker
nwparker merged commit 885cd6f into main Oct 22, 2026
46 checks passed
jordanyeo pushed a commit to Statvis/orca that referenced this pull request Oct 23, 2026
…orkspace (read-only) (stablyai#31990)"

This reverts commit 8556d11 for the release ad hoc build only. stablyai#31990 builds on stablyai#31974, which is reverted next.
jordanyeo pushed a commit to Statvis/orca that referenced this pull request Oct 23, 2026
…lyai#31974)"

This reverts commit 885cd6f for the release ad hoc build only: release scan blocker STA-11786 (closing a tab mid-save can silently drop a later revert in the same file's other tab).
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.

1 participant