Skip to content

Uploaded files are ingested: the extractor service extracts them and a job publishes the next revision - #229

Merged
zlareb1 merged 21 commits into
mainfrom
feat/knowledge-ingest-jobs
Nov 4, 2026
Merged

zlareb1 merged 21 commits into
mainfrom
feat/knowledge-ingest-jobs

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A file can be uploaded (POST /v1/knowledge.upload) and ingested (knowledge.ingest, knowledge.ingest_status, knowledge.ingest_cancel): a background job sends the original to the document extractor service, checks what it returns, and publishes the next revision of the source with chunks that cite their lines, keys and sections. The server parses no file itself.

Why

knowledge.add takes text the caller already extracted. The previous steps of the related issue added the extractor service and its wire contract (the server sends bytes, gets an artifact back and checks it as untrusted input) and its image beside the server; nothing called it. This adds the path that does: a stored upload, a job that extracts, chunks, embeds and publishes it, and revisions of a source so a re-ingest replaces the earlier one only once it is complete.

Scope and risk

  • Upload (server/knowledge/app/upload_routes.py): POST /v1/knowledge.upload?org=&namespace=&filename= with the file as the raw body (writer role; sha256= and content_type= optional), admitted as an op is (after authorization it draws knowledge.add's rate-limit buckets and holds one of the org's in-flight slots). The authorship helpers in writes.py become public (authorship, authorship_of), since the upload route and the ingest ops use them. The in-flight counter and the fail-open counter move from server/transport/gates.py to server/core/tenant_slots.py and server/core/fail_open.py, so a route below the transport shares them; a limiter that fails admits the upload and is counted and logged as an op's is. Its 429s are raised as RateLimitedError, so they carry Retry-After and retry_after_s as an op's do. An upload is drawn as interactive traffic: it is not classified as an op is. The body is copied to bytes once (the ClickHouse client writes only bytes into a String column), so an upload holds up to twice its size while it is stored, and the org's in-flight slots bound how many do. The request-body cap (LOOM_MAX_REQUEST_BYTES) passes this one path through, since the route streams the body against its own limit and refuses one that ends before its Content-Length. The file is stored as an original (in ClickHouse or the document bucket, as LOOM_DOCUMENT_OBJECT_STORE says) and recorded in loom.knowledge_uploads with a scoped upload_id and expires_at (LOOM_KNOWLEDGE_UPLOAD_TTL_SECONDS, a day). Over LOOM_KNOWLEDGE_UPLOAD_MAX_BYTES (64 MiB) is 413, a wrong sha256 422 CHECKSUM_MISMATCH, a legacy Office name 415 UNSUPPORTED_FILE, all before anything is stored. The format comes from the file name and media type (server.core.document_formats.format_of); the server does not read the bytes to type them, so content that is another format is refused by the extractor at ingest (format_mismatch).
  • Jobs (server/knowledge/app/ingest.py, ingest_run.py, worker knowledge_ingest, loom.knowledge_ingestions): knowledge.ingest names an upload the caller made in the same namespace and the source_uri to publish under (title, description, tags, metadata as for knowledge.add; options format, encoding, sql_dialect; expected_revision; allow_partial) and answers a pending job. A pass of the worker runs one job (so it has the whole pass deadline, LOOM_KNOWLEDGE_INGEST_MAX_PASS_SECONDS, 15 minutes), one source's jobs one at a time in the order asked (only the oldest open job of a source is offered, so a later ingest of it waits while an earlier one runs, and a job a crashed pass left processing runs again before any later one of its source), and namespaces in turn (each offers one due job, a stale processing one first, and the namespace served longest ago goes first), so one namespace's backlog does not hold up every other's. The job is taken under a deployment-wide lease on its id, then:
    1. parse: reads the original (blobs.locate, then blobs.read) and sends the bytes to the extractor its format is routed to through server/knowledge/app/extractors.py, which checks the artifact; stores the artifact JSON as an original of its own;
    2. embed: cuts structural chunks, gives each the context knowledge.add gives its chunks (chunk_context: title, description, section breadcrumb), and reuses the stored vector of every embedding input this namespace already embedded under the same model;
    3. publish: checks a cancel, records the new chunk version on the job (doc_version) and inserts the chunks; then, under the source's claim, checks a cancel and expected_revision again, writes the document row (revision, format, artifact, parser, coverage) and retires the earlier version's chunks. The claim covers those few statements only, so its time limits hold for a source at LOOM_KNOWLEDGE_INGEST_MAX_CHUNKS.
      Every slow step runs before anything is published, so a failed or cancelled job leaves the earlier revision serving. The job row records the chunk version a publish inserts before its document row (doc_version); when a run stops between the two, those chunks are retired before the job goes back to pending or ends, or by its next run after a crash, so a pending or ended job leaves none live, except a job stopped at an uncertain document insert, whose chunks stay live until its next run settles it. An erasure that lands after the chunk insert stops the document row (the job's row is read again first); chunk rows inserted after the erasure's delete are left to the residue census, which fails the saga closed. The document row names the job that wrote it (ingestion_id, which a metadata edit and a delete keep), and a rerun looks for any version row of the source naming it (documents.revision_by_ingestion, read without FINAL, so a text write since does not hide it; a run that stopped mid-publish (still processing at the publish stage, saved with its chunk version), when a later write of the source has landed since and the job's row is not found because a merge may have folded it away, fails revision_conflict instead of perhaps publishing its older file over that write; a job a retry put back to pending is not judged so). A document insert that fails transiently (a client-side timeout, or the source claim's deadline cutting it off) may still land, so the run stops there as a crash would: the job stays processing at its publish stage with its chunks live, a cancel of it answers processing, and the run leaves the job's lease to expire rather than release it (a new Lease.let_expire: stop renewing, keep the key), and a pass deadline or shutdown that cancels the run with the insert in flight does the same. The run's last lease.held check is inside the source claim's block (the lease renewed within 60 s before it), and the send comes within that block's 24 s bound of the check, so no worker can take the job until at least 36 s after the send, judged on the server's clock; the insert's own bound is 29 s (a carried send may start up to 15 s after it was sent, run 9 s under the source claim, and commit within 5 s more), so by then it has landed or been stopped; the next run finishes it published when its row landed, whatever was written after it (a delete stays a delete, a text write stands), and otherwise retires the chunks before it publishes or fails, so a rerun of a job that published (it stopped before its last save) finishes without publishing again, retiring the chunks it published, or bringing back a document deleted since; it retires the earlier revision's chunks in case the first run stopped before it did, and it is checked before a cancel, so a published revision is reported as published. A failure of any kind after this run's own document insert returned (the retire of the earlier chunks, the claim's release), on any attempt, ends the job published rather than failed or pending; a failure before it retires the run's own chunks. The run knows which from its own steps; only a run that crashed is judged from the stored rows.
  • Cancel: knowledge.ingest_cancel writes a row of its own in loom.knowledge_ingest_cancels, never a version of the job's row, which only the worker holding the job's lease writes (a version built from the caller's read would replace what the worker saved after it). The op reads the job again after writing the cancel: still pending, no worker has started it, so it answers cancelled, and the status op reports a pending job with a cancel as cancelled; the worker that takes it ends it without running it. A running job reads the cancel before its chunk insert and under the claim before the document row. A run cut off at the pass deadline, or by a stopped process, writes no failure, so a job whose attempts are spent fails retries_exhausted before it extracts again.
  • Failure classes: a source the extractor refuses (ExtractionFailed) fails the job at once with the extractor's code (format_mismatch, encoding, corrupt, ...). A format no configured extractor takes (none routed, the service does not declare it, or it answers format_unavailable) is a new FormatUnavailable, a subclass of ExtractorUnavailable, and fails the job with format_unavailable, since retrying does not change a deployment's extractors. An artifact that breaks the contract fails the job at once (invalid_artifact), as does a 4xx with no contract code (refused). An extractor that cannot be reached, times out, or answers a status that is neither a result nor a refusal (ExtractorUnavailable), and store and provider outages (a store down, past its deadline or out of slots: BACKEND_NOT_NOW), put the job back with a doubling delay (at most an hour) until LOOM_KNOWLEDGE_INGEST_MAX_ATTEMPTS (retries_exhausted). Other codes: incomplete_extraction (unless allow_partial), no_text, too_large (more than LOOM_KNOWLEDGE_INGEST_MAX_CHUNKS), revision_conflict, original_gone, cancelled, internal.
  • Revisions: every write of a source (knowledge.add, upsert, bulk_add, ingest) numbers the next revision, one past the latest, deleted or not. Ingests of a source publish under its claim, one at a time, so expected_revision guards an ingest against another (0 expects no live document, so it also refuses a live document still at revision 0: one written before this change or by an older build); text writes take no claim (as before), so a text write racing another write of the same source can reuse a revision number and the later version row wins; an ingest whose document row a later write of the source (a text write, a metadata edit or a delete) would outrank fails revision_conflict instead of reporting a revision no reader sees. Taking the claim on text writes too means moving their embedding and original upload out of the claimed section, which is left for a follow-up. Document rows gain revision, format, parser, coverage, artifact_sha256 and the ingestion_id that published them; chunk rows gain keys (the exact names a block defines or reads) and embed_input_sha256 (what the reuse matches on). knowledge.get returns the new document fields. knowledge.update_metadata with reindex=true refuses an ingested document (422): its chunks come from the artifact, and re-cutting the rendered text would make it a text document; ingesting it again re-indexes it.
  • Erasure: the three new tables are in the namespace and user erasure steps and the residue census; an upload row holds the file name and its uploader, a job row the document's title, description, tags, metadata, file name and error message, and a cancel row its job and who asked, so all three are erased with the documents; all also expire (a cancel 30 days after it was asked) (an upload's row a week after expires_at, a job's 30 days after its last change). The blob sweep keeps an original a live upload or job still holds (HELD_SQL).
  • Settings: knowledge_upload_max_bytes (live), knowledge_upload_ttl_seconds, knowledge_ingest_enabled, knowledge_ingest_interval_seconds, knowledge_ingest_idle_interval_seconds, knowledge_ingest_max_pass_seconds, knowledge_ingest_max_attempts, knowledge_ingest_max_chunks (live).
  • Migration 082_knowledge_ingestion.sql: three new tables (uploads, jobs, cancels), the new document and chunk columns, all additive with defaults, and bloom-filter skip indexes on the chunks' keys and embed_input_sha256 (the vector-reuse lookup).

Search, outline and synthesis read the published chunks as they read any document's; the revision-qualified citations and the structural search filters are the next step.

Compatibility, rollout and rollback

  • Wire: three new ops (knowledge.ingest, knowledge.ingest_status, knowledge.ingest_cancel), one new HTTP route (POST /v1/knowledge.upload), new optional output fields on knowledge.get (revision, format, parser, coverage). No field is removed or changed.
  • Operator steps: migration 082 runs at startup. Ingestion needs an extractor: the compose stack and the chart run one (The document extractor ships as its own image, beside the server in compose and Helm #194); elsewhere set LOOM_DOCUMENT_EXTRACTOR_URL and LOOM_DOCUMENT_EXTRACTOR_TOKEN, or every job fails format_unavailable.
  • Rolling upgrade: build N-1 ignores the new tables and columns (all defaulted); a job queued on N waits until an N worker takes it. loom.documents is now carried (rmt_carry, rule 2), which protects later upgrades but not this one: an N-1 knowledge.update_metadata or delete of a document N ingested writes a version row without the new columns, so that document's revision reads 0 and its format, parser, coverage and artifact_sha256 empty until it is written again, and the sweep may collect its artifact (a re-ingest stores it again). Nothing N-1 serves ingests, so this touches only documents ingested during the rollout window and edited by an N-1 replica before it is replaced. While an N-1 process holds the originals sweep's lease, the sweep knows neither the upload and job holds nor artifact_sha256: an upload that waits longer than the sweep's minimum age (an hour) for an N worker can be collected, and its job fails original_gone (upload it again).
  • Rollback: the tables and columns stay (additive); uploads and jobs expire by their TTL.

Tests

  • Unit: tests/unit/test_knowledge_ingest.py (68), test_leases.py (a lease left to expire keeps its key): the reference extractor served in process (httpx.ASGITransport), the stores faked; ingest naming its upload, options and authorship; a job publishing chunks, then the document, then retiring older versions; the artifact stored and the text rendered from it; a second ingest the next revision reusing identical vectors; each failure code; an unreachable extractor leaving the job pending with retrying; a job erased mid-run, a lost lease and a crash after publishing each leaving nothing half-written; a publish that fails after its chunks retiring them, when the job fails and when it goes back to pending (a cancel of that pending job leaves none live); a rerun after its document row retiring the earlier revision's chunks; expected_revision=0 refusing a live document at revision 0; a cancel writing no version of the job; a job that published ending ready after a later cancel; a rerun of a published job after a metadata edit or a delete keeping its chunks and publishing nothing more; a failure after the document row ending the job published on the last attempt and before it; an ingest a later write overtook failing revision_conflict and retiring its chunks; a rerun of a published job after a text write publishing nothing more, with its own row still there (ready) or merged away (revision_conflict); a document deleted after a late-landing insert staying deleted, with the job's own row merged away or not; a document insert cut off by the source claim's deadline stopping the run like a timeout; the lease left to expire after an uncertain insert or a cancel mid-insert, and the earliest takeover outlasting the insert's bound; a lease lost once the claim is taken stopping the publish before its document row; a store out of slots putting the job back; a cancel after an uncertain insert answering processing and the job ending published once its row landed; an erasure between the chunk insert and the document row writing no document row; the test fakes retiring every chunk version a publish, a text write or a delete retires; a retry publishing over a metadata edit that landed while it waited; a document insert that timed out keeping its chunks, then ending the job published (its own chunks live) when the row landed or failed and retired when it did not; the retry delay stopping at an hour; a job whose runs never finished failing retries_exhausted before it extracts again; a pass running one job and passing over one another process holds; the worker's settings; an upload named by its name without reading the bytes, stored whole above the request-body cap, refused 429 at the org's in-flight cap or rate limit, admitted and counted when the limiter fails; reindex of an ingested document refused. test_body_limit.py: a path that bounds its own body passes through. test_knowledge_extractors.py: FormatUnavailable for an undeclared format. Erasure order, census, schema baseline, RMT write paths, read-scope admission, SDK surface and settings snapshots updated.
  • Full pytest tests/unit on the rebased tree (after the sources and erasure-actor changes took migrations 080 and 081, so this one is now 082): 26,277 passed, 20 skipped, 1 xfailed; the affected tests again at this head (knowledge, ingest, erasure, census, schema, generators, the document-source sync): 4,992 passed, and after the last change 2,068 focused (ingest, erasure, leases, claims, admission) (one host-load timing failure, test_workers_child.py::test_the_child_dies_with_its_parent, fails the same on main here and is untouched by this change), and tests/analyzer 12 passed. An earlier run found 4 failures from this branch, since fixed: the job stage names the LLM-stage scan read as stages, a SELECT alias shadowing a filtered column, the SDK surface snapshot, and settings no test named.
  • Integration tests/integration/test_knowledge_ingest_queue.py on a real ClickHouse: two namespaces' due jobs, the one with a backlog offering only its oldest and going after the other; one source's later job not offered while the earlier one runs, and the earlier one offered first once it is stale.
  • Integration against a server built from this branch, a ClickHouse migrated from empty, and the reference extractor run as a service (python -m loom_extractor): test_knowledge_ingest_live.py (now with a cancelled pending job read as cancelled over HTTP and ended by the worker) and test_erasure_user_originals_shared.py, test_knowledge_ingest_queue.py and test_knowledge_sources.py rerun after the rebase (9 passed, 6 skipped); pytest tests/integration -k "knowledge or document or erasure or ingest or blob or upload" 135 passed, 44 skipped (settings this server does not set). The store's upload, job and cancel reads checked against the same ClickHouse.
  • make format, make lint (generators current), make typecheck: pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/knowledge-ingest-jobs branch 14 times, most recently from 1b602ed to 89c13a9 Compare November 4, 2026 02:40
zlareb1 and others added 16 commits November 4, 2026 03:07
…r service, publish it as a document revision

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… row ends published, fence a later text write

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…source; the retry delay stops at an hour

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…hing; count the job's own chunks

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…its does not fail it

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ry keeps its chunk version

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…blished only with live chunks, else publish again

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… or text write after its publish stands

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…uld; re-read the job before the document row

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… run like a timeout

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…er version first, own row included

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… slots is transient

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…it makes unreachable; typed 429s on upload

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 5 commits November 4, 2026 03:07
…s past the insert's bound

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…t (Lease.let_expire)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…test that it sits inside the claim

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
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