fix(server): large DOCX and XLSX attachments keep their text up to the node limit #46592 (GitHub #46591)
main fromrefs/pull/46591/head·opened View on GitHubProblem#
Fixes #46590. When one XML part of an attachment passes 200,000 elements, xml() throws Truncation and the whole part is discarded. A 10,000-row × 10-column sheet (290 KB), or a Word document of a few thousand real paragraphs, reaches the agent as only [Extracted text truncated at document extraction limits.] with none of its text. The other limits (output budget, row cap, column cap) keep the text before the cut.
Change#
In apps/server/src/provider/documentTextWorker.ts:
- Text parts are now read with
readText(): the DOCX body, ODFcontent.xml, XLSX worksheets and shared strings, PPTX slides and EPUB chapters. When one of them reaches the node limit, parsing stops there and the elements still open are closed with their normal close handling, so hidden-text, merge and AlternateContent rules still apply. - The paragraph, table row or shared string that the cut landed in (
p,h,si,row,tr,table-row) is dropped rather than closed. It is incomplete, and a later child can still hide it (an ODFtext:hidden-paragraphfield, for example). A partial row would also read as a complete row with fewer columns. - The text then ends right after that part with the usual notice (
endIfCut). A cut slide, sheet or chapter is the last thing before the notice, the same as with the row limit. - Parts that decide structure or visibility are unchanged: styles, relationships, workbook,
container.xml, footnotes, and the ODFstyles.xmlreads. A partial style table could let text hidden by a style defined after the cut leak through.
Scope#
Bug fix: text is lost below the limits the worker advertises. One problem, server only. No contract or client changes.
Verification#
- I found the bug by generating synthetic files (script in #46590) and running
extractDocumentTexton them. - I extended the existing bounded-archive table in
userInputAttachments.test.ts:- The DOCX node-limit row now cuts inside the second row of a table and expects
Kept, then the notice, withPartialandLostabsent. - Two new XLSX rows cut inside a worksheet row and inside a shared string, with a later sheet present. They expect
Kept, then the notice, withPartial,LostandAfterabsent. - These rows fail on
mainfor the right reason:Keptis missing because the whole part was dropped. They pass with the fix.
- The DOCX node-limit row now cuts inside the second row of a table and expects
vp run check:changedpasses: format, lint, knip, typecheck, and the three affected test files (74 tests).- Runtime transcript with the worker's 192 MB heap cap (
NODE_OPTIONS=--max-old-space-size=192), files from the #46590 script:
large.xlsx (289,280 bytes)
main: "## Sheet: Data\n\n[Extracted text truncated at document extraction limits.]" 271 ms, rss 209 MiB
fixed: "## Sheet: Data\n0\t1\t2…95220\t…\t95229\n\n[Extracted text truncated …]" (560,385 chars) 1030 ms, rss 250 MiB
large.docx (92,882 bytes)
main: "\n\n[Extracted text truncated at document extraction limits.]" 384 ms, rss 225 MiB
fixed: "Paragraph 0 starts here, …" (1,000,059 chars, ends at the output budget) 635 ms, rss 245 MiB
- Peak RSS rises only by the cost of producing the text. The tree is still capped at 200,000 nodes, and the rest of a cut part is no longer parsed at all. A 31 MB worksheet now gives 1,000,058 chars at about 303 MiB RSS, against 272 MiB for the notice alone.
- A separate review pass built adversarial fixtures and compared them with
main. They cover a hidden style defined after the limit, an ODF hidden-paragraph field after the cut, a truncated EPUBcontainer.xml, oversized ODFstyles.xmland footnotes, and cuts insidemc:Choice, an ODS row, an ODP slide and a hidden XHTMLdiv. Each gives the same output asmain, or the kept text followed by the notice, with no hidden text. - Not covered: content held inside a single table row (a whole document laid out in one
tbl > tr > tc) still gives only the notice, as onmain. I did not test in a running desktop or web client; this is a server-only change with no UI surface.
Written by Claude Opus 5.5 in Claude Code (running in T3 Code), with Claude Fable 5.2 subagents for the investigation and code review.
🤖 Generated with Claude Code
- 1
- 2
Pushed657f207
juliusmarminge added labelstakeover:needs-julius
juliusmarminge removed labelstakeover:in-progress
- @@ -318,3 +320,4 @@318320319321 type WordStyleRunHidden = (id: string | undefined, ancestors: readonly XmlNode[]) => boolean;320322323+const PARTIAL_DROPPED = new Set(["p", "h", "si", "row", "tr", "table-row"]);
Drop an incomplete XHTML heading at the node cap. I ran the production extractor on two EPUB fixtures with the same complete heading and paragraph, then a large block cut off at the 200,000-node limit. When the cut landed in a
p, the output retained the complete blocks and skipped the unfinished paragraph. With anh2in the same spot, it emitted## Unfinished section startbefore the truncation notice, even though the rest of the heading had never been read. The untruncatedh2control emitted the full heading.paragraphs()treatsh1–h6as heading blocks, but the cutoff only dropspandhamong the text blocks. A local trial addingh1–h6to that cutoff check passed all three EPUB cases and the existing bounded office/EPUB archive test.Thanks, confirmed.
paragraphs()readsh1–h6as text blocks, but the cut only droppedpandh, so a cut XHTML heading was printed with only its start. f703571565 addsh1–h6toPARTIAL_DROPPED. It also adds an EPUB row to the bounded-archive table for each ofpandh2: a complete paragraph, then a block cut at the node limit, then a second chapter. Theh2case failed before the change (Partialleaked) and both pass now.vp run check:changedpasses.Retested current head 8a3d98e7 on Windows with Node 24.11.0 using the same three synthetic EPUB cases: incomplete paragraph at the node cap, incomplete h2 at the cap, and a complete-h2 control. The current head drops both incomplete blocks while preserving completed text and the truncation notice. The complete-heading control retains its heading and later chapter.
The original 657f2079 reproduces the unfinished-heading leak; f7035715 and current 8a3d98e7 pass the same assertions. This verifies the original reported path against the updated head. The check exercised the extracted production worker and input code; I did not rerun the complete attachment suite.
- 3
Sign in to join this conversation.
STiFLeR7(GitHub profile, opens in a new tab) reviewed
I reviewed the truncation path on Windows at
657f207938. The stock attachment suite passed 68 of 70 tests. The two RTF code-page cases failed with the same output on the base. I found one inconsistency in which partial EPUB blocks are kept.