Skip to content

feat(clients): add kosha-ingest for PDF/DOCX/HTML with page boundaries (#587) - #602

Merged
decover-admin merged 8 commits into
mainfrom
devin/1795744360-ingest
Nov 27, 2026
Merged

decover-admin merged 8 commits into
mainfrom
devin/1795744360-ingest

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Part of #587 (Phase L, #589). Until now Kosha has only indexed JSON documents. This PR adds kosha-ingest (clients/python/kosha_client/ingest.py, under the new ingest extra), which turns a folder of .pdf, .docx, .html/.htm, .txt and .md files into Kosha documents.

Build vs. integrate: extraction runs client-side and integrates OSS extractors, pypdf and python-docx, both pure Python. HTML uses the stdlib html.parser. It needs no new server dependency or worker, and a hosted parser or pdfium could replace extract() later.

Pages:

  • PDF pages are joined with a form feed (\f). That is the page separator read(page=n) cuts on in feat(api): add read for line, page and byte ranges inside a document (#586) #600, so read returns the right PDF page.
  • A DOCX page ends at an explicit page break or pageBreakBefore. Word's rendered layout pages are not available without rendering.
  • Form feeds in a .txt file (pdftotext output) are page breaks too.
  • --per-page writes one document per page instead, with a page Integer field, so a search hit carries its page number.

Fields:

Field Type Contents
path Keyword --prefix + the relative path (#585)
body Text Extracted text
title Text Document title
mime_type Keyword MIME type
size_bytes Integer File size
modified_at Date mtime, UTC
content_sha256 Keyword Content hash
page_count Integer Number of pages
scanned Boolean No text layer found
source Keyword Where the file came from

Idempotent re-ingest:

  • The id is f_ + sha256(path)[:32], so a changed file upserts over its old document.
  • Before writing, it looks up the stored content_sha256 for each batch of 64 paths and skips unchanged files. --force overrides this.
  • With --per-page, pages past a shortened file's new length are deleted.

Scanned PDFs: pages with no text layer are flagged with scanned: true, have an empty body and are listed in the report. OCR stays out of scope for v1.

Failure handling: a file that fails to extract is reported under failed and the rest of the folder still ingests. The exit code is 1 when any file fails. Hidden files are skipped.

Docs are in clients/python/README.md ("Ingesting PDF, DOCX and HTML files").

How it was tested

  • tests/test_ingest.py covers:
    • PDF fixtures built in-test: page separation, the title, and scanned detection.
    • DOCX: page breaks and tables.
    • HTML: title, skipped script/style, block structure and entities.
    • Plain text with form feeds.
    • Paths and ids, and the document shape in whole-file and per-page modes.
    • Re-ingest: unchanged files are skipped, a changed file is replaced, --force re-indexes, and per-page deletes run when a file shrinks.
    • A broken PDF reported as a failure, plus unsupported and hidden files.
  • make client-python-test (now installs .[ingest]): 343 passed, 35 skipped.
  • By hand, against a local kosha-server, on a mixed folder (a 3-page PDF, a scanned PDF, a 3-page DOCX and an HTML file):
    • First run: indexed: 4, with the scanned PDF flagged. Second run: unchanged: 4.
    • With --per-page, searching for monthly returned /contracts/2024/msa.pdf page 3 and /contracts/lease.docx page 2. Both are correct.
    • Whole-file bodies hold \f-separated pages, and prefix on path and term on scanned work.

Compatibility

  • The on-disk segment format (files under a segment, footer, posting/vector/filter layout)
  • The manifest, WAL, or Postgres schema
  • The /v1 wire contract (proto/kosha/v1/kosha.proto, request/response JSON)
  • None of the above

This change is client-only. ingest is an optional extra, so a plain pip install kosha-client still has no dependencies. #601 edits the same pip install lines in ci.yml and Makefile, so whichever PR merges second needs .[mcp,ingest] on those lines.

Checklist

  • cargo fmt --all -- --check (no Rust changes)
  • cargo clippy --all-targets --all-features -- -D warnings (no Rust changes)
  • cargo test --all-features (no Rust changes)
  • make client-python-test (if clients/python changed)
  • docker build -t kosha:ci . (if the Dockerfile or build configuration changed)
  • New KOSHA_* variables have a row in docs/configuration.md (none)
  • README / docs updated for user-visible changes

Link to Devin session: https://app.devin.ai/sessions/91ac22e94520430382b0941c2adf82e1
Open in Devin Desktop: https://app.devin.ai/desktop/session/91ac22e94520430382b0941c2adf82e1?variant=devin
Requested by: @ravi-decoverai


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

#587)

Co-Authored-By: Ravi Decover AI <[email protected]>

---kosha-bench---
microbenchmark: segment_memory (cargo bench -p kosha-query --bench segment_memory)
corpus: 8 segments × 4000 docs (32000 total), vocab ~20000
runner: Linux 6.6.141 x86_64 | rustc 1.100.0 (0ef1bc386 2026-11-09) | 2026-11-27T01:57:39Z

latency (v2 = current lazy format, median of inner samples):
  cold broad ("the")                 3.2 ms
  warm broad ("the")                 0.11 ms
  warm 2-term AND                    0.12 ms
  warm phrase                        0.09 ms
  warm wildcard w1*                  45.28 ms
---end kosha-bench---
@github-actions
github-actions Bot force-pushed the devin/1795744360-ingest branch from d2937a2 to 46ae404 Compare November 27, 2026 01:57
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

kNN microbench — main vs this PR (knn_open_search)

✅ No kNN regression past its limit.

before = main 9d807303a · after = PR 628dcbbf8 · same runner, runs interleaved (2× / 2×), best p50 of each side

metric before after Δ limit
warm kNN p50 0.26 ms 0.24 ms -7.4% +25%
p90 (not gated) 0.32 ms 0.29 ms -11.0%
p99 (not gated) 0.66 ms 0.56 ms -15.7%
warm hybrid p50 0.33 ms 0.34 ms +5.6% +25%
p90 (not gated) 0.37 ms 0.50 ms +35.1%
p99 (not gated) 0.43 ms 0.57 ms +33.4%
cold kNN p50 1.99 ms 2.23 ms +12.0% +40%
p90 (not gated) 2.52 ms 2.48 ms -1.5%
p99 (not gated) 3.11 ms 2.75 ms -11.6%
open all segments p50 1.09 ms 1.10 ms +0.5% +40%
p90 (not gated) 1.17 ms 1.31 ms +12.8%
p99 (not gated) 1.28 ms 1.38 ms +7.2%
postings probed / query 100.0 100.0 +0.0% +10%

corpus 8 segs × 4000 docs, dim 256, 64 clusters · latency fails only past both the % limit and an absolute floor · postings/query is deterministic · label perf-regression-accepted to accept an intended cost

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⏱ Microbench — main vs this PR (segment_memory, v2 format)

before = merge-base 9d807303a · after = PR head 628dcbbf8 · same runner, back to back

metric pctl before (main) after (PR) Δ
cold broad ("the") p50 2.76 ms 2.69 ms -2.5%
p90 3.73 ms 2.96 ms -20.5% 🟢
p99 3.99 ms 3.14 ms -21.2% 🟢
warm broad ("the") p50 0.092 ms 0.087 ms -4.6%
p90 0.140 ms 0.093 ms -33.8% 🟢
p99 0.275 ms 0.113 ms -58.8% 🟢
warm 2-term AND p50 0.103 ms 0.103 ms -0.4%
p90 0.109 ms 0.106 ms -3.0%
p99 0.141 ms 0.135 ms -4.8%
warm 3-term AND broad p50 0.115 ms 0.116 ms +0.4%
p90 0.146 ms 0.151 ms +3.1%
p99 0.156 ms 0.160 ms +2.1%
warm phrase p50 0.079 ms 0.078 ms -0.7%
p90 0.084 ms 0.081 ms -3.5%
p99 0.095 ms 0.108 ms +12.6% ⚠️
warm wildcard w1* p50 42.4 ms 42.1 ms -0.5%
p90 43.4 ms 43.0 ms -0.9%
p99 45.2 ms 44.3 ms -2.1%
resident while open — 13.1 MiB 13.1 MiB +0.0%

corpus 8 segs × 4000 docs · cold n=25, warm n=200 · nearest-rank percentiles · deltas within ±10% are usually runner noise

devin-ai-integration Bot and others added 3 commits November 27, 2026 02:02
Co-Authored-By: Ravi Decover AI <[email protected]>

---kosha-bench---
microbenchmark: segment_memory (cargo bench -p kosha-query --bench segment_memory)
corpus: 8 segments × 4000 docs (32000 total), vocab ~20000
runner: Linux 6.6.141 x86_64 | rustc 1.100.0 (0ef1bc386 2026-11-09) | 2026-11-27T02:04:36Z

latency (v2 = current lazy format, median of inner samples):
  cold broad ("the")                 2.8 ms
  warm broad ("the")                 0.11 ms
  warm 2-term AND                    0.12 ms
  warm phrase                        0.08 ms
  warm wildcard w1*                  43.17 ms
---end kosha-bench---
@github-actions
github-actions Bot force-pushed the devin/1795744360-ingest branch from fd252af to d7e3e52 Compare November 27, 2026 02:04
@blacksmith-sh

blacksmith-sh Bot commented Nov 27, 2026

Copy link
Copy Markdown

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
test_ingest_splits_writes_by_request_size View Logs

Fix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

devin-ai-integration Bot and others added 4 commits November 27, 2026 02:08
…-source

Co-Authored-By: Ravi Decover AI <[email protected]>

---kosha-bench---
microbenchmark: segment_memory (cargo bench -p kosha-query --bench segment_memory)
corpus: 8 segments × 4000 docs (32000 total), vocab ~20000
runner: Linux 6.6.141 x86_64 | rustc 1.100.0 (0ef1bc386 2026-11-09) | 2026-11-27T02:10:06Z

latency (v2 = current lazy format, median of inner samples):
  cold broad ("the")                 2.8 ms
  warm broad ("the")                 0.10 ms
  warm 2-term AND                    0.12 ms
  warm phrase                        0.09 ms
  warm wildcard w1*                  42.07 ms
---end kosha-bench---
@github-actions
github-actions Bot force-pushed the devin/1795744360-ingest branch from 0c0cb41 to 628dcbb Compare November 27, 2026 02:10

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@decover-admin
decover-admin merged commit 565693a into main Nov 27, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants