Skip to content

A restore of a Discard leaves out a draft whose Live text was edited since - #1735

Merged
haowjy merged 12 commits into
feat/document-commands-never-refusefrom
fix/server-discard-undo-edited
Nov 16, 2026
Merged

haowjy merged 12 commits into
feat/document-commands-never-refusefrom
fix/server-discard-undo-edited

Conversation

@haowjy

@haowjy haowjy commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Changelog

  • Fixed: Undoing a Discard all, or restoring it from Trash, no longer brings the AI's change back over a word you typed in Live since. It says "1 change couldn't come back: the text changed after it was discarded." and the draft keeps your word.

Why

Fixes #1733. Found while fixing #1727.

A server Discard (Discard all in the Changes list, or any Discard that closes a draft) has an Undo that reopens the closed draft as it was, with its old base. A closed draft is not pulled into, so its base is the Live text it last saw. After the writer typed over the discarded word in Live, the next pull merged Live into the reopened draft with contentMerge, where the draft's side wins a conflict. The AI's word came back as a proposal over the writer's, with no line saying so. The room Discard's Undo (the chat strip and the editor, #1734) already leaves that out and says so, so two Undos of the same kind of action disagreed.

After merge, both Undos follow one rule: restoring a discarded draft leaves out a change whose Live text was edited since it was discarded, counts it in leftOut, and words it the same way.

Examples

Undo Discard all after typing over the discarded word in Live

  1. Setup: MODEL_PROVIDER=mock pnpm dev. ./mf doc put manuscript://Gate --text "The old gate stood at the end of the road.". A new chat in Draft mode whose mock reply replaces "old gate" with "crumbling gate" (./mf mock script). Open Gate beside the chat.
  2. In the Changes list press Discard all.
  3. In Live, select "old" and type "grey". Press the row's Undo discard of Gate at once, or wait 12 s first.
  4. Expect: on the base branch the draft returns with the AI's change over "grey". On this branch the row says "Not restored" (or "Partly restored" when other documents came back) and its line is "1 change couldn't come back: the text changed after it was discarded." and Live keeps "grey".
Before (base branch, Undo at once) After: Undo at once After: Undo after 12 s
before after at once after 12 s

An Undo with no Live typing restores everything

Same steps without step 3: the draft comes back with its change.

no typing

Restore from Trash follows the same rule, and a fully left-out entry leaves Trash

Same setup, Discard all, type "grey" in Live, then open Trash and choose Restore. The row is struck through with "Not restored" and the line "1 change couldn't come back: the text changed after it was discarded."; after a refetch the entry is gone from Trash. Restoring it again has nothing to restore.

Right after Restore After reload
row gone

An edit right beside the AI's change also leaves it out

Discard all, then type "big " in front of the discarded word. The room path's touchedSince counts an edit beside the change, so Undo now does too: the row reads "Not restored".

beside

A partial restore

Two documents in one Discard, one typed over: Restore brings Road back and leaves Gate out ("Drafted 2 documents: 1 discarded, 1 still a draft").

partial

A fully left-out restore is settled in the notice

Discard all, type over the discarded word in Live, press the chat's Undo. The notice becomes "1 change couldn't come back: the text changed after it was discarded." with no Undo (before, it kept the "Discarded just now" line and an Undo that could only send a second restore).

notice

Trash's Undo takes back exactly what the restore brought back

A Work document has a drafted rename and a text change. Discard all, type over the word in Live, Restore from Trash: the rename is back as a pending change and the text stays out. Then Trash's Undo: the rename goes, the entry returns to Trash.

After Restore (rename pending, text left out) After Trash's Undo
restore undo

One "edited since" rule, run on both paths

Base "The old gate stood at the end of the road."; the AI changed "old" to "crumbling"; Discard; one Live edit; Restore. Left column: the room's touchedSince on a Y.Doc (real writeDiscardBlocks, markStepDone, a Live write from another client). Right column: the server's Restore over Postgres (real Discard kernel, Live edit through journal.append, undo_discard → reopen). Scratch probes, not kept.

Live edit after the Discard room server
none restored restored
"old" to "grey" (inside) left out left out
insert " big" before "old" (beside, insertion) left out left out
"gate" to "gates" (next word) restored restored
"The" to "A" (word before) restored restored
bold on "gate" (marks of the next word) restored restored
AI changed "old" and "stood"; Live "gate" to "door" (the word between) restored restored
"stood" to "stands" (two words away) restored restored
delete "old " left out left out
join "First line." and the paragraph holding the change left out left out
split the paragraph away from the change restored restored
split it right after the change restored restored

Before: the server left out the next word, the word before, the bold and the word between; the room restored them.

Third round. AI insertion rows (base as above; the AI inserted " big" before "gate"), AI deletion and new-paragraph rows, retyping, and long paragraphs (words w0 w1 …, the AI changed w500 or w5000):

AI change, then Live edit room server
insertion; "gate" to "gates" (next word) restored restored
insertion; "old" to "older" (word before) restored restored
insertion; "stood" to "stands" restored restored
insertion; Live deletes "gate" restored restored
insertion; "old" to "new" restored restored
insertion; Live inserts " huge" at the same spot left out left out
insertion; "old gate" to "ancient door" (one range around it) left out left out
insertion; "old" to "older" and "gate" to "gates" in one edit restored left out
deletion of "old "; "gate" to "gates" restored restored
deletion of "old "; "The" to "A" restored restored
new paragraph after "First line."; Live "First line." to "First lines." restored restored
"old" to "grey", then "grey" back to "old" (retyping) left out restored
1,000 words, w500 changed; Live edits w510 restored restored
1,000 words; Live edits w500 itself left out left out
1,000 words; Live edits w501 (next to it) restored restored
1,000 words; two separate Live edits at w5 and w995 restored restored
10,000 words, w5000 changed; Live edits w5010 restored restored
10,000 words; Live edits w5000 itself left out left out
10,000 words; Live edits w5001 restored restored
10,000 words; two separate Live edits at w5 and w9995 restored restored
10,000 words; Live edits 400 words spread over it, not w5000 left out restored
10,000 words; Live rewrites every word, or every 5th (over 1,000 edit operations; Live's edit is a fallback hunk and is no evidence) left out left out
10,000 words, the AI changed w5000; Live edits words 100–350 and 9000–9250 (past the bound, so the diff is one coarse replacement) restored restored
100 paragraphs of 100 words, the AI changed one; Live edits another paragraph, or another word of that one restored restored

Where the two paths differ, and why (two cases; the third bullet is a limit of my probe):

  • Retyping (the writer types over the changed word and types the original back). The rule is the text, and it is as the Discard left it, so the server restores. The room leaves it out because its Yjs Undo would double the text (probe: "The crumblingold gate stood…"). Documented beside touchedSince and editedSince.
  • Any edits to both words neighbouring an AI insertion (at one time or two: the server reads the final text). The server compares word tokens, and one hunk covers both words and the point between them. The room's writer edits at character level (old to older inserts "er"), so it sees two edits beside the insertion. The server leaves out; nothing is lost. Rare.
  • 400 spread edits. The room column is a limit of my probe: it writes Live's text with writeBlocks, which replaces everything between the first and last edit as one range above 4,000 keys, so the room sees the AI's text rewritten. Real typing arrives as separate edits (the two-edit rows above). The server reads the final text, so it is exact.

Real database

A rename plus a text change on one document, Discard, type in Live, Restore: restored 1, leftOut 1, brought { text: [], tree: [doc] }. Trash's Undo sends that document with ends: "tree"; the Work's rename record is gone afterwards (the old filter sent no document, closed nothing and left the rename pending). A fully left-out draft reads closed / left_out / closed_by = Discard; listWorkDraftStates answers discarded, Trash's entry list is empty, a second Restore answers already, and the day-30 purge deletes the draft with its Discard.

Benchmarks

Restore end to end over Postgres, which holds the document locks throughout (one run each, local): short paragraph 13–19 ms; one 10,000-word paragraph 25–35 ms; a 100-paragraph chapter 31–35 ms; a 10,000-word paragraph rewritten past the 1,000-operation bound (the coarse fallback) 34–42 ms. The long-paragraph diff is a Myers search capped at 1,000 edit operations, so its cost follows the edits, not the size or how unlike the texts are.

Design decisions

  1. The check lives in reopen, behind leaveOutEdited. The notice's Undo, Trash's Restore and the editor's Restore all reach undo_discard → reopen, so one check covers each. Un-Apply's reopen also uses reopen but must not ask: its own Apply wrote the Live text the draft's base lacks, so the merge would always conflict. Ruled out: a client check, which Trash and a second tab cannot run, and a check inside undo-discard.ts, which would read the draft twice.
  2. "Edited since" is one rule on two representations. A Live edit leaves a draft out when its range overlaps a change's range, or when Live's edit is a pure insertion at the change's edge or inside it (a neighbouring word's replacement shares an endpoint and does not reach an AI insertion). The server runs it on uncleaned token ranges (changed.raw against Live's raw hunks), after replaying the draft's waiting Un-Applies onto a copy of its base. A neighbouring word, the marks of the next word and a split of a block do not count. A join into the block before (or a new block type) counts, on both paths. I was asked to make a block change that spares the change's own text not count on both; for a join that cannot hold in the room: Yjs's Undo of the Discard cannot follow the text into the joined block (probe: Undo after the join leaves "First line.The old gate stood." with the AI's change lost), so touchedSince keeps its answer and the server now matches it. touchedSince itself is unchanged apart from its comment.
  3. A left-out draft keeps its Discard and gets its own cause. closed_cause = left_out (migration: the check constraint), closed_by unchanged. Trash's entry list and closedBy() skip it; the purge, the model's write path (listWorkDraftStates reads it as discarded) and receipts read it as the Discard's draft. Reopening it empty would leave nothing to review, and clearing closed_by had made it read as "the Work was purged".
  4. The app reads left_out changed_since as its own restore state. The answer is restored: 0, which the app treated as "gone from Trash". It now answers the notice and Trash's row with the room path's sentence (leftOutSentence).
  5. Edited-since ranges are exact. unitHunks made a unit over 1,000 tokens one whole replacement, so any Live edit in a long paragraph left a change out for good. Those units are now diffed by token key with boundedDiff (Myers, exact up to 1,000 edit operations, then one replacement of what the ends do not share, flagged approx). The bound counts edit operations, so a replaced word costs 2 and the bound is about 500 replaced words. editedSince takes an approx hunk, on either side, as "can't tell" and restores: a wrongly restored proposal is one the writer sees and can discard, a wrongly left-out one is lost for good. The display keeps the fallback. It lives in align.ts's oversized branch, so review hunks in long paragraphs are exact too; one rule for display and for this check. Ruled out: a second diff used only here, and raising the size cap, which makes dissimilar paragraphs quadratic.
  6. Retyping follows the text. See the table: the server restores, the room leaves it out. Ruled out: a history footprint shared by both paths, a project of its own whose only gain is the retyping row, which loses no text either way.

Changes

# Part Why it's here Key paths Source Tests Docs
1 reopen leaves out an edited draft The rule drizzle-reopen-drafts.ts, composition.ts +50 / -5 0 0
2 Restore counts and says it leftOut, left_out changed_since drizzle-undo-discard.ts +15 / -3 0 0
3 App words it, and a left_out restore state Notice, Changes row, Trash row, editor pill CommandNotice.tsx, useWorkBranchCommands.tsx, TrashDiscardRow.tsx, locale catalogs +19 / -9 0 0
4 Test and probe wiring reopen takes the Live journal 3 test and probe files +4 / 0 +4 / 0 0
5 Docs The rule and the app's wording apply.md, draft-review.md 0 0 +9 / -2
6 Exact ranges for long paragraphs; third-round fixes boundedDiff in align.ts's oversized branch, reaches, already on a second Restore, ofDiscard, brought from reopen, "Not restored", Trash row's left-out line bounded-diff.ts, align.ts, drizzle-reopen-drafts.ts, drizzle-undo-discard.ts, drizzle-command-documents.ts, draft-notices.ts, TrashDiscardRow.tsx +203 / -30 0 +14 / -4
Total (14 files) +88 / -17 +4 / 0 +9 / -2

Compatibility and risk

  • Breaking or changed for writers: an Undo or Restore of a server Discard no longer reopens a draft whose Live text was edited since. It says so.
  • Migrations and state: the baseline's document_drafts_closed_cause_valid check gains left_out (regenerated with pnpm db:generate; a dev database needs pnpm db:reset). The restore answer gains brought.
  • Clients during a deploy: an older app gets restored: 0 and shows "Gone from Trash" for that case until it reloads.
  • Rollback: reverting leaves nothing behind.
  • Where to look: editedSince in drizzle-reopen-drafts.ts. It reads the draft and the Live journal under the document locks undo_discard already holds, so Agents and skills are documents you can edit #1534's lockDocumentMutation and B4's purge order are unchanged.

Verification

  • Browser: agent-browser on the mock stack, before and after, as in Examples: Undo at once, after 12 s, with no typing, and Restore from Trash.
  • Model: mock provider, since the model's output is a scripted replace.
  • Tests: none added; pnpm check passes. Existing: pnpm test:db -- drizzle-apply-restore branch-tree.apply-discard passes (24 tests), including the restores that reopen drafts after Un-Apply. branch-tree.apply-discard now passes the real journal instead of a stub.
  • Browser (agent-browser, mock stack, a Work in Draft mode): the notice after a fully left-out restore (no Undo), and Trash's Restore then Undo of a document with a rename and a left-out text change.
  • Not exercised in the browser after the third round: Trash's restored row now shows what stayed out, and a notice where nothing came back reads "Not restored" (checked by typecheck and reading, not a browser run). A notice of two documents, one restored and one left out, reads "partial" and not "Not restored": run against the real DraftNotices with handles that answer as a room does (ok, and gone with a left-out count). The coarse-fallback row ran through the real Discard and Restore over Postgres; it left the draft out before this fix and restores now. The table ran on the real code of both paths.
  • Gates: pnpm check.

Deferred

  • diffKeys (tokens.ts, the room's write path) still falls back to one coarse replacement past 4,000 keys. It could use boundedDiff for that middle, which makes the room's writes exact on long paragraphs and leaves one bounded diff. Its own PR.
  • boundedDiff copies its whole diagonal array each round (about 8 MB of short-lived garbage per exhausted search); saving only the diagonals in use would halve it.

References

Spawn Agent Model Role
p18485 coder Sonnet 5.5 Implemented and verified.

…since (#1733)

Undoing a server Discard (Discard all, or a Restore from Trash) reopened the
closed draft as it was, so its change came back over a word the writer typed
in Live meanwhile. The room Discard's Undo already leaves that out.

`reopen` now takes `leaveOutEdited`: it merges the draft's base (what Live
last showed it, since a closed draft is not pulled into), Live now and the
draft's text, and a conflict keeps the whole draft closed. The restore counts
its changes in `leftOut` and says `left_out changed_since`. The app words it
as the room path does, on the Changes row and on Trash's row.
@haowjy
haowjy marked this pull request as ready for review November 16, 2026 21:57
haowjy and others added 9 commits November 16, 2026 17:04
…pan, and takes a left-out entry out of Trash (#1733)

A restore of a Discard now judges 'edited since' on the draft's base with its waiting Un-Applies replayed, and as the room path's touchedSince does: Live editing, or writing beside, what the draft changed. A left-out draft stays closed with closed_by cleared, so the entry leaves Trash instead of leaving the same thing out on every Restore. Trash's Undo of a partial restore discards only the documents the restore reopened.
…use' into fix/server-discard-undo-edited

# Conflicts:
#	apps/app/src/features/draft-review/CommandNotice.tsx
#	apps/app/src/features/draft-review/draft-notices.ts
#	apps/app/src/locales/en/messages.po
#	apps/app/src/locales/en/messages.ts
#	apps/app/src/locales/zh/messages.po
#	apps/app/src/locales/zh/messages.ts
…hes the room's; a left-out restore is settled; Trash's Undo takes back what the restore brought (#1735)

A draft Restore leaves out keeps closed_by (the day-30 purge, the model's write path and receipts read it as the Discard's draft) and takes closed_cause 'left_out'; Trash's entry list and closedBy() skip it. The edited-since check compares uncleaned token ranges and counts a shared endpoint only for a pure insertion, so a neighbouring word, its marks or a split no longer leave a draft out; a join into the block before still does, as in the room. DraftNotices counts a fully left-out share as settled. The restore answer names the halves it brought back (brought), and Trash's Undo discards exactly those.
…d does not reach an AI insertion; small Restore fixes (#1735)

An oversized unit (over 1,000 tokens) was one whole-paragraph replacement, so any Live edit in a long paragraph left a restorable draft out forever. Its token keys are now diffed by a bounded Myers (exact up to 1,000 changed tokens, cost bounded by the edit distance), which also makes review hunks in long paragraphs exact. A shared endpoint reaches a change only when Live's edit is the pure insertion. reopen returns the documents whose draft came back, so undo-discard stops re-deriving them. A second Restore of a fully left-out entry answers already; ofDiscard skips left_out drafts; a notice where nothing came back reads Not restored; Trash's restored row says what stayed out. The room/server difference on retyping is documented beside touchedSince.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… is not "Not restored"; the bound counts edit operations (#1735)

boundedDiff says whether it was exact, and the hunks of a fallback carry approx. editedSince ignores an approx hunk on either side, so Live edits at both ends of a long paragraph no longer leave a proposal in the middle out; the display keeps the fallback. The notice's nothingBack also remembers a room share that undid. Docs and comments say the bound is 1,000 edit operations, about 500 replaced words.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@haowjy
haowjy merged commit e036598 into feat/document-commands-never-refuse Nov 16, 2026
11 checks passed
@haowjy
haowjy deleted the fix/server-discard-undo-edited branch November 16, 2026 23:47
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