Repository navigation
fix(html): stop key-value graph rendering at a to_child cycle - #923
Conversation
|
✅ DCO Check Passed Thanks @r0h1tb, all your commits are properly signed off. 🎉 |
Merge Protections🟢 All 2 merge protections satisfied — ready to merge. Show 2 satisfied protections🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 Require two reviewer for test updatesWhen test data is updated, we require two reviewers
|
AI triage (advisory)Checked commit Warning The AI analysis did not finish. Only the deterministic checks are shown. CI safety: not assessed. The |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the full diff at d90a2a2f81. It read the code and did not run it.
The fix stops the RecursionError for a to_child cycle, and the new test traces correctly against the code. The remaining risk is that the walk follows every simple path, not every cell once. A dense cyclic graph can still take exponential time or produce huge HTML.
A maintainer makes the merge decision. This review never approves the pull request.
| cell_text = f"<strong>{cell_text}</strong>: {', '.join(value_texts)}" | ||
|
|
||
| path = ancestors | {cell_id} | ||
| child_ids = [child_id for child_id in child_links.get(cell_id, []) if child_id not in path] |
There was a problem hiding this comment.
question: Path-only cycle check can still blow up on dense cyclic graphs, and the PR text says "rendered once"
The check skips only children that are on the current path. A cell that several paths reach is rendered once per path. In a graph with many cross-linked to_child edges, the number of simple paths grows exponentially. XBRL graphs with both presentation and calculation links are the case this PR targets. The PR checks only the small mlac document (0.04 s). It does not show that larger XBRL graphs finish. The PR text also says "each cell on a cycle is rendered once". That holds only for the small test graph.
Please show evidence on a larger or denser cyclic graph, for example a clique of 8 to 10 to_child cells under one root. If it blows up, use a global visited set. That changes the output for DAGs with shared children, which today render once per parent. If the team wants to keep that output, add a regression test that pins the diamond behavior. Otherwise, correct the PR text.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ceberam
left a comment
There was a problem hiding this comment.
Thanks @r0h1tb for the fix and for the clear description.
However, I think we should address the comment left by the AI bot. The automated review comment about this is legitimate, and I confirmed it with measurements. The new check only skips a child that is on the current root-to-cell path, so it prevents infinite recursion but not exponential output. A cell that several paths reach is rendered once per path, and each back edge adds more simple paths.
All timings are on the PR branch merged with current main. The first graph is one root linked to k cells that are all linked to each other (a clique of to_child links, which is the case the bot suggested):
| k (clique under one root) | <li> elements |
HTML size | time |
|---|---|---|---|
| 8 | 69,281 | 1.6 MB | 0.08 s |
| 9 | 623,530 | 14.7 MB | 0.52 s |
| 10 | 6,235,301 | 147.7 MB | 5.3 s |
| 11 | 68,588,312 | 1.63 GB | 60.6 s |
On main the same graphs raise RecursionError, so the PR turns a crash into a run that grows about 10x per added cell. At k=11 the export is effectively a hang and needs 1.6 GB of memory for the string. A document with a handful of densely cross-linked concepts can therefore still make export_to_html() unusable, which is the scenario the PR sets out to fix.
The duplication is not only theoretical for the XBRL data cited in the description. In mlac-20251231.xml.json the key-value graph has 1,819 cells and 526 to_child links. 37 cells have more than one parent. The export currently produces 770 <li> elements, and a variant that renders each cell once produces 523, so about a third of the current output is repeated subtrees. The mlac document is small, but the PR only shows this one document (0.04 s), so it does not demonstrate that larger filings, which have more presentation and calculation links, finish. Please add evidence for a larger or denser case. The clique above is the quickest way to produce one.
The same growth already exists on main for acyclic graphs with shared children. A root with 24 cells linked in a forward-only order (no cycles) renders 8.4 million <li> elements in 4.6 s. Cycles are not the only trigger, but they make the pattern much more likely. This also means a global visited set changes the output of graphs that never had a cycle, so it needs an explicit decision.
I have the following suggestion: track the cells already rendered for the whole walk. Thread a rendered: set[int] through _render_cell_tree, add the cell on entry and skip children already in it, and skip a child inside the loop as well as in the list comprehension, because an earlier sibling's subtree can render a later sibling. I prototyped this on top of your branch. All the graphs in the table above render in under 0.03 s with one <li> per cell, the mlac document drops from 770 to 523 <li> elements and renders in 0.03 s, and tests/test_serialization.py still passes (249 passed, 1 xfailed), so no existing ground-truth file depends on the repeated subtrees. The side effect is that a cell with several parents is shown only under the first parent that reaches it, which is the output change the PR text would need to mention. Please add a regression test with a diamond (root -> A, root -> B, A -> C, B -> C) that pins which parent keeps the child, and keep the cycle test.
Please, update the PR description according to these changes.
Thanks again for the fix. The structure of the change is good, and I expect this to be a quick follow-up.
_render_cell_tree followed to_child links without tracking the path, so a cycle recursed until RecursionError and export_to_html() failed. The XBRL backend in docling produces such a cycle in tests/data/xbrl (mlac-20251231.xml): its presentation and calculation linkbases give GeneralAndAdministrativeExpense and OperatingIncomeLoss opposite parent-child links. A child already on the current path is now skipped. Signed-off-by: Rohit Behera <126186063+r0h1tb@users.noreply.github.com>
Skipping only children on the current path stopped the recursion, but a cell reachable through several paths was still rendered once per path, so a densely cross-linked graph grew about tenfold per added cell. Track the cells rendered for the whole graph and skip a child that was already rendered, also inside the loop, since an earlier sibling's subtree can render a later sibling. A cell with several parents is now shown only under the first parent that reaches it. A cell whose children were all rendered elsewhere keeps its own list item. Signed-off-by: Rohit Behera <126186063+r0h1tb@users.noreply.github.com>
d90a2a2 to
ba91028
Compare
|
Done, on top of current main. One difference from your prototype: a cell whose children were all rendered under an earlier parent falls into the leaf branch, which returns an empty string when the cell has no value, so B in the diamond would disappear. It now keeps its own The clique renders k+1 list items for k = 9, 11 and 50, in at most 0.03 s. The mlac document renders 503 list items, not your 523; I checked that each cell with a value or children appears exactly once. The full suite passes (1711 → 1714), and I updated the description. |
export_to_html()raisesRecursionErroron a document whose key-value graph has ato_childcycle, because_HTMLGraphDataSerializer._render_cell_treefollows child links without tracking what it has rendered.docling's own XBRL backend produces such a graph. Its ground truth
tests/data/xbrl/groundtruth/mlac-20251231.xml.jsonhasGeneralAndAdministrativeExpenseandOperatingIncomeLosseach linked as the other's child: the backend keeps one cell per concept and addsto_childlinks from both the presentation and the calculation linkbase. Markdown, text and DocTags export of that document work; HTML export fails._render_cell_treenow tracks the cells rendered for the whole graph and skips a child that was already rendered, so each cell is rendered once and a cycle ends.Output change: a cell with several parents is shown only under the first parent that reaches it. On main it is repeated under every parent, which also affects acyclic graphs with shared children. A cell whose children were all shown elsewhere keeps its own list item. On the mlac document the export goes from 770 list items (the earlier one-per-path version of this PR) to 503, one per cell that has a value or children.
Dense graphs, one root over k cells that all link to each other:
On main these raise
RecursionError. With one render per path, k=9 produced 623,530 list items (14.7 MB).Tests:
test_html_key_value_graph_with_cycle: a two-cell cycle renders each cell once. Fails on main withRecursionError.test_html_key_value_graph_shared_child_rendered_under_first_parent: a diamond (root -> A,root -> B,A -> C,B -> C) pins the full list, with C under A only and B kept. Fails on main.test_html_key_value_graph_dense_cycles_render_each_cell_once: an 8-cell clique renders 9 list items. Fails on main and on the one-per-path version.Full suite with
uv sync --all-extras --all-packages: 1711 → 1714 passed, no failures. ruff passes;tyreports the same diagnostics as main.