Skip to content

Fix #1871: Tower persists the cloud's slug, follows renames, reacts to deregistration, adds --claim - #1988

Merged
waleedkadous merged 15 commits into
mainfrom
builder/bugfix-1871
Nov 25, 2026
Merged

waleedkadous merged 15 commits into
mainfrom
builder/bugfix-1871

Conversation

@waleedkadous

@waleedkadous waleedkadous commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Tower stored and advertised the tower name the user typed, not the slug the cloud assigned. When the cloud registered a Tower as <name>-2, every public URL Tower built pointed at the wrong slug and the IDE's assets 502'd. A cloud-side delete also left Tower retrying a dead key forever. This is the Tower half of codev-cloud#17: persist and show the canonical slug, follow a cloud-side rename, react to deregistration, and add an explicit name takeover.

Fixes #1871

Root Cause

  • lib/token-exchange.ts redeemToken parsed only towerId/apiKey and dropped the slug/name the cloud returns. The OAuth callback (servers/tower-tunnel.ts) and afx tower connect (commands/tower-cloud.ts) both persisted the typed name as tower_name. The access URL, the success page, and the IDE x-forwarded-prefix and cookie Path all derive from tower_name.
  • lib/tunnel-client.ts read only towerId from auth_ok, so a rename made in the cloud never reached Tower.
  • handleAuthError('invalid_api_key') could not tell a key that never worked from a key that worked and then died, so both got the tunnel-client: wedges after sustained uplink flap — instant-fail loop, then a hung connect attempt with no timeout; never self-recovers without manual reconnect #1372 15-minute half-open retry indefinitely. Nothing recorded or surfaced that the Tower had been deregistered.

Fix

  1. Persist the canonical identity. redeemToken returns slug and name. When either is absent (an older cloud) it falls back to the requested name, which is today's behaviour. The callback and the CLI write tower_name: slug plus a new optional display_name. The success page and log line name the slug and say when it differs from the requested name.
  2. Follow renames. TunnelClient.onAuthOk(identity) reports {towerId, slug?, name?}, and Tower rewrites cloud-config.json when the slug or name changed. An auth_ok without a slug leaves the stored identity alone.
  3. React to deregistration. invalid_api_key after the client has been connected is a new terminal TunnelState deregistered, with no half-open retry.
  4. Explicit takeover. afx tower connect --claim sends claimSlug: true on redeem. Without the flag nothing is sent and the cloud still bumps to <name>-2.
  5. Show the canonical name. afx tower status prints the slug, the display name and the access URL built from the slug.

Small adjacent fix: the callback success page now HTML-escapes the names it shows (it interpolated pending.name raw).

Docs: afx tower connect/status in codev/ and codev-skeleton/ resources/commands/agent-farm.md, the arch.md note on tower_name, and all four afx skill copies (--claim), with shipped-skill-hashes.ts regenerated.

Cloud-side gap (not worked around on the Tower side)

A rename in the cloud UI does not close the live tunnel session. A connected Tower therefore keeps its old slug until its next reconnect, which is when auth_ok delivers the new one. Closing the session on rename is the cloud's change to make. Note that a tunnel-borne IDE request already uses the relay-stamped prefix (#1914), so only Tower's stored and advertised name lags.

Scope notes

CMAP review fixes

  • codex: a stale tunnel client survived a re-registration made while connected. The config watcher skipped every change while the tunnel was connected, so new credentials never replaced the client. Once the cloud rejected the old key, the client sat in terminal deregistered with nothing to connect the new registration. The watcher now skips only when the live client was built from the same api_key/tower_id/server_url; an identity-only change from auth_ok still skips. The regression test now requires Tower to reconnect on the replacement key and never retry the old one, and it fails on the previous condition.
  • codex and claude: the home page Connect dialog prefilled the slug. It now prefills the display name (mac, not mac-2). The dialog normalizes names before validating them. Re-checked in the browser.
  • claude: the dashboard title switched to the slug. The /api/state hostname (Spec 443 title) now uses display_name, falling back to tower_name, which keeps the title users already saw. Covered by a test with display name Ana's MBP and slug ana-s-mbp.
  • claude: the guard-failed branch of markDeregistered. Now covered by the watcher fix: the write of the new registration wakes the watcher, which replaces the parked client.
  • Not changed: porch's own chore(porch): … commit subjects (codex), which porch writes itself.
  • The Tower's canonical name comes only from the slug. auth_ok.name and the redeem response's name go only to display_name.

Test Plan

  • Regression test added: bugfix-1871-cloud-identity.test.ts drives the real tower-tunnel, cloud-config, token-exchange and TunnelClient against a fake cloud. MockTunnelServer now also serves redeem and the codev-cloud#17 auth_ok shape. The suite uses its own agent-farm dir. It covers:

    • a bumped slug lands in cloud-config.json, the success page, getPublicAuthority()'s prefix and the status accessUrl;
    • a response without slug/name behaves as today;
    • claimSlug is sent only when asked;
    • an auth_ok with a new slug rewrites the config, and one without a slug changes nothing;
    • invalid_api_key after a connect stamps deregistered_at, keeps the credentials, makes no auth attempt even past the half-open interval, and survives a Tower restart and smart reconnect;
    • a rejected key never stamps a registration written since.

    6 of its 7 tests fail on the pre-fix source. The 7th pins the unchanged older-cloud path.

  • Unit tests:

  • Build passes (porch build).

  • All tests pass (porch tests: full packages/codev suite).

  • Browser check of the Tower home page (Playwright, headless). The built Tower ran on a spare port with an isolated CODEV_AGENT_FARM_DIR seeded with a deregistered config. The page shows a red dot, mac-2 deregistered, a Connect button and an explanatory tooltip, with no page errors. afx tower status -p <port> prints the deregistered block, and afx tower connect --help lists --claim. The live Tower and the real cloud-config.json were not touched.

  • Windows (current head 3baa9f5): https://github.com/cluesmith/codev/actions/runs/45702187250.

  • Not exercised: the issue's acceptance steps against the real cloud (register under a taken name, rename in the cloud UI, delete in the cloud UI, --claim). They need a real registration, which the architect ruled out for this builder. The fake cloud plays the same contract.

Existing failures (not flaky)

…o deregistration

Tower stored and advertised the name the user typed, so when the cloud
registered it as <name>-2 every public URL (access URL, success page, IDE
x-forwarded-prefix and cookie Path) pointed at the wrong slug. A cloud-side
delete left Tower retrying a dead key forever.

- redeemToken returns the cloud's slug and display name (falling back to the
  requested name for an older cloud) and sends claimSlug only when asked.
- The OAuth callback and `afx tower connect` persist tower_name = slug plus
  display_name; the success page and log say when the slug differs.
- TunnelClient reports the auth_ok identity; Tower rewrites cloud-config.json
  when the slug changed, so a cloud rename lands on the next reconnect.
- invalid_api_key after this client has connected is a terminal
  `deregistered` state with no half-open retry. Tower stamps deregistered_at
  in cloud-config.json (keyed on the same api_key, credentials kept), does not
  reconnect it at start, on config change, or on smart reconnect, and reports it
  in /api/tunnel/status, the Tower home page and `afx tower status`.
- `afx tower connect --claim` takes the name back from a disconnected tower.
- `afx tower status` prints slug, display name and the slug-built access URL.
- The callback page now HTML-escapes the names it shows.
- Docs (both trees), arch.md and the afx skill (hashes regenerated).
…laim against a fake cloud

bugfix-1871-cloud-identity.test.ts drives the real tower-tunnel, cloud-config,
token-exchange and TunnelClient against MockTunnelServer (now able to serve
redeem and the codev-cloud#17 auth_ok shape) in its own agent-farm dir:
a bumped slug lands in cloud-config.json and getPublicAuthority's prefix,
auth_ok renames rewrite it, an older cloud's response changes nothing, and
invalid_api_key after a connect stamps deregistered_at, keeps the credentials
and never retries. Six of its seven tests fail on the pre-fix source; the
seventh pins the unchanged older-cloud behaviour.

Unit tests pin the TunnelClient state split (deregistered vs the #1372
breaker), the auth_ok identity, updateCloudConfig's key guard, and the CLI
(--claim, slug persistence, status output, deregistered re-registration).
The fourth afx skill tree (root .codex/skills) was missed, which failed the
skill-parity, spec-1280 and spec-1273 byte-identity tests.
… while connected; prefill the display name

CMAP (codex): the config watcher skipped every change while connected, so a
re-registration written during a live connection left Tower on the old key;
once the cloud rejected it the client sat in terminal `deregistered` with
nothing to connect the new registration. The skip now applies only when the
live client was built from the same api_key/tower_id/server_url (an
identity-only change from auth_ok still skips).

The home page Connect dialog now offers the display name rather than the
cloud's slug, so re-registering a deregistered `mac-2` asks for `mac`
(the dialog normalizes the name before validating it).

The regression test now requires Tower to reconnect on the replacement key
and never retry the old one; it fails on the previous watcher condition.
…e slug

CMAP (claude): tower_name now holds the cloud's routing slug, so the
dashboard title (Spec 443, fed by /api/state hostname) silently switched
from the name the owner chose to the slug. Use display_name, falling back to
tower_name for configs written before #1871.
@waleedkadous
waleedkadous merged commit d840367 into main Nov 25, 2026
8 checks passed
waleedkadous added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant