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
110 changes: 110 additions & 0 deletions src/main/ipc/orcad-runtime-conversion-handlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

const mocks = vi.hoisted(() => ({
handle: vi.fn(),
convert: vi.fn(),
pending: vi.fn(),
preflight: vi.fn(),
manifest: vi.fn(),
terminals: vi.fn(),
inventory: vi.fn()
}))

vi.mock('electron', () => ({ ipcMain: { handle: mocks.handle } }))
vi.mock('../ssh/orcad-runtime-conversion', () => ({
convertSshTargetToManagedOrcad: mocks.convert
}))
vi.mock('../ssh/orcad-managed-migration-status', () => ({
listPendingManagedOrcadMigrations: mocks.pending
}))
vi.mock('../ssh/orcad-managed-runtime-context', () => ({
requireManagedOrcadInfrastructure: () => ({
targetStore: {
getOrcadMigrationSource: () => ({ getSshTarget: () => ({ id: 'ssh-1', label: 'Builder' }) })
}
})
}))
vi.mock('../ssh/ssh-target-orcad-preflight', () => ({
preflightOrcadMigrationExport: mocks.preflight
}))
vi.mock('../ssh/orcad-migration-manifest-export', () => ({
createOrcadMigrationManifest: mocks.manifest
}))
vi.mock('../ssh/orcad-migration-terminal-gate', () => ({
assessOrcadMigrationTerminals: mocks.terminals
}))
vi.mock('../ssh/orcad-migration-relay-pty-lister', () => ({
orcadMigrationRelayPtyLister: mocks.inventory
}))
vi.mock('../ssh/orcad-runtime-conversion-wiring', () => ({
conversionCollaborators: () => ({ marker: 'live-collaborators' })
}))

const { registerOrcadRuntimeConversionHandlers } =
await import('./orcad-runtime-conversion-handlers')

function handler(channel: string): (_event: unknown, args?: unknown) => unknown {
const registration = mocks.handle.mock.calls.find(([name]) => name === channel)
if (!registration) {
throw new Error(`${channel} handler was not registered`)
}
return registration[1]
}

describe('managed server conversion IPC', () => {
beforeEach(() => {
vi.clearAllMocks()
registerOrcadRuntimeConversionHandlers(() => '/profile')
})

it('previews what moves and keeps only blockers that stop the move', async () => {
mocks.preflight.mockReturnValue({
targetLabel: 'Builder',
blockers: [
{ code: 'orcad_migration_direct_ssh_repositories', category: 'drainable-static-state' },
{ code: 'orcad_migration_saved_port_forwards', category: 'client-owned-state' },
{ code: 'orcad_migration_target_owned', category: 'exclusive-ownership' }
]
})
mocks.manifest.mockReturnValue({
payload: {
repositories: [{}, {}],
projectGroups: [{}],
folderWorkspaces: [{}],
dormantState: { automations: [{}], workspaceSession: {} }
}
})
mocks.terminals.mockResolvedValue({ verdict: 'live', ptyIds: ['p'], reason: 'running' })
await expect(
handler('runtimeEnvironments:previewOrcadConversion')(null, { sshTargetId: 'ssh-1' })
).resolves.toEqual({
sshTargetId: 'ssh-1',
targetLabel: 'Builder',
moves: {
repositories: 2,
projectGroups: 1,
folderWorkspaces: 1,
automations: 1,
workspaceSession: true
},
blockers: [{ code: 'orcad_migration_target_owned', category: 'exclusive-ownership' }],
terminals: { verdict: 'live', ptyIds: ['p'], reason: 'running' }
})
expect(mocks.inventory).toHaveBeenCalledWith('ssh-1')
})

it('converts with the live collaborators and lists pending migrations from the profile', async () => {
mocks.convert.mockResolvedValue({ outcome: 'converted' })
await handler('runtimeEnvironments:convertSshHostToManagedOrcad')(null, {
sshTargetId: ' ssh-1 ',
name: 'Builder'
})
expect(mocks.convert).toHaveBeenCalledWith('/profile', {
sshTargetId: 'ssh-1',
name: 'Builder',
marker: 'live-collaborators'
})
handler('runtimeEnvironments:listPendingOrcadMigrations')(null)
expect(mocks.pending).toHaveBeenCalledWith('/profile')
})
})
79 changes: 79 additions & 0 deletions src/main/ipc/orcad-runtime-conversion-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ipcMain } from 'electron'
import type {
OrcadManagedConversionPreview,
OrcadManagedConversionResult,
OrcadManagedPendingMigrationRow
} from '../../shared/orcad-managed-runtime'
import { listPendingManagedOrcadMigrations } from '../ssh/orcad-managed-migration-status'
import { requireManagedOrcadInfrastructure } from '../ssh/orcad-managed-runtime-context'
import { createOrcadMigrationManifest } from '../ssh/orcad-migration-manifest-export'
import { assessOrcadMigrationTerminals } from '../ssh/orcad-migration-terminal-gate'
import { convertSshTargetToManagedOrcad } from '../ssh/orcad-runtime-conversion'
import { orcadMigrationRelayPtyLister } from '../ssh/orcad-migration-relay-pty-lister'
import { conversionCollaborators } from '../ssh/orcad-runtime-conversion-wiring'
import { preflightOrcadMigrationExport } from '../ssh/ssh-target-orcad-preflight'
import { requiredString } from './orcad-runtime-lifecycle-handlers'

