# Runbook: the companion PWA and Web Push The companion is the same page installed to a phone's home screen. It can read the desk and approve a capital move. Web Push is what makes it speak first: a proposal waiting, a proposal that expired, a halt raised or cleared, and a refusal that means "you cannot trade right now". Push is optional. With the VAPID variables unset the desk runs exactly as before, and the Moves page says `notifications: unavailable`. ## 1. Generate the VAPID pair once Run this on the desk host (the output is two environment lines; the private key is a secret): ```bash uv run python - <<'PY' import base64 from py_vapid import Vapid from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat v = Vapid() v.generate_keys() private = v.private_key.private_numbers().private_value.to_bytes(32, "big") public = v.public_key.public_bytes(Encoding.X962, PublicFormat.UncompressedPoint) enc = lambda raw: base64.urlsafe_b64encode(raw).rstrip(b"=").decode() print("TAPEBOOK_VAPID_PRIVATE_KEY=" + enc(private)) print("TAPEBOOK_VAPID_PUBLIC_KEY=" + enc(public)) PY ``` Put both lines in `.env` (mode `0600`). Set the subject to a mailbox the push services can contact about a dead subscription: ```bash TAPEBOOK_VAPID_SUBJECT=mailto:you@example.com ``` Generate once and keep the private key. Replacing it invalidates every existing subscription; each device must turn notifications off and on again. Never paste the private key into a ticket, a screenshot, a commit, or a log. ## 2. Apply the migration and restart ```bash uv run tape migrate docker compose up -d serve runner ``` Migration `0024_push_subscriptions.sql` creates `push_subscriptions`. The application role may `SELECT`, `INSERT`, `UPDATE` and `DELETE` it; no other table changes. Check that it is there: ```bash uv run tape doctor ``` The Moves page (`/moves`, the PWA start page) now shows the notifications row. ## 3. Turn it on for one device 1. Open `/moves` on the phone, in the installed app if possible. 2. Press **Turn on for this device**. This is the only moment the browser asks for permission. 3. The row changes to `on (this device)`. 4. Confirm the row was written: ```sql SELECT endpoint, user_agent, created_at, last_success_at, failure_count FROM push_subscriptions; ``` `failure_count` is the health signal. A successful delivery resets it to zero and sets `last_success_at`. A growing count with an old `last_success_at` means the phone is not receiving; the desk does not retry, so the next event is the next attempt. ### iOS Web Push in Safari requires iOS/iPadOS 16.4 or later **and** the page added to the Home Screen (Share → Add to Home Screen). It does not work in a normal Safari tab. The row will report `off` or an error if the browser cannot subscribe. ## 4. Turn it off, or forget a dead device Press **Turn off for this device** in the app. The browser unsubscribes and the API deletes the row. If the phone is lost or broken: ```bash curl -X POST http://127.0.0.1:8000/api/push/unsubscribe \ -H 'Content-Type: application/json' \ -d '{"endpoint":"https://…"}' ``` The full endpoint is in `push_subscriptions.endpoint`. It is a capability URL — do not paste it into a chat or an issue. A `404` or `410` from the push service deletes the row automatically. ## 5. What the notification says | event | title | body example | |---|---|---| | proposal | `A move needs approval` | `momentum · buy INFY · expires in 10 min` | | expired | `A proposal expired` | `momentum · buy INFY expired unapproved; nothing moved` | | halt | `Sleeve halted` | `momentum · book and broker disagree on INFY` | | cleared | `Halt cleared` | `momentum · trading resumes on the next due cycle` | | refusal | `Trading refused` | `momentum · the venue rejected the order` | Tapping opens `/moves#proposal-…` or `/sleeve/?tab=chat`. The payload deliberately carries no amount, balance, price, account number or token: a lock screen is public. If a notification body looks wrong, the source sentence is in the proposal, halt or alert record; the payload is a summary, never the record. ## 6. Troubleshooting | symptom | likely cause | action | |---|---|---| | row says `unavailable` | VAPID variables unset or partial | set all three in the API's environment and restart `serve` | | button does nothing, detail says permission denied | browser blocked the prompt | site settings → notifications → allow, then press the button again | | `off (this device)` after pressing | no service worker or no `PushManager` | HTTPS (or localhost) is required; iOS needs Add to Home Screen | | no push, `failure_count` climbing | push service unreachable or keys rotated | check the API log line's endpoint host; turn the device off/on to re-subscribe | | expiry push missing | no `serve` process running the sweep, or push was configured after the proposal lapsed | push expiry is swept every 30 s by `serve`; restart `serve` after setting VAPID | | halt push missing | the halt was raised by the CLI while no runner cycle has seen it | the runner announces a standing halt on its next pass (this is the existing alert path) | A refused action that happened inside the runner's execution path (venue/egress at placement) is in the runner log and the `TAPEBOOK_ALERT_WEBHOOK` event, not in Web Push: the API never executes. See ADR-0043 for that boundary.