# A document pipeline you can prove

> Most document pipelines have one stage too many. For the stages left, split workers, stamp attempts, and derive a DAG trace from events already recorded.

- Canonical: https://www.alpacamango.com/docs/a-document-pipeline-you-can-prove
- Author: Kevin
- Published: 2026-11-24 · Updated: 2026-11-23
- Topics: Fintech, building-taxadoo, Architecture

*Building Taxadoo, part 7 of 7. Lessons from building the statement engine behind Taxadoo, a UK bookkeeping product — the decisions and the failures, not the internals. *[*The whole series*](/docs/tag/building-taxadoo)*.*

Most document pipelines have one stage too many. Reading each document once into a whole-file table deletes per-page retries, trace nodes and the staleness window in one move. If a closing balance disagrees with its line items, a whole-file check finds the file, not the stage. A provable pipeline uses separate workers under a durable workflow runtime, stamps every write with an attempt ID and app version, and derives a DAG trace from the events it already records.

## Split at phase boundaries, keep workers deterministic

A single serverless function that extracts, categorises and inserts rows gets killed by database contention, and retrying it repeats completed work. Give each phase its own invocation that reads inputs from the database; a durable workflow runtime owns the sequence.

Do not build that sequence from chained fire-and-forget calls. Nested request chains hit depth limits on many serverless platforms, and a polling dispatcher adds dead time. Keep each worker a deterministic function of `(job, chunk)` so moving to another orchestrator is a packaging job.

## Re-runs must use current code and own their writes

A re-run button that calls the old flow is unsafe the moment a new parser ships. An execution started before a deploy can stay bound to the deployment that started it and pull queued steps from the old version. Stamp the app version on every event and give each attempt its own run ID. Workers update only rows carrying the attempt’s run ID; a stale worker writes nothing. That stops stale workers from contradicting each other; it does not stop two orchestrators running under the same ID.

Watchdog resets must distinguish a deterministic failure from a run still doing real work. If an API layer has not absorbed a schema change, a lease update can match no rows and exit silently, after which a watchdog resets a healthy job. Log every failed claim, and do not mark a job complete while its rows are missing.

## The trace is a DAG, not a flat log

A flat event log tells you events happened, not where a value stopped being right. Build the trace from the events you already have: each node carries input and output summaries and a boundary check. When a later stage applies a learned “usual direction” over a correct reading, the node whose closing balance disagrees with its line items is the one you open first — you are not bisecting the pipeline by hand.

Make determinism testable. The same document and settings should produce identical output and trace. Fix each row’s identity and order before any model call; the model fills in fields on rows it cannot invent, drop or reorder. Hash stage inputs and outputs, and record what a re-run replaced. A mismatch separates a code change, model non-determinism or an upstream ordering difference; the hash alone will not separate model drift from a code change.

## Cut the tail, not the whole request

Among the model calls that remain, slowness is often the server, not the document. A shorter deadline with more retries still waits the full deadline when a call is merely late. Hedged requests in the Tail at Scale work send a fallback after the 95th percentile of expected latency and take whichever answers first, on the first attempt only. Tag which mitigation won.

Treat a deadline as a measurement: a chunk that fails consistently may simply need longer, so measure isolated latency before blaming the content.

Before adding per-page retries, hedged calls or extra trace nodes, check whether one whole-file read removes the stage entirely. The largest simplification is usually deletion, not tuning.
