Files
orca/src/shared/managed-orcad-execution-host.test.ts
T
Neil a65fdc887f feat(wsl): WSL distro hosts, main-process wiring and persistence (WSL-via-orcad C7a, experimental) (#27933)
* feat(wsl): persist WSL host deployments in their own store and type orcad deployment links by kind

* chore(wsl): drop an unused link type export

* feat(wsl): add, persist and connect WSL distro hosts behind an experimental flag (WSL-via-orcad C7a)

Main-process wiring for WSL hosts: list registered WSL 2 distros, add one (C4 deploy and
launch through C6 liveness, then persist the paired environment and its C2 record), keep a
relaunch's new device token, connect a host only for the user or an open project or terminal,
adopt holders at startup, and remove a host (release its holder, drop both records). Refuses a
distro whose orcad this desktop already reaches as another server (R25).

* fix(wsl): read distro registrations through one reader that returns the canonical unbraced GUID

C4's registry reader returned the braced GUID while the C2 record only accepts it unbraced, and
C7 parsed the same output a second time. The one reader now yields lowercase unbraced ids and
each registration's WSL version; the host distro list uses it.

* fix(wsl): refuse to connect a WSL host while the experiment is off

Adding, connecting and background connects all stop while Experimental → WSL distros as hosts
is off; removing still works. The pre-deploy refusals move to their own module.

* fix(wsl): stop a connected WSL host's server when the host is removed

Remove ended the bridge and released the distro's holder but left orcad to exit with the
distro. A connected server is now ended right away, only in a distro already running.

* fix(wsl): import the orcad host errors from their new orcad/ home

* fix(wsl): refuse WSL hosts in a build without the orcad template, and say so up front

A build shipped without the orcad deployment template let the add dialog run and fail after
seconds with a raw internal error. Main now checks the template as SSH hosts do, refuses add
and connect (and skips lazy connects and holder adoption) before any deploy work, and exposes
the capability so the UI can show it.

* test(wsl): let the e2e WSL fake re-read its distro list, to register one mid-run

* fix(wsl): end a removed host's terminals and daemon, and stop WSL hosts when the experiment is off

Remove now ends the host's terminals through orcad's terminal RPC, waits for the terminal census
to prove none remain, closes the bridge, and runs the managed decommission SSH hosts use, which
stops orcad and retires its idle terminal daemon. What it could not end is reported back.

Turning Experimental → WSL distros as hosts off closes every connection and liveness watch,
releases the holders so distros can idle-stop, and keeps liveness from relaunching for visible
work; turning it on resumes only the hosts open work needs.

* fix(wsl): keep WSL hosts out of the servers a phone reaches through this desktop

They are this desktop's own distros, connected only for work open here; a phone listing or
routing to one would wake it behind the user's back.

* fix(wsl): end a removed host's terminals through its own route, and never respawn it

Removal reached orcad with the bridge's raw pairing offer rather than the routed environment and
saved pairing the app already uses for the host; on a real orcad ending terminals failed fast and
left them, the daemon and orcad running. It now calls by environment id through the same
transport as every other call to the host, and the e2e fake runs these real steps against a
headless server.

Removal also stops tracking the host first (open work, connects, runtime calls), so nothing
reconnects it while it stops, and a spawn guard in the connector refuses any holder, deploy or
bridge for a distro whose host is gone (the respawn loop after removal). A WSL host that isn't
connected answers runtime calls with why, instead of failing as an unreachable network server.

* fix(wsl): suggest reconnecting a WSL host, not Tailscale, when it can't be reached

Unreachable-server hints are chosen by the owner host's kind: a WSL host is a distro on this
computer, so it points at its reconnect instead of the Tailscale advice for network servers.

* test(wsl): parse the terminal list removal reads, and log the e2e fake's connects to a file

* fix(wsl): close a removed host's terminals by handle and report stopped only on proof

Removal stopped terminals per workspace, so one orphan whose project was gone threw
selector_not_found and aborted before orcad was stopped. It now closes each terminal by handle,
keeps going past failures and names them, and still asks orcad to stop.

It reports stopped only when the census shows no session left, the daemon retired, and the
orcad the distro records as active is the one this computer was connected to; a re-add that
adopted an orcad the record does not name is reported instead of claimed stopped.

* feat(wsl): close WSL hosts behind an internal-only gate in every build

WSL distros as hosts must not be reachable by users until moving a project
between hosts is lossless and the feature matches today's WSL support.
ORCA_INTERNAL_WSL_HOSTS=1 opens the gate; otherwise main creates no WSL host
service (no holder adoption, wsl.exe spawn or reconnect), refuses add,
connect, remove and every runtime call to a WSL host, keeps persisted WSL
hosts out of the server list, startup status probes and AI vault scans while
leaving them on disk, and the preload exposes no WSL host API.

* fix(wsl): keep SSH and paired servers listed whatever the WSL deployment file holds

An unreadable WSL record no longer hides an SSH-linked server, and with the internal gate closed a corrupt WSL file is warned about instead of failing the whole registry.

* fix(wsl): let an unreadable WSL record hide only a loopback pairing

A WSL host always pairs over a loopback endpoint, so a stale record can no longer remove a remote paired server from the list.

* fix(wsl): ignore unreadable WSL deployment state while the internal gate is closed

A newer or stale record could otherwise hide a re-paired loopback server for users who never open the gate.
2026-10-13 08:18:08 -07:00

177 lines
6.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { buildExecutionHostRegistry } from './execution-host-registry'
import {
expandEquivalentExecutionHostIds,
indexExecutionHostsById,
pickerExecutionHosts,
widenSavedExecutionHostIds
} from './managed-orcad-execution-host'
import type { SshConnectionState } from './ssh-types'
const SSH_TARGETS = new Map([['omarchy-target', 'Omarchy']])
const MANAGED_SERVER = {
id: 'omarchy-server',
name: 'Omarchy server',
orcadDeployment: { sshTargetId: 'omarchy-target' }
}
function connectionStates(
managedServer: SshConnectionState['managedServer']
): Map<string, SshConnectionState> {
return new Map([
[
'omarchy-target',
{
targetId: 'omarchy-target',
status: 'connected',
error: null,
reconnectAttempt: 0,
managedServer
}
]
])
}
function registry(managedServer?: SshConnectionState['managedServer']) {
return buildExecutionHostRegistry({
repos: [],
settings: null,
hostSource: 'configured-only',
sshTargetLabels: SSH_TARGETS,
sshConnectionStates: managedServer ? connectionStates(managedServer) : undefined,
runtimeEnvironments: [MANAGED_SERVER]
})
}
describe('managed Orca server host merging', () => {
it('keeps both ids resolvable and shows one row routed to the server', () => {
const hosts = registry({ kind: 'managed', environmentId: 'omarchy-server' })
// Why both: setups, folders and filters are owned by one id or the other.
expect(hosts.map((host) => host.id)).toEqual([
'local',
'runtime:omarchy-server',
'ssh:omarchy-target'
])
expect(pickerExecutionHosts(hosts).map((host) => [host.id, host.label, host.detail])).toEqual([
['local', expect.any(String), 'This computer'],
['runtime:omarchy-server', 'Omarchy', 'SSH']
])
expect(hosts[1]?.aliasHostIds).toEqual(['ssh:omarchy-target'])
expect(hosts[2]).toMatchObject({
label: 'Omarchy',
mergedIntoHostId: 'runtime:omarchy-server'
})
})
it('routes to the server before main reports a route', () => {
expect(pickerExecutionHosts(registry()).map((host) => host.id)).toEqual([
'local',
'runtime:omarchy-server'
])
})
it('shows the SSH id when main reports the host on its relay', () => {
const hosts = registry({ kind: 'relay', reason: 'source_changed' })
expect(pickerExecutionHosts(hosts).map((host) => [host.id, host.label])).toEqual([
['local', expect.any(String)],
['ssh:omarchy-target', 'Omarchy']
])
expect(hosts.find((host) => host.id === 'runtime:omarchy-server')?.mergedIntoHostId).toBe(
'ssh:omarchy-target'
)
})
it('keeps a manually paired server that no SSH host deployed', () => {
const hosts = buildExecutionHostRegistry({
repos: [],
settings: null,
hostSource: 'configured-only',
sshTargetLabels: SSH_TARGETS,
runtimeEnvironments: [{ id: 'paired-server', name: 'Omarchy' }]
})
expect(pickerExecutionHosts(hosts).map((host) => host.id)).toEqual([
'local',
'runtime:paired-server',
'ssh:omarchy-target'
])
})
it('keeps a WSL-managed server as its own row, even beside an SSH host of the same name', () => {
const hosts = buildExecutionHostRegistry({
repos: [],
settings: null,
hostSource: 'configured-only',
sshTargetLabels: new Map([['Ubuntu', 'Ubuntu over SSH']]),
runtimeEnvironments: [{ id: 'wsl-server', name: 'Ubuntu', orcadDeployment: { kind: 'wsl' } }]
})
expect(pickerExecutionHosts(hosts).map((host) => [host.id, host.label])).toEqual([
['local', expect.any(String)],
['runtime:wsl-server', 'Ubuntu'],
['ssh:Ubuntu', 'Ubuntu over SSH']
])
expect(hosts.every((host) => !host.aliasHostIds && !host.mergedIntoHostId)).toBe(true)
})
it('keeps an orphaned managed server whose SSH host was removed', () => {
const hosts = buildExecutionHostRegistry({
repos: [],
settings: null,
hostSource: 'configured-only',
sshTargetLabels: new Map(),
runtimeEnvironments: [MANAGED_SERVER]
})
expect(pickerExecutionHosts(hosts).map((host) => [host.id, host.label])).toEqual([
['local', expect.any(String)],
['runtime:omarchy-server', 'Omarchy server']
])
expect(hosts[1]?.aliasHostIds).toBeUndefined()
})
it.each([
['managed', { kind: 'managed', environmentId: 'omarchy-server' } as const],
['relay', { kind: 'relay', reason: 'source_changed' } as const]
])('resolves a selection saved under either id to the %s row', (_route, managedServer) => {
const hosts = registry(managedServer)
const shownId = managedServer.kind === 'relay' ? 'ssh:omarchy-target' : 'runtime:omarchy-server'
const byId = indexExecutionHostsById(hosts)
expect(byId.get('runtime:omarchy-server')?.id).toBe(shownId)
expect(byId.get('ssh:omarchy-target')?.id).toBe(shownId)
expect(byId.get('runtime:omarchy-server')?.label).toBe('Omarchy')
expect(new Set(expandEquivalentExecutionHostIds(hosts, [shownId]))).toEqual(
new Set(['runtime:omarchy-server', 'ssh:omarchy-target'])
)
})
it('names the merged row with a rename saved on either id', () => {
const hosts = buildExecutionHostRegistry({
repos: [],
settings: null,
hostSource: 'configured-only',
sshTargetLabels: SSH_TARGETS,
runtimeEnvironments: [MANAGED_SERVER],
hostLabelOverrides: new Map([['ssh:omarchy-target', 'Build box']])
})
expect(hosts.find((host) => host.id === 'runtime:omarchy-server')?.label).toBe('Build box')
})
it('widens a host scope an older build saved with only one id of the pair', () => {
const hosts = registry({ kind: 'managed', environmentId: 'omarchy-server' })
expect(new Set(widenSavedExecutionHostIds(hosts, ['local', 'ssh:omarchy-target']))).toEqual(
new Set(['local', 'ssh:omarchy-target', 'runtime:omarchy-server'])
)
expect(
widenSavedExecutionHostIds(hosts, ['ssh:omarchy-target', 'runtime:omarchy-server'])
).toBeNull()
expect(widenSavedExecutionHostIds(hosts, ['local'])).toBeNull()
expect(widenSavedExecutionHostIds(hosts, null)).toBeNull()
})
})