File size: 12,620 Bytes
5840d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
717452f
5840d20
c113bf8
5840d20
717452f
df571be
 
717452f
df571be
717452f
5840d20
 
 
 
 
e8cc26c
 
 
5840d20
 
 
 
 
 
15028da
 
 
 
5840d20
 
 
 
717452f
 
 
 
 
 
5840d20
 
 
717452f
 
 
 
 
 
 
 
 
 
 
5840d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cb1336
 
 
 
 
 
 
 
 
 
 
 
 
c113bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5840d20
 
 
 
 
 
 
 
 
 
 
a0e59e4
 
 
 
 
 
 
 
 
 
 
 
5840d20
 
 
 
df571be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5840d20
 
 
 
 
 
 
 
 
 
 
c64a0cb
 
 
 
 
 
 
 
 
 
 
 
 
5840d20
 
 
 
 
 
 
 
 
 
 
a0e59e4
 
 
 
 
5840d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Data shapes handed between pipeline stages β€” the parsing/extraction seam.

Extraction consumes `ParsedDocument`, never a file path and never the parser's
API. That is what keeps the parser swappable: a Tesseract or Azure Document
Intelligence path emits the same artifact, and the extraction half never learns
which one ran.

Design note β€” these fields were derived from real MinerU output, not designed on
paper. Verified against actual `content_list.json` output and cross-checked
against MinerU's source for both the `pipeline` and `vlm` backends.

Two things worth knowing before reviewing:

1. `bbox` and `text_level` are emitted by **both** backends β€” verified in
   MinerU's source, where `pipeline` and `vlm` run identical logic:

       elif para_type == BlockType.TITLE:
           title_level = get_title_level(para_block)
           if title_level != 0:
               para_content['text_level'] = title_level

   So heading structure is read from the document when MinerU detects titles,
   and only derived from numbering patterns as a fallback.

2. Heading availability is **document-dependent, not backend-dependent**. A
   document with explicit numbered headings (e.g. the BUMA standard: `2.` /
   `2.1.` / `2.1.1.`) yields a clean hierarchy. A document of mid-chapter pages
   with no headings yields none β€” which is why `section_no`, `heading` and
   `heading_path` stay Optional rather than required.

⚠️ `Chunk.text` must stay VERBATIM from the source document. The extraction-side
guardrail locates LLM-quoted spans literally inside this text; if the text is
ever cleaned up (lines rejoined, whitespace normalised), the lookup fails and
the field is silently set to null instead of raising. The failure looks like a
bad LLM, but the cause would be here.
"""

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field, computed_field

ChunkKind = Literal["text", "table", "chart", "equation", "image"]

# 0.3.3 β€” adds the computed 1-based `page_no` (S6b). Derived from `page_idx`.
# 0.3.2 β€” adds the optional `source_title`.
# 0.3.1 β€” adds the optional `normaliser_version`.
# All patch bumps: purely additive, so every 0.3.0 consumer reads them unchanged
# and the new fields are null on anything written earlier.
SCHEMA_VERSION = "0.3.3"


class Chunk(BaseModel):
    """One unit of a parsed document, ready for the extraction stage."""

    # Provenance key. Format is part of the interface: `f"{doc_id}::{ordinal:04d}"`,
    # separator `::`, agreed 2026-08-24. Every candidate entry points back here, so
    # the two halves must mint and read the same shape or provenance breaks silently.
    chunk_id: str
    doc_id: str
    kind: ChunkKind

    # Content. VERBATIM β€” never reflowed or normalised. See note above.
    #
    # Does NOT include the section's heading line; that lives in `heading`.
    # Consumers that need both compose them β€” the extraction half already builds
    # its span-check haystack as `heading + "\n" + text` and treats a heading
    # that names the term as a mention at offset 0.
    text: str

    # Location in the source document.
    #
    # Named `page_idx` deliberately: these are 0-BASED, exactly as the parser
    # reports them, with no conversion β€” so the artifact always matches the raw
    # parser output kept alongside it. The 1-based number a human reads is
    # `page_no` below: a review queue is about to see these, and an off-by-one
    # there is invisible until an expert opens the wrong page, so the conversion
    # is done once, here, rather than left to each consumer (S6b, 2026-09-02).
    page_idx: int
    page_idxs: list[int] = Field(default_factory=list)

    @computed_field  # type: ignore[prop-decorator]
    @property
    def page_no(self) -> int:
        """1-based page number, for humans and the review UI.

        Derived from `page_idx`, never stored, so it cannot drift from it β€” and
        neither normaliser has to remember to set it. `page_idx` stays 0-based
        and raw; this is the single number the reviewer sees.
        """
        return self.page_idx + 1

    # Structural context. All Optional β€” many documents carry no headings.
    section_no: str | None = None  # e.g. "2.1.3", when the document is numbered

    # This chunk's own section title, VERBATIM as the document writes it β€”
    # including wording a reader may be tempted to "fix". The BUMA standard says
    # "Physical of Availability (PA)", not "Physical Availability"; that exact
    # string must reach the extraction model, or it gets silently normalised and
    # the discrepancy never reaches the expert.
    heading: str | None = None

    # Running headers of every page this chunk spans, in page order. A list
    # rather than a single value: a chunk crossing a chapter boundary would
    # otherwise silently keep only the first page's chapter.
    chapters: list[str] = Field(default_factory=list)

    # Breadcrumb of enclosing headings, outermost first, built from MinerU's
    # `text_level` hierarchy. Example from the BUMA standard:
    #   ["2. PENJELASAN PARAMETER", "2.1. Production Parameter", "2.1.1. Production"]
    heading_path: list[str] = Field(default_factory=list)

    # Flags for cheap filtering downstream
    has_formula: bool = False
    is_tabular: bool = False

    # Trace back to the source: item indices in MinerU's content_list.json
    source_items: list[int] = Field(default_factory=list)

    # Position on the page of the first source item, as MinerU reports it.
    # Carried through for a curation UI that highlights where on the page a
    # definition came from. Nothing in the pipeline reasons about it β€” ordering
    # uses item sequence, never coordinates.
    bbox: list[int] | None = None

    # Non-text attachments (formula images, table/chart crops)
    images: list[str] = Field(default_factory=list)

    # Source markup, kept verbatim beside the rendered prose in `text`.
    #
    # `text` carries a readable rendering because the term filter is an NER
    # model reading prose: MinerU writes formulas character-spaced
    # ("P u r c h a s i n g ~ c o s t s") and tables as HTML, and neither
    # produces a single mention. Measured on the same document and gold set,
    # only the parse differing: raw markup in `text` scored recall 0.7561 against
    # 0.8537 for plain text; rendering it back recovered 0.8293.
    #
    # The markup is not discarded β€” the formula branch needs exactly this form.
    latex: list[str] = Field(default_factory=list)
    table_html: str | None = None

    # ⚠️ MODEL-WRITTEN, NOT FROM THE DOCUMENT. MinerU's `vlm` backend (and
    # `hybrid` at effort=high) describes images and charts; that description
    # arrives in the item's `content` field.
    #
    # It is kept OUT of `text` deliberately. `text` is the haystack the
    # extraction guardrail searches to prove a quote is verbatim; putting
    # generated prose there would let a hallucinated sentence pass the very
    # check that exists to catch hallucination. A caption printed in the
    # document is verbatim and belongs in `text`; a description invented about
    # the picture does not.
    #
    # Any consumer using this must treat it as a model claim about the image,
    # never as evidence β€” it cannot be span-checked against anything.
    generated_description: str | None = None


class ParsedDocument(BaseModel):
    """The artifact itself β€” one parsed document, self-describing.

    The chunk list alone is not enough to hand across the seam: an artifact that
    travels to the extraction half must carry its own provenance. Without
    `parser_name` / `parser_version` / `parser_backend`, a MinerU upgrade and a
    prompt change are indistinguishable when extraction results shift.

    `content_hash` is the hash of the SOURCE FILE, so re-parsing the same
    document is detectable and a changed document forces a new artifact version.

    **Metadata is declared before `chunks`, and that ordering is deliberate.**
    Pydantic serialises in declaration order, so it decides what a person sees on
    opening the file. With `chunks` first, checking which parser or which
    normaliser produced an artifact means scrolling past every chunk to the end β€”
    thousands of lines in a real document. That is not hypothetical fussiness:
    verifying `parser_config` and `normaliser_version` is now a routine step when
    an artifact is handed between the two halves, and burying them is how a stale
    artifact went unnoticed for a day.

    JSON objects are unordered by specification and every consumer reads by key,
    so this changes nothing for code β€” only for the person reading it.
    """

    doc_id: str

    # The document's name as a HUMAN reads it. Today this is the file stem and
    # therefore equal to `doc_id` β€” the point is that it is a separate field, so
    # the two can diverge without anything having to change here.
    #
    # `doc_id` is on its way to becoming an identifier rather than a label (see
    # the parsing README): every `term_id` / `formula_id` / `rule_id` downstream
    # is a digest of it, so it must stop depending on what someone typed into a
    # Save dialog. Once it does, `Provenance` β€” which is what a reviewing expert
    # actually reads β€” would otherwise show them a hash instead of the standard's
    # name. This field is what travels alongside it.
    #
    # Added ahead of that switch because it is needed whichever identifier wins,
    # and adding it now costs nothing and breaks nothing.
    source_title: str | None = None

    # Identity of the source
    source_path: str
    content_hash: str                    # sha256 of the source file
    n_pages: int

    # Which parser produced this, and how
    parser_name: str = "mineru"
    parser_version: str | None = None    # e.g. "3.4.4"
    parser_backend: str | None = None    # "pipeline" | "vlm" | "hybrid", as MinerU recorded it
    parser_config: str | None = None     # fingerprint of the settings that affect output

    # Fingerprint of the code that turned MinerU's output into these chunks β€”
    # `normalize.py`, `render.py`, `checks.py`, `contracts.py`. OPTIONAL and
    # purely informational: nothing downstream has to read it, and an artifact
    # written before this field existed simply carries null.
    #
    # It closes a real gap. `parser_config` fingerprints MinerU's settings, and
    # `schema_version` versions the artifact's SHAPE β€” neither moves when a
    # normalisation bug is fixed, because the shape is unchanged. So a stale
    # artifact and a correct one declared identical metadata, and the only
    # observable difference was the character count. That cost a day on
    # 2026-08-26 (see `normalize.normaliser_version`).
    normaliser_version: str | None = None

    # Version of THIS ARTIFACT for this document β€” bumped when the document is
    # re-parsed (new source content, new parser version, or changed settings).
    # Distinct from `schema_version`, which versions the contract itself.
    version: int = 1

    schema_version: str = SCHEMA_VERSION
    created_at: str | None = None

    # Where the untouched MinerU output for this document lives
    raw_output_dir: str | None = None

    # LAST on purpose β€” see the class docstring. Everything above answers "what
    # is this file?", and a reader should not have to scroll past the payload to
    # find out.
    chunks: list[Chunk]


# --- Seam for the downstream stages (declared, not yet used) ---
# Written here so the handoff shape is visible from the start. The extraction
# half owns the final form of these two β€” see `KNOWLEDGE_PIPELINE_TODO.md` Β§5.


class Mention(BaseModel):
    """One occurrence of a term inside a chunk."""

    term: str
    chunk_id: str
    page_idx: int


class TermRecord(BaseModel):
    """Extracted knowledge for one term (one cluster of mentions)."""

    term: str
    full_name: str | None = None

    # What the document literally says, before any normalisation β€” e.g.
    # "Physical of Availability (PA)" where `full_name` may read "Physical
    # Availability". Span-checked against `Chunk.text` like every other field,
    # so the discrepancy surfaces to the expert instead of being quietly fixed.
    source_wording: str | None = None

    definition: str | None = None
    formula_latex: str | None = None
    subdomain_tags: list[str] = Field(default_factory=list)
    mention_count: int = 0
    provenance: dict = Field(default_factory=dict)
    extraction_status: str = "ok"