Skip to content

From pipeline to cluster

Backseat Driver is one idea: read the scenes, process each image with a vision-language model, write the descriptions. Everything else in the repository is that idea stretched across more machines. This page shows how, one rung at a time, and where each added piece enters the code.

Showing the results is a separate role. It reads what was written and never takes part in the three steps (see Describe and show).

Rung 1: the pipeline

flowchart LR
    subgraph proc["one process · one function call · pipeline.py"]
        direction LR
        R["<b>read</b><br/>SceneLoader<br/>local disk"] --> P["<b>process</b><br/>Captioner<br/>BLIP / Ollama / Claude"] --> W["<b>write</b><br/>write_json<br/>one JSON file"]
    end
just describe --camera front        # = uv run backseat-driver describe ...

describe loads the keyframes, calls describe_keyframe for each, and writes the list. Nothing is queued, stored or served. This is the whole program, and the code that runs it is pipeline.py.

Code, in reading order

Step Port Adapter Wired in
read SceneLoader NuScenesSceneLoader stacks.pipeline
process Captioner BackendCaptioner over a HuggingFace, Ollama or Anthropic backend process.factory
write write_json cli/describe.py

Tests: test_pipeline.py, tests/unittests/read/, process/, write/, and the command itself in test_pipeline_end_to_end.py.

Rung 2: the seam

flowchart LR
    subgraph proc["still one process · one event loop · the API monolith (just dev)"]
        direction LR
        R["<b>read</b><br/>ingest worker"] --> Q(["<b>transport</b><br/>JobQueue<br/>in memory"]) --> P["<b>process</b><br/>caption worker"] --> W["<b>write</b><br/>JobStore<br/>results + job state<br/>SQLite file"]
    end
just dev            # POST /jobs on :8080

The API turns the run into tasks. Ingest is the read step turned into a producer: it reads once and enqueues one task per image. The caption worker is the process and write steps run once per task. Both still run on a background thread inside the API process, so nothing extra is needed. The queue (transport/) is the new thing, and it is a seam: a place where read and process could be pulled apart.

The write changes with it. Tasks finish one at a time, in any order and possibly twice, and no process holds all the results, so the write becomes incremental: the caption worker records one row per description. The same store keeps the job record. Ingest splits the job, so it records how many descriptions to expect; the store collects them, and the job is complete when the count reaches that number. The state is derived on every read and never stored, so there is no "mark complete" step for concurrent workers to race. That is why the results and the job record are one store, and why transport/job_store/ sits in the seam's package: it is there from rung 2, and only the database changes across machines.

Code, in reading order

What Where
The seam, in one docstring transport/__init__.py
The queue port and its in-process adapter JobQueue, InProcessJobQueue
Read as a producer; process and write per task IngestWorker, CaptionWorker, both reusing describe_keyframe
The job store the workers write to JobStore, SqlJobStore over SQLite
The front door POST /jobs
Wired in stacks.seam

Tests: tests/unittests/transport/, transport/job_store/, and the API end to end in tests/integrationtests/api/.

Rung 3: machines

flowchart LR
    subgraph svc["separate services · just up · Kubernetes"]
        direction LR
        R["<b>read</b><br/>ingest pod"] --> Q(["<b>transport</b><br/>RabbitMQ"]) --> P["<b>process</b><br/>caption pods × N"] --> W["<b>write</b><br/>Postgres"]
        S[("S3 bucket<br/>the dataset · read/s3/")]
        R -- "tables" --> S
        S -- "one image" --> P
    end

Once the queue crosses machines, two things stop working, and each gets a replacement:

At the seam Across machines Replaced by
Images are read from the local disk The caption pod cannot see the ingest pod's disk S3: read/s3/ holds the dataset in a bucket, and each task carries the image's URI
The job store is a SQLite file inside the API process The API and the caption pods share no file Postgres: the same SqlJobStore (transport/job_store/) over a database every service can reach

Code, in reading order

What Where
RabbitMQ as the queue CeleryJobQueue, the tasks and worker entrypoints in tasks.py, worker ingest --once in consume_one.py
The dataset in a bucket read/s3/: S3DatasetStore, StoredSceneLoader, the one-time DatasetUploader
The job store in Postgres SqlJobStore over orm.py and storage.py
Submitting from the CLI ApiJobClient behind describe --mode distributed
Wired in stacks.machines, celery_queue, postgres_store, stored_loader

Tests: tests/unittests/read/s3/, tests/integrationtests/read/s3/, tests/unittests/transport/ and tests/integrationtests/transport/.

What changes, and what does not

The models (SceneKeyframe in, SceneDescription out), the three ports and describe_keyframe are the same at every rung. The hand-off and the write contract change:

