Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions .github/workflows/pm-console-edge-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
name: pm-console-edge deploy

# CI tier policy: see .github/ci-policy.md. This is a deploy, not CI,
# so it keeps a push trigger (restricted to main).
#
# Deploys the Worker that serves the PM app from the edge
# (pm-stack/console/edge): every push to main that touches the console,
# or a manual run, runs the console's tests (which drive the Worker's
# routing and state against the host's real handler), bundles the
# Worker, and uploads it with the CLOUDFLARE_API_TOKEN and
# CLOUDFLARE_ACCOUNT_ID repository secrets, the ones message-bridge
# deploys with.
#
# The Worker has no secrets: the keys it uses it makes itself, or are
# published by the host. It has no route here either. The host's
# pm-stack/console/scripts/expose.py sends the hostname through it once
# it answers its health check, and `expose.py --edge off` takes the
# hostname off it again, which is the rollback that needs no deploy. A
# manual run with rollback_version puts an earlier version back.
#
# Until both secrets exist the job says so in its summary and skips the
# upload instead of failing.
on:
push:
branches: [main]
paths:
- "pm-stack/console/**"
- "!pm-stack/console/android/**"
- "pm-stack/aggregator/src/assets/icons/**"
- ".github/workflows/pm-console-edge-deploy.yml"
workflow_dispatch:
inputs:
rollback_version:
description: "Existing Worker version id to put back instead of uploading; empty deploys this commit"
required: false
default: ""

permissions:
contents: read

# Deploys run one at a time, in order; a newer push never cancels one
# that is uploading.
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
deploy:
name: deploy pm-console-edge
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Check deploy secrets
id: secrets
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
if [ -n "$CLOUDFLARE_API_TOKEN" ] && [ -n "$CLOUDFLARE_ACCOUNT_ID" ]; then
echo "ready=true" >> "$GITHUB_OUTPUT"
else
echo "ready=false" >> "$GITHUB_OUTPUT"
echo "Skipped the upload: set the CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID repository secrets to deploy pm-console-edge from the repo." >> "$GITHUB_STEP_SUMMARY"
fi
- uses: actions/checkout@v7
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
- uses: actions/setup-node@v7
with:
node-version: 24
- name: Test the console and its edge
working-directory: pm-stack/console
run: bun test
- name: Install the deploy tool
working-directory: pm-stack/console/edge
run: npm ci --no-audit --no-fund
- name: Bundle the Worker
working-directory: pm-stack/console/edge
run: |
node build.mjs
npx wrangler deploy --dry-run --outdir .wrangler/dry
- name: Deploy
if: steps.secrets.outputs.ready == 'true'
working-directory: pm-stack/console/edge
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
ROLLBACK_VERSION: ${{ inputs.rollback_version }}
run: |
# The input goes through the env, never into the script text,
# and must look like a version id before it is used.
if [ -n "${ROLLBACK_VERSION:-}" ]; then
if ! printf '%s' "$ROLLBACK_VERSION" | grep -Eq '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'; then
echo "rollback_version is not a version id" >&2
exit 2
fi
npx wrangler rollback "$ROLLBACK_VERSION" --yes --message "rollback from workflow run ${GITHUB_RUN_ID}"
echo "Put pm-console-edge back to version ${ROLLBACK_VERSION}." >> "$GITHUB_STEP_SUMMARY"
else
npx wrangler deploy --var "VERSION:${GITHUB_SHA::12}" --message "git ${GITHUB_SHA}"
echo "Deployed pm-console-edge from ${GITHUB_SHA}." >> "$GITHUB_STEP_SUMMARY"
fi
- name: Check the deployed Worker answers
if: steps.secrets.outputs.ready == 'true' && inputs.rollback_version == ''
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
# The workers.dev address answers the health check and the
# public files only. Its subdomain is the account's.
sub="$(curl -fsS -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
"https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/workers/subdomain" \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["result"]["subdomain"])')"
url="https://pm-console-edge.${sub}.workers.dev/console/health"
want="${GITHUB_SHA::12}"
for attempt in $(seq 1 20); do
got="$(curl -fsS --max-time 10 "$url" | python3 -c 'import json,sys; d=json.load(sys.stdin); print(d.get("version") if d.get("ok") and d.get("edge") else "")' 2>/dev/null || true)"
if [ "$got" = "$want" ]; then
echo "pm-console-edge answers as ${want}." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
sleep 6
done
echo "the deployed Worker did not answer its health check as ${want} (last answer: ${got:-none})" >&2
exit 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# PM console served from the edge

Status at writing: built and tested, about to be deployed. Later
sections record what happened when it went live.

## What was asked

The owner, 2026-11-21 23:58 CST: the app's pages "still don't load
correctly", and "making everything work inside of the PM stack from
Cloudflare without requiring the laptop is completely within your
purview". Then, 2026-11-22 04:32 CST: "continue fully implementing this
system and relying on cloudflare instead of laptop for operations where
possible".

## What was wrong

