fix(terminals): negotiate explicit close intent for paired runtimes (#10129)

This commit is contained in:
OrcaWin
2026-07-27 15:22:55 -07:00
committed by GitHub
parent 0f91af821d
commit 24706ccff0
29 changed files with 1231 additions and 45 deletions
+2
View File
@@ -63,6 +63,7 @@ export const CODEX_RESET_CREDIT_RUNTIME_CAPABILITY = 'accounts.codex-reset-credi
// Why: older hosts cannot reconcile terminal.create's mutation after losing the reply, so clients may only retry unknown outcomes when advertised.
export const TERMINAL_CREATE_IDEMPOTENCY_RUNTIME_CAPABILITY =
'terminal.create-idempotency.v2' as const
export const SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY = 'session-tabs.close-intent.v1' as const
export { REMOTE_SERVER_UPDATE_CAPABILITY } from './remote-server-update'
export const AGENT_SESSION_HOST_AUTHORITY_RUNTIME_CAPABILITY =
'agent-session.host-authority.v1' as const
@@ -95,6 +96,7 @@ export const RUNTIME_CAPABILITIES = [
TERMINAL_QUICK_COMMANDS_RUNTIME_CAPABILITY,
WORKTREE_CREATE_IDEMPOTENCY_RUNTIME_CAPABILITY,
TERMINAL_CREATE_IDEMPOTENCY_RUNTIME_CAPABILITY,
SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY,
REMOTE_SERVER_UPDATE_CAPABILITY,
AGENT_SESSION_HOST_AUTHORITY_RUNTIME_CAPABILITY,
AGENT_SESSION_OMP_RESUME_PATH_RUNTIME_CAPABILITY,
+13 -1
View File
@@ -13,6 +13,7 @@ import {
} from './e2ee-crypto'
import { sendRemoteRuntimeRequest, subscribeRemoteRuntimeRequest } from './remote-runtime-client'
import { MAX_TIMER_DELAY_MS } from './timer-delay'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
const servers: WebSocketServer[] = []
@@ -65,6 +66,11 @@ describe('subscribeRemoteRuntimeRequest', () => {
expect.objectContaining({ ok: true, result: { type: 'subscribed' } })
)
)
await expect(server.nextAuth).resolves.toEqual({
type: 'e2ee_auth',
deviceToken: 'device-token',
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
const bytes = new Uint8Array([1, 2, 3])
expect(subscription.sendBinary(bytes)).toBe(true)
await expect(server.nextBinary).resolves.toEqual(bytes)
@@ -300,12 +306,17 @@ async function createSubscriptionServer(
): Promise<{
pairing: PairingOffer
nextBinary: Promise<Uint8Array>
nextAuth: Promise<unknown>
}> {
const serverKeyPair = generateKeyPair()
let resolveBinary: (bytes: Uint8Array) => void = () => {}
const nextBinary = new Promise<Uint8Array>((resolve) => {
resolveBinary = resolve
})
let resolveAuth: (auth: unknown) => void = () => {}
const nextAuth = new Promise<unknown>((resolve) => {
resolveAuth = resolve
})
const wss = new WebSocketServer({ port: 0, autoPong: options.disableAutoPong !== true })
servers.push(wss)
@@ -341,6 +352,7 @@ async function createSubscriptionServer(
return
}
if (!authenticated) {
resolveAuth(JSON.parse(plaintext))
authenticated = true
sendEncrypted(ws, sharedKey, { type: 'e2ee_authenticated' })
return
@@ -379,7 +391,7 @@ async function createSubscriptionServer(
if (!pairing) {
throw new Error('Failed to create test pairing')
}
return { pairing, nextBinary }
return { pairing, nextBinary, nextAuth }
}
function sendEncrypted(ws: WebSocket, sharedKey: Uint8Array, message: unknown): void {
+5 -2
View File
@@ -21,6 +21,7 @@ import {
type RuntimeOrchestrationEnvelope,
type RuntimeRpcResponse
} from './runtime-rpc-envelope'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
// Re-export so existing value importers of `RemoteRuntimeClientError` are
// unaffected; the class lives in a ws-free module so type-only consumers
// (and mobile's typecheck) don't compile this file's Node-only deps.
@@ -94,7 +95,8 @@ export async function sendRemoteRuntimeRequest<TResult>(
const requestId = randomUUID()
const serializedAuth = serializeRemoteRuntimePayload({
type: 'e2ee_auth',
deviceToken: pairing.deviceToken
deviceToken: pairing.deviceToken,
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
const pendingRequest = {
preparedRequest: prepareRemoteRuntimeRequest(new Map(), () =>
@@ -410,7 +412,8 @@ export async function subscribeRemoteRuntimeRequest<TResult>(
})
const serializedAuth = serializeRemoteRuntimePayload({
type: 'e2ee_auth',
deviceToken: pairing.deviceToken
deviceToken: pairing.deviceToken,
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
return await new Promise((resolve, reject) => {
const keyPair = generateKeyPair()
@@ -11,11 +11,13 @@ import {
publicKeyToBase64
} from './e2ee-crypto'
import { RemoteRuntimeRequestConnection } from './remote-runtime-request-connection'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
type TestServer = {
wss: WebSocketServer
pairing: PairingOffer
requests: unknown[]
auths: unknown[]
connectionCount: () => number
}
@@ -54,6 +56,11 @@ describe('RemoteRuntimeRequestConnection', () => {
_meta: { runtimeId: 'runtime-test' }
})
expect(server.connectionCount()).toBe(1)
expect(server.auths).toContainEqual(
expect.objectContaining({
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
)
expect(server.requests).toMatchObject([
{ method: 'status.get' },
{ method: 'terminal.send', params: { terminal: 't1', text: 'ab' } }
@@ -66,6 +73,7 @@ describe('RemoteRuntimeRequestConnection', () => {
async function createServer(): Promise<TestServer> {
const serverKeyPair = generateKeyPair()
const requests: unknown[] = []
const auths: unknown[] = []
let connectionCount = 0
const wss = new WebSocketServer({ port: 0 })
servers.push(wss)
@@ -94,7 +102,12 @@ async function createServer(): Promise<TestServer> {
}
if (!authenticated) {
const auth = JSON.parse(plaintext) as { type: string; deviceToken: string }
expect(auth).toEqual({ type: 'e2ee_auth', deviceToken: 'device-token' })
auths.push(auth)
expect(auth).toEqual({
type: 'e2ee_auth',
deviceToken: 'device-token',
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
authenticated = true
sendEncrypted(ws, sharedKey, { type: 'e2ee_authenticated' })
return
@@ -133,6 +146,7 @@ async function createServer(): Promise<TestServer> {
wss,
pairing,
requests,
auths,
connectionCount: () => connectionCount
}
}
@@ -30,6 +30,7 @@ import {
type RemoteRuntimeRequestReadyWaiter
} from './remote-runtime-request-ready-waiters'
import { openRemoteRuntimeWebSocket } from './remote-runtime-request-websocket'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
type ConnectionState = 'closed' | 'awaiting_ready' | 'awaiting_authenticated' | 'ready'
@@ -205,7 +206,8 @@ export class RemoteRuntimeRequestConnection {
encrypt(
serializeRemoteRuntimePayload({
type: 'e2ee_auth',
deviceToken: this.pairing.deviceToken
deviceToken: this.pairing.deviceToken,
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
}),
sharedKey
)
@@ -20,12 +20,14 @@ import { getRemoteRuntimeRequestAdmissionEvidence } from './remote-runtime-prepa
import { RemoteRuntimeSharedControlConnection } from './remote-runtime-shared-control-connection'
import * as sharedControlProtocol from './remote-runtime-shared-control-protocol'
import { isRuntimeSubscriptionReplayResponse } from './runtime-subscription-replay'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
const TEST_PROJECT_PATH = path.join('tmp', 'project')
type TestServer = {
pairing: PairingOffer
requests: { id: string; method: string; params?: unknown }[]
auths: unknown[]
connectionCount: () => number
flushDelayedResponses: () => void
}
@@ -57,6 +59,11 @@ describe('RemoteRuntimeSharedControlConnection', () => {
expect(first).toMatchObject({ ok: true, result: { method: 'worktree.ps' } })
expect(second).toMatchObject({ ok: true, result: { method: 'session.tabs.listAll' } })
expect(server.connectionCount()).toBe(1)
expect(server.auths).toContainEqual({
type: 'e2ee_auth',
deviceToken: 'device-token',
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
expect(server.requests.map((request) => request.method)).toEqual([
'worktree.ps',
'session.tabs.listAll'
@@ -667,6 +674,7 @@ async function createServer(
): Promise<TestServer> {
const serverKeyPair = generateKeyPair()
const requests: TestServer['requests'] = []
const auths: unknown[] = []
const delayedResponses: (() => void)[] = []
let connectionCount = 0
let closedAfterFirstStreamingResponse = false
@@ -699,6 +707,7 @@ async function createServer(
return
}
if (!authenticated) {
auths.push(JSON.parse(plaintext))
authenticated = true
sendEncrypted(ws, sharedKey, { type: 'e2ee_authenticated' })
if (options.sendBinaryAfterAuth) {
@@ -742,6 +751,7 @@ async function createServer(
return {
pairing,
requests,
auths,
connectionCount: () => connectionCount,
flushDelayedResponses: () => delayedResponses.splice(0).forEach((send) => send())
}
@@ -1,5 +1,6 @@
import { parseAuthenticatedFrame, parseReadyFrame } from './remote-runtime-request-frames'
import type { RemoteRuntimeClientError } from './remote-runtime-client-error'
import { SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY } from './protocol-version'
import { dispatchSharedControlFrame } from './remote-runtime-shared-control-frame-dispatch'
import { parseSharedControlFrame } from './remote-runtime-shared-control-protocol'
import { resolveSharedControlReadyWaiters } from './remote-runtime-shared-control-state'
@@ -32,7 +33,11 @@ export function handleSharedControlTextFrame(args: {
return
}
args.setState('awaiting_authenticated')
args.sendEncrypted({ type: 'e2ee_auth', deviceToken: args.deviceToken })
args.sendEncrypted({
type: 'e2ee_auth',
deviceToken: args.deviceToken,
clientCapabilities: [SESSION_TAB_CLOSE_INTENT_RUNTIME_CAPABILITY]
})
return
}