Skip to content

ADR-0005: The item pipeline runs one durable task per batch, with one phase per stage

  • Status: accepted
  • Date: 2026-11-14
  • Decided by: Stefan Imhoff

Context

SI-533 needs a pipeline that takes every new item through several AI stages (extract, gate, dedupe, summarize, score), where a crash or deploy resumes without repeating a paid call, stages can be turned off per user, and progress is visible. The SI-512 spike showed two ways to run work in pi-durable: an agent run (a model decides which tools to call) or custom tasks with phases (defineTask). Its notes recommend custom tasks for the pipeline.

Some stages give the same result for every user (full text, summary), others depend on the user (gate, score). At 350–500 items a day, a few users at most, and two SQLite files linked only by ID (ADR-0002).

Decision

  • One custom task per batch of up to 25 items, not per item. Its phases are start, then one stage phase per stage (the stage names are fixed in the checkpoint when the batch starts), then finish.
  • Paid calls go through once(key, call), which stores the result in the task’s memos before returning, so the rerun of an interrupted stage doesn’t call again.
  • Progress lives in hotaru.db: pipeline_batches, item_pipeline (one row per user and item) and item_pipeline_steps (one per user, item and stage). Pages and the API read only these.
  • Stages declare a scope. An item stage runs once per item for all users it’s on for, a user stage once per user and item. Steps are always per user, so turning a stage off and the budget work per user.
  • A loop in the app process queues new items, forms batches and creates their tasks, like the scheduler (ADR-0004). Only items that arrive after the pipeline first ran with a stage are queued on their own.

Rejected alternatives

  • One task per item. Simpler state, but no way for a stage to batch several items into one model call, and a few thousand tasks a week in durable.db instead of a few hundred.
  • An agent run per item (the reference news agent). Every item would cost model calls just to decide what to do next, and the order of the stages would be up to the model.
  • One task per user and item. Shared stages (full text, summary) would run once per subscriber and pay that often, unless they check each other’s results across tasks.
  • Progress only in durable.db. pi-durable’s task records hold everything, but pages would need the harness for every read, and a lost durable.db would lose the history too.

Consequences

  • A stage that throws fails its units for that batch only; the batch goes on. A bug outside a stage faults the task, and the loop marks its batch failed.
  • A batch’s items wait for its slowest stage. With 25 items and a concurrency of a few calls, that’s seconds.
  • A stage must write its results as upserts and wrap every paid call in once(). pipeline.md says so for whoever writes the next stage.
  • durable.db can be lost without losing the pipeline’s history: the loop marks the batches whose task is gone as failed, and their items can be re-run.