Skip to content

Item pipeline

Takes every new item through the AI stages, one after another, as durable pi-durable tasks (SI-533). Code: packages/core/src/pipeline/. The design is in ADR-0005. In the app: Settings → Executions (/settings/executions).

The stages are their own issues: extract (SI-535, extraction.md), gate (SI-539, gate.md), keywords (SI-537, keywords.md), dedupe (SI-540, dedupe.md), summarize (SI-536, summaries.md), research (SI-551, research.md), score (SI-541, part of the gate’s call: scores.md), the user’s own checks (SI-567, also part of the gate’s call: checks.md). Ranking (SI-542) is no stage: the stream ranks when it’s read (ranking.md). Each one adds itself to PIPELINE_STAGES. extract, gate, keywords, dedupe, summarize and research are in; gate is the first paid stage. While that list is empty, the pipeline queues nothing.

The way through

  1. Queue. Every 30 seconds, and a second after the fetcher stores new items, the pipeline loop queues each user’s new items: one item_pipeline row per user and item, status queued. Items a filter rule muted are left out (filters.md).
  2. Batch. Queued items go into batches of up to 25 (pipeline_batches). At most two batches are queued or running at once; more items wait.
  3. Run. Each batch is one durable task in durable.db. Its first phase marks the batch running. Then it runs one phase per stage, in order, and a last phase that gives each item its status.
  4. Show. Every stage writes one item_pipeline_steps row per user and item: done, skipped (with a reason) or failed (with the error). The Executions view and the API read these rows, never durable.db.

An item ends:

Status When
done At least one stage ran for it, and none failed
skipped Every stage was skipped: turned off, over budget, or not needed for the item
failed A stage failed for it. Its later stages are skipped (upstream_failed). error names the stage

Stages

A stage (stage.ts) has a name (stored, never renamed), a label, a scope, whether it’s paid, and whether it’s on by default. Its run(context, units) gets the batch’s units and returns one outcome per unit, in order.

  • Scope item runs once per item for every user it’s on for: full text and summaries are the same for everyone. A unit has the item and those users.
  • Scope user runs once per user and item, because the result depends on the user: their interests, weights and keywords.
  • Batching is the stage’s choice: one model call per unit (eachUnit() runs them with a concurrency limit), or several units in one call.
  • Results go to hotaru.db from the stage itself, as upserts keyed by their natural ID (conventions.md). A step’s note is a short line for the Executions view, such as a score.
  • A throw fails every unit of the batch for that stage. The batch goes on with the next stage.

Before a stage runs, units are left out, with a skipped step, when the user turned the stage off (disabled), an earlier stage failed for the item (upstream_failed), or the stage is paid and the user may not use AI, no admin and no OpenRouter key of their own (no_key, costs.md), or is over their monthly AI budget (budget, through allowPaid).

Paid stages call models and jev through context.ai, which checks the budget again before every call and records it in the cost ledger. A call refused for the budget skips its unit with the reason budget. See costs.md.

Users turn stages on or off in Settings → Executions, stored in user_settings.preferences.pipelineStages.

Never paying twice

A crash, a deploy or kill -9 can stop a batch anywhere. The next process opens durable.db, and pi-durable resumes the task at the phase it was in, that is, at the stage. Three things keep work from repeating:

  • context.once(unit, key, call). A stage wraps every paid call (context.ai.chat(), context.ai.decide()) in it. The result is stored in the task’s memos in durable.db before once() returns, so the rerun of the stage gets the stored result instead of calling again. Every model and jev call must go through once(). Only a call that was still running when the process stopped runs again. The memo is named after the stage, the unit’s item and, in a user stage, its user, so two users’ results for one item never mix; key tells apart several calls for one unit.
  • Stored steps. A stage that already stored its steps, then stopped before its checkpoint, skips the units that have a step.
  • One task per batch. The task is created in the same commit that records it under the batch’s ID (BatchTasks), so a batch whose task was created just before a crash doesn’t get a second one.

