Files
orca/src/cli/execution-host-flag.test.ts
T
Neil 9d1dfc314f fix(cli): resolve host names across both kinds, and stop ssh: answering empty (#15449)
* fix(cli): resolve host names across both kinds, and stop ssh: answering empty

`--host ssh:<id>` was never validated. An unknown target filtered to nothing and
returned ok:true with an empty list — the same silent wrong-machine answer that
unknown `runtime:` ids gave before they were rejected. And because SSH target
ids are machine-generated (`ssh-<timestamp>-<random>`) while the name anyone
actually knows is the label, this fired on the ordinary spelling rather than a
rare typo: every human-typed SSH name missed.

The two kinds of remote machine are also reached on different axes. A paired
Orca server is a connection (`--environment <name>`); an SSH target is a machine
the connected host reaches (`--host ssh:<id>`). A caller only knows "the machine
called X", so naming X on the wrong axis was the common failure and produced
either an empty answer or a dead-end "unknown environment".

Now: `ssh:` resolves labels as well as ids and rejects an unknown target with the
known ones listed; `runtime:` accepts the environment name as well as its id,
matching --environment, and canonicalizes to the id so stored host ids still
compare; and when a name misses on one axis but exists on the other, the error
says which and gives the exact flag. Candidates ride along in error.data so an
agent can recover without parsing prose.

`orca host list` is the discovery surface that was missing entirely — nothing in
the CLI listed SSH targets, so a caller told to use one had nowhere to look. It
prints this machine, the SSH targets registered on the connected host, and the
paired servers, each with the selector to use.

* fix(cli): give --environment the same cross-kind hint, and validate the ssh host on setup-create

Two gaps a follow-up survey found in the first pass.

`--environment openclaw` still dead-ended with a bare "Unknown environment"
while an SSH target by that name sat right there — the inverse of the case just
fixed, and the direction the report actually hit. The store's own error cannot
carry the hint: translateStoreError forwards code and message and drops data. So
the selector is resolved before the client is built, where the payload survives.
Only the explicit flag is asserted eagerly; an ambient ORCA_ENVIRONMENT stays
lazy, because failing local-only commands over stale background config would be
a regression.

`project setup-create` records independent metadata and, unlike the other setup
paths, is not covered by the runtime's ssh rejection — so an unknown target
persisted a row pointing at a machine that does not exist. It now resolves the
host. `local` and `runtime:` still pass through untouched: this is also the
provisioning path, where a runtime host legitimately may not exist yet when its
metadata is written.

`setup-existing-folder` and `setup-clone` deliberately keep the unresolved id.
The runtime rejects every ssh host for those operations regardless of whether it
exists, so resolving first would answer "no such target" and imply the command
would have worked with the right id.

* fix(cli): refuse an ambiguous host name instead of resolving the first match

Name lookup took the first match while the environment store itself refuses an
ambiguous name rather than guessing. That put the guess back, in the selector
whose entire purpose is to stop a command reaching a machine the caller did not
choose — and it applied to both spellings: two SSH targets sharing a label, and
two paired servers sharing a name.

Both now resolve to nothing and report every candidate with its id, so the
caller picks. An exact id still resolves past a colliding name, since an id is
never ambiguous.

Also pins the property that makes accepting a name safe at all: `runtime:<id>`
is a persisted token that lands in ProjectHostSetup.hostId and is embedded in
generated setup ids, so the name is canonicalized to the id before anything
downstream sees it. A test now asserts a name never reaches the wire.

* fix(cli): fall back to the older ssh listing so an old host is not read as having no targets

Hosts predating ssh.listTargetSummaries still answer ssh.listTargets, and both
are served by the same summariser. Swallowing the method_not_found made such a
host indistinguishable from one with no SSH targets registered, which would
reject a target id that is valid there — a new-client/old-host regression on a
path that previously passed the id through unvalidated.
2026-08-19 17:20:21 -07:00

277 lines
9.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
const { listEnvironmentsMock, resolveEnvironmentMock, getDefaultUserDataPathMock } = vi.hoisted(
() => ({
listEnvironmentsMock: vi.fn(),
resolveEnvironmentMock: vi.fn(),
getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data')
})
)
vi.mock('./runtime/environments', () => ({
addEnvironmentFromPairingCode: vi.fn(),
listEnvironments: listEnvironmentsMock,
removeEnvironment: vi.fn(),
resolveEnvironment: resolveEnvironmentMock
}))
vi.mock('./runtime-client', () => ({
getDefaultUserDataPath: getDefaultUserDataPathMock
}))
import {
assertEnvironmentSelectorResolvable,
hostFilterMatchesHostId,
parseHostFlag,
resolveHostFlagEnvironmentId
} from './execution-host-flag'
import { parseExecutionHostId } from '../shared/execution-host'
const listSshTargetsMock = vi.fn(async () => [] as { id: string; label: string }[])
const NO_SELECTION = {
listSshTargets: listSshTargetsMock,
pairingCode: null,
environmentSelector: null
}
function flags(entries: Record<string, string | boolean>): Map<string, string | boolean> {
return new Map(Object.entries(entries))
}
function environment(id: string, name = id) {
return { id, name }
}
describe('parseHostFlag', () => {
it('returns undefined when --host is absent', () => {
expect(parseHostFlag(flags({}))).toBeUndefined()
})
it('rejects a --host flag with no value', () => {
expect(() => parseHostFlag(flags({ host: true }))).toThrow('Missing value for --host')
})
it.each(['runtime:', 'ssh:', 'nonsense'])('rejects the unparseable host id %s', (value) => {
expect(() => parseHostFlag(flags({ host: value }))).toThrow(`Invalid --host value: ${value}`)
})
it('parses the three supported host kinds', () => {
expect(parseHostFlag(flags({ host: 'local' }))?.kind).toBe('local')
expect(parseHostFlag(flags({ host: 'ssh:box-1' }))?.kind).toBe('ssh')
expect(parseHostFlag(flags({ host: 'runtime:env-1' }))).toEqual({
kind: 'runtime',
id: 'runtime:env-1',
environmentId: 'env-1'
})
})
})
describe('resolveHostFlagEnvironmentId', () => {
it('keeps the current connection for local and ssh hosts', async () => {
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'local' }), NO_SELECTION)
).resolves.toBe(null)
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'ssh:box-1' }), NO_SELECTION)
).resolves.toBe(null)
expect(listEnvironmentsMock).not.toHaveBeenCalled()
})
it('routes a paired runtime host to that environment', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'gpu')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-1' }), NO_SELECTION)
).resolves.toBe('env-1')
})
it('rejects a runtime host id that no paired environment owns', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'gpu')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-missing' }), NO_SELECTION)
).rejects.toThrow('no paired Orca server is named or has id env-missing')
})
// Why: the name is what a person or agent actually knows; requiring the raw uuid made the
// obvious spelling fail against a server that is sitting right there.
it('accepts the environment name as well as its id', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'gpu')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:gpu' }), NO_SELECTION)
).resolves.toBe('env-1')
})
it('decodes percent-encoded environment ids', async () => {
listEnvironmentsMock.mockReturnValue([environment('env one')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env%20one' }), NO_SELECTION)
).resolves.toBe('env one')
})
it('refuses a runtime host alongside --pairing-code', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-1' }), {
listSshTargets: listSshTargetsMock,
pairingCode: 'orca://pair?x',
environmentSelector: null
})
).rejects.toThrow('not both')
})
it('refuses a runtime host that disagrees with --environment', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1')])
resolveEnvironmentMock.mockReturnValue(environment('env-2'))
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-1' }), {
listSshTargets: listSshTargetsMock,
pairingCode: null,
environmentSelector: { value: 'other', label: '--environment' }
})
).rejects.toThrow('name different Orca servers')
})
it('names the ambient variable when ORCA_ENVIRONMENT is the conflicting selector', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1')])
resolveEnvironmentMock.mockReturnValue(environment('env-2'))
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-1' }), {
listSshTargets: listSshTargetsMock,
pairingCode: null,
environmentSelector: { value: 'staging', label: 'ORCA_ENVIRONMENT' }
})
).rejects.toThrow('ORCA_ENVIRONMENT staging name different Orca servers')
})
it('hands an agent the known environment ids to retry with', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'gpu')])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:missing' }), NO_SELECTION)
).rejects.toMatchObject({
code: 'invalid_argument',
data: { knownEnvironments: [{ id: 'env-1', name: 'gpu' }] }
})
})
it('accepts --environment naming the same server as the runtime host', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'gpu')])
resolveEnvironmentMock.mockReturnValue(environment('env-1', 'gpu'))
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-1' }), {
listSshTargets: listSshTargetsMock,
pairingCode: null,
environmentSelector: { value: 'gpu', label: '--environment' }
})
).resolves.toBe('env-1')
})
})
describe('hostFilterMatchesHostId', () => {
const runtimeHost = parseExecutionHostId('runtime:env-1')!
const localHost = parseExecutionHostId('local')!
const sshHost = parseExecutionHostId('ssh:box-1')!
it('matches the identical host id', () => {
expect(hostFilterMatchesHostId(runtimeHost, 'runtime:env-1')).toBe(true)
expect(hostFilterMatchesHostId(localHost, 'local')).toBe(true)
expect(hostFilterMatchesHostId(sshHost, 'ssh:box-1')).toBe(true)
})
it('treats the routed runtime own local rows as that runtime', () => {
expect(hostFilterMatchesHostId(runtimeHost, 'local')).toBe(true)
})
it('does not widen local or ssh filters', () => {
expect(hostFilterMatchesHostId(localHost, 'runtime:env-1')).toBe(false)
expect(hostFilterMatchesHostId(sshHost, 'local')).toBe(false)
})
it('does not match a different runtime host', () => {
expect(hostFilterMatchesHostId(runtimeHost, 'runtime:env-2')).toBe(false)
expect(hostFilterMatchesHostId(runtimeHost, null)).toBe(false)
})
})
describe('ambiguous environment names', () => {
it('refuses --host runtime:<name> when two servers share that name', async () => {
listEnvironmentsMock.mockReturnValue([
environment('env-1', 'awin'),
environment('env-2', 'awin')
])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:awin' }), NO_SELECTION)
).rejects.toThrow('2 paired servers are named awin')
})
it('refuses --environment <name> when two servers share that name', async () => {
listEnvironmentsMock.mockReturnValue([
environment('env-1', 'awin'),
environment('env-2', 'awin')
])
await expect(assertEnvironmentSelectorResolvable('awin', listSshTargetsMock)).rejects.toThrow(
'2 paired servers are named awin'
)
})
it('still routes an exact id past a colliding name', async () => {
listEnvironmentsMock.mockReturnValue([
environment('env-1', 'awin'),
environment('env-2', 'awin')
])
await expect(
resolveHostFlagEnvironmentId(flags({ host: 'runtime:env-2' }), NO_SELECTION)
).resolves.toBe('env-2')
})
})
describe('assertEnvironmentSelectorResolvable', () => {
it('accepts a paired server by name or id', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'awin')])
listSshTargetsMock.mockClear()
await expect(
assertEnvironmentSelectorResolvable('awin', listSshTargetsMock)
).resolves.toBeUndefined()
await expect(
assertEnvironmentSelectorResolvable('env-1', listSshTargetsMock)
).resolves.toBeUndefined()
expect(listSshTargetsMock).not.toHaveBeenCalled()
})
// Why: the inverse of the --host case, and the one the report actually hit — a name that is
// an SSH target dead-ended with a bare "unknown environment".
it('names the SSH target when the selector is one', async () => {
listEnvironmentsMock.mockReturnValue([environment('env-1', 'awin')])
listSshTargetsMock.mockResolvedValue([{ id: 'ssh-1-a', label: 'openclaw' }])
await expect(
assertEnvironmentSelectorResolvable('openclaw', listSshTargetsMock)
).rejects.toMatchObject({
code: 'invalid_argument',
data: {
nextSteps: expect.arrayContaining([expect.stringContaining('--host ssh:ssh-1-a')])
}
})
})
it('still points at host list when the name is on neither axis', async () => {
listEnvironmentsMock.mockReturnValue([])
listSshTargetsMock.mockResolvedValue([])
await expect(
assertEnvironmentSelectorResolvable('nowhere', listSshTargetsMock)
).rejects.toThrow('no paired Orca server is named or has id nowhere')
})
})