Repository navigation
feat(api): add read for line, page and byte ranges inside a document (#586) - #600
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
…d byte ranges (#586) One stored field of one document, by id or keyword path, whole or as a 1-based line range, a form-feed separated page, or a byte range, capped at max_bytes and cut at a line end, with line/page/byte references. Beta, API contract 1.2.0. 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:42:03Z latency (v2 = current lazy format, median of inner samples): cold broad ("the") 4.5 ms warm broad ("the") 0.15 ms warm 2-term AND 0.15 ms warm phrase 0.10 ms warm wildcard w1* 45.41 ms ---end kosha-bench---
4fdf4a4 to
403ea4d
Compare
kNN microbench — main vs this PR (
|
| metric | before | after | Δ | limit | |
|---|---|---|---|---|---|
| warm kNN p50 | 0.26 ms | 0.27 ms | +5.5% | +25% | |
| p90 (not gated) | 0.31 ms | 0.32 ms | +5.2% | ||
| p99 (not gated) | 0.64 ms | 0.63 ms | -2.4% | ||
| warm hybrid p50 | 0.35 ms | 0.36 ms | +2.9% | +25% | |
| p90 (not gated) | 0.41 ms | 0.46 ms | +13.8% | ||
| p99 (not gated) | 0.46 ms | 0.53 ms | +15.2% | ||
| cold kNN p50 | 2.41 ms | 2.11 ms | -12.7% | +40% | |
| p90 (not gated) | 3.01 ms | 2.54 ms | -15.5% | ||
| p99 (not gated) | 3.59 ms | 4.04 ms | +12.5% | ||
| open all segments p50 | 1.14 ms | 1.18 ms | +3.1% | +40% | |
| p90 (not gated) | 1.22 ms | 1.26 ms | +3.2% | ||
| p99 (not gated) | 1.36 ms | 1.39 ms | +2.3% | ||
| 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
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.8.0-1060-aws x86_64 | rustc 1.99.1 (58cf8f9c3 2026-10-13) | 2026-11-27T01:42:51Z latency (v2 = current lazy format, median of inner samples): cold broad ("the") 4.1 ms warm broad ("the") 0.29 ms warm 2-term AND 0.31 ms warm phrase 0.23 ms warm wildcard w1* 96.71 ms ---end kosha-bench---
⏱ Microbench — main vs this PR (
|
| metric | pctl | before (main) | after (PR) | Δ |
|---|---|---|---|---|
| cold broad ("the") | p50 | 2.81 ms | 4.25 ms | +51.1% |
| p90 | 3.53 ms | 4.51 ms | +27.8% |
|
| p99 | 3.95 ms | 4.58 ms | +15.8% |
|
| warm broad ("the") | p50 | 0.091 ms | 0.142 ms | +56.6% |
| p90 | 0.104 ms | 0.161 ms | +54.1% |
|
| p99 | 0.119 ms | 0.215 ms | +80.3% |
|
| warm 2-term AND | p50 | 0.122 ms | 0.148 ms | +22.2% |
| p90 | 0.129 ms | 0.174 ms | +34.9% |
|
| p99 | 0.187 ms | 0.235 ms | +25.9% |
|
| warm 3-term AND broad | p50 | 0.125 ms | 0.157 ms | +26.1% |
| p90 | 0.158 ms | 0.179 ms | +13.3% |
|
| p99 | 0.223 ms | 0.250 ms | +11.8% |
|
| warm phrase | p50 | 0.094 ms | 0.110 ms | +17.2% |
| p90 | 0.099 ms | 0.126 ms | +27.0% |
|
| p99 | 0.157 ms | 0.139 ms | -11.6% 🟢 | |
| warm wildcard w1* | p50 | 45.5 ms | 50.1 ms | +10.1% |
| p90 | 50.0 ms | 60.6 ms | +21.0% |
|
| p99 | 63.6 ms | 68.8 ms | +8.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
There was a problem hiding this comment.
Devin Review found 1 finding. It is left for you to triage below.
| (Some(id), None) => serde_json::json!({ | ||
| "query_text": "", | ||
| "max_results": 1, | ||
| "filter": {"match_all": {}}, | ||
| "sort": [{"_id": {"order": "desc"}}], | ||
| "search_after": [format!("{id}\0")], | ||
| "source_includes": [field], |
There was a problem hiding this comment.
🔴 Full namespace scans on ID reads
On an uncached ID read, handle_read_with_ns requests a match-all search. The filter-only search scores every live document before the cursor cut. Even a one-line read pays the CPU and memory cost of scanning the namespace.
Devin left this for you to triage: Confirmed: a read by id costs a match_all scan, the same as the Python client's get seek today, so it is slow on large namespaces. The fix belongs in kosha-query: a point lookup by doc id (segment id index plus buffer) that read, get and mget would all use. I recommend a follow-up issue rather than a fix in this PR; read by path already avoids the scan.
Learn more
The read handler uses a descending ID cursor to identify the nearest matching ID. The generic search engine treats that request as a filter-only search and scores every live document, because the result size is not zero. It then allocates a candidate and copies its ID for each ranked document before applying the cursor and result cap. Ranged storage reads limit the final payload fetch but do not limit that scoring or allocation work. A new ID normally gives the request a different search cursor, so caching one lookup does not help the next ID.
Example: An agent reads one line from each of several distinct IDs in a million-document namespace. Each cache miss walks the live metadata for all million documents before fetching the single result record.
Recommended fix: Add an exact-ID lookup against the same search snapshot, checking live metadata and tombstones before page hydration. Use an existing ID lookup where available, or at least avoid scoring and allocating candidates for nonmatching IDs. Verify parity across shards, buffered writes and branches, and measure how much metadata work a point read performs.
Was this helpful? React with 👍 or 👎 to provide feedback.
What and why
Closes #586 (Phase L, #589).
Adds
POST /v1/namespaces/{namespace}/read(beta) so an agent can read part of a document without paging the whole stored value into its context:{"path": "/contracts/a.txt", "field": "body", "lines": {"start": 10, "end": 40}}idor by a keywordpath(path_field, defaultpath). A path that names two documents returns 400.lines(1-based, inclusive),page(the value's form-feed separated parts, which is howpdftotextwrites pages), orbytes(widened to whole UTF-8 characters). With none set, the whole value is returned.text,start_line/end_line,page,byte_start/byte_end,total_lines/total_pages/total_bytesandtruncated. A cut longer thanmax_bytes(default 64 KiB, at most 1 MiB) is shortened at a line end; read on frombyte_end. A range past the end returns an empty cut, so a caller can page until it gets one._idseek, the same technique the Python client'sgetuses, or atermon the path field. Auth (Read scope), sharding, branches, consistency and buffered writes therefore behave exactly as in search.Not in this PR: the issue's "a small line range reads a small byte range" criterion is only partly met. A cold read fetches just that document's doc-store record with a ranged GET, not a line span inside the record, so a very large single document is still fetched whole. Range reads inside a record would need line/page offset tables or chunked records in the doc store, which is a segment-format change. I'd like to do that as a follow-up once #587 decides how ingested PDFs/DOCX are stored.
How it was tested
doc_readunit tests: line/page/byte cuts and their references, past-the-end and invalid ranges,max_bytestruncation at line ends, and multibyte boundaries.read_returns_line_page_and_byte_ranges_by_id_or_path: covers reads before and after flush, by id and by path, page and byte reads, truncation, and every 400/404.make api-check, the conformance coverage check and the fullkosha-serversuite all pass.Compatibility
/v1wire contract (proto/kosha/v1/kosha.proto, request/response JSON)The change is additive: a new beta route and new messages. The API contract version moves to 1.2.0, with an
api/CHANGELOG.mdentry. No typed SDK method is added yet (that is #584); the route is excluded inclients/conformance/check_coverage.pywith that reason.Checklist
cargo fmt --all -- --checkcargo clippy --all-targets --all-features -- -D warningscargo test --all-features(kosha-server suite)make client-python-test(ifclients/pythonchanged) (n/a)docker build -t kosha:ci .(if the Dockerfile or build configuration changed) (n/a)KOSHA_*variables have a row indocs/configuration.md(none added)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
👀 1 finding for you to triage · Follow Devin's work