export function registerOrcadRuntimeConversionHandlers(getUserDataPath: () => string): void {
ipcMain.handle(
'runtimeEnvironments:previewOrcadConversion',
(_event, args: { sshTargetId: string }): Promise<OrcadManagedConversionPreview> =>
previewConversion(requiredString(args?.sshTargetId, 'SSH target'))
)
ipcMain.handle(
'runtimeEnvironments:convertSshHostToManagedOrcad',
(
_event,
args: { sshTargetId: string; name: string }
): Promise<OrcadManagedConversionResult> => {
const sshTargetId = requiredString(args?.sshTargetId, 'SSH target')
return convertSshTargetToManagedOrcad(getUserDataPath(), {
sshTargetId,
name: requiredString(args?.name, 'Server name'),
...conversionCollaborators(sshTargetId)
})
}
)
ipcMain.handle(
'runtimeEnvironments:listPendingOrcadMigrations',
(): OrcadManagedPendingMigrationRow[] => listPendingManagedOrcadMigrations(getUserDataPath())
)
}

/** Read-only: exports nothing and fences nothing. */
async function previewConversion(sshTargetId: string): Promise<OrcadManagedConversionPreview> {
const { targetStore } = requireManagedOrcadInfrastructure()
const store = targetStore.getOrcadMigrationSource()
const target = store.getSshTarget(sshTargetId)
if (!target) {
throw new Error('SSH target is required.')
}
const preflight = preflightOrcadMigrationExport(store, sshTargetId)
const manifest = createOrcadMigrationManifest(store, target)
const dormant = manifest.payload.dormantState
const terminals = await assessOrcadMigrationTerminals(
store,
sshTargetId,
orcadMigrationRelayPtyLister(sshTargetId)
)
return {
sshTargetId,
targetLabel: preflight.targetLabel,
moves: {
repositories: manifest.payload.repositories.length,
projectGroups: manifest.payload.projectGroups.length,
folderWorkspaces: manifest.payload.folderWorkspaces.length,
automations: dormant?.automations?.length ?? 0,
workspaceSession: Boolean(dormant?.workspaceSession)
},
blockers: preflight.blockers.filter(
(blocker) =>
blocker.category !== 'drainable-static-state' &&
blocker.code !== 'orcad_migration_saved_port_forwards'
),
terminals:
terminals.verdict === 'exited'
? { verdict: 'exited' }
: { verdict: terminals.verdict, ptyIds: terminals.ptyIds, reason: terminals.reason }
}
}
3 changes: 3 additions & 0 deletions src/main/ipc/runtime-environment-handler-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const RUNTIME_ENVIRONMENT_HANDLER_CHANNELS = [
'runtimeEnvironments:recoverOrcad',
'runtimeEnvironments:stopOrcad',
'runtimeEnvironments:cancelOrcadStop',
'runtimeEnvironments:previewOrcadConversion',
'runtimeEnvironments:convertSshHostToManagedOrcad',
'runtimeEnvironments:listPendingOrcadMigrations',
'runtimeEnvironments:createOrcadSshHost',
'runtimeEnvironments:resumeOrcadSshHost',
'runtimeEnvironments:listPendingOrcadSshProvisioning'
Expand Down
6 changes: 6 additions & 0 deletions src/main/ipc/runtime-environments-pairing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ describe('registerRuntimeEnvironmentHandlers', () => {
'runtimeEnvironments:listPendingOrcadSshProvisioning',
'runtimeEnvironments:deployOrcad',
'runtimeEnvironments:getOrcadStatus',
'runtimeEnvironments:previewOrcadConversion',
'runtimeEnvironments:convertSshHostToManagedOrcad',
'runtimeEnvironments:listPendingOrcadMigrations',
'runtimeEnvironments:updateOrcad',
'runtimeEnvironments:rollbackOrcad',
'runtimeEnvironments:recoverOrcad',
Expand Down Expand Up @@ -199,6 +202,9 @@ describe('registerRuntimeEnvironmentHandlers', () => {
'runtimeEnvironments:recoverOrcad',
'runtimeEnvironments:stopOrcad',
'runtimeEnvironments:cancelOrcadStop',
'runtimeEnvironments:previewOrcadConversion',
'runtimeEnvironments:convertSshHostToManagedOrcad',
'runtimeEnvironments:listPendingOrcadMigrations',
'runtimeEnvironments:createOrcadSshHost',
'runtimeEnvironments:resumeOrcadSshHost',
'runtimeEnvironments:listPendingOrcadSshProvisioning',
Expand Down
2 changes: 2 additions & 0 deletions src/main/ipc/runtime-environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './runtime-environment-subscriptions'
import { RUNTIME_ENVIRONMENT_HANDLER_CHANNELS } from './runtime-environment-handler-channels'
import { registerOrcadRuntimeLifecycleHandlers } from './orcad-runtime-lifecycle-handlers'
import { registerOrcadRuntimeConversionHandlers } from './orcad-runtime-conversion-handlers'
import { registerOrcadRuntimeMaintenanceHandlers } from './orcad-runtime-maintenance-handlers'
import { registerRuntimeSshAccessHandlers } from './runtime-ssh-access-handlers'
import { retirePairedRuntimeBrowserClientHostEnvironment } from '../browser/paired-runtime-browser-client-host-runtime'
Expand Down Expand Up @@ -75,6 +76,7 @@ export function registerRuntimeEnvironmentHandlers(store: Store): void {
invalidateTransport: invalidateRuntimeEnvironmentTransport
})
registerOrcadRuntimeLifecycleHandlers({ getUserDataPath })
registerOrcadRuntimeConversionHandlers(getUserDataPath)
registerOrcadRuntimeMaintenanceHandlers({
getUserDataPath,
getActiveEnvironmentId: () => store.getSettings().activeRuntimeEnvironmentId,
Expand Down
42 changes: 42 additions & 0 deletions src/main/ssh/orcad-runtime-conversion-wiring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** The live collaborators a host conversion needs: the relay, the direct session and the server. */
import { encodePairingOffer } from '../../shared/pairing'
import {
getPreferredPairingOffer,
type KnownRuntimeEnvironment
} from '../../shared/runtime-environments'
import { disconnectRegisteredSshTarget } from '../ipc/ssh-session-teardown'
import type { OrcadMigrationDestinationCatalog } from './orcad-migration-cutover-coordinator'
import {
abortRemoteOrcadMigrationCatalog,
commitRemoteOrcadMigrationCatalog,
readRemoteOrcadMigrationCatalogState,
stageRemoteOrcadMigrationCatalog,
stageRemoteOrcadMigrationSnapshotChunk
} from './orcad-migration-catalog-client'
import { orcadMigrationRelayPtyLister } from './orcad-migration-relay-pty-lister'
import type { OrcadManagedConversionArgs } from './orcad-runtime-conversion'

/** The T6-9 client against this server, pinned to the runtime it paired with. */
export function orcadMigrationDestinationFor(
environment: KnownRuntimeEnvironment
): OrcadMigrationDestinationCatalog {
const pairingCode = encodePairingOffer(getPreferredPairingOffer(environment))
const options = environment.runtimeId ? { expectedRuntimeId: environment.runtimeId } : {}
return {
readState: (manifest) => readRemoteOrcadMigrationCatalogState(pairingCode, manifest, options),
stage: (manifest) => stageRemoteOrcadMigrationCatalog(pairingCode, manifest, options),
commit: (manifest) => commitRemoteOrcadMigrationCatalog(pairingCode, manifest, options),
abort: (manifest) => abortRemoteOrcadMigrationCatalog(pairingCode, manifest, options),
stageChunk: (request) => stageRemoteOrcadMigrationSnapshotChunk(pairingCode, request, options)
}
}

export function conversionCollaborators(
sshTargetId: string
): Pick<OrcadManagedConversionArgs, 'destinationFor' | 'listRelayPtyIds' | 'releaseDirectSession'> {
return {
destinationFor: orcadMigrationDestinationFor,
listRelayPtyIds: orcadMigrationRelayPtyLister(sshTargetId),
releaseDirectSession: disconnectRegisteredSshTarget
}
}
57 changes: 57 additions & 0 deletions src/preload/api/managed-orcad-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** Managed orcad servers over SSH; desktop-only, so web clients leave it undefined. */
import { ipcRenderer } from 'electron'
import type {
OrcadManagedCancelStopResult,
OrcadManagedConversionPreview,
OrcadManagedConversionResult,
OrcadManagedDeployResult,
OrcadManagedPendingMigrationRow,
OrcadManagedRecoveryResult,
OrcadManagedRollbackResult,
OrcadManagedRuntimeStatus,
OrcadManagedStopResult
} from '../../shared/orcad-managed-runtime'
import type { PublicKnownRuntimeEnvironment } from '../../shared/runtime-environments'
import type {
RuntimeSshAccessLinkRequest,
RuntimeSshAccessUnlinkRequest
} from '../../shared/runtime-ssh-access'

export type ManagedOrcadPreloadApi = {
deploy: (args: {
name: string
sshTargetId: string
force?: boolean
}) => Promise<OrcadManagedDeployResult>
getStatus: (args: { selector: string }) => Promise<OrcadManagedRuntimeStatus>
update: (args: { selector: string; force?: boolean }) => Promise<OrcadManagedDeployResult>
rollback: (args: { selector: string }) => Promise<OrcadManagedRollbackResult>
recover: (args: { selector: string }) => Promise<OrcadManagedRecoveryResult>
stop: (args: { selector: string }) => Promise<OrcadManagedStopResult>
cancelStop: (args: { selector: string }) => Promise<OrcadManagedCancelStopResult>
linkSshAccess: (args: RuntimeSshAccessLinkRequest) => Promise<PublicKnownRuntimeEnvironment>
unlinkSshAccess: (args: RuntimeSshAccessUnlinkRequest) => Promise<PublicKnownRuntimeEnvironment>
previewConversion: (args: { sshTargetId: string }) => Promise<OrcadManagedConversionPreview>
convertSshHost: (args: {
sshTargetId: string
name: string
}) => Promise<OrcadManagedConversionResult>
listPendingMigrations: () => Promise<OrcadManagedPendingMigrationRow[]>
}

export const managedOrcadApi: ManagedOrcadPreloadApi = {
deploy: (args) => ipcRenderer.invoke('runtimeEnvironments:deployOrcad', args),
getStatus: (args) => ipcRenderer.invoke('runtimeEnvironments:getOrcadStatus', args),
update: (args) => ipcRenderer.invoke('runtimeEnvironments:updateOrcad', args),
rollback: (args) => ipcRenderer.invoke('runtimeEnvironments:rollbackOrcad', args),
recover: (args) => ipcRenderer.invoke('runtimeEnvironments:recoverOrcad', args),
stop: (args) => ipcRenderer.invoke('runtimeEnvironments:stopOrcad', args),
cancelStop: (args) => ipcRenderer.invoke('runtimeEnvironments:cancelOrcadStop', args),
linkSshAccess: (args) => ipcRenderer.invoke('runtimeEnvironments:linkSshAccess', args),
unlinkSshAccess: (args) => ipcRenderer.invoke('runtimeEnvironments:unlinkSshAccess', args),
previewConversion: (args) =>
ipcRenderer.invoke('runtimeEnvironments:previewOrcadConversion', args),
convertSshHost: (args) =>
ipcRenderer.invoke('runtimeEnvironments:convertSshHostToManagedOrcad', args),
listPendingMigrations: () => ipcRenderer.invoke('runtimeEnvironments:listPendingOrcadMigrations')
}
3 changes: 3 additions & 0 deletions src/preload/api/runtime-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ManagedOrcadPreloadApi } from './managed-orcad-api'
import type { RuntimeHostStatusSnapshot } from '../../shared/runtime-host-status'
import type {
RuntimeBrowserDriverState,
Expand Down Expand Up @@ -141,6 +142,8 @@ export type RuntimeApi = {
onClose?: () => void
}
) => Promise<RuntimeEnvironmentSubscriptionHandle>
// Why optional: only the desktop preload deploys servers over SSH.
managedOrcad?: ManagedOrcadPreloadApi
}
wsl: {
isAvailable: () => Promise<boolean>
Expand Down
4 changes: 3 additions & 1 deletion src/preload/api/runtime-environments-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type RuntimeEnvironmentSubscriptionHandle
} from '../runtime-environment-subscriptions'
import type { PreloadApi } from '../api-types'
import { managedOrcadApi } from './managed-orcad-api'

export const runtimeEnvironmentsApi = {
getStatusSnapshots: (): Promise<RuntimeHostStatusSnapshot[]> =>
Expand Down Expand Up @@ -106,5 +107,6 @@ export const runtimeEnvironmentsApi = {
onClose?: () => void
}
): Promise<RuntimeEnvironmentSubscriptionHandle> =>
subscribeRuntimeEnvironmentFromPreload(ipcRenderer, args, callbacks)
subscribeRuntimeEnvironmentFromPreload(ipcRenderer, args, callbacks),
managedOrcad: managedOrcadApi
} satisfies PreloadApi['runtimeEnvironments']
3 changes: 3 additions & 0 deletions src/renderer/src/components/settings/ExperimentalPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { translate } from '@/i18n/i18n'
import { NativeChatExperimentalSetting } from './NativeChatExperimentalSetting'
import { AgentDashboardExperimentalSetting } from './AgentDashboardExperimentalSetting'
import { EphemeralVmsExperimentalSetting } from './EphemeralVmsExperimentalSetting'
import { ManagedServersExperimentalSetting } from './ManagedServersExperimentalSetting'
import {
MAX_AGENT_HIBERNATION_IDLE_MS,
MIN_AGENT_HIBERNATION_IDLE_MS,
Expand Down Expand Up @@ -268,6 +269,8 @@ export function ExperimentalPane({

<EphemeralVmsExperimentalSetting settings={settings} updateSettings={updateSettings} />

<ManagedServersExperimentalSetting settings={settings} updateSettings={updateSettings} />

{hiddenExperimentalUnlocked ? <HiddenExperimentalGroup /> : null}
</div>
)
Expand Down
Loading
Loading