Skip to content

Tower persists the typed tower name instead of the cloud's canonical slug; add rename, deregistration and name-takeover handling (Tower half of codev-cloud#17) #1871

Done

Description

@amrmelsayed

Problem

Tower's side of the Codev Cloud tower lifecycle is incomplete. Observed 2026-11-02 during the Windows manual pass for #1658 (review finding M5): after a reconnect, the cloud registered the Tower under a deduplicated name (<name>-2), Tower kept advertising and stamping the name the user typed, and every IDE asset under the cloud URL 502'd. The stranded <name> row could not be removed from the cloud UI either. The cloud side is tracked as codev-cloud#17 (delete reachable from the towers list, delete closes a live tunnel, rename endpoint, explicit name takeover, name in the redeem response). This issue is codev's half of the same contract.

Root cause (codev side), verified in source

  • redeemToken (lib/token-exchange.ts) parses only towerId and apiKey; the cloud's redeem response already carries slug, the server-derived routing identity. The OAuth callback in servers/tower-tunnel.ts persists tower_name: pending.name, the user's requested name. Every public URL Tower builds (accessUrl, the connect success page, the x-forwarded-prefix stamped by ide-forward.ts) derives from that stored name, so Tower and cloud disagree whenever the cloud bumps or renames.
  • On auth_error with invalid_api_key, lib/tunnel-client.ts re-parks and retries forever with a failed-marker log line. After a cloud-side delete the tunnel closes and the key is dead, but Tower keeps its cloud-config.json and keeps retrying; afx tower status does not say the Tower was deregistered.
  • POST /api/tunnel/disconnect already calls DELETE /api/towers/:id with the Bearer key (best effort), so codev can delete its own live registration today. A stranded row whose key was rotated by a later re-registration can only be deleted from the cloud UI (codev-cloud#17).

Fix (Tower only; the cloud's protocol does not change for these)

  1. Persist the canonical identity. redeemToken returns slug and name alongside towerId and apiKey (fallback: requested name when absent, for older cloud deployments). The callback persists tower_name: slug and a new display_name: name. The success page and the log line show the slug, and say so when it differs from the typed name ("registered as <name>-2; <name> was taken").
  2. Re-read the slug on every reconnect. Once the cloud's auth_ok carries slug (requested from codev-cloud), update cloud-config.json when it differs so a rename made in the cloud UI takes effect on the next reconnect without re-registering.
  3. React to deregistration. On auth_error: invalid_api_key after a previously successful connection, stop retrying, mark the registration deregistered in cloud-config.json (do not silently delete credentials the user may want to inspect), surface it in afx tower status and on the Tower home page, and point at afx tower connect.
  4. Explicit takeover. afx tower connect --claim sends claimSlug: true on redeem, so a user re-registering a machine whose old row is not connected gets the name back instead of <name>-2. Default stays the bump.
  5. Show the canonical name. afx tower status prints slug, display name and the access URL built from the slug.
  6. Tests: redeem response with a different slug yields cloud-config.json carrying the slug and getPublicAuthority() building the prefix from it; auth_ok with a new slug rewrites the config; invalid_api_key after a successful connect moves to the deregistered state and stops the retry loop; --claim sets the flag; a response without slug/name behaves as today.

Acceptance

  • Register under a name the cloud already has: afx tower status shows the bumped slug, the connect page names it, and https://<cloud>/t/<bumped>/ide/?folder=… loads the workbench with every asset under the bumped prefix.
  • Rename the tower in the cloud UI, let Tower reconnect: status shows the new slug and the IDE loads under it.
  • Delete the tower in the cloud UI: Tower logs the close, status says deregistered, no retry storm.
  • afx tower connect --claim on a machine whose old row is disconnected takes the old name.
  • Register under a free name: behaviour unchanged.

Cloud contract (settled with the codev-cloud workspace, tracked as codev-cloud#17)

  • auth_ok → { towerId, slug, name }
  • redeem → { towerId, slug, name, apiKey }; claimSlug: true takes over a not-connected tower of the same user
  • PATCH /api/towers/:id { name } → 200 { towerId, slug, name } or 409 on slug conflict; old slug 404s immediately
  • delete closes the live tunnel session; /t/<slug>/ answers 410 for a tombstone window, then 404

Steps 1, 3 and 5 can land before the cloud ships; steps 2 and 4 are tested against the cloud's staging once codev-cloud#17 is deployed there. Source: #1658 review finding M5.

Activity

  1. added
    area/tower
    on Nov 3, 2026
  2. amrmelsayed commented on Nov 17, 2026

    @amrmelsayed
    CollaboratorAuthor

    Cloud half is on staging (note from the codev-cloud architect, 2026-11-18)

    The cloud side of this work (codev-cloud#17, merged to develop via its PR #18) is deployed to STAGING. What a builder on this issue can test against there:

    • auth_ok now carries {towerId, slug, name}; the redeem response carries {towerId, slug, name, apiKey} and claimSlug.
    • PATCH /api/towers/:id {name} renames; 409 on a name conflict.
    • DELETE deregisters and closes the live session.
    • /t/<old-slug>/ answers 410 (owner-scoped, for one hour) and then 404 after a rename or deregistration; the towers list links to the detail page.

    Production follows when the owner merges develop to main on the cloud side. Tower must persist and display the slug the cloud returns, never the typed name, and must handle the rename and takeover cases above. Related: #1914 builds its IDE link relative to the page so a slug change cannot strand it.

  3. amrmelsayed commented on Nov 17, 2026

    @amrmelsayed
    CollaboratorAuthor

    Cloud half is now in PRODUCTION (codev-cloud architect, 2026-11-18)

    Supersedes the staging note above. The tower lifecycle contract is live on the cloud:

    • auth_ok carries {towerId, slug, name}.
    • Redeem returns {towerId, slug, name, apiKey}; claimSlug: true takes over a not-connected tower of the same user. A connected holder is bumped rather than answered 409, so the token is not burned.
    • PATCH /api/towers/:id {name} returns {towerId, slug, name} or 409 on conflict.
    • DELETE closes the live tunnel; /t/<slug>/ answers 410 to the deleting user for one hour, then 404.
    • The towers list links to a detail page with Deregister and Rename.

    A builder on this issue can develop and verify against production. Staging equals production for now.

  4. waleedkadous commented on Nov 25, 2026

    @waleedkadous
    Contributor

    On it! Working on a fix now.

  5. added 7 commits that reference this issue on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/tower

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions