Skip to content

feat(pm-stack): serve the PM app from the edge; the laptop becomes a worker behind it - #12505

Merged
dezren39 merged 2 commits into
mainfrom
claude/pm-edge
Nov 22, 2026
Merged

dezren39 merged 2 commits into
mainfrom
claude/pm-edge

Conversation

@dezren39

@dezren39 dezren39 commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

What

The PM app is served from Cloudflare. The laptop becomes a worker behind it.

Why. The app ran only on the laptop, behind a tunnel. On 2026-11-22 the laptop's uplink dropped every tunnel connection for a quarter hour (#12414) and nothing at pm.security.cab loaded; under the fleet's compile load the pages were slow at the best of times.

Now. A Worker (pm-stack/console/edge) sits in front of the hostname.

Thing Where When the laptop cannot be reached
The app's page, script, icons edge unchanged
State (in progress, next, needs me, prompts, …) edge, from the laptop's signed report served as of the last report, with its age shown
Notifications; linked phones and the widget edge unchanged; one notification says the laptop has gone quiet
Actions safe to do late (prompts, answers, comments, rank, move) laptop, relayed at once held, run at most once when it is back, dropped after a day and said so
Merge, unit operations, unblock, cancel laptop, relayed at once refused, saying why
The dashboard's own pages laptop a plain notice with a link to the app
  • No shared secret. The laptop signs its reports with a key made on the laptop; the edge learns the public half by asking the laptop through the tunnel. The Worker has no secrets configured.
  • The edge never refuses on its own. It answers when it is sure of the owner and otherwise passes the request to the laptop. If the Worker throws, or has not heard from the laptop yet, requests go to the laptop as before.
  • Deploys from GitHub Actions (pm-console-edge-deploy.yml, the Cloudflare secrets message-bridge already uses). The Worker has no route in its config: expose.py sends the hostname through it once it answers its health check.
  • Merged console code puts itself into service on the laptop (redeploy.sh, every minute) after a self-check; code that does not start is left out.

Design record: doc/2026-11-22T11-10-00Z_architecture_pm_console_edge/.

Evidence

