Skip to content

fix(editor): cancel paste when its target model changes - #34904

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/editor-paste-target
Oct 29, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/editor-paste-target

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​46 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​45
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​29 $\color{#cf222e}{\Huge{\mathbf{−}}}$​13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​16

Problem

Paste can land in a different model than the one it was aimed at when the editor's model is replaced while the paste is awaiting:

  • Context-menu / command Paste reads the clipboard over IPC. After the await it only re-checked hasTextFocus() (native path), so if the model was swapped with focus retained, e.g. a diff refresh calling setModel on the same diff widget while an agent rewrites the file, the paste went into the replacement model at its cursor.
  • Large (chunked) paste took its target snapshot only after the yielding UTF-8 byte measurement, so a model swapped during measurement was accepted as the target.

Fix

  • Export snapshotMonacoPasteTarget / isMonacoPasteTargetCurrent from monaco-large-text-paste.ts.
  • Context-menu paste snapshots the target (model, container, focus) before the clipboard read and afterwards requires the same target and a writable editor; otherwise it returns noop / target-lost.
  • executeMonacoLargeTextPaste snapshots before measurement and passes the snapshot to insertMonacoTextInChunks, which re-validates it before inserting.

If the race is hit, Paste does nothing instead of inserting into the swapped model. Normal paste behavior is unchanged.

Tests

  • monaco-context-menu-paste.test.ts: native and chunked context-menu paste are rejected when the model is replaced during the clipboard read.
  • monaco-large-text-paste.test.ts: a model replaced during the yielded byte measurement receives no edits.

All three fail without the fix. pnpm check:changed passes.

@OrcaWin
OrcaWin force-pushed the OrcaWin/editor-paste-target branch from 9b00453 to 87d8338 Compare October 28, 2026 22:49
@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:08
@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Keeps paste tied to the editor model it started with.

  • Context-menu paste stays with the editor that started it.
  • Large pastes stay tied to their original editor model.

No actionable issues found. Review used source inspection; tests were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Paste starts] --> B[Capture model and container]
  B --> C[Read clipboard or measure text]
  C --> D{Original target still current?}
  D -->|No| E[Stop without inserting]
  D -->|Yes| F[Insert text]
  F --> G{More chunks?}
  G -->|Yes| H[Yield]
  H --> D
  G -->|No| I[Done]
Loading

Reviews (2) · Last reviewed commit: "fix(editor): cancel paste when its targe..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 8 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

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: 5513f5cb-eb84-41d8-9642-f75e99fa6227
Commits

Reviewing files that changed from the base of the PR and between 87d8338 and 63ee345.

Files selected for processing: 4
  • src/renderer/src/components/editor/monaco-context-menu-paste.test.ts
  • src/renderer/src/components/editor/monaco-context-menu-paste.ts
  • src/renderer/src/components/editor/monaco-large-text-paste.test.ts
  • src/renderer/src/components/editor/monaco-large-text-paste.ts

No actionable comments were generated in the recent review.

Recent 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: cb4490c8-e5a0-46ce-b70f-e987b725ae95
Commits

Reviewing files that changed from the base of the PR and between 3f56b8f and 87d8338.

Files selected for processing: 4
  • src/renderer/src/components/editor/monaco-context-menu-paste.test.ts
  • src/renderer/src/components/editor/monaco-context-menu-paste.ts
  • src/renderer/src/components/editor/monaco-large-text-paste.test.ts
  • src/renderer/src/components/editor/monaco-large-text-paste.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

Large-text paste now captures the editor target before measuring text and rejects chunk insertion if the target is unavailable or no longer current. Context-menu paste validates its captured target after reading nonempty clipboard text. Tests cover model replacement during byte measurement and clipboard reading.

Priority: Normal

Merge Risk: Minimal · previous review 87d83

The paste target is revalidated after asynchronous operations. No merge-blocking risk was identified.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Description check Warning The description explains the problem, fix, and tests, but it omits required template sections and information, including an issue link, Visual Proof or an explicit N/A explanation, ELI5, and checklist… Add the missing required sections. Include a linked issue using Fixes #..., provide visual proof or write N/A with a reason, add the ELI5 explanation, and complete the testing and checklist items.
Docstring Coverage Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: canceling paste when the target model changes.
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 explains the problem, fix, and tests, but it omits required template sections and information, including an issue link, Visual Proof or an explicit N/A explanation, ELI5, and checklist/testing confirmations.

Resolution

Add the missing required sections. Include a linked issue using Fixes #..., provide visual proof or write N/A with a reason, add the ELI5 explanation, and complete the testing and checklist items.

Full details: Docstring Coverage

Explanation

Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 4 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

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.

Snapshot the paste target (model, container, focus) before the context-menu
clipboard IPC read and before the yielding byte measurement of large pastes,
and re-check it afterwards so a model swapped during the await (e.g. a diff
refresh via setModel) does not receive the paste.
@OrcaWin
OrcaWin force-pushed the OrcaWin/editor-paste-target branch from 87d8338 to 63ee345 Compare October 29, 2026 00:27
@OrcaWin
OrcaWin merged commit cdbaccb into main Oct 29, 2026
35 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.

1 participant