Repository navigation
Check each edit a draft's edge keys make against how the draft read before - #7117
Merged
Merged
Conversation
…efore Backspace and Delete at the edge of a draft's words, Return's break in a paragraph or heading, and typing after a hard break now propose their edits as candidates, and the editor makes the first one under which the draft still reads as before (`keeps` in `field-editor.js`). A candidate passes when the draft shows the same characters, apart from those the edit takes and those the user typed, and when the leaf blocks above and below the lines it edits keep their kinds, depths and lines. Where no candidate passes, the key changes nothing. The check replaces four rules that each guessed at one context: `sameBlocks`, `unwrap`'s guess of whether the paragraph above would take a line in, the heading split's `own` predicate, and the paragraph-span predicates on Return's break and on typing after a hard break. The file's code lines go from 2,255 to 2,251; comments add 22 lines. It fixes three cases the earlier checks let through: - Return in `foo_bar_` wrote `foo\` and `_bar_`, which reads as emphasis and hides both underscores; the `_` is now escaped. - Typing `1. hi` after a hard break left the break's `\` showing above a list, though pasting the same words escaped the `.`; the space after `1.` now runs the check, and the `.` is escaped. - Return in the setext heading `| xa | b |` over `---` left `| x` over `---`, a one-column table that dropped `b`; Return now changes nothing there. To weigh: Return used to fall back to an unchecked edit where no break fitted, and now changes nothing; in the tests and 6,000 fuzzed drafts only the table case reached that fallback. A join that would set an emphasis mark beside a letter, showing `**`, no longer happens, so the fuzzer's exemption for it goes. In a message, Backspace or Delete beside a blank line now writes the edit from the block before to the block after, so the check sees the paragraphs it joins; the result is the same text. Return's other branches, Shift+Return, Tab and typing on a blank line are not routed through the check: their edits change how the draft reads by design. Over 5,000 fuzzed drafts the check would have refused 78 Return, 770 Shift+Return and 1 Tab edits that the tests expect. Cost: each checked candidate renders the draft before and after it (`markdownWords`). On a 16 KB draft in Chromium a render takes 2 to 4 ms beside a 10 ms parse; the render of the draft before is reused while the user keeps typing. Tests: the field-editor tests with three new edge cases and two message cases, `test:runtime` (1,130 of 1,130), the draft-edge browser test in Chromium and Firefox, `pnpm run check`, and the fuzzer over 165,000 drafts (four seeds of 30,000, three of 15,000). max-sixty asked for this after #7112 landed.
A review of the check found eight drafts where it refused a right edit or passed a wrong one; each now does what main did, or better: - Delete on an empty quote line between two lists joined them, since the check compared only leaf blocks; it now compares the lists, quotes and items beside the edit too, so the line stays. - Delete at the end of `# A` above `B` and `C` changed nothing, since `C` was a block the check hadn't seen start below the edit; a block starting below that is what is left of one that ended there passes. - Typing `[link](url)` after a hard break escaped the `[`, since the `)` hides the characters before it; typing may now hide what it completes into markup. - Return with `©` selected changed nothing; what the selected source shows, `©`, may go with it. - A pasted `word` and `---` after a two-space hard break made the paragraph above a heading; typed words after a hard break must again leave it a paragraph, and the check now runs for any paste of several lines there. - Delete on a blank line holding spaces in a message moved the spaces onto `# Head`, which stopped reading as a heading; the line goes whole again, the edit running from the block above so the check sees the paragraphs it joins. - Backspace at `B` in `- A` over `> - B` changed nothing; an item's marker goes plainly where no hard break fits. - Return before a heading's closing `#` opened a second heading; it now acts past the closing marks, as after the heading's last word.
A second review found three drafts where the check refused a right edit, each now as on main: - Backspace at `B` in a one-item list above a blank line and `C` changed nothing, since the parser gives a list the blank lines after it, so the list seemed to end below the edit; a block now ends at its last line that holds anything. - Delete at the end of `- B` above `> C` changed nothing, for the same reason. - Pasting `B` and `©` after a hard break left the break's `\` showing, since the check counted `©` and not the `©` it shows; typed text may show what it spells. Return inside the words of `# Hello #` now moves `llo` below and keeps the closing `#` on the heading, where main moved it below as words.
leaf-agent
reviewed
Nov 25, 2026
leaf-agent
left a comment
Collaborator
There was a problem hiding this comment.
Typing on a line that follows a hard break and starts with punctuation costs about three times as much per key as on main. Every keystroke on such a line now parses and renders the whole draft. The PR body describes this as limited to a space typed at the line's start. Details and measurements are inline.
A line opens a block by the marks, digits and spaces it starts with, so the edit check on a line a hard break opens now runs only for text put among them or just after them, or for a paste of several lines. Before, every key typed anywhere on a line whose words start with punctuation, such as `(see …`, `*emphasis*` or Leaf's own `1\. hi`, parsed and rendered the whole draft. Tend's review of #7117 measured this at about three times main's cost per key. Appending `y` to such a line in a 13 KB draft, under Node, median per key: | line after `A\` | main | before | after | | ---------------- | ------ | ------ | ------ | | `words here` | 2.3 ms | 2.5 ms | 2.7 ms | | `(see words here`| 2.2 ms | 5.4 ms | 2.2 ms | | `1\. hi words` | 2.2 ms | 5.2 ms | 2.3 ms | | `*emphasis words`| 2.2 ms | 5.2 ms | 2.4 ms | Typing ` x` after `1.` still escapes the item's mark (`1\. x`). The field-editor tests pass, and the fuzzer passed 50,000 drafts on two seeds.
leaf-agent
approved these changes
Nov 25, 2026
This was referenced Nov 25, 2026
leaf-agent
added a commit
that referenced
this pull request
Nov 25, 2026
… that line (#7185) Since #7117, Backspace and Delete on a message's blank line propose an edit that runs from the end of the paragraph above, so the draft check (`keeps`) sees the blocks the edit joins. `deleteAtEdge` dispatched that wide edit as written. CodeMirror then rewrote every blank line between the paragraph and the caret with the children they already had, and the teardown's write watch failed both desktop delete cases of `tests/journeys/test_thread_response_continuity.py::test_native_reply_edits_after_semantic_paint_keep_the_first_line` with `unchanged write: children of div.cm-content.cm-lineWrapping`. `deleteAtEdge` still checks the wide edit. It now dispatches the edit without the source the edit puts back as it was (`narrowed` in `field-editor.js`), so the field replaces only the line the key takes. The draft and the caret after the key don't change, since the resulting document is the same. No user-visible behaviour changes apart from the DOM writes that go away. Verification: - All 8 cases of the journey test pass. On `main` (`32dc564a8`) both `*-delete-desktop` cases error at teardown. - A new runtime test, "a key taking a message's blank line replaces only that line", fails without the fix and passes with it. `pnpm run test:runtime` passes 1132 of 1132. - `pnpm run check:browser` and `test_a_draft_nests_its_blocks_as_its_sent_message_does` pass. Closes #7174 — automated triage --------- Co-authored-by: leaf-agent <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backspace and Delete at the edge of a draft's words, Return's line break inside a paragraph or heading, and typing after a hard break now propose their edits as candidates, and the editor makes the first under which the draft still reads as before (
keepsinfield-editor.js). The draft passes when it shows no character it didn't, apart from those the user types; loses none, apart from those the key deletes or the user's typing turns into markup; and keeps the blocks above and below the edited lines, lists and quotes included, of the same kinds on the same lines. Where no candidate passes, the key changes nothing.It fixes four cases
maingets wrong: Return infoo_bar_turned the underscores into emphasis; typing1. hiafter a hard break left the break's\above a list; Return in the setext heading| xa | b |over---made a one-column table that droppedb; and Return inside# Hello #moved the closing#below as words. The check replaces four rules that each guessed at one context (sameBlocks,unwrap's guess of whether the paragraph above takes a line in, the heading split's paragraph predicate, and the paragraph-span predicate on Return's break). It is not a simplification: the file's code lines go from 2,255 to 2,290 and its decision points from 715 to 734, since two Codex reviews found eleven drafts where the first version erred and the fixes taught it to compare containers and the remainder of a block an edit splits.To weigh: where no candidate passes, Return now changes nothing, where
mainmade an unchecked edit; in the tests and 6,000 fuzzed drafts only the table case reached that. Return's other branches, Shift+Return, Tab and typing on a blank line stay off the check, since their edits change the reading by design: over 5,000 fuzzed drafts it would refuse 106 Return, 56 Shift+Return and 14 Tab edits the fuzzer accepts. Each checked candidate renders the draft (markdownWords); on a 16 KB draft in Chromium, Backspace, Delete and Return at these edges take 2 to 3 ms more than onmainbeside the existing 10 ms parse, and a key typed among the marks that start a line after a hard break, whichmaindidn't check, takes 13 ms more; typing further along such a line costs what it does onmain. Two cases stay as onmain: Delete joining# **Ato**BoverC**moves the bold, and Backspace at# ▏# Ttakes the visible#; comparing the words of each surviving source range, rather than counts of characters, would catch both.max-sixty asked for this after #7112 landed. Tests: the field-editor tests with sixteen new cases,
test:runtime(1,130 of 1,130), the draft-edge browser test in Chromium and Firefox,pnpm run check:browser, and the fuzzer over 365,000 drafts across the three revisions; the final revision passed 100,000 on four seeds.