Files
hermeven/docs/architecture.md
DanandClaude Fable 5.2 1507cea077 Address Cody review round 3: CODY-003 and CODY-006
CODY-003 Request identity is durable before submission. Storage results are
         explicit at the DevicePort boundary (storageSet resolves true only
         on the host's own timely confirmation; storageGet distinguishes
         unknown from empty). A request is not sent until the phone has
         confirmed storing its id; a timeout or refusal holds it back,
         unsent and retryable under the same id. Unreadable storage at
         start is not treated as unpaired; an unsaveable pairing is revoked.
CODY-006 The state directory is set to 0700 on every open, including when
         it already existed, and the bridge refuses to start with an
         operator-facing error if it cannot be. Deployment guide creates
         it with install -d -m 0700.

Checks: typecheck clean; npm test 194 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.

Not reviewed. No physical-device, droplet or production evidence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 20:29:05 -08:00

107 lines
5.0 KiB
Markdown

# Architecture
```
G2 mic / touch / HUD
| Bluetooth (Even's protocol, not ours)
Even phone app -- WebView running apps/even-client (public Even Hub SDK)
| HTTPS, device credential, polling
services/bridge (Fastify + SQLite)
|-- STT adapter --> built-in Whisper (in process), or an external endpoint
|-- Hermes adapter --> Hermes API server (Runs API) for one configured profile
```
## Who owns what
| Concern | Owner |
|---|---|
| Agent identity, memory, transcript, model, tools, approval policy | Hermes |
| Device authorisation, request deduplication, turn lifecycle, deadlines | Bridge |
| Microphone, gestures, HUD, app lifecycle | Even runtime on the phone |
| Starting, cancelling, resetting, approving | The user |
The bridge stores references and a short-lived result cache, not a transcript. Question and
answer text is erased from the bridge 15 minutes after a turn ends (configurable).
## Packages
| Path | Role |
|---|---|
| `packages/contracts` | Contract v1: JSON Schemas (TypeBox), types, stable error codes. Imported by both sides. |
| `services/bridge` | HTTP API, pairing and credentials, turn engine, Hermes and STT adapters, operator CLI. |
| `apps/even-client` | Pure state reducer, HUD renderer, pagination, Even adapter, bridge client, phone page. |
| `tests/fakes` | Labelled fakes: Hermes Runs API, STT endpoint, glasses, stub model. |
| `tests/integration` | Bridge over HTTP, and client-to-bridge end to end, against the fakes. |
| `tests/simulator` | The real client in the official simulator. |
| `tests/real-hermes` | The bridge against a real Hermes gateway with a stub model. |
## Client
`core/reducer.ts` is a pure function `(state, event) -> (state, effects)`. It has no clock, no
network and no randomness: request ids arrive inside events and work leaves as effects.
```
READY -> LISTENING -> TRANSCRIBING -> THINKING -> ANSWER
plus APPROVAL, ERROR, DISCONNECTED, CANCELLED, UNPAIRED
```
Every user action that starts, cancels or abandons something increments a **generation**
counter. Each asynchronous effect carries the generation it started under, and the reducer drops
any event whose generation is not current or whose request id is not the current one. That is the
single mechanism behind "a late result must not replace a newer answer or resurrect a cancelled
screen".
`core/controller.ts` runs the effects: microphone capture, submit with retry, polling, cancel,
conversation creation, persistence. `device/port.ts` is the seam to the glasses; the Even adapter
and the test fake both implement it.
## Bridge turn lifecycle
```
accepted -> transcribing -> thinking -> done
|-> approval_required -> thinking
|-> cancelling -> done
```
`done` always carries an outcome: `succeeded`, `incomplete`, `failed`, `cancelled` or `unknown`.
Order of operations for one turn:
0. On the phone, before any of this: the app stores the request id in app storage and waits
for the host to confirm the write. No confirmation, no request.
1. Validate, authenticate, check the conversation belongs to the device and is open, and that no
other turn is running in it. Insert the turn row keyed by the client's request id. A repeat of
the same request id returns the existing turn.
2. Audio only: wrap the PCM as WAV in memory, transcribe, drop the audio.
3. Persist the exact Hermes request body, then send it with `Idempotency-Key`.
4. Persist the returned run id and erase the stored body.
5. Poll the run until it is terminal, a deadline passes, or a cancel is requested.
Why the order matters: after step 3 a crash can always be recovered by re-sending the identical
body under the same key (Hermes returns the original run), and before step 3 nothing has reached
Hermes, so failing the turn is provably safe.
## How ambiguous situations are classified
| Situation | Outcome | Retryable |
|---|---|---|
| STT error, timeout or empty transcript | failed | yes: nothing reached Hermes |
| Bridge restarted before step 3 | failed (`interrupted_before_agent`) | yes |
| Connection to Hermes refused on every attempt | failed (`agent_unreachable`) | yes |
| Hermes answered 4xx | failed (`agent_rejected`) | no |
| Request sent, no usable reply, deadline passed | **unknown** | no |
| Hermes reports `unknown` or no longer knows the run | **unknown** | no |
| Deadline passed, stop requested, Hermes confirmed | cancelled (`agent_timeout`) | no |
| Stop requested, not confirmed within the grace period | **unknown** | no |
| Cancel requested but the run completed first | succeeded, `cancel_requested: true` | n/a |
"Retryable" means a deliberate new question by the user is safe. Nothing is ever replayed
automatically under a new request id.
## Deliberate limits of this slice
- One process, one profile, one active conversation per device.
- Polling only; no streaming.
- A second question while one is running in the same conversation is refused
(`conversation_busy`), not queued.
- Capture ends by tap or cap. There is no end-of-speech detection.