Check Result
cd pm-stack/console && bun test 98 pass. 18 drive 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
python3 -m unittest discover -s pm-stack/console/scripts 12 pass (route added last, only once the Worker answers; --edge off remembered; another Worker's route never touched)
Mutation check 37 planted faults, 36 caught. The one left (dropping a signature length check) changes no behaviour
A fault the mutation check exposed an unchanged section looked stale at the edge while the laptop was reporting normally; the report now says how current every section is
The Worker in the real runtime (wrangler dev) against a local host 17 checks pass: signed report, key fetch, a 2 MB section kept in pieces, unsigned report refused, host gone
The app in Chrome through the edge code opens; prompt sent with host up; host down, prompt held and shown as waiting; host back, sent once and shown as sent late; 0 page errors

Not verified before merge: the owner's real sign-in token at the edge. The health check counts requests the edge answered as the owner's and requests it left to the laptop, so the first real visit shows which happened.

Going back

python3 pm-stack/console/scripts/expose.py --edge off     # hostname straight to the laptop; remembered
gh workflow run pm-console-edge-deploy.yml -f rollback_version=<id>   # an earlier Worker version

or revert this PR.


Generated by Claude Code


Run Proof on this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…worker behind it

The app ran only on the laptop, behind a tunnel, so it was as available
as the laptop's uplink and processor: on 2026-11-22 the uplink dropped
every tunnel connection for a quarter hour (#12414) and nothing loaded.

A Cloudflare Worker (pm-stack/console/edge) now sits in front of the
hostname and serves the app, its state, notifications and the phone
widget from Cloudflare.

- State is a copy the laptop reports (only the sections that changed,
  signed with a key made on the laptop; the edge learns the public half
  by asking the laptop through the tunnel, so there is no shared
  secret). The app shows how old the copy is.
- An action that needs the laptop is relayed at once when it is in
  reach. Taken while it is not, one that is safe to do late (a prompt,
  an answer, a comment) is held and run at most once when the laptop is
  back; a merge, a unit operation or a task state change is refused.
- The edge answers only when it is sure of the owner and otherwise
  passes the request to the laptop, so a fault in its check cannot lock
  the owner out. If the Worker throws, or has not heard from the laptop
  yet, requests go to the laptop as before.
- Notifications are sent from the edge, and one says when the laptop
  has gone quiet for ten minutes, and when it is back.
- Deploys run from GitHub Actions with the Cloudflare secrets
  message-bridge already uses. The Worker has no secrets of its own and
  no route in its config: expose.py sends the hostname through it once
  it answers its health check, and `expose.py --edge off` takes it off.
- Merged console code now puts itself into service on the laptop
  (redeploy.sh, every minute), after a self-check that listens on
  nothing; code that does not start is left out.

The classes that keep state (notifications, linked phones) take a Store
instead of a file path, so the same code runs in both places.

Tests: 98 bun tests (18 drive the Worker's real routing and state with
the laptop's real handler behind them; 6 run redeploy.sh against a
checkout made for the test), 12 python tests for expose.py. 36 of 37
planted faults caught; the one left changes no behaviour. The Worker
was also run in the real runtime (wrangler dev) against a local host,
17 checks, and the app was driven in Chrome through the edge code with
the host up, down and back.

Design record: doc/2026-11-22T11-10-00Z_architecture_pm_console_edge.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF
@dezren39
dezren39 requested a review from a team as a code owner November 22, 2026 11:15
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 35 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 050ba4d1-c40a-492b-9789-9a88c53282ff
Commits

Reviewing files that changed from the base of the PR and between a968bf0 and 87d57c4.

Files ignored due to path filters: 1

  • pm-stack/console/edge/package-lock.json is excluded by !**/package-lock.json
Files selected for processing: 42

  • .github/workflows/pm-console-edge-deploy.yml
  • doc/2026-11-22T11-10-00Z_architecture_pm_console_edge/2026-11-22T11-10-00Z_architecture_pm_console_edge.md
  • doc/fleet-schedule-registry.md
  • pm-stack/console/README.md
  • pm-stack/console/edge/.gitignore
  • pm-stack/console/edge/README.md
  • pm-stack/console/edge/build.mjs
  • pm-stack/console/edge/package.json
  • pm-stack/console/edge/src/edge.ts
  • pm-stack/console/edge/src/entry.ts
  • pm-stack/console/edge/src/worker.ts
  • pm-stack/console/edge/wrangler.jsonc
  • pm-stack/console/install.sh
  • pm-stack/console/public/app.js
  • pm-stack/console/redeploy.sh
  • pm-stack/console/scripts/demo-state.ts
  • pm-stack/console/scripts/demo.ts
  • pm-stack/console/scripts/edge-demo.ts
  • pm-stack/console/scripts/edge-smoke.ts
  • pm-stack/console/scripts/expose.py
  • pm-stack/console/scripts/test_expose.py
  • pm-stack/console/src/actions.ts
  • pm-stack/console/src/config.ts
  • pm-stack/console/src/devices.ts
  • pm-stack/console/src/docs.ts
  • pm-stack/console/src/edge-proto.ts
  • pm-stack/console/src/errors.ts
  • pm-stack/console/src/file-store.ts
  • pm-stack/console/src/http.ts
  • pm-stack/console/src/notify.ts
  • pm-stack/console/src/ops.ts
  • pm-stack/console/src/push.ts
  • pm-stack/console/src/server.ts
  • pm-stack/console/src/store.ts
  • pm-stack/console/src/sync.ts
  • pm-stack/console/test/devices.test.ts
  • pm-stack/console/test/edge.test.ts
  • pm-stack/console/test/push.test.ts
  • pm-stack/console/test/redeploy.test.ts
  • pm-stack/console/test/support.ts
  • pm-stack/systemd/pm-console-redeploy.service
  • pm-stack/systemd/pm-console-redeploy.timer
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T11:23:30.256287Z b7b881d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…nd the edge's alarm

Same pass as the code that adds them (#12505): the laptop timer that
puts merged console code into service, the console's report to the
edge, and the Durable Object alarm that notices a silent laptop.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF
@dezren39
dezren39 merged commit ad4e696 into main Nov 22, 2026
2 checks passed
@dezren39
dezren39 deleted the claude/pm-edge branch November 22, 2026 11:23

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b7b881dc37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +145 to +146
const answer = await io.host(withHeader(forHost, "x-pm-edge", "1"), ACT_MS);
if (answer === null) return tell("/internal/hold", detail);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Deduplicate actions before relaying or holding them

If the laptop executes a relayed action but the connection drops before its response arrives, host() returns null and this branch queues the action for execution again. For non-idempotent actions such as ask.send or issue.comment, this creates duplicate prompts or comments when the next sync runs. The immediate request and held command receive unrelated IDs, so EdgeLink.take() cannot recognize the original execution. Assign and persist a shared deduplication ID before relaying, or avoid automatically retrying an ambiguously completed request.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: an action is named at the edge before it is relayed (x-pm-act), the laptop writes the name down before starting and the outcome after, and a held action offered under a name it already knows is answered from that record instead of being run. One still running when the edge gave up is left to finish. Tests cover the answer lost after the work and the edge giving up mid-work.


Generated by Claude Code

Comment on lines +175 to +176
this.push = new Push(
kvStore(this.kv, "push"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Migrate existing notification and phone state to the edge

When this is deployed over an existing console, the new Durable Object starts with empty push and devices stores, while the existing subscriptions, VAPID key and phone-token hashes remain only in the laptop's files; the sync protocol does not transfer them. Existing browsers consequently stop receiving notifications even though viewNotify() still reports them as enabled based on their browser subscription. Existing widgets receive unlinked: true, which android/.../Fleet.java handles by forgetting their token. Import the existing state before switching these endpoints to the edge, or implement an explicit client recovery flow.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half real. Notifications: fixed in #12511 from the client side, with no key moved between machines. On opening, the app checks whether the sender still holds this device and under which key, and re-makes or re-registers the subscription without asking. Until a device next opens the app the laptop goes on sending to it, since its own notifier and store are untouched. Phones: none has been linked yet (the build with the widget had not been handed out when the edge went in), so nothing was lost; carrying linked phones across an edge on/off change is tracked as a follow-up.


Generated by Claude Code

Comment on lines +5 to +7
OnBootSec=2min
OnUnitActiveSec=1min
AccuracySec=10s

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Register the new laptop redeploy timer

The new every-minute timer is enabled by pm-stack/console/install.sh, but a search of doc/fleet-schedule-registry.md finds only the existing expose timer, not pm-console-redeploy.timer. This introduces an active laptop schedule outside the unified inventory. Add its cadence, service and operating context to the registry in this change, as required by the fleet-schedules rule in AGENTS.md.

AGENTS.md reference: AGENTS.md:L56-L61

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already in the merged change: doc/fleet-schedule-registry.md, table 6, row pm-console-redeploy.timer (with its cadence, service and context), plus rows for the report to the edge and the silent-host alarm. The review ran on an earlier commit of the branch.


Generated by Claude Code


const here = dirname(fileURLToPath(import.meta.url));
const dist = join(here, "dist");
rmSync(dist, { recursive: true, force: true });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject unexpected arguments before rebuilding assets

The new build entry point never validates process.argv, so node build.mjs --dry-run, a misspelled flag or a stray positional argument reaches this deletion and rebuilds the output instead of failing before side effects. The new scripts/edge-smoke.ts similarly ignores arguments before opening a listener and issuing requests. Declare each command's supported arguments and reject everything else before performing work; the operator-script contract in AGENTS.md explicitly requires this for PM-stack scripts.

AGENTS.md reference: AGENTS.md:L44-L48

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #12511: build.mjs and edge-smoke.ts refuse any argument before doing anything (exit 2 with a usage line).


Generated by Claude Code

while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
'' | '#'* | ';'*) continue ;;
*=*) settings+=("$line") ;;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Decode EnvironmentFile quoting before the self-check

For a supported service setting such as PM_CONSOLE_PORT="4872", this preserves the quote characters when constructing the self-check environment, unlike systemd's EnvironmentFile parsing. env --help specifies that it sets each NAME to VALUE, so it does not remove those quotes; readConfig() then rejects the value as a nondecimal port. The failed self-check also caches the code tree in deploy-failed, preventing subsequent automatic deployment of otherwise valid code. Parse the settings with semantics matching the service; install.sh already explicitly supports quoted port values.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: redeploy.sh strips one pair of quotes around a value, as systemd and install.sh do; the test now uses PM_CONSOLE_PORT="4872", a single-quoted value with a space, and an unquoted value containing quotes.


Generated by Claude Code

for (const [name, s] of this.sections) {
if (s.goodAt === 0) continue;
const hash = createHash("sha1").update(JSON.stringify(s.data)).digest("base64url");
out.push({ name, at: s.goodAt, hash, data: s.data });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve loader errors when mirroring host sections

If a laptop loader fails after its first successful read, Docs.refresh() retains the old data but records ok: false and the source error. This snapshot drops both health fields, and the edge treats the retained mirror as a successful Stamped load, reporting ok: true with no error. Consequently, the existing source-failure notices rendered by public/app.js disappear when the app is served from the edge, even while GitHub or queue reads continue failing. Include section health in the protocol and propagate health changes even when the data hash is unchanged.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: the report says of every section whether its latest read failed and why, including when the data (and so its fingerprint) has not changed; the edge shows that with the last good copy and remembers it across its own restarts.


Generated by Claude Code

console = console_origin(env)
# Through the edge only if that is wanted and the Worker is really
# there; otherwise straight to this host, as before the edge existed.
edge = edge_choice(env.parent, None if args.check else args.edge)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply the requested edge choice during check mode

On an otherwise reconciled edge-on installation, expose.py --check --edge off discards the explicit off argument here and evaluates the existing on state instead. It therefore reports success without proposing removal of either PM_CONSOLE_EDGE_URL or the Worker route, although running the same command without --check changes both. Separate evaluation of the requested override from persistence of the marker so check mode accurately previews the requested operation without writing files.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: a choice given with --check is evaluated and nothing is written. Tested both ways (on→off and off→on), including that the marker file, the route and the environment are untouched afterwards.


Generated by Claude Code

ensure_dns(cf, args.host, tunnel, zone, dns_needed)
# Last: by now the host is up, knows where the edge is, and is
# reachable behind the hostname the Worker will sit on.
ensure_edge_route(cf, zone, args.host, edge)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the Worker's hostname before assigning its route

When the existing --host option selects a hostname other than pm.security.cab, this installs a route to a Worker whose PUBLIC_HOST remains hardcoded to pm.security.cab in edge/wrangler.jsonc. The workers.dev health check still succeeds, but entry.ts considers requests on the alternate hostname unrouted and returns 503 for host-key acquisition and host relays. Consequently, the laptop cannot enroll or report, and the alternate-host console stops opening. Align or validate the Worker's configured hostname before routing traffic through it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: the Worker's health answer now says which hostname it is built for, and expose.py only routes that hostname through it.


Generated by Claude Code

Comment on lines +500 to +502
await perform(table, docs, held.kind, held.args);
docs.logAct({ ...entry, status: "done", note: "Sent late: held while the laptop could not be reached." });
await done({ ok: true });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Record refused held answers as failures

When a held question answer is rejected after reconnect—for example because another answer already resolved the question—Work.submit() returns a nonempty refused array rather than throwing. This code discards that result and logs success, causing EdgeLink.take() to acknowledge the command successfully and the edge to remove it. The owner therefore sees the answer marked done even though it was not applied; unlike the immediate path in public/app.js, nothing reports the refusal reason. Inspect the returned outcomes and preserve failed or partially refused results in the held action's status.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real. Fixed in #12511: a late answer that comes back refused is recorded as failed with the reason ("not recorded: already-resolved"), and partly refused ones say how many were recorded.


Generated by Claude Code

dezren39 commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

Went live at 2026-11-22T11:49Z after #12511.

Not checked: the owner's real sign-in at the edge. answered and deferred in the health answer were both 0 at the time of writing; the first visit moves one of them.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants