Files
orca/src/main/ssh/ssh-target-readoption.test.ts
T
Jinjing cda2280d63 Show all automations (#16532)
* Add all-host automations with scoped ownership and multi-authority suppo

Enable automations to run on multiple hosts (SSH targets and local) with
owner-fenced mutations, scoped list queries per host, and conflict
resolution. Introduces desktop and runtime authorities as distinct
automation storage owners, with per-host caching, invalidation, and
retry scheduling on the renderer. Captures registration generations for
SSH hosts to survive re-adoption. Adds CLI support for destination
selection and conflict recovery.

* Filter automation create projects by destination host

Only offer projects available on the selected destination, preventing
the mismatches that would fail at submit time. Auto-adjust the project
selection if it becomes unavailable when the destination changes.

* Add runtime storage authority support for automations

- Support both runtime and desktop as automation storage authorities
- Make owner preconditions optional for legacy-client compatibility
- Cache automation list projections to improve performance
- Add per-row repo/worktree resolution for cross-authority collisions
- Extend automation.list RPC to always include owner metadata

* Replace child_process.execFile with runProcess for external automations

- Migrate external-manager to use cross-platform runProcess wrapper per child-process safety policy
- Abstract electron app/ipcMain APIs in orca-runtime via environment accessors
- Install fake app environment in automation tests for consistent setup
- Reorganize imports to use specific module paths (ssh-target-registry, agent-detection, browser-error)
- Remove external-manager from child-process import allowlists (no longer violates direct import)

* Unify desktop automation CRUD onto the local runtime RPC surface

The desktop authority now speaks the same automation.* RPC contract as
remote runtimes, via callRuntimeRpc({kind:'local'}) -> runtime:call ->
the shared RpcDispatcher. The automations:list/listRuns/create/update/
delete/runNow IPC arms, their preload members, and every renderer
desktop-vs-runtime transport fork are retired; the runtime methods are
the single implementation of scoped lists, owner fencing, and change
publication for both transports (mobile clients already exercised them).

The desktop probe scheduler's priority lease survives the move as an
AutomationService hook the IPC registration installs and the runtime
methods take, so Orca's own automation traffic still parks queued
external-manager probes.

External-manager scope arms and dispatch-loop plumbing stay on IPC by
design; automation change events keep their existing channels (renderer
ingestion already converges them by authority).

* Remove automation ghost SSH tombstone scanning

This functionality for synthesizing tombstones for automation-referenced SSH
targets is no longer needed as part of the automation system refactoring.

* Refuse orphan automations at dispatch time, not migration time

Remove migration-time disabling of orphan automations and the `enabledDecidedBy` field. Dispatch now refuses orphans at runtime instead, simplifying state management and UI. Orphans are left unstamped and enabled; dispatch refuses to run them via `resolveAutomationRunTarget`.

* Show all automations in flat table with unified filter menu

- Replace host picker component with comprehensive Filters menu supporting status, last run, agent, and host filters
- Flatten automation list layout to single table instead of host-grouped sections
- Add Host column to display execution host for each automation
- Display active filters as removable pills below toolbar
- Delete unused AutomationHostPicker* components

* Add automation owner fencing and destination validation

- New AUTOMATION_OWNER_FENCING_RUNTIME_CAPABILITY for owner preconditions; legacy clients get owner metadata snapshotted at RPC boundary for compatibility
- Editor captures and revalidates automation destination before save, preventing silent retargeting if SSH infrastructure changes mid-edit
- SSH target types now isolate renderer-authored fields; generation is server-owned and stripped by IPC handlers

* Route automation recovery actions to the origin host

When an automation action fails due to owner fencing, recovery verbs
("Update server", "Reconnect") must run on the host where the refusal
originated: the row's captured owner for row operations, or the
destination the create dialog captured, not the list's filtered host.

* Remove external manager scope limitation notices

Consolidate create destination eligibility checks with a unified predicate
and fix the bug where desktop repo IDs could be sent to runtime hosts where
they cannot resolve.

* Persist only store-derived automation contexts, not client-perspective o

Store contexts must never be based on client-provided runContext or sourceContext
values—clients speak a different perspective (e.g., 'runtime:<id>' for host IDs
they assign), and persisting those makes the store projection orphan automations
it actually owns. Derived contexts now take precedence in create and update paths,
with explicit null still honored to clear a value. Tests verify this by simulating
drift after storage and confirming that moves re-derive while toggles preserve.
2026-08-26 09:50:12 -07:00

203 lines
7.0 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
buildRemovedSshTargetTombstone,
readoptOrphanedWorkspacesForTarget
} from './ssh-target-readoption'
import type { Store } from '../persistence'
import type { RemovedSshTargetTombstone, SshTarget } from '../../shared/ssh-types'
function makeTarget(overrides: Partial<SshTarget> = {}): SshTarget {
return {
id: 'ssh-new',
label: 'Dev box',
host: 'dev.example.com',
port: 22,
username: 'tim',
...overrides
}
}
/** Minimal in-memory store exposing only what re-adoption touches. */
function makeFakeStore(tombstones: RemovedSshTargetTombstone[]) {
const reassigned: { oldId: string; newId: string }[] = []
let current = [...tombstones]
const store = {
getRemovedSshTargetTombstones: () => [...current],
releaseRemovedSshTargetTombstone: (oldTargetId: string) => {
current = current.filter((t) => t.oldTargetId !== oldTargetId)
},
reassignSshTargetId: (oldTargetId: string, newTargetId: string) => {
reassigned.push({ oldId: oldTargetId, newId: newTargetId })
return ['repo-1']
}
} as unknown as Store
return { store, reassigned, remaining: () => current }
}
const tombstone = (
overrides: Partial<RemovedSshTargetTombstone> = {}
): RemovedSshTargetTombstone => ({
oldTargetId: 'ssh-old',
host: 'dev.example.com',
port: 22,
username: 'tim',
label: 'Dev box',
removedAt: 1,
...overrides
})
describe('readoptOrphanedWorkspacesForTarget', () => {
it('re-adopts on matching configHost alias', () => {
const fake = makeFakeStore([tombstone({ configHost: 'devbox', host: 'changed.example.com' })])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ configHost: 'devbox', host: 'now.example.com' })
)
expect(readoptions).toEqual([
{ oldTargetId: 'ssh-old', newTargetId: 'ssh-new', repoIds: ['repo-1'] }
])
expect(fake.reassigned).toEqual([{ oldId: 'ssh-old', newId: 'ssh-new' }])
expect(fake.remaining()).toHaveLength(0) // tombstone consumed
})
it('re-adopts on matching host+user+port when no alias', () => {
const fake = makeFakeStore([tombstone()])
const readoptions = readoptOrphanedWorkspacesForTarget(fake.store, makeTarget())
expect(readoptions).toEqual([
{ oldTargetId: 'ssh-old', newTargetId: 'ssh-new', repoIds: ['repo-1'] }
])
expect(fake.reassigned).toEqual([{ oldId: 'ssh-old', newId: 'ssh-new' }])
})
it('does not re-adopt when identity differs', () => {
const fake = makeFakeStore([tombstone({ host: 'other.example.com', username: 'root' })])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ host: 'dev.example.com', username: 'tim' })
)
expect(readoptions).toEqual([])
expect(fake.reassigned).toEqual([])
expect(fake.remaining()).toHaveLength(1) // tombstone left for a future match
})
it('matches host/user/port case-insensitively', () => {
const fake = makeFakeStore([tombstone({ host: 'Dev.Example.COM', username: 'Tim' })])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ host: 'dev.example.com', username: 'tim' })
)
expect(readoptions).toHaveLength(1)
})
it('does not match alias against a different host tuple', () => {
// Different alias AND different tuple => no match.
const fake = makeFakeStore([
tombstone({ configHost: 'prod', host: 'prod.example.com', username: 'root' })
])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ configHost: 'devbox', host: 'dev.example.com', username: 'tim' })
)
expect(readoptions).toEqual([])
})
it('is a no-op when there are no tombstones', () => {
const fake = makeFakeStore([])
expect(readoptOrphanedWorkspacesForTarget(fake.store, makeTarget())).toEqual([])
})
it('does NOT re-adopt across two different aliases that share host+user+port', () => {
// prod-deploy removed; prod-admin re-added — same box, different alias.
// These are deliberately distinct targets and must not steal workspaces.
const fake = makeFakeStore([
tombstone({ configHost: 'prod-deploy', host: 'prod.example.com', username: 'deploy' })
])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ configHost: 'prod-admin', host: 'prod.example.com', username: 'deploy' })
)
expect(readoptions).toEqual([])
expect(fake.reassigned).toEqual([])
expect(fake.remaining()).toHaveLength(1) // tombstone preserved
})
it('does NOT re-adopt a different account/port on the same host (implicit alias)', () => {
// Manual adds default configHost to host, so both carry configHost === host.
// That is NOT a real alias — matching on it alone would ignore port/username
// and reattach workspaces to the wrong SSH account on the same machine.
const fake = makeFakeStore([
tombstone({
configHost: 'dev.example.com',
host: 'dev.example.com',
port: 22,
username: 'alice'
})
])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({
configHost: 'dev.example.com',
host: 'dev.example.com',
port: 2222,
username: 'bob'
})
)
expect(readoptions).toEqual([])
expect(fake.reassigned).toEqual([])
})
it('re-adopts the same account/host/port even with implicit configHost === host', () => {
const fake = makeFakeStore([
tombstone({
configHost: 'dev.example.com',
host: 'dev.example.com',
port: 22,
username: 'alice'
})
])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({
configHost: 'dev.example.com',
host: 'dev.example.com',
port: 22,
username: 'alice'
})
)
expect(readoptions).toHaveLength(1)
})
it('still re-adopts via tuple when the re-added target has no alias', () => {
// Manual re-add (no configHost) of a host that was config-managed: fall back
// to the tuple since one side lacks an alias to distinguish it.
const fake = makeFakeStore([
tombstone({ configHost: 'devbox', host: 'dev.example.com', username: 'tim' })
])
const readoptions = readoptOrphanedWorkspacesForTarget(
fake.store,
makeTarget({ configHost: undefined, host: 'dev.example.com', username: 'tim' })
)
expect(readoptions).toHaveLength(1)
})
})
describe('buildRemovedSshTargetTombstone', () => {
it('captures identity fields and omits configHost when absent', () => {
const t = buildRemovedSshTargetTombstone(makeTarget(), 123)
expect(t).toEqual({
oldTargetId: 'ssh-new',
host: 'dev.example.com',
port: 22,
username: 'tim',
label: 'Dev box',
removedAt: 123
})
expect('configHost' in t).toBe(false)
})
it('includes configHost when present', () => {
const t = buildRemovedSshTargetTombstone(makeTarget({ configHost: 'devbox' }), 1)
expect(t.configHost).toBe('devbox')
})
})