Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 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
12 changes: 7 additions & 5 deletions .github/swift-file-length-budget.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
34658 CLI/cmux.swift
17892 Sources/AppDelegate.swift
16491 Sources/ContentView.swift
14239 Sources/TerminalController.swift
14283 Sources/TerminalController.swift
12921 Sources/Workspace.swift
12348 cmuxTests/AppDelegateShortcutRoutingTests.swift
12233 Sources/GhosttyTerminalView.swift
Expand All @@ -14,7 +14,7 @@
7982 Sources/Panels/BrowserPanelView.swift
7366 cmuxTests/WorkspaceUnitTests.swift
7218 cmuxTests/WorkspaceRemoteConnectionTests.swift
6895 Packages/iOS/CmuxMobileShell/Sources/CmuxMobileShell/MobileShellComposite.swift
6900 Packages/iOS/CmuxMobileShell/Sources/CmuxMobileShell/MobileShellComposite.swift
6359 cmuxTests/SessionPersistenceTests.swift
6217 cmuxTests/GhosttyConfigTests.swift
6180 Sources/TabManager.swift
Expand All @@ -39,7 +39,7 @@
2565 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
2233 Sources/TerminalWindowPortal.swift
Expand All @@ -59,7 +59,7 @@
1652 cmuxTests/CMUXCLIErrorOutputRegressionTests.swift
1581 Packages/iOS/CmuxMobileTerminal/Sources/CmuxMobileTerminal/TerminalInputTextView.swift
1560 cmuxTests/TextBoxMentionCompletionTests.swift
1547 cmuxTests/TerminalControllerSocketSecurityTests.swift
1548 cmuxTests/TerminalControllerSocketSecurityTests.swift
1500 cmuxUITests/MultiWindowNotificationsUITests.swift
1499 cmuxTests/OmnibarAndToolsTests.swift
1447 Sources/FileExplorerStore.swift
Expand All @@ -68,9 +68,9 @@
1380 cmuxUITests/MenuKeyEquivalentRoutingUITests.swift
1373 cmuxTests/AppDelegateIssue2907RoutingTests.swift
1363 Sources/CMUXInstalledExtensionSidebarHostView.swift
1361 cmuxTests/MobileHostAuthorizationTests.swift
1360 Sources/Feed/FeedButtonStyleDebugWindowController.swift
1295 Packages/macOS/CmuxTerminalCore/Sources/CmuxTerminalCore/Config/GhosttyConfig.swift
1295 cmuxTests/MobileHostAuthorizationTests.swift
1287 cmuxTests/RestorableAgentSessionIndexTests.swift
1285 cmuxUITests/SidebarHelpMenuUITests.swift
1258 Sources/Feed/FeedCoordinator.swift
Expand Down Expand Up @@ -165,6 +165,7 @@
632 cmuxTests/AgentSessionAutoResumeSwiftTests.swift
630 Packages/macOS/CmuxSettings/Sources/CmuxSettings/Values/ShortcutWhenClause.swift
627 Packages/macOS/CmuxSettingsUI/Sources/CmuxSettingsUI/Sections/KeyboardShortcutsSection.swift
621 Packages/iOS/CmuxMobileRPC/Tests/CmuxMobileRPCTests/MobileCoreRPCClientTests.swift
620 cmuxTests/FinderFileDropRegressionTests.swift
620 cmuxTests/TerminalNotificationQueueTests.swift
613 Sources/SettingsNavigation.swift
Expand Down Expand Up @@ -227,6 +228,7 @@
518 Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/CMUXMobileRootView.swift
518 Packages/macOS/CmuxSwiftRender/Tests/CmuxSwiftRenderTests/Corpus/stress-git-review-queue-command-deck.swift
516 Sources/TerminalImageTransfer.swift
515 Packages/iOS/CmuxMobileRPC/Sources/CmuxMobileRPC/MobileCoreRPCClient.swift
514 Packages/macOS/CmuxSwiftRender/Sources/CmuxSwiftRender/ExpressionEvaluator.swift
514 cmuxUITests/UpdatePillUITests.swift
509 Packages/macOS/CMUXAgentLaunch/Sources/CMUXAgentLaunch/AgentLaunchSanitizerAdditionalPolicies.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ 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 still decode: `u` is interpreted
/// as the Mac account email, extra `e` (expiry) and `n` (display name) keys
/// are ignored, and explicit route `i` ids and endpoint `t` types are honored.
/// - 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 +45,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=2&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 Down Expand Up @@ -63,7 +62,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 +96,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
/// `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 @@ -139,6 +142,7 @@ public struct CmxPairingQRCode: Sendable {
/// this grammar. v1 URLs carry the base64 `payload` item instead.
public func isPairingCodeURL(_ components: URLComponents) -> Bool {
components.queryItems?.first(where: { $0.name == "v" })?.value == "\(Self.version)"
&& queryValue(named: "tr", in: components) != nil
Comment thread
austinywang marked this conversation as resolved.
Resolved
}

