Skip to content

diff.compare pairs paragraphs by position since @superdoc/sdk 2.27.0: one edit is reported as a cascade #4166

Todo

Description

@nandoofz

Summary

Since @superdoc/sdk 2.27.0, diff.compare pairs body paragraphs by position instead of by content. A single inserted or deleted paragraph is reported as a chain of replacements covering every paragraph after it, so paragraphs that are byte for byte identical in both documents come back as modified.

On 2.26.0 the same two pairs are reported as exactly one deletion and one insertion.

This is the second regression we found in 2.27.0. The first one is #4165.

Versions

Six plain paragraphs in the base. One target removes the third paragraph; another adds one paragraph after the second.

@superdoc/sdk one paragraph removed one paragraph added
2.26.0 1 difference (delete) 1 difference (insert)
2.27.0 4 differences 5 differences
2.28.0 4 differences 5 differences
2.30.0 4 differences 5 differences
2.31.0 4 differences 5 differences

Node 26, Windows x64 (@superdoc/sdk-windows-x64).

What 2.31.0 answers for the added paragraph, from payload.applyPlan.bodyOperations:

replace | "Charlie terceiro paragrafo do documento" -> "Golf paragrafo inteiramente novo"
replace | "Delta quarto paragrafo do documento"    -> "Charlie terceiro paragrafo do documento"
replace | "Echo quinto paragrafo do documento"     -> "Delta quarto paragrafo do documento"
replace | "Foxtrot sexto paragrafo do documento"   -> "Echo quinto paragrafo do documento"
insert  | ""                                       -> "Foxtrot sexto paragrafo do documento"

Every paragraph slid down by one. Only "Golf" is new, and four untouched paragraphs are reported as replaced.

And for the removed paragraph:

delete  | "Foxtrot sexto paragrafo do documento" -> ""
replace | "Charlie terceiro paragrafo do documento" -> "Delta quarto paragrafo do documento"
replace | "Delta quarto paragrafo do documento"     -> "Echo quinto paragrafo do documento"
replace | "Echo quinto paragrafo do documento"      -> "Foxtrot sexto paragrafo do documento"

"Charlie" is the paragraph that was removed, and "Foxtrot" is reported as deleted although it is present in both documents.

On 2.26.0 the body family of payload.semanticAnalysis.familyDeltas carried a single operation instead:

{ "kind": "delete", "entityKey": "00000003", "baseId": "00000003", "targetId": null, "reviewType": "deletion" }

Reproduction

This script writes the three documents, so there is no attachment to download:

import zipfile

BASE = [
    "Alfa primeiro paragrafo do documento",
    "Bravo segundo paragrafo do documento",
    "Charlie terceiro paragrafo do documento",
    "Delta quarto paragrafo do documento",
    "Echo quinto paragrafo do documento",
    "Foxtrot sexto paragrafo do documento",
]
REMOVED = [p for i, p in enumerate(BASE) if i != 2]
ADDED = BASE[:2] + ["Golf paragrafo inteiramente novo"] + BASE[2:]

CONTENT_TYPES = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
    '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
    '<Default Extension="xml" ContentType="application/xml"/>'
    '<Override PartName="/word/document.xml" '
    'ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>'
    '<Override PartName="/word/styles.xml" '
    'ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>'
    "</Types>"
)
PACKAGE_RELS = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
    '<Relationship Id="rId1" '
    'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" '
    'Target="word/document.xml"/></Relationships>'
)
DOCUMENT_RELS = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
    '<Relationship Id="rId1" '
    'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" '
    'Target="styles.xml"/></Relationships>'
)
STYLES = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
    '<w:style w:type="paragraph" w:default="1" w:styleId="Normal">'
    '<w:name w:val="Normal"/><w:qFormat/></w:style></w:styles>'
)
SECTION = '<w:sectPr><w:pgSz w:w="11906" w:h="16838"/></w:sectPr>'

def paragraph(text):
    return '<w:p><w:r><w:t xml:space="preserve">' + text + "</w:t></w:r></w:p>"

def write(name, paragraphs):
    document = (
        '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
        '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
        "<w:body>" + "".join(paragraph(p) for p in paragraphs) + SECTION + "</w:body></w:document>"
    )
    with zipfile.ZipFile(name, "w", zipfile.ZIP_DEFLATED) as z:
        z.writestr("[Content_Types].xml", CONTENT_TYPES)
        z.writestr("_rels/.rels", PACKAGE_RELS)
        z.writestr("word/document.xml", document)
        z.writestr("word/_rels/document.xml.rels", DOCUMENT_RELS)
        z.writestr("word/styles.xml", STYLES)

write("base.docx", BASE)
write("target-removed.docx", REMOVED)
write("target-added.docx", ADDED)
import { createSuperDocClient } from '@superdoc/sdk'

const client = createSuperDocClient({ user: { name: 'alignment' }, requestTimeoutMs: 120_000 })
await client.connect()
const baseDoc = await client.open({ doc: 'base.docx' })
const targetDoc = await client.open({ doc: 'target-added.docx' })
const snapshot = await targetDoc.diff.capture({})
const diff = await baseDoc.diff.compare({ targetSnapshot: snapshot })

for (const op of diff.payload.applyPlan.bodyOperations) {
  for (const pair of op.pairs ?? []) console.log(op.kind, '|', pair.base?.text, '->', pair.target?.text)
  for (const b of op.removed ?? []) console.log(op.kind, '|', b.text, '-> ""')
  for (const b of op.inserted ?? []) console.log(op.kind, '| "" ->', b.text)
}
await client.dispose()

Expected: one difference, naming the paragraph that was actually added or removed.
Actual: from 2.27.0, four or five differences, most of them naming paragraphs that are identical in both documents.

Why it matters to us, and why a round trip does not catch it

The text still round-trips correctly: accepting every mark gives the target, rejecting every mark gives the base. So the document is never wrong. What is wrong is what a reader sees, because a paragraph nobody touched comes back struck through and retyped. In a legal review that is the difference between a usable redline and one a lawyer has to re-read in full.

It also makes any correlation by text ambiguous for us, which is why we keep this measurement as the first check we run on a new version.

Happy to test a build if that helps. Thanks for looking at this.

Activity

  1. added
    status: triage
    on Nov 23, 2026
  2. added theissue type on Nov 23, 2026
  3. nandoofz commented on Nov 23, 2026

    @nandoofz
    Author

    A note on how we can help verify a fix here.

    We are currently pinned to 2.26.0 by #4165, which is a breaking change for a feature we have in production. So while that one is open we cannot run any release from 2.27.0 onward, and we will not be able to confirm a fix for this issue on a newer version either.

    That is not a reason to prioritise this one over that one, and we are not asking for that. We mention it only so the order is clear: once #4165 is resolved we can test both on the same build and report back here with the numbers from the reproduction above.

    Thanks again for looking at these.

  4. added
    status: investigating
    and removed
    status: triage
    on Nov 24, 2026
  5. linear-code commented on Nov 24, 2026

    @linear-code
  6. added
    status: queued
    and removed
    status: investigating
    on Nov 24, 2026
  7. caio-pizzol commented on Nov 24, 2026

    @caio-pizzol
    Contributor

    Thanks for the clear repro. Confirmed: it's a regression from 2.27.0. It only affects paragraphs without w14:paraId. For those, compare ends up pairing paragraphs by position instead of by content. Files with real paraIds, like ones saved by Word, give the single expected change. We've found the cause and are working on a fix.

    Got it on #4165. We'll let you know here when there's a build to test both on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: queued

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions