Skip to content

refactor(editor): tab buffer state lives in a per-tab store record, mirrored onto the tab - #30885

Merged
nwparker merged 1 commit into
mainfrom
nwparker/editor-buffer-1a-record
Oct 19, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/editor-buffer-1a-record

Conversation

@nwparker

@nwparker nwparker commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​348 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​348
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​352 $\color{#cf222e}{\Huge{\mathbf{−}}}$​107 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​245

ELI5

Each open editor tab keeps a few facts about its file: whether it has unsaved edits, which disk version those edits are based on, whether the file changed on disk underneath it, and a few "don't autosave yet" holds. Today those facts live directly on the tab. This PR moves them into their own small record in the store, one per tab, and keeps a copy on the tab so nothing that reads them has to change. Nothing looks or behaves differently.

This is Phase 1a of the design in #30851: the first, purely mechanical step towards one shared buffer per file.

What Changed

  • New record. editorBuffers in the store holds, per open tab id, exactly six fields: isDirty, lastKnownDiskSignature, externalMutation, pendingDiskBaselineVerification, pendingLiveDiskVerification and pendingOwnerMigration. The same fields on OpenFile stay as a copy, written in the same store update. Unsaved drafts (editorDrafts) and the move-echo marker (pendingSelfMoveEcho) stay where they are.
  • Writers write both. Every action that changes one of those fields now writes the record and the tab's copy together: the six open-file-mutations / restored-owner setters, the restored-owner migration's success and failure paths, the self-move re-key (which also sets the move's autosave hold), and mirrored web-client tabs from a host snapshot.
  • Records follow the open tabs. A small store middleware keeps record keys identical to the open tab ids within the same update: opening a tab creates its record from the tab, and closing a tab or tearing down a workspace removes it. This covers every path that adds or removes tabs, including layout-frame loads and preview replacement, without bookkeeping at each site. Hydrate builds fresh records from the restored session. Id changes (self-move re-key and restored-owner migration) move the record to the new id explicitly. A missing record is always seeded from its tab, never from defaults.
  • Unchanged. Every reader, the text models, the dirty check (content compare, including the markdown trailing-whitespace leniency), the save paths and the changed-on-disk banner all behave as on main.

Why

The goal is one record per file as the single owner of its text and disk state. Doing that in one step would mix a large mechanical move with behaviour changes. Moving the state first, keyed by the existing tab id and with a copy on the tab, keeps this PR behaviour-neutral. Later phases can then switch readers and merge records without also moving state.

How the invariant protects "no behaviour change": in dev and test builds, the middleware checks after every store write that record keys equal the open tab ids and that every copied field on a tab equals its record. A store action that changes one of the six fields on a tab without going through the record throws in tests, so a missed writer fails CI instead of drifting quietly. The whole existing renderer suite runs with the check on. Fixtures that seed tabs through useAppStore.setState are treated like hydrate: their records are seeded from the tab. In production builds the same mismatch adopts the tab's value, so a missed writer still behaves exactly as main does.

Alternatives considered: deriving the record from the tab after every write would make the tab the real owner and defeat the point. Explicit create/delete calls at every open and close site would touch dozens of places that add or remove tabs. Tying membership to the store write is smaller and can't miss a site.

Linked Issue

Phase 1a of the design in #30851.

Visual Proof

N/A: state is moved inside the store with no UI or interaction change.

Testing

  • I manually tested these changes locally

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

  • New store/editor-buffer-record-lifecycle.test.ts: each of the six setters writes the record and its copy in a single store notification; open creates, close deletes, self-move re-key moves the record and sets the move's autosave hold on both copies; hydrate creates records from the session instead of from a replaced tab with the same id; an externally seeded tab gets a record seeded from the tab; a store action that writes a copied field without its record throws.

  • New runtime/editor-buffer-dirty-readers.test.ts: a dirty change made through the record reaches the web-session tab sync (sameOpenFiles), session save (buildEditorSessionData persists the draft), the mobile session projections, and the mobile markdown bridge read.

  • Full src/renderer vitest suite with the invariant enabled: 4,996 files passing. One terminal-pane test (remote-snapshot-alt-screen-host-chain) timed out on a cold import under load and passes when rerun; it mocks the store.

  • pnpm tc, pnpm run check:code-quality:changed.

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

  • Renderer-only store change. Nothing goes over the wire and nothing new is persisted, so SSH, runtime and mobile peers see the same data as before.
  • Cost: one check of the six fields per open tab, and only on writes that replace openFiles. Writes that don't touch openFiles return immediately.
  • Most of store/index.ts is indentation from the extra middleware wrapper. View it with whitespace hidden.

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)

@coderabbitai

coderabbitai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8f514802-e566-4f2f-af9b-5d694bfda6e0
📥 Commits

Reviewing files that changed from the base of the PR and between b03ac56 and ff6f5f3.

📒 Files selected for processing: 16
  • src/renderer/src/runtime/editor-buffer-dirty-readers.test.ts
  • src/renderer/src/runtime/web-session-tabs-sync/apply-final-patch.ts
  • src/renderer/src/runtime/web-session-tabs-sync/apply-preparation-browser.ts
  • src/renderer/src/runtime/web-session-tabs-sync/state.ts
  • src/renderer/src/store/editor-buffer-record-lifecycle.test.ts
  • src/renderer/src/store/editor-buffer-record-lifecycle.ts
  • src/renderer/src/store/index.ts
  • src/renderer/src/store/slices/editor/actions/hydrated-editor-session-state.ts
  • src/renderer/src/store/slices/editor/actions/open-file-mutations.ts
  • src/renderer/src/store/slices/editor/actions/open-file-state.ts
  • src/renderer/src/store/slices/editor/actions/rekey-open-files-action.ts
  • src/renderer/src/store/slices/editor/actions/restored-editor-owner-destination.ts
  • src/renderer/src/store/slices/editor/actions/restored-editor-owner-transition.ts
  • src/renderer/src/store/slices/editor/actions/restored-editor-owner.ts
  • src/renderer/src/store/slices/editor/buffers/editor-buffer-record.ts
  • src/renderer/src/store/slices/editor/types/editor-files-slice.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.

📝 Walkthrough

The change adds editor buffer records keyed by open-file IDs and keeps them aligned with tab fields during store writes, hydration, mutations, and file re-keying. Web session tab synchronization now prepares and applies buffer records with changed open files. Tests cover record lifecycle and dirty-state reads through web, session-save, and mobile paths.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to ff6f5

This change moves editor buffer state into per-tab store records while keeping the existing tab fields in sync. No concrete behavior regression was found, so it looks ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 45.83% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 24 functions across 16 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: moving tab buffer state into per-tab store records while mirroring it onto tabs.
Description check Passed The description is complete and matches the template. It explains the user impact, implementation, rationale, linked issue, visual proof, testing, compatibility considerations, and checklist status. T…
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 is complete and matches the template. It explains the user impact, implementation, rationale, linked issue, visual proof, testing, compatibility considerations, and checklist status. The empty Review section and unchecked manual-test item are minor omissions because the automated testing and other required sections are documented.

  • Fix all pre-merge checks with AI
✨ 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.

@nwparker
nwparker merged commit c8a4fc6 into main Oct 19, 2026
53 checks passed
nwparker added a commit that referenced this pull request Oct 20, 2026
…rt stacks (#31216)

Fixes bugs found reviewing #30270, #30605, #30885, #30417, #31113, #30415,
#30992 and #30997, and removes code those PRs made unnecessary.

Co-authored-by: Claude <[email protected]>
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