On close, the running stage is aborted through context.signal. A stage that stops this way stores no steps, so it runs again on resume. Pass the signal to fetch and to model calls. The server closes the pipeline on SIGTERM and SIGINT (adapter-node’s sveltekit:shutdown), so a deploy is a close, not a crash: pi-durable counts crashes, and a task cut off by five in a row ends faulted.

Never change the task’s version or name (hotaru.pipeline.batch, task.ts) without a migrate for the stored tasks. A task whose definition doesn’t fit can’t run; the loop then fails its batch, so it doesn’t hold a batch slot forever.

Two tests show this: one closes the pipeline in the middle of a stage, the other runs it in a child process and kills it with SIGKILL (pipeline.spec.ts). In both, the finished calls aren’t made again.

Unsubscribing

Unsubscribing removes the user’s items of the source from the pipeline too, with their steps, so no paid stage runs for items that left the stream. A batch that is running skips them from its next stage on.

Re-runs

Run again in the Executions view, or POST /api/v1/items/:id/pipeline, clears the item’s steps for the user and queues it. The next round puts it in a batch, from the first stage. A queued or running item can’t be re-run (409). A re-run works for any item in the user’s stream, also one from before the pipeline was turned on.

Automatic retries

A failed item runs again by itself, from the first stage: 2 minutes after it failed, then 5, then 15. Most failures pass: a model that answered with broken JSON, a timeout, a short outage. The growing pauses keep an outage from tripling the spend within a few minutes. After the third retry it stays failed until someone clicks Run again, which starts the three retries over. Only failed items are retried. Items skipped for the budget or a turned-off stage aren’t failed, so they don’t run again.

item_pipeline.retries counts the retries since the item last started fresh, and runs counts every run. The Executions view says when an item will try again, or how often it already did. The API returns retries and retryAt with each execution.

Cost: a retry costs what a run does, about $0.0005 per item (Settings → Executions shows the current estimate). A retry runs in a new batch, so it pays again for every paid stage, also the ones that worked the first time. Three retries at most mean a failing item costs up to four runs.

Turning it on

The first round with at least one stage stores instance_settings.pipeline.enabledSince. Only items that reach a stream after that are queued on their own, so the first deploy with a stage doesn’t pay for the whole history. Older items can be re-run one by one.

PIPELINE_ENABLED=false keeps the server from opening durable.db and running the loop (deployment.md). CLI scripts and tests never open it: pi-durable allows one writing process per file.

Restores and a lost durable.db

hotaru.db and durable.db are backed up separately (backups.md) and are linked only by IDs (ADR-0002).

  • durable.db lost or older than hotaru.db: batches whose task is gone, or whose task ID now belongs to another task, are marked failed (“The batch task is gone”) at the next round. Their items can be re-run.
  • hotaru.db older than durable.db: a task whose batch has no rows left runs through its stages with nothing to do and ends.
  • A task that faults (a bug outside a stage, or crashing its process five times in a row) ends its batch failed with the error.
  • A storage error on durable.db (a commit that fails, such as a locked or full disk): pi-durable fails its session, and from then on it refuses everything with SessionFailed (“close and reopen it”). The log first says storage_failed: <cause>. The pipeline then closes durable.db and opens it again within a second or so, which resumes from what was committed: running batches carry on, and their finished paid calls aren’t made again. The log says reopened durable.db. When reopening fails too, every round tries again. Before this, the pipeline stayed stuck until the container restarted, with items in Queued.

In the app and the API

Settings → Executions lists the user’s items in the pipeline, most recently changed first, with each stage’s outcome and a filter by status. It also turns stages on and off.

GET /api/v1/pipeline The stages (with enabled), counts per status, and the items with their steps. ?status=, ?limit= (≤ 200), ?before= with the previous page’s nextBefore (an opaque cursor) for the next page
PATCH /api/v1/pipeline/stages { "stages": { "<name>": true | false } }. 400 for an unknown stage. Needs a session or a write token
POST /api/v1/items/:id/pipeline Run again: 202 { "status": "queued" }, 409 when queued or running, 404 when the item isn’t the user’s. Needs a session or a write token

The SDK has pipeline(), setPipelineStages() and rerunPipeline().