The app (`pm-stack/console`) ran only on the laptop, behind a Cloudflare
tunnel. Measured on 2026-11-22:

- The laptop's uplink dropped every tunnel connection at 04:58:53Z and
flapped until 05:14 (evidence on #12414). During that time nothing at
`pm.security.cab` loaded at all.
- The laptop ran at load average 47 to 75 on 16 cores while the fleet
compiled, which starved the management services (`CPUWeight=900` was
set on them in #12452).

A service on one laptop is as available as that laptop's uplink and
processor. The app should not be.

## Decision

Put a Cloudflare Worker in front of the hostname and serve the app from
it. The laptop stays the machine with the fleet's tools, and becomes a
worker behind the edge.

1. **The edge serves** the app's files, its state (from a copy the
laptop reports), notifications (keys, subscriptions, sending) and the
linked-phone summary.
2. **The laptop reports** the sections of state that changed, signed
with its own key, and says how current each unchanged section is.
3. **An action that needs the laptop is relayed at once** when it is in
reach. When it is not, an action that is safe to do late is held at
the edge and run once when the laptop is back; one that is not safe
(a merge, a unit operation, a change to a task's state) is refused.
4. **The edge never refuses on its own.** It answers when it is sure of
the owner; otherwise it passes the request to the laptop. With the
laptop out of reach an unsure caller gets a plain notice and nothing
else.
5. **Deploys run from GitHub Actions**; the laptop's `expose.py` only
decides whether the hostname goes through the Worker, and can take it
off again.

## Why this shape

| Choice | Instead of | Reason |
| ----------------------------------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Laptop pushes state to the edge | Edge pulls from the laptop | A push needs the edge to recognise the laptop (a public key). A pull needs the laptop to recognise the edge (a secret kept at the edge). |
| The laptop's key, fetched through the tunnel | A shared secret in Bitwarden and in the Worker | Nothing to store, rotate or leak. Only the laptop can answer through its tunnel. |
| Edge config (owner, issuer, audience) arrives in the report | Worker variables | One place it is set, and nothing private in a public repository. |
| Serve from the copy, tell the laptop someone is looking | Ask the laptop first and fall back | The copy answers in milliseconds whatever the laptop's load; asking first makes every read as slow as the laptop. |
| Held actions limited to a named list | Hold everything | A merge or a unit operation acts on what the owner saw. Done hours later it may act on something else. |
| At most once for a held action | At least once | A second prompt or a second comment is worse than one reported as interrupted. |
| Defer to the laptop when unsure | Refuse at the edge | A fault in the edge's check must not lock the owner out. The laptop's check is the one already proven. |
| One Durable Object | KV or D1 | One writer, small state, an alarm for the silent-laptop check, and consistent reads straight after writes. |
| `wrangler deploy` from CI | The `cf` versions flow under Nix used by message-bridge | The Worker has no secrets, so a code-only deploy is the whole deploy, and it could be run and checked on the build machine before it was written into CI. Moving to the `cf` flow is a packaging change with no change of behaviour. |

## What the laptop trusts

The laptop used to act only for a caller with a sign-in token it had
verified. It now also runs the held actions in the answer to its own
report. That answer comes over TLS from the hostname's Worker, in the
owner's Cloudflare account, which verified the owner's token when the
action was taken. An attacker who controls that account already controls
the sign-in in front of the laptop, so this adds no one to the set of
parties the laptop relies on. The laptop still limits what it will run
late to the named list, runs each at most once, and records each with
who took it and when.

## What this does not do

- With the laptop off, state does not go on changing at the edge: it is
the last report, with its age shown. Reading the queue store and
GitHub straight from the edge would fix that for the queue and for
prompts; it needs those two credentials at the edge and is a separate
step.
- Questions and their answers still live in the laptop's decision store.
An answer given with the laptop away is held, not applied.
- The dashboard's own pages are the laptop's. With it away they are a
notice with a link to the app.

## How it was checked before going live

- `bun test` in `pm-stack/console`: the Worker's real routing and state,
with the laptop's real handler behind them and the laptop's real link
reporting through the same routing. Stand-ins only at the outside
(storage, the sign-in issuer's key, the push service).
- A mutation check: 37 planted faults in the edge, the routing, the link
and the protocol. 36 were caught. The one left (dropping a length
check on a signature) changes no behaviour: verification refuses a
short signature anyway.
- One of those faults found a real defect: a section that had not
changed looked stale at the edge while the laptop was reporting
normally, because only changed sections carried a time. The report now
says how current every section is.
- The Worker in the real runtime (`wrangler dev`) against a local host:
17 checks, including the signed report, the key fetch, a 2 MB section
kept in pieces, and the host going away.
- The app driven in Chrome through the edge code: open, send a prompt
with the host up, take the host down, send another (held), bring it
back (sent once, shown as sent late).

Not checked before going live: the owner's real sign-in token at the
edge. The edge counts requests it answered as the owner's and requests
it left to the laptop (`/console/health`), so the first real visit shows
which happened.
Loading