Skip to content

fix(docx): prevent tracked change w:id collision with existing bookmarks - #541

Open
Lubrsy706 wants to merge 1 commit into
anthropics:mainfrom
Lubrsy706:fix/docx-tracked-change-id-collision
Open

Lubrsy706 wants to merge 1 commit into
anthropics:mainfrom
Lubrsy706:fix/docx-tracked-change-id-collision

Conversation

@Lubrsy706

Copy link
Copy Markdown

Summary

Fixes document corruption when the DOCX skill adds tracked changes to documents with existing bookmarks.

Root cause: In OOXML, w:id is a shared ID space across bookmarks, tracked changes, comments, and move ranges. The SKILL.md examples used hardcoded low IDs (1, 2, 3) which commonly collide with existing bookmark IDs, causing Word to reject the file as corrupt.

Changes

1. skills/docx/SKILL.md

  • Replace all hardcoded low w:id values in tracked change examples with high-offset IDs (100, 101, etc.)
  • Add CRITICAL guidance: scan for max existing w:id across ALL element types before assigning new IDs
  • Add CRITICAL nesting-level warning: w:ins/w:del must be at w:p level, never inside w:r or w:t

2. skills/docx/scripts/office/validators/base.py

  • Add 12 tracked change element types to UNIQUE_ID_REQUIREMENTS: ins, del, rPrChange, pPrChange, sectPrChange, tblPrChange, tcPrChange, trPrChange, cellIns, cellDel, cellMerge, numberingChange
  • Add SHARED_ID_SPACE_ELEMENTS set defining elements that share the w:id space
  • Modify validate_unique_ids() to check cross-type ID uniqueness for shared-space elements (e.g., detect bookmarkStart id="1" conflicting with ins id="1")

Closes #489

Test plan

  • base.py compiles without syntax errors
  • All tracked change examples in SKILL.md use non-colliding high-offset IDs
  • Validator now detects cross-type ID duplicates (bookmark vs tracked change)
  • Non-shared-space elements (sp, pic, etc.) still validate independently as before

🤖 Generated with Claude Code

Two changes to fix document corruption when adding tracked changes:

1. SKILL.md: Replace hardcoded low IDs (1, 2) in tracked change examples
   with high-offset IDs (100, 101) and add explicit guidance to scan for
   max existing w:id before assigning new ones. Add nesting-level warning.

2. base.py validator: Add tracked change elements (ins, del, rPrChange,
   pPrChange, etc.) to UNIQUE_ID_REQUIREMENTS and introduce a shared ID
   space check so cross-type duplicates (e.g. bookmarkStart id="1" vs
   ins id="1") are detected. Previously only same-type duplicates were
   caught.

Closes anthropics#489
@Lubrsy706
Lubrsy706 force-pushed the fix/docx-tracked-change-id-collision branch from 0f5749e to a4fd9cf Compare March 6, 2026 15:22
@Lubrsy706

Copy link
Copy Markdown
Author

Friendly ping: this PR has been ready for review since March 6, 2026. If there is anything blocking it or if you would prefer changes before merge, let me know and I can update it. Would appreciate a review when someone has time.

@Lubrsy706

Copy link
Copy Markdown
Author

Following up on this PR. The fix should be ready for review; please let me know if you'd like any updates or additional test coverage from my side.

@Lubrsy706

Copy link
Copy Markdown
Author

Following up on this PR. The fix should be ready for review from my side; if you would prefer any updates or additional coverage before merge, let me know and I can handle it. Would appreciate review when someone has time.

@Lubrsy706

Copy link
Copy Markdown
Author

Following up on this PR. The docx fix should be ready for review from my side; if you would like any adjustments or extra coverage around tracked changes and bookmark interactions, I can update it. Would appreciate a review when someone has time.

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.

DOCX skill: tracked change w:id values collide with existing bookmark IDs, corrupting files

1 participant