Captures now get a WebP thumbnail: 400px on the long edge, written to
thumbs/<uuid>_400.webp (SPEC 3.3). /ingest enqueues the job in the same
transaction as the capture row, so the worker never sees a job for a capture
that was rolled back, and decoding stays off the request path - it is the one
genuinely expensive part of a capture, and the device is waiting with a burst
still to upload.
api/images.py moves up to poorbird/images.py. Both sides use it now, and a
worker importing from api would be the wrong way round. The split by cost
stays: everything the ingest path touches still reads nine bytes of JPEG
header, and Pillow appears only in the thumbnail half.
Handler behaviour is about which failures are worth retrying:
- An undecodable frame returns rather than raises. The handler contract
makes a raise mean "retry", and a frame that will never decode should not
spend three attempts proving it. Logged, thumb_path left NULL.
- A missing source file raises, because it should be there and a mount that
has not come back yet is worth retrying for.
- A capture deleted between enqueue and run is not an error. Retention will
do that routinely in Phase 5.
- A malformed payload is validated with a Pydantic model and recorded, not
retried (SEC-003 applied to a job payload).
- Never upscales: blowing a 100px frame up to 400px produces a blurry lie.
- Fills in width/height when the ingest header parse could not. This is the
first full decode, so it is the chance to fix that.
Verified end to end against a real device row, a real HTTP ingest and the
real worker dispatch path - claim, _run_one, complete - not just the handler
called directly: 1600x1200 JPEG in, WEBP 400x300 on disk, job marked done.
That run is what caught the rest of this commit.
Tests were leaking jobs into the shared queue, which broke test_queue.py once
/ingest started enqueuing. Three separate causes, all the same shape as the
teardown bug fixed in 269a6ea:
- test_ingest cleaned captures but not their thumbnail jobs, and one test
hand-rolled teardown for a second device and forgot half of it. Devices
now come from a fixture that cleans frames, captures and jobs for every
device a test makes.
- test_queue claimed jobs without checking they were its own, so a leftover
thumbnail job could be failed on its behalf, and one test claimed a
second job to prove the first did not recur and then stranded it in
'running' forever. It now enqueues at a priority that wins, asserts every
claim is its own, and releases anything else it touches.
The suite now runs twice in a row leaving zero jobs behind, which is the
assertion that actually matters here.
Tests: 175 -> 193.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The plumbing every Phase 2 endpoint needs, before any endpoint exists.
api/schemas.py: Pydantic models for the whole device contract - enrollment,
the shared /ingest + /heartbeat envelope, the conditions body and command
acks. Enums are imported from the models, so a schema cannot drift from the
CHECK constraint that backs it.
api/deps.py: credential generation, hashing and bearer resolution.
Written against firmware/API.md and, where prose and code disagree, against
firmware/src/api_client.cpp. Four places where REVIEW.AI.md's reference code
would not have worked:
- Its /enroll returns devices.token_hash to the device. The device would then
present the hash, the server would hash it again, and every request after
enrollment would 401. Plaintext goes back once, only the hash is stored.
- It checks only the shared bootstrap secret on re-enrollment. The firmware
swaps that for a per-device reenroll_secret after first enrollment, so that
check would lock out every device that had ever succeeded - the exact
failure the re-enrollment path exists to prevent.
- Its /ingest is multipart. API.md deviates from SPEC 4.1 deliberately: the
body is the raw JPEG and metadata rides in headers, so the device does not
assemble a second buffer. Modelled as IngestMeta.
- It uses secrets.token_urlsafe(32) - 43 non-hex chars. API.md says 64 hex,
and the firmware's char[65] buffers make that exact. token_hex(32) it is.
capture_id is documented as an int but captures.id is a UUID. parseEnvelope
never reads the field, so it is sent as a UUID string.
The conditions models accept unknown keys on purpose: firmware grows fields
before the server learns them, device_status.raw keeps the whole body, and a
422 would take a camera offline for reporting something new.
devices.token_hash is now indexed and unique - it is resolved on every
authenticated request, and two devices must never share a token. No migration:
the live database already had exactly this index, so the model had drifted
from it. 'alembic check' is clean.
Tests: 17 -> 57.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Set up the dev environment (uv-managed CPython 3.14.7, postgres:17 in a
Podman container) and ran the skeleton for real: migrations apply, the
API serves /api/v1/health and /healthz, the worker claims a job and
drains it, 12/12 tests pass with nothing skipped.
The schema-drift test failed on its first real run and found a genuine
problem: Postgres reflects a varchar default as 'x'::character varying
while the model renders 'x', so all ten string defaults read as
permanent drift. Left alone that is the failure mode where you learn to
ignore the test and then miss the one real change. Add schema_compare.py,
a normaliser shared by migrations/env.py and the test so autogenerate and
the test can never disagree about what counts as a difference. A real
default change still fails it.
Also: ruff import-order fix in captures.py, and the README gains the
uv + Podman dev setup, the expected test count, and the CREATE DATABASE
privilege the drift test needs.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>