One body, two endpoints. The firmware builds the conditions payload once, in
buildConditions, and posts it to both; they differ only in the response.
/heartbeat -> 200 and the envelope: commands, config version, firmware
/status -> 204 and nothing
The split is not cosmetic and is now pinned by a test. The device posts
status immediately before deep sleep, so delivering commands there would mark
them delivered to a device about to be unable to run them.
api/envelope.py holds the shared builder, because /ingest returns the same
shape and parseEnvelope handles both with one function - they must not drift.
- Commands are marked delivered when handed over, not on ack: the ack may
never come, since the device runs reboot and sleep_now and is gone.
acked_at stays the separate, later signal that it actually ran.
- Expired commands are skipped but left in place, so the admin UI can show
that a command timed out rather than silently losing it. The worker's
prune job is what removes them.
- The firmware URL is deliberately relative. ota_mgr.cpp:86 appends any
non-http url to the API base, which already ends in /api/v1, so
/firmware/0.2.0.bin resolves to /api/v1/firmware/0.2.0.bin.
- Being on target means the firmware key is absent, not null: naming a
different version is itself the instruction to flash (main.cpp:104).
Recording conditions appends a device_status row and refreshes the
denormalised columns on devices. Three details worth stating:
- A field the device omits does not erase the last good value. None means
not sent, not zero.
- battery.valid false means NOT MEASURED and is stored as false, so the UI
renders an em dash instead of inventing a low-battery alarm.
- The device's reported config_version never overwrites the server's. The
difference between them is exactly what tells the device to GET /config.
Tests: 77 -> 99. Verified by making /status build the envelope: the contract
test fails, as intended.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The first device endpoint, and the one every other endpoint depends on:
nothing else can authenticate until a device has a token.
Guarded by X-Enroll-Secret rather than a bearer token, accepting either the
shared bootstrap secret or the device's own re-enrollment secret. Issues a
token and a per-device re-enrollment secret, stores only their hashes, and
returns the plaintext exactly once.
Re-enrollment of a known device_uid returns the existing row with a fresh
token, never a duplicate (API.md). The firmware re-enrolls on any 401, so
this is the normal path by which a rotated token heals itself without
physical access to a mounted camera.
Two judgement calls, both marked in the code:
- A disabled device is refused 403 and its token is left alone. Otherwise
disabling a device in the admin UI would achieve nothing, since it would
collect a fresh token on its next 401.
- Hardware facts (mac, hw_revision, fw_version) follow the device on every
enrollment, because it is the authority on what it is running. The name
does not: it is set at first enrollment and is an operator label after
that, so a re-enrollment must not silently undo a rename.
Status codes are chosen for how the firmware reacts, not just for
correctness: 401 makes it clear its token and retry, so a misconfigured
server returns 503 and a contended write returns 503, never 4xx.
Tests: 57 -> 77. Verified by reintroducing the REVIEW.AI.md bug of returning
token_hash instead of the token: three tests fail, including the round trip
that feeds the issued token back through verify_device_token.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The review documents are branch-scoped snapshots pinned to a commit, not
living documentation of the directory they sit in. Kept at a fixed path,
every reviewed branch writes the same two filenames with different content.
Merging the two feature branches produced exactly that:
CONFLICT (add/add): server/REVIEW.AI.md
CONFLICT (add/add): server/REVIEW.md
Neither blob is an ancestor of the other, so git cannot resolve it, and it
would recur on every future review. The same fixed path also caused the
review OF the UI branch to live under server/, which reads as server
documentation and is how it got cherry-picked onto the wrong branch once
already.
One directory per reviewed scope fixes both. The filenames stay REVIEW.md and
REVIEW.AI.md, so existing references in the code still resolve. A merge probe
against feature/UI_Basic_implementation is now clean.
docs/reviews/README.md records the convention, how to read a snapshot whose
commit is behind the tip, and why not to copy a review between branches.
feature/UI_Basic_implementation still holds its review at server/REVIEW*.md.
That no longer conflicts, but it is still the misleading path and wants the
same move.
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>