devel #23

Merged
troed merged 13 commits from devel into main 2026-10-17 19:43:24 +02:00
Owner
No description provided.
Which trust set is in force, and whether it is actually complete. A partial set
is never reported as usable -- a half-written dev set that reported 'dev' would
be silently trusted.
One CA, a server and a crew leaf, and an Ed25519 signing pair, written
atomically into $STARFLEET_TRUST_DIR (a partial set is never usable). openssl
only: the sandbox has no cryptography module. Re-keying is explicit, because a
new CA means every device pinned to the old one must be reflashed.
gen_ota_trust.sh becomes a delegator and accepts --out/--dev/--repo-root/--san,
so a test can emit a header without touching the tracked one. Without production
keys the header is refused (exit 3) unless --allow-dev is given, which keeps
release_firmware.sh failing closed until the release assertions land.
--san-from-kconfig reads CONFIG_SERVER_HOST, merged with --san values and
de-duplicated first-seen, so the firmware path can declare the address it
actually dials without repeating it by hand.
Review findings, all reproduced first as failing tests:
- an empty production ca.pem counted as production and rendered a header
  flagged OTA_TRUST_PRESENT with no CA in it (regression of the earlier
  [[ -s ]] guard); production material must now be non-empty, and a header can
  never be rendered from an empty CA
- the header was written with write_text, truncating the tracked destination
  first; it is now written to a sibling temp file and os.replace'd
- unusable material and unreadable build configs surfaced as raw tracebacks
  instead of the documented exit 4
# fix: a failed connect was completely silent

## What this is

`main/app_state.c`'s connect-failure path set `UI_ERROR` and returned to `IDLE`
in the same breath, and `enter(APP_IDLE)` immediately sets `UI_IDLE`. The ring
task samples every 33 ms, so the error state was never observed: a failed
connect produced a brief blue ring and then nothing at all.

That is the failure chased on hardware during the fleet TLS-only work: the
device joined its network, obtained an IP, and then died at the WebSocket
upgrade because the server serves `wss://` while the flashed build spoke
plaintext `ws://`. A plaintext upgrade into a TLS listener is closed with zero
bytes, so the symptom read as "the wake word stopped working" — and the one
signal that could have said otherwise was never drawn.

## Change

Surface the error after the app has settled into idle, and hold it past one ring
animation cycle, then return to idle:

```c
if (from_retry) disconnect_and_idle();
else enter(APP_IDLE);
show_error_briefly();      /* set UI_ERROR, hold past one animation, then IDLE */
```

`UI_ERROR` is a notification, not a state — the ring animates a triple flash
over 12 steps and then restores `s_prev` — so the hold is what makes it visible.
The trailing `ui_ring_set(UI_IDLE)` makes the end of the flash deterministic
rather than dependent on the auto-revert. This also gives the mid-conversation
retry failure an indication it never had.

## Verification

Induced three real connect failures on hardware (temporarily pointing the build
at a dead port): the log shows `connect stages: ... -> failed` → `state -> IDLE`
each time, and the ring showed the red triple flash each time.

`power_idle_set()` only flips the ring's low-power flag (speaker amp off, slower
key poll) and never cuts an LED rail, and the `UI_ERROR` branch deliberately
ignores that flag — so it renders in low-power idle.

**No automated test**, and that is a gap rather than a claim: the host suite
covers only the pure-logic components (`protocol`, `ed25519`, `creds`), and this
is ordering/timing inside the FreeRTOS state machine. A ring-state test seam
would be a reasonable follow-up.

## Notes

Independent of the per-device Wi-Fi feature; cherry-picked onto `devel` after
that branch merged, so it can be reviewed on its own.

Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #16
## What

Piece 2 of the fleet TLS-only transport design: make the ESP32 firmware's realtime transport **unconditionally TLS**, so no build path can silently put the protocol on the wire unencrypted or without trust material.

- **Trust material is compiler-required.** `main/ota_trust.h`'s tracked placeholder is now an `#error` (no empty CA can ever compile), and `main/CMakeLists.txt` regenerates the header at configure time: production keys (`tools/ota-keys/`) when present, otherwise the shared dev set with a `DEV TRUST MATERIAL` banner.
- **No TLS opt-out.** `CONFIG_WS_TLS` is deleted; `main/ws_client.c` always builds `wss://` with `.transport = WEBSOCKET_TRANSPORT_OVER_SSL` and the pinned CA.
- **`covers()` reads the leaf's real SANs** instead of trusting the `MANIFEST` (the piece-1 review's suggestion).
- **`tools/release_firmware.sh` refuses dev-trust firmware** (replaces the old `CONFIG_WS_TLS=y` assertion).
- **Docs** (README/AGENTS) state the always-TLS transport and required material.
- **Static invariant fence** (`host/tests/test_tls_invariant.py`) pins the source-level properties, complementing the build gate.