/// The integer grammar version declared by an attach URL's `v` query item,
Expand Down Expand Up @@ -169,6 +173,8 @@ public struct CmxPairingQRCode: Sendable {
///
/// The ticket comes back unscoped with an empty `macDeviceID`; the shell
/// recovers the Mac's identity post-handshake from `mobile.host.status`.
/// The `tr` ticket reference is mandatory; without it the phone has nothing
/// to redeem after Stack auth.
/// - Parameter components: The parsed `cmux-ios://attach?v=2&...` URL.
/// - Throws: ``MobileSyncPairingPayloadError/invalidURL`` for malformed
/// input and ``MobileSyncPairingPayloadError/loopbackRouteRejected``
Expand All @@ -178,6 +184,9 @@ public struct CmxPairingQRCode: Sendable {
guard isPairingCodeURL(components) else {
throw MobileSyncPairingPayloadError.invalidURL
}
guard let ticketRef = queryValue(named: "tr", in: components) else {
throw MobileSyncPairingPayloadError.invalidURL
}
let rawRoutes = (components.queryItems ?? [])
.filter { $0.name == "r" }
.compactMap(\.value)
Expand Down Expand Up @@ -208,6 +217,7 @@ public struct CmxPairingQRCode: Sendable {
macAppBuild: queryValue(named: "ab", in: components),
routes: routes,
expiresAt: nil,
ticketRef: ticketRef,
authToken: nil
)
try ticket.validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public struct CmxAttachRoute: Codable, Equatable, Sendable {
public enum CmxAttachTicketError: Error, Equatable, Sendable {
case unsupportedVersion(Int)
case noRoutes
case emptyTicketRef
case emptyAuthToken
}

Expand Down Expand Up @@ -182,6 +183,7 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
case macAppBuild
case routes
case expiresAt
case ticketRef
case authToken = "auth_token"
}

Expand Down Expand Up @@ -213,12 +215,19 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
/// The Mac app's build number, displayed with version mismatch warnings when present.
public let macAppBuild: String?
public let routes: [CmxAttachRoute]
/// When the ticket's attach token stops being usable, or `nil` for tickets
/// that never expire (the pairing QR carries no token and no expiry; Stack
/// auth is the host's sole authorization gate). Expiry is data for the
/// token consumers (`MobileCoreRPCClient`, the host's ticket store), not a
/// structural validity condition; see ``isExpired(at:)``.
/// When the ticket's attach token stops being usable, or `nil` when this
/// value carries no inline token expiry. Compact QR tickets carry a
/// `ticketRef` instead; the host-side record owns that reference's TTL.
/// Expiry is data for token consumers (`MobileCoreRPCClient`, the host's
/// ticket store), not a structural validity condition; see
/// ``isExpired(at:)``.
public let expiresAt: Date?
/// Non-secret host-side reference used by compact pairing QR codes.
///
/// The reference is resolved only after the phone authenticates to the Mac
/// with Stack auth. It is not a bearer credential; it lets the QR point at
/// the ticket record without carrying the ticket's `authToken`.
public let ticketRef: String?
public let authToken: String?

public init(from decoder: Decoder) throws {
Expand All @@ -239,6 +248,7 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
macAppBuild: container.decodeIfPresent(String.self, forKey: .macAppBuild),
routes: container.decode([CmxAttachRoute].self, forKey: .routes),
expiresAt: container.decodeIfPresent(Date.self, forKey: .expiresAt),
ticketRef: container.decodeIfPresent(String.self, forKey: .ticketRef),
authToken: try Self.decodeAuthToken(from: decoder)
)
try validate()
Expand Down Expand Up @@ -271,6 +281,7 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
macAppBuild: String? = nil,
routes: [CmxAttachRoute],
expiresAt: Date? = nil,
ticketRef: String? = nil,
authToken: String? = nil
) throws {
self.version = version
Expand All @@ -285,18 +296,24 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
self.macAppBuild = macAppBuild
self.routes = routes
self.expiresAt = expiresAt
self.ticketRef = ticketRef
self.authToken = authToken
try validate()
}

/// Structural validity only. Expiry is intentionally NOT validated here:
/// a scanned pairing QR must keep working however long it sat on screen
/// (the host authorizes by Stack account, not by ticket age). Token-based
/// consumers check ``isExpired(at:)`` where the token is actually used.
/// scanned pairing QR URLs carry no inline expiry, and the host-side
/// reference/token record owns age checks. Token-based consumers check
/// ``isExpired(at:)`` where the token is actually used.
public func validate() throws {
guard version == Self.currentVersion else {
throw CmxAttachTicketError.unsupportedVersion(version)
}
if let ticketRef {
guard !ticketRef.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw CmxAttachTicketError.emptyTicketRef
}
}
if let authToken {
guard !authToken.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw CmxAttachTicketError.emptyAuthToken
Expand All @@ -310,9 +327,9 @@ public struct CmxAttachTicket: Codable, Equatable, Sendable {
}
}

/// Whether the ticket's attach token lifetime has elapsed at `now`.
/// Tickets without an expiry (`expiresAt == nil`, e.g. decoded from the
/// pairing QR) never expire.
/// Whether this value's inline attach-token lifetime has elapsed at `now`.
/// Tickets without an inline expiry (`expiresAt == nil`, e.g. decoded from
/// the pairing QR before redeeming `ticketRef`) return `false` here.
public func isExpired(at now: Date) -> Bool {
guard let expiresAt else {
return false
Expand Down
Loading
Loading