Skip to content

OSS-162: send several files in one ingest request (20 files / 10 MiB per request) - #61

Open
adityamishra-hydradb wants to merge 2 commits into
mainfrom
adityamishra/oss-162-batch-file-ingest
Open

adityamishra-hydradb wants to merge 2 commits into
mainfrom
adityamishra/oss-162-batch-file-ingest

Conversation

@adityamishra-hydradb

@adityamishra-hydradb adityamishra-hydradb commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

For humans

hydradb ingest <files> sent one request per file and waited for each answer before it sent the next file. 30 small files took about 16 s. One request with the same 30 files takes about 1.5 s.

This PR sends several files in one request:

  • A request holds at most 20 files and at most 10 MiB of file data. A file larger than 10 MiB goes in a request of its own.
  • Each file still gets its own row, id and result. The CLI matches the API's per-file results to the files by id.
  • When the API refuses a whole request with a 4xx, it stored nothing. The CLI then sends each file of that request again, one at a time, to find the file that the API refused.
  • After a 5xx, the outcome is unknown. The files of that request are reported as failed, and the CLI does not send them again (as before, a write is sent once).
  • 401, 403, 429 and network errors still stop the command. The exit code is still 1 when any file failed or was skipped.

Why 20 files and 10 MiB:

Limit Value Source
One file 50 MiB, else 413 for the whole request ingest docs, API middleware DefaultMultipartFileSizeLimitBytes
Whole multipart body 50 MiB + 1 MiB API middleware maxMultipartRequestBytes
Items in one request 1000 (free and ship plans; larger batch -> 413 "split the batch") API rate-limit policy ingestion_items
File bytes in one request free 10 MiB, ship 50 MiB, scale 100 MiB (larger -> 413) API rate-limit policy knowledge_bytes

10 MiB fits every plan. 20 files keeps a request short, so the progress line moves and a refused request is quick to resend one file at a time.

Fixes OSS-162.

For agents

  • src/hydradb_cli/commands/_impl.py
    • _upload_batches(planned): groups planned files in order; new batch when 20 files or adding the file would pass 10 MiB (size from stat).
    • _upload_batch(...): reads each file of a multi-file batch into memory (bounded by 10 MiB) and probes its content type inside the per-file OSError handler, so a read error skips that file only; a file sent alone is streamed. Sends one context.ingest with documents=[(label, content, ctype), ...] and a document_metadata array in the same order.
    • _match_results(...): each API result is used once, by id first (the CLI sends the id), then the leftover results by position only when as many results as files are left. A file with no result fails ("the API's answer has no result for this file"). An answer with no results list reads as queued, as a one-file upload did before.
    • Errors: fatal statuses -> handle_api_error (unchanged); other 4xx on a batch of more than one file -> resend each file alone (recursive, batch of one); 5xx -> every file of the batch failed, no resend.
    • do_ingest_knowledge_files: loops over batches; action (new/updated) is set after all batches.
  • src/hydradb_cli/hydra/client.py: context.ingest(documents=) takes one file or a list; docstring updated. The SDK's documents is List[File] on 2.1.7 and 2.1.8.
  • Tests: tests/test_cli_commands.py::TestFileIngest (batching, byte cap, results by id, one result per file, fewer results than files, 5xx, 4xx resend, read error and content-type probe error at send time); tests/e2e/sim/test_simulated.py::test_file_ingest_sends_accepted_keys now checks one multipart request with both files through the real SDK. The e2e simulator (tests/e2e/sim/simulator.py) answers a file upload with one result per file, as the API does.
  • Not touched: stdin, kinds and error-message code paths (OSS-152, OSS-168, OSS-155). Separate PR for the SDK range: OSS-169.

Proof

