Skip to content

Decide the Compare decoration source for mark-only and attribute-only edits #207

Closed

Description

@HMarzban

Resolved 2026-09-01. A custom TokenEncoder now feeds ChangeSet.create, so formatting edits paint. Verified in a real browser against the live 27-node schema. See the build comment below.

Problem

Open version history, turn Compare on, and pick two versions. Three real edits paint no decoration:

  • text made bold, or any other mark change
  • a link whose href changed while its text did not
  • a heading moved from level 2 to level 3

The reader sees an unchanged document. The edit is there, and the view says it is not. That is worse than showing nothing, because the reader trusts the answer.

The cause is the diff source. buildCompareDecorations calls ChangeSet.create(docA) with one argument (apps/webapp/src/components/pages/history/utils/compareDecorations.ts:26), so it takes the library default token encoder. The Compare hook calls that function at apps/webapp/src/components/pages/history/hooks/useHistoryCompareDecorations.ts:73. The import comes from @tiptap/pm/changeset (same file, compareDecorations.ts:2), which re-exports [email protected].

That default encoder keys a character on its character code alone, and a node on its type name alone. Marks and node attributes never reach a token. The package documents the same limit on ChangeSet.create.

All three symptoms follow. Hyperlink is a mark (extensions/extension-hyperlink/src/hyperlink.ts:130), and href is one of its attributes (hyperlink.ts:214), so neither reaches a token. Heading stays one node type, and the webapp registers levels 1 to 6 as an attribute (apps/webapp/src/components/TipTap/TipTap.tsx:108). Every level therefore encodes as the same token.

What to decide

Which source Compare uses for its decorations. Two options. No build should start before the ruling.

Keep the current diff and state the limit. Compare stays as it is. The Compare UI tells the reader that a styling-only edit paints no decoration. Cheap, honest, and the reader still cannot find the edit.

Replace the decoration source with the backend block comparison. The backend already ships one. diffBlocks decodes each side through decodeSide (apps/hocuspocus.server/src/modules/document-versions/domain/diffBlocks.ts:32, called at diffBlocks.ts:66). decodeSide hashes each top-level block from its canonical JSON (diffBlocks.ts:43). That JSON sorts the attribute keys, and drops the volatile toc-id plus every null-valued attribute (apps/hocuspocus.server/src/modules/document-versions/domain/canonicalizeBlock.ts:10, apps/hocuspocus.server/src/modules/document-versions/types.ts:221). It is served at GET /api/documents/:documentId/versions/:version/diff. The router registers diffPath (apps/hocuspocus.server/src/modules/document-versions/http/router.ts:46) on a get route (router.ts:78-79), and the app mounts that router under /api/documents (apps/hocuspocus.server/src/index.ts:92).

Three costs come with the second option. The diff route takes the service-role guard (router.ts:80), so the history client cannot call it today. It reports whole blocks, with one from and to per block, not character ranges (types.ts:231). Decorations therefore get coarser. It decodes through the migration schema (diffBlocks.ts:15), not editor.schema, and the two schemas already disagree.

Acceptance

  • A reviewer bolds text in one version, opens Compare, and sees a decoration or a stated limit. Verified in the browser 2026-09-01: the decoration paints.
  • The same walk with a changed link href gives the same answer. Verified.
  • The same walk with a heading moved from level 2 to level 3 gives the same answer. Verified.

Notes

The schema disagreement is real. The migration schema declares a color attribute on the highlight mark, and it defaults to null (apps/hocuspocus.server/src/lib/migration-extensions.ts:78). The webapp mark leaves the upstream multicolor option off, so it declares no attribute at all (apps/webapp/src/components/TipTap/extensions/highlight.ts:16). Because canonicalizeBlock.ts:10 drops null attributes, the block comparison reports this gap only for a stored document that carries a non-null color. That gap is filed separately as the highlight parity issue.

The library limit also needs recording in the tree, whichever option wins. That is filed separately as the changeset documentation issue.

