Skip to content
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b08f9e6
Use ticket refs in mobile pairing QR
Jun 26, 2026
9744561
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
Jun 27, 2026
084f4a2
Refresh Swift length budget after main merge
Jun 27, 2026
61fe802
Address mobile pairing review feedback
Jun 27, 2026
5686984
Complete mobile redeem localizations
Jun 27, 2026
23b3a19
Merge origin/main into issue 5540
Jun 27, 2026
e7a73d2
Fix compact ticket decode return
Jun 27, 2026
712aa37
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
Jun 29, 2026
4186f18
Fix cancelled ticket redemption retry
Jun 29, 2026
b4a3054
Split ticket redemption gate test
Jun 29, 2026
2efad57
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
Jun 29, 2026
8a67814
Address mobile pairing ticket review
Jun 29, 2026
e7f094e
Address mobile RPC review seam
Jun 29, 2026
fab1e72
Address mobile pairing review follow-ups
Jun 29, 2026
d9939a3
Address pairing QR review follow-ups
Jun 29, 2026
51a51de
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jun 29, 2026
8a7e0e9
Bound abandoned redemption tasks in the ticket gate
austinywang Jun 30, 2026
e7dc163
Document CmxAttachTicketError public cases
austinywang Jun 30, 2026
9607489
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 2, 2026
15fab0e
Add failing test for legacy compact v1 opaque user-id decoding
austinywang Jul 2, 2026
42ac0e0
Preserve legacy compact v1 opaque user-id decoding
austinywang Jul 2, 2026
e8033f6
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 2, 2026
fcf8170
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 2, 2026
8e9808a
Split CmuxMobileRPC helper types into their own files
austinywang Jul 2, 2026
020ad94
Extract ticket-redemption helpers to their own file
austinywang Jul 2, 2026
714273d
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 2, 2026
19e634a
Constrain redeemed ticket scope to the scanned QR scope
austinywang Jul 2, 2026
90be424
Keep scanned QR scope authoritative over redeemed scope
austinywang Jul 2, 2026
6e93193
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 2, 2026
3f1b92d
Make scope-merge helper a file-scope private func
austinywang Jul 2, 2026
6f5d596
Drop test-only observability seam from redemption gate
austinywang Jul 2, 2026
70fed08
Scope-merge helper: instance method, not file-scope func
austinywang Jul 2, 2026
b617d9f
Add failing test: shared redemption poisons longer waiter
austinywang Jul 2, 2026
4d78c41
Bound shared redemption by the gate, not the first waiter's deadline
austinywang Jul 2, 2026
158d0ab
Refresh Swift file-length budget for redemption regression test
austinywang Jul 2, 2026
e116c3e
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 3, 2026
90dc248
Merge remote-tracking branch 'origin/main' into issue-5540-compact-mo…
austinywang Jul 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions .github/swift-file-length-budget.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# Reduce counts as files shrink. CI fails if tracked files exceed this budget.
34430 CLI/cmux.swift
17978 Sources/AppDelegate.swift
16487 Sources/ContentView.swift
14232 Sources/TerminalController.swift
16484 Sources/ContentView.swift
14276 Sources/TerminalController.swift
13071 Sources/Workspace.swift
12348 cmuxTests/AppDelegateShortcutRoutingTests.swift
12212 Sources/GhosttyTerminalView.swift
11671 Sources/Panels/BrowserPanel.swift
9497 cmuxTests/CLINotifyProcessIntegrationRegressionTests.swift
8032 Sources/Panels/BrowserPanelView.swift
8016 CLI/cmux_open.swift
7783 Packages/iOS/CmuxMobileShell/Sources/CmuxMobileShell/MobileShellComposite.swift
7795 Packages/iOS/CmuxMobileShell/Sources/CmuxMobileShell/MobileShellComposite.swift
7404 cmuxTests/WorkspaceUnitTests.swift
7218 cmuxTests/WorkspaceRemoteConnectionTests.swift
6359 cmuxTests/SessionPersistenceTests.swift
Expand All @@ -24,7 +24,7 @@
4487 Sources/Panels/FilePreviewPanel.swift
4483 Sources/cmuxApp.swift
4367 cmuxTests/BrowserPanelTests.swift
4257 Packages/iOS/CmuxMobileTerminal/Sources/CmuxMobileTerminal/GhosttySurfaceView.swift
4239 Packages/iOS/CmuxMobileTerminal/Sources/CmuxMobileTerminal/GhosttySurfaceView.swift
4121 Sources/BrowserWindowPortal.swift
3934 Sources/Feed/FeedPanelView.swift
3926 cmuxTests/TabManagerUnitTests.swift
Expand All @@ -39,7 +39,7 @@
2563 Sources/Panels/CmuxWebView.swift
2546 cmuxTests/WorkspaceManualUnreadTests.swift
2524 cmuxTests/CommandPaletteSearchEngineTests.swift
2395 Sources/Mobile/MobileHostService.swift
2399 Sources/Mobile/MobileHostService.swift
2328 cmuxTests/CJKIMEInputTests.swift
2242 Sources/TerminalNotificationStore.swift
2229 Sources/TerminalWindowPortal.swift
Expand All @@ -57,20 +57,20 @@
1680 cmuxUITests/BrowserPaneNavigationKeybindUITests.swift
1656 Sources/FileExplorerView.swift
1652 cmuxTests/CMUXCLIErrorOutputRegressionTests.swift
1649 cmuxTests/TerminalControllerSocketSecurityTests.swift
1650 cmuxTests/TerminalControllerSocketSecurityTests.swift
1581 Packages/iOS/CmuxMobileTerminal/Sources/CmuxMobileTerminal/TerminalInputTextView.swift
1560 cmuxTests/TextBoxMentionCompletionTests.swift
1500 cmuxUITests/MultiWindowNotificationsUITests.swift
1499 cmuxTests/OmnibarAndToolsTests.swift
1447 Sources/FileExplorerStore.swift
1433 Sources/RemoteTmuxControlConnection.swift
1420 cmuxTests/AppDelegateIssue2907RoutingTests.swift
1384 cmuxTests/KeyboardShortcutSettingsFileStoreStartupTests.swift
1380 cmuxUITests/MenuKeyEquivalentRoutingUITests.swift
1363 Sources/CMUXInstalledExtensionSidebarHostView.swift
1360 Sources/Feed/FeedButtonStyleDebugWindowController.swift
1359 cmuxTests/MobileHostAuthorizationTests.swift
1317 Sources/FileExplorerStore.swift
1295 Packages/macOS/CmuxTerminalCore/Sources/CmuxTerminalCore/Config/GhosttyConfig.swift
1295 cmuxTests/MobileHostAuthorizationTests.swift
1290 cmuxTests/TextBoxSubmitActionTests.swift
1287 cmuxTests/RestorableAgentSessionIndexTests.swift
1285 cmuxUITests/SidebarHelpMenuUITests.swift
Expand Down Expand Up @@ -132,9 +132,9 @@
756 Sources/Panels/AgentSessionWebRendererCoordinator.swift
754 Sources/TerminalController+ControlWorkspaceContext.swift
752 cmuxUITests/CloseWorkspaceCmdDUITests.swift
750 Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView.swift
739 cmuxTests/CLICodexHookTimeoutRegressionTests.swift
738 Packages/macOS/CMUXProjectModel/Sources/CMUXProjectModel/XcodeProjectAdapter.swift
724 Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView.swift
718 Packages/Shared/CmuxAuthRuntime/Sources/CmuxAuthRuntime/Coordinator/AuthCoordinator.swift
716 Sources/TaskManagerSnapshot.swift
715 Sources/AppleScriptSupport.swift
Expand Down Expand Up @@ -171,6 +171,7 @@
630 Packages/macOS/CmuxSettings/Sources/CmuxSettings/Values/ShortcutWhenClause.swift
627 Packages/macOS/CmuxSettingsUI/Sources/CmuxSettingsUI/Sections/KeyboardShortcutsSection.swift
624 Packages/iOS/CmuxMobileShell/Tests/CmuxMobileShellTests/MobileShellRenderGridLivenessTests.swift
621 Packages/iOS/CmuxMobileRPC/Tests/CmuxMobileRPCTests/MobileCoreRPCClientTests.swift
620 cmuxTests/FinderFileDropRegressionTests.swift
620 cmuxTests/TerminalNotificationQueueTests.swift
617 Sources/SettingsNavigation.swift
Expand Down Expand Up @@ -240,6 +241,7 @@
514 Packages/macOS/CmuxSwiftRender/Sources/CmuxSwiftRender/ExpressionEvaluator.swift
514 cmuxUITests/UpdatePillUITests.swift
513 Packages/iOS/CmuxAgentChatUI/Sources/CmuxAgentChatUI/Transcript/ChatTranscriptTableView.swift
513 Packages/iOS/CmuxMobileRPC/Sources/CmuxMobileRPC/MobileCoreRPCClient.swift
509 Packages/macOS/CMUXAgentLaunch/Sources/CMUXAgentLaunch/AgentLaunchSanitizerAdditionalPolicies.swift
507 Sources/TerminalControllerTopSupport.swift
506 Sources/App/MainWindowVisibilityController.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ import Foundation
/// The compact grammar keeps the same envelope but encodes only what pairing
/// actually consumes: short keys, no empty optional fields, no auth token, no
/// display name (read post-handshake from `mobile.host.status`), and no
/// expiry. It does keep non-secret pairing context: the Mac account email,
/// shared pairing compatibility level, and app version/build, so the phone can
/// fail fast on an account mismatch and warn before continuing across
/// compatibility skew. A pairing QR never expires;
/// the owner's Stack access token is the
/// host's sole authorization gate (`MobileHostService.authorizationError(for:)`),
/// so ticket age authorizes nothing.
/// inline expiry. It does keep non-secret pairing context: the ticket reference,
/// Mac account id, shared pairing compatibility level, and app version/build,
/// so the phone can redeem the host-side ticket record after Stack auth, fail
/// fast on an account mismatch, and warn before continuing across compatibility
/// skew. The ticket reference is not a bearer credential; the owner's Stack
/// access token is the host's authorization gate.
///
/// Compatibility:
/// - New decoders accept both grammars: ``CmxAttachTicketInput`` routes a
/// payload whose top-level object carries `"v"` here and everything else
/// (legacy `"version"` payloads) through the original `Codable` path.
/// - Payloads from the first compact revision still decode: their extra `e`
/// (expiry) and `n` (display name) keys are ignored, their explicit route
/// `i` ids and endpoint `t` types are honored.
/// - Payloads from the first compact revision (`v == 1`) still decode: `u` is
/// read as the Mac account email when it looks like one (an `@` probe) and as
/// the opaque Stack user id otherwise, extra `e` (expiry) and `n` (display
/// name) keys are ignored, and explicit route `i` ids and endpoint `t` types
/// are honored. The current grammar (`v == 2`) always carries the user id.
/// - Old decoders reject the compact grammar loudly (a `DecodingError` from
/// the missing `"version"` key), so an outdated phone scanning a new QR
/// shows a pairing error instead of silently misreading the ticket.
///
/// Key map (ticket): `v` version, `w` workspaceID (omitted when empty),
/// `t` terminalID, `d` macDeviceID, `u` Mac account email, `pc` pairing
/// compatibility version, `av` app version, `ab` app build, `r` routes.
/// Key map (ticket): `v` compact grammar version, `w` workspaceID (omitted when empty),
/// `t` terminalID, `d` macDeviceID, `u` Mac account id, `pc` pairing
/// compatibility version, `av` app version, `ab` app build, `q` ticket
/// reference, `r` routes.
/// Key map (route): `i` id (omitted when the decoder can resynthesize it:
/// `kind` for the first route of a kind, `kind_N` for the Nth), `k` kind raw
/// value, `p` priority (omitted when 0), `e` endpoint.
Expand All @@ -45,9 +47,9 @@ public struct CmxAttachTicketCompactCoder: Sendable {
/// Encode a ticket into the compact JSON grammar.
///
/// Any `authToken`, `macDisplayName`, and `expiresAt` on the ticket are
/// intentionally not encoded: the token never authorizes anything on the
/// host (Stack auth is the sole gate), the name is read post-handshake
/// from `mobile.host.status`, and a pairing QR never expires.
/// intentionally not encoded: the token is resolved from `ticketRef` only
/// after Stack auth, the name is read post-handshake from
/// `mobile.host.status`, and the host-side ticket record owns expiry.
public func encode(_ ticket: CmxAttachTicket) throws -> Data {
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import Foundation

/// The minimal pairing-QR grammar: expected Mac account/build metadata plus
/// plain `host:port` routes in the URL query.
/// The compact pairing-QR grammar: a non-secret ticket reference, expected Mac
/// account/build metadata, plus plain `host:port` routes in the URL query.
///
/// `cmux-ios://attach?v=2&ub=<stack-user-id>&pc=<compat>&av=<version>&ab=<build>&r=<host>:<port>[&r=<host>:<port>...]`
/// `cmux-ios://attach?v=3&tr=<ticket-ref>&ub=<stack-user-id>&pc=<compat>&av=<version>&ab=<build>&r=<host>:<port>[&r=<host>:<port>...]`
///
/// A pairing QR needs to tell the phone where to dial and which non-secret
/// account/build context to check before dialing. The account value is the
/// opaque Stack user id, never the email itself. Everything else the earlier
/// grammars carried has a better channel or no reason to exist:
/// - **No auth token.** The owner's Stack access token is the host's sole
/// authorization gate; a token in the QR authorized nothing and made the
/// code look like a leaked credential.
/// - **No expiry.** Ticket age authorizes nothing, so a code that sat on
/// screen for an hour still pairs.
/// ticket/account/build context to check before dialing. The account value is
/// the opaque Stack user id, never the email itself. Everything else the
/// earlier grammars carried has a better channel or no reason to exist:
/// - **No auth token.** The phone redeems `tr` only after sending the owner's
/// Stack access token to the Mac. The reference alone is not a bearer
/// credential.
/// - **No inline expiry.** The host-side ticket record owns TTL/revocation.
/// - **No display name, no device id.** Both arrive post-handshake from
/// `mobile.host.status`; the decoder leaves `macDeviceID` empty and the
/// shell adopts the host-reported identity once connected.
Expand All @@ -29,9 +28,9 @@ import Foundation
/// timeout before the Tailscale route was ever tried.
///
/// The payload is deliberately *not* wrapped in base64 JSON: anyone can read
/// the URL off the QR and see for themselves that it carries only an address.
/// Plain text is also smaller, which lowers the QR version (fewer, larger
/// modules) and makes the code scan faster from a Mac screen.
/// the URL off the QR and see for themselves that it carries only a reference
/// and addresses. Plain text is also smaller, which lowers the QR version
/// (fewer, larger modules) and makes the code scan faster from a Mac screen.
///
/// Compatibility: this grammar only ever appears in the Mac's pairing QR.
/// Workspace-scoped tickets, dev loopback tickets, and every RPC consumer
Expand All @@ -40,8 +39,11 @@ import Foundation
public struct CmxPairingQRCode: Sendable {
/// The grammar version carried in the URL's `v` query item. Distinct from
/// ``CmxAttachTicket/currentVersion`` (the ticket *structure* version):
/// `v=1` URLs carry a base64 JSON `payload`, `v=2` URLs carry bare routes.
public static let version = 2
/// `v=1` URLs carry a base64 JSON `payload`, `v=2` URLs carry legacy bare
/// routes, and `v=3` URLs carry bare routes plus a ticket reference.
public static let version = 3

private static let legacyBareRouteVersion = 2

/// Defensive cap on routes accepted from a scanned code. The Mac's route
/// resolver emits at most a couple (MagicDNS name + Tailscale IP); a QR
Expand All @@ -53,7 +55,7 @@ public struct CmxPairingQRCode: Sendable {
/// site; every instance speaks the same grammar version.
public init() {}

/// Encode `ticket` as a v2 pairing URL, or `nil` when the ticket does not
/// Encode `ticket` as a v3 pairing URL, or `nil` when the ticket does not
/// qualify (see ``canEncode(_:)``); callers fall back to the compact v1
/// payload so every ticket still has an attach URL.
///
Expand All @@ -63,7 +65,10 @@ public struct CmxPairingQRCode: Sendable {
guard let routes = encodableRoutes(of: ticket) else {
return nil
}
var items: [String] = ["v=\(Self.version)"]
guard let ticketRef = normalizedNonEmpty(ticket.ticketRef) else {
return nil
}
var items: [String] = ["v=\(Self.version)", "tr=\(percentEncodeQueryValue(ticketRef))"]
if let userID = normalizedNonEmpty(ticket.macUserID) {
items.append("ub=\(percentEncodeQueryValue(userID))")
}
Expand Down Expand Up @@ -94,16 +99,17 @@ public struct CmxPairingQRCode: Sendable {
/// Whether `ticket` is expressible in the minimal grammar; see
/// ``encodableRoutes(of:)`` for the rules.
public func canEncode(_ ticket: CmxAttachTicket) -> Bool {
encodableRoutes(of: ticket) != nil
normalizedNonEmpty(ticket.ticketRef) != nil && encodableRoutes(of: ticket) != nil
}

/// The route subsequence a v2 pairing URL would carry for `ticket`, or
/// The route subsequence a v3 pairing URL would carry for `ticket`, or
/// `nil` when the ticket is not expressible in the minimal grammar.
///
/// Expressible means: an unscoped pairing ticket whose Tailscale routes
/// are exactly the canonical `host:port` sequence the decoder
/// resynthesizes (ids `tailscale`, `tailscale_2`, ... and priorities 10,
/// 20, ...), with no loopback host and no host that needs escaping.
/// Expressible means: an unscoped pairing ticket with a ticket reference,
/// whose Tailscale routes are exactly the canonical `host:port` sequence
/// the decoder resynthesizes (ids `tailscale`, `tailscale_2`, ... and
/// priorities 10, 20, ...), with no loopback host and no host that needs
/// escaping.
/// The only routes this grammar may silently drop are loopback ones (a
/// DEBUG Mac's dev loopback route), which no phone may ever dial anyway.
/// Anything else (workspace-scoped tickets, custom route ids, no
Expand Down Expand Up @@ -136,9 +142,23 @@ public struct CmxPairingQRCode: Sendable {
}

/// Whether `components` (an already-parsed `cmux-ios://attach` URL) speaks
/// this grammar. v1 URLs carry the base64 `payload` item instead.
/// the bare-route pairing grammar. v1 URLs carry the base64 `payload` item
/// instead; v2 is accepted only for backwards-compatible route-only codes.
public func isPairingCodeURL(_ components: URLComponents) -> Bool {
components.queryItems?.first(where: { $0.name == "v" })?.value == "\(Self.version)"
guard let version = Self.attachURLVersion(components) else {
return false
}
switch version {
case Self.legacyBareRouteVersion:
return queryValue(named: "payload", in: components) == nil
&& !queryValues(named: "r", in: components).isEmpty
case Self.version:
return queryValue(named: "payload", in: components) == nil
&& queryValue(named: "tr", in: components) != nil
&& !queryValues(named: "r", in: components).isEmpty
default:
return false
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// The integer grammar version declared by an attach URL's `v` query item,
Expand All @@ -152,7 +172,7 @@ public struct CmxPairingQRCode: Sendable {
return Int(raw)
}

/// Whether `rawValue` is a v2 pairing URL. String-level convenience for
/// Whether `rawValue` is a bare-route pairing URL. String-level convenience for
/// callers that hold the encoded URL (the Mac's pairing window asserting
/// the code it is about to display speaks the minimal grammar).
public func isPairingCodeURLString(_ rawValue: String) -> Bool {
Expand All @@ -165,19 +185,26 @@ public struct CmxPairingQRCode: Sendable {
return isPairingCodeURL(components)
}

/// Decode a v2 pairing URL into a validated ``CmxAttachTicket``.
/// Decode a bare-route pairing URL into a validated ``CmxAttachTicket``.
///
/// The ticket comes back unscoped with an empty `macDeviceID`; the shell
/// recovers the Mac's identity post-handshake from `mobile.host.status`.
/// - Parameter components: The parsed `cmux-ios://attach?v=2&...` URL.
/// The `tr` ticket reference is mandatory for the current v3 grammar;
/// legacy v2 route-only URLs still decode without one for compatibility.
/// - Parameter components: The parsed `cmux-ios://attach?v=3&...` URL.
/// - Throws: ``MobileSyncPairingPayloadError/invalidURL`` for malformed
/// input and ``MobileSyncPairingPayloadError/loopbackRouteRejected``
/// when any route names a loopback host (a scanned code must never
/// point the phone at itself).
public func decode(_ components: URLComponents) throws -> CmxAttachTicket {
let grammarVersion = Self.attachURLVersion(components)
guard isPairingCodeURL(components) else {
throw MobileSyncPairingPayloadError.invalidURL
}
let ticketRef = queryValue(named: "tr", in: components)
if grammarVersion == Self.version, ticketRef == nil {
throw MobileSyncPairingPayloadError.invalidURL
}
let rawRoutes = (components.queryItems ?? [])
.filter { $0.name == "r" }
.compactMap(\.value)
Expand Down Expand Up @@ -208,6 +235,7 @@ public struct CmxPairingQRCode: Sendable {
macAppBuild: queryValue(named: "ab", in: components),
routes: routes,
expiresAt: nil,
ticketRef: ticketRef,
authToken: nil
)
try ticket.validate()
Expand Down Expand Up @@ -283,6 +311,12 @@ private extension CmxPairingQRCode {
normalizedNonEmpty(components.queryItems?.first(where: { $0.name == name })?.value)
}

func queryValues(named name: String, in components: URLComponents) -> [String] {
(components.queryItems ?? [])
.filter { $0.name == name }
.compactMap { normalizedNonEmpty($0.value) }
}

func queryInt(named name: String, in components: URLComponents) -> Int? {
guard let value = queryValue(named: name, in: components) else { return nil }
return Int(value)
Expand Down
Loading
Loading