A = base main @ 5a5842d · B = this PR @ 398e2d7. Both installed from wheels built at those commits, in clean venvs (Python 3.12, hydradb-sdk 2.1.7). Run 2026-12-04 22:49-22:53 UTC. Raw output: ~/hydradb/dogfood/sdk/proof/oss-162/ (timing.txt + timing/*.json, edge.txt + edge/*.json, mock_requests.txt, mock_bytecap_*.txt, tests.txt).

1. A/B on the real repro: 30 small files, prod

Own database dogfood-fix-oss-162 in our test org (deleted after). Command: time hydradb -o json ingest n*.txt (30 files, about 45 bytes each), a new collection per run. Runs alternate A, B.

Run A (base @5a5842d) B (PR @398e2d7)
1 15.32 s (22:49:53Z) 2.64 s (22:50:09Z)
2 17.47 s (22:50:11Z) 2.41 s (22:50:29Z)
3 14.40 s (22:50:31Z) 3.01 s (22:50:46Z)
Exit code, every run 0 0
Result, every run 30 success, 0 failed, 0 skipped, all queued 30 success, 0 failed, 0 skipped, all queued
Per-file rows (run 1: file, id, status, action, error) 30 rows 30 rows, identical to A

B is about 6x faster. B's time includes the "already stored?" lookup (one /context/list request), which A also makes.

2. Requests sent (local mock server, repro/mock.py)

Files A (base @5a5842d): POST /context/ingest B (PR @398e2d7): POST /context/ingest Rows, both
30 30 2 (20 + 10) 30
45 45 3 (20 + 20 + 5) 45

3. Regression

Full suite, pytest -q:

A (base @5a5842d) B (PR @398e2d7)
Totals 1958 passed, 133 skipped 1968 passed, 133 skipped

B adds 10 tests and changes 4 (the file ingest fakes now take a list of files; two tests that counted one request per file now count batches). ruff check . and ruff format --check . pass. CI on B is green (test (3.10-3.13), lint, live, dco-check).

Unchanged behavior, prod, same mixed set on A and B (edge.txt): 21 Markdown files, a random binary blob.bin, an empty file, a missing file, Dockerfile (no extension), and docs/e01.md listed twice.

Case A (base @5a5842d) B (PR @398e2d7)
Mixed set: exit code, counts rc=1, 22 success, 1 failed, 3 skipped rc=1, 22 success, 1 failed, 3 skipped
blob.bin (API refuses it in results) failed: "This file format isn't supported..." same
empty / missing / listed twice skipped: file is empty / file not found / listed more than once same
Dockerfile (sent as text/plain) queued, new queued, new
Same 2 files again (upsert) rc=0, both updated rc=0, e01 updated, e02 new (*)
Same 2 files, --no-upsert rc=1, both failed: "A source with this file already exists. Set upsert=true to replace it." same

(*) B ran the second command about 1 s after the first, and A about 15 s after. The new/updated label comes from the /context/list lookup, which had not listed e02 yet. Run again at 22:51:40Z, B reports both updated (edge/edge-B-2b.json). The label logic is unchanged by this PR.

4. Smoke

Section 1 (B column) and section 3 are the smoke runs on prod: plain files, Markdown in a folder, an extensionless file, re-ingest with upsert, and --no-upsert.

5. Stress: the byte cap (local mock, bounded)

Files: three of 4 MiB, one of 12 MiB, two tiny. Request body sizes sent to POST /context/ingest:

A (base @5a5842d) B (PR @398e2d7)
Requests (MiB each) 6: [4.0, 4.0, 4.0, 12.0, 0.0, 0.0] 4: [8.0, 4.0, 12.0, 0.0]

B never puts more than 10 MiB of files in one request; the 12 MiB file goes alone. No load test on prod: the only prod runs are the bounded ones above (30 files per run).

6. Edge cases the change targets (unit tests, B)

Case Test B
45 files -> 20 + 20 + 5, rows in order test_files_are_sent_in_batches_of_at_most_20 pass
Byte cap splits a batch test_a_batch_stays_under_the_byte_cap pass
Results in another order: matched by id test_results_are_matched_by_id_not_position pass
One result used once; an id-less failure goes to the file left over (Mira finding) test_a_result_is_used_once_and_an_unmatched_file_fails pass
Fewer results than files: those files fail, exit 1 (Mira finding) test_fewer_results_than_files_fails_the_files_without_one pass
2xx with no results list: queued, as before test_an_answer_without_results_reads_as_queued pass
4xx on a batch: resent one file at a time, only the refused file fails test_a_4xx_batch_is_resent_one_file_at_a_time, test_an_api_error_on_one_file_does_not_stop_the_batch pass
5xx on a batch: its files fail, no resend, next batch goes on test_a_5xx_fails_its_batch_without_a_resend_and_the_next_batch_goes_on pass
401 stops the command after one request test_an_auth_error_stops_the_batch pass
Read error at send time skips only that file (Devin finding) test_a_read_error_at_send_time_skips_only_that_file pass
Content-type probe error skips only that file (Devin + Mira finding) test_a_content_type_probe_error_skips_only_that_file pass
Real SDK multipart: one request, both files, one metadata item each test_file_ingest_sends_accepted_keys (e2e sim) pass

Not run on prod: a whole-request 4xx (it needs a request the API refuses as a whole, such as a file over the plan's byte cap). That path is covered by the unit tests above.

🤖 Generated with Claude Code

hydradb ingest <files> sent one request per file. It now groups files
into requests of at most 20 files and 10 MiB (the free plan's
per-request byte cap; the API takes 50 MiB per file and 1000 items per
request) and maps the API's per-file results back to each row by id.
A 4xx on a batch resends its files one at a time to find the refused
one; a 5xx fails the batch's files without a resend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: adityamishra-hydradb <[email protected]>
@adityamishra-hydradb

Copy link
Copy Markdown
Contributor Author

@hydra-mira-review review

@adityamishra-hydradb

Copy link
Copy Markdown
Contributor Author

/devin review

@devin-ai-integration

Copy link
Copy Markdown

Starting Devin Review.

Devin Review

@hydra-mira-review

hydra-mira-review Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Mira score 5/5 · 🟢 Good to merge

  • No findings in the code.
  • Confirm outside this PR: Multiple documents with ordered metadata and per-file results, No stored or queued files after a retried whole-request 4xx.
  • Read these high-risk changes closely: Retry rejected groups without replaying uncertain writes.

File ingestion sends multiple files per request to reduce upload time while preserving separate results for each file. Requests hold up to 20 files and 10 MiB, with oversized files sent alone. Eligible client errors trigger individual retries; server errors fail the batch without retries, while authentication, rate limit, and network errors stop ingestion.

Before this ships · 2 to confirm, 1 in place
  • ❔ Multiple documents with ordered metadata and per-file results (contract), needed in HydraDB API, POST /context/ingest. The simulator synthesizes per-file responses from metadata; production support for this complete request and response contract is not confirmed. Confirm the deployed API accepts repeated document parts, pairs metadata by order, and preserves supplied IDs before releasing the CLI.
  • ❔ No stored or queued files after a retried whole-request 4xx (contract), needed in HydraDB API ingestion handler and middleware. The CLI retries nonfatal 4xx responses, but the API's no-write guarantee for all those statuses is not confirmed. Establish that guarantee before release, or restrict individual retries to responses that explicitly guarantee no writes.
  • ✅ SDK support for multiple multipart files (contract), needed in hydradb-sdk. pyproject.toml pins hydradb-sdk==2.1.7, which the PR description identifies as supporting file lists.
How this PR works · 5 changes, 1 high risk, 3 medium risk, 1 low risk

Uploading many small files currently waits for one request per file. This PR groups files into requests containing at most 20 files and 10 MiB, while sending larger files alone. It preserves per-file results and separates safe retries after request rejection from unknown outcomes after server errors.

1. Accept multiple files in one ingest call · 🟠 medium risk · src/hydradb_cli/hydra/client.py

Callers can describe several uploaded files in one call. _Context.ingest widens documents from one file to one file or a list, with metadata ordered alongside the files.

Risk: If the SDK serializes file lists incorrectly, file uploads fail or reach the API with missing documents.

Check:

  • Check that the pinned SDK sends two document parts with metadata in the same order.

In client.py:329-361.

2. Send bounded batches while isolating unreadable files · 🟠 medium risk · src/hydradb_cli/commands/_impl.py

Files now travel in bounded groups instead of separate requests. _upload_batches preserves input order, caps groups at 20 files and 10 MiB, and sends oversized files alone.

Risk: Incorrect grouping can reject otherwise valid uploads or consume excessive memory during large ingest commands.

Check:

  • Check that 45 small files produce request sizes of 20, 20, and 5.
  • Check exact byte-limit boundaries and a file larger than 10 MiB between two small files.

In _impl.py:513-541, _impl.py:550-593, _impl.py:699-715.

3. Keep each response attached to its uploaded file · 🟠 medium risk · src/hydradb_cli/commands/_impl.py

Each file still receives its own output row when responses arrive out of order. Previously, the command used only the first result from each single-file request.

Risk: Incorrect matching attributes an error or success to the wrong file and can give users the wrong source ID.

Check:

  • Check reversed results containing one ID-less failure, confirming that no result is reused.
  • Check the distinction between an incomplete results list and a successful response without a results list.

In _impl.py:542-549, _impl.py:615-652, _impl.py:716-770.

4. Retry rejected groups without replaying uncertain writes · 🔴 high risk · src/hydradb_cli/commands/_impl.py

A rejected group now gets individual attempts so acceptable files can proceed. Server errors fail the affected group without resending it, and later groups continue.

Risk: If a rejected request already stored files, individual retries can repeat writes or misreport files that were accepted.

Check:

  • Confirm that every retried 4xx status occurs before any file is stored or queued.
  • Check that a failed first group receives no retry after 502, while the next group still uploads.

How it works:

  1. _upload_batch stops the command for status 0, 401, 403, or 429.
  2. _upload_batch retries other 4xx responses one file at a time when more than one file was sent.
  3. _upload_batch marks every sent file failed after other API errors, including 5xx, without retrying.
  4. do_ingest_knowledge_files stops on raw network errors; translated transport errors use fatal status 0.

What it touches:

  • src/hydradb_cli/commands/_impl.py::do_ingest_knowledge_files.
  • src/hydradb_cli/commands/_impl.py::_upload_batch.
  • File ingest responses from POST /context/ingest.
  • Individual retries assume a rejected 4xx request stored nothing. A 5xx may have stored files even though their output rows report failure.

In _impl.py:593-613, _impl.py:703-714.

5. Supporting changes · 🟢 low risk · tests/test_cli_commands.py +3

Tests now cover grouped requests, result matching, retries, and read failures. The simulator repeats its recorded result per metadata item, and the changelog documents batching.

Risk: An inaccurate simulated response can hide differences between the CLI's expectations and the production API.

Check:

  • Read the multipart assertion alongside the simulator's synthesized IDs; the simulator does not establish production ID preservation.

In test_cli_commands.py:240-833, simulator.py:160-176, simulator.py:247-248 and 2 more.

Questions for the author:

  • Does the deployed ingest API guarantee that every nonfatal 4xx leaves all files unstored and unqueued, including failures after request validation?
Requirement check · 6 met · from the PR description
  • ✅ Send at most 20 files and 10 MiB per request, with larger files sent alone (met). _upload_batches uses file sizes from stat to split ordered groups at _BATCH_MAX_FILES and _BATCH_MAX_BYTES.
  • ✅ Preserve a separate row, ID, and result for each file, matching results by ID (met). _upload_metadata sends stable IDs; _match_results matches IDs once and falls back to position when remaining counts agree.
  • ✅ After a nonfatal whole-request 4xx, resend the group's files individually (met). _upload_batch recursively sends singleton requests after nonfatal 4xx responses when more than one file was sent.
  • ✅ Report a 5xx group's files as failed without resending them (met). _upload_batch marks sent rows failed; _Resource._invoke_write also disables SDK retries with max_retries=0.
  • ✅ Keep 401, 403, 429, and network errors fatal (met). _FATAL_UPLOAD_STATUSES includes those HTTP statuses and transport status 0; raw request errors reach handle_network_error.
  • ✅ Exit with code 1 when any file fails or is skipped (met). do_ingest_knowledge_files counts failed and skipped rows and raises typer.Exit(code=1) when either count is nonzero.
Diagram of the changed files
graph LR
  unit["tests/test_cli_commands.py"] --> ingest["src/hydradb_cli/commands/_impl.py"]
  e2e["tests/e2e/sim/test_simulated.py"] --> ingest
  ingest --> client["src/hydradb_cli/hydra/client.py"]
  client --> simulator["tests/e2e/sim/simulator.py"]
Loading
⚠️ Other open PRs change the same code · 7

Merge them in order, or check that they agree.

  • #30 (merge-conflict risk). Unified items ingestion changes overlap the ingest command and client request construction modified by batching. Shared: CHANGELOG.md, src/hydradb_cli/commands/_impl.py, src/hydradb_cli/hydra/client.py, tests/test_cli_commands.py
  • #46 (merge-conflict risk). Unified routing and ingestion integration overlap the command and client ingestion paths being changed for batching. Shared: CHANGELOG.md, src/hydradb_cli/commands/_impl.py, src/hydradb_cli/hydra/client.py, tests/e2e/sim/simulator.py, tests/e2e/sim/test_simulated.py, tests/test_cli_commands.py
  • #60 (merge-conflict risk). SDK compatibility changes to context.ingest calls overlap the client ingestion changes needed for multi-file requests. Shared: CHANGELOG.md, src/hydradb_cli/hydra/client.py
  • #38 (merge-conflict risk). Layout-dependent ingestion changes overlap the command and client paths that construct multi-file requests. Shared: CHANGELOG.md, src/hydradb_cli/commands/_impl.py, src/hydradb_cli/hydra/client.py
  • #39 (merge-conflict risk). Strict ingest payload construction and typed SDK calls overlap the client ingestion path being changed for batching. Shared: CHANGELOG.md, src/hydradb_cli/commands/_impl.py, src/hydradb_cli/hydra/client.py
  • #40 (merge-conflict risk). File validation and ingestion response parsing overlap the file-processing and response-handling changes required for batching. Shared: src/hydradb_cli/commands/_impl.py, src/hydradb_cli/hydra/client.py
  • #47 (merge-conflict risk). Removing context_category from ingest overlaps changes to ingest request construction for batching. Shared: CHANGELOG.md, src/hydradb_cli/commands/_impl.py

Comment @hydra-mira-review help for commands.

devin-ai-integration[bot]

This comment was marked as resolved.

hydra-mira-review[bot]

This comment was marked as resolved.

- The content-type probe runs inside the per-file read handler, so a
  read error skips that file only.
- Files of a multi-file batch are read before the request (bounded by
  the 10 MiB batch cap); a file sent alone is still streamed.
- Each API result is used once: by id first, then the leftover results
  by position only when as many are left as files. A file with no
  result fails. An answer with no results list reads as queued, as before.
- The e2e simulator answers a file upload with one result per file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: adityamishra-hydradb <[email protected]>
@adityamishra-hydradb

Copy link
Copy Markdown
Contributor Author

@hydra-mira-review review

@adityamishra-hydradb

Copy link
Copy Markdown
Contributor Author

/devin review

@devin-ai-integration

Copy link
Copy Markdown

Starting Devin Review.

Devin Review

This branch has not been deployed

No deployments
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.

1 participant