Skip to content

fix: keep unsaved edits when another program writes the file (#81) - #110

Merged
BinaryOutlook merged 6 commits into
mainfrom
feat/external-write-conflict
Nov 13, 2026
Merged

BinaryOutlook merged 6 commits into
mainfrom
feat/external-write-conflict

Conversation

@BinaryOutlook

@BinaryOutlook BinaryOutlook commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Closes #81. Related: #11, which covers save infrastructure shared with this change. #11's queued-edit and native-save overlap protections are unchanged, and save-correctness still passes.

Problem

When another program wrote an open Markdown file that had unsaved visual edits, the file watcher replaced the document, saved it and cleared the editor's undo history. The local edits were lost with no prompt and no way to recover them.

Behavior after this change

  • Clean document: external writes reload as before. Files saved by replacement (rename over the original) are now detected too.
  • Unsaved edits: the edits stay in the editor and VS Code's buffer, the document stays dirty, and the other program's version remains on disk. One warning per conflict offers:
    • Compare, which opens VS Code's diff with the other program's latest version read-only on the left and the editable document on the right. The warning then returns.
    • Load Disk Version, an explicit discard that leaves that version clean. Undo in the editor restores the replaced edits.
  • Both choices read the file again, so they use the latest external version. Later writes during the same conflict don't repeat the warning.
  • A Changed on disk status bar item and Binary Markdown: Resolve External Change in the Command Palette reopen the choices after the warning is closed. Saving, reverting or loading ends the conflict.
  • Typing race: text typed just before a reload from disk, and not yet passed to VS Code, is kept instead of the reload, with the same warning. VS Code's newer-file check can't see this case. The editor's first save from its own shortcut is therefore held until the warning is answered, and saving again writes the text. A save started from VS Code itself can't be held; the editor guide says so.

Design

  • Revisions: the host numbers each update. edit, save and the native-save snapshot carry the base revision their text was edited from.
    • An edit older than the latest disk revision raises the conflict once and marks the next editor save as held. The host then sends the buffer back, so the editor and buffer converge.
    • An edit older than an explicit Load Disk Version is discarded.
  • Queued updates: the webview holds an update while the user is typing or edits are unsent. Sending local text supersedes it. If nothing new needs sending and no typing is in progress, the update is applied at once; this covers Source mode, which never starts the typing idle timer. Saving first settles a held update.
  • Electron: the standalone app keeps its contract. It sends no update messages, and its bridge ignores the new arguments.
  • Safe file names: names in warnings are inserted literally (no $ replacement patterns) and separated at ](, so a crafted name can't render as a notification command link.

Scope audit follow-up

A scope audit of the first commit found four non-blocking issues, now fixed:

  • a queued update stuck in Source mode;
  • a keyboard save during the typing race that wrote over the reload;
  • missing native coverage for Source mode, keyboard saves and late own-save notifications;
  • $ patterns in file names.

It also suggested QoL items, now added: the status bar route, re-reading the file for Compare and Load, event-based native waits, and French typography. The audit's CI blocker, the npm audit failure, was fixed on main in #109, which is merged here.

The Codex review found that a write detected while Load Disk Version applied could leave the editor and buffer on different revisions, and then save the older one over the newer file. Commit 0b8b7f3 uses one revision for the buffer, editor and save, and reports a mid-load write as a new conflict.

The VS Code 1.85 compatibility fix in e7dc70a uses Revert when the requested revision is still on disk, with an active-document guard and the existing save fallback. The follow-up scope audit found that Revert did not refresh the watcher's known disk baseline; ef01cbb updates it and invalidates older watcher reads, preventing missed external changes and false conflicts after loading.

Verification

Head ef01cbbc86c652e2e5c2c3ec5c2ab63943c25c79 includes main at 225ce8687eb1a761705b7bdf4237ac2177acf5e7.

  • Full CI: Validate VSIX build 186, attempt 1 passed all 16 jobs on PR merge commit 689a10504231d461ea46355d961bb866373feb32: four native lanes (Windows, Ubuntu, macOS, and VS Code 1.85.0), all nine browser shards with zero retries, and the required VSIX validation gate.
  • VS Code 1.85.0: the installed-VSIX log confirms Load Disk Version leaves the document clean, displays the loaded revision, clears the conflict status, and preserves Undo. The lane passed all 493 unit/package/converter tests with no failures or skips, including both watcher-baseline regressions.
  • Local Linux, Node 24.21.0, at ef01cbb: full compilation passed; 467 unit tests passed, 0 failed, 19 skipped for optional converter/package prerequisites. Lint passed with 0 errors and 11 existing warnings. Documentation and whitespace checks passed. The new previous-baseline regression was observed failing before the fix.
  • Scope audit: the complete 37-file diff remains within Prevent external file writes from silently discarding unsaved visual edits #81's conflict handling, revision propagation, translated controls, documentation, and regression coverage. Audit details and CI evidence.

Limitations

  • VS Code only detects a newer file on save when its size changed. The guide recommends Compare before saving.
  • A save started from VS Code itself during the typing race writes the typed text. The warning still offers the other version.
  • Load Disk Version may bring the target editor forward. If that editor cannot be confirmed active, the save fallback can still show VS Code 1.85's newer-file prompt.
  • The new strings in six languages follow each locale's existing terms but haven't had a native-speaker review.
  • The README still points to the 0.5.1 known limitation, which remains true for that release. Update it when the next version is prepared.

🤖 Generated with Claude Code

An external write to an open Markdown file replaced unsaved visual edits,
saved the result and cleared the editor's undo history without asking.

- Never replace a dirty document from the file watcher. Keep the local
  text and show one warning per conflict with Compare (read-only diff of
  the latest external revision) and Load Disk Version (explicit discard,
  undoable in the editor).
- Number host updates and send the base revision with webview edits,
  saves and save snapshots. Text typed before a reload from disk is kept
  and the replaced disk revision is reported; text typed before Load Disk
  Version is discarded.
- Hold external updates while edits are unsent, so a queued update can no
  longer overwrite typing or diverge from the host buffer.
- Watch file creation as well as change, so atomic saves are detected.
- Break notification link syntax in file names shown in warnings.

Covered by provider unit tests, browser specs and a new installed-VSIX
external-conflict suite. Documentation and translations are updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T10:48:17.247320Z 159f523 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 159f523f39

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editorProvider.ts Resolved
BinaryOutlook and others added 5 commits November 13, 2026 19:21
Non-blocking findings:
- Apply a held external update once an edit leaves nothing new to send;
  Source mode input never starts the typing idle timer.
- Hold the editor's first save while a conflict raised by text typed
  before a disk reload is unanswered. VS Code's newer-file check cannot
  see that case. Echo the buffer back after a stale edit so the editor
  and buffer converge.
- Insert file names literally in warning, error and diff-title text.
- Add native cases for Source mode with a keyboard save, late own-save
  notifications and the held save in the typing race.

Quality of life:
- Show a Changed on disk status bar item and add Binary Markdown: Resolve
  External Change, so a closed warning stays reachable.
- Read the file again for Compare and Load Disk Version.
- Replace fixed native-suite delays with observable events.
- Use no-break spaces inside French guillemets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring in the settings organization and build-tool audit patch from #109.
Resolve CHANGELOG.md by keeping both Unreleased entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Load Disk Version read the conflict's retained revision twice: once for the
buffer edit and again for the editor update. A write detected while the
edit was applied changed that revision in between, so the editor showed the
newer write while the buffer held the older one, and the older one was then
saved over the newer file.

Capture one revision for the buffer, editor and save. When a newer write
arrives during the load, skip the save and report it as a new conflict.

Reported by the Codex review on #110.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
VS Code 1.85, the minimum supported version, reports "The content of the
file is newer" when a document changed on disk is saved, even when the
buffer already equals the file. Load Disk Version therefore left the
document dirty there; CI's vscode-minimum lane caught it.

When the version to load is the file itself, revert the document instead:
it reloads the buffer from disk and clears the dirty state without writing.
Revert acts on the active editor, so it runs only after confirming the
active tab shows this document, bringing this editor forward if needed.
Otherwise, or when the version was already overwritten on disk, the
previous write-and-save path remains. A write detected during that path is
reported as a new conflict instead of being overwritten.

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

BinaryOutlook commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner Author

Scope re-audit complete at ef01cbbc86c652e2e5c2c3ec5c2ab63943c25c79. All 16 PR CI jobs passed; no remaining blockers found within the reviewed scope.

The VS Code 1.85 Load Disk Version concern is confirmed resolved on the final candidate. Validate VSIX build 186, attempt 1 tested merge commit 689a10504231d461ea46355d961bb866373feb32 (ef01cbb with base 225ce868). All four native lanes, all nine browser shards, and the required VSIX validation gate passed.

The VS Code 1.85 log explicitly records:

  • Load leaves the document clean and displays the loaded revision.
  • The conflict status clears, and Undo restores the discarded edit.
  • All 493 unit/package/converter tests pass, with 0 failures and 0 skips, including the two new regressions below.

The scope audit found one additional watcher-baseline defect, fixed in ef01cbb. After loading through Revert, a later external write containing the original pre-load text was ignored as an old save notification. Revert does not fire a save event, so the cached disk baseline had remained stale. Successful Revert now refreshes that baseline and invalidates earlier watcher reads.

Two regressions cover a write of the previous baseline and a delayed notification of the newly loaded revision, including when Revert reads a newer write. The previous-baseline regression was observed failing before the fix. The follow-up changes only src/editorProvider.ts and its provider tests.

The complete 37-file scope audit against 225ce868 covers #81's conflict handling, revision propagation, translated controls, documentation, and regression coverage. No unrelated scope expansion found. The active-editor guard, Revert path, and existing fallback remain intact; the previously noted focus, command-timing, and fallback limitations are unchanged.

Local checks on the final head with Node 24.21.0: compilation passed; 467 unit tests passed, 0 failed, 19 skipped for optional converter/package prerequisites; lint 0 errors, 11 existing warnings; documentation and whitespace checks passed. Native/platform acceptance above comes from CI, not a new local installed-VSIX run.

The PR remains open and has not been merged.

@BinaryOutlook
BinaryOutlook merged commit c0b8dd6 into main Nov 13, 2026
22 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.

Prevent external file writes from silently discarding unsaved visual edits

1 participant