Skip to content

fix(detect): keep tables in place and read all text in docx sidecars - #3833

Closed
L4XB wants to merge 1 commit into
Graphify-Labs:v8from
L4XB:fix/docx-markdown-document-order
Closed

L4XB wants to merge 1 commit into
Graphify-Labs:v8from
L4XB:fix/docx-markdown-document-order

Conversation

@L4XB

@L4XB L4XB commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

graphify.detect.docx_to_markdown writes the markdown sidecar that a .docx file is extracted from. It had three problems:

  • Tables moved to the end. It wrote every paragraph first and then appended all tables, so a table lost the heading it belongs to and ended up after the last section.
  • Missing text. Paragraph.text reads only the runs directly under a paragraph. So tracked insertions (w:ins), content controls (w:sdt, which is how forms and templates hold their values), simple fields, smart tags and text boxes were dropped.
  • Broken table rows. A cell's text went into its row as is: a line break in a cell split the markdown row, and a | in a cell added a column.

One document, before and after:

before                          after
# Pricing                       # Pricing
# Notes                         | Plan | Price |
- First item                    | --- | --- |
The fee is  euros.              | Basic \| Pro | 10 EUR per month |
Client:                         # Notes
Host                            - First item
| Plan | Price |                The fee is twelve euros.
| --- | --- |                   Client: Acme
| Basic | Pro | 10 EUR          Host
per month |                     Callout

The document has the table under # Pricing, a tracked change (ten deleted, twelve inserted), a content control holding Acme, and a text box Callout.

The change:

  • docx_to_markdown walks doc.element.body in document order. A table is written where it stands, in the same header / separator / rows form as before, and python-docx's row.cells still keeps merged cells aligned to the grid.
  • _docx_paragraph_text reads the text of every run in the paragraph. It skips runs under a tracked deletion, moved-away text, a ruby guide, a text box, or the mc:Fallback copy Word writes of each text box. A text box's paragraphs follow the paragraph that anchors it, once.
  • Content controls and custom XML are looked through for paragraphs, tables and rows.
  • _docx_cell_markdown puts a cell on one line (line breaks and paragraphs become spaces), flattens a nested table into the cell once, and escapes | as \|.

Headings, list items, plain and empty paragraphs are written exactly as before.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

How was this tested?

tests/test_docx_to_markdown.py is new, with 5 tests on real .docx files built with python-docx:

  • headings, a list item, plain, empty and tab-stop paragraphs keep their markdown exactly;
  • a table stays under its heading;
  • a cell with a pipe, a line break and a second paragraph stays on its row;
  • a nested table is flattened into its cell once;
  • a tracked insertion, inline and block content controls and a text box are read, while the deleted text and the text box's fallback copy are not.

4 of the 5 fail on v8; the one that passes is the plain-paragraph test, which pins that the output did not change there. Each of 9 mutants of the new code is caught by a failing test: tables dropped, pipes not escaped, cell whitespace kept, deletions read, text box runs read inline, fallback copy read, text boxes not emitted, content controls skipped, nested table dropped.

tests/ (full suite, Python 3.12)                5761 passed, 148 skipped, 3 failed
  the same 3 fail without this change (as in #3831): test_collect_files_skips_hidden (my checkout
  sits under a hidden directory), test_built_wheel_ships_the_full_skill_payload (no 'build'
  module installed) and test_hyperedge_convex_hull_js_is_geometrically_sound
tests/test_docx_to_markdown.py + test_office_limits.py + test_detect.py   282 passed
ruff 0.15.14 check --config pyproject.toml                                All checks passed!
python -m tools.skillgen --check                                          check OK: 134 artifact(s) match

Graphify-specific checklist

  • I added or updated tests for behavior changes.
  • I updated documentation or confirmed that no documentation is needed.
  • I updated generated skill artifacts when changing their source fragments. (No fragments changed; the skillgen check passes.)
  • I considered compatibility across supported Python versions.
  • I confirmed that no API keys, generated graph data, or local-only files are included.

docx_to_markdown wrote every paragraph first and appended all tables at the
end, so a table lost the heading it belongs to. Paragraph.text also drops
tracked insertions, content controls, fields and text boxes. And a cell's
text went into its markdown row as is, so a line break split the row and a
pipe added a column.

Walk the body in document order, read each paragraph's runs inside those
containers (skipping deleted text and the fallback copy of a text box), and
put a cell on one line with its pipes escaped.

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).


Graphify review — findings

Rewrites docx_to_markdown to walk the document body in order via _docx_blocks so tables stay under the headings they follow, instead of appending all tables after all paragraphs. Reads paragraph text through _docx_paragraph_text, which pulls run-level text (t, tab, br, etc.) while skipping tracked deletions, moved-from text, ruby guides, and text-box fallback copies, and looks through content controls/custom XML wrappers; text boxes render as their own paragraphs after the paragraph that hosts them. Renders table cells with _docx_cell_markdown, collapsing each cell to a single pipe-escaped line and flattening nested tables into their cell, and adds tests/test_docx_to_markdown.py covering headings, ordering, cell escaping, nesting, tracked changes, and text boxes.

No blocking issues surfaced. 4 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2464 functions depend on the 139 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract() — 703 callers, 47 callees
  • new: _rebuild_code() — 144 callers, 55 callees
  • new: detect() — 112 callers, 15 callees
  • new: _extract_generic() — 18 callers, 29 callees
  • new: save_manifest() — 40 callers, 11 callees
  • new: extract_js() — 87 callers, 4 callees
  • new: extract_files_direct() — 17 callers, 20 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • …and 50 more — each is listed as a finding

Verification — 2464 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 823 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

57 of 301 test file(s) selected (19%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_build.py — impact
  • tests/test_cargo_introspect.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_detect.py — impact
  • tests/test_docx_to_markdown.py — impact, changed-test
  • tests/test_dotnet.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_file_slice.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_image_vision.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_incremental_mtime_collision.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_languages.py — impact
  • tests/test_llm_backends.py — impact
  • tests/test_long_path_hashing.py — impact
  • tests/test_manifest_ingest.py — impact
  • tests/test_mcp_ingest.py — impact
  • tests/test_multilang.py — impact
  • tests/test_non_regular_files.py — impact
  • tests/test_office_incremental.py — impact
  • tests/test_office_limits.py — impact
  • tests/test_ollama.py — impact
  • tests/test_out_dir_evidence.py — impact
  • tests/test_oversized_document_slicing.py — impact
  • tests/test_package_json_subpath_imports.py — impact
  • tests/test_pdf_slicing.py — impact
  • tests/test_pdf_token_estimate.py — impact
  • tests/test_phantom_external_import.py — impact
  • tests/test_pipeline.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_stale_prune.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • … and 7 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

· 1 grounded finding(s) anchored inline below; 57 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/detect.py
return " ".join(" ".join(part.split()) for part in parts if part.strip()).replace("|", "\\|")


def docx_to_markdown(path: Path) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Health regression — docx_to_markdown()

high coupling complexity (Ca·Ce = 20).

Grounded coupling-delta finding (deterministic), not an LLM guess.

safishamsi added a commit that referenced this pull request Sep 29, 2026
Atomic label sidecars (#3853), docx table/all-text extraction (#3833),
wiki index-collision (#3818) and escaped wikilink aliases (#3772),
community-listing real-node filter (#3836), warn-once on missing pypdf
extra (#3710), plus the --help logo banner + app.graphify.com line.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.72 (now on PyPI) via an authorship-preserving cherry-pick, so your commit keeps contributor-graph credit. Thanks @L4XB! docx sidecar conversion keeps tables in place and reads all text.

Release: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.72

@safishamsi safishamsi closed this Sep 29, 2026
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