Skip to content

fix(editor): protect file content when a diff fails to load - #25407

Open
nwparker wants to merge 4 commits into
mainfrom
nwparker/editor-diff-load-error
Open

nwparker wants to merge 4 commits into
mainfrom
nwparker/editor-diff-load-error

Conversation

@nwparker

@nwparker nwparker commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​572 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​569
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​33 $\color{#cf222e}{\Huge{\mathbf{−}}}$​28 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5

ELI5

A failed diff load could put its error message into the editable pane. Pressing Save then replaced the real file with that message. The error pane now stays read-only until real content loads.

What Changed

Keep a local load-failure flag, use it to disable editing and Save, and clear it after a successful reload or genuine file save. An existing draft remains editable, including an empty draft. Editing callbacks now follow the mounted editor's editable state, so recovery works without remounting or moving focus.

Why

This reuses the existing diff load and editor lifecycle instead of adding another error screen or save path. Production code grows by five lines. Adapted from @nwparker's #14509, retaining the original author and co-author credit.

Linked Issue

Supersedes #14509.

Visual Proof

The before/after panes display the same load error. The hidden-app test also checks actual disk bytes: before, Save writes the error message; after, Save makes no write and preserves PROTECTED_FILE_CONTENT. Recovery then allows a normal edit and Save.

Before: error text was editable and saved over the file.

Before: failed diff after Save

After: the error stays visible, and Save leaves the real file untouched.

After: read-only failed diff

After a successful reload, editing and Save work again.

Recovered diff saved

Testing

  • Manually validated through the hidden Electron renderer on macOS: baseline overwrote actual file bytes; fixed behavior passed three repeated runs, including reload, typing, and Save.
  • Automated tests added: read-only errors, retained/empty drafts, reload without a remount, callback positive controls, StrictMode cleanup, and genuine-save recovery. Focused suites pass 39/39.
  • Full node/web/CLI typecheck and changed-file quality checks pass; fresh hidden-app build passes. Windows, Linux, and live SSH have not been run for this patch. Existing host routing and wire formats are unchanged.

Review

Independent production review found no blocker. Integration with the separate stable diff-model identity fix will be checked before ready.

Agent skill upstream boundary

  • Not applicable; no agent skill source or registry changes.

Notes

A failed load without a retained draft intentionally disables editing until reload succeeds. Saved drafts remain usable. No dependency or library changes.

Checklist

  • Small and focused
  • User-facing before/after, mechanism, and approach explained
  • Before/after proof attached
  • Self-reviewed for correctness, security, and performance
  • Cross-platform and SSH impact considered
  • Focused tests, typecheck, changed-file quality, and build pass; full lint remains covered by CI

orca-diff-error-before

orca-diff-error-after

orca-diff-error-recovered

Shared CI repair

This branch includes the three-identifier repair from #25494, which removes two unchanged localization-scanner false positives. The editor patches and contributor authors are preserved; every editor commit patch was compared across the rebase. Fresh affected regression suites, changed-code quality and localization coverage pass on this head. The original rendered proof still applies to the unchanged editor patches. Current-head full CI remains the final gate.

@nwparker
nwparker marked this pull request as ready for review October 5, 2026 05:57
@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

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: 76862d0a-361f-423d-96fc-947a38264df4
📥 Commits

Reviewing files that changed from the base of the PR and between 8498bc0 and f0cfa16.

📒 Files selected for processing (2)
  • src/renderer/src/components/editor/editor-panel-content-types.ts
  • src/renderer/src/lib/worktree-creation-hook-preparation.ts

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


📝 Walkthrough

Walkthrough

Diff content now records read failures. The editor surface disables editing for failed diff content unless an edit buffer exists. Saved content clears the failure marker. DiffViewer now installs and cleans up save, find, and content-change callbacks through an effect. Added tests cover callback lifecycle, failure handling, and recovery after a successful reload or save.

Priority: ⬆️ High

Merge Risk: ⚪ Minimal · up to f0cfa

Failed diffs remain read-only until valid content is restored, and the inspected save paths show no reachable overwrite route. The change appears ready for normal merge checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to f0cfa

The change reduces accidental file overwrites without adding new access or execution capabilities. Remaining uncertainty concerns unusual recovery states across separate save paths, not an identified new security defect.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The relevant outcome is overwriting the selected editor file through existing runtime file-write authority. The inspected change narrows the diagnostic-to-write path; it does not expand the target file context, connection selection, or file-access authority used by the save queue.

Security Findings and Attack Paths

  • inferred — The separate command-save route does not consult loadError and could supply an empty fallback for a dirty diff without a draft. This behavior predates the PR, and no increased exposure was established. Inspected discard and successful-save paths clear dirty state before removing drafts; reload clears both, and undo restores a draft before marking dirty. This conditional residual behavior is not a verified attack path or an active PR concern.

Trust Boundaries and Controls

  • observed — The new gate distinguishes failed-load diagnostic content from an existing user draft before wiring editing callbacks. The unchanged save queue separately rechecks live file identity and read-only status and derives runtime context and file access from the live file record.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly states the primary change: protect file content when a diff fails to load.
Description check ✅ Passed The description covers the required sections and explains the user impact, implementation, visual proof, and testing. The Linked Issue section references a superseded pull request rather than an issue…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
  • Autopilot · 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 force-pushed the nwparker/editor-diff-load-error branch from 8498bc0 to 4b69a06 Compare October 5, 2026 09:33

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

  • Added a loadError marker to failed diff reads — DiffContent gains loadError?: boolean, and useEditorPanelDiffContentLoader sets it on the catch-path text result so consumers can tell an error pane from real content.
  • Made a failed diff read-only unless a draft exists — EditorDiffFileSurface now derives isEditable from diffSource === 'unstaged' && (loadError !== true || editBuffer !== undefined), so an error message with no retained draft cannot be edited or saved, while a retained (even empty) draft stays saveable.
  • Recovered editing after a genuine save — useEditorPanelExternalContentEvents strips loadError and rebases modifiedContent on ORCA_EDITOR_FILE_SAVED_EVENT, so the pane stays editable once the draft is cleared.
  • Reworked DiffViewer subscription lifecycle — the save shortcut, both Monaco find shortcuts, and the content-change subscription moved from the onMount callback into a useEffect([editable, modifiedEditor]), allowing editable to flip without remounting or moving focus.
  • Added focused regressions — unit suites for subscription lifecycle, surface editability, loader marker set/clear, and save-marker clearing, plus a hidden-app e2e that asserts on-disk bytes are preserved when Save is pressed over a failed diff.

I audited every save/write path: all editor writes funnel through writeRuntimeFile, and each path (autosave, save-all, save-and-close, global Cmd+S) requires a draft or dirty state. The error string cannot become a draft or fallbackContent, so it cannot reach disk. Changes mode hardcodes editable={true} but renders the modifiedContent prop rather than the failed dc.modifiedContent, so it is not a leak; combined-diff sections use a separate loader and blank the body on error.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

This branch has not been deployed

No deployments
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