Raven-1/tests/test_knowledge_structure.py
Kevin Hu 82c76a82a9
feat(knowledge): read every format, show what the index holds, and let an agent search it (#489)
<img width="2318" height="1448" alt="image"
src="https://github.com/user-attachments/assets/bca71d69-4972-420c-b14e-c8cf74a17de9"
/>

## Summary

Everything a knowledge base could not read, and the pieces it cut them
into
saying what they are.

**Reading a PDF.** A PDF records glyphs at coordinates; whether they are
a
heading, a table cell or a running footer is inferred from how they
look. There
are now two readers. The geometric one infers structure from type size
and
position and needs nothing but the file. The other asks deepdoc's
models, ported
into `raven/knowledge/parser/deepdoc`: a layout model that says what
each region
is, a detector and recognizer that read a page carrying no text at all,
and a
table-structure model that rebuilds a grid from a picture of one. 
 Detecting boxes around text the file already spells
out exactly costs an inference pass per page to learn less, and
recognising it
costs accuracy.

The layout graph answers with a shape its own postprocessing
raises on, and its classes are DocLayout-YOLO's order rather than the
list
Both were established by running the model, and the label order is
pinned by a test.

**Tables go in as tables.** `<table>` is the only shape that keeps what
a grid
states -- which column a cell stood under, which cells are merged, which
row is
the header -- and it is the shape a model reads a table in, which is
what these
chunks are for. PDF, Word and PowerPoint all state their merges exactly,
and
they now survive as `colspan` and `rowspan`. The flat form this replaces
had to
repeat a header standing over two columns across both of them, because
there was
nowhere to record a span; it had no callers left afterwards and is gone.
Two
deviations from upstream, both about what the string is for: every
cell's text
is escaped, where upstream interpolates it raw and a cell holding a `<`
produces
markup that is no longer a table, and the span attributes are written
only where
there is a span, where upstream emits `<td  >` for every ordinary cell.

**A picture of where each chunk came from.** A chunk is a few hundred
words with
a page number on it, which is enough to cite and not enough to answer
whether
the parser read the page correctly. So every PDF chunk that knows where
it sits
carries one image: the regions it covers, cropped out of the rendered
pages and
stacked. here they are already structured in the parser's element spans,
so
the markers would only put text nobody wrote into the thing being
embedded.
The panel draws a chunk's tables as tables, parsed and
rebuilt as elements rather than injected: a person can rewrite any piece
from
that same panel, so what comes back from the store is user input however
it got
there.

**The page that reads them.** Upstream retired the knowledge page while
this
branch was out; it is rebuilt against the new architecture, and the half
that
never existed is the one a reader needs -- what a document actually
became once
it was cut and embedded. Documents sit in one-level folders and arrive
by four
routes: a file, a folder, a URL, a written note. Adding and indexing are
two
calls on the contract and nothing drains that queue on its own, so a
page that
stopped after the first left every upload saying Queued for good.

Opening a file takes the whole page: the document on the left, the
pieces the
index holds of it on the right. The left half is the file drawn as its
own
pages rather than framed by the browser's PDF viewer, which reads where
to open
once and answers to nothing afterwards -- and which nothing can be drawn
on.
Pages are rendered once and kept, and the region a piece was cut from is
washed
over the page it came from, placed in fractions of the page so the
picture's
own scale never enters the arithmetic. Clicking a piece takes the file
there;
double-clicking opens it for rewriting.

The pieces can be searched, filtered by state, turned on and off,
deleted,
rewritten and added to, one at a time or in a batch. Every one of those
was
already on the contract with no reader for it. A base's settings are
back and
fuller: how many pieces a search returns, how documents are cut, and how
much
prose comes with a table or a figure. The embedding model is a picker
while the
base holds no pieces and a stated fact once it does -- moving it then is
the
collection made again at the new width and every document re-cut, which
is not
a thing a panel does by being saved.

**An agent can read them.** The retrieval engine was there and nothing
could
reach it: no agent had a tool that opened a library. A reader now points
a
conversation at bases from the composer, the way they pin a working
directory,
and the selection rides in the same place -- the session's metadata,
bound
around the turn.

The tool takes a query and never a base. The scope is the reader's
choice, so a
model cannot reach material nobody offered it, and it is withheld where
nothing
was picked rather than offered and refused. A hit says which document
and page
it came from, because a hit a reader cannot place is a hit they cannot
check --
and the passages are pressable in the transcript, walking into the base,
the
document, the folder it is filed under and then the piece. A citation a
reader
cannot follow is a claim they have to take on trust, and the whole point
of
searching their own material is that they can go and look.

**The word segmenter and the resources.** `raven.core.Tokenizer` is
Infinity's
`rag_tokenizer`, ported: a forward and backward maximum match over a
dictionary
trie, with stemming and lemmatisation on the latin path. XGBoost is not
carried
over. None of the data lives in git -- the dictionary, the NLTK corpora
and the
ONNX graphs run to about a hundred megabytes, so `make fetch-resources`
on an
install and an optional Docker layer behind `RAVEN_RESOURCES` both point
at one
script. A checkout that could not reach the network still works, and
every
feature that reads this data says so when asked.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Run on macOS against Python 3.12, with the deepdoc weights fetched (the
model
tests skip without them):

```
uv run pytest                        27450 passed, 111 skipped, 3 failed
npm run test --prefix ui-web          3276 passed
npm run test --prefix ui-tui          2106 passed
npm run type-check --prefix ui-web    clean (tsc)
npm run lint --prefix ui-web          clean (4 pre-existing hook warnings)
make lint-python                      clean (ruff check + format)
make lint-types                       clean (ty)
make lint-imports                     10 contracts kept, 0 broken
make check-source-language            clean
make check-large-files                clean
make build-ui                         built, both boot snapshots match
node ui-web/scripts/check-css.mjs     clean
node ui-web/scripts/check-class-namespace.mjs  clean
```

The three Python failures are not this branch's and were each confirmed
on the
base alone: one asks a host without LibreOffice to refuse a render and
this host
has LibreOffice installed, and two simulation cases fail on an
unmodified
checkout of the base commit.

The RPC contract was regenerated for both consumers and both match it
(`npm run gen --prefix ui-web`, `npm run gen:rpc --prefix ui-tui`, then
`gen:check`). The page's boot snapshots and region goldens were
refreshed where
the composer bar gained a button, and each records that button and
nothing else.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

A PDF's tables, and a Word or PowerPoint file's, are now indexed as HTML
rather
than as header-prefixed lines. Chunk text changes shape for those
documents, so
a base indexed before this and searched after it holds both forms until
its
documents are reindexed; retrieval keeps working either way, because
both forms
are text. The same applies to how much prose a table or a figure carries
with
it: the setting now reaches as far as it says, where before it stopped
at the
nearest paragraph, so a chunk cut before this keeps the narrower context
until
its document is rebuilt. Rolling back is reverting the branch and
reindexing.

The deepdoc models are optional. An install that never ran `make
fetch-resources` reads PDFs geometrically, exactly as before, and says
so when
the models are asked for by name. Nothing here reaches the network at
request
time.

Chunk crops and rendered pages are written at index time and on first
view, both
under the state directory rather than in the repository, and both are
deleted
with their document. A page is immutable for the life of its document
id, so
the route serving them caches; the document route does not. All three
routes
take ids and no path, and the two image routes answer under the
strictest
sandbox in the tree.

What an agent may search is the reader's choice and nothing else. The
tool takes
no base argument, so a model cannot name one, and it is absent from the
schema
entirely for a conversation that named none -- a lane nobody pointed at
a base
is unchanged by this branch. The bases a session carries are stored
beside its
working directory and travel no further.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-opus-5) <noreply@anthropic.com>
2026-10-12 19:05:16 +08:00

438 lines
16 KiB
Python

"""Unit tests for heading-aware sectioning and chunking."""
from __future__ import annotations
import asyncio
from raven.knowledge._sections import MAX_SECTION_CHARS, SECTION_ORDINAL
from raven.knowledge._structure import HeadingAwareChunker, StructuredTextParser
from raven.knowledge._types import Section, TextBlock
def _parse(text: str, filename: str) -> list[Section]:
return asyncio.run(StructuredTextParser().parse(text, filename))
def _chunk(sections: list[Section], **kwargs) -> list:
return asyncio.run(HeadingAwareChunker(**kwargs).chunk(sections))
def _texts(items) -> list[str]:
return [item.content.text for item in items]
MARKDOWN = """\
Intro paragraph.
# Results
Overall summary.
## Latency
The p99 was 40ms.
## Throughput
Peaked at 900 rps.
# Appendix
Raw numbers.
"""
def test_markdown_splits_on_headings():
sections = _parse(MARKDOWN, "report.md")
assert [section.metadata.get("heading") for section in sections] == [
None,
"Results",
"Latency",
"Throughput",
"Appendix",
]
assert "Intro paragraph." in sections[0].content.text
assert sections[0].metadata == {}
def test_markdown_records_the_ancestor_path():
sections = _parse(MARKDOWN, "report.md")
latency = next(s for s in sections if s.metadata.get("heading") == "Latency")
assert latency.metadata["heading_path"] == ["Results", "Latency"]
assert latency.metadata["heading_level"] == 2
def test_a_sibling_heading_pops_the_deeper_level():
"""`Appendix` is an h1 arriving after two h2s; leaving them on the stack
would file the rest of the document under a subsection it left."""
sections = _parse(MARKDOWN, "report.md")
appendix = next(s for s in sections if s.metadata.get("heading") == "Appendix")
assert appendix.metadata["heading_path"] == ["Appendix"]
def test_markdown_keeps_its_heading_markers_in_the_section_text():
sections = _parse(MARKDOWN, "report.md")
latency = next(s for s in sections if s.metadata.get("heading") == "Latency")
assert latency.content.text.startswith("## Latency")
def test_a_hash_inside_a_code_fence_is_not_a_heading():
text = "# Real\n\n```bash\n# not a heading\necho hi\n```\n"
sections = _parse(text, "snippet.md")
assert len(sections) == 1
assert "# not a heading" in sections[0].content.text
def test_a_hash_without_a_space_is_not_a_heading():
sections = _parse("#hashtag stays inline\n", "note.md")
assert len(sections) == 1
assert sections[0].metadata == {}
def test_a_heading_holding_only_subsections_is_not_indexed_alone():
"""It would be a chunk of nothing but its own title; the children keep the
name in their path."""
sections = _parse("# Parent\n## Child\n\nBody.\n", "doc.md")
assert [s.metadata.get("heading") for s in sections] == ["Child"]
assert sections[0].metadata["heading_path"] == ["Parent", "Child"]
def test_a_document_without_headings_stays_one_section():
sections = _parse("Just prose.\nMore prose.\n", "plain.md")
assert len(sections) == 1
assert sections[0].metadata == {}
assert sections[0].source == "plain.md"
def test_an_empty_document_still_yields_one_section():
sections = _parse(" \n", "empty.md")
assert len(sections) == 1
HTML = """\
<html><head><title>T</title><style>h1 { color: red }</style></head>
<body>
<h1>Findings</h1>
<p>Overall&nbsp;good.</p>
<h2>Errors</h2>
<p>Three of them.</p>
<script>console.log("noise")</script>
<table><tr><td>a</td><td>b</td></tr></table>
</body></html>
"""
def test_html_splits_on_heading_tags():
sections = _parse(HTML, "report.html")
assert [s.metadata["heading"] for s in sections] == ["Findings", "Errors"]
assert sections[1].metadata["heading_path"] == ["Findings", "Errors"]
def test_html_tags_and_scripts_do_not_reach_the_index():
sections = _parse(HTML, "report.html")
body = "\n".join(_texts(sections))
assert "<p>" not in body
assert "console.log" not in body
assert "color: red" not in body
assert "Overall good." in body
def test_html_is_detected_without_an_html_extension():
sections = _parse("<h1>Title</h1><p>Body.</p>", "export")
assert sections[0].metadata["heading"] == "Title"
def test_a_markdown_extension_is_never_sniffed_as_html():
"""A fenced HTML example inside a .md file must not switch parsers."""
sections = _parse("# Doc\n\n```html\n<html><h1>x</h1></html>\n```\n", "guide.md")
assert sections[0].metadata["heading"] == "Doc"
assert "<html>" in sections[0].content.text
def test_chunks_never_span_two_sections():
chunks = _chunk(_parse(MARKDOWN, "report.md"))
assert all("Latency" not in text or "Throughput" not in text for text in _texts(chunks))
assert [chunk.chunk_index for chunk in chunks] == list(range(len(chunks)))
assert {chunk.total_chunks for chunk in chunks} == {len(chunks)}
def test_a_split_section_carries_its_heading_path_on_later_chunks():
long_body = "word " * 400
sections = _parse(f"# Top\n\n## Deep\n\n{long_body}\n", "long.md")
chunks = _chunk(sections, chunk_size=64, overlap=8)
assert len(chunks) > 1
assert chunks[0].content.text.startswith("## Deep")
assert all(chunk.content.text.startswith("Top > Deep\n\n") for chunk in chunks[1:])
def test_a_headingless_section_is_chunked_unchanged():
sections = [Section(content=TextBlock(text="a " * 400), source="plain.txt")]
chunks = _chunk(sections, chunk_size=64, overlap=8)
assert len(chunks) > 1
assert not any(chunk.content.text.startswith(" > ") for chunk in chunks)
assert all(chunk.metadata == {} for chunk in chunks)
def test_the_prefix_is_paid_for_out_of_the_chunk_budget():
"""Prefixing after the split would push a chunk past the size the embedding
model was configured for."""
sections = _parse("# A\n\n## B\n\n" + "word " * 400, "long.md")
chunks = _chunk(sections, chunk_size=64, overlap=8)
assert all(len(chunk.content.text.encode("utf-8")) // 4 <= 64 for chunk in chunks)
def test_a_split_section_stores_a_verbatim_copy_of_itself():
"""Reassembling a section from its chunks after the fact is unsafe on
repetitive text, so the split records the original instead."""
body = "One clause covering the cell, the system and the harness. " * 40
section = _parse(f"## Deep\n\n{body}\n", "long.md")[0]
chunks = _chunk([section], chunk_size=64, overlap=8)
stored = [c.metadata.get("section_text") for c in chunks if "section_text" in c.metadata]
assert len(chunks) > 1
assert stored == [section.content.text]
def test_a_section_that_fits_in_one_chunk_stores_no_copy():
"""The chunk already is the section; a copy would double it for nothing."""
chunks = _chunk(_parse("## Small\n\nShort body.\n", "s.md"))
assert all("section_text" not in chunk.metadata for chunk in chunks)
def test_an_oversized_section_is_not_copied():
"""It would never be inlined, so storing it only inflates the index."""
section = _parse("## Big\n\n" + "x" * (MAX_SECTION_CHARS + 100), "big.md")[0]
chunks = _chunk([section], chunk_size=64, overlap=8)
assert all("section_text" not in chunk.metadata for chunk in chunks)
# ------------------------------------------------------------ section identity
def test_each_section_gets_its_own_ordinal():
sections = _parse("# A\n\nbody a\n\n# B\n\nbody b\n", "doc.md")
assert [s.metadata[SECTION_ORDINAL] for s in sections] == [0, 1]
def test_two_same_named_siblings_are_told_apart():
"""The failure no guard on the reading side could close: one parent with two
same-named children gives both the same full heading path, and a reader keying
on the path hands a hit in the second the text of the first. The ordinal is
what separates them, and it can only be recorded here."""
text = "## Usage\n\nlead\n\n### Example\n\nfirst body\n\n### Example\n\nsecond body\n"
sections = _parse(text, "doc.md")
examples = [s for s in sections if s.metadata.get("heading") == "Example"]
assert len(examples) == 2
assert examples[0].metadata["heading_path"] == examples[1].metadata["heading_path"]
assert examples[0].metadata[SECTION_ORDINAL] != examples[1].metadata[SECTION_ORDINAL], (
"the two sections share an identity"
)
def test_a_document_with_no_headings_records_no_ordinal():
"""One section cannot be ambiguous, and the no-headings case is documented to
degrade to the previous behaviour exactly -- empty metadata included."""
sections = _parse("just prose, no headings at all\n", "doc.md")
assert len(sections) == 1
assert sections[0].metadata == {}
def test_every_chunk_of_a_section_carries_its_ordinal():
"""The ordinal has to reach the chunks, not only the sections: expansion reads
it off a chunk to find the section's siblings."""
long_body = "sentence. " * 400
text = f"## Usage\n\nlead\n\n### Example\n\n{long_body}\n"
chunks = _chunk(_parse(text, "doc.md"))
example = [c for c in chunks if c.metadata.get("heading") == "Example"]
assert len(example) > 1, "this case needs a split section"
assert {c.metadata[SECTION_ORDINAL] for c in example} == {1}
def test_the_structured_parser_only_claims_what_it_can_cut():
"""This parser claims only the two formats it can find headings in, so a
registry holding it alone would leave plain text, CSV, JSON, YAML and RST
with no parser at all. TextParser has to stay alongside it for the rest."""
from raven.knowledge.parser.text_parser import TextParser
assert StructuredTextParser.supported_media_types == ["text/markdown", "text/html"]
left_behind = set(TextParser.supported_media_types) - set(StructuredTextParser.supported_media_types)
assert left_behind, "TextParser would be redundant, and the service could drop it"
assert "text/plain" in left_behind
def test_html_that_omits_the_head_close_is_still_sectioned():
"""`</head>` is optional in HTML and `html.parser` never closes it, so the
drop counter used to stay above zero for the rest of the file: every body came
out empty, no sections were produced, and `parse` fell back to one whole-file
Section carrying the raw markup -- the exact input this module exists to avoid.
"""
html = (
"<html>\n<head><title>Quarterly</title><style>body{color:red}</style>\n"
"<body>\n<h1>Findings</h1><p>Revenue rose.</p>\n"
"<h2>Risks</h2><p>Supply chain.</p>\n</body></html>"
)
sections = _parse(html, "q.html")
assert [s.metadata["heading"] for s in sections] == ["Findings", "Risks"]
body = "\n".join(s.content.text for s in sections)
assert "<style" not in body and "<title" not in body
assert "Revenue rose." in body and "Supply chain." in body
def test_a_script_inside_the_body_is_still_dropped():
"""The counter reset keys on `<body>`, which must not weaken the guard for
tags that legitimately open after it."""
html = "<html><body><h1>A</h1><script>steal()</script><p>one</p></body></html>"
sections = _parse(html, "q.html")
body = "\n".join(s.content.text for s in sections)
assert "one" in body
assert "steal()" not in body
# -- cutting where the document allows -----------------------------
def _with_elements(parts: list[str]) -> Section:
"""A section whose element spans say where its parts begin and end.
What a parser records when it knows: the docx parser writes one span per
paragraph, table and figure. Built by hand here so the rule can be tested
without a document format in the way.
"""
text = "\n\n".join(parts)
spans = []
at = 0
for index, part in enumerate(parts):
spans.append({"reading_order": index, "layout_type": "text", "char_start": at, "char_end": at + len(part)})
at += len(part) + 2
return Section(content=TextBlock(text=text), source="handbook.docx", metadata={"elements": spans})
def test_a_paragraph_is_never_cut_in_half():
"""A paragraph is the unit a person wrote and the unit a reader reads. Half
of one is embedded as the half-thought it has become, and reads back as a
sentence that stops."""
paragraphs = [f"Paragraph {n}. " + " ".join(f"word{i}" for i in range(60)) for n in range(6)]
section = _with_elements(paragraphs)
chunks = _chunk([section], chunk_size=200, overlap=20)
assert len(chunks) > 1, "the section is too big for one chunk, so this is a real test"
for paragraph in paragraphs:
assert sum(paragraph in chunk.content.text for chunk in chunks) == 1, paragraph[:20]
def test_chunks_are_packed_with_whole_elements():
"""Packed until the next one will not fit, so a chunk is as full as whole
paragraphs allow rather than as full as bytes allow."""
section = _with_elements(["First.", "Second.", "Third."])
chunks = _chunk([section], chunk_size=200, overlap=20)
assert _texts(chunks) == ["First.\n\nSecond.\n\nThird."]
def test_an_element_larger_than_the_budget_is_still_split():
"""One paragraph can be longer than any window. Refusing to cut it would
mean refusing to index it."""
long_one = " ".join(f"word{i}" for i in range(400))
section = _with_elements([long_one, "A short one after it."])
chunks = _chunk([section], chunk_size=100, overlap=10)
assert len(chunks) > 1
assert "".join(c.content.text for c in chunks).count("word399") == 1
def test_a_split_inside_a_paragraph_falls_between_words():
"""Where it has to cut, it cuts at a break rather than through a word: two
halves of `paragraph` are not a word either half of the index knows."""
section = _with_elements([" ".join(f"word{i}" for i in range(400))])
chunks = _chunk([section], chunk_size=100, overlap=10)
for chunk in chunks[:-1]:
assert chunk.content.text.endswith(" ") or chunk.content.text[-1].isalnum()
# The tail of every piece but the last is a whole token.
tail = chunk.content.text.rstrip().rsplit(" ", 1)[-1]
assert tail.startswith("word") and tail[4:].isdigit(), tail
def test_a_section_with_no_element_spans_is_cut_as_before():
"""Markdown and plain text carry no spans, and nothing about them
changed."""
section = Section(content=TextBlock(text="x" * 4000), source="notes.md", metadata={})
chunks = _chunk([section], chunk_size=100, overlap=10)
assert len(chunks) > 1
def _tokens(text: str) -> int:
"""The same estimate the chunker sizes with."""
return len(text.encode("utf-8")) // 4
def test_the_heading_prefix_is_paid_out_of_the_chunk_budget():
"""The prefix is prepended to every piece after the first, so the split has
to be made against what is left after it. Split at the full size instead,
every later chunk comes back over the limit the budget exists to keep --
which is an embedding input limit, not a preference."""
path = ["Handbook", "Operations", "Escalation policy"]
section = Section(
content=TextBlock(text=" ".join(f"word{n}" for n in range(400))),
source="handbook.docx",
metadata={"heading_path": path},
)
chunks = _chunk([section], chunk_size=64, overlap=8)
assert len(chunks) > 1, "the section has to be split for this to mean anything"
assert max(_tokens(chunk.content.text) for chunk in chunks) <= 64
def test_an_element_too_big_on_its_own_is_split_against_the_budget_too():
"""The other branch, and the same rule: one paragraph longer than any
window still has to come back in pieces that fit with the prefix on."""
section = _with_elements([" ".join(f"word{n}" for n in range(400))])
section.metadata["heading_path"] = ["Handbook", "Operations", "Escalation policy"]
chunks = _chunk([section], chunk_size=64, overlap=8)
assert len(chunks) > 1
assert max(_tokens(chunk.content.text) for chunk in chunks) <= 64
def test_a_section_with_no_heading_still_cuts_at_the_full_size():
"""Nothing is prefixed to it, so nothing is taken off its budget."""
section = Section(content=TextBlock(text=" ".join(f"word{n}" for n in range(400))), source="n.md", metadata={})
chunks = _chunk([section], chunk_size=64, overlap=8)
assert max(_tokens(chunk.content.text) for chunk in chunks) <= 64
assert len(chunks) < 20, "and is not cut finer than it was asked to be"