Retrieval & Ingestion Pipeline: how the pieces fit together
A document becomes retrievable in five steps: an intake provider delivers it, the ingestion stage chunks and embeds it, the store keeps it, the quality gate retrieves from it, and answer synthesis reads what the gate returns. Each piece has its own detailed document; this one is the overview that ties them together and the order a document actually flows through them.
| Stage | Module | Detailed doc |
|---|---|---|
| Deliver a document | engine/document_intake.py |
the module's own docstring |
| Chunk a document by its type | engine/document_types.py, engine/chunking_strategies.py |
chunking-registry.md |
| Embed and store its chunks | engine/document_ingestion.py, engine/embedding.py, engine/embedding_store.py |
retrieval-embedding-infra.md |
| Quality-gated retrieval | engine/retrieval_gate.py |
retrieval-quality-gate.md |
| Synthesize an answer | engine/playbook_answer.py |
playbook-answer-synthesis.md |
The runtime path
A Node with a document_intake section ingests on its own. Nothing is run by hand and nothing is
seeded: each configured provider is polled on its own schedule, and every document it delivers goes
through the same path.
1. A provider delivers an item
The engine ships two provider kinds:
filereads a directory of text or markdown files as the documents of one type (see Document file sources). The intake seam refuses a document of that type from any other provider.restworks differently. A sender posts documents to the management API's document routes, and the API leaves them in a queue table of its own in the Node's database. A worker in the Node takes them one at a time through the same path as any other provider's items.- It is not polled for items, so its entry has two intervals: how often the worker looks while the queue is empty, and how often after it found something.
- Its calls are answered when queued, not when ingested.
- The routes, the caps and how a sender follows a document are under Documents in
management-api.md.
An item is the already-extracted plain text of one document revision plus what the provider knows about it:
- a stable
source_document_id; - when this revision was made;
- its
document_type, a name from the site'sdocument_typescatalogue (an item without one is agenericdocument); - optionally, an applicability (which product, which Mfg Order, which unit or date range, or none of those for a generic instruction), a title, a document number and a revision label.
The intake seam refuses an item that is missing a field or malformed, naming the field, and handles each revision once.
Document ids are site-wide. The store keys a document by its source_document_id alone, so two providers that deliver the same id are delivering the same document, and the later one replaces the earlier. Give documents from different source systems ids that cannot collide (a prefix is enough).
For a type that keeps each revision, @ joins the id and the revision label. An item whose id or label contains @ is therefore refused. A plain document of a type that replaces its revisions may contain one, but should not use an id of the form {id}@{label} that a kept document could also produce.
2. The sink chunks the document by its type's strategy
The document type's chunking strategy, set in the catalogue, cuts the text. The type's context
format is then rendered for each chunk (for example SOP-110 Seal Housing Assembly — Cleaning:).
Nothing here is chosen by a model: the same text under the same catalogue entry always yields the same
chunks.
3. The sink embeds the context and the chunk together
What is embedded is the rendered context, a line break and the chunk's text, so a chunk that says
"Ra 0.8" is found by a question that names the part it belongs to. The chunks go to the embedding
client in batches. A chunk whose context and text are longer than the embedding profile's
max_input_chars is refused before anything is embedded, and a document that yields no chunks is
refused too; in both cases what the store already holds for the document is left as it was.
At most two documents are embedded and written at once, however many providers are polling, so a bulk load does not swamp the embedding host.
4. The sink replaces the document in the store
EmbeddingStore.replace_document writes the new chunks and removes any the document no longer has,
in one transaction. Chunk ids are the stored document id, a colon and the chunk's sequence number, so
ingesting the same revision again replaces it and never duplicates it, and a revision that is shorter
than the last leaves no stale tail. If embedding fails, nothing has been written: the earlier chunks
stay and the item is offered again on the next poll.
How a type keeps its revisions is the revisions policy in its catalogue entry:
replacestores a document under its own id, and a new revision replaces the old.keepstores each revision under{source_document_id}@{revision_label}, with the plain id in the chunks'metadataasbase_document_id, so every revision stays retrievable. An item with no revision label is refused. Withprevious: deactivate, a revision's arrival deactivates the earlier revisions in the same transaction. The seam handles each kept revision once on its own, whatever its revision time: it knows a kept revision by its stored id, so two labels sent with the same time are both ingested, and a revision older than another label's is not skipped. Only a revision older than one already handled under the same label is skipped. A newer revision with no effective range is stored with a warning and an assumed start, or refused if the type setsrequire_effective_range(chunking-registry.md).
Each ingested document writes one Activity Log entry with its ids and counts (document id, document type, provider, chunk count, configuration version) and never its text.
With inference_enabled false nothing is embedded and every item is held: it is not recorded as
handled, and is ingested on a later poll once the Node runs with inference enabled. Ingestion does
not depend on the license; the documents are the site's own.
A type whose catalogue entry sets draft_claims is handed to a second consumer after the sink has
taken it: Instruction claim drafting finds the numbers with units in
the text and submits each for a person to confirm. It reads the text and writes to the sign-off store,
never to the embeddings table, and an item the sink refused is not drafted.
5. Query time: retrieve, with quality and applicability policy
from engine.embedding_store import ApplicabilityContext
from engine.retrieval_gate import RetrievalQualityGate
gate = RetrievalQualityGate(embedding_client, embedding_store)
result = gate.retrieve(
question, context=ApplicabilityContext(product_id=operators_current_product, unit_number=4812)
)
result.outcome is STRONG (proceed), BROADENED (a retry with a wider net found something), or
INCOMPLETE (nothing — result.chunks is []). context is a hard pre-filter, checked before any
similarity ranking runs and held fixed across the broadened retry — a wrong-product instruction
never becomes correct just because the similarity bar dropped. Only active chunks are searched.
A chunk's embedding context is stored with it but never comes back: RetrievedChunk.metadata does
not carry it, so neither a point-of-use answer nor a precedent prompt shows it.
6. Query time: synthesize one direct answer
from engine.playbook_answer import PlaybookAnswerer
answerer = PlaybookAnswerer(gate, inference_client, prompt_asset, budget, activity_log, ...)
answer = answerer.answer(question, context=applicability_context) # context is optional
answer.answer # synthesized text, never the raw excerpts
answer.grounded # False when the gate reports INCOMPLETE, or when inference is disabled
Answer synthesis takes the same RetrievalQualityGate and forwards its own context argument straight through to gate.retrieve(). Every retrieval-quality and applicability decision belongs to the gate, not to PlaybookAnswerer.
answer.grounded collapses the gate's three outcomes to the two that matter to a synthesized-answer caller: INCOMPLETE gives False, and STRONG or BROADENED gives True. grounded is also False when the answerer is built with inference_enabled=False, which returns INFERENCE_DISABLED_MESSAGE without retrieving at all. A caller that needs the full three-way outcome should call RetrievalQualityGate.retrieve directly.
There is no per-chunk retraction
The store's unit is the document. A document is replaced whole, deactivated or activated; no call removes one chunk of a document, and nothing removes a document either.
- Deactivate sets the
activeflag false on all of a document's chunks in one transaction. Retrieval, including precedent retrieval, stops returning them, and nothing is deleted. Activate turns them back on. Each change that changes something writes one Activity Log entry with the document id and the reason, never the text.similarity_searchandCasePrecedentRetriever.retrievetakeinclude_inactive=Truefor an analysis of everything on file. - Re-ingesting a document keeps its
activestate; a new document is active. - To take a document out of answers for good, deactivate it. A chunk that should not exist at all is a mistake in the source, and the fix is a corrected revision.
What a fully-ingested chunk row contains
| Field | Meaning |
|---|---|
chunk_id |
{stored document id}:{sequence}, stable across re-ingestion |
text, embedding |
The chunk alone, and the vector of its context plus the chunk |
source_document_id, source_revised_at |
Which document, which revision (under keep, the id carries @{revision_label}) |
document_type |
The catalogue type the document was cut by |
applicability.* |
Product, Mfg Order, unit and date scope — None on every field means shared |
active |
False once the document is deactivated |
metadata.embedded_context |
The rendered context that was put before the text for embedding (never returned by a search) |
metadata.config_version, metadata.config_fingerprint |
The version of the type's configuration the chunk was made under, and its fingerprint |
metadata.provider |
The configured name of the provider that delivered it |
metadata.locator, metadata.heading_path |
What the chunking strategy knew of where the chunk sits |
metadata.base_document_id, metadata.revision_label |
Only for a type that keeps each revision |
Every chunk of a document carries the same version, and the version is recorded in the Node's
configuration history (document_config_history), an append-only table that is never deleted from. A
configuration change leaves earlier chunks in place and retrievable; only a deliberate re-ingest
brings them up to date. See chunking-registry.md for which changes are worth that, and
retrieval-embedding-infra.md for the report of what is on which version.
Findings about a document
The ingestion result for a document carries its chunk count and the smallest, largest and mean chunk size. It also carries findings worth a look, which are written to the Activity Log entry and logged as a warning with the document id:
over_type_maximum: a chunk is longer than the type's configured maximum.boundary_rules_unmatched: none of aboundary_rulestype's patterns matched, so the document is one chunk. This is how a change in a site's document template shows up.
Tests, by stage
| Stage | Tests |
|---|---|
| Deliver | tests/test_document_intake.py, tests/test_file_intake.py |
| Chunk | tests/test_chunking.py, tests/test_chunking_strategies.py, tests/test_document_types.py |
| Embed and store | tests/test_document_ingestion.py, tests/test_embedding.py, tests/test_embedding_config.py, tests/test_embedding_store.py, tests/test_embedding_contract.py, tests/test_retrieval_live.py |
| Retrieve | tests/test_retrieval_gate.py |
| Synthesize | tests/test_playbook_answer.py, tests/test_playbook_answer_live.py |
The *_live.py and *_contract.py files are the only ones that need a reachable Ollama endpoint. Everything else runs against real Postgres with synthetic vectors, with no network dependency.
Release: v0.11.0