Files
archived-orca/src/shared/ssh-retained-payload-admission.ts
T
6837a4c180 fix(ssh): a relay-era desktop can join the server another desktop on the same login runs (#28886)
* fix(ssh): a relay-era desktop can join the server another desktop on the same login runs

A desktop that still had relay-era projects on a host whose managed server
another Orca version ran could never connect: its migration skipped the
running-version adoption, so it deferred on a terminal count only a paired
server can give. Once the versions matched it failed instead because the
server already held the same repository path, added by the other desktop.

The migration now pairs with the running server like a fresh desktop does,
before any upload. Projects the server already holds at the same host path
are linked to the server's project instead of imported twice: the journal
is re-cut without them, under the same migration id, only after the server
proves it holds nothing of the earlier manifest. A conflict the server will
keep refusing now says what conflicts and how to get past it.

* fix(ssh): never link a project a bridged relay terminal still runs in

The bridge reattaches a relay pane only into its own tab, which a linked
project never sends to the server, so linking would leave that terminal
running out of sight. Refuse the link instead, naming the project and how
to end the terminals.

* test(ssh): run the link test on Node's SQLite runtime and read the host's activation record

* test(ssh): establish desktop B's profile before seeding its relay-era state

* test(ssh): connect desktop B's host explicitly in the shared-login spec

* fix(ssh): say what a linked project left behind, and never blame it for an unowned relay terminal

A project this desktop moved that the host's server already held is linked to the
server's copy, so its tabs and automations from this desktop stay behind. The
connect now carries those linked projects and the SSH host card says so, with the
automation count, instead of dropping them silently.

A bridged relay terminal of unknown or removed project still blocks a link, but its
guidance no longer names the linked project as where it runs. Both terminal
messages now offer removing the project from this desktop as the way out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 19:48:25 -07:00

225 lines
7.3 KiB
TypeScript

import type {
DirectSshAuthority,
EnrichedDetectedPort,
SshConnectionState,
SshConnectionStatus,
SshPlainSshMode,
SshProviderEpoch
} from './ssh-types'
import { admitSshManagedServerStatus } from './ssh-managed-server-status-admission'
import {
isNonNegativeSafeInteger,
isSshRetainedIdentifier,
SSH_CONNECTION_ERROR_MAX_UTF8_BYTES
} from './ssh-retained-payload-limits'
import { clampUtf8TextPrefix, measureUtf8ByteLength } from './utf8-byte-limits'
export {
isSshRetainedIdentifier,
SSH_CONNECTION_ERROR_MAX_UTF8_BYTES,
SSH_RETAINED_IDENTIFIER_MAX_UTF8_BYTES
} from './ssh-retained-payload-limits'
export const SSH_PROVIDER_EPOCH_MAX_UTF8_BYTES = 128
const SSH_PLAIN_SSH_REASON_MAX_UTF8_BYTES = 64
export const SSH_DETECTED_PORTS_MAX_ENTRIES = 50
export const SSH_DETECTED_PORT_HOST_MAX_UTF8_BYTES = 1024
export const SSH_DETECTED_PORT_PROCESS_NAME_MAX_UTF8_BYTES = 4 * 1024
export const SSH_DETECTED_PORT_ADVERTISED_URL_MAX_UTF8_BYTES = 2048
const CONNECTION_STATUSES = new Set<SshConnectionStatus>([
'disconnected',
'connecting',
'auth-failed',
'deploying-relay',
'connected',
'reconnecting',
'reconnection-failed',
'error'
])
export function isAdmissibleDirectSshAuthority(value: unknown): value is DirectSshAuthority {
if (!value || typeof value !== 'object') {
return false
}
const authority = value as Record<string, unknown>
return (
isSshRetainedIdentifier(authority.targetId) &&
isSshProviderEpoch(authority.providerEpoch) &&
isNonNegativeSafeInteger(authority.connectionGeneration)
)
}
export function admitSshConnectionState(
value: unknown,
expectedTargetId: string
): SshConnectionState | null {
if (!value || typeof value !== 'object' || !isSshRetainedIdentifier(expectedTargetId)) {
return null
}
const input = value as Record<string, unknown>
if (
(input.targetId !== undefined &&
(!isSshRetainedIdentifier(input.targetId) || input.targetId !== expectedTargetId)) ||
typeof input.status !== 'string' ||
!CONNECTION_STATUSES.has(input.status as SshConnectionStatus) ||
!isNonNegativeSafeInteger(input.reconnectAttempt) ||
(input.error !== null && typeof input.error !== 'string')
) {
return null
}
const error = clampSshConnectionError(input.error)
const hasProviderEpoch = input.providerEpoch !== undefined && input.providerEpoch !== null
const hasConnectionGeneration = input.connectionGeneration !== undefined
if (
hasProviderEpoch !== hasConnectionGeneration ||
(hasProviderEpoch &&
(!isSshProviderEpoch(input.providerEpoch) ||
!isNonNegativeSafeInteger(input.connectionGeneration)))
) {
return null
}
return {
targetId: expectedTargetId,
status: input.status as SshConnectionStatus,
error,
reconnectAttempt: input.reconnectAttempt,
providerEpoch: hasProviderEpoch ? (input.providerEpoch as SshProviderEpoch) : null,
...(hasProviderEpoch ? { connectionGeneration: input.connectionGeneration as number } : {}),
...(typeof input.supportsFolderDownload === 'boolean'
? { supportsFolderDownload: input.supportsFolderDownload }
: {}),
...(input.remotePlatform === 'linux' ||
input.remotePlatform === 'darwin' ||
input.remotePlatform === 'win32'
? { remotePlatform: input.remotePlatform }
: {}),
...admitSshPlainSshMode(input.plainSsh),
...(input.hostNodeRuntime === true ? { hostNodeRuntime: true } : {}),
...admitSshManagedServerStatus(input.managedServer)
}
}
// Why field-by-field: a malformed optional mode must drop alone, never the whole state.
function admitSshPlainSshMode(value: unknown): { plainSsh?: SshPlainSshMode } {
if (!value || typeof value !== 'object') {
return {}
}
const reason = 'reason' in value ? value.reason : undefined
const message = 'message' in value ? value.message : undefined
if (typeof reason !== 'string' || typeof message !== 'string' || !reason || !message) {
return {}
}
return {
plainSsh: {
reason: clampUtf8TextPrefix(reason, SSH_PLAIN_SSH_REASON_MAX_UTF8_BYTES),
message: clampUtf8TextPrefix(message, SSH_CONNECTION_ERROR_MAX_UTF8_BYTES)
}
}
}
export function admitSshConnectionStateForAuthorityReconciliation(
value: unknown,
expectedTargetId: string
): SshConnectionState | null {
const admitted = admitSshConnectionState(value, expectedTargetId)
if (admitted || !value || typeof value !== 'object') {
return admitted
}
const input = value as Record<string, unknown>
const hasProviderEpoch = input.providerEpoch !== undefined && input.providerEpoch !== null
const hasConnectionGeneration = input.connectionGeneration !== undefined
if (hasProviderEpoch === hasConnectionGeneration) {
return null
}
return admitSshConnectionState(
{
targetId: input.targetId,
status: input.status,
error: input.error,
reconnectAttempt: input.reconnectAttempt,
supportsFolderDownload: input.supportsFolderDownload,
remotePlatform: input.remotePlatform,
plainSsh: input.plainSsh,
hostNodeRuntime: input.hostNodeRuntime,
managedServer: input.managedServer
},
expectedTargetId
)
}
function isSshProviderEpoch(value: unknown): value is SshProviderEpoch {
return (
typeof value === 'string' &&
value.length > 0 &&
!measureUtf8ByteLength(value, {
stopAfterBytes: SSH_PROVIDER_EPOCH_MAX_UTF8_BYTES
}).exceededLimit
)
}
export function clampSshConnectionError(error: string | null): string | null {
return typeof error === 'string'
? clampUtf8TextPrefix(error, SSH_CONNECTION_ERROR_MAX_UTF8_BYTES)
: null
}
export function admitSshDetectedPorts(value: unknown): EnrichedDetectedPort[] {
if (!Array.isArray(value)) {
return []
}
const retained: EnrichedDetectedPort[] = []
const scanLimit = Math.min(value.length, SSH_DETECTED_PORTS_MAX_ENTRIES)
for (let index = 0; index < scanLimit; index += 1) {
const port = admitDetectedPort(value[index])
if (port) {
retained.push(port)
}
}
return retained
}
function admitDetectedPort(value: unknown): EnrichedDetectedPort | null {
if (!value || typeof value !== 'object') {
return null
}
const input = value as Record<string, unknown>
if (
!Number.isSafeInteger(input.port) ||
(input.port as number) < 1 ||
(input.port as number) > 65_535 ||
!isStringWithinLimit(input.host, SSH_DETECTED_PORT_HOST_MAX_UTF8_BYTES)
) {
return null
}
const processName =
typeof input.processName === 'string'
? clampUtf8TextPrefix(input.processName, SSH_DETECTED_PORT_PROCESS_NAME_MAX_UTF8_BYTES)
: undefined
const advertisedUrl = isStringWithinLimit(
input.advertisedUrl,
SSH_DETECTED_PORT_ADVERTISED_URL_MAX_UTF8_BYTES
)
? input.advertisedUrl
: undefined
return {
port: input.port as number,
host: input.host,
...(isNonNegativeSafeInteger(input.pid) && input.pid > 0 ? { pid: input.pid } : {}),
...(processName ? { processName } : {}),
...(advertisedUrl ? { advertisedUrl } : {}),
...(input.advertisedProtocol === 'http' || input.advertisedProtocol === 'https'
? { advertisedProtocol: input.advertisedProtocol }
: {})
}
}
function isStringWithinLimit(value: unknown, maxBytes: number): value is string {
return (
typeof value === 'string' &&
value.length > 0 &&
!measureUtf8ByteLength(value, { stopAfterBytes: maxBytes }).exceededLimit
)
}