fix/connection-events #21

Merged
troed merged 6 commits from fix/connection-events into devel 2026-10-16 13:11:16 +02:00
Owner
No description provided.
The esp32 half of HANDOVER §3.5 that PR #19 deliberately left: the runtime
dev-trust provenance log, the grep-shaped invariant fences, and the narrow
release dev-refusal.

Runtime provenance (A). OTA_TRUST_IS_DEV was never referenced by the firmware,
so a flashed device gave no signal about which CA it pins. app_main() now logs
it: ESP_LOGW "trust material: DEV ..." when the header is dev-marked,
ESP_LOGI "trust material: production" otherwise. Verified with two real idf.py
builds -- production (tools/ota-keys/ present) and dev (keys hidden, dev set
generated, DEV TRUST MATERIAL banner) -- both compile main.c.

Release refusal widened (C). release_firmware.sh refused dev material with a
marker grep, which cannot tell two CAs apart: a stale header left from another
CA would still pass. The check moves into starfleet_trust.py as the testable
`verify-header` subcommand (presence, no dev marker, and the embedded CA must
equal --ca), and the script calls it against tools/ota-keys/ca.pem. Verified at
the shell level: a generated production header passes, a wrong CA exits 1.

Invariant fences reworked (B). 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: the committed placeholder must fail
to compile, a generated production/dev header must compile and report the right
markers plus a 32-byte key and a non-empty CA, and a header claiming
OTA_TRUST_PRESENT with no material must fail to compile. The remaining checks
stay static by necessity -- ws_client.c and the CMakeLists are ESP-IDF-only --
and are documented as complements, never replacements, for the idf.py build gate.

Verification: esp32 host suite 169 -> 177 passed (8 tests added); py_compile and
the added-line hygiene scan clean; both idf.py builds green.
Whole-branch review found no Critical and no Important items; these are the
worthwhile Minors, one of which was a regression this branch introduced.

- The argparse description sentinel still said "Three subcommands:", so after
  adding a fourth `--help` dumped the whole module docstring. It now splits on
  "Four subcommands:", and a test pins the short description.
- `embedded_ca_pem` now runs over comment-stripped text and anchors the
  initializer to a line start, so a commented-out `ota_trust_ca_pem[]` cannot
  satisfy it (defence in depth: the release path regenerates the header first).
- `verify_header` normalises CRLF in the expected CA file, so a Windows-saved
  `ca.pem` is not falsely refused.
- `test_firmware_logs_dev_trust_provenance` asserts the guarded form and both
  log lines, so naming the macro in a comment can no longer pass it.

180 passed (177 before); py_compile clean.
Field symptom: the wake word works, then a new chime followed by the LEDs
turning off. Two defects, both in how a lost connection reaches the state
machine.

1. ws_client posted a state-machine event straight from WEBSOCKET_EVENT_ERROR.
   That event exists to unblock a pending connect(); its side effect was a
   second "the session failed" report on top of the DISCONNECTED one, so a
   mid-conversation reconnect was started and then torn down by the stale
   second event -- in the same millisecond, on hardware.
2. A reconnect replayed the wake chime, the "I heard you, speak now" cue, so the
   user was told to speak exactly as the session was being rebuilt.

Changes:
- components/app_fsm: the connection decision table as a pure function, with
  connection ids. Lifecycle events (CONNECTED/FAILED/CLOSED) from a superseded
  connection are ignored. AUDIO/DONE/DRAINED are deliberately NOT scoped:
  DRAINED is posted by the player and the others arrive on the live connection,
  so an id mismatch can never drop a real event.
- ws_client: allocates the connection id (ws_client_conn_id()), posts it with the
  lifecycle events, and no longer reports WEBSOCKET_EVENT_ERROR to the state
  machine at all.
- app_state: decisions delegate to app_fsm_step(); the chime plays on a fresh
  wake only; the retry budget belongs to app_fsm (a fresh wake restores it, a
  successful reconnect does not), so one conversation spends at most one
  reconnect.
- host: app_fsm_host target, ctypes fixture and test_app_fsm.py (11 tests). The
  regression test is the one that fails with the epoch guard removed.

Verified: host FSM tests 11/11 green, and the mutation check fails exactly the
two stale-event tests; in-sandbox idf.py build clean (BUILD_EXIT=0). NOT yet
verified on hardware; the rest of the host suite needs its venv (no PyPI egress
in this sandbox, and /tmp caches are wiped by a recreate).

Context: docs/esp32-uplink-stall-findings.md -- the stall that triggers a
connection loss at all is a separate, still-open device-side issue.
lwIP's initial RTO is ~1 s and ws_client's send timeout was exactly 1000 ms, so
one lost segment failed the write - and esp_websocket_client answers a failed
write by aborting the connection, which ended the conversation. The buffers were
sized the same way: a 3.2 KB PCM chunk encodes to a ~4.4 KB JSON frame every
~100 ms against a 5760 B send buffer, and TTS deltas arrive in similar frames
against a 5760 B receive window, so barely one frame could be in flight.

- ws_client: SEND_TIMEOUT_MS 3000, which rides out the initial RTO plus one
  retransmission. The wait costs nothing: audio in flight is stale by then
  anyway and frames are dropped rather than queued.
- sdkconfig.defaults: CONFIG_LWIP_TCP_SND_BUF_DEFAULT and _WND_DEFAULT 5760 ->
  16384, and CONFIG_ESP_WS_CLIENT_SEPARATE_TX_LOCK=y so a stalled send cannot
  also block the receive path - the shared lock was what coupled them, and
  without this a longer timeout would have made that worse. (sdkconfig is
  gitignored and regenerated from these defaults; it was updated in place for
  this build too.)

Verified: idf.py build clean (BUILD_EXIT=0), the new values present in the built
config, production trust unchanged, app_fsm host tests still green. Not verified
on hardware.

Context: docs/esp32-uplink-stall-findings.md. This helps the stalls that do
surface as a write error; the capture shows some produce none at all.
troed merged commit ba0870a64b into devel 2026-10-16 13:11:16 +02:00
troed deleted branch fix/connection-events 2026-10-16 13:11:16 +02:00
troed referenced this pull request from a commit 2026-10-16 13:11:16 +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!21
No description provided.