Skip to content

OPE-275: Pairing: support new opencode pair / pair --remote flow (instructions + robustness) #549

Closed

Description

@ivanfenenko

OpenCode changed how pairing works in v2.0.25 and v2.0.26 (Oct 8). The app's QR parsing and code redemption already work with it, but the in-app instructions are out of date and connection handling should be more robust. These findings come from upstream v2 @ 229b422 and anomalyco/opentunnel @ 22156aa, read from the code; nothing was tested on a device.

What changed upstream

Plain opencode pair (from #53588, v2.0.25)

  • It needs the background service (opencode serve --service) and starts it if it isn't running.
  • It gets addresses from GET /api/info urls and prints an /auth/connect/<code> link for each one.
  • It draws a QR only if at least one address isn't loopback. The QR is JSON {code, urls} with loopback addresses removed.
  • The service binds to 127.0.0.1:49374 by default, so out of the box there is no QR. Users get a localhost link, an SSH port-forward hint, and a suggestion to run opencode service set hostname 0.0.0.0.
  • Once remote access has been enabled, plain pair shows a QR again, listing LAN addresses first and the tunnel address last.

opencode pair --remote (from #53837, v2.0.25; random subdomain from #53962, v2.0.26)

  • On the first run it turns remote access on and creates an OpenTunnel tunnel, printing "Setting up remote access; this can take a minute..." while a certificate is issued. It then restarts the service and waits up to 3 minutes for the tunnel address to appear in /api/info.
  • The QR and links contain only the tunnel address: https://<16-hex route>.<tunnel-id>.opentunnel.xyz.
  • The address stays the same across restarts. Running opencode service set remote false (or unset) forgets it, so turning remote access back on gives a new address.
  • The tunnel runs inside the background service, not inside pair, so it keeps running after pair exits and reconnects on its own. It is never opened under a foreground opencode serve.
  • OpenTunnel is Anomaly's relay. TLS is decrypted on the user's machine, so the relay sees the hostname and encrypted bytes but not the HTTP content. The route is not authentication; the server still requires its password or a token.

Unchanged (server side, since #50970, Sep 24)

  • QR payload: {"code":"<22-char base64url>","urls":[...]}.
  • The code is created by POST /api/pair. It is valid for 5 minutes, works once, and is held in memory only, so a service restart discards it. All printed links share one code.
  • GET /auth/connect/{code} with an Accept header that doesn't include text/html returns {token}. An expired or already-used code returns 401.
  • The token is valid for 30 days and cannot be refreshed. It is sent as Basic auth opencode:<token> and is not tied to an address, so it works on LAN, Tailscale, and the tunnel.
  • There is no device approval, device list, per-device token, or revoke. The only way to cut off devices is to change the password (opencode service set password), which logs out every device at once.

App status

The remote QR parses and pairs correctly. On iOS the whole flow was traced: PairingQRPayload.swift, ServerAddressProbe.swift, PairingCodeRedeemer.swift, ConnectionSetupViewModel.swift. On Android, PairingQRPayload.kt parses the same payload, but the rest of the Android flow wasn't traced. The tunnel needs no extra headers.

To do

  • Update the in-app pairing instructions. Explain that plain opencode pair shows no QR by default, and that users should run opencode pair --remote (first run takes about a minute to set up) or expose the service on the LAN with opencode service set hostname 0.0.0.0. Mention that the service must be running (opencode serve --service), that a code expires after 5 minutes and works only once, and that --remote requires OpenCode v2.0.25 or later.
  • Label the tunnel address. It currently shows as "Local network" in the saved-address rows. Update the qrSavedAddressesHint text, which mentions both addresses, Tailscale and local, and doesn't fit a QR that only has the tunnel.
  • Re-check addresses when the network changes or the event stream fails. On iOS the address is only chosen in ActiveConnectionManager ensureLoaded / apply / reloadFromStore, so leaving home Wi-Fi mid-session keeps the LAN address. Check how Android's pickReachableUrl orders its reachable list.
  • Refresh saved addresses from GET /api/info urls (skipping loopback) after each successful connect, so a new tunnel address is picked up automatically.
  • Ask the user to pair again when every address fails, for example when the tunnel address changed or the service is down.
  • Treat a 401 from a reachable server as "pair again". Send the user back to the QR or code flow instead of a password field, since tokens expire after 30 days or when the password changes.
  • Keep the existing event-stream reconnect logic for the tunnel. It's unclear whether the tunnel has an idle timeout for long-lived streams.

Imported from Linear OPE-275. Linear status: Done. Priority: High. Created 2026-10-09. Completed 2026-10-10.
Related: OPE-273, OPE-272.

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

    priority: high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions