Why DocumentIR, Issue IR, and CodeIR Have to Be Split
Documents, tickets, and code have different structures; only with a typed IR in place first can you chunk correctly, build the graph, and keep traceable locations.
On this page · 11 sections
Converting HTML, Jira JSON, PDF, and TypeScript all into one stretch of Markdown does get you hooked up to embedding quickly. But that unification happens only at the string level: a table's rows and columns, a ticket's status changes, code symbols, call edges, and commits are all flattened. A downstream model can find similar words, but it cannot reliably answer "which line calls what?" or "did this sentence come from a comment or from the requirement body?"
The choice in mt_rag is to unify the provenance contract while keeping a dedicated typed IR (intermediate representation, that is, a parsed form that keeps each source's own structure) for each kind of source.
The Short Version
- Converting every source into one stretch of Markdown unifies only at the string level: table rows and columns, ticket status changes, code symbols, and call edges are all flattened.
- What
mt_ragunifies is the provenance contract and the block contract; parsers and IR are split by source into DocumentIR, Issue IR, and Code CPG. - Parse failures must be observable: a warning, an error block, an audit event, or
unresolved_call, never an empty result that looks like a success. - Chunking uses the block and the symbol as boundaries first; only a long block over the limit is split internally as a fallback.
Knowledge Map for This Article
The topics can be summarized as document structure, source-specific semantics, code analysis, structure-aware chunking, and parser failure lineage.
What Is Unified Is the Block Contract, Not the Parser
The basic unit of DocumentIRV1 is the ordered DocumentBlockV1:
block_id / kind / ordinal / text
locator / section_path / links / asset_ref / metadata
kind can be Heading, Paragraph, List Item, Table, Code, Image, Page, Sheet, Slide, Note, or Error. Block ordinals must increase contiguously from 0, and the locator cannot be empty. That way every downstream chunk can state which page, character range, table, comment, or code line it came from, rather than merely citing a piece of text that cannot be located.
HTML: Strip the Page Chrome, Keep the Semantic Boundaries
The current HTML parser discards non-body content such as script, style, nav, header, footer, aside, and hidden regions; it keeps the H1–H6 hierarchy, paragraphs, lists, code, tables, links, and image references.
Each block records char_start, char_end, and the original HTML tag. In addition:
- A heading updates
section_path; - A table is organized by row/cell rather than joining all cells with no boundaries;
- An image stores its asset ref and alt text.
When a page plainly has content but no semantic block can be parsed from it, the IR writes a no_semantic_blocks warning instead of returning an empty document that looks like a success.
This is still not browser-grade DOM/layout reconstruction. Complex macros, dynamic content, and Confluence-specific components may need dedicated adapters, but the current contract can already distinguish "could not parse" from "the page is empty."
Jira: Body, Comments, and Status Changes Cannot Be Mixed into One Paragraph
The Jira parser reads Atlassian Document Format and puts the summary, metadata, description, comments, changelog, worklogs, and remote links in separate sections. Each kind of block carries different information:
- A comment block carries the comment ID, author, and created/updated timestamps;
- A changelog entry carries the field, from/to, and changed-at;
- A worklog keeps the author and started time.
So when retrieval surfaces "a field changed from A to B," the system knows it came from the changelog, not from the requirement body. The answer can say "the ticket status changed at one point" without miswriting it as a business rule of the system.
Multimodal: Native, OCR, and Unavailable Are All Observable States
Attachments are routed by media type:
- A PDF tries native text/layout first; a page with no text can be OCRed individually; when both apply, it is marked
pdf-mixed; - Tables and images keep their page, bbox, and table/image ordinal;
- An image can run OCR and captioning separately and keep each observation;
- XLSX, DOCX, and PPTX use structured blocks and keep locators such as sheet, table, slide, and note;
- When the local parser's result is insufficient, it can go through the Docling shadow/fallback; a failure writes an audit event or an error block.
Fail-soft does not mean swallowing errors. It lets one attachment fail without losing the whole source run, but the stage, status, and truncated error must be recorded; pdf-unavailable cannot masquerade as successfully extracted empty text.
Code: Establish Verifiable Structure Before Text Similarity
The current production CPG (Code Property Graph) path uses the standard-library AST for Python; TypeScript, TSX, JavaScript, Java, Swift, and Kotlin use the Tree-sitter named AST. The production batch requires Tree-sitter to be available; a missing language pack blocks that parsing path rather than silently falling back to regex and claiming equal trustworthiness.
The full CPG includes the layers below; look at what evidence each layer can provide:
| Layer | Purpose | Example evidence |
|---|---|---|
| AST | Syntactic parent-child structure | Node type, line, end line, column |
| Symbol/Type | Class, interface, function, method | Qualified name, signature, base type |
| Call Graph | Call relationships | Raw expression, resolver rule, commit |
| CFG | Sequence, branches, and joins | Branch kind, source line |
| DFG | Reads, writes, and value propagation | Assignment, name flow |
| Semantic CPG | The query-facing subgraph | CALLS, IMPORTS, READS, WRITES, ROUTES_TO |
"Full CPG" here is relative to the semantic projection that enters Neo4j: it retains the complete AST/CFG/DFG/call artifact that this extractor can produce. The goal is retrieval and evidence, not compiler-grade, language-complete program analysis.
The current CFG mostly covers sequence, branches, and joins, and the DFG focuses on assignment/name flow; dynamic dispatch, reflection, and cross-language runtime calls may still be unresolvable.
Edges that can be resolved precisely are marked EXTRACTED; a call that has only a name and whose target cannot be resolved falls to unresolved_call, with confidence AMBIGUOUS.
When the parser hits a syntax, encoding, or file-size problem, it generates a code error graph that preserves at least Repository → File, the commit, and the parse error, rather than letting the file disappear silently from the corpus.
Chunking: Respect Structure First, Then Consider Length
The document chunker uses the block as its boundary; the code chunker uses the symbol and line range. Only when a long block exceeds the limit does it fall back to splitting inside the block, and it records the boundary_reason.
The parent/child structure serves two opposing needs: a child is small enough to hit one method, table row, or subsection precisely; a parent keeps the complete section so the answer can read the context. Each chunk continues to carry the original CAS URI, content hash, source locator, entity, and evidence ref.
This is more complex than a fixed window of a few hundred characters, but a fixed window randomly cuts apart a function signature from its body, a heading from its paragraph, and a table header from its rows. Embedding can recall half a sentence; it cannot recover the structural semantics that were lost.
Failure Modes and Trade-offs
Each row below is one failure mode: the middle column is how the system currently represents it, and the right column is the cost.
| Failure mode | Current representation | Trade-off |
|---|---|---|
| HTML contains only navigation/scripts | A warning or empty blocks; the chrome is not indexed | A source-specific macro adapter may be needed |
| A PDF page has no text | Per-page OCR; a failure writes an audit event | OCR is slow and has recognition errors, and cannot overwrite the original image |
| Complex Office layout | Structured blocks and locators are kept | Pixel-level layout reconstruction is not guaranteed |
| Tree-sitter is unavailable | The production parser fails closed | Deployment dependencies are heavier, but regex results are not passed off as an AST |
| Dynamic dispatch cannot be resolved statically | unresolved_call + AMBIGUOUS |
Graph recall drops, in exchange for not fabricating call edges |
| A block is too long | Fallback split only inside the block | Some semantic units are still cut apart, but the extent is auditable |
Verifiable Project Evidence
src/mt_rag/documents/contracts.py: block kinds, locators, and the contiguous ordinal contract;src/mt_rag/documents/html.py: DOM-aware drop tags, heading/table/image handling, and source offsets;src/mt_rag/documents/jira.py: ADF, comments, changelog, worklogs, and remote links;src/mt_rag/multimodal/parser.pyandmultimodal/docling.py: PDF native/OCR, Office, images, fallback, and audit;src/mt_rag/code/cpg.py: the Python AST, Tree-sitter CPG, CFG/DFG, unresolved calls, and the semantic projection;src/mt_rag/code/batch.py: productionrequire_tree_sitter=true, the code error graph, and CAS reuse;src/mt_rag/chunking/semantic.py: structure-aware chunking for documents and code.
References
- Tree-sitter documentation;
- Python
astdocumentation; - Yamaguchi et al., Modeling and Discovering Vulnerabilities with Code Property Graphs;
- Joern Code Property Graph documentation;
- The Docling project;
- Azure AI Search guide to chunking documents.
A typed IR adds parser and schema cost, but it makes structure, location, and relationships verifiable, beyond "the text is related." A unified string is a convenient intermediate result; it cannot be the final representation of engineering evidence.
Related notes
Local RAG Design: Store the Evidence First, Then Retrieve
How mt_rag separates replayable evidence, rebuildable query views, verified graph paths, and bounded generation—and why current source still wins.
Why a Code Hit Is Not Yet an Answer
Engineering retrieval has to bind results to a repository, a commit, and a symbol; a hit that is missing, stale, or from the wrong repository is only a locating lead.
Working on something similar?
I'm always happy to compare notes on distributed systems, delivery, and applied AI.
[email protected]