Files
orca/src/main/runtime/worktree-list-host-scope.test.ts
T
Neil 95eed52801 fix(cli): report which hosts a worktree listing covered, and stop the cap starving remote ones (#18417)
`orca worktree list` returned zero of 24 SSH worktrees at the default limit
(#18104). Rows are resolved repo by repo, so every SSH repo's rows land
contiguously at the end of the fleet order — the 24 remote rows sat at indices
496-520 of 521 and a plain `slice(0, 200)` never reached them.

The omission was not fully silent: text output printed `truncated: showing 200
of 521` and JSON carried `totalCount` / `truncated`. What was missing is that
the omission was *categorically every remote host* — no host column, no
`hostScope`, nothing to distinguish "200 of 521" from "one host is entirely
absent". Per docs/reference/ssh-execution-boundary.md, a listing that does not
name its scope reads as absolute.

Adopt the mechanism `terminal list` already has rather than inventing a second
one:

- `RuntimeTerminalListHostScope` becomes an alias of a shared
  `RuntimeListingHostScope`, now also carried (optional, so old hosts are
  unaffected) on `worktree.list` and `worktree.ps` results.
- `src/shared/host-balanced-listing-page.ts` round-robins the row cap across
  hosts and returns the survivors in the caller's original relative order, so
  the page stays a subsequence of the unbounded listing and nothing downstream
  re-sorts. An uncapped listing is returned unchanged.
- `worktree list` / `worktree ps` text output gains a `host=` column and the
  same trailing `scope:` line `terminal list` prints.

Third defect, same mechanism: `hostScope.omittedHostIds` is built from the
runtime's own bookkeeping, so it names `runtime:` ids for servers that are no
longer paired — 6 of 9 in the recorded QA run hard-error when queried. Since
`hostScope` is *the* documented way to complete a partial listing, that makes
the mechanism unreliable for its intended use.

Annotate rather than filter. Dropping an id would shrink what the listing
admits it did not cover, and the boundary doc requires a listing to name its
gaps — the gap is real whether or not this machine can name the host that owns
it. `src/cli/omitted-host-scope-selectors.ts` resolves each omitted id against
this machine's pairing store and the runtime's SSH-target registry and attaches
the exact flag that reaches it, or `null` marked "not selectable from this
machine". This is a client-side annotation: nothing new goes over the wire, it
answers "can I select it" and never "is it up", and the SSH round trip is only
paid when an `ssh:` host was actually omitted.

No `--host` filter was added; the host column plus scope line covers the
reported need without a new selector axis.
2026-09-03 14:43:09 -07:00

171 lines
5.9 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { ExecutionHostId } from '../../shared/execution-host'
import type { Repo } from '../../shared/repo-types'
import { selectHostBalancedPage } from '../../shared/host-balanced-listing-page'
import { RuntimeManagedWorktreeQueries } from './runtime-managed-worktree-queries'
import type { ResolvedWorktree } from './runtime-worktree-path-identity'
import type { RuntimeStore } from './runtime-store-contract'
const LOCAL_REPO: Repo = {
id: 'repo-local',
path: '/workspace/app',
displayName: 'app',
badgeColor: '#000000',
addedAt: 1
}
const SSH_REPO: Repo = {
...LOCAL_REPO,
id: 'repo-ssh',
connectionId: 'box-1',
displayName: 'app (remote)'
}
const settings = {
workspaceDir: '/worktrees',
nestWorkspaces: true,
refreshLocalBaseRefOnWorktreeCreate: false,
branchPrefix: 'none',
branchPrefixCustom: ''
}
function worktree(repoId: string, path: string, hostId: string): ResolvedWorktree {
return {
id: `${repoId}::${path}`,
repoId,
path,
branch: 'main',
hostId,
displayName: path,
comment: '',
linkedIssue: null,
parentWorktreeId: null,
childWorktreeIds: [],
lineage: null,
git: { path, head: 'abc', branch: 'main', isBare: false, isMainWorktree: false }
} as unknown as ResolvedWorktree
}
/** The reproduced shape from #18104: every remote row lands contiguously at the end. */
function fleet(localCount: number, sshCount: number): ResolvedWorktree[] {
return [
...Array.from({ length: localCount }, (_, index) =>
worktree(LOCAL_REPO.id, `/worktrees/local-${index}`, 'local')
),
...Array.from({ length: sshCount }, (_, index) =>
worktree(SSH_REPO.id, `/remote/wt-${index}`, 'ssh:box-1')
)
]
}
function queries(
resolved: ResolvedWorktree[],
knownHostIds: ExecutionHostId[] = ['local', 'ssh:box-1']
): RuntimeManagedWorktreeQueries {
const store = {
getRepos: () => [LOCAL_REPO, SSH_REPO],
getRepo: () => LOCAL_REPO,
getAllWorktreeMeta: () => ({}),
getWorktreeMeta: () => undefined,
setWorktreeMeta: vi.fn(),
getAllWorktreeLineage: () => ({}),
getSettings: () => settings
} as unknown as RuntimeStore
return new RuntimeManagedWorktreeQueries({
getStore: () => store,
listResolved: async () => resolved,
resolveRepo: async () => SSH_REPO,
selectRepos: () => [SSH_REPO],
scanRepo: async () => ({ ok: true, worktrees: [] }),
listKnownHostIds: () => knownHostIds
})
}
describe('worktree.list host coverage under the row cap', () => {
it('returns remote rows that sit entirely past the cap', async () => {
// Why #18104: 497 local + 24 SSH rows, SSH at indices 496-520, and a 200-row cap returned
// `{local: 200}` — zero of 24 remote worktrees, with nothing saying the gap was a whole host.
const result = await queries(fleet(497, 24)).list(undefined, 200)
expect(result.totalCount).toBe(521)
expect(result.truncated).toBe(true)
expect(result.worktrees).toHaveLength(200)
const remote = result.worktrees.filter((row) => row.hostId === 'ssh:box-1')
expect(remote).toHaveLength(24)
expect(result.hostScope).toEqual({ hostIds: ['local', 'ssh:box-1'], omittedHostIds: [] })
})
it('keeps the page a subsequence of the unbounded listing', async () => {
// Why: balancing decides which rows survive the cap, never how the survivors are ordered.
const resolved = fleet(497, 24)
const result = await queries(resolved).list(undefined, 200)
const positions = result.worktrees.map((row) => resolved.findIndex((it) => it.id === row.id))
expect(positions).toEqual([...positions].sort((left, right) => left - right))
})
it('names a configured host that contributed no rows at all', async () => {
// Why: a repo whose scan failed contributes zero rows exactly like a host with no worktrees.
// docs/reference/ssh-execution-boundary.md forbids the listing from reading as absolute there.
const result = await queries(fleet(3, 0), ['local', 'ssh:box-1', 'runtime:paired']).list(
undefined,
200
)
expect(result.hostScope).toEqual({
hostIds: ['local'],
omittedHostIds: ['runtime:paired', 'ssh:box-1']
})
})
it('does not report configured hosts as omitted from a --repo listing', async () => {
// Why: the caller scoped this themselves, so naming the hosts they excluded is noise.
const result = await queries(fleet(0, 5)).list('id:repo-ssh', 200)
expect(result.hostScope).toEqual({ hostIds: ['ssh:box-1'], omittedHostIds: [] })
})
it('leaves an uncapped listing byte-identical', async () => {
const resolved = fleet(4, 2)
const result = await queries(resolved).list(undefined, 200)
expect(result.worktrees.map((row) => row.id)).toEqual(resolved.map((row) => row.id))
expect(result.truncated).toBe(false)
})
})
describe('selectHostBalancedPage', () => {
it('gives every host a share of the cap rather than filling it from the first', () => {
const rows = [
...Array.from({ length: 10 }, (_, index) => ({ host: 'local', index })),
...Array.from({ length: 10 }, (_, index) => ({ host: 'ssh:box-1', index: index + 10 }))
]
const page = selectHostBalancedPage(rows, 4, (row) => row.host)
expect(page.map((row) => row.host)).toEqual(['local', 'local', 'ssh:box-1', 'ssh:box-1'])
})
it('fills the cap from the remaining hosts when one runs out of rows', () => {
const rows = [
{ host: 'local', id: 'a' },
{ host: 'local', id: 'b' },
{ host: 'local', id: 'c' },
{ host: 'ssh:box-1', id: 'd' }
]
const page = selectHostBalancedPage(rows, 3, (row) => row.host)
expect(page.map((row) => row.id)).toEqual(['a', 'b', 'd'])
})
it('buckets rows with no host together instead of dropping them', () => {
const rows = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]
expect(selectHostBalancedPage(rows, 2, () => undefined).map((row) => row.id)).toEqual([
'a',
'b'
])
})
})