Skip to content

feat(api): add read for line, page and byte ranges inside a document (#586) - #600

Merged
decover-admin merged 2 commits into
mainfrom
devin/1795743138-doc-read
Nov 27, 2026
Merged

decover-admin merged 2 commits into
mainfrom
devin/1795743138-doc-read

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown
Contributor

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}}
  • The document is addressed by id or by a keyword path (path_field, default path). A path that names two documents returns 400.
  • Ranges: lines (1-based, inclusive), page (the value's form-feed separated parts, which is how pdftotext writes pages), or bytes (widened to whole UTF-8 characters). With none set, the whole value is returned.
  • The response carries text, start_line/end_line, page, byte_start/byte_end, total_lines/total_pages/total_bytes and truncated. A cut longer than max_bytes (default 64 KiB, at most 1 MiB) is shortened at a line end; read on from byte_end. A range past the end returns an empty cut, so a caller can page until it gets one.
  • Lookup goes through the search path: an _id seek, the same technique the Python client's get uses, or a term on 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_read unit tests: line/page/byte cuts and their references, past-the-end and invalid ranges, max_bytes truncation 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.
  • Route and authz pin tests were updated. make api-check, the conformance coverage check and the full kosha-server suite all pass.

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

The change is additive: a new beta route and new messages. The API contract version moves to 1.2.0, with an api/CHANGELOG.md entry. No typed SDK method is added yet (that is #584); the route is excluded in clients/conformance/check_coverage.py with that reason.

Checklist

  • cargo fmt --all -- --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-features (kosha-server suite)
  • make client-python-test (if clients/python changed) (n/a)
  • docker build -t kosha:ci . (if the Dockerfile or build configuration changed) (n/a)
  • New KOSHA_* variables have a row in docs/configuration.md (none added)
  • 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


👀 1 finding for you to triage · 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

…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---
@github-actions
github-actions Bot force-pushed the devin/1795743138-doc-read branch from 4fdf4a4 to 403ea4d Compare November 27, 2026 01:42
@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 7de69d8ea · same runner, runs interleaved (2× / 2×), best p50 of each side

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---
@github-actions

Copy link
Copy Markdown

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

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

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

@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 found 1 finding. It is left for you to triage below.

View findings in Devin Review · Follow Devin's work

Devin Review

Comment on lines +238 to +244
(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],

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.

🔴 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.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@decover-admin
decover-admin merged commit 2d6a87a into main Nov 27, 2026
26 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.

[agent] read(): line, page and byte ranges inside a document without fetching it whole

2 participants