Repository navigation
feat(provenance): persist document structure as document_blocks - #105
Merged
Merged
Conversation
`addDocumentFromFile()` read `parseResult.content/title/mimeType/metadata` and dropped `parseResult.structure`, so nothing downstream could tell which page or heading a character belonged to. Add the missing persistent layer. - `document_blocks` (schema + additive migration 0014): id, document_id, kind, order, page, level, text, start_offset, end_offset, bbox, metadata, with indexes on (document_id, order) and (document_id, page). - `buildDocumentBlocks()` turns a parse result into blocks in document order. It is pure and never re-cleaned text: every block's text is `content.slice(startOffset, endOffset)`, so the invariant `documents.content.slice(start_offset, end_offset) === text` holds by construction. Paged documents get one block per page with a normalized bbox; Markdown/DOCX use their heading structure (heading + paragraph blocks); text/URL/notes and structures with unusable offsets fall back to paragraph-level flat blocks. - `KnowledgeService` persists blocks in both ingestion paths (`addDocumentFromFile` and `addDocument`, which covers text, URL and note), exposes `getDocumentBlocks()`, and deletes a document's blocks explicitly rather than relying on a foreign-key cascade that the connection does not enable. - `PdfLoader` now records a page's normalized text bounding box, computed from its text items through the existing viewport transform. `chunks` are untouched, so retrieval and search results are unchanged; this issue only adds data. Verification: `test/blocks.test.ts` asserts the slice invariant, dense ordering, PDF page agreement and bbox normalization, DOCX/Markdown heading levels, and the flat fallback — 100 tests pass. `npm run typecheck`, `npm run check:design` and `npm run build` are clean (the build copies 0014 into out/main/db/migrations). Refs #66
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.
What does this PR do?
Persists the document structure the loaders already extract, so every character of a document can be traced back to a page, a heading and a bounding box. This is the data layer the citation work (#67, #69–#73) will build on.
Why?
addDocumentFromFile()consumed onlyparseResult.content/title/mimeType/metadataand discardedparseResult.structureentirely. There was nowhere to record which page or heading a span of text came from, so a future citation could only ever be document-level. See #66 and the epic #82.Related issue
Fixes #66
What changed?
document_blockstable (additive migration0014_naive_lifeguard.sql):id,document_id,kind,order,page,level,text,start_offset,end_offset,bbox,metadata; indexes on(document_id, order)and(document_id, page). No changes tochunks.src/main/services/blocks/documentBlocks.ts—buildDocumentBlocks()is a pure function from a parse result to ordered block drafts. It never re-cleans text: every block'stextiscontent.slice(startOffset, endOffset), sodocuments.content.slice(start_offset, end_offset) === textholds by construction.pagematchingstructure.pages, with the page's normalizedbbox.kind: 'heading',level) plus paragraph blocks for the body between headings. It only trusts a heading offset when the title actually appears there, so WebLoader's all-zero offsets fall back to flat blocks instead of becoming bogus headings.KnowledgeService— persists blocks in both ingestion paths:addDocumentFromFile()(with structure) andaddDocument()(text, URL and note, flat). AddsgetDocumentBlocks(), and deletes a document's blocks explicitly rather than relying on a foreign-key cascade the connection does not enable.PdfLoader— records a page's normalized textbbox, mapping its text items through the existing viewport transform so the renderer does not need to know the scale.chunksare untouched, so retrieval and search results are unchanged — this issue only adds data.How was this tested?
npm test— 100 tests pass (8 new intest/blocks.test.ts): the slice invariant for every block from every fixture, dense/ordered numbering, PDF page agreement, bbox normalization inside the unit square, DOCX/Markdown heading levels, the flat fallback, and the untrusted-offset fallback.npm run typecheck— clean.npm run build— clean; verified0014_naive_lifeguard.sqlandmeta/0014_snapshot.jsonare copied intoout/main/db/migrations.npm run check:design— clean.npx eslinton every changed file — clean.Not run locally:
build:unpack+smoke:packaged(noxvfbon this machine). CI runs both on all three platforms;runMigrations()there applies migration 0014 against a fresh database, which is the packaged check that matters for this change. A manual PDF import is the remaining manual step from the issue.Screenshots / recordings
Not applicable — no UI change.
Checklist
npm run typecheckpasses.npm run buildpasses.Desktop / build changes
npm run build:unpackpasses. (delegated to CI)npm run smoke:packagedpasses. (delegated to CI)