## Why

All three fleet repos shared one defect: TLS material optional, absence silently degrading to plaintext `ws://`. On 2026-10-14 the attached ESP32 joined its network, got an IP, then died at the WebSocket upgrade because the flashed build spoke plaintext into a `wss://` listener (closed with zero bytes — indistinguishable from a dead wake word).

- Design: `starfleet:docs/superpowers/specs/2026-10-14-fleet-tls-only-design.md`
- Plan: `starfleet:docs/superpowers/plans/2026-10-14-fleet-tls-piece-2-esp32-enforcement.md`

## Verification

- Host suite: **157 passed, 1 deselected** (the deselected test is the pre-existing environmental failure that shells out to a host-built venv).
- Sandbox `idf.py` builds, all three trust paths exercised and state restored:
  - production keys present → production material embedded, no banner (RF3);
  - no production keys → dev material + `DEV TRUST MATERIAL` banner, dev set at `<workspace>/.dev-trust`;
  - no keys + empty `CONFIG_SERVER_HOST` → configure prints the remedy and the build **fails at the placeholder `#error`** (RF1).
- Fresh code review (independent reviewer, no Critical; 2 Important fixed in one RED→GREEN pass: committed-header fence + provenance-aware regeneration, and honouring the build's `SDKCONFIG`).

## Not in this PR

- **Task 6, the on-hardware dev-trust run**, is deferred: it needs the attached unit, host actions, a server running with the dev leaf, and a human to hear the reply.
- `computer` and `communicator-sailfish` enforcement (pieces 3 and 4).
- One deferred doc minor: `AGENTS.md`'s server-side live-tap example still uses `ws://127.0.0.1` (a `computer`-side script; piece 3).

Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #17
Piece 3 (computer) made the canonical fake realtime server TLS-only:
FakeServer.__init__ now requires an ssl.SSLContext, so the esp32 host
conformance fixture failed at setup ("FakeServer.__init__() missing 1
required positional argument: 'ssl_context'") for all six conformance
tests. Pair the server with a throwaway self-signed leaf and dial wss://.

- host/tests/tls_certs.py: mint a two-day self-signed leaf with openssl and
  build the paired server/client contexts. Self-contained: this is the
  esp32-side copy of computer/tests/tls_certs.py, never a cross-repo import.
- fake_server_fixture.py: session-scoped throwaway cert, pass ssl_context=
  to FakeServer, expose a client context that trusts it.
- conftest.py: ws_url is now wss://; re-export the cert fixture.
- test_conformance.py: use the asyncio client, not websockets.sync.client.
  Over this TLS harness the sync client intermittently (~5%) never receives
  the opening handshake response and dies with "timed out while waiting for
  handshake response" after the full open_timeout, while the asyncio client
  is clean over the same connections (180+ verified). It is also the pattern
  computer's own conformance suite uses.
- requirements.txt: websockets>=13 -- the harness imports
  websockets.asyncio, which 12.0 does not provide.
- docs/conformance.md: say the harness is TLS-only.

Verified: host suite 158 passed across 8 consecutive full runs (was 152
passed + 6 errors before this change) and 30/30 conformance-only runs.
# fix(tools): trust-tool review minors (no silent re-key, unique staging, stricter fences)

The `communicator-esp32` half of the deferred review minors recorded in the
fleet TLS-only handover (`HANDOVER.md` §3.5). All of these were triaged "ship,
not blocking" during pieces 1 and 2; this addresses the six that live in the
fleet trust authority and its fences.

## What changes

**A complete set is never silently re-keyed** (the most serious of the set).
`ensure` treated a set whose `MANIFEST` could not be parsed as "no set" and
regenerated it. Regenerating changes the CA a flashed device has pinned, so a
corrupt MANIFEST would have quietly bricked the fleet's trust relationship. It
now returns `unreadable` — exit 2, printing the `--regenerate` remedy — and only
an explicit `--regenerate` re-keys. `header --allow-dev` has the same hole fixed
differently: it no longer consults the MANIFEST at all, because rendering a
header needs presence and the *leaf* is what decides whether a SAN is covered.

**`ensure`'s refusals print a remedy** naming `--regenerate`, as `header`'s
already did. Previously a refusal was a bare `TRUST: refused` with no next step.

**Concurrent `ensure` runs cannot collide.** Staging used fixed siblings
(`.dev-trust.tmp`, `.dev-trust.old`), so two builds sharing a trust directory
would delete each other's half-written material. Staging names now carry the pid
and a random suffix.

**SAN parsing is anchored and shared.** `_SAN_LABEL` was unanchored, so a label
occurring *inside* a value was eaten (a URI SAN `https://host/DNS:x` lost
`/DNS:x`). Parsing is now a pure `san_values()` — unit-testable without openssl —
and `missing_sans()` replaces the `try/except` that `_cmd_header` had duplicated
from `covers()`.

**Two fences are harder to fool.** The placeholder's prose no longer spells out
the markers it lacks, so a checker that greps for a marker *without* its
`#define` prefix cannot be satisfied by the file's own comment. And the invariant
fence now verifies that a header claiming presence really carries a PEM CA and a
32-byte public key, instead of inferring "placeholder" from the bare string
`#error`.

## Verification

```bash
cd communicator-esp32/host/tests && python3 -m pytest -q
# 169 passed   (158 before this branch; 11 tests added, 8 of them watched fail first)
```

## Not in this PR

Four items from the same §3.5 list remain, deliberately:

- the firmware has no runtime provenance log for `OTA_TRUST_IS_DEV` — this needs a
  `main/` C change and a real `idf.py` build to verify;
- the invariant tests remain static fences (there is no C unit harness) — a
  genuine rework, not a fix;
- the release dev-refusal is still a marker grep — widening it to compare the
  embedded material against `tools/ota-keys/` needs shell-level verification the
  host suite cannot give;
- the dev banner stays `message(STATUS)`: `main/CMakeLists.txt` documents that
  idf.py does not surface `WARNING` from a component's CMakeLists, so "make it a
  warning" would make it *invisible*. No change is the correct answer here.

Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #19
# fix(review): the last three esp32 TLS-only review minors

The esp32 half of the deferred review minors in `HANDOVER.md` §3.5 — the three
that esp32 PR #19 deliberately left behind, each for a stated reason. All three
are addressed here, and the reasons they were deferred are removed rather than
worked around.

## A. Runtime provenance for dev trust material

`OTA_TRUST_IS_DEV` existed in the header but was **never referenced by the
firmware**, so a flashed device gave no signal about which CA it pins — the one
thing a field diagnosis of a trust problem needs.

`app_main()` now logs it at boot:

```c
#if defined(OTA_TRUST_IS_DEV)
    ESP_LOGW(TAG, "trust material: DEV (throwaway CA, no production keys) - do not release this firmware");
#else
    ESP_LOGI(TAG, "trust material: production");
#endif
```

**Verified with two real `idf.py` builds**, since the deferral reason was "needs
a `main/` C change and a real `idf.py` build":

- **Production** (`tools/ota-keys/` present): configure generates production
  material, `main.c.obj` compiles, `communicator.bin` links.
- **Dev** (production keys hidden, `CONFIG_SERVER_HOST` covered by the shared
  dev set): the `DEV TRUST MATERIAL` banner fires, the header carries
  `#define OTA_TRUST_IS_DEV 1`, `main.c.obj` compiles the `ESP_LOGW` branch,
  and the image links.

Both builds left the tree restored: the tracked `main/ota_trust.h` is back to the
placeholder and the production keys are back in `tools/ota-keys/`.

## C. The release refusal now compares material, not markers

`release_firmware.sh` refused dev material with `grep -q "#define
OTA_TRUST_IS_DEV"`. A marker grep cannot tell two CAs apart: a **stale header
left from another CA would still pass** and ship a fleet that trusts the wrong
root.

The check moves into the trust tool as a real, testable subcommand:

```
starfleet_trust.py verify-header --header main/ota_trust.h --ca tools/ota-keys/ca.pem
```

It checks presence (`OTA_TRUST_PRESENT`), the absence of the dev marker, and
that the header's embedded CA — parsed out of the C string literal and
unescaped — equals `--ca`. `release_firmware.sh` calls it against
`tools/ota-keys/ca.pem`.

The deferral reason was "needs shell-level verification the host suite cannot
give". Moving the logic into Python removes that: the behaviour is now
unit-tested (`verify_header`, `embedded_ca_pem`, and the CLI exit codes), and it
was **also** verified at the shell level — a generated production header passes
(exit 0), a header checked against a different CA exits 1.

## B. The invariant fences are no longer greps-on-the-header

`host/tests/test_tls_invariant.py`'s trust-header checks were string-shape
greps. They now **compile the header with the C compiler** (`cc -std=c11 -Wall
-Werror`) and run a probe against it:

- the committed header must be the placeholder **and must fail to compile**
  (the `#error`), replacing the "either placeholder or real material" shape
  test;
- a generated production header compiles, defines `OTA_TRUST_PRESENT` and not
  `OTA_TRUST_IS_DEV`, and reports a 32-byte key and a non-empty CA;
- a generated dev header compiles and defines both markers;
- a header claiming `#define OTA_TRUST_PRESENT 1` **with no material fails to
  compile**, because nothing declares the symbols the firmware uses — the exact
  gap the old shape check could not close.

The remaining checks stay static, by necessity rather than by preference:
`ws_client.c` and `main/CMakeLists.txt` are ESP-IDF-only and cannot be compiled
by the host suite. They are now documented as what they are — complements to
the `idf.py` build gate, never replacements for it. One is added for this
change: `test_firmware_logs_dev_trust_provenance` pins that the firmware
references `OTA_TRUST_IS_DEV` and logs it.

## Review follow-up

Whole-branch review returned no Critical and no Important findings. The worthwhile
Minors were folded in as a follow-up commit: an argparse `--help` sentinel
regression this branch introduced (adding the fourth subcommand left `--help`
dumping the whole docstring), `embedded_ca_pem` hardened against a
commented-out initializer, CRLF normalisation in `verify_header`, and a stronger
item-A test that requires the guarded `#if defined(OTA_TRUST_IS_DEV)` form and both
log lines rather than bare substrings.

## Verification

```bash
cd communicator-esp32/host/tests
python3 -m pytest -q     # 180 passed   (169 before this branch; 11 tests added)
```

- `py_compile` on the changed Python, `bash -n` on the changed shell clean.
- added-line hygiene scan (no `/home/<user>`, no internal IPs) clean.
- production and dev `idf.py build` both green; working tree restored.

## Not in this PR

The on-hardware dev-trust run (HANDOVER §3.3) remains deferred: it needs the
physical unit on `/dev/ttyACM0`, a human to hear the reply, and a mandatory
re-flash back to production trust afterwards.

Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #20
Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #21
# feat(trust): generate a `roster` leaf in the dev trust set

## What this is

Part of the fleet **app-tier TLS** change (design and plan in the `starfleet`
docs repo: `2026-10-17-app-tier-tls-design.md` / `…-plan.md`). `roster` joins
`crew` as a TLS-required service, so it needs its own leaf from the fleet CA —
identical in shape to the existing `computer` and `crew` leaves.

This is the trust-material prerequisite; the `roster` transport change is a
sibling branch.

## Change

- `tools/starfleet_trust.py`: `DEV_FILES` gains `roster.pem` / `roster.key`,
  `PRIVATE_KEY_FILES` gains `roster.key`, and `LEAVES` gains
  `("roster", "starfleet-roster")`. `generate()` already iterates `LEAVES`, so
  emitting the files needs nothing further.
- `host/tests/test_starfleet_trust.py`: the pinned `DEV_FILES` assertion was
  extended, a new test asserts the roster membership, and the two tests that had
  hardcoded leaf and key lists now **iterate** `st.LEAVES` and
  `st.PRIVATE_KEY_FILES` — so the roster leaf is covered by the verify-chain and
  the 0600-permission assertions, and the next leaf addition needs no test edit.

## Verification

```bash
cd $HOME/dev/starfleet/communicator-esp32
python3 -m pytest host/tests/ -q
```

**81 passed** for the trust suites; the change touches only
`tools/starfleet_trust.py` and its test. A full `host/tests/` run needs
`websockets` and `numpy`, which the sandbox python lacks — run the full suite
where those are installed (CI). A manual check confirmed `roster.pem` is signed
by the CA, carries `CN=starfleet-roster`, and its key is mode 0600.

## Note for the operator

Adding entries to `DEV_FILES` makes any existing shared `.dev-trust/`
incomplete — a partial set is deliberately never "dev" — so the next
`ensure()` regenerates it with a **new CA**, which means a flashed dev device
must be reflashed. The design's "a new leaf does not re-key the CA" is not true
in practice; this is recorded in the app-tier TLS runbook.

## Deferred

- The repo's trust docs do not enumerate the dev-set files, so there is nothing
  to update there (verified by grep).

Co-authored-by: Troed Sångberg <github@troed.se>
Reviewed-on: #22
troed merged commit 2354077aa8 into main 2026-10-17 19:43:24 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starfleet/communicator-esp32!23
No description provided.