Activity

  1. added
    bug
    Editor
    on Aug 31, 2026
  2. HMarzban commented on Sep 1, 2026

    @HMarzban
    CollaboratorAuthor

    Research result — a third option, 2026-09-01

    Full evidence: REPORT-pm-diff-library-research.md.

    Recommended ruling: take neither listed option. Pass a custom TokenEncoder to the diff source Compare already uses.

    ChangeSet.create accepts a TokenEncoder as its third argument. Verified in this tree: [email protected] dist/index.d.ts:65 declares the interface, :156 exports it, and :153 shows the signature. The package already resolves first-party through @tiptap/[email protected]. So this needs no bun add, no bun.lock change, and no new dependency conversation.

    Measured, by building it and running it

    Probe Default encoder Custom encoder
    bold added 0 ranges 1 range, "world"
    link href changed 0 ranges 1 range, "the docs"
    heading 2 to 3 0 ranges 1 range, the node-start token
    one word inserted (control) 1 range identical numbers
    toc-id churn (control) 0 ranges 0 ranges

    The two controls matter as much as the fix. Text precision does not degrade, and volatile identity churn stays silent. Cost on the largest fixture: 0.955 ms against the default's 0.799 ms. A whole-document reformat is 96 ms, a one-off ceiling rather than the everyday path.

    Why the two listed options lose

    Option one, state the limit. The issue says it in its own words: honest, and the reader still cannot find the edit.

    Option two, use the backend block comparison. Three costs are already in this issue. Two more were measured: a one-word edit widens from a 3-position span to a 194-position block, and a removed block carries no from and no to, so the existing <del> widget has nowhere to anchor.

    Build notes

    1. New file apps/webapp/src/components/pages/history/utils/diffTokenEncoder.ts. Intern each distinct mark set to a small integer, then return markSetId * 0x200000 + charCode. Keep node tokens as strings so a node token can never equal a character token. compareTokens stays a === b.
    2. Change one line in compareDecorations.ts:26 to ChangeSet.create(docA, undefined, buildDiffTokenEncoder()), then add two rendering arms. Check after && !after.isText before textA === textB, or an image src change paints as a formatting span. Guard nodeAfter against null.
    3. VOLATILE_BLOCK_ATTRS already exists twice: apps/hocuspocus.server/src/modules/document-versions/types.ts, and a hand-copied new Set(['toc-id']) at apps/webapp/src/components/pages/history/utils/blockAuthors.ts:8. The encoder is a third consumer. Give the webapp one shared home; do not add a fourth copy.
    4. Pin the contiguity merge in a test. Bold on the last word of a paragraph plus a heading change on the next node returns one merged range. Detection is never lost; only rendering fidelity is. It is a known limit, not a defect.
    5. Re-measure in a real browser on the three largest stored documents before shipping. Every number above came from Bun.

    On the 42,489 ms that rejected this twice

    Nobody could reproduce it. A naive fresh-object-per-token encoder measured 142.8 ms here, not 42,489 ms. This is not a claim that the earlier measurement was wrong — it was a different encoder shape against a different input, so the numbers are not comparable. The new fact is the mechanism: an interned-integer encoder keeps the library's cheap prefix and suffix trim alive, which a fresh-object encoder destroys.

    Order

    Rule this issue before writing #206. The note #206 records changes depending on the ruling here.

  3. HMarzban commented on Sep 1, 2026

    @HMarzban
    CollaboratorAuthor

    Built — 2026-09-01

    The third option is implemented. ChangeSet.create now receives a custom TokenEncoder, so mark-only and attribute-only edits produce decorations.

    Changed

    File What
    apps/webapp/src/components/pages/history/utils/diffTokenEncoder.ts New, 52 lines. Interns each distinct mark set to an integer and returns markSetId * 0x200000 + charCode. Node tokens stay strings, so a node token can never equal a character token.
    apps/webapp/src/components/pages/history/utils/compareDecorations.ts One import, one changed call, the node-decoration arm, and an empty-widget guard.
    apps/webapp/src/components/pages/history/utils/compareDecorations.test.ts Four tests and one helper.
    apps/webapp/src/styles/editor/_history-diff.scss One class, .history-diff-attr.

    No new dependency. No bun.lock change.

    A defect the tests caught

    A heading level change returned two decorations, not one. The second was an empty <del> widget: an attribute-only edit changes a node-start token, so the A side reports a span holding no text, and the existing removed-text arm fired anyway. An empty del tells the reader a word went when none did. Fixed at the source with if (!removed.trim()) continue.

    A formatting edit gets an edge, not a tint

    .history-diff-added fills the background, which is right for new words and wrong when the words did not change. .history-diff-attr draws a left border instead, so the reader sees that the block changed without reading it as rewritten text.

    Verified against the real schema, in the browser

    Jest runs on prosemirror-schema-basic. The live page runs 27 node types and 11 marks, and heading carries both toc-id and level. The encoder was run against that live schema:

    Case Token A Token B Differs
    plain versus bold 119 2097271 yes
    href a versus b 4194423 6291575 yes
    heading level 2 versus 3 <heading|level=2 <heading|level=3 yes
    the same bold twice 2097271 2097271 no, stable
    toc-id aaa versus zzz <heading|level=2 <heading|level=2 no, stripped

    The last two rows carry as much weight as the first three. Interning is stable, which keeps the library's cheap prefix and suffix trim alive. toc-id never reaches a token, so the first-open rewrite still paints nothing.

    Version History opened with 11 versions, Compare engaged, and the console carried no errors.

    Gates

    8/8 unit tests, four of them new. typecheck exit 0. eslint --max-warnings=0 clean. prettier --check clean. stylelint clean.

    Still open — the acceptance walk

    The three acceptance boxes are not ticked, deliberately. Nobody has yet bolded text in a real document, opened Compare, and seen the decoration paint. The evidence above is token-level and unit-level, not a screenshot. A reviewer should do that walk in light and dark before this closes.

    Known limit, pinned rather than hidden

    Bold on a paragraph's last word plus a heading change on the very next node merge into one range. Detection is never lost; only rendering fidelity. It is a library behaviour, not a defect in this change.

  4. HMarzban commented on Sep 1, 2026

    @HMarzban
    CollaboratorAuthor

    Confirmed working in the browser on 2026-09-01. A real document, Compare across Aug 19, 1:26 AM → Sep 1, 4:46 AM, and formatting edits now paint. A replacement that changed both the words and their formatting rendered the new text in its new weight, which the default encoder reported as no change at all. Closing.

  5. added a commit that references this issue on Sep 2, 2026
    9690787
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Editorbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions