Skip to content

Fix pairing with dark desktop codes and with a QR's own address - #34

Merged
Tarquinen merged 4 commits into
devfrom
fix/pairing-qr
Nov 11, 2026
Merged

Tarquinen merged 4 commits into
devfrom
fix/pairing-qr

Conversation

@Tarquinen

Copy link
Copy Markdown
Member

Fixes two pairing bugs reported on anomalyco/opencode, and tidies the pairing error and scanner copy.

#63150: dark desktop pairing codes don't scan

In the dark theme the desktop draws its pairing QR as light modules on a dark page (renderSVG with blackColor: currentColor, whiteColor: transparent), and the quiet zone is dark too. Android 0.3.0 couldn't read inverted codes at all, and it analysed 640x480 frames, so even light codes had to fill about a third of the frame. v0.3.8 fixed most of this (77358fe ALSO_INVERTED, c6d391a 1440x1080 frames). That explains the "scanner simply does not trigger" comment on 0.3.0, and I found no other way for the scanner to fail silently.

ALSO_INVERTED only flips the binarized matrix, though. HybridBinarizer has already made the dark quiet zone white, so after the flip it turns black and the finder patterns lose their margin. Dark codes therefore still needed to be about twice as large in the frame as light ones. QRDecoder now tries a second pass on the inverted luminance when the first pass finds nothing.

  • Synthetic-frame harness: real OpenCode QRs from renderSVG / renderUnicodeCompact, 1440 blurred, noisy and rotated frames.
  • At 1440x1080, reads go from 343 to 370 of 720. The desktop dark code now reads at 0.35 of frame height (it didn't before at the tested sizes) and cli --url dark at 0.3 (was 0.5).
  • No case got worse, light terminal codes included, and there were no false positives.
  • Cost on a laptop JVM: about 15 ms more per frame with no code.

#62901: a QR's own address loses to the OpenTunnel address

requestRedeemPairing adds the addresses the server reports in /api/info, including OpenTunnel, as alternates. ServerLinkStore.race then tries every address at once, and the first to answer wins. So a code from opencode pair --url https://x.tailnet.ts.net could end up on the tunnel. Deleting the old entry didn't help, because the new pairing learned the tunnel again. iOS behaves the same way today.

Now the addresses in the code (or a typed pairing link) are saved as OpenCodeServer.preferredURLs, a new JSON field that defaults to empty, so existing saved servers decode unchanged.

  • The race starts the preferred addresses first.
  • Other addresses start after a 1.5 s head start, or as soon as every preferred address has failed.
  • If every preferred address is relayed (the desktop QR and pair --remote carry only the tunnel), direct LAN and tailnet addresses aren't held back.
  • Editing the address by hand clears the preference.

iOS needs the same change for #62901. The parity rows in docs/parity/server.md describe the difference.

Also

  • When one address in a pairing code can't be used, the error now names it and says why: “Couldn’t use “http://…/app” from this pairing code: it has a path, and the server must be at the root of its address.” The code is still rejected as a whole, as on iOS. The address is shown without credentials, query or fragment.
  • The scanner's caption and its fallback messages now mention the desktop's Settings → Pairing and plain opencode pair, not only pair --remote. This differs from iOS, as noted in docs/parity/settings.md.

Verified

  • vm.sh check :feature:settings, vm.sh check :server :app (assemble, lint, unit tests). There are new tests for the preferred-first race, for falling back when the preferred address is unreachable or refuses, for relayed-only preferences, for persistence and decoding old JSON, for redemption saving the preference, and for the per-address error.
  • Emulator FX_1 with session-lab behind a throwaway pairing proxy. The proxy answers /auth/connect on two ports and reports both in /api/info. A, the code's address, answers 400 ms late; B, the reported one, answers at once.
    • Pairing via https://opencode.ai/pair#… naming only A: every request went to A, and B was never contacted.
    • Relaunch: still on A, so the preference persists.
    • A stopped, then relaunch: connected over B, the fallback.
    • A restarted, then relaunch: back on A.
    • The origin/dev build on the same saved server: “Local Network answered first”, and traffic moved to B. That reproduces #62901.
    • A link with a bad second address shows the new error.
  • QR harness re-run: same results as above.

OpenCode Desktop draws its pairing QR light-on-dark in the dark theme
(#63150). ZXing's ALSO_INVERTED flips the binarized frame, so the dark
page's quiet zone, which HybridBinarizer reads as white, turns black and
small codes are missed. A frame with no code found is now read again with
its luminance inverted before binarizing (a plain QR reader on
luminance.invert()); the first pass is unchanged, so nothing that read
before stops reading.

Verified with a throwaway ZXing 3.5.4 harness (not committed): QRs made by
OpenCode's own encoder (Desktop light/dark, `opencode pair` multi-URL and
--url in dark/light terminals with and without line gaps, TUI link),
rendered into Y-plane frames at 8 sizes x 6 conditions (blur, noise, low
contrast, 0-12 degree tilt). Smallest size, as a share of the 1080-pixel
frame side, that reads in at least 5 of 6 conditions, at 1440x1080:

  Desktop dark:          0.35 -> 0.30 (Desktop light: 0.30)
                         decodes 20 -> 26 of 48 (light: 27)
  pair --url, dark page: 0.50 -> 0.30 (light: 0.25)
  pair multi-URL, dark:  never -> 0.50 (light: 0.50)
  light terminal, TUI:   0.35 -> 0.21; pair --url 0.50 -> 0.30
  every other case:      unchanged; no case decodes fewer frames

For an 8 cm code and a ~50 degree field across the short side, 0.35 is
about 25 cm away and 0.30 about 29 cm. All frames: 343 -> 370 of 720 at
1440x1080, 160 -> 179 at 640x480; no false positives on blank frames.
Inverting the source alone (dropping ALSO_INVERTED) lost light-terminal
frames, so both passes stay. Cost on the laptop JVM at 1440x1080: 18 ->
32 ms for a frame with no code, 11 -> 17 ms with one; analysis keeps only
the latest frame, so a slower frame drops frames rather than queueing.

scripts/check.sh :feature:settings passes on the VM.
…ports

A code from `opencode pair --url https://x.tailnet.ts.net` kept connecting
over the server's OpenTunnel address (#62901). Redemption added every
address `/api/info` reports to the saved server, and the address race
starts every address at once, so whichever answered first won, often the
tunnel. Deleting the server didn't help: pairing again reported the
tunnel again.

Now redemption also saves the code's own addresses (the redeemed origin
first) as `OpenCodeServer.preferredURLs`; reported addresses stay in
`alternateURLs` as fallbacks, since the desktop's QR carries only the
tunnel. `ServerLinkStore.race` starts `firstAttemptURLs` at once and the
rest after `preferredHeadStart` (1.5 s) or as soon as every first
address has failed. A code that names only relayed addresses (the
desktop's and `pair --remote`'s tunnel) doesn't hold the server's LAN
and tailnet addresses back, so those still win at home as before.

`preferredURLs` defaults to empty and isn't part of `persistenceID`, so
servers saved before this decode unchanged, keep their identity and race
every address at once as before. Editing a server's address clears it.

This differs from iOS until iOS makes the same change (parity rows say
so).

Verified: scripts/check.sh :server :app (VM) passes, with new
ServerLinkTests (paired address wins over a faster reported one; others
join after the head start; others start at once when every paired one
fails; tunnel-only code still lets LAN win; stale or all-preferred lists
race as before; the preference persists and old saved JSON still loads)
and an AppFeatureTests wire test of the redemption's saved addresses.
A code with an address the app can't use (for example `opencode pair
--url https://box.example/opencode` behind a reverse proxy at a path)
was reported as "That isn’t an OpenCode pairing code", which sent people
looking for the wrong problem. Now it reads “Couldn’t use “<address>”
from this pairing code: it has a path, and the server must be at the
root of its address.” (also: not http(s), no host, a user name or
password, a query or fragment, a port outside 1–65535). The shown
address drops any user name, password, query or fragment and is capped
at 80 characters.

One bad address still rejects the whole code, as on iOS: skipping it
would pair over a different address than the one the person asked for,
the same surprise as #62901, and the server never lists a bad address
itself, so a bad one means the `--url` needs fixing.

Verified: scripts/check.sh :server :app (VM) passes, with new tests for
the decoder (`anUnusableAddressInAPairingCodeIsNamedWithoutItsSecrets`)
and the scan error (`aPairingCodeWithAnUnusableAddressSaysWhichAndWhy`).
The scanner's caption and its no-camera, camera-off and nothing-to-paste
messages only named `opencode pair --remote`. The desktop's Settings ›
Pairing page and plain `opencode pair` (with or without --url) show codes
the scanner reads just as well, so people pairing over the LAN or a tailnet
were steered to the tunnel. The caption now reads “On your computer, open
OpenCode’s Settings → Pairing, or run opencode pair in a terminal.”, close
to the connection screen's existing hint, and the fallbacks say
`opencode pair`.

This differs from iOS's PairingScanner.swift copy until iOS changes;
noted in docs/parity/settings.md.

Verified: vm.sh check :feature:settings (assemble, lint, unit tests) passed.
@Tarquinen
Tarquinen merged commit 748ec86 into dev Nov 11, 2026
Tarquinen added a commit that referenced this pull request Nov 11, 2026
Conflicts: MainActivity imports (kept both sides) and the
FloatingCallStageContent parity row (took dev's, which already
describes the reveal-loop fix this branch also carried, so that fix
drops out of this PR's diff).
Tarquinen added a commit that referenced this pull request Nov 12, 2026
refactor(server): simplify the preferred-address race from #34
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.

1 participant