Repository navigation
knowledge.add reads headings and fences as CommonMark does - #171
Merged
Merged
Conversation
The heading splitter closes a fence only on a bare run of its character at least as long, refuses list items, rules, HTML blocks and comment lines as setext titles, finds setext headings in CRLF files and under an underline indented up to three spaces, and drops only inline tags, code backticks and matched * emphasis from a title, reading markup in its first 500 characters. A heading line with a long run of blanks is read in linear time. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A line inside a multi-line comment, raw-text block, declaration or block-tag block is text, never a heading: the block ends at its end marker, or a blank line for block tags and tag-only lines, and a tag-only line does not interrupt a paragraph. The fence and HTML state move into _Opaque, the unused _atx_body goes, and __all__ is back to the public names. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ed, and a one-line <hN> opens its block CommonMark's condition 7 takes one open or closing tag on a line, so a badge line of several tags is a paragraph a heading below interrupts. Only a paragraph's line keeps a tag-only line from opening a block (not a closed block, a rule or an empty ATX line). An ATX heading may be indented up to three spaces, and a one-line <hN> opens a block to the next blank line. The -->-line refusal is named a heuristic. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ceholder is text Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…what the splitter reads A parametrised test pins each title the change cleans differently, and a backtick run with a backtick after it opens no fence. The long-heading test checks a literal title, _atx is named for what it strips, and the docs name the headings and the lines that are never headings. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #157
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
knowledge.addreads headings and fences as CommonMark does, so a fence that closes on a longer info string, an HTML block, a list item over---or a code identifier in a heading no longer moves chunk boundaries or garbles section paths, and a heading line with a long run of blanks no longer takes seconds to read. A document's chunks change when it is next written or re-chunked.Why
The heading splitter behind
knowledge.add,upsertandbulk_addread Markdown with ad-hoc patterns. They closed a fence on any line starting with three backticks, stripped every<...>,_and*from titles (Option<String>,max_rows,__init__,2 * 3), took list items, HTML blocks and comment lines over---as headings, missed setext headings in CRLF files, and backtracked on an ATX line with a long run of blanks: 40 KB of blanks took 25 s. These rules were found while building the document parser for the file-ingestion proposal; that parser now lives in the extractor service, so the splitter's fixes stand on their own here.The splitter now follows CommonMark's line rules for what it reads: ATX and setext headings, fenced code (a fence closes only on a bare run of its own character at least as long), list items, thematic breaks and HTML blocks (each kind ends where CommonMark ends it). A title drops only inline HTML tags from a fixed list, code-span backticks and matched
*emphasis; underscores are kept, since they are names far more often than emphasis. Markup is read in a title's first 500 characters, so the patterns stay bounded on hostile input, and the rest of the title is kept as written.Scope and risk
Only the splitter in
server/knowledge/domain/chunking.pychanges behaviour; its tests and one sentence ofdocs/features/knowledge.mdfollow. The heading, fence and title helpers are rewritten (private, as before;__all__is unchanged), anddetect_sectionsreads lines through a small_Opaquestate that knows when a fence or an HTML block is open. All toward CommonMark; a document changes on its next write orloom rechunk --applyonly if it has one of these, and then its chunks change, not only their section paths: a fence that closes elsewhere or a line that is now or no longer a heading moves chunk boundaries, text,char_startand the embedding input.```pythonline inside an open```fence```js``` is inline## `max_rows`maxrowsmax_rows## Option<String> fields,Promise<T>,Into<S>,Map<a, b>,<your token>,<Badge text="beta" />,<wbr><img ...>,<br/>,<code>x</code>,<b>, ...)## Using C#Using CUsing C#__init__,max_rows, a lone*(2 * 3)## _Note_,## __Bold__(underscore emphasis)Note,Bold_Note_,__Bold__(underscores are kept: names far more often than emphasis)<!-- note --> Installover---Install-->over----was refused)Next step -->over---Next step -->-->may close one## a*b*c(stars inside a word)abca*b*c(only matched emphasis is dropped)## a<br/>b,## a<img src=x>b(an inline tag between words)aba b(a tag is replaced by a space)``ab`` `abab` (a code span closes on a backtick run of its own length)## Don`t,## **Note(an unmatched backtick or**)Dont,Note# #, a bare#(an empty heading), also over---### Closed ##in a CRLF fileClosed ##Closed**Warning**,*Note*,*args,--force,-1 is negativeover---or===-or*was refused)-->), opening with a block-level tag or holding one tag alone is refused)1. Overview,2) Setup,+ xover---***and-were never one and still are not)<p>Title</p>,<p align="center"><img ...></p>,<div></div>over---Title, or no heading (every tag was stripped and an empty title refused)<https://example.com>over---;## See <https://x><...>stripped, so an empty title);Seehttps://example.com;See https://x(an autolink keeps its URL)<pre>x</pre>,<!DOCTYPE html>over---x; no heading<!...>line starts an HTML block)## Draftinside a multi-line<!-- ... -->,<pre>, or a<div>block before its blank line# Setup(indented up to three spaces)Setup(four spaces is still not one)Titleover--or=# Bright after<h2>A</h2>B<hN>is a heading and opens a block-tag HTML block to the next blank line, as in CommonMark```line inside a```` fence``` # end)*and tag strippedCompatibility, rollout and rollback
knowledge.add,upsertorbulk_add, re-indexed byknowledge.update_metadatawithreindex=true, or re-chunked withloom rechunk --apply. Chunk-level dedup (knowledge.dedup_listwith chunk clusters,upsert'sduplicate_chunk_count) compares chunk content hashes, so two identical documents chunked on either side of this change stop pairing at chunk level until both are re-chunked; document-level dedup is unaffected. During a rolling deploy, builds N-1 and N chunk such a document differently, each consistently.loom rechunk --applybrings existing documents to the new rules if wanted.Tests
tests/unit/test_knowledge_chunking.py: the existing tests pass; new ones cover a fence that does not close on a longer or info-string line, a bold or emphasised line over a rule as a heading, a rule over a rule, underlines indented up to three spaces (not four or a tab), list items over a rule, HTML block lines over a rule (block tags, tag-only lines, comments, raw-text blocks,<!DOCTYPE>, a line ending-->), HTML blocks ending at their end marker so the next line can be a title, headings inside a multi-line comment,<pre>,<div>or declaration read as block text, block-tag blocks ending at a blank line, a tag-only line not interrupting a paragraph, a one-line<h2>still a heading whose block runs to the blank line, an autolink over---a heading, a badge line of several tags interrupted by an ATX heading, a lone tag after a closed comment or a rule opening a block, a<YOUR_API_KEY>placeholder read as text, every title row of the table above (code spans, generics, placeholders,C#, underscores, a lone or inner*, inline tags, unmatched backticks and**,*Note*,*args,--force,-1 is negativeover a rule), a backtick run with a backtick after it opening no fence, an ATX heading indented up to three spaces, an underline shorter than three kept as text, autolinks kept in titles, a bare#as an empty heading, a long heading kept whole with its markup read in a bounded head, a heading with a 40 KB run of blanks read whole, and hostile lines (a 1 MB backtick run, 200 KB of blanks, 600 KB of unclosed emphasis, an HTML line over 50,000 underlines, 40 self-closing tags, an unclosed raw-text block) chunked in bounded time.The two timing rows of the table, reproduced with:
tests/unit/test_loom_ingest.py,test_doc_links.py,test_public_copy.py: pass.make format-check,make lint, the mypy gate: pass.pytest tests/unit -k "chunk or knowledge or rechunk or outline or ingest or document or markdown or heading": 1,085 passed at the first push.pytest tests/unit -p randomlyat b4abcad: 24,207 passed (the commits after it add three title cases and the underline test and move an import, andtest_knowledge_chunking.pypasses at the head);test_rmt_version_unit.py::test_no_writer_builds_a_millisecond_versionwas deselected, as it fails onmaintoo.Integration, since
knowledge.addchunks change:pytest tests/integration -k "knowledge or outline or document or chunk"against a server and ClickHouse built from this branch: 58 passed; 36 skipped (the knowledge-sharing tests that need a test end-user issuer, and others that need settings this server does not set).AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.docs/reference/configuration.mdand thedocs/reference/tables (python scripts/codegen/docs_reference.py), the reach manifest,schemas/schema.sql(make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code