Stage Rung 1: pipeline Rung 2: seam Rung 3: machines
Read The loader returns a list of keyframes Ingest reads, then enqueues one task per keyframe Same, with images and tables from S3
Hand-off A function call over the list InProcessJobQueue CeleryJobQueue over RabbitMQ
Process describe_keyframe in a loop The same function, once per task The same function, on N pods
Write Collect everything, then write one JSON file One row per result (SQLite) One row per result (Postgres)
Job state None; the run just ends Derived from counts Derived from counts and safe to redeliver

The real difference is in the write. At rung 1 it is a batch at the end. From rung 2 up, results arrive one at a time, in any order and possibly twice, so the write must be incremental and idempotent, and "is the job finished" is derived from counts instead of being a step someone performs. The job record (the expected count and any error) lives with the results for that reason: the state is a comparison of the two.

Where each piece lives

backseat_driver/
├── models/          SceneKeyframe, SceneDescription ...       shared by every rung
├── read/            SceneLoader, ImageStore, local loader     ┐
├── process/         Captioner, backends                       ├ the core (rung 1)
├── write/           write_json                                │
├── pipeline.py      read -> process, run by `describe`        ┘
│
├── transport/       JobQueue, ingest + caption workers        ┐ rung 2: the seam
│                    InProcessJobQueue, CeleryJobQueue         │ and the write it forces
│   └── job_store/   JobStore: results + job record            ┘ (Celery and Postgres: rung 3)
├── write/job_store/ the job database: tables and queries      the part that actually writes
├── read/s3/         bucket-backed ImageStore and loader       ─ rung 3: workers share no disk
│
├── stacks.py        which adapter each rung plugs into each port   the wiring, in one place
├── show/            report and UI over what was written       separate role
├── api/             /jobs, /images, /describe                 the front door of rungs 2-3
└── cli/             describe, report, ui, worker, db, dataset

The core never imports the added packages. tests/unittests/test_layering.py checks that on every run: read, process, write, pipeline and models may not import transport, read/s3, write/job_store, Celery, aioboto3 or SQLAlchemy.

The wiring in one file

stacks.py is where the table above becomes code. It has one recipe per rung, and each only builds the adapters for it:

Port Rung 1: pipeline Rung 2: seam Rung 3: machines
Scene loader NuScenesSceneLoader RelativeSceneLoader StoredSceneLoader (tables from the bucket)
Image store LocalImageStore LocalImageStore S3DatasetStore
Queue none InProcessJobQueue CeleryJobQueue
Captioner BackendCaptioner BackendCaptioner BackendCaptioner
Write write_json SqlJobStore (SQLite) SqlJobStore (Postgres)

describe calls stacks.pipeline, the API calls stacks.build_job_backend (which picks seam or machines from BACKSEAT_DRIVER_MODE), and the Celery workers build their ends from stored_loader, celery_queue and postgres_store. To see what a rung is made of, read its function.

One HTTP client per process

Every HTTP call the app makes (the Ollama and Anthropic backends, the API client behind describe --mode distributed and the report) goes through the async HttpClient port, whose one adapter, HttpxHttpClient, wraps a single shared httpx.AsyncClient. Requests reuse its connections instead of opening a connection and doing a TLS handshake each, which is what a worker captioning thousands of keyframes, or a batch fanning out to Anthropic, would otherwise pay for on every request. The recipe build_http_client only builds it; the client is created on the first call and never connects before that.

An httpx client belongs to the event loop that first used it, so whoever owns the loop closes it: the API's lifespan and the report UI's lifespan, a Celery worker's worker_loop (which awaits registered closers before the loop closes), and a one-shot command inside its single asyncio.run. A call from any other loop raises a RuntimeError that says so. Each call is bounded by asyncio.timeout in its caller; the client itself has no timeout. Its connection limit (100) caps sockets, not requests in flight: the caption batch size is what bounds those, and a request waiting for a free connection would already be inside its caller's timeout, so keep the limit above the batch size.

One command, either rung

uv run backseat-driver describe --camera front --model Salesforce/blip-image-captioning-base
uv run backseat-driver describe --mode distributed --output output/cluster.json

--mode distributed submits the same job to a running API, waits for the workers, and writes the same JSON file as rung 1. The model, camera and dataset are the workers' own settings, so those flags are rejected in that mode, and --output is required because the client cannot know which model the workers use.

Describe and show

flowchart LR
    subgraph describe["describe"]
        direction LR
        R[read] --> P[process] --> W[write]
    end
    subgraph show["show"]
        direction LR
        O["report, ui"]
        A["ui --all-jobs"]
    end
    W --> F["output/*.json"] --> O
    W --> J[("job store")] --> API[API] --> A

describe produces descriptions. report and ui (show/) read them from JSON files, or from the API's completed jobs, score them against the nuScenes label and render the comparison page. They never run a model or open the dataset, so the UI deployment mounts nothing.

Where to go next