Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 52 additions & 1 deletion mobile/src/transport/rpc-client-delivery-ambiguity.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { connect } from './rpc-client'
import { isRpcDeliveryUnknown } from './rpc-delivery-ambiguity'
import {
createStableLogicalRpcClient,
isLogicalClientCutoverError,
LogicalClientCutoverError
} from './stable-logical-rpc-client'

vi.mock('./e2ee', () => ({
generateKeyPair: () => ({
Expand Down Expand Up @@ -72,7 +77,7 @@ function hasSentRequest(socket: MockWebSocket, method: string): boolean {

function connectAuthenticated(): { client: ReturnType<typeof connect>; socket: MockWebSocket } {
const client = connect('ws://desktop.invalid', 'token', 'server-key')
const socket = mockSockets[0]!
const socket = mockSockets[mockSockets.length - 1]!
socket.open()
socket.receive(JSON.stringify({ type: 'e2ee_ready' }))
socket.receive('encrypted:{"type":"e2ee_authenticated"}')
Expand All @@ -94,6 +99,52 @@ describe('mobile rpc-client delivery ambiguity marking', () => {
globalThis.WebSocket = originalWebSocket
})

it.each([true, false])(
'preserves physical delivery evidence at the cutover caller (sent=%s)',
async (sent) => {
const physical = sent
? connectAuthenticated()
: {
client: connect('ws://desktop.invalid', 'token', 'server-key'),
socket: mockSockets[0]!
}
const client = createStableLogicalRpcClient(physical.client, 'lan')
const replacement = connectAuthenticated()
const requestError = client
.sendRequest('worktree.create', { name: 'new' })
.catch((error: unknown) => error)
await Promise.resolve()
expect(hasSentRequest(physical.socket, 'worktree.create')).toBe(sent)

await client.migrateTo(replacement.client, 'relay')

const error = await requestError
expect(isLogicalClientCutoverError(error)).toBe(true)
expect(isRpcDeliveryUnknown(error)).toBe(sent)
expect(error).toBeInstanceOf(LogicalClientCutoverError)
expect(isRpcDeliveryUnknown(error instanceof Error ? error.cause : null)).toBe(sent)
expect(hasSentRequest(replacement.socket, 'worktree.create')).toBe(false)
expect(
physical.socket.sent.filter((payload) => payload.includes('worktree.create'))
).toHaveLength(sent ? 1 : 0)
client.close()
}
)

it('recognizes a cutover by class even when its message changes', () => {
const error = new LogicalClientCutoverError()
error.message = 'wrapped migration'
expect(isLogicalClientCutoverError(error)).toBe(true)
})

it('recognizes a cutover message from another bundle copy', () => {
expect(isLogicalClientCutoverError(new Error('RPC interrupted by connection migration'))).toBe(
true
)
expect(isLogicalClientCutoverError(new Error('Client closed'))).toBe(false)
expect(isLogicalClientCutoverError('RPC interrupted by connection migration')).toBe(false)
})

it('marks in-flight requests as delivery-unknown when the socket drops', async () => {
const { client, socket } = connectAuthenticated()
const requestError = client.sendRequest('terminal.send', { terminal: 't' }).then(
Expand Down
13 changes: 13 additions & 0 deletions mobile/src/transport/rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ export type RpcClient = UnvalidatedRpcRequestPort & {
getLastInboundAt?: () => number | null
onStateChange: (listener: (state: ConnectionState) => void) => () => void
notifyForeground: (reason?: ForegroundNudgeReason) => void
/**
* Must settle every pending `sendRequest` promise before returning.
*
* `StableLogicalRpcClient.migrateTo` no longer rejects pendings itself — the physical
* sender is the only layer that knows whether a request reached the wire, so
* `previous.close()` is the sole settlement path for the retiring generation. An
* implementation that leaves a request pending strands its caller for good.
*
* Requests that did reach the wire must reject with a delivery-unknown error
* (`markRpcDeliveryUnknown`), since the host may already have executed them. Pinned
* against the real clients in `rpc-client-delivery-ambiguity.test.ts` (direct) and
* `mobile-relay-rpc-session.test.ts` (relay) — a new implementation needs its own case.
*/
close: () => void
}

Expand Down
1 change: 1 addition & 0 deletions mobile/src/transport/stable-logical-rpc-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('stable logical RPC client', () => {
const nextSession = new FakeSession('connecting')
const pending = deferred<RpcResponse>()
oldSession.sendRequest.mockReturnValue(pending.promise)
oldSession.close.mockImplementation(() => pending.reject(new Error('Client closed')))
nextSession.sendRequest.mockResolvedValue(success('next'))
const client = createStableLogicalRpcClient(oldSession, 'lan')
const stream = vi.fn()
Expand Down
29 changes: 13 additions & 16 deletions mobile/src/transport/stable-logical-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
import { waitForAuthenticated } from './replacement-session-authentication'
import { projectMobileRpcRequestParams } from './mobile-rpc-request-projection'
import { LogicalClientConnectionPath } from './logical-client-connection-path'
import { isRpcDeliveryUnknown, markRpcDeliveryUnknown } from './rpc-delivery-ambiguity'

export type MobileConnectionPath = 'lan' | 'tailscale' | 'relay'

export class LogicalClientCutoverError extends Error {
constructor() {
super('RPC interrupted by connection migration')
constructor(cause?: unknown) {
super('RPC interrupted by connection migration', { cause })
if (isRpcDeliveryUnknown(cause)) {
markRpcDeliveryUnknown(this)
}
}
}

Expand All @@ -33,10 +37,6 @@ type SubscriptionRecord = {
cancelled: boolean
}

type PendingRequest = {
reject: (error: Error) => void
}

export type StableLogicalRpcClient = RpcClient & {
migrateTo(
session: RpcClient,
Expand Down Expand Up @@ -75,7 +75,6 @@ export function createStableLogicalRpcClient(
let nextSubscriptionId = 0
let activeStateUnsubscribe: (() => void) | null = null
const subscriptions = new Map<number, SubscriptionRecord>()
const pendingRequests = new Set<PendingRequest>()
const stateListeners = new Set<(state: ConnectionState) => void>()
let state = initialSession.getState()
const connectionPath = new LogicalClientConnectionPath(() => state === 'connected')
Expand All @@ -90,22 +89,23 @@ export function createStableLogicalRpcClient(
if (suspended) {
return Promise.reject(new Error('Client suspended'))
}
const requestGeneration = generation
const session = activeSession
return new Promise<RpcResponse>((resolve, reject) => {
const pending = { reject }
pendingRequests.add(pending)
void session
.sendRequest(method, projectMobileRpcRequestParams(method, params), options)
.then(
(response) => {
pendingRequests.delete(pending)
// A correlated response is definitive even if close/cutover won the
// callback race after the physical promise had already settled.
resolve(response)
},
(error: unknown) => {
pendingRequests.delete(pending)
reject(error)
// Why: the retiring physical session settles this, so keep its error as the
// cause — it is the only evidence of whether the frame reached the wire.
reject(
requestGeneration !== generation ? new LogicalClientCutoverError(error) : error
)
}
)
})
Expand Down Expand Up @@ -260,15 +260,12 @@ export function createStableLogicalRpcClient(
suspended = false
previousStateUnsubscribe?.()
bindActiveState(nextSession, nextGeneration)
for (const pending of pendingRequests) {
pending.reject(new LogicalClientCutoverError())
}
pendingRequests.clear()
state = nextSession.getState()
connectionPath.clearAfterConnected()
for (const listener of stateListeners) {
listener(state)
}
// Only the physical sender knows whether a pending request reached the wire.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
previous.close()
},

Expand Down
6 changes: 5 additions & 1 deletion mobile/src/worktree/home-host-worktree-fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ function fakeSession(): FakeSession {
getLastConnectedAt: () => null,
onStateChange: () => () => {},
notifyForeground: () => {},
close: () => {}
close: () => {
for (const settle of pending.splice(0)) {
settle(new Error('Client closed'))
}
}
}
}
return fake
Expand Down