Skip to content

Check each edit a draft's edge keys make against how the draft read before - #7117

Merged
max-sixty merged 6 commits into
mainfrom
draft-blank-lines
Nov 25, 2026
Merged

max-sixty merged 6 commits into
mainfrom
draft-blank-lines

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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 (keeps in field-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 main gets wrong: Return in foo_bar_ turned the underscores into emphasis; typing 1. hi after a hard break left the break's \ above a list; Return in the setext heading | xa | b | over --- made a one-column table that dropped b; 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 main made 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 on main beside the existing 10 ms parse, and a key typed among the marks that start a line after a hard break, which main didn't check, takes 13 ms more; typing further along such a line costs what it does on main. Two cases stay as on main: Delete joining # **A to **B over C** moves the bold, and Backspace at # ▏# T takes 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.

This was written by Claude Code on behalf of max-sixty

…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 leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.
@max-sixty
max-sixty merged commit 0669d8a into main Nov 25, 2026
8 checks passed
@max-sixty
max-sixty deleted the draft-blank-lines branch November 25, 2026 06:15
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]>
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.

2 participants